petra: fix and complete c1efebab15

This was SVN commit r16992.
This commit is contained in:
mimo 2015-09-07 18:14:50 +00:00
parent a40fdd52d9
commit bd355c531a
2 changed files with 25 additions and 18 deletions

View File

@ -1329,7 +1329,7 @@ m.AttackPlan.prototype.update = function(gameState, events)
{ // if units are attacked, abandon their target (if it was a structure or a support) and retaliate { // if units are attacked, abandon their target (if it was a structure or a support) and retaliate
// also if our unit is attacking a range unit and the attacker is a melee unit, retaliate // also if our unit is attacking a range unit and the attacker is a melee unit, retaliate
var orderData = ourUnit.unitAIOrderData(); var orderData = ourUnit.unitAIOrderData();
if (orderData.length !== 0 && orderData[0]["target"]) if (orderData && orderData.length && orderData[0]["target"])
{ {
var target = gameState.getEntityById(orderData[0]["target"]); var target = gameState.getEntityById(orderData[0]["target"]);
if (target && !target.hasClass("Structure") && !target.hasClass("Support")) if (target && !target.hasClass("Structure") && !target.hasClass("Support"))
@ -1462,14 +1462,14 @@ m.AttackPlan.prototype.update = function(gameState, events)
{ {
if (!maybeUpdate) if (!maybeUpdate)
{ {
this.CheckCapture(ent); this.CheckCapture(gameState, ent);
continue; continue;
} }
let deltat = (ent.unitAIState() === "INDIVIDUAL.COMBAT.APPROACHING") ? 10 : 5; let deltat = (ent.unitAIState() === "INDIVIDUAL.COMBAT.APPROACHING") ? 10 : 5;
let lastAttackPlanUpdateTime = ent.getMetadata(PlayerID, "lastAttackPlanUpdateTime"); let lastAttackPlanUpdateTime = ent.getMetadata(PlayerID, "lastAttackPlanUpdateTime");
if (lastAttackPlanUpdateTime && (time - lastAttackPlanUpdateTime) < deltat) if (lastAttackPlanUpdateTime && (time - lastAttackPlanUpdateTime) < deltat)
{ {
this.CheckCapture(ent); this.CheckCapture(gameState, ent);
continue; continue;
} }
} }
@ -1636,7 +1636,7 @@ m.AttackPlan.prototype.update = function(gameState, events)
this.unitCollection.forEach( function (unit) { this.unitCollection.forEach( function (unit) {
if (!unit.position()) if (!unit.position())
return; return;
if (unit.unitAIState().split(".")[1] !== "COMBAT" || unit.unitAIOrderData().length == 0 if (unit.unitAIState().split(".")[1] !== "COMBAT" || !unit.unitAIOrderData().length
|| !unit.unitAIOrderData()[0]["target"]) || !unit.unitAIOrderData()[0]["target"])
return; return;
var dist = API3.SquareVectorDistance(unit.position(), ent.position()); var dist = API3.SquareVectorDistance(unit.position(), ent.position());
@ -1787,22 +1787,22 @@ m.AttackPlan.prototype.debugAttack = function()
API3.warn("------------------------------"); API3.warn("------------------------------");
}; };
m.AttackPlan.prototype.CheckCapture = function(ent) m.AttackPlan.prototype.CheckCapture = function(gameState, ent)
{ {
let state = ent.unitAIState(); let state = ent.unitAIState();
if (!state || !state.split(".")[1] || state.split(".")[1] !== "COMBAT") if (!state || !state.split(".")[1] || state.split(".")[1] !== "COMBAT")
return; return;
let orderData = ent.unitAIOrderData(); let orderData = ent.unitAIOrderData();
if (!orderData || !orderData.target || !orderData.attackType || orderData.attackType !== "Capture") if (!orderData || !orderData.length || !orderData[0].target || !orderData[0].attackType || orderData[0].attackType !== "Capture")
return; return;
let target = gameState.getEntityById(orderData.target); let target = gameState.getEntityById(orderData[0].target);
if (!target) if (!target)
return; return;
// For the time being, do not try to capture rams // For the time being, do not try to capture rams
if (target.hasClass("Siege") && target.hasClass("Melee")) if (target.hasClass("Siege") && target.hasClass("Melee"))
{ {
ent.attack(target.id(), false); ent.attack(orderData[0].target, false);
return; return;
} }
@ -1814,7 +1814,7 @@ m.AttackPlan.prototype.CheckCapture = function(ent)
// but TODO need to know how many units are currently capturing this target // but TODO need to know how many units are currently capturing this target
// For the time being, we check on our army length // For the time being, we check on our army length
if (target.garrisoned() && target.garrisoned().length > Math.floor(this.unitCollection.length / 2)) if (target.garrisoned() && target.garrisoned().length > Math.floor(this.unitCollection.length / 2))
ent.attack(target.id(), false); ent.attack(orderData[0].target, false);
}; };
m.AttackPlan.prototype.Serialize = function() m.AttackPlan.prototype.Serialize = function()

View File

@ -25,9 +25,9 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID)
var distMin = undefined; var distMin = undefined;
var idMinAll = undefined; var idMinAll = undefined;
var distMinAll = undefined; var distMinAll = undefined;
for (var id of this.foeEntities) for (let id of this.foeEntities)
{ {
var eEnt = gameState.getEntityById(id); let eEnt = gameState.getEntityById(id);
if (!eEnt || !eEnt.position()) // probably can't happen. if (!eEnt || !eEnt.position()) // probably can't happen.
continue; continue;
@ -43,7 +43,7 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID)
|| (this.assignedAgainst[id].length > 5 && !eEnt.hasClass("Hero") && !eEnt.hasClass("Siege"))) || (this.assignedAgainst[id].length > 5 && !eEnt.hasClass("Hero") && !eEnt.hasClass("Siege")))
continue; continue;
var dist = API3.SquareVectorDistance(ent.position(), eEnt.position()); let dist = API3.SquareVectorDistance(ent.position(), eEnt.position());
if (idMinAll === undefined || dist < distMinAll) if (idMinAll === undefined || dist < distMinAll)
{ {
idMinAll = id; idMinAll = id;
@ -65,14 +65,15 @@ m.DefenseArmy.prototype.assignUnit = function (gameState, entID)
else else
return false; return false;
var ownIndex = gameState.ai.accessibility.getAccessValue(ent.position()); let ownIndex = gameState.ai.accessibility.getAccessValue(ent.position());
var foePosition = gameState.getEntityById(idFoe).position(); let foeEnt = gameState.getEntityById(idFoe);
var foeIndex = gameState.ai.accessibility.getAccessValue(foePosition); let foePosition = foeEnt.position();
let foeIndex = gameState.ai.accessibility.getAccessValue(foePosition);
if (ownIndex == foeIndex || ent.hasClass("Ship")) if (ownIndex == foeIndex || ent.hasClass("Ship"))
{ {
this.assignedTo[entID] = idFoe; this.assignedTo[entID] = idFoe;
this.assignedAgainst[idFoe].push(entID); this.assignedAgainst[idFoe].push(entID);
ent.attack(idFoe); ent.attack(idFoe, (!foeEnt.hasClass("Siege") || !foeEnt.hasClass("Melee")));
} }
else else
gameState.ai.HQ.navalManager.requireTransport(gameState, ent, ownIndex, foeIndex, foePosition); gameState.ai.HQ.navalManager.requireTransport(gameState, ent, ownIndex, foeIndex, foePosition);
@ -105,9 +106,15 @@ m.DefenseArmy.prototype.update = function (gameState)
let ent = gameState.getEntityById(entId); let ent = gameState.getEntityById(entId);
if (!ent) if (!ent)
continue; continue;
let orders = ent.unitAIOrderData(); let orderData = ent.unitAIOrderData();
if (orders.length == 0 && !ent.getMetadata(PlayerID, "transport")) if (!orderData.length && !ent.getMetadata(PlayerID, "transport"))
this.assignUnit(gameState, entId); this.assignUnit(gameState, entId);
else if (orderData.length && orderData[0].target && orderData[0].attackType && orderData[0].attackType === "Capture")
{
let target = gameState.getEntityById(orderData[0].target);
if (target && target.hasClass("Siege") && target.hasClass("Melee"))
ent.attack(orderData[0].target, false);
}
} }
return this.onUpdate(gameState); return this.onUpdate(gameState);