Claiming a new room, please help
-
Creeps are created from "flagManager" and they already know which is their flag when they born. However, assignNewFlag is used in case a creep can't find his flag because i removed it.
Here is "assignNewFlag"
module.exports = { run: function(creep, flagColor,flagSecColor){ //Assign the creep to a flag which "needs" it for(let i in Game.flags){ let thatFlag = Game.flags[i]; if(thatFlag.color == flagColor && thatFlag.secondaryColor == flagSecColor && Game.flags[i].memory.desiredMinions > Game.flags[i].memory.currentMinions){ creep.memory.targetFlag = thatFlag.name; } } //If there aren't, just assign to a flag of the right color if(!(Game.flags[creep.memory.targetFlag] && Game.flags[creep.memory.targetFlag].color == flagColor && Game.flags[creep.memory.targetFlag].secondaryColor == flagSecColor)){ for(let i in Game.flags){ let thatFlag = Game.flags[i]; if(thatFlag.color == flagColor && thatFlag.secondaryColor == flagSecColor){ creep.memory.targetFlag = thatFlag.name; } } } //If i can't find the right flag after all, send creep back to the spawn it was born if(!Game.flags[creep.memory.targetFlag]){ creep.memory.targetFlag = null; if(!creep.fatige){ creep.moveTo(Game.spawns[creep.memory.mySpawn], {reusePath: REUSE_PATH, visualizePathStyle: {stroke: '#ffffff'}}); } } } };
-
so i am guessing flagmanager is its own module?
-
@uncreativeusername Yes, each piece of code i posted has it's own module
-
so, i I just want one of my roles to involve flags, because my other roles are already working, how could I tell the one role to be assigned to a flag? like, without using assign new flag, and encorperating it into the same module?
-
@uncreativeusername The way i create the creeps involving flags is by using the name of the flag. It must be an integer (could be 1, 01, 001....2....), and it means the number of creeps you want for that flag.
You can take my "flagManager" code and reduce the switch statement so it works only with 1 color, and if you need in the future it'll be pretty easy to add more.
Then, from the main module...
for(let i in Game.flags){ let thisFlag = Game.flags[i]; FlagManager.run(thisFlag); }
You don't really need to use "assignNewFlag" as long as you make sure the flag exists as long as creep does.
-
or, could I do somthing like this in the module:
var roleclaimer = {
run: function(creep) {
let flag = Game.flags[creep.memory.targetFlag]; let ctrl = creep.room.controller; if(flag){ if(creep.room == flag.room){ let claimResult = creep.claimController(ctrl); if(claimResult == ERR_NOT_IN_RANGE){ creep.moveTo(ctrl, {visualizePathStyle: {stroke: '#cc00cc'}}); } } else{ creep.moveTo(flag, {visualizePathStyle: {stroke: '#cc00cc'}}); } }
} }; module.exports = roleclaimer;
and then, since i will only use each creep once, spawn the creep like this :
Game.spawns['S1'].spawnCreep( [CLAIM, MOVE], 'Claimer1', { memory: { role: 'claimer' } {targetFlag: 'flag1'} );
-
@uncreativeusername Yes, despite possible errors on the code i think that should do it. I also started with a simpler code, but it became more and more complex as i detected exceptions, errors and undesired behaviours.
I recommend you to do it with a new role whose body is just [MOVE] and see what happens, so you don't bankrupt while you solve it xDD
-
@Calfa thank you this is a huge help.
-
@uncreativeusername A big pleasure to help. Even more considering the amount of knowledge and help i got from this forum.
-
confirmed, role.claimer is functional!