-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
connect_containers.dm
68 lines (56 loc) · 2.53 KB
/
connect_containers.dm
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
/// This component behaves similar to connect_loc_behalf, but it's nested and hooks a signal onto all MOVABLES containing this atom.
/datum/component/connect_containers
dupe_mode = COMPONENT_DUPE_UNIQUE_PASSARGS
/// An assoc list of signal -> procpath to register to the loc this object is on.
var/list/connections
/**
* The atom the component is tracking. The component will delete itself if the tracked is deleted.
* Signals will also be updated whenever it moves.
*/
var/atom/movable/tracked
/datum/component/connect_containers/Initialize(atom/movable/tracked, list/connections)
. = ..()
if (!ismovable(tracked))
return COMPONENT_INCOMPATIBLE
src.connections = connections
set_tracked(tracked)
/datum/component/connect_containers/Destroy()
set_tracked(null)
return ..()
/datum/component/connect_containers/InheritComponent(datum/component/component, original, atom/movable/tracked, list/connections)
// Not equivalent. Checks if they are not the same list via shallow comparison.
if(!compare_list(src.connections, connections))
stack_trace("connect_containers component attached to [parent] tried to inherit another connect_containers component with different connections")
return
if(src.tracked != tracked)
set_tracked(tracked)
/datum/component/connect_containers/proc/set_tracked(atom/movable/new_tracked)
if(tracked)
UnregisterSignal(tracked, list(COMSIG_MOVABLE_MOVED, COMSIG_QDELETING))
unregister_signals(tracked.loc)
tracked = new_tracked
if(!tracked)
return
RegisterSignal(tracked, COMSIG_MOVABLE_MOVED, PROC_REF(on_moved))
RegisterSignal(tracked, COMSIG_QDELETING, PROC_REF(handle_tracked_qdel))
update_signals(tracked)
/datum/component/connect_containers/proc/handle_tracked_qdel()
SIGNAL_HANDLER
qdel(src)
/datum/component/connect_containers/proc/update_signals(atom/movable/listener)
if(!ismovable(listener.loc))
return
for(var/atom/movable/container as anything in get_nested_locs(listener))
RegisterSignal(container, COMSIG_MOVABLE_MOVED, PROC_REF(on_moved))
for(var/signal in connections)
parent.RegisterSignal(container, signal, connections[signal])
/datum/component/connect_containers/proc/unregister_signals(atom/movable/location)
if(!ismovable(location))
return
for(var/atom/movable/target as anything in (get_nested_locs(location) + location))
UnregisterSignal(target, COMSIG_MOVABLE_MOVED)
parent.UnregisterSignal(target, connections)
/datum/component/connect_containers/proc/on_moved(atom/movable/listener, atom/old_loc)
SIGNAL_HANDLER
unregister_signals(old_loc)
update_signals(listener)