Petra: small changes in naval transport to (hopefully) make it more robust

This was SVN commit r15587.
This commit is contained in:
mimo 2014-07-30 21:36:48 +00:00
parent f85272e3de
commit cf121baa1d
3 changed files with 55 additions and 32 deletions

View File

@ -1442,16 +1442,12 @@ m.AttackPlan.prototype.Abort = function(gameState)
// If the attack was started, and we are on the same land as the rallyPoint, go back there
var rallyPoint = this.rallyPoint;
var withdrawal = (this.isStarted() && !this.overseas);
var self = this;
this.unitCollection.forEach(function(ent) {
ent.stopMoving();
if (withdrawal)
ent.move(rallyPoint[0], rallyPoint[1]);
if (ent.hasClass("CitizenSoldier") && ent.getMetadata(PlayerID, "role") !== "worker")
{
ent.setMetadata(PlayerID, "role", "worker");
ent.setMetadata(PlayerID, "subrole", undefined);
}
ent.setMetadata(PlayerID, "plan", -1);
self.removeUnit(ent);
});
}
@ -1465,6 +1461,16 @@ m.AttackPlan.prototype.Abort = function(gameState)
gameState.ai.queueManager.removeQueue("plan_" + this.name + "_siege");
};
m.AttackPlan.prototype.removeUnit = function(ent)
{
if (ent.hasClass("CitizenSoldier") && ent.getMetadata(PlayerID, "role") !== "worker")
{
ent.setMetadata(PlayerID, "role", "worker");
ent.setMetadata(PlayerID, "subrole", undefined);
}
ent.setMetadata(PlayerID, "plan", -1);
};
m.AttackPlan.prototype.checkEvents = function(gameState, events)
{
if (this.state === "unexecuted")

View File

@ -290,6 +290,7 @@ m.NavalManager.prototype.checkEvents = function(gameState, queues, events)
|| ent.getMetadata(PlayerID, "onBoard") === shipId)
ent.setMetadata(PlayerID, "onBoard", undefined);
});
plan.needTransportShips = (plan.transportShips.length === 0);
}
else if (plan.state === "sailing")
{
@ -352,7 +353,7 @@ m.NavalManager.prototype.requireTransport = function(gameState, entity, startInd
plan.addUnit(entity, endPos);
return true;
}
var plan = new m.TransportPlan(gameState, [entity], startIndex, endIndex, endPos, false);
var plan = new m.TransportPlan(gameState, [entity], startIndex, endIndex, endPos);
if (plan.failed)
{
if (this.Config.debug > 0)
@ -368,7 +369,7 @@ m.NavalManager.prototype.splitTransport = function(gameState, plan)
{
if (this.Config.debug > 0)
API3.warn(">>>> split of transport plan started <<<<");
var newplan = new m.TransportPlan(gameState, [], plan.startIndex, plan.endIndex, plan.endPos, false);
var newplan = new m.TransportPlan(gameState, [], plan.startIndex, plan.endIndex, plan.endPos);
if (newplan.failed)
{
if (this.Config.debug > 0)

View File

@ -149,15 +149,14 @@ m.TransportPlan.prototype.assignShip = function(gameState)
break;
}
}
if (nearest)
{
nearest.setMetadata(PlayerID, "transporter", this.ID);
this.ships.updateEnt(nearest);
this.transportShips.updateEnt(nearest);
this.needTransportShips = false;
return true;
}
return false;
if (!nearest)
return false;
nearest.setMetadata(PlayerID, "transporter", this.ID);
this.ships.updateEnt(nearest);
this.transportShips.updateEnt(nearest);
this.needTransportShips = false;
return true;
};
// add a unit to this plan
@ -274,7 +273,7 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
{
self.nTry[shipId] = 0;
if (self.debug > 0)
API3.warn(shipId + " new attempt for a landing point ");
API3.warn("ship " + shipId + " new attempt for a landing point ");
self.boardingPos[shipId] = self.getBoardingPos(gameState, self.startIndex, self.sea, undefined, false);
}
ship.move(self.boardingPos[shipId][0], self.boardingPos[shipId][1]);
@ -318,8 +317,8 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
if (!ready)
return;
this.ships.forEach(function (ship) { self.boardingPos[ship.id()] = undefined; });
this.ships.forEach(function (ship) {
self.boardingPos[ship.id()] = undefined;
self.boardingPos[ship.id()] = self.getBoardingPos(gameState, self.endIndex, self.sea, self.endPos, true);
ship.move(self.boardingPos[ship.id()][0], self.boardingPos[ship.id()][1]);
});
@ -333,19 +332,11 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
m.TransportPlan.prototype.isOnBoard = function(ent)
{
var ret = false;
var self = this;
this.transportShips.forEach(function (ship) {
if (ret || ship._entity.garrisoned.indexOf(ent.id()) === -1)
return;
ret = true;
ent.setMetadata(PlayerID, "onBoard", "onBoard");
if (self.debug > 0)
{
if (ent.getMetadata(PlayerID, "role") === "attack")
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [ent.id()], "rgb": [0,0,2]});
else
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [ent.id()], "rgb": [1,1,1]});
}
});
return ret;
};
@ -413,22 +404,35 @@ m.TransportPlan.prototype.onSailing = function(gameState)
var index = gameState.ai.accessibility.getAccessValue(ent.position());
if (gameState.ai.HQ.allowedRegions[index])
{
if (gameState.ai.HQ.Config.debug > 0)
API3.warn("recovered entity kept " + ent.id());
ent.setMetadata(PlayerID, "transport", undefined);
ent.setMetadata(PlayerID, "onBoard", undefined);
ent.setMetadata(PlayerID, "endPos", undefined);
if (gameState.ai.HQ.Config.debug > 0)
API3.warn("recovered entity kept " + ent.id());
// if from an army or attack, remove it
if (ent.getMetadata(PlayerID, "plan") >= 0)
{
var plan = gameState.ai.HQ.attackManager.getPlan(ent.getMetadata(PlayerID, "plan"));
plan.removeUnit(ent);
}
if (ent.getMetadata(PlayerID, "PartOfArmy"))
{
var army = gameState.ai.HQ.defenseManager.getArmy(ent.getMetadata(PlayerID, "PartOfArmy"));
army.removeOwn(gameState, ent.id());
}
}
else
{
if (gameState.ai.HQ.Config.debug > 0)
API3.warn("recovered entity destroyed " + ent.id());
ent.setMetadata(PlayerID, "transport", undefined);
ent.setMetadata(PlayerID, "onBoard", undefined);
ent.destroy();
}
}
this.recovered = [];
// Check that the units unloaded on the previous turn have been really unloaded
// Check that the units unloaded on the previous turn have been really unloaded and in the right position
var shipsToMove = {};
for (var entId of this.unloaded)
{
@ -445,12 +449,16 @@ m.TransportPlan.prototype.onSailing = function(gameState)
else
{
API3.warn("Petra transportPlan problem: unit not on ship without position ???");
ent.setMetadata(PlayerID, "transport", undefined);
ent.setMetadata(PlayerID, "onBoard", undefined);
ent.destroy();
}
}
else
{
API3.warn("Petra transportPlan problem: unit on ship, but no ship ???");
ent.setMetadata(PlayerID, "transport", undefined);
ent.setMetadata(PlayerID, "onBoard", undefined);
ent.destroy();
}
}
@ -471,6 +479,8 @@ m.TransportPlan.prototype.onSailing = function(gameState)
{
if (gameState.ai.HQ.Config.debug > 0)
API3.warn("no way ... we destroy it");
ent.setMetadata(PlayerID, "transport", undefined);
ent.setMetadata(PlayerID, "onBoard", undefined);
ent.destroy();
}
}
@ -490,14 +500,15 @@ m.TransportPlan.prototype.onSailing = function(gameState)
if (this.canceled)
{
this.ships.forEach(function (ship) { self.boardingPos[ship.id()] = undefined; });
this.ships.forEach(function (ship) {
self.boardingPos[ship.id()] = undefined;
self.boardingPos[ship.id()] = self.getBoardingPos(gameState, self.endIndex, self.sea, self.endPos, true);
ship.move(self.boardingPos[ship.id()][0], self.boardingPos[ship.id()][1]);
});
this.canceled = undefined;
}
var self = this;
this.transportShips.forEach(function (ship) {
if (ship.unitAIState() === "INDIVIDUAL.WALKING")
return;
@ -518,7 +529,12 @@ m.TransportPlan.prototype.onSailing = function(gameState)
}
}
if (remaining === 0) // when empty, release the ship and move apart to leave room for other ships. TODO fight
var recovering = 0;
for (var recov of self.recovered)
if (recov.shipId === shipId)
recovering++;
if (!remaining && !recovering) // when empty, release the ship and move apart to leave room for other ships. TODO fight
{
ship.moveApart(self.boardingPos[shipId], 15);
ship.setMetadata(PlayerID, "transporter", undefined);