Skip to content

Commit

Permalink
Add a PreUpdate variable to scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharpe49 committed Aug 21, 2021
1 parent 0641acb commit 054e318
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Source/Orts.Simulation/Common/Scripting/Common.cs
Expand Up @@ -15,10 +15,10 @@
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.

using System;
using Orts.Common;
using Orts.Simulation;
using Orts.Simulation.RollingStocks;
using System;

namespace ORTS.Scripting.Api
{
Expand All @@ -35,6 +35,10 @@ public abstract class AbstractScriptClass
/// Clock value (in seconds) for the simulation. Starts with a value = 0.
/// </summary>
public Func<float> GameTime;
/// <summary>
/// Simulator is in pre-update mode (update during loading screen).
/// </summary>
public Func<bool> PreUpdate;
}
/// <summary>
/// Base class for scripts related to train subsystems.
Expand Down
14 changes: 7 additions & 7 deletions Source/Orts.Simulation/Simulation/AIs/AI.cs
Expand Up @@ -54,7 +54,8 @@ public class AI
public List<AITrain> AutoGenTrains = new List<AITrain>(); // auto-generated trains
public double clockTime; // clock time : local time before activity start, common time from simulator after start
private bool localTime; // if true : clockTime is local time
public bool PreUpdate; // if true : running in pre-update phase
public bool PreUpdate => Simulator.PreUpdate; // if true : running in pre-update phase

public List<AITrain> TrainsToRemove = new List<AITrain>();
public List<AITrain> TrainsToAdd = new List<AITrain>();
public List<AITrain> TrainsToRemoveFromAI = new List<AITrain>();
Expand Down Expand Up @@ -341,14 +342,14 @@ private void PrerunAI(CancellationToken cancellation)

clockTime = firstAITime - 1.0f;
localTime = true;
PreUpdate = true;
Simulator.PreUpdate = true;

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

AIUpdate((float)(runTime - clockTime), PreUpdate);
AIUpdate((float)(runTime - clockTime), Simulator.PreUpdate);
Simulator.Signals.Update(true);
clockTime = runTime;
if (cancellation.IsCancellationRequested) return; // ping watchdog process
Expand All @@ -371,14 +372,14 @@ private void PrerunAI(int playerTrainOriginalTrain, TTTrain.FormCommand playerTr

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

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

if (activeTrains)
{
Expand Down Expand Up @@ -625,8 +626,7 @@ private void PrerunAI(int playerTrainOriginalTrain, TTTrain.FormCommand playerTr
}

Trace.Write("\n");
PreUpdate = false;

Simulator.PreUpdate = false;
}

/// <summary>
Expand Down
Expand Up @@ -343,6 +343,7 @@ public void Initialize()
// AbstractScriptClass
Script.ClockTime = () => (float)Simulator.ClockTime;
Script.GameTime = () => (float)Simulator.GameTime;
Script.PreUpdate = () => Simulator.PreUpdate;
Script.DistanceM = () => Locomotive.DistanceM;
Script.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;
Expand Down
Expand Up @@ -131,6 +131,7 @@ public void Initialize()
// AbstractScriptClass
Script.ClockTime = () => (float)Simulator.ClockTime;
Script.GameTime = () => (float)Simulator.GameTime;
Script.PreUpdate = () => Simulator.PreUpdate;
Script.DistanceM = () => Locomotive.DistanceM;
Script.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;
Expand Down
Expand Up @@ -253,6 +253,7 @@ protected virtual void AssignScriptFunctions()
// AbstractScriptClass
AbstractScript.ClockTime = () => (float)Simulator.ClockTime;
AbstractScript.GameTime = () => (float)Simulator.GameTime;
AbstractScript.PreUpdate = () => Simulator.PreUpdate;
AbstractScript.DistanceM = () => Locomotive.DistanceM;
AbstractScript.SpeedMpS = () => Math.Abs(Locomotive.SpeedMpS);
AbstractScript.Confirm = Locomotive.Simulator.Confirmer.Confirm;
Expand Down
Expand Up @@ -302,6 +302,7 @@ protected virtual void AssignScriptFunctions()
// AbstractScriptClass
Script.ClockTime = () => (float)Simulator.ClockTime;
Script.GameTime = () => (float)Simulator.GameTime;
Script.PreUpdate = () => Simulator.PreUpdate;
Script.DistanceM = () => Wagon.DistanceM;
Script.SpeedMpS = () => Math.Abs(Wagon.SpeedMpS);
Script.Confirm = Simulator.Confirmer.Confirm;
Expand Down
Expand Up @@ -114,6 +114,7 @@ public void Initialize()
// AbstractScriptClass
Script.ClockTime = () => (float)Simulator.ClockTime;
Script.GameTime = () => (float)Simulator.GameTime;
Script.PreUpdate = () => Simulator.PreUpdate;
Script.DistanceM = () => Locomotive.DistanceM;
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;
Script.Message = Locomotive.Simulator.Confirmer.Message;
Expand Down
Expand Up @@ -260,6 +260,7 @@ public void Initialize()
// AbstractScriptClass
Script.ClockTime = () => (float)Simulator.ClockTime;
Script.GameTime = () => (float)Simulator.GameTime;
Script.PreUpdate = () => Simulator.PreUpdate;
Script.DistanceM = () => Locomotive.DistanceM;
Script.Confirm = Locomotive.Simulator.Confirmer.Confirm;
Script.Message = Locomotive.Simulator.Confirmer.Message;
Expand Down
Expand Up @@ -334,6 +334,7 @@ internal void AttachToHead(SignalHead signalHead)
// Build AbstractScriptClass API functions
ClockTime = () => (float)SignalObject.signalRef.Simulator.ClockTime;
GameTime = () => (float)SignalObject.signalRef.Simulator.GameTime;
PreUpdate = () => SignalObject.signalRef.Simulator.PreUpdate;
}

// Functions to be implemented in script
Expand Down
3 changes: 2 additions & 1 deletion Source/Orts.Simulation/Simulation/Simulator.cs
Expand Up @@ -90,6 +90,7 @@ public class Simulator
public string ActivityFileName;
public string TimetableFileName;
public bool TimetableMode;
public bool PreUpdate;
public ActivityFile Activity;
public Activity ActivityRun;
public TrackDatabaseFile TDB;
Expand Down Expand Up @@ -578,7 +579,7 @@ AITrain InitializeAPTrains(CancellationToken cancellation)
if (playerTrain != null)
{
var validPosition = playerTrain.PostInit(); // place player train after pre-running of AI trains
if (validPosition && AI != null) AI.PreUpdate = false;
if (validPosition && AI != null) PreUpdate = false;
if (playerTrain.InitialSpeed > 0 && playerTrain.MovementState != AITrain.AI_MOVEMENT_STATE.STATION_STOP)
{
playerTrain.InitializeMoving();
Expand Down

0 comments on commit 054e318

Please sign in to comment.