Index: ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js +++ ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js @@ -221,9 +221,6 @@ return; } - // For packable units: - // 1. If packed, we can move. - // 2. If unpacked, we first need to pack, then follow case 1. if (this.CanPack()) { this.PushOrderFront("Pack", { "force": true }); @@ -237,8 +234,6 @@ // (these will be overridden by various states) "Order.LeaveFoundation": function(msg) { - // If foundation is not ally of entity, or if entity is unpacked siege, - // ignore the order if (!this.WillMoveFromFoundation(msg.data.target)) { this.FinishOrder(); @@ -259,11 +254,9 @@ return; } - // Stop moving immediately. this.StopMoving(); this.FinishOrder(); - // No orders left, we're an individual now if (this.IsAnimal()) this.SetNextState("ANIMAL.IDLE"); else if (this.IsFormationMember()) @@ -281,9 +274,6 @@ return; } - // For packable units: - // 1. If packed, we can move. - // 2. If unpacked, we first need to pack, then follow case 1. if (this.CanPack()) { this.PushOrderFront("Pack", { "force": true }); @@ -291,7 +281,6 @@ } this.SetHeldPosition(this.order.data.x, this.order.data.z); - // It's not too bad if we don't arrive at exactly the right position. this.order.data.relaxed = true; if (this.IsAnimal()) this.SetNextState("ANIMAL.WALKING"); @@ -307,9 +296,6 @@ return; } - // For packable units: - // 1. If packed, we can move. - // 2. If unpacked, we first need to pack, then follow case 1. if (this.CanPack()) { this.PushOrderFront("Pack", { "force": true }); @@ -317,7 +303,6 @@ } this.SetHeldPosition(this.order.data.x, this.order.data.z); - // It's not too bad if we don't arrive at exactly the right position. this.order.data.relaxed = true; if (this.IsAnimal()) this.SetNextState("ANIMAL.WALKING"); // WalkAndFight not applicable for animals @@ -334,9 +319,6 @@ return; } - // For packable units: - // 1. If packed, we can move. - // 2. If unpacked, we first need to pack, then follow case 1. if (this.CanPack()) { this.PushOrderFront("Pack", { "force": true }); @@ -351,7 +333,6 @@ return true; } - // It's not too bad if we don't arrive at exactly the right position. this.order.data.relaxed = true; if (this.IsAnimal()) @@ -409,14 +390,12 @@ }, "Order.Attack": function(msg) { - // Check the target is alive if (!this.TargetIsAlive(this.order.data.target)) { this.FinishOrder(); return; } - // Work out how to attack the given target var type = this.GetBestAttackAgainst(this.order.data.target, this.order.data.allowCapture); if (!type) { @@ -430,12 +409,8 @@ if (this.order.data.hunting && this.orderQueue.length > 1 && this.orderQueue[1].type === "Gather") this.RememberTargetPosition(this.orderQueue[1].data); - // If we are already at the target, try attacking it from here if (this.CheckTargetAttackRange(this.order.data.target, this.order.data.attackType)) { - // For packable units within attack range: - // 1. If unpacked, we can attack the target. - // 2. If packed, we first need to unpack, then follow case 1. if (this.CanUnpack()) { this.PushOrderFront("Unpack", { "force": true }); @@ -453,17 +428,13 @@ return; } - // If we can't reach the target, but are standing ground, then abandon this attack order. - // Unless we're hunting, that's a special case where we should continue attacking our target. + // If we're hunting, that's a special case where we should continue attacking our target. if (this.GetStance().respondStandGround && !this.order.data.force && !this.order.data.hunting || !this.AbleToMove()) { this.FinishOrder(); return; } - // For packable units out of attack range: - // 1. If packed, we need to move to attack range and then unpack. - // 2. If unpacked, we first need to pack, then follow case 1. if (this.CanPack()) { this.PushOrderFront("Pack", { "force": true }); @@ -493,14 +464,12 @@ return; } - // It's not too bad if we don't arrive at exactly the right position. this.order.data.relaxed = true; this.SetNextState("INDIVIDUAL.PATROL"); }, "Order.Heal": function(msg) { - // Check the target is alive if (!this.TargetIsAlive(this.order.data.target)) { this.FinishOrder(); @@ -514,15 +483,12 @@ return; } - // Check if the target is in range if (this.CheckTargetRange(this.order.data.target, IID_Heal)) { this.SetNextState("INDIVIDUAL.HEAL.HEALING"); return; } - // If we can't reach the target, but are standing ground, - // then abandon this heal order if (this.GetStance().respondStandGround && !this.order.data.force) { this.FinishOrder(); @@ -533,7 +499,6 @@ }, "Order.Gather": function(msg) { - // If the target is still alive, we need to kill it first if (this.MustKillGatherTarget(this.order.data.target)) { // Make sure we can attack the target, else we'll get very stuck @@ -589,7 +554,6 @@ }, "Order.ReturnResource": function(msg) { - // Check if the dropsite is already in range let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); if (this.CheckTargetRange(this.order.data.target, IID_ResourceGatherer) && this.CanReturnResource(this.order.data.target, true, cmpResourceGatherer)) @@ -622,7 +586,6 @@ }, "Order.Repair": function(msg) { - // Try to move within range if (this.CheckTargetRange(this.order.data.target, IID_Builder)) this.SetNextState("INDIVIDUAL.REPAIR.REPAIRING"); else @@ -644,9 +607,6 @@ return; } - // For packable units: - // 1. If packed, we can move to the garrison target. - // 2. If unpacked, we first need to pack, then follow case 1. if (this.CanPack()) { this.PushOrderFront("Pack", { "force": true }); @@ -762,7 +722,6 @@ if (cmpTargetUnitAI && cmpTargetUnitAI.IsFormationMember()) target = cmpTargetUnitAI.GetFormationController(); - // Check if we are already in range, otherwise walk there if (!this.CheckFormationTargetAttackRange(target)) { if (this.TargetIsAlive(target) && this.CheckTargetVisible(target)) @@ -787,7 +746,6 @@ this.FinishOrder(); return; } - // Check if we are already in range, otherwise walk there if (!this.CheckGarrisonRange(msg.data.target)) { if (!this.CheckTargetVisible(msg.data.target)) @@ -836,11 +794,9 @@ } // TODO: on what should we base this range? - // Check if we are already in range, otherwise walk there if (!this.CheckTargetRangeExplicit(msg.data.target, 0, 10)) { if (!this.CanGather(msg.data.target) || !this.CheckTargetVisible(msg.data.target)) - // The target isn't gatherable or not visible any more. this.FinishOrder(); // TODO: Should we issue a gather-near-position order // if the target isn't gatherable/doesn't exist anymore? @@ -857,7 +813,6 @@ "Order.GatherNearPosition": function(msg) { // TODO: on what should we base this range? - // Check if we are already in range, otherwise walk there if (!this.CheckPointRangeExplicit(msg.data.x, msg.data.z, 0, 20)) { // Out of range; move there in formation @@ -872,11 +827,9 @@ "Order.Heal": function(msg) { // TODO: on what should we base this range? - // Check if we are already in range, otherwise walk there if (!this.CheckTargetRangeExplicit(msg.data.target, 0, 10)) { if (!this.TargetIsAlive(msg.data.target) || !this.CheckTargetVisible(msg.data.target)) - // The target was destroyed this.FinishOrder(); else // Out of range; move there in formation @@ -891,11 +844,9 @@ "Order.Repair": function(msg) { // TODO: on what should we base this range? - // Check if we are already in range, otherwise walk there if (!this.CheckTargetRangeExplicit(msg.data.target, 0, 10)) { if (!this.TargetIsAlive(msg.data.target) || !this.CheckTargetVisible(msg.data.target)) - // The building was finished or destroyed this.FinishOrder(); else // Out of range move there in formation @@ -910,11 +861,9 @@ "Order.ReturnResource": function(msg) { // TODO: on what should we base this range? - // Check if we are already in range, otherwise walk there if (!this.CheckTargetRangeExplicit(msg.data.target, 0, 10)) { if (!this.TargetIsAlive(msg.data.target) || !this.CheckTargetVisible(msg.data.target)) - // The target was destroyed this.FinishOrder(); else // Out of range; move there in formation @@ -988,7 +937,6 @@ }, "Timer": function(msg) { - // check if there are no enemies to attack this.FindWalkAndFightTargets(); }, @@ -1000,13 +948,13 @@ "PATROL": { "enter": function(msg) { - // Memorize the origin position in case that we want to go back let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); if (!cmpPosition || !cmpPosition.IsInWorld()) { this.FinishOrder(); return true; } + // Memorize the origin position in case that we want to go back. if (!this.patrolStartPosOrder) { this.patrolStartPosOrder = cmpPosition.GetPosition(); @@ -1028,7 +976,6 @@ }, "Timer": function(msg) { - // Check if there are no enemies to attack this.FindWalkAndFightTargets(); }, @@ -1083,8 +1030,6 @@ "leave": function() { this.StopMoving(); - - // If a pickup has been requested and not yet canceled, cancel it. if (this.pickup) { Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); @@ -1169,7 +1114,6 @@ "enter": function(msg) { let target = this.order.data.target; let allowCapture = this.order.data.allowCapture; - // Check if we are already in range, otherwise walk there if (!this.CheckFormationTargetAttackRange(target)) { if (this.TargetIsAlive(target) && this.CheckTargetVisible(target)) @@ -1192,7 +1136,6 @@ "Timer": function(msg) { let target = this.order.data.target; let allowCapture = this.order.data.allowCapture; - // Check if we are already in range, otherwise walk there if (!this.CheckFormationTargetAttackRange(target)) { if (this.TargetIsAlive(target) && this.CheckTargetVisible(target)) @@ -1248,15 +1191,12 @@ }, "Timer": function(msg) { - // Have all members finished the task? let cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); if (cmpFormation && !cmpFormation.AreAllMembersWaiting()) return; - // Execute the next order if (this.FinishOrder()) { - // if WalkAndFight order, look for new target before moving again if (this.IsWalkingAndFighting()) this.FindWalkAndFightTargets(); return; @@ -1302,7 +1242,6 @@ return; } - // No orders left, we're an individual now this.formationAnimationVariant = undefined; this.SetNextState("INDIVIDUAL.IDLE"); }, @@ -1311,8 +1250,6 @@ // anything more important (and we might be stuck in the WALKING // state forever and need to get out of foundations in that case) "Order.LeaveFoundation": function(msg) { - // If foundation is not ally of entity, or if entity is unpacked siege, - // ignore the order if (!this.WillMoveFromFoundation(msg.data.target)) { this.FinishOrder(); @@ -1325,7 +1262,6 @@ "enter": function() { if (this.IsAnimal()) { - // Animals can't go in formation. warn("Entity " + this.entity + " was put in FORMATIONMEMBER state but is an animal"); this.FinishOrder(); this.SetNextState("ANIMAL.IDLE"); @@ -1379,7 +1315,6 @@ let facePointAfterMove = this.GetFacePointAfterMove(); this.SetFacePointAfterMove(false); this.StopMoving(); - // Reset previous behaviour. this.SetFacePointAfterMove(facePointAfterMove); }, @@ -1421,14 +1356,12 @@ "INDIVIDUAL": { "enter": function() { - // Sanity-checking if (this.IsAnimal()) error("Animal got moved into INDIVIDUAL.* state"); return false; }, "Attacked": function(msg) { - // Respond to attack if we always target attackers or during unforced orders if (this.GetStance().targetAttackersAlways || !this.order || !this.order.data || !this.order.data.force) this.RespondToTargetedEntities([msg.data.attacker]); }, @@ -1459,7 +1392,6 @@ return; } - // if the attacker is a building and we can repair the guarded, repair it rather than attacking var cmpBuildingAI = Engine.QueryInterface(msg.data.attacker, IID_BuildingAI); if (cmpBuildingAI && this.CanRepair(this.isGuardOf)) { @@ -1467,7 +1399,6 @@ return; } - // target the unit if (this.CheckTargetVisible(msg.data.attacker)) this.PushOrderFront("Attack", { "target": msg.data.attacker, "force": false, "allowCapture": true }); else @@ -1547,7 +1478,6 @@ }, "Timer": function(msg) { - // If the unit is guarding/escorting, go back to its duty. if (this.isGuardOf) { this.Guard(this.isGuardOf, false); @@ -1601,8 +1531,8 @@ }, "MovementUpdate": function(msg) { - // If it looks like the path is failing, and we are close enough (3 tiles) - // stop anyways. This avoids pathing for an unreachable goal and reduces lag considerably. + // If it looks like the path is failing, and we are close enough stop anyways. + // This avoids pathing for an unreachable goal and reduces lag considerably. if (msg.likelyFailure || msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.DefaultRelaxedMaxRange) || this.CheckRange(this.order.data)) this.FinishOrder(); @@ -1634,8 +1564,8 @@ }, "MovementUpdate": function(msg) { - // If it looks like the path is failing, and we are close enough (3 tiles) - // stop anyways. This avoids pathing for an unreachable goal and reduces lag considerably. + // If it looks like the path is failing, and we are close enough stop anyways. + // This avoids pathing for an unreachable goal and reduces lag considerably. if (msg.likelyFailure || msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.DefaultRelaxedMaxRange) || this.CheckRange(this.order.data)) this.FinishOrder(); @@ -1644,7 +1574,6 @@ "PATROL": { "enter": function() { - // Memorize the origin position in case that we want to go back let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); if (!cmpPosition || !cmpPosition.IsInWorld() || !this.MoveTo(this.order.data)) @@ -1653,6 +1582,7 @@ return true; } + // Memorize the origin position in case that we want to go back. if (!this.patrolStartPosOrder) { this.patrolStartPosOrder = cmpPosition.GetPosition(); @@ -1711,14 +1641,12 @@ }, "Timer": function(msg) { - // Check the target is alive if (!this.TargetIsAlive(this.isGuardOf)) { this.FinishOrder(); return; } - // Adapt the speed to the one of the target if needed let cmpObstructionManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ObstructionManager); if (cmpObstructionManager.IsInTargetRange(this.entity, this.isGuardOf, 0, 3 * this.guardRange, false)) this.TryMatchTargetSpeed(this.isGuardOf, false); @@ -1755,13 +1683,11 @@ }, "Timer": function(msg) { - // Check the target is alive if (!this.TargetIsAlive(this.isGuardOf)) { this.FinishOrder(); return; } - // Then check is the target has moved and try following it. // TODO: find out what to do if we cannot move. if (!this.CheckTargetRangeExplicit(this.isGuardOf, 0, this.guardRange) && this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) @@ -1769,7 +1695,6 @@ else { this.FaceTowardsTarget(this.order.data.target); - // if nothing better to do, check if the guarded needs to be healed or repaired var cmpHealth = Engine.QueryInterface(this.isGuardOf, IID_Health); if (cmpHealth && cmpHealth.IsInjured()) { @@ -1803,7 +1728,6 @@ this.PlaySound("panic"); - // Run quickly this.SetSpeedMultiplier(this.GetRunMultiplier()); return false; }, @@ -1826,7 +1750,6 @@ }, "MovementUpdate": function(msg) { - // When we've run far enough, stop fleeing if (msg.likelyFailure || this.CheckTargetRangeExplicit(this.order.data.target, this.order.data.distanceToFlee, -1)) this.FinishOrder(); }, @@ -1878,7 +1801,6 @@ this.StopMoving(); this.FinishOrder(); - // Return to our original position if (this.GetStance().respondHoldGround) this.WalkToHeldPosition(); } @@ -1905,7 +1827,6 @@ this.SetNextState("COMBAT.FINDINGNEWTARGET"); return; } - // Go to the last known position and try to find enemies there. let lastPos = this.order.data.lastPos; this.PushOrder("WalkAndFight", { "x": lastPos.x, "z": lastPos.z, "force": false }); return; @@ -1913,7 +1834,6 @@ if (this.CheckTargetAttackRange(this.order.data.target, this.order.data.attackType)) { - // If the unit needs to unpack, do so if (this.CanUnpack()) { this.PushOrderFront("Unpack", { "force": true }); @@ -1933,7 +1853,6 @@ "enter": function() { let target = this.order.data.target; let cmpFormation = Engine.QueryInterface(target, IID_Formation); - // if the target is a formation, save the attacking formation, and pick a member if (cmpFormation) { this.order.data.formationTarget = target; @@ -1979,7 +1898,6 @@ this.SetAnimationVariant("combat"); this.oldAttackType = this.order.data.attackType; - // add prefix + no capital first letter for attackType this.SelectAnimation("attack_" + this.order.data.attackType.toLowerCase()); this.SetAnimationSync(prepare, this.attackTimers.repeat); this.StartTimer(prepare, this.attackTimers.repeat); @@ -2015,7 +1933,6 @@ let target = this.order.data.target; let attackType = this.order.data.attackType; - // Check the target is still alive and attackable if (!this.CanAttack(target)) { this.SetNextState("COMBAT.FINDINGNEWTARGET"); @@ -2056,7 +1973,6 @@ return; } - // Can't reach it - try to chase after it if (this.ShouldChaseTargetedEntity(target, this.order.data.force)) { if (this.CanPack()) @@ -2075,7 +1991,6 @@ // until the next Timer event "Attacked": function(msg) { - // If we are capturing and are attacked by something that we would not capture, attack that entity instead if (this.order.data.attackType == "Capture" && (this.GetStance().targetAttackersAlways || !this.order.data.force) && this.order.data.target != msg.data.attacker && this.GetBestAttackAgainst(msg.data.attacker, true) != "Capture") this.RespondToTargetedEntities([msg.data.attacker]); @@ -2113,11 +2028,9 @@ return true; } - // See if we can switch to a new nearby enemy if (this.FindNewTargets()) return true; - // Return to our original position if (this.GetStance().respondHoldGround) this.WalkToHeldPosition(); @@ -2174,7 +2087,6 @@ this.StopMoving(); this.FinishOrder(); - // Return to our original position if (this.GetStance().respondHoldGround) this.WalkToHeldPosition(); } @@ -2216,7 +2128,6 @@ } if (this.CheckTargetAttackRange(this.order.data.target, this.order.data.attackType)) { - // If the unit needs to unpack, do so if (this.CanUnpack()) { this.PushOrderFront("Unpack", { "force": true }); @@ -2243,9 +2154,9 @@ this.StopMoving(); }, "MovementUpdate": function(msg) { - // If it looks like the path is failing, and we are close enough (3 tiles) from wanted range + // If it looks like the path is failing, and we are close enough from wanted range // stop anyways. This avoids pathing for an unreachable goal and reduces lag considerably. - if (msg.likelyFailure || + if (msg.likelyFailure || msg.obstructed && this.RelaxedMaxRangeCheck(this.order.data, this.order.data.max + this.DefaultRelaxedMaxRange) || !msg.obstructed && this.CheckRange(this.order.data)) this.FinishOrder(); @@ -2264,7 +2175,6 @@ "enter": function() { this.gatheringTarget = this.order.data.target; // temporary, deleted in "leave". - // Check that we can gather from the resource we're supposed to gather from. let cmpSupply = Engine.QueryInterface(this.gatheringTarget, IID_ResourceSupply); let cmpMirage = Engine.QueryInterface(this.gatheringTarget, IID_Mirage); if ((!cmpMirage || !cmpMirage.Mirages(IID_ResourceSupply)) && @@ -2482,7 +2392,6 @@ return; } - // Find a new target if the current one is exhausted if (status.exhausted) this.SetNextState("FINDINGNEWTARGET"); }, @@ -2573,7 +2482,6 @@ "HEAL": { "Attacked": function(msg) { - // If we stand ground we will rather die than flee if (!this.GetStance().respondStandGround && !this.order.data.force) this.Flee(msg.data.attacker, false); }, @@ -2658,18 +2566,15 @@ "Timer": function(msg) { let target = this.order.data.target; - // Check the target is still alive and healable if (!this.TargetIsAlive(target) || !this.CanHeal(target)) { this.SetNextState("FINDINGNEWTARGET"); return; } - // Check if we can still reach the target if (!this.CheckRange(this.order.data, IID_Heal)) { if (this.ShouldChaseTargetedEntity(target, this.order.data.force)) { - // Can't reach it - try to chase after it if (this.CanPack()) { this.PushOrderFront("Pack", { "force": true }); @@ -2703,11 +2608,9 @@ if (this.FinishOrder()) return true; - // Heal another one if (this.FindNewHealTargets()) return true; - // Return to our original position if (this.GetStance().respondHoldGround) this.WalkToHeldPosition(); @@ -2850,10 +2753,8 @@ // Needed to remove the entity from the builder list when leaving this state. this.repairTarget = this.order.data.target; - // Check we can still reach and repair the target. if (!this.CanRepair(this.repairTarget)) { - // Can't reach it, no longer owned by ally, or it doesn't exist any more. this.FinishOrder(); return true; } @@ -2863,7 +2764,7 @@ this.SetNextState("APPROACHING"); return true; } - // Check if the target is still repairable. + let cmpHealth = Engine.QueryInterface(this.repairTarget, IID_Health); if (cmpHealth && cmpHealth.GetHitpoints() >= cmpHealth.GetMaxHitpoints()) { @@ -2896,10 +2797,8 @@ }, "Timer": function(msg) { - // Check we can still reach and repair the target. if (!this.CanRepair(this.repairTarget)) { - // No longer owned by ally, or it doesn't exist any more. this.FinishOrder(); return; } @@ -2922,7 +2821,6 @@ if (msg.data.entity != this.order.data.target) return; // ignore other buildings - // Save the current order's data in case we need it later let oldData = this.order.data; // Save the current state so we can continue walking if necessary @@ -2930,7 +2828,6 @@ // Idle animation while moving towards finished construction looks weird (ghosty). let oldState = this.GetCurrentState(); - // Drop any resource we can if we are in range when the construction finishes let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); let canReturnResources = this.CanReturnResource(msg.data.newentity, true, cmpResourceGatherer); if (this.CheckTargetRange(msg.data.newentity, IID_Builder) && canReturnResources) @@ -2939,7 +2836,6 @@ this.SetDefaultAnimationVariant(); } - // We finished building it. // Switch to the next order (if any) if (this.FinishOrder()) { @@ -2993,7 +2889,6 @@ } } - // Look for a nearby foundation to help with. let nearbyFoundation = this.FindNearbyFoundation(this.TargetPosOrEntPos(msg.data.newentity)); if (nearbyFoundation) { @@ -3010,7 +2905,6 @@ "GARRISON": { "leave": function() { - // If a pickup has been requested and not yet canceled, cancel it. if (this.pickup) { Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); @@ -3027,11 +2921,9 @@ return true; } - // If a pickup was still pending, cancel that. if (this.pickup) Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); - // If the garrisonholder should pickup, warn it so it can take needed action. let cmpGarrisonHolder = Engine.QueryInterface(this.order.data.target, IID_GarrisonHolder); if (cmpGarrisonHolder && cmpGarrisonHolder.CanPickup(this.entity)) { @@ -3063,13 +2955,10 @@ if (this.IsGarrisoned()) return false; - // Check that we can garrison here if (this.CanGarrison(target)) - // Check that we're in range of the garrison target if (this.CheckGarrisonRange(target)) { var cmpGarrisonHolder = Engine.QueryInterface(target, IID_GarrisonHolder); - // Check that garrisoning succeeds if (cmpGarrisonHolder.Garrison(this.entity)) { this.isGarrisoned = true; @@ -3080,9 +2969,6 @@ var cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); if (cmpFormation) { - // disable rearrange for this removal, - // but enable it again for the next - // move command var rearrange = cmpFormation.rearrange; cmpFormation.SetRearrange(false); cmpFormation.RemoveMembers([this.entity]); @@ -3090,7 +2976,6 @@ } } - // Check if we are garrisoned in a dropsite. let cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); if (this.CanReturnResource(target, true, cmpResourceGatherer)) { @@ -3098,7 +2983,6 @@ this.SetDefaultAnimationVariant(); } - // If a pickup has been requested, remove it if (this.pickup) { Engine.PostMessage(this.pickup, MT_PickupCanceled, { "entity": this.entity }); @@ -3132,7 +3016,7 @@ this.SetNextState("APPROACHING"); return true; } - // Garrisoning failed for some reason, so finish the order + this.FinishOrder(); return true; }, @@ -3295,7 +3179,6 @@ // (We need an IDLE state so that FinishOrder works) "enter": function() { - // Start feeding immediately this.SetNextState("FEEDING"); return true; }, @@ -3303,10 +3186,8 @@ "ROAMING": { "enter": function() { - // Walk in a random direction this.SetFacePointAfterMove(false); this.MoveRandomly(+this.template.RoamDistance); - // Set a random timer to switch to feeding state this.StartTimer(randIntInclusive(+this.template.RoamTimeMin, +this.template.RoamTimeMax)); return false; }, @@ -3344,7 +3225,6 @@ "FEEDING": { "enter": function() { - // Stop and eat for a while this.SelectAnimation("feeding"); this.StopMoving(); this.StartTimer(randIntInclusive(+this.template.FeedTimeMin, +this.template.FeedTimeMax)); @@ -3371,9 +3251,9 @@ }, }, - "FLEEING": "INDIVIDUAL.FLEEING", // reuse the same fleeing behaviour for animals + "FLEEING": "INDIVIDUAL.FLEEING", - "COMBAT": "INDIVIDUAL.COMBAT", // reuse the same combat behaviour for animals + "COMBAT": "INDIVIDUAL.COMBAT", "WALKING": "INDIVIDUAL.WALKING", // reuse the same walking behaviour for animals // only used for domestic animals @@ -3405,7 +3285,6 @@ }, }, - // Reuse the same garrison behaviour for animals. "GARRISON": "INDIVIDUAL.GARRISON", }, }; @@ -3624,7 +3503,6 @@ UnitAI.prototype.OnVisionRangeChanged = function(msg) { - // Update range queries if (this.entity == msg.entity) this.SetupRangeQueries(); }; @@ -3636,10 +3514,8 @@ UnitAI.prototype.OnPickupRequested = function(msg) { - // First check if we already have such a request if (this.HasPickupOrder(msg.entity)) return; - // Otherwise, insert the PickUp order after the last forced order this.PushOrderAfterForced("PickupUnit", { "target": msg.entity }); }; @@ -3877,7 +3753,6 @@ var order = { "type": type, "data": data }; this.orderQueue.push(order); - // If we didn't already have an order, then process this new one if (this.orderQueue.length == 1) { this.order = order; @@ -4000,14 +3875,11 @@ UnitAI.prototype.WillMoveFromFoundation = function(target, checkPacking = true) { - // If foundation is not ally of entity, or if entity is unpacked siege, - // ignore the order. if (!IsOwnedByAllyOfEntity(this.entity, target) && !Engine.QueryInterface(SYSTEM_ENTITY, IID_CeasefireManager).IsCeasefireActive() || checkPacking && this.IsPacking() || this.CanPack() || !this.AbleToMove()) return false; - // Move a tile outside the building. return !this.CheckTargetRangeExplicit(target, g_LeaveFoundationRange, -1); }; @@ -4082,19 +3954,15 @@ UnitAI.prototype.UpdateWorkOrders = function(type) { var isWorkType = type => type == "Gather" || type == "Trade" || type == "Repair" || type == "ReturnResource"; - - // If we are being re-affected to a work order, forget the previous ones if (isWorkType(type)) { this.workOrders = []; return; } - // Then if we already have work orders, keep them if (this.workOrders.length) return; - // First if the unit is in a formation, get its workOrders from it if (this.IsFormationMember()) { var cmpUnitAI = Engine.QueryInterface(this.formationController, IID_UnitAI); @@ -4134,13 +4002,11 @@ return false; } - // Clear the order queue considering special orders not to avoid this.orderQueue = []; this.AddOrders(this.workOrders); Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); - // And if the unit is in a formation, remove it from the formation if (this.IsFormationMember()) { var cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); @@ -4379,7 +4245,6 @@ let amount = cmpResourceSupply.GetCurrentAmount(); let template = cmpTemplateManager.GetCurrentTemplateName(ent); - // Remove "resource|" prefix from template names, if present. if (template.indexOf("resource|") != -1) template = template.slice(9); @@ -4407,7 +4272,6 @@ // Maximum distance a point on an obstruction can be from the center of the obstruction. let maxDifference = 40; - // Find dropsites owned by this unit's player or allied ones if allowed. let owner = cmpOwnership.GetOwner(); let cmpPlayer = QueryOwnerInterface(this.entity); let players = cmpPlayer && cmpPlayer.HasSharedDropsites() ? cmpPlayer.GetMutualAllies() : [owner]; @@ -4457,7 +4321,6 @@ if (!cmpOwnership || cmpOwnership.GetOwner() == INVALID_PLAYER) return undefined; - // Find buildings owned by this unit's player let players = [cmpOwnership.GetOwner()]; let range = 64; // TODO: what's a sensible number? @@ -4475,7 +4338,6 @@ */ UnitAI.prototype.PlaySound = function(name) { - // If we're a formation controller, use the sounds from our first member if (this.IsFormationController()) { var cmpFormation = Engine.QueryInterface(this.entity, IID_Formation); @@ -4485,7 +4347,6 @@ } else { - // Otherwise use our own sounds PlaySound(name, this.entity); } }; @@ -4521,7 +4382,6 @@ { let typename = "carry_" + type.generic; - // Special case for meat. if (type.specific == "meat") typename = "carry_" + type.specific; @@ -5102,7 +4962,6 @@ return false; } - // Stop if we're in hold-ground mode and it's too far from the holding point if (this.GetStance().respondHoldGround) if (!this.CheckTargetDistanceFromHeldPosition(target, iid, type)) return true; @@ -5183,8 +5042,6 @@ return; var pos = cmpPosition.GetPosition(); - - // Add new order to move into formation at the current position this.PushOrderFront("MoveIntoFormation", { "x": pos.x, "z": pos.z, "force": true }); }; @@ -5216,7 +5073,6 @@ case "ReturnResource": case "Repair": case "Garrison": - // Find the target unit's position var cmpTargetPosition = Engine.QueryInterface(order.data.target, IID_Position); if (!cmpTargetPosition || !cmpTargetPosition.IsInWorld()) return targetPositions; @@ -5258,7 +5114,6 @@ pos = targetPositions[i]; } - // Return the total distance to the end of the order queue return distance; }; @@ -5293,8 +5148,6 @@ if (target === this.entity) return; - // if we already had an old guard order, do nothing if the target is the same - // and the order is running, otherwise remove the previous order if (this.isGuardOf) { if (this.isGuardOf == target && this.order && this.order.type == "Guard") @@ -5440,7 +5293,6 @@ if (this.order && (this.order.type == "LeaveFoundation" || (this.order.type == "Flee" && this.order.data.target == target))) return; - // Ignore also the request if we are packing. if (this.orderQueue.length && this.orderQueue[0].type == "Unpack" && this.WillMoveFromFoundation(target, false)) { let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); @@ -5551,8 +5403,6 @@ // we won't go from hunting slow safe animals to dangerous fast ones var cmpTemplateManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_TemplateManager); var template = cmpTemplateManager.GetCurrentTemplateName(target); - - // Remove "resource|" prefix from template name, if present. if (template.indexOf("resource|") != -1) template = template.slice(9); @@ -5575,7 +5425,6 @@ */ UnitAI.prototype.GatherNearPosition = function(x, z, type, template, queued) { - // Remove "resource|" prefix from template name, if present. if (template.indexOf("resource|") != -1) template = template.slice(9); @@ -5757,7 +5606,7 @@ this.waypoints.unshift(null); // additionnal dummy point for the market } - if (this.MoveToMarket(nextMarket)) // We've started walking to the next market + if (this.MoveToMarket(nextMarket)) this.SetNextState("APPROACHINGMARKET"); else this.StopTrading(); @@ -5808,14 +5657,12 @@ UnitAI.prototype.Pack = function(queued) { - // Check that we can pack if (this.CanPack()) this.AddOrder("Pack", { "force": true }, queued); }; UnitAI.prototype.Unpack = function(queued) { - // Check that we can unpack if (this.CanUnpack()) this.AddOrder("Unpack", { "force": true }, queued); }; @@ -6199,7 +6046,6 @@ if (!cmpGarrisonHolder) return false; - // Verify that the target is owned by this entity's player or a mutual ally of this player let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (!cmpOwnership || !(IsOwnedByPlayer(cmpOwnership.GetOwner(), target) || IsOwnedByMutualAllyOfPlayer(cmpOwnership.GetOwner(), target))) return false; @@ -6211,7 +6057,7 @@ { if (this.IsTurret()) return false; - // The target must be a valid resource supply, or the mirage of one. + var cmpResourceSupply = QueryMiragedInterface(target, IID_ResourceSupply); if (!cmpResourceSupply) return false; @@ -6221,12 +6067,10 @@ if (this.IsFormationController()) return true; - // Verify that we're able to respond to Gather commands var cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); if (!cmpResourceGatherer) return false; - // Verify that we can gather from this target if (!cmpResourceGatherer.GetTargetGatherRate(target)) return false; @@ -6263,7 +6107,6 @@ if (this.IsFormationController()) return true; - // Verify that we're able to respond to ReturnResource commands if (!cmpResourceGatherer) { cmpResourceGatherer = Engine.QueryInterface(this.entity, IID_ResourceGatherer); @@ -6271,21 +6114,17 @@ return false; } - // Verify that the target is a dropsite let cmpResourceDropsite = Engine.QueryInterface(target, IID_ResourceDropsite); if (!cmpResourceDropsite) return false; if (checkCarriedResource) { - // Verify that we are carrying some resources, - // and can return our current resource to this target let type = cmpResourceGatherer.GetMainCarryingType(); if (!type || !cmpResourceDropsite.AcceptsType(type)) return false; } - // Verify that the dropsite is owned by this entity's player (or a mutual ally's if allowed) let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); if (cmpOwnership && IsOwnedByPlayer(cmpOwnership.GetOwner(), target)) return true; @@ -6303,7 +6142,6 @@ if (this.IsFormationController()) return true; - // Verify that we're able to respond to Trade commands var cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); return cmpTrader && cmpTrader.CanTrade(target); }; @@ -6322,13 +6160,11 @@ if (!cmpBuilder) return false; - // Verify that the target can be either built or repaired var cmpFoundation = QueryMiragedInterface(target, IID_Foundation); var cmpRepairable = Engine.QueryInterface(target, IID_Repairable); if (!cmpFoundation && !cmpRepairable) return false; - // Verify that the target is owned by an ally of this entity's player var cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership); return cmpOwnership && IsOwnedByAllyOfPlayer(cmpOwnership.GetOwner(), target); };