Makes the global materials lookup lazily initialized

This commit is contained in:
Emmett Gaines
2020-02-03 10:17:13 -05:00
parent b93781eb71
commit ab3a127295
18 changed files with 56 additions and 61 deletions

View File

@@ -4,10 +4,6 @@
/// Hard materials, such as iron or metal
#define MAT_CATEGORY_RIGID "rigid material"
/// Gets the reference for the material type that was given
#define getmaterialref(A) (SSmaterials.materials[A] || A)
/// Flag for atoms, this flag ensures it isn't re-colored by materials. Useful for snowflake icons such as default toolboxes.
#define MATERIAL_COLOR (1<<0)
#define MATERIAL_ADD_PREFIX (1<<1)

View File

@@ -107,7 +107,6 @@
#define INIT_ORDER_INPUT 85
#define INIT_ORDER_VIS 80
#define INIT_ORDER_ACHIEVEMENTS 77
#define INIT_ORDER_MATERIALS 76
#define INIT_ORDER_RESEARCH 75
#define INIT_ORDER_EVENTS 70
#define INIT_ORDER_JOBS 65

View File

@@ -7,8 +7,7 @@ These materials call on_applied() on whatever item they are applied to, common e
SUBSYSTEM_DEF(materials)
name = "Materials"
flags = SS_NO_FIRE
init_order = INIT_ORDER_MATERIALS
flags = SS_NO_FIRE | SS_NO_INIT
///Dictionary of material.type || material ref
var/list/materials = list()
///Dictionary of category || list of material refs
@@ -20,14 +19,15 @@ SUBSYSTEM_DEF(materials)
new /datum/stack_recipe("sink", /obj/structure/sink/greyscale, one_per_turf = TRUE, on_floor = TRUE, applies_mats = TRUE),
)
/datum/controller/subsystem/materials/Initialize(timeofday)
InitializeMaterials()
return ..()
///Ran on initialize, populated the materials and materials_by_category dictionaries with their appropiate vars (See these variables for more info)
/datum/controller/subsystem/materials/proc/InitializeMaterials(timeofday)
/datum/controller/subsystem/materials/proc/InitializeMaterials()
for(var/type in subtypesof(/datum/material))
var/datum/material/ref = new type
materials[type] = ref
for(var/c in ref.categories)
materials_by_category[c] += list(ref)
/datum/controller/subsystem/materials/proc/GetMaterialRef(datum/material/fakemat)
if(!materials)
InitializeMaterials()
return materials[fakemat] || fakemat

View File

@@ -42,7 +42,7 @@
RegisterSignal(parent, COMSIG_PARENT_EXAMINE, .proc/OnExamine)
for(var/mat in mat_list) //Make the assoc list ref | amount
var/datum/material/M = getmaterialref(mat) || mat
var/datum/material/M = SSmaterials.GetMaterialRef(mat)
materials[M] = 0
/datum/component/material_container/proc/OnExamine(datum/source, mob/user)
@@ -130,7 +130,7 @@
/// For inserting an amount of material
/datum/component/material_container/proc/insert_amount_mat(amt, var/datum/material/mat)
if(!istype(mat))
mat = getmaterialref(mat)
mat = SSmaterials.GetMaterialRef(mat)
if(amt > 0 && has_space(amt))
var/total_amount_saved = total_amount
if(mat)
@@ -145,7 +145,7 @@
/// Uses an amount of a specific material, effectively removing it.
/datum/component/material_container/proc/use_amount_mat(amt, var/datum/material/mat)
if(!istype(mat))
mat = getmaterialref(mat)
mat = SSmaterials.GetMaterialRef(mat)
var/amount = materials[mat]
if(mat)
if(amount >= amt)
@@ -157,7 +157,7 @@
/// Proc for transfering materials to another container.
/datum/component/material_container/proc/transer_amt_to(var/datum/component/material_container/T, amt, var/datum/material/mat)
if(!istype(mat))
mat = getmaterialref(mat)
mat = SSmaterials.GetMaterialRef(mat)
if((amt==0)||(!T)||(!mat))
return FALSE
if(amt<0)
@@ -190,7 +190,7 @@
for(var/x in mats) //Loop through all required materials
var/datum/material/req_mat = x
if(!istype(req_mat))
req_mat = getmaterialref(req_mat) //Get the ref if necesary
req_mat = SSmaterials.GetMaterialRef(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
@@ -251,7 +251,7 @@
var/datum/material/req_mat = x
if(!istype(req_mat))
if(ispath(req_mat)) //Is this an actual material, or is it a category?
req_mat = getmaterialref(req_mat) //Get the ref
req_mat = SSmaterials.GetMaterialRef(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?
@@ -316,5 +316,5 @@
/// Returns the amount of a specific material in this container.
/datum/component/material_container/proc/get_material_amount(var/datum/material/mat)
if(!istype(mat))
mat = getmaterialref(mat)
mat = SSmaterials.GetMaterialRef(mat)
return(materials[mat])

View File

@@ -161,7 +161,7 @@
var/temp_list = list()
for(var/i in custom_materials)
temp_list[getmaterialref(i)] = custom_materials[i] //Get the proper instanced version
temp_list[SSmaterials.GetMaterialRef(i)] = custom_materials[i] //Get the proper instanced version
custom_materials = null //Null the list to prepare for applying the materials properly
set_custom_materials(temp_list)
@@ -1204,7 +1204,7 @@
if(custom_materials) //Only runs if custom materials existed at first. Should usually be the case but check anyways
for(var/i in custom_materials)
var/datum/material/custom_material = getmaterialref(i)
var/datum/material/custom_material = SSmaterials.GetMaterialRef(i)
custom_material.on_removed(src, material_flags) //Remove the current materials
if(!length(materials))
@@ -1213,7 +1213,7 @@
custom_materials = list() //Reset the list
for(var/x in materials)
var/datum/material/custom_material = getmaterialref(x)
var/datum/material/custom_material = SSmaterials.GetMaterialRef(x)
if(!(material_flags & MATERIAL_NO_EFFECTS))
custom_material.on_applied(src, materials[custom_material] * multiplier * material_modifier, material_flags)

View File

@@ -142,7 +142,7 @@
/obj/machinery/autolathe/proc/AfterMaterialInsert(item_inserted, id_inserted, amount_inserted)
if(istype(item_inserted, /obj/item/stack/ore/bluespace_crystal))
use_power(MINERAL_MATERIAL_AMOUNT / 10)
else if(custom_materials && custom_materials.len && custom_materials[getmaterialref(/datum/material/glass)])
else if(custom_materials && custom_materials.len && custom_materials[SSmaterials.GetMaterialRef(/datum/material/glass)])
flick("autolathe_r",src)//plays glass insertion animation by default otherwise
else
flick("autolathe_o",src)//plays metal insertion animation

View File

@@ -66,8 +66,8 @@
if(iswallturf(T))
T.attackby(src, user, params)
var/metal_amt = round(custom_materials[getmaterialref(/datum/material/iron)]/MINERAL_MATERIAL_AMOUNT) //Replace this shit later
var/glass_amt = round(custom_materials[getmaterialref(/datum/material/glass)]/MINERAL_MATERIAL_AMOUNT) //Replace this shit later
var/metal_amt = round(custom_materials[SSmaterials.GetMaterialRef(/datum/material/iron)]/MINERAL_MATERIAL_AMOUNT) //Replace this shit later
var/glass_amt = round(custom_materials[SSmaterials.GetMaterialRef(/datum/material/glass)]/MINERAL_MATERIAL_AMOUNT) //Replace this shit later
if(W.tool_behaviour == TOOL_WRENCH && (metal_amt || glass_amt))
to_chat(user, "<span class='notice'>You dismantle [src].</span>")

View File

@@ -52,7 +52,7 @@
mats_per_unit = list()
var/in_process_mat_list = custom_materials.Copy()
for(var/i in custom_materials)
mats_per_unit[getmaterialref(i)] = in_process_mat_list[i]
mats_per_unit[SSmaterials.GetMaterialRef(i)] = in_process_mat_list[i]
custom_materials[i] *= amount
. = ..()
if(merge)
@@ -62,7 +62,7 @@
var/list/temp_recipes = get_main_recipes()
recipes = temp_recipes.Copy()
if(material_type)
var/datum/material/M = getmaterialref(material_type) //First/main material
var/datum/material/M = SSmaterials.GetMaterialRef(material_type) //First/main material
for(var/i in M.categories)
switch(i)
if(MAT_CATEGORY_RIGID)
@@ -236,7 +236,7 @@
if(R.applies_mats && custom_materials && custom_materials.len)
var/list/used_materials = list()
for(var/i in custom_materials)
used_materials[getmaterialref(i)] = R.req_amount / R.res_amount * (MINERAL_MATERIAL_AMOUNT / custom_materials.len)
used_materials[SSmaterials.GetMaterialRef(i)] = R.req_amount / R.res_amount * (MINERAL_MATERIAL_AMOUNT / custom_materials.len)
O.set_custom_materials(used_materials)
//START: oh fuck i'm so sorry

View File

@@ -314,7 +314,7 @@
if(remaining_mats)
for(var/M=1 to remaining_mats)
new stack_type(get_turf(loc))
else if(custom_materials[getmaterialref(/datum/material/iron)])
else if(custom_materials[SSmaterials.GetMaterialRef(/datum/material/iron)])
new /obj/item/stack/rods(get_turf(loc), 2)
qdel(src)

View File

@@ -193,7 +193,7 @@
/obj/item/IntegrateAmount() //returns the amount of resources gained when eating this item
if(custom_materials)
if(custom_materials[getmaterialref(/datum/material/iron)] || custom_materials[getmaterialref(/datum/material/glass)])
if(custom_materials[SSmaterials.GetMaterialRef(/datum/material/iron)] || custom_materials[SSmaterials.GetMaterialRef(/datum/material/glass)])
return 1
return ..()

View File

@@ -14,10 +14,10 @@
if(!isitem(O))
return 0
var/obj/item/I = O
if(!(getmaterialref(material_id) in I.custom_materials))
if(!(SSmaterials.GetMaterialRef(material_id) in I.custom_materials))
return 0
var/amount = I.custom_materials[getmaterialref(material_id)]
var/amount = I.custom_materials[SSmaterials.GetMaterialRef(material_id)]
if(istype(I, /obj/item/stack/ore))
amount *= 0.8 // Station's ore redemption equipment is really goddamn good.

View File

@@ -310,8 +310,8 @@
for(var/obj/item/O in ingredients)
O.microwave_act(src)
if(O.custom_materials && length(O.custom_materials))
if(O.custom_materials[getmaterialref(/datum/material/iron)])
metal += O.custom_materials[getmaterialref(/datum/material/iron)]
if(O.custom_materials[SSmaterials.GetMaterialRef(/datum/material/iron)])
metal += O.custom_materials[SSmaterials.GetMaterialRef(/datum/material/iron)]
if(metal)
spark()

View File

@@ -194,7 +194,7 @@
dat += "<A href='?src=[REF(src)];create=[D.id];amount=5'>x5</A>"
if(ispath(D.build_path, /obj/item/stack))
dat += "<A href='?src=[REF(src)];create=[D.id];amount=10'>x10</A>"
dat += "([D.materials[getmaterialref(/datum/material/biomass)]/efficiency])<br>"
dat += "([D.materials[SSmaterials.GetMaterialRef(/datum/material/biomass)]/efficiency])<br>"
dat += "</div>"
else
dat += "<div class='statusDisplay'>No container inside, please insert container.</div>"
@@ -236,14 +236,14 @@
menustat = "void"
/obj/machinery/biogenerator/proc/check_cost(list/materials, multiplier = 1, remove_points = TRUE)
if(materials.len != 1 || materials[1] != getmaterialref(/datum/material/biomass))
if(materials.len != 1 || materials[1] != SSmaterials.GetMaterialRef(/datum/material/biomass))
return FALSE
if (materials[getmaterialref(/datum/material/biomass)]*multiplier/efficiency > points)
if (materials[SSmaterials.GetMaterialRef(/datum/material/biomass)]*multiplier/efficiency > points)
menustat = "nopoints"
return FALSE
else
if(remove_points)
points -= materials[getmaterialref(/datum/material/biomass)]*multiplier/efficiency
points -= materials[SSmaterials.GetMaterialRef(/datum/material/biomass)]*multiplier/efficiency
update_icon()
updateUsrDialog()
return TRUE

View File

@@ -86,7 +86,7 @@
proximity_monitor = new(src, 1)
AddComponent(/datum/component/material_container, list(/datum/material/iron, /datum/material/glass, /datum/material/silver, /datum/material/gold, /datum/material/diamond, /datum/material/plasma, /datum/material/uranium, /datum/material/bananium, /datum/material/titanium, /datum/material/bluespace), INFINITY, TRUE, /obj/item/stack)
stored_research = new /datum/techweb/specialized/autounlocking/smelter
selected_material = getmaterialref(/datum/material/iron)
selected_material = SSmaterials.GetMaterialRef(/datum/material/iron)
/obj/machinery/mineral/processing_unit/Destroy()
CONSOLE = null

View File

@@ -31,7 +31,7 @@
/datum/material/plastic,
/datum/material/runite
), MINERAL_MATERIAL_AMOUNT * 75, FALSE, /obj/item/stack)
chosen = getmaterialref(chosen)
chosen = SSmaterials.GetMaterialRef(chosen)
/obj/machinery/mineral/mint/process()

View File

@@ -83,8 +83,8 @@
if(is_type_in_list(S, list(/obj/item/stack/sheet/metal, /obj/item/stack/rods, /obj/item/stack/tile/plasteel)))
if(S.custom_materials && custom_materials.len)
if(S.custom_materials[getmaterialref(/datum/material/iron)])
S.cost = S.custom_materials[getmaterialref(/datum/material/iron)] * 0.25
if(S.custom_materials[SSmaterials.GetMaterialRef(/datum/material/iron)])
S.cost = S.custom_materials[SSmaterials.GetMaterialRef(/datum/material/iron)] * 0.25
S.source = get_or_create_estorage(/datum/robot_energy_storage/metal)
else if(istype(S, /obj/item/stack/sheet/glass))

View File

@@ -60,7 +60,7 @@
/mob/living/simple_animal/hostile/retaliate/goose/vomit/handle_automated_action()
if(length(nummies))
var/obj/item/E = pick(nummies)
if(!(E.custom_materials && E.custom_materials[getmaterialref(/datum/material/plastic)]))
if(!(E.custom_materials && E.custom_materials[SSmaterials.GetMaterialRef(/datum/material/plastic)]))
nummies -= E // remove non-plastic item from queue
E = locate(/obj/item/reagent_containers/food) in nummies // find food
if(E && E.loc == loc)
@@ -70,7 +70,7 @@
/mob/living/simple_animal/hostile/retaliate/goose/proc/feed(obj/item/suffocator)
if(stat == DEAD || choking) // plapatin I swear to god
return FALSE
if(suffocator.custom_materials && suffocator.custom_materials[getmaterialref(/datum/material/plastic)]) // dumb goose'll swallow food or drink with plastic in it
if(suffocator.custom_materials && suffocator.custom_materials[SSmaterials.GetMaterialRef(/datum/material/plastic)]) // dumb goose'll swallow food or drink with plastic in it
visible_message("<span class='danger'>[src] hungrily gobbles up \the [suffocator]! </span>")
visible_message("<span class='boldwarning'>[src] is choking on \the [suffocator]! </span>")
suffocator.forceMove(src)

View File

@@ -53,7 +53,7 @@ other types of metals and chemistry for reagents).
for(var/i in materials) //Go through all of our materials, get the subsystem instance, and then replace the list.
var/amount = materials[i]
if(!istext(i)) //Not a category, so get the ref the normal way
var/datum/material/M = getmaterialref(i)
var/datum/material/M = SSmaterials.GetMaterialRef(i)
temp_list[M] = amount
else
temp_list[i] = amount