Fix for when end point is unreachable. Find closest reachable point

This commit is contained in:
maverickdgg 2023-03-24 07:41:05 +08:00
parent a785a972e4
commit 9dd180c1e6
11 changed files with 219 additions and 19 deletions

View File

@ -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

View File

@ -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;
}
}
}

View File

@ -0,0 +1,3 @@
Name: BaseState
ID: 167091082
Type: 9

View File

@ -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
{
}
}

View File

@ -0,0 +1,3 @@
Name: HomeOwnerAI
ID: 162553450
Type: 9

View File

@ -4,30 +4,45 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SHADE;
using static System.IO.Enumeration.FileSystemEnumerable<TResult>;
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<Navigation>();
Transform endTransform = endPoint.GetComponent<Transform>();
if (endTransform)
nav.MoveTo(endTransform.GlobalPosition);
}
protected override void update()
{
timer += Time.DeltaTimeF;
Navigation nav = GetComponent<Navigation>();
Transform transform = GetComponent<Transform>();
if (nav && transform)
if (timer >= 1.0f)
{
timer = 0.0f;
Transform endTransform = endPoint.GetComponent<Transform>();
if (endTransform)
nav.MoveTo(endTransform.GlobalPosition);
}
if (nav && transform)
{
transform.LocalPosition = transform.LocalPosition + ( nav.GetForward() * Time.DeltaTimeF * speed);
}

View File

@ -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<Type, BaseState> stateDictionary;
public BaseState currentState = null;
public string currentStateName;
public void InitStateMachine(Dictionary<Type,BaseState> 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);
}
}
}

View File

@ -0,0 +1,3 @@
Name: StateMachine
ID: 165140157
Type: 9

View File

@ -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()
{
}
}
}

View File

@ -0,0 +1,3 @@
Name: IdleState
ID: 164902316
Type: 9

View File

@ -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<SHTransformComponent>(comp.GetEID());
@ -352,7 +352,7 @@ namespace SHADE
startingNode.parent.row = NullGridIndex;
startingNode.parent.column = NullGridIndex;
startingNode.h = 0;
startingNode.h = std::numeric_limits<uint32_t>::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<uint32_t>::max() || endNode.h == std::numeric_limits<uint32_t>::max() || endNode.f == std::numeric_limits<uint32_t>::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<uint32_t>::max();
for (std::map<NavigationGridIndex, NavigationNode>::iterator it = closedList.begin(); it != closedList.end(); ++it)
{
if (it->second.h < lowestH)
{
lowestH = it->second.h;
endNode = (it->second);
}
}
//return;
}