Fix AI errors when promoting an athenian spearman to champion.

Ideally the AI would try to carry on and recover gracefully but the code is setup in such a way that this seems like it would lead to a lot of different bugs down the line.

With help from @langbart on QA and code.

Fixes #7425

(cherry picked from commit c20ca02911)
Signed-off-by: Itms <itms@wildfiregames.com>
This commit is contained in:
Lancelot de Ferrière 2024-12-30 18:26:33 +01:00 committed by Itms
parent 85363e861e
commit 9798b9180b
No known key found for this signature in database
GPG key ID: C7E52BD14CE14E09
2 changed files with 15 additions and 3 deletions

View file

@ -549,7 +549,7 @@ PETRA.BaseManager.prototype.assignRolelessUnits = function(gameState, roleless)
if (ent.hasClasses(["Worker", "CitizenSoldier", "FishingBoat"]))
ent.setMetadata(PlayerID, "role", PETRA.Worker.ROLE_WORKER);
else if (ent.hasClass("Support") && ent.hasClass("Elephant"))
ent.setMetadata(PlayerID, "role", "worker");
ent.setMetadata(PlayerID, "role", PETRA.Worker.ROLE_WORKER);
}
};

View file

@ -150,9 +150,21 @@ PETRA.BasesManager.prototype.checkEvents = function(gameState, events)
for (const evt of events.EntityRenamed)
{
const ent = gameState.getEntityById(evt.newentity);
if (!ent || ent.owner() != PlayerID || ent.getMetadata(PlayerID, "base") === undefined)
const baseID = ent?.getMetadata(PlayerID, "base");
if (!ent || ent.owner() != PlayerID || baseID === undefined)
continue;
const base = this.getBaseByID(ent.getMetadata(PlayerID, "base"));
const base = this.getBaseByID(baseID);
// Promoted workers should be reset - their new gathering rates/capabilities might differ meaningfully.
if (ent.getMetadata(PlayerID, "role") === PETRA.Worker.ROLE_WORKER) {
ent.deleteAllMetadata(PlayerID);
ent.setMetadata(PlayerID, "base", baseID);
base.assignRolelessUnits(undefined, [ent]);
continue;
}
// Handle cases where a base anchor changes
if (!base.anchorId || base.anchorId != evt.entity)
continue;
base.anchorId = evt.newentity;