Exemplo n.º 1
0
        public const UInt32 headerMagic = 0x50414d58; // "XMAP"

        public void CreateNewMap(Map map, MapObjects mapObjects, MiniMap miniMap, SelectedBrush selectedBrush,
                                 List <MapItem> changedItems, Configs configs, MapLoadMode mode = MapLoadMode.All)
        {
            map.tiles       = new List <MapItem>();
            map.startTurn   = -1;
            map.startPlayer = -1;
            Random rnd = new Random();

            int players = 2; //zmienna

            for (int i = 0; i < players; i++)
            {
                map.playersInTurnOrder.Add(i); //zmienna
            }

            for (int i = 0; i < players; i++)
            {
                List <Resource> playerResources = new List <Resource>();
                bool            isAi            = false; //zmienna
                bool            useColor        = true;
                map.isAi.Add(isAi);
                map.useColor.Add(useColor);
                if (useColor)
                {
                    map.r.Add((float)rnd.NextDouble()); //zmienna
                    map.g.Add((float)rnd.NextDouble()); //zmienna
                    map.b.Add((float)rnd.NextDouble()); //zmienna
                }

                int resourceCount = 1; //zmienna
                map.resourceCount.Add(resourceCount);
                for (int j = 0; j < resourceCount; j++)
                {
                    playerResources.Add(new Resource("gold", 12000)); //zmienna
                }
                map.resourcesList.Add(playerResources);
            }

            // Prefab palette
            int           palletteSize = 0;
            List <string> palette      = new List <string>();

            foreach (Sprite sprite in mapObjects.terrains)
            {
                for (int j = 0; j < sprite.imagesList.Count; j++)
                {
                    sprite.setName = Char.ToLowerInvariant(sprite.setName[0]) + sprite.setName.Substring(1);
                    string spriteName = sprite.setName + "_" + j;
                    //palette.Add(spriteName);
                    map.prefabPath.Add(spriteName);
                    palletteSize++;
                }
            }

            foreach (Sprite sprite in mapObjects.roads)
            {
                for (int j = 0; j < sprite.imagesList.Count; j++)
                {
                    sprite.setName = Char.ToLowerInvariant(sprite.setName[0]) + sprite.setName.Substring(1);
                    string spriteName = sprite.setName + "_" + j;
                    //palette.Add(spriteName);
                    map.prefabPath.Add(spriteName);
                    palletteSize++;
                }
            }
            map.paletteSize = palletteSize;


            // Tiles
            for (int i = 0; i < map.rows; i++)
            {
                for (int j = 0; j < map.columns; j++)
                {
                    map.tiles.Add(new MapItem(1, 1, mapObjects.terrains[1].setName, mapObjects.terrains[1].category, mapObjects, j, i, miniMap, selectedBrush, changedItems, map));
                }
            }

            // Overlay tiles
            map.overlayTilesCount = 0;
        }
Exemplo n.º 2
0
        public Map LoadMapFromBytes(MapObjects mapObjects, string path, MiniMap miniMap, SelectedBrush selectedBrush, List <MapItem> changedItems, Configs configs, MapLoadMode mode = MapLoadMode.All)
        {
            byte[] serializedMap = File.ReadAllBytes(path);

            Map map = new Map();

            using (BinaryReader reader = new BinaryReader(new MemoryStream(serializedMap)))
            {
                // Header & metadata
                if (reader.ReadUInt32() != headerMagic)
                {
                    throw new IOException("Invalid map header");
                }

                map.columns = reader.ReadInt32();
                map.rows    = reader.ReadInt32();
                if (map.columns <= 0 || map.rows <= 0)
                {
                    throw new IOException("Invalid rows/columns values");
                }
                map.tiles       = new List <MapItem>();
                map.name        = reader.ReadString();
                map.description = reader.ReadString();
                map.startTurn   = reader.ReadInt32();
                map.startPlayer = reader.ReadInt32();

                int players = reader.ReadInt32();
                for (int i = 0; i < players; i++)
                {
                    map.playersInTurnOrder.Add(reader.ReadInt32());
                }

                for (int i = 0; i < players; i++)
                {
                    List <Resource> playerResources = new List <Resource>();
                    bool            isAi            = reader.ReadBoolean();
                    bool            useColor        = reader.ReadBoolean();
                    map.isAi.Add(isAi);
                    map.useColor.Add(useColor);
                    if (useColor)
                    {
                        map.r.Add(reader.ReadSingle());
                        map.g.Add(reader.ReadSingle());
                        map.b.Add(reader.ReadSingle());
                    }

                    int resourceCount = reader.ReadInt32();
                    map.resourceCount.Add(resourceCount);
                    for (int j = 0; j < resourceCount; j++)
                    {
                        playerResources.Add(new Resource(reader.ReadString(), reader.ReadInt32()));
                    }
                    map.resourcesList.Add(playerResources);
                }

                // Prefab palette
                map.paletteSize = reader.ReadInt16();
                List <string> palette = new List <string>();
                if (map.paletteSize <= 0)
                {
                    throw new IOException("Palette is empty");
                }
                Regex regex = new Regex(@"^[a-zA-Z0-9_]+$");
                for (int i = 0; i < map.paletteSize; i++)
                {
                    string prefabPath = reader.ReadString();
                    map.prefabPath.Add(prefabPath);
                    if (!regex.IsMatch(prefabPath))
                    {
                        throw new IOException("Invalid prefab path: " + prefabPath);
                    }
                    palette.Add(prefabPath);
                }

                if (!map.validate(mapObjects, configs))
                {
                    return(null);
                }

                // Tiles
                for (int i = 0; i < map.rows; i++)
                {
                    for (int j = 0; j < map.columns; j++)
                    {
                        int prefabId  = reader.ReadInt16();
                        int setIndex  = 0;
                        int itemIndex = 0;
                        if (prefabId < 0 || prefabId >= map.paletteSize)
                        {
                            throw new IOException("Invalid prefab ID");
                        }
                        setIndex  = mapObjects.terrains.FindIndex(t => t.setName.ToLower() == palette[prefabId].Split('_')[0]);
                        itemIndex = Int16.Parse(palette[prefabId].Split('_')[1]);

                        map.tiles.Add(new MapItem(itemIndex, setIndex, mapObjects.terrains[setIndex].setName, mapObjects.terrains[setIndex].category, mapObjects, j, i, miniMap, selectedBrush, changedItems, map));
                    }
                }

                // Overlay tiles
                map.overlayTilesCount = reader.ReadInt32();
                for (int i = 0; i < map.overlayTilesCount; i++)
                {
                    map.overlayTilesX.Add(reader.ReadInt32());
                    map.overlayTilesY.Add(reader.ReadInt32());
                    map.overlayTilesPrefabId.Add(reader.ReadInt32());

                    int migrationIndex = mapObjects.terrains.FindIndex(t => t.setName.ToLower() == palette[map.overlayTilesPrefabId[i]].Split('_')[0]);
                    if (migrationIndex > -1)
                    {
                        mapObjects.roads.Add(mapObjects.terrains[migrationIndex]);
                        mapObjects.terrains.RemoveAt(migrationIndex);
                    }

                    int tileID = map.tiles.FindIndex(t => (t.Xcoordinate == map.overlayTilesX[i]) && (t.Ycoordinate == map.overlayTilesY[i]));
                    map.tiles[tileID].objectCategory = "Roads";
                    map.tiles[tileID].objectSet      = mapObjects.roads.FindIndex(t => t.setName.ToLower() == palette[map.overlayTilesPrefabId[i]].Split('_')[0]);
                    map.tiles[tileID].objectName     = palette[map.overlayTilesPrefabId[i]].Split('_')[0];
                    map.tiles[tileID].objectIndex    = Int16.Parse(palette[map.overlayTilesPrefabId[i]].Split('_')[1]);
                    map.tiles[tileID].combineImages();
                }

                // Castles
                int castleCount = reader.ReadInt32();
                for (int i = 0; i < castleCount; i++)
                {
                    string prefabName = reader.ReadString();
                    string castleName = reader.ReadString();
                    int    x          = reader.ReadInt32();
                    int    y          = reader.ReadInt32();
                    int    owner      = reader.ReadInt32();
                    bool   razed      = reader.ReadBoolean();
                    int    buildings  = reader.ReadInt32();

                    HashSet <string> purchasedBuildings = new HashSet <string>();
                    for (int j = 0; j < buildings; j++)
                    {
                        purchasedBuildings.Add(reader.ReadString());
                    }

                    string producedUnitName        = reader.ReadString();
                    int    producedUnitTurnsLeft   = reader.ReadInt32();
                    string producedUnitDestination = reader.ReadString();

                    int            queuedUnits = reader.ReadInt32();
                    Queue <string> unitQueue   = new Queue <string>();
                    for (int j = 0; j < queuedUnits; j++)
                    {
                        unitQueue.Enqueue(reader.ReadString());
                    }

                    map.castles.Add(new CastleInfo(prefabName, castleName, x, y, owner, razed, purchasedBuildings,
                                                   producedUnitName, producedUnitTurnsLeft, producedUnitDestination, unitQueue));

                    int tileID      = map.tiles.FindIndex(t => (t.Xcoordinate == map.castles[i].x) && (t.Ycoordinate == map.castles[i].y));
                    int objectIndex = configs.fractions.FindIndex(o => o.name == map.castles[i].prefabName);
                    map.tiles[tileID].objectCategory = "Castles";
                    map.tiles[tileID].objectSet      = 0;
                    map.tiles[tileID].objectIndex    = objectIndex;
                    map.tiles[tileID].castleName     = map.castles[i].castleName;
                    map.tiles[tileID].castleOwner    = map.castles[i].owner;
                    map.tiles[tileID].combineImages();
                }

                // Ruins
                int ruinCount = reader.ReadInt32();
                for (int i = 0; i < ruinCount; i++)
                {
                    string prefabName = reader.ReadString();
                    int    x          = reader.ReadInt32();
                    int    y          = reader.ReadInt32();
                    bool   isVisited  = reader.ReadBoolean();

                    map.ruins.Add(new RuinsInfo(prefabName, x, y, isVisited));

                    int tileID    = map.tiles.FindIndex(t => (t.Xcoordinate == map.ruins[i].x) && (t.Ycoordinate == map.ruins[i].y));
                    int objectSet = configs.ruinsData.FindIndex(o => o.name == map.ruins[i].prefabName);

                    map.tiles[tileID].objectCategory = "Ruins";
                    map.tiles[tileID].objectSet      = objectSet;
                    map.tiles[tileID].objectIndex    = new Random().Next(configs.ruinsData[objectSet].sprites.Count);
                    map.tiles[tileID].combineImages();
                }

                // Units
                int unitContainers = reader.ReadInt32();
                for (int i = 0; i < unitContainers; i++)
                {
                    int x         = reader.ReadInt32();
                    int y         = reader.ReadInt32();
                    int owner     = reader.ReadInt32();
                    int unitCount = reader.ReadInt32();

                    List <UnitInfo> unitInfos = new List <UnitInfo>();
                    for (int j = 0; j < unitCount; j++)
                    {
                        string     unitName        = reader.ReadString();
                        string     unitDisplayName = reader.ReadString();
                        float      range           = reader.ReadSingle();
                        int        experience      = reader.ReadInt32();
                        int        level           = reader.ReadInt32();
                        bool       overrideStats   = reader.ReadBoolean();
                        Statistics stats           = new Statistics();
                        if (overrideStats)
                        {
                            stats.attack        = reader.ReadSingle();
                            stats.defense       = reader.ReadSingle();
                            stats.power         = reader.ReadSingle();
                            stats.wisdom        = reader.ReadSingle();
                            stats.hp            = reader.ReadSingle();
                            stats.speed         = reader.ReadSingle();
                            stats.damageMin     = reader.ReadSingle();
                            stats.damageMax     = reader.ReadSingle();
                            stats.timeToProduce = reader.ReadSingle();
                            stats.canShoot      = reader.ReadBoolean();
                            stats.canFly        = reader.ReadBoolean();
                            stats.range         = reader.ReadSingle();
                        }

                        // Modifiers
                        StatsModifiers modifiers     = new StatsModifiers();
                        int            modifierCount = reader.ReadInt32();
                        for (int k = 0; k < modifierCount; k++)
                        {
                            string statType         = reader.ReadString();
                            int    modifierSubCount = reader.ReadInt32();
                            for (int m = 0; m < modifierSubCount; m++)
                            {
                                modifiers.addModifier(statType, new StatsModifiersEntry(
                                                          reader.ReadString(),
                                                          reader.ReadSingle(),
                                                          (StatsModifiersEntry.DurationType)reader.ReadInt32(),
                                                          reader.ReadInt32(),
                                                          (StatsModifiersEntry.ModifierType)reader.ReadInt32()
                                                          ));
                            }
                        }

                        unitInfos.Add(new UnitInfo(unitName, unitDisplayName, range, experience, level, stats, overrideStats, modifiers));
                    }

                    map.units.Add(new UnitContainerInfo(x, y, owner, unitInfos));
                }
            }

            return(map);
        }