-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathautolathe.dm
440 lines (393 loc) · 14.8 KB
/
autolathe.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#define AUTOLATHE_MAIN_MENU 1
#define AUTOLATHE_CATEGORY_MENU 2
#define AUTOLATHE_SEARCH_MENU 3
/obj/machinery/autolathe
name = "autolathe"
desc = "It produces items using metal and glass."
icon_state = "autolathe"
density = TRUE
use_power = IDLE_POWER_USE
idle_power_usage = 10
active_power_usage = 100
circuit = /obj/item/circuitboard/machine/autolathe
layer = BELOW_OBJ_LAYER
/// Are hacked designs unlocked
var/hacked = FALSE
/// Is it disabled(Can it print)
var/disabled = FALSE
/// Will it taze you when you interact with it
var/shocked = FALSE
/// Resource use multiplier
var/prod_coeff = 1
/// Internal techweb of designs
var/datum/techweb/stored_research
/// name of the design to search for
var/search
/// Maximum length of the queue
var/queue_max_len = 12
/// Is it currently printing
var/processing_queue = FALSE
/// Requested item to be made
var/datum/design/request
/// Items being built
var/list/being_built = list()
/// Item queue
var/list/autoqueue = list()
/// List describing the items for the UI
var/processing_line
/// Direction its qill output when the item is printed (0 for ontop of itself)
var/printdirection = 0
/// Length of the queue
var/queuelength = 0
/// Avaliable categories
var/list/categories = list("Tools","Electronics","Construction","T-Comm","Security","Machinery","Medical","Miscellaneous","Dinnerware","Imported", "Search")
/obj/machinery/autolathe/Initialize(mapload)
AddComponent(/datum/component/material_container, list(/datum/material/iron, /datum/material/glass), 0, TRUE, null, null, CALLBACK(src, PROC_REF(AfterMaterialInsert)))
. = ..()
wires = new /datum/wires/autolathe(src)
stored_research = new /datum/techweb/specialized/autounlocking/autolathe
request = list()
/obj/machinery/autolathe/Destroy()
QDEL_NULL(wires)
return ..()
/obj/machinery/autolathe/ui_interact(mob/user, datum/tgui/ui)
ui = SStgui.try_update_ui(user, src, ui)
if(!is_operational())
return
if(shocked && !(stat & NOPOWER))
shock(user,50)
if(!ui)
ui = new(user, src, "Autolathe", name)
ui.open()
/obj/machinery/autolathe/proc/wallcheck(direction) //Check for nasty walls and update ui
if(iswallturf(get_step(src,(direction))))
return TRUE
else
return FALSE
/obj/machinery/autolathe/ui_data(mob/user) // All the data the ui will need
var/list/data = list()
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
data["total_amount"] = materials.total_amount
data["max_amount"] = materials.max_amount
data["stored_materials"] = list()
data["stored_materials"][getmaterialref(/datum/material/iron)] = materials.get_material_amount(/datum/material/iron)
data["stored_materials"][getmaterialref(/datum/material/glass)] = materials.get_material_amount(/datum/material/glass)
data["rightwall"] = wallcheck(4) // Wall data for ui
data["leftwall"] = wallcheck(8)
data["abovewall"] = wallcheck(1)
data["belowwall"] = wallcheck(2)
processing_line = being_built.len ? get_processing_line() : null
data["processing"] = processing_line
data["printdir"] = printdirection
data["isprocessing"] = processing_queue
data["queuelength"] = queuelength
data["categories"] = categories
data["disabled"] = disabled
if(istype(autoqueue) && autoqueue.len)
var/list/uidata = list()
var/index = 1
for(var/list/L in autoqueue)
var/datum/design/D = L[1]
uidata[++uidata.len] = list("name" = initial(D.name), "multiplier" = L[2], "index" = index)
index++
data["queue"] = uidata
else
data["queue"] = list()
return data
/obj/machinery/autolathe/ui_static_data(mob/user)
var/list/data = list()
var/list/designs = list()
for(var/v in stored_research.researched_designs)
var/datum/design/D = SSresearch.techweb_design_by_id(v)
var/coeff = (ispath(D.build_path, /obj/item/stack) ? 1 : prod_coeff)
var/list/design = list()
design["name"] = D.name
design["id"] = D.id
design["category"] = D.category
design["materials"] = list()
for(var/i in D.materials)
design["materials"][i] = D.materials[i] * coeff
designs += list(design)
data["designs"] = designs
return data
/obj/machinery/autolathe/ui_act(action, params)
if(..())
return
switch(action)
if("make") // Lets try make the item supplied via the UI
request = stored_research.isDesignResearchedID(params["item_id"])
if(!request)
return
var/multiplier = text2num(params["multiplier"])
multiplier = clamp(multiplier,1,50)
if((autoqueue.len + 1) < queue_max_len)
add_to_queue(request, multiplier) // Add item to queue for processing
else
to_chat(usr, span_warning("The autolathe queue is full!"))
if("eject")
request = stored_research.isDesignResearchedID(params["item_id"])
if(processing_queue)
to_chat(usr, span_warning("The autolathe queue is processing, please stop before ejecting material"))
if(!request)
return
var/multiplier = text2num(params["multiplier"])
multiplier = clamp(multiplier,1,50)
make_item(request, multiplier)
processing_queue = FALSE
if("process_queue") // Processing queue flag triggers the queue
if(processing_queue)
processing_queue = FALSE
return
processing_queue = TRUE
process_queue()
if("remove_from_queue")
var/index = text2num(params["index"])
if(isnum(index) && ISINRANGE(index, 1, autoqueue.len))
remove_from_queue(index)
if("queue_move") // Moves items up and down the list
var/index = text2num(params["index"])
var/new_index = index + text2num(params["queue_move"])
if(isnum(index) && isnum(new_index))
if(ISINRANGE(new_index, 1, autoqueue.len))
autoqueue.Swap(index,new_index)
if("clear_queue")
queuelength = 0
processing_queue = FALSE
autoqueue = list()
processing_line = null
if("printdir")
printdirection = text2num(params["direction"])
if(printdirection > 8) // Simple Sanity Check
printdirection = 0
update_appearance(UPDATE_ICON)
/obj/machinery/autolathe/on_deconstruction()
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
materials.retrieve_all()
/obj/machinery/autolathe/attackby(obj/item/O, mob/living/user, params)
if(default_deconstruction_crowbar(O))
return TRUE
if(panel_open && is_wire_tool(O))
wires.interact(user)
return TRUE
if(stat)
return TRUE
if(istype(O, /obj/item/disk/design_disk))
user.visible_message("[user] begins to load \the [O] in \the [src]...",
"You begin to load a design from \the [O]...",
"You hear the chatter of a floppy drive.")
var/obj/item/disk/design_disk/D = O
if(do_after(user, 1.5 SECONDS, src))
for(var/B in D.blueprints)
if(B)
stored_research.add_design(B)
update_static_data(user)
return TRUE
return ..()
/obj/machinery/autolathe/attackby_secondary(obj/item/weapon, mob/user, params)
if(default_deconstruction_screwdriver(user, "autolathe_t", "autolathe", weapon))
return SECONDARY_ATTACK_CANCEL_ATTACK_CHAIN
return ..()
/obj/machinery/autolathe/proc/AfterMaterialInsert(type_inserted, id_inserted, amount_inserted)
if(ispath(type_inserted, /obj/item/stack/ore/bluespace_crystal))
use_power(MINERAL_MATERIAL_AMOUNT / 10)
else
switch(id_inserted)
if (/datum/material/iron)
flick("autolathe_o",src)//plays metal insertion animation
else
flick("autolathe_r",src)//plays glass insertion animation
use_power(min(1000, amount_inserted / 100))
/obj/machinery/autolathe/RefreshParts()
var/T = 0
for(var/obj/item/stock_parts/matter_bin/MB in component_parts)
T += MB.rating * 75000
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
materials.max_amount = T
T=1.2
for(var/obj/item/stock_parts/manipulator/M in component_parts)
T -= M.rating*0.2
prod_coeff = min(1,max(0,T)) // Coeff going 1 -> 0,8 -> 0,6 -> 0,4
/obj/machinery/autolathe/examine(mob/user)
. += ..()
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
if(in_range(user, src) || isobserver(user))
. += "<span class='notice'>The status display reads: Storing up to <b>[materials.max_amount]</b> material units.<br>Material consumption at <b>[prod_coeff*100]%</b>.<span>"
/obj/machinery/autolathe/proc/materials_printout()
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
var/dat = "<b>Total amount:</b> [materials.total_amount] / [materials.max_amount] cm<sup>3</sup><br>"
for(var/mat_id in materials.materials)
var/datum/material/M = mat_id
var/mineral_amount = materials.materials[mat_id]
if(mineral_amount > 0)
dat += "<b>[M.name] amount:</b> [mineral_amount] cm<sup>3</sup><br>"
return dat
/obj/machinery/autolathe/proc/can_build(datum/design/D, amount = 1)
if(D.make_reagents.len)
return FALSE
var/coeff = (ispath(D.build_path, /obj/item/stack) ? 1 : prod_coeff)
var/list/required_materials = list()
for(var/i in D.materials)
required_materials[i] = D.materials[i] * coeff * amount
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
if(wallcheck(printdirection))
say("Output blocked, please remove obstruction.")
return FALSE
if(!materials)
say("Error, invalid object.")
return FALSE
return materials.has_materials(required_materials)
/obj/machinery/autolathe/proc/reset(wire)
switch(wire)
if(WIRE_HACK)
if(!wires.is_cut(wire))
if(!(obj_flags & EMAGGED))
adjust_hacked(FALSE)
if(WIRE_SHOCK)
if(!wires.is_cut(wire))
shocked = FALSE
if(WIRE_DISABLE)
if(!wires.is_cut(wire))
disabled = FALSE
/obj/machinery/autolathe/proc/shock(mob/user, prb)
if(stat & (BROKEN|NOPOWER)) // unpowered, no shock
return FALSE
if(!prob(prb))
return FALSE
var/datum/effect_system/spark_spread/s = new /datum/effect_system/spark_spread
s.set_up(5, 1, src)
s.start()
if (electrocute_mob(user, get_area(src), src, 0.7, TRUE))
return TRUE
else
return FALSE
/obj/machinery/autolathe/proc/adjust_hacked(state)
hacked = state
for(var/id in SSresearch.techweb_designs)
var/datum/design/D = SSresearch.techweb_design_by_id(id)
if((D.build_type & AUTOLATHE) && ("hacked" in D.category))
if(hacked)
stored_research.add_design(D)
else
stored_research.remove_design(D)
/obj/machinery/autolathe/hacked/Initialize(mapload)
. = ..()
adjust_hacked(TRUE)
/obj/machinery/autolathe/emag_act(mob/user, obj/item/card/emag/emag_card)
if(obj_flags & EMAGGED)
return FALSE
obj_flags |= EMAGGED
if(!hacked)
adjust_hacked(TRUE)
playsound(src, "sparks", 75, TRUE, -1)
to_chat(user, span_notice("You use the cryptographic sequencer on [src]."))
return TRUE
//Called when the object is constructed by an autolathe
//Has a reference to the autolathe so you can do !!FUN!! things with hacked lathes
/obj/item/proc/autolathe_crafted(obj/machinery/autolathe/A)
return
/obj/machinery/autolathe/proc/make_item(datum/design/D, multiplier)
var/is_stack = ispath(D.build_path, /obj/item/stack)
var/coeff = (is_stack ? 1 : prod_coeff) //stacks are unaffected by production coefficient
var/total_amount = 0
for(var/MAT in D.materials)
total_amount += D.materials[MAT]
var/power = max(2000, (total_amount)*multiplier/5) //Change this to use all materials
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
if (!materials)
say("No access to material storage, please contact the quartermaster.")
return FALSE
if(can_build(D, multiplier)) // Check if we can build if not, return
var/list/materials_used = list()
var/list/picked_materials
var/list/custom_materials = list() //These will apply their material effect, This should usually only be one.
for(var/MAT in D.materials)
var/datum/material/used_material = MAT
var/amount_needed = D.materials[MAT] * coeff * multiplier
if(istext(used_material)) //This means its a category
var/list/list_to_show = list()
for(var/i in SSmaterials.materials_by_category[used_material])
if(materials.materials[i])
list_to_show += i
used_material = input("Choose [used_material]", "Custom Material") as null|anything in list_to_show
if(!used_material)
return //Didn't pick any material, so you can't build shit either.
custom_materials[used_material] += amount_needed
materials_used[used_material] = amount_needed
if(materials.has_materials(materials_used))
use_power(power)
materials.use_materials(materials_used)
being_built = list(D, multiplier)
desc = "It's building \a [initial(D.name)]."
icon_state = "autolathe_n"
var/time = is_stack ? 32 : 32 * coeff * multiplier
sleep(time)
if(wallcheck(printdirection))
printdirection = 0
var/atom/A = drop_location()
var/location = get_step(src,(printdirection))
if(printdirection)
A = location
if(is_stack) // If its a stack we need to define it as so
var/obj/item/stack/N = new D.build_path(A, multiplier)
N.update_appearance(UPDATE_ICON)
N.autolathe_crafted(src)
else
for(var/i=1, i<=multiplier, i++)
var/obj/item/new_item = new D.build_path(A)
new_item.materials = new_item.materials.Copy()
new_item.item_flags |= AUTOLATHED
for(var/mat in materials_used)
new_item.materials[mat] = materials_used[mat] / multiplier
new_item.autolathe_crafted(src)
if(picked_materials?.len)
new_item.set_custom_materials(picked_materials, 1 / multiplier) //Ensure we get the non multiplied amount
icon_state = "autolathe"
desc = initial(desc)
return TRUE
else
say("Not enough resources. Queue processing stopped.")
return FALSE
/obj/machinery/autolathe/proc/add_to_queue(D, multiplier)
queuelength++
if(!istype(autoqueue))
autoqueue = list()
if(D)
autoqueue.Add(list(list(D,multiplier)))
return autoqueue.len
/obj/machinery/autolathe/proc/remove_from_queue(index)
queuelength--
if(!isnum(index) || !istype(autoqueue) || (index<1 || index>autoqueue.len))
return FALSE
autoqueue.Cut(index,++index)
return TRUE
/obj/machinery/autolathe/proc/process_queue() //Process the queue from the autoqueue list. Will add temp metal and glass later.
var/datum/design/D = autoqueue[1][1]
var/multiplier = autoqueue[1][2]
if(!multiplier || !isnum(multiplier) || isnan(multiplier))
message_admins("[ADMIN_FULLMONTY(usr)] is attempting to create NAN stacks of items")
return
if(!processing_queue)
say("Queue processing halted.")
return
if(stat&(NOPOWER|BROKEN) || panel_open)
processing_queue = FALSE
return
if(!can_build(D,multiplier))
say("Not enough resources. Queue processing terminated.")
processing_queue = FALSE
return
remove_from_queue(1)
if(!make_item(D,multiplier))
return
if(autoqueue.len)
process_queue()
else
being_built = new /list()
say("Queue processing finished successfully.")
processing_queue = FALSE
/obj/machinery/autolathe/proc/get_processing_line() //Gets processing line for whats building for UI
var/datum/design/D = being_built[1]
var/multiplier = being_built[2]
var/is_stack = (multiplier>1)
var/output = "[initial(D.name)][is_stack?" (x[multiplier])":null]"
return output