Add ability to shake fizzy drinks so they spray in peoples' faces (#25574)

* Implemented Shakeable

* Prevent shaking open Openables

* Prevent shaking empty drinks. Moved part of DrinkSystem to Shared.

* DrinkSystem can have a little more prediction, as a treat

* Cleanup

* Overhauled PressurizedDrink

* Make soda cans/bottles and champagne shakeable. The drink shaker too, for fun.

* We do a little refactoring.
PressurizedDrink is now PressurizedSolution, and fizziness now only works on solutions containing a reagent marked as fizzy.

* Documentation, cleanup, and tweaks.

* Changed fizziness calculation to use a cubic-out easing curve.

* Removed broken YAML that has avoid the linter's wrath for far too long

* Changed reagent fizzy bool to fizziness float.
Solution fizzability now scales with reagent proportion.

* Rename file to match changed class name

* DoAfter improvements. Cancel if the user moves away; block if no hands.

* Match these filenames too

* And this one

* guh

* Updated to use Shared puddle methods

* Various fixes and improvements.

* Made AttemptShakeEvent a struct

* AttemptAddFizzinessEvent too
This commit is contained in:
Tayrtahn
2024-04-17 21:49:58 -04:00
committed by GitHub
parent 8d64d2bc1e
commit cfa94be4c2
25 changed files with 807 additions and 146 deletions

View File

@@ -0,0 +1,7 @@
using Content.Shared.Nutrition.EntitySystems;
namespace Content.Client.Nutrition.EntitySystems;
public sealed class DrinkSystem : SharedDrinkSystem
{
}

View File

@@ -1,27 +0,0 @@
using Content.Server.Nutrition.EntitySystems;
using Robust.Shared.Audio;
namespace Content.Server.Nutrition.Components;
/// <summary>
/// Lets a drink burst open when thrown while closed.
/// Requires <see cref="DrinkComponent"/> and <see cref="OpenableComponent"/> to work.
/// </summary>
[RegisterComponent, Access(typeof(DrinkSystem))]
public sealed partial class PressurizedDrinkComponent : Component
{
/// <summary>
/// Chance for the drink to burst when thrown while closed.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public float BurstChance = 0.25f;
/// <summary>
/// Sound played when the drink bursts.
/// </summary>
[DataField]
public SoundSpecifier BurstSound = new SoundPathSpecifier("/Audio/Effects/flash_bang.ogg")
{
Params = AudioParams.Default.WithVolume(-4)
};
}

View File

@@ -5,7 +5,6 @@ using Content.Server.Chemistry.ReagentEffects;
using Content.Server.Fluids.EntitySystems;
using Content.Server.Forensics;
using Content.Server.Inventory;
using Content.Server.Nutrition.Components;
using Content.Server.Popups;
using Content.Shared.Administration.Logs;
using Content.Shared.Body.Components;
@@ -16,7 +15,6 @@ using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.Examine;
using Content.Shared.FixedPoint;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
@@ -25,24 +23,21 @@ using Content.Shared.Mobs.Systems;
using Content.Shared.Nutrition;
using Content.Shared.Nutrition.Components;
using Content.Shared.Nutrition.EntitySystems;
using Content.Shared.Throwing;
using Content.Shared.Verbs;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
namespace Content.Server.Nutrition.EntitySystems;
public sealed class DrinkSystem : EntitySystem
public sealed class DrinkSystem : SharedDrinkSystem
{
[Dependency] private readonly BodySystem _body = default!;
[Dependency] private readonly FlavorProfileSystem _flavorProfile = default!;
[Dependency] private readonly FoodSystem _food = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly OpenableSystem _openable = default!;
@@ -66,33 +61,10 @@ public sealed class DrinkSystem : EntitySystem
SubscribeLocalEvent<DrinkComponent, ComponentInit>(OnDrinkInit);
// run before inventory so for bucket it always tries to drink before equipping (when empty)
// run after openable so its always open -> drink
SubscribeLocalEvent<DrinkComponent, UseInHandEvent>(OnUse, before: new[] { typeof(ServerInventorySystem) }, after: new[] { typeof(OpenableSystem) });
SubscribeLocalEvent<DrinkComponent, UseInHandEvent>(OnUse, before: [typeof(ServerInventorySystem)], after: [typeof(OpenableSystem)]);
SubscribeLocalEvent<DrinkComponent, AfterInteractEvent>(AfterInteract);
SubscribeLocalEvent<DrinkComponent, GetVerbsEvent<AlternativeVerb>>(AddDrinkVerb);
// put drink amount after opened
SubscribeLocalEvent<DrinkComponent, ExaminedEvent>(OnExamined, after: new[] { typeof(OpenableSystem) });
SubscribeLocalEvent<DrinkComponent, ConsumeDoAfterEvent>(OnDoAfter);
SubscribeLocalEvent<PressurizedDrinkComponent, LandEvent>(OnPressurizedDrinkLand);
}
private FixedPoint2 DrinkVolume(EntityUid uid, DrinkComponent? component = null)
{
if (!Resolve(uid, ref component))
return FixedPoint2.Zero;
if (!_solutionContainer.TryGetSolution(uid, component.Solution, out _, out var sol))
return FixedPoint2.Zero;
return sol.Volume;
}
public bool IsEmpty(EntityUid uid, DrinkComponent? component = null)
{
if (!Resolve(uid, ref component))
return true;
return DrinkVolume(uid, component) <= 0;
}
/// <summary>
@@ -129,38 +101,6 @@ public sealed class DrinkSystem : EntitySystem
return total;
}
private void OnExamined(Entity<DrinkComponent> entity, ref ExaminedEvent args)
{
TryComp<OpenableComponent>(entity, out var openable);
if (_openable.IsClosed(entity.Owner, null, openable) || !args.IsInDetailsRange || !entity.Comp.Examinable)
return;
var empty = IsEmpty(entity, entity.Comp);
if (empty)
{
args.PushMarkup(Loc.GetString("drink-component-on-examine-is-empty"));
return;
}
if (HasComp<ExaminableSolutionComponent>(entity))
{
//provide exact measurement for beakers
args.PushText(Loc.GetString("drink-component-on-examine-exact-volume", ("amount", DrinkVolume(entity, entity.Comp))));
}
else
{
//general approximation
var remainingString = (int) _solutionContainer.PercentFull(entity) switch
{
100 => "drink-component-on-examine-is-full",
> 66 => "drink-component-on-examine-is-mostly-full",
> 33 => HalfEmptyOrHalfFull(args),
_ => "drink-component-on-examine-is-mostly-empty",
};
args.PushMarkup(Loc.GetString(remainingString));
}
}
private void AfterInteract(Entity<DrinkComponent> entity, ref AfterInteractEvent args)
{
if (args.Handled || args.Target == null || !args.CanReach)
@@ -177,25 +117,6 @@ public sealed class DrinkSystem : EntitySystem
args.Handled = TryDrink(args.User, args.User, entity.Comp, entity);
}
private void OnPressurizedDrinkLand(Entity<PressurizedDrinkComponent> entity, ref LandEvent args)
{
if (!TryComp<DrinkComponent>(entity, out var drink) || !TryComp<OpenableComponent>(entity, out var openable))
return;
if (!openable.Opened &&
_random.Prob(entity.Comp.BurstChance) &&
_solutionContainer.TryGetSolution(entity.Owner, drink.Solution, out var soln, out var interactions))
{
// using SetOpen instead of TryOpen to not play 2 sounds
_openable.SetOpen(entity, true, openable);
var solution = _solutionContainer.SplitSolution(soln.Value, interactions.Volume);
_puddle.TrySpillAt(entity, solution, out _);
_audio.PlayPvs(entity.Comp.BurstSound, entity);
}
}
private void OnDrinkInit(Entity<DrinkComponent> entity, ref ComponentInit args)
{
if (TryComp<DrainableSolutionComponent>(entity, out var existingDrainable))
@@ -433,16 +354,4 @@ public sealed class DrinkSystem : EntitySystem
ev.Verbs.Add(verb);
}
// some see half empty, and others see half full
private string HalfEmptyOrHalfFull(ExaminedEvent args)
{
string remainingString = "drink-component-on-examine-is-half-full";
if (TryComp<MetaDataComponent>(args.Examiner, out var examiner) && examiner.EntityName.Length > 0
&& string.Compare(examiner.EntityName.Substring(0, 1), "m", StringComparison.InvariantCultureIgnoreCase) > 0)
remainingString = "drink-component-on-examine-is-half-empty";
return remainingString;
}
}

View File

@@ -104,6 +104,13 @@ namespace Content.Shared.Chemistry.Reagent
[DataField]
public bool Slippery;
/// <summary>
/// How easily this reagent becomes fizzy when aggitated.
/// 0 - completely flat, 1 - fizzes up when nudged.
/// </summary>
[DataField]
public float Fizziness;
/// <summary>
/// How much reagent slows entities down if it's part of a puddle.
/// 0 - no slowdown; 1 - can't move.

View File

@@ -1,28 +1,30 @@
using Content.Server.Nutrition.EntitySystems;
using Content.Shared.Nutrition.EntitySystems;
using Content.Shared.FixedPoint;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Server.Nutrition.Components;
namespace Content.Shared.Nutrition.Components;
[RegisterComponent, Access(typeof(DrinkSystem))]
[NetworkedComponent, AutoGenerateComponentState]
[RegisterComponent, Access(typeof(SharedDrinkSystem))]
public sealed partial class DrinkComponent : Component
{
[DataField, ViewVariables(VVAccess.ReadWrite)]
[DataField]
public string Solution = "drink";
[DataField]
[DataField, AutoNetworkedField]
public SoundSpecifier UseSound = new SoundPathSpecifier("/Audio/Items/drink.ogg");
[DataField, ViewVariables(VVAccess.ReadWrite)]
[DataField, AutoNetworkedField]
public FixedPoint2 TransferAmount = FixedPoint2.New(5);
/// <summary>
/// How long it takes to drink this yourself.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
[DataField, AutoNetworkedField]
public float Delay = 1;
[DataField, ViewVariables(VVAccess.ReadWrite)]
[DataField, AutoNetworkedField]
public bool Examinable = true;
/// <summary>
@@ -30,12 +32,12 @@ public sealed partial class DrinkComponent : Component
/// This means other systems such as equipping on use can run.
/// Example usecase is the bucket.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
[DataField]
public bool IgnoreEmpty;
/// <summary>
/// This is how many seconds it takes to force feed someone this drink.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
[DataField, AutoNetworkedField]
public float ForceFeedDelay = 3;
}

View File

@@ -0,0 +1,106 @@
using Content.Shared.Nutrition.EntitySystems;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Nutrition.Components;
/// <summary>
/// Represents a solution container that can hold the pressure from a solution that
/// gets fizzy when aggitated, and can spray the solution when opened or thrown.
/// Handles simulating the fizziness of the solution, responding to aggitating events,
/// and spraying the solution out when opening or throwing the entity.
/// </summary>
[NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause]
[RegisterComponent, Access(typeof(PressurizedSolutionSystem))]
public sealed partial class PressurizedSolutionComponent : Component
{
/// <summary>
/// The name of the solution to use.
/// </summary>
[DataField]
public string Solution = "drink";
/// <summary>
/// The sound to play when the solution sprays out of the container.
/// </summary>
[DataField]
public SoundSpecifier SpraySound = new SoundPathSpecifier("/Audio/Items/soda_spray.ogg");
/// <summary>
/// The longest amount of time that the solution can remain fizzy after being aggitated.
/// Put another way, how long the solution will remain fizzy when aggitated the maximum amount.
/// Used to calculate the current fizziness level.
/// </summary>
[DataField]
public TimeSpan FizzinessMaxDuration = TimeSpan.FromSeconds(120);
/// <summary>
/// The time at which the solution will be fully settled after being shaken.
/// </summary>
[DataField, AutoNetworkedField, AutoPausedField]
public TimeSpan FizzySettleTime;
/// <summary>
/// How much to increase the solution's fizziness each time it's shaken.
/// This assumes the solution has maximum fizzability.
/// A value of 1 will maximize it with a single shake, and a value of
/// 0.5 will increase it by half with each shake.
/// </summary>
[DataField]
public float FizzinessAddedOnShake = 1.0f;
/// <summary>
/// How much to increase the solution's fizziness when it lands after being thrown.
/// This assumes the solution has maximum fizzability.
/// </summary>
[DataField]
public float FizzinessAddedOnLand = 0.25f;
/// <summary>
/// How much to modify the chance of spraying when the entity is opened.
/// Increasing this effectively increases the fizziness value when checking if it should spray.
/// </summary>
[DataField]
public float SprayChanceModOnOpened = -0.01f; // Just enough to prevent spraying at 0 fizziness
/// <summary>
/// How much to modify the chance of spraying when the entity is shaken.
/// Increasing this effectively increases the fizziness value when checking if it should spray.
/// </summary>
[DataField]
public float SprayChanceModOnShake = -1; // No spraying when shaken by default
/// <summary>
/// How much to modify the chance of spraying when the entity lands after being thrown.
/// Increasing this effectively increases the fizziness value when checking if it should spray.
/// </summary>
[DataField]
public float SprayChanceModOnLand = 0.25f;
/// <summary>
/// Holds the current randomly-rolled threshold value for spraying.
/// If fizziness exceeds this value when the entity is opened, it will spray.
/// By rolling this value when the entity is aggitated, we can have randomization
/// while still having prediction!
/// </summary>
[DataField, AutoNetworkedField]
public float SprayFizzinessThresholdRoll;
/// <summary>
/// Popup message shown to user when sprayed by the solution.
/// </summary>
[DataField]
public LocId SprayHolderMessageSelf = "pressurized-solution-spray-holder-self";
/// <summary>
/// Popup message shown to others when a user is sprayed by the solution.
/// </summary>
[DataField]
public LocId SprayHolderMessageOthers = "pressurized-solution-spray-holder-others";
/// <summary>
/// Popup message shown above the entity when the solution sprays without a target.
/// </summary>
[DataField]
public LocId SprayGroundMessage = "pressurized-solution-spray-ground";
}

View File

@@ -0,0 +1,50 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Nutrition.Components;
/// <summary>
/// Adds a "Shake" verb to the entity's verb menu.
/// Handles checking the entity can be shaken, displaying popups when shaking,
/// and raising a ShakeEvent when a shake occurs.
/// Reacting to being shaken is left up to other components.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class ShakeableComponent : Component
{
/// <summary>
/// How long it takes to shake this item.
/// </summary>
[DataField]
public TimeSpan ShakeDuration = TimeSpan.FromSeconds(1f);
/// <summary>
/// Does the entity need to be in the user's hand in order to be shaken?
/// </summary>
[DataField]
public bool RequireInHand;
/// <summary>
/// Label to display in the verbs menu for this item's shake action.
/// </summary>
[DataField]
public LocId ShakeVerbText = "shakeable-verb";
/// <summary>
/// Text that will be displayed to the user when shaking this item.
/// </summary>
[DataField]
public LocId ShakePopupMessageSelf = "shakeable-popup-message-self";
/// <summary>
/// Text that will be displayed to other users when someone shakes this item.
/// </summary>
[DataField]
public LocId ShakePopupMessageOthers = "shakeable-popup-message-others";
/// <summary>
/// The sound that will be played when shaking this item.
/// </summary>
[DataField]
public SoundSpecifier ShakeSound = new SoundPathSpecifier("/Audio/Items/soda_shake.ogg");
}

View File

@@ -16,9 +16,9 @@ namespace Content.Shared.Nutrition.EntitySystems;
/// </summary>
public sealed partial class OpenableSystem : EntitySystem
{
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] protected readonly SharedAudioSystem Audio = default!;
[Dependency] protected readonly SharedPopupSystem Popup = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
@@ -31,6 +31,8 @@ public sealed partial class OpenableSystem : EntitySystem
SubscribeLocalEvent<OpenableComponent, AfterInteractEvent>(HandleIfClosed);
SubscribeLocalEvent<OpenableComponent, GetVerbsEvent<Verb>>(AddOpenCloseVerbs);
SubscribeLocalEvent<OpenableComponent, SolutionTransferAttemptEvent>(OnTransferAttempt);
SubscribeLocalEvent<OpenableComponent, AttemptShakeEvent>(OnAttemptShake);
SubscribeLocalEvent<OpenableComponent, AttemptAddFizzinessEvent>(OnAttemptAddFizziness);
}
private void OnInit(EntityUid uid, OpenableComponent comp, ComponentInit args)
@@ -100,6 +102,20 @@ public sealed partial class OpenableSystem : EntitySystem
}
}
private void OnAttemptShake(Entity<OpenableComponent> entity, ref AttemptShakeEvent args)
{
// Prevent shaking open containers
if (entity.Comp.Opened)
args.Cancelled = true;
}
private void OnAttemptAddFizziness(Entity<OpenableComponent> entity, ref AttemptAddFizzinessEvent args)
{
// Can't add fizziness to an open container
if (entity.Comp.Opened)
args.Cancelled = true;
}
/// <summary>
/// Returns true if the entity either does not have OpenableComponent or it is opened.
/// Drinks that don't have OpenableComponent are automatically open, so it returns true.
@@ -126,7 +142,7 @@ public sealed partial class OpenableSystem : EntitySystem
return false;
if (user != null)
Popup.PopupEntity(Loc.GetString(comp.ClosedPopup, ("owner", uid)), user.Value, user.Value);
_popup.PopupEntity(Loc.GetString(comp.ClosedPopup, ("owner", uid)), user.Value, user.Value);
return true;
}
@@ -139,13 +155,13 @@ public sealed partial class OpenableSystem : EntitySystem
if (!Resolve(uid, ref comp))
return;
Appearance.SetData(uid, OpenableVisuals.Opened, comp.Opened, appearance);
_appearance.SetData(uid, OpenableVisuals.Opened, comp.Opened, appearance);
}
/// <summary>
/// Sets the opened field and updates open visuals.
/// </summary>
public void SetOpen(EntityUid uid, bool opened = true, OpenableComponent? comp = null)
public void SetOpen(EntityUid uid, bool opened = true, OpenableComponent? comp = null, EntityUid? user = null)
{
if (!Resolve(uid, ref comp, false) || opened == comp.Opened)
return;
@@ -155,12 +171,12 @@ public sealed partial class OpenableSystem : EntitySystem
if (opened)
{
var ev = new OpenableOpenedEvent();
var ev = new OpenableOpenedEvent(user);
RaiseLocalEvent(uid, ref ev);
}
else
{
var ev = new OpenableClosedEvent();
var ev = new OpenableClosedEvent(user);
RaiseLocalEvent(uid, ref ev);
}
@@ -176,8 +192,8 @@ public sealed partial class OpenableSystem : EntitySystem
if (!Resolve(uid, ref comp, false) || comp.Opened)
return false;
SetOpen(uid, true, comp);
Audio.PlayPredicted(comp.Sound, uid, user);
SetOpen(uid, true, comp, user);
_audio.PlayPredicted(comp.Sound, uid, user);
return true;
}
@@ -190,9 +206,9 @@ public sealed partial class OpenableSystem : EntitySystem
if (!Resolve(uid, ref comp, false) || !comp.Opened || !comp.Closeable)
return false;
SetOpen(uid, false, comp);
SetOpen(uid, false, comp, user);
if (comp.CloseSound != null)
Audio.PlayPredicted(comp.CloseSound, uid, user);
_audio.PlayPredicted(comp.CloseSound, uid, user);
return true;
}
}
@@ -201,10 +217,10 @@ public sealed partial class OpenableSystem : EntitySystem
/// Raised after an Openable is opened.
/// </summary>
[ByRefEvent]
public record struct OpenableOpenedEvent;
public record struct OpenableOpenedEvent(EntityUid? User = null);
/// <summary>
/// Raised after an Openable is closed.
/// </summary>
[ByRefEvent]
public record struct OpenableClosedEvent;
public record struct OpenableClosedEvent(EntityUid? User = null);

View File

@@ -0,0 +1,285 @@
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Nutrition.Components;
using Content.Shared.Throwing;
using Content.Shared.IdentityManagement;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Robust.Shared.Prototypes;
using Robust.Shared.Network;
using Content.Shared.Fluids;
using Content.Shared.Popups;
namespace Content.Shared.Nutrition.EntitySystems;
public sealed partial class PressurizedSolutionSystem : EntitySystem
{
[Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
[Dependency] private readonly OpenableSystem _openable = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedPuddleSystem _puddle = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PressurizedSolutionComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<PressurizedSolutionComponent, ShakeEvent>(OnShake);
SubscribeLocalEvent<PressurizedSolutionComponent, OpenableOpenedEvent>(OnOpened);
SubscribeLocalEvent<PressurizedSolutionComponent, LandEvent>(OnLand);
SubscribeLocalEvent<PressurizedSolutionComponent, SolutionContainerChangedEvent>(OnSolutionUpdate);
}
/// <summary>
/// Helper method for checking if the solution's fizziness is high enough to spray.
/// <paramref name="chanceMod"/> is added to the actual fizziness for the comparison.
/// </summary>
private bool SprayCheck(Entity<PressurizedSolutionComponent> entity, float chanceMod = 0)
{
return Fizziness((entity, entity.Comp)) + chanceMod > entity.Comp.SprayFizzinessThresholdRoll;
}
/// <summary>
/// Calculates how readily the contained solution becomes fizzy.
/// </summary>
private float SolutionFizzability(Entity<PressurizedSolutionComponent> entity)
{
if (!_solutionContainer.TryGetSolution(entity.Owner, entity.Comp.Solution, out var _, out var solution))
return 0;
// An empty solution can't be fizzy
if (solution.Volume <= 0)
return 0;
var totalFizzability = 0f;
// Check each reagent in the solution
foreach (var reagent in solution.Contents)
{
if (_prototypeManager.TryIndex(reagent.Reagent.Prototype, out ReagentPrototype? reagentProto) && reagentProto != null)
{
// What portion of the solution is this reagent?
var proportion = (float) (reagent.Quantity / solution.Volume);
totalFizzability += reagentProto.Fizziness * proportion;
}
}
return totalFizzability;
}
/// <summary>
/// Increases the fizziness level of the solution by the given amount,
/// scaled by the solution's fizzability.
/// 0 will result in no change, and 1 will maximize fizziness.
/// Also rerolls the spray threshold.
/// </summary>
private void AddFizziness(Entity<PressurizedSolutionComponent> entity, float amount)
{
var fizzability = SolutionFizzability(entity);
// Can't add fizziness if the solution isn't fizzy
if (fizzability <= 0)
return;
// Make sure nothing is preventing fizziness from being added
var attemptEv = new AttemptAddFizzinessEvent(entity, amount);
RaiseLocalEvent(entity, ref attemptEv);
if (attemptEv.Cancelled)
return;
// Scale added fizziness by the solution's fizzability
amount *= fizzability;
// Convert fizziness to time
var duration = amount * entity.Comp.FizzinessMaxDuration;
// Add to the existing settle time, if one exists. Otherwise, add to the current time
var start = entity.Comp.FizzySettleTime > _timing.CurTime ? entity.Comp.FizzySettleTime : _timing.CurTime;
var newTime = start + duration;
// Cap the maximum fizziness
var maxEnd = _timing.CurTime + entity.Comp.FizzinessMaxDuration;
if (newTime > maxEnd)
newTime = maxEnd;
entity.Comp.FizzySettleTime = newTime;
// Roll a new fizziness threshold
RollSprayThreshold(entity);
}
/// <summary>
/// Helper method. Performs a <see cref="SprayCheck"/>. If it passes, calls <see cref="TrySpray"/>. If it fails, <see cref="AddFizziness"/>.
/// </summary>
private void SprayOrAddFizziness(Entity<PressurizedSolutionComponent> entity, float chanceMod = 0, float fizzinessToAdd = 0, EntityUid? user = null)
{
if (SprayCheck(entity, chanceMod))
TrySpray((entity, entity.Comp), user);
else
AddFizziness(entity, fizzinessToAdd);
}
/// <summary>
/// Randomly generates a new spray threshold.
/// This is the value used to compare fizziness against when doing <see cref="SprayCheck"/>.
/// Since RNG will give different results between client and server, this is run on the server
/// and synced to the client by marking the component dirty.
/// We roll this in advance, rather than during <see cref="SprayCheck"/>, so that the value (hopefully)
/// has time to get synced to the client, so we can try be accurate with prediction.
/// </summary>
private void RollSprayThreshold(Entity<PressurizedSolutionComponent> entity)
{
// Can't predict random, so we wait for the server to tell us
if (!_net.IsServer)
return;
entity.Comp.SprayFizzinessThresholdRoll = _random.NextFloat();
Dirty(entity, entity.Comp);
}
#region Public API
/// <summary>
/// Does the entity contain a solution capable of being fizzy?
/// </summary>
public bool CanSpray(Entity<PressurizedSolutionComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp, false))
return false;
return SolutionFizzability((entity, entity.Comp)) > 0;
}
/// <summary>
/// Attempts to spray the solution onto the given entity, or the ground if none is given.
/// Fails if the solution isn't able to be sprayed.
/// </summary>
public bool TrySpray(Entity<PressurizedSolutionComponent?> entity, EntityUid? target = null)
{
if (!Resolve(entity, ref entity.Comp))
return false;
if (!CanSpray(entity))
return false;
if (!_solutionContainer.TryGetSolution(entity.Owner, entity.Comp.Solution, out var soln, out var interactions))
return false;
// If the container is openable, open it
_openable.SetOpen(entity, true);
// Get the spray solution from the container
var solution = _solutionContainer.SplitSolution(soln.Value, interactions.Volume);
// Spray the solution onto the ground and anyone nearby
if (TryComp<TransformComponent>(entity, out var transform))
_puddle.TrySplashSpillAt(entity, transform.Coordinates, solution, out _, sound: false);
var drinkName = Identity.Entity(entity, EntityManager);
if (target != null)
{
var victimName = Identity.Entity(target.Value, EntityManager);
var selfMessage = Loc.GetString(entity.Comp.SprayHolderMessageSelf, ("victim", victimName), ("drink", drinkName));
var othersMessage = Loc.GetString(entity.Comp.SprayHolderMessageOthers, ("victim", victimName), ("drink", drinkName));
_popup.PopupPredicted(selfMessage, othersMessage, target.Value, target.Value);
}
else
{
// Show a popup to everyone in PVS range
if (_timing.IsFirstTimePredicted)
_popup.PopupEntity(Loc.GetString(entity.Comp.SprayGroundMessage, ("drink", drinkName)), entity);
}
_audio.PlayPredicted(entity.Comp.SpraySound, entity, target);
// We just used all our fizziness, so clear it
TryClearFizziness(entity);
return true;
}
/// <summary>
/// What is the current fizziness level of the solution, from 0 to 1?
/// </summary>
public double Fizziness(Entity<PressurizedSolutionComponent?> entity)
{
// No component means no fizz
if (!Resolve(entity, ref entity.Comp, false))
return 0;
// No negative fizziness
if (entity.Comp.FizzySettleTime <= _timing.CurTime)
return 0;
var currentDuration = entity.Comp.FizzySettleTime - _timing.CurTime;
return Easings.InOutCubic((float) Math.Min(currentDuration / entity.Comp.FizzinessMaxDuration, 1));
}
/// <summary>
/// Attempts to clear any fizziness in the solution.
/// </summary>
/// <remarks>Rolls a new spray threshold.</remarks>
public void TryClearFizziness(Entity<PressurizedSolutionComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp))
return;
entity.Comp.FizzySettleTime = TimeSpan.Zero;
// Roll a new fizziness threshold
RollSprayThreshold((entity, entity.Comp));
}
#endregion
#region Event Handlers
private void OnMapInit(Entity<PressurizedSolutionComponent> entity, ref MapInitEvent args)
{
RollSprayThreshold(entity);
}
private void OnOpened(Entity<PressurizedSolutionComponent> entity, ref OpenableOpenedEvent args)
{
// Make sure the opener is actually holding the drink
var held = args.User != null && _hands.IsHolding(args.User.Value, entity, out _);
SprayOrAddFizziness(entity, entity.Comp.SprayChanceModOnOpened, -1, held ? args.User : null);
}
private void OnShake(Entity<PressurizedSolutionComponent> entity, ref ShakeEvent args)
{
SprayOrAddFizziness(entity, entity.Comp.SprayChanceModOnShake, entity.Comp.FizzinessAddedOnShake, args.Shaker);
}
private void OnLand(Entity<PressurizedSolutionComponent> entity, ref LandEvent args)
{
SprayOrAddFizziness(entity, entity.Comp.SprayChanceModOnLand, entity.Comp.FizzinessAddedOnLand);
}
private void OnSolutionUpdate(Entity<PressurizedSolutionComponent> entity, ref SolutionContainerChangedEvent args)
{
if (args.SolutionId != entity.Comp.Solution)
return;
// If the solution is no longer capable of being fizzy, clear any built up fizziness
if (SolutionFizzability(entity) <= 0)
TryClearFizziness((entity, entity.Comp));
}
#endregion
}
[ByRefEvent]
public record struct AttemptAddFizzinessEvent(Entity<PressurizedSolutionComponent> Entity, float Amount)
{
public bool Cancelled;
}

View File

@@ -0,0 +1,155 @@
using Content.Shared.DoAfter;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement;
using Content.Shared.Nutrition.Components;
using Content.Shared.Popups;
using Content.Shared.Verbs;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Serialization;
namespace Content.Shared.Nutrition.EntitySystems;
public sealed partial class ShakeableSystem : EntitySystem
{
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ShakeableComponent, GetVerbsEvent<Verb>>(AddShakeVerb);
SubscribeLocalEvent<ShakeableComponent, ShakeDoAfterEvent>(OnShakeDoAfter);
}
private void AddShakeVerb(EntityUid uid, ShakeableComponent component, GetVerbsEvent<Verb> args)
{
if (args.Hands == null || !args.CanAccess || !args.CanInteract)
return;
if (!CanShake((uid, component), args.User))
return;
var shakeVerb = new Verb()
{
Text = Loc.GetString(component.ShakeVerbText),
Act = () => TryStartShake((args.Target, component), args.User)
};
args.Verbs.Add(shakeVerb);
}
private void OnShakeDoAfter(Entity<ShakeableComponent> entity, ref ShakeDoAfterEvent args)
{
if (args.Handled || args.Cancelled)
return;
TryShake((entity, entity.Comp), args.User);
}
/// <summary>
/// Attempts to start the doAfter to shake the entity.
/// Fails and returns false if the entity cannot be shaken for any reason.
/// If successful, displays popup messages, plays shake sound, and starts the doAfter.
/// </summary>
public bool TryStartShake(Entity<ShakeableComponent?> entity, EntityUid user)
{
if (!Resolve(entity, ref entity.Comp))
return false;
if (!CanShake(entity, user))
return false;
var doAfterArgs = new DoAfterArgs(EntityManager,
user,
entity.Comp.ShakeDuration,
new ShakeDoAfterEvent(),
eventTarget: entity,
target: user,
used: entity)
{
NeedHand = true,
BreakOnDamage = true,
DistanceThreshold = 1,
MovementThreshold = 0.01f,
BreakOnHandChange = entity.Comp.RequireInHand,
};
if (entity.Comp.RequireInHand)
doAfterArgs.BreakOnHandChange = true;
if (!_doAfter.TryStartDoAfter(doAfterArgs))
return false;
var userName = Identity.Entity(user, EntityManager);
var shakeableName = Identity.Entity(entity, EntityManager);
var selfMessage = Loc.GetString(entity.Comp.ShakePopupMessageSelf, ("user", userName), ("shakeable", shakeableName));
var othersMessage = Loc.GetString(entity.Comp.ShakePopupMessageOthers, ("user", userName), ("shakeable", shakeableName));
_popup.PopupPredicted(selfMessage, othersMessage, user, user);
_audio.PlayPredicted(entity.Comp.ShakeSound, entity, user);
return true;
}
/// <summary>
/// Attempts to shake the entity, skipping the doAfter.
/// Fails and returns false if the entity cannot be shaken for any reason.
/// If successful, raises a ShakeEvent on the entity.
/// </summary>
public bool TryShake(Entity<ShakeableComponent?> entity, EntityUid? user = null)
{
if (!Resolve(entity, ref entity.Comp))
return false;
if (!CanShake(entity, user))
return false;
var ev = new ShakeEvent(user);
RaiseLocalEvent(entity, ref ev);
return true;
}
/// <summary>
/// Is it possible for the given user to shake the entity?
/// </summary>
public bool CanShake(Entity<ShakeableComponent?> entity, EntityUid? user = null)
{
if (!Resolve(entity, ref entity.Comp, false))
return false;
// If required to be in hand, fail if the user is not holding this entity
if (user != null && entity.Comp.RequireInHand && !_hands.IsHolding(user.Value, entity, out _))
return false;
var attemptEv = new AttemptShakeEvent();
RaiseLocalEvent(entity, ref attemptEv);
if (attemptEv.Cancelled)
return false;
return true;
}
}
/// <summary>
/// Raised when a ShakeableComponent is shaken, after the doAfter completes.
/// </summary>
[ByRefEvent]
public record struct ShakeEvent(EntityUid? Shaker);
/// <summary>
/// Raised when trying to shake a ShakeableComponent. If cancelled, the
/// entity will not be shaken.
/// </summary>
[ByRefEvent]
public record struct AttemptShakeEvent()
{
public bool Cancelled;
}
[Serializable, NetSerializable]
public sealed partial class ShakeDoAfterEvent : SimpleDoAfterEvent
{
}

View File

@@ -0,0 +1,90 @@
using Content.Shared.Chemistry.Components.SolutionManager;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Examine;
using Content.Shared.FixedPoint;
using Content.Shared.Nutrition.Components;
namespace Content.Shared.Nutrition.EntitySystems;
public abstract partial class SharedDrinkSystem : EntitySystem
{
[Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
[Dependency] private readonly OpenableSystem _openable = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DrinkComponent, AttemptShakeEvent>(OnAttemptShake);
SubscribeLocalEvent<DrinkComponent, ExaminedEvent>(OnExamined);
}
protected void OnAttemptShake(Entity<DrinkComponent> entity, ref AttemptShakeEvent args)
{
if (IsEmpty(entity, entity.Comp))
args.Cancelled = true;
}
protected void OnExamined(Entity<DrinkComponent> entity, ref ExaminedEvent args)
{
TryComp<OpenableComponent>(entity, out var openable);
if (_openable.IsClosed(entity.Owner, null, openable) || !args.IsInDetailsRange || !entity.Comp.Examinable)
return;
var empty = IsEmpty(entity, entity.Comp);
if (empty)
{
args.PushMarkup(Loc.GetString("drink-component-on-examine-is-empty"));
return;
}
if (HasComp<ExaminableSolutionComponent>(entity))
{
//provide exact measurement for beakers
args.PushText(Loc.GetString("drink-component-on-examine-exact-volume", ("amount", DrinkVolume(entity, entity.Comp))));
}
else
{
//general approximation
var remainingString = (int) _solutionContainer.PercentFull(entity) switch
{
100 => "drink-component-on-examine-is-full",
> 66 => "drink-component-on-examine-is-mostly-full",
> 33 => HalfEmptyOrHalfFull(args),
_ => "drink-component-on-examine-is-mostly-empty",
};
args.PushMarkup(Loc.GetString(remainingString));
}
}
protected FixedPoint2 DrinkVolume(EntityUid uid, DrinkComponent? component = null)
{
if (!Resolve(uid, ref component))
return FixedPoint2.Zero;
if (!_solutionContainer.TryGetSolution(uid, component.Solution, out _, out var sol))
return FixedPoint2.Zero;
return sol.Volume;
}
protected bool IsEmpty(EntityUid uid, DrinkComponent? component = null)
{
if (!Resolve(uid, ref component))
return true;
return DrinkVolume(uid, component) <= 0;
}
// some see half empty, and others see half full
private string HalfEmptyOrHalfFull(ExaminedEvent args)
{
string remainingString = "drink-component-on-examine-is-half-full";
if (TryComp<MetaDataComponent>(args.Examiner, out var examiner) && examiner.EntityName.Length > 0
&& string.Compare(examiner.EntityName.Substring(0, 1), "m", StringComparison.InvariantCultureIgnoreCase) > 0)
remainingString = "drink-component-on-examine-is-half-empty";
return remainingString;
}
}

View File

@@ -93,6 +93,16 @@
copyright: "User Hanbaal on freesound.org. Converted to ogg by TheShuEd"
source: "https://freesound.org/people/Hanbaal/sounds/178669/"
- files: ["soda_shake.ogg"]
license: "CC-BY-NC-4.0"
copyright: "User mcmast on freesound.org. Converted and edited by Tayrtahn"
source: "https://freesound.org/people/mcmast/sounds/456703/"
- files: ["soda_spray.ogg"]
license: "CC0-1.0"
copyright: "User Hajisounds on freesound.org. Converted and edited by Tayrtahn"
source: "https://freesound.org/people/Hajisounds/sounds/709149/"
- files: ["newton_cradle.ogg"]
license: "CC-BY-4.0"
copyright: "User LoafDV on freesound.org. Converted to ogg end edited by lzk228"

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,3 @@
pressurized-solution-spray-holder-self = { CAPITALIZE(THE($drink)) } sprays on you!
pressurized-solution-spray-holder-others = { CAPITALIZE(THE($drink)) } sprays on { THE($victim) }!
pressurized-solution-spray-ground = The contents of { THE($drink) } spray out!

View File

@@ -0,0 +1,3 @@
shakeable-verb = Shake
shakeable-popup-message-others = { CAPITALIZE(THE($user)) } shakes { THE($shakeable) }
shakeable-popup-message-self = You shake { THE($shakeable) }

View File

@@ -15,6 +15,9 @@
solutions:
drink:
maxVol: 50
- type: PressurizedSolution
solution: drink
- type: Shakeable
- type: Sprite
state: icon
- type: Item

View File

@@ -39,6 +39,9 @@
- type: PhysicalComposition
materialComposition:
Plastic: 100
- type: PressurizedSolution
solution: drink
- type: Shakeable
- type: entity
parent: DrinkBottlePlasticBaseFull

View File

@@ -6,7 +6,7 @@
components:
- type: Drink
- type: Openable
- type: PressurizedDrink
- type: Shakeable
- type: SolutionContainerManager
solutions:
drink:
@@ -34,6 +34,8 @@
solution: drink
- type: DrainableSolution
solution: drink
- type: PressurizedSolution
solution: drink
- type: Appearance
- type: GenericVisualizer
visuals:

View File

@@ -146,6 +146,9 @@
- type: RandomFillSolution
solution: drink
weightedRandomId: RandomFillMopwata
- type: PressurizedSolution
solution: drink
- type: Shakeable
- type: Appearance
- type: GenericVisualizer
visuals:

View File

@@ -9,6 +9,7 @@
drink:
maxVol: 100
- type: Drink
- type: Shakeable # Doesn't do anything, but I mean...
- type: FitsInDispenser
solution: drink
- type: DrawableSolution

View File

@@ -39,6 +39,7 @@
metamorphicMaxFillLevels: 5
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.6
- type: reagent
id: Beer
@@ -55,6 +56,7 @@
metamorphicMaxFillLevels: 6
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.6
- type: reagent
id: BlueCuracao
@@ -458,6 +460,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.3
fizziness: 0.8
# Mixed Alcohol
@@ -675,6 +678,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.15
fizziness: 0.3
- type: reagent
id: BlackRussian
@@ -814,6 +818,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.07
fizziness: 0.2
- type: reagent
id: DemonsBlood
@@ -829,6 +834,7 @@
metamorphicMaxFillLevels: 4
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.3
- type: reagent
id: DevilsKiss
@@ -916,6 +922,7 @@
metamorphicMaxFillLevels: 5
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.15
- type: reagent
id: GargleBlaster
@@ -962,6 +969,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.07
fizziness: 0.4 # A little high, but it has fizz in the name
- type: reagent
id: GinTonic
@@ -985,6 +993,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.07
fizziness: 0.4
- type: reagent
id: Gildlager
@@ -1062,6 +1071,7 @@
metamorphicMaxFillLevels: 6
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.6
- type: reagent
id: IrishCarBomb
@@ -1199,6 +1209,7 @@
metamorphicMaxFillLevels: 2
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.7
- type: reagent
id: Margarita
@@ -1252,6 +1263,7 @@
metamorphicMaxFillLevels: 5
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.4
- type: reagent
id: Mojito
@@ -1267,6 +1279,7 @@
metamorphicMaxFillLevels: 6
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.3
- type: reagent
id: Moonshine
@@ -1371,6 +1384,7 @@
metamorphicMaxFillLevels: 5
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.4
- type: reagent
id: PinaColada
@@ -1500,6 +1514,7 @@
metamorphicMaxFillLevels: 6
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.3
- type: reagent
id: SuiDream
@@ -1515,6 +1530,7 @@
metamorphicMaxFillLevels: 5
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.2
- type: reagent
id: SyndicateBomb
@@ -1530,6 +1546,7 @@
metamorphicMaxFillLevels: 6
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.6
- type: reagent
id: TequilaSunrise
@@ -1568,6 +1585,7 @@
metamorphicMaxFillLevels: 3
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.2
- type: reagent
id: ThreeMileIsland
@@ -1655,6 +1673,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.07
fizziness: 0.4
- type: reagent
id: WhiskeyCola
@@ -1678,6 +1697,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.07
fizziness: 0.3
- type: reagent
id: WhiskeySoda
@@ -1701,6 +1721,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.07
fizziness: 0.4
- type: reagent
id: WhiteGilgamesh
@@ -1718,6 +1739,7 @@
- !type:AdjustReagent
reagent: Ethanol
amount: 0.15
fizziness: 0.5
- type: reagent
id: WhiteRussian

View File

@@ -40,6 +40,7 @@
collection: FootstepSticky
params:
volume: 6
fizziness: 0.5
- type: reagent
id: BaseAlcohol
@@ -75,4 +76,4 @@
footstepSound:
collection: FootstepSticky
params:
volume: 6
volume: 6

View File

@@ -322,6 +322,7 @@
damage:
types:
Poison: 1
fizziness: 0.5
- type: reagent
id: SodaWater
@@ -331,6 +332,7 @@
physicalDesc: reagent-physical-desc-fizzy
flavor: fizzy
color: "#619494"
fizziness: 0.8
- type: reagent
id: SoyLatte
@@ -373,6 +375,7 @@
physicalDesc: reagent-physical-desc-fizzy
flavor: tonicwater
color: "#0064C8"
fizziness: 0.4
- type: reagent
id: Water
@@ -467,6 +470,7 @@
effects:
- !type:SatiateThirst
factor: 1
fizziness: 0.3
- type: reagent
id: Posca
@@ -491,6 +495,7 @@
metamorphicMaxFillLevels: 3
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.3
- type: reagent
id: Rewriter
@@ -506,6 +511,7 @@
metamorphicMaxFillLevels: 5
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0.3
- type: reagent
id: Mopwata

View File

@@ -59,6 +59,7 @@
- !type:AdjustReagent
reagent: Theobromine
amount: 0.05
fizziness: 0.4
- type: reagent
id: GrapeSoda
@@ -84,6 +85,7 @@
metamorphicMaxFillLevels: 5
metamorphicFillBaseName: fill-
metamorphicChangeColor: true
fizziness: 0
- type: reagent
id: LemonLime
@@ -102,6 +104,7 @@
physicalDesc: reagent-physical-desc-fizzy
flavor: pwrgamesoda
color: "#9385bf"
fizziness: 0.9 # gamers crave the fizz
- type: reagent
id: RootBeer
@@ -132,6 +135,7 @@
metamorphicMaxFillLevels: 7
metamorphicFillBaseName: fill-
metamorphicChangeColor: false
fizziness: 0.4
- type: reagent
id: SolDry