Radiation rework (#10970)

This commit is contained in:
Alex Evgrashin
2022-10-11 05:09:10 +02:00
committed by GitHub
parent 667fc1970d
commit 7d882f22c9
34 changed files with 1010 additions and 46 deletions

View File

@@ -0,0 +1,166 @@
using Content.Server.Radiation.Components;
using Content.Shared.Doors;
using Content.Shared.Doors.Components;
namespace Content.Server.Radiation.Systems;
// create and update map of radiation blockers
public partial class RadiationSystem
{
private void InitRadBlocking()
{
SubscribeLocalEvent<RadiationBlockerComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<RadiationBlockerComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<RadiationBlockerComponent, AnchorStateChangedEvent>(OnAnchorChanged);
SubscribeLocalEvent<RadiationBlockerComponent, ReAnchorEvent>(OnReAnchor);
SubscribeLocalEvent<RadiationBlockerComponent, DoorStateChangedEvent>(OnDoorChanged);
SubscribeLocalEvent<RadiationGridResistanceComponent, EntityTerminatingEvent>(OnGridRemoved);
}
private void OnInit(EntityUid uid, RadiationBlockerComponent component, ComponentInit args)
{
if (!component.Enabled)
return;
AddTile(uid, component);
}
private void OnShutdown(EntityUid uid, RadiationBlockerComponent component, ComponentShutdown args)
{
if (component.Enabled)
return;
RemoveTile(uid, component);
}
private void OnAnchorChanged(EntityUid uid, RadiationBlockerComponent component, ref AnchorStateChangedEvent args)
{
if (args.Anchored)
{
AddTile(uid, component);
}
else
{
RemoveTile(uid, component);
}
}
private void OnReAnchor(EntityUid uid, RadiationBlockerComponent component, ref ReAnchorEvent args)
{
// probably grid was split
// we need to remove entity from old resistance map
RemoveTile(uid, component);
// and move it to the new one
AddTile(uid, component);
}
private void OnDoorChanged(EntityUid uid, RadiationBlockerComponent component, DoorStateChangedEvent args)
{
switch (args.State)
{
case DoorState.Open:
SetEnabled(uid, false, component);
break;
case DoorState.Closed:
SetEnabled(uid, true, component);
break;
}
}
private void OnGridRemoved(EntityUid uid, RadiationGridResistanceComponent component, ref EntityTerminatingEvent args)
{
// grid is about to be removed - lets delete grid component first
// this should save a bit performance when blockers will be deleted
RemComp(uid, component);
}
public void SetEnabled(EntityUid uid, bool isEnabled, RadiationBlockerComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (isEnabled == component.Enabled)
return;
component.Enabled = isEnabled;
if (!component.Enabled)
RemoveTile(uid, component);
else
AddTile(uid, component);
}
private void AddTile(EntityUid uid, RadiationBlockerComponent component)
{
// check that last position was removed
if (component.CurrentPosition != null)
{
RemoveTile(uid, component);
}
// check if entity even provide some rad protection
if (!component.Enabled || component.RadResistance <= 0)
return;
// check if it's on a grid
var trs = Transform(uid);
if (!trs.Anchored || !TryComp(trs.GridUid, out IMapGridComponent? grid))
return;
// save resistance into rad protection grid
var gridId = trs.GridUid.Value;
var tilePos = grid.Grid.TileIndicesFor(trs.Coordinates);
AddToTile(gridId, tilePos, component.RadResistance);
// and remember it as last valid position
component.CurrentPosition = (gridId, tilePos);
}
private void RemoveTile(EntityUid uid, RadiationBlockerComponent component)
{
// check if blocker was placed on grid before component was removed
if (component.CurrentPosition == null)
return;
var (gridId, tilePos) = component.CurrentPosition.Value;
// try to remove
RemoveFromTile(gridId, tilePos, component.RadResistance);
component.CurrentPosition = null;
}
private void AddToTile(EntityUid gridUid, Vector2i tilePos, float radResistance)
{
// get existing rad resistance grid or create it if it doesn't exist
var resistance = EnsureComp<RadiationGridResistanceComponent>(gridUid);
var grid = resistance.ResistancePerTile;
// add to existing cell more rad resistance
var newResistance = radResistance;
if (grid.TryGetValue(tilePos, out var existingResistance))
{
newResistance += existingResistance;
}
grid[tilePos] = newResistance;
}
private void RemoveFromTile(EntityUid gridUid, Vector2i tilePos, float radResistance)
{
// get grid
if (!TryComp(gridUid, out RadiationGridResistanceComponent? resistance))
return;
var grid = resistance.ResistancePerTile;
// subtract resistance from tile
if (!grid.TryGetValue(tilePos, out var existingResistance))
return;
existingResistance -= radResistance;
// remove tile from grid if no resistance left
if (existingResistance > 0)
grid[tilePos] = existingResistance;
else
{
grid.Remove(tilePos);
if (grid.Count == 0)
RemComp(gridUid, resistance);
}
}
}

View File

@@ -0,0 +1,48 @@
using Content.Shared.CCVar;
namespace Content.Server.Radiation.Systems;
// cvar updates
public partial class RadiationSystem
{
public float MinIntensity { get; private set; }
public float GridcastUpdateRate { get; private set; }
public bool GridcastSimplifiedSameGrid { get; private set; }
public float GridcastMaxDistance { get; private set; }
private void SubscribeCvars()
{
_cfg.OnValueChanged(CCVars.RadiationMinIntensity, SetMinRadiationIntensity, true);
_cfg.OnValueChanged(CCVars.RadiationGridcastUpdateRate, SetGridcastUpdateRate, true);
_cfg.OnValueChanged(CCVars.RadiationGridcastSimplifiedSameGrid, SetGridcastSimplifiedSameGrid, true);
_cfg.OnValueChanged(CCVars.RadiationGridcastMaxDistance, SetGridcastMaxDistance, true);
}
private void UnsubscribeCvars()
{
_cfg.UnsubValueChanged(CCVars.RadiationMinIntensity, SetMinRadiationIntensity);
_cfg.UnsubValueChanged(CCVars.RadiationGridcastUpdateRate, SetGridcastUpdateRate);
_cfg.UnsubValueChanged(CCVars.RadiationGridcastSimplifiedSameGrid, SetGridcastSimplifiedSameGrid);
_cfg.UnsubValueChanged(CCVars.RadiationGridcastMaxDistance, SetGridcastMaxDistance);
}
private void SetMinRadiationIntensity(float radiationMinIntensity)
{
MinIntensity = radiationMinIntensity;
}
private void SetGridcastUpdateRate(float updateRate)
{
GridcastUpdateRate = updateRate;
}
private void SetGridcastSimplifiedSameGrid(bool simplifiedSameGrid)
{
GridcastSimplifiedSameGrid = simplifiedSameGrid;
}
private void SetGridcastMaxDistance(float maxDistance)
{
GridcastMaxDistance = maxDistance;
}
}

View File

@@ -0,0 +1,105 @@
using System.Linq;
using Content.Server.Administration;
using Content.Server.Radiation.Components;
using Content.Shared.Administration;
using Content.Shared.Radiation.Events;
using Content.Shared.Radiation.Systems;
using Robust.Shared.Console;
using Robust.Shared.Enums;
using Robust.Shared.Players;
namespace Content.Server.Radiation.Systems;
// radiation overlay debug logic
// rad rays send only to clients that enabled debug overlay
public partial class RadiationSystem
{
private readonly HashSet<ICommonSession> _debugSessions = new();
/// <summary>
/// Toggle radiation debug overlay for selected player.
/// </summary>
public void ToggleDebugView(ICommonSession session)
{
bool isEnabled;
if (_debugSessions.Add(session))
{
isEnabled = true;
}
else
{
_debugSessions.Remove(session);
isEnabled = false;
}
var ev = new OnRadiationOverlayToggledEvent(isEnabled);
RaiseNetworkEvent(ev, session.ConnectedClient);
}
/// <summary>
/// Send new information for radiation overlay.
/// </summary>
private void UpdateDebugOverlay(EntityEventArgs ev)
{
var sessions = _debugSessions.ToArray();
foreach (var session in sessions)
{
if (session.Status != SessionStatus.InGame)
_debugSessions.Remove(session);
RaiseNetworkEvent(ev, session.ConnectedClient);
}
}
private void UpdateResistanceDebugOverlay()
{
if (_debugSessions.Count == 0)
return;
var query = GetEntityQuery<RadiationGridResistanceComponent>();
var dict = new Dictionary<EntityUid, Dictionary<Vector2i, float>>();
foreach (var grid in _mapManager.GetAllGrids())
{
var gridUid = grid.GridEntityId;
if (!query.TryGetComponent(gridUid, out var resistance))
continue;
var resMap = resistance.ResistancePerTile;
dict.Add(gridUid, resMap);
}
var ev = new OnRadiationOverlayResistanceUpdateEvent(dict);
UpdateDebugOverlay(ev);
}
private void UpdateGridcastDebugOverlay(double elapsedTime, int totalSources,
int totalReceivers, List<RadiationRay> rays)
{
if (_debugSessions.Count == 0)
return;
var ev = new OnRadiationOverlayUpdateEvent(elapsedTime, totalSources, totalReceivers, rays);
UpdateDebugOverlay(ev);
}
}
/// <summary>
/// Toggle visibility of radiation rays coming from rad sources.
/// </summary>
[AdminCommand(AdminFlags.Admin)]
public sealed class RadiationViewCommand : IConsoleCommand
{
public string Command => "showradiation";
public string Description => Loc.GetString("radiation-command-description");
public string Help => Loc.GetString("radiation-command-help");
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var session = shell.Player;
if (session == null)
return;
var entityManager = IoCManager.Resolve<IEntityManager>();
entityManager.System<RadiationSystem>().ToggleDebugView(session);
}
}

View File

@@ -0,0 +1,186 @@
using Content.Server.Radiation.Components;
using Content.Shared.Radiation.Components;
using Content.Shared.Radiation.Systems;
using Robust.Shared.Collections;
using Robust.Shared.Map;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Server.Radiation.Systems;
// main algorithm that fire radiation rays to target
public partial class RadiationSystem
{
private void UpdateGridcast()
{
// should we save debug information into rays?
// if there is no debug sessions connected - just ignore it
var saveVisitedTiles = _debugSessions.Count > 0;
var stopwatch = new Stopwatch();
stopwatch.Start();
var sources = EntityQuery<RadiationSourceComponent, TransformComponent>();
var destinations = EntityQuery<RadiationReceiverComponent, TransformComponent>();
var resistanceQuery = GetEntityQuery<RadiationGridResistanceComponent>();
var transformQuery = GetEntityQuery<TransformComponent>();
// precalculate world positions for each source
// so we won't need to calc this in cycle over and over again
var sourcesData = new ValueList<(RadiationSourceComponent, TransformComponent, Vector2)>();
foreach (var (source, sourceTrs) in sources)
{
var worldPos = _transform.GetWorldPosition(sourceTrs, transformQuery);
var data = (source, sourceTrs, worldPos);
sourcesData.Add(data);
}
// trace all rays from rad source to rad receivers
var rays = new List<RadiationRay>();
var receiversTotalRads = new ValueList<(RadiationReceiverComponent, float)>();
foreach (var (dest, destTrs) in destinations)
{
var destWorld = _transform.GetWorldPosition(destTrs, transformQuery);
var rads = 0f;
foreach (var (source, sourceTrs, sourceWorld) in sourcesData)
{
// send ray towards destination entity
var ray = Irradiate(sourceTrs.Owner, sourceTrs, sourceWorld,
destTrs.Owner, destTrs, destWorld,
source.Intensity, source.Slope, saveVisitedTiles, resistanceQuery);
if (ray == null)
continue;
// save ray for debug
rays.Add(ray);
// add rads to total rad exposure
if (ray.ReachedDestination)
rads += ray.Rads;
}
receiversTotalRads.Add((dest, rads));
}
// update information for debug overlay
var elapsedTime = stopwatch.Elapsed.TotalMilliseconds;
var totalSources = sourcesData.Count;
var totalReceivers = receiversTotalRads.Count;
UpdateGridcastDebugOverlay(elapsedTime, totalSources, totalReceivers, rays);
// send rads to each entity
foreach (var (receiver, rads) in receiversTotalRads)
{
// update radiation value of receiver
// if no radiation rays reached target, that will set it to 0
receiver.CurrentRadiation = rads;
// also send an event with combination of total rad
if (rads > 0)
IrradiateEntity(receiver.Owner, rads,GridcastUpdateRate);
}
}
private RadiationRay? Irradiate(EntityUid sourceUid, TransformComponent sourceTrs, Vector2 sourceWorld,
EntityUid destUid, TransformComponent destTrs, Vector2 destWorld,
float incomingRads, float slope, bool saveVisitedTiles,
EntityQuery<RadiationGridResistanceComponent> resistanceQuery)
{
// lets first check that source and destination on the same map
if (sourceTrs.MapID != destTrs.MapID)
return null;
var mapId = sourceTrs.MapID;
// get direction from rad source to destination and its distance
var dir = destWorld - sourceWorld;
var dist = dir.Length;
// check if receiver is too far away
if (dist > GridcastMaxDistance)
return null;
// will it even reach destination considering distance penalty
var rads = incomingRads - slope * dist;
if (rads <= MinIntensity)
return null;
// create a new radiation ray from source to destination
// at first we assume that it doesn't hit any radiation blockers
// and has only distance penalty
var ray = new RadiationRay(mapId, sourceUid, sourceWorld, destUid, destWorld, rads);
// if source and destination on the same grid it's possible that
// between them can be another grid (ie. shuttle in center of donut station)
// however we can do simplification and ignore that case
if (GridcastSimplifiedSameGrid && sourceTrs.GridUid != null && sourceTrs.GridUid == destTrs.GridUid)
{
// todo: entity queries doesn't support interface - use it when IMapGridComponent will be removed
if (!TryComp(sourceTrs.GridUid.Value, out IMapGridComponent? gridComponent))
return ray;
return Gridcast(gridComponent.Grid, ray, saveVisitedTiles, resistanceQuery);
}
// lets check how many grids are between source and destination
// do a box intersection test between target and destination
// it's not very precise, but really cheap
var box = Box2.FromTwoPoints(sourceWorld, destWorld);
var grids = _mapManager.FindGridsIntersecting(mapId, box, true);
// gridcast through each grid and try to hit some radiation blockers
// the ray will be updated with each grid that has some blockers
foreach (var grid in grids)
{
ray = Gridcast(grid, ray, saveVisitedTiles, resistanceQuery);
// looks like last grid blocked all radiation
// we can return right now
if (ray.Rads <= 0)
return ray;
}
return ray;
}
private RadiationRay Gridcast(IMapGrid grid, RadiationRay ray, bool saveVisitedTiles,
EntityQuery<RadiationGridResistanceComponent> resistanceQuery)
{
var blockers = new List<(Vector2i, float)>();
// if grid doesn't have resistance map just apply distance penalty
var gridUid = grid.GridEntityId;
if (!resistanceQuery.TryGetComponent(gridUid, out var resistance))
return ray;
var resistanceMap = resistance.ResistancePerTile;
// get coordinate of source and destination in grid coordinates
var sourceGrid = grid.TileIndicesFor(ray.Source);
var destGrid = grid.TileIndicesFor(ray.Destination);
// iterate tiles in grid line from source to destination
var line = new GridLineEnumerator(sourceGrid, destGrid);
while (line.MoveNext())
{
var point = line.Current;
if (!resistanceMap.TryGetValue(point, out var resData))
continue;
ray.Rads -= resData;
// save data for debug
if (saveVisitedTiles)
blockers.Add((point, ray.Rads));
// no intensity left after blocker
if (ray.Rads <= MinIntensity)
{
ray.Rads = 0;
break;
}
}
// save data for debug if needed
if (saveVisitedTiles && blockers.Count > 0)
ray.Blockers.Add(gridUid, blockers);
return ray;
}
}

View File

@@ -1,52 +1,46 @@
using Content.Shared.Radiation.Events;
using Robust.Shared.Configuration;
using Robust.Shared.Map;
namespace Content.Server.Radiation.Systems;
public sealed class RadiationSystem : EntitySystem
public sealed partial class RadiationSystem : EntitySystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
private const float RadiationCooldown = 1.0f;
private float _accumulator;
public override void Initialize()
{
base.Initialize();
SubscribeCvars();
InitRadBlocking();
}
public override void Shutdown()
{
base.Shutdown();
UnsubscribeCvars();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
_accumulator += frameTime;
if (_accumulator < GridcastUpdateRate)
return;
while (_accumulator > RadiationCooldown)
{
_accumulator -= RadiationCooldown;
// All code here runs effectively every RadiationCooldown seconds, so use that as the "frame time".
foreach (var comp in EntityManager.EntityQuery<RadiationSourceComponent>())
{
var ent = comp.Owner;
if (Deleted(ent))
continue;
var cords = Transform(ent).MapPosition;
IrradiateRange(cords, comp.Range, comp.RadsPerSecond, RadiationCooldown);
}
}
}
public void IrradiateRange(MapCoordinates coordinates, float range, float radsPerSecond, float time)
{
var lookUp = _lookup.GetEntitiesInRange(coordinates, range);
foreach (var uid in lookUp)
{
if (Deleted(uid))
continue;
IrradiateEntity(uid, radsPerSecond, time);
}
UpdateGridcast();
UpdateResistanceDebugOverlay();
_accumulator = 0f;
}
public void IrradiateEntity(EntityUid uid, float radsPerSecond, float time)
{
var msg = new OnIrradiatedEvent(time, radsPerSecond);
RaiseLocalEvent(uid, msg, true);
RaiseLocalEvent(uid, msg);
}
}