Skip to content

Commit a208b1d

Browse files
committed
Refactor door handling and add TCS hooks
1 parent 9f08bc0 commit a208b1d

File tree

14 files changed

+337
-120
lines changed

14 files changed

+337
-120
lines changed

Source/Orts.Simulation/Common/Commands.cs

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

11971197
public override void Redo()
11981198
{
1199-
if (Receiver.GetCabFlipped()) Receiver.ToggleDoorsRight();
1200-
else Receiver.ToggleDoorsLeft();
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);
12011202
// Report();
12021203
}
12031204
}
@@ -1215,8 +1216,9 @@ public ToggleDoorsRightCommand(CommandLog log)
12151216

12161217
public override void Redo()
12171218
{
1218-
if (Receiver.GetCabFlipped()) Receiver.ToggleDoorsLeft();
1219-
else Receiver.ToggleDoorsRight();
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);
12201222
// Report();
12211223
}
12221224
}

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using System.IO;
2020
using ORTS.Common;
2121
using Orts.Common;
22-
using Orts.Simulation.RollingStocks;
22+
using Orts.Simulation.RollingStocks.SubSystems;
2323
using ORTS.Scripting.Api.ETCS;
2424

2525
namespace ORTS.Scripting.Api
@@ -188,14 +188,6 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
188188
/// </summary>
189189
public Func<bool> IsRearCab;
190190
/// <summary>
191-
/// True if left doors are open
192-
/// </summary>
193-
public Func<bool> AreLeftDoorsOpen;
194-
/// <summary>
195-
/// True if right doors are open
196-
/// </summary>
197-
public Func<bool> AreRightDoorsOpen;
198-
/// <summary>
199191
/// True if train brake controller is in emergency position, otherwise false.
200192
/// </summary>
201193
public Func<bool> IsBrakeEmergency;
@@ -229,6 +221,14 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
229221
/// </summary>
230222
public Func<bool> ArePantographsDown;
231223
/// <summary>
224+
/// Get left doors state
225+
/// </summary>
226+
public Func<DoorState> GetLeftDoorState;
227+
/// <summary>
228+
/// Get right doors state
229+
/// </summary>
230+
public Func<DoorState> GetRightDoorState;
231+
/// <summary>
232232
/// Returns throttle percent
233233
/// </summary>
234234
public Func<float> ThrottlePercent;
@@ -396,6 +396,24 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
396396
/// </summary>
397397
public Action<bool> SetHorn;
398398
/// <summary>
399+
/// Open or close left doors
400+
/// bool: true for closing order, false for opening order
401+
/// </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;
412+
/// <summary>
413+
/// Lock right doors so they cannot be opened
414+
/// </summary>
415+
public Action<bool> LockRightDoors;
416+
/// <summary>
399417
/// Trigger Alert1 sound event
400418
/// </summary>
401419
public Action TriggerSoundAlert1;
@@ -680,6 +698,22 @@ public enum TCSEvent
680698
/// Traction cut-off relay has been opened.
681699
/// </summary>
682700
TractionCutOffRelayOpen,
701+
/// <summary>
702+
/// Left doors have been opened.
703+
/// </summary>
704+
LeftDoorsOpen,
705+
/// <summary>
706+
/// Left doors have been closed.
707+
/// </summary>
708+
LeftDoorsClosed,
709+
/// <summary>
710+
/// Right doors have been opened.
711+
/// </summary>
712+
RightDoorsOpen,
713+
/// <summary>
714+
/// Right doors have been closed.
715+
/// </summary>
716+
RightDoorsClosed
683717
}
684718

685719
/// <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-
if (t.LeadLocomotive != null) ((MSTSWagon)(t.LeadLocomotive)).ToggleDoorsLeft();
2227+
t.ToggleDoors(false, EventState == 1);
22282228
MPManager.BroadCast(this.ToString()); //if the server, will broadcast
22292229
}
22302230
else if (EventName == "DOORR")
22312231
{
2232-
if (t.LeadLocomotive != null) ((MSTSWagon)(t.LeadLocomotive)).ToggleDoorsRight();
2232+
t.ToggleDoors(true, EventState == 1);
22332233
MPManager.BroadCast(this.ToString()); //if the server, will broadcast
22342234
}
22352235
else if (EventName == "MIRRORS")

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16284,16 +16284,62 @@ public void ToggleDoors(bool right, bool open)
1628416284
foreach (TrainCar car in Cars)
1628516285
{
1628616286
var mstsWagon = car as MSTSWagon;
16287-
if (!car.Flipped && right || car.Flipped && !right)
16287+
if ((!car.Flipped && right) || (car.Flipped && !right))
1628816288
{
16289-
mstsWagon.DoorRightOpen = open;
16289+
mstsWagon.RightDoor.SetDoor(open);
1629016290
}
1629116291
else
1629216292
{
16293-
mstsWagon.DoorLeftOpen = open;
16293+
mstsWagon.LeftDoor.SetDoor(open);
1629416294
}
16295-
mstsWagon.SignalEvent(open ? Event.DoorOpen : Event.DoorClose); // hook for sound trigger
1629616295
}
16296+
if (Simulator.PlayerLocomotive?.Train == this && MPManager.IsMultiPlayer()) MPManager.Notify((new MSGEvent(MPManager.GetUserName(), right ? "DORR" : "DOORL", open ? 1 : 0)).ToString());
16297+
}
16298+
16299+
/// <summary>
16300+
/// LockDoors
16301+
/// Locks doors of a train so they cannot be opened
16302+
/// Parameters: right = true if right doors; lck = true if locking
16303+
/// </summary>
16304+
public void LockDoors(bool right, bool lck)
16305+
{
16306+
foreach (TrainCar car in Cars)
16307+
{
16308+
var mstsWagon = car as MSTSWagon;
16309+
if ((!car.Flipped && right) || (car.Flipped && !right))
16310+
{
16311+
mstsWagon.RightDoor.SetDoorLock(lck);
16312+
}
16313+
else
16314+
{
16315+
mstsWagon.LeftDoor.SetDoorLock(lck);
16316+
}
16317+
}
16318+
}
16319+
16320+
/// <summary>
16321+
/// GetDoorState
16322+
/// Returns status of doors of a train
16323+
/// Parameter: right = true if right doors
16324+
/// </summary>
16325+
public DoorState GetDoorState(bool right)
16326+
{
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
16337+
{
16338+
wagonDoorState = mstsWagon.LeftDoor.State;
16339+
}
16340+
if (state < wagonDoorState) state = wagonDoorState;
16341+
}
16342+
return state;
1629716343
}
1629816344

1629916345
/// <summary>

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,7 +5089,9 @@ public virtual float GetDataOf(CabViewControl cvc)
50895089
}
50905090
case CABViewControlTypes.DOORS_DISPLAY:
50915091
{
5092-
data = DoorLeftOpen | DoorRightOpen ? 1 : 0;
5092+
var leftDoorState = Train.GetDoorState(false);
5093+
var rightDoorState = Train.GetDoorState(true);
5094+
data = (leftDoorState != DoorState.Closed || rightDoorState != DoorState.Closed)? 1 : 0;
50935095
break;
50945096
}
50955097
case CABViewControlTypes.SANDERS:
@@ -5306,10 +5308,12 @@ public virtual float GetDataOf(CabViewControl cvc)
53065308
data = CabLightOn ? 1 : 0;
53075309
break;
53085310
case CABViewControlTypes.ORTS_LEFTDOOR:
5309-
data = GetCabFlipped() ? (DoorRightOpen ? 1 : 0) : DoorLeftOpen ? 1 : 0;
5310-
break;
53115311
case CABViewControlTypes.ORTS_RIGHTDOOR:
5312-
data = GetCabFlipped() ? (DoorLeftOpen ? 1 : 0) : DoorRightOpen ? 1 : 0;
5312+
{
5313+
bool right = (cvc.ControlType == CABViewControlTypes.ORTS_RIGHTDOOR) ^ Flipped ^ GetCabFlipped();
5314+
var state = Train.GetDoorState(right);
5315+
data = (state == DoorState.Opening || state == DoorState.Open) ? 1 : 0;
5316+
}
53135317
break;
53145318
case CABViewControlTypes.ORTS_MIRRORS:
53155319
data = MirrorOpen ? 1 : 0;

Source/Orts.Simulation/Simulation/RollingStocks/MSTSWagon.cs

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ public class MSTSWagon : TrainCar
6868
{
6969
public Pantographs Pantographs;
7070
public ScriptedPassengerCarPowerSupply PassengerCarPowerSupply => PowerSupply as ScriptedPassengerCarPowerSupply;
71-
public bool DoorLeftOpen;
72-
public bool DoorRightOpen;
71+
72+
public Door RightDoor;
73+
public Door LeftDoor;
7374
public bool MirrorOpen;
7475
public bool UnloadingPartsOpen;
7576
public bool WaitForAnimationReady; // delay counter to start loading/unliading is on;
@@ -345,6 +346,8 @@ public MSTSWagon(Simulator simulator, string wagFilePath)
345346
: base(simulator, wagFilePath)
346347
{
347348
Pantographs = new Pantographs(this);
349+
RightDoor = new Door(this, true);
350+
LeftDoor = new Door(this, false);
348351
}
349352

350353
public void Load()
@@ -951,6 +954,8 @@ public void GetMeasurementUnits()
951954
public override void Initialize()
952955
{
953956
Pantographs.Initialize();
957+
RightDoor.Initialize();
958+
LeftDoor.Initialize();
954959
PassengerCarPowerSupply?.Initialize();
955960

956961
base.Initialize();
@@ -1375,7 +1380,6 @@ public virtual void Parse(string lowercasetoken, STFReader stf)
13751380
case "wagon(ortspantographs":
13761381
Pantographs.Parse(lowercasetoken, stf);
13771382
break;
1378-
13791383
case "wagon(ortspowersupply":
13801384
case "wagon(ortspowerondelay":
13811385
case "wagon(ortsbattery(mode":
@@ -1540,6 +1544,8 @@ public virtual void Copy(MSTSWagon copy)
15401544
foreach (MSTSCoupling coupler in copy.Couplers)
15411545
Couplers.Add(coupler);
15421546
Pantographs.Copy(copy.Pantographs);
1547+
LeftDoor.Copy(copy.LeftDoor);
1548+
RightDoor.Copy(copy.RightDoor);
15431549
if (copy.FreightAnimations != null)
15441550
{
15451551
FreightAnimations = new FreightAnimations(copy.FreightAnimations, this);
@@ -1673,6 +1679,8 @@ public override void Save(BinaryWriter outf)
16731679
foreach (MSTSCoupling coupler in Couplers)
16741680
coupler.Save(outf);
16751681
Pantographs.Save(outf);
1682+
LeftDoor.Save(outf);
1683+
RightDoor.Save(outf);
16761684
PassengerCarPowerSupply?.Save(outf);
16771685
if (FreightAnimations != null)
16781686
{
@@ -1725,6 +1733,8 @@ public override void Restore(BinaryReader inf)
17251733
MaxHandbrakeForceN = inf.ReadSingle();
17261734
Couplers = ReadCouplersFromSave(inf).ToList();
17271735
Pantographs.Restore(inf);
1736+
LeftDoor.Restore(inf);
1737+
RightDoor.Restore(inf);
17281738
PassengerCarPowerSupply?.Restore(inf);
17291739
if (FreightAnimations != null)
17301740
{
@@ -1897,6 +1907,9 @@ public override void Update(float elapsedClockSeconds)
18971907

18981908
Pantographs.Update(elapsedClockSeconds);
18991909

1910+
LeftDoor.Update(elapsedClockSeconds);
1911+
RightDoor.Update(elapsedClockSeconds);
1912+
19001913
MSTSBrakeSystem.Update(elapsedClockSeconds);
19011914

19021915
// Updates freight load animations when defined in WAG file - Locomotive and Tender load animation are done independently in UpdateTenderLoad() & UpdateLocomotiveLoadPhysics()
@@ -3345,70 +3358,6 @@ public override void SignalEvent(PowerSupplyEvent evt, int id)
33453358
base.SignalEvent(evt, id);
33463359
}
33473360

3348-
public void ToggleDoorsLeft()
3349-
{
3350-
DoorLeftOpen = !DoorLeftOpen;
3351-
if (Simulator.PlayerLocomotive == this || Train.LeadLocomotive == this) // second part for remote trains
3352-
{//inform everyone else in the train
3353-
foreach (var car in Train.Cars)
3354-
{
3355-
var mstsWagon = car as MSTSWagon;
3356-
if (car != this && mstsWagon != null)
3357-
{
3358-
if (!car.Flipped ^ Flipped)
3359-
{
3360-
mstsWagon.DoorLeftOpen = DoorLeftOpen;
3361-
mstsWagon.SignalEvent(DoorLeftOpen ? Event.DoorOpen : Event.DoorClose); // hook for sound trigger
3362-
}
3363-
else
3364-
{
3365-
mstsWagon.DoorRightOpen = DoorLeftOpen;
3366-
mstsWagon.SignalEvent(DoorLeftOpen ? Event.DoorOpen : Event.DoorClose); // hook for sound trigger
3367-
}
3368-
}
3369-
}
3370-
if (DoorLeftOpen) SignalEvent(Event.DoorOpen); // hook for sound trigger
3371-
else SignalEvent(Event.DoorClose);
3372-
if (Simulator.PlayerLocomotive == this)
3373-
{
3374-
if (!GetCabFlipped()) Simulator.Confirmer.Confirm(CabControl.DoorsLeft, DoorLeftOpen ? CabSetting.On : CabSetting.Off);
3375-
else Simulator.Confirmer.Confirm(CabControl.DoorsRight, DoorLeftOpen ? CabSetting.On : CabSetting.Off);
3376-
}
3377-
}
3378-
}
3379-
3380-
public void ToggleDoorsRight()
3381-
{
3382-
DoorRightOpen = !DoorRightOpen;
3383-
if (Simulator.PlayerLocomotive == this || Train.LeadLocomotive == this) // second part for remote trains
3384-
{ //inform everyone else in the train
3385-
foreach (TrainCar car in Train.Cars)
3386-
{
3387-
var mstsWagon = car as MSTSWagon;
3388-
if (car != this && mstsWagon != null)
3389-
{
3390-
if (!car.Flipped ^ Flipped)
3391-
{
3392-
mstsWagon.DoorRightOpen = DoorRightOpen;
3393-
mstsWagon.SignalEvent(DoorRightOpen ? Event.DoorOpen : Event.DoorClose); // hook for sound trigger
3394-
}
3395-
else
3396-
{
3397-
mstsWagon.DoorLeftOpen = DoorRightOpen;
3398-
mstsWagon.SignalEvent(DoorRightOpen ? Event.DoorOpen : Event.DoorClose); // hook for sound trigger
3399-
}
3400-
}
3401-
}
3402-
if (DoorRightOpen) SignalEvent(Event.DoorOpen); // hook for sound trigger
3403-
else SignalEvent(Event.DoorClose);
3404-
if (Simulator.PlayerLocomotive == this)
3405-
{
3406-
if (!GetCabFlipped()) Simulator.Confirmer.Confirm(CabControl.DoorsRight, DoorRightOpen ? CabSetting.On : CabSetting.Off);
3407-
else Simulator.Confirmer.Confirm(CabControl.DoorsLeft, DoorRightOpen ? CabSetting.On : CabSetting.Off);
3408-
}
3409-
}
3410-
}
3411-
34123361
public void ToggleMirrors()
34133362
{
34143363
MirrorOpen = !MirrorOpen;

0 commit comments

Comments
 (0)