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 542 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
if (!roleless) | if (!roleless) | ||||
roleless = this.units.filter(API3.Filters.not(API3.Filters.byHasMetadata(PlayerID, "role"))).values(); | roleless = this.units.filter(API3.Filters.not(API3.Filters.byHasMetadata(PlayerID, "role"))).values(); | ||||
for (let ent of roleless) | for (let ent of roleless) | ||||
{ | { | ||||
if (ent.hasClasses(["Worker", "CitizenSoldier", "FishingBoat"])) | if (ent.hasClasses(["Worker", "CitizenSoldier", "FishingBoat"])) | ||||
ent.setMetadata(PlayerID, "role", PETRA.Worker.ROLE_WORKER); | ent.setMetadata(PlayerID, "role", PETRA.Worker.ROLE_WORKER); | ||||
else if (ent.hasClass("Support") && ent.hasClass("Elephant")) | |||||
ent.setMetadata(PlayerID, "role", "worker"); | |||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* If the numbers of workers on the resources is unbalanced then set some of workers to idle so | * If the numbers of workers on the resources is unbalanced then set some of workers to idle so | ||||
* they can be reassigned by reassignIdleWorkers. | * they can be reassigned by reassignIdleWorkers. | ||||
* TODO: actually this probably should be in the HQ. | * TODO: actually this probably should be in the HQ. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | if (!idleWorkers) | ||||
idleWorkers = gameState.updatingCollection("idle-workers-base-" + this.ID, filter, this.workers).values(); | idleWorkers = gameState.updatingCollection("idle-workers-base-" + this.ID, filter, this.workers).values(); | ||||
} | } | ||||
for (let ent of idleWorkers) | for (let ent of idleWorkers) | ||||
{ | { | ||||
// Check that the worker isn't garrisoned | // Check that the worker isn't garrisoned | ||||
if (!ent.position()) | if (!ent.position()) | ||||
continue; | continue; | ||||
// Support elephant can only be builders | |||||
if (ent.hasClass("Support") && ent.hasClass("Elephant")) | |||||
{ | |||||
ent.setMetadata(PlayerID, "subrole", "idle"); | |||||
continue; | |||||
} | |||||
if (ent.hasClass("Worker")) | if (ent.hasClass("Worker")) | ||||
{ | { | ||||
// Just emergency repairing here. It is better managed in assignToFoundations | // Just emergency repairing here. It is better managed in assignToFoundations | ||||
if (ent.isBuilder() && this.anchor && this.anchor.needsRepair() && | if (ent.isBuilder() && this.anchor && this.anchor.needsRepair() && | ||||
gameState.getOwnEntitiesByMetadata("target-foundation", this.anchor.id()).length < 2) | gameState.getOwnEntitiesByMetadata("target-foundation", this.anchor.id()).length < 2) | ||||
ent.repair(this.anchor); | ent.repair(this.anchor); | ||||
else if (ent.isGatherer()) | else if (ent.isGatherer()) | ||||
▲ Show 20 Lines • Show All 206 Lines • ▼ Show 20 Lines | let nonBuilderWorkers = workers.filter(function(ent) { | ||||
return false; | return false; | ||||
if (ent.getMetadata(PlayerID, "transport")) | if (ent.getMetadata(PlayerID, "transport")) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
}).toEntityArray(); | }).toEntityArray(); | ||||
let time = target.buildTime(); | let time = target.buildTime(); | ||||
nonBuilderWorkers.sort((workerA, workerB) => { | nonBuilderWorkers.sort((workerA, workerB) => { | ||||
let coeffA = API3.SquareVectorDistance(target.position(), workerA.position()); | let coeffA = API3.SquareVectorDistance(target.position(), workerA.position()); | ||||
if (workerA.getMetadata(PlayerID, "gather-type") == "food") | // elephant moves slowly, so when far away they are only useful if build time is long | ||||
if (workerA.hasClass("Elephant")) | |||||
coeffA *= 0.5 * (1 + Math.sqrt(coeffA)/5/time); | |||||
else if (workerA.getMetadata(PlayerID, "gather-type") == "food") | |||||
coeffA *= 3; | coeffA *= 3; | ||||
let coeffB = API3.SquareVectorDistance(target.position(), workerB.position()); | let coeffB = API3.SquareVectorDistance(target.position(), workerB.position()); | ||||
if (workerB.getMetadata(PlayerID, "gather-type") == "food") | if (workerB.hasClass("Elephant")) | ||||
coeffB *= 0.5 * (1 + Math.sqrt(coeffB)/5/time); | |||||
else if (workerB.getMetadata(PlayerID, "gather-type") == "food") | |||||
coeffB *= 3; | coeffB *= 3; | ||||
return coeffA - coeffB; | return coeffA - coeffB; | ||||
}); | }); | ||||
let current = 0; | let current = 0; | ||||
let nonBuilderTot = nonBuilderWorkers.length; | let nonBuilderTot = nonBuilderWorkers.length; | ||||
while (assigned < targetNB && builderTot < maxTotalBuilders && current < nonBuilderTot) | while (assigned < targetNB && builderTot < maxTotalBuilders && current < nonBuilderTot) | ||||
{ | { | ||||
++assigned; | ++assigned; | ||||
▲ Show 20 Lines • Show All 254 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator