Differential D4343 Diff 19161 ps/trunk/binaries/data/mods/public/simulation/ai/petra/transportPlan.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/ai/petra/transportPlan.js
Show First 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | for (let ent of units) | ||||
ent.setMetadata(PlayerID, "transport", this.ID); | ent.setMetadata(PlayerID, "transport", this.ID); | ||||
ent.setMetadata(PlayerID, "endPos", endPos); | ent.setMetadata(PlayerID, "endPos", endPos); | ||||
} | } | ||||
if (this.debug > 1) | if (this.debug > 1) | ||||
API3.warn("Starting a new transport plan with ID " + this.ID + | API3.warn("Starting a new transport plan with ID " + this.ID + | ||||
" to index " + endIndex + " with units length " + units.length); | " to index " + endIndex + " with units length " + units.length); | ||||
this.state = "boarding"; | this.state = PETRA.TransportPlan.BOARDING; | ||||
this.boardingPos = {}; | this.boardingPos = {}; | ||||
this.needTransportShips = ship === undefined; | this.needTransportShips = ship === undefined; | ||||
this.nTry = {}; | this.nTry = {}; | ||||
return true; | return true; | ||||
}; | }; | ||||
/** | |||||
* We're trying to board units onto our ships. | |||||
*/ | |||||
PETRA.TransportPlan.BOARDING = "boarding"; | |||||
/** | |||||
* We're moving ships and eventually unload units. | |||||
*/ | |||||
PETRA.TransportPlan.SAILING = "sailing"; | |||||
PETRA.TransportPlan.prototype.init = function(gameState) | PETRA.TransportPlan.prototype.init = function(gameState) | ||||
{ | { | ||||
this.units = gameState.getOwnUnits().filter(API3.Filters.byMetadata(PlayerID, "transport", this.ID)); | this.units = gameState.getOwnUnits().filter(API3.Filters.byMetadata(PlayerID, "transport", this.ID)); | ||||
this.ships = gameState.ai.HQ.navalManager.ships.filter(API3.Filters.byMetadata(PlayerID, "transporter", this.ID)); | this.ships = gameState.ai.HQ.navalManager.ships.filter(API3.Filters.byMetadata(PlayerID, "transporter", this.ID)); | ||||
this.transportShips = gameState.ai.HQ.navalManager.transportShips.filter(API3.Filters.byMetadata(PlayerID, "transporter", this.ID)); | this.transportShips = gameState.ai.HQ.navalManager.transportShips.filter(API3.Filters.byMetadata(PlayerID, "transporter", this.ID)); | ||||
this.units.registerUpdates(); | this.units.registerUpdates(); | ||||
this.ships.registerUpdates(); | this.ships.registerUpdates(); | ||||
Show All 27 Lines | PETRA.TransportPlan.prototype.assignUnitToShip = function(gameState, ent) | ||||
for (let ship of this.transportShips.values()) | for (let ship of this.transportShips.values()) | ||||
{ | { | ||||
if (this.countFreeSlotsOnShip(ship) == 0) | if (this.countFreeSlotsOnShip(ship) == 0) | ||||
continue; | continue; | ||||
ent.setMetadata(PlayerID, "onBoard", ship.id()); | ent.setMetadata(PlayerID, "onBoard", ship.id()); | ||||
if (this.debug > 1) | if (this.debug > 1) | ||||
{ | { | ||||
if (ent.getMetadata(PlayerID, "role") == "attack") | if (ent.getMetadata(PlayerID, "role") === PETRA.Worker.ROLE_ATTACK) | ||||
Engine.PostCommand(PlayerID, { "type": "set-shading-color", "entities": [ent.id()], "rgb": [2, 0, 0] }); | Engine.PostCommand(PlayerID, { "type": "set-shading-color", "entities": [ent.id()], "rgb": [2, 0, 0] }); | ||||
else | else | ||||
Engine.PostCommand(PlayerID, { "type": "set-shading-color", "entities": [ent.id()], "rgb": [0, 2, 0] }); | Engine.PostCommand(PlayerID, { "type": "set-shading-color", "entities": [ent.id()], "rgb": [0, 2, 0] }); | ||||
} | } | ||||
return; | return; | ||||
} | } | ||||
if (this.flotilla) | if (this.flotilla) | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | if (ship.getMetadata(PlayerID, "transporter") != this.ID) | ||||
return; | return; | ||||
} | } | ||||
let defaultStance = ship.get("UnitAI/DefaultStance"); | let defaultStance = ship.get("UnitAI/DefaultStance"); | ||||
if (defaultStance) | if (defaultStance) | ||||
ship.setStance(defaultStance); | ship.setStance(defaultStance); | ||||
ship.setMetadata(PlayerID, "transporter", undefined); | ship.setMetadata(PlayerID, "transporter", undefined); | ||||
if (ship.getMetadata(PlayerID, "role") == "switchToTrader") | if (ship.getMetadata(PlayerID, "role") === PETRA.Worker.ROLE_SWITCH_TO_TRADER) | ||||
ship.setMetadata(PlayerID, "role", "trader"); | ship.setMetadata(PlayerID, "role", PETRA.Worker.ROLE_TRADER); | ||||
}; | }; | ||||
PETRA.TransportPlan.prototype.releaseAll = function() | PETRA.TransportPlan.prototype.releaseAll = function() | ||||
{ | { | ||||
for (let ship of this.ships.values()) | for (let ship of this.ships.values()) | ||||
this.releaseShip(ship); | this.releaseShip(ship); | ||||
for (let ent of this.units.values()) | for (let ent of this.units.values()) | ||||
Show All 32 Lines | PETRA.TransportPlan.prototype.cancelTransport = function(gameState) | ||||
this.endIndex = this.startIndex; | this.endIndex = this.startIndex; | ||||
this.endPos = base.anchor.position(); | this.endPos = base.anchor.position(); | ||||
this.canceled = true; | this.canceled = true; | ||||
return true; | return true; | ||||
}; | }; | ||||
/** | /** | ||||
* try to move on. There are two states: | * Try to move on and then clear the plan. | ||||
* - "boarding" means we're trying to board units onto our ships | |||||
* - "sailing" means we're moving ships and eventually unload units | |||||
* - then the plan is cleared | |||||
*/ | */ | ||||
PETRA.TransportPlan.prototype.update = function(gameState) | PETRA.TransportPlan.prototype.update = function(gameState) | ||||
{ | { | ||||
if (this.state == "boarding") | if (this.state === PETRA.TransportPlan.BOARDING) | ||||
this.onBoarding(gameState); | this.onBoarding(gameState); | ||||
else if (this.state == "sailing") | else if (this.state === PETRA.TransportPlan.SAILING) | ||||
this.onSailing(gameState); | this.onSailing(gameState); | ||||
return this.units.length; | return this.units.length; | ||||
}; | }; | ||||
PETRA.TransportPlan.prototype.onBoarding = function(gameState) | PETRA.TransportPlan.prototype.onBoarding = function(gameState) | ||||
{ | { | ||||
let ready = true; | let ready = true; | ||||
▲ Show 20 Lines • Show All 122 Lines • ▼ Show 20 Lines | if (!ready) | ||||
return; | return; | ||||
for (let ship of this.ships.values()) | for (let ship of this.ships.values()) | ||||
{ | { | ||||
this.boardingPos[ship.id()] = undefined; | this.boardingPos[ship.id()] = undefined; | ||||
this.boardingPos[ship.id()] = this.getBoardingPos(gameState, ship, this.endIndex, this.sea, this.endPos, true); | this.boardingPos[ship.id()] = this.getBoardingPos(gameState, ship, this.endIndex, this.sea, this.endPos, true); | ||||
ship.move(this.boardingPos[ship.id()][0], this.boardingPos[ship.id()][1]); | ship.move(this.boardingPos[ship.id()][0], this.boardingPos[ship.id()][1]); | ||||
} | } | ||||
this.state = "sailing"; | this.state = PETRA.TransportPlan.SAILING; | ||||
this.nTry = {}; | this.nTry = {}; | ||||
this.unloaded = []; | this.unloaded = []; | ||||
this.recovered = []; | this.recovered = []; | ||||
}; | }; | ||||
/** tell if a unit is garrisoned in one of the ships of this plan, and update its metadata if yes */ | /** tell if a unit is garrisoned in one of the ships of this plan, and update its metadata if yes */ | ||||
PETRA.TransportPlan.prototype.isOnBoard = function(ent) | PETRA.TransportPlan.prototype.isOnBoard = function(ent) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 290 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator