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 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brains.Framework.Behaviors.PathFinding;
using Microsoft.Xna.Framework;
using Brains.Framework.Map;
using Brains.Framework.Utility;
namespace Brains.Framework.Behaviors
{
public class GoToBehavior :CompositeBehavior,IActionBehavior
{
protected CompositeBehavior findPath = new FindPathBehavior();
protected CompositeBehavior followPath = new FollowPathBehavior();
GridCell _startNode;
GridCell _endNode;
Journey currentJourney;
protected float tolerance;
// How far from the desiret path a character can deviate and still
// be recognised as if following path exactly.
public float Tolarance
{
get { return tolerance; }
set
{
tolerance = value;
// propagate new tolerance value to the follow path behaviour
if (followPath != null)
{
((FollowPathBehavior)(followPath)).Tolerance = value;
}
}
}
public GridCell StartNode
{
get { return _startNode; }
set
{
if (value != null)
{
_startNode= value;
}
}
}
public GridCell EndNode
{
get { return _endNode; }
set
{
if (value != null)
{
_endNode = value;
}
}
}
public GoToBehavior():base()
{
SubBehaviors.Add(findPath);
SubBehaviors.Add(followPath);
}
~GoToBehavior()
{
findPath = null;
followPath = null;
}
public override void Reset()
{
currentJourney = null;
SubJournies.Clear();
GridCell start = StartNode;
GridCell end = EndNode;
List<int> grididschecked = new List<int>();
if (start.Parent != end.Parent)
{
GridCell currentStartCell;
GridCell currentEndCell;
Grid startGrid = StartNode.Parent;
Grid endGrid = EndNode.Parent;
int startId = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(startGrid);
int endId = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(endGrid);
List<Grid> gridsBetween = GetGridsBetween(StartNode, EndNode);
int ii = 0;
foreach (var item in gridsBetween)
{
GridCell startJourney = StartNode;
GridCell endJourney = null;
int index = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(item);
int y = (index - (index % Owner.ParentWorld.Map.ClusterGrid.Cols)) / Owner.ParentWorld.Map.ClusterGrid.Cols;
int x = index % Owner.ParentWorld.Map.ClusterGrid.Cols;
Console.WriteLine(x + " : " + y);
if (item == startGrid)
{
Grid next = gridsBetween[ii + 1];
Grid nextPlusOne = gridsBetween[ii + 2];
foreach (var path in next.PredeterminedPaths)
{
int tmpindex = index;
int tmpindex1 =
Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(nextPlusOne);
if (path.FromGrid == tmpindex &&
path.ToGrid == tmpindex1)
{
int _newX = path.Nodes[0].X;
int _newY = path.Nodes[0].X;
if (path.Nodes[0].X == 0)
_newX = startGrid.Cols - 1;
if (path.Nodes[0].X == startGrid.Cols - 1)
_newX = 0;
if (path.Nodes[0].Y == 0)
_newY = startGrid.Rows - 1;
if (path.Nodes[0].Y == startGrid.Rows - 1)
_newY = 0;
Journey j = new Journey(StartNode,
startGrid.GetCell(_newX, _newY));
}
}
//Journey j=new Journey(StartNode,
//startGrid.GetCell(
}
else
{
Grid next = gridsBetween[ii + 1];
Grid nextPlusOne = gridsBetween[ii + 2];
foreach (var path in next.PredeterminedPaths)
{
int tmpindex = index;
int tmpindex1 =
Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(nextPlusOne);
if (path.FromGrid == tmpindex &&
path.ToGrid == tmpindex1)
{
int _newX = path.Nodes[0].X;
int _newY = path.Nodes[0].X;
if (path.Nodes[0].X == 0)
_newX = startGrid.Cols - 1;
if (path.Nodes[0].X == startGrid.Cols - 1)
_newX = 0;
if (path.Nodes[0].Y == 0)
_newY = startGrid.Rows - 1;
if (path.Nodes[0].Y == startGrid.Rows - 1)
_newY = 0;
Journey j = new Journey(item.GetCell(_newX, _newY),
item.GetCell(
path.Nodes[path.Nodes.Count - 1].X,
path.Nodes[path.Nodes.Count - 1].Y));
}
else
{
}
}
}
ii++;
}
currentStartCell = StartNode;
bool finished = false;
Grid current = startGrid;
int currentId = startId;
Grid closest = null;
Cluster cluster = Owner.ParentWorld.Map.ClusterGrid;
int lastId = 0;
}
else
SubJournies.Add(new Journey(start, end));
}
private List<Grid> GetGridsBetween(GridCell StartNode, GridCell EndNode)
{
Grid start = StartNode.Parent;
Grid end = EndNode.Parent;
int startId=Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(start);
int endId=Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(end);
List<Grid> ids = new List<Grid>();
ids.Add(start);
bool finished=false;
Grid current=start;
int currentId=startId;
Grid closest=null;
Cluster cluster = Owner.ParentWorld.Map.ClusterGrid;
while (!finished)
{
float dist = 0;
int y = (currentId - (currentId % cluster.Cols)) / cluster.Cols;
int x = currentId% cluster.Cols;
//Top
if(y>0)
{
int newindex = (y - 1) * cluster.Cols + x;
Grid topGrid = cluster.Grids[newindex];
float tmpDist = Vector2.Distance(topGrid.Center, end.Center);
if (tmpDist < dist || closest == null)
{
dist=tmpDist;
closest=topGrid;
}
}
//Bottom
if (y<cluster.Rows-1)
{
int newindex = (y + 1) * cluster.Cols + x;
Grid bottomGrid = cluster.Grids[newindex];
float tmpDist = Vector2.Distance(bottomGrid.Center, end.Center);
if (tmpDist < dist || closest == null)
{
dist = tmpDist;
closest = bottomGrid;
}
}
//Right
if (x<cluster.Cols-1)
{
int newindex = y * cluster.Cols + (x+1);
Grid rightGrid = cluster.Grids[newindex];
float tmpDist = Vector2.Distance(rightGrid.Center, end.Center);
if (tmpDist < dist || closest == null)
{
dist = tmpDist;
closest = rightGrid;
}
}
//Left
if (x>0)
{
int newindex = y * cluster.Cols + (x - 1);
Grid leftGrid = cluster.Grids[newindex];
float tmpDist = Vector2.Distance(leftGrid.Center, end.Center);
if (tmpDist < dist || closest == null)
{
dist = tmpDist;
closest = leftGrid;
}
}
if (dist > 0)
{
ids.Add(closest);
current = closest;
currentId = Owner.ParentWorld.Map.ClusterGrid.Grids.IndexOf(closest);
}
else
{
if (closest == end)
{
finished = true;
}
}
closest = null;
}
ids.Add(end);
return ids;
}
public override void Update(GameTime gameTime)
{
if (currentJourney == null && SubJournies.Count == 0)
{
Reset();
}
if (currentJourney == null && SubJournies.Count > 0)
{
currentJourney = SubJournies[0];
((FindPathBehavior)findPath).StartNode = currentJourney.StartNode;
((FindPathBehavior)findPath).EndNode= currentJourney.EndNode;
((FindPathBehavior)findPath).initialized = false;
}
if (((FindPathBehavior)findPath).StartNode != null &&
((FindPathBehavior)findPath).EndNode != null)
{
base.Update(gameTime);
}
}
public override void OnSubBehaviorSuccess()
{
if (CurrentSubBehavior== 0)
{
Owner.Locomotion.Stationary = false;
((FollowPathBehavior)(followPath)).PathFinder =
((FindPathBehavior)(findPath)).PathFinder;
SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;
CurrentSubBehavior++;
}
else if (CurrentSubBehavior == 1)
{
SubJournies.Remove(currentJourney);
currentJourney = null;
SubBehaviors[CurrentSubBehavior].State = BehaviorState.Idle;
((FollowPathBehavior)SubBehaviors[CurrentSubBehavior]).Reset();
((FollowPathBehavior)SubBehaviors[CurrentSubBehavior]).PathFinder.Reset();
CurrentSubBehavior = 0;
if (SubJournies.Count == 0)
{
Owner.Locomotion.Stationary = true;
State = BehaviorState.Success;
}
}
base.OnSubBehaviorSuccess();
}
public List<Journey> SubJournies = new List<Journey>();
}
public class Journey
{
public GridCell StartNode { get; set; }
public GridCell EndNode{ get; set; }
public Journey(GridCell start, GridCell end)
{
StartNode = start;
EndNode = end;
}
}
} |