Changeset View
Standalone View
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(); | |||||||||
Stan: Bit weird to mark the pointer for the array const... Considering we change the contents… | ||||||||||
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); | ||||||||||
Done Inline ActionsMight need to check for foundations separately? Freagarach: Might need to check for foundations separately?
It seems that functionality (ownStructures or… | ||||||||||
Done Inline ActionsYes, we lose functionality here. Previously foundations were accounted (Identity classes are merged) for, now they are not (ResourceDropsite is not merged). Freagarach: Yes, we lose functionality here. Previously foundations were accounted (Identity classes are… | ||||||||||
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; | |||||||||
let droppableResources = template.resourceDropsiteTypes(); | ||||||||||
Lint: prefer-const 'droppableResources' is never reassigned. Use 'const' instead. Lint: prefer-const: 'droppableResources' is never reassigned. Use 'const' instead. | ||||||||||
FreagarachAuthorUnsubmitted Done Inline Actionsconst Freagarach: `const` | ||||||||||
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. | |||||||||
Done Inline Actions
Dumb question: are we building a weighted list? Stan: Dumb question: are we building a weighted list? | ||||||||||
Done Inline ActionsNo, we're just picking the highest value, the previous values are not stored. Freagarach: No, we're just picking the highest value, the previous values are not stored. | ||||||||||
let total = 2*gameState.sharedScript.resourceMaps[resource].map[j]; | const total = 2 * gameState.sharedScript.resourceMaps[resource].map[j]; | |||||||||
for (let res in gameState.sharedScript.resourceMaps) | for (let res in gameState.sharedScript.resourceMaps) | |||||||||
StanUnsubmitted Done Inline ActionsNo const there ? Stan: No const there ? | ||||||||||
FreagarachAuthorUnsubmitted Done Inline ActionsI don't change that line. ^^ Freagarach: I don't change that line. ^^ | ||||||||||
if (res != "food") | if (droppableResources.indexOf(res) != -1) | |||||||||
Done Inline ActionsWe might want to subtract resources that cannot be dropped here. Freagarach: We might want to subtract resources that cannot be dropped here. | ||||||||||
Done Inline Actionsaren't you doing it already by not adding it? Silier: aren't you doing it already by not adding it? | ||||||||||
Done Inline ActionsWell, kind of. But if you have two equal sites, one which has only trees and one has also berries, you probably want the one without berries (less crowded and easier to place a farmstead there). Anyway, for another patch :) Freagarach: Well, kind of. But if you have two equal sites, one which has only trees and one has also… | ||||||||||
total += gameState.sharedScript.resourceMaps[res].map[j]; | total += gameState.sharedScript.resourceMaps[res].map[j]; | |||||||||
Done Inline Actionsmmh, pretty sure that should error out. you're changing its value l294 and 296 Stan: mmh, pretty sure that should error out. you're changing its value l294 and 296 | ||||||||||
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)]; | |||||||||
for (let dp of dpEnts) | for (let dp of dpEnts) | |||||||||
{ | { | |||||||||
let dpPos = dp.position(); | let dpPos = dp.position(); | |||||||||
if (!dpPos) | if (!dpPos) | |||||||||
continue; | continue; | |||||||||
let dist = API3.SquareVectorDistance(dpPos, pos); | let dist = API3.SquareVectorDistance(dpPos, pos); | |||||||||
if (dist < 3600) | if (dist < 3600) | |||||||||
{ | { | |||||||||
total = 0; | total = 0; | |||||||||
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 |
Bit weird to mark the pointer for the array const... Considering we change the contents afterwards.