15a31c5291
- The sub can be moved from location to another in the map view by double clicking in debug builds. - Level wall color can be changed in level generation parameters. - Fixed level geometry not being rendered if the ocean floor is visible (which isn't a problem in most level types, but there can be levels where the ocean floor is so close to the actual level that they can both be visible at the same time). - Background sprite scale is taken into account when calculating particle emitter positions. - Fixed limb lights being rendered even if the character is disabled.
196 lines
5.8 KiB
C#
196 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Barotrauma
|
|
{
|
|
class ScriptedEvent
|
|
{
|
|
protected string name;
|
|
protected string description;
|
|
|
|
protected int commonness;
|
|
protected int difficulty;
|
|
|
|
protected bool isFinished;
|
|
|
|
public Dictionary<string, int> OverrideCommonness;
|
|
|
|
public string Name
|
|
{
|
|
get { return name; }
|
|
}
|
|
|
|
public string Description
|
|
{
|
|
get { return description; }
|
|
}
|
|
|
|
public int Commonness
|
|
{
|
|
get { return commonness; }
|
|
}
|
|
|
|
public string MusicType
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public virtual bool IsActive
|
|
{
|
|
get { return true; }
|
|
}
|
|
|
|
public bool IsFinished
|
|
{
|
|
get { return isFinished; }
|
|
}
|
|
|
|
public int Difficulty
|
|
{
|
|
get { return difficulty; }
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return "ScriptedEvent ("+name+")";
|
|
}
|
|
|
|
public ScriptedEvent(XElement element)
|
|
{
|
|
name = ToolBox.GetAttributeString(element, "name", "");
|
|
description = ToolBox.GetAttributeString(element, "description", "");
|
|
|
|
difficulty = ToolBox.GetAttributeInt(element, "difficulty", 1);
|
|
commonness = ToolBox.GetAttributeInt(element, "commonness", 1);
|
|
|
|
MusicType = ToolBox.GetAttributeString(element, "musictype", "default");
|
|
|
|
OverrideCommonness = new Dictionary<string, int>();
|
|
|
|
foreach (XElement subElement in element.Elements())
|
|
{
|
|
switch (subElement.Name.ToString().ToLowerInvariant())
|
|
{
|
|
case "overridecommonness":
|
|
string levelType = ToolBox.GetAttributeString(subElement, "leveltype", "");
|
|
if (!OverrideCommonness.ContainsKey(levelType))
|
|
{
|
|
OverrideCommonness.Add(levelType, ToolBox.GetAttributeInt(subElement, "commonness", 1));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static ScriptedEvent LoadRandom(Random rand)
|
|
{
|
|
var configFiles = GameMain.Config.SelectedContentPackage.GetFilesOfType(ContentType.RandomEvents);
|
|
|
|
if (!configFiles.Any())
|
|
{
|
|
DebugConsole.ThrowError("No config files for random events found in the selected content package");
|
|
return null;
|
|
}
|
|
|
|
string configFile = configFiles[0];
|
|
|
|
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);
|
|
|
|
//if the event has been previously selected, it's less likely to be selected now
|
|
//int previousEventIndex = previousEvents.FindIndex(x => x == i);
|
|
//if (previousEventIndex >= 0)
|
|
//{
|
|
// //how many shifts ago was the event last selected
|
|
// int eventDist = eventCount - previousEventIndex;
|
|
|
|
// float weighting = (1.0f / eventDist) * PreviouslyUsedWeight;
|
|
|
|
// eventProbability[i] *= weighting;
|
|
//}
|
|
|
|
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 an event class of the type \"" + type + "\".");
|
|
continue;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
DebugConsole.ThrowError("Error in " + configFile + "! Could not find an event class of the type \"" + type + "\".");
|
|
continue;
|
|
}
|
|
|
|
ConstructorInfo constructor = t.GetConstructor(new[] { typeof(XElement) });
|
|
object instance = null;
|
|
try
|
|
{
|
|
instance = constructor.Invoke(new object[] { element });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
DebugConsole.ThrowError(ex.InnerException!=null ? ex.InnerException.ToString() : ex.ToString());
|
|
}
|
|
|
|
//previousEvents.Add(i);
|
|
|
|
return (ScriptedEvent)instance;
|
|
}
|
|
|
|
randomNumber -= eventProbability[i];
|
|
i++;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public virtual void Init()
|
|
{
|
|
isFinished = false;
|
|
}
|
|
|
|
public virtual void Update(float deltaTime)
|
|
{
|
|
}
|
|
|
|
public virtual void Finished()
|
|
{
|
|
isFinished = true;
|
|
}
|
|
}
|
|
}
|