-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathmachines.dm
181 lines (155 loc) · 6.04 KB
/
machines.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
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
SUBSYSTEM_DEF(machines)
name = "Machines"
init_order = INIT_ORDER_MACHINES
flags = SS_KEEP_TIMING
wait = 2 SECONDS
var/list/processing = list()
var/list/currentrun = list()
var/list/powernets = list()
var/list/ainets = list()
/// Assosciative list of all machines that exist.
VAR_PRIVATE/list/machines_by_type = list()
/// All machines, not just those that are processing.
VAR_PRIVATE/list/all_machines = list()
///List of all powernets on the server.
/datum/controller/subsystem/machines/Initialize()
makepowernets()
makeainets()
fire()
return SS_INIT_SUCCESS
/// Registers a machine with the machine subsystem; should only be called by the machine itself during its creation.
/datum/controller/subsystem/machines/proc/register_machine(obj/machinery/machine)
LAZYADD(machines_by_type[machine.type], machine)
all_machines |= machine
for(var/obj/structure/cable/PC in GLOB.cable_list)
if(!PC.powernet)
var/datum/powernet/NewPN = new(PC.loc.z)
NewPN.add_cable(PC)
propagate_network(PC,PC.powernet)
/datum/controller/subsystem/machines/proc/makeainets()
for(var/datum/ai_network/AN in ainets)
qdel(AN)
ainets.Cut()
for(var/obj/structure/ethernet_cable/EC in GLOB.ethernet_cable_list)
if(!EC.network)
var/datum/ai_network/NewAN = new()
NewAN.add_cable(EC)
propagate_ai_network(EC,EC.network)
for(var/obj/machinery/ai/networking/N in GLOB.ai_networking_machines)
N.roundstart_connect()
/datum/controller/subsystem/machines/stat_entry(msg)
msg = "M:[length(processing)]|PN:[length(powernets)]|AN:[length(ainets)]"
return ..()
/datum/controller/subsystem/machines/get_metrics()
. = ..()
.["machines"] = length(processing)
.["powernets"] = length(powernets)
/datum/controller/subsystem/machines/fire(resumed = 0)
if (!resumed)
for(var/datum/powernet/Powernet in powernets)
Powernet.reset() //reset the power state.
src.currentrun = processing.Copy()
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/obj/machinery/thing = currentrun[currentrun.len]
currentrun.len--
if(!QDELETED(thing) && thing.process(wait * 0.1) != PROCESS_KILL)
if(thing.use_power)
thing.auto_use_power() //add back the power state
else
processing -= thing
if (!QDELETED(thing))
thing.datum_flags &= ~DF_ISPROCESSING
if (MC_TICK_CHECK)
return
/datum/controller/subsystem/machines/proc/setup_template_powernets(list/cables)
for(var/A in cables)
var/obj/structure/cable/PC = A
if(!PC.powernet)
var/datum/powernet/NewPN = new(PC.loc.z)
NewPN.add_cable(PC)
propagate_network(PC,PC.powernet)
/datum/controller/subsystem/machines/proc/setup_template_ainets(list/cables)
for(var/A in cables)
var/obj/structure/ethernet_cable/PC = A
if(!PC.network)
var/datum/ai_network/NewPN = new()
NewPN.add_cable(PC)
propagate_ai_network(PC,PC.network)
/datum/controller/subsystem/machines/Recover()
if (istype(SSmachines.processing))
processing = SSmachines.processing
if (istype(SSmachines.powernets))
powernets = SSmachines.powernets
if (istype(SSmachines.ainets))
ainets = SSmachines.ainets
/// Removes a machine from the machine subsystem; should only be called by the machine itself inside Destroy.
/datum/controller/subsystem/machines/proc/unregister_machine(obj/machinery/machine)
var/list/existing = machines_by_type[machine.type]
existing -= machine
if(!length(existing))
machines_by_type -= machine.type
all_machines -= machine
/// Gets a list of all machines that are either the passed type or a subtype.
/datum/controller/subsystem/machines/proc/get_machines_by_type_and_subtypes(obj/machinery/machine_type)
if(!ispath(machine_type))
machine_type = machine_type.type
if(!ispath(machine_type, /obj/machinery))
CRASH("called get_machines_by_type_and_subtypes with a non-machine type [machine_type]")
var/list/machines = list()
for(var/next_type in typesof(machine_type))
var/list/found_machines = machines_by_type[next_type]
if(found_machines)
machines += found_machines
return machines
/// Gets a list of all machines that are the exact passed type.
/datum/controller/subsystem/machines/proc/get_machines_by_type(obj/machinery/machine_type)
if(!ispath(machine_type))
machine_type = machine_type.type
if(!ispath(machine_type, /obj/machinery))
CRASH("called get_machines_by_type with a non-machine type [machine_type]")
var/list/machines = machines_by_type[machine_type]
return machines?.Copy() || list()
/datum/controller/subsystem/machines/proc/get_all_machines()
return all_machines.Copy()
/datum/controller/subsystem/machines/proc/makepowernets()
for(var/datum/powernet/power_network as anything in powernets)
qdel(power_network)
powernets.Cut()
for(var/obj/structure/cable/power_cable as anything in GLOB.cable_list)
if(!power_cable.powernet)
var/datum/powernet/new_powernet = new()
new_powernet.add_cable(power_cable)
propagate_network(power_cable, power_cable.powernet)
/datum/controller/subsystem/machines/stat_entry(msg)
msg = "M:[length(processing)]|PN:[length(powernets)]"
return ..()
/datum/controller/subsystem/machines/get_metrics()
. = ..()
.["machines"] = length(processing)
.["powernets"] = length(powernets)
/datum/controller/subsystem/machines/fire(resumed = FALSE)
if (!resumed)
for(var/datum/powernet/powernet as anything in powernets)
powernet.reset() //reset the power state.
src.currentrun = processing.Copy()
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/obj/machinery/thing = currentrun[currentrun.len]
currentrun.len--
if(QDELETED(thing) || thing.process(wait * 0.1) == PROCESS_KILL)
processing -= thing
thing.datum_flags &= ~DF_ISPROCESSING
if (MC_TICK_CHECK)
return
/datum/controller/subsystem/machines/Recover()
if(islist(SSmachines.processing))
processing = SSmachines.processing
if(islist(SSmachines.powernets))
powernets = SSmachines.powernets
if(islist(SSmachines.all_machines))
all_machines = SSmachines.all_machines
if(islist(SSmachines.machines_by_type))
machines_by_type = SSmachines.machines_by_type