Skip to content

Commit 7a9fc6c

Browse files
committed
Refactor code due to satisfy review
1 parent 1dbf5a5 commit 7a9fc6c

File tree

13 files changed

+125
-112
lines changed

13 files changed

+125
-112
lines changed

Source/Orts.Simulation/Common/Commands.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,9 +1196,9 @@ public ToggleDoorsLeftCommand(CommandLog log)
11961196

11971197
public override void Redo()
11981198
{
1199-
bool right = Receiver.GetCabFlipped() ^ Receiver.Flipped;
1200-
var state = Receiver.Train.GetDoorState(right);
1201-
Receiver.Train.ToggleDoors(right, state == DoorState.Closed || state == DoorState.Closing);
1199+
var side = Receiver.GetCabFlipped() ^ Receiver.Flipped ? DoorSide.Right : DoorSide.Left;
1200+
var state = Receiver.Train.DoorState(side);
1201+
Receiver.Train.SetDoors(side, state <= DoorState.Closing);
12021202
// Report();
12031203
}
12041204
}
@@ -1216,9 +1216,9 @@ public ToggleDoorsRightCommand(CommandLog log)
12161216

12171217
public override void Redo()
12181218
{
1219-
bool right = !Receiver.GetCabFlipped() ^ Receiver.Flipped;
1220-
var state = Receiver.Train.GetDoorState(right);
1221-
Receiver.Train.ToggleDoors(right, state == DoorState.Closed || state == DoorState.Closing);
1219+
var side = Receiver.GetCabFlipped() ^ Receiver.Flipped ? DoorSide.Left : DoorSide.Right;
1220+
var state = Receiver.Train.DoorState(side);
1221+
Receiver.Train.SetDoors(side, state <= DoorState.Closing);
12221222
// Report();
12231223
}
12241224
}

Source/Orts.Simulation/Common/Scripting/TrainControlSystem.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,9 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
221221
/// </summary>
222222
public Func<bool> ArePantographsDown;
223223
/// <summary>
224-
/// Get left doors state
224+
/// Get doors state
225225
/// </summary>
226-
public Func<DoorState> GetLeftDoorState;
227-
/// <summary>
228-
/// Get right doors state
229-
/// </summary>
230-
public Func<DoorState> GetRightDoorState;
226+
public Func<DoorSide, DoorState> DoorState;
231227
/// <summary>
232228
/// Returns throttle percent
233229
/// </summary>
@@ -396,23 +392,15 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
396392
/// </summary>
397393
public Action<bool> SetHorn;
398394
/// <summary>
399-
/// Open or close left doors
395+
/// Open or close doors
396+
/// DoorSide: side for which doors will be opened or closed
400397
/// bool: true for closing order, false for opening order
401398
/// </summary>
402-
public Action<bool> ToggleLeftDoors;
403-
/// <summary>
404-
/// Open or close right doors
405-
/// bool: true for closing order, false for opening order
406-
/// </summary>
407-
public Action<bool> ToggleRightDoors;
408-
/// <summary>
409-
/// Lock left doors so they cannot be opened
410-
/// </summary>
411-
public Action<bool> LockLeftDoors;
399+
public Action<DoorSide, bool> SetDoors;
412400
/// <summary>
413-
/// Lock right doors so they cannot be opened
401+
/// Lock doors so they cannot be opened
414402
/// </summary>
415-
public Action<bool> LockRightDoors;
403+
public Action<DoorSide, bool> LockDoors;
416404
/// <summary>
417405
/// Trigger Alert1 sound event
418406
/// </summary>

Source/Orts.Simulation/MultiPlayer/Message.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,12 +2224,12 @@ public override void HandleMsg()
22242224
}
22252225
else if (EventName == "DOORL")
22262226
{
2227-
t.ToggleDoors(false, EventState == 1);
2227+
t.SetDoors(DoorSide.Left, EventState == 1);
22282228
MPManager.BroadCast(this.ToString()); //if the server, will broadcast
22292229
}
22302230
else if (EventName == "DOORR")
22312231
{
2232-
t.ToggleDoors(true, EventState == 1);
2232+
t.SetDoors(DoorSide.Right, EventState == 1);
22332233
MPManager.BroadCast(this.ToString()); //if the server, will broadcast
22342234
}
22352235
else if (EventName == "MIRRORS")

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
using Orts.MultiPlayer;
4040
using Orts.Simulation.Physics;
4141
using Orts.Simulation.RollingStocks;
42+
using Orts.Simulation.RollingStocks.SubSystems;
4243
using Orts.Simulation.RollingStocks.SubSystems.Brakes.MSTS;
4344
using Orts.Simulation.Signalling;
4445
using ORTS.Common;
@@ -260,12 +261,12 @@ public AITrain(Simulator simulator, BinaryReader inf, AI airef)
260261
if (thisStation.PlatformItem.PlatformSide[0])
261262
{
262263
//open left doors
263-
ToggleDoors(frontIsFront, true);
264+
SetDoors(frontIsFront ? DoorSide.Right : DoorSide.Left, true);
264265
}
265266
if (thisStation.PlatformItem.PlatformSide[1])
266267
{
267268
//open right doors
268-
ToggleDoors(!frontIsFront, true);
269+
SetDoors(frontIsFront ? DoorSide.Left : DoorSide.Right, true);
269270
}
270271
}
271272
}
@@ -1958,12 +1959,12 @@ public virtual void UpdateStationState(float elapsedClockSeconds, int presentTim
19581959
if (thisStation.PlatformItem.PlatformSide[0])
19591960
{
19601961
//open left doors
1961-
ToggleDoors(frontIsFront, true);
1962+
SetDoors(frontIsFront ? DoorSide.Right : DoorSide.Left, true);
19621963
}
19631964
if (thisStation.PlatformItem.PlatformSide[1])
19641965
{
19651966
//open right doors
1966-
ToggleDoors(!frontIsFront, true);
1967+
SetDoors(frontIsFront ? DoorSide.Left : DoorSide.Right, true);
19671968
}
19681969
}
19691970
}
@@ -1974,13 +1975,13 @@ public virtual void UpdateStationState(float elapsedClockSeconds, int presentTim
19741975
{
19751976
if (thisStation.PlatformItem.PlatformSide[0])
19761977
{
1977-
//close left doors
1978-
ToggleDoors(frontIsFront, false);
1978+
//open left doors
1979+
SetDoors(frontIsFront ? DoorSide.Right : DoorSide.Left, false);
19791980
}
19801981
if (thisStation.PlatformItem.PlatformSide[1])
19811982
{
1982-
//close right doors
1983-
ToggleDoors(!frontIsFront, false);
1983+
//open right doors
1984+
SetDoors(frontIsFront ? DoorSide.Left : DoorSide.Right, false);
19841985
}
19851986
}
19861987
}

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16275,71 +16275,74 @@ public virtual void TestAbsDelay(ref int delay, int correctedTime)
1627516275
}
1627616276

1627716277
/// <summary>
16278-
/// ToggleDoors
16279-
/// Toggles status of doors of a train
16280-
/// Parameters: right = true if right doors; open = true if opening
16278+
/// SetDoors
16279+
/// Sets status of doors of a train
1628116280
/// </summary>
16282-
public void ToggleDoors(bool right, bool open)
16281+
public void SetDoors(DoorSide side, bool open)
1628316282
{
1628416283
foreach (TrainCar car in Cars)
1628516284
{
1628616285
var mstsWagon = car as MSTSWagon;
16287-
if ((!car.Flipped && right) || (car.Flipped && !right))
16286+
var carSide = car.Flipped ? Doors.FlippedDoorSide(side) : side;
16287+
if (carSide != DoorSide.Left)
1628816288
{
1628916289
mstsWagon.RightDoor.SetDoor(open);
1629016290
}
16291-
else
16291+
if (carSide != DoorSide.Right)
1629216292
{
1629316293
mstsWagon.LeftDoor.SetDoor(open);
1629416294
}
1629516295
}
16296-
if (Simulator.PlayerLocomotive?.Train == this && MPManager.IsMultiPlayer()) MPManager.Notify((new MSGEvent(MPManager.GetUserName(), right ? "DORR" : "DOORL", open ? 1 : 0)).ToString());
16296+
if (Simulator.PlayerLocomotive?.Train == this && MPManager.IsMultiPlayer())
16297+
{
16298+
if (side != DoorSide.Left) MPManager.Notify((new MSGEvent(MPManager.GetUserName(), "DOORR", open ? 1 : 0)).ToString());
16299+
if (side != DoorSide.Right) MPManager.Notify((new MSGEvent(MPManager.GetUserName(), "DOORL", open ? 1 : 0)).ToString());
16300+
}
1629716301
}
1629816302

1629916303
/// <summary>
1630016304
/// LockDoors
1630116305
/// Locks doors of a train so they cannot be opened
1630216306
/// Parameters: right = true if right doors; lck = true if locking
1630316307
/// </summary>
16304-
public void LockDoors(bool right, bool lck)
16308+
public void LockDoors(DoorSide side, bool lck)
1630516309
{
1630616310
foreach (TrainCar car in Cars)
1630716311
{
1630816312
var mstsWagon = car as MSTSWagon;
16309-
if ((!car.Flipped && right) || (car.Flipped && !right))
16313+
var carSide = car.Flipped ? Doors.FlippedDoorSide(side) : side;
16314+
if (carSide != DoorSide.Left)
1631016315
{
1631116316
mstsWagon.RightDoor.SetDoorLock(lck);
1631216317
}
16313-
else
16318+
if (carSide != DoorSide.Right)
1631416319
{
1631516320
mstsWagon.LeftDoor.SetDoorLock(lck);
1631616321
}
1631716322
}
1631816323
}
1631916324

1632016325
/// <summary>
16321-
/// GetDoorState
16326+
/// DoorState
1632216327
/// Returns status of doors of a train
16323-
/// Parameter: right = true if right doors
1632416328
/// </summary>
16325-
public DoorState GetDoorState(bool right)
16329+
public DoorState DoorState(DoorSide side)
1632616330
{
16327-
DoorState state = DoorState.Closed;
16328-
foreach (TrainCar car in Cars)
16329-
{
16330-
var mstsWagon = car as MSTSWagon;
16331-
DoorState wagonDoorState;
16332-
if ((!car.Flipped && right) || (car.Flipped && !right))
16333-
{
16334-
wagonDoorState = mstsWagon.RightDoor.State;
16335-
}
16336-
else
16331+
return Cars.Select(car => {
16332+
var wagon = (car as MSTSWagon);
16333+
var carSide = car.Flipped ? Doors.FlippedDoorSide(side) : side;
16334+
switch(carSide)
1633716335
{
16338-
wagonDoorState = mstsWagon.LeftDoor.State;
16336+
case DoorSide.Left:
16337+
return wagon.Doors.LeftDoor.State;
16338+
case DoorSide.Right:
16339+
return wagon.Doors.RightDoor.State;
16340+
default:
16341+
var left = wagon.Doors.LeftDoor.State;
16342+
var right = wagon.Doors.RightDoor.State;
16343+
return left < right ? right : left;
1633916344
}
16340-
if (state < wagonDoorState) state = wagonDoorState;
16341-
}
16342-
return state;
16345+
}).Max();
1634316346
}
1634416347

1634516348
/// <summary>

Source/Orts.Simulation/Simulation/RollingStocks/MSTSLocomotive.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,9 +5089,7 @@ public virtual float GetDataOf(CabViewControl cvc)
50895089
}
50905090
case CABViewControlTypes.DOORS_DISPLAY:
50915091
{
5092-
var leftDoorState = Train.GetDoorState(false);
5093-
var rightDoorState = Train.GetDoorState(true);
5094-
data = (leftDoorState != DoorState.Closed || rightDoorState != DoorState.Closed)? 1 : 0;
5092+
data = Train.DoorState(DoorSide.Both) != DoorState.Closed ? 1 : 0;
50955093
break;
50965094
}
50975095
case CABViewControlTypes.SANDERS:
@@ -5311,7 +5309,7 @@ public virtual float GetDataOf(CabViewControl cvc)
53115309
case CABViewControlTypes.ORTS_RIGHTDOOR:
53125310
{
53135311
bool right = (cvc.ControlType == CABViewControlTypes.ORTS_RIGHTDOOR) ^ Flipped ^ GetCabFlipped();
5314-
var state = Train.GetDoorState(right);
5312+
var state = Train.DoorState(right ? DoorSide.Right : DoorSide.Left);
53155313
data = (state == DoorState.Opening || state == DoorState.Open) ? 1 : 0;
53165314
}
53175315
break;

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/Door.cs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014, 2015 by the Open Rails project.
1+
// COPYRIGHT 2022 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -14,12 +14,10 @@
1414
//
1515
// You should have received a copy of the GNU General Public License
1616
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17+
using System.IO;
1718
using Orts.Common;
1819
using Orts.Parsers.Msts;
1920
using ORTS.Scripting.Api;
20-
using System;
21-
using System.IO;
22-
using System.Linq;
2321

2422
namespace Orts.Simulation.RollingStocks.SubSystems
2523
{
@@ -28,8 +26,14 @@ public enum DoorState
2826
{
2927
Closed,
3028
Closing,
31-
Opening,
3229
Open,
30+
Opening,
31+
}
32+
public enum DoorSide
33+
{
34+
Left,
35+
Right,
36+
Both
3337
}
3438
public class Doors : ISubSystem<Doors>
3539
{
@@ -38,8 +42,8 @@ public class Doors : ISubSystem<Doors>
3842

3943
public Doors(MSTSWagon wagon)
4044
{
41-
LeftDoor = new Door(wagon, false);
42-
RightDoor = new Door(wagon, true);
45+
LeftDoor = new Door(wagon, DoorSide.Left);
46+
RightDoor = new Door(wagon, DoorSide.Right);
4347
}
4448

4549
public virtual void Parse(string lowercasetoken, STFReader stf)
@@ -99,6 +103,19 @@ public virtual void Update(float elapsedClockSeconds)
99103
LeftDoor.Update(elapsedClockSeconds);
100104
RightDoor.Update(elapsedClockSeconds);
101105
}
106+
107+
public static DoorSide FlippedDoorSide(DoorSide trainSide)
108+
{
109+
switch(trainSide)
110+
{
111+
case DoorSide.Left:
112+
return DoorSide.Right;
113+
case DoorSide.Right:
114+
return DoorSide.Left;
115+
default:
116+
return DoorSide.Both;
117+
}
118+
}
102119
}
103120
public class Door : ISubSystem<Door>
104121
{
@@ -110,17 +127,17 @@ public class Door : ISubSystem<Door>
110127
// Variables
111128
readonly MSTSWagon Wagon;
112129
protected Simulator Simulator => Wagon.Simulator;
113-
public readonly bool RightSide;
130+
public readonly DoorSide Side;
114131
protected Timer OpeningTimer;
115132
protected Timer ClosingTimer;
116133

117134
public DoorState State { get; protected set; } = DoorState.Closed;
118135
public bool Locked {get; protected set; }
119136

120-
public Door(MSTSWagon wagon, bool right)
137+
public Door(MSTSWagon wagon, DoorSide side)
121138
{
122139
Wagon = wagon;
123-
RightSide = right;
140+
Side = side;
124141

125142
OpeningTimer = new Timer(wagon);
126143
ClosingTimer = new Timer(wagon);
@@ -192,19 +209,28 @@ public void SetDoorLock(bool lck)
192209
}
193210
public void SetDoor(bool open)
194211
{
195-
if (!Locked && open && (State == DoorState.Closed || State == DoorState.Closing))
196-
{
197-
State = DoorState.Opening;
198-
Wagon.SignalEvent(Event.DoorOpen);
199-
bool driverRightSide = RightSide ^ Wagon.GetCabFlipped();
200-
Confirm(driverRightSide ? CabControl.DoorsRight : CabControl.DoorsLeft, CabSetting.On);
201-
}
202-
else if (!open && (State == DoorState.Open || State == DoorState.Opening))
212+
switch (State)
203213
{
204-
State = DoorState.Closing;
205-
Wagon.SignalEvent(Event.DoorClose);
206-
bool driverRightSide = RightSide ^ Wagon.GetCabFlipped();
207-
Confirm(driverRightSide ? CabControl.DoorsRight : CabControl.DoorsLeft, CabSetting.Off);
214+
case DoorState.Closed:
215+
case DoorState.Closing:
216+
if (!Locked && open)
217+
{
218+
State = DoorState.Opening;
219+
Wagon.SignalEvent(Event.DoorOpen);
220+
bool driverRightSide = (Side == DoorSide.Right) ^ Wagon.GetCabFlipped();
221+
Confirm(driverRightSide ? CabControl.DoorsRight : CabControl.DoorsLeft, CabSetting.On);
222+
}
223+
break;
224+
case DoorState.Open:
225+
case DoorState.Opening:
226+
if (!open)
227+
{
228+
State = DoorState.Closing;
229+
Wagon.SignalEvent(Event.DoorClose);
230+
bool driverRightSide = (Side == DoorSide.Right) ^ Wagon.GetCabFlipped();
231+
Confirm(driverRightSide ? CabControl.DoorsRight : CabControl.DoorsLeft, CabSetting.Off);
232+
}
233+
break;
208234
}
209235
}
210236
protected void Confirm(CabControl control, CabSetting setting)

0 commit comments

Comments
 (0)