2020-08-22 22:29:20 +02:00
|
|
|
using System;
|
2020-06-02 14:42:23 -04:00
|
|
|
using System.Linq;
|
2021-10-10 12:47:26 +02:00
|
|
|
using Content.Server.Stunnable;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Stunnable.Components;
|
|
|
|
|
using Content.Server.UserInterface;
|
|
|
|
|
using Content.Shared.ActionBlocker;
|
|
|
|
|
using Content.Shared.Hands;
|
|
|
|
|
using Content.Shared.Instruments;
|
|
|
|
|
using Content.Shared.Interaction;
|
2021-09-26 15:18:45 +02:00
|
|
|
using Content.Shared.Popups;
|
2021-06-27 19:02:46 +10:00
|
|
|
using Content.Shared.Standing;
|
2021-10-10 12:47:26 +02:00
|
|
|
using Content.Shared.Stunnable;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Throwing;
|
2019-11-25 00:11:47 +01:00
|
|
|
using Robust.Server.GameObjects;
|
2020-05-22 11:13:34 +02:00
|
|
|
using Robust.Server.Player;
|
|
|
|
|
using Robust.Shared.Enums;
|
2019-11-25 00:11:47 +01:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-11-27 17:12:45 +01:00
|
|
|
using Robust.Shared.Localization;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Network;
|
2020-04-20 10:36:02 +01:00
|
|
|
using Robust.Shared.Players;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2019-11-25 00:11:47 +01:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Instruments
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2020-06-02 14:42:23 -04:00
|
|
|
|
2019-11-25 00:11:47 +01:00
|
|
|
[RegisterComponent]
|
2020-06-02 14:42:23 -04:00
|
|
|
public class InstrumentComponent
|
2021-11-23 18:19:08 +00:00
|
|
|
: SharedInstrumentComponent
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2020-11-27 17:12:45 +01:00
|
|
|
private InstrumentSystem _instrumentSystem = default!;
|
2020-05-20 15:15:17 +02:00
|
|
|
|
2020-05-20 17:13:49 +02:00
|
|
|
[ViewVariables]
|
|
|
|
|
private bool _playing = false;
|
|
|
|
|
|
2020-05-21 15:20:37 +02:00
|
|
|
[ViewVariables]
|
2020-05-20 15:15:17 +02:00
|
|
|
private float _timer = 0f;
|
2020-05-20 17:13:49 +02:00
|
|
|
|
2020-05-21 15:20:37 +02:00
|
|
|
[ViewVariables]
|
2020-05-21 19:18:10 +02:00
|
|
|
private int _batchesDropped = 0;
|
|
|
|
|
|
2020-06-02 14:42:23 -04:00
|
|
|
[ViewVariables]
|
|
|
|
|
private int _laggedBatches = 0;
|
|
|
|
|
|
2020-05-21 19:18:10 +02:00
|
|
|
[ViewVariables]
|
|
|
|
|
private uint _lastSequencerTick = 0;
|
2020-05-21 15:20:37 +02:00
|
|
|
|
2020-05-20 17:13:49 +02:00
|
|
|
[ViewVariables]
|
|
|
|
|
private int _midiEventCount = 0;
|
2020-05-20 15:15:17 +02:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("program")]
|
|
|
|
|
private byte _instrumentProgram = 1;
|
|
|
|
|
[DataField("bank")]
|
2020-10-30 10:34:22 +01:00
|
|
|
private byte _instrumentBank;
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("allowPercussion")]
|
2020-10-30 10:34:22 +01:00
|
|
|
private bool _allowPercussion;
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("allowProgramChange")]
|
2020-10-30 10:34:22 +01:00
|
|
|
private bool _allowProgramChange;
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("respectMidiLimits")]
|
|
|
|
|
private bool _respectMidiLimits = true;
|
2020-10-30 10:34:22 +01:00
|
|
|
|
|
|
|
|
public override byte InstrumentProgram { get => _instrumentProgram;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_instrumentProgram = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte InstrumentBank { get => _instrumentBank;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_instrumentBank = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool AllowPercussion { get => _allowPercussion;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_allowPercussion = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool AllowProgramChange { get => _allowProgramChange;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_allowProgramChange = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 01:43:29 +01:00
|
|
|
public override bool RespectMidiLimits { get => _respectMidiLimits;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_respectMidiLimits = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 18:19:08 +00:00
|
|
|
public IPlayerSession? InstrumentPlayer => Owner.GetComponentOrNull<ActivatableUIComponent>()?.CurrentSingleUser;
|
2019-11-25 00:11:47 +01:00
|
|
|
|
2020-05-21 15:20:37 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the instrument is currently playing or not.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public bool Playing
|
|
|
|
|
{
|
|
|
|
|
get => _playing;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_playing = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 20:47:17 +02:00
|
|
|
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(InstrumentUiKey.Key);
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2020-11-27 17:12:45 +01:00
|
|
|
_instrumentSystem = EntitySystem.Get<InstrumentSystem>();
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 09:09:07 +01:00
|
|
|
public override ComponentState GetComponentState(ICommonSession player)
|
2020-05-21 15:20:37 +02:00
|
|
|
{
|
2020-12-20 01:43:29 +01:00
|
|
|
return new InstrumentState(Playing, InstrumentProgram, InstrumentBank, AllowPercussion, AllowProgramChange, RespectMidiLimits, _lastSequencerTick);
|
2020-05-21 15:20:37 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 18:10:40 +02:00
|
|
|
[Obsolete("Component Messages are deprecated, use Entity Events instead.")]
|
2020-08-22 22:29:20 +02:00
|
|
|
public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession? session = null)
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2020-04-20 10:36:02 +01:00
|
|
|
base.HandleNetworkMessage(message, channel, session);
|
2020-04-26 00:53:09 +02:00
|
|
|
|
2020-11-27 17:12:45 +01:00
|
|
|
var maxMidiLaggedBatches = _instrumentSystem.MaxMidiLaggedBatches;
|
|
|
|
|
var maxMidiEventsPerSecond = _instrumentSystem.MaxMidiEventsPerSecond;
|
|
|
|
|
var maxMidiEventsPerBatch = _instrumentSystem.MaxMidiEventsPerBatch;
|
|
|
|
|
|
2019-11-25 00:11:47 +01:00
|
|
|
switch (message)
|
|
|
|
|
{
|
|
|
|
|
case InstrumentMidiEventMessage midiEventMsg:
|
2021-11-23 18:19:08 +00:00
|
|
|
if (!Playing || session != InstrumentPlayer || InstrumentPlayer == null) return;
|
2020-06-02 14:42:23 -04:00
|
|
|
|
|
|
|
|
var send = true;
|
|
|
|
|
|
|
|
|
|
var minTick = midiEventMsg.MidiEvent.Min(x => x.Tick);
|
|
|
|
|
if (_lastSequencerTick > minTick)
|
|
|
|
|
{
|
|
|
|
|
_laggedBatches++;
|
2020-11-27 17:12:45 +01:00
|
|
|
|
2020-12-20 01:43:29 +01:00
|
|
|
if (_respectMidiLimits)
|
2020-11-27 17:12:45 +01:00
|
|
|
{
|
2020-12-20 01:43:29 +01:00
|
|
|
if (_laggedBatches == (int) (maxMidiLaggedBatches * (1 / 3d) + 1))
|
|
|
|
|
{
|
2021-03-15 21:55:49 +01:00
|
|
|
InstrumentPlayer.AttachedEntity?.PopupMessage(
|
2021-06-21 02:13:54 +02:00
|
|
|
Loc.GetString("instrument-component-finger-cramps-light-message"));
|
2020-12-20 01:43:29 +01:00
|
|
|
} else if (_laggedBatches == (int) (maxMidiLaggedBatches * (2 / 3d) + 1))
|
|
|
|
|
{
|
2021-03-15 21:55:49 +01:00
|
|
|
InstrumentPlayer.AttachedEntity?.PopupMessage(
|
2021-06-21 02:13:54 +02:00
|
|
|
Loc.GetString("instrument-component-finger-cramps-serious-message"));
|
2020-12-20 01:43:29 +01:00
|
|
|
}
|
2020-06-02 14:42:23 -04:00
|
|
|
}
|
|
|
|
|
|
2020-11-27 17:12:45 +01:00
|
|
|
if (_laggedBatches > maxMidiLaggedBatches)
|
2020-06-02 14:42:23 -04:00
|
|
|
{
|
|
|
|
|
send = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 17:12:45 +01:00
|
|
|
if (++_midiEventCount > maxMidiEventsPerSecond
|
|
|
|
|
|| midiEventMsg.MidiEvent.Length > maxMidiEventsPerBatch)
|
2020-06-02 14:42:23 -04:00
|
|
|
{
|
|
|
|
|
_batchesDropped++;
|
|
|
|
|
|
|
|
|
|
send = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 01:43:29 +01:00
|
|
|
if (send || !_respectMidiLimits)
|
2020-06-02 14:42:23 -04:00
|
|
|
{
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning disable 618
|
2020-05-20 17:48:36 +02:00
|
|
|
SendNetworkMessage(midiEventMsg);
|
2021-10-27 18:10:40 +02:00
|
|
|
#pragma warning restore 618
|
2020-06-02 14:42:23 -04:00
|
|
|
}
|
2020-05-21 15:20:37 +02:00
|
|
|
|
2020-06-02 14:42:23 -04:00
|
|
|
var maxTick = midiEventMsg.MidiEvent.Max(x => x.Tick);
|
2021-02-04 13:37:26 +01:00
|
|
|
_lastSequencerTick = Math.Max(maxTick, minTick);
|
2019-11-25 00:11:47 +01:00
|
|
|
break;
|
2020-05-20 15:15:17 +02:00
|
|
|
case InstrumentStartMidiMessage startMidi:
|
2021-11-23 18:19:08 +00:00
|
|
|
if (session != InstrumentPlayer)
|
2020-06-25 15:01:46 +02:00
|
|
|
break;
|
2020-05-21 15:20:37 +02:00
|
|
|
Playing = true;
|
2020-05-20 15:15:17 +02:00
|
|
|
break;
|
|
|
|
|
case InstrumentStopMidiMessage stopMidi:
|
2021-11-23 18:19:08 +00:00
|
|
|
if (session != InstrumentPlayer)
|
2020-06-25 15:01:46 +02:00
|
|
|
break;
|
2020-05-21 15:20:37 +02:00
|
|
|
Playing = false;
|
2020-06-02 14:42:23 -04:00
|
|
|
Clean();
|
2020-05-20 15:15:17 +02:00
|
|
|
break;
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 18:19:08 +00:00
|
|
|
public void Clean()
|
2019-11-25 00:11:47 +01:00
|
|
|
{
|
2021-11-23 18:19:08 +00:00
|
|
|
if (Playing)
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable 618
|
|
|
|
|
SendNetworkMessage(new InstrumentStopMidiMessage());
|
|
|
|
|
#pragma warning restore 618
|
|
|
|
|
}
|
2020-05-21 15:20:37 +02:00
|
|
|
Playing = false;
|
2020-06-02 14:42:23 -04:00
|
|
|
_lastSequencerTick = 0;
|
|
|
|
|
_batchesDropped = 0;
|
|
|
|
|
_laggedBatches = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 15:15:17 +02:00
|
|
|
public override void Update(float delta)
|
|
|
|
|
{
|
|
|
|
|
base.Update(delta);
|
2020-05-20 17:13:49 +02:00
|
|
|
|
2020-11-27 17:12:45 +01:00
|
|
|
var maxMidiLaggedBatches = _instrumentSystem.MaxMidiLaggedBatches;
|
|
|
|
|
var maxMidiBatchDropped = _instrumentSystem.MaxMidiBatchesDropped;
|
|
|
|
|
|
|
|
|
|
if ((_batchesDropped >= maxMidiBatchDropped
|
|
|
|
|
|| _laggedBatches >= maxMidiLaggedBatches)
|
2020-12-20 01:43:29 +01:00
|
|
|
&& InstrumentPlayer != null && _respectMidiLimits)
|
2020-05-21 19:18:10 +02:00
|
|
|
{
|
2020-05-22 11:13:34 +02:00
|
|
|
var mob = InstrumentPlayer.AttachedEntity;
|
|
|
|
|
|
2021-11-23 18:19:08 +00:00
|
|
|
// Just in case
|
|
|
|
|
Clean();
|
2020-08-22 22:29:20 +02:00
|
|
|
UserInterface?.CloseAll();
|
2020-05-22 11:13:34 +02:00
|
|
|
|
2021-02-20 15:55:35 -07:00
|
|
|
if (mob != null)
|
2020-06-02 14:42:23 -04:00
|
|
|
{
|
2021-10-15 14:45:04 -07:00
|
|
|
EntitySystem.Get<StunSystem>().TryParalyze(mob.Uid, TimeSpan.FromSeconds(1));
|
2021-03-15 21:55:49 +01:00
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessage(mob, "instrument-component-finger-cramps-max-message");
|
2020-06-02 14:42:23 -04:00
|
|
|
}
|
2020-05-21 19:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-20 17:13:49 +02:00
|
|
|
_timer += delta;
|
|
|
|
|
if (_timer < 1) return;
|
2020-06-02 14:42:23 -04:00
|
|
|
|
2020-05-20 17:13:49 +02:00
|
|
|
_timer = 0f;
|
|
|
|
|
_midiEventCount = 0;
|
2021-02-04 13:37:26 +01:00
|
|
|
_laggedBatches = 0;
|
|
|
|
|
_batchesDropped = 0;
|
2020-05-20 15:15:17 +02:00
|
|
|
}
|
2020-06-02 14:42:23 -04:00
|
|
|
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|
2020-06-02 14:42:23 -04:00
|
|
|
|
2019-11-25 00:11:47 +01:00
|
|
|
}
|