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 @@ -202,7 +202,7 @@ // Called when being told to walk as part of a formation "Order.FormationWalk": function(msg) { // Let players move captured domestic animals around - if (this.IsAnimal() && !this.IsDomestic() || this.IsTurret()) + if (this.IsTurret()) { this.FinishOrder(); return; @@ -240,7 +240,7 @@ "Order.Stop": function(msg) { // We have no control over non-domestic animals. - if (this.IsAnimal() && !this.IsDomestic()) + if (!this.IsDomestic()) { this.FinishOrder(); return; @@ -251,9 +251,7 @@ this.FinishOrder(); // No orders left, we're an individual now - if (this.IsAnimal()) - this.SetNextState("ANIMAL.IDLE"); - else if (this.IsFormationMember()) + if (this.IsFormationMember()) this.SetNextState("FORMATIONMEMBER.IDLE"); else this.SetNextState("INDIVIDUAL.IDLE"); @@ -262,7 +260,7 @@ "Order.Walk": function(msg) { // Let players move captured domestic animals around - if (this.IsAnimal() && !this.IsDomestic() || this.IsTurret()) + if (this.IsTurret()) { this.FinishOrder(); return; @@ -280,15 +278,12 @@ 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"); - else - this.SetNextState("INDIVIDUAL.WALKING"); + this.SetNextState("INDIVIDUAL.WALKING"); }, "Order.WalkAndFight": function(msg) { // Let players move captured domestic animals around - if (this.IsAnimal() && !this.IsDomestic() || this.IsTurret()) + if (this.IsTurret()) { this.FinishOrder(); return; @@ -306,16 +301,13 @@ 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 - else - this.SetNextState("INDIVIDUAL.WALKINGANDFIGHTING"); + this.SetNextState("INDIVIDUAL.WALKINGANDFIGHTING"); }, "Order.WalkToTarget": function(msg) { // Let players move captured domestic animals around - if (this.IsAnimal() && !this.IsDomestic() || this.IsTurret()) + if (this.IsTurret()) { this.FinishOrder(); return; @@ -341,10 +333,7 @@ // 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"); - else - this.SetNextState("INDIVIDUAL.WALKING"); + this.SetNextState("INDIVIDUAL.WALKING"); }, "Order.PickupUnit": function(msg) { @@ -391,10 +380,7 @@ }, "Order.Flee": function(msg) { - if (this.IsAnimal()) - this.SetNextState("ANIMAL.FLEEING"); - else - this.SetNextState("INDIVIDUAL.FLEEING"); + this.SetNextState("INDIVIDUAL.FLEEING"); }, "Order.Attack": function(msg) { @@ -435,10 +421,7 @@ if (!this.EnsureCorrectPackStateForAttack(false)) return; - if (this.IsAnimal()) - this.SetNextState("ANIMAL.COMBAT.ATTACKING"); - else - this.SetNextState("INDIVIDUAL.COMBAT.ATTACKING"); + this.SetNextState("INDIVIDUAL.COMBAT.ATTACKING"); return; } @@ -463,14 +446,11 @@ if (!this.EnsureCorrectPackStateForAttack(true)) return; - if (this.IsAnimal()) - this.SetNextState("ANIMAL.COMBAT.APPROACHING"); - else - this.SetNextState("INDIVIDUAL.COMBAT.APPROACHING"); + this.SetNextState("INDIVIDUAL.COMBAT.APPROACHING"); }, "Order.Patrol": function(msg) { - if (this.IsAnimal() || this.IsTurret()) + if (this.IsTurret()) { this.FinishOrder(); return; @@ -630,10 +610,7 @@ } else if (this.IsGarrisoned()) { - if (this.IsAnimal()) - this.SetNextState("ANIMAL.GARRISON.GARRISONED"); - else - this.SetNextState("INDIVIDUAL.GARRISON.GARRISONED"); + this.SetNextState("INDIVIDUAL.GARRISON.GARRISONED"); return; } @@ -646,10 +623,7 @@ return; } - if (this.IsAnimal()) - this.SetNextState("ANIMAL.GARRISON.APPROACHING"); - else - this.SetNextState("INDIVIDUAL.GARRISON.APPROACHING"); + this.SetNextState("INDIVIDUAL.GARRISON.APPROACHING"); }, "Order.Ungarrison": function() { @@ -1296,15 +1270,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"); - return true; - } - let cmpFormation = Engine.QueryInterface(this.formationController, IID_Formation); if (cmpFormation) { @@ -1407,9 +1372,6 @@ "INDIVIDUAL": { "enter": function() { - // Sanity-checking - if (this.IsAnimal()) - error("Animal got moved into INDIVIDUAL.* state"); return false; }, @@ -1417,6 +1379,25 @@ // 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]); + + if (!this.template.NaturalBehaviour) + return; + + if (this.template.NaturalBehaviour == "skittish" || + this.template.NaturalBehaviour == "passive") + { + this.Flee(msg.data.attacker, false); + } + else if (this.IsDangerousAnimal() || this.template.NaturalBehaviour == "defensive") + { + if (this.CanAttack(msg.data.attacker)) + this.Attack(msg.data.attacker, false); + } + else if (this.template.NaturalBehaviour == "domestic") + { + // Never flee, stop what we were doing + this.SetNextState("IDLE"); + } }, "GuardedAttacked": function(msg) { @@ -1524,6 +1505,12 @@ }, "Timer": function(msg) { + if (this.HasNaturalBehaviour()) + { + this.SetNextState("FEEDING"); + return; + } + // If the unit is guarding/escorting, go back to its duty. if (this.isGuardOf) { @@ -3171,47 +3158,6 @@ }, }, }, - }, - - "ANIMAL": { - "Attacked": function(msg) { - if (this.template.NaturalBehaviour == "skittish" || - this.template.NaturalBehaviour == "passive") - { - this.Flee(msg.data.attacker, false); - } - else if (this.IsDangerousAnimal() || this.template.NaturalBehaviour == "defensive") - { - if (this.CanAttack(msg.data.attacker)) - this.Attack(msg.data.attacker, false); - } - else if (this.template.NaturalBehaviour == "domestic") - { - // Never flee, stop what we were doing - this.SetNextState("IDLE"); - } - }, - - "Order.LeaveFoundation": function(msg) { - // Move a tile outside the building - if (this.CheckTargetRangeExplicit(msg.data.target, g_LeaveFoundationRange, -1)) - { - this.FinishOrder(); - return; - } - this.order.data.min = g_LeaveFoundationRange; - this.SetNextState("WALKING"); - }, - - "IDLE": { - // (We need an IDLE state so that FinishOrder works) - - "enter": function() { - // Start feeding immediately - this.SetNextState("FEEDING"); - return true; - }, - }, "ROAMING": { "enter": function() { @@ -3230,7 +3176,8 @@ }, "LosRangeUpdate": function(msg) { - if (this.template.NaturalBehaviour == "skittish") + if (this.template.NaturalBehaviour && + this.template.NaturalBehaviour == "skittish") { if (msg.data.added.length > 0) { @@ -3294,16 +3241,6 @@ this.SetNextState("ROAMING"); }, }, - - "FLEEING": "INDIVIDUAL.FLEEING", // reuse the same fleeing behaviour for animals - - "COMBAT": "INDIVIDUAL.COMBAT", // reuse the same combat behaviour for animals - - "WALKING": "INDIVIDUAL.WALKING", // reuse the same walking behaviour for animals - // only used for domestic animals - - // Reuse the same garrison behaviour for animals. - "GARRISON": "INDIVIDUAL.GARRISON", }, }; @@ -3360,11 +3297,16 @@ this.finishedOrder = false; }; -UnitAI.prototype.IsAnimal = function() +UnitAI.prototype.HasNaturalBehaviour = function() { return (this.template.NaturalBehaviour ? true : false); }; +UnitAI.prototype.IsAnimal = function() +{ + return this.HasNaturalBehaviour(); +}; + UnitAI.prototype.IsDangerousAnimal = function() { return (this.IsAnimal() && (this.template.NaturalBehaviour == "violent" || @@ -3438,8 +3380,8 @@ UnitAI.prototype.OnCreate = function() { - if (this.IsAnimal()) - this.UnitFsm.Init(this, "ANIMAL.FEEDING"); + if (this.HasNaturalBehaviour()) + this.UnitFsm.Init(this, "INDIVIDUAL.FEEDING"); else if (this.IsFormationController()) this.UnitFsm.Init(this, "FORMATIONCONTROLLER.IDLE"); else @@ -3846,6 +3788,9 @@ UnitAI.prototype.WillMoveFromFoundation = function(target, checkPacking = true) { + if (this.IsAnimal()) + return true; + // If foundation is not ally of entity, or if entity is unpacked siege, // ignore the order. if (!IsOwnedByAllyOfEntity(this.entity, target) &&