Esempio n. 1
0
        public static void LoadGame(string sveFile, string mapFile)
        {
            if (_instance != null)
            {
                Log("ERROR: Game instance already exists");
                return;
            }

            using (IGameData adapter = SaveDataAdapter.Load(File.ReadAllBytes(sveFile)))
            {
                if (!adapter.ValidData)
                {
                    Log("SaveDataAdapter failed to load game");
                    return;
                }

                Map.Instance.LoadMap(mapFile, adapter.RandomSeed);
                _instance = new Game(adapter);
                Log($"Game instance loaded (difficulty: {_instance._difficulty}, competition: {_instance._competition}");
            }
        }
Esempio n. 2
0
 public void Save(string sveFile, string mapFile)
 {
     using (IGameData gameData = new SaveDataAdapter())
     {
         gameData.GameTurn             = _gameTurn;
         gameData.HumanPlayer          = (ushort)PlayerNumber(HumanPlayer);
         gameData.RandomSeed           = Map.Instance.SaveMap(mapFile);
         gameData.Difficulty           = (ushort)_difficulty;
         gameData.ActiveCivilizations  = _players.Select(x => (x.Civilization is Barbarian) || (x.Cities.Any(c => c.Size > 0) || GetUnits().Any(u => x == u.Owner))).ToArray();
         gameData.CivilizationIdentity = _players.Select(x => (byte)(x.Civilization.Id > 7 ? 1 : 0)).ToArray();
         gameData.CurrentResearch      = HumanPlayer.CurrentResearch?.Id ?? 0;
         byte[][] discoveredAdvanceIDs = new byte[_players.Length][];
         for (int p = 0; p < _players.Length; p++)
         {
             discoveredAdvanceIDs[p] = _players[p].Advances.Select(x => x.Id).ToArray();
         }
         gameData.DiscoveredAdvanceIDs = discoveredAdvanceIDs;
         gameData.LeaderNames          = _players.Select(x => x.LeaderName).ToArray();
         gameData.CivilizationNames    = _players.Select(x => x.TribeNamePlural).ToArray();
         gameData.CitizenNames         = _players.Select(x => x.TribeName).ToArray();
         gameData.CityNames            = CityNames;
         gameData.PlayerGold           = _players.Select(x => x.Gold).ToArray();
         gameData.ResearchProgress     = _players.Select(x => x.Science).ToArray();
         gameData.TaxRate           = _players.Select(x => (ushort)x.TaxesRate).ToArray();
         gameData.ScienceRate       = _players.Select(x => (ushort)(10 - x.ScienceRate - x.TaxesRate)).ToArray();
         gameData.StartingPositionX = _players.Select(x => (ushort)x.StartX).ToArray();
         gameData.Government        = _players.Select(x => (ushort)x.Government.Id).ToArray();
         gameData.Cities            = _cities.GetCityData().ToArray();
         gameData.Units             = _players.Select(p => _units.Where(u => p == u.Owner).GetUnitData().ToArray()).ToArray();
         ushort[] wonders = Enumerable.Repeat(ushort.MaxValue, 22).ToArray();
         for (byte i = 0; i < _cities.Count(); i++)
         {
             foreach (IWonder wonder in _cities[i].Wonders)
             {
                 wonders[wonder.Id] = i;
             }
         }
         gameData.Wonders     = wonders;
         bool[][,] visibility = new bool[_players.Length][, ];
         for (int p = 0; p < visibility.Length; p++)
         {
             visibility[p] = new bool[80, 50];
             for (int xx = 0; xx < 80; xx++)
             {
                 for (int yy = 0; yy < 50; yy++)
                 {
                     if (!_players[p].Visible(xx, yy))
                     {
                         continue;
                     }
                     visibility[p][xx, yy] = true;
                 }
             }
         }
         gameData.TileVisibility = visibility;
         ushort[] firstDiscovery = new ushort[72];
         foreach (byte key in _advanceOrigin.Keys)
         {
             firstDiscovery[key] = _advanceOrigin[key];
         }
         gameData.AdvanceFirstDiscovery = firstDiscovery;
         gameData.GameOptions           = new bool[]
         {
             Settings.InstantAdvice,
             Settings.AutoSave,
             Settings.EndOfTurn,
             Settings.Animations,
             Settings.Sound,
             Settings.EnemyMoves,
             Settings.CivilopediaText,
             // Settings.Palace
         };
         gameData.NextAnthologyTurn = _anthologyTurn;
         gameData.OpponentCount     = (ushort)(_players.Length - 2);
         gameData.ReplayData        = _replayData.ToArray();
         File.WriteAllBytes(sveFile, gameData.GetBytes());
     }
 }