Skip to content

Commit 5866028

Browse files
committed
1 parent d279e38 commit 5866028

File tree

6 files changed

+91
-6
lines changed

6 files changed

+91
-6
lines changed

Source/Orts.Simulation/Common/Events.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public interface EventHandler
2727
public enum Event
2828
{
2929
None,
30+
AITrainApproachingStation,
31+
AITrainHelperLoco,
32+
AITrainLeadLoco,
33+
AITrainLeavingStation,
3034
BatterySwitchOff,
3135
BatterySwitchOn,
3236
BatterySwitchCommandOff,
@@ -123,6 +127,8 @@ public enum Event
123127
PermissionDenied,
124128
PermissionGranted,
125129
PermissionToDepart,
130+
PlayerTrainHelperLoco,
131+
PlayerTrainLeadLoco,
126132
PowerKeyOff,
127133
PowerKeyOn,
128134
ReverserChange,
@@ -550,6 +556,14 @@ public static Event From(Source source, int eventID)
550556

551557
case 320: return Event.SteamBoosterChange;
552558

559+
// AI train related events
560+
case 330: return Event.AITrainLeadLoco;
561+
case 331: return Event.AITrainHelperLoco;
562+
case 332: return Event.PlayerTrainLeadLoco;
563+
case 333: return Event.PlayerTrainHelperLoco;
564+
case 334: return Event.AITrainApproachingStation;
565+
case 335: return Event.AITrainLeavingStation;
566+
553567
default: return 0;
554568
}
555569
case Source.MSTSCrossing:

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class AITrain : Train
7575
public float doorOpenDelay = -1f;
7676
public float doorCloseAdvance = -1f;
7777
public AILevelCrossingHornPattern LevelCrossingHornPattern { get; set; }
78+
public bool ApproachTriggerSet = false; // station approach trigger for AI trains has been set
7879

7980
public float PathLength;
8081

@@ -246,6 +247,7 @@ public AITrain(Simulator simulator, BinaryReader inf, AI airef)
246247
UncondAttach = inf.ReadBoolean();
247248
doorCloseAdvance = inf.ReadSingle();
248249
doorOpenDelay = inf.ReadSingle();
250+
ApproachTriggerSet = inf.ReadBoolean();
249251
if (!Simulator.TimetableMode && doorOpenDelay <= 0 && doorCloseAdvance > 0 && Simulator.OpenDoorsInAITrains &&
250252
MovementState == AI_MOVEMENT_STATE.STATION_STOP && StationStops.Count > 0)
251253
{
@@ -339,6 +341,7 @@ public override void Save(BinaryWriter outf)
339341
outf.Write(UncondAttach);
340342
outf.Write(doorCloseAdvance);
341343
outf.Write(doorOpenDelay);
344+
outf.Write(ApproachTriggerSet);
342345
if (LevelCrossingHornPattern != null)
343346
{
344347
outf.Write(0);
@@ -1300,6 +1303,7 @@ public virtual void SetNextStationAction(bool fromAutopilotSwitch = false)
13001303
AIActionItem newAction = new AIActionItem(null, AIActionItem.AI_ACTION_TYPE.STATION_STOP);
13011304
newAction.SetParam(distancesM[1], 0.0f, distancesM[0], DistanceTravelledM);
13021305
requiredActions.InsertAction(newAction);
1306+
ApproachTriggerSet = false;
13031307

13041308
#if DEBUG_REPORTS
13051309
if (StationStops[0].ActualStopType == StationStop.STOPTYPE.STATION_STOP)
@@ -2060,6 +2064,7 @@ public virtual void UpdateStationState(float elapsedClockSeconds, int presentTim
20602064

20612065
Delay = TimeSpan.FromSeconds((presentTime - thisStation.DepartTime) % (24 * 3600));
20622066
}
2067+
if (Cars[0] is MSTSLocomotive) Cars[0].SignalEvent(Event.AITrainLeavingStation);
20632068

20642069
#if DEBUG_REPORTS
20652070
DateTime baseDTd = new DateTime();
@@ -2621,6 +2626,13 @@ public virtual void UpdateBrakingState(float elapsedClockSeconds, int presentTim
26212626
}
26222627
}
26232628

2629+
if (nextActionInfo != null && nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.STATION_STOP &&
2630+
distanceToGoM < 150 + StationStops[0].PlatformItem.Length && !ApproachTriggerSet)
2631+
{
2632+
if (Cars[0] is MSTSLocomotive) Cars[0].SignalEvent(Event.AITrainApproachingStation);
2633+
ApproachTriggerSet = true;
2634+
}
2635+
26242636
if (nextActionInfo != null && nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.STATION_STOP)
26252637
creepDistanceM = 0.0f;
26262638
if (nextActionInfo == null && requiredSpeedMpS == 0)
@@ -4367,6 +4379,7 @@ public void CoupleAI(Train attachTrain, bool thisTrainFront, bool attachTrainFro
43674379
Simulator.OnPlayerLocomotiveChanged();
43684380
AI.AITrains.Add(this);
43694381
}
4382+
else if (attachTrain is AITrain) RedefineAITriggers(attachTrain as AITrain);
43704383
if (!UncondAttach)
43714384
{
43724385
RemoveTrain();
@@ -4475,6 +4488,7 @@ public void CoupleAIToStatic(Train attachTrain, bool thisTrainFront, bool attach
44754488
AddTrackSections();
44764489
ResetActions(true);
44774490
physicsUpdate(0);
4491+
RedefineAITriggers(this);
44784492
}
44794493

44804494
//================================================================================================//
@@ -4716,7 +4730,8 @@ public void TerminateCoupling(Train attachTrain, bool thisTrainFront, bool attac
47164730
// Move WP, if any, just under the loco;
47174731
AuxActionsContain.MoveAuxActionAfterReversal(this);
47184732
ResetActions(true);
4719-
4733+
RedefineAITriggers(this);
4734+
if (attachTrain is AITrain) RedefineAITriggers(attachTrain as AITrain);
47204735
physicsUpdate(0);// Stop the wheels from moving etc
47214736

47224737
}
@@ -6578,6 +6593,27 @@ public void RestartWaitingTrain(RestartWaitingTrain restartWaitingTrain)
65786593
}
65796594
}
65806595

6596+
//================================================================================================//
6597+
/// <summary>
6598+
/// Redefine sound triggers for AI trains
6599+
/// </summary>
6600+
public void RedefineAITriggers(AITrain train)
6601+
{
6602+
var leadFound = false;
6603+
foreach (var car in train.Cars)
6604+
{
6605+
if (car is MSTSLocomotive)
6606+
{
6607+
if (!leadFound)
6608+
{
6609+
car.SignalEvent(Event.AITrainLeadLoco);
6610+
leadFound = true;
6611+
}
6612+
else car.SignalEvent(Event.AITrainHelperLoco);
6613+
}
6614+
}
6615+
}
6616+
65816617
}
65826618

65836619

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,8 @@ public void ReverseCars()
15181518
// Update flipped state of each car.
15191519
for (var i = 0; i < Cars.Count; i++)
15201520
Cars[i].Flipped = !Cars[i].Flipped;
1521+
// if AI train redefine first loco for sound
1522+
if (TrainType == TRAINTYPE.AI) (this as AITrain).RedefineAITriggers(this as AITrain);
15211523
}
15221524

15231525
/// <summary>

Source/Orts.Simulation/Simulation/Simulator.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,14 +742,20 @@ public void SetWagonCommandReceivers(MSTSWagon wag)
742742
public TrainCar SetPlayerLocomotive(Train playerTrain)
743743
{
744744
TrainCar PlayerLocomotive = null;
745+
var leadFound = false;
745746
foreach (TrainCar car in playerTrain.Cars)
746747
if (car.IsDriveable) // first loco is the one the player drives
747748
{
748-
PlayerLocomotive = car;
749-
playerTrain.LeadLocomotive = car;
750-
playerTrain.InitializeBrakes();
751-
PlayerLocomotive.LocalThrottlePercent = playerTrain.AITrainThrottlePercent;
752-
break;
749+
if (!leadFound)
750+
{
751+
PlayerLocomotive = car;
752+
playerTrain.LeadLocomotive = car;
753+
playerTrain.InitializeBrakes();
754+
PlayerLocomotive.LocalThrottlePercent = playerTrain.AITrainThrottlePercent;
755+
PlayerLocomotive.SignalEvent(Event.PlayerTrainLeadLoco);
756+
leadFound = true;
757+
}
758+
else car.SignalEvent(Event.PlayerTrainHelperLoco);
753759
}
754760
if (PlayerLocomotive == null)
755761
throw new InvalidDataException("Can't find player locomotive in activity");
@@ -969,6 +975,7 @@ private void FinishCoupling(Train drivenTrain, Train train, bool couple_to_front
969975
}
970976
drivenTrain.Cars.Clear();
971977
AI.TrainsToRemoveFromAI.Add((AITrain)train);
978+
PlayerLocomotive.SignalEvent(Event.PlayerTrainHelperLoco);
972979
PlayerLocomotive = SetPlayerLocomotive(train);
973980
(train as AITrain).SwitchToPlayerControl();
974981
OnPlayerLocomotiveChanged();
@@ -1083,6 +1090,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
10831090
{
10841091
drivenTrain.Cars.Add(car);
10851092
car.Train = drivenTrain;
1093+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
10861094
}
10871095
FinishRearCoupling(drivenTrain, train, true);
10881096
return;
@@ -1113,6 +1121,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
11131121
drivenTrain.Cars.Add(car);
11141122
car.Train = drivenTrain;
11151123
car.Flipped = !car.Flipped;
1124+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
11161125
}
11171126
FinishRearCoupling(drivenTrain, train, false);
11181127
return;
@@ -1177,6 +1186,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
11771186
TrainCar car = train.Cars[i];
11781187
drivenTrain.Cars.Insert(i, car);
11791188
car.Train = drivenTrain;
1189+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
11801190
}
11811191
if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
11821192
FinishFrontCoupling(drivenTrain, train, lead, true);
@@ -1210,6 +1220,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
12101220
drivenTrain.Cars.Insert(0, car);
12111221
car.Train = drivenTrain;
12121222
car.Flipped = !car.Flipped;
1223+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
12131224
}
12141225
if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
12151226
FinishFrontCoupling(drivenTrain, train, lead, false);
@@ -1797,6 +1808,19 @@ public void UncoupleBehind(TrainCar car, bool keepFront)
17971808
train2.TrainType = Train.TRAINTYPE.AI;
17981809
train.IncorporatedTrainNo = -1;
17991810
train2.MUDirection = Direction.Forward;
1811+
var leadFound = false;
1812+
foreach (var trainCar in train2.Cars)
1813+
{
1814+
if (trainCar is MSTSLocomotive)
1815+
{
1816+
if (!leadFound)
1817+
{
1818+
trainCar.SignalEvent(Event.AITrainLeadLoco);
1819+
leadFound = true;
1820+
}
1821+
}
1822+
else trainCar.SignalEvent(Event.AITrainHelperLoco);
1823+
}
18001824
}
18011825
else train2.TrainType = Train.TRAINTYPE.STATIC;
18021826
train2.LeadLocomotive = null;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ public override void Save(BinaryWriter outf)
583583
outf.Write(UncondAttach);
584584
outf.Write(doorCloseAdvance);
585585
outf.Write(doorOpenDelay);
586+
outf.Write(ApproachTriggerSet);
586587
// Dummy for level crossing horn pattern
587588
outf.Write(-1);
588589

Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,14 @@ protected void LoadCarSound(string wagonFolderSlash, string filename)
14021402
try
14031403
{
14041404
Viewer.SoundProcess.AddSoundSource(this, new SoundSource(Viewer, MSTSWagon, smsFilePath));
1405+
if (MSTSWagon is MSTSLocomotive && MSTSWagon.Train != null && MSTSWagon.Train.TrainType == Simulation.Physics.Train.TRAINTYPE.AI)
1406+
{
1407+
if (MSTSWagon.CarID == MSTSWagon.Train.Cars[0].CarID)
1408+
// Lead loco, enable AI train trigger
1409+
MSTSWagon.SignalEvent(Orts.Common.Event.AITrainLeadLoco);
1410+
// AI train helper loco
1411+
else MSTSWagon.SignalEvent(Orts.Common.Event.AITrainHelperLoco);
1412+
}
14051413
}
14061414
catch (Exception error)
14071415
{

0 commit comments

Comments
 (0)