--- /dev/null
+namespace Content.Server.Chat;
+
+using Content.Server.Chat.Systems;
+using Content.Shared.Chat.Prototypes;
+using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
+
+/// <summary>
+/// Causes an entity to automatically emote at a set interval.
+/// </summary>
+[RegisterComponent, Access(typeof(AutoEmoteSystem))]
+public sealed class AutoEmoteComponent : Component
+{
+ /// <summary>
+ /// A set of emotes that the entity will preform.
+ /// <see cref="AutoEmotePrototype"/>
+ /// </summary>
+ [DataField("emotes", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<AutoEmotePrototype>)), ViewVariables(VVAccess.ReadOnly)]
+ public HashSet<string> Emotes = new HashSet<string>();
+
+ /// <summary>
+ /// A dictionary storing the time of the next emote attempt for each emote.
+ /// Uses AutoEmotePrototype IDs as keys.
+ /// <summary>
+ [ViewVariables(VVAccess.ReadOnly)] //TODO: make this a datafield and (de)serialize values as time offsets when https://github.com/space-wizards/RobustToolbox/issues/3768 is fixed
+ public Dictionary<string, TimeSpan> EmoteTimers = new Dictionary<string, TimeSpan>();
+
+ /// <summary>
+ /// Time of the next emote. Redundant, but avoids having to iterate EmoteTimers each update.
+ /// </summary>
+ [ViewVariables(VVAccess.ReadOnly)]
+ public TimeSpan NextEmoteTime = TimeSpan.MaxValue;
+}
--- /dev/null
+namespace Content.Server.Chat.Systems;
+
+using System.Linq;
+using Content.Shared.Chat.Prototypes;
+using Robust.Shared.Prototypes;
+using Robust.Shared.Random;
+using Robust.Shared.Timing;
+using Robust.Shared.Utility;
+
+public sealed class AutoEmoteSystem : EntitySystem
+{
+ [Dependency] private readonly IGameTiming _gameTiming = default!;
+ [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
+ [Dependency] private readonly IRobustRandom _random = default!;
+ [Dependency] private readonly ChatSystem _chatSystem = default!;
+
+ public override void Initialize()
+ {
+ base.Initialize();
+
+ SubscribeLocalEvent<AutoEmoteComponent, MapInitEvent>(OnMapInit);
+ SubscribeLocalEvent<AutoEmoteComponent, EntityUnpausedEvent>(OnUnpaused);
+ }
+
+ public override void Update(float frameTime)
+ {
+ base.Update(frameTime);
+
+ var curTime = _gameTiming.CurTime;
+ foreach (var autoEmote in EntityQuery<AutoEmoteComponent>())
+ {
+ var uid = autoEmote.Owner;
+
+ if (autoEmote.NextEmoteTime > curTime)
+ continue;
+
+ foreach ((var key, var time) in autoEmote.EmoteTimers)
+ {
+ if (time > curTime)
+ continue;
+
+ var autoEmotePrototype = _prototypeManager.Index<AutoEmotePrototype>(key);
+ ResetTimer(uid, key, autoEmote, autoEmotePrototype);
+
+ if (!_random.Prob(autoEmotePrototype.Chance))
+ continue;
+
+ if (autoEmotePrototype.WithChat)
+ {
+ _chatSystem.TryEmoteWithChat(uid, autoEmotePrototype.EmoteId, autoEmotePrototype.HiddenFromChatWindow);
+ }
+ else
+ {
+ _chatSystem.TryEmoteWithoutChat(uid, autoEmotePrototype.EmoteId);
+ }
+ }
+ }
+ }
+
+ private void OnMapInit(EntityUid uid, AutoEmoteComponent autoEmote, MapInitEvent args)
+ {
+ // Start timers
+ foreach (var autoEmotePrototypeId in autoEmote.Emotes)
+ {
+ ResetTimer(uid, autoEmotePrototypeId, autoEmote);
+ }
+ }
+
+ private void OnUnpaused(EntityUid uid, AutoEmoteComponent autoEmote, ref EntityUnpausedEvent args)
+ {
+ foreach (var key in autoEmote.EmoteTimers.Keys)
+ {
+ autoEmote.EmoteTimers[key] += args.PausedTime;
+ }
+ autoEmote.NextEmoteTime += args.PausedTime;
+ }
+
+ /// <summary>
+ /// Try to add an emote to the entity, which will be performed at an interval.
+ /// </summary>
+ public bool AddEmote(EntityUid uid, string autoEmotePrototypeId, AutoEmoteComponent? autoEmote = null)
+ {
+ if (!Resolve(uid, ref autoEmote, logMissing: false))
+ return false;
+
+ if (autoEmote.Emotes.Contains(autoEmotePrototypeId))
+ return false;
+
+ autoEmote.Emotes.Add(autoEmotePrototypeId);
+ ResetTimer(uid, autoEmotePrototypeId, autoEmote);
+
+ return true;
+ }
+
+ /// <summary>
+ /// Stop preforming an emote.
+ /// </summary>
+ public bool RemoveEmote(EntityUid uid, string autoEmotePrototypeId, AutoEmoteComponent? autoEmote = null)
+ {
+ if (!Resolve(uid, ref autoEmote, logMissing: false))
+ return false;
+
+ DebugTools.Assert(_prototypeManager.HasIndex<AutoEmotePrototype>(autoEmotePrototypeId), "Prototype not found. Did you make a typo?");
+
+ if (!autoEmote.EmoteTimers.Remove(autoEmotePrototypeId))
+ return false;
+
+ autoEmote.NextEmoteTime = autoEmote.EmoteTimers.Values.Min();
+ return true;
+ }
+
+ /// <summary>
+ /// Reset the timer for a specific emote, or return false if it doesn't exist.
+ /// </summary>
+ public bool ResetTimer(EntityUid uid, string autoEmotePrototypeId, AutoEmoteComponent? autoEmote = null, AutoEmotePrototype? autoEmotePrototype = null)
+ {
+ if (!Resolve(uid, ref autoEmote))
+ return false;
+
+ if (!autoEmote.Emotes.Contains(autoEmotePrototypeId))
+ return false;
+
+ autoEmotePrototype ??= _prototypeManager.Index<AutoEmotePrototype>(autoEmotePrototypeId);
+
+ var curTime = _gameTiming.CurTime;
+ var time = curTime + autoEmotePrototype.Interval;
+ autoEmote.EmoteTimers[autoEmotePrototypeId] = time;
+
+ if (autoEmote.NextEmoteTime > time || autoEmote.NextEmoteTime <= curTime)
+ autoEmote.NextEmoteTime = time;
+
+ return true;
+ }
+}
namespace Content.Server.Zombies;
+/// <summary>
+/// Indicates a zombie that is "alive", i.e not crit/dead.
+/// Causes it to emote when damaged.
+/// TODO: move this to generic EmoteWhenDamaged comp/system.
+/// </summary>
[RegisterComponent]
public sealed class ActiveZombieComponent : Component
{
/// <summary>
- /// The chance that on a random attempt
- /// that a zombie will do a groan
+ /// What emote to preform.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
- public float GroanChance = 0.2f;
+ public string GroanEmoteId = "Scream";
/// <summary>
- /// Minimum time between groans
+ /// Minimum time between groans.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
- public float GroanCooldown = 2;
+ public TimeSpan DamageGroanCooldown = TimeSpan.FromSeconds(2);
/// <summary>
- /// The length of time between each zombie's random groan
- /// attempt.
+ /// Chance to groan.
/// </summary>
- [ViewVariables(VVAccess.ReadWrite)]
- public float RandomGroanAttempt = 5;
-
- [ViewVariables(VVAccess.ReadWrite)]
- public string GroanEmoteId = "Scream";
-
- [ViewVariables(VVAccess.ReadWrite)]
- public float LastDamageGroanCooldown = 0f;
+ public float DamageGroanChance = 0.5f;
+ /// <summary>
+ /// The last time the zombie groaned from taking damage.
+ /// </summary>
[ViewVariables(VVAccess.ReadWrite)]
- public float Accumulator = 0f;
+ public TimeSpan LastDamageGroan = TimeSpan.Zero;
}
using System.Linq;
using Content.Server.Body.Systems;
+using Content.Server.Chat;
using Content.Server.Chat.Systems;
using Content.Server.Cloning;
using Content.Server.Disease;
using Content.Server.Drone.Components;
using Content.Server.Humanoid;
using Content.Server.Inventory;
-using Content.Server.Speech;
using Content.Shared.Bed.Sleep;
using Content.Shared.Chemistry.Components;
-using Content.Server.Chat.Systems;
using Content.Server.Emoting.Systems;
using Content.Server.Speech.EntitySystems;
-using Content.Shared.Movement.Systems;
-using Content.Shared.Bed.Sleep;
using Content.Shared.Damage;
using Content.Shared.Disease.Events;
using Content.Shared.Inventory;
using Content.Shared.Zombies;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
+using Robust.Shared.Timing;
namespace Content.Server.Zombies
{
[Dependency] private readonly ZombifyOnDeathSystem _zombify = default!;
[Dependency] private readonly ServerInventorySystem _inv = default!;
[Dependency] private readonly ChatSystem _chat = default!;
+ [Dependency] private readonly AutoEmoteSystem _autoEmote = default!;
+ [Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly HumanoidAppearanceSystem _humanoidSystem = default!;
SubscribeLocalEvent<ActiveZombieComponent, DamageChangedEvent>(OnDamage);
SubscribeLocalEvent<ActiveZombieComponent, AttemptSneezeCoughEvent>(OnSneeze);
SubscribeLocalEvent<ActiveZombieComponent, TryingToSleepEvent>(OnSleepAttempt);
-
}
private void OnSleepAttempt(EntityUid uid, ActiveZombieComponent component, ref TryingToSleepEvent args)
private void OnMobState(EntityUid uid, ZombieComponent component, MobStateChangedEvent args)
{
+ //BUG: this won't work when an entity becomes a zombie some other way, such as admin smite
if (args.NewMobState == MobState.Alive)
+ {
+ // Groaning when damaged
EnsureComp<ActiveZombieComponent>(uid);
+
+ // Random groaning
+ EnsureComp<AutoEmoteComponent>(uid);
+ _autoEmote.AddEmote(uid, "ZombieGroan");
+ }
else
+ {
+ // Stop groaning when damaged
RemComp<ActiveZombieComponent>(uid);
+
+ // Stop random groaning
+ _autoEmote.RemoveEmote(uid, "ZombieGroan");
+ }
}
private void OnDamage(EntityUid uid, ActiveZombieComponent component, DamageChangedEvent args)
{
if (args.DamageIncreased)
- DoGroan(uid, component);
+ AttemptDamageGroan(uid, component);
}
private void OnSneeze(EntityUid uid, ActiveZombieComponent component, ref AttemptSneezeCoughEvent args)
}
}
- private void DoGroan(EntityUid uid, ActiveZombieComponent component)
+ private void AttemptDamageGroan(EntityUid uid, ActiveZombieComponent component)
{
- if (component.LastDamageGroanCooldown > 0)
+ if (component.LastDamageGroan + component.DamageGroanCooldown > _gameTiming.CurTime)
return;
- if (_robustRandom.Prob(0.5f)) //this message is never seen by players so it just says this for admins
- // What? Is this REALLY the best way we have of letting admins know there are zombies in a round?
- // [automated maintainer groan]
- _chat.TrySendInGameICMessage(uid, "[automated zombie groan]", InGameICChatType.Speak, false);
- else
- _chat.TryEmoteWithoutChat(uid, component.GroanEmoteId);
-
- component.LastDamageGroanCooldown = component.GroanCooldown;
- }
-
- public override void Update(float frameTime)
- {
- base.Update(frameTime);
-
- foreach (var zombiecomp in EntityQuery<ActiveZombieComponent>())
- {
- zombiecomp.Accumulator += frameTime;
- zombiecomp.LastDamageGroanCooldown -= frameTime;
-
- if (zombiecomp.Accumulator < zombiecomp.RandomGroanAttempt)
- continue;
- zombiecomp.Accumulator -= zombiecomp.RandomGroanAttempt;
-
- if (!_robustRandom.Prob(zombiecomp.GroanChance))
- continue;
+ if (_robustRandom.Prob(component.DamageGroanChance))
+ return;
- //either do a random accent line or scream
- DoGroan(zombiecomp.Owner, zombiecomp);
- }
+ _chat.TryEmoteWithoutChat(uid, component.GroanEmoteId);
+ component.LastDamageGroan = _gameTiming.CurTime;
}
/// <summary>
using Content.Server.Body.Components;
using Content.Server.Atmos.Components;
using Content.Server.Nutrition.Components;
-using Robust.Shared.Player;
using Content.Server.Popups;
using Content.Server.Speech.Components;
using Content.Server.Body.Systems;
using Content.Shared.Mobs;
using Content.Shared.Movement.Systems;
using Content.Shared.Weapons.Melee;
-using Robust.Shared.Audio;
namespace Content.Server.Zombies
{
--- /dev/null
+namespace Content.Shared.Chat.Prototypes;
+
+using Robust.Shared.Prototypes;
+using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
+
+[Prototype("autoEmote")]
+public sealed class AutoEmotePrototype : IPrototype
+{
+ /// <inheritdoc/>
+ [IdDataField]
+ public string ID { get; } = default!;
+
+ /// <summary>
+ /// The ID of the emote prototype.
+ /// </summary>
+ [DataField("emote", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<EmotePrototype>))]
+ public string EmoteId = String.Empty;
+
+ /// <summary>
+ /// How often an attempt at the emote will be made.
+ /// </summary>
+ [DataField("interval", required: true)]
+ public TimeSpan Interval;
+
+ /// <summary>
+ /// Probability of performing the emote each interval.
+ /// <summary>
+ [DataField("chance")]
+ public float Chance = 1;
+
+ /// <summary>
+ /// Also send the emote in chat.
+ /// <summary>
+ [DataField("withChat")]
+ public bool WithChat = true;
+
+ /// <summary>
+ /// Hide the chat message from the chat window, only showing the popup.
+ /// This does nothing if WithChat is false.
+ /// <summary>
+ [DataField("hiddenFromChatWindow")]
+ public bool HiddenFromChatWindow = false;
+}
--- /dev/null
+# Zombie
+- type: autoEmote
+ id: ZombieGroan
+ emote: Scream
+ interval: 5.0
+ chance: 0.1
+ withChat: false