-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathpower.dm
422 lines (342 loc) · 11.8 KB
/
power.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
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
//////////////////////////////
// POWER MACHINERY BASE CLASS
//////////////////////////////
/////////////////////////////
// Definitions
/////////////////////////////
/obj/machinery/power
name = null
icon = 'icons/obj/power.dmi'
anchored = TRUE
obj_flags = CAN_BE_HIT | ON_BLUEPRINTS
use_power = NO_POWER_USE
idle_power_usage = 0
active_power_usage = 0
var/image/wire_vision_img //specifically for wirecrawling
/obj/machinery/power/Destroy()
if(wire_vision_img)
qdel(wire_vision_img)
disconnect_from_network()
return ..()
///////////////////////////////
// General procedures
//////////////////////////////
// common helper procs for all power machines
// All power generation handled in add_avail()
// Machines should use add_load(), surplus(), avail()
// Non-machines should use add_delayedload(), delayed_surplus(), newavail()
/obj/machinery/proc/add_avail(amount)
if(powernet)
powernet.newavail += amount
return TRUE
else
return FALSE
/obj/machinery/proc/add_load(amount)
if(powernet)
powernet.load += amount
/obj/machinery/proc/surplus()
if(powernet)
return clamp(powernet.avail-powernet.load, 0, powernet.avail)
else
return 0
/obj/machinery/proc/avail()
if(powernet)
return powernet.avail
else
return 0
/obj/machinery/proc/add_delayedload(amount)
if(powernet)
powernet.delayedload += amount
/obj/machinery/proc/delayed_surplus()
if(powernet)
return clamp(powernet.newavail - powernet.delayedload, 0, powernet.newavail)
else
return 0
/obj/machinery/proc/newavail()
if(powernet)
return powernet.newavail
else
return 0
/obj/machinery/proc/disconnect_terminal() // machines without a terminal will just return, no harm no fowl.
return
// returns true if the area has power on given channel (or doesn't require power).
// defaults to power_channel
/obj/machinery/proc/powered(chan = -1) // defaults to power_channel
if(!loc)
return FALSE
if(!use_power)
return TRUE
var/area/A = get_area(src) // make sure it's in an area
if(!A)
return FALSE // if not, then not powered
if(chan == -1)
chan = power_channel
return A.powered(chan) // return power status of the area
// increment the power usage stats for an area
/obj/machinery/proc/use_power(amount, chan = -1) // defaults to power_channel
var/area/A = get_area(src) // make sure it's in an area
if(!A)
return
if(chan == -1)
chan = power_channel
A.use_power(amount, chan)
/obj/machinery/proc/addStaticPower(value, powerchannel)
var/area/A = get_area(src)
if(!A)
return
A.addStaticPower(value, powerchannel)
/obj/machinery/proc/removeStaticPower(value, powerchannel)
addStaticPower(-value, powerchannel)
/**
* Called whenever the power settings of the containing area change
*
* by default, check equipment channel & set flag, can override if needed
*
* Returns TRUE if the NOPOWER flag was toggled
*/
/obj/machinery/proc/power_change()
//SIGNAL_HANDLER
if(stat & BROKEN)
update_appearance()
return
if(powered(power_channel))
if(stat & NOPOWER)
SEND_SIGNAL(src, COMSIG_MACHINERY_POWER_RESTORED)
. = TRUE
stat &= ~NOPOWER
else
if(!(stat & NOPOWER))
SEND_SIGNAL(src, COMSIG_MACHINERY_POWER_LOST)
. = TRUE
stat |= NOPOWER
update_appearance()
// connect the machine to a powernet if a node cable is present on the turf
/obj/machinery/proc/connect_to_network()
var/turf/T = src.loc
if(!T || !istype(T))
return FALSE
var/obj/structure/cable/C = T.get_cable_node() //check if we have a node cable on the machine turf, the first found is picked
if(!C || !C.powernet)
return FALSE
C.powernet.add_machine(src)
return TRUE
// remove and disconnect the machine from its current powernet
/obj/machinery/proc/disconnect_from_network()
if(!powernet)
return FALSE
powernet.remove_machine(src)
return TRUE
// attach a wire to a power machine - leads from the turf you are standing on
//almost never called, overwritten by all power machines but terminal and generator
/obj/machinery/power/attackby(obj/item/W, mob/user, params)
if(istype(W, /obj/item/stack/cable_coil))
var/obj/item/stack/cable_coil/coil = W
var/turf/T = user.loc
if(T.underfloor_accessibility < UNDERFLOOR_INTERACTABLE || !isfloorturf(T))
return
if(get_dist(src, user) > 1)
return
coil.place_turf(T, user)
else
return ..()
///////////////////////////////////////////
// Powernet handling helpers
//////////////////////////////////////////
//returns all the cables WITHOUT a powernet in neighbors turfs,
//pointing towards the turf the machine is located at
/obj/machinery/proc/get_connections()
. = list()
var/cdir
var/turf/T
for(var/card in GLOB.cardinals)
T = get_step(loc,card)
cdir = get_dir(T,loc)
for(var/obj/structure/cable/C in T)
if(C.powernet)
continue
if(C.d1 == cdir || C.d2 == cdir)
. += C
return .
//returns all the cables in neighbors turfs,
//pointing towards the turf the machine is located at
/obj/machinery/proc/get_marked_connections()
. = list()
var/cdir
var/turf/T
for(var/card in GLOB.cardinals)
T = get_step(loc,card)
cdir = get_dir(T,loc)
for(var/obj/structure/cable/C in T)
if(C.d1 == cdir || C.d2 == cdir)
. += C
return .
//returns all the NODES (O-X) cables WITHOUT a powernet in the turf the machine is located at
/obj/machinery/proc/get_indirect_connections()
. = list()
for(var/obj/structure/cable/C in loc)
if(C.powernet)
continue
if(C.d1 == 0) // the cable is a node cable
. += C
return .
///////////////////////////////////////////
// GLOBAL PROCS for powernets handling
//////////////////////////////////////////
// returns a list of all power-related objects (nodes, cable, junctions) in turf,
// excluding source, that match the direction d
// if unmarked==1, only return those with no powernet
/proc/power_list(turf/T, source, d, unmarked=0, cable_only = 0)
. = list()
for(var/AM in T)
if(AM == source)
continue //we don't want to return source
if(!cable_only && istype(AM, /obj/machinery))
var/obj/machinery/P = AM
if(P.powernet == 0)
continue // exclude APCs which have powernet=0
if(!unmarked || !P.powernet) //if unmarked=1 we only return things with no powernet
if(d == 0)
. += P
else if(istype(AM, /obj/structure/cable))
var/obj/structure/cable/C = AM
if(!unmarked || !C.powernet)
if(C.d1 == d || C.d2 == d)
. += C
return .
//remove the old powernet and replace it with a new one throughout the network.
/proc/propagate_network(obj/O, datum/powernet/PN)
var/list/worklist = list()
var/list/found_machines = list()
var/index = 1
var/obj/P = null
worklist+=O //start propagating from the passed object
while(index<=worklist.len) //until we've exhausted all power objects
P = worklist[index] //get the next power object found
index++
if( istype(P, /obj/structure/cable))
var/obj/structure/cable/C = P
if(C.powernet != PN) //add it to the powernet, if it isn't already there
PN.add_cable(C)
worklist |= C.get_connections() //get adjacents power objects, with or without a powernet
else if(P.anchored && istype(P, /obj/machinery))
var/obj/machinery/M = P
found_machines |= M //we wait until the powernet is fully propagates to connect the machines
else
continue
//now that the powernet is set, connect found machines to it
for(var/obj/machinery/PM in found_machines)
if(!PM.connect_to_network()) //couldn't find a node on its turf...
PM.disconnect_from_network() //... so disconnect if already on a powernet
//Merge two powernets, the bigger (in cable length term) absorbing the other
/proc/merge_powernets(datum/powernet/net1, datum/powernet/net2)
if(!net1 || !net2) //if one of the powernet doesn't exist, return
return
if(net1 == net2) //don't merge same powernets
return
//We assume net1 is larger. If net2 is in fact larger we are just going to make them switch places to reduce on code.
if(net1.cables.len < net2.cables.len) //net2 is larger than net1. Let's switch them around
var/temp = net1
net1 = net2
net2 = temp
//merge net2 into net1
for(var/obj/structure/cable/Cable in net2.cables) //merge cables
net1.add_cable(Cable)
for(var/obj/machinery/Node in net2.nodes) //merge power machines
if(!Node.connect_to_network())
Node.disconnect_from_network() //if somehow we can't connect the machine to the new powernet, disconnect it from the old nonetheless
return net1
/// Extracts the powernet and cell of the provided power source
/proc/get_powernet_info_from_source(power_source)
var/area/source_area
if(isarea(power_source))
source_area = power_source
power_source = source_area.get_apc()
else if(istype(power_source, /obj/structure/cable))
var/obj/structure/cable/Cable = power_source
power_source = Cable.powernet
var/datum/powernet/PN
var/obj/item/stock_parts/cell/cell
if(istype(power_source, /datum/powernet))
PN = power_source
else if(istype(power_source, /obj/item/stock_parts/cell))
cell = power_source
else if(istype(power_source, /obj/machinery/power/apc))
var/obj/machinery/power/apc/apc = power_source
cell = apc.cell
if(apc.terminal)
PN = apc.terminal.powernet
else
return FALSE
if(!cell && !PN)
return FALSE
return list("powernet" = PN, "cell" = cell)
//Determines how strong could be shock, deals damage to mob, uses power.
//M is a mob who touched wire/whatever
//power_source is a source of electricity, can be power cell, area, apc, cable, powernet or null
//source is an object caused electrocuting (airlock, grille, etc)
//siemens_coeff - layman's terms, conductivity
//dist_check - set to only shock mobs within 1 of source (vendors, airlocks, etc.)
//zone_override - allows checking a specific body part for shock protection instead of the hands
//No animations will be performed by this proc.
/proc/electrocute_mob(mob/living/carbon/victim, power_source, obj/source, siemens_coeff = 1, dist_check = FALSE, zone = HANDS)
if(!istype(victim) || ismecha(victim.loc))
return FALSE //feckin mechs are dumb
if(dist_check)
if(!in_range(source, victim))
return FALSE
if(victim.getarmor(zone, ELECTRIC) >= 100)
SEND_SIGNAL(victim, COMSIG_LIVING_SHOCK_PREVENTED, power_source, source, siemens_coeff, dist_check)
return FALSE //to avoid spamming with insulated glvoes on
var/list/powernet_info = get_powernet_info_from_source(power_source)
if (!powernet_info)
return FALSE
var/datum/powernet/PN = powernet_info["powernet"]
var/obj/item/stock_parts/cell/cell = powernet_info["cell"]
var/PN_damage = 0
var/cell_damage = 0
if(PN)
PN_damage = PN.get_electrocute_damage()
if(cell)
cell_damage = cell.get_electrocute_damage()
var/shock_damage = 0
if(PN_damage >= cell_damage)
power_source = PN
shock_damage = PN_damage
else
power_source = cell
shock_damage = cell_damage
var/drained_hp = victim.electrocute_act(shock_damage, source, siemens_coeff) //zzzzzzap!
log_combat(source, victim, "electrocuted")
var/drained_energy = drained_hp*20
if (isarea(power_source))
var/area/source_area = power_source
source_area.use_power(drained_energy/GLOB.CELLRATE)
else if (istype(power_source, /datum/powernet))
var/drained_power = drained_energy/GLOB.CELLRATE //convert from "joules" to "watts"
PN.delayedload += (min(drained_power, max(PN.newavail - PN.delayedload, 0)))
else if (istype(power_source, /obj/item/stock_parts/cell))
cell.use(drained_energy)
return drained_energy
////////////////////////////////////////////////
// Misc.
///////////////////////////////////////////////
// return a knot cable (O-X) if one is present in the turf
// null if there's none
/turf/proc/get_cable_node()
if(!can_have_cabling())
return null
for(var/obj/structure/cable/C in src)
if(C.d1 == 0)
return C
return null
/turf/proc/get_ai_cable_node()
if(!can_have_cabling())
return null
for(var/obj/structure/ethernet_cable/C in src)
if(C.d1 == 0)
return C
return null
/area/proc/get_apc()
for(var/obj/machinery/power/apc/APC in GLOB.apcs_list)
if(APC.area == src)
return APC