public override void Save(XElement element)
        {
            XElement modeElement = new XElement("SinglePlayerCampaign",
                                                new XAttribute("money", Money),
                                                new XAttribute("cheatsenabled", CheatsEnabled));

            //save and remove all items that are in someone's inventory so they don't get included in the sub file as well
            foreach (Character c in Character.CharacterList)
            {
                if (c.Info == null)
                {
                    continue;
                }
                if (c.IsDead)
                {
                    CrewManager.RemoveCharacterInfo(c.Info);
                }
                c.Info.LastControlled = c == lastControlledCharacter;
                c.Info.HealthData     = new XElement("health");
                c.CharacterHealth.Save(c.Info.HealthData);
                if (c.Inventory != null)
                {
                    c.Info.InventoryData = new XElement("inventory");
                    c.SaveInventory(c.Inventory, c.Info.InventoryData);
                    c.Inventory?.DeleteAllItems();
                }
            }

            CrewManager.Save(modeElement);
            CampaignMetadata.Save(modeElement);
            Map.Save(modeElement);
            CargoManager?.SavePurchasedItems(modeElement);
            UpgradeManager?.SavePendingUpgrades(modeElement, UpgradeManager?.PendingUpgrades);
            element.Add(modeElement);
        }
        public static void PerformOperation(CampaignMetadata metadata, string identifier, object value, OperationType operation)
        {
            if (metadata == null)
            {
                return;
            }

            object currentValue = metadata.GetValue(identifier);

            float?originalValue = ConvertValueToFloat(currentValue ?? 0);
            float?newValue      = ConvertValueToFloat(value);

            if ((originalValue == null || newValue == null) && operation != OperationType.Set)
            {
                DebugConsole.ThrowError($"Tried to perform numeric operations to a non number via SetDataAction (Existing: {currentValue?.GetType()}, New: {value.GetType()})");
                return;
            }

            switch (operation)
            {
            case OperationType.Set:
                metadata.SetValue(identifier, value);
                break;

            case OperationType.Add:
                metadata.SetValue(identifier, originalValue + newValue ?? 0);
                break;

            case OperationType.Multiply:
                metadata.SetValue(identifier, originalValue * newValue ?? 0);
                break;
            }
        }
        /// <summary>
        /// Loads a previously saved single player campaign from XML
        /// </summary>
        private SinglePlayerCampaign(XElement element) : base(GameModePreset.SinglePlayerCampaign)
        {
            IsFirstRound = false;

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "crew":
                    GameMain.GameSession.CrewManager = new CrewManager(subElement, true);
                    break;

                case "map":
                    map = Map.Load(this, subElement);
                    break;

                case "metadata":
                    CampaignMetadata = new CampaignMetadata(this, subElement);
                    break;

                case "cargo":
                    CargoManager.LoadPurchasedItems(subElement);
                    break;

                case "pendingupgrades":
                    UpgradeManager = new UpgradeManager(this, subElement, isSingleplayer: true);
                    break;
                }
            }

            CampaignMetadata ??= new CampaignMetadata(this);

            UpgradeManager ??= new UpgradeManager(this);

            InitCampaignData();

            InitUI();

            Money         = element.GetAttributeInt("money", 0);
            CheatsEnabled = element.GetAttributeBool("cheatsenabled", false);
            if (CheatsEnabled)
            {
                DebugConsole.CheatsEnabled = true;
#if USE_STEAM
                if (!SteamAchievementManager.CheatsEnabled)
                {
                    SteamAchievementManager.CheatsEnabled = true;
                    new GUIMessageBox("Cheats enabled", "Cheat commands have been enabled on the campaign. You will not receive Steam Achievements until you restart the game.");
                }
#endif
            }

            if (map == null)
            {
                throw new System.Exception("Failed to load the campaign save file (saved with an older, incompatible version of Barotrauma).");
            }

            savedOnStart = true;
        }
Ejemplo n.º 4
0
 private MultiPlayerCampaign() : base(GameModePreset.MultiPlayerCampaign)
 {
     currentCampaignID++;
     CampaignID       = currentCampaignID;
     CampaignMetadata = new CampaignMetadata(this);
     UpgradeManager   = new UpgradeManager(this);
     InitCampaignData();
 }
        public override void Save(XElement element)
        {
            element.Add(new XAttribute("campaignid", CampaignID));
            XElement modeElement = new XElement("MultiPlayerCampaign",
                                                new XAttribute("money", Money),
                                                new XAttribute("purchasedlostshuttles", PurchasedLostShuttles),
                                                new XAttribute("purchasedhullrepairs", PurchasedHullRepairs),
                                                new XAttribute("purchaseditemrepairs", PurchasedItemRepairs),
                                                new XAttribute("cheatsenabled", CheatsEnabled));

            CampaignMetadata?.Save(modeElement);
            Map.Save(modeElement);
            CargoManager?.SavePurchasedItems(modeElement);
            UpgradeManager?.SavePendingUpgrades(modeElement, UpgradeManager?.PendingUpgrades);

            if (petsElement != null)
            {
                modeElement.Add(petsElement);
            }

            // save bots
            CrewManager.SaveMultiplayer(modeElement);

            // save available submarines
            XElement availableSubsElement = new XElement("AvailableSubs");

            for (int i = 0; i < GameMain.NetLobbyScreen.CampaignSubmarines.Count; i++)
            {
                availableSubsElement.Add(new XElement("Sub", new XAttribute("name", GameMain.NetLobbyScreen.CampaignSubmarines[i].Name)));
            }
            modeElement.Add(availableSubsElement);

            element.Add(modeElement);

            //save character data to a separate file
            string    characterDataPath = GetCharacterDataSavePath();
            XDocument characterDataDoc  = new XDocument(new XElement("CharacterData"));

            foreach (CharacterCampaignData cd in characterData)
            {
                characterDataDoc.Root.Add(cd.Save());
            }
            try
            {
                characterDataDoc.SaveSafe(characterDataPath);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Saving multiplayer campaign characters to \"" + characterDataPath + "\" failed!", e);
            }

            lastSaveID++;
            DebugConsole.Log("Campaign saved, save ID " + lastSaveID);
        }
 /// <summary>
 /// Instantiates a new single player campaign
 /// </summary>
 private SinglePlayerCampaign(string mapSeed) : base(GameModePreset.SinglePlayerCampaign)
 {
     CampaignMetadata = new CampaignMetadata(this);
     UpgradeManager   = new UpgradeManager(this);
     map = new Map(this, mapSeed);
     foreach (JobPrefab jobPrefab in JobPrefab.Prefabs)
     {
         for (int i = 0; i < jobPrefab.InitialCount; i++)
         {
             var variant = Rand.Range(0, jobPrefab.Variants);
             CrewManager.AddCharacterInfo(new CharacterInfo(CharacterPrefab.HumanSpeciesName, jobPrefab: jobPrefab, variant: variant));
         }
     }
     InitCampaignData();
     InitUI();
 }
Ejemplo n.º 7
0
        public override void Save(XElement element)
        {
            XElement modeElement = new XElement("SinglePlayerCampaign",
                                                new XAttribute("money", Money),
                                                new XAttribute("purchasedlostshuttles", PurchasedLostShuttles),
                                                new XAttribute("purchasedhullrepairs", PurchasedHullRepairs),
                                                new XAttribute("purchaseditemrepairs", PurchasedItemRepairs),
                                                new XAttribute("cheatsenabled", CheatsEnabled));

            modeElement.Add(Settings.Save());

            //save and remove all items that are in someone's inventory so they don't get included in the sub file as well
            foreach (Character c in Character.CharacterList)
            {
                if (c.Info == null)
                {
                    continue;
                }
                if (c.IsDead)
                {
                    CrewManager.RemoveCharacterInfo(c.Info);
                }
                c.Info.LastControlled = c == lastControlledCharacter;
                c.Info.HealthData     = new XElement("health");
                c.CharacterHealth.Save(c.Info.HealthData);
                if (c.Inventory != null)
                {
                    c.Info.InventoryData = new XElement("inventory");
                    c.SaveInventory();
                    c.Inventory?.DeleteAllItems();
                }
                c.Info.SaveOrderData();
            }

            petsElement = new XElement("pets");
            PetBehavior.SavePets(petsElement);
            modeElement.Add(petsElement);

            CrewManager.Save(modeElement);
            CampaignMetadata.Save(modeElement);
            Map.Save(modeElement);
            CargoManager?.SavePurchasedItems(modeElement);
            UpgradeManager?.Save(modeElement);
            element.Add(modeElement);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Loads the campaign from an XML element. Creates the map if it hasn't been created yet, otherwise updates the state of the map.
        /// </summary>
        private void Load(XElement element)
        {
            Money = element.GetAttributeInt("money", 0);
            PurchasedLostShuttles = element.GetAttributeBool("purchasedlostshuttles", false);
            PurchasedHullRepairs  = element.GetAttributeBool("purchasedhullrepairs", false);
            PurchasedItemRepairs  = element.GetAttributeBool("purchaseditemrepairs", false);
            CheatsEnabled         = element.GetAttributeBool("cheatsenabled", false);
            if (CheatsEnabled)
            {
                DebugConsole.CheatsEnabled = true;
#if USE_STEAM
                if (!SteamAchievementManager.CheatsEnabled)
                {
                    SteamAchievementManager.CheatsEnabled = true;
#if CLIENT
                    new GUIMessageBox("Cheats enabled", "Cheat commands have been enabled on the server. You will not receive Steam Achievements until you restart the game.");
#else
                    DebugConsole.NewMessage("Cheat commands have been enabled.", Color.Red);
#endif
                }
#endif
            }

#if SERVER
            List <SubmarineInfo> availableSubs = new List <SubmarineInfo>();
            List <SubmarineInfo> sourceList    = new List <SubmarineInfo>();
            sourceList.AddRange(SubmarineInfo.SavedSubmarines);
#endif

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "campaignsettings":
                    Settings = new CampaignSettings(subElement);
                    break;

                case "map":
                    if (map == null)
                    {
                        //map not created yet, loading this campaign for the first time
                        map = Map.Load(this, subElement, Settings);
                    }
                    else
                    {
                        //map already created, update it
                        //if we're not downloading the initial save file (LastSaveID > 0),
                        //show notifications about location type changes
                        map.LoadState(subElement, LastSaveID > 0);
                    }
                    break;

                case "metadata":
                    CampaignMetadata = new CampaignMetadata(this, subElement);
                    break;

                case "upgrademanager":
                case "pendingupgrades":
                    UpgradeManager = new UpgradeManager(this, subElement, isSingleplayer: false);
                    break;

                case "bots" when GameMain.NetworkMember != null && GameMain.NetworkMember.IsServer:
                    CrewManager.HasBots = subElement.GetAttributeBool("hasbots", false);
                    CrewManager.AddCharacterElements(subElement);
                    CrewManager.ActiveOrdersElement = subElement.GetChildElement("activeorders");
                    break;

                case "cargo":
                    CargoManager?.LoadPurchasedItems(subElement);
                    break;

                case "pets":
                    petsElement = subElement;
                    break;

#if SERVER
                case "availablesubs":
                    foreach (XElement availableSub in subElement.Elements())
                    {
                        string        subName     = availableSub.GetAttributeString("name", "");
                        SubmarineInfo matchingSub = sourceList.Find(s => s.Name == subName);
                        if (matchingSub != null)
                        {
                            availableSubs.Add(matchingSub);
                        }
                    }
                    break;
#endif
                }
            }

            CampaignMetadata ??= new CampaignMetadata(this);
            UpgradeManager ??= new UpgradeManager(this);

            InitCampaignData();
#if SERVER
            // Fallback if using a save with no available subs assigned, use vanilla submarines
            if (availableSubs.Count == 0)
            {
                GameMain.NetLobbyScreen.CampaignSubmarines.AddRange(sourceList.FindAll(s => s.IsCampaignCompatible && s.IsVanillaSubmarine()));
            }

            GameMain.NetLobbyScreen.CampaignSubmarines = availableSubs;

            characterData.Clear();
            string characterDataPath = GetCharacterDataSavePath();
            if (!File.Exists(characterDataPath))
            {
                DebugConsole.ThrowError($"Failed to load the character data for the campaign. Could not find the file \"{characterDataPath}\".");
            }
            else
            {
                var characterDataDoc = XMLExtensions.TryLoadXml(characterDataPath);
                if (characterDataDoc?.Root == null)
                {
                    return;
                }
                foreach (XElement subElement in characterDataDoc.Root.Elements())
                {
                    characterData.Add(new CharacterCampaignData(subElement));
                }
            }
#endif
        }
Ejemplo n.º 9
0
 public Faction(CampaignMetadata metadata, FactionPrefab prefab)
 {
     Prefab     = prefab;
     Reputation = new Reputation(metadata, $"faction.{prefab.Identifier}", prefab.MinReputation, prefab.MaxReputation, prefab.InitialReputation);
 }