add fuel costs back to finishing welding (#27030)

* add fuel costs back to welding

* ack

* meh

* eek!
This commit is contained in:
Nemanja
2024-04-19 19:20:30 -04:00
committed by GitHub
parent 299da35c87
commit a47c5561a9
26 changed files with 403 additions and 466 deletions

View File

@@ -0,0 +1,170 @@
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Components.SolutionManager;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.Examine;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Item.ItemToggle.Components;
using Content.Shared.Tools.Components;
namespace Content.Shared.Tools.Systems;
public abstract partial class SharedToolSystem
{
public void InitializeWelder()
{
SubscribeLocalEvent<WelderComponent, ExaminedEvent>(OnWelderExamine);
SubscribeLocalEvent<WelderComponent, AfterInteractEvent>(OnWelderAfterInteract);
SubscribeLocalEvent<WelderComponent, DoAfterAttemptEvent<ToolDoAfterEvent>>(OnWelderToolUseAttempt);
SubscribeLocalEvent<WelderComponent, ToolDoAfterEvent>(OnWelderDoAfter);
SubscribeLocalEvent<WelderComponent, ItemToggledEvent>(OnToggle);
SubscribeLocalEvent<WelderComponent, ItemToggleActivateAttemptEvent>(OnActivateAttempt);
}
public virtual void TurnOn(Entity<WelderComponent> entity, EntityUid? user)
{
if (!SolutionContainerSystem.ResolveSolution(entity.Owner, entity.Comp.FuelSolutionName, ref entity.Comp.FuelSolution))
return;
SolutionContainerSystem.RemoveReagent(entity.Comp.FuelSolution.Value, entity.Comp.FuelReagent, entity.Comp.FuelLitCost);
AdminLogger.Add(LogType.InteractActivate, LogImpact.Low,
$"{ToPrettyString(user):user} toggled {ToPrettyString(entity.Owner):welder} on");
entity.Comp.Enabled = true;
Dirty(entity, entity.Comp);
}
public void TurnOff(Entity<WelderComponent> entity, EntityUid? user)
{
AdminLogger.Add(LogType.InteractActivate, LogImpact.Low,
$"{ToPrettyString(user):user} toggled {ToPrettyString(entity.Owner):welder} off");
entity.Comp.Enabled = false;
Dirty(entity, entity.Comp);
}
public (FixedPoint2 fuel, FixedPoint2 capacity) GetWelderFuelAndCapacity(EntityUid uid, WelderComponent? welder = null, SolutionContainerManagerComponent? solutionContainer = null)
{
if (!Resolve(uid, ref welder, ref solutionContainer)
|| !SolutionContainerSystem.ResolveSolution((uid, solutionContainer), welder.FuelSolutionName, ref welder.FuelSolution, out var fuelSolution))
return (FixedPoint2.Zero, FixedPoint2.Zero);
return (fuelSolution.GetTotalPrototypeQuantity(welder.FuelReagent), fuelSolution.MaxVolume);
}
private void OnWelderExamine(Entity<WelderComponent> entity, ref ExaminedEvent args)
{
using (args.PushGroup(nameof(WelderComponent)))
{
if (ItemToggle.IsActivated(entity.Owner))
{
args.PushMarkup(Loc.GetString("welder-component-on-examine-welder-lit-message"));
}
else
{
args.PushMarkup(Loc.GetString("welder-component-on-examine-welder-not-lit-message"));
}
if (args.IsInDetailsRange)
{
var (fuel, capacity) = GetWelderFuelAndCapacity(entity.Owner, entity.Comp);
args.PushMarkup(Loc.GetString("welder-component-on-examine-detailed-message",
("colorName", fuel < capacity / FixedPoint2.New(4f) ? "darkorange" : "orange"),
("fuelLeft", fuel),
("fuelCapacity", capacity),
("status", string.Empty))); // Lit status is handled above
}
}
}
private void OnWelderAfterInteract(Entity<WelderComponent> entity, ref AfterInteractEvent args)
{
if (args.Handled)
return;
if (args.Target is not { Valid: true } target || !args.CanReach)
return;
if (TryComp(target, out ReagentTankComponent? tank)
&& tank.TankType == ReagentTankType.Fuel
&& SolutionContainerSystem.TryGetDrainableSolution(target, out var targetSoln, out var targetSolution)
&& SolutionContainerSystem.ResolveSolution(entity.Owner, entity.Comp.FuelSolutionName, ref entity.Comp.FuelSolution, out var welderSolution))
{
var trans = FixedPoint2.Min(welderSolution.AvailableVolume, targetSolution.Volume);
if (trans > 0)
{
var drained = SolutionContainerSystem.Drain(target, targetSoln.Value, trans);
SolutionContainerSystem.TryAddSolution(entity.Comp.FuelSolution.Value, drained);
_audioSystem.PlayPredicted(entity.Comp.WelderRefill, entity, user: args.User);
_popup.PopupClient(Loc.GetString("welder-component-after-interact-refueled-message"), entity, args.User);
}
else if (welderSolution.AvailableVolume <= 0)
{
_popup.PopupClient(Loc.GetString("welder-component-already-full"), entity, args.User);
}
else
{
_popup.PopupClient(Loc.GetString("welder-component-no-fuel-in-tank", ("owner", args.Target)), entity, args.User);
}
args.Handled = true;
}
}
private void OnWelderToolUseAttempt(Entity<WelderComponent> entity, ref DoAfterAttemptEvent<ToolDoAfterEvent> args)
{
var user = args.DoAfter.Args.User;
if (!ItemToggle.IsActivated(entity.Owner))
{
_popup.PopupClient(Loc.GetString("welder-component-welder-not-lit-message"), entity, user);
args.Cancel();
return;
}
var (fuel, _) = GetWelderFuelAndCapacity(entity);
if (args.Event.Fuel > fuel)
{
_popup.PopupClient(Loc.GetString("welder-component-cannot-weld-message"), entity, user);
args.Cancel();
}
}
private void OnWelderDoAfter(Entity<WelderComponent> ent, ref ToolDoAfterEvent args)
{
if (args.Cancelled)
return;
if (!SolutionContainerSystem.TryGetSolution(ent.Owner, ent.Comp.FuelSolutionName, out var solution))
return;
SolutionContainerSystem.RemoveReagent(solution.Value, ent.Comp.FuelReagent, FixedPoint2.New(args.Fuel));
}
private void OnToggle(Entity<WelderComponent> entity, ref ItemToggledEvent args)
{
if (args.Activated)
TurnOn(entity, args.User);
else
TurnOff(entity, args.User);
}
private void OnActivateAttempt(Entity<WelderComponent> entity, ref ItemToggleActivateAttemptEvent args)
{
if (!SolutionContainerSystem.ResolveSolution(entity.Owner, entity.Comp.FuelSolutionName, ref entity.Comp.FuelSolution, out var solution))
{
args.Cancelled = true;
args.Popup = Loc.GetString("welder-component-no-fuel-message");
return;
}
var fuel = solution.GetTotalPrototypeQuantity(entity.Comp.FuelReagent);
if (fuel == FixedPoint2.Zero || fuel < entity.Comp.FuelLitCost)
{
args.Popup = Loc.GetString("welder-component-no-fuel-message");
args.Cancelled = true;
}
}
}

View File

@@ -1,8 +1,12 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Item.ItemToggle;
using Content.Shared.Maps;
using Content.Shared.Popups;
using Content.Shared.Tools.Components;
using JetBrains.Annotations;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
@@ -15,12 +19,15 @@ public abstract partial class SharedToolSystem : EntitySystem
{
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] protected readonly ISharedAdminLogManager AdminLogger = default!;
[Dependency] protected readonly ISharedAdminLogManager AdminLogger = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
[Dependency] protected readonly SharedInteractionSystem InteractionSystem = default!;
[Dependency] protected readonly SharedItemToggleSystem ItemToggle = default!;
[Dependency] private readonly SharedMapSystem _maps = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] protected readonly SharedSolutionContainerSystem SolutionContainerSystem = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly TileSystem _tiles = default!;
[Dependency] private readonly TurfSystem _turfs = default!;
@@ -29,6 +36,7 @@ public abstract partial class SharedToolSystem : EntitySystem
{
InitializeMultipleTool();
InitializeTile();
InitializeWelder();
SubscribeLocalEvent<ToolComponent, ToolDoAfterEvent>(OnDoAfter);
}
@@ -66,6 +74,7 @@ public abstract partial class SharedToolSystem : EntitySystem
/// <param name="toolQualitiesNeeded">The qualities needed for this tool to work.</param>
/// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
/// will be directed at the tool target.</param>
/// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
@@ -75,6 +84,7 @@ public abstract partial class SharedToolSystem : EntitySystem
float doAfterDelay,
IEnumerable<string> toolQualitiesNeeded,
DoAfterEvent doAfterEv,
float fuel = 0,
ToolComponent? toolComponent = null)
{
return UseTool(tool,
@@ -84,6 +94,7 @@ public abstract partial class SharedToolSystem : EntitySystem
toolQualitiesNeeded,
doAfterEv,
out _,
fuel,
toolComponent);
}
@@ -101,6 +112,7 @@ public abstract partial class SharedToolSystem : EntitySystem
/// will be directed at the tool target.</param>
/// <param name="id">The id of the DoAfter that was created. This may be null even if the function returns true in
/// the event that this tool-use cancelled an existing DoAfter</param>
/// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
@@ -111,31 +123,30 @@ public abstract partial class SharedToolSystem : EntitySystem
IEnumerable<string> toolQualitiesNeeded,
DoAfterEvent doAfterEv,
out DoAfterId? id,
float fuel = 0,
ToolComponent? toolComponent = null)
{
id = null;
if (!Resolve(tool, ref toolComponent, false))
return false;
if (!CanStartToolUse(tool, user, target, toolQualitiesNeeded, toolComponent))
if (!CanStartToolUse(tool, user, target, fuel, toolQualitiesNeeded, toolComponent))
return false;
var toolEvent = new ToolDoAfterEvent(doAfterEv, GetNetEntity(target));
var toolEvent = new ToolDoAfterEvent(fuel, doAfterEv, GetNetEntity(target));
var doAfterArgs = new DoAfterArgs(EntityManager, user, delay / toolComponent.SpeedModifier, toolEvent, tool, target: target, used: tool)
{
BreakOnDamage = true,
BreakOnMove = true,
BreakOnWeightlessMove = false,
NeedHand = tool != user,
AttemptFrequency = IsWelder(tool) ? AttemptFrequency.EveryTick : AttemptFrequency.Never
AttemptFrequency = fuel > 0 ? AttemptFrequency.EveryTick : AttemptFrequency.Never
};
_doAfterSystem.TryStartDoAfter(doAfterArgs, out id);
return true;
}
protected abstract bool IsWelder(EntityUid uid);
/// <summary>
/// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
/// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
@@ -148,6 +159,7 @@ public abstract partial class SharedToolSystem : EntitySystem
/// <param name="toolQualityNeeded">The quality needed for this tool to work.</param>
/// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
/// will be directed at the tool target.</param>
/// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
@@ -157,6 +169,7 @@ public abstract partial class SharedToolSystem : EntitySystem
float doAfterDelay,
string toolQualityNeeded,
DoAfterEvent doAfterEv,
float fuel = 0,
ToolComponent? toolComponent = null)
{
return UseTool(tool,
@@ -166,6 +179,7 @@ public abstract partial class SharedToolSystem : EntitySystem
new[] { toolQualityNeeded },
doAfterEv,
out _,
fuel,
toolComponent);
}
@@ -180,12 +194,13 @@ public abstract partial class SharedToolSystem : EntitySystem
/// <summary>
/// Whether a tool entity has all specified qualities or not.
/// </summary>
[PublicAPI]
public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
}
private bool CanStartToolUse(EntityUid tool, EntityUid user, EntityUid? target, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
private bool CanStartToolUse(EntityUid tool, EntityUid user, EntityUid? target, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
@@ -220,6 +235,9 @@ public abstract partial class SharedToolSystem : EntitySystem
[Serializable, NetSerializable]
protected sealed partial class ToolDoAfterEvent : DoAfterEvent
{
[DataField]
public float Fuel;
/// <summary>
/// Entity that the wrapped do after event will get directed at. If null, event will be broadcast.
/// </summary>
@@ -233,10 +251,11 @@ public abstract partial class SharedToolSystem : EntitySystem
{
}
public ToolDoAfterEvent(DoAfterEvent wrappedEvent, NetEntity? originalTarget)
public ToolDoAfterEvent(float fuel, DoAfterEvent wrappedEvent, NetEntity? originalTarget)
{
DebugTools.Assert(wrappedEvent.GetType().HasCustomAttribute<NetSerializableAttribute>(), "Tool event is not serializable");
Fuel = fuel;
WrappedEvent = wrappedEvent;
OriginalTarget = originalTarget;
}
@@ -249,14 +268,14 @@ public abstract partial class SharedToolSystem : EntitySystem
if (evClone == WrappedEvent)
return this;
return new ToolDoAfterEvent(evClone, OriginalTarget);
return new ToolDoAfterEvent(Fuel, evClone, OriginalTarget);
}
}
[Serializable, NetSerializable]
protected sealed partial class LatticeCuttingCompleteEvent : DoAfterEvent
{
[DataField("coordinates", required:true)]
[DataField(required:true)]
public NetCoordinates Coordinates;
private LatticeCuttingCompleteEvent()
@@ -273,9 +292,7 @@ public abstract partial class SharedToolSystem : EntitySystem
}
[Serializable, NetSerializable]
public sealed partial class CableCuttingFinishedEvent : SimpleDoAfterEvent
{
}
public sealed partial class CableCuttingFinishedEvent : SimpleDoAfterEvent;
#endregion

View File

@@ -69,7 +69,7 @@ public sealed class WeldableSystem : EntitySystem
if (!CanWeld(uid, tool, user, component))
return false;
if (!_toolSystem.UseTool(tool, user, uid, component.WeldingTime.Seconds, component.WeldingQuality, new WeldFinishedEvent()))
if (!_toolSystem.UseTool(tool, user, uid, component.Time.Seconds, component.WeldingQuality, new WeldFinishedEvent(), component.Fuel))
return false;
// Log attempt
@@ -140,10 +140,10 @@ public sealed class WeldableSystem : EntitySystem
if (!_query.Resolve(uid, ref component))
return;
if (component.WeldingTime.Equals(time))
if (component.Time.Equals(time))
return;
component.WeldingTime = time;
component.Time = time;
Dirty(uid, component);
}
}