@@ -33,6 +33,8 @@ public sealed class SpreaderSystem : EntitySystem
33
33
// TODO PERFORMANCE Assign each prototype to an index and convert dictionary to array
34
34
private readonly Dictionary < EntityUid , Dictionary < string , int > > _gridUpdates = [ ] ;
35
35
36
+ private EntityQuery < EdgeSpreaderComponent > _query ;
37
+
36
38
public const float SpreadCooldownSeconds = 1 ;
37
39
38
40
[ ValidatePrototypeId < TagPrototype > ]
@@ -47,6 +49,8 @@ public override void Initialize()
47
49
48
50
SubscribeLocalEvent < EdgeSpreaderComponent , EntityTerminatingEvent > ( OnTerminating ) ;
49
51
SetupPrototypes ( ) ;
52
+
53
+ _query = GetEntityQuery < EdgeSpreaderComponent > ( ) ;
50
54
}
51
55
52
56
private void OnPrototypeReload ( PrototypesReloadedEventArgs obj )
@@ -66,7 +70,7 @@ private void SetupPrototypes()
66
70
67
71
private void OnAirtightChanged ( ref AirtightChanged ev )
68
72
{
69
- ActivateGetSpreadableNeighbors ( ev . Entity , ev . Airtight , ev . Position ) ;
73
+ ActivateSpreadableNeighbors ( ev . Entity , ev . Position ) ;
70
74
}
71
75
72
76
private void OnGridInit ( GridInitializeEvent ev )
@@ -76,13 +80,7 @@ private void OnGridInit(GridInitializeEvent ev)
76
80
77
81
private void OnTerminating ( Entity < EdgeSpreaderComponent > entity , ref EntityTerminatingEvent args )
78
82
{
79
- var neighbors = GetSpreadableNeighbors ( entity ) ;
80
-
81
- foreach ( var neighbor in neighbors )
82
- {
83
- if ( ! TerminatingOrDeleted ( neighbor ) )
84
- EnsureComp < ActiveEdgeSpreaderComponent > ( neighbor ) ;
85
- }
83
+ ActivateSpreadableNeighbors ( entity ) ;
86
84
}
87
85
88
86
/// <inheritdoc/>
@@ -248,8 +246,7 @@ public void GetNeighbors(EntityUid uid, TransformComponent comp, ProtoId<EdgeSpr
248
246
if ( ! _map . TryGetTileRef ( neighborEnt , neighborGrid , neighborPos , out var tileRef ) || tileRef . Tile . IsEmpty )
249
247
continue ;
250
248
251
- var directionEnumerator =
252
- _map . GetAnchoredEntitiesEnumerator ( neighborEnt , neighborGrid , neighborPos ) ;
249
+ var directionEnumerator = _map . GetAnchoredEntitiesEnumerator ( neighborEnt , neighborGrid , neighborPos ) ;
253
250
var occupied = false ;
254
251
255
252
while ( directionEnumerator . MoveNext ( out var ent ) )
@@ -271,8 +268,7 @@ public void GetNeighbors(EntityUid uid, TransformComponent comp, ProtoId<EdgeSpr
271
268
continue ;
272
269
273
270
var oldCount = occupiedTiles . Count ;
274
- directionEnumerator =
275
- _map . GetAnchoredEntitiesEnumerator ( neighborEnt , neighborGrid , neighborPos ) ;
271
+ directionEnumerator = _map . GetAnchoredEntitiesEnumerator ( neighborEnt , neighborGrid , neighborPos ) ;
276
272
277
273
while ( directionEnumerator . MoveNext ( out var ent ) )
278
274
{
@@ -293,13 +289,11 @@ public void GetNeighbors(EntityUid uid, TransformComponent comp, ProtoId<EdgeSpr
293
289
}
294
290
295
291
/// <summary>
296
- /// Given an entity, this returns a list of all adjacent entities with a <see cref="EdgeSpreaderComponent"/>.
292
+ /// This function activates all spreaders that are adjacent to a given entity. This also activates other spreaders
293
+ /// on the same tile as the current entity (for thin airtight entities like windoors).
297
294
/// </summary>
298
- public void ActivateGetSpreadableNeighbors ( EntityUid uid , AirtightComponent ? comp = null ,
299
- ( EntityUid Grid , Vector2i Tile ) ? position = null )
295
+ public void ActivateSpreadableNeighbors ( EntityUid uid , ( EntityUid Grid , Vector2i Tile ) ? position = null )
300
296
{
301
- Resolve ( uid , ref comp , false ) ;
302
-
303
297
Vector2i tile ;
304
298
EntityUid ent ;
305
299
MapGridComponent ? grid ;
@@ -308,34 +302,39 @@ public void ActivateGetSpreadableNeighbors(EntityUid uid, AirtightComponent? com
308
302
{
309
303
var transform = Transform ( uid ) ;
310
304
if ( ! TryComp ( transform . GridUid , out grid ) || TerminatingOrDeleted ( transform . GridUid . Value ) )
311
- return neighbors ;
305
+ return ;
306
+
312
307
tile = _map . TileIndicesFor ( transform . GridUid . Value , grid , transform . Coordinates ) ;
313
308
ent = transform . GridUid . Value ;
314
309
}
315
310
else
316
311
{
317
312
if ( ! TryComp ( position . Value . Grid , out grid ) )
318
- return neighbors ;
319
- tile = position . Value . Tile ;
320
- ent = position . Value . Grid ;
313
+ return ;
314
+ ( ent , tile ) = position . Value ;
321
315
}
322
316
323
- var spreaderQuery = GetEntityQuery < EdgeSpreaderComponent > ( ) ;
317
+ var anchored = _map . GetAnchoredEntitiesEnumerator ( ent , grid , tile ) ;
318
+ while ( anchored . MoveNext ( out var entity ) )
319
+ {
320
+ if ( entity == ent )
321
+ continue ;
322
+ DebugTools . Assert ( Transform ( entity . Value ) . Anchored ) ;
323
+ if ( _query . HasComponent ( ent ) && ! TerminatingOrDeleted ( entity . Value ) )
324
+ EnsureComp < ActiveEdgeSpreaderComponent > ( entity . Value ) ;
325
+ }
324
326
325
327
for ( var i = 0 ; i < Atmospherics . Directions ; i ++ )
326
328
{
327
329
var direction = ( AtmosDirection ) ( 1 << i ) ;
328
- if ( comp != null && ! comp . AirBlockedDirection . IsFlagSet ( direction ) )
329
- continue ;
330
-
331
- var directionEnumerator =
332
- _map . GetAnchoredEntitiesEnumerator ( ent , grid , SharedMapSystem . GetDirection ( tile , direction . ToDirection ( ) ) ) ;
330
+ var adjacentTile = SharedMapSystem . GetDirection ( tile , direction . ToDirection ( ) ) ;
331
+ anchored = _map . GetAnchoredEntitiesEnumerator ( ent , grid , adjacentTile ) ;
333
332
334
- while ( directionEnumerator . MoveNext ( out var entity ) )
333
+ while ( anchored . MoveNext ( out var entity ) )
335
334
{
336
335
DebugTools . Assert ( Transform ( entity . Value ) . Anchored ) ;
337
- if ( spreaderQuery . HasComponent ( entity ) && ! TerminatingOrDeleted ( entity . Value ) )
338
- neighbors . Add ( entity . Value ) ;
336
+ if ( _query . HasComponent ( ent ) && ! TerminatingOrDeleted ( entity . Value ) )
337
+ EnsureComp < ActiveEdgeSpreaderComponent > ( entity . Value ) ;
339
338
}
340
339
}
341
340
}
0 commit comments