Ryan_F_Bracy

Screeps newCreep function - First Attempt

Sep 22nd, 2020
2,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     My idea is for a polymorphic function to create new creeps,
  3.     without an excess of repetitive code. To begin, it will take
  4.     the single parameter which determines the role. In the future
  5.     further parameters might determine body size/type.
  6.  
  7. */
  8.  
  9. // Store the body types here
  10. var harvester_body = '[MOVE, MOVE, MOVE, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE]';
  11. var upgrader_body = '[MOVE, CARRY, MOVE, WORK, WORK, MOVE, CARRY, MOVE]';
  12. var builder_body = '[MOVE, MOVE, WORK, CARRY, CARRY, WORK, MOVE, MOVE]';
  13. var repairer_body = '[MOVE, CARRY, MOVE, WORK, WORK, MOVE, CARRY, MOVE]';
  14. var miner_body = '[MOVE, WORK, WORK, WORK, MOVE]';
  15.  
  16.  
  17. var newCreep = {
  18.     run: function (creepRole) {
  19.         // My thought here is to generate the name via the role
  20.         var newName = (creepRole + Game.time);
  21.         // And then using the role spawn the creep
  22.         switch (creepRole) {
  23.             case 'harvester':
  24.                 Game.spawns.Spawn1.spawnCreep(harvester_body, newName,
  25.                     { memory: { role: creepRole } });
  26.             case 'upgrader':
  27.                 Game.spawns.Spawn1.spawnCreep(upgrader_body, newName,
  28.                     { memory: { role: creepRole } });
  29.             case 'builder':
  30.                 Game.spawns.Spawn1.spawnCreep(builder_body, newName,
  31.                     { memory: { role: creepRole } });
  32.             case 'repairer':
  33.                 Game.spawns.Spawn1.spawnCreep(repairer_body, newName,
  34.                     { memory: { role: creepRole } });
  35.             case 'miner':
  36.                 Game.spawns.Spawn1.spawnCreep(miner_body, newName,
  37.                     { memory: { role: creepRole } });
  38.         }
  39.         // I would prefer to be able to call the body type using the role
  40.         // as well, as this would reduce the amount of code required even further.
  41.     }
  42. };
  43.  
  44. module.exports = newCreep;
  45.  
Add Comment
Please, Sign In to add comment