Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Module code goes here. Use 'module.exports' to export things:
- * module.exports.thing = 'a thing';
- *
- * You can import it from another modules like this:
- * var mod = require('path');
- * mod.thing == 'a thing'; // true
- */
- exports.clean = function()
- {
- if (Memory.path_cache != undefined)
- {
- if (Memory.path_cache_clean_timer == undefined || Game.time - Memory.path_cache_clean_timer > 60 )
- {
- Memory.path_cache_clean_timer = Game.time
- var text_out = "Path memory cleanup "
- var pathcount = 0
- for (var hash in this.path_cache)
- {
- var created = Game.time - this.path_cache[hash]["created"]
- var last_used = Game.time - this.path_cache[hash]["last_used"]
- text_out = text_out + pathcount+"] "+hash+"("+this.path_cache[hash]["path"].length+") "+created+" >"+last_used+"| "+ this.path_cache[hash]["times_used"]+"\n"
- pathcount= pathcount +1
- if (created >1000 && last_used > 1000)
- {
- delete this.path_cache[hash]
- text_out = text_out +"Cleaning up record has expired\n"
- }
- }
- console.log(text_out)
- }
- }
- }
- 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}
- exports.pathmemorystart = function ()
- {
- if (Memory.path_cache == undefined)
- {
- this.path_cache = {}
- } else {
- this.path_cache = Memory.path_cache
- }
- this.clean()
- }
- exports.pathmemoryend = function ()
- {
- Memory.path_cache = this.path_cache
- }
- exports.startpath = function (creep,dest,parm)
- {
- // write the values in memory of the creep
- creep.memory.DEST = dest.id
- creep.memory.POS = dest.pos
- for (var p in parm)
- {
- creep.memory[p] = parm[p]
- }
- this.continue_path(creep,dest.pos)
- }
- exports.deletepath = function (creep)
- {
- delete this.path_cache[creep.memory.HASH]
- }
- exports.endpath = function (creep,parm)
- {
- // write the values in memory of the creep
- delete creep.memory.DEST
- delete creep.memory.POS
- delete creep.memory.STEP
- delete creep.memory.HASH
- delete creep.memory.ACTION
- for (var p in parm)
- {
- delete creep.memory[p]
- }
- }
- exports.continue_path = function (creep, to_pos)
- {
- var temp_hash
- var from_pos = creep.pos
- var opt ={}// {ignoreCreeps : true}
- if (creep.memory.HASH == undefined)
- {
- creep.memory.STEP = 0
- creep.memory.HASH = this.string_path(from_pos, to_pos)
- }
- if (this.path_cache == undefined) this.path_cache = {}
- if (this.path_cache[creep.memory.HASH] == undefined)
- {
- // we need to make a new path record
- this.path_cache[creep.memory.HASH] ={}
- this.path_cache[creep.memory.HASH]["created"] = Game.time
- this.path_cache[creep.memory.HASH]["last_used"] = Game.time
- this.path_cache[creep.memory.HASH]["times_used"] = 1
- this.path_cache[creep.memory.HASH]["path"] = from_pos.findPathTo(to_pos,opt)
- } else {
- this.path_cache[creep.memory.HASH]["times_used"] = this.path_cache[creep.memory.HASH]["times_used"] +1
- this.path_cache[creep.memory.HASH]["last_used"] = Game.time
- }
- if (this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP] != undefined)
- {
- //console.log ("DDD "+this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP]["direction"])
- 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"])
- {
- creep.memory.STEP = creep.memory.STEP + 1
- creep.say(creep.memory.STEP+"/"+this.path_cache[creep.memory.HASH]["path"].length,1)
- delete creep.memory.block
- } else {
- if (creep.memory.block == undefined)
- {
- creep.memory.block =1
- } else if (creep.memory.block > 10) {
- temp_hash =creep.memory.HASH
- this.deletepath(creep)
- this.endpath(creep,["ACTION","RESOURCE"])
- creep.memory.block = 0
- } else {
- creep.memory.block = creep.memory.block + 1
- creep.say("! "+ creep.memory.block,1)
- }
- //
- }
- if (creep.memory.HASH !== undefined && this.path_cache[creep.memory.HASH] != undefined && this.path_cache[creep.memory.HASH]["path"])
- {
- var move = creep.move(this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP]["direction"])
- // console.log(creep+"move "+move)
- // console.log("after")
- // console.log(creep.pos.x+" "+this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP-1]["x"])
- // console.log(creep.pos.y+" "+this.path_cache[creep.memory.HASH]["path"][creep.memory.STEP-1]["y"])
- if (move != OK && move != -4)
- {
- console.log(creep+" ERROR: "+move)
- } else {
- }
- if (move < 0)
- {
- creep.say("!",1)
- } else {
- }
- } else {
- console.log("Problem with path : "+ temp_hash)
- }
- } else {
- this.endpath(creep,["ACTION","RESOURCE"])
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement