Example for storing and using a path in memory?
-
I'm at the point where my CPU get's to it's limits. and i think that for my knowledge i have done the optimizations i could find.
I see that pathfinding is a major part of my used CPU, however i have no experiance at all with storing and reusing paths, and couldnt find something that helped me (as a newb to programming).
So, would anyone be willing to share some code snippets with me?
1. how and where to store a path
2. how to know when to use a path that is stored
3. how to use a stored path.
-
Just so you know, you are asking for what is close to a framework, not a snippet. Path re-use is one of the more complicated tasks to undertake in Screeps.
-
Hmm... ok. In that case i'll look into other ways of optimization first.
But in case someone wants to give some more general directions to the topic i'm very interested
-
1. You store serialized moving path in creep.memory (ex. creep.memory.movePath). Move path return from RoomPosition.findPathTo() is an array of path, you should use Room.serializePath to serialize it to string before store in memory.
2. You need some way to track your current destination (ex. creep.memory.moveDest) and check if your destination is changed or not before move. If it change then find new move path. Otherwise move your creep using Creep.moveByPath(creep.memory.movePath)
3. in #2
See example in http://support.screeps.com/hc/en-us/articles/203013212-Creep#moveByPath
if(!creep.memory.path) { creep.memory.path = creep.pos.findPathTo(target); } creep.moveByPath(creep.memory.path);
Same as other function document above code only show you a very simple usage for moveByPath. You should apply more complex logic in your code by yourself. Please note that, above example does not serialized path before storing it in memory.
Moving between rooms and using path is something more complex than moving within room.
-
save and reuse path is a bit complicate for me so I only store the result from "Game.map.findRoute" and use it when my creep need to move between room, for the moving inside the room I only try to optimize the minimum creep need in that room instead. and also try to find a way to make them need to move as little as possible
-
And here I was trying to make sense of _move.path, and all proud that I had figured out what I needed. I was trying to figure out which direction the pathfinder was taking my creep when doing moveTo(x,y,"");
Just in case anyone else was looking, here's a quick snippet.
creep.moveTo(new RoomPosition(5, 19, creep.room.name));
console.log(JSON.stringify(Room.deserializePath(creep.memory._move.path)));