Index: binaries/data/mods/public/simulation/components/UnitAI.js =================================================================== --- binaries/data/mods/public/simulation/components/UnitAI.js +++ binaries/data/mods/public/simulation/components/UnitAI.js @@ -542,7 +542,12 @@ return; } - this.PushOrderFront("Attack", { "target": this.order.data.target, "force": !!this.order.data.force, "hunting": true, "allowCapture": false }); + this.PushOrderFront("Attack", { + "target": this.order.data.target, + "force": !!this.order.data.force, + "hunting": true, + "allowCapture": false + }); return; } @@ -762,11 +767,10 @@ this.FinishOrder(); return; } - else - { - this.SetNextState("GARRISON.APPROACHING"); - return; - } + + this.SetNextState("GARRISON.APPROACHING"); + return; + } this.SetNextState("GARRISON.GARRISONING"); @@ -798,7 +802,14 @@ } return; } - this.PushOrderFront("Attack", { "target": msg.data.target, "force": !!msg.data.force, "hunting": true, "allowCapture": false, "min": 0, "max": 10 }); + this.PushOrderFront("Attack", { + "target": msg.data.target, + "force": !!msg.data.force, + "hunting": true, + "allowCapture": false, + "min": 0, + "max": 10 + }); return; } @@ -1030,7 +1041,7 @@ }, }, - "GARRISON":{ + "GARRISON": { "enter": function() { // If the garrisonholder should pickup, warn it so it can take needed action var cmpGarrisonHolder = Engine.QueryInterface(this.order.data.target, IID_GarrisonHolder); @@ -1156,7 +1167,11 @@ if (this.TargetIsAlive(target) && this.CheckTargetVisible(target)) { this.FinishOrder(); - this.PushOrderFront("Attack", { "target": target, "force": false, "allowCapture": allowCapture }); + this.PushOrderFront("Attack", { + "target": target, + "force": false, + "allowCapture": allowCapture + }); return true; } this.FinishOrder(); @@ -1979,8 +1994,10 @@ "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") + 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]); }, }, @@ -2338,7 +2355,10 @@ { let nearby = this.FindNearestDropsite(resourceType.generic); if (nearby) - this.orderQueue.splice(1, 0, { "type": "ReturnResource", "data": { "target": nearby, "force": false } }); + this.orderQueue.splice(1, 0, { + "type": "ReturnResource", + "data": { "target": nearby, "force": false } + }); } // Must go before FinishOrder or this.order will be undefined. @@ -2707,7 +2727,10 @@ { // The building was already finished/fully repaired before we arrived; // let the ConstructionFinished handler handle this. - this.OnGlobalConstructionFinished({"entity": this.repairTarget, "newentity": this.repairTarget}); + this.OnGlobalConstructionFinished({ + "entity": this.repairTarget, + "newentity": this.repairTarget + }); return true; } @@ -3215,12 +3238,15 @@ }, }, - "FLEEING": "INDIVIDUAL.FLEEING", // reuse the same fleeing behaviour for animals + // reuse the same fleeing behaviour for animals + "FLEEING": "INDIVIDUAL.FLEEING", - "COMBAT": "INDIVIDUAL.COMBAT", // reuse the same combat behaviour for animals + // 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 + // reuse the same walking behaviour for animals + // only used for domestic animals + "WALKING": "INDIVIDUAL.WALKING", }, }; @@ -3277,7 +3303,7 @@ UnitAI.prototype.IsAnimal = function() { - return (this.template.NaturalBehaviour ? true : false); + return !!this.template.NaturalBehaviour; }; UnitAI.prototype.IsDangerousAnimal = function() @@ -3393,7 +3419,7 @@ { let index = this.GetCurrentState().indexOf("."); if (index != -1) - this.UnitFsm.SwitchToNextState(this, this.GetCurrentState().slice(0,index)); + this.UnitFsm.SwitchToNextState(this, this.GetCurrentState().slice(0, index)); this.Stop(false); } @@ -3449,7 +3475,10 @@ if (this.orderQueue[i].type != "PickupUnit" || this.orderQueue[i].data.target != msg.entity) continue; if (i == 0) - this.UnitFsm.ProcessMessage(this, {"type": "PickupCanceled", "data": msg}); + this.UnitFsm.ProcessMessage(this, { + "type": "PickupCanceled", + "data": msg + }); else this.orderQueue.splice(i, 1); Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); @@ -3533,7 +3562,7 @@ }; -//// FSM linkage functions //// +// FSM linkage functions // Setting the next state to the current state will leave/re-enter the top-most substate. UnitAI.prototype.SetNextState = function(state) @@ -3579,9 +3608,10 @@ let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); if (this.orderQueue.length && (this.IsGarrisoned() || cmpPosition && cmpPosition.IsInWorld())) { - let ret = this.UnitFsm.ProcessMessage(this, - { "type": "Order."+this.order.type, "data": this.order.data } - ); + let ret = this.UnitFsm.ProcessMessage(this, { + "type": "Order."+this.order.type, + "data": this.order.data + }); Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); @@ -3632,9 +3662,10 @@ if (this.orderQueue.length == 1) { this.order = order; - let ret = this.UnitFsm.ProcessMessage(this, - { "type": "Order."+this.order.type, "data": this.order.data } - ); + let ret = this.UnitFsm.ProcessMessage(this, { + "type": "Order."+this.order.type, + "data": this.order.data + }); // If the order was rejected then immediately take it off // and process the remaining queue @@ -3668,9 +3699,10 @@ { this.orderQueue.unshift(order); this.order = order; - let ret = this.UnitFsm.ProcessMessage(this, - { "type": "Order."+this.order.type, "data": this.order.data } - ); + let ret = this.UnitFsm.ProcessMessage(this, { + "type": "Order."+this.order.type, + "data": this.order.data + }); // If the order was rejected then immediately take it off again; // assume the previous active order is still valid (the short-lived @@ -3703,7 +3735,7 @@ continue; if (this.orderQueue[i].type == type) continue; - this.orderQueue.splice(i, 0, {"type": type, "data": data}); + this.orderQueue.splice(i, 0, { "type": type, "data": data }); Engine.PostMessage(this.entity, MT_UnitAIOrderDataChanged, { "to": this.GetOrderData() }); return; } @@ -3874,7 +3906,11 @@ if (data.timerRepeat === undefined) this.timer = undefined; - this.UnitFsm.ProcessMessage(this, {"type": "Timer", "data": data, "lateness": lateness}); + this.UnitFsm.ProcessMessage(this, { + "type": "Timer", + "data": data, + "lateness": lateness + }); }; /** @@ -3919,7 +3955,7 @@ // TODO: This is a bit inefficient since every unit listens to every // construction message - ideally we could scope it to only the one we're building - this.UnitFsm.ProcessMessage(this, {"type": "ConstructionFinished", "data": msg}); + this.UnitFsm.ProcessMessage(this, { "type": "ConstructionFinished", "data": msg }); }; UnitAI.prototype.OnGlobalEntityRenamed = function(msg) @@ -3944,33 +3980,37 @@ UnitAI.prototype.OnAttacked = function(msg) { - this.UnitFsm.ProcessMessage(this, {"type": "Attacked", "data": msg}); + this.UnitFsm.ProcessMessage(this, { "type": "Attacked", "data": msg }); }; UnitAI.prototype.OnGuardedAttacked = function(msg) { - this.UnitFsm.ProcessMessage(this, {"type": "GuardedAttacked", "data": msg.data}); + this.UnitFsm.ProcessMessage(this, { "type": "GuardedAttacked", "data": msg.data }); }; UnitAI.prototype.OnHealthChanged = function(msg) { - this.UnitFsm.ProcessMessage(this, {"type": "HealthChanged", "from": msg.from, "to": msg.to}); + this.UnitFsm.ProcessMessage(this, { + "type": "HealthChanged", + "from": msg.from, + "to": msg.to + }); }; UnitAI.prototype.OnRangeUpdate = function(msg) { if (msg.tag == this.losRangeQuery) - this.UnitFsm.ProcessMessage(this, {"type": "LosRangeUpdate", "data": msg}); + this.UnitFsm.ProcessMessage(this, { "type": "LosRangeUpdate", "data": msg }); else if (msg.tag == this.losHealRangeQuery) - this.UnitFsm.ProcessMessage(this, {"type": "LosHealRangeUpdate", "data": msg}); + this.UnitFsm.ProcessMessage(this, { "type": "LosHealRangeUpdate", "data": msg }); }; UnitAI.prototype.OnPackFinished = function(msg) { - this.UnitFsm.ProcessMessage(this, {"type": "PackFinished", "packed": msg.packed}); + this.UnitFsm.ProcessMessage(this, { "type": "PackFinished", "packed": msg.packed }); }; -//// Helper functions to be called by the FSM //// +// Helper functions to be called by the FSM UnitAI.prototype.GetWalkSpeed = function() { @@ -4639,9 +4679,9 @@ UnitAI.prototype.AttackEntityInZone = function(ents) { var target = ents.find(target => - this.CanAttack(target) - && this.CheckTargetDistanceFromHeldPosition(target, IID_Attack, this.GetBestAttackAgainst(target, true)) - && (this.GetStance().respondChaseBeyondVision || this.CheckTargetIsInVisionRange(target)) + this.CanAttack(target) && + this.CheckTargetDistanceFromHeldPosition(target, IID_Attack, this.GetBestAttackAgainst(target, true)) && + (this.GetStance().respondChaseBeyondVision || this.CheckTargetIsInVisionRange(target)) ); if (!target) return false; @@ -4704,11 +4744,11 @@ // If we are guarding/escorting, don't abandon as long as the guarded unit is in target range of the attacker if (this.isGuardOf) { - var cmpUnitAI = Engine.QueryInterface(target, IID_UnitAI); + var cmpUnitAI = Engine.QueryInterface(target, IID_UnitAI); var cmpAttack = Engine.QueryInterface(target, IID_Attack); if (cmpUnitAI && cmpAttack && cmpAttack.GetAttackTypes().some(type => cmpUnitAI.CheckTargetAttackRange(this.isGuardOf, type))) - return false; + return false; } // Stop if we're in hold-ground mode and it's too far from the holding point @@ -4746,7 +4786,7 @@ // If we are guarding/escorting, chase at least as long as the guarded unit is in target range of the attacker if (this.isGuardOf) { - let cmpUnitAI = Engine.QueryInterface(target, IID_UnitAI); + let cmpUnitAI = Engine.QueryInterface(target, IID_UnitAI); let cmpAttack = Engine.QueryInterface(target, IID_Attack); if (cmpUnitAI && cmpAttack && cmpAttack.GetAttackTypes().some(type => cmpUnitAI.CheckTargetAttackRange(this.isGuardOf, type))) @@ -4759,7 +4799,7 @@ return false; }; -//// External interface functions //// +// External interface functions UnitAI.prototype.SetFormationController = function(ent) { @@ -4915,8 +4955,7 @@ { if (this.isGuardOf == target && this.order && this.order.type == "Guard") return; - else - this.RemoveGuard(); + this.RemoveGuard(); } this.AddOrder("Guard", { "target": target, "force": false }, queued); @@ -5247,7 +5286,7 @@ this.workOrders.length && this.workOrders[0].type == "Trade") { let cmpTrader = Engine.QueryInterface(this.entity, IID_Trader); - if (cmpTrader.HasBothMarkets() && + if (cmpTrader.HasBothMarkets() && (cmpTrader.GetFirstMarket() == target && cmpTrader.GetSecondMarket() == source || cmpTrader.GetFirstMarket() == source && cmpTrader.GetSecondMarket() == target)) { @@ -5528,11 +5567,11 @@ { var cmpIdentity = Engine.QueryInterface(targ, IID_Identity); var targetClasses = this.order.data.targetClasses; - if (targetClasses.attack && cmpIdentity - && !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack)) + if (targetClasses.attack && cmpIdentity && + !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack)) continue; - if (targetClasses.avoid && cmpIdentity - && MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid)) + if (targetClasses.avoid && cmpIdentity && + MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid)) continue; // Only used by the AIs to prevent some choices of targets if (targetClasses.vetoEntities && targetClasses.vetoEntities[targ]) @@ -5554,11 +5593,11 @@ { var cmpIdentity = Engine.QueryInterface(targ, IID_Identity); var targetClasses = this.order.data.targetClasses; - if (cmpIdentity && targetClasses.attack - && !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack)) + if (cmpIdentity && targetClasses.attack && + !MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.attack)) continue; - if (cmpIdentity && targetClasses.avoid - && MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid)) + if (cmpIdentity && targetClasses.avoid && + MatchesClassList(cmpIdentity.GetClassesList(), targetClasses.avoid)) continue; // Only used by the AIs to prevent some choices of targets if (targetClasses.vetoEntities && targetClasses.vetoEntities[targ]) @@ -5714,7 +5753,7 @@ UnitAI.prototype.SetHeldPosition = function(x, z) { - this.heldPosition = {"x": x, "z": z}; + this.heldPosition = { "x": x, "z": z }; }; UnitAI.prototype.SetHeldPositionOnEntity = function(entity) @@ -5741,7 +5780,7 @@ return false; }; -//// Helper functions //// +// Helper functions UnitAI.prototype.CanAttack = function(target) { @@ -5945,16 +5984,16 @@ return cmpPack && cmpPack.IsPacking(); }; -//// Formation specific functions //// +// Formation specific functions UnitAI.prototype.IsAttackingAsFormation = function() { var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); - return cmpAttack && cmpAttack.CanAttackAsFormation() - && this.GetCurrentState() == "FORMATIONCONTROLLER.COMBAT.ATTACKING"; + return cmpAttack && cmpAttack.CanAttackAsFormation() && + this.GetCurrentState() == "FORMATIONCONTROLLER.COMBAT.ATTACKING"; }; -//// Animal specific functions //// +// Animal specific functions UnitAI.prototype.MoveRandomly = function(distance) {