Cargo missions, a menu that shows the mission description mid-round

This commit is contained in:
Regalis
2016-04-16 15:36:01 +03:00
parent 6876bdc481
commit 31a04c7bc1
13 changed files with 327 additions and 42 deletions
@@ -0,0 +1,100 @@
using Barotrauma.Items.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Barotrauma
{
class CargoMission : Mission
{
private XElement itemConfig;
private List<Item> items;
private int requiredDeliveryAmount;
public CargoMission(XElement element)
: base(element)
{
itemConfig = element.Element("Items");
requiredDeliveryAmount = ToolBox.GetAttributeInt(element, "requireddeliveryamount", 0);
}
private void InitItems()
{
items = new List<Item>();
if (itemConfig==null)
{
DebugConsole.ThrowError("Failed to initialize items for cargo mission (itemConfig == null)");
return;
}
foreach (XElement subElement in itemConfig.Elements())
{
LoadItemAsChild(subElement, null);
}
if (requiredDeliveryAmount == 0) requiredDeliveryAmount = items.Count();
}
private void LoadItemAsChild(XElement element, Item parent)
{
string itemName = ToolBox.GetAttributeString(element, "name", "");
ItemPrefab itemPrefab = ItemPrefab.list.Find(ip => ip.Name == itemName) as ItemPrefab;
if (itemPrefab==null)
{
DebugConsole.ThrowError("Couldn't spawn item for cargo mission: item prefab ''"+element.Name.ToString()+"'' not found");
return;
}
WayPoint cargoSpawnPos = WayPoint.GetRandom(SpawnType.Cargo);
if (cargoSpawnPos==null)
{
DebugConsole.ThrowError("Couldn't spawn items for cargo mission, cargo spawnpoint not found");
return;
}
var item = new Item(itemPrefab, cargoSpawnPos.Position, Submarine.Loaded);
items.Add(item);
if (parent != null) parent.Combine(item);
foreach (XElement subElement in element.Elements())
{
int amount = ToolBox.GetAttributeInt(subElement, "amount", 1);
for (int i = 0; i < amount; i++)
{
LoadItemAsChild(subElement, item);
}
}
}
public override void Start(Level level)
{
InitItems();
}
public override void End()
{
if (Submarine.Loaded != null && Submarine.Loaded.AtEndPosition)
{
int deliveredItemCount = items.Count(i => i.CurrentHull != null && i.Condition > 0.0f);
if (deliveredItemCount >= requiredDeliveryAmount)
{
GiveReward();
completed = true;
}
}
items.ForEach(i => i.Remove());
}
}
}
@@ -0,0 +1,203 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
namespace Barotrauma
{
class Mission
{
private static List<Mission> list = new List<Mission>();
private string name;
private string description;
protected bool completed;
protected string successMessage;
protected string failureMessage;
protected string radarLabel;
protected List<string> headers;
protected List<string> messages;
private int reward;
public string Name
{
get { return name; }
}
public string Description
{
get { return description; }
}
public int Reward
{
get { return reward; }
}
public bool Completed
{
get { return completed; }
}
public virtual string RadarLabel
{
get { return radarLabel; }
}
public virtual Vector2 RadarPosition
{
get { return Vector2.Zero; }
}
public string SuccessMessage
{
get { return successMessage; }
}
public string FailureMessage
{
get { return failureMessage; }
}
public Mission(XElement element)
{
name = ToolBox.GetAttributeString(element, "name", "");
description = ToolBox.GetAttributeString(element, "description", "");
reward = ToolBox.GetAttributeInt(element, "reward", 1);
successMessage = ToolBox.GetAttributeString(element, "successmessage",
"Mission completed successfully");
failureMessage = ToolBox.GetAttributeString(element, "failuremessage",
"Mission failed");
radarLabel = ToolBox.GetAttributeString(element, "radarlabel", "");
messages = new List<string>();
headers = new List<string>();
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLower() != "message") continue;
headers.Add(ToolBox.GetAttributeString(subElement, "header", ""));
messages.Add(ToolBox.GetAttributeString(subElement, "text", ""));
}
}
public static Mission LoadRandom(Location[] locations, Random rand)
{
var files = GameMain.SelectedPackage.GetFilesOfType(ContentType.Missions);
string configFile = files[rand.Next(files.Count)];
XDocument doc = ToolBox.TryLoadXml(configFile);
if (doc == null) return null;
int eventCount = doc.Root.Elements().Count();
//int[] commonness = new int[eventCount];
float[] eventProbability = new float[eventCount];
float probabilitySum = 0.0f;
int i = 0;
foreach (XElement element in doc.Root.Elements())
{
eventProbability[i] = ToolBox.GetAttributeInt(element, "commonness", 1);
probabilitySum += eventProbability[i];
i++;
}
float randomNumber = (float)rand.NextDouble() * probabilitySum;
i = 0;
foreach (XElement element in doc.Root.Elements())
{
if (randomNumber <= eventProbability[i])
{
Type t;
string type = element.Name.ToString();
try
{
t = Type.GetType("Barotrauma." + type, true, true);
if (t == null)
{
DebugConsole.ThrowError("Error in " + configFile + "! Could not find a mission class of the type ''" + type + "''.");
continue;
}
}
catch
{
DebugConsole.ThrowError("Error in " + configFile + "! Could not find a mission class of the type ''" + type + "''.");
continue;
}
ConstructorInfo constructor = t.GetConstructor(new[] { typeof(XElement) });
object instance = constructor.Invoke(new object[] { element });
Mission mission = (Mission)instance;
for (int n = 0; n<2; n++)
{
mission.description = mission.description.Replace("[location"+(n+1)+"]", locations[n].Name);
mission.successMessage = mission.successMessage.Replace("[location" + (n + 1) + "]", locations[n].Name);
mission.failureMessage = mission.failureMessage.Replace("[location" + (n + 1) + "]", locations[n].Name);
}
return mission;
}
randomNumber -= eventProbability[i];
i++;
}
return null;
}
public virtual void Start(Level level) { }
public virtual void Update(float deltaTime) { }
public void ShowMessage(int index)
{
if (index >= headers.Count && index >= messages.Count) return;
string header = index < headers.Count ? headers[index] : "";
string message = index < messages.Count ? messages[index] : "";
Barotrauma.Networking.GameServer.Log("Mission info: " + header + " - " + message, Color.Cyan);
new GUIMessageBox(header, message);
}
/// <summary>
/// End the mission and give a reward if it was completed successfully
/// </summary>
public virtual void End()
{
completed = true;
GiveReward();
}
public void GiveReward()
{
var mode = GameMain.GameSession.gameMode as SinglePlayerMode;
if (mode == null) return;
mode.Money += reward;
}
}
}
@@ -0,0 +1,67 @@
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using System.Xml.Linq;
namespace Barotrauma
{
class MonsterMission : Mission
{
private string monsterFile;
private int state;
private Character monster;
private Vector2 radarPosition;
public override Vector2 RadarPosition
{
get { return monster != null && !monster.IsDead ? radarPosition : Vector2.Zero; }
}
public MonsterMission(XElement element)
: base(element)
{
monsterFile = ToolBox.GetAttributeString(element, "monsterfile", "");
}
public override void Start(Level level)
{
Vector2 position = level.GetRandomInterestingPosition(true, true);
monster = Character.Create(monsterFile, position, null, GameMain.Client != null);
monster.Enabled = false;
radarPosition = monster.Position;
}
public override void Update(float deltaTime)
{
switch (state)
{
case 0:
if (monster.Enabled)
{
radarPosition = monster.Position;
}
else if (GameMain.Client == null)
{ Vector2 diff = monster.WorldPosition-Submarine.Loaded.WorldPosition;
monster.Enabled = FarseerPhysics.ConvertUnits.ToSimUnits(diff.Length()) < NetConfig.CharacterIgnoreDistance;
}
if (!monster.IsDead) return;
ShowMessage(state);
state = 1;
break;
}
}
public override void End()
{
if (!monster.IsDead) return;
GiveReward();
completed = true;
}
}
}
@@ -0,0 +1,79 @@
using FarseerPhysics;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Barotrauma
{
class SalvageMission : Mission
{
private ItemPrefab itemPrefab;
private Item item;
private int state;
public override Vector2 RadarPosition
{
get
{
return state>0 ? Vector2.Zero : ConvertUnits.ToDisplayUnits(item.SimPosition);
}
}
public SalvageMission(XElement element)
: base(element)
{
string itemName = ToolBox.GetAttributeString(element, "itemname", "");
itemPrefab = ItemPrefab.list.Find(ip => ip.Name == itemName) as ItemPrefab;
if (itemPrefab == null)
{
DebugConsole.ThrowError("Error in SalvageMission: couldn't find an item prefab with the name "+itemName);
}
}
public override void Start(Level level)
{
Vector2 position = Level.Loaded.GetRandomItemPos(30.0f);
item = new Item(itemPrefab, position, null);
item.MoveWithLevel = true;
item.body.FarseerBody.IsKinematic = true;
}
public override void Update(float deltaTime)
{
switch (state)
{
case 0:
//item.body.LinearVelocity = Vector2.Zero;
if (item.ParentInventory!=null) item.body.FarseerBody.IsKinematic = false;
if (item.CurrentHull == null) return;
ShowMessage(state);
state = 1;
break;
case 1:
if (!Submarine.Loaded.AtEndPosition && !Submarine.Loaded.AtStartPosition) return;
ShowMessage(state);
state = 2;
break;
}
}
public override void End()
{
if (item.CurrentHull == null || item.Removed) return;
item.Remove();
GiveReward();
completed = true;
}
}
}