Merge ~carlosanit1/or/+git/orts-git-test:feature/AITrainSound into ~twpol/or/+git/orts-git-test:develop

Proposed by Carlo Santucci
Status: Merged
Approved by: James Ross
Approved revision: a2dd038550a4ef6a13012a05c029cd13f2656783
Merge reported by: James Ross
Merged at revision: a2dd038550a4ef6a13012a05c029cd13f2656783
Proposed branch: ~carlosanit1/or/+git/orts-git-test:feature/AITrainSound
Merge into: ~twpol/or/+git/orts-git-test:develop
Diff against target: 279 lines (+93/-8)
5 files modified
Source/Orts.Simulation/Common/Events.cs (+14/-1)
Source/Orts.Simulation/Simulation/AIs/AITrain.cs (+38/-2)
Source/Orts.Simulation/Simulation/Physics/Train.cs (+2/-0)
Source/Orts.Simulation/Simulation/Simulator.cs (+31/-5)
Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs (+8/-0)
Reviewer Review Type Date Requested Status
James Ross Approve
Review via email: mp+328470@code.launchpad.net

Description of the change

This branch adds sound triggers specific for AI trains.

To post a comment you must log in.
Revision history for this message
James Ross (twpol) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/Source/Orts.Simulation/Common/Events.cs b/Source/Orts.Simulation/Common/Events.cs
2index de0b851..6b56b95 100644
3--- a/Source/Orts.Simulation/Common/Events.cs
4+++ b/Source/Orts.Simulation/Common/Events.cs
5@@ -27,6 +27,10 @@ namespace Orts.Common
6 public enum Event
7 {
8 None,
9+ AITrainApproachingStation,
10+ AITrainHelperLoco,
11+ AITrainLeadLoco,
12+ AITrainLeavingStation,
13 BellOff,
14 BellOn,
15 BlowerChange,
16@@ -88,6 +92,8 @@ namespace Orts.Common
17 PermissionDenied,
18 PermissionGranted,
19 PermissionToDepart,
20+ PlayerTrainHelperLoco,
21+ PlayerTrainLeadLoco,
22 ReverserChange,
23 ReverserToForwardBackward,
24 ReverserToNeutral,
25@@ -323,7 +329,14 @@ namespace Orts.Common
26 case 161: return Event.CabLightSwitchToggle;
27 case 162: return Event.CabRadioOn;
28 case 163: return Event.CabRadioOff;
29- //
30+ //
31+ case 165: return Event.AITrainLeadLoco;
32+ case 166: return Event.AITrainHelperLoco;
33+ case 167: return Event.PlayerTrainLeadLoco;
34+ case 168: return Event.PlayerTrainHelperLoco;
35+ case 169: return Event.AITrainApproachingStation;
36+ case 170: return Event.AITrainLeavingStation;
37+
38
39 default: return 0;
40 }
41diff --git a/Source/Orts.Simulation/Simulation/AIs/AITrain.cs b/Source/Orts.Simulation/Simulation/AIs/AITrain.cs
42index cd872b9..a2cb0ac 100644
43--- a/Source/Orts.Simulation/Simulation/AIs/AITrain.cs
44+++ b/Source/Orts.Simulation/Simulation/AIs/AITrain.cs
45@@ -72,6 +72,8 @@ namespace Orts.Simulation.AIs
46 public float doorOpenDelay = -1f;
47 public float doorCloseAdvance = -1f;
48
49+ public bool approachTriggerSet = false; // station approach trigger for AI trains has been set
50+
51
52 public enum AI_MOVEMENT_STATE
53 {
54@@ -237,6 +239,7 @@ namespace Orts.Simulation.AIs
55 UncondAttach = inf.ReadBoolean();
56 doorCloseAdvance = inf.ReadSingle();
57 doorOpenDelay = inf.ReadSingle();
58+ approachTriggerSet = inf.ReadBoolean();
59 if ( !Simulator.TimetableMode && doorOpenDelay <= 0 && doorCloseAdvance > 0 && Simulator.OpenDoorsInAITrains &&
60 MovementState == AI_MOVEMENT_STATE.STATION_STOP && StationStops.Count > 0)
61 {
62@@ -323,6 +326,7 @@ namespace Orts.Simulation.AIs
63 outf.Write(UncondAttach);
64 outf.Write(doorCloseAdvance);
65 outf.Write(doorOpenDelay);
66+ outf.Write(approachTriggerSet);
67 if (ServiceDefinition != null) ServiceDefinition.Save(outf);
68 else outf.Write(-1);
69 }
70@@ -1321,6 +1325,7 @@ namespace Orts.Simulation.AIs
71 AIActionItem newAction = new AIActionItem(null, AIActionItem.AI_ACTION_TYPE.STATION_STOP);
72 newAction.SetParam(distancesM[1], 0.0f, distancesM[0], DistanceTravelledM);
73 requiredActions.InsertAction(newAction);
74+ approachTriggerSet = false;
75
76 #if DEBUG_REPORTS
77 if (StationStops[0].ActualStopType == StationStop.STOPTYPE.STATION_STOP)
78@@ -2095,6 +2100,8 @@ namespace Orts.Simulation.AIs
79 Delay = TimeSpan.FromSeconds((presentTime - thisStation.DepartTime) % (24 * 3600));
80 }
81
82+ if (Cars[0] is MSTSLocomotive) Cars[0].SignalEvent(Event.AITrainLeavingStation);
83+
84 #if DEBUG_REPORTS
85 DateTime baseDTd = new DateTime();
86 DateTime depTime = baseDTd.AddSeconds(presentTime);
87@@ -2668,6 +2675,13 @@ namespace Orts.Simulation.AIs
88 }
89 }
90
91+ if (nextActionInfo != null && nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.STATION_STOP &&
92+ distanceToGoM < 150 + StationStops[0].PlatformItem.Length && !approachTriggerSet)
93+ {
94+ if (Cars[0] is MSTSLocomotive) Cars[0].SignalEvent(Event.AITrainApproachingStation);
95+ approachTriggerSet = true;
96+ }
97+
98 if (nextActionInfo != null && nextActionInfo.NextAction == AIActionItem.AI_ACTION_TYPE.STATION_STOP)
99 creepDistanceM = 0.0f;
100 if (nextActionInfo == null && requiredSpeedMpS == 0)
101@@ -4403,6 +4417,7 @@ namespace Orts.Simulation.AIs
102 Simulator.OnPlayerLocomotiveChanged();
103 AI.AITrains.Add(this);
104 }
105+ else if (attachTrain is AITrain) RedefineAITriggers(attachTrain as AITrain);
106 if (!UncondAttach)
107 {
108 RemoveTrain();
109@@ -4509,6 +4524,7 @@ namespace Orts.Simulation.AIs
110 AddTrackSections();
111 ResetActions(true);
112 physicsUpdate(0);
113+ RedefineAITriggers(this);
114
115 }
116
117@@ -4745,7 +4761,8 @@ namespace Orts.Simulation.AIs
118 // move WP, if any, just under the loco;
119 AuxActionsContain.MoveAuxActionAfterReversal(this);
120 ResetActions(true);
121-
122+ RedefineAITriggers(this);
123+ if (attachTrain is AITrain) RedefineAITriggers(attachTrain as AITrain);
124 physicsUpdate(0); // stop the wheels from moving etc
125
126 }
127@@ -6561,7 +6578,26 @@ namespace Orts.Simulation.AIs
128 }
129 }
130 }
131-
132+ //================================================================================================//
133+ /// <summary>
134+ /// Redefine sound triggers for AI trains
135+ /// </summary>
136+ public void RedefineAITriggers (AITrain train)
137+ {
138+ var leadFound = false;
139+ foreach (var car in train.Cars)
140+ {
141+ if (car is MSTSLocomotive)
142+ {
143+ if (!leadFound)
144+ {
145+ car.SignalEvent(Event.AITrainLeadLoco);
146+ leadFound = true;
147+ }
148+ else car.SignalEvent(Event.AITrainHelperLoco);
149+ }
150+ }
151+ }
152 }
153
154
155diff --git a/Source/Orts.Simulation/Simulation/Physics/Train.cs b/Source/Orts.Simulation/Simulation/Physics/Train.cs
156index 6fa8a5c..d36d68a 100644
157--- a/Source/Orts.Simulation/Simulation/Physics/Train.cs
158+++ b/Source/Orts.Simulation/Simulation/Physics/Train.cs
159@@ -1377,6 +1377,8 @@ namespace Orts.Simulation.Physics
160 // Update flipped state of each car.
161 for (var i = 0; i < Cars.Count; i++)
162 Cars[i].Flipped = !Cars[i].Flipped;
163+ // if AI train redefine first loco for sound
164+ if (TrainType == TRAINTYPE.AI) (this as AITrain).RedefineAITriggers(this as AITrain);
165 }
166
167
168diff --git a/Source/Orts.Simulation/Simulation/Simulator.cs b/Source/Orts.Simulation/Simulation/Simulator.cs
169index d131e6d..745c8f6 100644
170--- a/Source/Orts.Simulation/Simulation/Simulator.cs
171+++ b/Source/Orts.Simulation/Simulation/Simulator.cs
172@@ -569,15 +569,23 @@ namespace Orts.Simulation
173 public TrainCar SetPlayerLocomotive(Train playerTrain)
174 {
175 TrainCar PlayerLocomotive = null;
176+ var leadFound = false;
177 foreach (TrainCar car in playerTrain.Cars)
178+ {
179 if (car.IsDriveable) // first loco is the one the player drives
180 {
181- PlayerLocomotive = car;
182- playerTrain.LeadLocomotive = car;
183- playerTrain.InitializeBrakes();
184- PlayerLocomotive.LocalThrottlePercent = playerTrain.AITrainThrottlePercent;
185- break;
186+ if (!leadFound)
187+ {
188+ PlayerLocomotive = car;
189+ playerTrain.LeadLocomotive = car;
190+ playerTrain.InitializeBrakes();
191+ PlayerLocomotive.LocalThrottlePercent = playerTrain.AITrainThrottlePercent;
192+ PlayerLocomotive.SignalEvent(Event.PlayerTrainLeadLoco);
193+ leadFound = true;
194+ }
195+ else car.SignalEvent(Event.PlayerTrainHelperLoco);
196 }
197+ }
198 if (PlayerLocomotive == null)
199 throw new InvalidDataException("Can't find player locomotive in activity");
200 return PlayerLocomotive;
201@@ -783,6 +791,7 @@ namespace Orts.Simulation
202 }
203 drivenTrain.Cars.Clear();
204 AI.TrainsToRemoveFromAI.Add((AITrain)train);
205+ PlayerLocomotive.SignalEvent(Event.PlayerTrainHelperLoco);
206 PlayerLocomotive = SetPlayerLocomotive(train);
207 (train as AITrain).SwitchToPlayerControl();
208 OnPlayerLocomotiveChanged();
209@@ -890,6 +899,7 @@ namespace Orts.Simulation
210 {
211 drivenTrain.Cars.Add(car);
212 car.Train = drivenTrain;
213+ if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
214 }
215 FinishRearCoupling(drivenTrain, train, true);
216 return;
217@@ -913,6 +923,7 @@ namespace Orts.Simulation
218 drivenTrain.Cars.Add(car);
219 car.Train = drivenTrain;
220 car.Flipped = !car.Flipped;
221+ if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
222 }
223 FinishRearCoupling(drivenTrain, train, false);
224 return;
225@@ -950,6 +961,7 @@ namespace Orts.Simulation
226 TrainCar car = train.Cars[i];
227 drivenTrain.Cars.Insert(i, car);
228 car.Train = drivenTrain;
229+ if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
230 }
231 if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
232 FinishFrontCoupling(drivenTrain, train, lead, true);
233@@ -975,6 +987,7 @@ namespace Orts.Simulation
234 drivenTrain.Cars.Insert(0, car);
235 car.Train = drivenTrain;
236 car.Flipped = !car.Flipped;
237+ if (car is MSTSLocomotive) car.SignalEvent(Event.PlayerTrainHelperLoco);
238 }
239 if (drivenTrain.LeadLocomotiveIndex >= 0) drivenTrain.LeadLocomotiveIndex += train.Cars.Count;
240 FinishFrontCoupling(drivenTrain, train, lead, false);
241@@ -1556,6 +1569,19 @@ namespace Orts.Simulation
242 train2.TrainType = Train.TRAINTYPE.AI;
243 train.IncorporatedTrainNo = -1;
244 train2.MUDirection = Direction.Forward;
245+ var leadFound = false;
246+ foreach (var trainCar in train2.Cars)
247+ {
248+ if (trainCar is MSTSLocomotive)
249+ {
250+ if (!leadFound)
251+ {
252+ trainCar.SignalEvent(Event.AITrainLeadLoco);
253+ leadFound = true;
254+ }
255+ }
256+ else trainCar.SignalEvent(Event.AITrainHelperLoco);
257+ }
258 }
259 else train2.TrainType = Train.TRAINTYPE.STATIC;
260 train2.LeadLocomotive = null;
261diff --git a/Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs b/Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs
262index 9f9b972..625c473 100644
263--- a/Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs
264+++ b/Source/RunActivity/Viewer3D/RollingStock/MSTSWagonViewer.cs
265@@ -629,6 +629,14 @@ namespace Orts.Viewer3D.RollingStock
266 try
267 {
268 Viewer.SoundProcess.AddSoundSource(this, new SoundSource(Viewer, MSTSWagon, smsFilePath));
269+ if (MSTSWagon is MSTSLocomotive && MSTSWagon.Train != null && MSTSWagon.Train.TrainType == Simulation.Physics.Train.TRAINTYPE.AI)
270+ {
271+ if (MSTSWagon.CarID == MSTSWagon.Train.Cars[0].CarID)
272+ // Lead loco, enable AI train trigger
273+ MSTSWagon.SignalEvent(Orts.Common.Event.AITrainLeadLoco);
274+ // AI train helper loco
275+ else MSTSWagon.SignalEvent(Orts.Common.Event.AITrainHelperLoco);
276+ }
277 }
278 catch (Exception error)
279 {

Subscribers

People subscribed via source and target branches

to all changes: