Compare commits

...

2 Commits

Author SHA1 Message Date
Marc Riera bcc0f1d4b9 Doors: Make AI play door closing sound in time 2024-11-03 18:01:45 +01:00
Marc Riera bcebb8599c Fix in managers 2024-11-03 18:01:18 +01:00
4 changed files with 75 additions and 10 deletions

View File

@ -33,6 +33,21 @@ namespace OpenbveFcmbTrainPlugin
/// <summary>Whether the AI should trigger the door closing sound.</summary>
private bool AiTriggerDoorClosingSound;
/// <summary>The timeout after which the AI needs to close doors if they become stuck, in seconds.</summary>
private double AiDoorStuckTimeout = 10;
/// <summary>The counter for the AI to close doors if they become stuck.</summary>
private double AiDoorStuckCounter;
/// <summary>The index of the door closing sound.</summary>
private int DoorClosingSoundIndex = 10;
/// <summary>The current time.</summary>
private Time CurrentTime = new Time(0);
/// <summary>The time when the doors last opened.</summary>
private Time DoorOpenTime = new Time(0);
/// <summary>Is called when the device state should be updated.</summary>
/// <param name="train">The current train.</param>
@ -41,6 +56,9 @@ namespace OpenbveFcmbTrainPlugin
/// <param name="elapsedTime">The time elapsed since the previous call.</param>
internal override void Update(Train train, Route route, bool init, Time elapsedTime)
{
// Update current time
CurrentTime = route.CurrentTime;
if (init)
{
if (!RequireDoorClosingSound)
@ -109,8 +127,32 @@ namespace OpenbveFcmbTrainPlugin
}
// Check if AI needs to trigger the door closing sound
// TODO: check departure time
AiTriggerDoorClosingSound |= (train.DoorState != DoorStates.None && DoorClosingSoundCounter > DoorClosingSoundDuration + DoorClosingSoundTimeout);
if (train.DoorState != DoorStates.None && DoorClosingSoundCounter > DoorClosingSoundDuration + DoorClosingSoundTimeout)
{
if (route.CurrentStation.Type == StationType.Normal || route.CurrentStation.Type == StationType.RequestStop)
{
// The current station is an intermediate stop
if (route.CurrentStation.DepartureTime < 0)
{
// The current station has dwell time, not a specific departure time
// Calculate departure time from the time when the doors opened
AiTriggerDoorClosingSound |= CurrentTime.Seconds >= DoorOpenTime.Seconds + (route.CurrentStation.StopTime - DoorClosingSoundDuration);
}
else
{
// Use departure time - door closing sound duration
AiTriggerDoorClosingSound |= CurrentTime.Seconds >= route.CurrentStation.DepartureTime - DoorClosingSoundDuration;
}
}
else
{
AiTriggerDoorClosingSound = false;
}
}
else
{
AiTriggerDoorClosingSound = false;
}
// Update panel variables for door selection state
train.Panel[50] = (RequestedDoorInterlock == DoorInterlockStates.Left || RequestedDoorInterlock == DoorInterlockStates.Unlocked) || (((train.DoorState & DoorStates.Left) != 0) && !LeftDoorsClosing) ? 1 : 0;
@ -133,6 +175,10 @@ namespace OpenbveFcmbTrainPlugin
// Play the door closing sound and reset timeout
DoorClosingSoundCounter = 0;
TriggerDoorUnlock = true;
if (!SoundManager.Playing(DoorClosingSoundIndex))
{
SoundManager.Play(DoorClosingSoundIndex, 1, 1, false);
}
}
break;
case VirtualKeys.G:
@ -189,22 +235,30 @@ namespace OpenbveFcmbTrainPlugin
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
// Unselect doors automatically when closing
if ((train.DoorState & DoorStates.Left) > 0 && (RequestedDoorInterlock == DoorInterlockStates.Left || RequestedDoorInterlock == DoorInterlockStates.Unlocked))
if ((train.DoorState & DoorStates.Left) > 0 && !LeftDoorsClosing && (RequestedDoorInterlock == DoorInterlockStates.Left || RequestedDoorInterlock == DoorInterlockStates.Unlocked))
{
LeftDoorsClosing = true;
RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Left ? DoorInterlockStates.Locked : DoorInterlockStates.Right;
}
else
{
LeftDoorsClosing = false;
}
}
break;
case VirtualKeys.RightDoors:
if (!OpenbveFcmbTrainPlugin.KeysPressed[(int)key])
{
// Unselect doors automatically when closing
if ((train.DoorState & DoorStates.Right) > 0 && (RequestedDoorInterlock == DoorInterlockStates.Right || RequestedDoorInterlock == DoorInterlockStates.Unlocked))
if ((train.DoorState & DoorStates.Right) > 0 && !RightDoorsClosing && (RequestedDoorInterlock == DoorInterlockStates.Right || RequestedDoorInterlock == DoorInterlockStates.Unlocked))
{
RightDoorsClosing = true;
RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Right ? DoorInterlockStates.Locked : DoorInterlockStates.Left;
}
else
{
RightDoorsClosing = false;
}
}
break;
}
@ -246,6 +300,12 @@ namespace OpenbveFcmbTrainPlugin
RequestedDoorInterlock = RequestedDoorInterlock == DoorInterlockStates.Right ? DoorInterlockStates.Right : DoorInterlockStates.Locked;
}
}
// Set arrival time when doors open
if (oldState == DoorStates.None && newState != DoorStates.None)
{
DoorOpenTime = CurrentTime;
}
}
/// <summary>Is called when the device should perform the AI.</summary>

View File

@ -22,7 +22,7 @@ namespace OpenbveFcmbTrainPlugin
/// <summary>Shows a message to the player.</summary>
/// <param name="message">The message to be shown.</param>
/// <param name="color">The color of the message.</param>
/// <param name="time">The time to show the message.</param>
/// <param name="time">The time to show the message, in seconds.</param>
internal static void ShowMessage(string message, OpenBveApi.Colors.MessageColor color, double time)
{
MessageDelegate(message, color, time);
@ -31,7 +31,7 @@ namespace OpenbveFcmbTrainPlugin
/// <summary>Shows a message to the player.</summary>
/// <param name="message">The message to be shown.</param>
/// <param name="color">The color of the message.</param>
/// <param name="time">The time to show the message.</param>
/// <param name="time">The time to show the message, in seconds.</param>
/// <param name="score">The score for the player.</param>
internal static void ShowMessage( string message, OpenBveApi.Colors.MessageColor color, double time, int score)
{

View File

@ -39,7 +39,7 @@ namespace OpenbveFcmbTrainPlugin
// Validate index before doing anything
if (soundIndex < Sounds.Length && soundIndex >= 0)
{
if (Sounds[soundIndex].Playing)
if (Playing(soundIndex))
{
// The sound is still playing, update volume and pitch
Sounds[soundIndex].Volume = volume;
@ -64,7 +64,7 @@ namespace OpenbveFcmbTrainPlugin
// Validate index before doing anything
if (soundIndex < Sounds.Length && soundIndex >= 0)
{
if (Sounds[soundIndex].Playing)
if (Playing(soundIndex))
{
// The sound is still playing, update volume and pitch
Sounds[soundIndex].Volume = volume;
@ -82,8 +82,7 @@ namespace OpenbveFcmbTrainPlugin
/// <param name="soundIndex">The index of the sound to be stopped.</param>
internal static void Stop(int soundIndex)
{
// Validate index before doing anything
if (soundIndex < Sounds.Length && soundIndex >= 0 && Sounds[soundIndex] != null)
if (Playing(soundIndex))
{
// Stop sound
Sounds[soundIndex].Stop();

View File

@ -28,6 +28,9 @@ namespace OpenbveFcmbTrainPlugin
/// <summary>The next station in the route.</summary>
internal Station NextStation;
/// <summary>The current time in the route.</summary>
internal Time CurrentTime { get; private set; } = new Time(0);
/// <summary>The current state of the train doors.</summary>
private DoorStates DoorState;
@ -38,6 +41,9 @@ namespace OpenbveFcmbTrainPlugin
/// <param name="data">The data passed to the plugin.</param>
internal void Elapse(ElapseData data)
{
// Update current time
CurrentTime = data.TotalTime;
if (OpenbveFcmbTrainPlugin.Initializing)
{
// Get the list of stations and set the current station