- 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:
Regalis
2016-04-02 02:25:44 +03:00
parent 3ac8139fc7
commit 5f05db7ca4
24 changed files with 107 additions and 69 deletions
+2 -2
View File
@@ -282,7 +282,7 @@ namespace Barotrauma.Items.Components
} }
item.SendSignal((isOpen) ? "1" : "0", "state_out"); item.SendSignal(0, (isOpen) ? "1" : "0", "state_out");
} }
public override void UpdateBroken(float deltaTime, Camera cam) public override void UpdateBroken(float deltaTime, Camera cam)
@@ -436,7 +436,7 @@ 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 (isStuck || GameMain.Client != null) return; if (isStuck || GameMain.Client != null) return;
@@ -458,7 +458,7 @@ namespace Barotrauma.Items.Components
//called then the item is dropped or dragged out of a "limbslot" //called then the item is dropped or dragged out of a "limbslot"
public virtual void Unequip(Character character) { } public virtual void Unequip(Character character) { }
public virtual void ReceiveSignal(string signal, Connection connection, Item sender, float power = 0.0f) public virtual void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power = 0.0f)
{ {
switch (connection.Name) switch (connection.Name)
@@ -137,7 +137,7 @@ namespace Barotrauma.Items.Components
return false; return false;
} }
item.SendSignal("1", "trigger_out"); item.SendSignal(0, "1", "trigger_out");
ApplyStatusEffects(ActionType.OnUse, 1.0f, activator); ApplyStatusEffects(ActionType.OnUse, 1.0f, activator);
@@ -173,7 +173,7 @@ namespace Barotrauma.Items.Components
if (focusTarget == null) if (focusTarget == null)
{ {
item.SendSignal(ToolBox.Vector2ToString(character.CursorWorldPosition), "position_out"); item.SendSignal(0, ToolBox.Vector2ToString(character.CursorWorldPosition), "position_out");
return; return;
} }
@@ -185,13 +185,13 @@ namespace Barotrauma.Items.Components
if (!character.IsNetworkPlayer || character.ViewTarget == focusTarget) if (!character.IsNetworkPlayer || character.ViewTarget == focusTarget)
{ {
item.SendSignal(ToolBox.Vector2ToString(character.CursorWorldPosition), "position_out"); item.SendSignal(0, ToolBox.Vector2ToString(character.CursorWorldPosition), "position_out");
} }
} }
public override bool Pick(Character picker) public override bool Pick(Character picker)
{ {
item.SendSignal("1", "signal_out"); item.SendSignal(0, "1", "signal_out");
PlaySound(ActionType.OnUse, item.WorldPosition); PlaySound(ActionType.OnUse, item.WorldPosition);
@@ -237,7 +237,7 @@ namespace Barotrauma.Items.Components
IsActive = true; IsActive = true;
} }
item.SendSignal("1", "signal_out"); item.SendSignal(0, "1", "signal_out");
return true; return true;
} }
@@ -124,9 +124,9 @@ namespace Barotrauma.Items.Components
force = MathHelper.Lerp(force, 0.0f, 0.1f); force = MathHelper.Lerp(force, 0.0f, 0.1f);
} }
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);
if (connection.Name == "set_force") if (connection.Name == "set_force")
{ {
@@ -163,9 +163,9 @@ 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)
{ {
base.ReceiveSignal(signal, connection, sender, power); base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
if (connection.Name == "toggle") if (connection.Name == "toggle")
{ {
@@ -315,7 +315,7 @@ namespace Barotrauma.Items.Components
ExtraCooling = 0.0f; ExtraCooling = 0.0f;
AvailableFuel = 0.0f; AvailableFuel = 0.0f;
item.SendSignal(((int)temperature).ToString(), "temperature_out"); item.SendSignal(0, ((int)temperature).ToString(), "temperature_out");
sendUpdateTimer = Math.Max(sendUpdateTimer - deltaTime, 0.0f); sendUpdateTimer = Math.Max(sendUpdateTimer - deltaTime, 0.0f);
@@ -519,7 +519,7 @@ namespace Barotrauma.Items.Components
GUI.DrawLine(spriteBatch, prevPoint, lastPoint, color); GUI.DrawLine(spriteBatch, prevPoint, lastPoint, color);
} }
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power) public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power)
{ {
switch (connection.Name) switch (connection.Name)
{ {
@@ -135,13 +135,13 @@ namespace Barotrauma.Items.Components
UpdateAutoPilot(deltaTime); UpdateAutoPilot(deltaTime);
} }
item.SendSignal(targetVelocity.X.ToString(CultureInfo.InvariantCulture), "velocity_x_out"); item.SendSignal(0, targetVelocity.X.ToString(CultureInfo.InvariantCulture), "velocity_x_out");
float targetLevel = -targetVelocity.Y; float targetLevel = -targetVelocity.Y;
targetLevel += (neutralBallastLevel - 0.5f) * 100.0f; targetLevel += (neutralBallastLevel - 0.5f) * 100.0f;
item.SendSignal(targetLevel.ToString(CultureInfo.InvariantCulture), "velocity_y_out"); item.SendSignal(0, targetLevel.ToString(CultureInfo.InvariantCulture), "velocity_y_out");
voltage -= deltaTime; voltage -= deltaTime;
@@ -299,7 +299,7 @@ namespace Barotrauma.Items.Components
return true; return true;
} }
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 == "velocity_in") if (connection.Name == "velocity_in")
{ {
@@ -307,7 +307,7 @@ namespace Barotrauma.Items.Components
} }
else else
{ {
base.ReceiveSignal(signal, connection, sender, power); base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
} }
} }
@@ -208,7 +208,7 @@ namespace Barotrauma.Items.Components
return true; return true;
} }
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power) public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power)
{ {
if (!connection.IsPower) return; if (!connection.IsPower) return;
@@ -61,7 +61,7 @@ namespace Barotrauma.Items.Components
pt.powerLoad += (fullLoad - pt.powerLoad) / inertia; pt.powerLoad += (fullLoad - pt.powerLoad) / inertia;
pt.currPowerConsumption += (-fullPower - pt.currPowerConsumption) / inertia; pt.currPowerConsumption += (-fullPower - pt.currPowerConsumption) / inertia;
pt.Item.SendSignal("", "power", fullPower / Math.Max(fullLoad, 1.0f)); pt.Item.SendSignal(0, "", "power", fullPower / Math.Max(fullLoad, 1.0f));
//damage the item if voltage is too high //damage the item if voltage is too high
if (-pt.currPowerConsumption < Math.Max(pt.powerLoad * Rand.Range(1.95f,2.05f), 200.0f)) continue; if (-pt.currPowerConsumption < Math.Max(pt.powerLoad * Rand.Range(1.95f,2.05f), 200.0f)) continue;
@@ -182,13 +182,13 @@ namespace Barotrauma.Items.Components
spriteBatch.DrawString(GUI.Font, "Load: " + (int)powerLoad + " kW", new Vector2(x + 30, y + 100), Color.White); spriteBatch.DrawString(GUI.Font, "Load: " + (int)powerLoad + " kW", new Vector2(x + 30, y + 100), Color.White);
} }
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power) public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power)
{ {
base.ReceiveSignal(signal, connection, sender, power); base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
if (connection.Name.Length > 5 && connection.Name.Substring(0, 6).ToLower() == "signal") if (connection.Name.Length > 5 && connection.Name.Substring(0, 6).ToLower() == "signal")
{ {
connection.SendSignal(signal, sender, 0.0f); connection.SendSignal(stepsTaken, signal, sender, 0.0f);
} }
} }
@@ -83,7 +83,7 @@ namespace Barotrauma.Items.Components
} }
} }
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power = 0) public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power = 0)
{ {
if (currPowerConsumption == 0.0f) voltage = 0.0f; if (currPowerConsumption == 0.0f) voltage = 0.0f;
if (connection.IsPower) voltage = power; if (connection.IsPower) voltage = power;
@@ -5,7 +5,7 @@ namespace Barotrauma.Items.Components
{ {
class AndComponent : ItemComponent 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 //an array to keep track of how long ago a non-zero signal was received on both inputs
protected float[] timeSinceReceived; protected float[] timeSinceReceived;
@@ -30,6 +30,13 @@ namespace Barotrauma.Items.Components
set { output = value; } set { output = value; }
} }
[InGameEditable, HasDefaultValue("", true)]
public string FalseOutput
{
get { return falseOutput; }
set { falseOutput = value; }
}
public AndComponent(Item item, XElement element) public AndComponent(Item item, XElement element)
: base (item, element) : base (item, element)
{ {
@@ -47,13 +54,13 @@ namespace Barotrauma.Items.Components
timeSinceReceived[i] += deltaTime; timeSinceReceived[i] += deltaTime;
} }
if (sendOutput) string signalOut = sendOutput ? output : falseOutput;
{ if (string.IsNullOrEmpty(signalOut)) return;
item.SendSignal(output, "signal_out");
} 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) 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++) for (int i = 0; i<MaxLinked; i++)
{ {
@@ -164,7 +164,7 @@ namespace Barotrauma.Items.Components
foreach (ItemComponent ic in recipient.item.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) foreach (StatusEffect effect in recipient.effects)
@@ -9,8 +9,10 @@ namespace Barotrauma.Items.Components
{ {
class DelayComponent : ItemComponent class DelayComponent : ItemComponent
{ {
const int SignalQueueSize = 500;
//the output is sent if both inputs have received a signal within the timeframe //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; private Queue<Tuple<string, DateTime>> signalQueue;
@@ -40,15 +42,17 @@ namespace Barotrauma.Items.Components
{ {
var signalOut = signalQueue.Dequeue(); 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) switch (connection.Name)
{ {
case "signal_in": case "signal_in":
if (signalQueue.Count >= SignalQueueSize) return;
signalQueue.Enqueue(new Tuple<string, DateTime>(signal, DateTime.Now)); signalQueue.Enqueue(new Tuple<string, DateTime>(signal, DateTime.Now));
break; break;
} }
@@ -163,9 +163,9 @@ namespace Barotrauma.Items.Components
light.Remove(); 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) 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; 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) public override void Update(float deltaTime, Camera cam)
{ {
bool sendOutput = true; bool sendOutput = false;
for (int i = 0; i<timeSinceReceived.Length; i++) for (int i = 0; i<timeSinceReceived.Length; i++)
{ {
if (timeSinceReceived[i] > timeFrame) sendOutput = false; if (timeSinceReceived[i] <= timeFrame) sendOutput = true;
timeSinceReceived[i] += deltaTime; timeSinceReceived[i] += deltaTime;
} }
if (sendOutput) string signalOut = sendOutput ? output : falseOutput;
{ if (string.IsNullOrEmpty(signalOut)) return;
item.SendSignal(output, "signal_out");
} item.SendSignal(0, signalOut, "signal_out");
} }
} }
} }
@@ -14,7 +14,7 @@ namespace Barotrauma.Items.Components
{ {
if (item.CurrentHull == null) return; 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 catch
{ {
item.SendSignal("ERROR", "signal_out"); item.SendSignal(0, "ERROR", "signal_out");
return; return;
} }
} }
@@ -67,17 +67,17 @@ namespace Barotrauma.Items.Components
} }
catch catch
{ {
item.SendSignal("ERROR", "signal_out"); item.SendSignal(0, "ERROR", "signal_out");
previousResult = false; previousResult = false;
return; 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) switch (connection.Name)
{ {
@@ -20,20 +20,23 @@ namespace Barotrauma.Items.Components
[Editable, HasDefaultValue(false, true)] [Editable, HasDefaultValue(false, true)]
public bool IsOn public bool IsOn
{ {
get { return IsActive; } get; set;
set
{
IsActive = value;
}
} }
public RelayComponent(Item item, XElement element) public RelayComponent(Item item, XElement element)
: base (item, element) : base (item, element)
{ {
IsActive = true; 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; if (item.Condition <= 0.0f) return;
@@ -41,7 +44,7 @@ namespace Barotrauma.Items.Components
if (connection.Name.Contains("_in")) if (connection.Name.Contains("_in"))
{ {
if (!IsActive) return; if (!IsOn) return;
string outConnection = connection.Name.Contains("power_in") ? "power_out" : "signal_out"; string outConnection = connection.Name.Contains("power_in") ? "power_out" : "signal_out";
@@ -50,15 +53,15 @@ namespace Barotrauma.Items.Components
if (connectionNumber > 0) outConnection += connectionNumber; if (connectionNumber > 0) outConnection += connectionNumber;
item.SendSignal(signal, outConnection, power); item.SendSignal(stepsTaken, signal, outConnection, power);
} }
else if (connection.Name == "toggle") else if (connection.Name == "toggle")
{ {
IsActive = !IsActive; IsOn = !IsOn;
} }
else if (connection.Name == "set_state") 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) switch (connection.Name)
{ {
case "signal_in": case "signal_in":
item.SendSignal((signal == targetSignal) ? output : "0", "signal_out"); item.SendSignal(stepsTaken, (signal == targetSignal) ? output : "0", "signal_out");
break; break;
case "set_output": case "set_output":
@@ -12,7 +12,7 @@ namespace Barotrauma.Items.Components
public override void Update(float deltaTime, Camera cam) 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); 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 //prevent an ininite loop of wificomponents sending messages between each other
if (sender.GetComponent<WifiComponent>()!=null) return; if (sender.GetComponent<WifiComponent>()!=null) return;
@@ -39,7 +39,7 @@ namespace Barotrauma.Items.Components
foreach (WifiComponent wifiComp in list) foreach (WifiComponent wifiComp in list)
{ {
if (wifiComp == this || wifiComp.channel != channel) continue; if (wifiComp == this || wifiComp.channel != channel) continue;
wifiComp.item.SendSignal(signal, "signal_out"); wifiComp.item.SendSignal(stepsTaken, signal, "signal_out");
} }
break; break;
} }
+1 -1
View File
@@ -327,7 +327,7 @@ namespace Barotrauma.Items.Components
return projectiles; return projectiles;
} }
public override void ReceiveSignal(string signal, Connection connection, Item sender, float power) public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power)
{ {
switch (connection.Name) switch (connection.Name)
{ {
+26 -2
View File
@@ -986,18 +986,42 @@ namespace Barotrauma
return connectedComponents; return connectedComponents;
} }
public void SendSignal(string signal, string connectionName, float power = 0.0f) public void SendSignal(int stepsTaken, string signal, string connectionName, float power = 0.0f)
{ {
stepsTaken++;
ConnectionPanel panel = GetComponent<ConnectionPanel>(); ConnectionPanel panel = GetComponent<ConnectionPanel>();
if (panel == null) return; if (panel == null) return;
foreach (Connection c in panel.Connections) foreach (Connection c in panel.Connections)
{ {
if (c.Name != connectionName) continue; if (c.Name != connectionName) continue;
c.SendSignal(signal, this, power); if (stepsTaken > 10)
{
//use a coroutine to prevent infinite loops by creating a one
//frame delay if the "signal chain" gets too long
CoroutineManager.StartCoroutine(SendSignal(signal, c, power));
}
else
{
c.SendSignal(stepsTaken, signal, this, power);
}
} }
} }
private IEnumerable<object> SendSignal(string signal, Connection connection, float power = 0.0f)
{
//wait one frame
yield return CoroutineStatus.Running;
ConnectionPanel panel = GetComponent<ConnectionPanel>();
if (panel == null) yield return CoroutineStatus.Success;
connection.SendSignal(0, signal, this, power);
yield return CoroutineStatus.Success;
}
/// <param name="position">Position of the Character doing the pick, only items that are close enough to this are checked</param> /// <param name="position">Position of the Character doing the pick, only items that are close enough to this are checked</param>
/// <param name="pickPosition">the item closest to pickPosition is returned</param> /// <param name="pickPosition">the item closest to pickPosition is returned</param>
/// <param name="hull">If a hull is specified, only items within that hull are checked</param> /// <param name="hull">If a hull is specified, only items within that hull are checked</param>