Option to choose mission type in mp

This commit is contained in:
Regalis
2016-05-10 18:42:50 +03:00
parent 1a3da8b29f
commit e7a06a6171
12 changed files with 129 additions and 32 deletions
-1
View File
@@ -72,7 +72,6 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" />
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+42 -4
View File
@@ -9,7 +9,7 @@ namespace Barotrauma
{ {
class Mission class Mission
{ {
//private static List<Mission> list = new List<Mission>(); public static List<string> MissionTypes = new List<string>() { "Random" };
private string name; private string name;
@@ -67,6 +67,25 @@ namespace Barotrauma
get { return failureMessage; } get { return failureMessage; }
} }
public static void Init()
{
var files = GameMain.SelectedPackage.GetFilesOfType(ContentType.Missions);
foreach (string file in files)
{
XDocument doc = ToolBox.TryLoadXml(file);
if (doc == null || doc.Root == null) continue;
foreach (XElement element in doc.Root.Elements())
{
string missionTypeName = element.Name.ToString();
missionTypeName = missionTypeName.Replace("Mission", "");
if (!MissionTypes.Contains(missionTypeName)) MissionTypes.Add(missionTypeName);
}
}
}
public Mission(XElement element) public Mission(XElement element)
{ {
name = ToolBox.GetAttributeString(element, "name", ""); name = ToolBox.GetAttributeString(element, "name", "");
@@ -92,8 +111,10 @@ namespace Barotrauma
} }
} }
public static Mission LoadRandom(Location[] locations, MTRandom rand) public static Mission LoadRandom(Location[] locations, MTRandom rand, string missionType = "")
{ {
missionType = missionType.ToLowerInvariant();
var files = GameMain.SelectedPackage.GetFilesOfType(ContentType.Missions); var files = GameMain.SelectedPackage.GetFilesOfType(ContentType.Missions);
string configFile = files[rand.Next(files.Count)]; string configFile = files[rand.Next(files.Count)];
@@ -106,8 +127,25 @@ namespace Barotrauma
float probabilitySum = 0.0f; float probabilitySum = 0.0f;
List<XElement> matchingElements = new List<XElement>();
if (missionType == "random")
{
matchingElements = doc.Root.Elements().ToList();
}
else if (missionType == "none")
{
return null;
}
else
{
matchingElements = doc.Root.Elements().ToList().FindAll(m => m.Name.ToString().ToLowerInvariant().Replace("mission", "") == missionType);
}
int i = 0; int i = 0;
foreach (XElement element in doc.Root.Elements()) foreach (XElement element in matchingElements)
{ {
eventProbability[i] = ToolBox.GetAttributeInt(element, "commonness", 1); eventProbability[i] = ToolBox.GetAttributeInt(element, "commonness", 1);
@@ -119,7 +157,7 @@ namespace Barotrauma
float randomNumber = (float)rand.NextDouble() * probabilitySum; float randomNumber = (float)rand.NextDouble() * probabilitySum;
i = 0; i = 0;
foreach (XElement element in doc.Root.Elements()) foreach (XElement element in matchingElements)
{ {
if (randomNumber <= eventProbability[i]) if (randomNumber <= eventProbability[i])
{ {
+1
View File
@@ -204,6 +204,7 @@ namespace Barotrauma
TitleScreen.LoadState = 2.0f; TitleScreen.LoadState = 2.0f;
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
Mission.Init();
MapEntityPrefab.Init(); MapEntityPrefab.Init();
TitleScreen.LoadState = 10.0f; TitleScreen.LoadState = 10.0f;
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
@@ -50,7 +50,7 @@ namespace Barotrauma
get { return preset; } get { return preset; }
} }
public GameMode(GameModePreset preset) public GameMode(GameModePreset preset, object param)
{ {
this.preset = preset; this.preset = preset;
} }
@@ -19,19 +19,17 @@ namespace Barotrauma
public GameModePreset(string name, Type type, bool isSinglePlayer = false) public GameModePreset(string name, Type type, bool isSinglePlayer = false)
{ {
this.Name = name; this.Name = name;
//Constructor = constructor;
Constructor = type.GetConstructor(new Type[] { typeof(GameModePreset), typeof(object) });
Constructor = type.GetConstructor(new Type[] { typeof(GameModePreset) });
IsSinglePlayer = isSinglePlayer; IsSinglePlayer = isSinglePlayer;
list.Add(this); list.Add(this);
} }
public GameMode Instantiate() public GameMode Instantiate(object param)
{ {
object[] lobject = new object[] { this }; object[] lobject = new object[] { this, param };
return (GameMode)Constructor.Invoke(lobject); return (GameMode)Constructor.Invoke(lobject);
} }
@@ -14,8 +14,8 @@ namespace Barotrauma
} }
} }
public MissionMode(GameModePreset preset) public MissionMode(GameModePreset preset, object param)
: base(preset) : base(preset, param)
{ {
Location[] locations = new Location[2]; Location[] locations = new Location[2];
@@ -25,13 +25,16 @@ namespace Barotrauma
{ {
locations[i] = Location.CreateRandom(new Vector2((float)rand.NextDouble() * 10000.0f, (float)rand.NextDouble() * 10000.0f)); locations[i] = Location.CreateRandom(new Vector2((float)rand.NextDouble() * 10000.0f, (float)rand.NextDouble() * 10000.0f));
} }
mission = Mission.LoadRandom(locations, rand);
mission = Mission.LoadRandom(locations, rand, param as string);
} }
public override void Start() public override void Start()
{ {
base.Start(); base.Start();
if (mission == null) return;
new GUIMessageBox(mission.Name, mission.Description, 400, 400); new GUIMessageBox(mission.Name, mission.Description, 400, 400);
Networking.GameServer.Log("Mission: " + mission.Name, Color.Cyan); Networking.GameServer.Log("Mission: " + mission.Name, Color.Cyan);
@@ -45,8 +45,8 @@ namespace Barotrauma
get { return GameMain.GameSession.CrewManager; } get { return GameMain.GameSession.CrewManager; }
} }
public SinglePlayerMode(GameModePreset preset) public SinglePlayerMode(GameModePreset preset, object param)
: base(preset) : base(preset, param)
{ {
CargoManager = new CargoManager(); CargoManager = new CargoManager();
@@ -79,7 +79,7 @@ namespace Barotrauma
} }
public SinglePlayerMode(XElement element) public SinglePlayerMode(XElement element)
: this(GameModePreset.list.Find(gm => gm.Name == "Single Player")) : this(GameModePreset.list.Find(gm => gm.Name == "Single Player"), null)
{ {
string mapSeed = ToolBox.GetAttributeString(element, "mapseed", "a"); string mapSeed = ToolBox.GetAttributeString(element, "mapseed", "a");
@@ -15,8 +15,8 @@ namespace Barotrauma
tutorialType.Initialize(); tutorialType.Initialize();
} }
public TutorialMode(GameModePreset preset) public TutorialMode(GameModePreset preset, object param)
: base(preset) : base(preset, param)
{ {
} }
+2 -2
View File
@@ -66,7 +66,7 @@ namespace Barotrauma
get { return shiftSummary; } get { return shiftSummary; }
} }
public GameSession(Submarine submarine, string saveFile, GameModePreset gameModePreset = null) public GameSession(Submarine submarine, string saveFile, GameModePreset gameModePreset = null, string missionType="")
{ {
GameMain.GameSession = this; GameMain.GameSession = this;
@@ -81,7 +81,7 @@ namespace Barotrauma
infoButton = new GUIButton(new Rectangle(10, 10, 100, 20), "Info", GUI.Style, null); infoButton = new GUIButton(new Rectangle(10, 10, 100, 20), "Info", GUI.Style, null);
infoButton.OnClicked = ToggleInfoFrame; infoButton.OnClicked = ToggleInfoFrame;
if (gameModePreset!=null) gameMode = gameModePreset.Instantiate(); if (gameModePreset!=null) gameMode = gameModePreset.Instantiate(missionType);
this.submarine = submarine; this.submarine = submarine;
} }
+3 -1
View File
@@ -610,6 +610,8 @@ namespace Barotrauma.Networking
string levelSeed = inc.ReadString(); string levelSeed = inc.ReadString();
int missionTypeIndex = inc.ReadByte();
string mapName = inc.ReadString(); string mapName = inc.ReadString();
string mapHash = inc.ReadString(); string mapHash = inc.ReadString();
@@ -633,7 +635,7 @@ namespace Barotrauma.Networking
Rand.SetSyncedSeed(seed); Rand.SetSyncedSeed(seed);
//int gameModeIndex = inc.ReadInt32(); //int gameModeIndex = inc.ReadInt32();
GameMain.GameSession = new GameSession(GameMain.NetLobbyScreen.SelectedSub, "", gameMode); GameMain.GameSession = new GameSession(GameMain.NetLobbyScreen.SelectedSub, "", gameMode, Mission.MissionTypes[missionTypeIndex]);
GameMain.GameSession.StartShift(levelSeed); GameMain.GameSession.StartShift(levelSeed);
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
+3 -1
View File
@@ -876,7 +876,7 @@ namespace Barotrauma.Networking
roundStartSeed = DateTime.Now.Millisecond; roundStartSeed = DateTime.Now.Millisecond;
Rand.SetSyncedSeed(roundStartSeed); Rand.SetSyncedSeed(roundStartSeed);
GameMain.GameSession = new GameSession(selectedSub, "", selectedMode); GameMain.GameSession = new GameSession(selectedSub, "", selectedMode, Mission.MissionTypes[GameMain.NetLobbyScreen.MissionTypeIndex]);
GameMain.GameSession.StartShift(GameMain.NetLobbyScreen.LevelSeed); GameMain.GameSession.StartShift(GameMain.NetLobbyScreen.LevelSeed);
GameServer.Log("Starting a new round...", Color.Cyan); GameServer.Log("Starting a new round...", Color.Cyan);
@@ -978,6 +978,8 @@ namespace Barotrauma.Networking
msg.Write(GameMain.NetLobbyScreen.LevelSeed); msg.Write(GameMain.NetLobbyScreen.LevelSeed);
msg.Write((byte)GameMain.NetLobbyScreen.MissionTypeIndex);
msg.Write(selectedSub.Name); msg.Write(selectedSub.Name);
msg.Write(selectedSub.MD5Hash.Hash); msg.Write(selectedSub.MD5Hash.Hash);
+63 -9
View File
@@ -22,6 +22,9 @@ namespace Barotrauma
private GUIButton[] traitorProbabilityButtons; private GUIButton[] traitorProbabilityButtons;
private GUITextBlock traitorProbabilityText; private GUITextBlock traitorProbabilityText;
private GUIButton[] missionTypeButtons;
private GUIComponent missionTypeBlock;
private GUIListBox jobList; private GUIListBox jobList;
private GUITextBox textBox, seedBox; private GUITextBox textBox, seedBox;
@@ -75,6 +78,12 @@ namespace Barotrauma
get { return modeList.SelectedData as GameModePreset; } get { return modeList.SelectedData as GameModePreset; }
} }
public int MissionTypeIndex
{
get { return (int)missionTypeBlock.UserData; }
set { missionTypeBlock.UserData = value; }
}
//for guitextblock delegate //for guitextblock delegate
public string GetServerName() public string GetServerName()
{ {
@@ -220,6 +229,23 @@ namespace Barotrauma
} }
//mission type ------------------------------------------------------------------
missionTypeBlock = new GUITextBlock(new Rectangle(columnX, 0, 300, 20), "Mission type:", GUI.Style, Alignment.BottomLeft, Alignment.TopLeft, infoFrame);
missionTypeBlock.UserData = 0;
missionTypeButtons = new GUIButton[2];
missionTypeButtons[0] = new GUIButton(new Rectangle(120, 0, 20, 20), "<", Alignment.BottomLeft, GUI.Style, missionTypeBlock);
missionTypeButtons[0].UserData = -1;
new GUITextBlock(new Rectangle(120, 0, 120, 20), "Random", GUI.Style, Alignment.BottomLeft, Alignment.TopCenter, missionTypeBlock).UserData = 0;
missionTypeButtons[1] = new GUIButton(new Rectangle(240, 0, 20, 20), ">", Alignment.BottomLeft, GUI.Style, missionTypeBlock);
missionTypeButtons[1].UserData = 1;
missionTypeBlock.Visible = false;
columnX += columnWidth + 20; columnX += columnWidth + 20;
//gamemode description ------------------------------------------------------------------ //gamemode description ------------------------------------------------------------------
@@ -257,6 +283,7 @@ namespace Barotrauma
traitorProbabilityButtons[1] = new GUIButton(new Rectangle(columnX + 100, 205, 20, 20), ">", GUI.Style, infoFrame); traitorProbabilityButtons[1] = new GUIButton(new Rectangle(columnX + 100, 205, 20, 20), ">", GUI.Style, infoFrame);
traitorProbabilityButtons[1].UserData = 1; traitorProbabilityButtons[1].UserData = 1;
//automatic restart ------------------------------------------------------------------ //automatic restart ------------------------------------------------------------------
autoRestartBox = new GUITickBox(new Rectangle(columnX, 240, 20, 20), "Automatic restart", Alignment.TopLeft, infoFrame); autoRestartBox = new GUITickBox(new Rectangle(columnX, 240, 20, 20), "Automatic restart", Alignment.TopLeft, infoFrame);
@@ -308,6 +335,9 @@ namespace Barotrauma
traitorProbabilityButtons[0].Enabled = GameMain.Server != null; traitorProbabilityButtons[0].Enabled = GameMain.Server != null;
traitorProbabilityButtons[1].Enabled = GameMain.Server != null; traitorProbabilityButtons[1].Enabled = GameMain.Server != null;
missionTypeButtons[0].Enabled = GameMain.Server != null;
missionTypeButtons[1].Enabled = GameMain.Server != null;
ServerName = (GameMain.Server==null) ? "Server" : GameMain.Server.Name; ServerName = (GameMain.Server==null) ? "Server" : GameMain.Server.Name;
infoFrame.RemoveChild(infoFrame.children.Find(c => c.UserData as string == "startButton")); infoFrame.RemoveChild(infoFrame.children.Find(c => c.UserData as string == "startButton"));
@@ -326,6 +356,9 @@ namespace Barotrauma
traitorProbabilityButtons[0].OnClicked = ToggleTraitorsEnabled; traitorProbabilityButtons[0].OnClicked = ToggleTraitorsEnabled;
traitorProbabilityButtons[1].OnClicked = ToggleTraitorsEnabled; traitorProbabilityButtons[1].OnClicked = ToggleTraitorsEnabled;
missionTypeButtons[0].OnClicked = ToggleMissionType;
missionTypeButtons[1].OnClicked = ToggleMissionType;
GUIButton startButton = new GUIButton(new Rectangle(0, 0, 80, 30), "Start", Alignment.BottomRight, GUI.Style, infoFrame); GUIButton startButton = new GUIButton(new Rectangle(0, 0, 80, 30), "Start", Alignment.BottomRight, GUI.Style, infoFrame);
startButton.OnClicked = GameMain.Server.StartGameClicked; startButton.OnClicked = GameMain.Server.StartGameClicked;
startButton.UserData = "startButton"; startButton.UserData = "startButton";
@@ -482,6 +515,27 @@ namespace Barotrauma
return true; return true;
} }
private void SetMissionType(int missionTypeIndex)
{
if (missionTypeIndex < 0 || missionTypeIndex >= Mission.MissionTypes.Count) return;
missionTypeBlock.GetChild<GUITextBlock>().Text = Mission.MissionTypes[missionTypeIndex];
missionTypeBlock.UserData = missionTypeIndex;
}
public bool ToggleMissionType(GUIButton button, object userData)
{
int missionTypeIndex = (int)missionTypeBlock.UserData;
missionTypeIndex += (int)userData;
if (missionTypeIndex<0) missionTypeIndex = Mission.MissionTypes.Count-1;
if (missionTypeIndex>=Mission.MissionTypes.Count) missionTypeIndex=0;
SetMissionType(missionTypeIndex);
return true;
}
public bool ToggleTraitorsEnabled(GUIButton button, object userData) public bool ToggleTraitorsEnabled(GUIButton button, object userData)
{ {
if (GameMain.Server == null) return false; if (GameMain.Server == null) return false;
@@ -830,6 +884,8 @@ namespace Barotrauma
//} //}
GameModePreset modePreset = obj as GameModePreset; GameModePreset modePreset = obj as GameModePreset;
missionTypeBlock.Visible = modePreset.Name == "Mission";
if (modePreset == null) return false; if (modePreset == null) return false;
valueChanged = true; valueChanged = true;
@@ -993,11 +1049,9 @@ namespace Barotrauma
msg.WriteRangedInteger(0, 2, (int)GameMain.Server.TraitorsEnabled); msg.WriteRangedInteger(0, 2, (int)GameMain.Server.TraitorsEnabled);
//msg.Write(AllowSubVoting); msg.WriteRangedInteger(0, Mission.MissionTypes.Count-1, MissionTypeIndex);
//msg.Write(AllowModeVoting);
msg.Write((byte)modeList.SelectedIndex); msg.Write((byte)modeList.SelectedIndex);
//msg.Write(durationBar.BarScroll);
msg.Write(LevelSeed); msg.Write(LevelSeed);
msg.Write(GameMain.Server != null && GameMain.Server.AutoRestart); msg.Write(GameMain.Server != null && GameMain.Server.AutoRestart);
@@ -1035,14 +1089,11 @@ namespace Barotrauma
traitorsEnabled = (YesNoMaybe)msg.ReadRangedInteger(0, 2); traitorsEnabled = (YesNoMaybe)msg.ReadRangedInteger(0, 2);
//AllowSubVoting = msg.ReadBoolean(); SetMissionType(msg.ReadRangedInteger(0, Mission.MissionTypes.Count - 1));
//AllowModeVoting = msg.ReadBoolean();
modeIndex = msg.ReadByte(); modeIndex = msg.ReadByte();
//durationScroll = msg.ReadFloat(); newSeed = msg.ReadString();
newSeed = msg.ReadString();
autoRestart = msg.ReadBoolean(); autoRestart = msg.ReadBoolean();
restartTimer = msg.ReadFloat(); restartTimer = msg.ReadFloat();
@@ -1063,7 +1114,10 @@ namespace Barotrauma
if (!string.IsNullOrWhiteSpace(mapName) && !GameMain.NetworkMember.Voting.AllowSubVoting) TrySelectSub(mapName, md5Hash); if (!string.IsNullOrWhiteSpace(mapName) && !GameMain.NetworkMember.Voting.AllowSubVoting) TrySelectSub(mapName, md5Hash);
if (!GameMain.NetworkMember.Voting.AllowModeVoting) modeList.Select(modeIndex, true); if (!GameMain.NetworkMember.Voting.AllowModeVoting)
{
SelectMode(modeList.children[modeIndex], modeList.children[modeIndex].UserData);
}
SetTraitorsEnabled(traitorsEnabled); SetTraitorsEnabled(traitorsEnabled);