From 8c90e32ef3a94ad80ff3d83367f7bbd92ec21c81 Mon Sep 17 00:00:00 2001 From: Aronai Sieyes Date: Thu, 26 Aug 2021 19:19:07 -0400 Subject: [PATCH] Fix annoying runtime with exosuit fab --- code/game/mecha/mech_fabricator.dm | 41 +++++++++++++++++---- code/game/objects/items/stacks/stack.dm | 15 +++++--- code/modules/research/protolathe.dm | 47 +++++++++++++++++-------- 3 files changed, 78 insertions(+), 25 deletions(-) diff --git a/code/game/mecha/mech_fabricator.dm b/code/game/mecha/mech_fabricator.dm index 8f0aabdf7c..4e92255d05 100644 --- a/code/game/mecha/mech_fabricator.dm +++ b/code/game/mecha/mech_fabricator.dm @@ -451,6 +451,7 @@ if(..()) return if(!allowed(user)) + to_chat(user, SPAN_WARNING("\The [src] rejects your use due to lack of access!")) return tgui_interact(user) @@ -667,14 +668,42 @@ /obj/machinery/mecha_part_fabricator/proc/eject_materials(var/material, var/amount) // 0 amount = 0 means ejecting a full stack; -1 means eject everything var/recursive = amount == -1 ? TRUE : FALSE var/matstring = lowertext(material) - var/datum/material/M = get_material_by_name(matstring) - var/obj/item/stack/material/S = M.place_sheet(get_turf(src)) - if(amount <= 0) - amount = S.max_amount - var/ejected = min(round(materials[matstring] / S.perunit), amount) - if(!S.set_amount(ejected, amount)) + // 0 or null, nothing to eject + if(!materials[matstring]) return + // Problem, fix problem and abort + if(materials[matstring] < 0) + warning("[src] tried to eject material '[material]', which it has 'materials[matstring]' of!") + materials[matstring] = 0 + return + + // Find the material datum for our material + var/datum/material/M = get_material_by_name(matstring) + if(!M) + warning("[src] tried to eject material '[matstring]', which didn't match any known material datum!") + return + // Find what type of sheets it makes + var/obj/item/stack/material/S = M.stack_type + if(!S) + warning("[src] tried to eject material '[matstring]', which didn't have a stack_type!") + return + + // If we were passed -1, then it's recursive ejection and we should eject all we can + if(amount <= 0) + amount = initial(S.max_amount) + // Smaller of what we have left, or the desired amount (note the amount is in sheets, but the array stores perunit values) + var/ejected = min(round(materials[matstring] / initial(S.perunit)), amount) + + // Place a sheet + S = M.place_sheet(get_turf(src), ejected) + if(!istype(S)) + warning("[src] tried to eject material '[material]', which didn't generate a proper stack when asked!") + return + + // Reduce our amount stored materials[matstring] -= ejected * S.perunit + + // Recurse if we have enough left for more sheets if(recursive && materials[matstring] >= S.perunit) eject_materials(matstring, -1) diff --git a/code/game/objects/items/stacks/stack.dm b/code/game/objects/items/stacks/stack.dm index 7dfa0ed492..2ea9ad0667 100644 --- a/code/game/objects/items/stacks/stack.dm +++ b/code/game/objects/items/stacks/stack.dm @@ -29,14 +29,19 @@ var/pass_color = FALSE // Will the item pass its own color var to the created item? Dyed cloth, wood, etc. var/strict_color_stacking = FALSE // Will the stack merge with other stacks that are different colors? (Dyed cloth, wood, etc) -/obj/item/stack/Initialize(var/ml, var/amount) +/obj/item/stack/Initialize(var/ml, var/starting_amount) . = ..() if(!stacktype) stacktype = type - if(!isnull(amount)) // Could be 0 - if(amount < 0) - amount = max_amount - set_amount(amount, TRUE) + if(!isnull(starting_amount)) // Could be 0 + // Negative numbers are 'give full stack', like -1 + if(starting_amount < 0) + // But sometimes a coder forgot to define what that even means + if(max_amount) + starting_amount = max_amount + else + starting_amount = 1 + set_amount(starting_amount, TRUE) update_icon() /obj/item/stack/Destroy() diff --git a/code/modules/research/protolathe.dm b/code/modules/research/protolathe.dm index a9e9f93ae4..7a5b17dca4 100644 --- a/code/modules/research/protolathe.dm +++ b/code/modules/research/protolathe.dm @@ -234,24 +234,43 @@ /obj/machinery/r_n_d/protolathe/proc/eject_materials(var/material, var/amount) // 0 amount = 0 means ejecting a full stack; -1 means eject everything var/recursive = amount == -1 ? TRUE : FALSE - material = lowertext(material) - var/obj/item/stack/material/mattype - var/datum/material/MAT = get_material_by_name(material) + var/matstring = lowertext(material) - if(!MAT) + // 0 or null, nothing to eject + if(!materials[matstring]) + return + // Problem, fix problem and abort + if(materials[matstring] < 0) + warning("[src] tried to eject material '[material]', which it has 'materials[matstring]' of!") + materials[matstring] = 0 return - mattype = MAT.stack_type - - if(!mattype) + // Find the material datum for our material + var/datum/material/M = get_material_by_name(matstring) + if(!M) + warning("[src] tried to eject material '[matstring]', which didn't match any known material datum!") + return + // Find what type of sheets it makes + var/obj/item/stack/material/S = M.stack_type + if(!S) + warning("[src] tried to eject material '[matstring]', which didn't have a stack_type!") return - var/obj/item/stack/material/S = new mattype(loc) + // If we were passed -1, then it's recursive ejection and we should eject all we can if(amount <= 0) - amount = S.max_amount - var/ejected = min(round(materials[material] / S.perunit), amount) - if(!S.set_amount(ejected, amount)) + amount = initial(S.max_amount) + // Smaller of what we have left, or the desired amount (note the amount is in sheets, but the array stores perunit values) + var/ejected = min(round(materials[matstring] / initial(S.perunit)), amount) + + // Place a sheet + S = M.place_sheet(get_turf(src), ejected) + if(!istype(S)) + warning("[src] tried to eject material '[material]', which didn't generate a proper stack when asked!") return - materials[material] -= ejected * S.perunit - if(recursive && materials[material] >= S.perunit) - eject_materials(material, -1) + + // Reduce our amount stored + materials[matstring] -= ejected * S.perunit + + // Recurse if we have enough left for more sheets + if(recursive && materials[matstring] >= S.perunit) + eject_materials(matstring, -1)