Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/defenseManager.js
Show First 20 Lines • Show All 732 Lines • ▼ Show 20 Lines | m.DefenseManager.prototype.checkEvents = function(gameState, events) | ||||
} | } | ||||
}; | }; | ||||
m.DefenseManager.prototype.garrisonUnitsInside = function(gameState, target, data) | m.DefenseManager.prototype.garrisonUnitsInside = function(gameState, target, data) | ||||
{ | { | ||||
if (target.hitpoints() < target.garrisonEjectHealth() * target.maxHitpoints()) | if (target.hitpoints() < target.garrisonEjectHealth() * target.maxHitpoints()) | ||||
return false; | return false; | ||||
let minGarrison = data.min || target.garrisonMax(); | let minGarrison = data.min || target.garrisonMax(); | ||||
if (gameState.ai.HQ.garrisonManager.numberOfGarrisonedUnits(target) >= minGarrison) | if (gameState.ai.HQ.garrisonManager.numberOfGarrisonedSlots(target) >= minGarrison) | ||||
return false; | return false; | ||||
if (data.attacker) | if (data.attacker) | ||||
{ | { | ||||
let attackTypes = target.attackTypes(); | let attackTypes = target.attackTypes(); | ||||
if (!attackTypes || attackTypes.indexOf("Ranged") == -1) | if (!attackTypes || attackTypes.indexOf("Ranged") == -1) | ||||
return false; | return false; | ||||
let dist = API3.SquareVectorDistance(data.attacker.position(), target.position()); | let dist = API3.SquareVectorDistance(data.attacker.position(), target.position()); | ||||
let range = target.attackRange("Ranged").max; | let range = target.attackRange("Ranged").max; | ||||
Show All 35 Lines | let units = gameState.getOwnUnits().filter(ent => { | ||||
if (m.getLandAccess(gameState, ent) != access) | if (m.getLandAccess(gameState, ent) != access) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
}).filterNearest(target.position()); | }).filterNearest(target.position()); | ||||
let ret = false; | let ret = false; | ||||
for (let ent of units.values()) | for (let ent of units.values()) | ||||
{ | { | ||||
if (garrisonManager.numberOfGarrisonedUnits(target) >= minGarrison) | if (garrisonManager.numberOfGarrisonedSlots(target) >= minGarrison) | ||||
break; | break; | ||||
if (ent.getMetadata(PlayerID, "plan") !== undefined && ent.getMetadata(PlayerID, "plan") >= 0) | if (ent.getMetadata(PlayerID, "plan") !== undefined && ent.getMetadata(PlayerID, "plan") >= 0) | ||||
{ | { | ||||
let attackPlan = gameState.ai.HQ.attackManager.getPlan(ent.getMetadata(PlayerID, "plan")); | let attackPlan = gameState.ai.HQ.attackManager.getPlan(ent.getMetadata(PlayerID, "plan")); | ||||
if (attackPlan) | if (attackPlan) | ||||
attackPlan.removeUnit(ent, true); | attackPlan.removeUnit(ent, true); | ||||
} | } | ||||
let army = ent.getMetadata(PlayerID, "PartOfArmy") ? this.getArmy(ent.getMetadata(PlayerID, "PartOfArmy")) : undefined; | let army = ent.getMetadata(PlayerID, "PartOfArmy") ? this.getArmy(ent.getMetadata(PlayerID, "PartOfArmy")) : undefined; | ||||
Show All 13 Lines | m.DefenseManager.prototype.garrisonSiegeUnit = function(gameState, unit) | ||||
let unitAccess = m.getLandAccess(gameState, unit); | let unitAccess = m.getLandAccess(gameState, unit); | ||||
let garrisonManager = gameState.ai.HQ.garrisonManager; | let garrisonManager = gameState.ai.HQ.garrisonManager; | ||||
for (let ent of gameState.getAllyStructures().values()) | for (let ent of gameState.getAllyStructures().values()) | ||||
{ | { | ||||
if (!ent.isGarrisonHolder()) | if (!ent.isGarrisonHolder()) | ||||
continue; | continue; | ||||
if (!MatchesClassList(unit.classes(), ent.garrisonableClasses())) | if (!MatchesClassList(unit.classes(), ent.garrisonableClasses())) | ||||
continue; | continue; | ||||
if (garrisonManager.numberOfGarrisonedUnits(ent) >= ent.garrisonMax()) | if (garrisonManager.numberOfGarrisonedSlots(ent) >= ent.garrisonMax()) | ||||
continue; | continue; | ||||
if (ent.hitpoints() < ent.garrisonEjectHealth() * ent.maxHitpoints()) | if (ent.hitpoints() < ent.garrisonEjectHealth() * ent.maxHitpoints()) | ||||
continue; | continue; | ||||
if (m.getLandAccess(gameState, ent) != unitAccess) | if (m.getLandAccess(gameState, ent) != unitAccess) | ||||
continue; | continue; | ||||
let dist = API3.SquareVectorDistance(ent.position(), unit.position()); | let dist = API3.SquareVectorDistance(ent.position(), unit.position()); | ||||
if (dist > distmin) | if (dist > distmin) | ||||
continue; | continue; | ||||
Show All 19 Lines | m.DefenseManager.prototype.garrisonAttackedUnit = function(gameState, unit, emergency = false) | ||||
for (let ent of gameState.getAllyStructures().values()) | for (let ent of gameState.getAllyStructures().values()) | ||||
{ | { | ||||
if (!ent.isGarrisonHolder()) | if (!ent.isGarrisonHolder()) | ||||
continue; | continue; | ||||
if (!emergency && !ent.buffHeal()) | if (!emergency && !ent.buffHeal()) | ||||
continue; | continue; | ||||
if (!MatchesClassList(unit.classes(), ent.garrisonableClasses())) | if (!MatchesClassList(unit.classes(), ent.garrisonableClasses())) | ||||
continue; | continue; | ||||
if (garrisonManager.numberOfGarrisonedUnits(ent) >= ent.garrisonMax() && | if (garrisonManager.numberOfGarrisonedSlots(ent) >= ent.garrisonMax() && | ||||
(!emergency || !ent.garrisoned().length)) | (!emergency || !ent.garrisoned().length)) | ||||
continue; | continue; | ||||
if (ent.hitpoints() < ent.garrisonEjectHealth() * ent.maxHitpoints()) | if (ent.hitpoints() < ent.garrisonEjectHealth() * ent.maxHitpoints()) | ||||
continue; | continue; | ||||
if (m.getLandAccess(gameState, ent) != unitAccess) | if (m.getLandAccess(gameState, ent) != unitAccess) | ||||
continue; | continue; | ||||
let dist = API3.SquareVectorDistance(ent.position(), unit.position()); | let dist = API3.SquareVectorDistance(ent.position(), unit.position()); | ||||
if (dist > distmin) | if (dist > distmin) | ||||
continue; | continue; | ||||
distmin = dist; | distmin = dist; | ||||
nearest = ent; | nearest = ent; | ||||
} | } | ||||
if (!nearest) | if (!nearest) | ||||
return false; | return false; | ||||
if (!emergency) | if (!emergency) | ||||
{ | { | ||||
garrisonManager.garrison(gameState, unit, nearest, "protection"); | garrisonManager.garrison(gameState, unit, nearest, "protection"); | ||||
return true; | return true; | ||||
} | } | ||||
if (garrisonManager.numberOfGarrisonedUnits(nearest) >= nearest.garrisonMax()) // make room for this ent | if (garrisonManager.numberOfGarrisonedSlots(nearest) >= nearest.garrisonMax()) // make room for this ent | ||||
nearest.unload(nearest.garrisoned()[0]); | nearest.unload(nearest.garrisoned()[0]); | ||||
garrisonManager.garrison(gameState, unit, nearest, nearest.buffHeal() ? "protection" : "emergency"); | garrisonManager.garrison(gameState, unit, nearest, nearest.buffHeal() ? "protection" : "emergency"); | ||||
return true; | return true; | ||||
}; | }; | ||||
/** | /** | ||||
* Be more inclined to help an ally attacked by several enemies | * Be more inclined to help an ally attacked by several enemies | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator