Skip to content

Commit e2dd24e

Browse files
committed
Rebasing...
1 parent 99a47c9 commit e2dd24e

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,
@@ -561,6 +567,14 @@ public static Event From(Source source, int eventID)
561567
case 321: return Event.BoosterCylinderCocksOpen;
562568
case 322: return Event.BoosterCylinderCocksClose;
563569

570+
// AI train related events
571+
case 330: return Event.AITrainLeadLoco;
572+
case 331: return Event.AITrainHelperLoco;
573+
case 332: return Event.PlayerTrainLeadLoco;
574+
case 333: return Event.PlayerTrainHelperLoco;
575+
case 334: return Event.AITrainApproachingStation;
576+
case 335: return Event.AITrainLeavingStation;
577+
564578
default: return 0;
565579
}
566580
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 DoorOpenTimer = -1f;
7676
public float DoorCloseTimer = -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
DoorCloseTimer = inf.ReadSingle();
248249
DoorOpenTimer = inf.ReadSingle();
250+
ApproachTriggerSet = inf.ReadBoolean();
249251
if (!Simulator.TimetableMode && DoorOpenTimer <= 0 && DoorCloseTimer > 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(DoorCloseTimer);
341343
outf.Write(DoorOpenTimer);
344+
outf.Write(ApproachTriggerSet);
342345
if (LevelCrossingHornPattern != null)
343346
{
344347
outf.Write(0);
@@ -1302,6 +1305,7 @@ public virtual void SetNextStationAction(bool fromAutopilotSwitch = false)
13021305
AIActionItem newAction = new AIActionItem(null, AIActionItem.AI_ACTION_TYPE.STATION_STOP);
13031306
newAction.SetParam(distancesM[1], 0.0f, distancesM[0], DistanceTravelledM);
13041307
requiredActions.InsertAction(newAction);
1308+
ApproachTriggerSet = false;
13051309

13061310
#if DEBUG_REPORTS
13071311
if (StationStops[0].ActualStopType == StationStop.STOPTYPE.STATION_STOP)
@@ -2061,6 +2065,7 @@ public virtual void UpdateStationState(float elapsedClockSeconds, int presentTim
20612065

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

20652070
#if DEBUG_REPORTS
20662071
DateTime baseDTd = new DateTime();
@@ -2622,6 +2627,13 @@ public virtual void UpdateBrakingState(float elapsedClockSeconds, int presentTim
26222627
}
26232628
}
26242629

2630+
if (nextActionInfo != null && nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.STATION_STOP &&
2631+
distanceToGoM < 150 + StationStops[0].PlatformItem.Length && !ApproachTriggerSet)
2632+
{
2633+
if (Cars[0] is MSTSLocomotive) Cars[0].SignalEvent(Event.AITrainApproachingStation);
2634+
ApproachTriggerSet = true;
2635+
}
2636+
26252637
if (nextActionInfo != null && nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.STATION_STOP)
26262638
creepDistanceM = 0.0f;
26272639
if (nextActionInfo == null && requiredSpeedMpS == 0)
@@ -4369,6 +4381,7 @@ public void CoupleAI(Train attachTrain, bool thisTrainFront, bool attachTrainFro
43694381
AI.AITrains.Add(this);
43704382
AI.aiListChanged = true;
43714383
}
4384+
else if (attachTrain is AITrain) RedefineAITriggers(attachTrain as AITrain);
43724385
if (!UncondAttach)
43734386
{
43744387
RemoveTrain();
@@ -4477,6 +4490,7 @@ public void CoupleAIToStatic(Train attachTrain, bool thisTrainFront, bool attach
44774490
AddTrackSections();
44784491
ResetActions(true);
44794492
physicsUpdate(0);
4493+
RedefineAITriggers(this);
44804494
}
44814495

44824496
//================================================================================================//
@@ -4718,7 +4732,8 @@ public void TerminateCoupling(Train attachTrain, bool thisTrainFront, bool attac
47184732
// Move WP, if any, just under the loco;
47194733
AuxActionsContain.MoveAuxActionAfterReversal(this);
47204734
ResetActions(true);
4721-
4735+
RedefineAITriggers(this);
4736+
if (attachTrain is AITrain) RedefineAITriggers(attachTrain as AITrain);
47224737
physicsUpdate(0);// Stop the wheels from moving etc
47234738

47244739
}
@@ -6580,6 +6595,27 @@ public void RestartWaitingTrain(RestartWaitingTrain restartWaitingTrain)
65806595
}
65816596
}
65826597

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

65856621

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,8 @@ public void ReverseCars()
15381538
// Update flipped state of each car.
15391539
for (var i = 0; i < Cars.Count; i++)
15401540
Cars[i].Flipped = !Cars[i].Flipped;
1541+
// if AI train redefine first loco for sound
1542+
if (TrainType == TRAINTYPE.AI) (this as AITrain).RedefineAITriggers(this as AITrain);
15411543
}
15421544

15431545
/// <summary>

Source/Orts.Simulation/Simulation/Simulator.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -760,14 +760,20 @@ public void SetWagonCommandReceivers(MSTSWagon wag)
760760
public TrainCar SetPlayerLocomotive(Train playerTrain)
761761
{
762762
TrainCar PlayerLocomotive = null;
763+
var leadFound = false;
763764
foreach (TrainCar car in playerTrain.Cars)
764765
if (car.IsDriveable) // first loco is the one the player drives
765766
{
766-
PlayerLocomotive = car;
767-
playerTrain.LeadLocomotive = car;
768-
playerTrain.InitializeBrakes();
769-
PlayerLocomotive.LocalThrottlePercent = playerTrain.AITrainThrottlePercent;
770-
break;
767+
if (!leadFound)
768+
{
769+
PlayerLocomotive = car;
770+
playerTrain.LeadLocomotive = car;
771+
playerTrain.InitializeBrakes();
772+
PlayerLocomotive.LocalThrottlePercent = playerTrain.AITrainThrottlePercent;
773+
PlayerLocomotive.SignalEvent(Event.PlayerTrainLeadLoco);
774+
leadFound = true;
775+
}
776+
else car.SignalEvent(Event.PlayerTrainHelperLoco);
771777
}
772778
if (PlayerLocomotive == null)
773779
throw new InvalidDataException("Can't find player locomotive in activity");
@@ -987,6 +993,7 @@ private void FinishCoupling(Train drivenTrain, Train train, bool couple_to_front
987993
}
988994
drivenTrain.Cars.Clear();
989995
AI.TrainsToRemoveFromAI.Add((AITrain)train);
996+
PlayerLocomotive.SignalEvent(Event.PlayerTrainHelperLoco);
990997
PlayerLocomotive = SetPlayerLocomotive(train);
991998
(train as AITrain).SwitchToPlayerControl();
992999
OnPlayerLocomotiveChanged();
@@ -1101,6 +1108,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
11011108
{
11021109
drivenTrain.Cars.Add(car);
11031110
car.Train = drivenTrain;
1111+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
11041112
}
11051113
FinishRearCoupling(drivenTrain, train, true);
11061114
return;
@@ -1131,6 +1139,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
11311139
drivenTrain.Cars.Add(car);
11321140
car.Train = drivenTrain;
11331141
car.Flipped = !car.Flipped;
1142+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
11341143
}
11351144
FinishRearCoupling(drivenTrain, train, false);
11361145
return;
@@ -1195,6 +1204,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
11951204
TrainCar car = train.Cars[i];
11961205
drivenTrain.Cars.Insert(i, car);
11971206
car.Train = drivenTrain;
1207+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
11981208
}
11991209
if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
12001210
FinishFrontCoupling(drivenTrain, train, lead, true);
@@ -1228,6 +1238,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
12281238
drivenTrain.Cars.Insert(0, car);
12291239
car.Train = drivenTrain;
12301240
car.Flipped = !car.Flipped;
1241+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
12311242
}
12321243
if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
12331244
FinishFrontCoupling(drivenTrain, train, lead, false);
@@ -1836,6 +1847,19 @@ public void UncoupleBehind(TrainCar car, bool keepFront)
18361847
train2.TrainType = Train.TRAINTYPE.AI;
18371848
train.IncorporatedTrainNo = -1;
18381849
train2.MUDirection = Direction.Forward;
1850+
var leadFound = false;
1851+
foreach (var trainCar in train2.Cars)
1852+
{
1853+
if (trainCar is MSTSLocomotive)
1854+
{
1855+
if (!leadFound)
1856+
{
1857+
trainCar.SignalEvent(Event.AITrainLeadLoco);
1858+
leadFound = true;
1859+
}
1860+
}
1861+
else trainCar.SignalEvent(Event.AITrainHelperLoco);
1862+
}
18391863
}
18401864
else train2.TrainType = Train.TRAINTYPE.STATIC;
18411865
train2.LeadLocomotive = null;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ public override void Save(BinaryWriter outf)
585585
outf.Write(UncondAttach);
586586
outf.Write(DoorCloseTimer);
587587
outf.Write(DoorOpenTimer);
588+
outf.Write(ApproachTriggerSet);
588589
// Dummy for level crossing horn pattern
589590
outf.Write(-1);
590591

Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,14 @@ protected void LoadCarSound(string wagonFolderSlash, string filename)
14601460
try
14611461
{
14621462
Viewer.SoundProcess.AddSoundSource(this, new SoundSource(Viewer, MSTSWagon, smsFilePath));
1463+
if (MSTSWagon is MSTSLocomotive && MSTSWagon.Train != null && MSTSWagon.Train.TrainType == Simulation.Physics.Train.TRAINTYPE.AI)
1464+
{
1465+
if (MSTSWagon.CarID == MSTSWagon.Train.Cars[0].CarID)
1466+
// Lead loco, enable AI train trigger
1467+
MSTSWagon.SignalEvent(Orts.Common.Event.AITrainLeadLoco);
1468+
// AI train helper loco
1469+
else MSTSWagon.SignalEvent(Orts.Common.Event.AITrainHelperLoco);
1470+
}
14631471
}
14641472
catch (Exception error)
14651473
{

0 commit comments

Comments
 (0)