[MIRROR] Port material containers to datum components (#2562)
* Port material containers to datum components * Fixes Rej
This commit is contained in:
committed by
Poojawa
parent
a4c92a8155
commit
32300086d5
@@ -0,0 +1,97 @@
|
||||
/datum/component
|
||||
var/enabled = TRUE // Enables or disables the components
|
||||
var/dupe_mode = COMPONENT_DUPE_HIGHLANDER // How components of the same type are handled in the same parent
|
||||
var/list/signal_procs // list of signals -> callbacks
|
||||
var/datum/parent // parent datum
|
||||
|
||||
/datum/component/New(datum/P, ...)
|
||||
var/dm = dupe_mode
|
||||
if(dm != COMPONENT_DUPE_ALLOWED)
|
||||
var/datum/component/old = P.GetExactComponent(type)
|
||||
if(old)
|
||||
switch(dm)
|
||||
if(COMPONENT_DUPE_HIGHLANDER)
|
||||
P.RemoveComponent(old)
|
||||
old = null //in case SendSignal() blocks
|
||||
if(COMPONENT_DUPE_UNIQUE)
|
||||
qdel(src)
|
||||
return
|
||||
P.SendSignal(COMSIG_COMPONENT_ADDED, list(src), FALSE)
|
||||
LAZYADD(P.datum_components, src)
|
||||
parent = P
|
||||
|
||||
/datum/component/Destroy()
|
||||
RemoveNoSignal()
|
||||
return ..()
|
||||
|
||||
/datum/component/proc/RemoveNoSignal()
|
||||
var/datum/P = parent
|
||||
if(P)
|
||||
LAZYREMOVE(P.datum_components, src)
|
||||
parent = null
|
||||
|
||||
/datum/component/proc/RegisterSignal(sig_type, proc_on_self, override = FALSE)
|
||||
var/list/procs = signal_procs
|
||||
if(!procs)
|
||||
procs = list()
|
||||
signal_procs = procs
|
||||
|
||||
if(!override)
|
||||
. = procs[sig_type]
|
||||
if(.)
|
||||
stack_trace("[sig_type] overridden. Use override = TRUE to suppress this warning")
|
||||
|
||||
procs[sig_type] = CALLBACK(src, proc_on_self)
|
||||
|
||||
/datum/var/list/datum_components //list of /datum/component
|
||||
|
||||
// Send a signal to all other components in the container.
|
||||
/datum/proc/SendSignal(sigtype, list/sig_args, async = FALSE)
|
||||
var/list/comps = datum_components
|
||||
. = FALSE
|
||||
for(var/I in comps)
|
||||
var/datum/component/C = I
|
||||
if(!C.enabled)
|
||||
continue
|
||||
var/list/sps = C.signal_procs
|
||||
var/datum/callback/CB = LAZYACCESS(sps, sigtype)
|
||||
if(!CB)
|
||||
continue
|
||||
if(!async)
|
||||
. |= CB.Invoke(sig_args)
|
||||
else
|
||||
. |= CB.InvokeAsync(sig_args)
|
||||
|
||||
/datum/proc/GetComponent(c_type)
|
||||
for(var/I in datum_components)
|
||||
if(istype(I, c_type))
|
||||
return I
|
||||
|
||||
/datum/proc/GetExactComponent(c_type)
|
||||
for(var/I in datum_components)
|
||||
var/datum/component/C = I
|
||||
if(C.type == c_type)
|
||||
return I
|
||||
|
||||
/datum/proc/GetComponents(c_type)
|
||||
. = list()
|
||||
for(var/I in datum_components)
|
||||
if(istype(I, c_type))
|
||||
. += I
|
||||
|
||||
/datum/proc/AddComponents(list/new_types)
|
||||
for(var/new_type in new_types)
|
||||
AddComponent(new_type)
|
||||
|
||||
/datum/proc/AddComponent(new_type, ...)
|
||||
var/nt = new_type
|
||||
args[1] = src
|
||||
var/datum/component/C = new nt(arglist(args))
|
||||
return QDELING(C) ? GetComponent(new_type) : C
|
||||
|
||||
/datum/proc/RemoveComponent(datum/component/C)
|
||||
if(!C)
|
||||
return
|
||||
C.RemoveNoSignal()
|
||||
SendSignal(COMSIG_COMPONENT_REMOVING, list(C), FALSE)
|
||||
qdel(C)
|
||||
@@ -105,4 +105,4 @@ Stands have a lot of procs which mimic mob procs. Rather than inserting hooks fo
|
||||
* Called when a component recieves any signal and is enabled
|
||||
* Default implementation looks if the signal is registered and runs the appropriate proc
|
||||
|
||||
### See/Define signals and their arguments in __DEFINES\components.dm
|
||||
### See signals and their arguments in __DEFINES\components.dm
|
||||
|
||||
@@ -5,22 +5,34 @@
|
||||
amount - raw amount of the mineral this container is holding, calculated by the defined value MINERAL_MATERIAL_AMOUNT=2000.
|
||||
max_amount - max raw amount of mineral this container can hold.
|
||||
sheet_type - type of the mineral sheet the container handles, used for output.
|
||||
owner - object that this container is being used by, 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/material_container
|
||||
/datum/component/material_container
|
||||
var/total_amount = 0
|
||||
var/max_amount
|
||||
var/sheet_type
|
||||
var/obj/owner
|
||||
var/list/materials = list()
|
||||
var/list/materials
|
||||
var/show_on_examine
|
||||
var/list/allowed_typecache
|
||||
var/last_inserted_type
|
||||
var/last_amount_inserted
|
||||
var/last_insert_success
|
||||
var/datum/callback/precondition
|
||||
//MAX_STACK_SIZE = 50
|
||||
//MINERAL_MATERIAL_AMOUNT = 2000
|
||||
|
||||
/datum/material_container/New(obj/O, list/mat_list, max_amt = 0)
|
||||
owner = O
|
||||
/datum/component/material_container/Initialize(list/mat_list, max_amt = 0, _show_on_examine = FALSE, list/allowed_types, datum/callback/_precondition)
|
||||
materials = list()
|
||||
max_amount = max(0, max_amt)
|
||||
show_on_examine = _show_on_examine
|
||||
if(allowed_types)
|
||||
allowed_typecache = typecacheof(allowed_types)
|
||||
precondition = _precondition
|
||||
|
||||
RegisterSignal(COMSIG_PARENT_ATTACKBY, .proc/OnAttackBy)
|
||||
RegisterSignal(COMSIG_PARENT_EXAMINE, .proc/OnExamine)
|
||||
|
||||
var/list/possible_mats = list()
|
||||
for(var/mat_type in subtypesof(/datum/material))
|
||||
@@ -31,12 +43,47 @@
|
||||
var/mat_path = possible_mats[id]
|
||||
materials[id] = new mat_path()
|
||||
|
||||
/datum/material_container/Destroy()
|
||||
owner = null
|
||||
return ..()
|
||||
/datum/component/material_container/proc/OnExamine(mob/user)
|
||||
for(var/I in materials)
|
||||
var/datum/material/M = materials[I]
|
||||
var/amt = amount(M.id)
|
||||
if(amt)
|
||||
to_chat(user, "<span class='notice'>It has [amt] units of [lowertext(M.name)] stored.</span>")
|
||||
|
||||
/datum/component/material_container/proc/OnAttackBy(obj/item/I, mob/living/user)
|
||||
var/list/tc = allowed_typecache
|
||||
if(user.a_intent == INTENT_HARM || (I.flags_2 & HOLOGRAM_2) || (tc && !is_type_in_typecache(I, tc)))
|
||||
return FALSE
|
||||
. = TRUE
|
||||
last_insert_success = FALSE
|
||||
var/datum/callback/pc = precondition
|
||||
if(pc && !pc.Invoke())
|
||||
return
|
||||
var/material_amount = get_item_material_amount(I)
|
||||
if(!material_amount)
|
||||
to_chat(user, "<span class='warning'>[I] does not contain sufficient amounts of metal or glass to be accepted by [parent].</span>")
|
||||
return
|
||||
if(!has_space(material_amount))
|
||||
to_chat(user, "<span class='warning'>[parent] is full. Please remove metal or glass from [parent] in order to insert more.</span>")
|
||||
return
|
||||
if(!user.temporarilyRemoveItemFromInventory(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)
|
||||
if(inserted)
|
||||
last_insert_success = TRUE
|
||||
if(istype(I, /obj/item/stack))
|
||||
to_chat(user, "<span class='notice'>You insert [inserted] sheet[inserted>1 ? "s" : ""] into [parent].</span>")
|
||||
if(!QDELETED(I))
|
||||
user.put_in_active_hand(I)
|
||||
else
|
||||
to_chat(user, "<span class='notice'>You insert a material total of [inserted] into [parent].</span>")
|
||||
qdel(I)
|
||||
else
|
||||
user.put_in_active_hand(I)
|
||||
|
||||
//For inserting an amount of material
|
||||
/datum/material_container/proc/insert_amount(amt, id = null)
|
||||
/datum/component/material_container/proc/insert_amount(amt, id = null)
|
||||
if(amt > 0 && has_space(amt))
|
||||
var/total_amount_saved = total_amount
|
||||
if(id)
|
||||
@@ -52,7 +99,7 @@
|
||||
return (total_amount - total_amount_saved)
|
||||
return 0
|
||||
|
||||
/datum/material_container/proc/insert_stack(obj/item/stack/S, amt = 0)
|
||||
/datum/component/material_container/proc/insert_stack(obj/item/stack/S, amt = 0)
|
||||
if(amt <= 0)
|
||||
return 0
|
||||
if(amt > S.amount)
|
||||
@@ -67,10 +114,12 @@
|
||||
return 0
|
||||
|
||||
insert_materials(S,amt)
|
||||
last_inserted_type = S.type
|
||||
S.use(amt)
|
||||
last_amount_inserted = amt
|
||||
return amt
|
||||
|
||||
/datum/material_container/proc/insert_item(obj/item/I, multiplier = 1)
|
||||
/datum/component/material_container/proc/insert_item(obj/item/I, multiplier = 1)
|
||||
if(!I)
|
||||
return 0
|
||||
if(istype(I, /obj/item/stack))
|
||||
@@ -82,9 +131,11 @@
|
||||
return 0
|
||||
|
||||
insert_materials(I, multiplier)
|
||||
last_inserted_type = I.type
|
||||
last_amount_inserted = material_amount
|
||||
return material_amount
|
||||
|
||||
/datum/material_container/proc/insert_materials(obj/item/I, multiplier = 1) //for internal usage only
|
||||
/datum/component/material_container/proc/insert_materials(obj/item/I, multiplier = 1) //for internal usage only
|
||||
var/datum/material/M
|
||||
for(var/MAT in materials)
|
||||
M = materials[MAT]
|
||||
@@ -93,7 +144,7 @@
|
||||
|
||||
//For consuming material
|
||||
//mats is a list of types of material to use and the corresponding amounts, example: list(MAT_METAL=100, MAT_GLASS=200)
|
||||
/datum/material_container/proc/use_amount(list/mats, multiplier=1)
|
||||
/datum/component/material_container/proc/use_amount(list/mats, multiplier=1)
|
||||
if(!mats || !mats.len)
|
||||
return 0
|
||||
|
||||
@@ -112,7 +163,7 @@
|
||||
return total_amount_save - total_amount
|
||||
|
||||
|
||||
/datum/material_container/proc/use_amount_type(amt, id)
|
||||
/datum/component/material_container/proc/use_amount_type(amt, id)
|
||||
var/datum/material/M = materials[id]
|
||||
if(M)
|
||||
if(M.amount >= amt)
|
||||
@@ -121,7 +172,7 @@
|
||||
return amt
|
||||
return 0
|
||||
|
||||
/datum/material_container/proc/can_use_amount(amt, id, list/mats)
|
||||
/datum/component/material_container/proc/can_use_amount(amt, id, list/mats)
|
||||
if(amt && id)
|
||||
var/datum/material/M = materials[id]
|
||||
if(M && M.amount >= amt)
|
||||
@@ -136,7 +187,7 @@
|
||||
return FALSE
|
||||
|
||||
//For spawning mineral sheets; internal use only
|
||||
/datum/material_container/proc/retrieve(sheet_amt, datum/material/M, target = null)
|
||||
/datum/component/material_container/proc/retrieve(sheet_amt, datum/material/M, target = null)
|
||||
if(!M.sheet_type)
|
||||
return 0
|
||||
if(sheet_amt > 0)
|
||||
@@ -144,12 +195,12 @@
|
||||
sheet_amt = round(M.amount / MINERAL_MATERIAL_AMOUNT)
|
||||
var/count = 0
|
||||
while(sheet_amt > MAX_STACK_SIZE)
|
||||
new M.sheet_type(get_turf(owner), MAX_STACK_SIZE)
|
||||
new M.sheet_type(get_turf(parent), MAX_STACK_SIZE)
|
||||
count += MAX_STACK_SIZE
|
||||
use_amount_type(sheet_amt * MINERAL_MATERIAL_AMOUNT, M.id)
|
||||
sheet_amt -= MAX_STACK_SIZE
|
||||
if(round((sheet_amt * MINERAL_MATERIAL_AMOUNT) / MINERAL_MATERIAL_AMOUNT))
|
||||
var/obj/item/stack/sheet/s = new M.sheet_type(get_turf(owner), sheet_amt)
|
||||
var/obj/item/stack/sheet/s = new M.sheet_type(get_turf(parent), sheet_amt)
|
||||
if(target)
|
||||
s.forceMove(target)
|
||||
count += sheet_amt
|
||||
@@ -157,15 +208,15 @@
|
||||
return count
|
||||
return 0
|
||||
|
||||
/datum/material_container/proc/retrieve_sheets(sheet_amt, id, target = null)
|
||||
/datum/component/material_container/proc/retrieve_sheets(sheet_amt, id, target = null)
|
||||
if(materials[id])
|
||||
return retrieve(sheet_amt, materials[id], target)
|
||||
return 0
|
||||
|
||||
/datum/material_container/proc/retrieve_amount(amt, id, target)
|
||||
/datum/component/material_container/proc/retrieve_amount(amt, id, target)
|
||||
return retrieve_sheets(amount2sheet(amt), id, target)
|
||||
|
||||
/datum/material_container/proc/retrieve_all(target = null)
|
||||
/datum/component/material_container/proc/retrieve_all(target = null)
|
||||
var/result = 0
|
||||
var/datum/material/M
|
||||
for(var/MAT in materials)
|
||||
@@ -173,10 +224,10 @@
|
||||
result += retrieve_sheets(amount2sheet(M.amount), MAT, target)
|
||||
return result
|
||||
|
||||
/datum/material_container/proc/has_space(amt = 0)
|
||||
/datum/component/material_container/proc/has_space(amt = 0)
|
||||
return (total_amount + amt) <= max_amount
|
||||
|
||||
/datum/material_container/proc/has_materials(list/mats, multiplier=1)
|
||||
/datum/component/material_container/proc/has_materials(list/mats, multiplier=1)
|
||||
if(!mats || !mats.len)
|
||||
return 0
|
||||
|
||||
@@ -187,23 +238,23 @@
|
||||
return 0
|
||||
return 1
|
||||
|
||||
/datum/material_container/proc/amount2sheet(amt)
|
||||
/datum/component/material_container/proc/amount2sheet(amt)
|
||||
if(amt >= MINERAL_MATERIAL_AMOUNT)
|
||||
return round(amt / MINERAL_MATERIAL_AMOUNT)
|
||||
return 0
|
||||
|
||||
/datum/material_container/proc/sheet2amount(sheet_amt)
|
||||
/datum/component/material_container/proc/sheet2amount(sheet_amt)
|
||||
if(sheet_amt > 0)
|
||||
return sheet_amt * MINERAL_MATERIAL_AMOUNT
|
||||
return 0
|
||||
|
||||
/datum/material_container/proc/amount(id)
|
||||
/datum/component/material_container/proc/amount(id)
|
||||
var/datum/material/M = materials[id]
|
||||
return M ? M.amount : 0
|
||||
|
||||
//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/material_container/proc/get_item_material_amount(obj/item/I)
|
||||
/datum/component/material_container/proc/get_item_material_amount(obj/item/I)
|
||||
if(!istype(I))
|
||||
return 0
|
||||
var/material_amount = 0
|
||||
@@ -278,4 +329,4 @@
|
||||
|
||||
/datum/material/biomass
|
||||
name = "Biomass"
|
||||
id = MAT_BIOMASS
|
||||
id = MAT_BIOMASS
|
||||
@@ -0,0 +1,6 @@
|
||||
//These components require a powered machine as a parent to run
|
||||
/datum/component/powered
|
||||
|
||||
/datum/component/powered/ReceiveSignal(sigtype, list/sig_args, async)
|
||||
var/obj/machinery/M = parent
|
||||
return M.is_operational() && ..()
|
||||
Reference in New Issue
Block a user