-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
smes.dm
293 lines (234 loc) · 7.65 KB
/
smes.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
// the SMES
// stores power
#define SMESMAXCHARGELEVEL 200000
#define SMESMAXOUTPUT 200000
/obj/machinery/power/smes/magical
name = "magical power storage unit"
desc = "A high-capacity superconducting magnetic energy storage (SMES) unit. Magically produces power, using magic."
process()
capacity = INFINITY
charge = INFINITY
..()
/obj/machinery/power/smes
name = "power storage unit"
desc = "A high-capacity superconducting magnetic energy storage (SMES) unit."
icon_state = "smes"
density = 1
anchored = 1
requires_power = FALSE
var/output = 30000
var/lastout = 0
var/loaddemand = 0
var/capacity = 1e8
var/charge = 2e7
var/charging = 0
var/chargemode = 1
var/chargecount = 0
var/chargelevel = 30000
var/lastexcess = 0
var/online = 1
var/n_tag = null
var/obj/machinery/power/terminal/terminal = null
get_desc()
. = {"It's [online ? "on" : "off"]line. [charging ? "It's charging, and it" : "It"] looks about [round(charge / capacity * 100, 20)]% full."}
/obj/machinery/power/smes/construction
New(var/turf/iloc, var/idir = 2)
if (!isturf(iloc))
qdel(src)
set_dir(idir)
var/turf/Q = get_step(iloc, idir)
if (!Q)
qdel(src)
var/obj/machinery/power/terminal/term = new /obj/machinery/power/terminal(Q)
term.set_dir(get_dir(Q, iloc))
..()
/obj/machinery/power/smes/emp_act()
..()
src.online = 0
src.charging = 0
src.output = 0
src.charge -= 1e6
if (src.charge < 0)
src.charge = 0
SPAWN_DBG(10 SECONDS)
src.output = initial(src.output)
src.charging = initial(src.charging)
src.online = initial(src.online)
return
/obj/machinery/power/smes/New()
..()
SPAWN_DBG(0.5 SECONDS)
dir_loop:
for(var/d in cardinal)
var/turf/T = get_step(src, d)
for(var/obj/machinery/power/terminal/term in T)
if (term?.dir == turn(d, 180))
terminal = term
break dir_loop
if (!terminal)
status |= BROKEN
return
terminal.master = src
updateicon()
/obj/machinery/power/smes/proc/updateicon()
if (status & BROKEN)
ClearAllOverlays()
return
var/image/I = SafeGetOverlayImage("operating", 'icons/obj/power.dmi', "smes-op[online]")
UpdateOverlays(I, "operating")
I = SafeGetOverlayImage("chargemode",'icons/obj/power.dmi', "smes-oc1")
if (charging)
I.icon_state = "smes-oc1"
else if (chargemode)
I.icon_state = "smes-oc0"
else
I = null
UpdateOverlays(I, "chargemode", 0, 1)
var/clevel = chargedisplay()
if (clevel>0)
I = SafeGetOverlayImage("chargedisp",'icons/obj/power.dmi',"smes-og[clevel]")
UpdateOverlays(I, "chargedisp")
/obj/machinery/power/smes/proc/chargedisplay()
return round(5.5*charge/capacity)
/obj/machinery/power/smes/process(mult)
if (status & BROKEN)
return
//store machine state to see if we need to update the icon overlays
var/last_disp = chargedisplay()
var/last_chrg = charging
var/last_onln = online
// Had to revert a hack here that caused SMES to continue charging despite insufficient power coming in on the input (terminal) side.
if (terminal)
var/excess = terminal.surplus()
var/load = 0
if (charging)
if (excess >= 0) // if there's power available, try to charge
load = min(capacity-charge, chargelevel) // charge at set rate, limited to spare capacity
// Adjusting mult to other power sources would likely cause more harm than good as it would cause unusual surges
// of power that would only be noticed though hotwire or be unrationalizable to player. This will extrapolate power
// benefits to charged value so that minimal loss occurs.
charge += load * mult // increase the charge
add_load(load) // add the load to the terminal side network
else // if not enough capcity
charging = 0 // stop charging
chargecount = 0
else if (chargemode)
if (chargecount > 2)
charging = 1
chargecount = 0
else if (excess >= chargelevel)
chargecount++
else
chargecount = 0
lastexcess = load + excess
if (online) // if outputting
if (prob(5))
SPAWN_DBG(1 DECI SECOND)
playsound(src.loc, pick(ambience_power), 60, 1)
lastout = min(charge, output) //limit output to that stored
charge -= lastout // reduce the storage (may be recovered in /restore() if excessive)
add_avail(lastout) // add output to powernet (smes side)
if (charge < 0.0001)
online = 0 // stop output if charge falls to zero
// only update icon if state changed
if (last_disp != chargedisplay() || last_chrg != charging || last_onln != online)
updateicon()
src.updateDialog()
// called after all power processes are finished
// restores charge level to smes if there was excess this ptick
/obj/machinery/power/smes/proc/restore()
if (status & BROKEN)
return
if (!online)
loaddemand = 0
return
var/excess = powernet.netexcess // this was how much wasn't used on the network last ptick, minus any removed by other SMESes
excess = min(lastout, excess) // clamp it to how much was actually output by this SMES last ptick
excess = min(capacity-charge, excess) // for safety, also limit recharge by space capacity of SMES (shouldn't happen)
// now recharge this amount
var/clev = chargedisplay()
charge += excess
powernet.netexcess -= excess // remove the excess from the powernet, so later SMESes don't try to use it
loaddemand = lastout - excess
if (clev != chargedisplay())
updateicon()
///obj/machinery/power/smes/add_avail(var/amount)
// if (terminal?.powernet)
// terminal.powernet.newavail += amount
/obj/machinery/power/smes/add_load(var/amount)
if (terminal?.powernet)
terminal.powernet.newload += amount
/obj/machinery/power/smes/ui_interact(mob/user, datum/tgui/ui)
ui = tgui_process.try_update_ui(user, src, ui)
if(!ui)
ui = new(user, src, "Smes", src.name)
ui.open()
/obj/machinery/power/smes/ui_static_data(mob/user)
. = list(
"inputLevelMax" = SMESMAXCHARGELEVEL,
"outputLevelMax" = SMESMAXOUTPUT,
)
/obj/machinery/power/smes/ui_data(mob/user)
. = list(
"capacity" = src.capacity,
"charge" = src.charge,
"inputAttempt" = src.chargemode,
"inputting" = src.charging,
"inputLevel" = src.chargelevel,
"inputAvailable" = src.lastexcess,
"outputAttempt" = src.online,
"outputting" = src.loaddemand,
"outputLevel" = src.output,
)
/obj/machinery/power/smes/ui_act(action, params)
. = ..()
if (.)
return
switch(action)
if("toggle-input")
src.chargemode = !src.chargemode
if (!chargemode)
charging = 0
src.updateicon()
. = TRUE
if("toggle-output")
src.online = !src.online
src.updateicon()
. = TRUE
if("set-input")
var/target = params["target"]
var/adjust = params["adjust"]
if(target == "min")
src.chargelevel = 0
. = TRUE
else if(target == "max")
src.chargelevel = SMESMAXCHARGELEVEL
. = TRUE
else if(adjust)
src.chargelevel = clamp((src.chargelevel + adjust), 0 , SMESMAXCHARGELEVEL)
. = TRUE
else if(text2num(target) != null) //set by drag
src.chargelevel = clamp(text2num(target), 0 , SMESMAXCHARGELEVEL)
. = TRUE
if("set-output")
var/target = params["target"]
var/adjust = params["adjust"]
if(target == "min")
src.output = 0
. = TRUE
else if(target == "max")
src.output = SMESMAXOUTPUT
. = TRUE
else if(adjust)
src.output = clamp((src.output + adjust), 0 , SMESMAXOUTPUT)
. = TRUE
else if(text2num(target) != null) //set by drag
src.output = clamp(text2num(target), 0 , SMESMAXOUTPUT)
. = TRUE
/proc/rate_control(var/S, var/V, var/C, var/Min=1, var/Max=5, var/Limit=null)
var/href = "<A href='?src=\ref[S];rate control=1;[V]"
var/rate = "[href]=-[Max]'>-</A>[href]=-[Min]'>-</A> [(C?C : 0)] [href]=[Min]'>+</A>[href]=[Max]'>+</A>"
if (Limit) return "[href]=-[Limit]'>-</A>"+rate+"[href]=[Limit]'>+</A>"
return rate
#undef SMESMAXCHARGELEVEL
#undef SMESMAXOUTPUT