root/src/BRAINSFramework/World.cs
| 20 | 23 | ||
|---|---|---|---|
9 | using Brains.Framework.QuadTree; | 9 | using Brains.Framework.QuadTree; |
10 | using Brains.Framework.Map; | 10 | using Brains.Framework.Map; |
11 | using Brains.Framework.Utility; | 11 | using Brains.Framework.Utility; |
12 | using Brains.Framework.Behaviors.PathFinding; | ||
13 | using Brains.Grouping; | ||
12 | namespace Brains.Framework | 14 | namespace Brains.Framework |
13 | { | 15 | { |
14 | /// <summary> | 16 | /// <summary> |
... | ... | ||
23 | private List<Agent> _agentsToRemove = new List<Agent>(); | 25 | private List<Agent> _agentsToRemove = new List<Agent>(); |
24 | private Texture2D _mapTexture; | 26 | private Texture2D _mapTexture; |
25 | private WorldMap _map; | 27 | private WorldMap _map; |
28 | |||
26 | // | 29 | // |
27 | //Public properties | 30 | //Public properties |
28 | // | 31 | // |
... | ... | ||
36 | /// </summary> | 39 | /// </summary> |
37 | public List<Agent> Actors = new List<Agent>(); | 40 | public List<Agent> Actors = new List<Agent>(); |
38 | 41 | ||
42 | public Dictionary<int,Group> Groups { get; set; } | ||
43 | |||
39 | /// <summary> | 44 | /// <summary> |
40 | /// Gets the world map | 45 | /// Gets the world map |
41 | /// </summary> | 46 | /// </summary> |
42 | public WorldMap Map { get { return _map; } } | 47 | public WorldMap Map { get { return _map; } } |
43 | 48 | ||
49 | public World() | ||
50 | { | ||
51 | Groups = new Dictionary<int, Group>(); | ||
52 | } | ||
44 | // | 53 | // |
45 | //Public Methods | 54 | //Public Methods |
46 | // | 55 | // |
... | ... | ||
52 | { | 61 | { |
53 | foreach (var item in Actors) | 62 | foreach (var item in Actors) |
54 | { | 63 | { |
64 | item.UpdatePerception(gameTime); | ||
65 | } | ||
66 | foreach (var item in Actors) | ||
67 | { | ||
55 | item.Update(gameTime); | 68 | item.Update(gameTime); |
56 | } | 69 | } |
57 | if (_agentsToRemove.Count > 0) | 70 | if (_agentsToRemove.Count > 0) |
... | ... | ||
74 | { | 87 | { |
75 | SetupMap(width, height, cellsize, width / cellsize, height / cellsize,typeof(Grid)); | 88 | SetupMap(width, height, cellsize, width / cellsize, height / cellsize,typeof(Grid)); |
76 | } | 89 | } |
90 | protected static sbyte[,] direction = new sbyte[4, 2] | ||
91 | { | ||
92 | { 0,-1} , | ||
93 | { 1, 0}, | ||
94 | { 0, 1}, | ||
95 | {-1, 0} | ||
96 | }; | ||
77 | 97 | ||
78 | /// <summary> | 98 | /// <summary> |
79 | /// This method will create a game Map of the defined size and automatically cluster the map into grids | 99 | /// This method will create a game Map of the defined size and automatically cluster the map into grids |
... | ... | ||
119 | } | 139 | } |
120 | 140 | ||
121 | } | 141 | } |
122 | | 142 | |
143 | | ||
144 | | ||
123 | public void AnotateMap() | 145 | public void AnotateMap() |
124 | { | 146 | { |
125 | List<QuadTreePositionItem<GridCell>> list = new List<QuadTreePositionItem<GridCell>>(); | 147 | List<QuadTreePositionItem<GridCell>> list = new List<QuadTreePositionItem<GridCell>>(); |
... | ... | ||
197 | { | 219 | { |
198 | for (int x = 0; x < cols; x++) | 220 | for (int x = 0; x < cols; x++) |
199 | { | 221 | { |
200 | 222 | SetHorizontalEntrancesOnGrid(x, y); | |
201 | //Check right edge | 223 | SetVerticalEntrancesOnGrid(x, y); |
202 | if (cluster.Grids.Count > index + 1) | 224 | ////Check right edge |
203 | { | 225 | //if (cluster.Grids.Count > index + 1) |
204 | for (int row = 0; row < cluster.Grids[index].Rows; row++) | 226 | //{ |
205 | { | 227 | // for (int row = 0; row < cluster.Grids[index].Rows; row++) |
206 | GridCell leftcell = cluster.Grids[index].GetCell( | 228 | // { |
207 | cluster.Grids[index].Cols - 1, | 229 | // GridCell leftcell = cluster.Grids[index].GetCell( |
208 | row); | 230 | // cluster.Grids[index].Cols - 1, |
209 | GridCell rightcell = cluster.Grids[index + 1].GetCell( | 231 | // row); |
210 | 0, | 232 | // GridCell rightcell = cluster.Grids[index + 1].GetCell( |
211 | row); | 233 | // 0, |
234 | // row); | ||
212 | 235 | ||
213 | if (leftcell.Type != AIConsts.BLOCKED_TILE && | 236 | // if (leftcell.Type != AIConsts.BLOCKED_TILE && |
214 | rightcell.Type != AIConsts.BLOCKED_TILE) | 237 | // rightcell.Type != AIConsts.BLOCKED_TILE) |
215 | { | 238 | // { |
216 | leftcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 239 | // //leftcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
217 | rightcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 240 | // //rightcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
218 | } | 241 | // } |
219 | } | 242 | // } |
220 | } | 243 | //} |
221 | 244 | ||
222 | //check left edge | 245 | ////check left edge |
223 | 246 | ||
224 | if (index > 1) | 247 | //if (index > 1) |
225 | { | 248 | //{ |
226 | for (int row = 0; row < cluster.Grids[index].Rows; row++) | 249 | // for (int row = 0; row < cluster.Grids[index].Rows; row++) |
227 | { | 250 | // { |
228 | GridCell leftcell = cluster.Grids[index - 1].GetCell( | 251 | // GridCell leftcell = cluster.Grids[index - 1].GetCell( |
229 | cluster.Grids[index - 1].Cols - 1, | 252 | // cluster.Grids[index - 1].Cols - 1, |
230 | row); | 253 | // row); |
231 | GridCell rightcell = cluster.Grids[index].GetCell( | 254 | // GridCell rightcell = cluster.Grids[index].GetCell( |
232 | 0, | 255 | // 0, |
233 | row); | 256 | // row); |
234 | 257 | ||
235 | if (leftcell.Type != AIConsts.BLOCKED_TILE && | 258 | // if (leftcell.Type != AIConsts.BLOCKED_TILE && |
236 | rightcell.Type != AIConsts.BLOCKED_TILE) | 259 | // rightcell.Type != AIConsts.BLOCKED_TILE) |
237 | { | 260 | // { |
238 | leftcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 261 | // //leftcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
239 | rightcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 262 | // //rightcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
240 | } | 263 | // } |
241 | } | 264 | // } |
242 | } | 265 | //} |
243 | 266 | ||
244 | //check bottom edge | 267 | ////check bottom edge |
245 | 268 | ||
246 | if (index + cols < cluster.Grids.Count) | 269 | //if (index + cols < cluster.Grids.Count) |
247 | { | 270 | //{ |
248 | for (int col = 0; col < cluster.Grids[index].Cols; col++) | 271 | // for (int col = 0; col < cluster.Grids[index].Cols; col++) |
249 | { | 272 | // { |
250 | GridCell topcell = cluster.Grids[index].GetCell( | 273 | // GridCell topcell = cluster.Grids[index].GetCell( |
251 | col, | 274 | // col, |
252 | cluster.Grids[index].Rows - 1); | 275 | // cluster.Grids[index].Rows - 1); |
253 | GridCell bottomcell = cluster.Grids[index + cols].GetCell( | 276 | // GridCell bottomcell = cluster.Grids[index + cols].GetCell( |
254 | col, | 277 | // col, |
255 | 0); | 278 | // 0); |
256 | 279 | ||
257 | if (topcell.Type != AIConsts.BLOCKED_TILE && | 280 | // if (topcell.Type != AIConsts.BLOCKED_TILE && |
258 | bottomcell.Type != AIConsts.BLOCKED_TILE) | 281 | // bottomcell.Type != AIConsts.BLOCKED_TILE) |
259 | { | 282 | // { |
260 | bottomcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 283 | // // bottomcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
261 | topcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 284 | // // topcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
262 | } | 285 | // } |
263 | } | 286 | // } |
264 | } | 287 | //} |
265 | //Check Top | 288 | ////Check Top |
266 | if (index - cols > 0) | 289 | //if (index - cols > 0) |
267 | { | 290 | //{ |
268 | for (int col = 0; col < cluster.Grids[index].Cols; col++) | 291 | // for (int col = 0; col < cluster.Grids[index].Cols; col++) |
269 | { | 292 | // { |
270 | GridCell topcell = cluster.Grids[index - cols].GetCell( | 293 | // GridCell topcell = cluster.Grids[index - cols].GetCell( |
271 | col, | 294 | // col, |
272 | cluster.Grids[index - cols].Rows - 1); | 295 | // cluster.Grids[index - cols].Rows - 1); |
273 | GridCell bottomcell = cluster.Grids[index].GetCell( | 296 | // GridCell bottomcell = cluster.Grids[index].GetCell( |
274 | col, | 297 | // col, |
275 | 0); | 298 | // 0); |
276 | 299 | ||
277 | if (topcell.Type != AIConsts.BLOCKED_TILE && | 300 | // if (topcell.Type != AIConsts.BLOCKED_TILE && |
278 | bottomcell.Type != AIConsts.BLOCKED_TILE) | 301 | // bottomcell.Type != AIConsts.BLOCKED_TILE) |
279 | { | 302 | // { |
280 | bottomcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 303 | // //bottomcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
281 | topcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; | 304 | // //topcell.Labels[AIConsts.ISVALIDCLUSTEREDGE] = 1; |
282 | } | 305 | // } |
283 | } | 306 | // } |
284 | } | 307 | //} |
285 | index++; | 308 | index++; |
286 | } | 309 | } |
287 | } | 310 | } |
288 | } | 311 | } |
312 | |||
313 | |||
314 | //Precalculate paths for each grid transition | ||
315 | int gridIndex = 0; | ||
316 | foreach (var item in Map.ClusterGrid.Grids) | ||
317 | { | ||
318 | |||
319 | int y = (gridIndex - (gridIndex % Map.ClusterGrid.Cols)) / Map.ClusterGrid.Cols; | ||
320 | int x = gridIndex % Map.ClusterGrid.Cols; | ||
321 | //North To East | ||
322 | if (y > 0 && x < Map.ClusterGrid.Cols - 1) | ||
323 | { | ||
324 | PredeterminePaths(item,x, y - 1, x + 1, y); | ||
325 | } | ||
326 | //East To South | ||
327 | if (x < Map.ClusterGrid.Cols - 1 && y<Map.ClusterGrid.Rows-1) | ||
328 | { | ||
329 | PredeterminePaths(item, x + 1, y, x, y + 1); | ||
330 | } | ||
331 | //South To West | ||
332 | if (y < Map.ClusterGrid.Rows - 1 && x > 0) | ||
333 | { | ||
334 | PredeterminePaths(item, x, y + 1, x - 1, y); | ||
335 | } | ||
336 | //West To Nort | ||
337 | if (x>0 && y>0) | ||
338 | { | ||
339 | PredeterminePaths(item, x - 1, y, x, y - 1); | ||
340 | } | ||
341 | |||
342 | //West To East | ||
343 | if (x > 0 && x < Map.ClusterGrid.Cols-1) | ||
344 | { | ||
345 | PredeterminePaths(item, x - 1, y, x + 1, y); | ||
346 | } | ||
347 | |||
348 | //North To South | ||
349 | if (y > 0 && y < Map.ClusterGrid.Rows- 1) | ||
350 | { | ||
351 | PredeterminePaths(item, x, y - 1, x, y + 1); | ||
352 | } | ||
353 | gridIndex++; | ||
354 | } | ||
355 | |||
356 | |||
357 | //Cluster cl = Map.ClusterGrid; | ||
358 | //int index1 = 0; | ||
359 | |||
360 | |||
361 | //foreach (var item in Map.ClusterGrid.Grids) | ||
362 | //{ | ||
363 | // int y = (index1 - (index1 % cl.Cols)) / cl.Cols; | ||
364 | // int x = index1 % cl.Cols; | ||
365 | |||
366 | // //Top To Right | ||
367 | // if (y > 0 && x < cl.Cols - 1) | ||
368 | // { | ||
369 | // int indexF = (y-1) * cl.Cols + x ; | ||
370 | // int indexT = y * cl.Cols + (x+1); | ||
371 | // GridCell from; | ||
372 | // GridCell to; | ||
373 | |||
374 | // from = GetFreeCellAt(item, Direction.up); | ||
375 | // to = GetFreeCellAt(item, Direction.rigth); | ||
376 | // if(from!=null&& to!=null) | ||
377 | // CalculatePath(from, to,indexF,indexT); | ||
378 | // } | ||
379 | // //Right To Bottom | ||
380 | // if (y < cl.Rows - 1 && x < cl.Cols - 1) | ||
381 | // { | ||
382 | // int indexF = y * cl.Cols + (x + 1); | ||
383 | // int indexT = (y + 1) * cl.Cols + x; | ||
384 | // GridCell from; | ||
385 | // GridCell to; | ||
386 | |||
387 | // from = GetFreeCellAt(item, Direction.rigth); | ||
388 | // to = GetFreeCellAt(item, Direction.down); | ||
389 | // if (from != null && to != null) | ||
390 | // CalculatePath(from, to, indexF, indexT); | ||
391 | // } | ||
392 | // //Bottom To Left | ||
393 | // if (y < cl.Rows - 1 && x > 0) | ||
394 | // { | ||
395 | // int indexF = (y + 1) * cl.Cols + x; | ||
396 | // int indexT = y * cl.Cols + (x - 1); | ||
397 | // GridCell from; | ||
398 | // GridCell to; | ||
399 | |||
400 | // from = GetFreeCellAt(item, Direction.down); | ||
401 | // to = GetFreeCellAt(item, Direction.left); | ||
402 | // if (from != null && to != null) | ||
403 | // CalculatePath(from, to, indexF, indexT); | ||
404 | // } | ||
405 | // //Left To Top | ||
406 | // if (y > 0 && x > 0) | ||
407 | // { | ||
408 | // int indexF = y * cl.Cols + (x-1); | ||
409 | // int indexT = (y-1) * cl.Cols + x; | ||
410 | // GridCell from; | ||
411 | // GridCell to; | ||
412 | |||
413 | // from = GetFreeCellAt(item, Direction.left); | ||
414 | // to = GetFreeCellAt(item, Direction.up); | ||
415 | // if (from != null && to != null) | ||
416 | // CalculatePath(from, to, indexF, indexT); | ||
417 | // } | ||
418 | // //Top to bottom | ||
419 | // if (y > 0 && y < cl.Rows - 1) | ||
420 | // { | ||
421 | // int indexF = (y - 1) * cl.Cols + x; | ||
422 | // int indexT = (y + 1) * cl.Cols + x; | ||
423 | // GridCell from; | ||
424 | // GridCell to; | ||
425 | |||
426 | // from = GetFreeCellAt(item, Direction.up); | ||
427 | // to = GetFreeCellAt(item, Direction.down); | ||
428 | // if (from != null && to != null) | ||
429 | // CalculatePath(from, to, indexF, indexT); | ||
430 | // } | ||
431 | // //Left To Right | ||
432 | // if (x > 0 && x < cl.Cols - 1) | ||
433 | // { | ||
434 | // int indexF = y * cl.Cols + (x - 1); | ||
435 | // int indexT = y * cl.Cols + (x + 1); | ||
436 | // GridCell from; | ||
437 | // GridCell to; | ||
438 | |||
439 | // from = GetFreeCellAt(item, Direction.left); | ||
440 | // to = GetFreeCellAt(item, Direction.rigth); | ||
441 | // if (from != null && to != null) | ||
442 | // CalculatePath(from, to,indexF,indexT); | ||
443 | // } | ||
444 | |||
445 | // index1++; | ||
446 | //} | ||
289 | } | 447 | } |
448 | |||
449 | private void PredeterminePaths(Grid grid,int x1,int y1,int x2,int y2) | ||
450 | { | ||
451 | |||
452 | int indexF = Map.ClusterGrid.GetGridIndex(x1, y1); | ||
453 | int indexT = Map.ClusterGrid.GetGridIndex(x2, y2); | ||
454 | |||
455 | Grid gridA = Map.ClusterGrid.GetGrid(x1, y1); | ||
456 | GridCell cellA= GetValidCellFromEntrance(gridA,grid); | ||
457 | |||
458 | Grid gridB= Map.ClusterGrid.GetGrid(x2, y2); | ||
459 | GridCell cellB = GetValidCellFromEntrance(gridB, grid); | ||
460 | |||
461 | if (cellB != null && cellA != null) | ||
462 | { | ||
463 | CalculatePath( | ||
464 | grid.GetCell(cellA.X,cellA.Y), | ||
465 | grid.GetCell(cellB.X, cellB.Y), | ||
466 | indexF, | ||
467 | indexT); | ||
468 | } | ||
469 | } | ||
470 | |||
471 | private GridCell GetValidCellFromEntrance(Grid fromGrid,Grid toGrid) | ||
472 | { | ||
473 | foreach (var entrance in Map.ClusterGrid.Entrances) | ||
474 | { | ||
475 | if (entrance.CellA.Parent == fromGrid && entrance.CellB.Parent==toGrid) | ||
476 | return entrance.CellB; | ||
477 | if (entrance.CellB.Parent == fromGrid && entrance.CellA.Parent == toGrid) | ||
478 | return entrance.CellA; | ||
479 | } | ||
480 | return null; | ||
481 | } | ||
482 | |||
483 | private void SetVerticalEntrancesOnGrid(int x, int y) | ||
484 | { | ||
485 | bool checkEast = false; | ||
486 | bool checkWest = false; | ||
487 | |||
488 | Grid thisGrid = Map.ClusterGrid.GetGrid(x, y); | ||
489 | |||
490 | if (x > 0) | ||
491 | checkWest= true; | ||
492 | |||
493 | if (x < Map.ClusterGrid.Cols- 1) | ||
494 | checkEast = true; | ||
495 | |||
496 | if (checkEast) | ||
497 | { | ||
498 | Grid eastGrid = Map.ClusterGrid.GetGrid(x+1, y); | ||
499 | SetVertEntranceBetweenGrids(thisGrid, eastGrid); | ||
500 | } | ||
501 | if (checkWest) | ||
502 | { | ||
503 | Grid westGrid= Map.ClusterGrid.GetGrid(x-1, y); | ||
504 | SetVertEntranceBetweenGrids(thisGrid, westGrid); | ||
505 | } | ||
506 | } | ||
507 | |||
508 | private void SetVertEntranceBetweenGrids(Grid gridA, Grid gridB) | ||
509 | { | ||
510 | int yIndexa = 0; | ||
511 | int yIndexb = gridA.Cols- 1; | ||
512 | if (gridB.Position.X > gridA.Position.X) | ||
513 | { | ||
514 | yIndexa = yIndexb; | ||
515 | yIndexb = 0; | ||
516 | } | ||
517 | //Start near the middle | ||
518 | int startRow = gridA.Rows/ 2; | ||
519 | //Currently supports 1 transition per set of grids | ||
520 | bool continueChecking = true; | ||
521 | //Loop to end ignoring the corner cell | ||
522 | for (int i = startRow; i < gridA.Rows - 1; i++) | ||
523 | { | ||
524 | |||
525 | GridCell _edgeCell = gridA.GetCell(yIndexa,i); | ||
526 | GridCell _adjacentCell = gridB.GetCell(yIndexb,i); | ||
527 | if (_edgeCell.Labels[AIConsts.BLOCKED_TILE] == 0 && | ||
528 | _adjacentCell.Labels[AIConsts.BLOCKED_TILE] == 0) | ||
529 | { | ||
530 | Map.ClusterGrid.AddEntrance(_edgeCell, _adjacentCell); | ||
531 | continueChecking = false; | ||
532 | break; | ||
533 | } | ||
534 | } | ||
535 | if (continueChecking) | ||
536 | { | ||
537 | //Loop to beginning column ignoring the corner cell | ||
538 | for (int i = startRow; i > 0; i--) | ||
539 | { | ||
540 | GridCell _edgeCell = gridA.GetCell(yIndexa, i); | ||
541 | GridCell _adjacentCell = gridB.GetCell(yIndexb, i); | ||
542 | if (_edgeCell.Labels[AIConsts.BLOCKED_TILE] == 0 && _adjacentCell.Labels[AIConsts.BLOCKED_TILE] == 0) | ||
543 | { | ||
544 | Map.ClusterGrid.AddEntrance(_edgeCell, _adjacentCell); | ||
545 | continueChecking = false; | ||
546 | break; | ||
547 | } | ||
548 | } | ||
549 | } | ||
550 | |||
551 | } | ||
290 | 552 | ||
553 | private void SetHorizontalEntrancesOnGrid(int x, int y) | ||
554 | { | ||
555 | bool checkNorth = false; | ||
556 | bool checkSouth = false; | ||
557 | |||
558 | Grid thisGrid = Map.ClusterGrid.GetGrid(x,y); | ||
559 | |||
560 | if (y > 0) | ||
561 | checkNorth = true; | ||
562 | |||
563 | if (y < Map.ClusterGrid.Rows-1) | ||
564 | checkSouth= true; | ||
565 | |||
566 | if (checkNorth) | ||
567 | { | ||
568 | Grid northGrid = Map.ClusterGrid.GetGrid(x, y - 1); | ||
569 | SetHorizEntranceBetweenGrids(thisGrid, northGrid); | ||
570 | } | ||
571 | if (checkSouth) | ||
572 | { | ||
573 | Grid southGrid = Map.ClusterGrid.GetGrid(x, y + 1); | ||
574 | SetHorizEntranceBetweenGrids(thisGrid, southGrid); | ||
575 | } | ||
576 | |||
577 | |||
578 | } | ||
579 | |||
580 | private void SetHorizEntranceBetweenGrids(Grid gridA, Grid gridB) | ||
581 | { | ||
582 | int yIndexa = 0; | ||
583 | int yIndexb = gridA.Rows - 1; | ||
584 | if (gridB.Position.Y > gridA.Position.Y) | ||
585 | { | ||
586 | yIndexa = yIndexb; | ||
587 | yIndexb = 0; | ||
588 | } | ||
589 | //Start near the middle | ||
590 | int startCol = gridA.Cols / 2; | ||
591 | //Currently supports 1 transition per set of grids | ||
592 | bool continueChecking = true; | ||
593 | //Loop to end ignoring the corner cell | ||
594 | for (int i = startCol; i < gridA.Cols - 1; i++) | ||
595 | { | ||
596 | |||
597 | GridCell _edgeCell = gridA.GetCell(i, yIndexa); | ||
598 | GridCell _adjacentCell = gridB.GetCell(i, yIndexb); | ||
599 | if (_edgeCell.Labels[AIConsts.BLOCKED_TILE] == 0 && | ||
600 | _adjacentCell.Labels[AIConsts.BLOCKED_TILE] == 0) | ||
601 | { | ||
602 | Map.ClusterGrid.AddEntrance(_edgeCell, _adjacentCell); | ||
603 | continueChecking = false; | ||
604 | break; | ||
605 | } | ||
606 | } | ||
607 | if (continueChecking) | ||
608 | { | ||
609 | //Loop to beginning column ignoring the corner cell | ||
610 | for (int i = startCol; i > 0; i--) | ||
611 | { | ||
612 | GridCell _edgeCell = gridA.GetCell(i, yIndexa); | ||
613 | GridCell _adjacentCell = gridB.GetCell(i, yIndexb); | ||
614 | if (_edgeCell.Labels[AIConsts.BLOCKED_TILE] == 0 && _adjacentCell.Labels[AIConsts.BLOCKED_TILE] == 0) | ||
615 | { | ||
616 | Map.ClusterGrid.AddEntrance(_edgeCell, _adjacentCell); | ||
617 | continueChecking = false; | ||
618 | break; | ||
619 | } | ||
620 | } | ||
621 | } | ||
622 | |||
623 | |||
624 | } | ||
625 | |||
626 | private void CalculatePath(GridCell from, GridCell to,int gridfrom,int gridto) | ||
627 | { | ||
628 | PathFinding.PathFinder _f = new Brains.Framework.PathFinding.PathFinder(); | ||
629 | _f.Grid = from.Parent; | ||
630 | _f.StartNode = from; | ||
631 | _f.EndNode = to; | ||
632 | _f.Reset(); | ||
633 | |||
634 | while (_f.State == Brains.Framework.PathFinding.PathFinderState.Working) | ||
635 | { | ||
636 | _f.Iterate(); | ||
637 | } | ||
638 | if (_f.State == Brains.Framework.PathFinding.PathFinderState.Finished) | ||
639 | { | ||
640 | // copying nodes | ||
641 | PreStoredPath _path = new PreStoredPath(); | ||
642 | _path.Nodes = new List<StoredPathNode>(); | ||
643 | |||
644 | int counter = 0; | ||
645 | //for (int index = pathFinder.closeList.Count - 1; index >= 0; index--) | ||
646 | for (int index = 0; index <= _f.closeList.Count - 1; index++) | ||
647 | { | ||
648 | StoredPathNode newNode = new StoredPathNode(); | ||
649 | Grid grid = from.Parent; | ||
650 | GridCell _node = grid.GetCell(_f.closeList[index].X, | ||
651 | _f.closeList[index].Y); | ||
652 | newNode.nodePosition.X = grid.GetCell(_f.closeList[index].X, | ||
653 | _f.closeList[index].Y).Position.X; | ||
654 | newNode.nodePosition.Y = grid.GetCell(_f.closeList[index].X, | ||
655 | _f.closeList[index].Y).Position.Y; | ||
656 | newNode.X = _f.closeList[index].X; | ||
657 | newNode.Y = _f.closeList[index].Y; | ||
658 | |||
659 | |||
660 | _path.FromGrid =gridfrom; | ||
661 | _path.ToGrid = gridto; | ||
662 | //pathNodes[counter] = newNode; | ||
663 | _path.Nodes.Add(newNode); | ||
664 | counter++; | ||
665 | } | ||
666 | |||
667 | |||
668 | from.Parent.PredeterminedPaths.Add(_path); | ||
669 | } | ||
670 | if (_f.State == Brains.Framework.PathFinding.PathFinderState.Failed) | ||
671 | { | ||
672 | Console.WriteLine(""); | ||
673 | } | ||
674 | |||
675 | } | ||
676 | |||
291 | /// <summary> | 677 | /// <summary> |
292 | /// Adds the specified Actor to the Actors collection | 678 | /// Adds the specified Actor to the Actors collection |
293 | /// </summary> | 679 | /// </summary> |
... | ... | ||
324 | /// <param name="cols">The number of cols to split the map into</param> | 710 | /// <param name="cols">The number of cols to split the map into</param> |
325 | /// <param name="rows">The number of rows to split the map into</param> | 711 | /// <param name="rows">The number of rows to split the map into</param> |
326 | /// <param name="gridType">The type of grid to create</param> | 712 | /// <param name="gridType">The type of grid to create</param> |
327 | public void LoadMapDataFromTexture(Texture2D texture, int cols, int rows, int cellSize, Type gridType) | 713 | public void LoadMapDataFromTexture(Texture2D texture, int clusterColumnSplit, int clusterRowSplit, int cellSize, Type gridType) |
328 | { | 714 | { |
329 | _mapTexture = texture; | 715 | _mapTexture = texture; |
330 | 716 | ||
331 | Vector2 position = new Vector2(); | 717 | Vector2 position = new Vector2(); |
332 | int clusterCols = texture.Width / cols; | 718 | int clusterCols = texture.Width / clusterColumnSplit; |
333 | int clusterRows = texture.Height / rows; | 719 | int clusterRows = texture.Height / clusterRowSplit; |
334 | 720 | ||
335 | SetupMap( | 721 | SetupMap( |
336 | texture.Width * cellSize, | 722 | texture.Width * cellSize, |
... | ... | ||
341 | gridType); | 727 | gridType); |
342 | 728 | ||
343 | int index = 0; | 729 | int index = 0; |
344 | for (int y = 0; y < rows; y++) | 730 | for (int y = 0; y < clusterRowSplit; y++) |
345 | { | 731 | { |
346 | for (int x = 0; x < cols; x++) | 732 | for (int x = 0; x < clusterColumnSplit; x++) |
347 | { | 733 | { |
348 | Grid item = Map.ClusterGrid.Grids[index]; | 734 | Grid item = Map.ClusterGrid.Grids[index]; |
349 | index++; | 735 | index++; |
... | ... | ||
408 | { | 794 | { |
409 | _agentsToRemove.Add(agent); | 795 | _agentsToRemove.Add(agent); |
410 | } | 796 | } |
797 | |||
798 | public void AddGroup(Group group) | ||
799 | { | ||
800 | Groups.Add(group.GroupID, group); | ||
801 | } | ||
802 | public Group GetGroup(int id) | ||
803 | { | ||
804 | return Groups[id]; | ||
805 | } | ||
411 | } | 806 | } |
412 | } | 807 | } |
Download diff