Picking up dropped energy
-
Re: [QUESTION] Math equation for distance & energy on the ground
At the current moment, I have a code that looks something like this to pick up dropped energy on the ground:
if(creep.store.getFreeCapacity() > 0) { var sources = creep.room.find(FIND_TOMBSTONES && FIND_DROPPED_RESOURCES, RESOURCE_ENERGY); if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); } if(creep.withdraw(sources[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(sources[0], {visualizePathStyle: {stroke: "#ffaa00"}}); } } else { var targets = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_TOWER) && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; } }); if(targets.length > 0) { if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); } } }
I deliberately didn't put in the unnecessary code. This is put into a creep, who I have named "Collector", and I have fully linked it as it collects from tombstones but not dropped energy.
-
Sorry, by unnecessary, I mean the parts that aren't making it do stuff and are linking the creep to my main module etc.
-
As far as I am aware, you can not aggregate find calls in this manner, the parameter for the find accepts ONE OF the find constants, not multiple.
From the API:
One of the FIND_* constants.
You can look at the engine code for it here as well (for private servers but likely how it operations on the MMO as well) https://github.com/screeps/engine/blob/master/src/game/rooms.js#L584
If you want to link the results of multiple find calls into one aggerate array you can combine the two arrays or spread the find calls out over one array, then find as you see fit.
For example you can use the spread operator with two calls to have one array
let sources = [ ...creep.room.find(FIND_TOMBSTONES), ...creep.room.find(FIND_DROPPED_RESOURCES)]
Which should result in one array, with objects from both find calls. You can read about the spread syntax here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
To be clear as well, for dropped resources you use
pickup
to gather the energy so even if it was finding dropped resources, harvest/withdraw would not apply (see API: https://docs.screeps.com/api/#Creep.pickup)Hope that helps, elsewise would highly suggest that you check out the Screep's discord. Its a little more dynamic than the forums for finding solutions / requesting code assistance.
-
Thanks for the help, I was actually just experimenting random things (I didn't know pickup existed) and I typed in creep.pickup (fancy that) before I had even gotten a response to my question I then figured it out from there!