public void findPath() { aStar = new MySolver <MyPathNode, Object>(grid); try { if (tanks[playerNum].health < 70) { if (lifePacks.ElementAt <LifePack>(0) != null) { path = aStar.Search(new Vector2(position.X, position.Y), lifePacks, null, tanks[playerNum].direction); } else { path = aStar.Search(new Vector2(position.X, position.Y), coinPiles, null, tanks[playerNum].direction); } } else { if (coinPiles.ElementAt <Coins>(0) == null) { path = aStar.Search(new Vector2(position.X, position.Y), new Vector2(9, 9), null, tanks[playerNum].direction); } else { path = aStar.Search(new Vector2(position.X, position.Y), coinPiles, null, tanks[playerNum].direction); } } } catch (ArgumentOutOfRangeException e) { path = aStar.Search(new Vector2(position.X, position.Y), new Vector2(9, 9), null, tanks[playerNum].direction); } }
public void findUpdatedPath(int currentX, int currentY) { MySolver <MyPathNode, System.Object> aStar = new MySolver <MyPathNode, System.Object>(Game.grid); IEnumerable <MyPathNode> path = aStar.Search(new Vector2(currentX, currentY), new Vector2(endGridPosition.x, endGridPosition.y), null); int x = 0; if (path != null) { foreach (MyPathNode node in path) { if (x == 1) { nextNode = node; break; } x++; } // Changes enemy's path color //foreach (GameObject g in GameObject.FindGameObjectsWithTag("GridBox")) //{ // if (g.GetComponent<Renderer>().material.color != Color.red && g.GetComponent<Renderer>().material.color == myColor) // g.GetComponent<Renderer>().material.color = Color.white; //} //foreach (MyPathNode node in path) //{ // //GameObject.Find(node.X + "," + node.Y).GetComponent<Renderer>().material.color = myColor; //} } }
public void FindUpdatedPath(float currentX, float currentY) { if (_matrixMap.MatrixMapCells == null) { Debug.LogError("_matrixMap.MatrixMapCells == null"); return; } var aStar = new MySolver <MatrixMapCell, object>(_matrixMap.MatrixMapCells); var path = aStar.Search(new Vector2(currentX, currentY), new Vector2(_endGridPosition.X, _endGridPosition.Y), null, _formula, _enemy.Flying); var x = 0; if (path != null) { foreach (var node in path) { if (x == 1) { _nextNode = node; break; } x++; } } else { Debug.LogError("I'm stuck!"); _matrixMap.RemoveLastTower(); } }
public IEnumerable <MyPathNode> GetPath(Location loc) { System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); s.Start(); LoadGrid(); int startX, startY, EndX, EndY; startX = GridSize / 2;// the center of the grid is player location startY = GridSize / 2; EndX = loc.X - client.PlayerLocation.X + GridSize / 2; EndY = loc.Y - client.PlayerLocation.Y + GridSize / 2; grid[EndX, EndY] = new MyPathNode() { IsWall = false, X = EndX, Y = EndY, Cost = 1, }; MySolver <MyPathNode, Object> aStar = new MySolver <MyPathNode, Object>(grid); IEnumerable <MyPathNode> path = aStar.Search(new Point(startX, startY), new Point(EndX, EndY), null); s.Stop(); client.StatusBar = "total MS = " + s.ElapsedMilliseconds.ToString(); return(path); }
public void FindPath() { previousLastPosition.X = -1; previousLastPosition.Y = -1; MySolver <PathNode, Object> aStar = new MySolver <PathNode, Object>(grid); LinkedList <PathNode> path = aStar.Search(new System.Drawing.Point(playerPosition.X, playerPosition.Y), new System.Drawing.Point(EnemyPosition.X, EnemyPosition.Y), null); PathNode lastPathNode; PathNode previousLastPathNode; if (path != null) { if (path.Last.List.Count > 2) { previousLastPathNode = path.Last.Previous.Previous.Value; previousLastPosition.X = previousLastPathNode.X; previousLastPosition.Y = previousLastPathNode.Y; } if (path.Last.List.Count > 1) { lastPathNode = path.Last.Previous.Value; } else { lastPathNode = path.Last.Value; } lastPosition.X = lastPathNode.X; lastPosition.Y = lastPathNode.Y; } }
public void findNext() { int counter = 0; try { foreach (MyPathNode node in path) { if (counter == 0) { counter++; continue; } Vector2 p = new Vector2(node.X, node.Y); if (p.X == position.X) { if (p.Y == position.Y - 1) { nextMove = "UP#"; } else if (p.Y == position.Y + 1) { nextMove = "DOWN#"; } else { Console.WriteLine("Wrong algo!!!!!!!!!!!!!!" + p.Y + "\t" + position.Y); } } else if (p.Y == position.Y) { if (p.X == position.X - 1) { nextMove = "LEFT#"; } else if (p.X == position.X + 1) { nextMove = "RIGHT#"; } else { Console.WriteLine("Wrong algo!!!!!!!!!!!!!!" + p.X + "\t" + position.X); } } else { Console.WriteLine("Wrong algo!!!!!!!!!!!!!!" + p.X + "\t" + position.X + "\t" + p.Y + "\t" + position.Y); } break; } } catch (NullReferenceException e) { Console.WriteLine("No path found!!!"); path = aStar.Search(new Vector2(position.X, position.Y), new Vector2(0, 0), null, tanks[playerNum].direction); // this.findPath(); this.findNext(); } }
void Start() { myColor = getRandomColor(); // Straight line pathfinding int startPos = UnityEngine.Random.Range(0, Game.gridHeight - 1); int endPos = (int)fetchGridPosition().y; startGridPosition = new gridPosition(0, startPos); endGridPosition = new gridPosition(17, startPos); initializePosition(); MySolver <MyPathNode, System.Object> aStar = new MySolver <MyPathNode, System.Object>(Game.grid); IEnumerable <MyPathNode> path = aStar.Search(new Vector2(startGridPosition.x, startGridPosition.y), new Vector2(endGridPosition.x, endGridPosition.y), null); updatePath(); this.GetComponent <Renderer>().material.color = myColor; }
/// <summary> /// Sets NextNode to be the next best tile to take for the AStar algorithm. /// </summary> private void findUpdatedPath(int currentX, int currentY) { MySolver <MyPathNode, System.Object> aStar = new MySolver <MyPathNode, System.Object>(grid); IEnumerable <MyPathNode> path = aStar.Search(new Vector2(currentX, currentY), new Vector2(endGridPosition.x, endGridPosition.y), null); int x = 0; if (path != null) { foreach (MyPathNode node in path) { if (x == 1) { NextGridNode = node; break; } x++; } } }
public ArrayList generateNewPath(Vector2 startPos, Vector2 endPos) { MySolver <MyPathNode, System.Object> aStar = new MySolver <MyPathNode, System.Object>(grid); LinkedList <MyPathNode> Newpath = aStar.Search(startPos, endPos, null); ArrayList path = new ArrayList(); if (Newpath != null) { foreach (MyPathNode tempstep in Newpath) { path.Add(new Vector2(tempstep.X, tempstep.Y)); } path.RemoveAt(0);//удаляем первый элемент, потому что он равен текущей позиции } else { path.Add(Vector2.zero); } return(path); }
// Use this for initialization void Start() { startGridPosition = new gridPosition(0, UnityEngine.Random.Range(0, Game.gridHeight - 1)); endGridPosition = new gridPosition(Game.gridWidth - 1, UnityEngine.Random.Range(0, Game.gridHeight - 1)); initializePosition(); MySolver <MyPathNode, System.Object> aStar = new MySolver <MyPathNode, System.Object>(Game.grid); IEnumerable <MyPathNode> path = aStar.Search(new Vector2(startGridPosition.x, startGridPosition.y), new Vector2(endGridPosition.x, endGridPosition.y), null); foreach (GameObject g in GameObject.FindGameObjectsWithTag("GridBox")) { g.GetComponent <Renderer>().material.color = Color.white; } findUpdatedPath(currentGridPosition.x, currentGridPosition.y); this.GetComponent <Renderer>().material.color = myColor; }
void InitPath() { endGridPosition = new gridPosition(Game.targetx, Game.targety); startGridPosition = new gridPosition(startx, starty); MySolver <MyPathNode, System.Object> aStar = new MySolver <MyPathNode, System.Object>(Game.grid); IEnumerable <MyPathNode> path = aStar.Search(new Vector2(startGridPosition.x, startGridPosition.y), new Vector2(endGridPosition.x, endGridPosition.y), null); //foreach (GameObject g in GameObject.FindGameObjectsWithTag("GridBox")) //{ // g.GetComponent<Renderer>().material.color = Color.white; //} updatePath(); this.GetComponent <Renderer>().material.color = myColor; }
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); }
private unsafe void Form1_Load(object sender, EventArgs e) { try { Random rnd = new Random(); Bitmap gridBmp = new Bitmap(512, 512, System.Drawing.Imaging.PixelFormat.Format32bppArgb); MyPathNode[,] grid = new MyPathNode[gridBmp.Width, gridBmp.Height]; SettlersEngine.ImagePixelLock locked = new SettlersEngine.ImagePixelLock(gridBmp, false); using (locked) { int *pixels = locked.Pixels; // setup grid with walls for (int x = 0; x < gridBmp.Width; x++) { for (int y = 0; y < gridBmp.Height; y++) { Boolean isWall = ((y % 2) != 0) && (rnd.Next(0, 10) != 8); if (isWall) { *pixels = unchecked ((int)0xFF000000); } else { *pixels = unchecked ((int)0xFFFFFFFF); } grid[x, y] = new MyPathNode() { IsWall = isWall, X = x, Y = y, }; pixels++; } } } // compute and display path MySolver <MyPathNode, Object> aStar = new MySolver <MyPathNode, Object>(grid); IEnumerable <MyPathNode> path = aStar.Search(new Point(0, 0), new Point(gridBmp.Width - 2, gridBmp.Height - 2), null); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); { aStar.Search(new Point(0, 0), new Point(gridBmp.Width - 2, gridBmp.Height - 2), null); } watch.Stop(); MessageBox.Show("Pathfinding took " + watch.ElapsedMilliseconds + "ms to complete."); foreach (MyPathNode node in path) { gridBmp.SetPixel(node.X, node.Y, Color.Red); } pictureBox1.Image = gridBmp; gridBmp.Save(".\\dump.png"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Unknown error...", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Create random fake sensors /// </summary> /// <param name="sensorsDataList">Sensor list</param> /// <returns>Fake sensors</returns> private List <Tuple <Sensor, Sensor, Sensor> > CreateRandomSensors4(IEnumerable <Sensor> sensorsDataList) { List <Tuple <Sensor, Sensor, Sensor> > fakeSensors = new List <Tuple <Sensor, Sensor, Sensor> >(); List <Tuple <int, int> > keyList = new List <Tuple <int, int> >(this.modelTrianglePoints.Keys); Random rand = new Random(); Sensor closestSensor = new Sensor(); Sensor sndClosestSensor = new Sensor(); for (int i = 0; i < 90; i++) { int min1 = int.MaxValue; int min2 = int.MaxValue; Tuple <int, int> randomKey = keyList[rand.Next(keyList.Count)]; Point rndSensorPnt = new Point(randomKey.Item1 - (this.offsetX / 10), randomKey.Item2 - (this.offsetY / 10)); foreach (Sensor sensor in sensorsDataList) { // Get the distance between randomKey and 7 sensors and return the minumum Point sensorPnt = new Point(Convert.ToInt32((sensor.X - this.offsetX) / 10), Convert.ToInt32((sensor.Y - this.offsetY) / 10)); int pathLen = 0; try { MySolver <MyPathNode, Object> astar = new MySolver <MyPathNode, Object>(this.grid3); IEnumerable <MyPathNode> path = astar.Search(rndSensorPnt, sensorPnt, null); pathLen = path.Count(); } catch (Exception) { throw new Exception("Unexpected astar algorithm error"); } if (pathLen < min1) { min2 = min1; min1 = pathLen; sndClosestSensor = closestSensor; closestSensor = sensor; } else if (pathLen < min2) { min2 = pathLen; sndClosestSensor = sensor; } } Sensor rstSensor = new Sensor("Fake Sensor", (rndSensorPnt.X * 10) + this.offsetX, (rndSensorPnt.Y * 10) + this.offsetY, 0); Tuple <int, int> key = new Tuple <int, int>(Convert.ToInt32(rstSensor.X), Convert.ToInt32(rstSensor.Y)); Tuple <Sensor, Sensor, Sensor> fk = new Tuple <Sensor, Sensor, Sensor>(rstSensor, closestSensor, sndClosestSensor); fakeSensors.Add(fk); keyList.Remove(randomKey); } return(fakeSensors); }
private static IEnumerable<MyPathNode> findPath() { grid[gridSize / 2, gridSize / 2].IsWall = false; MySolver<MyPathNode, Object> aStar = new MySolver<MyPathNode, Object>(grid); IEnumerable<MyPathNode> path = aStar.Search(new Point(0, 0), new Point(gridSize / 2, gridSize / 2), null); System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); { aStar.Search(new Point(0, 0), new Point(gridSize - 2, gridSize - 2), null); } watch.Stop(); Console.WriteLine("Pathfinding took " + watch.ElapsedMilliseconds + "ms to complete."); return path; }
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); }
private void calcPath_Click(object sender, RoutedEventArgs e) { var platums = 4; var augstums = 6; var grid = new Labirints[platums, augstums]; string ievads = inputTextFileContent.Text; ievads.Trim(); string ievads2 = ievads.Remove(10, 5); char[] koordinatas = ievads2.ToCharArray(5, ievads2.Length - 5); for (int i = 0; i < koordinatas.Length - 1; i++) { if (!Char.IsNumber(koordinatas[i])) { char temp = SwitchChar(koordinatas[i]); koordinatas[i] = temp; } } textBlockOutput.Text = new string(koordinatas); int[] sienas = new int[(koordinatas.Length - 6) / 3]; for (int i = 6; i < koordinatas.Length; i += 3) { sienas[(i - 6) / 3] = Convert.ToInt32(Char.GetNumericValue(koordinatas[i])) * 10 + Convert.ToInt32(Char.GetNumericValue(koordinatas[i + 1])); } Array.Sort(sienas); int sienuSkaititajs = 0; for (var x = 0; x < platums; x++) { for (var y = 0; y < augstums; y++) { bool siena = false; if (sienuSkaititajs < sienas.Length && x + 1 == Math.Floor((double)(sienas[sienuSkaititajs] / 10)) && (y + 1 == sienas[sienuSkaititajs] - Math.Floor((double)(sienas[sienuSkaititajs] / 10) * 10))) { siena = true; sienuSkaititajs++; } grid[x, y] = new Labirints { Siena = siena, Platums = x, Augstums = y }; } } var x1 = Char.GetNumericValue(koordinatas[0]) - 1; var y1 = Char.GetNumericValue(koordinatas[1]) - 1; var x2 = Char.GetNumericValue(koordinatas[3]) - 1; var y2 = Char.GetNumericValue(koordinatas[4]) - 1; var aStar = new MySolver <Labirints, object>(grid); var path = aStar.Search(new Point(x1, y1), new Point(x2, y2), null); aStar.Search(new Point(x1, y1), new Point(x2, y2), null); points = ""; if (path != null) { foreach (var node in path) { textBlockOutput.Text = textBlockOutput.Text + "\t(" + (node.Platums + 1) + " " + (node.Augstums + 1) + ")"; points += node.Platums + 1; points += node.Augstums + 1; } textBlockOutput.Text += " " + points; irCels = true; } else { textBlockOutput.Text = "Ceļa nav"; irCels = false; } TextBlock[] labLauki = { txt11, txt12, txt13, txt14, txt15, txt16, txt21, txt22, txt23, txt24, txt25, txt26, txt31, txt32, txt33, txt34, txt35, txt36, txt41, txt42, txt43, txt44, txt45, txt46 }; String[] laukuTeksts = { "A1", "A2", "A3", "A4", "A5", "A6", "B1", "B2", "B3", "B4", "B5", "B6", "C1", "C2", "C3", "C4", "C5", "C6", "D1", "D2", "D3", "D4", "D5", "D6" }; int skaits = 0; foreach (var lauks in labLauki) { lauks.Text = laukuTeksts[skaits]; lauks.Background = Brushes.White; skaits++; mapColour(x1 + 1, y1 + 1, x2 + 1, y2 + 1, sienas, points, lauks); } }
public IEnumerable <MyPathNode> GetPath(Objects.Location loc, List <Location> BlockingLocs = null) { lock (lockThis) { int maxX = 150; int maxY = 150; // IsWalking = true; StopWalk = false; if (!loc.IsInRange(maxX, maxY)) { client.StatusBar = "Destination is out of range"; return(null); } 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]; if (StopWalk) { return(null); } this.LoadMapfiles(); 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, }; } } foreach (Tile t in client.Map.GetTilesSameFloor()) { bool isWall = false; int cost = 0; int cx, cy; cx = t.Location.X - playerX + 300 / 2; cy = t.Location.Y - playerY + 300 / 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 && !WalkAbleIds.Contains(i.Id))) { 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().FirstOrDefault(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, }; } if (BlockingLocs != null) { foreach (Location l in BlockingLocs) { int cx, cy; cx = l.X - playerX + 300 / 2; cy = l.Y - playerY + 300 / 2; grid[cx, cy] = new MyPathNode() { IsWall = false, X = cx, Y = cy, Cost = 2000, }; } } grid[EndX, EndY] = new MyPathNode() { IsWall = false, X = EndX, Y = EndY, Cost = 0, }; 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); } }