04-09-2015, 10:14 AM
Hello.
Problem of finding and recharging power cells can be divided in two parts. First one is to find all power cells that need to be recharged. Second, recharge all found power cells.
First part is to make a list for power cells. We can use empty list of objects to store them:
To find all power cells we need to use retobject() function. We need to iterate over all objects in the world, see if given object is power cell, whether it is carried by something else and, finally, if it needs to be recharged. Then we add it to the list.
Then we actually recharge all power cells. We iterate over our list, find each power cell and recharge it:
If you put together all the above code, you get complete solution to your problem. I hope that helped.
Problem of finding and recharging power cells can be divided in two parts. First one is to find all power cells that need to be recharged. Second, recharge all found power cells.
First part is to make a list for power cells. We can use empty list of objects to store them:
Code:
// list of power cells to recharge
object[] list;
To find all power cells we need to use retobject() function. We need to iterate over all objects in the world, see if given object is power cell, whether it is carried by something else and, finally, if it needs to be recharged. Then we add it to the list.
Code:
// find all power cells to recharge
for(int i=0, j=0; ; i++)
{
// next object
object item = retobject(i);
// no more items
if(item == null) break;
// item is not power cell
if(item.category != PowerCell) continue;
// power cell was put in some other object
if(item.position.x == nan) continue;
// power cell has less than 100% energy
if(item.energyLevel < 1.0)
{
// add to list
list[j++] = item;
}
}
Then we actually recharge all power cells. We iterate over our list, find each power cell and recharge it:
Code:
// recharge all power cells
for(int i=0; i<sizeof(list); i++)
{
// take next power cell
object item = list[i];
goto(item.position);
grab();
// find and go to power station
item = radar(PowerStation);
goto(item.position);
// recharge power cell
while(this.load.energyLevel < 1.0)
{
wait(0.1);
}
// find free space and drop power cell
goto(space());
drop();
}
If you put together all the above code, you get complete solution to your problem. I hope that helped.
"After three days without programming, life becomes meaningless."
~The Tao of Programming
~The Tao of Programming