Browse Source

WPList implementation revised; waitingTimer implemented with "draw without replacement"

Furkan Karakocaoglu 2 years ago
parent
commit
7a93cc673b

+ 3 - 3
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/General/InstantiatePrefab.cs

@@ -41,7 +41,7 @@ public class InstantiatePrefab : MonoBehaviour
     [HideInInspector]
     public float[] wanderTimer;
     [HideInInspector]
-    public List<float>[] waitingTimer; // waitingTime (1-5 s) X WPs
+    public List<List<int>> waitingTimer; // waitingTime (1-5 s) X WPs
     [HideInInspector]
     public Transform[] waypointsArray;
 
@@ -89,10 +89,10 @@ public class InstantiatePrefab : MonoBehaviour
         wanderTimer = new float[humanGameObject.Length];
 
         // Set waitingTimer
-        waitingTimer = new List<float>[waypointsArray.Length];
+        waitingTimer = new List<List<int>>();
         for (int i = 0; i < waypointsArray.Length; ++i)
             // create Matrix waitingTime X WPs with waitingTime from 1-5 seconds, bc we want all WP vistited the same duration
-            waitingTimer[i] = new List<float> { 1, 2, 3, 4, 5 };
+            waitingTimer.Add(new List<int> { 1, 2, 3, 4, 5 });
 
         int humansLeft = amount;
 

+ 76 - 77
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/WanderingAI/WanderingAI.cs

@@ -10,7 +10,6 @@ public class WanderingAI : MonoBehaviour
     public Transform[] waypoints;
     public List<Transform> waypointsList;
     public float[] wanderTimer;
-    public List<float>[] waitingTimer;
     [Header("Scattering around the target")]
     public float targetScattering = 5.0f; //Streuung
     [Header("Leave Settings")]
@@ -24,9 +23,17 @@ public class WanderingAI : MonoBehaviour
 
     private float[] timer;
     private float globalTimer;
+    private bool leaveMarket = false;
     private Vector3[][] target;
     private int[][] humansPrio;
 
+    // Settings WP and Waiting time
+    private int maxWPCount;
+    private List<List<int>> waitingTimer;
+    private List<int> waitingList;
+    private float[] currentWait;
+    private int countFilling = 0;
+
     private GameObject[][] humansGO;
     private NavMeshAgent[][] humansNMA;
     private Animator[][] humansA;
@@ -41,113 +48,96 @@ public class WanderingAI : MonoBehaviour
         humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
         humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
         wanderTimer = gameObject.GetComponent<InstantiatePrefab>().wanderTimer;
-        waitingTimer = gameObject.GetComponent<InstantiatePrefab>().waitingTimer;
+        //waitingTimer = gameObject.GetComponent<InstantiatePrefab>().waitingTimer;
         waypoints = gameObject.GetComponent<InstantiatePrefab>().waypointsArray;
-        waypointsList = new List<Transform>(waypoints);
         humansPrio = gameObject.GetComponent<InstantiatePrefab>().humanPriorities;
         // Set length of variables to humanGo.Length
         target = new Vector3[humansGO.Length][];
         timer = new float[humansGO.Length];
 
+        waypointsList = new List<Transform>(waypoints);
+        maxWPCount = waypointsList.Count;
+        waitingTimer = new List<List<int>>(gameObject.GetComponent<InstantiatePrefab>().waitingTimer);
+        waitingList = new List<int>(waitingTimer[0]);
+        currentWait = new float[humansGO.Length];
+
         // Initialize size and content
-        for(int i = 0; i < humansGO.Length; i++)
+        for (int i = 0; i < humansGO.Length; i++)
         {
             target[i] = new Vector3[humansGO[i].Length];
-
-            for(int j = 0; j < humansGO[i].Length; j++)
-            {
-                timer[i] = wanderTimer[i];
-            }
+            timer[i] = 0;
         }
 
         globalTimer = 0;
     }
 
-    //private void FixedUpdate()
-    //{
-    //    for(int i = 0; i < humansGO.Length; ++i)
-    //    {
-    //        if (timer[i] >= wanderTimer[i])
-    //        {
-    //            if (waypoints.Length == 0) return;
-
-    //            int currentWP = Random.Range(0, waypointsList.Count);
-    //            target[i][0] = CheckTarget(waypointsList[currentWP].transform.position, targetScattering);
-
-    //            if(target[i][0].x != float.PositiveInfinity)
-    //            {
-    //                for (int j = 0; j < humansGO[i].Length; ++j)
-    //                {
-    //                    target[i][j] = target[i][0];
-
-    //                    if (humansNMA[i][j].isActiveAndEnabled)
-    //                        humansNMA[i][j].SetDestination(target[i][j]);
-
-    //                    humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
-    //                }
-    //                timer[i] = 0;
-    //                // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
-    //                ReduceWaypoints(currentWP);
-    //            }
-    //        }
-    //        else
-    //        {
-    //            for (int j = 0; j < humansGO[i].Length; ++j)
-    //            {
-    //                humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
-
-    //                // if humans are not walking, then set priority to 0 so that the other humans dont disturb them
-    //                if(humansNMA[i][j].velocity.magnitude <= 0.01f)
-    //                    humansNMA[i][j].avoidancePriority = 0;
-    //                else
-    //                    humansNMA[i][j].avoidancePriority = humansPrio[i][j];
-    //            }
-    //        }
-    //        timer[i] += Time.deltaTime;
-    //    }
-    //}
-
     private void FixedUpdate()
     {
+        if(globalTimer >= leaveTimer)
+            leaveMarket = true;
+
         for (int i = 0; i < humansGO.Length; ++i)
         {
             if (waypoints.Length == 0) return;
-            
-            for(int j  = 0; j < humansGO[i].Length; ++j)
+
+            if (timer[i] >= currentWait[i])
             {
-                if(target[i][j] == Vector3.zero || 
-                    humansNMA[i][j].velocity.magnitude <= 0.01f || 
-                    humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance ||
-                    humansNMA[i][j].pathStatus == NavMeshPathStatus.PathComplete || 
-                    !humansNMA[i][j].hasPath)
+                for (int j = 0; j < humansGO[i].Length; ++j)
                 {
-                    int currentWP = Random.Range(0, waypointsList.Count);
-                    target[i][j] = CheckTarget(waypointsList[currentWP].transform.position, targetScattering);
-
-                    if (target[i][0].x != float.PositiveInfinity)
+                    if (target[i][j] == Vector3.zero ||
+                        humansNMA[i][j].velocity.magnitude <= 0.01f ||
+                        humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance ||
+                        humansNMA[i][j].pathStatus == NavMeshPathStatus.PathComplete ||
+                        !humansNMA[i][j].hasPath)
                     {
-                        if (humansNMA[i][j].isActiveAndEnabled)
-                            humansNMA[i][j].SetDestination(target[i][j]);
-
-                        humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
-
+                        if (maxWPCount <= 0) maxWPCount = waypointsList.Count;
+                        int currentWPIndex = Random.Range(0, maxWPCount);
+                        target[i][j] = CheckTarget(waypointsList[currentWPIndex].transform.position, targetScattering);
+
+                        int currentWaitingIndex = Random.Range(0, waitingTimer[currentWPIndex].Count);
+                        currentWait[i] = waitingTimer[currentWPIndex][currentWaitingIndex];
+                        RemoveIndexFromWaitingTimer(currentWPIndex, currentWaitingIndex);
+                        // fill waiting Timer, bc preparation for next iteration
+                        if (waitingTimer[i].Count <= 0)
+                        {
+                            waitingTimer[i] = new List<int>(waitingList);
+                            countFilling++;
+                        }
+
+                        if (target[i][0].x != float.PositiveInfinity)
+                        {
+                            if (humansNMA[i][j].isActiveAndEnabled)
+                                humansNMA[i][j].SetDestination(target[i][j]);
+
+                            humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
+
+                        }
+                        // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
+                        AppendIndexToEndWP(currentWPIndex);
+                        AppendIndexToEndWT(currentWPIndex);
+                        maxWPCount--;
+                        timer[i] = 0;
                     }
-                    // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
-                    ReduceWaypoints(currentWP);
                 }
-                else
+            }
+            else
+            {
+                for (int j = 0; j < humansGO[i].Length; ++j)
                 {
                     humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
+                    if (humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance)
+                        timer[i] += Time.deltaTime;
                     // if humans are not walking, then set priority to 0 so that the other humans dont disturb them
                     if (humansNMA[i][j].velocity.magnitude <= 0.01f)
+                    {
                         humansNMA[i][j].avoidancePriority = 0;
+                    }
                     else
                         humansNMA[i][j].avoidancePriority = humansPrio[i][j];
                 }
             }
-            timer[i] += Time.deltaTime;
         }
-
+        globalTimer += Time.deltaTime;
     }
 
     public static Vector3 CheckTarget(Vector3 target, float dist)
@@ -163,12 +153,21 @@ public class WanderingAI : MonoBehaviour
         return navHit.position;
     }
 
-    public void ReduceWaypoints(int index)
+    public void AppendIndexToEndWP(int index)
     {
+        waypointsList.Add(waypointsList[index]);
         waypointsList.RemoveAt(index);
+    }
+
+    public void AppendIndexToEndWT(int index)
+    {
+        waitingTimer.Add(waitingTimer[index]);
+        waitingTimer.RemoveAt(index);
+    }
 
-        if(waypointsList.Count <= 0)
-            waypointsList = new List<Transform>(waypoints);
+    public void RemoveIndexFromWaitingTimer(int i, int removeIndex)
+    {
+        waitingTimer[i].RemoveAt(removeIndex);
     }
 
     public void LeaveMarket(int i)

+ 270 - 0
testumgebung/CrowdModelling/Assets/Scenes/Origin_OnlyYears.unity

@@ -22114,6 +22114,276 @@ Transform:
     type: 3}
   m_PrefabInstance: {fileID: 4495873106775748148}
   m_PrefabAsset: {fileID: 0}
+--- !u!1 &5565439461837125953
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 5565439461837125958}
+  - component: {fileID: 5565439461837125978}
+  - component: {fileID: 5565439461837125957}
+  m_Layer: 13
+  m_Name: Stand (2)
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 4294967255
+  m_IsActive: 1
+--- !u!23 &5565439461837125957
+MeshRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461837125953}
+  m_Enabled: 1
+  m_CastShadows: 1
+  m_ReceiveShadows: 1
+  m_DynamicOccludee: 1
+  m_MotionVectors: 1
+  m_LightProbeUsage: 1
+  m_ReflectionProbeUsage: 1
+  m_RayTracingMode: 2
+  m_RenderingLayerMask: 1
+  m_RendererPriority: 0
+  m_Materials:
+  - {fileID: 2100000, guid: 5139e315f3291984fb5c28c72b20f998, type: 2}
+  m_StaticBatchInfo:
+    firstSubMesh: 0
+    subMeshCount: 0
+  m_StaticBatchRoot: {fileID: 0}
+  m_ProbeAnchor: {fileID: 0}
+  m_LightProbeVolumeOverride: {fileID: 0}
+  m_ScaleInLightmap: 1
+  m_ReceiveGI: 1
+  m_PreserveUVs: 0
+  m_IgnoreNormalsForChartDetection: 0
+  m_ImportantGI: 0
+  m_StitchLightmapSeams: 1
+  m_SelectedEditorRenderState: 3
+  m_MinimumChartSize: 4
+  m_AutoUVMaxDistance: 0.5
+  m_AutoUVMaxAngle: 89
+  m_LightmapParameters: {fileID: 0}
+  m_SortingLayerID: 0
+  m_SortingLayer: 0
+  m_SortingOrder: 0
+--- !u!4 &5565439461837125958
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461837125953}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 33, y: 1, z: 9}
+  m_LocalScale: {x: 4, y: 1, z: 5}
+  m_Children: []
+  m_Father: {fileID: 5565439462362332742}
+  m_RootOrder: 1
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!33 &5565439461837125978
+MeshFilter:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461837125953}
+  m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
+--- !u!4 &5565439461855765160
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461855765163}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 33, y: 1, z: 21}
+  m_LocalScale: {x: 4, y: 1, z: 5}
+  m_Children: []
+  m_Father: {fileID: 5565439462362332742}
+  m_RootOrder: 0
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!1 &5565439461855765163
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 5565439461855765160}
+  - component: {fileID: 5565439461855765164}
+  - component: {fileID: 5565439461855765167}
+  m_Layer: 13
+  m_Name: Stand (1)
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 4294967255
+  m_IsActive: 1
+--- !u!33 &5565439461855765164
+MeshFilter:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461855765163}
+  m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
+--- !u!23 &5565439461855765167
+MeshRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461855765163}
+  m_Enabled: 1
+  m_CastShadows: 1
+  m_ReceiveShadows: 1
+  m_DynamicOccludee: 1
+  m_MotionVectors: 1
+  m_LightProbeUsage: 1
+  m_ReflectionProbeUsage: 1
+  m_RayTracingMode: 2
+  m_RenderingLayerMask: 1
+  m_RendererPriority: 0
+  m_Materials:
+  - {fileID: 2100000, guid: 5139e315f3291984fb5c28c72b20f998, type: 2}
+  m_StaticBatchInfo:
+    firstSubMesh: 0
+    subMeshCount: 0
+  m_StaticBatchRoot: {fileID: 0}
+  m_ProbeAnchor: {fileID: 0}
+  m_LightProbeVolumeOverride: {fileID: 0}
+  m_ScaleInLightmap: 1
+  m_ReceiveGI: 1
+  m_PreserveUVs: 0
+  m_IgnoreNormalsForChartDetection: 0
+  m_ImportantGI: 0
+  m_StitchLightmapSeams: 1
+  m_SelectedEditorRenderState: 3
+  m_MinimumChartSize: 4
+  m_AutoUVMaxDistance: 0.5
+  m_AutoUVMaxAngle: 89
+  m_LightmapParameters: {fileID: 0}
+  m_SortingLayerID: 0
+  m_SortingLayer: 0
+  m_SortingOrder: 0
+--- !u!4 &5565439461904859880
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461904859883}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 40.5, y: 1, z: 27.5}
+  m_LocalScale: {x: 4, y: 1, z: 4}
+  m_Children: []
+  m_Father: {fileID: 5565439462362332742}
+  m_RootOrder: 2
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!1 &5565439461904859883
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 5565439461904859880}
+  - component: {fileID: 5565439461904859884}
+  - component: {fileID: 5565439461904859887}
+  m_Layer: 13
+  m_Name: Stand (4)
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 4294967255
+  m_IsActive: 1
+--- !u!33 &5565439461904859884
+MeshFilter:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461904859883}
+  m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
+--- !u!23 &5565439461904859887
+MeshRenderer:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439461904859883}
+  m_Enabled: 1
+  m_CastShadows: 1
+  m_ReceiveShadows: 1
+  m_DynamicOccludee: 1
+  m_MotionVectors: 1
+  m_LightProbeUsage: 1
+  m_ReflectionProbeUsage: 1
+  m_RayTracingMode: 2
+  m_RenderingLayerMask: 1
+  m_RendererPriority: 0
+  m_Materials:
+  - {fileID: 2100000, guid: 5139e315f3291984fb5c28c72b20f998, type: 2}
+  m_StaticBatchInfo:
+    firstSubMesh: 0
+    subMeshCount: 0
+  m_StaticBatchRoot: {fileID: 0}
+  m_ProbeAnchor: {fileID: 0}
+  m_LightProbeVolumeOverride: {fileID: 0}
+  m_ScaleInLightmap: 1
+  m_ReceiveGI: 1
+  m_PreserveUVs: 0
+  m_IgnoreNormalsForChartDetection: 0
+  m_ImportantGI: 0
+  m_StitchLightmapSeams: 1
+  m_SelectedEditorRenderState: 3
+  m_MinimumChartSize: 4
+  m_AutoUVMaxDistance: 0.5
+  m_AutoUVMaxAngle: 89
+  m_LightmapParameters: {fileID: 0}
+  m_SortingLayerID: 0
+  m_SortingLayer: 0
+  m_SortingOrder: 0
+--- !u!1 &5565439462362332737
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 5565439462362332742}
+  m_Layer: 0
+  m_Name: '## Waypoints ## (1)'
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!4 &5565439462362332742
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 5565439462362332737}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_Children:
+  - {fileID: 5565439461855765160}
+  - {fileID: 5565439461837125958}
+  - {fileID: 5565439461904859880}
+  m_Father: {fileID: 0}
+  m_RootOrder: 12
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
 --- !u!1001 &5722838057186411996
 PrefabInstance:
   m_ObjectHideFlags: 0