Skip to content

Commit

Permalink
3d pathfinding (#1467)
Browse files Browse the repository at this point in the history
* 3d Pathfinding

* Work tiles above and below

* CompareTo to ==

* Who yah gonna call? Pathfinders!
  • Loading branch information
koosemose authored Sep 30, 2016
1 parent 19ee676 commit 18517fd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected override void OnChanged(Character character)

GameObject char_go = objectGameObjectMap[character];

char_go.transform.position = new Vector3(character.X, character.Y, 0);
char_go.transform.position = new Vector3(character.X, character.Y, character.Z);
}

protected override void OnRemoved(Character character)
Expand Down
22 changes: 17 additions & 5 deletions Assets/Scripts/Models/Buildable/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,30 @@ public bool IsNeighbour(Tile tile, bool diagOkay = false)
/// <param name="diagOkay">Is diagonal movement okay?.</param>
public Tile[] GetNeighbours(bool diagOkay = false)
{
Tile[] tiles = diagOkay == false ? new Tile[4] : new Tile[8];
Tile[] tiles = diagOkay == false ? new Tile[6] : new Tile[10];
tiles[0] = World.Current.GetTileAt(X, Y + 1, Z);
tiles[1] = World.Current.GetTileAt(X + 1, Y, Z);
tiles[2] = World.Current.GetTileAt(X, Y - 1, Z);
tiles[3] = World.Current.GetTileAt(X - 1, Y, Z);

// FIXME: This is a bit of a dirty hack, but it works for preventing characters from phasing through the floor for now.
Tile tileup = World.Current.GetTileAt(X, Y, Z - 1);
if (tileup != null && tileup.Type == TileType.Empty)
{
tiles[4] = World.Current.GetTileAt(X, Y, Z - 1);
}

if (Type == TileType.Empty)
{
tiles[5] = World.Current.GetTileAt(X, Y, Z + 1);
}

if (diagOkay == true)
{
tiles[4] = World.Current.GetTileAt(X + 1, Y + 1, Z);
tiles[5] = World.Current.GetTileAt(X + 1, Y - 1, Z);
tiles[6] = World.Current.GetTileAt(X - 1, Y - 1, Z);
tiles[7] = World.Current.GetTileAt(X - 1, Y + 1, Z);
tiles[6] = World.Current.GetTileAt(X + 1, Y + 1, Z);
tiles[7] = World.Current.GetTileAt(X + 1, Y - 1, Z);
tiles[8] = World.Current.GetTileAt(X - 1, Y - 1, Z);
tiles[9] = World.Current.GetTileAt(X - 1, Y + 1, Z);
}

return tiles.Where(tile => tile != null).ToArray();
Expand Down
13 changes: 1 addition & 12 deletions Assets/Scripts/Models/Job/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,7 @@ public Pathfinder.GoalEvaluator IsTileAtJobSite
}

// TODO: This doesn't handle multi-tile furniture
if (adjacent)
{
return otherTile => (
tile.Z == otherTile.Z &&
(tile.X - 1) <= otherTile.X && (tile.X + 1) >= otherTile.X &&
(tile.Y - 1) <= otherTile.Y && (tile.Y + 1) >= otherTile.Y &&
tile.IsClippingCorner(otherTile) == false);
}
else
{
return otherTile => this.tile.CompareTo(otherTile) == 0;
}
return Pathfinder.GoalTileEvaluator(tile, adjacent);
}
}

Expand Down
14 changes: 8 additions & 6 deletions Assets/Scripts/Pathfinding/Path_TileGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public Path_TileGraph(World world)
{
for (int y = 0; y < world.Height; y++)
{
Tile t = world.GetTileAt(x, y, 0);
for (int z = 0; z < world.Depth; z++)
{
Tile t = world.GetTileAt(x, y, z);

////if(t.movementCost > 0) { // Tiles with a move cost of 0 are unwalkable
Path_Node<Tile> n = new Path_Node<Tile>();
n.data = t;
nodes.Add(t, n);
////}
////if(t.movementCost > 0) { // Tiles with a move cost of 0 are unwalkable
Path_Node<Tile> n = new Path_Node<Tile>();
n.data = t;
nodes.Add(t, n);
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions Assets/Scripts/Pathfinding/Pathfinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static PathfindingHeuristic DefaultDistanceHeuristic(Tile goalTile)
/// </summary>
public static PathfindingHeuristic ManhattanDistance(Tile goalTile)
{
return tile => Mathf.Abs(tile.X - goalTile.X) + Mathf.Abs(tile.Y - goalTile.Y);
return tile => Mathf.Abs(tile.X - goalTile.X) + Mathf.Abs(tile.Y - goalTile.Y) + Mathf.Abs(tile.Z - goalTile.Z);
}

/// <summary>
Expand All @@ -177,15 +177,21 @@ public static GoalEvaluator GoalTileEvaluator(Tile goalTile, bool adjacent)
int maxX = goalTile.X + 1;
int minY = goalTile.Y - 1;
int maxY = goalTile.Y + 1;
int minZ = goalTile.Z - 1;
int maxZ = goalTile.Z + 1;

return tile => (
tile.X >= minX && tile.X <= maxX &&
(tile.X >= minX && tile.X <= maxX &&
tile.Y >= minY && tile.Y <= maxY &&
goalTile.IsClippingCorner(tile) == false);
tile.Z == goalTile.Z &&
goalTile.IsClippingCorner(tile) == false) ||
(tile.Z >= minZ && tile.Z <= maxZ &&
tile.X == goalTile.X &&
tile.Y == goalTile.Y));
}
else
{
return tile => goalTile == tile;
return tile => tile == goalTile;
}
}

Expand Down

0 comments on commit 18517fd

Please sign in to comment.