Explosion refactor TEST MERG (#6995)

* Explosions

* fix yaml typo

and prevent silly UI inputs

* oop

Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
This commit is contained in:
Moony
2022-03-04 13:48:01 -06:00
committed by GitHub
parent 4e203f49d2
commit 4a466f4927
71 changed files with 3958 additions and 760 deletions

View File

@@ -29,21 +29,6 @@ namespace Content.Shared.Acts
void OnBreak(BreakageEventArgs eventArgs);
}
public interface IExAct
{
/// <summary>
/// Called when explosion reaches the entity
/// </summary>
void OnExplosion(ExplosionEventArgs eventArgs);
}
public sealed class ExplosionEventArgs : EventArgs
{
public EntityCoordinates Source { get; set; }
public EntityUid Target { get; set; }
public ExplosionSeverity Severity { get; set; }
}
[UsedImplicitly]
public sealed class ActSystem : EntitySystem
{
@@ -59,23 +44,7 @@ namespace Content.Shared.Acts
destroyAct.OnDestroy(eventArgs);
}
EntityManager.QueueDeleteEntity(owner);
}
public void HandleExplosion(EntityCoordinates source, EntityUid target, ExplosionSeverity severity)
{
var eventArgs = new ExplosionEventArgs
{
Source = source,
Target = target,
Severity = severity
};
var exActs = EntityManager.GetComponents<IExAct>(target).ToList();
foreach (var exAct in exActs)
{
exAct.OnExplosion(eventArgs);
}
QueueDel(owner);
}
public void HandleBreakage(EntityUid owner)
@@ -89,11 +58,4 @@ namespace Content.Shared.Acts
}
}
}
public enum ExplosionSeverity
{
Light,
Heavy,
Destruction,
}
}

View File

@@ -0,0 +1,52 @@
using Content.Shared.Eui;
using Robust.Shared.Serialization;
using Robust.Shared.Map;
using Content.Shared.Explosion;
namespace Content.Shared.Administration;
public static class SpawnExplosionEuiMsg
{
[Serializable, NetSerializable]
public sealed class Close : EuiMessageBase { }
/// <summary>
/// This message is sent to the server to request explosion preview data.
/// </summary>
[Serializable, NetSerializable]
public sealed class PreviewRequest : EuiMessageBase
{
public readonly MapCoordinates Epicenter;
public readonly string TypeId;
public readonly float TotalIntensity;
public readonly float IntensitySlope;
public readonly float MaxIntensity;
public PreviewRequest(MapCoordinates epicenter, string typeId, float totalIntensity, float intensitySlope, float maxIntensity)
{
Epicenter = epicenter;
TypeId = typeId;
TotalIntensity = totalIntensity;
IntensitySlope = intensitySlope;
MaxIntensity = maxIntensity;
}
}
/// <summary>
/// This message is used to send explosion-preview data to the client.
/// </summary>
[Serializable, NetSerializable]
public sealed class PreviewData : EuiMessageBase
{
public readonly float Slope;
public readonly float TotalIntensity;
public readonly ExplosionEvent Explosion;
public PreviewData(ExplosionEvent explosion, float slope, float totalIntensity)
{
Slope = slope;
TotalIntensity = totalIntensity;
Explosion = explosion;
}
}
}

View File

@@ -348,6 +348,85 @@ namespace Content.Shared.CCVar
public static readonly CVarDef<bool> AdminAnnounceLogout =
CVarDef.Create("admin.announce_logout", true, CVar.SERVERONLY);
/*
* Explosions
*/
/// <summary>
/// How many tiles the explosion system will process per tick
/// </summary>
/// <remarks>
/// Setting this too high will put a large load on a single tick. Setting this too low will lead to
/// unnaturally "slow" explosions.
/// </remarks>
public static readonly CVarDef<int> ExplosionTilesPerTick =
CVarDef.Create("explosion.tilespertick", 100, CVar.SERVERONLY);
/// <summary>
/// Upper limit on the size of an explosion before physics-throwing is disabled.
/// </summary>
/// <remarks>
/// Large nukes tend to generate a lot of shrapnel that flies through space. This can functionally cripple
/// the server TPS for a while after an explosion (or even during, if the explosion is processed
/// incrementally.
/// </remarks>
public static readonly CVarDef<int> ExplosionThrowLimit =
CVarDef.Create("explosion.throwlimit", 400, CVar.SERVERONLY);
/// <summary>
/// If this is true, explosion processing will pause the NodeGroupSystem to pause updating.
/// </summary>
/// <remarks>
/// This only takes effect if an explosion needs more than one tick to process (i.e., covers more than <see
/// cref="ExplosionTilesPerTick"/> tiles). If this is not enabled, the node-system will rebuild its graph
/// every tick as the explosion shreds the station, causing significant slowdown.
/// </remarks>
public static readonly CVarDef<bool> ExplosionSleepNodeSys =
CVarDef.Create("explosion.nodesleep", true, CVar.SERVERONLY);
/// <summary>
/// Upper limit on the total area that an explosion can affect before the neighbor-finding algorithm just
/// stops. Defaults to a 60-rile radius explosion.
/// </summary>
/// <remarks>
/// Actual area may be larger, as it currently doesn't terminate mid neighbor finding. I.e., area may be that of a ~51 tile radius circle instead.
/// </remarks>
public static readonly CVarDef<int> ExplosionMaxArea =
CVarDef.Create("explosion.maxarea", (int) 3.14f * 50 * 50, CVar.SERVERONLY);
/// <summary>
/// Upper limit on the number of neighbor finding steps for the explosion system neighbor-finding algorithm.
/// </summary>
/// <remarks>
/// Effectively places an upper limit on the range that any explosion can have. In the vast majority of
/// instances, <see cref="ExplosionMaxArea"/> will likely be hit before this becomes a limiting factor.
/// </remarks>
public static readonly CVarDef<int> ExplosionMaxIterations =
CVarDef.Create("explosion.maxiterations", 150, CVar.SERVERONLY);
/// <summary>
/// Max Time in milliseconds to spend processing explosions every tick.
/// </summary>
/// <remarks>
/// This time limiting is not perfectly implemented. Firstly, a significant chunk of processing time happens
/// due to queued entity deletions, which happen outside of the system update code. Secondly, explosion
/// spawning cannot currently be interrupted & resumed, and may lead to exceeding this time limit.
/// </remarks>
public static readonly CVarDef<float> ExplosionMaxProcessingTime =
CVarDef.Create("explosion.maxtime", 7f, CVar.SERVERONLY);
/// <summary>
/// If the explosion is being processed incrementally over several ticks, this variable determines whether
/// updating the grid tiles should be done incrementally at the end of every tick, or only once the explosion has finished processing.
/// </summary>
/// <remarks>
/// The most notable consequence of this change is that explosions will only punch a hole in the station &
/// create a vacumm once they have finished exploding. So airlocks will no longer slam shut as the explosion
/// expands, just suddenly at the end.
/// </remarks>
public static readonly CVarDef<bool> ExplosionIncrementalTileBreaking =
CVarDef.Create("explosion.incrementaltile", false, CVar.SERVERONLY);
/*
* Admin logs
*/

View File

@@ -25,7 +25,7 @@ namespace Content.Shared.Damage
[RegisterComponent]
[NetworkedComponent()]
[Friend(typeof(DamageableSystem))]
public sealed class DamageableComponent : Component, IRadiationAct, IExAct
public sealed class DamageableComponent : Component, IRadiationAct
{
/// <summary>
/// This <see cref="DamageContainerPrototype"/> specifies what damage types are supported by this component.
@@ -92,27 +92,6 @@ namespace Content.Shared.Damage
EntitySystem.Get<DamageableSystem>().TryChangeDamage(Owner, damage);
}
// TODO EXPLOSION Remove this.
void IExAct.OnExplosion(ExplosionEventArgs eventArgs)
{
var damageValue = eventArgs.Severity switch
{
ExplosionSeverity.Light => FixedPoint2.New(20),
ExplosionSeverity.Heavy => FixedPoint2.New(60),
ExplosionSeverity.Destruction => FixedPoint2.New(250),
_ => throw new ArgumentOutOfRangeException()
};
// Explosion should really just be a damage group instead of a list of types.
DamageSpecifier damage = new();
foreach (var typeID in ExplosionDamageTypeIDs)
{
damage.DamageDict.Add(typeID, damageValue);
}
EntitySystem.Get<DamageableSystem>().TryChangeDamage(Owner, damage);
}
}
[Serializable, NetSerializable]

View File

@@ -0,0 +1,81 @@
using Content.Shared.Inventory;
using Robust.Shared.Map;
using Robust.Shared.Serialization;
namespace Content.Shared.Explosion;
/// <summary>
/// Raised directed at an entity to determine its explosion resistance, probably right before it is about to be
/// damaged by one.
/// </summary>
public class GetExplosionResistanceEvent : EntityEventArgs, IInventoryRelayEvent
{
/// <summary>
/// Can be set to whatever, but currently is being additively increased by components & clothing. So think twice
/// before multiplying or directly setting this.
/// </summary>
public float Resistance = 0;
public readonly string ExplotionPrototype;
SlotFlags IInventoryRelayEvent.TargetSlots => ~SlotFlags.POCKET;
public GetExplosionResistanceEvent(string id)
{
ExplotionPrototype = id;
}
}
/// <summary>
/// An explosion event. Used for client side rendering.
/// </summary>
[Serializable, NetSerializable]
public class ExplosionEvent : EntityEventArgs
{
public MapCoordinates Epicenter;
public Dictionary<int, List<Vector2i>>? SpaceTiles;
public Dictionary<GridId, Dictionary<int, List<Vector2i>>> Tiles;
public List<float> Intensity;
public string TypeID;
public Matrix3 SpaceMatrix;
public byte ExplosionId;
public ExplosionEvent(
byte explosionId,
MapCoordinates epicenter,
string typeID,
List<float> intensity,
Dictionary<int, List<Vector2i>>? spaceTiles,
Dictionary<GridId, Dictionary<int, List<Vector2i>>> tiles,
Matrix3 spaceMatrix)
{
Epicenter = epicenter;
SpaceTiles = spaceTiles;
Tiles = tiles;
Intensity = intensity;
TypeID = typeID;
SpaceMatrix = spaceMatrix;
ExplosionId = explosionId;
}
}
/// <summary>
/// Update visual rendering of the explosion to correspond to the servers processing of it.
/// </summary>
[Serializable, NetSerializable]
public class ExplosionOverlayUpdateEvent : EntityEventArgs
{
public int Index;
public byte ExplosionId;
public ExplosionOverlayUpdateEvent(byte explosionId, int index)
{
Index = index;
ExplosionId = explosionId;
}
}

View File

@@ -0,0 +1,89 @@
using Content.Shared.Damage;
using Content.Shared.Sound;
using Robust.Shared.Prototypes;
namespace Content.Shared.Explosion;
[Prototype("explosion")]
public sealed class ExplosionPrototype : IPrototype
{
[DataField("id", required: true)]
public string ID { get; } = default!;
/// <summary>
/// Damage to deal to entities. This is scaled by the explosion intensity.
/// </summary>
[DataField("damagePerIntensity", required: true)]
public readonly DamageSpecifier DamagePerIntensity = default!;
/// <summary>
/// This set of points, together with <see cref="_tileBreakIntensity"/> define a function that maps the
/// explosion intensity to a tile break chance via linear interpolation.
/// </summary>
[DataField("tileBreakChance")]
private readonly float[] _tileBreakChance = { 0f, 1f };
/// <summary>
/// This set of points, together with <see cref="_tileBreakChance"/> define a function that maps the
/// explosion intensity to a tile break chance via linear interpolation.
/// </summary>
[DataField("tileBreakIntensity")]
private readonly float[] _tileBreakIntensity = {0f, 15f };
/// <summary>
/// When a tile is broken by an explosion, the intensity is reduced by this amount and is used to try and
/// break the tile a second time. This is repeated until a roll fails or the tile has become space.
/// </summary>
/// <remarks>
/// If this number is too small, even relatively weak explosions can have a non-zero
/// chance to create a space tile.
/// </remarks>
[DataField("tileBreakRerollReduction")]
public readonly float TileBreakRerollReduction = 10f;
/// <summary>
/// Color emitted by a point light at the center of the explosion.
/// </summary>
[DataField("lightColor")]
public readonly Color LightColor = Color.Orange;
/// <summary>
/// Color used to modulate the fire texture.
/// </summary>
[DataField("fireColor")]
public readonly Color? FireColor;
[DataField("Sound")]
public readonly SoundSpecifier Sound = new SoundCollectionSpecifier("explosion");
[DataField("texturePath")]
public readonly string TexturePath = "/Textures/Effects/fire.rsi";
// Theres probably a better way to do this. Currently Atmos just hard codes a constant int, so I have no one to
// steal code from.
[DataField("fireStates")]
public readonly int FireStates = 3;
/// <summary>
/// Basic function for linear interpolation of _tileBreakChance and _tileBreakIntensity
/// </summary>
public float TileBreakChance(float intensity)
{
if (_tileBreakChance.Length == 0 || _tileBreakChance.Length != _tileBreakIntensity.Length)
{
Logger.Error($"Malformed tile break chance definitions for explosion prototype: {ID}");
return 0;
}
if (intensity >= _tileBreakIntensity[^1] || _tileBreakIntensity.Length == 1)
return _tileBreakChance[^1];
if (intensity <= _tileBreakIntensity[0])
return _tileBreakChance[0];
int i = Array.FindIndex(_tileBreakIntensity, k => k >= intensity);
var slope = (_tileBreakChance[i] - _tileBreakChance[i - 1]) / (_tileBreakIntensity[i] - _tileBreakIntensity[i - 1]);
return _tileBreakChance[i - 1] + slope * (intensity - _tileBreakIntensity[i - 1]);
}
}

View File

@@ -234,6 +234,11 @@ namespace Content.Shared.FixedPoint
_value == unit._value;
}
public bool EqualsApprox(FixedPoint2 other, FixedPoint2 tolerance)
{
return Math.Abs(_value - other._value) < tolerance._value;
}
public override readonly int GetHashCode()
{
// ReSharper disable once NonReadonlyMemberInGetHashCode

View File

@@ -1,38 +1,10 @@
using System.Linq;
using Content.Shared.Acts;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Inventory;
public abstract class InventoryComponent : Component, IExAct
public abstract class InventoryComponent : Component
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[DataField("templateId", required: true,
customTypeSerializer: typeof(PrototypeIdSerializer<InventoryTemplatePrototype>))]
public string TemplateId { get; } = "human";
void IExAct.OnExplosion(ExplosionEventArgs eventArgs)
{
if (eventArgs.Severity < ExplosionSeverity.Heavy)
{
return;
}
if (EntitySystem.Get<InventorySystem>()
.TryGetContainerSlotEnumerator(Owner, out var enumerator, this))
{
while (enumerator.MoveNext(out var container))
{
if (!container.ContainedEntity.HasValue) continue;
foreach (var exAct in _entityManager.GetComponents<IExAct>(container.ContainedEntity.Value).ToArray())
{
exAct.OnExplosion(eventArgs);
}
}
}
}
}

View File

@@ -1,8 +1,8 @@
using Content.Shared.Damage;
using Content.Shared.Damage;
using Content.Shared.Electrocution;
using Content.Shared.Explosion;
using Content.Shared.Movement.EntitySystems;
using Content.Shared.Slippery;
using Robust.Shared.GameObjects;
namespace Content.Shared.Inventory;
@@ -14,6 +14,7 @@ public partial class InventorySystem
SubscribeLocalEvent<InventoryComponent, ElectrocutionAttemptEvent>(RelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, SlipAttemptEvent>(RelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, RefreshMovementSpeedModifiersEvent>(RelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, GetExplosionResistanceEvent>(RelayInventoryEvent);
}
protected void RelayInventoryEvent<T>(EntityUid uid, InventoryComponent component, T args) where T : EntityEventArgs, IInventoryRelayEvent