From f814510b2e7eec0f01e75a4c9dad6e2cfbc8f6c3 Mon Sep 17 00:00:00 2001 From: mimo Date: Sun, 5 Jan 2014 18:29:38 +0000 Subject: [PATCH] revise the chase and flee This was SVN commit r14517. --- .../public/simulation/components/UnitAI.js | 47 +++++++++++++++---- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index 76b469d367..568bfd2d8d 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -376,15 +376,8 @@ var UnitFsmSpec = { "Order.Flee": function(msg) { // We use the distance between the entities to account for ranged attacks var distance = DistanceBetweenEntities(this.entity, this.order.data.target) + (+this.template.FleeDistance); - var cmpTargetPosition = Engine.QueryInterface(this.order.data.target, IID_Position); - if (!cmpTargetPosition) - { - this.StopMoving(); - this.FinishOrder(); - return; - } - var pos = cmpTargetPosition.GetPosition2D(); - if (this.MoveToPointRange(pos.x, pos.y, distance, -1)) + var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); + if (cmpUnitMotion.MoveToTargetRange(this.order.data.target, distance, -1)) { // We've started fleeing from the given target if (this.IsAnimal()) @@ -1554,6 +1547,11 @@ var UnitFsmSpec = { this.SetMoveSpeed(speed); }, + "HealthChanged": function() { + var speed = this.GetRunSpeed(); + this.SetMoveSpeed(speed); + }, + "leave": function() { // Reset normal speed this.SetMoveSpeed(this.GetWalkSpeed()); @@ -1827,10 +1825,27 @@ var UnitFsmSpec = { this.SetGathererAnimationOverride(true); this.SelectAnimation("move"); + var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); + if (cmpUnitAI && cmpUnitAI.IsFleeing()) + { + // Run after a fleeing target + var speed = this.GetRunSpeed(); + this.SetMoveSpeed(speed); + } this.StartTimer(1000, 1000); }, + "HealthChanged": function() { + var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); + if (!cmpUnitAI || !cmpUnitAI.IsFleeing()) + return; + var speed = this.GetRunSpeed(); + this.SetMoveSpeed(speed); + }, + "leave": function() { + // Reset normal speed in case it was changed + this.SetMoveSpeed(this.GetWalkSpeed()); // Show carried resources when walking. this.SetGathererAnimationOverride(); @@ -3031,6 +3046,12 @@ UnitAI.prototype.IsGarrisoned = function() return this.isGarrisoned; }; +UnitAI.prototype.IsFleeing = function() +{ + var state = this.GetCurrentState().split(".").pop(); + return (state == "FLEEING"); +}; + UnitAI.prototype.IsWalking = function() { var state = this.GetCurrentState().split(".").pop(); @@ -3672,7 +3693,13 @@ UnitAI.prototype.GetWalkSpeed = function() UnitAI.prototype.GetRunSpeed = function() { var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); - return cmpUnitMotion.GetRunSpeed(); + var runSpeed = cmpUnitMotion.GetRunSpeed(); + var walkSpeed = cmpUnitMotion.GetWalkSpeed(); + if (runSpeed <= walkSpeed) + return runSpeed; + var cmpHealth = Engine.QueryInterface(this.entity, IID_Health); + var health = cmpHealth.GetHitpoints()/cmpHealth.GetMaxHitpoints(); + return (health*runSpeed + (1-health)*walkSpeed); }; /**