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 @@ -200,6 +200,10 @@ this.SetNextState(this.GetCurrentState()); }, + "ResetHeldPosition": function() { + delete this.heldPosition; + }, + // Formation handlers: "FormationLeave": function(msg) { @@ -284,7 +288,6 @@ return; } - 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()) @@ -310,7 +313,6 @@ return; } - 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()) @@ -695,17 +697,14 @@ "FORMATIONCONTROLLER": { "Order.Walk": function(msg) { - this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); this.SetNextState("WALKING"); }, "Order.WalkAndFight": function(msg) { - this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); this.SetNextState("WALKINGANDFIGHTING"); }, "Order.MoveIntoFormation": function(msg) { - this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); this.SetNextState("FORMING"); }, @@ -732,7 +731,6 @@ }, "Order.Patrol": function(msg) { - this.CallMemberFunction("SetHeldPosition", [msg.data.x, msg.data.z]); this.SetNextState("PATROL"); }, @@ -1618,6 +1616,8 @@ this.patrolStartPosOrder.allowCapture = this.order.data.allowCapture; } + this.order.onPop = "ResetHeldPosition"; + this.StartTimer(0, 1000); this.SetAnimationVariant("combat"); return false; @@ -1631,6 +1631,13 @@ }, "Timer": function(msg) { + let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); + if (cmpPosition && cmpPosition.IsInWorld()) + { + let pos = cmpPosition.GetPosition2D(); + this.heldPosition = { "x": pos.x, "z": pos.y }; + } + this.FindWalkAndFightTargets(); }, @@ -1664,7 +1671,12 @@ this.SetAnimationVariant("combat"); this.StartTimer(0, 1000); - this.SetHeldPositionOnEntity(this.isGuardOf); + let cmpGuardedPosition = Engine.QueryInterface(this.isGuardOf, IID_Position); + if (cmpGuardedPosition && cmpGuardedPosition.IsInWorld()) + { + let pos = cmpGuardedPosition.GetPosition2D(); + this.heldPosition = { "x": pos.x, "z": pos.y }; + } return false; }, @@ -1681,7 +1693,12 @@ if (cmpObstructionManager.IsInTargetRange(this.entity, this.isGuardOf, 0, 3 * this.guardRange, false)) this.TryMatchTargetSpeed(this.isGuardOf, false); - this.SetHeldPositionOnEntity(this.isGuardOf); + let cmpGuardedPosition = Engine.QueryInterface(this.isGuardOf, IID_Position); + if (cmpGuardedPosition && cmpGuardedPosition.IsInWorld()) + { + let pos = cmpGuardedPosition.GetPosition2D(); + this.heldPosition = { "x": pos.x, "z": pos.y }; + } }, "leave": function(msg) { @@ -1701,9 +1718,15 @@ "enter": function() { this.StopMoving(); this.StartTimer(1000, 1000); - this.SetHeldPositionOnEntity(this.entity); this.SetAnimationVariant("combat"); this.FaceTowardsTarget(this.order.data.target); + + let cmpGuardedPosition = Engine.QueryInterface(this.isGuardOf, IID_Position); + if (cmpGuardedPosition && cmpGuardedPosition.IsInWorld()) + { + let pos = cmpGuardedPosition.GetPosition2D(); + this.heldPosition = { "x": pos.x, "z": pos.y }; + } return false; }, @@ -1727,6 +1750,13 @@ this.SetNextState("ESCORTING"); else { + let cmpGuardedPosition = Engine.QueryInterface(this.isGuardOf, IID_Position); + if (cmpGuardedPosition && cmpGuardedPosition.IsInWorld()) + { + let pos = cmpGuardedPosition.GetPosition2D(); + this.heldPosition = { "x": pos.x, "z": pos.y }; + } + 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); @@ -1836,10 +1866,6 @@ { this.StopMoving(); this.FinishOrder(); - - // Return to our original position - if (this.GetStance().respondHoldGround) - this.WalkToHeldPosition(); } else { @@ -2064,10 +2090,6 @@ if (this.FindNewTargets()) return true; - // Return to our original position - if (this.GetStance().respondHoldGround) - this.WalkToHeldPosition(); - return true; }, }, @@ -2114,10 +2136,6 @@ { this.StopMoving(); this.FinishOrder(); - - // Return to our original position - if (this.GetStance().respondHoldGround) - this.WalkToHeldPosition(); } else { @@ -2659,10 +2677,6 @@ if (this.FindNewHealTargets()) return true; - // Return to our original position - if (this.GetStance().respondHoldGround) - this.WalkToHeldPosition(); - // We quit this state right away. return true; }, @@ -3353,8 +3367,6 @@ this.isIdle = false; this.isImmobile = false; // True if the unit is currently unable to move (garrisoned,...) - this.heldPosition = undefined; - // Queue of remembered works this.workOrders = []; @@ -3712,6 +3724,9 @@ error("FinishOrder called for entity " + this.entity + " (" + template + ") when order queue is empty\n" + stack); } + if (this.orderQueue[0].onPop) + this.UnitFsm.ProcessMessage(this, { "type": this.orderQueue[0].onPop }); + this.orderQueue.shift(); this.order = this.orderQueue[0]; @@ -3930,12 +3945,18 @@ { var order = { "type": type, "data": data }; var cheeringOrder = this.orderQueue.shift(); + for (let order of this.orderQueue) + if (order.onPop) + this.UnitFsm.ProcessMessage(this, { "type": order.onPop }); this.orderQueue = [cheeringOrder, order]; } else if (this.IsPacking() && type != "CancelPack" && type != "CancelUnpack") { var order = { "type": type, "data": data }; var packingOrder = this.orderQueue.shift(); + for (let order of this.orderQueue) + if (order.onPop) + this.UnitFsm.ProcessMessage(this, { "type": order.onPop }); if (type == "Attack") { // The Attack order is able to handle a packing unit, while other orders can't. @@ -3947,6 +3968,8 @@ // Immediately cancel unpacking before processing an order that demands a packed unit. let cmpPack = Engine.QueryInterface(this.entity, IID_Pack); cmpPack.CancelPack(); + if (packingOrder.onPop) + this.UnitFsm.ProcessMessage(this, { "type": packingOrder.onPop }); this.orderQueue = []; this.PushOrder(type, data); } @@ -3955,6 +3978,10 @@ } else { + for (let order of this.orderQueue) + if (order.onPop) + this.UnitFsm.ProcessMessage(this, { "type": order.onPop }); + this.orderQueue = []; this.PushOrder(type, data); } @@ -4875,9 +4902,8 @@ let halfvision = cmpVision.GetRange() / 2; let pos = cmpPosition.GetPosition(); - let heldPosition = this.heldPosition; - if (heldPosition === undefined) - heldPosition = { "x": pos.x, "z": pos.z }; + warn("heldpos " + uneval(this.heldPosition)); + let heldPosition = this.heldPosition ? this.heldPosition : { "x": pos.x, "z": pos.z }; return Math.euclidDistance2D(pos.x, pos.z, heldPosition.x, heldPosition.z) < halfvision + range.max; }; @@ -5768,8 +5794,6 @@ var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); if (!cmpPosition || !cmpPosition.IsInWorld()) return; - var pos = cmpPosition.GetPosition(); - this.SetHeldPosition(pos.x, pos.z); this.SetStance(stance); // Stop moving if switching to stand ground @@ -6026,35 +6050,6 @@ orderData.lastPos = cmpPosition.GetPosition(); }; -UnitAI.prototype.SetHeldPosition = function(x, z) -{ - this.heldPosition = {"x": x, "z": z}; -}; - -UnitAI.prototype.SetHeldPositionOnEntity = function(entity) -{ - var cmpPosition = Engine.QueryInterface(this.entity, IID_Position); - if (!cmpPosition || !cmpPosition.IsInWorld()) - return; - var pos = cmpPosition.GetPosition(); - this.SetHeldPosition(pos.x, pos.z); -}; - -UnitAI.prototype.GetHeldPosition = function() -{ - return this.heldPosition; -}; - -UnitAI.prototype.WalkToHeldPosition = function() -{ - if (this.heldPosition) - { - this.AddOrder("Walk", { "x": this.heldPosition.x, "z": this.heldPosition.z, "force": false }, false); - return true; - } - return false; -}; - //// Helper functions //// /** Index: binaries/data/mods/public/simulation/helpers/Transform.js =================================================================== --- binaries/data/mods/public/simulation/helpers/Transform.js +++ binaries/data/mods/public/simulation/helpers/Transform.js @@ -62,9 +62,6 @@ var cmpNewUnitAI = Engine.QueryInterface(newEnt, IID_UnitAI); if (cmpUnitAI && cmpNewUnitAI) { - let pos = cmpUnitAI.GetHeldPosition(); - if (pos) - cmpNewUnitAI.SetHeldPosition(pos.x, pos.z); if (cmpUnitAI.GetStanceName()) cmpNewUnitAI.SwitchToStance(cmpUnitAI.GetStanceName()); cmpNewUnitAI.AddOrders(cmpUnitAI.GetOrders());