Skip to content

Commit 0db4bb3

Browse files
authored
Merge pull request #706 from cesarBLG/tcs-extensions
Extended door functionality
2 parents 7decf71 + 91bcfa2 commit 0db4bb3

File tree

16 files changed

+436
-129
lines changed

16 files changed

+436
-129
lines changed

Source/Documentation/Manual/features-rollingstock.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,22 @@ shape movement, however for greater accuracy the modeler can add specific values
12171217
``ORTSLengthAirHose``. In addition the length values suggested in the Derailment Coefficient should
12181218
also be added.
12191219

1220+
Passenger doors
1221+
===============
1222+
1223+
.. index:: ORTSDoors
1224+
1225+
Passenger doors are opened and closed (by default) using the ``<Q>`` and ``<Shift+Q>`` keys.
1226+
It is possible to add opening and closing delays, which can be useful to delay the indication of
1227+
"Doors closed" until all doors are fully closed.
1228+
The delays can be added inserting the following block in the wagon section of any
1229+
ENG or WAG file::
1230+
1231+
ORTSDoors (
1232+
ClosingDelay ( 5s )
1233+
OpeningDelay ( 1s )
1234+
)
1235+
12201236

12211237
C# engine scripting
12221238
===================

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+
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);
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+
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);
12201222
// Report();
12211223
}
12221224
}

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

Lines changed: 31 additions & 1 deletion
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
@@ -221,6 +221,10 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
221221
/// </summary>
222222
public Func<bool> ArePantographsDown;
223223
/// <summary>
224+
/// Get doors state
225+
/// </summary>
226+
public Func<DoorSide, DoorState> CurrentDoorState;
227+
/// <summary>
224228
/// Returns throttle percent
225229
/// </summary>
226230
public Func<float> ThrottlePercent;
@@ -388,6 +392,16 @@ public abstract class TrainControlSystem : AbstractTrainScriptClass
388392
/// </summary>
389393
public Action<bool> SetHorn;
390394
/// <summary>
395+
/// Open or close doors
396+
/// DoorSide: side for which doors will be opened or closed
397+
/// bool: true for closing order, false for opening order
398+
/// </summary>
399+
public Action<DoorSide, bool> SetDoors;
400+
/// <summary>
401+
/// Lock doors so they cannot be opened
402+
/// </summary>
403+
public Action<DoorSide, bool> LockDoors;
404+
/// <summary>
391405
/// Trigger Alert1 sound event
392406
/// </summary>
393407
public Action TriggerSoundAlert1;
@@ -672,6 +686,22 @@ public enum TCSEvent
672686
/// Traction cut-off relay has been opened.
673687
/// </summary>
674688
TractionCutOffRelayOpen,
689+
/// <summary>
690+
/// Left doors have been opened.
691+
/// </summary>
692+
LeftDoorsOpen,
693+
/// <summary>
694+
/// Left doors have been closed.
695+
/// </summary>
696+
LeftDoorsClosed,
697+
/// <summary>
698+
/// Right doors have been opened.
699+
/// </summary>
700+
RightDoorsOpen,
701+
/// <summary>
702+
/// Right doors have been closed.
703+
/// </summary>
704+
RightDoorsClosed
675705
}
676706

677707
/// <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.SetDoors(DoorSide.Left, 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.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: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16275,27 +16275,76 @@ 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
{
16289-
mstsWagon.DoorRightOpen = open;
16289+
mstsWagon.RightDoor.SetDoor(open);
1629016290
}
16291-
else
16291+
if (carSide != DoorSide.Right)
16292+
{
16293+
mstsWagon.LeftDoor.SetDoor(open);
16294+
}
16295+
}
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+
}
16301+
}
16302+
16303+
/// <summary>
16304+
/// LockDoors
16305+
/// Locks doors of a train so they cannot be opened
16306+
/// Parameters: right = true if right doors; lck = true if locking
16307+
/// </summary>
16308+
public void LockDoors(DoorSide side, bool lck)
16309+
{
16310+
foreach (TrainCar car in Cars)
16311+
{
16312+
var mstsWagon = car as MSTSWagon;
16313+
var carSide = car.Flipped ? Doors.FlippedDoorSide(side) : side;
16314+
if (carSide != DoorSide.Left)
16315+
{
16316+
mstsWagon.RightDoor.SetDoorLock(lck);
16317+
}
16318+
if (carSide != DoorSide.Right)
1629216319
{
16293-
mstsWagon.DoorLeftOpen = open;
16320+
mstsWagon.LeftDoor.SetDoorLock(lck);
1629416321
}
16295-
mstsWagon.SignalEvent(open ? Event.DoorOpen : Event.DoorClose); // hook for sound trigger
1629616322
}
1629716323
}
1629816324

16325+
/// <summary>
16326+
/// DoorState
16327+
/// Returns status of doors of a train
16328+
/// </summary>
16329+
public DoorState DoorState(DoorSide side)
16330+
{
16331+
return Cars.Select(car => {
16332+
var wagon = (car as MSTSWagon);
16333+
var carSide = car.Flipped ? Doors.FlippedDoorSide(side) : side;
16334+
switch(carSide)
16335+
{
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;
16344+
}
16345+
}).Max();
16346+
}
16347+
1629916348
/// <summary>
1630016349
/// Check if it's time to have a failed car or locomotive
1630116350
/// </summary>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5561,7 +5561,7 @@ public virtual float GetDataOf(CabViewControl cvc)
55615561
}
55625562
case CABViewControlTypes.DOORS_DISPLAY:
55635563
{
5564-
data = DoorLeftOpen | DoorRightOpen ? 1 : 0;
5564+
data = Train.DoorState(DoorSide.Both) != DoorState.Closed ? 1 : 0;
55655565
break;
55665566
}
55675567
case CABViewControlTypes.SANDERS:
@@ -5778,10 +5778,12 @@ public virtual float GetDataOf(CabViewControl cvc)
57785778
data = CabLightOn ? 1 : 0;
57795779
break;
57805780
case CABViewControlTypes.ORTS_LEFTDOOR:
5781-
data = GetCabFlipped() ? (DoorRightOpen ? 1 : 0) : DoorLeftOpen ? 1 : 0;
5782-
break;
57835781
case CABViewControlTypes.ORTS_RIGHTDOOR:
5784-
data = GetCabFlipped() ? (DoorLeftOpen ? 1 : 0) : DoorRightOpen ? 1 : 0;
5782+
{
5783+
bool right = (cvc.ControlType == CABViewControlTypes.ORTS_RIGHTDOOR) ^ Flipped ^ GetCabFlipped();
5784+
var state = Train.DoorState(right ? DoorSide.Right : DoorSide.Left);
5785+
data = state >= DoorState.Opening ? 1 : 0;
5786+
}
57855787
break;
57865788
case CABViewControlTypes.ORTS_MIRRORS:
57875789
data = MirrorOpen ? 1 : 0;

0 commit comments

Comments
 (0)