Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/defenseArmy.js
var PETRA = function(m) | (function(m) | ||||
{ | { | ||||
/** Armies used by the defense manager. | /** Armies used by the defense manager. | ||||
* An army is a collection of own entities and enemy entities. | * An army is a collection of own entities and enemy entities. | ||||
* | * | ||||
* Types of armies: | * Types of armies: | ||||
* "default": army to counter an invading army | * "default": army to counter an invading army | ||||
* "capturing": army set to capture a gaia building or recover capture points to one of its own structures | * "capturing": army set to capture a gaia building or recover capture points to one of its own structures | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | m.DefenseArmy.prototype.removeOwn = function(gameState, id, Entity) | ||||
// Remove from tranport plan if not yet on Board | // Remove from tranport plan if not yet on Board | ||||
if (ent.getMetadata(PlayerID, "transport") !== undefined) | if (ent.getMetadata(PlayerID, "transport") !== undefined) | ||||
{ | { | ||||
let plan = gameState.ai.HQ.navalManager.getPlan(ent.getMetadata(PlayerID, "transport")); | let plan = gameState.ai.HQ.navalManager.getPlan(ent.getMetadata(PlayerID, "transport")); | ||||
if (plan && plan.state == "boarding" && ent.position()) | if (plan && plan.state == "boarding" && ent.position()) | ||||
plan.removeUnit(gameState, ent); | plan.removeUnit(gameState, ent); | ||||
} | } | ||||
/* | /* | ||||
// TODO be sure that all units in the transport need the cancelation | // TODO be sure that all units in the transport need the cancelation | ||||
if (!ent.position()) // this unit must still be in a transport plan ... try to cancel it | if (!ent.position()) // this unit must still be in a transport plan ... try to cancel it | ||||
{ | { | ||||
let planID = ent.getMetadata(PlayerID, "transport"); | let planID = ent.getMetadata(PlayerID, "transport"); | ||||
// no plans must mean that the unit was in a ship which was destroyed, so do nothing | // no plans must mean that the unit was in a ship which was destroyed, so do nothing | ||||
if (planID) | if (planID) | ||||
{ | { | ||||
if (gameState.ai.Config.debug > 0) | if (gameState.ai.Config.debug > 0) | ||||
▲ Show 20 Lines • Show All 199 Lines • ▼ Show 20 Lines | |||||
* merge this army with another properly. | * merge this army with another properly. | ||||
* assumes units are in only one army. | * assumes units are in only one army. | ||||
* also assumes that all have been properly cleaned up (no dead units). | * also assumes that all have been properly cleaned up (no dead units). | ||||
*/ | */ | ||||
m.DefenseArmy.prototype.merge = function(gameState, otherArmy) | m.DefenseArmy.prototype.merge = function(gameState, otherArmy) | ||||
{ | { | ||||
// copy over all parameters. | // copy over all parameters. | ||||
for (let i in otherArmy.assignedAgainst) | for (let i in otherArmy.assignedAgainst) | ||||
{ | |||||
if (this.assignedAgainst[i] === undefined) | if (this.assignedAgainst[i] === undefined) | ||||
this.assignedAgainst[i] = otherArmy.assignedAgainst[i]; | this.assignedAgainst[i] = otherArmy.assignedAgainst[i]; | ||||
else | else | ||||
this.assignedAgainst[i] = this.assignedAgainst[i].concat(otherArmy.assignedAgainst[i]); | this.assignedAgainst[i] = this.assignedAgainst[i].concat(otherArmy.assignedAgainst[i]); | ||||
} | |||||
for (let i in otherArmy.assignedTo) | for (let i in otherArmy.assignedTo) | ||||
this.assignedTo[i] = otherArmy.assignedTo[i]; | this.assignedTo[i] = otherArmy.assignedTo[i]; | ||||
for (let id of otherArmy.foeEntities) | for (let id of otherArmy.foeEntities) | ||||
this.addFoe(gameState, id, true); | this.addFoe(gameState, id, true); | ||||
// TODO: reassign those ? | // TODO: reassign those ? | ||||
for (let id of otherArmy.ownEntities) | for (let id of otherArmy.ownEntities) | ||||
this.addOwn(gameState, id, true); | this.addOwn(gameState, id, true); | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | |||||
/** adds or remove the strength of the entity either to the enemy or to our units. */ | /** adds or remove the strength of the entity either to the enemy or to our units. */ | ||||
m.DefenseArmy.prototype.evaluateStrength = function(ent, isOwn, remove) | m.DefenseArmy.prototype.evaluateStrength = function(ent, isOwn, remove) | ||||
{ | { | ||||
if (!ent) | if (!ent) | ||||
return; | return; | ||||
let entStrength; | let entStrength; | ||||
if (ent.hasClass("Structure")) | if (ent.hasClass("Structure")) | ||||
{ | |||||
if (ent.owner() !== PlayerID) | if (ent.owner() !== PlayerID) | ||||
entStrength = ent.getDefaultArrow() ? 6*ent.getDefaultArrow() : 4; | entStrength = ent.getDefaultArrow() ? 6*ent.getDefaultArrow() : 4; | ||||
else // small strength used only when we try to recover capture points | else // small strength used only when we try to recover capture points | ||||
entStrength = 2; | entStrength = 2; | ||||
} | |||||
else | else | ||||
entStrength = m.getMaxStrength(ent); | entStrength = m.getMaxStrength(ent); | ||||
// TODO adapt the getMaxStrength function for animals. | // TODO adapt the getMaxStrength function for animals. | ||||
// For the time being, just increase it for elephants as the returned value is too small. | // For the time being, just increase it for elephants as the returned value is too small. | ||||
if (ent.hasClass("Animal") && ent.hasClass("Elephant")) | if (ent.hasClass("Animal") && ent.hasClass("Elephant")) | ||||
entStrength *= 3; | entStrength *= 3; | ||||
if (remove) | if (remove) | ||||
entStrength *= -1; | entStrength *= -1; | ||||
if (isOwn) | if (isOwn) | ||||
this.ownStrength += entStrength; | this.ownStrength += entStrength; | ||||
else | else | ||||
this.foeStrength += entStrength; | this.foeStrength += entStrength; | ||||
}; | }; | ||||
m.DefenseArmy.prototype.checkEvents = function(gameState, events) | m.DefenseArmy.prototype.checkEvents = function(gameState, events) | ||||
{ | { | ||||
// Warning the metadata is already cloned in shared.js. Futhermore, changes should be done before destroyEvents | // Warning the metadata is already cloned in shared.js. Futhermore, changes should be done before destroyEvents | ||||
// otherwise it would remove the old entity from this army list | // otherwise it would remove the old entity from this army list | ||||
// TODO we should may-be reevaluate the strength | // TODO we should may-be reevaluate the strength | ||||
for (let evt of events.EntityRenamed) // take care of promoted and packed units | for (let evt of events.EntityRenamed) // take care of promoted and packed units | ||||
{ | |||||
if (this.foeEntities.indexOf(evt.entity) !== -1) | if (this.foeEntities.indexOf(evt.entity) !== -1) | ||||
{ | { | ||||
let ent = gameState.getEntityById(evt.newentity); | let ent = gameState.getEntityById(evt.newentity); | ||||
if (ent && ent.templateName().indexOf("resource|") !== -1) // corpse of animal killed | if (ent && ent.templateName().indexOf("resource|") !== -1) // corpse of animal killed | ||||
continue; | continue; | ||||
let idx = this.foeEntities.indexOf(evt.entity); | let idx = this.foeEntities.indexOf(evt.entity); | ||||
this.foeEntities[idx] = evt.newentity; | this.foeEntities[idx] = evt.newentity; | ||||
this.assignedAgainst[evt.newentity] = this.assignedAgainst[evt.entity]; | this.assignedAgainst[evt.newentity] = this.assignedAgainst[evt.entity]; | ||||
Show All 11 Lines | else if (this.ownEntities.indexOf(evt.entity) !== -1) | ||||
for (let against in this.assignedAgainst) | for (let against in this.assignedAgainst) | ||||
{ | { | ||||
if (!this.assignedAgainst[against]) | if (!this.assignedAgainst[against]) | ||||
continue; | continue; | ||||
if (this.assignedAgainst[against].indexOf(evt.entity) !== -1) | if (this.assignedAgainst[against].indexOf(evt.entity) !== -1) | ||||
this.assignedAgainst[against][this.assignedAgainst[against].indexOf(evt.entity)] = evt.newentity; | this.assignedAgainst[against][this.assignedAgainst[against].indexOf(evt.entity)] = evt.newentity; | ||||
} | } | ||||
} | } | ||||
} | |||||
for (let evt of events.Garrison) | for (let evt of events.Garrison) | ||||
this.removeFoe(gameState, evt.entity); | this.removeFoe(gameState, evt.entity); | ||||
for (let evt of events.OwnershipChanged) // captured | for (let evt of events.OwnershipChanged) // captured | ||||
{ | |||||
if (!gameState.isPlayerEnemy(evt.to)) | if (!gameState.isPlayerEnemy(evt.to)) | ||||
this.removeFoe(gameState, evt.entity); | this.removeFoe(gameState, evt.entity); | ||||
else if (evt.from === PlayerID) | else if (evt.from === PlayerID) | ||||
this.removeOwn(gameState, evt.entity); | this.removeOwn(gameState, evt.entity); | ||||
} | |||||
for (let evt of events.Destroy) | for (let evt of events.Destroy) | ||||
{ | { | ||||
let entityObj = evt.entityObj || undefined; | let entityObj = evt.entityObj || undefined; | ||||
// we may have capture+destroy, so do not trust owner and check all possibilities | // we may have capture+destroy, so do not trust owner and check all possibilities | ||||
this.removeOwn(gameState, evt.entity, entityObj); | this.removeOwn(gameState, evt.entity, entityObj); | ||||
this.removeFoe(gameState, evt.entity, entityObj); | this.removeFoe(gameState, evt.entity, entityObj); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
m.DefenseArmy.prototype.Deserialize = function(data) | m.DefenseArmy.prototype.Deserialize = function(data) | ||||
{ | { | ||||
for (let key in data) | for (let key in data) | ||||
this[key] = data[key]; | this[key] = data[key]; | ||||
}; | }; | ||||
return m; | }(PETRA)); | ||||
}(PETRA); |
Wildfire Games · Phabricator