Skip to content

Instantly share code, notes, and snippets.

@Sheeo
Created December 26, 2016 22:39
Show Gist options
  • Save Sheeo/f5f872b547202b5c50add6af63eeb4ee to your computer and use it in GitHub Desktop.
Save Sheeo/f5f872b547202b5c50add6af63eeb4ee to your computer and use it in GitHub Desktop.
export function CalculateIntegrationField(room: Room, goalName: string, goals: [number, number][]): IntegrationField {
if(!goals) throw new Error("No goals provided");
if(!room.memory.costField || room.memory.costFieldInvalid) {
CalculateCostField(room);
}
let pre = Game.cpu.getUsed();
let costField = room.memory.costField;
let integrationField: number[][] = [];
let openList: [number, number][] = [];
setCells(integrationField, 65535);
for(let [i,j] of goals) {
openList.push([i,j]);
integrationField[i][j] = 0;
}
let currentNode: [number, number] | undefined = undefined;
do {
currentNode = openList.pop();
if(!currentNode) continue;
let currentCost = integrationField[currentNode[0]][currentNode[1]];
for(let [i,j] of neighbours(currentNode!)) {
let oldCost = integrationField[i][j];
let newCost = costField[i][j] + currentCost;
if(newCost < oldCost && newCost < 255) {
integrationField[i][j] = newCost;
openList.push([i,j]);
}
}
}
while(currentNode);
console.log("Integration-matrix computation cost", Game.cpu.getUsed()-pre);
if(!_.isObject(room.memory.integrationFields)) {
room.memory.integrationFields = {};
}
room.memory.integrationFields[goalName] = integrationField;
return integrationField;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment