-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatAgent.cs
178 lines (150 loc) · 5.27 KB
/
CatAgent.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
169
170
171
172
173
174
175
176
177
178
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class CatAgent : Agent
{
public CatAgent(char id, Location loc) : base(id, loc)
{
planList = new List<Plan>()
{
new CatPlan.Flee(),
new CatPlan.Wait(),
new CatPlan.Search(),
new CatPlan.Eat(),
};
goalList = new List<IGoal>()
{
new CatGoal.Flee(),
new CatGoal.Wait(),
new CatGoal.Eat(),
new CatGoal.Search()
};
}
protected override Intention Deliberate(Beliefs b, Intention i)
{
foreach (IGoal g in goalList)
{
if (g.HasReasonFor(new Subject(this), b, i))
return g.ExtractIntention(b, new Subject(this));
}
return new Intention("none");
}
protected override Queue<Location> Plan(Beliefs b, Intention i)
{
Queue<Location> plan = new Queue<Location>();
foreach (Plan p in planList)
{
if (p.IsPlanFor(i))
{
plan = p.ExtractPlan(new Subject(this), i, b, Path);
break;
}
}
Debug.Log("(" + ID + ")" + " " + i.Name + " " + i.ID);
if (b.Rooms.Count == 0)
b.Rooms = Util.CloneAllRooms();
float t = (1f / (float)Manager.Board.simulationSpeed);
// Set the eating or waiting timer.
if (i.Name == "eat")
Manager.Board.StartCoroutine(Count(t *= Manager.Board.eatTime));
else if (i.Name == "wait")
Manager.Board.StartCoroutine(Count(t *= Manager.Board.waitTime));
// When fleeing forget the location of food objects seen.
if (i.Name == "flee")
{
b.Foods.Clear();
ExecTime = (int)Speed.VeryFast;
}
else
ExecTime = (int)Speed.Medium;
return plan;
}
protected override bool Succeeded(Intention i, Beliefs b)
{
// If eating and counter is done, remove food object from level.
if (i.Name == "eat")
{
if (counter < 0.1f)
{
b.Foods.Remove(Location);
Manager.Instance.RemoveObject(Location);
return true;
}
}
if (i.Name == "wait")
{
if (counter < 0.1f)
return true;
}
// Searching succeedes if room has been explored.
if (i.Name == "search" && !b.Rooms.ContainsKey(i.ID))
return true;
return false;
}
protected override bool Reconsider(Intention i, Beliefs b)
{
// If not currently fleeing from the nearest child, reconsider intention.
if ((i.Name == "flee")
&& (b.Sees.Agents.Count > 0 || b.Hears.Agents.Count > 0))
{
List<Subject> perceived = Util.Merge(b.Hears.Agents, b.Sees.Agents).Values.ToList();
if (i.ID != Util.NearestAgent(perceived.ToList(), Location).ID)
return true;
}
// If hears or sees a child agent and not currently fleeing or waiting,
// reconsider current intention.
if ((b.Sees.Agents.Count > 0 || b.Hears.Agents.Count > 0)
&& !(i.Name == "wait" || i.Name == "flee"))
{
return true;
}
// If not waiting or fleeing and sees food and the current intention
// is not to eat, reconsider intention.
if (!(i.Name == "wait" || i.Name == "flee")
&& i.ID != 'F'
&& b.Sees.Foods.Count > 0)
{
return true;
}
// If current intention is nothing and all rooms are searched, but food
// remains, re-fill rooms and reconsider intention.
if ((i.Name == "none")
&& b.Rooms.Count == 0
&& Manager.Board.Foods.Count > 0)
{
b.Rooms = Manager.Board.Rooms.ToDictionary(x => x.Key, x => x.Value.Clone());
return true;
}
// If waiting and hears a child or sees a child in close proximity,
// reconsider current intention.
if (i.Name == "wait")
{
Subject nearest = Util.NearestAgent(b.Sees.Agents.Values.ToList(), Location);
if (((b.Sees.Agents.Count > 0 && Util.L1Distance(Location, nearest.Location) < 3)
|| b.Hears.Agents.Count > 0))
{
return true;
}
}
return false;
}
protected override bool Sound(Queue<Location> plan, Intention i, Beliefs b)
{
// If exploring and the entire path is within FOV, this area of the room
// is already searched so re-plan.
if (i.Name == "explore")
{
List<Location> intersect = plan.Intersect(b.Sees.Locations).ToList();
if (intersect.Count == plan.Count)
return false;
}
// Re-plan if an obstacle is discovered which is located on the current
// planned path.
foreach (Location loc in plan)
{
if (b.Obstacles.ContainsKey(loc))
return false;
}
return true;
}
}