2 using Microsoft.Xna.Framework;
4 using System.Collections.Generic;
12 private float maxPower;
16 private float prevVoltage;
18 private float? newVoltage =
null;
21 private float internalLoadBuffer = 0;
24 private float prevInternalLoad = 0;
27 private float prevExternalLoad = 0;
30 private float bufferDiff = 0;
32 private float thirdInverseMax = 0, loadEqnConstant = 0;
34 private static readonly Dictionary<string, string> connectionPairs =
new Dictionary<string, string>
36 {
"power_in",
"power_out"},
37 {
"signal_in",
"signal_out" },
38 {
"signal_in1",
"signal_out1" },
39 {
"signal_in2",
"signal_out2" },
40 {
"signal_in3",
"signal_out3" },
41 {
"signal_in4",
"signal_out4" },
42 {
"signal_in5",
"signal_out5" }
51 if (powerOut !=
null && powerOut.Grid !=
null)
53 return powerOut.Grid.Load;
65 get {
return maxPower; }
68 maxPower = Math.Max(0.0f, value);
69 SetLoadFormulaValues();
96 SetLoadFormulaValues();
99 private void SetLoadFormulaValues()
103 thirdInverseMax = 1 / (3 * maxPower);
104 loadEqnConstant = (8 * maxPower) / 3;
112 if (connections !=
null)
114 foreach (KeyValuePair<string, string> connectionPair
in connectionPairs)
116 if (connections.Any(c => c.Name == connectionPair.Key) && !connections.Any(c => c.Name == connectionPair.Value))
118 DebugConsole.ThrowError(
"Error in item \"" +
Name +
"\" - matching connection pair not found for the connection \"" + connectionPair.Key +
"\" (expecting \"" + connectionPair.Value +
"\").");
120 else if (connections.Any(c => c.Name == connectionPair.Value) && !connections.Any(c => c.Name == connectionPair.Key))
122 DebugConsole.ThrowError(
"Error in item \"" +
Name +
"\" - matching connection pair not found for the connection \"" + connectionPair.Value +
"\" (expecting \"" + connectionPair.Key +
"\").");
171 currentLoad = MathHelper.Clamp(internalLoadBuffer * internalLoadBuffer * thirdInverseMax - 2 * internalLoadBuffer + loadEqnConstant, 0.001f,
MaxPower);
174 currentLoad = MathHelper.Clamp((currentLoad + prevInternalLoad * 0.1f) / 1.1f, 0.001f,
MaxPower);
175 prevInternalLoad = currentLoad;
189 private bool RelayCanOutput()
192 return isOn &&
powerIn !=
null &&
powerIn.
Grid !=
null && internalLoadBuffer > 0 && powerOut !=
null && powerOut.Grid !=
null &&
powerIn.
Grid != powerOut.Grid;
201 if (connection == powerOut)
203 if (RelayCanOutput())
206 float bufferDraw = MathHelper.
Min(internalLoadBuffer,
MaxPower);
207 float voltageLimit = MathHelper.Min(
MaxPower, load) * prevVoltage;
212 voltageLimit *= prevVoltage;
215 float maxOutput = MathHelper.Min(voltageLimit, bufferDraw);
235 else if (RelayCanOutput())
238 float bufferDraw = MathHelper.Min(internalLoadBuffer,
MaxPower);
239 float voltageLimit = MathHelper.Min(
MaxPower, load) * prevVoltage;
240 float maxOut = MathHelper.Min(voltageLimit, bufferDraw);
249 prevExternalLoad = load;
252 PowerLoad = MathHelper.Clamp((load * prevVoltage - power) / MathHelper.Max(minMaxPower.
Max, 1E-20f) * -maxOut, -maxOut, 0);
286 if (newVoltage ==
null)
290 bufferDiff = addToBuffer;
301 if (newVoltage ==
null)
309 UpdateBuffer(
PowerLoad, (
float)newVoltage);
314 private void UpdateBuffer(
float addToBuffer,
float newVoltage)
326 internalLoadBuffer = MathHelper.Clamp(internalLoadBuffer + bufferDiff + addToBuffer, 0, limit);
331 newVoltage = MathHelper.Max(newVoltage - 0.0005f, 1);
334 prevVoltage = newVoltage;
335 this.newVoltage =
null;
343 if (connectionPairs.TryGetValue(connection.
Name, out
string outConnection))
345 if (!
IsOn) {
return; }
348 else if (connection.
Name ==
"toggle")
350 if (signal.
value ==
"0") {
return; }
353 else if (connection.
Name ==
"set_state")
359 public void SetState(
bool on,
bool isNetworkMessage)
368 item.CreateServerEvent(
this);
List< Connection > Connections
List< Repairable > Repairables
void SendSignal(string signal, string connectionName)
void ApplyStatusEffects(ActionType type, float deltaTime, Character character=null, Limb targetLimb=null, Entity useTarget=null, Character user=null, Vector2? worldPosition=null, float afflictionMultiplier=1.0f)
float ExtraLoad
Additional load coming from somewhere else than the devices connected to the junction box (e....
void RefreshConnections()
void SetAllConnectionsDirty()
float CurrPowerConsumption
float currPowerConsumption
The amount of power currently consumed by the item. Negative values mean that the item is providing p...
override void GridResolved(Connection conn)
Connection's grid resolved, determine the difference to be added to the buffer. Ensure the prevVoltag...
override float GetConnectionPowerOut(Connection connection, float power, PowerRange minMaxPower, float load)
Power out for the relay connection. Relay will output the necessary power to the grid based on maximu...
void ClientEventRead(IReadMessage msg, float sendingTime)
override float GetCurrentPowerConsumption(Connection connection=null)
Relay power consumption. Load consumption is based on the internal buffer. This allows for the relay ...
void ServerEventWrite(IWriteMessage msg, Client c, NetEntityEvent.IData extraData=null)
void SetState(bool on, bool isNetworkMessage)
RelayComponent(Item item, ContentXElement element)
override void OnItemLoaded()
Called when all the components of the item have been loaded. Use to initialize connections between co...
override PowerRange MinMaxPowerOut(Connection connection, float load=0)
Minimum and maximum power out for the relay. Max out is adjusted to allow for other relays to compens...
override PowerPriority Priority
override void ReceiveSignal(Signal signal, Connection connection)
override void Update(float deltaTime, Camera cam)
Interface for entities that the server can send events to the clients
void WriteBoolean(bool val)
PowerPriority
Order in which power sources will provide to a grid, lower number is higher priority
ActionType
ActionTypes define when a StatusEffect is executed.
static readonly PowerRange Zero