Skip to content

Commit 539be1f

Browse files
committed
Automatic merge of T1.5.1-687-gd279e384a and 18 pull requests
- Pull request #570 at c59c788: Experimental glTF 2.0 support with PBR lighting - Pull request #839 at d00beb9: First phase of https://blueprints.launchpad.net/or/+spec/additional-cruise-control-parameters - Pull request #865 at 67014b7: Dispatcher window improvements - Pull request #874 at f8dbeab: Dynamic brake controller refactoring - Pull request #875 at 43bf33e: Bug fix for https://bugs.launchpad.net/or/+bug/2036346 Player train switching doesn't work with 3D cabs - Pull request #876 at f92de76: docs: add source for documents previously on website to source Documentation folder - Pull request #878 at 25f5e06: Implement Polach Adhesion - Pull request #882 at d8a1c4d: Blueprint/train car operations UI window - Pull request #883 at edcc2dd: SwitchPanel disconnect/connect handling - Pull request #885 at c81447b: feat: Add notifications to Menu - Pull request #886 at 7c4922e: Scene viewer extension to TrackViewer - Pull request #887 at 4665bda: docs: Document projects, assemblies, namespaces - Pull request #888 at d7daf62: docs: Document player application model - Pull request #889 at 43341cf: No speed update - Pull request #892 at 1f5ba4c: Signal Function OPP_SIG_ID_TRAINPATH - Pull request #893 at bf8876b: Signal errors - Pull request #894 at 794fddf: Correct Decrease Colour - Pull request #896 at 5866028: First implementation of https://blueprints.launchpad.net/or/+spec/specific-sounds-for-ai-trains
20 parents b92a7bf + d279e38 + c59c788 + d00beb9 + 67014b7 + f8dbeab + 43bf33e + f92de76 + 25f5e06 + d8a1c4d + edcc2dd + c81447b + 7c4922e + 4665bda + d7daf62 + 43341cf + 1f5ba4c + bf8876b + 794fddf + 5866028 commit 539be1f

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,14 +780,20 @@ public void SetWagonCommandReceivers(MSTSWagon wag)
780780
public TrainCar SetPlayerLocomotive(Train playerTrain)
781781
{
782782
TrainCar PlayerLocomotive = null;
783+
var leadFound = false;
783784
foreach (TrainCar car in playerTrain.Cars)
784785
if (car.IsDriveable) // first loco is the one the player drives
785786
{
787+
if (!leadFound)
788+
{
786789
PlayerLocomotive = car;
787790
playerTrain.LeadLocomotive = car;
788791
playerTrain.InitializeBrakes();
789792
PlayerLocomotive.LocalThrottlePercent = playerTrain.AITrainThrottlePercent;
790-
break;
793+
PlayerLocomotive.SignalEvent(Event.PlayerTrainLeadLoco);
794+
leadFound = true;
795+
}
796+
else car.SignalEvent(Event.PlayerTrainHelperLoco);
791797
}
792798
if (PlayerLocomotive == null)
793799
throw new InvalidDataException("Can't find player locomotive in activity");
@@ -1010,6 +1016,7 @@ private void FinishCoupling(Train drivenTrain, Train train, bool couple_to_front
10101016
}
10111017
drivenTrain.Cars.Clear();
10121018
AI.TrainsToRemoveFromAI.Add((AITrain)train);
1019+
PlayerLocomotive.SignalEvent(Event.PlayerTrainHelperLoco);
10131020
PlayerLocomotive = SetPlayerLocomotive(train);
10141021
(train as AITrain).SwitchToPlayerControl();
10151022
OnPlayerLocomotiveChanged();
@@ -1124,6 +1131,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
11241131
{
11251132
drivenTrain.Cars.Add(car);
11261133
car.Train = drivenTrain;
1134+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
11271135
}
11281136
FinishRearCoupling(drivenTrain, train, true);
11291137
return;
@@ -1154,6 +1162,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
11541162
drivenTrain.Cars.Add(car);
11551163
car.Train = drivenTrain;
11561164
car.Flipped = !car.Flipped;
1165+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
11571166
}
11581167
FinishRearCoupling(drivenTrain, train, false);
11591168
return;
@@ -1218,6 +1227,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
12181227
TrainCar car = train.Cars[i];
12191228
drivenTrain.Cars.Insert(i, car);
12201229
car.Train = drivenTrain;
1230+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
12211231
}
12221232
if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
12231233
FinishFrontCoupling(drivenTrain, train, lead, true);
@@ -1251,6 +1261,7 @@ public void CheckForCoupling(Train drivenTrain, float elapsedClockSeconds)
12511261
drivenTrain.Cars.Insert(0, car);
12521262
car.Train = drivenTrain;
12531263
car.Flipped = !car.Flipped;
1264+
if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
12541265
}
12551266
if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
12561267
FinishFrontCoupling(drivenTrain, train, lead, false);
@@ -1838,6 +1849,19 @@ public void UncoupleBehind(TrainCar car, bool keepFront)
18381849
train2.TrainType = Train.TRAINTYPE.AI;
18391850
train.IncorporatedTrainNo = -1;
18401851
train2.MUDirection = Direction.Forward;
1852+
var leadFound = false;
1853+
foreach (var trainCar in train2.Cars)
1854+
{
1855+
if (trainCar is MSTSLocomotive)
1856+
{
1857+
if (!leadFound)
1858+
{
1859+
trainCar.SignalEvent(Event.AITrainLeadLoco);
1860+
leadFound = true;
1861+
}
1862+
}
1863+
else trainCar.SignalEvent(Event.AITrainHelperLoco);
1864+
}
18411865
}
18421866
else train2.TrainType = Train.TRAINTYPE.STATIC;
18431867
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
@@ -1399,6 +1399,14 @@ protected void LoadCarSound(string wagonFolderSlash, string filename)
13991399
try
14001400
{
14011401
Viewer.SoundProcess.AddSoundSource(this, new SoundSource(Viewer, MSTSWagon, smsFilePath));
1402+
if (MSTSWagon is MSTSLocomotive && MSTSWagon.Train != null && MSTSWagon.Train.TrainType == Simulation.Physics.Train.TRAINTYPE.AI)
1403+
{
1404+
if (MSTSWagon.CarID == MSTSWagon.Train.Cars[0].CarID)
1405+
// Lead loco, enable AI train trigger
1406+
MSTSWagon.SignalEvent(Orts.Common.Event.AITrainLeadLoco);
1407+
// AI train helper loco
1408+
else MSTSWagon.SignalEvent(Orts.Common.Event.AITrainHelperLoco);
1409+
}
14021410
}
14031411
catch (Exception error)
14041412
{

0 commit comments

Comments
 (0)