Cleans up and improves microwave code (#41832)

* Cleans up and improves microwave code

* /the to /The

* Update microwave.dm
This commit is contained in:
nicbn
2018-12-09 22:56:02 +13:00
committed by oranges
parent 6fa0017586
commit b6d45650b8
@@ -1,3 +1,9 @@
#define MICROWAVE_NORMAL 0
#define MICROWAVE_MUCK 1
#define MICROWAVE_PRE 2
//Microwaving doesn't use recipes, instead it calls the microwave_act of the objects. For food, this creates something based on the food's cooked_type
/obj/machinery/microwave
name = "microwave oven"
desc = "Cooks and boils stuff."
@@ -12,176 +18,150 @@
pass_flags = PASSTABLE
light_color = LIGHT_COLOR_YELLOW
light_power = 3
var/operating = FALSE // Is it on?
var/dirty = 0 // = {0..100} Does it need cleaning?
var/broken = 0 // ={0,1,2} How broken is it???
var/max_n_of_items = 10 // whatever fat fuck made this a global var needs to look at themselves in the mirror sometime
var/operating = FALSE
var/dirty = 0 // 0 to 100 // Does it need cleaning?
var/dirty_anim_playing = FALSE
var/broken = 0 // 0, 1 or 2 // How broken is it???
var/max_n_of_items = 10
var/efficiency = 0
var/datum/looping_sound/microwave/soundloop
//Microwaving doesn't use recipes, instead it calls the microwave_act of the objects. For food, this creates something based on the food's cooked_type
/*******************
* Initialising
********************/
/obj/machinery/microwave/Initialize()
. = ..()
create_reagents(100)
soundloop = new(list(src), FALSE)
/obj/machinery/microwave/RefreshParts()
var/E
var/max_items = 10
efficiency = 0
for(var/obj/item/stock_parts/micro_laser/M in component_parts)
E += M.rating
efficiency += M.rating
for(var/obj/item/stock_parts/matter_bin/M in component_parts)
max_items = 10 * M.rating
efficiency = E
max_n_of_items = max_items
max_n_of_items = 10 * M.rating
break
/obj/machinery/microwave/examine(mob/user)
..()
if(!operating)
to_chat(user, "<span class='notice'>Alt-click [src] to turn it on.</span>")
if(in_range(user, src) || isobserver(user))
to_chat(user, "<span class='notice'>The status display reads: Capacity: <b>[max_n_of_items]</b> items.<br>Cook time reduced by <b>[(efficiency*25)-25]%</b>.<span>")
to_chat(user, "<span class='notice'>The status display reads: Capacity: <b>[max_n_of_items]</b> items.<br>Cook time reduced by <b>[(efficiency - 1) * 25]%</b>.<span>")
/*******************
* Item Adding
********************/
/obj/machinery/microwave/update_icon()
if(broken)
icon_state = "mwb"
else if(dirty_anim_playing)
icon_state = "mwbloody1"
else if(dirty == 100)
icon_state = "mwbloody"
else if(operating)
icon_state = "mw1"
else if(panel_open)
icon_state = "mw-o"
else
icon_state = "mw"
/obj/machinery/microwave/attackby(obj/item/O, mob/user, params)
if(operating)
return
if(!broken && dirty<100)
if(default_deconstruction_screwdriver(user, "mw-o", "mw", O))
return
if(default_unfasten_wrench(user, O))
return
if(default_deconstruction_crowbar(O))
return
if(broken > 0)
if(broken == 2 && O.tool_behaviour == TOOL_WIRECUTTER) // If it's broken and they're using a screwdriver
user.visible_message( \
"[user] starts to fix part of the microwave.", \
"<span class='notice'>You start to fix part of the microwave...</span>" \
)
if (O.use_tool(src, user, 20))
user.visible_message( \
"[user] fixes part of the microwave.", \
"<span class='notice'>You fix part of the microwave.</span>" \
)
user.visible_message("[user] starts to fix part of \the [src].", "<span class='notice'>You start to fix part of \the [src]...</span>")
if(O.use_tool(src, user, 20))
user.visible_message("[user] fixes part of \the [src].", "<span class='notice'>You fix part of \the [src].</span>")
broken = 1 // Fix it a bit
else if(broken == 1 && O.tool_behaviour == TOOL_WELDER) // If it's broken and they're doing the wrench
user.visible_message( \
"[user] starts to fix part of the microwave.", \
"<span class='notice'>You start to fix part of the microwave...</span>" \
)
if (O.use_tool(src, user, 20))
user.visible_message( \
"[user] fixes the microwave.", \
"<span class='notice'>You fix the microwave.</span>" \
)
icon_state = "mw"
broken = 0 // Fix it!
dirty = 0 // just to be sure
user.visible_message("[user] starts to fix part of \the [src].", "<span class='notice'>You start to fix part of \the [src]...</span>")
if(O.use_tool(src, user, 20))
user.visible_message("[user] fixes \the [src].", "<span class='notice'>You fix \the [src].</span>")
broken = 0
container_type = OPENCONTAINER
return 0 //to use some fuel
update_icon()
return FALSE //to use some fuel
else
to_chat(user, "<span class='warning'>It's broken!</span>")
return 1
else if(istype(O, /obj/item/reagent_containers/spray/))
return TRUE
updateUsrDialog()
return
if(istype(O, /obj/item/reagent_containers/spray))
var/obj/item/reagent_containers/spray/clean_spray = O
if(clean_spray.reagents.has_reagent("cleaner",clean_spray.amount_per_transfer_from_this))
clean_spray.reagents.remove_reagent("cleaner",clean_spray.amount_per_transfer_from_this,1)
if(clean_spray.reagents.has_reagent("cleaner", clean_spray.amount_per_transfer_from_this))
clean_spray.reagents.remove_reagent("cleaner", clean_spray.amount_per_transfer_from_this,1)
playsound(loc, 'sound/effects/spray3.ogg', 50, 1, -6)
user.visible_message( \
"[user] has cleaned the microwave.", \
"<span class='notice'>You clean the microwave.</span>" \
)
dirty = 0 // It's clean!
broken = 0 // just to be sure
icon_state = "mw"
user.visible_message("[user] has cleaned \the [src].", "<span class='notice'>You clean \the [src].</span>")
dirty = 0
container_type = OPENCONTAINER
update_icon()
updateUsrDialog()
return 1 // Disables the after-attack so we don't spray the floor/user.
else
to_chat(user, "<span class='warning'>You need more space cleaner!</span>")
return 1
return TRUE
else if(istype(O, /obj/item/soap/)) // If they're trying to clean it then let them
if(istype(O, /obj/item/soap))
var/obj/item/soap/P = O
user.visible_message( \
"[user] starts to clean the microwave.", \
"<span class='notice'>You start to clean the microwave...</span>" \
)
if (do_after(user, P.cleanspeed, target = src))
user.visible_message( \
"[user] has cleaned the microwave.", \
"<span class='notice'>You clean the microwave.</span>" \
)
dirty = 0 // It's clean!
broken = 0 // just to be sure
icon_state = "mw"
user.visible_message("[user] starts to clean \the [src].", "<span class='notice'>You start to clean \the [src]...</span>")
if(do_after(user, P.cleanspeed, target = src))
user.visible_message("[user] has cleaned \the [src].", "<span class='notice'>You clean \the [src].</span>")
dirty = 0
container_type = OPENCONTAINER
update_icon()
return TRUE
else if(dirty==100) // The microwave is all dirty so can't be used!
to_chat(user, "<span class='warning'>It's dirty!</span>")
return 1
if(dirty == 100) // The microwave is all dirty so can't be used!
to_chat(user, "<span class='warning'>\The [src] is dirty!</span>")
return TRUE
else if(istype(O, /obj/item/storage/bag/tray))
if(istype(O, /obj/item/storage/bag/tray))
var/obj/item/storage/T = O
var/loaded = 0
for(var/obj/item/reagent_containers/food/snacks/S in T.contents)
if (contents.len>=max_n_of_items)
to_chat(user, "<span class='warning'>[src] is full, you can't put anything in!</span>")
return 1
if(contents.len >= max_n_of_items)
to_chat(user, "<span class='warning'>\The [src] is full, you can't put anything in!</span>")
return TRUE
if(SEND_SIGNAL(T, COMSIG_TRY_STORAGE_TAKE, S, src))
loaded++
if(loaded)
to_chat(user, "<span class='notice'>You insert [loaded] items into [src].</span>")
to_chat(user, "<span class='notice'>You insert [loaded] items into \the [src].</span>")
updateUsrDialog()
return
if(O.w_class <= WEIGHT_CLASS_NORMAL && !istype(O, /obj/item/storage) && user.a_intent == INTENT_HELP)
if(contents.len >= max_n_of_items)
to_chat(user, "<span class='warning'>\The [src] is full, you can't put anything in!</span>")
return TRUE
if(!user.transferItemToLoc(O, src))
to_chat(user, "<span class='warning'>\The [O] is stuck to your hand, you cannot put it in \the [src]!</span>")
return FALSE
user.visible_message("[user] has added \the [O] to \the [src].", "<span class='notice'>You add \the [O] to \the [src].</span>")
updateUsrDialog()
return
else if(O.w_class <= WEIGHT_CLASS_NORMAL && !istype(O, /obj/item/storage) && user.a_intent == INTENT_HELP)
if (contents.len>=max_n_of_items)
to_chat(user, "<span class='warning'>[src] is full, you can't put anything in!</span>")
return 1
else
if(!user.transferItemToLoc(O, src))
to_chat(user, "<span class='warning'>\the [O] is stuck to your hand, you cannot put it in \the [src]!</span>")
return 0
if(dirty < 100)
if(default_deconstruction_screwdriver(user, "", "", O) || default_unfasten_wrench(user, O))
update_icon()
return
user.visible_message( \
"[user] has added \the [O] to \the [src].", \
"<span class='notice'>You add \the [O] to \the [src].</span>")
else
..()
..()
updateUsrDialog()
/obj/machinery/microwave/AltClick(mob/user)
if(user.canUseTopic(src, BE_CLOSE) && !(operating || broken > 0 || panel_open || !anchored || dirty == 100))
cook()
/*******************
* Microwave Menu
********************/
/obj/machinery/microwave/ui_interact(mob/user) // The microwave Menu
/obj/machinery/microwave/ui_interact(mob/user)
. = ..()
if(panel_open || !anchored)
return
var/dat = "<div class='statusDisplay'>"
if(broken > 0)
dat += "ERROR: 09734014-A2379-D18746 --Bad memory<BR>Contact your operator or use command line to rebase memory ///git checkout {HEAD} -a commit pull --rebase push {*NEW HEAD*}</div>" //Thats how all the git fiddling looks to me
dat += "ERROR: 09734014-A2379-D18746 --Bad memory<BR>Contact your operator or use command line to rebase memory ///git checkout {HEAD} -a commit pull --rebase push {*NEW HEAD*}</div>" // Thats how all the git fiddling looks to me
else if(operating)
dat += "Microwaving in progress!<BR>Please wait...!</div>"
else if(dirty==100)
dat += "ERROR: >> 0 --Response input zero<BR>Contact your operator of the device manifactor support.</div>"
else if(dirty == 100)
dat += "ERROR: >> 0 --Response input zero<BR>Contact your operator of the device manufacturer support.</div>"
else
var/list/items_counts = new
for (var/obj/O in contents)
@@ -206,132 +186,133 @@
popup.set_content(dat)
popup.open()
/***********************************
* Microwave Menu Handling/Cooking
************************************/
/obj/machinery/microwave/Topic(href, href_list)
if(..())
return
if(panel_open)
return
usr.set_machine(src)
if(!operating)
switch(href_list["action"])
if("cook")
cook()
if("dispose")
dispose()
updateUsrDialog()
/obj/machinery/microwave/proc/dispose()
for(var/obj/O in contents)
O.forceMove(drop_location())
to_chat(usr, "<span class='notice'>You dispose of \the [src] contents.</span>")
updateUsrDialog()
/obj/machinery/microwave/proc/cook()
if(stat & (NOPOWER|BROKEN))
return
if(prob(max((5 / efficiency) - 5, dirty * 5))) //a clean unupgraded microwave has no risk of failure
muck()
return
for(var/obj/O in contents)
if(istype(O, /obj/item/reagent_containers/food) || istype(O, /obj/item/grown))
continue
if(prob(min(dirty * 5, 100)))
start_can_fail()
return
break
start()
if (prob(max(5/efficiency-5,dirty*5))) //a clean unupgraded microwave has no risk of failure
muck_start()
if (!microwaving(4))
muck_finish()
return
muck_finish()
return
/obj/machinery/microwave/proc/turn_on()
visible_message("\The [src] turns on.", "<span class='italics'>You hear a microwave humming.</span>")
operating = TRUE
else
if(has_extra_item() && prob(min(dirty*5,100)) && !microwaving(4))
broke()
return
set_light(1.5)
soundloop.start()
update_icon()
updateUsrDialog()
if(!microwaving(10))
abort()
return
stop()
var/metal = 0
for(var/obj/item/O in contents)
O.microwave_act(src)
if(O.materials[MAT_METAL])
metal += O.materials[MAT_METAL]
if(metal)
visible_message("<span class='warning'>Sparks fly around [src]!</span>")
if(prob(max(metal/2, 33)))
explosion(loc,0,1,2)
broke()
return
dropContents()
return
/obj/machinery/microwave/proc/microwaving(seconds as num)
for (var/i=1 to seconds)
if (stat & (NOPOWER|BROKEN))
return 0
use_power(500)
sleep(max(12-2*efficiency,2)) // standard microwave means sleep(10). The better the efficiency, the faster the cooking
return 1
/obj/machinery/microwave/proc/has_extra_item()
for (var/obj/O in contents)
if ( \
!istype(O, /obj/item/reagent_containers/food) && \
!istype(O, /obj/item/grown) \
)
return 1
return 0
/obj/machinery/microwave/proc/spark()
visible_message("<span class='warning'>Sparks fly around [src]!</span>")
var/datum/effect_system/spark_spread/s = new
s.set_up(2, 1, src)
s.start()
/obj/machinery/microwave/proc/start()
visible_message("The microwave turns on.", "<span class='italics'>You hear a microwave humming.</span>")
soundloop.start()
operating = TRUE
icon_state = "mw1"
updateUsrDialog()
set_light(1.5)
turn_on()
loop(MICROWAVE_NORMAL, 10)
/obj/machinery/microwave/proc/abort()
operating = FALSE // Turn it off again aferwards
icon_state = "mw"
updateUsrDialog()
set_light(0)
soundloop.stop()
/obj/machinery/microwave/proc/start_can_fail()
turn_on()
loop(MICROWAVE_PRE, 4)
/obj/machinery/microwave/proc/stop()
abort()
/obj/machinery/microwave/proc/muck()
turn_on()
playsound(src.loc, 'sound/effects/splat.ogg', 50, 1)
dirty_anim_playing = TRUE
update_icon()
loop(MICROWAVE_MUCK, 4)
/obj/machinery/microwave/proc/dispose()
for (var/obj/O in contents)
O.forceMove(drop_location())
to_chat(usr, "<span class='notice'>You dispose of the microwave contents.</span>")
updateUsrDialog()
/obj/machinery/microwave/proc/loop(type, time, wait = max(12 - 2 * efficiency, 2)) // standard wait is 10
if(stat & (NOPOWER|BROKEN))
if(MICROWAVE_PRE)
pre_fail()
return
if(!time)
switch(type)
if(MICROWAVE_NORMAL)
loop_finish()
if(MICROWAVE_MUCK)
muck_finish()
if(MICROWAVE_PRE)
pre_success()
return
time--
use_power(500)
addtimer(CALLBACK(src, .proc/loop, type, time, wait), wait)
/obj/machinery/microwave/proc/muck_start()
playsound(src.loc, 'sound/effects/splat.ogg', 50, 1) // Play a splat sound
icon_state = "mwbloody1" // Make it look dirty!!
/obj/machinery/microwave/proc/loop_finish()
operating = FALSE
var/metal = 0
for(var/obj/item/O in contents)
O.microwave_act(src)
if(O.materials[MAT_METAL])
metal += O.materials[MAT_METAL]
if(metal)
spark()
broken = 2
if(prob(max(metal / 2, 33)))
explosion(loc, 0, 1, 2)
else
dropContents()
after_finish_loop()
/obj/machinery/microwave/proc/pre_fail()
broken = 2
operating = FALSE
spark()
after_finish_loop()
/obj/machinery/microwave/proc/pre_success()
loop(MICROWAVE_NORMAL, 10)
/obj/machinery/microwave/proc/muck_finish()
visible_message("<span class='warning'>The microwave gets covered in muck!</span>")
dirty = 100 // Make it dirty so it can't be used util cleaned
icon_state = "mwbloody" // Make it look dirty too
operating = FALSE // Turn it off again aferwards
updateUsrDialog()
visible_message("<span class='warning'>\The [src] gets covered in muck!</span>")
dirty = 100
dirty_anim_playing = FALSE
operating = FALSE
for(var/obj/item/reagent_containers/food/snacks/S in src)
if(prob(50))
new /obj/item/reagent_containers/food/snacks/badrecipe(src)
qdel(S)
after_finish_loop()
/obj/machinery/microwave/proc/after_finish_loop()
set_light(0)
soundloop.stop()
/obj/machinery/microwave/proc/broke()
var/datum/effect_system/spark_spread/s = new
s.set_up(2, 1, src)
s.start()
icon_state = "mwb" // Make it look all busted up and shit
visible_message("<span class='warning'>The microwave breaks!</span>") //Let them know they're stupid
broken = 2 // Make it broken so it can't be used util fixed
operating = FALSE // Turn it off again aferwards
updateUsrDialog()
set_light(0)
soundloop.stop()
/obj/machinery/microwave/Topic(href, href_list)
if(..() || panel_open)
return
usr.set_machine(src)
if(operating)
updateUsrDialog()
return
switch(href_list["action"])
if ("cook")
cook()
if ("dispose")
dispose()
update_icon()
updateUsrDialog()