Implemented such that AI gets sent to a waypoint of choice (in editor) after capture #391

Merged
mushgunAX merged 3 commits from AIFixes into main 2023-03-04 12:50:20 +08:00
5 changed files with 97 additions and 17 deletions
Showing only changes of commit dc54ae92a2 - Show all commits

View File

@ -3477,6 +3477,7 @@
Text: My name is Brandon.
Font: 174412429
Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: true
UI Component:
Canvas ID: 199
@ -3518,6 +3519,7 @@
Text: My name is Brandon.
Font: 176667660
Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: true
UI Component:
Canvas ID: 199
@ -3539,6 +3541,7 @@
Text: X2
Font: 174412429
Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: true
UI Component:
Canvas ID: 199
@ -5855,7 +5858,7 @@
Components: ~
Scripts: ~
- EID: 165
Name: 1
Name: 0
IsActive: true
NumberOfChildren: 0
Components:
@ -5866,7 +5869,7 @@
IsActive: true
Scripts: ~
- EID: 164
Name: 2
Name: 1
IsActive: true
NumberOfChildren: 0
Components:
@ -5877,7 +5880,7 @@
IsActive: true
Scripts: ~
- EID: 163
Name: 3
Name: 2
IsActive: true
NumberOfChildren: 0
Components:
@ -5888,7 +5891,7 @@
IsActive: true
Scripts: ~
- EID: 162
Name: 4
Name: 3
IsActive: true
NumberOfChildren: 0
Components:
@ -5899,7 +5902,7 @@
IsActive: true
Scripts: ~
- EID: 161
Name: 5
Name: 4
IsActive: true
NumberOfChildren: 0
Components:
@ -5910,7 +5913,7 @@
IsActive: true
Scripts: ~
- EID: 160
Name: 6
Name: 5
IsActive: true
NumberOfChildren: 0
Components:
@ -5921,7 +5924,7 @@
IsActive: true
Scripts: ~
- EID: 159
Name: 7
Name: 6
IsActive: true
NumberOfChildren: 0
Components:
@ -5937,7 +5940,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
@ -5975,12 +5978,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
@ -6110,7 +6114,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:
@ -6183,6 +6187,7 @@
Text: Game Pause
Font: 176667660
Color: {x: 1, y: 1, z: 1, w: 1}
Text Size: {x: 1, y: 1, z: 1}
IsActive: false
UI Component:
Canvas ID: 10

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;
@ -151,6 +154,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());