(61d00a474) v0.9.7.1

This commit is contained in:
Regalis
2020-03-04 13:04:10 +01:00
parent 3c50efa5c9
commit 3c09ebe02f
5086 changed files with 786063 additions and 295871 deletions
@@ -0,0 +1,253 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma
{
abstract partial class CampaignMode : GameMode
{
public readonly CargoManager CargoManager;
public bool CheatsEnabled;
const int InitialMoney = 8700;
public const int HullRepairCost = 500, ItemRepairCost = 500, ShuttleReplaceCost = 1000;
protected bool watchmenSpawned;
protected Character startWatchman, endWatchman;
//key = dialog flag, double = Timing.TotalTime when the line was last said
private Dictionary<string, double> dialogLastSpoken = new Dictionary<string, double>();
public bool PurchasedHullRepairs, PurchasedLostShuttles, PurchasedItemRepairs;
public bool InitialSuppliesSpawned;
protected Map map;
public Map Map
{
get { return map; }
}
public override Mission Mission
{
get
{
return Map.CurrentLocation?.SelectedMission;
}
}
private int money;
public int Money
{
get { return money; }
set { money = Math.Max(value, 0); }
}
public CampaignMode(GameModePreset preset, object param)
: base(preset, param)
{
Money = InitialMoney;
CargoManager = new CargoManager(this);
}
public void GenerateMap(string seed)
{
map = new Map(seed);
}
protected List<Submarine> GetSubsToLeaveBehind(Submarine leavingSub)
{
//leave subs behind if they're not docked to the leaving sub and not at the same exit
return Submarine.Loaded.FindAll(s =>
s != leavingSub &&
!leavingSub.DockedTo.Contains(s) &&
s != Level.Loaded.StartOutpost && s != Level.Loaded.EndOutpost &&
(s.AtEndPosition != leavingSub.AtEndPosition || s.AtStartPosition != leavingSub.AtStartPosition));
}
public override void Start()
{
base.Start();
dialogLastSpoken.Clear();
watchmenSpawned = false;
startWatchman = null;
endWatchman = null;
if (PurchasedHullRepairs)
{
foreach (Structure wall in Structure.WallList)
{
if (wall.Submarine == null || wall.Submarine.IsOutpost) { continue; }
if (wall.Submarine == Submarine.MainSub || Submarine.MainSub.DockedTo.Contains(wall.Submarine))
{
for (int i = 0; i < wall.SectionCount; i++)
{
wall.AddDamage(i, -wall.Prefab.Health);
}
}
}
PurchasedHullRepairs = false;
}
if (PurchasedItemRepairs)
{
foreach (Item item in Item.ItemList)
{
if (item.Submarine == null || item.Submarine.IsOutpost) { continue; }
if (item.Submarine == Submarine.MainSub || Submarine.MainSub.DockedTo.Contains(item.Submarine))
{
if (item.GetComponent<Items.Components.Repairable>() != null)
{
item.Condition = item.Prefab.Health;
}
}
}
PurchasedItemRepairs = false;
}
PurchasedLostShuttles = false;
}
public override void Update(float deltaTime)
{
base.Update(deltaTime);
if (!IsRunning) { return; }
if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient) { return; }
if (!watchmenSpawned)
{
if (Level.Loaded.StartOutpost != null) { startWatchman = SpawnWatchman(Level.Loaded.StartOutpost); }
if (Level.Loaded.EndOutpost != null) { endWatchman = SpawnWatchman(Level.Loaded.EndOutpost); }
watchmenSpawned = true;
#if SERVER
(this as MultiPlayerCampaign).LastUpdateID++;
#endif
}
else
{
foreach (Character character in Character.CharacterList)
{
#if SERVER
if (string.IsNullOrEmpty(character.OwnerClientEndPoint)) { continue; }
#else
if (!CrewManager.GetCharacters().Contains(character)) { continue; }
#endif
if (character.Submarine == Level.Loaded.StartOutpost &&
Vector2.DistanceSquared(character.WorldPosition, startWatchman.WorldPosition) < 500.0f * 500.0f)
{
CreateDialog(new List<Character> { startWatchman }, "EnterStartOutpost", 5 * 60.0f);
}
else if (character.Submarine == Level.Loaded.EndOutpost &&
Vector2.DistanceSquared(character.WorldPosition, endWatchman.WorldPosition) < 500.0f * 500.0f)
{
CreateDialog(new List<Character> { endWatchman }, "EnterEndOutpost", 5 * 60.0f);
}
}
}
}
protected void CreateDialog(List<Character> speakers, string conversationTag, float minInterval)
{
if (dialogLastSpoken.TryGetValue(conversationTag, out double lastTime))
{
if (Timing.TotalTime - lastTime < minInterval) { return; }
}
CrewManager.AddConversation(
NPCConversation.CreateRandom(speakers, new List<string>() { conversationTag }));
dialogLastSpoken[conversationTag] = Timing.TotalTime;
}
private Character SpawnWatchman(Submarine outpost)
{
WayPoint watchmanSpawnpoint = WayPoint.WayPointList.Find(wp => wp.Submarine == outpost);
if (watchmanSpawnpoint == null)
{
DebugConsole.ThrowError("Failed to spawn a watchman at the outpost. No spawnpoints found inside the outpost.");
return null;
}
string seed = outpost == Level.Loaded.StartOutpost ? map.SelectedLocation.Name : map.CurrentLocation.Name;
Rand.SetSyncedSeed(ToolBox.StringToInt(seed));
JobPrefab watchmanJob = JobPrefab.Get("watchman");
var variant = Rand.Range(0, watchmanJob.Variants, Rand.RandSync.Server);
CharacterInfo characterInfo = new CharacterInfo(CharacterPrefab.HumanSpeciesName, jobPrefab: watchmanJob, variant: variant);
var spawnedCharacter = Character.Create(characterInfo, watchmanSpawnpoint.WorldPosition,
Level.Loaded.Seed + (outpost == Level.Loaded.StartOutpost ? "start" : "end"));
InitializeWatchman(spawnedCharacter);
var objectiveManager = (spawnedCharacter.AIController as HumanAIController)?.ObjectiveManager;
if (objectiveManager != null)
{
var moveOrder = new AIObjectiveGoTo(watchmanSpawnpoint, spawnedCharacter, objectiveManager, repeat: true, getDivingGearIfNeeded: false);
moveOrder.Completed += () =>
{
// Turn towards the center of the sub. Doesn't work in all possible cases, but this is the simplest solution for now.
spawnedCharacter.AnimController.TargetDir = spawnedCharacter.Submarine.WorldPosition.X > spawnedCharacter.WorldPosition.X ? Direction.Right : Direction.Left;
};
objectiveManager.SetOrder(moveOrder);
}
if (watchmanJob != null)
{
spawnedCharacter.GiveJobItems();
}
return spawnedCharacter;
}
protected void InitializeWatchman(Character character)
{
character.CharacterHealth.UseHealthWindow = false;
character.CharacterHealth.Unkillable = true;
character.CanInventoryBeAccessed = false;
character.CanBeDragged = false;
character.TeamID = Character.TeamType.FriendlyNPC;
character.SetCustomInteract(
WatchmanInteract,
#if CLIENT
hudText: TextManager.GetWithVariable("TalkHint", "[key]", GameMain.Config.KeyBindText(InputType.Select)));
#else
hudText: TextManager.Get("TalkHint"));
#endif
}
protected abstract void WatchmanInteract(Character watchman, Character interactor);
public abstract void Save(XElement element);
public void LogState()
{
DebugConsole.NewMessage("********* CAMPAIGN STATUS *********", Color.White);
DebugConsole.NewMessage(" Money: " + Money, Color.White);
DebugConsole.NewMessage(" Current location: " + map.CurrentLocation.Name, Color.White);
DebugConsole.NewMessage(" Available destinations: ", Color.White);
for (int i = 0; i < map.CurrentLocation.Connections.Count; i++)
{
Location destination = map.CurrentLocation.Connections[i].OtherLocation(map.CurrentLocation);
if (destination == map.SelectedLocation)
{
DebugConsole.NewMessage(" " + i + ". " + destination.Name + " [SELECTED]", Color.White);
}
else
{
DebugConsole.NewMessage(" " + i + ". " + destination.Name, Color.White);
}
}
if (map.CurrentLocation?.SelectedMission != null)
{
DebugConsole.NewMessage(" Selected mission: " + map.CurrentLocation.SelectedMission.Name, Color.White);
DebugConsole.NewMessage("\n" + map.CurrentLocation.SelectedMission.Description, Color.White);
}
}
public override void Remove()
{
base.Remove();
map?.Remove();
map = null;
}
}
}
@@ -0,0 +1,85 @@
using Barotrauma.Networking;
using System.Xml.Linq;
namespace Barotrauma
{
partial class CharacterCampaignData
{
public CharacterInfo CharacterInfo
{
get;
private set;
}
public readonly string Name;
public string ClientEndPoint
{
get;
private set;
}
public ulong SteamID
{
get;
private set;
}
private XElement itemData;
partial void InitProjSpecific(Client client);
public CharacterCampaignData(Client client)
{
Name = client.Name;
InitProjSpecific(client);
if (client.Character.Inventory != null)
{
itemData = new XElement("inventory");
client.Character.SaveInventory(client.Character.Inventory, itemData);
}
}
public CharacterCampaignData(XElement element)
{
Name = element.GetAttributeString("name", "Unnamed");
ClientEndPoint = element.GetAttributeString("endpoint", null) ?? element.GetAttributeString("ip", "");
string steamID = element.GetAttributeString("steamid", "");
if (!string.IsNullOrEmpty(steamID))
{
ulong.TryParse(steamID, out ulong parsedID);
SteamID = parsedID;
}
foreach (XElement subElement in element.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "character":
case "characterinfo":
CharacterInfo = new CharacterInfo(subElement);
break;
case "inventory":
itemData = subElement;
break;
}
}
}
public XElement Save()
{
XElement element = new XElement("CharacterCampaignData",
new XAttribute("name", Name),
new XAttribute("endpoint", ClientEndPoint),
new XAttribute("steamid", SteamID));
CharacterInfo?.Save(element);
if (itemData != null)
{
element.Add(itemData);
}
return element;
}
}
}
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
namespace Barotrauma
{
partial class GameMode
{
public static List<GameModePreset> PresetList = new List<GameModePreset>();
protected DateTime startTime;
protected bool isRunning;
protected GameModePreset preset;
private string endMessage;
protected CrewManager CrewManager
{
get { return GameMain.GameSession?.CrewManager; }
}
public virtual Mission Mission
{
get { return null; }
}
public bool IsRunning
{
get { return isRunning; }
}
public bool IsSinglePlayer
{
get { return preset.IsSinglePlayer; }
}
public string Name
{
get { return preset.Name; }
}
public string EndMessage
{
get { return endMessage; }
}
public GameModePreset Preset
{
get { return preset; }
}
public GameMode(GameModePreset preset, object param)
{
this.preset = preset;
}
public virtual void Start()
{
startTime = DateTime.Now;
endMessage = "The round has ended!";
isRunning = true;
}
public virtual void ShowStartMessage() { }
public virtual void AddToGUIUpdateList()
{
#if CLIENT
if (!isRunning) return;
GameMain.GameSession?.CrewManager.AddToGUIUpdateList();
#endif
}
public virtual void Update(float deltaTime)
{
CrewManager?.Update(deltaTime);
}
public virtual void End(string endMessage = "")
{
isRunning = false;
if (endMessage != "" || this.endMessage == null) this.endMessage = endMessage;
GameMain.GameSession.EndRound(endMessage);
}
public virtual void Remove() { }
}
}
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Barotrauma
{
class GameModePreset
{
public static List<GameModePreset> List = new List<GameModePreset>();
public readonly ConstructorInfo Constructor;
public readonly string Name;
public readonly string Description;
public readonly string Identifier;
public readonly bool IsSinglePlayer;
//are clients allowed to vote for this gamemode
public readonly bool Votable;
public GameModePreset(string identifier, Type type, bool isSinglePlayer = false, bool votable = true)
{
Name = TextManager.Get("GameMode." + identifier);
Description = TextManager.Get("GameModeDescription." + identifier, returnNull: true) ?? "";
Identifier = identifier;
Constructor = type.GetConstructor(new Type[] { typeof(GameModePreset), typeof(object) });
IsSinglePlayer = isSinglePlayer;
Votable = votable;
List.Add(this);
}
public GameMode Instantiate(object param)
{
object[] lobject = new object[] { this, param };
return (GameMode)Constructor.Invoke(lobject);
}
public static void Init()
{
#if CLIENT
new GameModePreset("singleplayercampaign", typeof(SinglePlayerCampaign), true);
new GameModePreset("tutorial", typeof(TutorialMode), true);
new GameModePreset("devsandbox", typeof(GameMode), true);
#endif
new GameModePreset("sandbox", typeof(GameMode), false);
new GameModePreset("mission", typeof(MissionMode), false);
new GameModePreset("multiplayercampaign", typeof(MultiPlayerCampaign), false, false);
}
}
}
@@ -0,0 +1,37 @@
namespace Barotrauma
{
partial class MissionMode : GameMode
{
private Mission mission;
public override Mission Mission
{
get
{
return mission;
}
}
public MissionMode(GameModePreset preset, object param)
: base(preset, param)
{
Location[] locations = { GameMain.GameSession.StartLocation, GameMain.GameSession.EndLocation };
if (param is MissionType missionType)
{
mission = Mission.LoadRandom(locations, GameMain.NetLobbyScreen.LevelSeed, false, missionType);
}
else if (param is MissionPrefab missionPrefab)
{
mission = missionPrefab.Instantiate(locations);
}
else if (param is Mission)
{
mission = (Mission)param;
}
else
{
throw new System.ArgumentException("Unrecognized MissionMode parameter \"" + param + "\"");
}
}
}
}
@@ -0,0 +1,225 @@
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
namespace Barotrauma
{
partial class MultiPlayerCampaign : CampaignMode
{
private UInt16 lastUpdateID;
public UInt16 LastUpdateID
{
get
{
#if SERVER
if (GameMain.Server != null && lastUpdateID < 1) lastUpdateID++;
#endif
return lastUpdateID;
}
set { lastUpdateID = value; }
}
private UInt16 lastSaveID;
public UInt16 LastSaveID
{
get
{
#if SERVER
if (GameMain.Server != null && lastSaveID < 1) lastSaveID++;
#endif
return lastSaveID;
}
set { lastSaveID = value; }
}
public UInt16 PendingSaveID
{
get;
set;
}
private static byte currentCampaignID;
public byte CampaignID
{
get; private set;
}
public MultiPlayerCampaign(GameModePreset preset, object param) :
base(preset, param)
{
currentCampaignID++;
CampaignID = currentCampaignID;
}
public override void Start()
{
base.Start();
if (GameMain.NetworkMember.IsServer) lastUpdateID++;
}
public override void End(string endMessage = "")
{
isRunning = false;
#if CLIENT
if (GameMain.Client != null)
{
GameMain.GameSession.EndRound("");
GameMain.GameSession.CrewManager.EndRound();
return;
}
#endif
#if SERVER
lastUpdateID++;
bool success =
GameMain.Server.ConnectedClients.Any(c => c.InGame && c.Character != null && !c.Character.IsDead);
success = success || (GameMain.Server.Character != null && !GameMain.Server.Character.IsDead);
/*if (success)
{
if (subsToLeaveBehind == null || leavingSub == null)
{
DebugConsole.ThrowError("Leaving submarine not selected -> selecting the closest one");
leavingSub = GetLeavingSub();
subsToLeaveBehind = GetSubsToLeaveBehind(leavingSub);
}
}*/
GameMain.GameSession.EndRound("");
//client character has spawned this round -> remove old data (and replace with an up-to-date one if the client still has an alive character)
characterData.RemoveAll(cd => cd.HasSpawned);
foreach (Client c in GameMain.Server.ConnectedClients)
{
if (c.Character?.Info != null && !c.Character.IsDead)
{
c.CharacterInfo = c.Character.Info;
characterData.Add(new CharacterCampaignData(c));
}
}
//remove all items that are in someone's inventory
foreach (Character c in Character.CharacterList)
{
c.Inventory?.DeleteAllItems();
}
if (success)
{
bool atEndPosition = Submarine.MainSub.AtEndPosition;
/*if (leavingSub != Submarine.MainSub && !leavingSub.DockedTo.Contains(Submarine.MainSub))
{
Submarine.MainSub = leavingSub;
GameMain.GameSession.Submarine = leavingSub;
foreach (Submarine sub in subsToLeaveBehind)
{
MapEntity.mapEntityList.RemoveAll(e => e.Submarine == sub && e is LinkedSubmarine);
LinkedSubmarine.CreateDummy(leavingSub, sub);
}
}*/
if (atEndPosition)
{
map.MoveToNextLocation();
//select a random location to make sure we've got some destination
//to head towards even if the host/clients don't select anything
map.SelectRandomLocation(true);
}
map.ProgressWorld();
SaveUtil.SaveGame(GameMain.GameSession.SavePath);
}
#endif
}
partial void SetDelegates();
public static MultiPlayerCampaign LoadNew(XElement element)
{
MultiPlayerCampaign campaign = new MultiPlayerCampaign(GameModePreset.List.Find(gm => gm.Identifier == "multiplayercampaign"), null);
campaign.Load(element);
campaign.SetDelegates();
return campaign;
}
public static string GetCharacterDataSavePath(string savePath)
{
return Path.Combine(SaveUtil.MultiplayerSaveFolder, Path.GetFileNameWithoutExtension(savePath) + "_CharacterData.xml");
}
public string GetCharacterDataSavePath()
{
return GetCharacterDataSavePath(GameMain.GameSession.SavePath);
}
public void Load(XElement element)
{
Money = element.GetAttributeInt("money", 0);
InitialSuppliesSpawned = element.GetAttributeBool("initialsuppliesspawned", 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
}
foreach (XElement subElement in element.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "map":
if (map == null)
{
//map not created yet, loading this campaign for the first time
map = Map.LoadNew(subElement);
}
else
{
//map already created, update it
//if we're not downloading the initial save file (LastSaveID > 0),
//show notifications about location type changes
map.Load(subElement, LastSaveID > 0);
}
break;
}
}
#if SERVER
characterData.Clear();
string characterDataPath = GetCharacterDataSavePath();
var characterDataDoc = XMLExtensions.TryLoadXml(characterDataPath);
if (characterDataDoc?.Root == null) return;
foreach (XElement subElement in characterDataDoc.Root.Elements())
{
characterData.Add(new CharacterCampaignData(subElement));
}
#endif
}
}
}