Exemplo n.º 1
0
        public static bool LoadMap(String mapPath)
        {
            if (!File.Exists(mapPath))
            {
                return(false);
            }

            using (FileStream stream = new FileStream(mapPath, FileMode.Open, FileAccess.Read))
            {
                StreamReader reader = new StreamReader(stream);

                String line;
                while (!reader.EndOfStream && (line = reader.ReadLine().Trim().ToLower()) != "map")
                {
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    String[] split = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (split.Length < 2)
                    {
                        return(false);
                    }

                    switch (split[0])
                    {
                    case "teams":
                        TeamCount = int.Parse(split[1]); break;

                    case "width":
                        MapWidth = int.Parse(split[1]); break;

                    case "height":
                        MapHeight = int.Parse(split[1]); break;

                    default:
                        return(false);
                    }
                }

                if (TeamCount == 0 || MapWidth == 0 || MapHeight == 0)
                {
                    return(false);
                }

                Program.Log("teams", TeamCount);
                Program.Log("width", MapWidth);
                Program.Log("height", MapHeight);

                Map = new Tile[MapWidth, MapHeight];

                Teams = new Team[TeamCount];

                for (int i = 0; i < GameState.TeamCount; ++i)
                {
                    Teams[i] = new Team(i);
                }

                Program.Log("map");

                if (!reader.EndOfStream)
                {
                    int y = 0;
                    while (!reader.EndOfStream && (line = reader.ReadLine().ToLower()).Trim() != "end")
                    {
                        if (line.Length < MapWidth * 2)
                        {
                            return(false);
                        }

                        line = line.Substring(0, MapWidth * 2);
                        Program.Log(line);

                        for (int x = 0; x < MapWidth; ++x)
                        {
                            char c = line[x << 1];
                            if (c == '#')
                            {
                                Map[x, y] = Tile.Wall;
                            }
                            else if (char.IsNumber(c))
                            {
                                int teamNo;
                                if (!int.TryParse(c.ToString(), out teamNo) || teamNo >= TeamCount)
                                {
                                    return(false);
                                }

                                Team team = Teams[teamNo];

                                Direction dir;
                                if (!Direction.TryParse(line[(x << 1) + 1].ToString(), out dir))
                                {
                                    return(false);
                                }

                                Position pos = new Position(x, y);

                                team.Bases.Add(pos);
                                team.Agents.Add(new Agent(team, pos, dir));

                                team.InitialBasePos = pos;
                                team.InitialBaseDir = dir;
                            }
                            else
                            {
                                Map[x, y] = Tile.Empty;
                            }
                        }
                        ++y;
                    }
                }

                Program.Log("end");
            }

            Dead     = new List <Agent>();
            Packages = new List <Position>();

            return(true);
        }