WanderingAI_Thief.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. [DefaultExecutionOrder(10)]
  5. [RequireComponent(typeof(InstantiatePrefab))]
  6. public class WanderingAI_Thief : MonoBehaviour
  7. {
  8. [Header("Only for Debugging (do not set from here)")]
  9. public Transform[] waypoints;
  10. public List<Transform> waypointsList;
  11. public float[] wanderTimer;
  12. [Header("Scattering around the target")]
  13. public float targetScattering = 2.0f; //Streuung
  14. [Header("Leave Settings")]
  15. public float leaveTimer = 120f;
  16. public Vector3 leavePosition;
  17. // Settings Leave Market
  18. private bool leaveMarket = false;
  19. private Vector3 endTarget;
  20. // Settings Thief
  21. [HideInInspector]
  22. public bool ignoreThief;
  23. private int thief_i;
  24. private int thief_j;
  25. // Settings Humans
  26. private Vector3[][] target;
  27. private int[][] humansPrio;
  28. // Settings Waypoints and Waiting Time
  29. private int maxWPCount;
  30. private List<List<int>> waitingTimer;
  31. private List<int> waitingList;
  32. private float[] currentWait;
  33. // Time
  34. private float[] timer;
  35. [HideInInspector]
  36. public float globalTimer;
  37. private GameObject[][] humansGO;
  38. private NavMeshAgent[][] humansNMA;
  39. private Animator[][] humansA;
  40. private const string isWalking = "isWalking";
  41. void Start()
  42. {
  43. // Get initialized Variables from InstantiatePrefab
  44. humansGO = gameObject.GetComponent<InstantiatePrefab>().humanGameObject;
  45. humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
  46. humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
  47. wanderTimer = gameObject.GetComponent<InstantiatePrefab>().wanderTimer;
  48. waypoints = gameObject.GetComponent<InstantiatePrefab>().waypointsArray;
  49. humansPrio = gameObject.GetComponent<InstantiatePrefab>().humanPriorities;
  50. // Set length of variables to humanGo.Length
  51. target = new Vector3[humansGO.Length][];
  52. timer = new float[humansGO.Length];
  53. // Create copies
  54. waypointsList = new List<Transform>(waypoints);
  55. waitingTimer = new List<List<int>>(gameObject.GetComponent<InstantiatePrefab>().waitingTimer);
  56. waitingList = new List<int>(waitingTimer[0]);
  57. currentWait = new float[humansGO.Length];
  58. maxWPCount = waypointsList.Count;
  59. // Initialize size and content
  60. for (int i = 0; i < humansGO.Length; i++)
  61. {
  62. target[i] = new Vector3[humansGO[i].Length];
  63. timer[i] = 0;
  64. }
  65. globalTimer = 0;
  66. thief_i = gameObject.GetComponent<ControllingThief>().thief_i;
  67. thief_j = gameObject.GetComponent<ControllingThief>().thief_j;
  68. endTarget = CheckTarget(leavePosition, 0);
  69. }
  70. // To ensure that all market stalls (WPs) are visited, all entries in the WP-list are first edited in a random order
  71. // and each edited one is appended to the end of the list so that all unedited ones can be selected by decreasing the range.
  72. // Since the order of the WP-list is the same as the first dimension of the waitingTimer (WT) matrix,
  73. // the WT matrix can be edited with the same index as for the selection of the WP. So the arrangement is always the same.
  74. // The second dimension (the times of the individual market stalls) of the WT matrix is first completely emptied each time and then refilled.
  75. // After the leaving time is reached, all humans walk straight outside the market, without waiting the waiting times.
  76. private void FixedUpdate()
  77. {
  78. if (waypoints.Length == 0) return;
  79. // Leave Market if time is up
  80. if(globalTimer >= leaveTimer)
  81. leaveMarket = true;
  82. for (int i = 0; i < humansGO.Length; ++i)
  83. {
  84. // Set a new destination if the waiting time is over
  85. if (timer[i] >= currentWait[i] && !leaveMarket)
  86. {
  87. for (int j = 0; j < humansGO[i].Length; ++j)
  88. {
  89. // Set new destination iff the human reached his destination or (only in first iteration) target vector is zero
  90. if (target[i][j] == Vector3.zero ||
  91. humansNMA[i][j].velocity.magnitude <= 0.01f ||
  92. humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance ||
  93. humansNMA[i][j].pathStatus == NavMeshPathStatus.PathComplete ||
  94. !humansNMA[i][j].hasPath)
  95. {
  96. // Random index for waypointsList
  97. if (maxWPCount <= 0) maxWPCount = waypointsList.Count;
  98. int currentWPIndex = Random.Range(0, maxWPCount);
  99. target[i][j] = CheckTarget(waypointsList[currentWPIndex].transform.position, targetScattering);
  100. // Set destination and animation
  101. if (target[i][0].x != float.PositiveInfinity)
  102. {
  103. // Random index for waitingTimer
  104. int currentWaitingIndex = Random.Range(0, waitingTimer[currentWPIndex].Count);
  105. currentWait[i] = waitingTimer[currentWPIndex][currentWaitingIndex];
  106. RemoveIndexFromWaitingTimer(currentWPIndex, currentWaitingIndex);
  107. // Fill waiting timer with default, if it is empty, to get a new waiting time for the next iteration
  108. if (waitingTimer[currentWPIndex].Count <= 0)
  109. waitingTimer[currentWPIndex] = new List<int>(waitingList);
  110. if (humansNMA[i][j].isActiveAndEnabled)
  111. humansNMA[i][j].SetDestination(target[i][j]);
  112. humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
  113. // Append the current WP and the waiting time from both lists to the end to ensure that all WPs and waiting timers are visited at the frequency indicated,
  114. // afterwords decrement the max length by 1
  115. AppendIndexToEndWP(currentWPIndex);
  116. AppendIndexToEndWT(currentWPIndex);
  117. maxWPCount--;
  118. timer[i] = 0;
  119. }
  120. }
  121. }
  122. }
  123. // if leaving time has arrived, then leave market
  124. else if (leaveMarket)
  125. {
  126. for (int j = 0; j < humansGO[i].Length; ++j)
  127. {
  128. if (ignoreThief && thief_i == i && thief_j == j)
  129. {
  130. continue;
  131. }
  132. if (humansNMA[i][j].isActiveAndEnabled && Vector3.Distance(humansNMA[i][j].destination, endTarget) > 0.01f)
  133. humansNMA[i][j].SetDestination(endTarget);
  134. humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
  135. if ((Vector3.Distance(humansNMA[i][j].transform.position, endTarget) <= 4))
  136. {
  137. humansGO[i][j].SetActive(false);
  138. }
  139. }
  140. }
  141. // Wait until the waiting time is over
  142. else
  143. {
  144. for (int j = 0; j < humansGO[i].Length; ++j)
  145. {
  146. // Set walking state and priority; increment timer
  147. humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
  148. if (humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance)
  149. timer[i] += Time.deltaTime;
  150. // if humans are not walking, then set priority to 0 so that the other humans dont disturb them
  151. if (humansNMA[i][j].velocity.magnitude <= 0.01f)
  152. humansNMA[i][j].avoidancePriority = 0;
  153. else
  154. humansNMA[i][j].avoidancePriority = humansPrio[i][j];
  155. }
  156. }
  157. }
  158. if(globalTimer >= 10.0f)
  159. gameObject.GetComponent<WriteInCSV>().startWriting = true;
  160. globalTimer += Time.deltaTime;
  161. }
  162. public static Vector3 CheckTarget(Vector3 target, float dist)
  163. {
  164. Vector3 modifiedTarget = Random.insideUnitSphere * dist + target;
  165. // SamplePosition also checks the y axis. However, this is not relevant for me, so valid positions (x & z) are also discarded.
  166. // 0.0f is the only valid entry for y
  167. modifiedTarget = new Vector3(modifiedTarget.x, 0f, modifiedTarget.z);
  168. NavMeshHit navHit;
  169. NavMesh.SamplePosition(modifiedTarget, out navHit, 1.0f, -1);
  170. return navHit.position;
  171. }
  172. public void AppendIndexToEndWP(int index)
  173. {
  174. waypointsList.Add(waypointsList[index]);
  175. waypointsList.RemoveAt(index);
  176. }
  177. public void AppendIndexToEndWT(int index)
  178. {
  179. waitingTimer.Add(waitingTimer[index]);
  180. waitingTimer.RemoveAt(index);
  181. }
  182. public void RemoveIndexFromWaitingTimer(int i, int removeIndex)
  183. {
  184. waitingTimer[i].RemoveAt(removeIndex);
  185. }
  186. private void LateUpdate()
  187. {
  188. for (int i = 0; i < humansGO.Length; ++i)
  189. {
  190. for (int j = 0; j < humansGO[i].Length; ++j)
  191. {
  192. humansA[i][j].speed = 0.5f + (humansNMA[i][j].velocity.magnitude / gameObject.GetComponent<InstantiatePrefab>().speedMinMax.y);
  193. }
  194. }
  195. }
  196. }