Power-reading multitool (#5046)

* AMEPartComponent: Switch to ToolSystem.HasQuality because this bugs me.

* Multitool can read power when you examine a cable with the multitool held.

* Multitool power reading: Condense storage information

* power-sensing multitool: Fix ToolSystem ref.

* Power-reading multitools: Fix misuse of GetComponent
This commit is contained in:
20kdc
2021-10-29 13:42:18 +01:00
committed by GitHub
parent b2aca94586
commit 44f5f15790
6 changed files with 212 additions and 4 deletions

View File

@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.NodeContainer;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.Power.Components;
using Content.Server.Power.Nodes;
using Content.Server.Power.Pow3r;
using Content.Server.Power.NodeGroups;
using Content.Server.Power.EntitySystems;
using Content.Server.Hands.Components;
using Content.Server.Tools;
using Content.Shared.Wires;
using Content.Shared.Examine;
using Content.Shared.Hands.Components;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Localization;
namespace Content.Server.Power.EntitySystems
{
[UsedImplicitly]
public sealed class CableMultitoolSystem : EntitySystem
{
[Dependency] private readonly ToolSystem _toolSystem = default!;
[Dependency] private readonly PowerNetSystem _pnSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CableComponent, ExaminedEvent>(OnExamined);
}
private void OnExamined(EntityUid uid, CableComponent component, ExaminedEvent args)
{
// Must be in details range to try this.
// Theoretically there should be a separate range at which a multitool works, but this does just fine.
if (args.IsInDetailsRange)
{
// Determine if they are holding a multitool.
if (args.Examiner.TryGetComponent<HandsComponent>(out var hands) && hands.TryGetActiveHand(out var hand))
{
var held = hand.HeldEntity;
// Pulsing is hardcoded here because I don't think it needs to be more complex than that right now.
// Update if I'm wrong.
if ((held != null) && _toolSystem.HasQuality(held.Uid, "Pulsing"))
{
args.PushMarkup(GenerateCableMarkup(uid));
// args.PushFancyUpdatingPowerGraphs(uid);
}
}
}
}
private string GenerateCableMarkup(EntityUid uid, NodeContainerComponent? nodeContainer = null)
{
if (!Resolve(uid, ref nodeContainer))
return Loc.GetString("cable-multitool-system-internal-error-missing-component");
foreach (var node in nodeContainer.Nodes)
{
if (!(node.Value.NodeGroup is IBasePowerNet))
continue;
var p = (IBasePowerNet) node.Value.NodeGroup;
var ps = _pnSystem.GetNetworkStatistics(p.NetworkNode);
float storageRatio = ps.InStorageCurrent / Math.Max(ps.InStorageMax, 1.0f);
float outStorageRatio = ps.OutStorageCurrent / Math.Max(ps.OutStorageMax, 1.0f);
return Loc.GetString("cable-multitool-system-statistics",
("supplyc", ps.SupplyCurrent),
("supplyb", ps.SupplyBatteries),
("supplym", ps.SupplyTheoretical),
("consumption", ps.Consumption),
("storagec", ps.InStorageCurrent),
("storager", storageRatio),
("storagem", ps.InStorageMax),
("storageoc", ps.OutStorageCurrent),
("storageor", outStorageRatio),
("storageom", ps.OutStorageMax)
);
}
return Loc.GetString("cable-multitool-system-internal-error-no-power-node");
}
}
}

View File

@@ -1,3 +1,4 @@
using System.Linq;
using System.Collections.Generic;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.Power.Components;
@@ -148,6 +149,60 @@ namespace Content.Server.Power.EntitySystems
};
}
public NetworkPowerStatistics GetNetworkStatistics(PowerState.Network network)
{
// Right, consumption. Now this is a big mess.
// Start by summing up consumer draw rates.
// Then deal with batteries.
// While for consumers we want to use their max draw rates,
// for batteries we ought to use their current draw rates,
// because there's all sorts of weirdness with them.
// A full battery will still have the same max draw rate,
// but will likely have deliberately limited current draw rate.
float consumptionW = network.Loads.Sum(s => _powerState.Loads[s].DesiredPower);
consumptionW += network.BatteriesCharging.Sum(s => _powerState.Batteries[s].CurrentReceiving);
// This is interesting because LastMaxSupplySum seems to match LastAvailableSupplySum for some reason.
// I suspect it's accounting for current supply rather than theoretical supply.
float maxSupplyW = network.Supplies.Sum(s => _powerState.Supplies[s].MaxSupply);
// Battery stuff is more complex.
// Without stealing PowerState, the most efficient way
// to grab the necessary discharge data is from
// PowerNetworkBatteryComponent (has Pow3r reference).
float supplyBatteriesW = 0.0f;
float storageCurrentJ = 0.0f;
float storageMaxJ = 0.0f;
foreach (var discharger in network.BatteriesDischarging)
{
var nb = _powerState.Batteries[discharger];
supplyBatteriesW += nb.CurrentSupply;
storageCurrentJ += nb.CurrentStorage;
storageMaxJ += nb.Capacity;
maxSupplyW += nb.MaxSupply;
}
// And charging
float outStorageCurrentJ = 0.0f;
float outStorageMaxJ = 0.0f;
foreach (var charger in network.BatteriesCharging)
{
var nb = _powerState.Batteries[charger];
outStorageCurrentJ += nb.CurrentStorage;
outStorageMaxJ += nb.Capacity;
}
return new()
{
SupplyCurrent = network.LastMaxSupplySum,
SupplyBatteries = supplyBatteriesW,
SupplyTheoretical = maxSupplyW,
Consumption = consumptionW,
InStorageCurrent = storageCurrentJ,
InStorageMax = storageMaxJ,
OutStorageCurrent = outStorageCurrentJ,
OutStorageMax = outStorageMaxJ
};
}
public override void Update(float frameTime)
{
base.Update(frameTime);
@@ -376,4 +431,17 @@ namespace Content.Server.Power.EntitySystems
public int CountSupplies;
public int CountBatteries;
}
public struct NetworkPowerStatistics
{
public float SupplyCurrent;
public float SupplyBatteries;
public float SupplyTheoretical;
public float Consumption;
public float InStorageCurrent;
public float InStorageMax;
public float OutStorageCurrent;
public float OutStorageMax;
}
}