-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgentHandler.cs
168 lines (133 loc) · 5.4 KB
/
AgentHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class AgentHandler : MonoBehaviour {
public int agentCount = 20;
public int agentLife = 100;
public float evap = 0.0032f;
public int stepCount = 0;
public int cubeHeldCount = 0;
public float pickupModifier = 10f; // Modifies the thereshold chance for pickup. Higher modifier = greater chance of pickup
public float dropModifier = 10f; // Modifes the threshold chance for drop. Higher modifier = greater chance of drop
protected List<AgentObj> agentList = new List<AgentObj>();
Architect architect;
TerrainManager terrainManager;
SceneBuilder sceneBuilder;
void Awake(){
architect = GetComponent<Architect>();
terrainManager = GetComponent<TerrainManager>();
sceneBuilder = GetComponent<SceneBuilder>();
}
public void SeedAgents(){
for(int agent = 0; agent < agentCount; agent++){
AgentObj newAgent = new AgentObj(sceneBuilder.levelSize, sceneBuilder.seedLayers, agentLife);
agentList.Add(newAgent);
newAgent.cubeNeighborhood = architect.NeighborCheck(newAgent.agentArrayPosition);
}
}
void Update(){
if(agentList.Count != 0){
foreach(AgentObj agent in agentList){
if(!agent.holdingCube) PickUp (agent);
RandomWalk(agent);
if(agent.holdingCube) Drop (agent);
//agent.agentLife--;
}
stepCount++;
}
}
void PickUp(AgentObj agent){
CubeObj[,,] neighborhood = agent.cubeNeighborhood;
float spontPick = 0.1f;
float amplifPick = 1.0f;
// Pickup targets are always 1 y coord below the worker
// If the pickup candidate cell is full and is not already flagged as picked up by another worker, then run stochastic check for pickup.
// If the roll is successful, then pick up.
if(neighborhood[1, 0, 1] != null && neighborhood[1,0,1].heldByAgent == false){
//Pickup targets are only influenced by the eight adjacent neighbors that share the same y coord
int neighborCount = 0;
for(int x = 0; x < neighborhood.GetLength(0); x++){
for(int z = 0; z < neighborhood.GetLength(2); z++){
if(neighborhood[x, 0, z] != null && !(x == 1 && z == 1)){
neighborCount++;
}
}
}
float pickupChance = spontPick / (amplifPick * neighborCount);
float pickupRoll = Random.Range(0.000f,1.000f);
pickupChance = pickupChance * pickupModifier;
if (pickupRoll < pickupChance){
CubeObj pickupTarget = neighborhood[1, 0, 1];
agent.holdingCube = true;
agent.cubeHeld = pickupTarget;
pickupTarget.heldByAgent = true;
cubeHeldCount++;
}
}
}
void RandomWalk(AgentObj agent){
List<Vector3> viableMoveList = new List<Vector3>();
CubeObj[,,] neighborhood = agent.cubeNeighborhood;
for(int x = 0; x < neighborhood.GetLength(0); x++){
for(int y = 0; y < neighborhood.GetLength(1); y++){
for(int z = 0; z < neighborhood.GetLength(2); z++){
if(neighborhood[x,y,z] != null){
viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition);
viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,1,0));
viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,-1,0));
viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(1,0,0));
viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(-1,1,0));
viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,0,1));
viableMoveList.Add(neighborhood[x,y,z].cubeArrayPosition + new Vector3(0,0,-1));
}
}
}
}
// Scrub list of coordinates outside the level size
viableMoveList.RemoveAll(coord => (
coord.x >= sceneBuilder.levelSize.x ||
coord.y >= sceneBuilder.levelSize.y ||
coord.z >= sceneBuilder.levelSize.z ||
coord.x < 0 ||
coord.y < 0 ||
coord.z < 0
));
//Select a move from the viable move list and update agent's cubeNeighborhood array
Vector3 newPosition = viableMoveList[Random.Range(0, viableMoveList.Count)];
agent.agentArrayPosition = newPosition;
agent.cubeNeighborhood = architect.NeighborCheck(agent.agentArrayPosition);
}
void Drop(AgentObj agent){
float spontDrop = 0.001f;
float drop1 = 0.01f;
float amplifDrop = 0.036f;
float dropChance = 0f;
float dropRoll = Random.Range(0.000f, 1.000f);
if(!terrainManager.CheckForCube(agent.agentArrayPosition) && agent.holdingCube){
// Find number of neighbors for current agent position
CubeObj[,,] neighborhood = agent.cubeNeighborhood;
int neighborCount = architect.CountNeighbors(neighborhood);
// Find latest drop time in neighborhood
float latestDropTime = terrainManager.LatestDropInNeighborhood(agent.agentArrayPosition);
if(neighborCount == 0) dropChance = spontDrop;
else {
dropChance = (drop1 + amplifDrop * (neighborCount - 1)) * Mathf.Exp(-(Time.time - latestDropTime) * evap);
}
dropChance *= dropModifier;
if(dropRoll < dropChance){
CubeObj dropCube = agent.cubeHeld;
Vector3 thisPosition = agent.agentArrayPosition;
terrainManager.cubePositionArray[(int)dropCube.cubeArrayPosition.x, (int)dropCube.cubeArrayPosition.y, (int)dropCube.cubeArrayPosition.z] = 0;
terrainManager.cubePositionArray[(int)thisPosition.x, (int)thisPosition.y, (int)thisPosition.z] = 1;
dropCube.cubeArrayPosition = thisPosition;
dropCube.cubeGameObj.transform.position = thisPosition * sceneBuilder.cubeSize;
agent.holdingCube = false;
dropCube.heldByAgent = false;
}
}
}
public void KillAgents(){
agentList.Clear();
}
}