示例#1
0
        private static void readNodeTypeVars(List<string[]> commands, World map, Dictionary<string, NodeType> nodeTypes, List<NodeType> nodeTypeNoID)
        {
            int tempInt = 0;
            double tempDbl = 0d;

            NodeType nodeType = new NodeType();

            for (int j = 1; j < commands.Count; j++)
            {
                string value = translateValue(commands[j][2], "[NodeType]", map);
                bool valueValid = true;
                switch (commands[j][0])
                {
                    case "ID":
                        nodeType.ID = value;
                        nodeTypes.Add(value, nodeType);
                        break;
                    case "IsParent":
                        valueValid = value == "true" || value == "false";
                        nodeType.IsParent = value == "true";
                        break;
                    case "NumSegments":
                        valueValid = int.TryParse(value, out tempInt);
                        nodeType.NumSegments = tempInt;
                        break;
                    case "Radius":
                        valueValid = double.TryParse(value, out tempDbl);
                        nodeType.Radius = (FInt)tempDbl;
                        break;
                    case "Spacing":
                        valueValid = double.TryParse(value, out tempDbl);
                        nodeType.Spacing = (FInt)tempDbl;
                        break;
                    case "GenSpacing":
                        valueValid = double.TryParse(value, out tempDbl);
                        nodeType.GenSpacing = (FInt)tempDbl;
                        break;
                    case "SightDistance":
                        valueValid = double.TryParse(value, out tempDbl);
                        nodeType.SightDistance = (FInt)tempDbl;
                        break;
                    case "BuildRangeMin":
                        valueValid = double.TryParse(value, out tempDbl);
                        nodeType.BuildRangeMin = (FInt)tempDbl;
                        break;
                    case "Name":
                        nodeType.Name = value;
                        break;
                    default:
                        throw new Exception("NodeType variable not recognized: " + commands[j][0]);
                }

                if (!valueValid)
                    throw new Exception("Value '" + commands[j][2] + "' not valid.");
            }

            if (nodeType.ID != null)
                map.addNodeType(nodeType);
            else
                nodeTypeNoID.Add(nodeType);
        }
示例#2
0
        public static World loadWorld(string path, GraphicsDeviceManager graphics)
        {
            Graphics = graphics;
            World map = new World();
            List<List<string[]>> commands = new List<List<string[]>>();

            // first read file into a list of commands
            TextReader reader = null;
            int debugI = -1;
            try
            {
                reader = new StreamReader(path);
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    debugI++;
                    if (debugI == 29)
                    { }
                    // remove comments
                    int commentIndex = line.IndexOf(@"//");
                    if (commentIndex != -1)
                        line = line.Substring(0, commentIndex);

                    // remove whitespace outside of quotes
                    {
                        bool inQuotes = false;
                        StringBuilder buf = new StringBuilder(line);
                        for (int i = 0; i < buf.Length; i++)
                        {
                            if (buf[i] == '"')
                                inQuotes = !inQuotes;
                            else if (!inQuotes && (buf[i] == ' ' || buf[i] == '\t'))
                            {
                                buf.Remove(i, 1);
                                i--;
                            }
                        }
                    }

                    if (string.IsNullOrEmpty(line))
                        continue;

                    List<string[]> section = null;

                    switch (line)
                    {
                        case "[World]":
                        case "[Player]":
                        case "[Camera]":
                        case "[Grid]":
                        case "[FogOfWar]":
                        case "[Scripts]":
                        case "[Node]":
                        case "[Segment]":
                        case "[Geo]":
                        case "[NodeType]":
                        case "[PathFinder]":
                        case "[Hotspot]":
                            commands.Add(new List<string[]>());
                            section = commands[commands.Count - 1];
                            section.Add(new string[3]);
                            section[0][0] = line;
                            break;
                        default:
                            int split = line.IndexOf('=');
                            string[] breakUp = new string[] { line.Substring(0, split), line.Substring(split + 1) };
                            section = commands[commands.Count - 1];
                            section.Add(new string[3]);
                            section[section.Count - 1][2] = breakUp[1];
                            if(breakUp[0].EndsWith("]"))
                            {
                                int openBrace = breakUp[0].IndexOf('[');

                                if (openBrace != -1)
                                {
                                    section[section.Count - 1][0] = breakUp[0].Substring(0, openBrace);
                                    section[section.Count - 1][1] = breakUp[0].Substring(openBrace + 1, breakUp[0].Length - openBrace - 2);
                                }
                                else
                                {
                                    section[section.Count - 1][0] = breakUp[0];
                                }
                            }
                            else
                            {
                                section[section.Count - 1][0] = breakUp[0];
                            }
                            break;
                    }
                }
            }
            catch (Exception ex)
            { }
            finally { if (reader != null) reader.Close(); }

            Dictionary<string, Player> players = new Dictionary<string, Player>();
            Dictionary<string, Node> nodes = new Dictionary<string,Node>();
            Dictionary<string, Segment> segments = new Dictionary<string,Segment>();
            Dictionary<string, Geo> geos = new Dictionary<string, Geo>();
            Dictionary<string, Hotspot> hotspots = new Dictionary<string, Hotspot>();
            Dictionary<string, NodeType> nodeTypes = new Dictionary<string, NodeType>();

            List<Node> nodeList = new List<Node>();
            List<Segment> segList = new List<Segment>();

            List<Node> nodeNoID = new List<Node>();
            List<Segment> segNoID = new List<Segment>();
            List<Geo> geoNoID = new List<Geo>();
            List<Hotspot> hotspotNoID = new List<Hotspot>();
            List<NodeType> nodeTypeNoID = new List<NodeType>();

            // process commands

            // world
            for (int i = 0; i < commands.Count; i++)
            {
                if (commands[i][0][0] == "[World]")
                {
                    readWorldVars(commands[i], map);
                    commands.RemoveAt(i);
                    break;
                }
            }

            // camera
            map.Cam.Width = (FInt)Graphics.PreferredBackBufferWidth;
            map.Cam.Height = (FInt)Graphics.PreferredBackBufferHeight;
            for (int i = 0; i < commands.Count; i++)
            {
                if (commands[i][0][0] == "[Camera]")
                {
                    readCameraVars(commands[i], map);
                    commands.RemoveAt(i);
                    break;
                }
            }

            // grid, fog of war, node types, and pathfinder
            bool gridFound = false;
            bool fogFound = false;
            for (int i = 0; i < commands.Count; i++)
            {
                if (!gridFound && commands[i][0][0] == "[Grid]")
                {
                    gridFound = true;
                    readGridVars(commands[i], map);
                    commands.RemoveAt(i);
                    i--;
                }
                else if (!fogFound && commands[i][0][0] == "[FogOfWar]")
                {
                    fogFound = true;
                    readFogOfWarVars(commands[i], map);
                    commands.RemoveAt(i);
                    i--;
                }
                else if (commands[i][0][0] == "[NodeType]")
                {
                    readNodeTypeVars(commands[i], map, nodeTypes, nodeTypeNoID);
                    commands.RemoveAt(i);
                    i--;
                }
                else if (commands[i][0][0] == "[PathFinder]")
                {
                    readPathFinderVars(commands[i], map);
                    commands.RemoveAt(i);
                    i--;
                }
            }

            // players, geos, and hotspots
            for (int i = 0; i < commands.Count; i++)
            {
                if (commands[i][0][0] == "[Player]")
                {
                    readPlayerVars(commands[i], map, players);
                    commands.RemoveAt(i);
                    i--;
                }
                else if (commands[i][0][0] == "[Geo]")
                {
                    readGeoVars(commands[i], map, geos, geoNoID);
                    commands.RemoveAt(i);
                    i--;
                }
                else if (commands[i][0][0] == "[Hotspot]")
                {
                    readHotspotVars(commands[i], map, hotspots, hotspotNoID);
                    commands.RemoveAt(i);
                    i--;
                }
            }

            // nodes and segments: 1st round
            for (int i = 0; i < commands.Count; i++)
            {
                if (commands[i][0][0] == "[Node]")
                {
                    readNodeVars1(commands[i], map, nodeList, nodes, players, nodeTypes, hotspots);
                    readNodeVars2(commands[i], map, nodeList[nodeList.Count - 1], nodes, players);
                }
                else if (commands[i][0][0] == "[Segment]")
                {
                    readSegmentVars1(commands[i], map, segList, segments, players);
                }
            }

            // nodes and segments: 2nd round
            int curNode = 0;
            int curSegment = 0;
            for (int i = 0; i < commands.Count; i++)
            {
                if (commands[i][0][0] == "[Node]")
                {
                    readNodeVars3(commands[i], map, nodeList[curNode], segments, nodeNoID);
                    curNode++;
                    commands.RemoveAt(i);
                    i--;
                }
                else if (commands[i][0][0] == "[Segment]")
                {
                    readSegmentVars2(commands[i], map, segList[curSegment], nodes);
                    curSegment++;
                }
            }

            // segments: 3rd round
            curSegment = 0;
            for (int i = 0; i < commands.Count; i++)
            {
                if (commands[i][0][0] == "[Segment]")
                {
                    readSegmentVars3(commands[i], map, segList[curSegment], segNoID);
                    curSegment++;
                }
                else
                {
                    throw new Exception("Invalid section: " + commands[i][0][0]);
                }
            }

            map.refreshNodeVars();
            map.sortNodeTypes();
            map.refreshNextGenIDs();

            // add IDless objects to map
            foreach (Node node in nodeNoID)
            {
                map.addNode(node);
            }
            foreach (Segment seg in segNoID)
            {
                map.addSegment(seg);
            }
            foreach (Geo geo in geoNoID)
            {
                map.addGeo(geo);
            }
            foreach (Hotspot hotspot in hotspotNoID)
            {
                map.addHotspot(hotspot);
            }
            foreach (NodeType nt in nodeTypeNoID)
            {
                map.addNodeType(nt);
            }

            // prepare player vars
            for (int i = 0; i < map.Players.Count; i++)
            {
                map.Players[i].Fog.applyVisibility(map.Players[i].Nodes);

                if (map.Players[i].Type == Player.PlayerType.Computer)
                {
                    map.Players[i].AIThreadBase = new AIThread(map.Players[i]);
                    map.Players[i].AIThreadBase.Path = new PathFinder(map.PathRows, map.PathCols, map);
                    Node[] plyrNodes = null;
                    Segment[] plyrSegs = null;
                    NodeSkel[] allNodes = null;
                    SegmentSkel[] allSegs = null;
                    map.Players[i].AIThreadBase.Grid = map.Grid.getSkeleton(map.Players[i], out plyrNodes, out plyrSegs, out allNodes, out allSegs);
                    map.Players[i].AIThreadBase.Nodes.AddRange(plyrNodes);
                    map.Players[i].AIThreadBase.Segments.AddRange(plyrSegs);
                    map.Players[i].AIThreadBase.buildDictionaries(allNodes, allSegs);

                    map.NodeEvents[i].Clear();
                    map.SegEvents[i].Clear();

                    map.Players[i].AIThreadBase.refreshVisibility();
                    map.Players[i].AIThreadBase.refreshPathIntersections();
                }
            }

            map.refreshVisibility();

            // let go of the graphics device, since it's no longer needed
            Graphics = null;

            return map;
        }