a bit of cleanup in petra naval code

This was SVN commit r16799.
This commit is contained in:
mimo 2015-06-20 20:49:31 +00:00
parent 92832b1dc7
commit 30d96af5a2
2 changed files with 134 additions and 138 deletions

View File

@ -67,7 +67,7 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
availableFishes[sea] = fish.resourceSupplyAmount();
}
for (var i = 0; i < gameState.ai.accessibility.regionSize.length; ++i)
for (let i = 0; i < gameState.ai.accessibility.regionSize.length; ++i)
{
if (!gameState.ai.HQ.navalRegions[i])
{
@ -84,16 +84,16 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
}
else
{
var collec = this.ships.filter(API3.Filters.byMetadata(PlayerID, "sea", i));
let collec = this.ships.filter(API3.Filters.byMetadata(PlayerID, "sea", i));
collec.registerUpdates();
this.seaShips.push(collec);
collec = this.transportShips.filter(API3.Filters.byMetadata(PlayerID, "sea", i));
collec.registerUpdates();
this.seaTransportShips.push(collec);
var collec = this.warShips.filter(API3.Filters.byMetadata(PlayerID, "sea", i));
collec = this.warShips.filter(API3.Filters.byMetadata(PlayerID, "sea", i));
collec.registerUpdates();
this.seaWarShips.push(collec);
var collec = this.fishShips.filter(API3.Filters.byMetadata(PlayerID, "sea", i));
collec = this.fishShips.filter(API3.Filters.byMetadata(PlayerID, "sea", i));
collec.registerUpdates();
this.seaFishShips.push(collec);
this.wantedTransportShips.push(0);
@ -110,12 +110,12 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
// determination of the possible landing zones
var width = gameState.getMap().width;
var length = width * gameState.getMap().height;
for (var i = 0; i < length; ++i)
for (let i = 0; i < length; ++i)
{
var land = gameState.ai.accessibility.landPassMap[i];
let land = gameState.ai.accessibility.landPassMap[i];
if (land < 2)
continue;
var naval = gameState.ai.accessibility.navalPassMap[i];
let naval = gameState.ai.accessibility.navalPassMap[i];
if (naval < 2)
continue;
if (!this.landingZones[land])
@ -125,15 +125,15 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
this.landingZones[land][naval].push(i);
}
// and keep only thoses with enough room around when possible
for (var land in this.landingZones)
for (let land in this.landingZones)
{
for (var sea in this.landingZones[land])
for (let sea in this.landingZones[land])
{
var nbmax = 0;
for (var i = 0; i < this.landingZones[land][sea].length; i++)
let nbmax = 0;
for (let i = 0; i < this.landingZones[land][sea].length; i++)
{
var j = this.landingZones[land][sea][i];
var nb = 0;
let j = this.landingZones[land][sea][i];
let nb = 0;
if (this.landingZones[land][sea].indexOf(j-1) !== -1)
nb++;
if (this.landingZones[land][sea].indexOf(j+1) !== -1)
@ -145,11 +145,11 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
if (nb > nbmax)
nbmax = nb;
}
var nbcut = Math.min(2, nbmax);
for (var i = 0; i < this.landingZones[land][sea].length; i++)
let nbcut = Math.min(2, nbmax);
for (let i = 0; i < this.landingZones[land][sea].length; i++)
{
var j = this.landingZones[land][sea][i];
var nb = 0;
let j = this.landingZones[land][sea][i];
let nb = 0;
if (this.landingZones[land][sea].indexOf(j-1) !== -1)
nb++;
if (this.landingZones[land][sea].indexOf(j+1) !== -1)
@ -165,13 +165,13 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
}
// load units and buildings from the config files
var civ = gameState.playerData.civ;
let civ = gameState.civ();
if (civ in this.Config.buildings.naval)
this.bNaval = this.Config.buildings.naval[civ];
else
this.bNaval = this.Config.buildings.naval['default'];
for (var i in this.bNaval)
for (let i in this.bNaval)
this.bNaval[i] = gameState.applyCiv(this.bNaval[i]);
if (deserializing)
@ -186,7 +186,7 @@ m.NavalManager.prototype.init = function(gameState, deserializing)
m.NavalManager.prototype.resetFishingBoats = function(gameState)
{
for (var i = 0; i < gameState.ai.accessibility.regionSize.length; ++i)
for (let i = 0; i < gameState.ai.accessibility.regionSize.length; ++i)
this.wantedFishShips[i] = 0;
};
@ -222,9 +222,9 @@ m.NavalManager.prototype.getDockIndex = function(gameState, dock, onWater)
// pre-positioned docks are sometimes not well positionned
var dockPos = dock.position();
var radius = dock.footprintRadius();
for (var i = 0; i < 16; i++)
for (let i = 0; i < 16; i++)
{
var pos = [ dockPos[0] + radius*Math.cos(i*Math.PI/8), dockPos[1] + radius*Math.sin(i*Math.PI/8)];
let pos = [ dockPos[0] + radius*Math.cos(i*Math.PI/8), dockPos[1] + radius*Math.sin(i*Math.PI/8)];
index = gameState.ai.accessibility.getAccessValue(pos, onWater);
if (index >= 2)
@ -244,7 +244,7 @@ m.NavalManager.prototype.getUnconnectedSeas = function(gameState, region)
docks.forEach(function (dock) {
if (dock.getMetadata(PlayerID, "access") !== region)
return;
var i = seas.indexOf(dock.getMetadata(PlayerID, "sea"));
let i = seas.indexOf(dock.getMetadata(PlayerID, "sea"));
if (i !== -1)
seas.splice(i--,1);
});
@ -253,7 +253,6 @@ m.NavalManager.prototype.getUnconnectedSeas = function(gameState, region)
m.NavalManager.prototype.checkEvents = function(gameState, queues, events)
{
// TODO: probably check stuffs like a base destruction.
for (let evt of events["ConstructionFinished"])
{
if (!evt || !evt.newentity)
@ -306,8 +305,8 @@ m.NavalManager.prototype.checkEvents = function(gameState, queues, events)
plan.units.forEach(function (ent) {
if (!ent.position()) // unit from another ship of this plan ... do nothing
return;
var access = gameState.ai.accessibility.getAccessValue(ent.position());
var endPos = ent.getMetadata(PlayerID, "endPos");
let access = gameState.ai.accessibility.getAccessValue(ent.position());
let endPos = ent.getMetadata(PlayerID, "endPos");
ent.setMetadata(PlayerID, "transport", undefined);
ent.setMetadata(PlayerID, "onBoard", undefined);
ent.setMetadata(PlayerID, "endPos", undefined);
@ -358,7 +357,7 @@ m.NavalManager.prototype.requireTransport = function(gameState, entity, startInd
return false;
}
for (var plan of this.transportPlans)
for (let plan of this.transportPlans)
{
if (plan.startIndex !== startIndex || plan.endIndex !== endIndex)
continue
@ -367,7 +366,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);
let plan = new m.TransportPlan(gameState, [entity], startIndex, endIndex, endPos);
if (plan.failed)
{
if (this.Config.debug > 1)
@ -452,14 +451,14 @@ m.NavalManager.prototype.checkLevels = function(gameState, queues)
if (queues.ships.length() !== 0)
return;
for (var sea = 0; sea < this.neededTransportShips.length; sea++)
for (let sea = 0; sea < this.neededTransportShips.length; sea++)
this.neededTransportShips[sea] = 0;
for (var plan of this.transportPlans)
for (let plan of this.transportPlans)
{
if (!plan.needTransportShips || plan.units.length < 2)
continue;
var sea = plan.sea;
let sea = plan.sea;
if (gameState.countOwnQueuedEntitiesWithMetadata("sea", sea) > 0
|| this.seaTransportShips[sea].length < this.wantedTransportShips[sea])
continue;
@ -471,7 +470,7 @@ m.NavalManager.prototype.checkLevels = function(gameState, queues)
}
}
for (var sea = 0; sea < this.neededTransportShips.length; sea++)
for (let sea = 0; sea < this.neededTransportShips.length; sea++)
if (this.neededTransportShips[sea] > 2)
++this.wantedTransportShips[sea];
};
@ -504,7 +503,7 @@ m.NavalManager.prototype.maintainFleet = function(gameState, queues)
if (this.seaFishShips[sea].length < this.wantedFishShips[sea])
{
var template = this.getBestShip(gameState, sea, "fishing");
let template = this.getBestShip(gameState, sea, "fishing");
if (template)
{
queues.ships.addItem(new m.TrainingPlan(gameState, template, { "base": 0, "role": "worker", "sea": sea }, 1, 1));
@ -517,7 +516,7 @@ m.NavalManager.prototype.maintainFleet = function(gameState, queues)
// assigns free ships to plans that need some
m.NavalManager.prototype.assignShipsToPlans = function(gameState)
{
for (var plan of this.transportPlans)
for (let plan of this.transportPlans)
if (plan.needTransportShips)
plan.assignShip(gameState);
};
@ -618,19 +617,19 @@ m.NavalManager.prototype.buildNavalStructures = function(gameState, queues)
if (nNaval === 0 || (nNaval < this.bNaval.length && gameState.getPopulation() > 120))
{
for (var naval of this.bNaval)
for (let naval of this.bNaval)
{
if (gameState.countEntitiesAndQueuedByType(naval, true) < 1 && gameState.ai.HQ.canBuild(gameState, naval))
{
var land = [];
for (var base of gameState.ai.HQ.baseManagers)
let land = [];
for (let base of gameState.ai.HQ.baseManagers)
{
if (!base.anchor)
continue;
if (land.indexOf(base.accessIndex) === -1)
land.push(base.accessIndex);
}
var sea = docks.toEntityArray()[0].getMetadata(PlayerID, "sea");
let sea = docks.toEntityArray()[0].getMetadata(PlayerID, "sea");
queues.militaryBuilding.addItem(new m.ConstructionPlan(gameState, naval, { "land": land, "sea": sea }));
break;
}
@ -645,11 +644,11 @@ m.NavalManager.prototype.getBestShip = function(gameState, sea, goal)
var trainableShips = [];
gameState.getOwnTrainingFacilities().filter(API3.Filters.byMetadata(PlayerID, "sea", sea)).forEach(function(ent) {
var trainables = ent.trainableEntities(civ);
for (var trainable of trainables)
for (let trainable of trainables)
{
if (gameState.isDisabledTemplates(trainable))
continue;
var template = gameState.getTemplate(trainable);
let template = gameState.getTemplate(trainable);
if (template && template.hasClass("Ship") && trainableShips.indexOf(trainable) === -1)
trainableShips.push(trainable);
}
@ -659,17 +658,17 @@ m.NavalManager.prototype.getBestShip = function(gameState, sea, goal)
var bestShip = undefined;
var limits = gameState.getEntityLimits();
var current = gameState.getEntityCounts();
for (var trainable of trainableShips)
for (let trainable of trainableShips)
{
var template = gameState.getTemplate(trainable);
let template = gameState.getTemplate(trainable);
if (!template.available(gameState))
continue;
var category = template.trainingCategory();
let category = template.trainingCategory();
if (category && limits[category] && current[category] >= limits[category])
continue;
var arrows = +(template.getDefaultArrow() || 0);
let arrows = +(template.getDefaultArrow() || 0);
if (goal === "attack") // choose the maximum default arrows
{
if (best > arrows)
@ -678,7 +677,7 @@ m.NavalManager.prototype.getBestShip = function(gameState, sea, goal)
}
else if (goal === "transport") // choose the maximum capacity, with a bonus if arrows or if siege transport
{
var capacity = +(template.garrisonMax() || 0);
let capacity = +(template.garrisonMax() || 0);
if (capacity < 2)
continue;
capacity += 10*arrows;
@ -689,10 +688,8 @@ m.NavalManager.prototype.getBestShip = function(gameState, sea, goal)
best = capacity;
}
else if (goal === "fishing")
{
if (!template.hasClass("FishingBoat"))
continue;
}
bestShip = trainable;
}
return bestShip;
@ -705,16 +702,15 @@ m.NavalManager.prototype.update = function(gameState, queues, events)
this.checkEvents(gameState, queues, events);
// close previous transport plans if finished
for (var i = 0; i < this.transportPlans.length; ++i)
for (let i = 0; i < this.transportPlans.length; ++i)
{
var remaining = this.transportPlans[i].update(gameState);
if (remaining === 0)
{
if (this.Config.debug > 1)
API3.warn("no more units on transport plan " + this.transportPlans[i].ID);
this.transportPlans[i].releaseAll();
this.transportPlans.splice(i--, 1);
}
let remaining = this.transportPlans[i].update(gameState);
if (remaining)
continue;
if (this.Config.debug > 1)
API3.warn("no more units on transport plan " + this.transportPlans[i].ID);
this.transportPlans[i].releaseAll();
this.transportPlans.splice(i--, 1);
}
// assign free ships to plans which need them
this.assignShipsToPlans(gameState);

View File

@ -100,9 +100,9 @@ m.TransportPlan.prototype.countFreeSlotsOnShip = function(ship)
{
if (ship.hitpoints() < ship.garrisonEjectHealth() * ship.maxHitpoints())
return 0;
var occupied = ship.garrisoned().length
let occupied = ship.garrisoned().length
+ this.units.filter(API3.Filters.byMetadata(PlayerID, "onBoard", ship.id())).length;
return (ship.garrisonMax() - occupied);
return Math.max(ship.garrisonMax() - occupied, 0);
};
m.TransportPlan.prototype.assignUnitToShip = function(gameState, ent)
@ -110,26 +110,20 @@ m.TransportPlan.prototype.assignUnitToShip = function(gameState, ent)
if (this.needTransportShips)
return;
var self = this;
var done = false;
this.transportShips.forEach(function (ship) {
if (done)
return;
if (self.countFreeSlotsOnShip(ship) > 0)
for (let ship of this.transportShips.values())
{
if (this.countFreeSlotsOnShip(ship) == 0)
continue;
ent.setMetadata(PlayerID, "onBoard", ship.id());
if (this.debug > 1)
{
ent.setMetadata(PlayerID, "onBoard", ship.id());
done = true;
if (self.debug > 1)
{
if (ent.getMetadata(PlayerID, "role") === "attack")
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [ent.id()], "rgb": [2,0,0]});
else
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [ent.id()], "rgb": [0,2,0]});
}
if (ent.getMetadata(PlayerID, "role") === "attack")
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [ent.id()], "rgb": [2,0,0]});
else
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [ent.id()], "rgb": [0,2,0]});
}
});
if (done)
return;
}
if (this.flotilla)
this.needTransportShips = true;
@ -139,22 +133,24 @@ m.TransportPlan.prototype.assignUnitToShip = function(gameState, ent)
m.TransportPlan.prototype.assignShip = function(gameState)
{
var distmin = Math.min();
var nearest = undefined;
var pos = undefined;
var pos;
// choose a unit of this plan not yet assigned to a ship
this.units.forEach(function (ent) {
if (pos || ent.getMetadata(PlayerID, "onBoard") !== undefined || !ent.position())
return;
for (let ent of this.units.values())
{
if (!ent.position() || ent.getMetadata(PlayerID, "onBoard") !== undefined)
continue;
pos = ent.position();
});
break;
}
// and choose the nearest available ship from this unit
var distmin = Math.min();
var nearest;
gameState.ai.HQ.navalManager.seaTransportShips[this.sea].forEach(function (ship) {
if (ship.getMetadata(PlayerID, "transporter"))
return;
if (pos)
{
var dist = API3.SquareVectorDistance(pos, ship.position());
let dist = API3.SquareVectorDistance(pos, ship.position());
if (dist > distmin)
return;
distmin = dist;
@ -255,8 +251,8 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
self.assignUnitToShip(gameState, ent);
if (ent.getMetadata(PlayerID, "onBoard"))
{
var shipId = ent.getMetadata(PlayerID, "onBoard");
var ship = gameState.getEntityById(shipId);
let shipId = ent.getMetadata(PlayerID, "onBoard");
let ship = gameState.getEntityById(shipId);
if (!self.boardingPos[shipId])
{
self.boardingPos[shipId] = self.getBoardingPos(gameState, ship, self.startIndex, self.sea, ent.position(), false);
@ -271,13 +267,13 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
else if (ent.getMetadata(PlayerID, "onBoard") !== "onBoard" && !self.isOnBoard(ent))
{
ready = false;
var shipId = ent.getMetadata(PlayerID, "onBoard");
var ship = gameState.getEntityById(shipId);
let shipId = ent.getMetadata(PlayerID, "onBoard");
let ship = gameState.getEntityById(shipId);
if (!ship) // the ship must have been destroyed
ent.setMetadata(PlayerID, "onBoard", undefined);
else
{
var distShip = API3.SquareVectorDistance(self.boardingPos[shipId], ship.position());
let distShip = API3.SquareVectorDistance(self.boardingPos[shipId], ship.position());
if (time - ship.getMetadata(PlayerID, "timeGarrison") > 8 && distShip > 225)
{
if (!self.nTry[shipId])
@ -296,8 +292,8 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
}
else if (time - ent.getMetadata(PlayerID, "timeGarrison") > 2)
{
var oldPos = ent.getMetadata(PlayerID, "posGarrison");
var newPos = ent.position();
let oldPos = ent.getMetadata(PlayerID, "posGarrison");
let newPos = ent.position();
if (oldPos[0] === newPos[0] && oldPos[1] === newPos[1])
{
if (distShip < 225) // looks like we are blocked ... try to go out of this trap
@ -347,20 +343,25 @@ m.TransportPlan.prototype.onBoarding = function(gameState)
// tell if a unit is garrisoned in one of the ships of this plan, and update its metadata if yes
m.TransportPlan.prototype.isOnBoard = function(ent)
{
var ret = false;
this.transportShips.forEach(function (ship) {
if (ret || ship.garrisoned().indexOf(ent.id()) === -1)
return;
ret = true;
for (let ship of this.transportShips.values())
{
if (ship.garrisoned().indexOf(ent.id()) === -1)
continue;
ent.setMetadata(PlayerID, "onBoard", "onBoard");
});
return ret;
return true;
}
return false;
};
// when avoidEnnemy is true, we try to not board/unboard in ennemy territory
m.TransportPlan.prototype.getBoardingPos = function(gameState, ship, landIndex, seaIndex, destination, avoidEnnemy)
{
if (!gameState.ai.HQ.navalManager.landingZones[landIndex][seaIndex])
if (!gameState.ai.HQ.navalManager.landingZones[landIndex])
{
API3.warn(" >>> no landing zone for land " + landIndex);
return destination;
}
else if (!gameState.ai.HQ.navalManager.landingZones[landIndex][seaIndex])
{
API3.warn(" >>> no landing zone for land " + landIndex + " and sea " + seaIndex);
return destination;
@ -371,11 +372,11 @@ m.TransportPlan.prototype.getBoardingPos = function(gameState, ship, landIndex,
var posmin = destination;
var width = gameState.getMap().width;
var cell = gameState.getMap().cellSize;
for (var i of gameState.ai.HQ.navalManager.landingZones[landIndex][seaIndex])
for (let i of gameState.ai.HQ.navalManager.landingZones[landIndex][seaIndex])
{
var pos = [i%width+0.5, Math.floor(i/width)+0.5];
let pos = [i%width+0.5, Math.floor(i/width)+0.5];
pos = [cell*pos[0], cell*pos[1]];
var dist = API3.SquareVectorDistance(startPos, pos);
let dist = API3.SquareVectorDistance(startPos, pos);
if (destination)
dist += API3.SquareVectorDistance(pos, destination);
if (avoidEnnemy)
@ -403,17 +404,15 @@ m.TransportPlan.prototype.getBoardingPos = function(gameState, ship, landIndex,
m.TransportPlan.prototype.onSailing = function(gameState)
{
var self = this;
// Check that the units recovered on the previous turn have been reloaded
for (var recov of this.recovered)
for (let recov of this.recovered)
{
var ent = gameState.getEntityById(recov.entId);
let ent = gameState.getEntityById(recov.entId);
if (!ent) // entity destroyed
continue;
if (!ent.position()) // reloading succeeded ... move a bit the ship before trying again
{
var ship = gameState.getEntityById(recov.shipId);
let ship = gameState.getEntityById(recov.shipId);
if (ship)
ship.moveApart(recov.entPos, 15);
continue;
@ -421,7 +420,7 @@ m.TransportPlan.prototype.onSailing = function(gameState)
if (this.debug > 1)
API3.warn(">>> transport " + this.ID + " reloading failed ... <<<");
// destroy the unit if inaccessible otherwise leave it there
var index = gameState.ai.accessibility.getAccessValue(ent.position());
let index = gameState.ai.accessibility.getAccessValue(ent.position());
if (gameState.ai.HQ.landRegions[index])
{
if (this.debug > 1)
@ -443,14 +442,14 @@ m.TransportPlan.prototype.onSailing = function(gameState)
// 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)
for (let entId of this.unloaded)
{
var ent = gameState.getEntityById(entId);
let ent = gameState.getEntityById(entId);
if (!ent) // entity destroyed
continue;
else if (!ent.position()) // unloading failed
{
var ship = gameState.getEntityById(ent.getMetadata(PlayerID, "onBoard"));
let ship = gameState.getEntityById(ent.getMetadata(PlayerID, "onBoard"));
if (ship)
{
if (ship.garrisoned().indexOf(entId) !== -1)
@ -474,7 +473,7 @@ m.TransportPlan.prototype.onSailing = function(gameState)
// unit unloaded on a wrong region - try to regarrison it and move a bit the ship
if (this.debug > 1)
API3.warn(">>> unit unloaded on a wrong region ! try to garrison it again <<<");
var ship = gameState.getEntityById(ent.getMetadata(PlayerID, "onBoard"));
let ship = gameState.getEntityById(ent.getMetadata(PlayerID, "onBoard"));
if (ship && !this.canceled)
{
shipsToMove[ship.id()] = ship;
@ -497,7 +496,7 @@ m.TransportPlan.prototype.onSailing = function(gameState)
ent.setMetadata(PlayerID, "endPos", undefined);
}
}
for (var shipId in shipsToMove)
for (let shipId in shipsToMove)
{
this.boardingPos[shipId] = this.getBoardingPos(gameState, shipsToMove[shipId], this.endIndex, this.sea, this.endPos, true);
shipsToMove[shipId].move(this.boardingPos[shipId][0], this.boardingPos[shipId][1]);
@ -506,65 +505,66 @@ m.TransportPlan.prototype.onSailing = function(gameState)
if (this.canceled)
{
this.ships.forEach(function (ship) {
self.boardingPos[ship.id()] = undefined;
self.boardingPos[ship.id()] = self.getBoardingPos(gameState, ship, self.endIndex, self.sea, self.endPos, true);
ship.move(self.boardingPos[ship.id()][0], self.boardingPos[ship.id()][1]);
});
for (let ship of this.ships.values())
{
this.boardingPos[ship.id()] = undefined;
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]);
}
this.canceled = undefined;
}
var self = this;
this.transportShips.forEach(function (ship) {
for (let ship of this.transportShips.values())
{
if (ship.unitAIState() === "INDIVIDUAL.WALKING")
return;
continue;
var shipId = ship.id();
var dist = API3.SquareVectorDistance(ship.position(), self.boardingPos[shipId]);
var dist = API3.SquareVectorDistance(ship.position(), this.boardingPos[shipId]);
var remaining = 0;
for (var entId of ship.garrisoned())
for (let entId of ship.garrisoned())
{
var ent = gameState.getEntityById(entId);
let ent = gameState.getEntityById(entId);
if (!ent.getMetadata(PlayerID, "transport"))
continue;
remaining++;
if (dist < 625)
{
ship.unload(entId);
self.unloaded.push(entId);
this.unloaded.push(entId);
ent.setMetadata(PlayerID, "onBoard", shipId);
}
}
var recovering = 0;
for (var recov of self.recovered)
for (let recov of this.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.moveApart(this.boardingPos[shipId], 15);
ship.setMetadata(PlayerID, "transporter", undefined);
if (ship.getMetadata(PlayerID, "role") === "switchToTrader")
ship.setMetadata(PlayerID, "role", "trader");
return;
continue;
}
if (dist > 225)
{
if (!self.nTry[shipId])
self.nTry[shipId] = 1;
if (!this.nTry[shipId])
this.nTry[shipId] = 1;
else
++self.nTry[shipId];
if (self.nTry[shipId] > 2) // we must have been blocked by something ... try with another boarding point
++this.nTry[shipId];
if (this.nTry[shipId] > 2) // we must have been blocked by something ... try with another boarding point
{
self.nTry[shipId] = 0;
if (self.debug > 1)
this.nTry[shipId] = 0;
if (this.debug > 1)
API3.warn(shipId + " new attempt for a landing point ");
self.boardingPos[shipId] = self.getBoardingPos(gameState, ship, self.endIndex, self.sea, undefined, true);
this.boardingPos[shipId] = this.getBoardingPos(gameState, ship, this.endIndex, this.sea, undefined, true);
}
ship.move(self.boardingPos[shipId][0], self.boardingPos[shipId][1]);
ship.move(this.boardingPos[shipId][0], this.boardingPos[shipId][1]);
}
});
}
};
m.TransportPlan.prototype.resetUnit = function(gameState, ent)
@ -575,13 +575,13 @@ m.TransportPlan.prototype.resetUnit = function(gameState, ent)
// if from an army or attack, remove it
if (ent.getMetadata(PlayerID, "plan") >= 0)
{
var attackPlan = gameState.ai.HQ.attackManager.getPlan(ent.getMetadata(PlayerID, "plan"));
let attackPlan = gameState.ai.HQ.attackManager.getPlan(ent.getMetadata(PlayerID, "plan"));
if (attackPlan)
attackPlan.removeUnit(ent, true);
}
if (ent.getMetadata(PlayerID, "PartOfArmy"))
{
var army = gameState.ai.HQ.defenseManager.getArmy(ent.getMetadata(PlayerID, "PartOfArmy"));
let army = gameState.ai.HQ.defenseManager.getArmy(ent.getMetadata(PlayerID, "PartOfArmy"));
if (army)
army.removeOwn(gameState, ent.id());
}