Unhardcode some logic related to objects with battery slots. Minor fix to fire helmets. (#11734)

This commit is contained in:
Mervill
2022-10-15 13:15:39 -07:00
committed by GitHub
parent 581a805063
commit c11c11bace
13 changed files with 86 additions and 101 deletions

View File

@@ -92,7 +92,10 @@ internal sealed class ChargerSystem : EntitySystem
if (!TryComp(args.EntityUid, out PowerCellSlotComponent? cellSlot)) if (!TryComp(args.EntityUid, out PowerCellSlotComponent? cellSlot))
return; return;
if (!cellSlot.FitsInCharger || !cellSlot.CellSlot.HasItem) if (!_itemSlotsSystem.TryGetSlotById(args.EntityUid, cellSlot.CellSlotId, out ItemSlot? itemSlot))
return;
if (!cellSlot.FitsInCharger || !itemSlot.HasItem)
args.Cancel(); args.Cancel();
} }
} }

View File

@@ -10,6 +10,7 @@ using Content.Shared.Rounding;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Content.Server.Kitchen.Components; using Content.Server.Kitchen.Components;
using Content.Shared.Containers.ItemSlots;
namespace Content.Server.PowerCell; namespace Content.Server.PowerCell;
@@ -17,8 +18,10 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
{ {
[Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!; [Dependency] private readonly SolutionContainerSystem _solutionsSystem = default!;
[Dependency] private readonly ExplosionSystem _explosionSystem = default!; [Dependency] private readonly ExplosionSystem _explosionSystem = default!;
[Dependency] private readonly IAdminLogManager _adminLogger= default!; [Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!; [Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _sharedAppearanceSystem = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -36,10 +39,13 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
private void OnSlotMicrowaved(EntityUid uid, PowerCellSlotComponent component, BeingMicrowavedEvent args) private void OnSlotMicrowaved(EntityUid uid, PowerCellSlotComponent component, BeingMicrowavedEvent args)
{ {
if (component.CellSlot.Item == null) if (_itemSlotsSystem.TryGetSlotById(uid, component.CellSlotId, out ItemSlot? slot))
return; {
if (slot.Item == null)
return;
RaiseLocalEvent(component.CellSlot.Item.Value, args, false); RaiseLocalEvent(slot.Item.Value, args, false);
}
} }
private void OnMicrowaved(EntityUid uid, BatteryComponent component, BeingMicrowavedEvent args) private void OnMicrowaved(EntityUid uid, BatteryComponent component, BeingMicrowavedEvent args)
@@ -69,14 +75,15 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
var frac = battery.CurrentCharge / battery.MaxCharge; var frac = battery.CurrentCharge / battery.MaxCharge;
var level = (byte) ContentHelpers.RoundToNearestLevels(frac, 1, PowerCellComponent.PowerCellVisualsLevels); var level = (byte) ContentHelpers.RoundToNearestLevels(frac, 1, PowerCellComponent.PowerCellVisualsLevels);
appearance.SetData(PowerCellVisuals.ChargeLevel, level); _sharedAppearanceSystem.SetData(uid, PowerCellVisuals.ChargeLevel, level, appearance);
// If this power cell is inside a cell-slot, inform that entity that the power has changed (for updating visuals n such). // If this power cell is inside a cell-slot, inform that entity that the power has changed (for updating visuals n such).
if (_containerSystem.TryGetContainingContainer(uid, out var container) if (_containerSystem.TryGetContainingContainer(uid, out var container)
&& TryComp(container.Owner, out PowerCellSlotComponent? slot) && TryComp(container.Owner, out PowerCellSlotComponent? slot)
&& slot.CellSlot.Item == uid) && _itemSlotsSystem.TryGetSlotById(container.Owner, slot.CellSlotId, out ItemSlot? itemSlot))
{ {
RaiseLocalEvent(container.Owner, new PowerCellChangedEvent(false), false); if (itemSlot.Item == uid)
RaiseLocalEvent(container.Owner, new PowerCellChangedEvent(false), false);
} }
} }
@@ -101,7 +108,13 @@ public sealed class PowerCellSystem : SharedPowerCellSystem
return false; return false;
} }
return TryComp(component.CellSlot.Item, out battery); if (_itemSlotsSystem.TryGetSlotById(uid, component.CellSlotId, out ItemSlot? slot))
{
return TryComp(slot.Item, out battery);
}
battery = null;
return false;
} }
private void OnSolutionChange(EntityUid uid, PowerCellComponent component, SolutionChangedEvent args) private void OnSolutionChange(EntityUid uid, PowerCellComponent component, SolutionChangedEvent args)

View File

@@ -125,6 +125,16 @@ namespace Content.Shared.Containers.ItemSlots
else else
Dirty(itemSlots); Dirty(itemSlots);
} }
public bool TryGetSlotById(EntityUid uid, string slotId, [NotNullWhen(true)] out ItemSlot? itemSlot, ItemSlotsComponent? component = null)
{
itemSlot = null;
if (!Resolve(uid, ref component))
return false;
return component.Slots.TryGetValue(slotId, out itemSlot);
}
#endregion #endregion
#region Interactions #region Interactions

View File

@@ -12,37 +12,8 @@ public sealed class PowerCellSlotComponent : Component
/// Given that <see cref="PowerCellSystem"/> needs to verify that a given cell has the correct cell-size before /// Given that <see cref="PowerCellSystem"/> needs to verify that a given cell has the correct cell-size before
/// inserting anyways, there is no need to specify a separate entity whitelist. In this slot's yaml definition. /// inserting anyways, there is no need to specify a separate entity whitelist. In this slot's yaml definition.
/// </remarks> /// </remarks>
[DataField("cellSlot")] [DataField("cellSlotId", required: true)]
public ItemSlot CellSlot = new(); public string CellSlotId = string.Empty;
/// <summary>
/// Name of the item-slot used to store cells. Determines the eject/insert verb text. E.g., "Eject > Power cell".
/// </summary>
/// <remarks>
/// This is simply used provide a default value for <see cref="CellSlot.Name"/>. If this string is empty or
/// whitespace, the verb will instead use the full name of any cell (e.g., "eject > small super-capacity power
/// cell").
/// </remarks>
[DataField("slotName")]
public readonly string SlotName = "power-cell-slot-component-slot-name-default"; // gets Loc.GetString()-ed by ItemSlotsSystem
/// <summary>
/// True if we don't want a cell inserted during map init. If a starting item is defined
/// in the <see cref="CellSlot"/> yaml definition, that always takes precedence.
/// </summary>
/// <remarks>
/// If false, the cell will start with a standard cell with a matching cell-size.
/// </remarks>
[DataField("startEmpty")]
public bool StartEmpty = false;
/// <summary>
/// Descriptive text to add to add when examining an entity with a cell slot. If empty or whitespace, will not add
/// any text.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("descFormatString")]
public string? DescFormatString { get; set; } = "power-cell-slot-component-description-default";
/// <summary> /// <summary>
/// Can this entity be inserted directly into a charging station? If false, you need to manually remove the power /// Can this entity be inserted directly into a charging station? If false, you need to manually remove the power
@@ -50,6 +21,7 @@ public sealed class PowerCellSlotComponent : Component
/// </summary> /// </summary>
[DataField("fitsInCharger")] [DataField("fitsInCharger")]
public bool FitsInCharger = true; public bool FitsInCharger = true;
} }
/// <summary> /// <summary>

View File

@@ -1,5 +1,3 @@
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Examine;
using Content.Shared.PowerCell.Components; using Content.Shared.PowerCell.Components;
using Robust.Shared.Containers; using Robust.Shared.Containers;
@@ -7,17 +5,9 @@ namespace Content.Shared.PowerCell;
public abstract class SharedPowerCellSystem : EntitySystem public abstract class SharedPowerCellSystem : EntitySystem
{ {
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
public const string CellSlotContainer = "cell_slot";
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<PowerCellSlotComponent, ComponentInit>(OnCellSlotInit);
SubscribeLocalEvent<PowerCellSlotComponent, ComponentRemove>(OnCellSlotRemove);
SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted); SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted);
SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved); SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved);
SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt); SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt);
@@ -28,7 +18,7 @@ public abstract class SharedPowerCellSystem : EntitySystem
if (!component.Initialized) if (!component.Initialized)
return; return;
if (args.Container.ID != component.CellSlot.ID) if (args.Container.ID != component.CellSlotId)
return; return;
if (!HasComp<PowerCellComponent>(args.EntityUid)) if (!HasComp<PowerCellComponent>(args.EntityUid))
@@ -42,7 +32,7 @@ public abstract class SharedPowerCellSystem : EntitySystem
if (!component.Initialized) if (!component.Initialized)
return; return;
if (args.Container.ID != component.CellSlot.ID) if (args.Container.ID != component.CellSlotId)
return; return;
RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false); RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false);
@@ -50,25 +40,9 @@ public abstract class SharedPowerCellSystem : EntitySystem
private void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args) private void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args)
{ {
if (args.Container.ID != component.CellSlot.ID) if (args.Container.ID != component.CellSlotId)
return; return;
RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false); RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false);
} }
private void OnCellSlotInit(EntityUid uid, PowerCellSlotComponent component, ComponentInit args)
{
_itemSlotsSystem.AddItemSlot(uid, CellSlotContainer, component.CellSlot);
if (string.IsNullOrWhiteSpace(component.CellSlot.Name) &&
!string.IsNullOrWhiteSpace(component.SlotName))
{
component.CellSlot.Name = component.SlotName;
}
}
private void OnCellSlotRemove(EntityUid uid, PowerCellSlotComponent component, ComponentRemove args)
{
_itemSlotsSystem.RemoveItemSlot(uid, component.CellSlot);
}
} }

View File

@@ -1,8 +1,2 @@
# Default examine descriptions
power-cell-slot-component-description-default = It uses {$size} power cells.
power-cell-slot-component-description-size-small = small
power-cell-slot-component-description-size-medium = medium-sized
power-cell-slot-component-description-size-large = large
# Verbs # Verbs
power-cell-slot-component-slot-name-default = Power cell power-cell-slot-component-slot-name-default = Power cell

View File

@@ -58,12 +58,16 @@
- type: FlashLightVisualizer - type: FlashLightVisualizer
- type: HandheldLight - type: HandheldLight
addPrefix: true addPrefix: true
- type: Battery - type: PowerCellSlot
maxCharge: 600 #lights drain 3/s but recharge of 2 makes this 1/s. Therefore 600 is 10 minutes of light. cellSlotId: cell_slot
startingCharge: 600 - type: ItemSlots
- type: BatterySelfRecharger slots:
autoRecharge: true cell_slot:
autoRechargeRate: 2 #recharge of 2 makes total drain 1w / s so max charge is 1:1 with time. Time to fully charge should be 5 minutes. Having recharge gives light an extended flicker period which gives you some warning to return to light area. name: power-cell-slot-component-slot-name-default
startingItem: PowerCellMedium
- type: ContainerContainer
containers:
cell_slot: !type:ContainerSlot {}
- type: entity - type: entity
abstract: true abstract: true

View File

@@ -35,14 +35,17 @@
head: head:
- state: on-equipped-HELMET - state: on-equipped-HELMET
- type: PowerCellSlot - type: PowerCellSlot
cellSlot: cellSlotId: cell_slot
startingItem: PowerCellMedium - type: ItemSlots
slots:
cell_slot:
name: power-cell-slot-component-slot-name-default
startingItem: PowerCellMedium
- type: Item - type: Item
heldPrefix: off heldPrefix: off
- type: ContainerContainer - type: ContainerContainer
containers: containers:
cell_slot: !type:ContainerSlot cell_slot: !type:ContainerSlot
- type: ItemSlots
- type: entity - type: entity
parent: ClothingHeadHatHardhatBase parent: ClothingHeadHatHardhatBase

View File

@@ -207,9 +207,6 @@
sprite: Clothing/Head/Helmets/firehelmet.rsi sprite: Clothing/Head/Helmets/firehelmet.rsi
quickEquip: true quickEquip: true
- type: IngestionBlocker - type: IngestionBlocker
- type: PowerCellSlot
cellSlot:
startingItem: PowerCellMedium
- type: TemperatureProtection - type: TemperatureProtection
coefficient: 0.01 coefficient: 0.01
- type: Armor - type: Armor
@@ -221,10 +218,10 @@
Heat: 0.65 Heat: 0.65
Radiation: 1 Radiation: 1
- type: IdentityBlocker - type: IdentityBlocker
- type: ItemSlots - type: Tag
- type: ContainerContainer tags:
containers: - HidesHair
cell_slot: !type:ContainerSlot {} - WhitelistChameleon
- type: entity - type: entity
parent: ClothingHeadLightBase parent: ClothingHeadLightBase

View File

@@ -8,13 +8,16 @@
- type: ItemCooldown - type: ItemCooldown
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
- type: ItemSlots
- type: ContainerContainer - type: ContainerContainer
containers: containers:
cell_slot: !type:ContainerSlot {} cell_slot: !type:ContainerSlot {}
- type: PowerCellSlot - type: PowerCellSlot
cellSlot: cellSlotId: cell_slot
startingItem: PowerCellMedium - type: ItemSlots
slots:
cell_slot:
name: power-cell-slot-component-slot-name-default
startingItem: PowerCellMedium
- type: Sprite - type: Sprite
sprite: Objects/Devices/Holoprojectors/custodial.rsi sprite: Objects/Devices/Holoprojectors/custodial.rsi
state: icon state: icon

View File

@@ -7,7 +7,11 @@
- type: HandheldLight - type: HandheldLight
addPrefix: true addPrefix: true
- type: PowerCellSlot - type: PowerCellSlot
cellSlotId: cell_slot
- type: ItemSlots - type: ItemSlots
slots:
cell_slot:
name: power-cell-slot-component-slot-name-default
- type: ContainerContainer - type: ContainerContainer
containers: containers:
cell_slot: !type:ContainerSlot cell_slot: !type:ContainerSlot

View File

@@ -26,12 +26,15 @@
- state: inhand-right-light - state: inhand-right-light
shader: unshaded shader: unshaded
- type: PowerCellSlot - type: PowerCellSlot
cellSlot: cellSlotId: cell_slot
startingItem: PowerCellMedium
- type: ContainerContainer - type: ContainerContainer
containers: containers:
cell_slot: !type:ContainerSlot cell_slot: !type:ContainerSlot
- type: ItemSlots - type: ItemSlots
slots:
cell_slot:
name: power-cell-slot-component-slot-name-default
startingItem: PowerCellMedium
- type: Sprite - type: Sprite
sprite: Objects/Tools/flashlight.rsi sprite: Objects/Tools/flashlight.rsi
netsync: false netsync: false
@@ -64,9 +67,11 @@
- type: Tag - type: Tag
tags: tags:
- SecBeltEquip - SecBeltEquip
- type: PowerCellSlot - type: ItemSlots
cellSlot: slots:
startingItem: PowerCellHigh cell_slot:
name: power-cell-slot-component-slot-name-default
startingItem: PowerCellHigh
- type: HandheldLight - type: HandheldLight
addPrefix: false addPrefix: false
- type: ToggleableLightVisuals - type: ToggleableLightVisuals

View File

@@ -28,9 +28,12 @@
- type: LanternVisualizer - type: LanternVisualizer
- type: ToggleableLightVisuals - type: ToggleableLightVisuals
- type: PowerCellSlot - type: PowerCellSlot
cellSlot: cellSlotId: cell_slot
startingItem: PowerCellMedium
- type: ItemSlots - type: ItemSlots
slots:
cell_slot:
name: power-cell-slot-component-slot-name-default
startingItem: PowerCellMedium
- type: ContainerContainer - type: ContainerContainer
containers: containers:
cell_slot: !type:ContainerSlot {} cell_slot: !type:ContainerSlot {}