Need some help with "hauler" logic
-
This is the code for my "hauler", or "refiller" as i call it
const PickEnergy = require('job.pickEnergy'); const UnloadEnergy = require('job.unloadEnergy'); const LoadEnergy = require('job.loadEnergy'); let roleRefiller = { run: function(creep){ if(creep.carry.energy == 0 && creep.memory.task != 'loading'){ creep.memory.task = 'picking'; }else if(creep.carry.energy == creep.carryCapacity){ creep.memory.task = 'unloading'; } if(creep.room.memory.enemies.length > 0 && creep.memory.task == 'unloading'){ unloadResult = UnloadEnergy.run(creep, ['towers'], 0); }else{ switch(creep.memory.task){ case 'loading': LoadEnergy.run(creep, ['containers', 'storages'], 0, Infinity); break; case 'unloading': let unloadResult = UnloadEnergy.run(creep, ['extensions', 'spawns'], 0); if(unloadResult != OK && unloadResult != ERR_NOT_IN_RANGE){ unloadResult = UnloadEnergy.run(creep, ['towers'], 0); if(unloadResult != OK && unloadResult != ERR_NOT_IN_RANGE){ unloadResult = UnloadEnergy.run(creep, ['containers', 'storages'], 0); if(unloadResult == 'noTarget'){ creep.memory.task = 'picking'; } } } break; case 'picking': let pickEnergyResult = PickEnergy.run(creep, ['full'], Infinity); if(pickEnergyResult == 'noTarget'){ creep.memory.task = 'loading'; } break; } } } } module.exports = roleRefiller;
Once it's full, it's task is "unloading", and it will try to unload at extensions and spawns. If it can't, will try to unload at towers and again, if it can't, will try to unload at containers. So far it's all ok.
Once it's finished unloading at extensions, spawns and towers, i want it to pick dropped energy from the ground and unload that energy at containers, but whenever there's no energy dropped on the ground it begins to load energy from a container and unload it at the same container in the next tick in an annoying loop.
How can i avoid this behaviour?
Thanks a lot in advance!!
-
something I did for a long time was have a creep to collect energy and a seperate creep to fill extensions. In theory you should be collecting energy all the time and in a busy room spawning should be happening regularly and have a large amount of extensions that need filling so its generally better to divide the jobs into different things
-
You almost do it as @ObamaLlama suggested with the difference that you squeeze both roles into one. This is where your problem comes from: The filler part of your role thinks: "Oh, I don't have energy, there's nothing on the ground, then let's take something from a container!" Then the hauler part thinks: "Oh, great, I have energy, there's nothing to be filled, so let's put it into a container for future use!"
If you really want to keep this role you can check whether you have to fill something before taking energy out of a container.
Another thing I noticed when reading your code was that when there are hostile creeps in your room your haulers don't fill your extensions and spawns anymore. You can't spawn anything anymore then which makes your room an easy target for enemies.
-
Already did it!
Made a new role called Scavenger that only collects energy from the ground and unloads it at containers and storages. My old role "refiller" only takes energy from storages and containers and unloads it at extensions and spawns. Also, got rid of any reference about collecting dropped resources on all the rest of the roles. There's now one more creep in the house but the overall logic for all the roles looks simplier now.
Thanks a lot!!
P.S. Now facing the situation where there are enemies in my rooms. What i plan to do is to have them all going to the nearest tower, so they can attract enemies while being healed, excepting drop miners, because they have a rampart over them.
-
@calfa A common implementation assigns haulers to sources and lets them be only as big as they have to be to get all the energy to the storage. This saves carry parts. Also if one creep dies then the whole logistics network doesn't crash until a new creep has been spawned which can take pretty long because it has to be pretty big.
Concerning your creeps attracting enemies to towers: This isn't optimal because your colony will freeze then letting enemies deal damage as they want while you don't have any defence. Also it isn't guaranteed that an attacker will really go for creeps instead of important structures such as spawns, storages, towers, terminals, ...
-
@duckymirror What i do is create the biggest possible body part for every creep depending on the available energy at the moment to spawn the creep. Will think about your way to implement it, tho.
About towers and creeps...i know it's terrible, but it was just a way to protect myself against invaders, since i've never been involved in a pvp skirmish. The reason i do it this way is because once the invader has finished killing all my creeps, in case i can't defend, it will despawn. However, if there's a tower defending the house, my room will only lose a few ticks due to the freezing.
I know it's still a terrible approach for pvp and even for pve, so i'll try to write a different behaviour. So far i thought about making all minions out of the tower scout range to enter in Panic Mode and then go to the tower, but this is still a PvE approach.
Thank you!!