- fixed "signal loops" causing StackOverFlowExceptions (now the signals can only take 10 "steps" between components per frame)
- parameter for changing the output value of And/Or components when the input conditions aren't met - or components work properly now - a limited number of signals (100) can be queued in a delay component
This commit is contained in:
@@ -5,7 +5,7 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
class AndComponent : ItemComponent
|
||||
{
|
||||
protected string output;
|
||||
protected string output, falseOutput;
|
||||
|
||||
//an array to keep track of how long ago a non-zero signal was received on both inputs
|
||||
protected float[] timeSinceReceived;
|
||||
@@ -30,6 +30,13 @@ namespace Barotrauma.Items.Components
|
||||
set { output = value; }
|
||||
}
|
||||
|
||||
[InGameEditable, HasDefaultValue("", true)]
|
||||
public string FalseOutput
|
||||
{
|
||||
get { return falseOutput; }
|
||||
set { falseOutput = value; }
|
||||
}
|
||||
|
||||
public AndComponent(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
@@ -47,13 +54,13 @@ namespace Barotrauma.Items.Components
|
||||
timeSinceReceived[i] += deltaTime;
|
||||
}
|
||||
|
||||
if (sendOutput)
|
||||
{
|
||||
item.SendSignal(output, "signal_out");
|
||||
}
|
||||
string signalOut = sendOutput ? output : falseOutput;
|
||||
if (string.IsNullOrEmpty(signalOut)) return;
|
||||
|
||||
item.SendSignal(0, signalOut, "signal_out");
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
|
||||
{
|
||||
switch (connection.Name)
|
||||
{
|
||||
|
||||
@@ -152,7 +152,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
}
|
||||
|
||||
public void SendSignal(string signal, Item sender, float power)
|
||||
public void SendSignal(int stepsTaken, string signal, Item sender, float power)
|
||||
{
|
||||
for (int i = 0; i<MaxLinked; i++)
|
||||
{
|
||||
@@ -164,7 +164,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
foreach (ItemComponent ic in recipient.item.components)
|
||||
{
|
||||
ic.ReceiveSignal(signal, recipient, this.item, power);
|
||||
ic.ReceiveSignal(stepsTaken, signal, recipient, this.item, power);
|
||||
}
|
||||
|
||||
foreach (StatusEffect effect in recipient.effects)
|
||||
|
||||
@@ -9,8 +9,10 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
class DelayComponent : ItemComponent
|
||||
{
|
||||
const int SignalQueueSize = 500;
|
||||
|
||||
//the output is sent if both inputs have received a signal within the timeframe
|
||||
protected TimeSpan delay;
|
||||
private TimeSpan delay;
|
||||
|
||||
private Queue<Tuple<string, DateTime>> signalQueue;
|
||||
|
||||
@@ -40,15 +42,17 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
var signalOut = signalQueue.Dequeue();
|
||||
|
||||
item.SendSignal(signalOut.Item1, "signal_out");
|
||||
item.SendSignal(0, signalOut.Item1, "signal_out");
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
|
||||
{
|
||||
switch (connection.Name)
|
||||
{
|
||||
case "signal_in":
|
||||
if (signalQueue.Count >= SignalQueueSize) return;
|
||||
|
||||
signalQueue.Enqueue(new Tuple<string, DateTime>(signal, DateTime.Now));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -163,9 +163,9 @@ namespace Barotrauma.Items.Components
|
||||
light.Remove();
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
|
||||
{
|
||||
base.ReceiveSignal(signal, connection, sender, power);
|
||||
base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
|
||||
|
||||
switch (connection.Name)
|
||||
{
|
||||
|
||||
@@ -9,11 +9,11 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
|
||||
{
|
||||
if (connection.Name != "signal_in") return;
|
||||
|
||||
item.SendSignal(signal=="0" ? "1" : "0", "signal_out");
|
||||
item.SendSignal(stepsTaken, signal=="0" ? "1" : "0", "signal_out");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,17 +11,17 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
bool sendOutput = true;
|
||||
bool sendOutput = false;
|
||||
for (int i = 0; i<timeSinceReceived.Length; i++)
|
||||
{
|
||||
if (timeSinceReceived[i] > timeFrame) sendOutput = false;
|
||||
if (timeSinceReceived[i] <= timeFrame) sendOutput = true;
|
||||
timeSinceReceived[i] += deltaTime;
|
||||
}
|
||||
|
||||
if (sendOutput)
|
||||
{
|
||||
item.SendSignal(output, "signal_out");
|
||||
}
|
||||
string signalOut = sendOutput ? output : falseOutput;
|
||||
if (string.IsNullOrEmpty(signalOut)) return;
|
||||
|
||||
item.SendSignal(0, signalOut, "signal_out");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (item.CurrentHull == null) return;
|
||||
|
||||
item.SendSignal(((int)item.CurrentHull.OxygenPercentage).ToString(), "signal_out");
|
||||
item.SendSignal(0, ((int)item.CurrentHull.OxygenPercentage).ToString(), "signal_out");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
catch
|
||||
{
|
||||
item.SendSignal("ERROR", "signal_out");
|
||||
item.SendSignal(0, "ERROR", "signal_out");
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -67,17 +67,17 @@ namespace Barotrauma.Items.Components
|
||||
}
|
||||
catch
|
||||
{
|
||||
item.SendSignal("ERROR", "signal_out");
|
||||
item.SendSignal(0, "ERROR", "signal_out");
|
||||
previousResult = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
item.SendSignal(previousResult ? output : "0", "signal_out");
|
||||
item.SendSignal(0, previousResult ? output : "0", "signal_out");
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power = 0.0f)
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power = 0.0f)
|
||||
{
|
||||
switch (connection.Name)
|
||||
{
|
||||
|
||||
@@ -20,20 +20,23 @@ namespace Barotrauma.Items.Components
|
||||
[Editable, HasDefaultValue(false, true)]
|
||||
public bool IsOn
|
||||
{
|
||||
get { return IsActive; }
|
||||
set
|
||||
{
|
||||
IsActive = value;
|
||||
}
|
||||
get; set;
|
||||
}
|
||||
|
||||
public RelayComponent(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
IsActive = true;
|
||||
IsOn = true;
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
|
||||
item.SendSignal(0, IsOn ? "1" : "0", "state_out");
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
|
||||
{
|
||||
if (item.Condition <= 0.0f) return;
|
||||
|
||||
@@ -41,7 +44,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
if (connection.Name.Contains("_in"))
|
||||
{
|
||||
if (!IsActive) return;
|
||||
if (!IsOn) return;
|
||||
|
||||
string outConnection = connection.Name.Contains("power_in") ? "power_out" : "signal_out";
|
||||
|
||||
@@ -50,15 +53,15 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
if (connectionNumber > 0) outConnection += connectionNumber;
|
||||
|
||||
item.SendSignal(signal, outConnection, power);
|
||||
item.SendSignal(stepsTaken, signal, outConnection, power);
|
||||
}
|
||||
else if (connection.Name == "toggle")
|
||||
{
|
||||
IsActive = !IsActive;
|
||||
IsOn = !IsOn;
|
||||
}
|
||||
else if (connection.Name == "set_state")
|
||||
{
|
||||
IsActive = signal == "1";
|
||||
IsOn = signal != "0";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,12 +27,12 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
|
||||
{
|
||||
switch (connection.Name)
|
||||
{
|
||||
case "signal_in":
|
||||
item.SendSignal((signal == targetSignal) ? output : "0", "signal_out");
|
||||
item.SendSignal(stepsTaken, (signal == targetSignal) ? output : "0", "signal_out");
|
||||
|
||||
break;
|
||||
case "set_output":
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
public override void Update(float deltaTime, Camera cam)
|
||||
{
|
||||
item.SendSignal(item.InWater ? "1" : "0", "signal_out");
|
||||
item.SendSignal(0, item.InWater ? "1" : "0", "signal_out");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Barotrauma.Items.Components
|
||||
list.Add(this);
|
||||
}
|
||||
|
||||
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power=0.0f)
|
||||
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
|
||||
{
|
||||
//prevent an ininite loop of wificomponents sending messages between each other
|
||||
if (sender.GetComponent<WifiComponent>()!=null) return;
|
||||
@@ -39,7 +39,7 @@ namespace Barotrauma.Items.Components
|
||||
foreach (WifiComponent wifiComp in list)
|
||||
{
|
||||
if (wifiComp == this || wifiComp.channel != channel) continue;
|
||||
wifiComp.item.SendSignal(signal, "signal_out");
|
||||
wifiComp.item.SendSignal(stepsTaken, signal, "signal_out");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user