Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/petra/navalManager.js
Show All 32 Lines | |||||||||
/** More initialisation for stuff that needs the gameState */ | /** More initialisation for stuff that needs the gameState */ | ||||||||
PETRA.NavalManager.prototype.init = function(gameState, deserializing) | PETRA.NavalManager.prototype.init = function(gameState, deserializing) | ||||||||
{ | { | ||||||||
// docks | // docks | ||||||||
this.docks = gameState.getOwnStructures().filter(API3.Filters.byClasses(["Dock", "Shipyard"])); | this.docks = gameState.getOwnStructures().filter(API3.Filters.byClasses(["Dock", "Shipyard"])); | ||||||||
this.docks.registerUpdates(); | this.docks.registerUpdates(); | ||||||||
this.ships = gameState.getOwnUnits().filter(API3.Filters.and(API3.Filters.byClass("Ship"), API3.Filters.not(API3.Filters.byMetadata(PlayerID, "role", "trader")))); | this.ships = gameState.getOwnUnits().filter(API3.Filters.and(API3.Filters.byClass("Ship"), API3.Filters.not(API3.Filters.byMetadata(PlayerID, "role", PETRA.Worker.ROLE_TRADER)))); | ||||||||
// note: those two can overlap (some transport ships are warships too and vice-versa). | // note: those two can overlap (some transport ships are warships too and vice-versa). | ||||||||
this.transportShips = this.ships.filter(API3.Filters.and(API3.Filters.byCanGarrison(), API3.Filters.not(API3.Filters.byClass("FishingBoat")))); | this.transportShips = this.ships.filter(API3.Filters.and(API3.Filters.byCanGarrison(), API3.Filters.not(API3.Filters.byClass("FishingBoat")))); | ||||||||
this.warShips = this.ships.filter(API3.Filters.byClass("Warship")); | this.warShips = this.ships.filter(API3.Filters.byClass("Warship")); | ||||||||
this.fishShips = this.ships.filter(API3.Filters.byClass("FishingBoat")); | this.fishShips = this.ships.filter(API3.Filters.byClass("FishingBoat")); | ||||||||
this.ships.registerUpdates(); | this.ships.registerUpdates(); | ||||||||
this.transportShips.registerUpdates(); | this.transportShips.registerUpdates(); | ||||||||
this.warShips.registerUpdates(); | this.warShips.registerUpdates(); | ||||||||
▲ Show 20 Lines • Show All 230 Lines • ▼ Show 20 Lines | if (!evt.entityObj.hasClass("Ship") || !evt.metadata[PlayerID].transporter) | ||||||||
continue; | continue; | ||||||||
let plan = this.getPlan(evt.metadata[PlayerID].transporter); | let plan = this.getPlan(evt.metadata[PlayerID].transporter); | ||||||||
if (!plan) | if (!plan) | ||||||||
continue; | continue; | ||||||||
let shipId = evt.entityObj.id(); | let shipId = evt.entityObj.id(); | ||||||||
if (this.Config.debug > 1) | if (this.Config.debug > 1) | ||||||||
API3.warn("one ship " + shipId + " from plan " + plan.ID + " destroyed during " + plan.state); | API3.warn("one ship " + shipId + " from plan " + plan.ID + " destroyed during " + plan.state); | ||||||||
if (plan.state == "boarding") | if (plan.state == PETRA.TransportPlan.BOARDING) | ||||||||
{ | { | ||||||||
// just reset the units onBoard metadata and wait for a new ship to be assigned to this plan | // just reset the units onBoard metadata and wait for a new ship to be assigned to this plan | ||||||||
plan.units.forEach(ent => { | plan.units.forEach(ent => { | ||||||||
if (ent.getMetadata(PlayerID, "onBoard") == "onBoard" && ent.position() || | if (ent.getMetadata(PlayerID, "onBoard") == "onBoard" && ent.position() || | ||||||||
ent.getMetadata(PlayerID, "onBoard") == shipId) | ent.getMetadata(PlayerID, "onBoard") == shipId) | ||||||||
ent.setMetadata(PlayerID, "onBoard", undefined); | ent.setMetadata(PlayerID, "onBoard", undefined); | ||||||||
}); | }); | ||||||||
plan.needTransportShips = !plan.transportShips.hasEntities(); | plan.needTransportShips = !plan.transportShips.hasEntities(); | ||||||||
} | } | ||||||||
else if (plan.state == "sailing") | else if (plan.state == PETRA.TransportPlan.SAILING) | ||||||||
{ | { | ||||||||
let endIndex = plan.endIndex; | let endIndex = plan.endIndex; | ||||||||
for (let ent of plan.units.values()) | for (let ent of plan.units.values()) | ||||||||
{ | { | ||||||||
if (!ent.position()) // unit from another ship of this plan ... do nothing | if (!ent.position()) // unit from another ship of this plan ... do nothing | ||||||||
continue; | continue; | ||||||||
let access = PETRA.getLandAccess(gameState, ent); | let access = PETRA.getLandAccess(gameState, ent); | ||||||||
let endPos = ent.getMetadata(PlayerID, "endPos"); | let endPos = ent.getMetadata(PlayerID, "endPos"); | ||||||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | if (ent.getMetadata(PlayerID, "transport") !== undefined) | ||||||||
if (this.Config.debug > 0) | if (this.Config.debug > 0) | ||||||||
API3.warn("Petra naval manager error: unit " + ent.id() + " has already required a transport"); | API3.warn("Petra naval manager error: unit " + ent.id() + " has already required a transport"); | ||||||||
return false; | return false; | ||||||||
} | } | ||||||||
let plans = []; | let plans = []; | ||||||||
for (let plan of this.transportPlans) | for (let plan of this.transportPlans) | ||||||||
{ | { | ||||||||
if (plan.startIndex != startIndex || plan.endIndex != endIndex || plan.state != "boarding") | if (plan.startIndex != startIndex || plan.endIndex != endIndex || plan.state != PETRA.TransportPlan.BOARDING) | ||||||||
continue; | continue; | ||||||||
// Limit the number of siege units per transport to avoid problems when ungarrisoning | // Limit the number of siege units per transport to avoid problems when ungarrisoning | ||||||||
if (PETRA.isSiegeUnit(ent) && plan.units.filter(unit => PETRA.isSiegeUnit(unit)).length > 3) | if (PETRA.isSiegeUnit(ent) && plan.units.filter(unit => PETRA.isSiegeUnit(unit)).length > 3) | ||||||||
continue; | continue; | ||||||||
plans.push(plan); | plans.push(plan); | ||||||||
} | } | ||||||||
if (plans.length) | if (plans.length) | ||||||||
▲ Show 20 Lines • Show All 235 Lines • ▼ Show 20 Lines | PETRA.NavalManager.prototype.moveApart = function(gameState) | ||||||||
} | } | ||||||||
for (let ship of gameState.ai.HQ.tradeManager.traders.filter(API3.Filters.byClass("Ship")).values()) | for (let ship of gameState.ai.HQ.tradeManager.traders.filter(API3.Filters.byClass("Ship")).values()) | ||||||||
{ | { | ||||||||
let shipPosition = ship.position(); | let shipPosition = ship.position(); | ||||||||
if (!shipPosition) | if (!shipPosition) | ||||||||
continue; | continue; | ||||||||
let role = ship.getMetadata(PlayerID, "role"); | let role = ship.getMetadata(PlayerID, "role"); | ||||||||
if (!role || role != "trader") // already accounted before | if (!role || role != PETRA.Worker.ROLE_TRADER) // already accounted before | ||||||||
FreagarachUnsubmitted Not Done Inline Actions
Freagarach: | |||||||||
continue; | continue; | ||||||||
let unitAIState = ship.unitAIState(); | let unitAIState = ship.unitAIState(); | ||||||||
if (unitAIState == "INDIVIDUAL.TRADE.APPROACHINGMARKET") | if (unitAIState == "INDIVIDUAL.TRADE.APPROACHINGMARKET") | ||||||||
{ | { | ||||||||
let previousPosition = ship.getMetadata(PlayerID, "previousPosition"); | let previousPosition = ship.getMetadata(PlayerID, "previousPosition"); | ||||||||
if (!previousPosition || previousPosition[0] != shipPosition[0] || | if (!previousPosition || previousPosition[0] != shipPosition[0] || | ||||||||
previousPosition[1] != shipPosition[1]) | previousPosition[1] != shipPosition[1]) | ||||||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | for (let ship of blockedShips) | ||||||||
for (let blockingShip of gameState.ai.HQ.tradeManager.traders.filter(API3.Filters.byClass("Ship")).values()) | for (let blockingShip of gameState.ai.HQ.tradeManager.traders.filter(API3.Filters.byClass("Ship")).values()) | ||||||||
{ | { | ||||||||
if (blockingShip.getMetadata(PlayerID, "sea") != sea) | if (blockingShip.getMetadata(PlayerID, "sea") != sea) | ||||||||
continue; | continue; | ||||||||
if (blockedIds.indexOf(blockingShip.id()) != -1 || !blockingShip.position()) | if (blockedIds.indexOf(blockingShip.id()) != -1 || !blockingShip.position()) | ||||||||
continue; | continue; | ||||||||
let role = blockingShip.getMetadata(PlayerID, "role"); | let role = blockingShip.getMetadata(PlayerID, "role"); | ||||||||
if (!role || role != "trader") // already accounted before | if (!role || role != PETRA.Worker.ROLE_TRADER) // already accounted before | ||||||||
Not Done Inline Actions
Freagarach: | |||||||||
continue; | continue; | ||||||||
let distSquare = API3.SquareVectorDistance(shipPosition, blockingShip.position()); | let distSquare = API3.SquareVectorDistance(shipPosition, blockingShip.position()); | ||||||||
let unitAIState = blockingShip.unitAIState(); | let unitAIState = blockingShip.unitAIState(); | ||||||||
if (unitAIState != "INDIVIDUAL.TRADE.APPROACHINGMARKET") | if (unitAIState != "INDIVIDUAL.TRADE.APPROACHINGMARKET") | ||||||||
{ | { | ||||||||
if (distSquare < 1600) | if (distSquare < 1600) | ||||||||
blockingShip.moveToRange(shipPosition[0], shipPosition[1], 40, 45); | blockingShip.moveToRange(shipPosition[0], shipPosition[1], 40, 45); | ||||||||
} | } | ||||||||
▲ Show 20 Lines • Show All 186 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator