Advertisement
purplemonday

Untitled

Oct 31st, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. /*
  2. * Module code goes here. Use 'module.exports' to export things:
  3. * module.exports.thing = 'a thing';
  4. *
  5. * You can import it from another modules like this:
  6. * var mod = require('path');
  7. * mod.thing == 'a thing'; // true
  8. */
  9.  
  10.  
  11. exports.clean = function()
  12. {
  13. if (Memory.path_cache != undefined)
  14. {
  15. if (Memory.path_cache_clean_timer == undefined || Game.time - Memory.path_cache_clean_timer > 60 )
  16. {
  17.  
  18. Memory.path_cache_clean_timer = Game.time
  19. var text_out = "Path memory cleanup "
  20. var pathcount = 0
  21. for (var hash in this.path_cache)
  22. {
  23.  
  24.  
  25. var created = Game.time - this.path_cache[hash]["created"]
  26. var last_used = Game.time - this.path_cache[hash]["last_used"]
  27. text_out = text_out + pathcount+"] "+hash+"("+this.path_cache[hash]["path"].length+") "+created+" >"+last_used+"| "+ this.path_cache[hash]["times_used"]+"\n"
  28. pathcount= pathcount +1
  29.  
  30. if (created >1000 && last_used > 1000)
  31. {
  32. delete this.path_cache[hash]
  33. text_out = text_out +"Cleaning up record has expired\n"
  34. }
  35.  
  36.  
  37. }
  38. console.log(text_out)
  39.  
  40. }
  41.  
  42. }
  43. }
  44. exports.string_path = function (from_pos, to_pos){ return from_pos.x+" "+from_pos.y+" "+from_pos.roomName+" "+to_pos.x+" "+to_pos.y+" "+to_pos.roomName}
  45. exports.pathmemorystart = function ()
  46. {
  47.  
  48. if (Memory.path_cache == undefined)
  49. {
  50. this.path_cache = {}
  51. } else {
  52. this.path_cache = Memory.path_cache
  53. }
  54. this.clean()
  55. }
  56. exports.pathmemoryend = function ()
  57. {
  58. Memory.path_cache = this.path_cache
  59. }
  60. exports.startpath = function (creep,dest,parm)
  61. {
  62. // write the values in memory of the creep
  63. creep.memory.DEST = dest.id
  64. creep.memory.POS = dest.pos
  65. for (var p in parm)
  66. {
  67. creep.memory[p] = parm[p]
  68. }
  69. this.continue_path(creep,dest.pos)
  70. }
  71. exports.deletepath = function (creep)
  72. {
  73. delete this.path_cache[creep.memory.HASH]
  74. }
  75. exports.endpath = function (creep,parm)
  76. {
  77. // write the values in memory of the creep
  78. delete creep.memory.DEST
  79. delete creep.memory.POS
  80. delete creep.memory.STEP
  81. delete creep.memory.HASH
  82. delete creep.memory.ACTION
  83. for (var p in parm)
  84. {
  85. delete creep.memory[p]
  86. }
  87. }
  88. exports.continue_path = function (creep, to_pos)
  89. {
  90. var temp_hash
  91. var from_pos = creep.pos
  92. var opt ={}// {ignoreCreeps : true}
  93. if (creep.memory.HASH == undefined)
  94. {
  95. creep.memory.STEP = 0
  96. creep.memory.HASH = this.string_path(from_pos, to_pos)
  97.  
  98.  
  99. }
  100. if (this.path_cache == undefined) this.path_cache = {}
  101. if (this.path_cache[creep.memory.HASH] == undefined)
  102. {
  103. // we need to make a new path record
  104. this.path_cache[creep.memory.HASH] ={}
  105. this.path_cache[creep.memory.HASH]["created"] = Game.time
  106. this.path_cache[creep.memory.HASH]["last_used"] = Game.time
  107. this.path_cache[creep.memory.HASH]["times_used"] = 1
  108. this.path_cache[creep.memory.HASH]["path"] = from_pos.findPathTo(to_pos,opt)
  109. } else {
  110. this.path_cache[creep.memory.HASH]["times_used"] = this.path_cache[creep.memory.HASH]["times_used"] +1
  111. this.path_cache[creep.memory.HASH]["last_used"] = Game.time
  112. }
  113.  
  114. if (this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP] != undefined)
  115. {
  116. //console.log ("DDD "+this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP]["direction"])
  117.  
  118. if (creep.pos.x == this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP]["x"] && creep.pos.y == this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP]["y"])
  119. {
  120.  
  121. creep.memory.STEP = creep.memory.STEP + 1
  122. creep.say(creep.memory.STEP+"/"+this.path_cache[creep.memory.HASH]["path"].length,1)
  123. delete creep.memory.block
  124. } else {
  125.  
  126. if (creep.memory.block == undefined)
  127. {
  128. creep.memory.block =1
  129. } else if (creep.memory.block > 10) {
  130. temp_hash =creep.memory.HASH
  131. this.deletepath(creep)
  132. this.endpath(creep,["ACTION","RESOURCE"])
  133.  
  134. creep.memory.block = 0
  135.  
  136. } else {
  137.  
  138. creep.memory.block = creep.memory.block + 1
  139. creep.say("! "+ creep.memory.block,1)
  140. }
  141. //
  142. }
  143.  
  144. if (creep.memory.HASH !== undefined && this.path_cache[creep.memory.HASH] != undefined && this.path_cache[creep.memory.HASH]["path"])
  145. {
  146. var move = creep.move(this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP]["direction"])
  147.  
  148. // console.log(creep+"move "+move)
  149. // console.log("after")
  150. // console.log(creep.pos.x+" "+this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP-1]["x"])
  151. // console.log(creep.pos.y+" "+this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP-1]["y"])
  152. if (move != OK && move != -4)
  153. {
  154. console.log(creep+" ERROR: "+move)
  155. } else {
  156.  
  157. }
  158. if (move < 0)
  159. {
  160. creep.say("!",1)
  161. } else {
  162.  
  163. }
  164. } else {
  165. console.log("Problem with path : "+ temp_hash)
  166. }
  167. } else {
  168. this.endpath(creep,["ACTION","RESOURCE"])
  169.  
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement