[Code Snippet] Get Possible Connections for Source
-
Here's a bit of code I came up with for getting how many creeps I can have 'harvesting' a source simultaneously:
function Get_Connections(source)
{
var result = [];
var North = source.room.lookAt(source.pos.x, source.pos.y - 1);
North.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[0] = object[LOOK_TERRAIN];
}
});
var NorthEast = source.room.lookAt(source.pos.x + 1, source.pos.y - 1);
NorthEast.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[1] = object[LOOK_TERRAIN];
}
});
var NorthWest = source.room.lookAt(source.pos.x - 1, source.pos.y - 1);
NorthWest.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[2] = object[LOOK_TERRAIN];
}
});
var East = source.room.lookAt(source.pos.x + 1, source.pos.y);
East.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[3] = object[LOOK_TERRAIN];
}
});
var South = source.room.lookAt(source.pos.x, source.pos.y + 1);
South.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[4] = object[LOOK_TERRAIN];
}
});
var SouthEast = source.room.lookAt(source.pos.x + 1, source.pos.y + 1);
SouthEast.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[5] = object[LOOK_TERRAIN];
}
});
var SouthWest = source.room.lookAt(source.pos.x - 1, source.pos.y + 1);
SouthWest.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[6] = object[LOOK_TERRAIN];
}
});
var West = source.room.lookAt(source.pos.x - 1, source.pos.y);
West.forEach(function(object)
{
if (object.type == LOOK_TERRAIN)
{
result[7] = object[LOOK_TERRAIN];
}
});
return result;
}and its usage:
function Assign(creep)
{
var sources = creep.room.find(FIND_SOURCES);
for (s = 0; s < sources.length; s++)
{
var connections = Get_Connections(sources[s]);
var count = 0;
for (c = 0; c < connections.length; c++)
{
if (connections[c] == "plain" ||
connections[c] == "swamp")
{
count++;
}
}
if (count > 1)
{
var connected = 0;
for (var name in Game.creeps)
{
var existing = Game.creeps[name];
if (existing.memory.assigned == sources[s].id)
{
connected++;
if (connected == count)
{
break;
}
}
}
if (connected < count)
{
creep.memory.assigned = sources[s].id;
break;
}
}
else
{
var assigned = false;
for (var name in Game.creeps)
{
var existing = Game.creeps[name];
if (existing.memory.assigned == sources[s].id)
{
assigned = true;
break;
}
}
if (assigned == false)
{
creep.memory.assigned = sources[s].id;
break;
}
}
}
}Obviously this doesn't take into account any structures next to the source, which you hopefully should never need to.
-
Hi, shouldn't you also count swamp as open space too?
-
This may seem important initially, but it really isn't. A single creep with 5 work can fully utilize a source needing only one space. You can create this creep at RCL 2 which is more or less instantly.
That being said, this same logic would be pretty useful for deciding the best place to attack a wall.
-
lol forgot about swamp... code updated
-
Could simplify checking for terrain with something like;
function Get_Connections(source) {
let area = source.room.lookForAtArea(LOOK_TERRAIN, source.pos.y - 1, source.pos.x - 1, source.pos.y + 1, source.pos.x + 1, true);
i=0;
for (let block of area) {
if (block.terrain == 'wall') {
i++;
}
}
return 9-i;
}
-
I have this similar kind of script to count for maximum miner that should be allowed in one room and collect it in memory so if I want to save CPU I'll search for drop only on that open space around the source but currently I'm too greedy and just let them search every place in the room
-
var fields = room.lookForAtArea(LOOK_TERRAIN, source.pos.y-1, source.pos.x-1, source.pos.y+1, source.pos.x+1, true);
var accessibleFields = 9-_.countBy( fields , "terrain" ).wall;
-
That's definitely simpler!
Had to use
10-_.countBy
instead of
9-_.countBy
since it gave the wrong count for me.
-
*edit* others posted my <200 lines solution already ...