From 257aaa331f3bfa4d7ef1e043f8afb6769b3d51c9 Mon Sep 17 00:00:00 2001 From: Brady Bodily Date: Tue, 15 Dec 2020 00:09:29 -0700 Subject: [PATCH] Done for the night Need to change planning algo for square to go right to left. --- .../.idea/contentModel.xml | 6 +- .../.idea.RobotIntelFinal/.idea/workspace.xml | 81 +++++++++---------- ConsoleApp/ConsoleApp.csproj | 1 + ConsoleApp/CoreModule.cs | 1 + ConsoleApp/Maps/Cell.cs | 12 +-- ConsoleApp/Maps/GlobalDirection.cs | 6 +- ConsoleApp/Maps/HexMap.cs | 53 ++++++++++-- ConsoleApp/Maps/ICell.cs | 1 - ConsoleApp/Maps/IMap.cs | 3 + ConsoleApp/Maps/SquareMap.cs | 6 +- ConsoleApp/{ => PathPlanners}/IPathPlanner.cs | 3 +- ConsoleApp/{ => PathPlanners}/PathPlanner.cs | 7 +- ConsoleApp/SimRunner.cs | 6 +- 13 files changed, 119 insertions(+), 67 deletions(-) rename ConsoleApp/{ => PathPlanners}/IPathPlanner.cs (63%) rename ConsoleApp/{ => PathPlanners}/PathPlanner.cs (91%) diff --git a/.idea/.idea.RobotIntelFinal/.idea/contentModel.xml b/.idea/.idea.RobotIntelFinal/.idea/contentModel.xml index 95e7f7b..e2c567b 100644 --- a/.idea/.idea.RobotIntelFinal/.idea/contentModel.xml +++ b/.idea/.idea.RobotIntelFinal/.idea/contentModel.xml @@ -9,7 +9,6 @@ - @@ -34,7 +33,10 @@ - + + + + diff --git a/.idea/.idea.RobotIntelFinal/.idea/workspace.xml b/.idea/.idea.RobotIntelFinal/.idea/workspace.xml index 2817021..ba7d1e4 100644 --- a/.idea/.idea.RobotIntelFinal/.idea/workspace.xml +++ b/.idea/.idea.RobotIntelFinal/.idea/workspace.xml @@ -18,17 +18,18 @@ - - - + - - + + + + + + + - + + + @@ -147,7 +157,7 @@ - + @@ -175,44 +185,31 @@ - file://$PROJECT_DIR$/ConsoleApp/SimRunner.cs - 49 - - - - - - - - - file://$PROJECT_DIR$/ConsoleApp/SimRunner.cs - 47 - - - - - - - - - file://$PROJECT_DIR$/ConsoleApp/PathPlanner.cs + file://$PROJECT_DIR$/ConsoleApp/PathPlanners/PathPlanner.cs 34 - - + + file://$PROJECT_DIR$/ConsoleApp/Maps/HexMap.cs + 62 + + + + + + + diff --git a/ConsoleApp/ConsoleApp.csproj b/ConsoleApp/ConsoleApp.csproj index cb79c52..0715900 100644 --- a/ConsoleApp/ConsoleApp.csproj +++ b/ConsoleApp/ConsoleApp.csproj @@ -15,6 +15,7 @@ + diff --git a/ConsoleApp/CoreModule.cs b/ConsoleApp/CoreModule.cs index 0b9500a..53e0122 100644 --- a/ConsoleApp/CoreModule.cs +++ b/ConsoleApp/CoreModule.cs @@ -1,6 +1,7 @@ using System.Text.Json; using System.Threading; using ConsoleApp.Maps; +using ConsoleApp.PathPlanners; using DryIoc; namespace ConsoleApp diff --git a/ConsoleApp/Maps/Cell.cs b/ConsoleApp/Maps/Cell.cs index 1665631..a5b5f14 100644 --- a/ConsoleApp/Maps/Cell.cs +++ b/ConsoleApp/Maps/Cell.cs @@ -6,18 +6,14 @@ namespace ConsoleApp.Maps { public int X { get; } public int Y { get; } - public int Z { get; } - public Cell(int x, int y, int z = default) + + public Cell(int x, int y) { - if (z != default) - { - if (x + y + z != 0) throw new ArgumentException("x + y + z must be 0"); - } - X = x; Y = y; - Z = z; } + + } } \ No newline at end of file diff --git a/ConsoleApp/Maps/GlobalDirection.cs b/ConsoleApp/Maps/GlobalDirection.cs index 8bff109..675471f 100644 --- a/ConsoleApp/Maps/GlobalDirection.cs +++ b/ConsoleApp/Maps/GlobalDirection.cs @@ -5,6 +5,10 @@ namespace ConsoleApp.Maps North, South, East, - West + West, + NorthEast, + SouthEast, + NorthWest, + SouthWest } } \ No newline at end of file diff --git a/ConsoleApp/Maps/HexMap.cs b/ConsoleApp/Maps/HexMap.cs index 3ecba18..625c3d9 100644 --- a/ConsoleApp/Maps/HexMap.cs +++ b/ConsoleApp/Maps/HexMap.cs @@ -1,13 +1,17 @@ using System; using System.Collections.Generic; +using HexCore; namespace ConsoleApp.Maps { public class HexMap : IHexMap { + private int _mapHeight; + private int _mapWidth; public Cell[,] Map { get; } public Cell StartingCell { get; } public Cell LastCell { get; } + public Graph HexGraph { get; } /// /// Generate Hex map with cells of 25cm X 25cm @@ -19,27 +23,60 @@ namespace ConsoleApp.Maps //convert to cm x *= 100; y *= 100; + //calculate number of cells on x and y axis var xCellCount = (int)Math.Ceiling((decimal) (x) / 25); var yCellCount = (int)Math.Ceiling((decimal) (y) / 25); + + _mapHeight = yCellCount; + _mapWidth = xCellCount; //Initialize Map Map = new Cell[xCellCount, yCellCount]; - - //set last cell; + //set Starting cell; StartingCell = Map[0, 0]; - for (int r = 0; r < yCellCount; r++) { - int r_offset = Convert.ToInt32(Math.Floor(Convert.ToDouble(r)/2)); - for (int q = r_offset; q < xCellCount - r_offset; q++) { - // Console.WriteLine($"r:{r}, q:{q}-----x:{x}, y:{y}"); - Map[r, q] = new Cell(q, r, -q-r); + + var unclearedTerrain = new TerrainType(1, "uncleared"); + var clearedTerrain = new TerrainType(2, "cleared"); + //Populate Map + var list = new List(); + for (int i = 0; i < _mapHeight; i++) + { + for (int j = 0; j < _mapWidth; j++) + { + list.Add(new CellState(false, new Coordinate2D(i, j, OffsetTypes.OddRowsRight), unclearedTerrain)); } } + var movement = new MovementType(1, "default"); + var movementTypes = new MovementTypes( + new ITerrainType[] { unclearedTerrain, clearedTerrain }, + new Dictionary> + { + [movement] = new Dictionary + { + [unclearedTerrain] = 1, + [clearedTerrain] = 2 + } + } + ); + HexGraph = GraphFactory.CreateRectangularGraph(_mapWidth, _mapHeight, movementTypes, unclearedTerrain); } public List PossibleMoves(ICell currentCell) { - throw new NotImplementedException(); + var x = currentCell.X; + var y = currentCell.Y; + var possibles = new List(); + + if(currentCell.Y != _mapHeight) + possibles.Add(GlobalDirection.North); + + if(currentCell.Y != 0) + possibles.Add(GlobalDirection.South); + + if(currentCell.X != _mapWidth && currentCell.Y != _mapHeight) + possibles.Add(GlobalDirection.NorthEast); + return possibles; } } } \ No newline at end of file diff --git a/ConsoleApp/Maps/ICell.cs b/ConsoleApp/Maps/ICell.cs index 034be3f..4faabb8 100644 --- a/ConsoleApp/Maps/ICell.cs +++ b/ConsoleApp/Maps/ICell.cs @@ -4,6 +4,5 @@ namespace ConsoleApp.Maps { int X { get; } int Y { get; } - int Z { get; } } } \ No newline at end of file diff --git a/ConsoleApp/Maps/IMap.cs b/ConsoleApp/Maps/IMap.cs index a00f3fb..5be44cf 100644 --- a/ConsoleApp/Maps/IMap.cs +++ b/ConsoleApp/Maps/IMap.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.ComponentModel; +using HexCore; namespace ConsoleApp.Maps { @@ -9,5 +10,7 @@ namespace ConsoleApp.Maps Cell StartingCell { get; } Cell LastCell { get; } public List PossibleMoves(ICell currentCell); + + Graph HexGraph { get; } } } \ No newline at end of file diff --git a/ConsoleApp/Maps/SquareMap.cs b/ConsoleApp/Maps/SquareMap.cs index 4a6f158..9a3174b 100644 --- a/ConsoleApp/Maps/SquareMap.cs +++ b/ConsoleApp/Maps/SquareMap.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using HexCore; namespace ConsoleApp.Maps { @@ -16,6 +17,7 @@ namespace ConsoleApp.Maps private int _mapHeight; public SquareMap(int x, int y) { + HexGraph = default; //convert to cm x *= 100; y *= 100; @@ -23,7 +25,7 @@ namespace ConsoleApp.Maps var xCellCount = (int)Math.Ceiling((decimal) (x) / 25); var yCellCount = (int)Math.Ceiling((decimal) (y) / 25); - //set Width and height fields + //set Width and height fields and Properties _mapWidth = xCellCount-1; _mapHeight = yCellCount-1; Height = _mapHeight; @@ -60,6 +62,8 @@ namespace ConsoleApp.Maps return possibles; } + public Graph HexGraph { get; } + public Cell this[in int x, in int y] => Map[x, y]; } } \ No newline at end of file diff --git a/ConsoleApp/IPathPlanner.cs b/ConsoleApp/PathPlanners/IPathPlanner.cs similarity index 63% rename from ConsoleApp/IPathPlanner.cs rename to ConsoleApp/PathPlanners/IPathPlanner.cs index c71d52d..16688bf 100644 --- a/ConsoleApp/IPathPlanner.cs +++ b/ConsoleApp/PathPlanners/IPathPlanner.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; using ConsoleApp.Maps; -namespace ConsoleApp +namespace ConsoleApp.PathPlanners { public interface IPathPlanner { Queue GenerateOptimalSquarePath(SquareMap map, IVehicle vehicle); + Queue GenerateOptimalHexPath(HexMap hexMap, IVehicle vehicle); } } \ No newline at end of file diff --git a/ConsoleApp/PathPlanner.cs b/ConsoleApp/PathPlanners/PathPlanner.cs similarity index 91% rename from ConsoleApp/PathPlanner.cs rename to ConsoleApp/PathPlanners/PathPlanner.cs index 2a75207..5be9589 100644 --- a/ConsoleApp/PathPlanner.cs +++ b/ConsoleApp/PathPlanners/PathPlanner.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using ConsoleApp.Maps; -namespace ConsoleApp +namespace ConsoleApp.PathPlanners { public class PathPlanner : IPathPlanner { @@ -52,5 +52,10 @@ namespace ConsoleApp return path; } + + public Queue GenerateOptimalHexPath(HexMap hexMap, IVehicle vehicle) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/ConsoleApp/SimRunner.cs b/ConsoleApp/SimRunner.cs index 5c55a52..ac695bc 100644 --- a/ConsoleApp/SimRunner.cs +++ b/ConsoleApp/SimRunner.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using ConsoleApp.Maps; +using ConsoleApp.PathPlanners; namespace ConsoleApp { @@ -27,8 +28,8 @@ namespace ConsoleApp public void Run() { - SquareSimulation(); - // var hexTask = Task.Run(() => HexSimulation()); + //SquareSimulation(); + HexSimulation(); // while(!squareTask.IsCompleted && !hexTask.IsCompleted){Thread.Sleep(500);} } @@ -37,6 +38,7 @@ namespace ConsoleApp { var hexMap = (HexMap)_mapFactory.Maps["HexMap"]; _vehicle.CurrentHexCell = hexMap.StartingCell; + var optimalPath = _pathPlanner.GenerateOptimalHexPath(hexMap, _vehicle); }