Get a global function by name
-
I have a whole bunch of creep behaviours stored in modules, and use them like this :
var harvester = require ("harvester"); var builder = require ("builder"); var guard = require ("guard"); var upgrader = require ("upgrader"); for (var name in Game.creeps) { var creep = Game.creeps[name] if (creep.memory.role == "harvester") { harvester (creep) } else if (creep.memory.role == "builder") { builder (creep) } else if (creep.memory.role == "guard") { guard (creep) } else if (creep.memory.role == "upgrader") { upgrader (creep) } }
But adding a new behaviour would require adding another four lines of code every time, so I'd like to have something like this:
var harvester = require ("harvester"); var builder = require ("builder"); var guard = require ("guard"); var upgrader = require ("upgrader"); for (var name in Game.creeps) { var creep = Game.creeps[name] getByName (creep.memory.role) (creep) }
How can I do that?
Thanks
-
It seems like I could do something like the following, but using the require statement for every creep doesn't seem to be a good thing to do:
javascript
for (var name in Game.creeps)
{
var creep = Game.creeps[name]
require (creep.memory.role) (creep)
}
-
I do this by not using separate files for each role. and using an organized table
module.exports = { "harvester": { body: [WORK,CARRY,CARRY,MOVE,MOVE], actn: function(creep) { ... }, }, "tower": { body: [RANGED_ATTACK,RANGED_ATTACK], actn: function(creep) { ... }, }, "digger": { body: [MOVE,MOVE,WORK,WORK], actn: function(creep) { ... }, }, "gopher": { body: [MOVE,CARRY,CARRY,CARRY,CARRY,MOVE], actn: function(creep) { ... }, }, };
then a main function looking like:
var roles = require('roles'); for(var name in Game.creeps) { var creep = Game.creeps[name]; var role = creep.memory.role; var body = [WORK,CARRY,MOVE]; var actn = function(doomed){doomed.suicide()}; if(role && roles[role]) { body = roles[role].body; actn = roles[role].actn; } else { if( !creep.memory.badRole ) { console.log("Invalid role found: " + role + " Creep: " + name); creep.memory.badRole = true; } } if(!creep.memory.oldCreep) { console.log(name + ": " + role); if(actn) { console.log(" actn: handler found"); } creep.memory.oldCreep = true; } actn(creep); }
-
I have it organized in Classes (creep roles). These classes export at least one body declaration and a task. I organize their import in a separate CreepClass Declaration:
module.exports = { Fighter : require("Creep_Fighter"), Hauler : require("Creep_Hauler"), Healer : require("Creep_Healer"), KeeperKiller : require("Creep_KeeperKiller"), Miner : require("Creep_Miner"), Worker : require("Creep_Worker"), };
Which then gets invoked like following:
var Classes= require("CreepClasses"); var Class = creep.memory.Class; var task = creep.memory.task; if (Classes[Class]){ if (Classes[Class].tasks[task]){ Classes[Class].tasks[task].call(creep); //...
This way, I only need to add a new Class file and add the import to my
CreepClasses
module.
-
Thanks a lot. That was very helpful!
-
Not sure how efficient the method is, but I create an object that contains my required modules.
I start with an var f = {};
and then each module is saved as a f.harvester = require('harvester');I can later just call that using the string in the memory of a creep
Memory.creeps.Harvester1.role = 'harvester';
f[Memory.creeps.Harvester1.role]; // this calls the script just fine using a string.