public static MovementPath AStar(Point start, Point end, Stage stage = null) { MovementPath newPath = new MovementPath(null); if (stage == null) { stage = Stage.CurrentStage; } // setup grid with walls //MyPathNode[,] grid = new MyPathNode[(int)stage.Width, (int)stage.Height]; MyPathNode[][] grid = new MyPathNode[(int)stage.Width][]; for (int x = 0; x < (int)stage.Width; x++) { grid[x] = new MyPathNode[(int) stage.Height]; for (int y = 0; y < (int)stage.Height; y++) { grid[x][y] = new MyPathNode() { IsWall = stage.GetTileAt(x, y).GetWalkable(), X = x, Y = y, }; } } SpatialAStar<MyPathNode, Object> aStar = new SpatialAStar<MyPathNode, Object>(grid); LinkedList<MyPathNode> path = aStar.Search(new Point(0, 0), new Point(stage.Width - 2, stage.Height - 2), null); return null; }
public static void Init() { string[] filePaths = Directory.GetFiles(@"map"); foreach (var file in filePaths) { var mapn = file.Substring(file.IndexOf(Path.DirectorySeparatorChar) + 1, file.Count() - (file.IndexOf(Path.DirectorySeparatorChar) + 1)); if (mapsizes.ContainsKey(mapn)) { var tl = new TiledMap(mapn, mapsizes[mapn].X, mapsizes[mapn].Y); var input = File.ReadAllBytes(file); int cnt = mapsizes[mapn].X * mapsizes[mapn].Y; int itr = 0, res2 = 0, res1 = 0; byte[] temp = new byte[4]; while (itr != cnt) { Array.Copy(input, offset + (itr * 4), temp, 0, 4); if (itr % mapsizes[mapn].X == 0 && itr != 0) { res2++; res1 = 0; } if (temp[0] == 3 || temp[0] == 5 || (temp[0] == 2 && temp[2] == 3)) { var tempp = new MyPathNode(res1, res2); tempp.IsWall = true; tl.tiles[res1++,res2] = tempp; //0 } else { var temp2 = new MyPathNode(res1, res2); tl.tiles[res1++,res2] = temp2; //1 } itr++; } loadedmaps.Add(mapn, tl); } } }
public MyPathNode AIStep(TiledMap map, Player target) { var mobs = World.NewMonsters.Where(xe => xe.Value != this && xe.Value.Alive && xe.Value.m_Map == target.Map); foreach (var mob in mobs) map.tiles[mob.Value.m_Loc.X, mob.Value.m_Loc.Y].IsWall = true; aStar = new MySolver<MyPathNode, Object>(map.tiles); path = aStar.SearchOnce(new Point(m_Loc.X, m_Loc.Y), new Point(target.X, target.Y), null); foreach (var mob in mobs) map.tiles[mob.Value.m_Loc.X, mob.Value.m_Loc.Y].IsWall = false; MyPathNode p = new MyPathNode(m_Loc.X, m_Loc.Y); if (path != null) { p.X = path.X; p.Y = path.Y; } path = null; aStar.Dispose(); aStar = null; return p; }
public void moveSpiders(MyPathNode[,] grid, int gridSize) { Random r = new Random(); if (r.Next() % 2 == 0) { // move spiders int action = (r.Next() % 4) + 1; doStuff(grid, action, 1, gridSize); } else { // jump spiders int action = (r.Next() % 4) + 1; doStuff(grid, action, 2, gridSize); } }
public void doStuff(MyPathNode[,] maze, int action, int steps, int dimension) { switch (action) { case 1: // Move / Jump Right if (X < (dimension - steps) && maze[(X + steps), Y].IsWall) { maze[X,Y].IsWall = true; X += steps; if (setType == IsType.Big) { maze[X, Y].IsSpider = "big"; maze[X, Y].IsWall = false; } else if (setType == IsType.Medium) { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "medium"; } else { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "small"; } break; } break; case 2: // Move / Jump left if ((X - steps) > 0 && maze[(X-steps), Y].IsWall) { maze[X, Y].IsWall = true; X -= steps; if (setType == IsType.Big) { maze[X, Y].IsSpider = "big"; maze[X, Y].IsWall = false; } else if (setType == IsType.Medium) { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "medium"; } else { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "small"; } break; } break; case 3: // Move / Jump Top if ((Y - steps) > 0 && maze[X, (Y - steps)].IsWall) { maze[X, Y].IsWall = true; Y -= steps; if (setType == IsType.Big) { maze[X, Y].IsSpider = "big"; maze[X, Y].IsWall = false; } else if (setType == IsType.Medium) { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "medium"; } else { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "small"; } break; } break; case 4: // Move / Jump Bottom if (Y < (dimension - steps) && maze[X, (Y + steps)].IsWall) { maze[X, Y].IsWall = true; Y += steps; if (setType == IsType.Big) { maze[X, Y].IsSpider = "big"; maze[X, Y].IsWall = false; } else if (setType == IsType.Medium) { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "medium"; } else { maze[X, Y].IsWall = false; maze[X, Y].IsSpider = "small"; } break; } break; default: break; } }
private static unsafe MyPathNode[,] testGrid() { Random rnd = new Random(); MyPathNode[,] grid = new MyPathNode[gridSize, gridSize]; { // setup grid with walls for (int x = 0; x < gridSize; x++) { for (int y = 0; y < gridSize; y++) { Boolean isWall = ((y % 2) != 0) && (rnd.Next(0, 10) != 8); grid[x, y] = new MyPathNode() { IsWall = isWall, X = x, Y = y, }; } } } return grid; }
private static bool isThesusSafe(MyPathNode thesus, MySpiderNode spider) { if ((Math.Abs(spider.X - thesus.X) + Math.Abs(spider.Y - thesus.Y)) <= 2) { return false; } return true; }
public void LoadGrid() { int playerX, playerY, playerZ; playerX = client.PlayerLocation.X; playerY = client.PlayerLocation.Y; playerZ = client.PlayerLocation.Z; for (int x = -GridSize / 2; x < GridSize / 2; x++) { for (int y = -GridSize / 2; y < GridSize / 2; y++) { int ArryX = playerX + x; int ArryY = playerY + y; int Speed = 0; bool isBlocking = IsBlocking(ArryX, ArryY, playerZ); grid[x + GridSize / 2, y + GridSize / 2] = new MyPathNode() { IsWall = isBlocking, X = x + GridSize / 2, Y = y + GridSize / 2, Cost = Speed, }; } } foreach (Tile t in client.Map.GetTilesSameFloor()) { bool isWall = false; int cost = 0; int cx, cy; cx = t.Location.X - playerX + GridSize / 2; cy = t.Location.Y - playerY + GridSize / 2; if (t.IsBlocking()) { isWall = true; cost = 500; } if (t.Ground.ItemData.Blocking || t.Ground.ItemData.BlocksPath || t.Items.Any(i => i.ItemData.Blocking || i.ItemData.BlocksPath)) { isWall = true; cost = 500; } foreach (TileObject o in t.Objects) { if (o.Id == 99) { //has creature if (o.Data == Core.client.Memory.ReadInt32(client.Addresses.Player.Id)) { break; } Creature cr = Core.client.Battlelist.GetCreatures().First(c => c.Id == o.Data); if (cr != null) { Objects.Bot.Target target = Core.Global.TargetList.FirstOrDefault(j => j.Name.ToLower() == cr.Name.ToLower()); if (target == null) { //it means it is a monster that we dont want. isWall = true; cost = 500; } } } } grid[cx, cy] = new MyPathNode() { IsWall = isWall, // X = t.Location.X - playerX, // Y = t.Location.Y - playerY, X = cx, Y = cy, Cost = 1, }; } }
public static void Find_Right_Path() { MyPathNode[,] grid = new MyPathNode[x_Length, y_Length]; bool isWall = false; for (var index_x = 0; index_x < x_Length; index_x++) { for (var index_y = 0; index_y < y_Length; index_y++) { isWall = false; switch (Board.Matrix[index_x, index_y].MyValue) { case MyValueType.snake: isWall = true; break; case MyValueType.wall: isWall = true; break; default: break; } grid[index_x, index_y] = new MyPathNode() { IsWall = isWall, X = index_x, Y = index_y, }; } } Snake_Element Main_element = SnakeElements.Where(x => x.Is_Main).ToList()[0]; MySolver <MyPathNode, Object> aStar = new MySolver <MyPathNode, object>(grid); LinkedList <MyPathNode> path = aStar.Search(new Point(Main_element.Point.x, Main_element.Point.y), new Point(target_position.x, target_position.y), null); if (path == null) { Game_Over(); } else { if (path.Count == 0) { Game_Over(); } } foreach (var item in path) { Right_Path.Add(new MyPoint(item.X, item.Y)); } Right_Path.RemoveAt(0); }
public IEnumerable <MyPathNode> GetPath(Objects.Location loc, int maxX = 150, int maxY = 150, bool isTarget = false) { System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch s2 = new System.Diagnostics.Stopwatch(); System.Diagnostics.Stopwatch s3 = new System.Diagnostics.Stopwatch(); s.Start(); int playerX, playerY, playerZ, playerId; playerX = client.PlayerLocation.X; playerY = client.PlayerLocation.Y; playerZ = client.PlayerLocation.Z; int StartX, StartY, EndX, EndY; StartX = maxX; StartY = maxY; EndX = loc.X - playerX + maxX; EndY = loc.Y - playerY + maxY; playerId = client.Player.Id; if (client.PlayerLocation.Z != loc.Z) { return(null); } MyPathNode[,] grid = new MyPathNode[maxX * 2, maxY * 2]; this.LoadMapfiles(); s.Stop(); s2.Start(); for (int x = 0; x < maxX * 2; x++) { for (int y = 0; y < maxX * 2; y++) { int xdiff = playerX + x - maxX; int ydiff = playerY + y - maxY; int MovmentSpeed = GetTileCost(xdiff, ydiff); bool isWall = IsBlocking(xdiff, ydiff); grid[x, y] = new MyPathNode() { IsWall = isWall, X = x, Y = y, Cost = 1, }; } } s2.Stop(); s3.Start(); foreach (Tile t in client.Map.GetTilesSameFloor()) { bool isWall = false; int cx, cy; cx = t.Location.X - playerX + 150; cy = t.Location.Y - playerY + 150; if (t.IsBlocking()) { isWall = true; } grid[cx, cy] = new MyPathNode() { IsWall = isWall, X = cx, Y = cy, Cost = 0, }; } grid[EndX, EndY] = new MyPathNode() { IsWall = false, X = EndX, Y = EndY, Cost = 0, }; s3.Stop(); client.StatusBar = "Load MapFiles = " + s.ElapsedMilliseconds.ToString() + " process mapfiles = " + s2.ElapsedMilliseconds.ToString() + " blist = " + s3.ElapsedMilliseconds.ToString(); MySolver <MyPathNode, Object> aStar = new MySolver <MyPathNode, Object>(grid); IEnumerable <MyPathNode> path = aStar.Search(new Point(StartX, StartY), new Point(EndX, EndY), null); return(path); }