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:
@@ -1,392 +1,274 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Atmos.Components;
|
||||
using Content.Server.Explosion.Components;
|
||||
using Content.Shared.Acts;
|
||||
using Content.Server.NodeContainer.EntitySystems;
|
||||
using Content.Shared.Camera;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Maps;
|
||||
using Content.Shared.Physics;
|
||||
using Content.Shared.Sound;
|
||||
using Content.Shared.Tag;
|
||||
using Robust.Server.GameObjects;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Explosion;
|
||||
using Robust.Server.Containers;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Explosion.EntitySystems
|
||||
namespace Content.Server.Explosion.EntitySystems;
|
||||
|
||||
public sealed partial class ExplosionSystem : EntitySystem
|
||||
{
|
||||
public sealed class ExplosionSystem : EntitySystem
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
|
||||
[Dependency] private readonly ContainerSystem _containerSystem = default!;
|
||||
[Dependency] private readonly NodeGroupSystem _nodeGroupSystem = default!;
|
||||
[Dependency] private readonly CameraRecoilSystem _recoilSystem = default!;
|
||||
[Dependency] private readonly EntityLookupSystem _entityLookup = default!;
|
||||
|
||||
/// <summary>
|
||||
/// "Tile-size" for space when there are no nearby grids to use as a reference.
|
||||
/// </summary>
|
||||
public const ushort DefaultTileSize = 1;
|
||||
|
||||
private AudioParams _audioParams = AudioParams.Default.WithVolume(-3f);
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
|
||||
base.Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// Distance used for camera shake when distance from explosion is (0.0, 0.0).
|
||||
/// Avoids getting NaN values down the line from doing math on (0.0, 0.0).
|
||||
/// </summary>
|
||||
private static readonly Vector2 EpicenterDistance = (0.1f, 0.1f);
|
||||
// handled in ExplosionSystemGridMap.cs
|
||||
SubscribeLocalEvent<GridRemovalEvent>(OnGridRemoved);
|
||||
SubscribeLocalEvent<GridStartupEvent>(OnGridStartup);
|
||||
SubscribeLocalEvent<ExplosionResistanceComponent, GetExplosionResistanceEvent>(OnGetResistance);
|
||||
_mapManager.TileChanged += OnTileChanged;
|
||||
|
||||
/// <summary>
|
||||
/// Chance of a tile breaking if the severity is Light and Heavy
|
||||
/// </summary>
|
||||
private const float LightBreakChance = 0.3f;
|
||||
private const float HeavyBreakChance = 0.8f;
|
||||
// handled in ExplosionSystemAirtight.cs
|
||||
SubscribeLocalEvent<AirtightComponent, DamageChangedEvent>(OnAirtightDamaged);
|
||||
SubscribeCvars();
|
||||
}
|
||||
|
||||
// TODO move this to the component
|
||||
private static readonly SoundSpecifier ExplosionSound = new SoundCollectionSpecifier("explosion");
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_mapManager.TileChanged -= OnTileChanged;
|
||||
UnsubscribeCvars();
|
||||
}
|
||||
|
||||
[Dependency] private readonly EntityLookupSystem _entityLookup = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly IMapManager _maps = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tiles = default!;
|
||||
private void OnGetResistance(EntityUid uid, ExplosionResistanceComponent component, GetExplosionResistanceEvent args)
|
||||
{
|
||||
args.Resistance += component.GlobalResistance;
|
||||
if (component.Resistances.TryGetValue(args.ExplotionPrototype, out var resistance))
|
||||
args.Resistance += resistance;
|
||||
}
|
||||
|
||||
[Dependency] private readonly ActSystem _acts = default!;
|
||||
[Dependency] private readonly EffectSystem _effects = default!;
|
||||
[Dependency] private readonly TriggerSystem _triggers = default!;
|
||||
[Dependency] private readonly AdminLogSystem _logSystem = default!;
|
||||
[Dependency] private readonly CameraRecoilSystem _cameraRecoil = default!;
|
||||
[Dependency] private readonly TagSystem _tags = default!;
|
||||
/// <summary>
|
||||
/// Given an entity with an explosive component, spawn the appropriate explosion.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Also accepts radius or intensity arguments. This is useful for explosives where the intensity is not
|
||||
/// specified in the yaml / by the component, but determined dynamically (e.g., by the quantity of a
|
||||
/// solution in a reaction).
|
||||
/// </remarks>
|
||||
public void TriggerExplosive(EntityUid uid, ExplosiveComponent? explosive = null, bool delete = true, float? totalIntensity = null, float? radius = null)
|
||||
{
|
||||
// log missing: false, because some entities (e.g. liquid tanks) attempt to trigger explosions when damaged,
|
||||
// but may not actually be explosive.
|
||||
if (!Resolve(uid, ref explosive, logMissing: false))
|
||||
return;
|
||||
|
||||
private bool IgnoreExplosivePassable(EntityUid e)
|
||||
// No reusable explosions here.
|
||||
if (explosive.Exploded)
|
||||
return;
|
||||
|
||||
explosive.Exploded = true;
|
||||
|
||||
// Override the explosion intensity if optional arguments were provided.
|
||||
if (radius != null)
|
||||
totalIntensity ??= RadiusToIntensity((float) radius, explosive.IntensitySlope, explosive.MaxIntensity);
|
||||
totalIntensity ??= explosive.TotalIntensity;
|
||||
|
||||
QueueExplosion(uid,
|
||||
explosive.ExplosionType,
|
||||
(float) totalIntensity,
|
||||
explosive.IntensitySlope,
|
||||
explosive.MaxIntensity);
|
||||
|
||||
if (delete)
|
||||
EntityManager.QueueDeleteEntity(uid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find the strength needed to generate an explosion of a given radius. More useful for radii larger then 4, when the explosion becomes less "blocky".
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This assumes the explosion is in a vacuum / unobstructed. Given that explosions are not perfectly
|
||||
/// circular, here radius actually means the sqrt(Area/pi), where the area is the total number of tiles
|
||||
/// covered by the explosion. Until you get to radius 30+, this is functionally equivalent to the
|
||||
/// actual radius.
|
||||
/// </remarks>
|
||||
public float RadiusToIntensity(float radius, float slope, float maxIntensity = 0)
|
||||
{
|
||||
// If you consider the intensity at each tile in an explosion to be a height. Then a circular explosion is
|
||||
// shaped like a cone. So total intensity is like the volume of a cone with height = slope * radius. Of
|
||||
// course, as the explosions are not perfectly circular, this formula isn't perfect, but the formula works
|
||||
// reasonably well.
|
||||
|
||||
// This should actually use the formula for the volume of a distorted octagonal frustum. But this is good
|
||||
// enough.
|
||||
|
||||
var coneVolume = slope * MathF.PI / 3 * MathF.Pow(radius, 3);
|
||||
|
||||
if (maxIntensity <= 0 || slope * radius < maxIntensity)
|
||||
return coneVolume;
|
||||
|
||||
// This explosion is limited by the maxIntensity.
|
||||
// Instead of a cone, we have a conical frustum.
|
||||
|
||||
// Subtract the volume of the missing cone segment, with height:
|
||||
var h = slope * radius - maxIntensity;
|
||||
return coneVolume - h * MathF.PI / 3 * MathF.Pow(h / slope, 2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inverse formula for <see cref="RadiusToIntensity"/>
|
||||
/// </summary>
|
||||
public float IntensityToRadius(float totalIntensity, float slope, float maxIntensity)
|
||||
{
|
||||
// max radius to avoid being capped by max-intensity
|
||||
var r0 = maxIntensity / slope;
|
||||
|
||||
// volume at r0
|
||||
var v0 = RadiusToIntensity(r0, slope);
|
||||
|
||||
if (totalIntensity <= v0)
|
||||
{
|
||||
return _tags.HasTag(e, "ExplosivePassable");
|
||||
// maxIntensity is a non-issue, can use simple inverse formula
|
||||
return MathF.Cbrt(3 * totalIntensity / (slope * MathF.PI));
|
||||
}
|
||||
|
||||
private ExplosionSeverity CalculateSeverity(float distance, float devastationRange, float heavyRange)
|
||||
return r0 * (MathF.Sqrt(12 * totalIntensity/ v0 - 3) / 6 + 0.5f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue an explosions, centered on some entity.
|
||||
/// </summary>
|
||||
public void QueueExplosion(EntityUid uid,
|
||||
string typeId,
|
||||
float intensity,
|
||||
float slope,
|
||||
float maxTileIntensity)
|
||||
{
|
||||
QueueExplosion(Transform(uid).MapPosition, typeId, intensity, slope, maxTileIntensity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queue an explosion, with a specified epicenter and set of starting tiles.
|
||||
/// </summary>
|
||||
public void QueueExplosion(MapCoordinates epicenter,
|
||||
string typeId,
|
||||
float totalIntensity,
|
||||
float slope,
|
||||
float maxTileIntensity)
|
||||
{
|
||||
if (totalIntensity <= 0 || slope <= 0)
|
||||
return;
|
||||
|
||||
if (!_prototypeManager.TryIndex<ExplosionPrototype>(typeId, out var type))
|
||||
{
|
||||
if (distance < devastationRange)
|
||||
{
|
||||
return ExplosionSeverity.Destruction;
|
||||
}
|
||||
else if (distance < heavyRange)
|
||||
{
|
||||
return ExplosionSeverity.Heavy;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ExplosionSeverity.Light;
|
||||
}
|
||||
Logger.Error($"Attempted to spawn unknown explosion prototype: {type}");
|
||||
return;
|
||||
}
|
||||
|
||||
private void CameraShakeInRange(EntityCoordinates epicenter, float maxRange)
|
||||
_explosionQueue.Enqueue(() => SpawnExplosion(epicenter, type, totalIntensity,
|
||||
slope, maxTileIntensity));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This function actually spawns the explosion. It returns an <see cref="Explosion"/> instance with
|
||||
/// information about the affected tiles for the explosion system to process. It will also trigger the
|
||||
/// camera shake and sound effect.
|
||||
/// </summary>
|
||||
private Explosion? SpawnExplosion(MapCoordinates epicenter,
|
||||
ExplosionPrototype type,
|
||||
float totalIntensity,
|
||||
float slope,
|
||||
float maxTileIntensity)
|
||||
{
|
||||
var results = GetExplosionTiles(epicenter, type.ID, totalIntensity, slope, maxTileIntensity);
|
||||
|
||||
if (results == null)
|
||||
return null;
|
||||
|
||||
var (area, iterationIntensity, spaceData, gridData, spaceMatrix) = results.Value;
|
||||
|
||||
RaiseNetworkEvent(GetExplosionEvent(epicenter, type.ID, spaceMatrix, spaceData, gridData.Values, iterationIntensity));
|
||||
|
||||
// camera shake
|
||||
CameraShake(iterationIntensity.Count * 2.5f, epicenter, totalIntensity);
|
||||
|
||||
//For whatever bloody reason, sound system requires ENTITY coordinates.
|
||||
var mapEntityCoords = EntityCoordinates.FromMap(EntityManager, _mapManager.GetMapEntityId(epicenter.MapId), epicenter);
|
||||
|
||||
// play sound.
|
||||
var audioRange = iterationIntensity.Count * 5;
|
||||
var filter = Filter.Pvs(epicenter).AddInRange(epicenter, audioRange);
|
||||
SoundSystem.Play(filter, type.Sound.GetSound(), mapEntityCoords, _audioParams);
|
||||
|
||||
return new Explosion(this,
|
||||
type,
|
||||
spaceData,
|
||||
gridData.Values.ToList(),
|
||||
iterationIntensity,
|
||||
epicenter,
|
||||
spaceMatrix,
|
||||
area
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for the shared <see cref="ExplosionEvent"/> using the server-exclusive explosion classes.
|
||||
/// </summary>
|
||||
internal ExplosionEvent GetExplosionEvent(MapCoordinates epicenter, string id, Matrix3 spaceMatrix, SpaceExplosion? spaceData, IEnumerable<GridExplosion> gridData, List<float> iterationIntensity)
|
||||
{
|
||||
var spaceTiles = spaceData?.TileLists;
|
||||
|
||||
Dictionary<GridId, Dictionary<int, List<Vector2i>>> tileLists = new();
|
||||
foreach (var grid in gridData)
|
||||
{
|
||||
var players = Filter.Empty()
|
||||
.AddInRange(epicenter.ToMap(EntityManager), MathF.Ceiling(maxRange))
|
||||
.Recipients;
|
||||
|
||||
foreach (var player in players)
|
||||
{
|
||||
if (player.AttachedEntity is not {Valid: true} playerEntity ||
|
||||
!EntityManager.HasComponent<CameraRecoilComponent>(playerEntity))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var playerPos = EntityManager.GetComponent<TransformComponent>(playerEntity).WorldPosition;
|
||||
var delta = epicenter.ToMapPos(EntityManager) - playerPos;
|
||||
|
||||
//Change if zero. Will result in a NaN later breaking camera shake if not changed
|
||||
if (delta.EqualsApprox((0.0f, 0.0f)))
|
||||
delta = EpicenterDistance;
|
||||
|
||||
var distance = delta.LengthSquared;
|
||||
var effect = 10 * (1 / (1 + distance));
|
||||
if (effect > 0.01f)
|
||||
{
|
||||
var kick = -delta.Normalized * effect;
|
||||
_cameraRecoil.KickCamera(player.AttachedEntity.Value, kick);
|
||||
}
|
||||
}
|
||||
tileLists.Add(grid.Grid.Index, grid.TileLists);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Damage entities inside the range. The damage depends on a discrete
|
||||
/// damage bracket [light, heavy, devastation] and the distance from the epicenter
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A dictionary of coordinates relative to the parents of every grid of entities that survived the explosion,
|
||||
/// have an airtight component and are currently blocking air. Like a wall.
|
||||
/// </returns>
|
||||
private void DamageEntitiesInRange(
|
||||
EntityCoordinates epicenter,
|
||||
Box2 boundingBox,
|
||||
float devastationRange,
|
||||
float heavyRange,
|
||||
float maxRange,
|
||||
MapId mapId)
|
||||
return new ExplosionEvent(_explosionCounter, epicenter, id, iterationIntensity, spaceTiles, tileLists, spaceMatrix);
|
||||
}
|
||||
|
||||
private void CameraShake(float range, MapCoordinates epicenter, float totalIntensity)
|
||||
{
|
||||
var players = Filter.Empty();
|
||||
players.AddInRange(epicenter, range, _playerManager, EntityManager);
|
||||
|
||||
foreach (var player in players.Recipients)
|
||||
{
|
||||
var entitiesInRange = _entityLookup.GetEntitiesInRange(mapId, boundingBox, 0).ToList();
|
||||
if (player.AttachedEntity is not EntityUid uid)
|
||||
continue;
|
||||
|
||||
var impassableEntities = new List<(EntityUid, float)>();
|
||||
var nonImpassableEntities = new List<(EntityUid, float)>();
|
||||
// TODO: Given this seems to rely on physics it should just query directly like everything else.
|
||||
var playerPos = Transform(player.AttachedEntity!.Value).WorldPosition;
|
||||
var delta = epicenter.Position - playerPos;
|
||||
|
||||
// The entities are paired with their distance to the epicenter
|
||||
// and splitted into two lists based on if they are Impassable or not
|
||||
foreach (var entity in entitiesInRange)
|
||||
{
|
||||
if (Deleted(entity) || entity.IsInContainer())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (delta.EqualsApprox(Vector2.Zero))
|
||||
delta = new(0.01f, 0);
|
||||
|
||||
if (!EntityManager.GetComponent<TransformComponent>(entity).Coordinates.TryDistance(EntityManager, epicenter, out var distance) ||
|
||||
distance > maxRange)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(entity, out FixturesComponent? fixturesComp) || fixturesComp.Fixtures.Count < 1)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(entity, out PhysicsComponent? body))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((body.CollisionLayer & (int) CollisionGroup.Impassable) != 0)
|
||||
{
|
||||
impassableEntities.Add((entity, distance));
|
||||
}
|
||||
else
|
||||
{
|
||||
nonImpassableEntities.Add((entity, distance));
|
||||
}
|
||||
}
|
||||
|
||||
// The Impassable entities are sorted in descending order
|
||||
// Entities closer to the epicenter are first
|
||||
impassableEntities.Sort((x, y) => x.Item2.CompareTo(y.Item2));
|
||||
|
||||
// Impassable entities are handled first. If they are damaged enough, they are destroyed and they may
|
||||
// be able to spawn a new entity. I.e Wall -> Girder.
|
||||
// Girder has a tag ExplosivePassable, and the predicate make it so the entities with this tag are ignored
|
||||
var epicenterMapPos = epicenter.ToMap(EntityManager);
|
||||
foreach (var (entity, distance) in impassableEntities)
|
||||
{
|
||||
if (!_interactionSystem.InRangeUnobstructed(epicenterMapPos, entity, maxRange, predicate: IgnoreExplosivePassable))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_acts.HandleExplosion(epicenter, entity, CalculateSeverity(distance, devastationRange, heavyRange));
|
||||
}
|
||||
|
||||
// Impassable entities were handled first so NonImpassable entities have a bigger chance to get hit. As now
|
||||
// there are probably more ExplosivePassable entities around
|
||||
foreach (var (entity, distance) in nonImpassableEntities)
|
||||
{
|
||||
if (!_interactionSystem.InRangeUnobstructed(epicenterMapPos, entity, maxRange, predicate: IgnoreExplosivePassable))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_acts.HandleExplosion(epicenter, entity, CalculateSeverity(distance, devastationRange, heavyRange));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Damage tiles inside the range. The type of tile can change depending on a discrete
|
||||
/// damage bracket [light, heavy, devastation], the distance from the epicenter and
|
||||
/// a probability bracket [<see cref="LightBreakChance"/>, <see cref="HeavyBreakChance"/>, 1.0].
|
||||
/// </summary>
|
||||
///
|
||||
private void DamageTilesInRange(EntityCoordinates epicenter,
|
||||
GridId gridId,
|
||||
Box2 boundingBox,
|
||||
float devastationRange,
|
||||
float heaveyRange,
|
||||
float maxRange)
|
||||
{
|
||||
if (!_maps.TryGetGrid(gridId, out var mapGrid))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!EntityManager.EntityExists(mapGrid.GridEntityId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var tilesInGridAndCircle = mapGrid.GetTilesIntersecting(boundingBox);
|
||||
var epicenterMapPos = epicenter.ToMap(EntityManager);
|
||||
|
||||
foreach (var tile in tilesInGridAndCircle)
|
||||
{
|
||||
var tileLoc = mapGrid.GridTileToLocal(tile.GridIndices);
|
||||
if (!tileLoc.TryDistance(EntityManager, epicenter, out var distance) ||
|
||||
distance > maxRange)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tile.IsBlockedTurf(false))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_interactionSystem.InRangeUnobstructed(tileLoc.ToMap(EntityManager), epicenterMapPos, maxRange, predicate: IgnoreExplosivePassable))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var tileDef = (ContentTileDefinition) _tiles[tile.Tile.TypeId];
|
||||
var baseTurfs = tileDef.BaseTurfs;
|
||||
if (baseTurfs.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var zeroTile = new Tile(_tiles[baseTurfs[0]].TileId);
|
||||
var previousTile = new Tile(_tiles[baseTurfs[^1]].TileId);
|
||||
|
||||
var severity = CalculateSeverity(distance, devastationRange, heaveyRange);
|
||||
|
||||
switch (severity)
|
||||
{
|
||||
case ExplosionSeverity.Light:
|
||||
if (!previousTile.IsEmpty && _random.Prob(LightBreakChance))
|
||||
{
|
||||
mapGrid.SetTile(tileLoc, previousTile);
|
||||
}
|
||||
break;
|
||||
case ExplosionSeverity.Heavy:
|
||||
if (!previousTile.IsEmpty && _random.Prob(HeavyBreakChance))
|
||||
{
|
||||
mapGrid.SetTile(tileLoc, previousTile);
|
||||
}
|
||||
break;
|
||||
case ExplosionSeverity.Destruction:
|
||||
mapGrid.SetTile(tileLoc, zeroTile);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FlashInRange(EntityCoordinates epicenter, float flashRange)
|
||||
{
|
||||
if (flashRange > 0)
|
||||
{
|
||||
var time = _timing.CurTime;
|
||||
var message = new EffectSystemMessage
|
||||
{
|
||||
EffectSprite = "Effects/explosion.rsi",
|
||||
RsiState = "explosionfast",
|
||||
Born = time,
|
||||
DeathTime = time + TimeSpan.FromSeconds(5),
|
||||
Size = new Vector2(flashRange / 2, flashRange / 2),
|
||||
Coordinates = epicenter,
|
||||
Rotation = 0f,
|
||||
ColorDelta = new Vector4(0, 0, 0, -1500f),
|
||||
Color = Vector4.Multiply(new Vector4(255, 255, 255, 750), 0.5f),
|
||||
Shaded = false
|
||||
};
|
||||
|
||||
_effects.CreateParticle(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void SpawnExplosion(
|
||||
EntityUid entity,
|
||||
int devastationRange = 0,
|
||||
int heavyImpactRange = 0,
|
||||
int lightImpactRange = 0,
|
||||
int flashRange = 0,
|
||||
EntityUid? user = null,
|
||||
ExplosiveComponent? explosive = null,
|
||||
TransformComponent? transform = null)
|
||||
{
|
||||
if (!Resolve(entity, ref transform))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Resolve(entity, ref explosive, false);
|
||||
|
||||
if (explosive is { Exploding: false })
|
||||
{
|
||||
_triggers.Explode(entity, explosive, user);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (EntityManager.EntityExists(entity) && entity.TryGetContainer(out var container))
|
||||
{
|
||||
entity = container.Owner;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(entity, out transform))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var epicenter = transform.Coordinates;
|
||||
|
||||
SpawnExplosion(epicenter, devastationRange, heavyImpactRange, lightImpactRange, flashRange, entity, user);
|
||||
}
|
||||
}
|
||||
|
||||
public void SpawnExplosion(
|
||||
EntityCoordinates epicenter,
|
||||
int devastationRange = 0,
|
||||
int heavyImpactRange = 0,
|
||||
int lightImpactRange = 0,
|
||||
int flashRange = 0,
|
||||
EntityUid? entity = null,
|
||||
EntityUid? user = null)
|
||||
{
|
||||
var mapId = epicenter.GetMapId(EntityManager);
|
||||
if (mapId == MapId.Nullspace)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// logging
|
||||
var range = $"{devastationRange}/{heavyImpactRange}/{lightImpactRange}/{flashRange}";
|
||||
if (entity == null || !entity.Value.IsValid())
|
||||
{
|
||||
_logSystem.Add(LogType.Explosion, LogImpact.High, $"Explosion spawned at {epicenter:coordinates} with range {range}");
|
||||
}
|
||||
else if (user == null || !user.Value.IsValid())
|
||||
{
|
||||
_logSystem.Add(LogType.Explosion, LogImpact.High,
|
||||
$"{ToPrettyString(entity.Value):entity} exploded at {epicenter:coordinates} with range {range}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logSystem.Add(LogType.Explosion, LogImpact.High,
|
||||
$"{ToPrettyString(user.Value):user} caused {ToPrettyString(entity.Value):entity} to explode at {epicenter:coordinates} with range {range}");
|
||||
}
|
||||
|
||||
var maxRange = MathHelper.Max(devastationRange, heavyImpactRange, lightImpactRange, 0);
|
||||
var epicenterMapPos = epicenter.ToMapPos(EntityManager);
|
||||
var boundingBox = new Box2(epicenterMapPos - new Vector2(maxRange, maxRange),
|
||||
epicenterMapPos + new Vector2(maxRange, maxRange));
|
||||
|
||||
SoundSystem.Play(Filter.Broadcast(), ExplosionSound.GetSound(), epicenter);
|
||||
DamageEntitiesInRange(epicenter, boundingBox, devastationRange, heavyImpactRange, maxRange, mapId);
|
||||
|
||||
var mapGridsNear = _maps.FindGridsIntersecting(mapId, boundingBox);
|
||||
|
||||
foreach (var gridId in mapGridsNear)
|
||||
{
|
||||
DamageTilesInRange(epicenter, gridId.Index, boundingBox, devastationRange, heavyImpactRange, maxRange);
|
||||
}
|
||||
|
||||
CameraShakeInRange(epicenter, maxRange);
|
||||
FlashInRange(epicenter, flashRange);
|
||||
var distance = delta.Length;
|
||||
var effect = 5 * MathF.Pow(totalIntensity, 0.5f) * (1 - distance / range);
|
||||
if (effect > 0.01f)
|
||||
_recoilSystem.KickCamera(uid, -delta.Normalized * effect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user