From fe26296daa8bdebcbb0e0baa3b0cc2964776d448 Mon Sep 17 00:00:00 2001 From: Marc Riera Date: Mon, 21 Oct 2024 23:55:00 +0200 Subject: [PATCH] Doors: Add mode where closing sound is required --- src/Devices/Doors.cs | 124 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 107 insertions(+), 17 deletions(-) diff --git a/src/Devices/Doors.cs b/src/Devices/Doors.cs index 5140d80..d5fb9d3 100644 --- a/src/Devices/Doors.cs +++ b/src/Devices/Doors.cs @@ -12,6 +12,25 @@ namespace OpenbveFcmbTrainPlugin /// Whether the right doors are closing. private bool RightDoorsClosing; + /// Whether the door closing sound is required to be played to close the doors. + private bool RequireDoorClosingSound; + + /// The duration of the door closing sound, in seconds. + private double DoorClosingSoundDuration = 5; + + /// The timeout after which the door closing sound needs to be played again, in seconds. + private double DoorClosingSoundTimeout = 15; + + /// The counter for the door closing sound. + private double DoorClosingSoundCounter; + + /// Whether to trigger a door unlock. + private bool TriggerDoorUnlock; + + /// Whether to trigger a door lock. + private bool TriggerDoorLock; + + /// Is called when the device state should be updated. /// The current train. /// The current route. @@ -21,28 +40,74 @@ namespace OpenbveFcmbTrainPlugin { if (init) { - // Set the selection state of the doors during initialization - switch (train.DoorState) + if (!RequireDoorClosingSound) { - case DoorStates.None: - RequestedDoorInterlock = DoorInterlockStates.Locked; - break; - case DoorStates.Both: - RequestedDoorInterlock = DoorInterlockStates.Unlocked; - break; - case DoorStates.Left: - case DoorStates.Right: - RequestedDoorInterlock = (DoorInterlockStates)train.DoorState; - break; + // Set the selection state of the doors during initialization + switch (train.DoorState) + { + case DoorStates.None: + RequestedDoorInterlock = DoorInterlockStates.Locked; + break; + case DoorStates.Both: + RequestedDoorInterlock = DoorInterlockStates.Unlocked; + break; + case DoorStates.Left: + case DoorStates.Right: + RequestedDoorInterlock = (DoorInterlockStates)train.DoorState; + break; + } } + // Set door closing counter to value higher than maximum where doors are allowed to close + DoorClosingSoundCounter = DoorClosingSoundDuration + DoorClosingSoundTimeout; } // Cut power when the doors are open RequestedPowerNotch = (train.DoorState != DoorStates.None) ? 0 : -1; + // Update door closing sound counter + DoorClosingSoundCounter += elapsedTime.Seconds; + + // Unlock doors for closing if the door closing sound has been played + if (RequireDoorClosingSound) + { + if (DoorClosingSoundCounter > DoorClosingSoundDuration && TriggerDoorUnlock) + { + switch (train.DoorState) + { + case DoorStates.Both: + RequestedDoorInterlock = DoorInterlockStates.Unlocked; + break; + case DoorStates.Left: + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Right ? DoorInterlockStates.Unlocked : DoorInterlockStates.Left; + break; + case DoorStates.Right: + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Left ? DoorInterlockStates.Unlocked : DoorInterlockStates.Right; + break; + } + TriggerDoorUnlock = false; + TriggerDoorLock = true; + } + else if (DoorClosingSoundCounter > DoorClosingSoundDuration + DoorClosingSoundTimeout && TriggerDoorLock) + { + switch (train.DoorState) + { + case DoorStates.Both: + RequestedDoorInterlock = DoorInterlockStates.Locked; + break; + case DoorStates.Left: + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Unlocked ? DoorInterlockStates.Right : DoorInterlockStates.Locked; + break; + case DoorStates.Right: + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Unlocked ? DoorInterlockStates.Left : DoorInterlockStates.Locked; + break; + } + TriggerDoorLock = false; + } + } + // Update panel variables for door selection state - train.Panel[50] = (RequestedDoorInterlock == DoorInterlockStates.Left || RequestedDoorInterlock == DoorInterlockStates.Unlocked) ? 1 : 0; - train.Panel[51] = (RequestedDoorInterlock == DoorInterlockStates.Right || RequestedDoorInterlock == DoorInterlockStates.Unlocked) ? 1 : 0; + train.Panel[50] = (RequestedDoorInterlock == DoorInterlockStates.Left || RequestedDoorInterlock == DoorInterlockStates.Unlocked) || (((train.DoorState & DoorStates.Left) != 0) && !LeftDoorsClosing) ? 1 : 0; + train.Panel[51] = (RequestedDoorInterlock == DoorInterlockStates.Right || RequestedDoorInterlock == DoorInterlockStates.Unlocked) || (((train.DoorState & DoorStates.Right) != 0) && !RightDoorsClosing) ? 1 : 0; } /// Is called when the state of a key changes. @@ -55,6 +120,14 @@ namespace OpenbveFcmbTrainPlugin { switch (key) { + case VirtualKeys.F: + if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key]) + { + // Play the door closing sound and reset timeout + DoorClosingSoundCounter = 0; + TriggerDoorUnlock = true; + } + break; case VirtualKeys.G: // Change the selection state of the left doors if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key]) @@ -123,7 +196,7 @@ namespace OpenbveFcmbTrainPlugin if ((train.DoorState & DoorStates.Right) > 0 && (RequestedDoorInterlock == DoorInterlockStates.Right || RequestedDoorInterlock == DoorInterlockStates.Unlocked)) { RightDoorsClosing = true; - RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Right ? DoorInterlockStates.Locked : DoorInterlockStates. Left; + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Right ? DoorInterlockStates.Locked : DoorInterlockStates.Left; } } break; @@ -141,13 +214,30 @@ namespace OpenbveFcmbTrainPlugin { LeftDoorsClosing = false; // Unselect doors automatically when closed, just in case - RequestedDoorInterlock = newState == DoorStates.None ? DoorInterlockStates.Locked : DoorInterlockStates.Right; + if (RequestedDoorInterlock == DoorInterlockStates.Unlocked || RequestedDoorInterlock == DoorInterlockStates.Left) + { + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Left ? DoorInterlockStates.Locked : DoorInterlockStates.Right; + } } if ((oldState == DoorStates.Both || oldState == DoorStates.Right) && (newState == DoorStates.None || newState == DoorStates.Left)) { RightDoorsClosing = false; // Unselect doors automatically when closed, just in case - RequestedDoorInterlock = newState == DoorStates.None ? DoorInterlockStates.Locked : DoorInterlockStates.Left; + if (RequestedDoorInterlock == DoorInterlockStates.Unlocked || RequestedDoorInterlock == DoorInterlockStates.Right) + { + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Right ? DoorInterlockStates.Locked : DoorInterlockStates.Left; + } + } + if (RequireDoorClosingSound) + { + if ((oldState == DoorStates.None || oldState == DoorStates.Left) && (newState == DoorStates.Both || newState == DoorStates.Right)) + { + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Left ? DoorInterlockStates.Left : DoorInterlockStates.Locked; + } + if ((oldState == DoorStates.None || oldState == DoorStates.Right) && (newState == DoorStates.Both || newState == DoorStates.Left)) + { + RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Right ? DoorInterlockStates.Right : DoorInterlockStates.Locked; + } } }