From bd2fd6c713fe50059bf1ca7f729323f8d09dc8b6 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Tue, 20 Jul 2010 08:45:09 +0000 Subject: [PATCH] Extend animal AI to all animals. This was SVN commit r7771. --- .../mods/public/art/actors/fauna/deer.xml | 55 ++++++++++--------- .../public/simulation/components/AnimalAI.js | 44 ++++++++++----- .../public/simulation/components/UnitAI.js | 10 +--- .../templates/gaia/fauna_chicken.xml | 10 ++-- .../simulation/templates/gaia/fauna_deer.xml | 8 ++- .../simulation/templates/template_unit.xml | 4 +- .../templates/template_unit_fauna.xml | 11 ++++ .../template_unit_fauna_breed_passive.xml | 4 +- .../template_unit_fauna_herd_passive.xml | 4 +- .../template_unit_fauna_hunt_aggressive.xml | 4 +- .../template_unit_fauna_hunt_defensive.xml | 4 +- .../template_unit_fauna_hunt_passive.xml | 4 +- .../template_unit_fauna_hunt_skittish.xml | 4 +- .../template_unit_fauna_hunt_violent.xml | 4 +- .../template_unit_fauna_wild_aggressive.xml | 4 +- .../template_unit_fauna_wild_defensive.xml | 4 +- .../template_unit_fauna_wild_passive.xml | 4 +- .../template_unit_fauna_wild_violent.xml | 4 +- .../simulation2/components/CCmpUnitMotion.cpp | 4 +- .../simulation2/components/ICmpUnitMotion.cpp | 4 +- .../simulation2/components/ICmpUnitMotion.h | 4 +- 21 files changed, 115 insertions(+), 83 deletions(-) diff --git a/binaries/data/mods/public/art/actors/fauna/deer.xml b/binaries/data/mods/public/art/actors/fauna/deer.xml index 5dd8990007..64c0e9fb73 100644 --- a/binaries/data/mods/public/art/actors/fauna/deer.xml +++ b/binaries/data/mods/public/art/actors/fauna/deer.xml @@ -4,40 +4,45 @@ - + - - - - - + + skeletal/deer_mesh.dae - - - - skeletal/animal_deer.dds - - - - - - - - - - - - - - - skeletal/deer_mesh.dae skeletal/animal_deer.dds + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/binaries/data/mods/public/simulation/components/AnimalAI.js b/binaries/data/mods/public/simulation/components/AnimalAI.js index 0958ca2a37..64ffa3884d 100644 --- a/binaries/data/mods/public/simulation/components/AnimalAI.js +++ b/binaries/data/mods/public/simulation/components/AnimalAI.js @@ -10,6 +10,12 @@ AnimalAI.prototype.Schema = "passive" + "skittish" + "" + + "" + + "" + + "" + + "" + + "" + + "" + ""; var AnimalFsmSpec = { @@ -18,7 +24,7 @@ var AnimalFsmSpec = { "ResourceGather": function(msg) { // If someone's carving chunks of meat off us, then run away - this.MoveAwayFrom(msg.gatherer, 12); + this.MoveAwayFrom(msg.gatherer, +this.template.FleeDistance); this.SetNextState("FLEEING"); this.PlaySound("panic"); }, @@ -26,8 +32,8 @@ var AnimalFsmSpec = { "ROAMING": { "enter": function() { // Walk in a random direction - this.SelectAnimation("walk", false); - this.MoveRandomly(); + this.SelectAnimation("walk", false, this.GetWalkSpeed()); + this.MoveRandomly(+this.template.RoamDistance); // Set a random timer to switch to feeding state this.StartTimer(RandomInt(2000, 8000)); }, @@ -41,14 +47,14 @@ var AnimalFsmSpec = { }, "MoveStopped": function() { - this.MoveRandomly(); + this.MoveRandomly(+this.template.RoamDistance); }, }, "FEEDING": { "enter": function() { // Stop and eat for a while - this.SelectAnimation("idle"); + this.SelectAnimation("feeding"); this.StopMoving(); this.StartTimer(RandomInt(1000, 4000)); }, @@ -67,13 +73,14 @@ var AnimalFsmSpec = { "FLEEING": { "enter": function() { // Run quickly - this.SelectAnimation("run", false); - this.SetMoveSpeedFactor(6.0); + var speed = this.GetRunSpeed(); + this.SelectAnimation("run", false, speed); + this.SetMoveSpeed(speed); }, "leave": function() { // Reset normal speed - this.SetMoveSpeedFactor(1.0); + this.SetMoveSpeed(this.GetWalkSpeed()); }, "MoveStopped": function() { @@ -95,7 +102,7 @@ AnimalAI.prototype.Init = function() AnimalAI.prototype.OnCreate = function() { - AnimalFsm.Init(this, "SKITTISH.ROAMING"); + AnimalFsm.Init(this, "SKITTISH.FEEDING"); }; AnimalAI.prototype.SetNextState = function(state) @@ -139,6 +146,18 @@ AnimalAI.prototype.TimerHandler = function(data, lateness) // Functions to be called by the FSM: +AnimalAI.prototype.GetWalkSpeed = function() +{ + var cmpMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); + return cmpMotion.GetWalkSpeed(); +}; + +AnimalAI.prototype.GetRunSpeed = function() +{ + var cmpMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); + return cmpMotion.GetRunSpeed(); +}; + AnimalAI.prototype.PlaySound = function(name) { PlaySound(name, this.entity); @@ -169,7 +188,7 @@ AnimalAI.prototype.SelectAnimation = function(name, once, speed, sound) cmpVisual.SelectAnimation(name, once, speed, soundgroup); }; -AnimalAI.prototype.MoveRandomly = function() +AnimalAI.prototype.MoveRandomly = function(distance) { // We want to walk in a random direction, but avoid getting stuck // in obstacles or narrow spaces. @@ -189,7 +208,6 @@ AnimalAI.prototype.MoveRandomly = function() var pos = cmpPosition.GetPosition(); - var distance = 4; var jitter = 0.5; // Randomly adjust the range's center a bit, so we tend to prefer @@ -213,10 +231,10 @@ AnimalAI.prototype.StopMoving = function() cmpMotion.StopMoving(); }; -AnimalAI.prototype.SetMoveSpeedFactor = function(factor) +AnimalAI.prototype.SetMoveSpeed = function(speed) { var cmpMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); - cmpMotion.SetSpeedFactor(factor); + cmpMotion.SetSpeed(speed); }; AnimalAI.prototype.StartTimer = function(interval, data) diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js index 284b0d3d9c..d65b246f19 100644 --- a/binaries/data/mods/public/simulation/components/UnitAI.js +++ b/binaries/data/mods/public/simulation/components/UnitAI.js @@ -37,15 +37,7 @@ function UnitAI() {} UnitAI.prototype.Schema = "Controls the unit's movement, attacks, etc, in response to commands from the player." + "" + - "" + // TODO: implement this - "" + - "violent" + - "aggressive" + - "defensive" + - "passive" + - "skittish" + - "" + - ""; + ""; UnitAI.prototype.Init = function() { diff --git a/binaries/data/mods/public/simulation/templates/gaia/fauna_chicken.xml b/binaries/data/mods/public/simulation/templates/gaia/fauna_chicken.xml index c6448f9fd5..a6a34f5060 100644 --- a/binaries/data/mods/public/simulation/templates/gaia/fauna_chicken.xml +++ b/binaries/data/mods/public/simulation/templates/gaia/fauna_chicken.xml @@ -16,12 +16,14 @@ 1.5 - skittish + 4.0 + 12.0 - 1 - default - default + 1.0 + + 6.0 + diff --git a/binaries/data/mods/public/simulation/templates/gaia/fauna_deer.xml b/binaries/data/mods/public/simulation/templates/gaia/fauna_deer.xml index 61a0f06206..b76f69e6f1 100644 --- a/binaries/data/mods/public/simulation/templates/gaia/fauna_deer.xml +++ b/binaries/data/mods/public/simulation/templates/gaia/fauna_deer.xml @@ -5,9 +5,9 @@ Deer - 5.0 + 2.0 - 12.0 + 10.0 600.0 5.0 @@ -15,4 +15,8 @@ fauna/deer.xml + + 8.0 + 32.0 + diff --git a/binaries/data/mods/public/simulation/templates/template_unit.xml b/binaries/data/mods/public/simulation/templates/template_unit.xml index 7f0f67f59a..112de4459e 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit.xml @@ -7,9 +7,7 @@ unit - - passive - + 1 diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna.xml index 5aba766538..3454dd588d 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna.xml @@ -16,4 +16,15 @@ 2.5 + + 2.0 + + 6.0 + + + + + 4.0 + 12.0 + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_breed_passive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_breed_passive.xml index 9bedf79809..c0064a0745 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_breed_passive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_breed_passive.xml @@ -2,7 +2,7 @@ - + passive - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_herd_passive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_herd_passive.xml index 15d6927d22..530e979b30 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_herd_passive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_herd_passive.xml @@ -2,7 +2,7 @@ - + passive - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_aggressive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_aggressive.xml index 1c9eb30ed3..ee8949679f 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_aggressive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_aggressive.xml @@ -2,7 +2,7 @@ - + aggressive - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_defensive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_defensive.xml index e4969a8376..37bf7b438b 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_defensive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_defensive.xml @@ -2,7 +2,7 @@ - + defensive - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_passive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_passive.xml index f5aee67c91..3ba6cf8fcf 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_passive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_passive.xml @@ -2,7 +2,7 @@ - + passive - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_skittish.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_skittish.xml index 4e6b672730..5041f72dbb 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_skittish.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_skittish.xml @@ -2,7 +2,7 @@ - + skittish - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_violent.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_violent.xml index 899335b614..a894caf37e 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_violent.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_hunt_violent.xml @@ -2,7 +2,7 @@ - + violent - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_aggressive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_aggressive.xml index 42dd3423a5..e151926438 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_aggressive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_aggressive.xml @@ -2,7 +2,7 @@ - + violent - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_defensive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_defensive.xml index 29d6320627..a37f8ea63a 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_defensive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_defensive.xml @@ -2,7 +2,7 @@ - + defensive - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_passive.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_passive.xml index 5beb3c57d4..4c893034c2 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_passive.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_passive.xml @@ -2,7 +2,7 @@ - + passive - + diff --git a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_violent.xml b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_violent.xml index 42dd3423a5..e151926438 100644 --- a/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_violent.xml +++ b/binaries/data/mods/public/simulation/templates/template_unit_fauna_wild_violent.xml @@ -2,7 +2,7 @@ - + violent - + diff --git a/source/simulation2/components/CCmpUnitMotion.cpp b/source/simulation2/components/CCmpUnitMotion.cpp index 0f3ab6d478..a0e0a86934 100644 --- a/source/simulation2/components/CCmpUnitMotion.cpp +++ b/source/simulation2/components/CCmpUnitMotion.cpp @@ -214,9 +214,9 @@ public: return m_RunSpeed; } - virtual void SetSpeedFactor(fixed factor) + virtual void SetSpeed(fixed speed) { - m_Speed = m_WalkSpeed.Multiply(factor); + m_Speed = speed; } virtual void SetDebugOverlay(bool enabled) diff --git a/source/simulation2/components/ICmpUnitMotion.cpp b/source/simulation2/components/ICmpUnitMotion.cpp index 418e719c1b..1118bfc1eb 100644 --- a/source/simulation2/components/ICmpUnitMotion.cpp +++ b/source/simulation2/components/ICmpUnitMotion.cpp @@ -27,6 +27,8 @@ DEFINE_INTERFACE_METHOD_3("IsInAttackRange", bool, ICmpUnitMotion, IsInAttackRan DEFINE_INTERFACE_METHOD_3("MoveToAttackRange", bool, ICmpUnitMotion, MoveToAttackRange, entity_id_t, entity_pos_t, entity_pos_t) DEFINE_INTERFACE_METHOD_4("MoveToPointRange", bool, ICmpUnitMotion, MoveToPointRange, entity_pos_t, entity_pos_t, entity_pos_t, entity_pos_t) DEFINE_INTERFACE_METHOD_0("StopMoving", void, ICmpUnitMotion, StopMoving) -DEFINE_INTERFACE_METHOD_1("SetSpeedFactor", void, ICmpUnitMotion, SetSpeedFactor, fixed) +DEFINE_INTERFACE_METHOD_1("SetSpeed", void, ICmpUnitMotion, SetSpeed, fixed) +DEFINE_INTERFACE_METHOD_0("GetWalkSpeed", fixed, ICmpUnitMotion, GetWalkSpeed) +DEFINE_INTERFACE_METHOD_0("GetRunSpeed", fixed, ICmpUnitMotion, GetRunSpeed) DEFINE_INTERFACE_METHOD_1("SetDebugOverlay", void, ICmpUnitMotion, SetDebugOverlay, bool) END_INTERFACE_WRAPPER(UnitMotion) diff --git a/source/simulation2/components/ICmpUnitMotion.h b/source/simulation2/components/ICmpUnitMotion.h index 7fe0b60cb3..e8b7013716 100644 --- a/source/simulation2/components/ICmpUnitMotion.h +++ b/source/simulation2/components/ICmpUnitMotion.h @@ -71,9 +71,9 @@ public: virtual void StopMoving() = 0; /** - * Set the current movement speed to be the default multiplied by the given factor. + * Set the current movement speed. */ - virtual void SetSpeedFactor(fixed factor) = 0; + virtual void SetSpeed(fixed speed) = 0; /** * Get the default speed that this unit will have when walking, in metres per second.