///<summary> ///load graph from map data ///</summary> ///<param name="mapData"></param> ///<returns>true if successful</returns> public bool Load(MapData mapData) { Clear(); //get the number of nodes and read them in int numNodes = mapData.NodeList.Count; for (int n = 0; n < numNodes; ++n) { NavGraphNode newNode = new NavGraphNode(mapData.NodeList[n]); //when editing graphs (with Buckland's Raven map editor), it's //possible to end up with a situation where some of the nodes //have been invalidated (their id's set to invalid_node_index). //Therefore when a node of index invalid_node_index is //encountered, it must still be added. if (!GraphNode.IsInvalidIndex(newNode.Index)) { AddNode(newNode); } else { Nodes.Add(newNode); //make sure an edgelist is added for each node Edges.Add(new LinkedList<NavGraphEdge>()); ++NextNodeIndex; } } //now add the edges int numEdges = mapData.EdgeList.Count; for (int e = 0; e < numEdges; ++e) { NavGraphEdge nextEdge = new NavGraphEdge(mapData.EdgeList[e]); Edges[nextEdge.From].AddLast(nextEdge); } return true; }
///<summary> ///sets up the game environment from map file ///</summary> ///<param name="filename"></param> ///<param name="mapData"></param> ///<returns></returns> public bool LoadMap(string filename, out MapData mapData) { mapData = null; try { mapData = MyGame.Instance.Content.Load<MapData>(filename); } catch (Exception e) { Assert.Fatal(false, "Map.LoadMap: Bad Map Filename -> " + e.Message); return false; } Clear(); //first of all read and create the navgraph. This must be done //before the entities are read from the map file because many of //the entities will be linked to a graph node (the graph node will //own a pointer to an instance of the entity) NavGraph = new SparseGraph(false); NavGraph.Load(mapData); LogUtil.WriteLineIfLogCreate("NavGraph for " + filename + " loaded okay"); LogUtil.WriteLineIfLogCreate(NavGraph.ToString()); //determine the average distance between graph nodes so that we can //partition them efficiently CellSpaceNeighborhoodRange = GraphUtil.CalculateAverageGraphEdgeLength(NavGraph) + 1; LogUtil.WriteLineIfLogCreate("Average edge length is " + GraphUtil.CalculateAverageGraphEdgeLength(NavGraph)); LogUtil.WriteLineIfLogCreate("Neighborhood range set to " + CellSpaceNeighborhoodRange); //load in the map size SizeX = mapData.SizeX; SizeY = mapData.SizeY; LogUtil.WriteLineIfLogCreate("Partitioning navgraph nodes..."); //partition the graph nodes PartitionNavGraph(); LogUtil.WriteLineIfLogCreate("Loading map..."); foreach (WallData wallData in mapData.WallList) { LogUtil.WriteLineIfLogCreate("Creating a wall <" + wallData.Name + "> between " + wallData.From + " and " + wallData.To); AddWall(wallData); } //note: add triggers before doors (or else!) foreach (DoorTriggerData doorTriggerData in mapData.DoorTriggerList) { LogUtil.WriteLineIfLogCreate("Creating a door trigger <" + doorTriggerData.Name + "> at " + doorTriggerData.Position); AddDoorTrigger(doorTriggerData); } foreach (DoorData doorData in mapData.DoorList) { LogUtil.WriteLineIfLogCreate("Creating a door <" + doorData.Name + "> between " + doorData.From + " and " + doorData.To); AddDoor(doorData); } foreach (SpawnPointData spawnPointData in mapData.SpawnPointList) { LogUtil.WriteLineIfLogCreate("Creating a spawn point <" + spawnPointData.Name + "> at " + spawnPointData.Position); AddSpawnPoint(spawnPointData); } foreach (HealthData healthData in mapData.HealthList) { LogUtil.WriteLineIfLogCreate( "Creating a health giver trigger <" + healthData.Name + "> at " + healthData.Position); AddHealth(healthData); } foreach (RailgunData railgunData in mapData.RailgunList) { LogUtil.WriteLineIfLogCreate( "Creating a rail gun weapon giver trigger <" + railgunData.Name + "> at " + railgunData.Position); AddRailgun(railgunData); } foreach (RocketLauncherData rocketLauncherData in mapData.RocketLauncherList) { LogUtil.WriteLineIfLogCreate( "Creating a rocket launcher weapon giver trigger <" + rocketLauncherData.Name + "> at " + rocketLauncherData.Position); AddRocketLauncher(rocketLauncherData); } foreach (ShotgunData shotgunData in mapData.ShotgunList) { LogUtil.WriteLineIfLogCreate( "Creating a shot gun weapon giver trigger <" + shotgunData.Name + "> at " + shotgunData.Position); AddShotgun(shotgunData); } LogUtil.WriteLineIfLogCreate(filename + " loaded okay"); //calculate the cost lookup table PathCosts = GraphUtil.CreateAllPairsCostsTable(NavGraph); return true; }
///<summary> ///Convert from Raven .map format to RavenX .xml and change scale ///</summary> ///<param name="ravenMapFilename"></param> ///<param name="xmlMapFilename"></param> ///<param name="scale"></param> public static void Convert( string ravenMapFilename, string xmlMapFilename, float scale) { MapData mapData = new MapData(); mapData.LoadFromRavenMap(ravenMapFilename); mapData.ChangeScale(scale); mapData.SaveToXml(xmlMapFilename); }