Exemplo n.º 1
0
    // Loads a map layout onto a map
    // Map layouts include spawn, artifact, resource and extraction zone locations
    // This function also removes certain aspects of the level that are already there e.g. wrecks
    public static void LoadMapLayout()
    {
        // No map loaded anymore
        MapXml     = "";
        LayoutName = "";

        Subsystem.AttributeLoader.PatchOverrideData = "";
        PatchName = "";

        if (CustomLayout)
        {
            // Move DoK engine objects
            foreach (Entity entity in Sim.Instance.EntitySystem)
            {
                if (entity.HasComponent(11))
                {
                    entity.GetComponent <Position>(10).Position2D = new Vector2r(Fixed64.FromInt(1000000), Fixed64.FromInt(1000000)); // Move off the map
                    entity.GetComponent <Resource>(11).Disabled   = true;                                                             // Make unminable, keeping these in just in case the AI tries to mine from these resources
                }
                else if (entity.HasComponent(36) || entity.HasComponent(14))
                {
                    entity.GetComponent <Position>(10).Position2D = new Vector2r(Fixed64.FromInt(1000000), Fixed64.FromInt(1000000));                    // Move off the map
                }
            }

            // Disable the pesky black box at the start of levels
            // Its there to be a transition from what I can tell
            GameObject blackBox = GameObject.Find("BlackPolygon");
            if (blackBox != null)
            {
                blackBox.transform.position = new Vector3(1000000.0f, 1000000.0f, 1000000.0f);
            }

            // Loading resource layout
            foreach (MapResourceData resource in resources)
            {
                DetectableAttributesData detectableAttributesData = new DetectableAttributesData()
                {
                    m_SetHasBeenSeenBeforeOnSpawn = true,
                };
                SceneEntityCreator.CreateResourcePoint((resource.type == 0) ? "Resource_CU" : "Resource_RU", resource.position, default(Orientation2), new string[0], new ResourceAttributesData((ResourceType)resource.type, resource.amount, resource.collectors), detectableAttributesData, false, default(ResourcePositionalVariations), false);
            }

            // Delete starting units of commanders with no starting fleet
            foreach (MapSpawnData spawn in spawns)
            {
                if (spawn.fleet)
                {
                    continue;
                }
                foreach (Commander commander in Sim.Instance.CommanderManager.Commanders)
                {
                    CommanderDirectorAttributes director = Sim.Instance.CommanderManager.GetCommanderDirectorFromID(commander.ID);
                    if (director.PlayerType == PlayerType.AI)
                    {
                        continue;                                                           // AI needs starting carrier
                    }
                    if (((GameMode == TeamSetting.Team) ? (1 - spawn.team) * 3 : spawn.team) + spawn.index == director.SpawnIndex)
                    {
                        foreach (Entity entity in Sim.Instance.EntitySystem.Query().Has(2))                           // All units
                        {
                            if (entity.GetComponent <OwningCommander>(5).ID == commander.ID)                          // Check if the faction is correct
                            {
                                entity.GetComponent <Unit>(2).RetireDespawn();
                            }
                        }
                    }
                }
            }

            // Loading units
            foreach (MapUnitData unit in units)
            {
                // Convert team + index to commander ID then spawn unit
                foreach (Commander commander in Sim.Instance.CommanderManager.Commanders)
                {
                    CommanderDirectorAttributes director = Sim.Instance.CommanderManager.GetCommanderDirectorFromID(commander.ID);
                    if (((GameMode == TeamSetting.Team) ? (1 - unit.team) * 3 : unit.team) + unit.index == director.SpawnIndex)
                    {
                        if (commander.CommanderAttributes.Name == "SPECTATOR")
                        {
                            continue;                             // Don't spawn units for spectators
                        }
                        else if (sobanUnits.Contains(unit.type) && commander.CommanderAttributes.Faction.ID != FactionID.Soban)
                        {
                            continue;                             // Don't spawn Soban units for non Soban players
                        }
                        else if (khaanUnits.Contains(unit.type) && commander.CommanderAttributes.Faction.ID != FactionID.Khaaneph)
                        {
                            continue;                             // Don't spawn Khaaneph units for non Khaaneph players
                        }
                        SceneEntityCreator.CreateEntity(unit.type, commander.ID, unit.position, unit.orientation);
                        break;
                    }
                }
            }

            // Loading wrecks
            foreach (MapWreckData wreck in wrecks)
            {
                DetectableAttributesData detectableAttributes = new DetectableAttributesData {
                    m_SetHasBeenSeenBeforeOnSpawn = true,
                };

                ResourcePositionalVariations positions = new ResourcePositionalVariations {
                    ModelOrientationEulersDegrees = new Vector3r(Fixed64.FromConstFloat(0.0f), Fixed64.FromConstFloat(wreck.angle), Fixed64.FromConstFloat(0.0f)),
                };

                ShapeAttributesData shape = new ShapeAttributesData {
                    m_Radius           = 100.0f,
                    m_BlocksLOF        = wreck.blockLof,
                    m_BlocksAllHeights = wreck.blockLof,
                };

                ResourceAttributesData res = new ResourceAttributesData {
                    m_ResourceType = ResourceType.Resource3,                     // Type = Wreck
                };

                SimWreckSectionResourceSpawnableAttributesData[] childResources = new SimWreckSectionResourceSpawnableAttributesData[wreck.resources.Count];
                for (int i = 0; i < wreck.resources.Count; i++)
                {
                    childResources[i] = new SimWreckSectionResourceSpawnableAttributesData {
                        m_DetectableAttributes         = new DetectableAttributesData(),
                        m_OverrideDetectableAttributes = true,
                        m_Tags = new string[0],
                        m_EntityTypeToSpawn                     = (wreck.resources[i].type == 1) ? "Resource_RU" : "Resource_CU",
                        m_ResourceAttributes                    = new ResourceAttributesData((ResourceType)wreck.resources[i].type, wreck.resources[i].amount, wreck.resources[i].collectors),
                        m_OverrideResourceAttributes            = true,
                        m_SpawnPositionOffsetFromSectionCenterX = Fixed64.IntValue((wreck.resources[i].position - wreck.position).X),
                        m_SpawnPositionOffsetFromSectionCenterY = Fixed64.IntValue((wreck.resources[i].position - wreck.position).Y),
                        m_UseNonRandomSpawnPositionOffset       = true,
                    };
                }

                SimWreckAttributesData wreckData = new SimWreckAttributesData {
                    m_WreckSections = new SimWreckSectionAttributesData[] {
                        new SimWreckSectionAttributesData {
                            m_ExplosionChance    = 100,
                            m_Health             = 1,
                            m_ResourceSpawnables = childResources,
                        },
                    },
                };

                // Orientation2.LocalForward -> (1.0, 0.0)
                SceneEntityCreator.CreateWreck("Resource_Wreck_MP", wreck.position, Orientation2.LocalForward, new string[0], wreckData, "", shape, res, detectableAttributes, false, positions, false);
            }
        }
    }
Exemplo n.º 2
0
    public static bool LoadLayout(string mapXml, string sceneName, TeamSetting gameMode, int players)
    {
        System.IO.File.WriteAllText(logName, "");
        // Loading map layout from XML
        try {
            XmlTextReader xmlDokmapReader = new XmlTextReader(new System.IO.StringReader(mapXml));
            while (xmlDokmapReader.Read())
            {
                if (xmlDokmapReader.NodeType == XmlNodeType.Element)
                {
                    switch (xmlDokmapReader.Name)
                    {
                    default:
                        System.IO.File.AppendAllText(logName, string.Format("[GE mod] WARNING: Unknown tag '{0}'" + Environment.NewLine, xmlDokmapReader.Name));
                        Debug.LogWarning(string.Format("[GE mod] WARNING: Unknown tag '{0}'", xmlDokmapReader.Name));
                        break;

                    case "meta":
                    case "dokmap":
                        break;

                    case "layout":
                        if ((TeamSetting)Enum.Parse(typeof(TeamSetting), xmlDokmapReader.GetAttribute("mode")) == gameMode &&
                            (Regex.Replace(xmlDokmapReader.GetAttribute("map"), @"\s+", "").Contains(sceneName) ||
                             Regex.Replace(xmlDokmapReader.GetAttribute("map"), @"\s+", "").Contains("*")) &&
                            xmlDokmapReader.GetAttribute("players").Contains(players.ToString()[0]))
                        {
                            XmlReader xmlLayoutReader = xmlDokmapReader.ReadSubtree();
                            while (xmlLayoutReader.Read())
                            {
                                if (xmlLayoutReader.NodeType == XmlNodeType.Element)
                                {
                                    switch (xmlLayoutReader.Name)
                                    {
                                    // Unimplemented but valid elements
                                    case "layout":
                                    case "resources":
                                    case "artifacts":
                                    case "ezs":
                                    case "spawns":
                                    case "units":
                                    case "colliders":
                                    case "wrecks":
                                        break;

                                    case "resource":
                                        // collectors is an optional attribute
                                        int collectors = 2;
                                        try {
                                            collectors = int.Parse(xmlLayoutReader.GetAttribute("collectors"));
                                        } catch {}

                                        resources.Add(new MapResourceData {
                                            position   = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("x"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("y")))),
                                            type       = (xmlLayoutReader.GetAttribute("type") == "ru") ? 1 : 0,
                                            amount     = int.Parse(xmlLayoutReader.GetAttribute("amount")),
                                            collectors = collectors,
                                        });
                                        break;

                                    case "wreck":
                                        bool blockLof = false;
                                        try {
                                            blockLof = Boolean.Parse(xmlLayoutReader.GetAttribute("blocklof"));
                                        } catch {}

                                        MapWreckData wreck = new MapWreckData {
                                            position  = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("x"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("y")))),
                                            angle     = float.Parse(xmlLayoutReader.GetAttribute("angle")),
                                            resources = new List <MapResourceData>(),
                                            blockLof  = blockLof,
                                        };

                                        // Read child resources
                                        XmlReader xmlLayoutReaderWreck = xmlLayoutReader.ReadSubtree();
                                        while (xmlLayoutReaderWreck.Read())
                                        {
                                            // collectors is an optional attribute
                                            if (xmlLayoutReaderWreck.NodeType == XmlNodeType.Element && xmlLayoutReaderWreck.Name == "resource")
                                            {
                                                collectors = 2;
                                                try {
                                                    collectors = int.Parse(xmlLayoutReaderWreck.GetAttribute("collectors"));
                                                } catch {}

                                                wreck.resources.Add(new MapResourceData {
                                                    position   = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReaderWreck.GetAttribute("x"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReaderWreck.GetAttribute("y")))),
                                                    type       = (xmlLayoutReader.GetAttribute("type") == "ru") ? 1 : 0,
                                                    amount     = int.Parse(xmlLayoutReaderWreck.GetAttribute("amount")),
                                                    collectors = collectors,
                                                });
                                            }
                                        }

                                        wrecks.Add(wreck);
                                        break;

                                    case "artifact":
                                        artifacts.Add(new MapArtifactData {
                                            entity   = Entity.None,
                                            position = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("x"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("y")))),
                                        });
                                        break;

                                    case "ez":
                                        ezs.Add(new MapEzData {
                                            team     = int.Parse(xmlLayoutReader.GetAttribute("team")),
                                            position = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("x"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("y")))),
                                            radius   = float.Parse(xmlLayoutReader.GetAttribute("radius")),
                                        });
                                        break;

                                    case "spawn":
                                        // Try to get optional index
                                        int spawnIndex = 0;
                                        try {
                                            spawnIndex = int.Parse(xmlLayoutReader.GetAttribute("index"));
                                        } catch {}

                                        // Try to get optional camera angle
                                        float cameraAngle = 0;
                                        try {
                                            cameraAngle = float.Parse(xmlLayoutReader.GetAttribute("camera"));
                                        } catch {}

                                        // Try to get optional fleet toggle
                                        bool fleet = true;
                                        try {
                                            fleet = Boolean.Parse(xmlLayoutReader.GetAttribute("fleet"));
                                        } catch {}

                                        spawns.Add(new MapSpawnData {
                                            team        = int.Parse(xmlLayoutReader.GetAttribute("team")),
                                            index       = spawnIndex,
                                            position    = new Vector3(float.Parse(xmlLayoutReader.GetAttribute("x")), 0.0f, float.Parse(xmlLayoutReader.GetAttribute("y"))),
                                            angle       = float.Parse(xmlLayoutReader.GetAttribute("angle")),
                                            cameraAngle = cameraAngle,
                                            fleet       = fleet,
                                        });
                                        break;

                                    case "unit":
                                        // Try to get optional index
                                        int unitIndex = 0;
                                        try {
                                            unitIndex = int.Parse(xmlLayoutReader.GetAttribute("index"));
                                        } catch {}

                                        units.Add(new MapUnitData {
                                            team        = int.Parse(xmlLayoutReader.GetAttribute("team")),
                                            index       = unitIndex,
                                            type        = xmlLayoutReader.GetAttribute("type"),
                                            position    = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("x"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("y")))),
                                            orientation = Orientation2.FromDirection(Vector2r.Rotate(new Vector2r(Fixed64.FromConstFloat(0.0f), Fixed64.FromConstFloat(1.0f)), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("angle")) / 180.0f * -3.14159f))),
                                        });
                                        break;

                                    case "heat":
                                        heatPoints = int.Parse(xmlLayoutReader.ReadInnerXml());
                                        break;

                                    case "bounds":
                                        overrideBounds = true;
                                        boundsMax      = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("right"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("top"))));
                                        boundsMin      = new Vector2r(Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("left"))), Fixed64.FromConstFloat(float.Parse(xmlLayoutReader.GetAttribute("bottom"))));
                                        break;

                                    case "blocker":
                                        // Parse vertices
                                        List <Vector2r> vertices = new List <Vector2r>();
                                        foreach (string vert in xmlLayoutReader.GetAttribute("verts").Split(';'))
                                        {
                                            float x = float.Parse(vert.Split(',')[0]);
                                            float z = float.Parse(vert.Split(',')[1]);
                                            vertices.Add(new Vector2r(Fixed64.FromConstFloat(x), Fixed64.FromConstFloat(z)));
                                        }

                                        ConvexPolygon collider = ConvexPolygon.FromPointCloud(vertices);
                                        UnitClass     mask     = (UnitClass)Enum.Parse(typeof(UnitClass), xmlLayoutReader.GetAttribute("class"));
                                        collider.SetLayerFlag((uint)mask);
                                        bool blockAllHeights = true;
                                        try {
                                            blockAllHeights = Boolean.Parse(xmlLayoutReader.GetAttribute("blockallheights"));
                                        } catch {
                                            blockAllHeights = Boolean.Parse(xmlLayoutReader.GetAttribute("blocklof"));
                                        }
                                        colliders.Add(new MapColliderData {
                                            collider        = collider,
                                            mask            = mask,
                                            blockLof        = Boolean.Parse(xmlLayoutReader.GetAttribute("blocklof")),
                                            blockAllHeights = blockAllHeights,
                                        });
                                        break;

                                    case "blockers":
                                        try {
                                            DisableCarrierNavs = !Boolean.Parse(xmlLayoutReader.GetAttribute("carrier"));
                                        } catch {}
                                        try {
                                            DisableAllBlockers = !Boolean.Parse(xmlLayoutReader.GetAttribute("existing"));
                                        } catch {}
                                        break;

                                    default:
                                        System.IO.File.AppendAllText(logName, string.Format("[GE mod] WARNING: Unknown tag '{0}'" + Environment.NewLine, xmlLayoutReader.Name));
                                        Debug.LogWarning(string.Format("[GE mod] WARNING: Unknown tag '{0}'", xmlLayoutReader.Name));
                                        break;
                                    }
                                }
                            }

                            return(true);
                        }
                        break;
                    }
                }
            }

            return(false);
        } catch (Exception e) {
            System.IO.File.AppendAllText(logName, string.Format("[GE mod] ERROR: parsing layout: {0}" + Environment.NewLine, e));
            Debug.LogWarning(string.Format("[GE mod] ERROR: parsing layout: {0}", e));
            System.Diagnostics.Process.Start(logName);
            ResetLayout();
            MapXml     = "";
            LayoutName = "";
            return(false);
        }
    }