public EntityManager(World world)
 {
     entities = new List<Entity.Entity>();
     entitiesToAdd = new List<Entity.Entity>();
     playerLoc = -1;
     this.mainGame = Game1.Instance;
     this.world = world;
 }
        private int mapWidth; //Size of the map

        #endregion Fields

        #region Constructors

        public DijkstraMap(World world, int width, int height, int startX, int startY, int scale = 1, params int[][] goals)
        {
            this.goals = goals;
            this.mapWidth = width * scale;
            this.mapHeight = height * scale;
            this.modX = startX;
            this.modY = startY;
            this.map = generateMap(world, goals);
            this.scale = scale;
            // printMap();
        }
 private void initalizeMaps(World world)
 {
     if (world.civilianMaps != null)
         return;
     Console.WriteLine("Initalizing map for world #" + world.GetHashCode() + " with goals at: ");
     int[][] goals = new int[world.doors.Length][];
     for (int x = 0; x < goals.Length; x++) {
         goals[x] = new int[] { (int) world.doors[x].X, (int) world.doors[x].Y };
         Console.WriteLine("X: " + goals[x][0] + "\tY: " + goals[x][1]);
         if (goals[x][0] > world.worldWidth || goals[x][1] > world.worldHeight)
             goals[x] = null;
     }
     world.civilianMaps = new DijkstraMap[1];
     world.civilianMaps[0] = new DijkstraMap(world, world.worldWidth, world.worldHeight, 0, 0, 1, goals);
     for (int x = 0; x < 27; x++) {
         NPC civvie = new NPC(new FloatRectangle(), Sprites.spritesDictionary["player"], 4);
         civvie.ai = new CivilianAI(civvie);
         if (!worldPools.ContainsKey(world))
             worldPools.Add(world, new Queue<Object[]>());
         worldPools[world].Enqueue(new Object[] { civvie, null});
     }
 }
 //Let's get a random list of valid goal points for civvies to walk towards!
 private int[][] getRandomGoals(World world, int baseNum = 7, bool keepTrying = false)
 {
     if (baseNum < 0)
         return null;
     Random random = Game1.Instance.random;
     int[][] list = new int[baseNum][];
     for (int k = 0; k < list.Length; k++) {
         int x = random.Next(0, world.worldWidth);
         int y = random.Next(0, world.worldHeight);
         int i = 0;
         while (!isTileValid(world, x, y) && (keepTrying || i < 500)) {
             x = random.Next(0, world.worldWidth);
             y = random.Next(0, world.worldHeight);
             i++;
         }
         if (keepTrying || i < 500)
             list[k] = new int[] { x, y };
         else
             list[k] = new int[] { -1, -1 };
     }
     return list;
 }
        /// <summary>
        /// loads the world from a given path
        /// </summary>
        /// <param name="worldPath">The path to the world</param>
        public World LoadWorld(String worldPath)
        {
            Stream worldStream = File.OpenRead("./Content/" + worldPath + ".txt");
            StreamReader worldReader = new StreamReader(worldStream);

            World tmpWorld = new World(int.Parse(worldReader.ReadLine()), int.Parse(worldReader.ReadLine()));

            tmpWorld.canRespawn = bool.Parse(worldReader.ReadLine());   //Josiah S DeVizia

            GUI.Door[] doors = new GUI.Door[int.Parse(worldReader.ReadLine())]; //Josiah S DeVizia

            //Josiah S DeVizia
            for(int x = 0; x < doors.Length; ++x)
            {
                doors[x] = new Door(worldReader.ReadLine(), int.Parse(worldReader.ReadLine()), int.Parse(worldReader.ReadLine()));
            }

            Vector2[] doorLocs = new Vector2[doors.Length]; //Josiah S DeVizia

            int y = 0;
            string line = worldReader.ReadLine();
            int row = 0;
            int doorCntr = 0;
            while (line != null)
            {
                for (int col = 0; col < line.Length; ++col)
                {
                    switch (line[col])
                    {
                        case '0':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["RoadTar"];
                            break;

                        case '1':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["RoadLine"];
                            break;

                        case '2':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["SideWalkBrick"];
                            break;

                        case '3':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["BuildingEdge"];
                            break;

                        case '4':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["BuildingRoof"];
                            break;

                        case '5':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["Water"];
                            break;

                        //Josiah S DeVizia
                        case '6':
                            doorLocs[y] = new Vector2(row, col);
                            y++;
                            tmpWorld.tiles[row][col] = doors[doorCntr];
                            doorCntr++;
                            break;

                        //Josiah S DeVizia
                        case '7':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["Debris"];
                            break;
                        case '8':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["Heal"];
                            break;
                        case '9':
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["Ammo"];
                            break;
                        default:
                            tmpWorld.tiles[row][col] = Tiles.tilesDictionary["BuildingEdge"];
                            break;
                    }
                }
                row++;
                line = worldReader.ReadLine();
            }
            tmpWorld.doors = doorLocs;
            //MainGame.worldManager.worlds.Add(worldPath, tmpWorld);
            return tmpWorld;
        }
 private void SpawnCivilians(World world)
 {
     if (!Game1.Instance.worldManager.CurrentWorld.canRespawn)
         return;
     if (world.manager.civilianCount >= 27)
         return;
     Console.WriteLine("Respawning Civilians: " + (27 - world.manager.civilianCount) + " for world: " + world.GetHashCode());
     for (int x = 0; x < 27 - world.manager.civilianCount; x++) {
         NPC civvie = new NPC(new FloatRectangle(), Sprites.spritesDictionary["player"], 4);
         civvie.ai = new CivilianAI(civvie);
         if (!worldPools.ContainsKey(world))
             worldPools.Add(world, new Queue<Object[]>());
         worldPools[world].Enqueue(new Object[] { civvie, null});
     }
 }
 private bool isTileValid(World world, int x, int y)
 {
     if (x < 0 || y < 0 || x >= world.tiles.Length || y >= world.tiles[0].Length)
         return false;
     if (!world.tiles[x][y].IsWalkable)
         return false;
     if (x - 1 > 0) {
         //if (y - 1 > 0)
         //    if (!world.tiles[x - 1][y - 1].IsWalkable)
         //        return false;
         if (!world.tiles[x - 1][y].IsWalkable)
             return false;
         //if (y + 1 < world.tiles[0].Length)
         //    if (!world.tiles[x - 1][y + 1].IsWalkable)
         //        return false;
     }
     if (y - 1 > 0)
         if (!world.tiles[x][y - 1].IsWalkable)
             return false;
     if (y + 1 < world.tiles[0].Length)
         if (!world.tiles[x][y + 1].IsWalkable)
             return false;
     if (x + 1 < world.tiles.Length) {
         //if (y - 1 > 0)
         //    if (!world.tiles[x + 1][y - 1].IsWalkable)
         //        return false;
         if (!world.tiles[x + 1][y].IsWalkable)
             return false;
         //if (y + 1 < world.tiles[0].Length)
         //    if (!world.tiles[x + 1][y + 1].IsWalkable)
         //        return false;
     }
     return true;
 }
 public DijkstraMap GenerateFleeMap(World world)
 {
     for (int x = 0; x < map.Length; x++)
         for (int y = 0; y < map[x].Length; y++)
             if (valid(world, modX + x, modY + y)) {
                 if (!world.tiles[modX + x][modY + y].IsWalkable)
                     continue;
                 map[x][y] = (int) (map[x][y] * -1.2);
             }
     map = scan(world, map);
     return this;
 }
        private int[][] generateMap(World world, params int[][] goals)
        {
            int[][] grid = new int[mapWidth][];
            //Initalizes the grid with 100
            for (int x = 0; x < grid.Length; x++) {
                grid[x] = new int[mapHeight];
                for (int y = 0; y < grid[x].Length; y++)
                    grid[x][y] = 100;
            }
            //Sets the goal points to 0
            foreach (int[] g in goals)
                if (g != null) {
                    int actualX = g[0] * scale;
                    int actualY = g[1] * scale;
                    grid[actualX][actualY] = 0;
                    //Console.WriteLine("GOAL: " + actualX + ", " + actualY);
                }

            scan(world, grid);

            for (int x = 0; x < grid.Length; x++)
                for (int y = 0; y < grid[x].Length; y++)
                    if (valid(world, modX + x, modY + y))
                        if (!world.tiles[modX + x / scale][modY + y / scale].IsWalkable)
                            grid[x][y] = 100;

            return grid;
        }
 public bool valid(World world, int x, int y)
 {
     return x > -1 && y > -1 && x / scale < world.tiles.Length && y / scale < world.tiles[0].Length;
 }
        public int[][] scan(World world, int[][] grid)
        {
            bool b = false;
            while (!b) {
                b = true;
                for (int x = 0; x < grid.Length; x++) {
                    for (int y = 0; y < grid[x].Length; y++)
                        if (valid(world, modX + x, modY + y)) {
                            //if (!world.tiles[modX + x][modY + y].IsWalkable)
                            //    continue;
                            #region Grid Checking
                            if (x - 1 >= 0) {
                                if (y - 1 >= 0) //top left
                                    if (grid[x - 1][y - 1] > grid[x][y] + 1) {
                                        grid[x - 1][y - 1] = grid[x][y] + 1;
                                        b = false;
                                    }
                                if (grid[x - 1][y] > grid[x][y] + 1) { //top middle
                                    grid[x - 1][y] = grid[x][y] + 1;
                                    b = false;
                                }
                                if (y + 1 < grid[x].Length)
                                    if (grid[x - 1][y + 1] > grid[x][y] + 1) { //top right
                                        grid[x - 1][y + 1] = grid[x][y] + 1;
                                        b = false;
                                    }
                            }
                            if (y - 1 >= 0)
                                if (grid[x][y - 1] > grid[x][y] + 1) { //middle left
                                    grid[x][y - 1] = grid[x][y] + 1;
                                    b = false;
                                }
                            if (y + 1 < grid[x].Length)
                                if (grid[x][y + 1] > grid[x][y] + 1) { //middle right
                                    grid[x][y + 1] = grid[x][y] + 1;
                                    b = false;
                                }

                            if (x + 1 < grid.Length) {
                                if (y - 1 >= 0)
                                    if (grid[x + 1][y - 1] > grid[x][y] + 1) { //bottom left
                                        grid[x + 1][y - 1] = grid[x][y] + 1;
                                        b = false;
                                    }
                                if (grid[x + 1][y] > grid[x][y] + 1) { //bottom middle
                                    grid[x + 1][y] = grid[x][y] + 1;
                                    b = false;
                                }
                                if (y + 1 < grid[x].Length)
                                    if (grid[x + 1][y + 1] > grid[x][y] + 1) { //bottom right
                                        grid[x + 1][y + 1] = grid[x][y] + 1;
                                        b = false;
                                    }
                            }
                            #endregion
                        }
                }
            }
            return grid;
        }