Advertisement
purplemonday

Untitled

Nov 14th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.25 KB | None | 0 0
  1.  
  2. exports.run = function (group ,roomname,config)
  3. {
  4. var spawing_busy = false
  5. function flee (creep)
  6. {
  7. creep.moveTo (common.functions.roomspawns(creep.memory.ROOM )[0])
  8.  
  9.  
  10. }
  11. var opt = {reusePath : 10}
  12.  
  13. var containers_dir = []
  14. var arival_dir ={}
  15. if (config[group].routes != undefined)
  16. {
  17. var routes = config[group].routes
  18.  
  19. //console.log("routes "+routes)
  20. for (var t in routes)
  21. {
  22. var route_container
  23. var pos = routes[t].position
  24. var enable = routes[t].enable
  25. // do we have creeps running this?
  26.  
  27. var my_group = _.filter(Game.creeps, (creep) => creep.memory.GROUP == group && creep.memory.ROOM == roomname && creep.memory.TASK == "harvester" && creep.memory.DEST == pos)
  28. // console.log(my_group.length+" "+pos+" "+enable)
  29. var my_group_expire = _.filter(my_group, (creep) => creep.memory.arival+30 < creep.ticksToLive || creep.memory.arival == undefined)
  30. //for (var ex in my_group_expire)
  31. //{
  32. // console.log("<h1>"+my_group_expire[ex]+" "+my_group_expire[ex].memory.arival +" "+my_group_expire[ex].ticksToLive+"</h1>")
  33. //}
  34. var my_room = Game.rooms[roomname]
  35. if (my_group_expire .length < 1 && enable > 0 && common.functions.cuttoff(config,roomname) > 0)
  36. {
  37.  
  38. spawing_busy = true
  39. // spawn a unit
  40. // common.functions.roomspawns(roomname)[0].createCreep([WORK,MOVE,CARRY,CARRY],{GROUP:group, ROOM : roomname, TASK : "harvester", DEST : pos})
  41.  
  42. if (my_room.energyCapacityAvailable >700)
  43. {
  44. console.log("700 "+common.functions.roomspawns(roomname)[0].createCreep([WORK,WORK,WORK,WORK,WORK,MOVE,MOVE,MOVE,CARRY],{GROUP:group, ROOM : roomname, TASK : "harvester", DEST : pos}))
  45. } else if (my_room.energyCapacityAvailable > 600){
  46. console.log("600 "+common.functions.roomspawns(roomname)[0].createCreep([WORK,WORK,WORK,WORK,WORK,MOVE,CARRY],{GROUP:group, ROOM : roomname, TASK : "harvester", DEST : pos}))
  47. } else if (my_room.energyCapacityAvailable > 500){
  48. var newcreep = common.functions.roomspawns(roomname)[0].createCreep([WORK,WORK,WORK,WORK,MOVE,CARRY],{GROUP:group, ROOM : roomname, TASK : "harvester", DEST : pos})
  49. console.log(roomname+" 500 "+newcreep+" "+common.functions.roomspawns(roomname)[0])
  50. } else if (my_room.energyCapacityAvailable > 400){
  51.  
  52. var newcreep = common.functions.roomspawns(roomname)[0].createCreep([WORK,WORK,WORK,MOVE,CARRY],{GROUP:group, ROOM : roomname, TASK : "harvester", DEST : pos})
  53. console.log(roomname+" 400 "+newcreep)
  54. } else if (my_room.energyCapacityAvailable > 300){
  55. common.functions.roomspawns(roomname)[0].createCreep([WORK,WORK,MOVE,CARRY],{GROUP:group, ROOM : roomname, TASK : "harvester", DEST : pos})
  56. } else {
  57.  
  58. }
  59. console.log("spawning harvester for route "+routes[t])
  60. }
  61.  
  62. if (my_group.length > 0)
  63. {
  64. for (var o in my_group)
  65. {
  66. var this_creep = my_group[o]
  67. if (external_hostiles[this_creep.pos.roomName] != undefined || this_creep.hits < this_creep.hitsMax)
  68. {
  69. console.log("HOSTILE!!!!!! FLEEiNG")
  70. flee (this_creep)
  71.  
  72. } else {
  73.  
  74. var s = this_creep.memory.DEST.split(" ")
  75. var p = new RoomPosition(s[0],s[1],s[2]);
  76. //console.log(this_creep.memory.POS+" "+p)
  77.  
  78. if (this_creep.pos.getRangeTo(p) > 1)
  79. {
  80. this_creep.say("1")
  81. this_creep.moveTo(p)
  82. } else {
  83. delete this_creep.memory.path
  84. if (this_creep.memory.arival == undefined)
  85. {
  86. this_creep.memory.arival = 1500 - this_creep.ticksToLive
  87. }
  88. var source = this_creep.room.find(FIND_SOURCES)
  89. source = this_creep.pos.findClosestByRange(source)
  90. var container = this_creep.room.find(FIND_STRUCTURES, {filter: (structure) => {return (structure.structureType == STRUCTURE_CONTAINER)}})
  91. container = source.pos.findInRange(container, 2);
  92.  
  93. if (container.length < 1)
  94. {
  95. container = this_creep.pos.findClosestByRange(container)
  96. this_creep.say("3")
  97. var build_containers =source.pos.findInRange(FIND_CONSTRUCTION_SITES, 1);
  98. if (build_containers.length < 1)
  99. {
  100. this_creep.say("3a")
  101. this_creep.pos.createConstructionSite(STRUCTURE_CONTAINER)
  102. } else {
  103. this_creep.say("3b")
  104. this_creep.build(build_containers[0])
  105. this_creep.harvest(source)
  106. }
  107. } else {
  108. this_creep.say("2")
  109. container = this_creep.pos.findClosestByRange(container)
  110. var energy = container.store[RESOURCE_ENERGY]
  111. if (this_creep.pos.getRangeTo(container.pos) > 0)
  112. {
  113. this_creep.moveTo(container)
  114. }
  115. if (energy < 2000 && this_creep.pos.getRangeTo(container.pos) < 1)
  116. {
  117. this_creep.harvest(source)
  118. }
  119. // there is a container now
  120. if (container.hits < 200000)
  121. {
  122. if (this_creep.carry.energy < 25)
  123. { this_creep.harvest(source)
  124. } else {
  125. this_creep.repair(container)
  126. }
  127. }
  128. this_creep.say(energy)
  129. //add object to the container directory
  130. if (container != undefined)
  131. {
  132. containers_dir.push(container)
  133. // arival_dir[container.id] = this_creep.memory.arival
  134. }
  135. if (this_creep.memory.CONTAINER == undefined)
  136. {
  137. this_creep.memory.CONTAINER = container.id
  138. }
  139. route_container = container
  140. }
  141. }}
  142. }
  143. }
  144. // this is the end of the route deal
  145. // console.log ("run the carriers for "+pos )
  146. var my_carrier_group = _.filter(Game.creeps, (creep) => creep.memory.GROUP == group && creep.memory.ROOM == roomname && creep.memory.TASK == "carrier" && creep.memory.ROUTE == pos)
  147.  
  148.  
  149. // find the config of this set.
  150.  
  151.  
  152. if ( route_container != undefined && my_carrier_group < routes[t].carriers && routes[t].enable > 0 && spawing_busy == false && route_container.store.energy > 1000)
  153. {
  154. var arg = {ROUTE :pos, GROUP:group, ROOM : roomname, TASK : "carrier"}
  155. if (my_room.energyCapacityAvailable >1750)
  156. {
  157. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  158. } else if (my_room.energyCapacityAvailable > 1450){
  159. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  160. } else if (my_room.energyCapacityAvailable > 1050){
  161. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  162. } else if (my_room.energyCapacityAvailable > 900){
  163. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  164. } else if (my_room.energyCapacityAvailable > 800){
  165. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  166. } else if (my_room.energyCapacityAvailable > 700){
  167. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  168. } else if (my_room.energyCapacityAvailable > 600){
  169. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  170. } else if (my_room.energyCapacityAvailable > 500){
  171. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,WORK],arg)
  172. } else if (my_room.energyCapacityAvailable > 400){
  173. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,WORK],arg)
  174. } else if (my_room.energyCapacityAvailable > 300){
  175. common.functions.roomspawns(roomname)[0].createCreep([CARRY,CARRY,MOVE,MOVE,WORK],arg)
  176. } else {
  177.  
  178. }
  179.  
  180. }
  181. if (my_carrier_group.length>0)
  182. {
  183. for (var tt in my_carrier_group)
  184. {
  185. var this_creep = my_carrier_group[tt]
  186. this_creep.say("1")
  187. if (external_hostiles[this_creep.pos.roomName] != undefined || this_creep.hits < this_creep.hitsMax)
  188. {
  189. console.log("HOSTILE!!!!!! FLEEiNG")
  190. flee (this_creep)
  191.  
  192. } else {
  193. //delete this_creep.memory.roundtrip
  194.  
  195. if (this_creep.memory.ACTION == undefined)
  196. {
  197. this_creep.memory.ACTION = "LOAD"
  198. }
  199. if (this_creep.memory.ACTION == "LOAD")
  200. {
  201.  
  202. if (route_container == undefined)
  203. {
  204. var s = this_creep.memory.ROUTE.split(" ")
  205. var p = new RoomPosition(s[0],s[1],s[2]);
  206. route_container = p
  207. if (this_creep.pos.getRangeTo(route_container) < 3)
  208. {
  209. route_container = p.findInRange(FIND_STRUCTURES,3);
  210. route_container = _.filter(route_container, (structure) => structure.structureType == STRUCTURE_CONTAINER)
  211. route_container = route_container[0]
  212. }
  213. }
  214. if (this_creep.pos.getRangeTo(route_container) > 1)
  215. {
  216. this_creep.say("1")
  217. this_creep.say(this_creep.moveTo(route_container))
  218. } else {
  219.  
  220. this_creep.repair(route_container)
  221. if (this_creep.pos.getRangeTo(route_container) > 1)
  222. {
  223. this_creep.say(this_creep.moveTo(route_container))
  224. } else {
  225. this_creep.withdraw(route_container,RESOURCE_ENERGY)
  226. }
  227.  
  228.  
  229. if (this_creep.carry.energy >this_creep.carryCapacity-2)
  230. {
  231. this_creep.memory.ACTION = "UNLOAD"
  232. }
  233. }
  234. }
  235. if (this_creep.memory.ACTION == "UNLOAD")
  236. {
  237. //console.log(config[group].dropoff)
  238. this_creep.say("u")
  239. var work_destination = Game.getObjectById(config[group].dropoff)
  240. if (this_creep.pos.getRangeTo(work_destination.pos) > 1)
  241. {
  242. this_creep.moveTo(work_destination,opt)
  243.  
  244. //var build_road = this_creep.room.find(FIND_CONSTRUCTION_SITES);
  245. var build_road = this_creep.pos.findInRange(FIND_CONSTRUCTION_SITES, 3);
  246.  
  247. if (build_road.length < 1)
  248. {
  249. // repair
  250. var roads = this_creep.pos.findInRange(FIND_STRUCTURES,3);
  251. var roads = _.filter(roads, (structure) => (structure.hits / structure.hitsMax) < 0.8)
  252. //roads = this_creep.pos.findInRange(roads, 3);
  253. //roads = this_creep.pos.findClosestByPath(roads)
  254. this_creep.say("R: "+this_creep.repair(roads[0]))
  255. this_creep.say("R: "+roads)
  256. //this_creep.say(roads)
  257. if (this_creep.carry.energy == 0)
  258. {
  259. this_creep.memory.ACTION = "LOAD"
  260. }
  261.  
  262. } else {
  263. // var constroad = this_creep.pos.findClosestByPath(build_road )
  264. this_creep.build(build_road[0])
  265.  
  266. }
  267.  
  268. } else {
  269. if (this_creep.memory.roundtrip == undefined)
  270. {
  271. this_creep.memory.roundtrip = 1500 - this_creep.ticksToLive
  272. }
  273.  
  274.  
  275.  
  276. this_creep.transfer(work_destination,RESOURCE_ENERGY)
  277. if (this_creep.carry.energy == 0)
  278. {
  279. if (this_creep.memory.roundtrip+10 > this_creep.ticksToLive)
  280. {
  281. this_creep.memory.ACTION = "recycle"
  282. } else {
  283. this_creep.memory.ACTION = "LOAD"
  284. }
  285. //console.log("<h2>age of creep "+this_creep.ticksToLive+" "+arival_dir[this_creep.memory.DEST]+" "+(arival_dir[this_creep.memory.DEST]*2)+" "+this_creep.memory.DEST+" "+"</h2>")
  286.  
  287. }
  288. }
  289.  
  290.  
  291. }
  292. if (this_creep.memory.ACTION == "recycle")
  293. {
  294.  
  295. var spawn = common.functions.roomspawns(roomname)[0]
  296. if (this_creep.pos.getRangeTo(spawn.pos) > 1)
  297. {
  298. this_creep.moveTo(spawn)
  299. this_creep.say("recycle")
  300. } else {
  301. spawn.recycleCreep(this_creep)
  302. }
  303. }}
  304. }
  305. }
  306. //console.log(cc+" "+containers_dir[cc].id+" "+containers_dir[cc].store.energy)
  307. //if (containers_dir[cc].store.energy > 500)
  308. //{
  309.  
  310. //}
  311.  
  312.  
  313.  
  314.  
  315.  
  316. }
  317. }
  318.  
  319.  
  320.  
  321.  
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement