- 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

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)
@@ -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;

View File

@@ -458,7 +458,7 @@ namespace Barotrauma.Items.Components
//called then the item is dropped or dragged out of a "limbslot"
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)

View File

@@ -137,7 +137,7 @@ namespace Barotrauma.Items.Components
return false;
}
item.SendSignal("1", "trigger_out");
item.SendSignal(0, "1", "trigger_out");
ApplyStatusEffects(ActionType.OnUse, 1.0f, activator);
@@ -173,7 +173,7 @@ namespace Barotrauma.Items.Components
if (focusTarget == null)
{
item.SendSignal(ToolBox.Vector2ToString(character.CursorWorldPosition), "position_out");
item.SendSignal(0, ToolBox.Vector2ToString(character.CursorWorldPosition), "position_out");
return;
}
@@ -185,13 +185,13 @@ namespace Barotrauma.Items.Components
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)
{
item.SendSignal("1", "signal_out");
item.SendSignal(0, "1", "signal_out");
PlaySound(ActionType.OnUse, item.WorldPosition);
@@ -237,7 +237,7 @@ namespace Barotrauma.Items.Components
IsActive = true;
}
item.SendSignal("1", "signal_out");
item.SendSignal(0, "1", "signal_out");
return true;
}

View File

@@ -124,9 +124,9 @@ namespace Barotrauma.Items.Components
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")
{

View File

@@ -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")
{

View File

@@ -315,7 +315,7 @@ namespace Barotrauma.Items.Components
ExtraCooling = 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);
@@ -519,7 +519,7 @@ namespace Barotrauma.Items.Components
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)
{

View File

@@ -135,13 +135,13 @@ namespace Barotrauma.Items.Components
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;
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;
@@ -299,7 +299,7 @@ namespace Barotrauma.Items.Components
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")
{
@@ -307,7 +307,7 @@ namespace Barotrauma.Items.Components
}
else
{
base.ReceiveSignal(signal, connection, sender, power);
base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
}
}

View File

@@ -208,7 +208,7 @@ namespace Barotrauma.Items.Components
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;

View File

@@ -61,7 +61,7 @@ namespace Barotrauma.Items.Components
pt.powerLoad += (fullLoad - pt.powerLoad) / 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
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);
}
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")
{
connection.SendSignal(signal, sender, 0.0f);
connection.SendSignal(stepsTaken, signal, sender, 0.0f);
}
}

View File

@@ -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 (connection.IsPower) voltage = power;

View File

@@ -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)
{

View File

@@ -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)

View File

@@ -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;
}

View File

@@ -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)
{

View File

@@ -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");
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -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");
}

View File

@@ -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)
{

View File

@@ -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";
}
}

View File

@@ -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":

View File

@@ -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");
}
}
}

View File

@@ -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;
}

View File

@@ -327,7 +327,7 @@ namespace Barotrauma.Items.Components
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)
{

View File

@@ -986,18 +986,42 @@ namespace Barotrauma
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>();
if (panel == null) return;
foreach (Connection c in panel.Connections)
{
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="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>