Skip to content

Commit

Permalink
Merge pull request tgstation#11580 from xxalpha/protocontainer
Browse files Browse the repository at this point in the history
Changed protolathe code to use material container.
  • Loading branch information
Razharas committed Sep 12, 2015
2 parents fbe1781 + 28889d2 commit f9a01d1
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 213 deletions.
34 changes: 22 additions & 12 deletions code/datums/material_container.dm
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@
if(amt > 0 && has_space(amt))
var/total_amount_saved = total_amount
if(material_type)
for(var/datum/material/M in materials)
if(M.material_type == material_type)
M.amount += amt
total_amount += amt
var/datum/material/M = materials[material_type]
if(M)
M.amount += amt
total_amount += amt
else
for(var/datum/material/M in materials)
M.amount += amt
Expand Down Expand Up @@ -93,21 +93,21 @@

//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)
/datum/material_container/proc/use_amount(list/mats, multiplier=1)
if(!mats || !mats.len)
return 0

var/datum/material/M
for(var/MAT in materials)
M = materials[MAT]
if(M.amount < mats[MAT])
if(M.amount < (mats[MAT] * multiplier))
return 0

var/total_amount_save = total_amount
for(var/MAT in materials)
M = materials[MAT]
M.amount -= mats[MAT]
total_amount -= mats[MAT]
M.amount -= mats[MAT] * multiplier
total_amount -= mats[MAT] * multiplier

return total_amount_save - total_amount

Expand All @@ -124,7 +124,9 @@

//For spawning mineral sheets; internal use only
/datum/material_container/proc/retrieve(sheet_amt, datum/material/M)
if(sheet_amt > 0 && M.amount >= (sheet_amt * MINERAL_MATERIAL_AMOUNT))
if(sheet_amt > 0)
if(M.amount < (sheet_amt * MINERAL_MATERIAL_AMOUNT))
sheet_amt = round(M.amount / MINERAL_MATERIAL_AMOUNT)
var/count = 0

while(sheet_amt > MAX_STACK_SIZE)
Expand Down Expand Up @@ -159,6 +161,17 @@
/datum/material_container/proc/has_space(amt = 0)
return (total_amount + amt) <= max_amount

/datum/material_container/proc/has_materials(list/mats, multiplier=1)
if(!mats || !mats.len)
return 0

var/datum/material/M
for(var/MAT in mats)
M = materials[MAT]
if(M.amount < (mats[MAT] * multiplier))
return 0
return 1

/datum/material_container/proc/amount2sheet(amt)
if(amt >= MINERAL_MATERIAL_AMOUNT)
return round(amt / MINERAL_MATERIAL_AMOUNT)
Expand All @@ -173,9 +186,6 @@
var/datum/material/M = materials[material_type]
return M ? M.amount : 0

/datum/material_container/proc/can_insert(obj/item/I)
return get_item_material_amount(I)

//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)
Expand Down
2 changes: 1 addition & 1 deletion code/game/machinery/autolathe.dm
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
if (stat)
return 1

var/material_amount = materials.can_insert(O)
var/material_amount = materials.get_item_material_amount(O)
if(!material_amount)
user << "<span class='warning'>This object does not contain sufficient amounts of metal or glass to be accepted by the autolathe.</span>"
return 1
Expand Down
2 changes: 1 addition & 1 deletion code/game/machinery/recycler.dm
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var/const/SAFETY_COOLDOWN = 100

if(sound)
playsound(src.loc, 'sound/items/Welder.ogg', 50, 1)
var/material_amount = materials.can_insert(I)
var/material_amount = materials.get_item_material_amount(I)
if(!material_amount)
qdel(I)
return
Expand Down
2 changes: 1 addition & 1 deletion code/modules/clothing/shoes/bananashoes.dm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
user << "<span class='notice'>You cannot retrieve any bananium from the prototype shoes.</span>"

/obj/item/clothing/shoes/clown_shoes/banana_shoes/attackby(obj/item/O, mob/user, params)
if(!bananium.can_insert(O))
if(!bananium.get_item_material_amount(O))
user << "<span class='notice'>This item has no bananium!</span>"
return
if(!user.unEquip(O))
Expand Down
1 change: 1 addition & 0 deletions code/modules/research/designs.dm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ other types of metals and chemistry for reagents).
var/construction_time //Amount of time required for building the object
var/build_path = "" //The file path of the object that gets created
var/list/category = null //Primarily used for Mech Fabricators, but can be used for anything
var/reagents


//A proc to calculate the reliability of a design based on tech levels and innate modifiers.
Expand Down
4 changes: 2 additions & 2 deletions code/modules/research/experimentor.dm
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@
visible_message("<span class='warning'>[src]'s crushing mechanism slowly and smoothly descends, flattening the [exp_on]!</span>")
new /obj/item/stack/sheet/plasteel(get_turf(pick(oview(1,src))))
if(linked_console.linked_lathe)
linked_console.linked_lathe.m_amount += min((linked_console.linked_lathe.max_material_storage - linked_console.linked_lathe.TotalMaterials()), (exp_on.materials[MAT_METAL]))
linked_console.linked_lathe.g_amount += min((linked_console.linked_lathe.max_material_storage - linked_console.linked_lathe.TotalMaterials()), (exp_on.materials[MAT_GLASS]))
for(var/material in exp_on.materials)
linked_console.linked_lathe.materials.insert_amount( min((linked_console.linked_lathe.materials.max_amount - linked_console.linked_lathe.materials.total_amount), (exp_on.materials[material])), material)
if(prob(EFFECT_PROB_VERYLOW-badThingCoeff))
visible_message("<span class='danger'>[src]'s crusher goes way too many levels too high, crushing right through space-time!</span>")
playsound(src.loc, 'sound/effects/supermatter.ogg', 50, 1, -3)
Expand Down
123 changes: 24 additions & 99 deletions code/modules/research/protolathe.dm
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ Note: Must be placed west/left of and R&D console to function.
icon_state = "protolathe"
flags = OPENCONTAINER

var/max_material_storage = 100000 //All this could probably be done better with a list but meh.
var/m_amount = 0.0
var/g_amount = 0.0
var/gold_amount = 0.0
var/silver_amount = 0.0
var/plasma_amount = 0.0
var/uranium_amount = 0.0
var/diamond_amount = 0.0
var/clown_amount = 0.0
var/adamantine_amount = 0.0
var/datum/material_container/materials
var/efficiency_coeff

var/list/categories = list(
Expand Down Expand Up @@ -51,46 +42,31 @@ Note: Must be placed west/left of and R&D console to function.
component_parts += new /obj/item/weapon/stock_parts/manipulator(src)
component_parts += new /obj/item/weapon/reagent_containers/glass/beaker(src)
component_parts += new /obj/item/weapon/reagent_containers/glass/beaker(src)
materials = new(src, list(MAT_METAL=1, MAT_GLASS=1, MAT_SILVER=1, MAT_GOLD=1, MAT_DIAMOND=1, MAT_PLASMA=1, MAT_URANIUM=1, MAT_BANANIUM=1))
RefreshParts()

reagents.my_atom = src

/obj/machinery/r_n_d/protolathe/proc/TotalMaterials() //returns the total of all the stored materials. Makes code neater.
return m_amount + g_amount + gold_amount + silver_amount + plasma_amount + uranium_amount + diamond_amount + clown_amount
/obj/machinery/r_n_d/protolathe/Destroy()
qdel(materials)
return ..()

/obj/machinery/r_n_d/protolathe/RefreshParts()
var/T = 0
for(var/obj/item/weapon/reagent_containers/glass/G in component_parts)
G.reagents.trans_to(src, G.reagents.total_volume)
for(var/obj/item/weapon/stock_parts/matter_bin/M in component_parts)
T += M.rating
max_material_storage = T * 75000
materials.max_amount = T * 75000
T = 0
for(var/obj/item/weapon/stock_parts/manipulator/M in component_parts)
T += (M.rating/3)
efficiency_coeff = max(T, 1)

/obj/machinery/r_n_d/protolathe/proc/check_mat(datum/design/being_built, M) // now returns how many times the item can be built with the material
var/A = 0
switch(M)
if(MAT_METAL)
A = m_amount
if(MAT_GLASS)
A = g_amount
if(MAT_GOLD)
A = gold_amount
if(MAT_SILVER)
A = silver_amount
if(MAT_PLASMA)
A = plasma_amount
if(MAT_URANIUM)
A = uranium_amount
if(MAT_DIAMOND)
A = diamond_amount
if(MAT_BANANIUM)
A = clown_amount
else
A = reagents.get_reagent_amount(M)
var/A = materials.amount(M)
if(!A)
A = reagents.get_reagent_amount(M)
A = A / max(1, (being_built.materials[M]/efficiency_coeff))
return A

Expand All @@ -110,33 +86,7 @@ Note: Must be placed west/left of and R&D console to function.
if(istype(O, /obj/item/weapon/crowbar))
for(var/obj/item/weapon/reagent_containers/glass/G in component_parts)
reagents.trans_to(G, G.reagents.maximum_volume)
if(m_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/metal/G = new /obj/item/stack/sheet/metal(src.loc)
G.amount = round(m_amount / G.perunit)
if(g_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/glass/G = new /obj/item/stack/sheet/glass(src.loc)
G.amount = round(g_amount / G.perunit)
if(plasma_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/mineral/plasma/G = new /obj/item/stack/sheet/mineral/plasma(src.loc)
G.amount = round(plasma_amount / G.perunit)
if(silver_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/mineral/silver/G = new /obj/item/stack/sheet/mineral/silver(src.loc)
G.amount = round(silver_amount / G.perunit)
if(gold_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/mineral/gold/G = new /obj/item/stack/sheet/mineral/gold(src.loc)
G.amount = round(gold_amount / G.perunit)
if(uranium_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/mineral/uranium/G = new /obj/item/stack/sheet/mineral/uranium(src.loc)
G.amount = round(uranium_amount / G.perunit)
if(diamond_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/mineral/diamond/G = new /obj/item/stack/sheet/mineral/diamond(src.loc)
G.amount = round(diamond_amount / G.perunit)
if(clown_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/mineral/bananium/G = new /obj/item/stack/sheet/mineral/bananium(src.loc)
G.amount = round(clown_amount / G.perunit)
if(adamantine_amount >= MINERAL_MATERIAL_AMOUNT)
var/obj/item/stack/sheet/mineral/adamantine/G = new /obj/item/stack/sheet/mineral/adamantine(src.loc)
G.amount = round(adamantine_amount / G.perunit)
materials.retrieve_all()
default_deconstruction_crowbar(O)
return 1
else
Expand All @@ -152,52 +102,27 @@ Note: Must be placed west/left of and R&D console to function.
return 1
if (O.is_open_container())
return
if (!istype(O, /obj/item/stack/sheet) || istype(O, /obj/item/stack/sheet/mineral/wood))
user << "<span class='warning'>You cannot insert this item into the [src.name]!</span>"
return 1
if (stat)
return 1
if(istype(O,/obj/item/stack/sheet))
var/obj/item/stack/sheet/S = O
if (TotalMaterials() + S.perunit > max_material_storage)
if(!materials.has_space( materials.get_item_material_amount(O) ))
user << "<span class='warning'>The [src.name]'s material bin is full! Please remove material before adding more.</span>"
return 1

var/obj/item/stack/sheet/stack = O
var/amount = round(input("How many sheets do you want to add?") as num)//No decimals
if(!stack || stack.amount <= 0 || amount <= 0 || !in_range(src, stack) || !user.Adjacent(src))
if(!in_range(src, stack) || !user.Adjacent(src))
return
if(amount > stack.amount)
amount = stack.amount
if(max_material_storage - TotalMaterials() < (amount*stack.perunit))//Can't overfill
amount = min(stack.amount, round((max_material_storage-TotalMaterials())/stack.perunit))

busy = 1
use_power(max(1000, (MINERAL_MATERIAL_AMOUNT*amount/10)))
user << "<span class='notice'>You add [amount] sheets to the [src.name].</span>"
if(istype(stack, /obj/item/stack/sheet/metal))
m_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/glass))
g_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/mineral/gold))
gold_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/mineral/silver))
silver_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/mineral/plasma))
plasma_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/mineral/uranium))
uranium_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/mineral/diamond))
diamond_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/mineral/bananium))
clown_amount += amount * MINERAL_MATERIAL_AMOUNT
else if(istype(stack, /obj/item/stack/sheet/mineral/adamantine))
adamantine_amount += amount * MINERAL_MATERIAL_AMOUNT
stack.use(amount)
var/amount_inserted = materials.insert_stack(O,amount)
if(!amount_inserted)
user << "<span class='warning'>You could not insert [O], it has no usable materials.</span>"
return 1
else
busy = 1
use_power(max(1000, (MINERAL_MATERIAL_AMOUNT*amount_inserted/10)))
user << "<span class='notice'>You add [amount_inserted] sheets to the [src.name].</span>"
overlays += "protolathe_[stack.name]"
sleep(10)
overlays -= "protolathe_[stack.name]"
busy = 0
updateUsrDialog()

overlays += "protolathe_[stack.name]"
sleep(10)
overlays -= "protolathe_[stack.name]"
busy = 0

Loading

0 comments on commit f9a01d1

Please sign in to comment.