Skip to content

Commit 4ee3658

Browse files
authored
Merge pull request #445 from Sharpe49/scripts-preupdate
Add a PreUpdate variable to scripts http://www.elvastower.com/forums/index.php?/topic/34792-c-signal-scripts/page__view__findpost__p__275090
2 parents bc95487 + e28b186 commit 4ee3658

File tree

14 files changed

+26
-16
lines changed

14 files changed

+26
-16
lines changed

Source/Orts.Simulation/Common/Scripting/Common.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
1717

18-
using System;
1918
using Orts.Common;
2019
using Orts.Simulation;
2120
using Orts.Simulation.RollingStocks;
21+
using System;
2222

2323
namespace ORTS.Scripting.Api
2424
{
@@ -35,6 +35,10 @@ public abstract class AbstractScriptClass
3535
/// Clock value (in seconds) for the simulation. Starts with a value = 0.
3636
/// </summary>
3737
public Func<float> GameTime;
38+
/// <summary>
39+
/// Simulator is in pre-update mode (update during loading screen).
40+
/// </summary>
41+
public Func<bool> PreUpdate;
3842
}
3943
/// <summary>
4044
/// Base class for scripts related to train subsystems.

Source/Orts.Simulation/Simulation/AIs/AI.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class AI
5454
public List<AITrain> AutoGenTrains = new List<AITrain>(); // auto-generated trains
5555
public double clockTime; // clock time : local time before activity start, common time from simulator after start
5656
private bool localTime; // if true : clockTime is local time
57-
public bool PreUpdate; // if true : running in pre-update phase
5857
public List<AITrain> TrainsToRemove = new List<AITrain>();
5958
public List<AITrain> TrainsToAdd = new List<AITrain>();
6059
public List<AITrain> TrainsToRemoveFromAI = new List<AITrain>();
@@ -341,14 +340,14 @@ private void PrerunAI(CancellationToken cancellation)
341340

342341
clockTime = firstAITime - 1.0f;
343342
localTime = true;
344-
PreUpdate = true;
343+
Simulator.PreUpdate = true;
345344

346345
for (double runTime = firstAITime; runTime < Simulator.ClockTime; runTime += 5.0) // update with 5 secs interval
347346
{
348347
int fullsec = Convert.ToInt32(runTime);
349348
if (fullsec % 3600 == 0) Trace.Write(" " + (fullsec / 3600).ToString("00") + ":00 ");
350349

351-
AIUpdate((float)(runTime - clockTime), PreUpdate);
350+
AIUpdate((float)(runTime - clockTime), Simulator.PreUpdate);
352351
Simulator.Signals.Update(true);
353352
clockTime = runTime;
354353
if (cancellation.IsCancellationRequested) return; // ping watchdog process
@@ -371,14 +370,14 @@ private void PrerunAI(int playerTrainOriginalTrain, TTTrain.FormCommand playerTr
371370

372371
clockTime = firstAITime - 1.0f;
373372
localTime = true;
374-
PreUpdate = true;
373+
Simulator.PreUpdate = true;
375374
bool activeTrains = false;
376375
for (double runTime = firstAITime; runTime < Simulator.ClockTime && !endPreRun; runTime += 5.0) // update with 5 secs interval
377376
{
378377
int fullsec = Convert.ToInt32(runTime);
379378
if (fullsec % 3600 < 5) Trace.Write(" " + (fullsec / 3600).ToString("00") + ":00 ");
380379

381-
endPreRun = AITTUpdate((float)(runTime - clockTime), PreUpdate, ref activeTrains);
380+
endPreRun = AITTUpdate((float)(runTime - clockTime), Simulator.PreUpdate, ref activeTrains);
382381

383382
if (activeTrains)
384383
{
@@ -541,7 +540,7 @@ private void PrerunAI(int playerTrainOriginalTrain, TTTrain.FormCommand playerTr
541540

542541
while (!playerTrainStarted)
543542
{
544-
endPreRun = AITTUpdate((float)(runTime - clockTime), PreUpdate, ref dummy);
543+
endPreRun = AITTUpdate((float)(runTime - clockTime), Simulator.PreUpdate, ref dummy);
545544
Simulator.Signals.Update(true);
546545
clockTime = runTime;
547546
runTime += deltaTime;
@@ -625,8 +624,7 @@ private void PrerunAI(int playerTrainOriginalTrain, TTTrain.FormCommand playerTr
625624
}
626625

627626
Trace.Write("\n");
628-
PreUpdate = false;
629-
627+
Simulator.PreUpdate = false;
630628
}
631629

632630
/// <summary>

Source/Orts.Simulation/Simulation/AIs/AITrain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5903,7 +5903,7 @@ public void EndProcessAction(bool actionValid, AIActionItem thisItem, bool actio
59035903
if (nextActionInfo.RequiredSpeedMpS == 0)
59045904
{
59055905
NextStopDistanceM = thisItem.ActivateDistanceM - PresentPosition[0].DistanceTravelledM;
5906-
if (AI.PreUpdate && !(nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.AUX_ACTION && NextStopDistanceM > minCheckDistanceM))
5906+
if (Simulator.PreUpdate && !(nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.AUX_ACTION && NextStopDistanceM > minCheckDistanceM))
59075907
{
59085908
AITrainBrakePercent = 100; // because of short reaction time
59095909
AITrainThrottlePercent = 0;

Source/Orts.Simulation/Simulation/Physics/Train.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ public void ReverseFormation(bool setMUParameters)
14611461
MUDirection = DirectionControl.Flip(MUDirection);
14621462
MUReverserPercent = -MUReverserPercent;
14631463
}
1464-
if (!((this is AITrain && (this as AITrain).AI.PreUpdate) || this.TrainType == TRAINTYPE.STATIC)) FormationReversed = true;
1464+
if (!((this is AITrain && Simulator.PreUpdate) || this.TrainType == TRAINTYPE.STATIC)) FormationReversed = true;
14651465
}
14661466

14671467
//================================================================================================//

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/Controllers/BrakeController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ public void Initialize()
343343
// AbstractScriptClass
344344
Script.ClockTime = () => (float)Simulator.ClockTime;
345345
Script.GameTime = () => (float)Simulator.GameTime;
346+
Script.PreUpdate = () => Simulator.PreUpdate;
346347
Script.DistanceM = () => Locomotive.DistanceM;
347348
Script.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
348349
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/CircuitBreaker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public void Initialize()
131131
// AbstractScriptClass
132132
Script.ClockTime = () => (float)Simulator.ClockTime;
133133
Script.GameTime = () => (float)Simulator.GameTime;
134+
Script.PreUpdate = () => Simulator.PreUpdate;
134135
Script.DistanceM = () => Locomotive.DistanceM;
135136
Script.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
136137
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/LocomotivePowerSupply.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ protected virtual void AssignScriptFunctions()
253253
// AbstractScriptClass
254254
AbstractScript.ClockTime = () => (float)Simulator.ClockTime;
255255
AbstractScript.GameTime = () => (float)Simulator.GameTime;
256+
AbstractScript.PreUpdate = () => Simulator.PreUpdate;
256257
AbstractScript.DistanceM = () => Locomotive.DistanceM;
257258
AbstractScript.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
258259
AbstractScript.Confirm = Locomotive.Simulator.Confirmer.Confirm;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/PassengerCarPowerSupply.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ protected virtual void AssignScriptFunctions()
302302
// AbstractScriptClass
303303
Script.ClockTime = () => (float)Simulator.ClockTime;
304304
Script.GameTime = () => (float)Simulator.GameTime;
305+
Script.PreUpdate = () => Simulator.PreUpdate;
305306
Script.DistanceM = () => Wagon.DistanceM;
306307
Script.SpeedMpS = () => Math.Abs(Wagon.SpeedMpS);
307308
Script.Confirm = Simulator.Confirmer.Confirm;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerSupplies/TractionCutOffRelay.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public void Initialize()
114114
// AbstractScriptClass
115115
Script.ClockTime = () => (float)Simulator.ClockTime;
116116
Script.GameTime = () => (float)Simulator.GameTime;
117+
Script.PreUpdate = () => Simulator.PreUpdate;
117118
Script.DistanceM = () => Locomotive.DistanceM;
118119
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;
119120
Script.Message = Locomotive.Simulator.Confirmer.Message;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/TrainControlSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ public void Initialize()
260260
// AbstractScriptClass
261261
Script.ClockTime = () => (float)Simulator.ClockTime;
262262
Script.GameTime = () => (float)Simulator.GameTime;
263+
Script.PreUpdate = () => Simulator.PreUpdate;
263264
Script.DistanceM = () => Locomotive.DistanceM;
264265
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;
265266
Script.Message = Locomotive.Simulator.Confirmer.Message;

Source/Orts.Simulation/Simulation/Signalling/CsSignalScript.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ internal void AttachToHead(SignalHead signalHead)
334334
// Build AbstractScriptClass API functions
335335
ClockTime = () => (float)SignalObject.signalRef.Simulator.ClockTime;
336336
GameTime = () => (float)SignalObject.signalRef.Simulator.GameTime;
337+
PreUpdate = () => SignalObject.signalRef.Simulator.PreUpdate;
337338
}
338339

339340
// Functions to be implemented in script

Source/Orts.Simulation/Simulation/Simulator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public class Simulator
9090
public string ActivityFileName;
9191
public string TimetableFileName;
9292
public bool TimetableMode;
93+
public bool PreUpdate;
9394
public ActivityFile Activity;
9495
public Activity ActivityRun;
9596
public TrackDatabaseFile TDB;
@@ -578,7 +579,7 @@ AITrain InitializeAPTrains(CancellationToken cancellation)
578579
if (playerTrain != null)
579580
{
580581
var validPosition = playerTrain.PostInit(); // place player train after pre-running of AI trains
581-
if (validPosition && AI != null) AI.PreUpdate = false;
582+
if (validPosition && AI != null) PreUpdate = false;
582583
if (playerTrain.InitialSpeed > 0 && playerTrain.MovementState != AITrain.AI_MOVEMENT_STATE.STATION_STOP)
583584
{
584585
playerTrain.InitializeMoving();

Source/Orts.Simulation/Simulation/Timetables/TTTrain.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ public override bool CheckStationPosition(PlatformDetails thisPlatform, int stat
22312231
// check position
22322232

22332233
float margin = 0.0f;
2234-
if (AI.PreUpdate)
2234+
if (Simulator.PreUpdate)
22352235
margin = 2.0f * clearingDistanceM; // allow margin in pre-update due to low update rate
22362236

22372237
int stationIndex = ValidRoute[0].GetRouteIndex(stationTCSectionIndex, PresentPosition[0].RouteListIndex);
@@ -5741,7 +5741,7 @@ public override void UpdateRunningState(float elapsedClockSeconds)
57415741
public void DelayedStartMoving(AI_START_MOVEMENT reason)
57425742
{
57435743
// do not apply delayed restart while running in pre-update
5744-
if (AI.PreUpdate)
5744+
if (Simulator.PreUpdate)
57455745
{
57465746
RestdelayS = 0.0f;
57475747
DelayedStart = false;
@@ -11820,7 +11820,7 @@ public void TTCouple(TTTrain attachTrain, bool thisTrainFront, bool attachTrainF
1182011820
}
1182111821

1182211822
// if not in preupdate there must be an engine
11823-
if (AI.Simulator.PlayerLocomotive == null && !AI.PreUpdate)
11823+
if (AI.Simulator.PlayerLocomotive == null && !Simulator.PreUpdate)
1182411824
{
1182511825
throw new InvalidDataException("Can't find player locomotive in " + attachTrain.Name);
1182611826
}

Source/Orts.Simulation/Simulation/Timetables/TTTurntable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2027,7 +2027,7 @@ public bool AutoRequestExit(int reqExit, Traveller.TravellerDirection entryPathD
20272027

20282028
// in prerun, also perform turntable update as simulation is not yet running
20292029
// also perform turntable update if this is not the active moving table
2030-
bool performUpdate = parentTrain.AI.PreUpdate;
2030+
bool performUpdate = parentTrain.Simulator.PreUpdate;
20312031
if (!performUpdate)
20322032
{
20332033
if (parentPool.Simulatorref.ActiveMovingTable == null)

0 commit comments

Comments
 (0)