-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSharedEntityStorageSystem.cs
More file actions
500 lines (405 loc) · 17.8 KB
/
SharedEntityStorageSystem.cs
File metadata and controls
500 lines (405 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
using System.Linq;
using System.Numerics;
using Content.Shared.Destructible;
using Content.Shared.Foldable;
using Content.Shared.Hands.Components;
using Content.Shared.Explosion;
using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Lock;
using Content.Shared.Movement.Events;
using Content.Shared.Popups;
using Content.Shared.Storage.Components;
using Content.Shared.Tools.Systems;
using Content.Shared.Verbs;
using Content.Shared.Wall;
using Content.Shared.Whitelist;
using Content.Shared.ActionBlocker;
using Content.Shared.Mobs.Components;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Shared.Storage.EntitySystems;
public abstract partial class SharedEntityStorageSystem : EntitySystem
{
[Dependency] private IGameTiming _timing = default!;
[Dependency] private INetManager _net = default!;
[Dependency] private EntityLookupSystem _lookup = default!;
[Dependency] private SharedAppearanceSystem _appearance = default!;
[Dependency] private SharedAudioSystem _audio = default!;
[Dependency] private SharedContainerSystem _container = default!;
[Dependency] private SharedInteractionSystem _interaction = default!;
[Dependency] private SharedJointSystem _joints = default!;
[Dependency] private SharedPhysicsSystem _physics = default!;
[Dependency] protected SharedPopupSystem Popup = default!;
[Dependency] protected SharedTransformSystem TransformSystem = default!;
[Dependency] private WeldableSystem _weldable = default!;
[Dependency] private EntityWhitelistSystem _whitelistSystem = default!;
[Dependency] private ActionBlockerSystem _actionBlocker = default!;
public const string ContainerName = "entity_storage";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EntityStorageComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<EntityStorageComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<EntityStorageComponent, ActivateInWorldEvent>(OnInteract, after: new[] { typeof(LockSystem) });
SubscribeLocalEvent<EntityStorageComponent, LockToggleAttemptEvent>(OnLockToggleAttempt);
SubscribeLocalEvent<EntityStorageComponent, DestructionEventArgs>(OnDestruction);
SubscribeLocalEvent<EntityStorageComponent, GetVerbsEvent<InteractionVerb>>(AddToggleOpenVerb);
SubscribeLocalEvent<EntityStorageComponent, ContainerRelayMovementEntityEvent>(OnRelayMovement);
SubscribeLocalEvent<EntityStorageComponent, FoldAttemptEvent>(OnFoldAttempt);
SubscribeLocalEvent<EntityStorageComponent, WeldableAttemptEvent>(OnWeldableAttempt);
SubscribeLocalEvent<EntityStorageComponent, BeforeExplodeEvent>(OnExploded);
SubscribeLocalEvent<InsideEntityStorageComponent, EntGotRemovedFromContainerMessage>(OnRemoved);
}
protected virtual void OnComponentInit(EntityUid uid, EntityStorageComponent component, ComponentInit args)
{
component.Contents = _container.EnsureContainer<Container>(uid, ContainerName);
component.Contents.ShowContents = component.ShowContents;
component.Contents.OccludesLight = component.OccludesLight;
}
private void OnComponentStartup(EntityUid uid, EntityStorageComponent component, ComponentStartup args)
{
_appearance.SetData(uid, StorageVisuals.Open, component.Open);
}
private void OnInteract(EntityUid uid, EntityStorageComponent component, ActivateInWorldEvent args)
{
if (args.Handled || !args.Complex)
return;
args.Handled = true;
ToggleOpen(args.User, uid, component);
}
private void OnLockToggleAttempt(EntityUid uid, EntityStorageComponent target, ref LockToggleAttemptEvent args)
{
// Cannot (un)lock open lockers.
if (target.Open)
args.Cancelled = true;
// Cannot (un)lock from the inside. Maybe a bad idea? Security jocks could trap nerds in lockers?
if (target.Contents.Contains(args.User))
args.Cancelled = true;
}
private void OnDestruction(EntityUid uid, EntityStorageComponent component, DestructionEventArgs args)
{
component.Open = true;
Dirty(uid, component);
if (!component.DeleteContentsOnDestruction)
{
EmptyContents(uid, component);
return;
}
foreach (var ent in new List<EntityUid>(component.Contents.ContainedEntities))
{
Del(ent);
}
}
private void OnRelayMovement(EntityUid uid, EntityStorageComponent component, ref ContainerRelayMovementEntityEvent args)
{
if (!HasComp<HandsComponent>(args.Entity))
return;
if (!_actionBlocker.CanMove(args.Entity))
return;
if (_timing.CurTime < component.NextInternalOpenAttempt)
return;
component.NextInternalOpenAttempt = _timing.CurTime + component.InternalOpenAttemptDelay;
Dirty(uid, component);
if (component.OpenOnMove)
TryOpenStorage(args.Entity, uid);
}
private void OnFoldAttempt(EntityUid uid, EntityStorageComponent component, ref FoldAttemptEvent args)
{
if (args.Cancelled)
return;
args.Cancelled = component.Open || component.Contents.ContainedEntities.Count != 0;
}
private void OnWeldableAttempt(EntityUid uid, EntityStorageComponent component, WeldableAttemptEvent args)
{
if (component.Open)
{
args.Cancel();
return;
}
if (component.Contents.Contains(args.User))
{
var msg = Loc.GetString("entity-storage-component-already-contains-user-message");
Popup.PopupEntity(msg, args.User, args.User);
args.Cancel();
}
}
private void OnExploded(Entity<EntityStorageComponent> ent, ref BeforeExplodeEvent args)
{
args.Contents.AddRange(ent.Comp.Contents.ContainedEntities);
}
private void OnRemoved(EntityUid uid, InsideEntityStorageComponent component, EntGotRemovedFromContainerMessage args)
{
if (_timing.ApplyingState)
return; // the component removal is already networked on its own
if (args.Container.Owner != component.Storage)
return;
RemComp(uid, component);
}
private void AddToggleOpenVerb(EntityUid uid, EntityStorageComponent component, GetVerbsEvent<InteractionVerb> args)
{
if (!args.CanAccess || !args.CanInteract)
return;
if (!CanOpen(args.User, args.Target, silent: true, component))
return;
InteractionVerb verb = new();
if (component.Open)
{
verb.Text = Loc.GetString("verb-common-close");
verb.Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/close.svg.192dpi.png"));
}
else
{
verb.Text = Loc.GetString("verb-common-open");
verb.Icon = new SpriteSpecifier.Texture(
new("/Textures/Interface/VerbIcons/open.svg.192dpi.png"));
}
verb.Act = () => ToggleOpen(args.User, args.Target, component);
args.Verbs.Add(verb);
}
public void ToggleOpen(EntityUid user, EntityUid target, EntityStorageComponent? component = null)
{
if (!Resolve(target, ref component))
return;
if (component.Open)
{
TryCloseStorage(target, user);
}
else
{
TryOpenStorage(user, target);
}
}
public void EmptyContents(EntityUid uid, EntityStorageComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
var uidXform = Transform(uid);
var containedArr = component.Contents.ContainedEntities.ToArray();
foreach (var contained in containedArr)
{
Remove(contained, uid, component, uidXform);
}
}
public void OpenStorage(EntityUid uid, EntityStorageComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (component.Open)
return;
var beforeev = new StorageBeforeOpenEvent();
RaiseLocalEvent(uid, ref beforeev);
component.Open = true;
Dirty(uid, component);
EmptyContents(uid, component);
ModifyComponents(uid, component);
if (_net.IsClient && _timing.IsFirstTimePredicted)
_audio.PlayPvs(component.OpenSound, uid);
ReleaseGas(uid, component);
var afterev = new StorageAfterOpenEvent();
RaiseLocalEvent(uid, ref afterev);
}
public void CloseStorage(EntityUid uid, EntityStorageComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (!component.Open)
return;
// Prevent the container from closing if it is queued for deletion. This is so that the container-emptying
// behaviour of DestructionEventArgs is respected. This exists because malicious players were using
// destructible boxes to delete entities by having two players simultaneously destroy and close the box in
// the same tick.
if (EntityManager.IsQueuedForDeletion(uid))
return;
component.Open = false;
Dirty(uid, component);
var entities = _lookup.GetEntitiesInRange(
new EntityCoordinates(uid, component.EnteringOffset),
component.EnteringRange,
LookupFlags.Approximate | LookupFlags.Dynamic | LookupFlags.Sundries
);
// Don't insert the container into itself.
entities.Remove(uid);
var ev = new StorageBeforeCloseEvent(entities, []);
RaiseLocalEvent(uid, ref ev);
foreach (var entity in ev.Contents)
{
if (!ev.BypassChecks.Contains(entity) && !CanInsert(entity, uid, component))
continue;
if (!AddToContents(entity, uid, component))
continue;
if (component.Contents.ContainedEntities.Count >= component.Capacity)
break;
}
TakeGas(uid, component);
ModifyComponents(uid, component);
if (_net.IsClient && _timing.IsFirstTimePredicted)
_audio.PlayPvs(component.CloseSound, uid);
var afterev = new StorageAfterCloseEvent();
RaiseLocalEvent(uid, ref afterev);
}
public bool Insert(EntityUid toInsert, EntityUid container, EntityStorageComponent? component = null)
{
if (!Resolve(container, ref component))
return false;
if (component.Open)
{
TransformSystem.DropNextTo(toInsert, container);
return true;
}
// TODO: This should be done automatically for all containers
_joints.RecursiveClearJoints(toInsert);
if (!_container.Insert(toInsert, component.Contents))
return false;
var inside = EnsureComp<InsideEntityStorageComponent>(toInsert);
inside.Storage = container;
Dirty(toInsert, inside);
return true;
}
public bool Remove(EntityUid toRemove, EntityUid container, EntityStorageComponent? component = null, TransformComponent? xform = null)
{
if (!Resolve(container, ref xform, false))
return false;
if (!Resolve(container, ref component))
return false;
_container.Remove(toRemove, component.Contents);
if (_container.IsEntityInContainer(container)
&& _container.TryGetOuterContainer(container, Transform(container), out var outerContainer))
{
var attemptEvent = new EntityStorageIntoContainerAttemptEvent(outerContainer);
RaiseLocalEvent(outerContainer.Owner, ref attemptEvent);
if (!attemptEvent.Cancelled)
{
_container.Insert(toRemove, outerContainer);
return true;
}
}
RemComp<InsideEntityStorageComponent>(toRemove);
var pos = TransformSystem.GetWorldPosition(xform) + TransformSystem.GetWorldRotation(xform).RotateVec(component.EnteringOffset);
TransformSystem.SetWorldPosition(toRemove, pos);
return true;
}
public bool CanInsert(EntityUid toInsert, EntityUid container, EntityStorageComponent? component = null)
{
if (!Resolve(container, ref component))
return false;
if (component.Open)
return true;
if (component.Contents.ContainedEntities.Count >= component.Capacity)
return false;
var aabb = _lookup.GetAABBNoContainer(toInsert, Vector2.Zero, 0);
if (component.MaxSize < aabb.Size.X || component.MaxSize < aabb.Size.Y)
return false;
// Allow other systems to prevent inserting the item: e.g. the item is actually a ghost.
var attemptEvent = new InsertIntoEntityStorageAttemptEvent(component.Contents, toInsert);
RaiseLocalEvent(toInsert, ref attemptEvent);
if (attemptEvent.Cancelled)
return false;
// Allow other components on the container to prevent inserting the item: e.g. the container is folded
var containerAttemptEvent = new EntityStorageInsertedIntoAttemptEvent(component.Contents, toInsert);
RaiseLocalEvent(container, ref containerAttemptEvent);
if (containerAttemptEvent.Cancelled)
return false;
// Check the whitelist/blacklist.
return _whitelistSystem.CheckBoth(toInsert, component.Blacklist, component.Whitelist);
}
public bool TryOpenStorage(EntityUid user, EntityUid target, bool silent = false)
{
if (!CanOpen(user, target, silent))
return false;
OpenStorage(target);
return true;
}
public bool TryCloseStorage(EntityUid target, EntityUid? user = null)
{
if (!CanClose(target, user))
{
return false;
}
CloseStorage(target);
return true;
}
public bool IsOpen(EntityUid target, EntityStorageComponent? component = null)
{
if (!Resolve(target, ref component))
return false;
return component.Open;
}
public bool CanOpen(EntityUid user, EntityUid target, bool silent = false, EntityStorageComponent? component = null)
{
if (!Resolve(target, ref component, false)) // Goobstation edit - suppress error
return false;
if (!HasComp<HandsComponent>(user))
return false;
if (_weldable.IsWelded(target))
{
if (!silent && !component.Contents.Contains(user))
Popup.PopupClient(Loc.GetString("entity-storage-component-welded-shut-message"), target, user);
return false;
}
//Checks to see if the opening position, if offset, is inside of a wall.
if (component.EnteringOffset != new Vector2(0, 0) && !HasComp<WallMountComponent>(target)) //if the entering position is offset
{
var newCoords = new EntityCoordinates(target, component.EnteringOffset);
if (!_interaction.InRangeUnobstructed(target, newCoords, 0, collisionMask: component.EnteringOffsetCollisionFlags))
{
if (!silent && _net.IsServer)
Popup.PopupEntity(Loc.GetString("entity-storage-component-cannot-open-no-space"), target);
return false;
}
}
var ev = new StorageOpenAttemptEvent(user, silent);
RaiseLocalEvent(target, ref ev, true);
return !ev.Cancelled;
}
public bool CanClose(EntityUid target, EntityUid? user = null, bool silent = false)
{
var ev = new StorageCloseAttemptEvent(user);
RaiseLocalEvent(target, ref ev, silent);
return !ev.Cancelled;
}
public bool AddToContents(EntityUid toAdd, EntityUid container, EntityStorageComponent? component = null)
{
if (!Resolve(container, ref component))
return false;
if (toAdd == container)
return false;
return Insert(toAdd, container, component);
}
private void ModifyComponents(EntityUid uid, EntityStorageComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (!component.IsCollidableWhenOpen && TryComp<FixturesComponent>(uid, out var fixtures) &&
fixtures.Fixtures.Count > 0)
{
// currently only works for single-fixture entities. If they have more than one fixture, then
// RemovedMasks needs to be tracked separately for each fixture, using a fixture Id Dictionary. Also the
// fixture IDs probably cant be automatically generated without causing issues, unless there is some
// guarantee that they will get deserialized with the same auto-generated ID when saving+loading the map.
var fixture = fixtures.Fixtures.First();
if (component.Open)
{
component.RemovedMasks = fixture.Value.CollisionLayer & component.MasksToRemove;
_physics.SetCollisionLayer(uid, fixture.Key, fixture.Value, fixture.Value.CollisionLayer & ~component.MasksToRemove,
manager: fixtures);
}
else
{
_physics.SetCollisionLayer(uid, fixture.Key, fixture.Value, fixture.Value.CollisionLayer | component.RemovedMasks,
manager: fixtures);
component.RemovedMasks = 0;
}
}
_appearance.SetData(uid, StorageVisuals.Open, component.Open);
_appearance.SetData(uid, StorageVisuals.HasContents, component.Contents.ContainedEntities.Count > 0);
}
protected virtual void TakeGas(EntityUid uid, EntityStorageComponent component) { }
public virtual void ReleaseGas(EntityUid uid, EntityStorageComponent component) { }
}