public bool Walk(ref int x, ref int y, bool recalculate_when_needed) { if (path.Count == 0) { return(false); } PathFindingDirection d = path[path.Count - 1]; path.RemoveAt(path.Count - 1); int newx = ox + dirx[(int)d]; int newy = oy + diry[(int)d]; /* check if the path is still valid */ if (!level.IsWalkable(newx, newy)) { if (!recalculate_when_needed) { return(false); /* don't walk */ } /* calculate a new path */ if (!Compute(ox, oy, dx, dy)) { return(false); /* cannot find a new path */ } return(Walk(ref x, ref y, true)); /* walk along the new path */ } x = newx; y = newy; ox = newx; oy = newy; return(true); }
public bool Compute(int ox, int oy, int dx, int dy) { this.ox = ox; this.oy = oy; this.dx = dx; this.dy = dy; path.Clear(); heap.Clear(); if (ox == dx && oy == dy) { return(true); /* trivial case */ } /* check that origin and destination are inside the map */ if (!((uint)ox < (uint)w && (uint)oy < (uint)h)) { return(false); } if (!((uint)dx < (uint)w && (uint)dy < (uint)h)) { return(false); } Array.Clear(grid, 0, w * h); for (int i = 0; i < w * h; i++) { prev[i] = PathFindingDirection.NONE; } heur[ox + oy * w] = 1.0f; TCOD_path_push_cell(ox, oy); /* put the origin cell as a bootstrap */ /* fill the djikstra grid until we reach dx,dy */ TCOD_path_set_cells(); if (grid[dx + dy * w] == 0) { return(false); /* no path found */ } /* there is a path. retrieve it */ do { /* walk from destination to origin, using the 'prev' array */ PathFindingDirection step = prev[dx + dy * w]; path.Add(step); dx -= dirx[(int)step]; dy -= diry[(int)step]; } while (dx != ox || dy != oy); return(true); }
public void GetPathElement(int index, out int x, out int y) { int pos; x = ox; y = oy; pos = path.Count - 1; do { Contract.Assert(index >= -1); Contract.Assert(pos >= 0); PathFindingDirection step = path[pos]; x += dirx[(int)step]; y += diry[(int)step]; pos--; index--; } while (index >= 0); }
public void Reverse() { int tmp, i; tmp = ox; ox = dx; dx = tmp; tmp = oy; oy = dy; dy = tmp; for (i = 0; i < path.Count; i++) { PathFindingDirection d = path[i]; d = invdir[(int)d]; path[i] = d; } }