petra fixes to reduce cases of traffic jams on naval maps

This was SVN commit r21533.
This commit is contained in:
mimo 2018-03-12 21:50:47 +00:00
parent f590c6ab40
commit df79180fa6
3 changed files with 58 additions and 28 deletions

View File

@ -428,9 +428,12 @@ m.dumpEntity = function(ent)
" subrole " + ent.getMetadata(PlayerID, "subrole"));
API3.warn("owner " + ent.owner() + " health " + ent.hitpoints() + " healthMax " + ent.maxHitpoints() +
" foundationProgress " + ent.foundationProgress());
API3.warn(" garrisoning " + ent.getMetadata(PlayerID, "garrisoning") + " garrisonHolder " + ent.getMetadata(PlayerID, "garrisonHolder") +
" plan " + ent.getMetadata(PlayerID, "plan") + " transport " + ent.getMetadata(PlayerID, "transport") +
" gather-type " + ent.getMetadata(PlayerID, "gather-type") + " target-foundation " + ent.getMetadata(PlayerID, "target-foundation") +
API3.warn(" garrisoning " + ent.getMetadata(PlayerID, "garrisoning") +
" garrisonHolder " + ent.getMetadata(PlayerID, "garrisonHolder") +
" plan " + ent.getMetadata(PlayerID, "plan") + " transport " + ent.getMetadata(PlayerID, "transport"));
API3.warn(" stance " + ent.getStance() + " transporter " + ent.getMetadata(PlayerID, "transporter") +
" gather-type " + ent.getMetadata(PlayerID, "gather-type") +
" target-foundation " + ent.getMetadata(PlayerID, "target-foundation") +
" PartOfArmy " + ent.getMetadata(PlayerID, "PartOfArmy"));
};

View File

@ -577,7 +577,7 @@ m.NavalManager.prototype.moveApart = function(gameState)
// New transport ships receive boarding commands only on the following turn.
if (gameState.ai.playedTurn < ship.getMetadata(PlayerID, "turnPreviousPosition") + 2)
continue;
ship.moveToRange(shipPosition[0], shipPosition[1], 30, 30);
ship.moveToRange(shipPosition[0] + randFloat(-1, 1), shipPosition[1] + randFloat(-1, 1), 30, 30);
blockedShips.push(ship);
blockedIds.push(ship.id());
}
@ -634,7 +634,7 @@ m.NavalManager.prototype.moveApart = function(gameState)
// New transport ships receives boarding commands only on the following turn.
if (gameState.ai.playedTurn < ship.getMetadata(PlayerID, "turnPreviousPosition") + 2)
continue;
ship.moveToRange(shipPosition[0], shipPosition[1], 30, 30);
ship.moveToRange(shipPosition[0] + randFloat(-1, 1), shipPosition[1] + randFloat(-1, 1), 30, 30);
blockedShips.push(ship);
blockedIds.push(ship.id());
}

View File

@ -284,9 +284,9 @@ m.TransportPlan.prototype.cancelTransport = function(gameState)
m.TransportPlan.prototype.update = function(gameState)
{
if (this.state === "boarding")
if (this.state == "boarding")
this.onBoarding(gameState);
else if (this.state === "sailing")
else if (this.state == "sailing")
this.onSailing(gameState);
return this.units.length;
@ -297,6 +297,7 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
let ready = true;
let time = gameState.ai.elapsedTime;
let shipTested = {};
for (let ent of this.units.values())
{
if (!ent.getMetadata(PlayerID, "onBoard"))
@ -329,26 +330,52 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
continue;
}
let distShip = API3.SquareVectorDistance(this.boardingPos[shipId], ship.position());
if (time - ship.getMetadata(PlayerID, "timeGarrison") > 8 && distShip > this.boardingRange)
if (!shipTested[shipId] && distShip > this.boardingRange)
{
if (shipTested[shipId])
continue;
shipTested[shipId] = true;
if (!this.nTry[shipId])
this.nTry[shipId] = 1;
else
++this.nTry[shipId];
if (this.nTry[shipId] > 1) // we must have been blocked by something ... try with another boarding point
let retry = false;
let unitAIState = ship.unitAIState();
if (unitAIState == "INDIVIDUAL.WALKING" ||
unitAIState == "INDIVIDUAL.PICKUP.APPROACHING")
{
this.nTry[shipId] = 0;
if (this.debug > 1)
API3.warn("ship " + shipId + " new attempt for a landing point ");
this.boardingPos[shipId] = this.getBoardingPos(gameState, ship, this.startIndex, this.sea, undefined, false);
if (time - ship.getMetadata(PlayerID, "timeGarrison") > 2)
{
let oldPos = ent.getMetadata(PlayerID, "posGarrison");
let newPos = ent.position();
if (oldPos[0] == newPos[0] && oldPos[1] == newPos[1])
retry = true;
ent.setMetadata(PlayerID, "posGarrison", newPos);
ent.setMetadata(PlayerID, "timeGarrison", time);
}
}
else if ((unitAIState != "INDIVIDUAL.PICKUP.LOADING" &&
time - ship.getMetadata(PlayerID, "timeGarrison") > 5) ||
time - ship.getMetadata(PlayerID, "timeGarrison") > 8)
{
retry = true;
ent.setMetadata(PlayerID, "timeGarrison", time);
}
if (retry)
{
if (!this.nTry[shipId])
this.nTry[shipId] = 1;
else
++this.nTry[shipId];
if (this.nTry[shipId] > 1) // we must have been blocked by something ... try with another boarding point
{
this.nTry[shipId] = 0;
if (this.debug > 1)
API3.warn("ship " + shipId + " new attempt for a landing point ");
this.boardingPos[shipId] = this.getBoardingPos(gameState, ship, this.startIndex, this.sea, undefined, false);
}
ship.move(this.boardingPos[shipId][0], this.boardingPos[shipId][1]);
ship.setMetadata(PlayerID, "timeGarrison", time);
}
ship.move(this.boardingPos[shipId][0], this.boardingPos[shipId][1]);
ship.setMetadata(PlayerID, "timeGarrison", time);
}
else if (time - ent.getMetadata(PlayerID, "timeGarrison") > 2)
if (time - ent.getMetadata(PlayerID, "timeGarrison") > 2)
{
let oldPos = ent.getMetadata(PlayerID, "posGarrison");
let newPos = ent.position();
@ -372,7 +399,7 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
ent.moveToRange(newPos[0], newPos[1], 30, 30);
ent.garrison(ship, true);
}
else // wait for the ship
else if (API3.SquareVectorDistance(this.boardingPos[shipId], newPos) > 225)
ent.moveToRange(this.boardingPos[shipId][0], this.boardingPos[shipId][1], 0, 15);
}
else
@ -409,7 +436,7 @@ m.TransportPlan.prototype.isOnBoard = function(ent)
{
for (let ship of this.transportShips.values())
{
if (ship.garrisoned().indexOf(ent.id()) === -1)
if (ship.garrisoned().indexOf(ent.id()) == -1)
continue;
ent.setMetadata(PlayerID, "onBoard", "onBoard");
return true;
@ -448,7 +475,7 @@ m.TransportPlan.prototype.getBoardingPos = function(gameState, ship, landIndex,
if (avoidEnnemy)
{
let territoryOwner = gameState.ai.HQ.territoryMap.getOwner(pos);
if (territoryOwner !== 0 && !gameState.isPlayerAlly(territoryOwner))
if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner))
dist += 100000000;
}
// require a small distance between all ships of the transport plan to avoid path finder problems
@ -531,7 +558,7 @@ m.TransportPlan.prototype.onSailing = function(gameState)
let ship = gameState.getEntityById(ent.getMetadata(PlayerID, "onBoard"));
if (ship)
{
if (ship.garrisoned().indexOf(entId) !== -1)
if (ship.garrisoned().indexOf(entId) != -1)
ent.setMetadata(PlayerID, "onBoard", "onBoard");
else
{
@ -603,7 +630,7 @@ m.TransportPlan.prototype.onSailing = function(gameState)
for (let ship of this.transportShips.values())
{
if (ship.unitAIState() === "INDIVIDUAL.WALKING")
if (ship.unitAIState() == "INDIVIDUAL.WALKING")
continue;
let shipId = ship.id();
let dist = API3.SquareVectorDistance(ship.position(), this.boardingPos[shipId]);
@ -624,7 +651,7 @@ m.TransportPlan.prototype.onSailing = function(gameState)
let recovering = 0;
for (let recov of this.recovered)
if (recov.shipId === shipId)
if (recov.shipId == shipId)
recovering++;
if (!remaining && !recovering) // when empty, release the ship and move apart to leave room for other ships. TODO fight