Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/garrisonManager.js
/** | /** | ||||
* Manage the garrisonHolders | * Manage the garrisonHolders | ||||
* When a unit is ordered to garrison, it must be done through this.garrison() function so that | * When a unit is ordered to garrison, it must be done through this.garrison() function so that | ||||
* an object in this.holders is created. This object contains an array with the entities | * an object in this.holders is created. This object contains an array with the entities | ||||
* in the process of being garrisoned. To have all garrisoned units, we must add those in holder.garrisoned(). | * in the process of being garrisoned. To have all garrisoned units, we must add those in holder.garrisoned(). | ||||
* Futhermore garrison units have a metadata garrisonType describing its reason (protection, transport, ...) | * Futhermore garrison units have a metadata garrisonType describing its reason (protection, transport, ...) | ||||
*/ | */ | ||||
PETRA.GarrisonManager = function(Config) | PETRA.GarrisonManager = function(Config) | ||||
{ | { | ||||
this.Config = Config; | this.Config = Config; | ||||
this.holders = new Map(); | this.holders = new Map(); | ||||
this.decayingStructures = new Map(); | this.decayingStructures = new Map(); | ||||
this.inEmergency = false; | |||||
}; | |||||
PETRA.GarrisonManager.prototype.updateEmergency = function(gameState, events) { | |||||
// We don't have anything else to do. | |||||
if (this.inEmergency) | |||||
return; | |||||
this.inEmergency = true; | |||||
this.ungarrisonAllUnits(gameState); | |||||
Freagarach: This will fail unless you check the events first. (E.g. an entity has garrisoned on the… | |||||
}; | }; | ||||
PETRA.GarrisonManager.prototype.update = function(gameState, events) | PETRA.GarrisonManager.prototype.update = function(gameState, events) | ||||
{ | { | ||||
// First check for possible upgrade of a structure | // First check for possible upgrade of a structure | ||||
for (let evt of events.EntityRenamed) | for (let evt of events.EntityRenamed) | ||||
{ | { | ||||
for (let id of this.holders.keys()) | for (let id of this.holders.keys()) | ||||
{ | { | ||||
if (id != evt.entity) | if (id != evt.entity) | ||||
continue; | continue; | ||||
let data = this.holders.get(id); | let data = this.holders.get(id); | ||||
let newHolder = gameState.getEntityById(evt.newentity); | let newHolder = gameState.getEntityById(evt.newentity); | ||||
if (newHolder && newHolder.isGarrisonHolder()) | if (newHolder && newHolder.isGarrisonHolder()) | ||||
Not Done Inline Actionsyou cant stop renaming events else ai will be messed up Silier: you cant stop renaming events else ai will be messed up | |||||
{ | { | ||||
this.holders.delete(id); | this.holders.delete(id); | ||||
this.holders.set(evt.newentity, data); | this.holders.set(evt.newentity, data); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
for (let entId of data.list) | for (let entId of data.list) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 323 Lines • ▼ Show 20 Lines | |||||
PETRA.GarrisonManager.prototype.removeDecayingStructure = function(entId) | PETRA.GarrisonManager.prototype.removeDecayingStructure = function(entId) | ||||
{ | { | ||||
if (!this.decayingStructures.has(entId)) | if (!this.decayingStructures.has(entId)) | ||||
return; | return; | ||||
this.decayingStructures.delete(entId); | this.decayingStructures.delete(entId); | ||||
}; | }; | ||||
PETRA.GarrisonManager.prototype.ungarrisonAllUnits = function(gameState) { | |||||
for (const [id, data] of this.holders.entries()) | |||||
{ | |||||
for (const garrisonedEnt of data.list) | |||||
{ | |||||
const ent = gameState.getEntityById(garrisonedEnt); | |||||
if (ent) | |||||
this.leaveGarrison(ent); | |||||
} | |||||
Not Done Inline ActionsTypo? Shouldn't it be in the if? Stan: Typo? Shouldn't it be in the if? | |||||
this.holders.delete(id); | |||||
Done Inline ActionsCan't you clear the entire map at the end instead ? This sounds bad in a loop where you could invalidate the iterator. Stan: Can't you clear the entire map at the end instead ? This sounds bad in a loop where you could… | |||||
} | |||||
}; | |||||
PETRA.GarrisonManager.prototype.Serialize = function() | PETRA.GarrisonManager.prototype.Serialize = function() | ||||
{ | { | ||||
return { "holders": this.holders, "decayingStructures": this.decayingStructures }; | return { "holders": this.holders, "decayingStructures": this.decayingStructures, "inEmergency": this.inEmergency }; | ||||
}; | }; | ||||
PETRA.GarrisonManager.prototype.Deserialize = function(data) | PETRA.GarrisonManager.prototype.Deserialize = function(data) | ||||
{ | { | ||||
for (let key in data) | for (let key in data) | ||||
this[key] = data[key]; | this[key] = data[key]; | ||||
}; | }; |
Wildfire Games · Phabricator
This will fail unless you check the events first. (E.g. an entity has garrisoned on the previous turn, so its holder has not been added to this.holders yet.)