Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/baseManager.js
Show First 20 Lines • Show All 258 Lines • ▼ Show 20 Lines | PETRA.BaseManager.prototype.findBestDropsiteLocation = function(gameState, resource) | ||||
// This builds a map. The procedure is fairly simple. It adds the resource maps | // This builds a map. The procedure is fairly simple. It adds the resource maps | ||||
// (which are dynamically updated and are made so that they will facilitate DP placement) | // (which are dynamically updated and are made so that they will facilitate DP placement) | ||||
// Then checks for a good spot in the territory. If none, and town/city phase, checks outside | // Then checks for a good spot in the territory. If none, and town/city phase, checks outside | ||||
// The AI will currently not build a CC if it wouldn't connect with an existing CC. | // The AI will currently not build a CC if it wouldn't connect with an existing CC. | ||||
let obstructions = PETRA.createObstructionMap(gameState, this.accessIndex, template); | let obstructions = PETRA.createObstructionMap(gameState, this.accessIndex, template); | ||||
let ccEnts = gameState.getOwnStructures().filter(API3.Filters.byClass("CivCentre")).toEntityArray(); | const dpEnts = gameState.getOwnStructures().filter(API3.Filters.isDropsite(resource)).toEntityArray(); | ||||
let dpEnts = gameState.getOwnStructures().filter(API3.Filters.byClassesOr(["Storehouse", "Dock"])).toEntityArray(); | |||||
// Foundations don't have the dropsite properties yet, so treat them separately. | |||||
for (const foundation of gameState.getOwnFoundations().toEntityArray()) | |||||
if (PETRA.getBuiltEntity(gameState, foundation).isResourceDropsite(resource)) | |||||
dpEnts.push(foundation); | |||||
let bestIdx; | let bestIdx; | ||||
let bestVal = 0; | let bestVal = 0; | ||||
let radius = Math.ceil(template.obstructionRadius().max / obstructions.cellSize); | let radius = Math.ceil(template.obstructionRadius().max / obstructions.cellSize); | ||||
let territoryMap = gameState.ai.HQ.territoryMap; | let territoryMap = gameState.ai.HQ.territoryMap; | ||||
let width = territoryMap.width; | let width = territoryMap.width; | ||||
let cellSize = territoryMap.cellSize; | let cellSize = territoryMap.cellSize; | ||||
const droppableResources = template.resourceDropsiteTypes(); | |||||
for (let j of this.territoryIndices) | for (let j of this.territoryIndices) | ||||
{ | { | ||||
let i = territoryMap.getNonObstructedTile(j, radius, obstructions); | let i = territoryMap.getNonObstructedTile(j, radius, obstructions); | ||||
if (i < 0) // no room around | if (i < 0) // no room around | ||||
continue; | continue; | ||||
// we add 3 times the needed resource and once the others (except food) | // We add 3 times the needed resource and once others that can be dropped here. | ||||
let total = 2*gameState.sharedScript.resourceMaps[resource].map[j]; | let total = 2 * gameState.sharedScript.resourceMaps[resource].map[j]; | ||||
for (let res in gameState.sharedScript.resourceMaps) | for (const res in gameState.sharedScript.resourceMaps) | ||||
if (res != "food") | if (droppableResources.indexOf(res) != -1) | ||||
total += gameState.sharedScript.resourceMaps[res].map[j]; | total += gameState.sharedScript.resourceMaps[res].map[j]; | ||||
total *= 0.7; // Just a normalisation factor as the locateMap is limited to 255 | total *= 0.7; // Just a normalisation factor as the locateMap is limited to 255 | ||||
if (total <= bestVal) | if (total <= bestVal) | ||||
continue; | continue; | ||||
let pos = [cellSize * (j%width+0.5), cellSize * (Math.floor(j/width)+0.5)]; | let pos = [cellSize * (j%width+0.5), cellSize * (Math.floor(j/width)+0.5)]; | ||||
Show All 9 Lines | for (let dp of dpEnts) | ||||
break; | break; | ||||
} | } | ||||
else if (dist < 6400) | else if (dist < 6400) | ||||
total *= (Math.sqrt(dist)-60)/20; | total *= (Math.sqrt(dist)-60)/20; | ||||
} | } | ||||
if (total <= bestVal) | if (total <= bestVal) | ||||
continue; | continue; | ||||
for (let cc of ccEnts) | |||||
{ | |||||
let ccPos = cc.position(); | |||||
if (!ccPos) | |||||
continue; | |||||
let dist = API3.SquareVectorDistance(ccPos, pos); | |||||
if (dist < 3600) | |||||
{ | |||||
total = 0; | |||||
break; | |||||
} | |||||
else if (dist < 6400) | |||||
total *= (Math.sqrt(dist)-60)/20; | |||||
} | |||||
if (total <= bestVal) | |||||
continue; | |||||
if (gameState.ai.HQ.isDangerousLocation(gameState, pos, halfSize)) | if (gameState.ai.HQ.isDangerousLocation(gameState, pos, halfSize)) | ||||
continue; | continue; | ||||
bestVal = total; | bestVal = total; | ||||
bestIdx = i; | bestIdx = i; | ||||
} | } | ||||
if (this.Config.debug > 2) | if (this.Config.debug > 2) | ||||
warn(" for dropsite best is " + bestVal); | warn(" for dropsite best is " + bestVal); | ||||
▲ Show 20 Lines • Show All 755 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator