Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. module.exports =
  2. {
  3. initSpawnQue: function()
  4. {
  5. if(Memory.spawnQue == undefined)
  6. Memory.spawnQue = [ ];
  7. },
  8.  
  9. addToQue: function(creep, unshift)
  10. {
  11. this.initSpawnQue();
  12.  
  13. if(unshift != undefined && unshift === true)
  14. Memory.spawnQue.unshift(creep);
  15. else
  16. Memory.spawnQue.push(creep);
  17. },
  18.  
  19. spawnNextInQue: function()
  20. {
  21. this.initSpawnQue();
  22.  
  23. if(!Memory.spawnQue.length)
  24. return;
  25.  
  26. var spawns = Game.getRoom('1-1').find(Game.MY_SPAWNS, {
  27. filter: function(spawn)
  28. {
  29. return spawn.spawning === undefined || spawn.spawning === null;
  30. }
  31. });
  32.  
  33. if(!spawns.length)
  34. return;
  35.  
  36. var role = Memory.spawnQue[0];
  37.  
  38. if(typeof role == "string")
  39. {
  40. role = { type: role, memory: { } };
  41. }
  42.  
  43. var me = this;
  44. var toSpawnAt = spawns.filter(function(spawn)
  45. {
  46. return me.canSpawn(spawn, role.type);
  47. });
  48.  
  49. if(! toSpawnAt.length)
  50. return;
  51.  
  52. toSpawnAt = toSpawnAt[0];
  53.  
  54. this.spawn(role.type, role.memory, toSpawnAt);
  55.  
  56. Memory.spawnQue.shift();
  57. },
  58.  
  59. spawn: function(role, memory, spawnPoint)
  60. {
  61. if(!spawnPoint)
  62. spawnPoint = Game.spawns.Spawn1;
  63.  
  64. var manager = require('roleManager');
  65.  
  66. if(!manager.roleExists(role))
  67. {
  68. return;
  69. }
  70.  
  71. if(!this.canSpawn(spawnPoint, role))
  72. {
  73. return;
  74. }
  75.  
  76. if(memory == undefined)
  77. memory = { };
  78.  
  79. memory['role'] = role;
  80.  
  81. var nameCount = 0;
  82. var name = null;
  83. while(name == null)
  84. {
  85. nameCount++;
  86. var tryName = role + nameCount;
  87. if(Game.creeps[tryName] == undefined)
  88. name = tryName;
  89. }
  90.  
  91. console.log('Spawning ' + role);
  92. spawnPoint.createCreep(manager.getRoleBodyParts(role), name, memory);
  93. },
  94.  
  95. canSpawn: function(spawnPoint, role)
  96. {
  97. if(typeof spawnPoint == "string" && role == undefined)
  98. {
  99. role = spawnPoint;
  100. spawnPoint = Game.spawns.Spawn1;
  101. }
  102.  
  103. return spawnPoint.energy >= this.spawnCost(role)
  104. && (spawnPoint.spawning == null
  105. || spawnPoint.spawning == undefined);
  106. },
  107.  
  108. spawnCost: function(role)
  109. {
  110. var manager = require('roleManager');
  111. var parts = manager.getRoleBodyParts(role);
  112.  
  113. var total = 0;
  114. for(var index in parts)
  115. {
  116. var part = parts[index];
  117. switch(part)
  118. {
  119. case Game.MOVE:
  120. total += 50
  121. break;
  122.  
  123. case Game.WORK:
  124. total += 20
  125. break;
  126.  
  127. case Game.CARRY:
  128. total += 50
  129. break;
  130.  
  131. case Game.ATTACK:
  132. total += 100
  133. break;
  134.  
  135. case Game.RANGED_ATTACK:
  136. total += 150
  137. break;
  138.  
  139. case Game.HEAL:
  140. total += 200
  141. break;
  142.  
  143. case Game.TOUGH:
  144. total += 5
  145. break;
  146. }
  147. }
  148.  
  149. return total;
  150. },
  151.  
  152. killAll: function(role)
  153. {
  154. for(var i in Game.creeps) {
  155. if(role == undefined || Game.creeps[i].memory.role == role)
  156. Game.creeps[i].suicide();
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement