Merge pull request #391 from SHADE-DP/AIFixes

Implemented such that AI gets sent to a waypoint of choice (in editor) after capture
This commit is contained in:
XiaoQiDigipen 2023-03-04 12:50:19 +08:00 committed by GitHub
commit ba78f6f7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 17 deletions

View File

@ -4243,6 +4243,7 @@
- Type: Homeowner1
Enabled: true
waypointsPool: 234
startWaypoint: 233
patrolSpeed: 3
chaseSpeed: 6
turningSpeed: 5

View File

@ -5912,7 +5912,7 @@
Components: ~
Scripts: ~
- EID: 165
Name: 1
Name: 0
IsActive: true
NumberOfChildren: 0
Components:
@ -5923,7 +5923,7 @@
IsActive: true
Scripts: ~
- EID: 164
Name: 2
Name: 1
IsActive: true
NumberOfChildren: 0
Components:
@ -5934,7 +5934,7 @@
IsActive: true
Scripts: ~
- EID: 163
Name: 3
Name: 2
IsActive: true
NumberOfChildren: 0
Components:
@ -5945,7 +5945,7 @@
IsActive: true
Scripts: ~
- EID: 162
Name: 4
Name: 3
IsActive: true
NumberOfChildren: 0
Components:
@ -5956,7 +5956,7 @@
IsActive: true
Scripts: ~
- EID: 161
Name: 5
Name: 4
IsActive: true
NumberOfChildren: 0
Components:
@ -5967,7 +5967,7 @@
IsActive: true
Scripts: ~
- EID: 160
Name: 6
Name: 5
IsActive: true
NumberOfChildren: 0
Components:
@ -5978,7 +5978,7 @@
IsActive: true
Scripts: ~
- EID: 159
Name: 7
Name: 6
IsActive: true
NumberOfChildren: 0
Components:
@ -5994,7 +5994,7 @@
NumberOfChildren: 0
Components:
Transform Component:
Translate: {x: 0.0576689839, y: 0, z: -2.61272359}
Translate: {x: -3.45969725, y: 0, z: -2.61272359}
Rotate: {x: -0, y: 0, z: -0}
Scale: {x: 1, y: 1, z: 1}
IsActive: true
@ -6032,12 +6032,13 @@
- Type: Homeowner1
Enabled: true
waypointsPool: 166
startWaypoint: 162
patrolSpeed: 3
chaseSpeed: 6
turningSpeed: 5
sightDistance: 8
eyeOffset: [0, 1.64999998, 0]
distanceToCapture: 0.5
distanceToCapture: 0.800000012
captureTime: 0.5
footstepSFXIntervalMultiplier: 0.5
- EID: 12
@ -6167,7 +6168,7 @@
Components:
Transform Component:
Translate: {x: 0, y: -300, z: 0}
Rotate: {x: -4.5, y: 2, z: -2.5}
Rotate: {x: 3.25, y: 0.5, z: 4.5}
Scale: {x: 400, y: 100, z: 500}
IsActive: true
Renderable Component:

View File

@ -37,6 +37,9 @@ public partial class Homeowner1 : BehaviourTree
private List<GameObject> waypoints;
[SerializeField]
private GameObject startWaypoint;
[SerializeField]
[Tooltip("The AI will patrol at this speed")]
private float patrolSpeed;
@ -157,6 +160,8 @@ public partial class Homeowner1 : BehaviourTree
SetData("distanceToCapture", distanceToCapture);
if (GetData("baseCaptureTime") == null || (float)GetData("baseCaptureTime") != captureTime)
SetData("baseCaptureTime", captureTime);
if (GetData("startWaypoint") == null || (GameObject)GetData("startWaypoint") != startWaypoint)
SetData("startWaypoint", startWaypoint);
events.Tick();

View File

@ -12,6 +12,7 @@
using SHADE;
using SHADE_Scripting.AIBehaviour.BehaviourTree;
using SHADE_Scripting.Audio;
using System;
using System.Collections.Generic;
using System.Linq;
@ -98,6 +99,35 @@ public partial class LeafAttack : BehaviourTreeNode
if (player.GetScript<PlayerController>().stateMachine && !player.GetScript<PlayerController>().stateMachine.IsState(typeof(PlayerCaughtState)))
player.GetScript<PlayerController>().stateMachine.SetState(typeof(PlayerCaughtState));
//Teleport AI back to home waypoint
int homeIndex = 0;
GameObject? startWaypoint = null;
List<GameObject>? waypoints = (List<GameObject>)GetNodeData("waypoints");
if (GetNodeData("startWaypoint") != null)
startWaypoint = (GameObject)GetNodeData("startWaypoint");
if (startWaypoint != null)
{
for (int i = 0; i < waypoints.Count; ++i)
{
if (startWaypoint == waypoints[i])
{
homeIndex = i;
}
}
}
SetNodeData("currentWaypointIndex", homeIndex);
SetNodeData("playerLastSightedWaypointIndex", homeIndex);
SetNodeData("isAlert", false);
Audio.SetParameterWithLabel("PlayerDetection", "Undetected");
AudioHandler.audioClipHandlers["SFXHumming"].Play();
Transform? transform = (Transform)GetNodeData("transform");
if (waypoints != null && transform != null)
{
transform.GlobalPosition = waypoints[homeIndex].GetComponent<Transform>().GlobalPosition;
}
ClearNodeData("target");
status = BehaviourTreeNodeStatus.SUCCESS;
onExit(BehaviourTreeNodeStatus.SUCCESS);
return status;

View File

@ -60,7 +60,7 @@ public partial class LeafChase : BehaviourTreeNode
}
}
//Debug.Log("Player is nearest " + nearestWaypointIndex);
//Debug.Log("I'm at " + (int)GetNodeData("currentWaypointIndex"));
//Debug.Log("AI going to " + (int)GetNodeData("currentWaypointIndex"));
SetNodeData("playerLastSightedWaypointIndex", nearestWaypointIndex);
}
@ -78,6 +78,7 @@ public partial class LeafChase : BehaviourTreeNode
GetNodeData("distanceToCapture") == null ||
GetNodeData("baseCaptureTime") == null)
{
//Debug.Log("Chase Failure: null values");
status = BehaviourTreeNodeStatus.FAILURE;
onExit(BehaviourTreeNodeStatus.FAILURE);
return status;

View File

@ -29,7 +29,7 @@ public partial class LeafPatrol : BehaviourTreeNode
private float chaseSpeed;
private float turningSpeed;
private float retreatTimer = 0.0f;
private int currentWaypointIndex = 0;
private int currentWaypointIndex = -1;
private bool retreatState = false;
private bool goingForwards = true;
@ -47,7 +47,7 @@ public partial class LeafPatrol : BehaviourTreeNode
//node, and hence we do not need to inherit its constructors
public LeafPatrol(string name) : base(name)
{
currentWaypointIndex = 0;
currentWaypointIndex = -1;
}
//When it comes to evaluating,
@ -57,6 +57,42 @@ public partial class LeafPatrol : BehaviourTreeNode
//Debug.LogWarning("LeafPatrol");
onEnter(BehaviourTreeNodeStatus.RUNNING);
//Initialise home waypoint
waypoints = (List<GameObject>)GetNodeData("waypoints");
if (currentWaypointIndex == -1)
{
if (waypoints != null)
{
//Debug.Log("Waypoints not null");
if (GetNodeData("startWaypoint") != null)
{
//Debug.Log("Getting start waypoint");
GameObject startWaypoint = (GameObject)(GetNodeData("startWaypoint"));
for (int i = 0; i < waypoints.Count; ++i)
{
if (startWaypoint == waypoints[i])
{
//Debug.Log("Leaf Patrol Constructor: Start Waypoint Index: " + i.ToString());
currentWaypointIndex = i;
SetNodeData("currentWaypointIndex", i);
}
}
}
else
{
status = BehaviourTreeNodeStatus.FAILURE;
onExit(BehaviourTreeNodeStatus.FAILURE);
return status;
}
}
else
{
status = BehaviourTreeNodeStatus.FAILURE;
onExit(BehaviourTreeNodeStatus.FAILURE);
return status;
}
}
//Get data
if (GetNodeData("transform") == null ||
GetNodeData("patrolSpeed") == null ||
@ -104,12 +140,13 @@ public partial class LeafPatrol : BehaviourTreeNode
ClearNodeData("isWaiting");
return;
}
waypoints = (List<GameObject>)GetNodeData("waypoints");
if (waypoints == null)
if (GetNodeData("currentWaypointIndex") != null)
{
return;
//2023 Mar 4, 0400, this is needed when the AI teleports back to the first spot after catching
currentWaypointIndex = (int)GetNodeData("currentWaypointIndex");
}
Vector3 targetPosition = waypoints[currentWaypointIndex].GetComponent<Transform>().GlobalPosition;
//Modulo operator to prevent out of range exceptions
Vector3 targetPosition = waypoints[(currentWaypointIndex % waypoints.Count)].GetComponent<Transform>().GlobalPosition;
//Reach waypoint by X and Z being near enough
//Do not consider Y of waypoints yet
Vector3 remainingDistance = targetPosition - transform.GlobalPosition;
@ -195,6 +232,8 @@ public partial class LeafPatrol : BehaviourTreeNode
//Debug.Log("AI is at " + transform.GlobalPosition.x.ToString() + " " + transform.GlobalPosition.y.ToString() + " " + transform.GlobalPosition.z.ToString());
Vector3 normalisedDifference = targetPosition - transform.GlobalPosition;
normalisedDifference.y = 0.0f; //Do not move vertically
//Debug.Log("Leaf Patrol Current Waypoint Index: " + currentWaypointIndex.ToString());
//Debug.Log("True Difference x " + normalisedDifference.x.ToString() + " z " + normalisedDifference.z.ToString());
normalisedDifference /= normalisedDifference.GetMagnitude();
//Debug.Log("Normalised Difference x " + normalisedDifference.x.ToString() + " z " + normalisedDifference.z.ToString());