Exemple #1
0
        public static bool AreConnected(this IMapCell cell, IMapCell other)
        {
            var fringe = new Queue <IMapCell>();

            fringe.Enqueue(cell);
            var tabu = new List <IMapCell>();

            while (fringe.Count > 0)
            {
                var check = fringe.Dequeue();
                if (check == other)
                {
                    return(true);
                }
                tabu.Add(check);
                foreach (var connectedCell in check.GetConnectedCells())
                {
                    if (!tabu.Contains(connectedCell))
                    {
                        fringe.Enqueue(connectedCell);
                    }
                }
            }
            return(false);
        }
Exemple #2
0
        public static bool CheckSymmetry(this IMapCell map)
        {
            var toCheck = new Queue <IMapCell>();
            var check   = new HashSet <IMapCell>();

            toCheck.Enqueue(map);
            while (toCheck.Count > 0)
            {
                var checking = toCheck.Dequeue();
                if (!CheckSymmetrySingle(checking))
                {
                    return(false);
                }
                var connected = checking.GetConnectedDirections();
                foreach (var direction in connected)
                {
                    var next = checking.GetCellInDirection(direction);
                    if (!check.Contains(next))
                    {
                        toCheck.Enqueue(next);
                    }
                }
                check.Add(checking);
            }
            return(true);
        }
Exemple #3
0
 private void RunPostGenHooks(IMapCell map)
 {
     foreach (var mapGeneratedHook in _genHooks)
     {
         mapGeneratedHook.MapGenerated(map);
     }
 }
Exemple #4
0
        private bool CheckCellRekursion(IMapCell cell, ICollection <IMapCell> visitedCells, ref bool pacificConnected, ref bool atlanticConnectected)
        {
            visitedCells.Add(cell);

            IEnumerable <IMapCell> possibleNeighbours = GetPossibleNeighbours(cell, visitedCells);

            if (possibleNeighbours.Contains(_map.GetCell(0, 0)))
            {
                pacificConnected = true;
            }
            if (possibleNeighbours.Contains(_map.GetCell(_map.SizeY - 1, _map.SizeX - 1)))
            {
                atlanticConnectected = true;
            }
            if (pacificConnected && atlanticConnectected)
            {
                return(true);
            }

            //There is more likely that higher neigbourhood will not be a dead end
            foreach (IMapCell pn in possibleNeighbours.OrderByDescending(n => n.Heigh))
            {
                if (CheckCellRekursion(pn, visitedCells, ref pacificConnected, ref atlanticConnectected))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #5
0
        public void CreateMap(IMap map, bool createEmptyCells)
        {
            m_Root  = new GameObject("map_root");
            m_Cells = new GameObject[map.GetWidth()][];

            for (int x = 0; x < map.GetWidth(); ++x)
            {
                m_Cells[x] = new GameObject[map.GetHeight()];

                for (int y = 0; y < map.GetHeight(); ++y)
                {
                    IMapCell mapCell = map.GetCell(x, y);

                    if (!createEmptyCells && mapCell.GetCellType().Equals(0))
                    {
                        continue;
                    }

                    byte cellPrefabIndex = mapCell.GetCellType();
                    byte surfaceType     = mapCell.GetSurfaceType();

                    CreateCellAt(x, y, cellPrefabIndex, surfaceType);
                }
            }

            ICheckPointsManager checkPointsManager = map.GetCheckPointsManager();

            foreach (ICheckPoint checkPoint in checkPointsManager.GetCheckPoints())
            {
                CreateCheckPoint(checkPoint);
            }
        }
Exemple #6
0
        protected override IResult CheckCell(IMapCell cell)
        {
            ICollection <IMapCell> visitedCells = new HashSet <IMapCell>();
            bool pacificConnected     = false;
            bool atlanticConnectected = false;
            bool success = CheckCellRekursion(cell, visitedCells, ref pacificConnected, ref atlanticConnectected);

            IResult result = new Result(cell, visitedCells.Count(), success);

            return(result);
        }
Exemple #7
0
        public static ActionResult FindWumpus(this IMapCell currentCell)
        {
            var fringe = new Queue <Tuple <IMapCell, Queue <Direction> > >();
            var check  = new List <IMapCell>();

            fringe.Enqueue(new Tuple <IMapCell, Queue <Direction> >(currentCell, new Queue <Direction>()));
            while (fringe.Count > 0)
            {
                var current = fringe.Dequeue();
                if (check.Contains(current.Item1))
                {
                    continue;
                }
                if (current.Item1 is WumpusMapCell)
                {
                    var result = new StringBuilder();
                    var q      = current.Item2;
                    result.AppendLine("Wumpus found");
                    while (q.Count > 0)
                    {
                        result.AppendLine(String.Format("{0}", q.Dequeue()));
                    }
                    return(new ActionResult()
                    {
                        GameOver = false,
                        Special = false,
                        Message = result.ToString(),
                        MoveSuccessful = true,
                        ScoreValue = 0
                    });
                }
                check.Add(current.Item1);
                var actionResult = current.Item1.MoveTo();
                if (current.Item1 is WallMapCell || actionResult.GameOver || actionResult.Special)
                {
                    continue;
                }
                foreach (var direction in current.Item1.GetConnectedDirections())
                {
                    var queue = new Queue <Direction>(current.Item2);
                    queue.Enqueue(direction);
                    var cellInDirection = current.Item1.GetCellInDirection(direction);
                    fringe.Enqueue(new Tuple <IMapCell, Queue <Direction> >(cellInDirection, queue));
                }
            }
            return(new ActionResult()
            {
                GameOver = false,
                Message = "Could not find Wumpus",
                MoveSuccessful = false,
                ScoreValue = 0,
                Special = false
            });
        }
Exemple #8
0
        private bool Validate(IMapCell generateMap)
        {
            if (generateMap == null)
            {
                return(false);
            }
            var wumpus   = generateMap.FindWumpus().MoveSuccessful;
            var symmetry = generateMap.CheckSymmetry();

            return(wumpus && symmetry);
        }
Exemple #9
0
        private static bool CheckSymmetrySingle(IMapCell cell)
        {
            bool ok            = true;
            var  connectedDirs = cell.GetConnectedDirections();

            foreach (var dir in connectedDirs)
            {
                var oppDir = dir.OppositeDirection();
                ok = ok && cell.GetCellInDirection(dir).GetCellInDirection(oppDir) == cell;
            }
            return(ok);
        }
        public CoordinateCell(IMapCell <C> cell)
        {
            this.Cell = cell;

            int _loc4 = cell.().dimensions().width();
            int _loc5 = cell.Id() / (_loc4 * 2 - 1);
            int _loc6 = cell.Id() - _loc5 * (_loc4 * 2 - 1);
            int _loc7 = _loc6 % _loc4;

            this.y = _loc5 - _loc7;
            this.x = (cell.Id() - (_loc4 - 1) * this.y) / _loc4;
        }
Exemple #11
0
        public void UpdateMapCellValue(int x, int y, byte type, byte surfaceType)
        {
            IMapCell cell = m_Map.GetCell(x, y);

            cell.SetCellType(type);
            cell.SetSurfaceType(surfaceType);

            LoaderOfMap.DestroyCellAt(x, y);
            GameObject newCellGo = LoaderOfMap.CreateCellAt(x, y, cell.GetCellType(), surfaceType);

            AssignCellParam(newCellGo, cell, x, y);
        }
Exemple #12
0
        private void AssignCellParams()
        {
            for (int x = 0; x < m_Map.GetWidth(); ++x)
            {
                for (int y = 0; y < m_Map.GetHeight(); ++y)
                {
                    GameObject cellObj = LoaderOfMap.GetCellAt(x, y);
                    IMapCell   mapCell = m_Map.GetCell(x, y);

                    AssignCellParam(cellObj, mapCell, x, y);
                }
            }
        }
Exemple #13
0
        private void AssignCellParam(GameObject go, IMapCell cell, int x, int y)
        {
            ClearOtherColliders(go);

            MapCellParam cellParam = go.AddComponent <MapCellParam>();
            BoxCollider  col       = go.AddComponent <BoxCollider>();

            col.size = new Vector3(2, 1, 2);

            cellParam.CellType = (MapCellTypes)cell.GetCellType();
            cellParam.X        = x;
            cellParam.Y        = y;
        }
Exemple #14
0
        /// <summary>
        /// Put walls around the map
        /// </summary>
        /// <param name="map"></param>
        public static void WallMap(this IMapCell map)
        {
            var stack = new Stack <IMapCell>();

            stack.Push(map);
            var processed = new HashSet <IMapCell>();

            while (stack.Count > 0)
            {
                var cell = stack.Pop();
                if (processed.Contains(cell))
                {
                    continue;
                }

                if (cell.North != null)
                {
                    stack.Push(cell.North);
                }
                else
                {
                    cell.North = new WallMapCell();
                }
                if (cell.East != null)
                {
                    stack.Push(cell.East);
                }
                else
                {
                    cell.East = new WallMapCell();
                }
                if (cell.South != null)
                {
                    stack.Push(cell.South);
                }
                else
                {
                    cell.South = new WallMapCell();
                }
                if (cell.West != null)
                {
                    stack.Push(cell.West);
                }
                else
                {
                    cell.West = new WallMapCell();
                }

                processed.Add(cell);
            }
        }
 public void MapGenerated(IMapCell map)
 {
     var allCells = map.GetAllCells().Where(x => x is IItemHolder)
         .OrderBy(x => Guid.NewGuid())
         .Cast<IItemHolder>();
     var goldLeft = GoldAvailable;
     foreach (var cell in allCells)
     {
         if(goldLeft <= 0)
             break;
         cell.AddItem(new GoldItem());
         goldLeft--;
     }
 }
Exemple #16
0
 public static IMapCell GetCellInDirection(this IMapCell cell, Direction direction)
 {
     if (direction == Direction.North)
     {
         return(cell.North);
     }
     if (direction == Direction.East)
     {
         return(cell.East);
     }
     if (direction == Direction.South)
     {
         return(cell.South);
     }
     return(cell.West);
 }
Exemple #17
0
        private IMapCell _GenerateMap(int mapSize)
        {
            var cellList = new List <IMapCell>();

            for (var i = 0; i < mapSize; i++)
            {
                cellList.Add(GetNextCell());
            }
            cellList.Add(new WumpusMapCell());
            foreach (var mapCell in cellList)
            {
                var connected  = new List <Direction>(mapCell.GetConnectedDirections());
                var toGenerate = new List <Direction>();
                while (connected.Count < 3)
                {
                    var d = RandomDirection(connected);
                    connected.Add(d);
                    toGenerate.Add(d);
                }
                foreach (var direction in toGenerate)
                {
                    IMapCell cell             = mapCell;
                    var      others           = cellList.Where(x => x != cell).OrderBy(x => Guid.NewGuid()).ToList();
                    var      alreadyConnected = mapCell.GetConnectedCells().OrderBy(x => Guid.NewGuid());
                    //Try and connect everything first
                    var next = others.FirstOrDefault(x => !cell.AreConnected(x) &&
                                                     !x.GetConnectedDirections().Contains(direction.OppositeDirection()));
                    if (next == null)
                    {
                        next = others
                               .FirstOrDefault(x => x.GetConnectedDirections().Count() < 3 &&
                                               !alreadyConnected.Contains(x) &&
                                               !x.GetConnectedDirections().Contains(direction.OppositeDirection()));
                    }

                    if (next == null)
                    {
                        break;
                    }
                    mapCell.SetMapCell(direction, next);
                }
            }
            cellList.First().WallMap();
            var generateMap = cellList.FirstOrDefault(x => !x.MoveTo().GameOver);

            return(generateMap);
        }
Exemple #18
0
        public void MapGenerated(IMapCell map)
        {
            var allCells = map.GetAllCells().Where(x => x is IItemHolder)
                           .OrderBy(x => Guid.NewGuid())
                           .Cast <IItemHolder>();
            var goldLeft = GoldAvailable;

            foreach (var cell in allCells)
            {
                if (goldLeft <= 0)
                {
                    break;
                }
                cell.AddItem(new GoldItem());
                goldLeft--;
            }
        }
Exemple #19
0
        public async Task CreateCell(IMapCell cell)
        {
            List <Task> tasks = new List <Task>();
            var         key   = (UInt64)cell.GetPrimaryKeyLong();

            var cellx = (key >> 48) & 0xFFFF;
            var celly = (key >> 32) & 0xFFFF;

            await cell.Create(State.InstanceID, (UInt32)cellx, (UInt32)celly);

            if (CreatureEntryByCellKey.ContainsKey(key))
            {
                var creatures = CreatureEntryByCellKey[key];
                tasks.Add(SpawnCreatures(creatures.ToArray()));
            }

            await Task.WhenAll(tasks);
        }
Exemple #20
0
        public async Task CreateCell(IMapCell cell)
        {
            List<Task> tasks = new List<Task>();
            var key = (UInt64) cell.GetPrimaryKeyLong();

            var cellx = (key >> 48) & 0xFFFF;
            var celly = (key >> 32) & 0xFFFF;

            await cell.Create(State.InstanceID, (UInt32) cellx, (UInt32) celly);

            if (CreatureEntryByCellKey.ContainsKey(key))
            {
                var creatures = CreatureEntryByCellKey[key];
                tasks.Add(SpawnCreatures(creatures.ToArray()));
            }

            await Task.WhenAll(tasks);
        }
Exemple #21
0
        protected virtual IEnumerable <IMapCell> GetPossibleNeighbours(IMapCell from, ICollection <IMapCell> visitedCells)
        {
            ICollection <IMapCell> possibleNeighbours = new HashSet <IMapCell>();
            IMapCell neighbour;

            if (from.Y > 0)
            {
                neighbour = _map.GetCell(from.Y - 1, from.X);
                if (!visitedCells.Contains(neighbour) && CheckFlowBetweenCells(from, neighbour))
                {
                    possibleNeighbours.Add(neighbour);
                }
            }

            if (from.Y < _map.SizeY - 1)
            {
                neighbour = _map.GetCell(from.Y + 1, from.X);
                if (!visitedCells.Contains(neighbour) && CheckFlowBetweenCells(from, neighbour))
                {
                    possibleNeighbours.Add(neighbour);
                }
            }

            if (from.X > 0)
            {
                neighbour = _map.GetCell(from.Y, from.X - 1);
                if (!visitedCells.Contains(neighbour) && CheckFlowBetweenCells(from, neighbour))
                {
                    possibleNeighbours.Add(neighbour);
                }
            }

            if (from.X < _map.SizeX - 1)
            {
                neighbour = _map.GetCell(from.Y, from.X + 1);
                if (!visitedCells.Contains(neighbour) && CheckFlowBetweenCells(from, neighbour))
                {
                    possibleNeighbours.Add(neighbour);
                }
            }

            return(possibleNeighbours);
        }
 public static bool AreConnected(this IMapCell cell, IMapCell other)
 {
     var fringe = new Queue<IMapCell>();
     fringe.Enqueue(cell);
     var tabu = new List<IMapCell>();
     while(fringe.Count > 0)
     {
         var check = fringe.Dequeue();
         if (check == other)
             return true;
         tabu.Add(check);
         foreach (var connectedCell in check.GetConnectedCells())
         {
             if(!tabu.Contains(connectedCell))
                 fringe.Enqueue(connectedCell);
         }
     }
     return false;
 }
Exemple #23
0
        private IEnumerable <IMapCell> GetAllReachableCells(IMapCell fromCell)
        {
            Queue <IMapCell>       q            = new Queue <IMapCell>();
            ICollection <IMapCell> visitedCells = new HashSet <IMapCell>();

            IMapCell cell = fromCell;

            q.Enqueue(cell);

            while (q.Count > 0)
            {
                visitedCells.Add(cell);

                IEnumerable <IMapCell> possibleNeighbours = GetPossibleNeighbours(cell, visitedCells);
                possibleNeighbours.ToList().ForEach(pn => q.Enqueue(pn));
                cell = q.Dequeue();
            }

            return(visitedCells);
        }
Exemple #24
0
        public static IEnumerable <IMapCell> GetAllCells(this IMapCell cell)
        {
            var queue = new Queue <IMapCell>();
            var found = new List <IMapCell>();

            queue.Enqueue(cell);
            while (queue.Count > 0)
            {
                var check = queue.Dequeue();
                found.Add(check);
                foreach (var connectedCell in check.GetConnectedCells())
                {
                    if (!found.Contains(connectedCell))
                    {
                        queue.Enqueue(connectedCell);
                    }
                }
            }
            return(found);
        }
Exemple #25
0
        public static string BuildPerception(this IMapCell currentCell)
        {
            var sb           = new StringBuilder();
            var probeCurrent = currentCell.ProbeCurrent();

            if (!string.IsNullOrEmpty(probeCurrent))
            {
                sb.AppendLine(probeCurrent);
            }
            var connected = currentCell.GetConnectedDirections();

            foreach (var direction in connected)
            {
                var next = currentCell.GetCellInDirection(direction);
                var m    = next.Probe();
                if (!string.IsNullOrEmpty(m))
                {
                    sb.AppendLine(m);
                }
            }
            return(sb.ToString());
        }
Exemple #26
0
        public static IEnumerable <Direction> GetConnectedDirections(this IMapCell mapCell)
        {
            var connected = new List <Direction>();

            if (mapCell.North != null && !(mapCell.North is WallMapCell))
            {
                connected.Add(Direction.North);
            }
            if (mapCell.East != null && !(mapCell.East is WallMapCell))
            {
                connected.Add(Direction.East);
            }
            if (mapCell.South != null && !(mapCell.South is WallMapCell))
            {
                connected.Add(Direction.South);
            }
            if (mapCell.West != null && !(mapCell.West is WallMapCell))
            {
                connected.Add(Direction.West);
            }
            return(connected.OrderBy(x => Guid.NewGuid()));
        }
Exemple #27
0
        protected override IResult CheckCell(IMapCell cell)
        {
            Stack <IMapCell>       stack        = new Stack <IMapCell>();
            ICollection <IMapCell> visitedCells = new HashSet <IMapCell>();
            bool     pacificConnected           = false;
            bool     atlanticConnectected       = false;
            bool     success   = false;
            IMapCell startCell = cell;

            stack.Push(cell);

            while (stack.Count > 0)
            {
                visitedCells.Add(cell);
                IEnumerable <IMapCell> possibleNeighbours = GetPossibleNeighbours(cell, visitedCells);
                //There is more likely that higher neigbourhood will not be a dead end
                possibleNeighbours.OrderBy(n => n.Heigh).ToList().ForEach(pn => stack.Push(pn));

                if (possibleNeighbours.Contains(_map.GetCell(0, 0)))
                {
                    pacificConnected = true;
                    TurnStackUpsideDown(ref stack);
                }
                if (possibleNeighbours.Contains(_map.GetCell(_map.SizeY - 1, _map.SizeX - 1)))
                {
                    atlanticConnectected = true;
                    TurnStackUpsideDown(ref stack);
                }
                if (pacificConnected && atlanticConnectected)
                {
                    success = true;
                    break;
                }

                cell = stack.Pop();
            }

            return(new Result(startCell, visitedCells.Count(), success));
        }
Exemple #28
0
        protected override IResult CheckCell(IMapCell cell)
        {
            Queue <IMapCell>       q            = new Queue <IMapCell>();
            ICollection <IMapCell> visitedCells = new HashSet <IMapCell>();
            bool     pacificConnected           = false;
            bool     atlanticConnectected       = false;
            int      startTime = System.Environment.TickCount;
            bool     success   = false;
            IMapCell startCell = cell;

            q.Enqueue(cell);

            while (q.Count > 0)
            {
                visitedCells.Add(cell);

                IEnumerable <IMapCell> possibleNeighbours = GetPossibleNeighbours(cell, visitedCells);
                possibleNeighbours.ToList().ForEach(pn => q.Enqueue(pn));

                if (possibleNeighbours.Contains(_map.GetCell(0, 0)))
                {
                    pacificConnected = true;
                }
                if (possibleNeighbours.Contains(_map.GetCell(_map.SizeY - 1, _map.SizeX - 1)))
                {
                    atlanticConnectected = true;
                }
                if (pacificConnected && atlanticConnectected)
                {
                    success = true;
                    break;
                }

                cell = q.Dequeue();
            }

            return(new Result(startCell, visitedCells.Count(), success));
        }
Exemple #29
0
 public static void SetMapCell(this IMapCell current, Direction dir, IMapCell next)
 {
     if (dir == Direction.West)
     {
         current.West = next;
         next.East    = current;
     }
     else if (dir == Direction.East)
     {
         current.East = next;
         next.West    = current;
     }
     else if (dir == Direction.North)
     {
         current.North = next;
         next.South    = current;
     }
     else
     {
         current.South = next;
         next.North    = current;
     }
 }
 private void RunPostGenHooks(IMapCell map)
 {
     foreach (var mapGeneratedHook in _genHooks)
     {
         mapGeneratedHook.MapGenerated(map);
     }
 }
 private static bool CheckSymmetrySingle(IMapCell cell)
 {
     bool ok = true;
     var connectedDirs = cell.GetConnectedDirections();
     foreach (var dir in connectedDirs)
     {
         var oppDir = dir.OppositeDirection();
         ok = ok && cell.GetCellInDirection(dir).GetCellInDirection(oppDir) == cell;
     }
     return ok;
 }
 public static void SetMapCell(this IMapCell current, Direction dir, IMapCell next)
 {
     if (dir == Direction.West)
     {
         current.West = next;
         next.East = current;
     }
     else if (dir == Direction.East)
     {   current.East = next;
         next.West = current;
     }
     else if (dir == Direction.North)
     {
         current.North = next;
         next.South = current;
     }
     else
     {
         current.South = next;
         next.North = current;
     }
 }
Exemple #33
0
 protected override bool CheckFlowBetweenCells(IMapCell from, IMapCell to)
 {
     return(to.Heigh >= from.Heigh);
 }
 public static IMapCell GetForwardCell(this IAgent agent, IMapCell current)
 {
     return current.GetCellInDirection(agent.CurrentDirection);
 }
Exemple #35
0
 public void MoveUnit(IMapCell anotherMapCell)
 {
     anotherMapCell.Unit = Unit;
     this.Unit           = null;
 }
 private bool Validate(IMapCell generateMap)
 {
     if (generateMap == null)
         return false;
     var wumpus = generateMap.FindWumpus().MoveSuccessful;
     var symmetry = generateMap.CheckSymmetry();
     return wumpus && symmetry;
 }
Exemple #37
0
 protected virtual bool CheckFlowBetweenCells(IMapCell from, IMapCell to)
 {
     return(to.Heigh <= from.Heigh);
 }
Exemple #38
0
 public void AddCell(IMapCell c)
 {
     _map[c.Y, c.X] = c;
 }