From 9dd180c1e6e887779ebb2e2855383543a2bb5824 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Fri, 24 Mar 2023 07:41:05 +0800 Subject: [PATCH] Fix for when end point is unreachable. Find closest reachable point --- Assets/Scenes/NavigationTest.shade | 6 +- .../AIBehaviour/AIRework/BaseState.cs | 38 +++++++++ .../AIBehaviour/AIRework/BaseState.cs.shmeta | 3 + .../AIBehaviour/AIRework/HomeOwnerAI.cs | 17 ++++ .../AIRework/HomeOwnerAI.cs.shmeta | 3 + .../AIRework/NavigationTestScript.cs | 25 ++++-- .../AIBehaviour/AIRework/StateMachine.cs | 80 +++++++++++++++++++ .../AIRework/StateMachine.cs.shmeta | 3 + .../AIBehaviour/AIRework/States/IdleState.cs | 27 +++++++ .../AIRework/States/IdleState.cs.shmeta | 3 + .../src/Navigation/SHNavigationSystem.cpp | 33 +++++--- 11 files changed, 219 insertions(+), 19 deletions(-) create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs.shmeta create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs.shmeta create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs.shmeta create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs create mode 100644 Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs.shmeta diff --git a/Assets/Scenes/NavigationTest.shade b/Assets/Scenes/NavigationTest.shade index 454f56a2..c4f5a3aa 100644 --- a/Assets/Scenes/NavigationTest.shade +++ b/Assets/Scenes/NavigationTest.shade @@ -261,7 +261,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 8.5, y: -1, z: -8.20610714} + Translate: {x: 8.5, y: -1, z: -6.41661787} Rotate: {x: -0, y: 0, z: -0} Scale: {x: 5, y: 5, z: 5} IsActive: true @@ -274,7 +274,7 @@ Forward: {x: 0, y: 0, z: 0} Recalculate Path: true Unreachable Target: false - Acceptance threshold: 0.100000001 + Acceptance threshold: 1 IsActive: true Scripts: - Type: SHADE_Scripting.Gameplay.AIBehaviour.AIRework.NavigationTestScript @@ -287,7 +287,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: -0.940230131, y: -1, z: -2.05140448} + Translate: {x: -0.853474855, y: -1, z: 0.354041398} Rotate: {x: -0, y: 0, z: -0} Scale: {x: 5, y: 5, z: 5} IsActive: true diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs new file mode 100644 index 00000000..22595f56 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SHADE; + +namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework +{ + public abstract class BaseState + { + protected string stateName = "Base State"; + protected StateMachine machine; + + protected BaseState(StateMachine stateMachine) + { + machine = stateMachine; + } + + public virtual void OnEnter() + { + + } + + public abstract void Update(); + + public virtual void OnExit() + { + + } + + + public string GetStateName() + { + return stateName; + } + } +} diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs.shmeta b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs.shmeta new file mode 100644 index 00000000..a0d4e7c8 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/BaseState.cs.shmeta @@ -0,0 +1,3 @@ +Name: BaseState +ID: 167091082 +Type: 9 diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs new file mode 100644 index 00000000..6e756a78 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SHADE; + +namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework +{ + public class HomeOwnerAI:Script + { + + + + + } +} diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs.shmeta b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs.shmeta new file mode 100644 index 00000000..354ae816 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/HomeOwnerAI.cs.shmeta @@ -0,0 +1,3 @@ +Name: HomeOwnerAI +ID: 162553450 +Type: 9 diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/NavigationTestScript.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/NavigationTestScript.cs index c8687820..4dde63d7 100644 --- a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/NavigationTestScript.cs +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/NavigationTestScript.cs @@ -4,30 +4,45 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using SHADE; -using static System.IO.Enumeration.FileSystemEnumerable; namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework { + + + public class NavigationTestScript :Script { public GameObject endPoint; public float speed = 1.0f; + float timer = 0.0f; protected override void start() { - + + Navigation nav = GetComponent(); + Transform endTransform = endPoint.GetComponent(); + if (endTransform) + nav.MoveTo(endTransform.GlobalPosition); } protected override void update() { + timer += Time.DeltaTimeF; Navigation nav = GetComponent(); Transform transform = GetComponent(); + if (timer >= 1.0f) + { + timer = 0.0f; + + Transform endTransform = endPoint.GetComponent(); + if (endTransform) + nav.MoveTo(endTransform.GlobalPosition); + } + + if (nav && transform) { - Transform endTransform = endPoint.GetComponent(); - if(endTransform) - nav.MoveTo(endTransform.GlobalPosition); transform.LocalPosition = transform.LocalPosition + ( nav.GetForward() * Time.DeltaTimeF * speed); } diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs new file mode 100644 index 00000000..07600772 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SHADE; + + +namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework +{ + public class StateMachine: Script + { + private Dictionary stateDictionary; + public BaseState currentState = null; + public string currentStateName; + + public void InitStateMachine(Dictionary dictionary) + { + stateDictionary = dictionary; + currentState = stateDictionary.First().Value; + currentStateName = currentState.GetStateName(); + currentState.OnEnter(); + } + + public bool HasState(Type type) + { + if(!type.IsSubclassOf(typeof(BaseState))) + { + return false; + } + else + { + return stateDictionary.ContainsKey(type); + } + } + public void SetState(Type type) + { + if (!type.IsSubclassOf(typeof(BaseState))) + { + return; + } + + + if (stateDictionary.ContainsKey(type)) + { + currentState.OnExit(); + currentState = stateDictionary[type]; + currentState.OnEnter(); + } + else + { + SetState(stateDictionary.First().Key); + } + } + + public BaseState GetState(Type type) + { + if (!stateDictionary.ContainsKey(type)) + return null; + + return stateDictionary[type]; + } + + protected override void update() + { + if(currentState != null) + { + currentStateName = currentState.GetStateName().ToString(); + currentState.Update(); + } + } + + public bool IsState(Type type) + { + return (currentState.GetType() == type); + } + + + } +} diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs.shmeta b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs.shmeta new file mode 100644 index 00000000..a36b2ec9 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/StateMachine.cs.shmeta @@ -0,0 +1,3 @@ +Name: StateMachine +ID: 165140157 +Type: 9 diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs new file mode 100644 index 00000000..438a585c --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SHADE; + +namespace SHADE_Scripting.Gameplay.AIBehaviour.AIRework.States +{ + public class IdleState: BaseState + { + + float idleDuration = 1.0f; + + public IdleState(StateMachine machine, float idleDuration): base(machine) + { + stateName = "Idle State"; + } + + + public override void Update() + { + + } + + } +} diff --git a/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs.shmeta b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs.shmeta new file mode 100644 index 00000000..93dcc589 --- /dev/null +++ b/Assets/Scripts/Gameplay/AIBehaviour/AIRework/States/IdleState.cs.shmeta @@ -0,0 +1,3 @@ +Name: IdleState +ID: 164902316 +Type: 9 diff --git a/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp b/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp index 56778771..38457ba5 100644 --- a/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp +++ b/SHADE_Engine/src/Navigation/SHNavigationSystem.cpp @@ -337,13 +337,13 @@ namespace SHADE //Check if ending position is set to true in navigation data. NavigationGridIndex endIndex = GetNavigationGridIndex(comp.target); - if (GetNavigationData(endIndex) == true) - { - //Target position is unreachable. - SHLOG_WARNING("Navigation System: GeneratePath() target position is unreachable EID: {}", comp.GetEID()) - comp.unreachableTarget = true; - return; - } + //if (GetNavigationData(endIndex) == true) + //{ + // //Target position is unreachable. + // SHLOG_WARNING("Navigation System: GeneratePath() target position is unreachable EID: {}", comp.GetEID()) + // comp.unreachableTarget = true; + // return; + //} auto transform = SHComponentManager::GetComponent(comp.GetEID()); @@ -352,7 +352,7 @@ namespace SHADE startingNode.parent.row = NullGridIndex; startingNode.parent.column = NullGridIndex; - startingNode.h = 0; + startingNode.h = std::numeric_limits::max(); startingNode.g = 0; startingNode.f = 0; @@ -516,9 +516,20 @@ namespace SHADE //Check if there is a path. if (endNode.g == std::numeric_limits::max() || endNode.h == std::numeric_limits::max() || endNode.f == std::numeric_limits::max()) { - SHLOG_WARNING("Navigation System: End Node not found after running through algo EID: {}", comp.GetEID()) - comp.unreachableTarget = true; - return; + //SHLOG_WARNING("Navigation System: End Node not found after running through algo EID: {}", comp.GetEID()) + //comp.unreachableTarget = true; + uint32_t lowestH = std::numeric_limits::max(); + + for (std::map::iterator it = closedList.begin(); it != closedList.end(); ++it) + { + if (it->second.h < lowestH) + { + lowestH = it->second.h; + endNode = (it->second); + } + } + + //return; }