From b89385144adeee5bb89469f3b0884c8989ef2199 Mon Sep 17 00:00:00 2001 From: Artorp Date: Tue, 17 Feb 2015 17:27:42 +0100 Subject: [PATCH] Added queue functionality to the autolathe --- code/game/machinery/autolathe.dm | 285 ++++++++++++++++++++++--------- 1 file changed, 203 insertions(+), 82 deletions(-) diff --git a/code/game/machinery/autolathe.dm b/code/game/machinery/autolathe.dm index 143853269dc..37610ddadc6 100644 --- a/code/game/machinery/autolathe.dm +++ b/code/game/machinery/autolathe.dm @@ -15,6 +15,9 @@ var/max_g_amount = 75000.0 var/operating = 0.0 + var/list/queue = list() + var/queue_max_len = 10 + var/turf/BuildTurf anchored = 1.0 var/list/L = list() var/list/LL = list() @@ -92,7 +95,7 @@ if(AUTOLATHE_SEARCH_MENU) dat = search_win(user) - var/datum/browser/popup = new(user, "autolathe", name, 500, 500) + var/datum/browser/popup = new(user, "autolathe", name, 800, 500) popup.set_content(dat) popup.open() @@ -151,7 +154,7 @@ flick("autolathe_r",src)//plays glass insertion animation stack.use(amount) else - if(!user.before_take_item(O)) + if(user.before_take_item(O)) user << "/the [O] is stuck to your hand, you can't put it in \the [src]!" O.loc = src icon_state = "autolathe" @@ -176,77 +179,65 @@ /obj/machinery/autolathe/Topic(href, href_list) if(..()) return 1 - if (!busy) - if(href_list["menu"]) - screen = text2num(href_list["menu"]) + if(href_list["menu"]) + screen = text2num(href_list["menu"]) - if(href_list["category"]) - selected_category = href_list["category"] + if(href_list["category"]) + selected_category = href_list["category"] - if(href_list["make"]) + if(href_list["make"]) + BuildTurf = get_step(src.loc, get_dir(src,usr)) - var/turf/T = get_step(src.loc, get_dir(src,usr)) + ///////////////// + //href protection + being_built = files.FindDesignByID(href_list["make"]) //check if it's a valid design + if(!being_built) + return + if(!(being_built.build_type & AUTOLATHE)) + return - ///////////////// - //href protection - being_built = files.FindDesignByID(href_list["make"]) //check if it's a valid design - if(!being_built) - return + //multiplier checks : only stacks can have one and its value is 1, 10 ,25 or max_multiplier + var/multiplier = text2num(href_list["multiplier"]) + var/max_multiplier = min(50, being_built.materials["$metal"] ?round(m_amount/being_built.materials["$metal"]):INFINITY,being_built.materials["$glass"]?round(g_amount/being_built.materials["$glass"]):INFINITY) + var/is_stack = ispath(being_built.build_path, /obj/item/stack) - //multiplier checks : only stacks can have one and its value is 1, 10 ,25 or max_multiplier - var/multiplier = text2num(href_list["multiplier"]) - var/max_multiplier = min(50, being_built.materials["$metal"] ?round(m_amount/being_built.materials["$metal"]):INFINITY,being_built.materials["$glass"]?round(g_amount/being_built.materials["$glass"]):INFINITY) - var/is_stack = ispath(being_built.build_path, /obj/item/stack) - - if(!is_stack && (multiplier > 1)) - return - if (!(multiplier in list(1,10,25,max_multiplier))) //"enough materials ?" is checked further down - return - ///////////////// - - var/coeff = (is_stack ? 1 : 2 ** prod_coeff) //stacks are unaffected by production coefficient - var/metal_cost = being_built.materials["$metal"] - var/glass_cost = being_built.materials["$glass"] - - var/power = max(2000, (metal_cost+glass_cost)*multiplier/5) - - if((m_amount >= metal_cost*multiplier/coeff) && (g_amount >= glass_cost*multiplier/coeff)) - busy = 1 - use_power(power) - icon_state = "autolathe" - flick("autolathe_n",src) - spawn(32/coeff) - use_power(power) - if(is_stack) - m_amount -= metal_cost*multiplier - g_amount -= glass_cost*multiplier - var/obj/item/stack/S = new being_built.build_path(T) - S.amount = multiplier - else - m_amount -= metal_cost/coeff - g_amount -= glass_cost/coeff - var/obj/item/new_item = new being_built.build_path(T) - new_item.m_amt /= coeff - new_item.g_amt /= coeff - if(m_amount < 0) - m_amount = 0 - if(g_amount < 0) - g_amount = 0 - busy = 0 - src.updateUsrDialog() - - if(href_list["search"]) - matching_designs.Cut() - - for(var/datum/design/D in files.known_designs) - if(findtext(D.name,href_list["to_search"])) - matching_designs.Add(D) - - else - usr << "The autolathe is busy. Please wait for completion of previous operation." + if(!is_stack && (multiplier > 1)) + return + if (!(multiplier in list(1,10,25,max_multiplier))) //"enough materials ?" is checked in the build proc + return + ///////////////// + + if(queue.len1) + output += "[initial(part.name)][is_stack?" (x[multiplier])":null] - [i>1?"":null] [i↓":null] Remove" + temp_metal = max(temp_metal-LL[1],1) + temp_glass = max(temp_glass-LL[2],1) + + output += "" + output += "Clear queue" + output += "" + return output + +/obj/machinery/autolathe/proc/add_to_queue(D,var/multiplier) + if(!istype(queue)) + queue = list() + if(D) + queue.Add(list(list(D,multiplier))) + return queue.len + +/obj/machinery/autolathe/proc/remove_from_queue(index) + if(!isnum(index) || !istype(queue) || (index<1 || index>queue.len)) + return 0 + queue.Cut(index,++index) + return 1 + +/obj/machinery/autolathe/proc/process_queue() + var/datum/design/D = queue[1][1] + var/multiplier = queue[1][2] + if(!D) + remove_from_queue(1) + if(queue.len) + return process_queue() + else + return + while(D) + if(stat&(NOPOWER|BROKEN)) + return 0 + if(!can_build(D,multiplier)) + visible_message("\icon[src] \The [src] beeps, \"Not enough resources. Queue processing terminated.\"") + queue = list() + return 0 + + remove_from_queue(1) + build_item(D,multiplier) + D = listgetindex(listgetindex(queue, 1),1) + multiplier = listgetindex(listgetindex(queue,1),2) + //visible_message("\icon[src] \The [src] beeps, \"Queue processing finished successfully.\"") + /obj/machinery/autolathe/proc/main_win(mob/user) - var/dat = "

Autolathe Menu:


" + var/dat = "" + dat += "
" + dat += "

Autolathe Menu:


" dat += "Metal amount: [src.m_amount] / [max_m_amount] cm3
" dat += "Glass amount: [src.g_amount] / [max_g_amount] cm3" @@ -285,11 +402,16 @@ line_length++ dat += "
" + dat += "" + dat += get_queue() + dat += "" return dat /obj/machinery/autolathe/proc/category_win(mob/user,var/selected_category) - var/dat = "Return to main menu" - dat += "

Browsing [selected_category]:


" + var/dat = "" + dat += get_queue() + dat += "
" + dat += "
" + dat += "Return to main menu" + dat += "

Browsing [selected_category]:


" dat += "Metal amount: [src.m_amount] / [max_m_amount] cm3
" dat += "Glass amount: [src.g_amount] / [max_g_amount] cm3
" @@ -314,11 +436,16 @@ dat += "[get_design_cost(D)]
" dat += "
" + dat += "
" return dat /obj/machinery/autolathe/proc/search_win(mob/user) - var/dat = "Return to main menu" - dat += "

Search results:


" + var/dat = "" + dat += get_queue() + dat += "
" + dat += "
" + dat += "Return to main menu" + dat += "

Search results:


" dat += "Metal amount: [src.m_amount] / [max_m_amount] cm3
" dat += "Glass amount: [src.g_amount] / [max_g_amount] cm3
" @@ -340,19 +467,13 @@ dat += "[get_design_cost(D)]
" dat += "
" + dat += "
" return dat -/obj/machinery/autolathe/proc/can_build(var/datum/design/D) - var/coeff = (ispath(D.build_path,/obj/item/stack) ? 1 : 2 ** prod_coeff) - - if(D.materials["$metal"] && (m_amount < (D.materials["$metal"] / coeff))) - return 0 - if(D.materials["$glass"] && (g_amount < (D.materials["$glass"] / coeff))) - return 0 - return 1 - /obj/machinery/autolathe/proc/get_design_cost(var/datum/design/D) - var/coeff = (ispath(D.build_path,/obj/item/stack) ? 1 : 2 ** prod_coeff) + var/coeff = get_coeff(D) var/dat if(D.materials["$metal"]) dat += "[D.materials["$metal"] / coeff] metal " @@ -378,7 +499,7 @@ if(hack) for(var/datum/design/D in files.possible_designs) - if((D.build_type & 4) && ("hacked" in D.category)) + if((D.build_type & AUTOLATHE) && ("hacked" in D.category)) files.known_designs += D else for(var/datum/design/D in files.known_designs)