Skip to content

Commit

Permalink
Add tiled client side logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobFischer committed Apr 7, 2017
1 parent 5cc373f commit c7a9282
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 7 deletions.
66 changes: 66 additions & 0 deletions Games/Stumped/AI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,72 @@ public bool RunTurn()
// <<-- /Creer-Merge: runTurn -->>
}

/// <summary>
/// A very basic path finding algorithm (Breadth First Search) that when given a starting Tile, will return a valid path to the goal Tile.
/// </summary>
/// <remarks>
/// This is NOT an optimal pathfinding algorithm. It is intended as a stepping stone if you want to improve it.
/// </remarks>
/// <param name="start">the starting Tile</param>
/// <param name="goal">the goal Tile</param>
/// <returns>A List of Tiles representing the path, the the first element being a valid adjacent Tile to the start, and the last element being the goal. Or an empty list if no path found.</returns>
List<Tile> FindPath(Tile start, Tile goal)
{
// no need to make a path to here...
if (start == goal)
{
return new List<Tile>();
}

// the tiles that will have their neighbors searched for 'goal'
Queue<Tile> fringe = new Queue<Tile>();

// How we got to each tile that went into the fringe.
Dictionary<Tile, Tile> cameFrom = new Dictionary<Tile, Tile>();

// Enqueue start as the first tile to have its neighbors searched.
fringe.Enqueue(start);

// keep exploring neighbors of neighbors... until there are no more.
while (fringe.Count > 0)
{
// the tile we are currently exploring.
Tile inspect = fringe.Dequeue();

// cycle through the tile's neighbors.
foreach (Tile neighbor in inspect.GetNeighbors())
{
if (neighbor == goal)
{
// Follow the path backward starting at the goal and return it.
List<Tile> path = new List<Tile>();
path.Add(goal);

// Starting at the tile we are currently at, insert them retracing our steps till we get to the starting tile
for (Tile step = inspect; step != start; step = cameFrom[step])
{
path.Insert(0, step);
}

return path;
}

// if the tile exists, has not been explored or added to the fringe yet, and it is pathable
if (neighbor != null && !cameFrom.ContainsKey(neighbor) && neighbor.IsPathable())
{
// add it to the tiles to be explored and add where it came from.
fringe.Enqueue(neighbor);
cameFrom.Add(neighbor, inspect);
}

} // foreach(neighbor)

} // while(fringe not empty)

// if you're here, that means that there was not a path to get to where you want to go.
// in that case, we'll just return an empty path.
return new List<Tile>();
}

// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional methods here for your AI to call
Expand Down
5 changes: 3 additions & 2 deletions Games/Stumped/Beaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public bool BuildLodge()
/// </summary>
/// <param name="tile">The Tile to drop branches/food on. Must be the same Tile that the Beaver is on, or an adjacent one.</param>
/// <param name="resource">The type of resource to drop ('branch' or 'food').</param>
/// <param name="amount">The amount of the resource to drop, numbers <= 0 will drop all the resource type.</param>
/// <param name="amount">The amount of the resource to drop, numbers &lt;= 0 will drop all the resource type.</param>
/// <returns>True if successfully dropped the resource, false otherwise.</returns>
public bool Drop(Stumped.Tile tile, string resource, int amount=0)
{
Expand Down Expand Up @@ -152,7 +152,7 @@ public bool Move(Stumped.Tile tile)
/// </summary>
/// <param name="tile">The Tile to pickup branches/food from. Must be the same Tile that the Beaver is on, or an adjacent one.</param>
/// <param name="resource">The type of resource to pickup ('branch' or 'food').</param>
/// <param name="amount">The amount of the resource to drop, numbers <= 0 will pickup all of the resource type.</param>
/// <param name="amount">The amount of the resource to drop, numbers &lt;= 0 will pickup all of the resource type.</param>
/// <returns>True if successfully picked up a resource, false otherwise.</returns>
public bool Pickup(Stumped.Tile tile, string resource, int amount=0)
{
Expand All @@ -164,6 +164,7 @@ public bool Pickup(Stumped.Tile tile, string resource, int amount=0)
}



// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional method(s) here.
// <<-- /Creer-Merge: methods -->>
Expand Down
17 changes: 17 additions & 0 deletions Games/Stumped/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ protected Game() : base()
}


/// <summary>
/// Gets the Tile at a specified (x, y) position
/// </summary>
/// <param name="x">integer between 0 and the MapWidth</param>
/// <param name="y">integer between 0 and the MapHeight</param>
/// <returns>the Tile at (x, y) or null if out of bounds</returns>
public Tile GetTileAt(int x, int y)
{
if (x < 0 || y < 0 || x >= this.MapWidth || y >= this.MapHeight)
{
// out of bounds
return null;
}

return this.Tiles[x + y * this.MapWidth];
}

// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional method(s) here.
// <<-- /Creer-Merge: methods -->>
Expand Down
1 change: 1 addition & 0 deletions Games/Stumped/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void Log(string message)
}



// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional method(s) here.
// <<-- /Creer-Merge: methods -->>
Expand Down
1 change: 1 addition & 0 deletions Games/Stumped/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public Stumped.Beaver Recruit(Stumped.Tile tile)
}



// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional method(s) here.
// <<-- /Creer-Merge: methods -->>
Expand Down
1 change: 1 addition & 0 deletions Games/Stumped/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected Player() : base()
}



// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional method(s) here.
// <<-- /Creer-Merge: methods -->>
Expand Down
1 change: 1 addition & 0 deletions Games/Stumped/Spawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected Spawner() : base()
}



// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional method(s) here.
// <<-- /Creer-Merge: methods -->>
Expand Down
57 changes: 57 additions & 0 deletions Games/Stumped/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,63 @@ protected Tile() : base()
}


/// <summary>
/// Gets the neighbors of this Tile
/// </summary>
/// <returns>The neighboring (adjacent) Tiles to this tile</returns>
public List<Tile> GetNeighbors()
{
var list = new List<Tile>();

if (this.TileNorth != null)
{
list.Add(this.TileNorth);
}

if (this.TileEast != null)
{
list.Add(this.TileEast);
}

if (this.TileSouth != null)
{
list.Add(this.TileSouth);
}

if (this.TileWest != null)
{
list.Add(this.TileWest);
}

return list;
}

/// <summary>
/// Checks if a Tile is pathable to units
/// </summary>
/// <returns>True if pathable, false otherwise</returns>
public bool IsPathable()
{
// <<-- Creer-Merge: is_pathable_builtin -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
return false; // DEVELOPER ADD LOGIC HERE
// <<-- /Creer-Merge: is_pathable_builtin -->>
}

/// <summary>
/// Checks if this Tile has a specific neighboring Tile
/// </summary>
/// <param name="tile">Tile to check against</param>
/// <returns>true if the tile is a neighbor of this Tile, false otherwise</returns>
public bool HasNeighbor(Tile tile)
{
if (tile == null)
{
return false;
}

return (this.TileNorth == tile || this.TileEast == tile || this.TileSouth == tile || this.TileEast == tile);
}

// <<-- Creer-Merge: methods -->> - Code you add between this comment and the end comment will be preserved between Creer re-runs.
// you can add additional method(s) here.
// <<-- /Creer-Merge: methods -->>
Expand Down
88 changes: 83 additions & 5 deletions _creer/Games/${game_name}/${obj_key}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace Joueur.cs.Games.${game_name}
{
/// <summary>
/// ${obj['description']}
/// ${shared['c#']['escape'](obj['description'])}
/// </summary>
class ${obj_key} : ${inherit_str}
{
Expand All @@ -36,7 +36,7 @@ class ${obj_key} : ${inherit_str}
if (obj_key == "Game" and (attr_name == "gameObjects" or attr_name == "name")) or attr_name == "id":
continue
%> /// <summary>
/// ${attr_parms['description']}
/// ${shared['c#']['escape'](attr_parms['description'])}
/// </summary>
public ${shared['c#']['type'](attr_parms['type'])} ${upcase_first(attr_name)} { get; protected set; }

Expand Down Expand Up @@ -70,16 +70,16 @@ class ${obj_key} : ${inherit_str}
arg_strings = []
return_type = None
%> /// <summary>
/// ${function_parms['description']}
/// ${shared['c#']['escape'](function_parms['description'])}
/// </summary>
% if 'arguments' in function_parms:
% for arg_parms in function_parms['arguments']:
/// <param name="${arg_parms['name']}">${arg_parms['description']}</param>
/// <param name="${arg_parms['name']}">${shared['c#']['escape'](arg_parms['description'])}</param>
% endfor
% endif
% if function_parms['returns']:
<% return_type = shared['c#']['type'](function_parms['returns']['type'])
%> /// <returns>${function_parms['returns']['description']}</returns>
%> /// <returns>${shared['c#']['escape'](function_parms['returns']['description'])}</returns>
% endif
public ${return_type or 'void'} ${upcase_first(function_name)}(${shared['c#']['args'](function_parms['arguments'])})
{
Expand All @@ -92,6 +92,84 @@ class ${obj_key} : ${inherit_str}

% endfor

% if 'Tile' in game_objs:
% if 'TiledGame' in game['serverParentClasses']: #// then we need to add some client side utility functions
% if obj_key == 'Game':
/// <summary>
/// Gets the Tile at a specified (x, y) position
/// </summary>
/// <param name="x">integer between 0 and the MapWidth</param>
/// <param name="y">integer between 0 and the MapHeight</param>
/// <returns>the Tile at (x, y) or null if out of bounds</returns>
public Tile GetTileAt(int x, int y)
{
if (x < 0 || y < 0 || x >= this.MapWidth || y >= this.MapHeight)
{
// out of bounds
return null;
}

return this.Tiles[x + y * this.MapWidth];
}
% elif obj_key == 'Tile':
/// <summary>
/// Gets the neighbors of this Tile
/// </summary>
/// <returns>The neighboring (adjacent) Tiles to this tile</returns>
public List<Tile> GetNeighbors()
{
var list = new List<Tile>();

if (this.TileNorth != null)
{
list.Add(this.TileNorth);
}

if (this.TileEast != null)
{
list.Add(this.TileEast);
}

if (this.TileSouth != null)
{
list.Add(this.TileSouth);
}

if (this.TileWest != null)
{
list.Add(this.TileWest);
}

return list;
}

/// <summary>
/// Checks if a Tile is pathable to units
/// </summary>
/// <returns>True if pathable, false otherwise</returns>
public bool IsPathable()
{
${merge(" // ", "is_pathable_builtin", " return false; // DEVELOPER ADD LOGIC HERE")}
}

/// <summary>
/// Checks if this Tile has a specific neighboring Tile
/// </summary>
/// <param name="tile">Tile to check against</param>
/// <returns>true if the tile is a neighbor of this Tile, false otherwise</returns>
public bool HasNeighbor(Tile tile)
{
if (tile == null)
{
return false;
}

return (this.TileNorth == tile || this.TileEast == tile || this.TileSouth == tile || this.TileEast == tile);
}
% endif
% endif

% endif
${merge(" // ", "methods", " // you can add additional method(s) here.", optional=True)}
#endregion
}
Expand Down
Loading

0 comments on commit c7a9282

Please sign in to comment.