Skip to content

Commit

Permalink
Cleaned up tons of stuff.
Browse files Browse the repository at this point in the history
Battery draining seems to be broken right now, reporting 0 charge at all times. Will fix soon.
  • Loading branch information
Pspritechologist committed Jun 23, 2023
1 parent e4cd838 commit 3420c0e
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Content.Server.SimpleStation14.Power.Components;

[RegisterComponent]
public class RandomBatteryChargeComponent : Component
{
/// <summary>
/// The minimum and maximum max charge the battery can have.
/// </summary>
[DataField("batteryMaxMinMax")]
public Vector2 BatteryMaxMinMax = (0.85f, 1.15f);

/// <summary>
/// The minimum and maximum current charge the battery can have.
/// </summary>
[DataField("batteryChargeMinMax")]
public Vector2 BatteryChargeMinMax = (1f, 1f);

/// <summary>
/// True if the current charge is based on the preexisting current charge, or false if it's based on the max charge.
/// </summary>
[DataField("basedOnMaxCharge")]
public bool BasedOnMaxCharge = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public sealed class BatteryDrinkerSystem : EntitySystem
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly SiliconChargeSystem _silicon = default!;

public override void Initialize()
{
Expand Down Expand Up @@ -52,7 +53,6 @@ private void AddAltVerb(EntityUid uid, BatteryComponent batteryComponent, GetVer

private bool TestDrinkableBattery(EntityUid target, BatteryDrinkerComponent drinkerComp)
{

if (!drinkerComp.DrinkAll && !HasComp<BatteryDrinkerSourceComponent>(target))
return false;

Expand All @@ -61,13 +61,16 @@ private bool TestDrinkableBattery(EntityUid target, BatteryDrinkerComponent drin

private bool TryGetFillableBattery(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? battery)
{
if (EntityManager.TryGetComponent<BatteryComponent>(uid, out battery))
if (_silicon.TryGetSiliconBattery(uid, out battery))
return true;

if (EntityManager.TryGetComponent(uid, out battery))
return true;

if (EntityManager.TryGetComponent<PowerCellSlotComponent>(uid, out var powerCellSlot) &&
_slots.TryGetSlot(uid, powerCellSlot.CellSlotId, out var slot) &&
slot.Item != null &&
EntityManager.TryGetComponent<BatteryComponent>(slot.Item.Value, out battery))
EntityManager.TryGetComponent(slot.Item.Value, out battery))
return true;

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.SimpleStation14.Power.Components;
using Robust.Shared.Random;
using Robust.Shared.Utility;

namespace Content.Server.SimpleStation14.Power.Systems;

public sealed class RandomBatteryFillSystem : EntitySystem
{
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly IRobustRandom _random = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<RandomBatteryChargeComponent, ComponentInit>(OnBatteryInit);
}

private void OnBatteryInit(EntityUid uid, RandomBatteryChargeComponent component, ComponentInit args)
{
var batteryComp = Comp<BatteryComponent>(uid);
DebugTools.AssertNotNull(batteryComp);

if (batteryComp == null)
return;

var (minMaxMod, maxMaxMod) = component.BatteryMaxMinMax;
var (minChargeMod, maxChargeMod) = component.BatteryChargeMinMax;

var newMax = batteryComp.MaxCharge * _random.NextFloat(minMaxMod, maxMaxMod);
float newCharge;

if (component.BasedOnMaxCharge)
newCharge = newMax * _random.NextFloat(minChargeMod, maxChargeMod);
else
newCharge = batteryComp.CurrentCharge * _random.NextFloat(minChargeMod, maxChargeMod);

_battery.SetMaxCharge(uid, newMax);
_battery.SetCharge(uid, newCharge);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using Content.Server.Power.Components;
using Content.Shared.SimpleStation14.Silicon.Components;
using Content.Shared.SimpleStation14.Silicon.Systems;
using Robust.Shared.Utility;
using Content.Server.Bed.Sleep;
using Content.Shared.Bed.Sleep;
using Content.Server.Sound.Components;
using Content.Server.SimpleStation14.Silicon.Charge;
using Serilog;

namespace Content.Server.SimpleStation14.Silicon.Death;

public sealed class SiliconDeathSystem : EntitySystem
{
[Dependency] private readonly SleepingSystem _sleepSystem = default!;
[Dependency] private readonly SleepingSystem _sleep = default!;
[Dependency] private readonly SiliconChargeSystem _silicon = default!;

public override void Initialize()
{
Expand All @@ -21,14 +22,10 @@ public override void Initialize()

private void OnSiliconChargeStateUpdate(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, SiliconChargeStateUpdateEvent args)
{
EntityManager.TryGetComponent<BatteryComponent>(uid, out var batteryComp);
EntityManager.TryGetComponent<SiliconComponent>(uid, out var siliconComp);
_silicon.TryGetSiliconBattery(uid, out var batteryComp);

DebugTools.AssertNotNull(batteryComp);
DebugTools.AssertNotNull(siliconComp);

if (batteryComp == null || siliconComp == null)
return;
Logger.Debug($"Silicon charge state update: {args.ChargeState}");
Logger.Debug($"Silicon battery: {batteryComp?.CurrentCharge}");

if (args.ChargeState == ChargeState.Dead && !siliconDeadComp.Dead)
{
Expand All @@ -40,7 +37,7 @@ private void OnSiliconChargeStateUpdate(EntityUid uid, SiliconDownOnDeadComponen
}
}

private void SiliconDead(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, BatteryComponent batteryComp)
private void SiliconDead(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, BatteryComponent? batteryComp)
{
var deadEvent = new SiliconChargeDyingEvent(uid, batteryComp);
RaiseLocalEvent(uid, deadEvent);
Expand All @@ -57,9 +54,9 @@ private void SiliconDead(EntityUid uid, SiliconDownOnDeadComponent siliconDeadCo
RaiseLocalEvent(uid, new SiliconChargeDeathEvent(uid, batteryComp));
}

private void SiliconUnDead(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, BatteryComponent batteryComp)
private void SiliconUnDead(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, BatteryComponent? batteryComp)
{
_sleepSystem.TryWaking(uid, null, true);
_sleep.TryWaking(uid, null, true);

siliconDeadComp.Dead = false;

Expand All @@ -73,9 +70,9 @@ private void SiliconUnDead(EntityUid uid, SiliconDownOnDeadComponent siliconDead
public sealed class SiliconChargeDyingEvent : CancellableEntityEventArgs
{
public EntityUid SiliconUid { get; }
public BatteryComponent BatteryComp { get; }
public BatteryComponent? BatteryComp { get; }

public SiliconChargeDyingEvent(EntityUid siliconUid, BatteryComponent batteryComp)
public SiliconChargeDyingEvent(EntityUid siliconUid, BatteryComponent? batteryComp)
{
SiliconUid = siliconUid;
BatteryComp = batteryComp;
Expand All @@ -88,9 +85,9 @@ public SiliconChargeDyingEvent(EntityUid siliconUid, BatteryComponent batteryCom
public sealed class SiliconChargeDeathEvent : EntityEventArgs
{
public EntityUid SiliconUid { get; }
public BatteryComponent BatteryComp { get; }
public BatteryComponent? BatteryComp { get; }

public SiliconChargeDeathEvent(EntityUid siliconUid, BatteryComponent batteryComp)
public SiliconChargeDeathEvent(EntityUid siliconUid, BatteryComponent? batteryComp)
{
SiliconUid = siliconUid;
BatteryComp = batteryComp;
Expand All @@ -103,9 +100,9 @@ public SiliconChargeDeathEvent(EntityUid siliconUid, BatteryComponent batteryCom
public sealed class SiliconChargeAliveEvent : EntityEventArgs
{
public EntityUid SiliconUid { get; }
public BatteryComponent BatteryComp { get; }
public BatteryComponent? BatteryComp { get; }

public SiliconChargeAliveEvent(EntityUid siliconUid, BatteryComponent batteryComp)
public SiliconChargeAliveEvent(EntityUid siliconUid, BatteryComponent? batteryComp)
{
SiliconUid = siliconUid;
BatteryComp = batteryComp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,70 @@
using Content.Shared.Movement.Systems;
using Content.Server.Body.Components;
using Content.Server.Power.EntitySystems;
using Robust.Shared.Containers;
using System.Diagnostics.CodeAnalysis;

namespace Content.Server.SimpleStation14.Silicon.Charge;

public sealed class SiliconChargeSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly FlammableSystem _flammableSystem = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly FlammableSystem _flammable = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifierSystem = default!;
[Dependency] private readonly MovementSpeedModifierSystem _moveMod = default!;
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;

public override void Initialize()
{
base.Initialize();

// Subscribe for init on entities with both SiliconComponent and BatteryComponent
SubscribeLocalEvent<BatteryComponent, ComponentInit>(OnBatteryInit);
SubscribeLocalEvent<SiliconComponent, ComponentStartup>(OnSiliconStartup);
}


private void OnBatteryInit(EntityUid uid, BatteryComponent component, ComponentInit args)
public bool TryGetSiliconBattery(EntityUid uid, [NotNullWhen(true)] out BatteryComponent? batteryComp)
{
if (!EntityManager.TryGetComponent<SiliconComponent>(uid, out var siliconComp) ||
!siliconComp.BatteryPowered)
return;
batteryComp = null;

if (!EntityManager.TryGetComponent(uid, out SiliconComponent? siliconComp))
return false;

if (siliconComp.BatteryContainer != null &&
siliconComp.BatteryContainer.ContainedEntities.Count > 0 &&
TryComp(siliconComp.BatteryContainer.ContainedEntities[0], out batteryComp))
{
return true;
}

_battery.SetMaxCharge(uid, component.MaxCharge * _random.NextFloat(0.85f, 1.15f));
if (TryComp(uid, out batteryComp))
return true;

var batteryLevelFill = component.MaxCharge;
return false;
}

if (siliconComp.StartCharged.Equals(StartChargedData.Randomized))
batteryLevelFill *= _random.NextFloat(0.40f, 0.80f);
private void OnSiliconStartup(EntityUid uid, SiliconComponent component, ComponentStartup args)
{
if (component.BatterySlot == null)
return;

_battery.SetCharge(uid, batteryLevelFill);
var container = _container.EnsureContainer<Container>(uid, component.BatterySlot);
component.BatteryContainer = container;
}

public override void Update(float frameTime)
{
base.Update(frameTime);

// For each siliconComp entity with a battery component, drain their charge.
var query = EntityQueryEnumerator<SiliconComponent, BatteryComponent>();
while (query.MoveNext(out var silicon, out var siliconComp, out var batteryComp))
var query = EntityQueryEnumerator<SiliconComponent>();
while (query.MoveNext(out var silicon, out var siliconComp))
{
if (!siliconComp.BatteryPowered || !TryGetSiliconBattery(silicon, out var batteryComp))
continue;

// If the silicon is dead, skip it.
if (_mobStateSystem.IsDead(silicon))
if (_mobState.IsDead(silicon))
continue;

var drainRate = 10 * siliconComp.DrainRateMulti;
Expand All @@ -84,10 +101,10 @@ public override void Update(float frameTime)

var currentState = chargePercent switch
{
var x when x > siliconComp.ChargeStateThresholdMid => ChargeState.Full,
var x when x > siliconComp.ChargeStateThresholdLow => ChargeState.Mid,
var x when x > siliconComp.ChargeStateThresholdCritical => ChargeState.Low,
var x when x > 0 || siliconComp.ChargeStateThresholdCritical == 0 => ChargeState.Critical,
var x when x > siliconComp.ChargeThresholdMid => ChargeState.Full,
var x when x > siliconComp.ChargeThresholdLow => ChargeState.Mid,
var x when x > siliconComp.ChargeThresholdCritical => ChargeState.Low,
var x when x > 0 || siliconComp.ChargeThresholdCritical == 0 => ChargeState.Critical,
_ => ChargeState.Dead,
};

Expand All @@ -98,7 +115,9 @@ public override void Update(float frameTime)

RaiseLocalEvent(silicon, new SiliconChargeStateUpdateEvent(currentState));

_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(silicon);
Logger.DebugS("silicon", $"Silicon {silicon} charge state updated to {currentState}.");

_moveMod.RefreshMovementSpeedModifiers(silicon);
}
}
}
Expand Down Expand Up @@ -135,7 +154,7 @@ private float SiliconHeatEffects(EntityUid silicon, float frameTime)
!flamComp.OnFire &&
_random.Prob(Math.Clamp(temperComp.CurrentTemperature / (upperThresh * 5), 0.001f, 0.9f)))
{
_flammableSystem.Ignite(silicon, flamComp);
_flammable.Ignite(silicon, flamComp);
}
else if ((flamComp == null || !flamComp.OnFire) &&
_random.Prob(Math.Clamp(temperComp.CurrentTemperature / upperThresh, 0.001f, 0.75f)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public sealed class SiliconChargerSystem : EntitySystem
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SiliconChargeSystem _silicon = default!;

public override void Initialize()
{
Expand Down Expand Up @@ -153,8 +154,8 @@ private void HandleChargingEntity(EntityUid entity, float chargeRate, SiliconCha

foreach (var entityToCharge in entitiesToCharge.ToList())
{
if (TryComp<BatteryComponent>(entityToCharge, out _))
ChargeBattery(entityToCharge, EntityManager.GetComponent<BatteryComponent>(entityToCharge), chargeRate, chargerComp, chargerUid);
if (_silicon.TryGetSiliconBattery(entityToCharge, out var batteryComp))
ChargeBattery(entityToCharge, batteryComp, chargeRate, chargerComp, chargerUid);
else if (TryComp<DamageableComponent>(entityToCharge, out var damageComp))
BurnEntity(entityToCharge, damageComp, frameTime, chargerComp, chargerUid);
}
Expand All @@ -164,22 +165,28 @@ private List<EntityUid> SearchThroughEntities(EntityUid entity, bool burn = true
{
var entitiesToCharge = new List<EntityUid>();

if (_silicon.TryGetSiliconBattery(entity, out var siliconBatteryComp))
{
if (siliconBatteryComp.CurrentCharge < siliconBatteryComp.MaxCharge)
entitiesToCharge.Add(entity);
}

// If the given entity has a battery, charge it.
if (!HasComp<UnremoveableComponent>(entity) && // Should probably be charge by the entity holding it, but also might be too small to be safe.
TryComp<BatteryComponent>(entity, out var batteryComp) &&
batteryComp.CurrentCharge < batteryComp.MaxCharge)
else if (!HasComp<UnremoveableComponent>(entity) && // Should probably be charged by the entity holding it. Might be too small to be safe.
TryComp<BatteryComponent>(entity, out var batteryComp))
{
entitiesToCharge.Add(entity);
if (batteryComp.CurrentCharge < batteryComp.MaxCharge)
entitiesToCharge.Add(entity);
}

// If the given entity contains a battery, charge it.
else if (!HasComp<UnremoveableComponent>(entity) && // Should probably be charge by the entity holding it, but also might be too small to be safe.
else if (!HasComp<UnremoveableComponent>(entity) && // Should probably be charged by the entity holding it. Might be too small to be safe.
TryComp<PowerCellSlotComponent>(entity, out var cellSlotComp) &&
_itemSlots.TryGetSlot(entity, cellSlotComp.CellSlotId, out var slot) &&
TryComp<BatteryComponent>(slot.Item, out var cellBattComp) &&
cellBattComp.CurrentCharge < cellBattComp.MaxCharge)
TryComp<BatteryComponent>(slot.Item, out var cellBattComp))
{
entitiesToCharge.Add(slot.Item.Value);
if (cellBattComp.CurrentCharge < cellBattComp.MaxCharge)
entitiesToCharge.Add(slot.Item.Value);
}

// If the given entity is fleshy, burn the fucker.
Expand Down
Loading

0 comments on commit 3420c0e

Please sign in to comment.