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 |
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Brains.Framework.Locomotion;
using Brains.Framework.Behaviors;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using Brains.Framework.Designer;
using Brains.Framework.Map;
namespace Brains.Framework
{
/// <summary>
/// Represents an autonomous agent in the AI world.
/// </summary>
/// <remarks>You can implement your own Agent by inheriting from this class</remarks>
public class Agent
{
private static int _id=0;
private float _rotation = 0;
private IBehavior _rootBehavior;
private LocomotionController _motionController;
/// <summary>
/// Gets or sets the Feelers on the agent.
/// </summary>
public List<Feeler> Feelers { get; set; }
/// <summary>
/// Gets or sets the name of the Agent
/// </summary>
public string Name { get; set; }
private int NextID
{
get
{
return _id++;
}
}
public Dictionary<uint, int> Labels;
/// <summary>
/// Gets the cells the agent is in
/// </summary>
/// <remarks>Current implementation only supports 1</remarks>
public List<GridCell> CellsIAmIn { get; set; }
/// <summary>
/// The position of the Actor
/// </summary>
public Vector2 Position { get; set; }
/// <summary>
/// Gets or sets the previous position of the agent
/// </summary>
public Vector2 PreviousPosition{ get; set; }
/// <summary>
/// The position the Actor would like to be at
/// </summary>
public Vector2 DesiredPosition { get; set; }
/// <summary>
/// The Actors Radius
/// </summary>
public float Radius { get; set; }
/// <summary>
/// The Orientation of the Actor
/// </summary>
/// <remarks>This value is normalized</remarks>
public Vector2 Orientation { get; set; }
/// <summary>
/// The Orientation the Actor would like to be facing
/// </summary>
public Vector2 DesiredOrientation { get; set; }
/// <summary>
/// The world in which this Actor is alive
/// </summary>
public World ParentWorld { get; set; }
/// <summary>
/// The rotation of the Actor
/// </summary>
public float Rotation { get { return _rotation; } }
/// <summary>
/// The Root Behavior of the Actor
/// </summary>
public IBehavior RootBehavior
{
get { return _rootBehavior; }
set
{
_rootBehavior = value;
_rootBehavior.SetOwner(this);
}
}
/// <summary>
/// Gets or sets the Locomotion object for the Agent
/// </summary>
/// <remarks>You can provide your own locomotion technique by inheriting the Locomotion class</remarks>
public LocomotionController Locomotion
{
get { return _motionController; }
set
{
_motionController = value;
if (_motionController != null)
_motionController.Owner = this;
}
}
/// <summary>
/// Default constructor of the agent
/// </summary>
/// <param name="position">Center position of the Agent</param>
/// <param name="radius">The radius of the agent</param>
public Agent(Vector2 position, float radius)
{
Position = DesiredOrientation = position;
Radius = radius;
Orientation = DesiredOrientation = new Vector2(1, 0);
Feelers = new List<Feeler>();
CellsIAmIn = new List<GridCell>();
AddFeeler(1, 0,Radius*4);
AddFeeler(.5f, .5f, Radius + 32);
AddFeeler(.5f, -.5f, Radius + 32);
Name = CreateUniqueName();
Labels = new Dictionary<uint, int>();
}
private string CreateUniqueName()
{
return "Agent #" + NextID;
}
private void AddFeeler(float x, float y,float length)
{
Feelers.Add(new Feeler(new Vector2(x, y),length,this));
}
public virtual void UpdatePerception(GameTime gameTime)
{
//Tag nearby
}
/// <summary>
/// Update the agent every frame.
/// </summary>
/// <param name="gameTime">The time elapsed since the last update</param>
public virtual void Update(GameTime gameTime)
{
if (RootBehavior != null &&
(RootBehavior.State == BehaviorState.Idle || RootBehavior.State == BehaviorState.Running))
{
RootBehavior.Update(gameTime);
}
PreviousPosition = Position;
if (Locomotion != null)
Locomotion.Update(gameTime);
if (PreviousPosition != Position || CellsIAmIn.Count == 0)
UpdateLocationData();
}
/// <summary>
/// Every time an actor changes its position, it remembers all map's nodes
/// he is in.
/// </summary>
private void UpdateLocationData()
{
// 1. remove itself from all previous AINodes
for (int index = 0; index < CellsIAmIn.Count; index++)
{
// remove myself from AINode I was registered previously
CellsIAmIn[index].Agents.Remove(this);
}
CellsIAmIn.Clear();
// 2. add itself to all
// - get distance from the center of a map,
// - extract x and y components,
// - for each component calculate row and col
Vector2 vDistanceFromMapCenter = this.Position;
Grid map = ParentWorld.Map.ClusterGrid.GetGridAtPosition(Position);
if (map == null)
return;
float _hstep = (map.Width/ ((float)map.Cols));
float _vstep = (map.Height/ ((float)map.Rows));
float xComponent = (vDistanceFromMapCenter.X / _hstep) ;
float yComponent = (vDistanceFromMapCenter.Y / _vstep);
// xComponent = xComponent - 0.5f;
//yComponent = yComponent - 0.5f;
//debugX = (int)xComponent;
//debugY = (int)yComponent;
int indexX = (int)xComponent;
int indexY = (int)yComponent;
indexX = indexX % map.Cols;
indexY = indexY % map.Rows;
map = ParentWorld.Map.ClusterGrid.GetGridAtPosition(Position);
if (map != null)
{
GridCell desiredNode = map.GetCell(indexX, indexY);
if (desiredNode != null)
{
// am I out of map?
desiredNode.Agents.Add(this);
CellsIAmIn.Add(desiredNode);
}
else
{
Console.Write("");
}
}
else
{
Console.Write("");
}
}
/// <summary>
/// Loads a Behavior from a file
/// </summary>
/// <param name="filename">The filename to load</param>
protected void LoadBehaviorFromFile(string filename)
{
StreamReader _reader = new StreamReader(filename);
XmlSerializer _ser = new XmlSerializer(typeof(BehaviorNode));
BehaviorNode _rootNode = (BehaviorNode)_ser.Deserialize(_reader);
_reader.Close();
CompositeBehavior _behavior = CreateBehaviorInstance(_rootNode.Parameters[0].Value);
SetupBehavior(_behavior, _rootNode);
RootBehavior = _behavior;
}
private void SetupBehavior(IBehavior parentBehavior, BehaviorNode rootNode)
{
foreach (var item in rootNode.SubBehaviors)
{
IBehavior behavior = CreateBehaviorInstance(item.TypeName);
foreach (var param in item.Parameters)
{
if (param.Name == "RootType")
continue;
PropertyInfo prop = behavior.GetType().GetProperty(param.Name);
if(prop.PropertyType ==typeof(bool))
prop.SetValue(behavior, bool.Parse(param.Value), null);
else if (prop.PropertyType == typeof(float))
prop.SetValue(behavior, float.Parse(param.Value), null);
else
prop.SetValue(behavior, param.Value, null);
}
if(parentBehavior is ISubBehaviorHolder)
((ISubBehaviorHolder)parentBehavior).SubBehaviors.Add(behavior);
SetupBehavior(behavior, item);
}
}
private CompositeBehavior CreateBehaviorInstance(string typeName)
{
foreach (var item in ParentWorld.Engine.Assemblies)
{
CompositeBehavior behavior =(CompositeBehavior)item.CreateInstance(typeName);
if (behavior != null)
return behavior;
}
return null;
}
/// <summary>
/// Gets the cost of a grid cell type for use with pathfinding.
/// </summary>
/// <param name="type">The Type value from a GridCell</param>
/// <returns>The cost of the cell</returns>
/// <remarks>Override this method for greater flexibility in pathfinding</remarks>
public virtual byte GetCostOfCellType(int type)
{
if (type == 0)
return 99;
else
return (byte)type;
}
public void SetLabel(uint labelid, int labelvalue)
{
if (!Labels.Keys.Contains(labelid))
Labels.Add(labelid, labelvalue);
else
Labels[labelid] = labelvalue;
}
public int GetLabel(uint labelid)
{
if (!Labels.Keys.Contains(labelid))
return 0;
else
return Labels[labelid];
}
public List<Agent> GetNearbyAgents(int distance)
{
return ParentWorld._spatialtable.GetNearbyItems(this, distance);
}
}
} |