I need some help finding the nearest room
-
Hello everyone!
I use flags to place them in rooms i don't control and then do stuff there. Once the flag is placed, "it" looks for the nearest room and uses its spawn to create desired creeps.
This is how it does...
let minDist = 10; // A random distance to start with let distance = 0; // Starting random value let targetSpawn = null; //If the flag aready has a target room... if(_.isString(flag.memory.targetRoom)){ targetSpawn = Game.getObjectById(Game.rooms[flag.memory.targetRoom].memory.spawns[0].id); //But if it doesn't, i "teach" it, and here is where my issue comes... }else{ //Here i try to look for the nearest room for (let i in Game.rooms){ if(Game.rooms[i].controller.my){ distance = Game.map.getRoomLinearDistance(flag.pos.roomName, Game.rooms[i].name, true); if(distance < minDist && null!=Game.rooms[i].memory.spawns){ minDist = distance; flag.memory.targetRoom = Game.rooms[i].name; } } } targetSpawn = Game.getObjectById(Game.rooms[flag.memory.targetRoom].memory.spawns[0].id); }
Mi issue is that it seems to consider adjacent rooms as near as diagonal rooms, and this is what i want to (and can't) avoid.
Thanks in advance, dear fellows!!
-
I think you can change
Game.map.getRoomLinearDistance
onGame.map.findRoute(flag.pos.roomName, Game.rooms[i].name)
. So you will found nearest room by path, not a range. Because some time nearest rooms have very long path to travel between those rooms.