mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-17 05:27:38 +03:00
# Description
Current implementation of `PowerCellSystem.Update()`:
```csharp
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<PowerCellDrawComponent, PowerCellSlotComponent>();
while (query.MoveNext(out var uid, out var comp, out var slot))
{
if (!comp.Enabled)
continue;
if (Timing.CurTime < comp.NextUpdateTime)
continue;
comp.NextUpdateTime += comp.Delay;
if (!TryGetBatteryFromSlot(uid, out var batteryEnt, out var battery, slot))
continue;
// TCJ: "Multiplying by frameTime to make this tick-invariant. Otherwise it'll draw 30x to 60x faster than you expect."
if (_battery.TryUseCharge(batteryEnt.Value, comp.DrawRate * frameTime, battery))
continue;
var ev = new PowerCellSlotEmptyEvent();
RaiseLocalEvent(uid, ref ev);
}
}
```
Multiplying `comp.DrawRate` and `frameTime` is only valid if we're
running this code each tick. Right now all power costs are divided by 30
or 60, which, ironically enough, makes power consumption 30 to 60 times
slower than intended.
This does not affect PowerCellDrawComponents with `Delay` set to zero,
since they do get updated every tick.
This will negatively affect equipment that uses this component for
consuming power while on.
Except flashlights, because `HandheldLightSystem` handles flashlight
power draw by itself.
---
<details><summary><h1>Media</h1></summary>
<p>
<details><summary>without fix</summary>

</details>
<details><summary>with fix</summary>

</details>
</p>
</details>
---
# Changelog
<!--
You can add an author after the `🆑` to change the name that appears
in the changelog (ex: `🆑 Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->
🆑
- fix: Fixed some items used less power while turned on than they
should.
(cherry picked from commit d8d9141e38c1300e20c50cec322dc287500a8011)
81 lines
2.5 KiB
C#
81 lines
2.5 KiB
C#
using Content.Server.Power.Components;
|
|
using Content.Shared.PowerCell;
|
|
using Content.Shared.PowerCell.Components;
|
|
|
|
namespace Content.Server.PowerCell;
|
|
|
|
public sealed partial class PowerCellSystem
|
|
{
|
|
/*
|
|
* Handles PowerCellDraw
|
|
*/
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
var query = EntityQueryEnumerator<PowerCellDrawComponent, PowerCellSlotComponent>();
|
|
|
|
while (query.MoveNext(out var uid, out var comp, out var slot))
|
|
{
|
|
if (!comp.Enabled)
|
|
continue;
|
|
|
|
// Any delay between zero and 1/tickrate will be equivalent to 1/tickrate delay.
|
|
// Setting delay to zero makes the draw "continuous"
|
|
float drawRate = comp.DrawRate;
|
|
if (comp.Delay == TimeSpan.Zero)
|
|
drawRate *= frameTime;
|
|
else
|
|
{
|
|
if (Timing.CurTime < comp.NextUpdateTime)
|
|
continue;
|
|
}
|
|
|
|
comp.NextUpdateTime += comp.Delay;
|
|
|
|
if (!TryGetBatteryFromSlot(uid, out var batteryEnt, out var battery, slot))
|
|
continue;
|
|
|
|
if (_battery.TryUseCharge(batteryEnt.Value, drawRate, battery))
|
|
continue;
|
|
|
|
var ev = new PowerCellSlotEmptyEvent();
|
|
RaiseLocalEvent(uid, ref ev);
|
|
}
|
|
}
|
|
|
|
private void OnDrawChargeChanged(EntityUid uid, PowerCellDrawComponent component, ref ChargeChangedEvent args)
|
|
{
|
|
// Update the bools for client prediction.
|
|
var canUse = component.UseRate <= 0f || args.Charge > component.UseRate;
|
|
|
|
var canDraw = component.DrawRate <= 0f || args.Charge > 0f;
|
|
|
|
if (canUse != component.CanUse || canDraw != component.CanDraw)
|
|
{
|
|
component.CanDraw = canDraw;
|
|
component.CanUse = canUse;
|
|
Dirty(uid, component);
|
|
}
|
|
}
|
|
|
|
private void OnDrawCellChanged(EntityUid uid, PowerCellDrawComponent component, PowerCellChangedEvent args)
|
|
{
|
|
var canDraw = !args.Ejected && HasCharge(uid, float.MinValue);
|
|
var canUse = !args.Ejected && HasActivatableCharge(uid, component);
|
|
|
|
if (!canDraw)
|
|
{
|
|
var ev = new PowerCellSlotEmptyEvent();
|
|
RaiseLocalEvent(uid, ref ev);
|
|
}
|
|
|
|
if (canUse != component.CanUse || canDraw != component.CanDraw)
|
|
{
|
|
component.CanDraw = canDraw;
|
|
component.CanUse = canUse;
|
|
Dirty(uid, component);
|
|
}
|
|
}
|
|
}
|