Created
December 26, 2016 22:39
-
-
Save Sheeo/f5f872b547202b5c50add6af63eeb4ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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