mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2025-12-11 10:43:20 +00:00
Material Container Component & Autolathe QoL
This commit is contained in:
@@ -61,3 +61,15 @@
|
||||
#define DEFAULT_WALL_MATERIAL "steel"
|
||||
|
||||
#define TABLE_BRITTLE_MATERIAL_MULTIPLIER 4 // Amount table damage is multiplied by if it is made of a brittle material (e.g. glass)
|
||||
|
||||
//Material Container Flags.
|
||||
///If the container shows the amount of contained materials on examine.
|
||||
#define MATCONTAINER_EXAMINE (1<<0)
|
||||
///If the container cannot have materials inserted through attackby().
|
||||
#define MATCONTAINER_NO_INSERT (1<<1)
|
||||
///if the user can insert mats into the container despite the intent.
|
||||
#define MATCONTAINER_ANY_INTENT (1<<2)
|
||||
///if the user won't receive a warning when attacking the container with an unallowed item.
|
||||
#define MATCONTAINER_SILENT (1<<3)
|
||||
|
||||
#define GET_MATERIAL_REF(arguments...) _GetMaterialRef(list(##arguments))
|
||||
6
code/_global_vars/typecache.dm
Normal file
6
code/_global_vars/typecache.dm
Normal file
@@ -0,0 +1,6 @@
|
||||
//please store common type caches here.
|
||||
//type caches should only be stored here if used in mutiple places or likely to be used in mutiple places.
|
||||
|
||||
//Note: typecache can only replace istype if you know for sure the thing is at least a datum.
|
||||
|
||||
GLOBAL_LIST_INIT(typecache_stack, typecacheof(/obj/item/stack))
|
||||
@@ -38,6 +38,28 @@
|
||||
name = "All"
|
||||
category_item_type = /datum/category_item/autolathe
|
||||
|
||||
// Copypasted from materials, they should show in All
|
||||
/datum/category_group/autolathe/all/New()
|
||||
..()
|
||||
|
||||
for(var/Name in name_to_material)
|
||||
var/datum/material/M = name_to_material[Name]
|
||||
|
||||
if(!M.stack_type) // Shouldn't happen, but might. Never know.
|
||||
continue
|
||||
|
||||
if(istype(M, /datum/material/alienalloy))
|
||||
continue
|
||||
|
||||
var/obj/item/stack/material/S = M.stack_type
|
||||
if(initial(S.name) in items_by_name)
|
||||
continue
|
||||
|
||||
var/datum/category_item/autolathe/materials/WorkDat = new(src, M)
|
||||
|
||||
items |= WorkDat
|
||||
items_by_name[WorkDat.name] = WorkDat
|
||||
|
||||
///datum/category_group/autolathe/all/New()
|
||||
|
||||
/datum/category_group/autolathe/arms
|
||||
|
||||
484
code/datums/components/material_container.dm
Normal file
484
code/datums/components/material_container.dm
Normal file
@@ -0,0 +1,484 @@
|
||||
/*!
|
||||
This datum should be used for handling mineral contents of machines and whatever else is supposed to hold minerals and make use of them.
|
||||
|
||||
Variables:
|
||||
amount - raw amount of the mineral this container is holding, calculated by the defined value SHEET_MATERIAL_AMOUNT=2000.
|
||||
max_amount - max raw amount of mineral this container can hold.
|
||||
stack_type - type of the mineral sheet the container handles, used for output.
|
||||
parent - object that this container is being used by, used for output.
|
||||
MAX_STACK_SIZE - size of a stack of mineral sheets. Constant.
|
||||
*/
|
||||
|
||||
/datum/component/material_container
|
||||
/// The total amount of materials this material container contains
|
||||
var/total_amount = 0
|
||||
/// The maximum amount of materials this material container can contain
|
||||
var/max_amount
|
||||
/// Map of material ref -> amount
|
||||
var/list/materials //Map of key = material ref | Value = amount
|
||||
/// The list of materials that this material container can accept
|
||||
var/list/allowed_materials
|
||||
/// The typecache of things that this material container can accept
|
||||
var/list/allowed_item_typecache
|
||||
/// The last main material that was inserted into this container
|
||||
var/last_inserted_id
|
||||
/// A callback for checking wheter we can insert a material into this container
|
||||
var/datum/callback/insertion_check
|
||||
/// A callback invoked before materials are inserted into this container
|
||||
var/datum/callback/precondition
|
||||
/// A callback invoked after materials are inserted into this container
|
||||
var/datum/callback/after_insert
|
||||
/// The material container flags. See __DEFINES/materials.dm.
|
||||
var/mat_container_flags
|
||||
|
||||
/// Sets up the proper signals and fills the list of materials with the appropriate references.
|
||||
/datum/component/material_container/Initialize(list/init_mats, max_amt = 0, _mat_container_flags=NONE, list/allowed_mats=init_mats, list/allowed_items, datum/callback/_insertion_check, datum/callback/_precondition, datum/callback/_after_insert)
|
||||
if(!isatom(parent))
|
||||
return COMPONENT_INCOMPATIBLE
|
||||
|
||||
materials = list()
|
||||
max_amount = max(0, max_amt)
|
||||
mat_container_flags = _mat_container_flags
|
||||
|
||||
allowed_materials = allowed_mats || list()
|
||||
if(allowed_items)
|
||||
if(ispath(allowed_items) && allowed_items == /obj/item/stack)
|
||||
allowed_item_typecache = GLOB.typecache_stack
|
||||
else
|
||||
allowed_item_typecache = typecacheof(allowed_items)
|
||||
|
||||
insertion_check = _insertion_check
|
||||
precondition = _precondition
|
||||
after_insert = _after_insert
|
||||
|
||||
for(var/mat in init_mats) //Make the assoc list material reference -> amount
|
||||
var/mat_ref = GET_MATERIAL_REF(mat)
|
||||
if(isnull(mat_ref))
|
||||
continue
|
||||
var/mat_amt = init_mats[mat]
|
||||
if(isnull(mat_amt))
|
||||
mat_amt = 0
|
||||
materials[mat_ref] += mat_amt
|
||||
|
||||
/datum/component/material_container/Destroy(force, silent)
|
||||
materials = null
|
||||
allowed_materials = null
|
||||
if(insertion_check)
|
||||
QDEL_NULL(insertion_check)
|
||||
if(precondition)
|
||||
QDEL_NULL(precondition)
|
||||
if(after_insert)
|
||||
QDEL_NULL(after_insert)
|
||||
return ..()
|
||||
|
||||
|
||||
/datum/component/material_container/RegisterWithParent()
|
||||
. = ..()
|
||||
|
||||
if(!(mat_container_flags & MATCONTAINER_NO_INSERT))
|
||||
RegisterSignal(parent, COMSIG_PARENT_ATTACKBY, .proc/on_attackby)
|
||||
if(mat_container_flags & MATCONTAINER_EXAMINE)
|
||||
RegisterSignal(parent, COMSIG_PARENT_EXAMINE, .proc/on_examine)
|
||||
|
||||
|
||||
/datum/component/material_container/vv_edit_var(var_name, var_value)
|
||||
var/old_flags = mat_container_flags
|
||||
. = ..()
|
||||
if(var_name == NAMEOF(src, mat_container_flags) && parent)
|
||||
if(!(old_flags & MATCONTAINER_EXAMINE) && mat_container_flags & MATCONTAINER_EXAMINE)
|
||||
RegisterSignal(parent, COMSIG_PARENT_EXAMINE, .proc/on_examine)
|
||||
else if(old_flags & MATCONTAINER_EXAMINE && !(mat_container_flags & MATCONTAINER_EXAMINE))
|
||||
UnregisterSignal(parent, COMSIG_PARENT_EXAMINE)
|
||||
|
||||
if(old_flags & MATCONTAINER_NO_INSERT && !(mat_container_flags & MATCONTAINER_NO_INSERT))
|
||||
RegisterSignal(parent, COMSIG_PARENT_ATTACKBY, .proc/on_attackby)
|
||||
else if(!(old_flags & MATCONTAINER_NO_INSERT) && mat_container_flags & MATCONTAINER_NO_INSERT)
|
||||
UnregisterSignal(parent, COMSIG_PARENT_ATTACKBY)
|
||||
|
||||
|
||||
/datum/component/material_container/proc/on_examine(datum/source, mob/user, list/examine_texts)
|
||||
SIGNAL_HANDLER
|
||||
|
||||
for(var/I in materials)
|
||||
var/datum/material/M = I
|
||||
var/amt = materials[I]
|
||||
if(amt)
|
||||
examine_texts += "<span class='notice'>It has [amt] units of [lowertext(M.name)] stored.</span>"
|
||||
|
||||
/// Proc that allows players to fill the parent with mats
|
||||
/datum/component/material_container/proc/on_attackby(datum/source, obj/item/I, mob/living/user)
|
||||
SIGNAL_HANDLER
|
||||
|
||||
var/list/tc = allowed_item_typecache
|
||||
if(!(mat_container_flags & MATCONTAINER_ANY_INTENT) && user.a_intent != I_HELP)
|
||||
return
|
||||
if(tc && !is_type_in_typecache(I, tc))
|
||||
if(!(mat_container_flags & MATCONTAINER_SILENT))
|
||||
to_chat(user, "<span class='warning'>[parent] won't accept [I]!</span>")
|
||||
return
|
||||
. = COMPONENT_NO_AFTERATTACK
|
||||
var/datum/callback/pc = precondition
|
||||
if(pc && !pc.Invoke(user))
|
||||
return
|
||||
if(istype(I, /obj/item/stack))
|
||||
var/obj/item/stack/S = I
|
||||
user_insert_stack(S, user, mat_container_flags)
|
||||
return
|
||||
var/material_amount = get_item_material_amount(I, mat_container_flags)
|
||||
if(!material_amount)
|
||||
to_chat(user, "<span class='warning'>[I] does not contain sufficient materials to be accepted by [parent].</span>")
|
||||
return
|
||||
if(!has_space(material_amount))
|
||||
to_chat(user, "<span class='warning'>[parent] is full. Please remove materials from [parent] in order to insert more.</span>")
|
||||
return
|
||||
user_insert(I, user, mat_container_flags)
|
||||
|
||||
/// Proc used for when player inserts a stack
|
||||
/datum/component/material_container/proc/user_insert_stack(obj/item/stack/S, mob/living/user, breakdown_flags = mat_container_flags)
|
||||
var/sheets = S.get_amount()
|
||||
if(sheets < 1)
|
||||
to_chat(user, "<span class='warning'>[S] does not contain sufficient materials to be accepted by [parent].</span>")
|
||||
return
|
||||
|
||||
// Cache this since S may go away after use()
|
||||
var/list/sheet_matter = S.matter
|
||||
|
||||
// Calculate total amount of material for one sheet
|
||||
var/matter_per_sheet = 0
|
||||
for(var/material in sheet_matter)
|
||||
matter_per_sheet += sheet_matter[material]
|
||||
|
||||
// If any part of a sheet can't go in us, the whole sheet is invalid
|
||||
if(!can_hold_material(GET_MATERIAL_REF(material)))
|
||||
to_chat(user, "<span class='warning'>[parent] cannot contain [material].</span>")
|
||||
return
|
||||
|
||||
// If we can't fit the material for one sheet, we're full.
|
||||
if(!has_space(matter_per_sheet))
|
||||
to_chat(user, "<span class='warning'>[parent] is full. Please remove materials from [parent] in order to insert more.</span>")
|
||||
return
|
||||
|
||||
// Calculate the maximum amount of sheets we could possibly accept.
|
||||
var/max_sheets = round((max_amount - total_amount) / matter_per_sheet)
|
||||
if(max_sheets <= 0)
|
||||
to_chat(user, "<span class='warning'>[parent] is full. Please remove materials from [parent] in order to insert more.</span>")
|
||||
return
|
||||
|
||||
// Calculate the amount of sheets we're actually going to use.
|
||||
var/sheets_to_use = min(sheets, max_sheets)
|
||||
|
||||
// It shouldn't be possible to add more matter than our max
|
||||
ASSERT((total_amount + (matter_per_sheet * sheets_to_use)) <= max_amount)
|
||||
|
||||
// Use the amount of sheets from the stack
|
||||
if(!S.use(sheets_to_use))
|
||||
to_chat(user, "<span class='warning'>Something went wrong with your stack. Split it manually and try again.</span>")
|
||||
return
|
||||
|
||||
// We're going to blindly insert all of the materials, our assertion above says it shouldn't be possible to overflow
|
||||
var/inserted = 0
|
||||
for(var/matter in sheet_matter)
|
||||
var/datum/material/MAT = GET_MATERIAL_REF(matter)
|
||||
inserted += insert_amount_mat(sheet_matter[matter] * sheets_to_use, MAT)
|
||||
last_inserted_id = matter
|
||||
|
||||
// Tell the user and wrap up.
|
||||
to_chat(user, "<span class='notice'>You insert a material total of [inserted] into [parent].</span>")
|
||||
if(after_insert)
|
||||
after_insert.Invoke(S, last_inserted_id, inserted)
|
||||
|
||||
/// Proc used for when player inserts materials
|
||||
/datum/component/material_container/proc/user_insert(obj/item/I, mob/living/user, breakdown_flags = mat_container_flags)
|
||||
set waitfor = FALSE
|
||||
var/active_held = user.get_active_hand() // differs from I when using TK
|
||||
if(!user.unEquip(I))
|
||||
to_chat(user, "<span class='warning'>[I] is stuck to you and cannot be placed into [parent].</span>")
|
||||
return
|
||||
var/inserted = insert_item(I, breakdown_flags = mat_container_flags)
|
||||
if(inserted)
|
||||
to_chat(user, "<span class='notice'>You insert a material total of [inserted] into [parent].</span>")
|
||||
qdel(I)
|
||||
if(after_insert)
|
||||
after_insert.Invoke(I, last_inserted_id, inserted)
|
||||
else if(I == active_held)
|
||||
user.put_in_active_hand(I)
|
||||
|
||||
/// Proc specifically for inserting items, returns the amount of materials entered.
|
||||
/datum/component/material_container/proc/insert_item(obj/item/I, multiplier = 1, breakdown_flags = mat_container_flags)
|
||||
if(QDELETED(I))
|
||||
return FALSE
|
||||
|
||||
multiplier = CEILING(multiplier, 0.01)
|
||||
|
||||
var/material_amount = get_item_material_amount(I, breakdown_flags)
|
||||
if(!material_amount || !has_space(material_amount))
|
||||
return FALSE
|
||||
|
||||
last_inserted_id = insert_item_materials(I, multiplier, breakdown_flags)
|
||||
return material_amount
|
||||
|
||||
/**
|
||||
* Inserts the relevant materials from an item into this material container.
|
||||
*
|
||||
* Arguments:
|
||||
* - [source][/obj/item]: The source of the materials we are inserting.
|
||||
* - multiplier: The multiplier for the materials being inserted.
|
||||
* - breakdown_flags: The breakdown bitflags that will be used to retrieve the materials from the source
|
||||
*/
|
||||
/datum/component/material_container/proc/insert_item_materials(obj/item/source, multiplier = 1, breakdown_flags = mat_container_flags)
|
||||
var/primary_mat
|
||||
var/max_mat_value = 0
|
||||
var/list/item_materials = source.get_material_composition(breakdown_flags)
|
||||
for(var/MAT in item_materials)
|
||||
if(!can_hold_material(MAT))
|
||||
continue
|
||||
materials[MAT] += item_materials[MAT] * multiplier
|
||||
total_amount += item_materials[MAT] * multiplier
|
||||
if(item_materials[MAT] > max_mat_value)
|
||||
max_mat_value = item_materials[MAT]
|
||||
primary_mat = MAT
|
||||
|
||||
return primary_mat
|
||||
|
||||
/**
|
||||
* The default check for whether we can add materials to this material container.
|
||||
*
|
||||
* Arguments:
|
||||
* - [mat][/atom/material]: The material we are checking for insertability.
|
||||
*/
|
||||
/datum/component/material_container/proc/can_hold_material(datum/material/mat)
|
||||
if(mat in allowed_materials)
|
||||
return TRUE
|
||||
if(istype(mat) && ((mat.name in allowed_materials) || (mat.type in allowed_materials)))
|
||||
allowed_materials += mat // This could get messy with passing lists by ref... but if you're doing that the list expansion is probably being taken care of elsewhere anyway...
|
||||
return TRUE
|
||||
if(insertion_check?.Invoke(mat))
|
||||
allowed_materials += mat
|
||||
return TRUE
|
||||
return FALSE
|
||||
|
||||
/// For inserting an amount of material
|
||||
/datum/component/material_container/proc/insert_amount_mat(amt, datum/material/mat)
|
||||
if(amt <= 0 || !has_space(amt))
|
||||
return 0
|
||||
|
||||
var/total_amount_saved = total_amount
|
||||
if(mat)
|
||||
if(!istype(mat))
|
||||
mat = GET_MATERIAL_REF(mat)
|
||||
materials[mat] += amt
|
||||
total_amount += amt
|
||||
else
|
||||
var/num_materials = length(materials)
|
||||
if(!num_materials)
|
||||
return 0
|
||||
|
||||
amt /= num_materials
|
||||
for(var/i in materials)
|
||||
materials[i] += amt
|
||||
total_amount += amt
|
||||
return (total_amount - total_amount_saved)
|
||||
|
||||
/// Uses an amount of a specific material, effectively removing it.
|
||||
/datum/component/material_container/proc/use_amount_mat(amt, datum/material/mat)
|
||||
if(!istype(mat))
|
||||
mat = GET_MATERIAL_REF(mat)
|
||||
|
||||
if(!mat)
|
||||
return 0
|
||||
var/amount = materials[mat]
|
||||
if(amount < amt)
|
||||
return 0
|
||||
|
||||
materials[mat] -= amt
|
||||
total_amount -= amt
|
||||
return amt
|
||||
|
||||
/// Proc for transfering materials to another container.
|
||||
/datum/component/material_container/proc/transer_amt_to(datum/component/material_container/T, amt, datum/material/mat)
|
||||
if(!istype(mat))
|
||||
mat = GET_MATERIAL_REF(mat)
|
||||
if((amt==0)||(!T)||(!mat))
|
||||
return FALSE
|
||||
if(amt<0)
|
||||
return T.transer_amt_to(src, -amt, mat)
|
||||
var/tr = min(amt, materials[mat], T.can_insert_amount_mat(amt, mat))
|
||||
if(tr)
|
||||
use_amount_mat(tr, mat)
|
||||
T.insert_amount_mat(tr, mat)
|
||||
return tr
|
||||
return FALSE
|
||||
|
||||
/// Proc for checking if there is room in the component, returning the amount or else the amount lacking.
|
||||
/datum/component/material_container/proc/can_insert_amount_mat(amt, datum/material/mat)
|
||||
if(!amt || !mat)
|
||||
return 0
|
||||
|
||||
if((total_amount + amt) <= max_amount)
|
||||
return amt
|
||||
else
|
||||
return (max_amount - total_amount)
|
||||
|
||||
|
||||
/// For consuming a dictionary of materials. mats is the map of materials to use and the corresponding amounts, example: list(M/datum/material/glass =100, datum/material/iron=200)
|
||||
/datum/component/material_container/proc/use_materials(list/mats, multiplier=1)
|
||||
if(!mats || !length(mats))
|
||||
return FALSE
|
||||
|
||||
var/list/mats_to_remove = list() //Assoc list MAT | AMOUNT
|
||||
|
||||
for(var/x in mats) //Loop through all required materials
|
||||
var/datum/material/req_mat = x
|
||||
if(!istype(req_mat))
|
||||
req_mat = GET_MATERIAL_REF(req_mat) //Get the ref if necesary
|
||||
if(!materials[req_mat]) //Do we have the resource?
|
||||
return FALSE //Can't afford it
|
||||
var/amount_required = mats[x] * multiplier
|
||||
if(!(materials[req_mat] >= amount_required)) // do we have enough of the resource?
|
||||
return FALSE //Can't afford it
|
||||
mats_to_remove[req_mat] += amount_required //Add it to the assoc list of things to remove
|
||||
continue
|
||||
|
||||
var/total_amount_save = total_amount
|
||||
|
||||
for(var/i in mats_to_remove)
|
||||
total_amount_save -= use_amount_mat(mats_to_remove[i], i)
|
||||
|
||||
return total_amount_save - total_amount
|
||||
|
||||
/// For spawning mineral sheets at a specific location. Used by machines to output sheets.
|
||||
/datum/component/material_container/proc/retrieve_sheets(sheet_amt, datum/material/M, atom/target = null)
|
||||
if(!M.stack_type)
|
||||
return 0 //Add greyscale sheet handling here later
|
||||
if(sheet_amt <= 0)
|
||||
return 0
|
||||
|
||||
if(!target)
|
||||
var/atom/parent_atom = parent
|
||||
target = parent_atom.drop_location()
|
||||
if(materials[M] < (sheet_amt * SHEET_MATERIAL_AMOUNT))
|
||||
sheet_amt = round(materials[M] / SHEET_MATERIAL_AMOUNT)
|
||||
|
||||
var/obj/item/stack/S = M.stack_type
|
||||
var/max_stack_size = initial(S.max_amount)
|
||||
|
||||
var/count = 0
|
||||
while(sheet_amt > max_stack_size)
|
||||
new M.stack_type(target, max_stack_size, null, list((M) = SHEET_MATERIAL_AMOUNT))
|
||||
count += max_stack_size
|
||||
use_amount_mat(sheet_amt * SHEET_MATERIAL_AMOUNT, M)
|
||||
sheet_amt -= max_stack_size
|
||||
if(sheet_amt >= 1)
|
||||
new M.stack_type(target, sheet_amt, null, list((M) = SHEET_MATERIAL_AMOUNT))
|
||||
count += sheet_amt
|
||||
use_amount_mat(sheet_amt * SHEET_MATERIAL_AMOUNT, M)
|
||||
return count
|
||||
|
||||
|
||||
/// Proc to get all the materials and dump them as sheets
|
||||
/datum/component/material_container/proc/retrieve_all(target = null)
|
||||
var/result = 0
|
||||
for(var/MAT in materials)
|
||||
var/amount = materials[MAT]
|
||||
result += retrieve_sheets(amount2sheet(amount), MAT, target)
|
||||
return result
|
||||
|
||||
/// Proc that returns TRUE if the container has space
|
||||
/datum/component/material_container/proc/has_space(amt = 0)
|
||||
return (total_amount + amt) <= max_amount
|
||||
|
||||
/// Checks if its possible to afford a certain amount of materials. Takes a dictionary of materials.
|
||||
/datum/component/material_container/proc/has_materials(list/mats, multiplier=1)
|
||||
if(!mats || !mats.len)
|
||||
return FALSE
|
||||
|
||||
for(var/x in mats) //Loop through all required materials
|
||||
var/datum/material/req_mat = x
|
||||
if(!istype(req_mat))
|
||||
if(ispath(req_mat) || istext(req_mat)) //Is this an actual material, or is it a category?
|
||||
req_mat = GET_MATERIAL_REF(req_mat) //Get the ref
|
||||
|
||||
// else // Its a category. (For example MAT_CATEGORY_RIGID)
|
||||
// if(!has_enough_of_category(req_mat, mats[x], multiplier)) //Do we have enough of this category?
|
||||
// return FALSE
|
||||
// else
|
||||
// continue
|
||||
|
||||
if(!has_enough_of_material(req_mat, mats[x], multiplier))//Not a category, so just check the normal way
|
||||
return FALSE
|
||||
|
||||
return TRUE
|
||||
|
||||
/// Returns all the categories in a recipe.
|
||||
/datum/component/material_container/proc/get_categories(list/mats)
|
||||
var/list/categories = list()
|
||||
for(var/x in mats) //Loop through all required materials
|
||||
if(!istext(x)) //This means its not a category
|
||||
continue
|
||||
categories += x
|
||||
return categories
|
||||
|
||||
/// Returns TRUE if you have enough of the specified material.
|
||||
/datum/component/material_container/proc/has_enough_of_material(datum/material/req_mat, amount, multiplier=1)
|
||||
if(!materials[req_mat]) //Do we have the resource?
|
||||
return FALSE //Can't afford it
|
||||
var/amount_required = amount * multiplier
|
||||
if(materials[req_mat] >= amount_required) // do we have enough of the resource?
|
||||
return TRUE
|
||||
return FALSE //Can't afford it
|
||||
|
||||
/// Returns TRUE if you have enough of a specified material category (Which could be multiple materials)
|
||||
// /datum/component/material_container/proc/has_enough_of_category(category, amount, multiplier=1)
|
||||
// for(var/i in SSmaterials.materials_by_category[category])
|
||||
// var/datum/material/mat = i
|
||||
// if(materials[mat] >= amount) //we have enough
|
||||
// return TRUE
|
||||
// return FALSE
|
||||
|
||||
/// Turns a material amount into the amount of sheets it should output
|
||||
/datum/component/material_container/proc/amount2sheet(amt)
|
||||
if(amt >= SHEET_MATERIAL_AMOUNT)
|
||||
return round(amt / SHEET_MATERIAL_AMOUNT)
|
||||
return FALSE
|
||||
|
||||
/// Turns an amount of sheets into the amount of material amount it should output
|
||||
/datum/component/material_container/proc/sheet2amount(sheet_amt)
|
||||
if(sheet_amt > 0)
|
||||
return sheet_amt * SHEET_MATERIAL_AMOUNT
|
||||
return FALSE
|
||||
|
||||
|
||||
///returns the amount of material relevant to this container; if this container does not support glass, any glass in 'I' will not be taken into account
|
||||
/datum/component/material_container/proc/get_item_material_amount(obj/item/I, breakdown_flags = mat_container_flags)
|
||||
if(!istype(I) || !I.matter)
|
||||
return 0
|
||||
var/material_amount = 0
|
||||
var/list/item_materials = I.get_material_composition(breakdown_flags)
|
||||
for(var/MAT in item_materials)
|
||||
if(!can_hold_material(MAT))
|
||||
continue
|
||||
material_amount += item_materials[MAT]
|
||||
return material_amount
|
||||
|
||||
/// Returns the amount of a specific material in this container.
|
||||
/datum/component/material_container/proc/get_material_amount(datum/material/mat)
|
||||
if(!istype(mat))
|
||||
mat = GET_MATERIAL_REF(mat)
|
||||
return materials[mat]
|
||||
|
||||
/// List format is list(list(name = ..., amount = ..., ref = ..., etc.), list(...))
|
||||
/datum/component/material_container/tgui_data(mob/user)
|
||||
var/list/data = list()
|
||||
|
||||
for(var/datum/material/material as anything in materials)
|
||||
var/amount = materials[material]
|
||||
|
||||
data += list(list(
|
||||
"name" = material.name,
|
||||
"ref" = REF(material),
|
||||
"amount" = amount,
|
||||
"sheets" = round(amount / SHEET_MATERIAL_AMOUNT),
|
||||
"removable" = amount >= SHEET_MATERIAL_AMOUNT,
|
||||
))
|
||||
|
||||
return data
|
||||
@@ -13,8 +13,6 @@
|
||||
circuit = /obj/item/weapon/circuitboard/autolathe
|
||||
|
||||
var/static/datum/category_collection/autolathe/autolathe_recipes
|
||||
var/list/stored_material = list(DEFAULT_WALL_MATERIAL = 0, MAT_GLASS = 0, MAT_PLASTEEL = 0, MAT_PLASTIC = 0)
|
||||
var/list/storage_capacity = list(DEFAULT_WALL_MATERIAL = 0, MAT_GLASS = 0, MAT_PLASTEEL = 0, MAT_PLASTIC = 0)
|
||||
|
||||
var/hacked = 0
|
||||
var/disabled = 0
|
||||
@@ -35,18 +33,12 @@
|
||||
var/filtertext
|
||||
|
||||
/obj/machinery/autolathe/Initialize()
|
||||
AddComponent(/datum/component/material_container, subtypesof(/datum/material), 0, MATCONTAINER_EXAMINE, _after_insert = CALLBACK(src, .proc/AfterMaterialInsert))
|
||||
. = ..()
|
||||
if(!autolathe_recipes)
|
||||
autolathe_recipes = new()
|
||||
wires = new(src)
|
||||
|
||||
for(var/Name in name_to_material)
|
||||
if(Name in stored_material)
|
||||
continue
|
||||
|
||||
stored_material[Name] = 0
|
||||
storage_capacity[Name] = 0
|
||||
|
||||
default_apply_parts()
|
||||
RefreshParts()
|
||||
|
||||
@@ -85,6 +77,7 @@
|
||||
"requirements" = M.resources,
|
||||
"hidden" = M.hidden,
|
||||
"coeff_applies" = !M.no_scale,
|
||||
"is_stack" = M.is_stack,
|
||||
)))
|
||||
data["recipes"] = recipes
|
||||
data["categories"] = categories
|
||||
@@ -98,19 +91,9 @@
|
||||
|
||||
/obj/machinery/autolathe/tgui_data(mob/user, datum/tgui/ui, datum/tgui_state/state)
|
||||
var/list/data = ..()
|
||||
|
||||
var/list/material_data = list()
|
||||
for(var/mat_id in stored_material)
|
||||
var/amount = stored_material[mat_id]
|
||||
var/list/material_info = list(
|
||||
"name" = mat_id,
|
||||
"amount" = amount,
|
||||
"sheets" = round(amount / SHEET_MATERIAL_AMOUNT),
|
||||
"removable" = amount >= SHEET_MATERIAL_AMOUNT
|
||||
)
|
||||
material_data += list(material_info)
|
||||
data["busy"] = busy
|
||||
data["materials"] = material_data
|
||||
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
|
||||
data["materials"] = materials.tgui_data()
|
||||
data["mat_efficiency"] = mat_efficiency
|
||||
return data
|
||||
|
||||
@@ -149,70 +132,14 @@
|
||||
wires.Interact(user)
|
||||
return
|
||||
|
||||
if(O.loc != user && !(istype(O,/obj/item/stack)))
|
||||
return 0
|
||||
|
||||
if(is_robot_module(O))
|
||||
return 0
|
||||
|
||||
if(istype(O,/obj/item/ammo_magazine/clip) || istype(O,/obj/item/ammo_magazine/s357) || istype(O,/obj/item/ammo_magazine/s38) || istype (O,/obj/item/ammo_magazine/s44)/* VOREstation Edit*/) // Prevents ammo recycling exploit with speedloaders.
|
||||
to_chat(user, "\The [O] is too hazardous to recycle with the autolathe!")
|
||||
return
|
||||
|
||||
//Resources are being loaded.
|
||||
var/obj/item/eating = O
|
||||
if(!eating.matter)
|
||||
to_chat(user, "\The [eating] does not contain significant amounts of useful materials and cannot be accepted.")
|
||||
return
|
||||
|
||||
var/filltype = 0 // Used to determine message.
|
||||
var/total_used = 0 // Amount of material used.
|
||||
var/mass_per_sheet = 0 // Amount of material constituting one sheet.
|
||||
|
||||
for(var/material in eating.matter)
|
||||
|
||||
if(isnull(stored_material[material]) || isnull(storage_capacity[material]))
|
||||
continue
|
||||
|
||||
if(stored_material[material] >= storage_capacity[material])
|
||||
continue
|
||||
|
||||
var/total_material = eating.matter[material]
|
||||
|
||||
//If it's a stack, we eat multiple sheets.
|
||||
if(istype(eating,/obj/item/stack))
|
||||
var/obj/item/stack/stack = eating
|
||||
total_material *= stack.get_amount()
|
||||
|
||||
if(stored_material[material] + total_material > storage_capacity[material])
|
||||
total_material = storage_capacity[material] - stored_material[material]
|
||||
filltype = 1
|
||||
else
|
||||
filltype = 2
|
||||
|
||||
stored_material[material] += total_material
|
||||
total_used += total_material
|
||||
mass_per_sheet += eating.matter[material]
|
||||
|
||||
if(!filltype)
|
||||
to_chat(user, "<span class='notice'>\The [src] is full. Please remove material from the autolathe in order to insert more.</span>")
|
||||
return
|
||||
else if(filltype == 1)
|
||||
to_chat(user, "You fill \the [src] to capacity with \the [eating].")
|
||||
else
|
||||
to_chat(user, "You fill \the [src] with \the [eating].")
|
||||
|
||||
flick("autolathe_loading", src) // Plays metal insertion animation. Work out a good way to work out a fitting animation. ~Z
|
||||
|
||||
if(istype(eating,/obj/item/stack))
|
||||
var/obj/item/stack/stack = eating
|
||||
stack.use(max(1, round(total_used/mass_per_sheet))) // Always use at least 1 to prevent infinite materials.
|
||||
else
|
||||
user.remove_from_mob(O)
|
||||
qdel(O)
|
||||
|
||||
updateUsrDialog()
|
||||
return
|
||||
|
||||
return ..()
|
||||
|
||||
/obj/machinery/autolathe/attack_hand(mob/user as mob)
|
||||
user.set_machine(src)
|
||||
@@ -236,36 +163,42 @@
|
||||
if(making.hidden && !hacked)
|
||||
return
|
||||
|
||||
var/multiplier = 1
|
||||
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
|
||||
|
||||
var/list/materials_used = list()
|
||||
|
||||
var/multiplier = (params["multiplier"] || 1)
|
||||
|
||||
if(making.is_stack)
|
||||
var/max_sheets
|
||||
for(var/material in making.resources)
|
||||
var/coeff = (making.no_scale ? 1 : mat_efficiency) //stacks are unaffected by production coefficient
|
||||
var/sheets = round(stored_material[material]/round(making.resources[material]*coeff))
|
||||
var/sheets = round(materials.get_material_amount(material) / round(making.resources[material] * coeff))
|
||||
if(isnull(max_sheets) || max_sheets > sheets)
|
||||
max_sheets = sheets
|
||||
if(!isnull(stored_material[material]) && stored_material[material] < round(making.resources[material]*coeff))
|
||||
if(!isnull(materials.get_material_amount(material)) && materials.get_material_amount(material) < round(making.resources[material] * coeff))
|
||||
max_sheets = 0
|
||||
//Build list of multipliers for sheets.
|
||||
multiplier = input(usr, "How many do you want to print? (0-[max_sheets])") as num|null
|
||||
if(!multiplier || multiplier <= 0 || multiplier > max_sheets || tgui_status(usr, state) != STATUS_INTERACTIVE)
|
||||
return FALSE
|
||||
|
||||
busy = making.name
|
||||
update_use_power(USE_POWER_ACTIVE)
|
||||
|
||||
//Check if we still have the materials.
|
||||
var/coeff = (making.no_scale ? 1 : mat_efficiency) //stacks are unaffected by production coefficient
|
||||
for(var/material in making.resources)
|
||||
if(!isnull(stored_material[material]))
|
||||
if(stored_material[material] < round(making.resources[material] * coeff) * multiplier)
|
||||
return
|
||||
|
||||
for(var/MAT in making.resources)
|
||||
var/datum/material/used_material = MAT
|
||||
var/amount_needed = making.resources[MAT] * coeff * multiplier
|
||||
materials_used[used_material] = amount_needed
|
||||
|
||||
//Consume materials.
|
||||
for(var/material in making.resources)
|
||||
if(!isnull(stored_material[material]))
|
||||
stored_material[material] = max(0, stored_material[material] - round(making.resources[material] * coeff) * multiplier)
|
||||
if(LAZYLEN(materials_used))
|
||||
if(!materials.has_materials(materials_used))
|
||||
return
|
||||
|
||||
materials.use_materials(materials_used)
|
||||
|
||||
busy = making.name
|
||||
update_use_power(USE_POWER_ACTIVE)
|
||||
|
||||
update_icon() // So lid closes
|
||||
|
||||
@@ -332,14 +265,15 @@
|
||||
for(var/obj/item/weapon/stock_parts/manipulator/M in component_parts)
|
||||
man_rating += M.rating
|
||||
|
||||
for(var/mat_name in storage_capacity)
|
||||
storage_capacity[mat_name] = mb_rating * 25000
|
||||
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
|
||||
materials.max_amount = mb_rating * 75000
|
||||
|
||||
build_time = 50 / man_rating
|
||||
mat_efficiency = 1.1 - man_rating * 0.1// Normally, price is 1.25 the amount of material, so this shouldn't go higher than 0.6. Maximum rating of parts is 5
|
||||
update_tgui_static_data(usr)
|
||||
|
||||
/obj/machinery/autolathe/dismantle()
|
||||
<<<<<<< HEAD
|
||||
for(var/mat in stored_material)
|
||||
var/datum/material/M = get_material_by_name(mat)
|
||||
if(!istype(M))
|
||||
@@ -450,3 +384,48 @@
|
||||
input_dir = WEST
|
||||
to_chat(src, "You set the material input to [input_dir_name]")
|
||||
//YWAdd END.
|
||||
||||||| parent of 6dc1c76a74... Merge pull request #10631 from ShadowLarkens/material_container
|
||||
for(var/mat in stored_material)
|
||||
var/datum/material/M = get_material_by_name(mat)
|
||||
if(!istype(M))
|
||||
continue
|
||||
var/obj/item/stack/material/S = new M.stack_type(get_turf(src))
|
||||
if(stored_material[mat] > S.perunit)
|
||||
S.amount = round(stored_material[mat] / S.perunit)
|
||||
else
|
||||
qdel(S)
|
||||
..()
|
||||
return 1
|
||||
|
||||
/obj/machinery/autolathe/proc/eject_materials(var/material, var/amount) // 0 amount = 0 means ejecting a full stack; -1 means eject everything
|
||||
var/recursive = amount == -1 ? 1 : 0
|
||||
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(stored_material[matstring] / S.perunit), amount)
|
||||
S.amount = min(ejected, amount)
|
||||
if(S.amount <= 0)
|
||||
qdel(S)
|
||||
return
|
||||
stored_material[matstring] -= ejected * S.perunit
|
||||
if(recursive && stored_material[matstring] >= S.perunit)
|
||||
eject_materials(matstring, -1)
|
||||
=======
|
||||
var/datum/component/material_container/materials = GetComponent(/datum/component/material_container)
|
||||
materials.retrieve_all()
|
||||
return ..()
|
||||
|
||||
/obj/machinery/autolathe/proc/AfterMaterialInsert(obj/item/item_inserted, id_inserted, amount_inserted)
|
||||
flick("autolathe_loading", src)//plays metal insertion animation
|
||||
// use_power(min(1000, amount_inserted / 100))
|
||||
SStgui.update_uis(src)
|
||||
|
||||
/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>[mat_efficiency*100]%</b>.</span>"
|
||||
>>>>>>> 6dc1c76a74... Merge pull request #10631 from ShadowLarkens/material_container
|
||||
|
||||
@@ -236,8 +236,6 @@
|
||||
if(!uses_charge)
|
||||
amount -= used
|
||||
if (amount <= 0)
|
||||
if(usr)
|
||||
usr.remove_from_mob(src)
|
||||
qdel(src) //should be safe to qdel immediately since if someone is still using this stack it will persist for a little while longer
|
||||
update_icon()
|
||||
return 1
|
||||
|
||||
@@ -49,6 +49,24 @@ var/list/name_to_material
|
||||
if(material)
|
||||
return material.name
|
||||
|
||||
|
||||
/**
|
||||
* Returns the material composition of the atom.
|
||||
*
|
||||
* Used when recycling items, specifically to turn alloys back into their component mats.
|
||||
*
|
||||
* Exists because I'd need to add a way to un-alloy alloys or otherwise deal
|
||||
* with people converting the entire stations material supply into alloys.
|
||||
*
|
||||
* Arguments:
|
||||
* - breakdown_flags: A set of flags determining how exactly the materials are broken down. (unused)
|
||||
*/
|
||||
/obj/proc/get_material_composition(breakdown_flags=NONE)
|
||||
. = list()
|
||||
for(var/mat in matter)
|
||||
var/datum/material/M = GET_MATERIAL_REF(mat)
|
||||
.[M] = matter[mat]
|
||||
|
||||
// Builds the datum list above.
|
||||
/proc/populate_material_list(force_remake=0)
|
||||
if(name_to_material && !force_remake) return // Already set up!
|
||||
@@ -72,6 +90,73 @@ var/list/name_to_material
|
||||
return material.display_name
|
||||
return null
|
||||
|
||||
/** Fetches a cached material singleton when passed sufficient arguments.
|
||||
*
|
||||
* Arguments:
|
||||
* - [arguments][/list]: The list of arguments used to fetch the material ref.
|
||||
* - The first element is a material datum, text string, or material type.
|
||||
* - [Material datums][/datum/material] are assumed to be references to the cached datum and are returned
|
||||
* - Text is assumed to be the text ID of a material and the corresponding material is fetched from the cache
|
||||
* - A material type is checked for bespokeness:
|
||||
* - If the material type is not bespoke the type is assumed to be the id for a material and the corresponding material is loaded from the cache.
|
||||
* - If the material type is bespoke a text ID is generated from the arguments list and used to load a material datum from the cache.
|
||||
* - The following elements are used to generate bespoke IDs
|
||||
*/
|
||||
/proc/_GetMaterialRef(list/arguments)
|
||||
if(!name_to_material)
|
||||
populate_material_list()
|
||||
|
||||
var/datum/material/key = arguments[1]
|
||||
if(istype(key))
|
||||
return key // we want to convert anything we're given to a material
|
||||
|
||||
if(istext(key)) // text ID
|
||||
. = name_to_material[key]
|
||||
if(!.)
|
||||
warning("Attempted to fetch material ref with invalid text id '[key]'")
|
||||
return
|
||||
|
||||
if(!ispath(key, /datum/material))
|
||||
CRASH("Attempted to fetch material ref with invalid key [key]")
|
||||
|
||||
key = GetIdFromArguments(arguments)
|
||||
. = name_to_material[key]
|
||||
if(!.)
|
||||
warning("Attempted to fetch nonexistent material with key [key]")
|
||||
|
||||
/** I'm not going to lie, this was swiped from [SSdcs][/datum/controller/subsystem/processing/dcs].
|
||||
* Credit does to ninjanomnom
|
||||
*
|
||||
* Generates an id for bespoke ~~elements~~ materials when given the argument list
|
||||
* Generating the id here is a bit complex because we need to support named arguments
|
||||
* Named arguments can appear in any order and we need them to appear after ordered arguments
|
||||
* We assume that no one will pass in a named argument with a value of null
|
||||
**/
|
||||
/proc/GetIdFromArguments(list/arguments)
|
||||
var/datum/material/mattype = arguments[1]
|
||||
var/list/fullid = list("[initial(mattype.name) || mattype]")
|
||||
var/list/named_arguments = list()
|
||||
for(var/i in 2 to length(arguments))
|
||||
var/key = arguments[i]
|
||||
var/value
|
||||
if(istext(key))
|
||||
value = arguments[key]
|
||||
if(!(istext(key) || isnum(key)))
|
||||
key = REF(key)
|
||||
key = "[key]" // Key is stringified so numbers dont break things
|
||||
if(!isnull(value))
|
||||
if(!(istext(value) || isnum(value)))
|
||||
value = REF(value)
|
||||
named_arguments["[key]"] = value
|
||||
else
|
||||
fullid += "[key]"
|
||||
|
||||
if(length(named_arguments))
|
||||
named_arguments = sortList(named_arguments)
|
||||
fullid += named_arguments
|
||||
return replacetext(list2params(fullid), "+", " ")
|
||||
|
||||
|
||||
// Material definition and procs follow.
|
||||
/datum/material
|
||||
var/name // Unique name for use in indexing the list.
|
||||
@@ -131,7 +216,7 @@ var/list/name_to_material
|
||||
var/tableslam_noise = 'sound/weapons/tablehit1.ogg'
|
||||
// Noise made when a simple door made of this material opens or closes.
|
||||
var/dooropen_noise = 'sound/effects/stonedoor_openclose.ogg'
|
||||
// Path to resulting stacktype. Todo remove need for this.
|
||||
// Path to resulting stacktype.
|
||||
var/stack_type
|
||||
// Wallrot crumble message.
|
||||
var/rotting_touch_message = "crumbles under your touch"
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Window } from "../layouts";
|
||||
import { Materials } from "./ExosuitFabricator";
|
||||
import { createSearch, toTitleCase } from 'common/string';
|
||||
|
||||
const canBeMade = (recipe, materials) => {
|
||||
const canBeMade = (recipe, materials, mult = 1) => {
|
||||
if (recipe.requirements === null) {
|
||||
return true;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ const canBeMade = (recipe, materials) => {
|
||||
if (!material) {
|
||||
continue; // yes, if we cannot find the material, we just ignore it :V
|
||||
}
|
||||
if (material.amount < recipe.requirements[mat_id]) {
|
||||
if (material.amount < (recipe.requirements[mat_id] * mult)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -77,10 +77,26 @@ export const Autolathe = (props, context) => {
|
||||
color={recipe.hidden && "red" || null}
|
||||
icon="hammer"
|
||||
iconSpin={busy === recipe.name}
|
||||
disabled={!canBeMade(recipe, materials)}
|
||||
disabled={!canBeMade(recipe, materials, 1)}
|
||||
onClick={() => act("make", { make: recipe.ref })}>
|
||||
{toTitleCase(recipe.name)}
|
||||
</Button>
|
||||
{!recipe.is_stack && (
|
||||
<Box as="span">
|
||||
<Button
|
||||
color={recipe.hidden && "red" || null}
|
||||
disabled={!canBeMade(recipe, materials, 5)}
|
||||
onClick={() => act("make", { make: recipe.ref, multiplier: 5 })}>
|
||||
x5
|
||||
</Button>
|
||||
<Button
|
||||
color={recipe.hidden && "red" || null}
|
||||
disabled={!canBeMade(recipe, materials, 10)}
|
||||
onClick={() => act("make", { make: recipe.ref, multiplier: 10 })}>
|
||||
x10
|
||||
</Button>
|
||||
</Box>
|
||||
) || null}
|
||||
</Flex.Item>
|
||||
<Flex.Item>
|
||||
{recipe.requirements && (
|
||||
|
||||
@@ -318,10 +318,22 @@ export const Materials = (props, context) => {
|
||||
|
||||
const materials = data.materials || [];
|
||||
|
||||
let display_materials = materials.filter(mat => displayAllMat || mat.amount > 0);
|
||||
|
||||
if (display_materials.length === 0) {
|
||||
return (
|
||||
<Box textAlign="center">
|
||||
<Icon textAlign="center" size={5} name="inbox" />
|
||||
<br />
|
||||
<b>No Materials Loaded.</b>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Flex
|
||||
wrap="wrap">
|
||||
{materials.map(material => (displayAllMat || material.amount > 0) && (
|
||||
{display_materials.map(material => (
|
||||
<Flex.Item
|
||||
width="80px"
|
||||
key={material.name}>
|
||||
|
||||
File diff suppressed because one or more lines are too long
8066
vorestation.dme
8066
vorestation.dme
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user