2024-06-09 15:45:27 +03:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Numerics;
|
2024-04-14 18:31:23 +03:00
|
|
|
|
using Content.Server.Atmos.Components;
|
|
|
|
|
|
using Content.Server.Atmos.EntitySystems;
|
2024-06-09 15:45:27 +03:00
|
|
|
|
using Content.Shared.Maps;
|
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
|
using Robust.Shared.Map.Components;
|
|
|
|
|
|
using Robust.Shared.Prototypes;
|
2024-04-14 18:31:23 +03:00
|
|
|
|
using Robust.Shared.Random;
|
|
|
|
|
|
using Robust.Shared.Timing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Content.Server._CP14.Temperature;
|
|
|
|
|
|
|
2024-04-20 12:45:25 +03:00
|
|
|
|
public sealed partial class CP14FireSpreadSystem : EntitySystem
|
2024-04-14 18:31:23 +03:00
|
|
|
|
{
|
|
|
|
|
|
[Dependency] private readonly FlammableSystem _flammable = default!;
|
|
|
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
|
[Dependency] private readonly EntityLookupSystem _lookup = default!;
|
|
|
|
|
|
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
|
|
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2024-06-09 15:45:27 +03:00
|
|
|
|
[Dependency] private readonly SharedMapSystem _mapSystem = default!;
|
|
|
|
|
|
[Dependency] private readonly TileSystem _tile = default!;
|
|
|
|
|
|
[Dependency] private readonly ITileDefinitionManager _tiledef = default!;
|
2024-04-14 18:31:23 +03:00
|
|
|
|
|
2024-06-09 15:45:27 +03:00
|
|
|
|
private EntProtoId _fireProto = "CP14Fire";
|
2024-11-13 17:56:07 +03:00
|
|
|
|
|
2024-04-14 18:31:23 +03:00
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
2024-06-09 15:45:27 +03:00
|
|
|
|
SubscribeLocalEvent<CP14FireSpreadComponent, OnFireChangedEvent>(OnCompInit);
|
|
|
|
|
|
SubscribeLocalEvent<CP14DespawnOnExtinguishComponent, OnFireChangedEvent>(OnFireChanged);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnFireChanged(Entity<CP14DespawnOnExtinguishComponent> ent, ref OnFireChangedEvent args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!args.OnFire)
|
|
|
|
|
|
QueueDel(ent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-20 12:45:25 +03:00
|
|
|
|
private void OnCompInit(Entity<CP14FireSpreadComponent> ent, ref OnFireChangedEvent args)
|
2024-04-14 18:31:23 +03:00
|
|
|
|
{
|
|
|
|
|
|
if (!args.OnFire)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
var cooldown = _random.NextFloat(ent.Comp.SpreadCooldownMin, ent.Comp.SpreadCooldownMax);
|
|
|
|
|
|
ent.Comp.NextSpreadTime = _gameTiming.CurTime + TimeSpan.FromSeconds(cooldown);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Update(frameTime);
|
|
|
|
|
|
|
2024-09-24 13:46:26 +03:00
|
|
|
|
UpdateFireSpread();
|
|
|
|
|
|
UpdateAutoIgnite();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateAutoIgnite()
|
|
|
|
|
|
{
|
|
|
|
|
|
var query = EntityQueryEnumerator<CP14AutoIgniteComponent, FlammableComponent>();
|
|
|
|
|
|
while (query.MoveNext(out var uid, out var autoIgnite, out var flammable))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!autoIgnite.Initialized || !flammable.Initialized)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (autoIgnite.IgniteTime == TimeSpan.Zero)
|
|
|
|
|
|
autoIgnite.IgniteTime = _gameTiming.CurTime + autoIgnite.IgniteDelay;
|
|
|
|
|
|
|
|
|
|
|
|
if (_gameTiming.CurTime < autoIgnite.IgniteTime)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
//Это такой пиздец, что-то сломалось у оффов, что поджигание сущности в момент инициализации (например MapInitEvent) нахрен все крашит.
|
|
|
|
|
|
//Поэтому я добавил 1-секундную задержку перед поджиганием.
|
|
|
|
|
|
|
|
|
|
|
|
_flammable.AdjustFireStacks(uid, autoIgnite.StartStack, flammable, true);
|
|
|
|
|
|
RemCompDeferred<CP14AutoIgniteComponent>(uid);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateFireSpread()
|
|
|
|
|
|
{
|
2024-06-09 15:45:27 +03:00
|
|
|
|
List<Entity<CP14FireSpreadComponent>> spreadUids = new();
|
2024-09-24 13:46:26 +03:00
|
|
|
|
var query = EntityQueryEnumerator<CP14FireSpreadComponent, FlammableComponent, TransformComponent>();
|
|
|
|
|
|
while (query.MoveNext(out var uid, out var spread, out var flammable, out var xform))
|
2024-04-14 18:31:23 +03:00
|
|
|
|
{
|
|
|
|
|
|
if (!flammable.OnFire)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-06-09 15:45:27 +03:00
|
|
|
|
if (spread.NextSpreadTime >= _gameTiming.CurTime)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-09-24 13:46:26 +03:00
|
|
|
|
if (xform.ParentUid != xform.GridUid) //we can't set a fire if we're inside a chest, for example.
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
2024-06-09 15:45:27 +03:00
|
|
|
|
var cooldown = _random.NextFloat(spread.SpreadCooldownMin, spread.SpreadCooldownMax);
|
|
|
|
|
|
spread.NextSpreadTime = _gameTiming.CurTime + TimeSpan.FromSeconds(cooldown);
|
|
|
|
|
|
|
|
|
|
|
|
spreadUids.Add(new Entity<CP14FireSpreadComponent>(uid, spread));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var uid in spreadUids)
|
|
|
|
|
|
{
|
|
|
|
|
|
IgniteEntities(uid, uid.Comp);
|
|
|
|
|
|
IgniteTiles(uid, uid.Comp);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void IgniteEntities(EntityUid uid, CP14FireSpreadComponent spread)
|
|
|
|
|
|
{
|
2024-11-13 17:56:07 +03:00
|
|
|
|
var targets = _lookup.GetEntitiesInRange<FlammableComponent>(_transform.GetMapCoordinates(uid),
|
|
|
|
|
|
spread.Radius,
|
|
|
|
|
|
LookupFlags.Uncontained);
|
2024-06-09 15:45:27 +03:00
|
|
|
|
foreach (var target in targets)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_random.Prob(spread.Prob))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
_flammable.Ignite(target, uid);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-04-14 18:31:23 +03:00
|
|
|
|
|
2024-06-09 15:45:27 +03:00
|
|
|
|
private void IgniteTiles(EntityUid uid, CP14FireSpreadComponent spread)
|
|
|
|
|
|
{
|
|
|
|
|
|
var xform = Transform(uid);
|
|
|
|
|
|
if (!TryComp<MapGridComponent>(xform.GridUid, out var grid))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2024-07-01 20:17:40 +03:00
|
|
|
|
// Ignore items inside containers
|
|
|
|
|
|
if (!HasComp<MapGridComponent>(xform.ParentUid))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2024-06-09 15:45:27 +03:00
|
|
|
|
var localPos = xform.Coordinates.Position;
|
|
|
|
|
|
var tileRefs = _mapSystem.GetLocalTilesIntersecting(grid.Owner,
|
|
|
|
|
|
grid,
|
|
|
|
|
|
new Box2(
|
|
|
|
|
|
localPos + new Vector2(-spread.Radius, -spread.Radius),
|
|
|
|
|
|
localPos + new Vector2(spread.Radius, spread.Radius)))
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var tileref in tileRefs)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_random.Prob(spread.ProbTile))
|
|
|
|
|
|
continue;
|
2024-04-14 18:31:23 +03:00
|
|
|
|
|
2024-06-09 15:45:27 +03:00
|
|
|
|
var tile = tileref.Tile.GetContentTileDefinition();
|
|
|
|
|
|
|
2024-06-27 22:16:05 +03:00
|
|
|
|
if (tile.BurnedTile is null)
|
2024-06-09 15:45:27 +03:00
|
|
|
|
continue;
|
2024-04-14 18:31:23 +03:00
|
|
|
|
|
2024-06-09 15:45:27 +03:00
|
|
|
|
Spawn(_fireProto, _mapSystem.ToCenterCoordinates(tileref, grid));
|
2024-11-13 17:56:07 +03:00
|
|
|
|
_tile.ReplaceTile(tileref, (ContentTileDefinition)_tiledef[tile.BurnedTile]);
|
2024-04-14 18:31:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|