/
IGoal.cs
433 lines (372 loc) · 13.2 KB
/
IGoal.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using System.Collections.Generic;
using System.Linq;
public interface IGoal
{
bool HasReasonFor(Subject s, Beliefs b, Intention i);
Intention ExtractIntention(Beliefs b, Subject s);
}
// The goals available to the child agents.
public abstract class ChildGoal
{
// If in the same location as another agent and is lower rank, then recover.
public class Recover : IGoal
{
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (i.Name != "recover")
{
foreach (Subject subj in b.Sees.Agents.Values)
{
if (subj.Location == s.Location && Util.IsHigherRank(subj.ID, s.ID))
return true;
}
}
return false;
}
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("recover");
}
}
// If the agent sees the cat and the cat is believed to hear the child
// or the cat is running, try and capture the cat.
public class Capture : IGoal
{
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Recover().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.ContainsKey('0'))
{
if (b.ToM['0'].Hears.Agents.ContainsKey(s.ID)
|| Util.GetAgentSpeed('0') == (int)Speed.VeryFast)
{
return true;
}
// If lower rank, Misdirect the cat if another agent is seen
// and sees this agent and they both see the cat.
if (b.Sees.Agents.Count > 1)
{
foreach (Subject subj in b.Sees.Agents.Values)
{
if (Util.IsChild(subj.ID)
&& b.ToM[subj.ID].Sees.Agents.ContainsKey(s.ID)
&& ((b.ToM[subj.ID].Sees.Agents.ContainsKey('0')
&& Util.IsHigherRank(subj.ID, s.ID))
|| !b.ToM[subj.ID].Sees.Agents.ContainsKey('0')))
{
return true;
}
}
}
}
}
return false;
}
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("capture");
}
}
// Halt if the cat is spotted and believed to have spotted the child
// or if in close proximity to the cat.
public class Halt : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("halt");
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Capture().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.ContainsKey('0'))
{
if (b.ToM['0'].Sees.Agents.ContainsKey(s.ID))
{
if (Util.GetAgentSpeed('0') != (int)Speed.VeryFast)
return true;
}
else
{
if (Util.L1Distance(s.Location, b.Agents['0'].Location) <= 2)
return true;
}
}
}
return false;
}
}
// Track the cat if the current intention is to capture the cat
// but it is no longer in sight.
public class Track : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("track");
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Recover().HasReasonFor(s, b, i))
{
if (!b.Sees.Agents.ContainsKey('0') && i.Name == "capture")
return true;
}
return false;
}
}
// Flank the cat if another agent has seen the cat but not this child agent
// and the cat is believed to not see this child agent.
public class Flank : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
Intention i = new Intention("flank");
foreach (KeyValuePair<char, Subject> kvp in b.Sees.Agents)
{
if (Util.IsChild(kvp.Key) && b.ToM[kvp.Key].Sees.Agents.ContainsKey('0')
&& !b.ToM[kvp.Key].Sees.Agents.ContainsKey(s.ID))
{
i.ID = kvp.Key;
break;
}
}
return i;
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Halt().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.ContainsKey('0'))
{
if (b.Sees.Agents.Count > 1
&& (b.Sees.Agents.ContainsKey('0')
&& !b.ToM['0'].Sees.Agents.ContainsKey(s.ID)
&& Util.GetAgentSpeed('0') != (int)Speed.VeryFast))
{
foreach (Subject subj in b.Sees.Agents.Values)
{
if (Util.IsChild(subj.ID)
&& b.ToM[subj.ID].Sees.Agents.ContainsKey('0')
&& !b.ToM[subj.ID].Sees.Agents.ContainsKey(s.ID))
{
return true;
}
}
}
}
}
return false;
}
}
// Assist a child agent if it is not moving, not recovering, not regrouping,
// and this agent does not see the cat.
public class Assist : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
Intention i = new Intention("assist");
List<Subject> subjects = new List<Subject>();
foreach (Subject subj in b.Sees.Agents.Values)
{
if (!Util.IsRegrouping(subj.ID) && !Util.IsMoving(subj.ID) && !Util.IsRecovering(subj.ID))
subjects.Add(subj);
}
i.ID = Util.NearestAgent(subjects, s.Location).ID;
return i;
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Track().HasReasonFor(s, b, i))
{
if (!b.Sees.Agents.ContainsKey('0') && i.Name != "ambush")
{
if (b.Sees.Agents.Count > 0)
{
foreach (Subject subj in b.Sees.Agents.Values)
{
if (!b.ToM[subj.ID].Sees.Agents.ContainsKey(s.ID)
&& !Util.IsRegrouping(subj.ID)
&& !Util.IsMoving(subj.ID)
&& !Util.IsRecovering(subj.ID))
return true;
}
}
}
}
return false;
}
}
// If not currently halting or flanking, ambush the cat if spotted
// and the cat does not see this child.
public class Ambush : IGoal
{
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Flank().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.ContainsKey('0'))
{
if (Util.InRoom(b.Agents['0'].Location)
&& !b.ToM['0'].Sees.Agents.ContainsKey(s.ID)
&& i.Name != "halt"
&& i.Name != "flank")
return true;
}
}
return false;
}
public Intention ExtractIntention(Beliefs b, Subject s)
{
Intention i = new Intention("ambush");
Room room = Util.GetRoom(b.Agents['0'].Location);
i.ID = room.ID;
return i;
}
}
// If the child does not see the cat, search for it.
public class Search : IGoal
{
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Assist().HasReasonFor(s, b, i))
{
if (!b.Sees.Agents.ContainsKey('0') && b.Rooms.Count > 0)
return true;
}
return false;
}
public Intention ExtractIntention(Beliefs b, Subject s)
{
Intention i = new Intention("search");
Room room = Util.NearestRoom(b.Rooms, s.Location);
i.ID = room.ID;
return i;
}
}
// Go to destination.
public class GoTo : IGoal
{
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Ambush().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.ContainsKey('0'))
{
return true;
}
}
return false;
}
public Intention ExtractIntention(Beliefs b, Subject s)
{
Intention i = new Intention("goto");
if (b.Sees.Agents.ContainsKey('0'))
i.ID = '0';
return i;
}
}
// If done searching and no other agent is visible, regroup.
public class Regroup : IGoal
{
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Search().HasReasonFor(s, b, i))
{
if (!b.Sees.Agents.ContainsKey('0') && b.Rooms.Count == 0)
return true;
}
return false;
}
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("regroup");
}
}
}
// The goals available to the cat agent.
public abstract class CatGoal
{
// If a child is heard or if a child is spotted within close proximity
// to the cat itself, then it will flee.
public class Flee : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("flee");
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
List<Subject> perceived = Util.Merge(b.Hears.Agents, b.Sees.Agents).Values.ToList();
char agtID = Util.NearestAgent(perceived, s.Location).ID;
if (i.Name == "wait" || b.Hears.Agents.Count > 0 ||
(b.Sees.Agents.Count > 0 &&
Util.L1Distance(s.Location, b.Agents[agtID].Location) <= 3))
{
return true;
}
return false;
}
}
// If the cat spots a child not within close proximity it will first wait.
public class Wait : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("wait");
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Flee().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.Count > 0)
{
List<Subject> perceived = Util.Merge(b.Hears.Agents, b.Sees.Agents).Values.ToList();
char agtID = Util.NearestAgent(perceived, s.Location).ID;
if (i.Name != "wait" && Util.L1Distance(s.Location, b.Agents[agtID].Location) > 3)
return true;
}
}
return false;
}
}
// If the cat does not see any child agents and sees food, it will
// try to eat the food.
public class Eat : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("eat");
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Flee().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.Count == 0 && b.Sees.Foods.Count > 0)
{
return true;
}
}
return false;
}
}
// Search for food while no children are spotted.
public class Search : IGoal
{
public Intention ExtractIntention(Beliefs b, Subject s)
{
return new Intention("search");
}
public bool HasReasonFor(Subject s, Beliefs b, Intention i)
{
if (!new Eat().HasReasonFor(s, b, i))
{
if (b.Sees.Agents.Count == 0 && b.Rooms.Count > 0)
{
return true;
}
}
return false;
}
}
}