Merge pull request #1859 from Datraen/SmarterStorage

Brings SmartFridges under the vending machine datum system.
This commit is contained in:
EmperorJon
2016-06-03 14:16:23 +01:00

View File

@@ -1,3 +1,63 @@
/*
* Datum that holds the instances and information about the items in the smartfridge.
*/
/datum/data/stored_item
var/item_name = "name" //Name of the item(s) displayed
var/item_path = null
var/amount = 0
var/list/instances //What items are actually stored
var/fridge //The attatched fridge
/datum/data/stored_item/New(var/fridge, var/path, var/name = null, var/amount = 0)
..()
src.item_path = path
if(!name)
var/atom/tmp = path
src.item_name = initial(tmp.name)
else
src.item_name = name
src.amount = amount
src.fridge = fridge
/datum/data/stored_item/Destroy()
fridge = null
if(instances)
for(var/product in instances)
qdel(product)
instances.Cut()
. = ..()
/datum/data/stored_item/proc/get_amount()
return instances ? instances.len : amount
/datum/data/stored_item/proc/get_product(var/product_location)
if(!get_amount() || !product_location)
return
init_products()
var/atom/movable/product = instances[instances.len] // Remove the last added product
instances -= product
product.forceMove(product_location)
/datum/data/stored_item/proc/add_product(var/atom/movable/product)
if(product.type != item_path)
return 0
init_products()
product.forceMove(fridge)
instances += product
/datum/data/stored_item/proc/init_products()
if(instances)
return
instances = list()
for(var/i = 1 to amount)
var/new_product = new item_path(fridge)
instances += new_product
/* SmartFridge. Much todo /* SmartFridge. Much todo
*/ */
/obj/machinery/smartfridge /obj/machinery/smartfridge
@@ -15,7 +75,8 @@
var/icon_on = "smartfridge" var/icon_on = "smartfridge"
var/icon_off = "smartfridge-off" var/icon_off = "smartfridge-off"
var/icon_panel = "smartfridge-panel" var/icon_panel = "smartfridge-panel"
var/item_quants = list() var/list/item_records = list()
var/datum/data/stored_item/currently_vending = null //What we're putting out of the machine.
var/seconds_electrified = 0; var/seconds_electrified = 0;
var/shoot_inventory = 0 var/shoot_inventory = 0
var/locked = 0 var/locked = 0
@@ -68,11 +129,14 @@
/obj/machinery/smartfridge/secure/extract/New() /obj/machinery/smartfridge/secure/extract/New()
..() ..()
var/datum/data/stored_item/I = new(src, /obj/item/xenoproduct/slime/core)
item_records.Add(I)
for(var/i=1 to 5) for(var/i=1 to 5)
var/obj/item/xenoproduct/slime/core/C = new(src) var/obj/item/xenoproduct/slime/core/C = new(src)
C.traits = new() C.traits = new()
C.nameVar = "grey" C.nameVar = "grey"
item_quants[C.name]++ I.add_product(C)
/obj/machinery/smartfridge/secure/medbay /obj/machinery/smartfridge/secure/medbay
name = "\improper Refrigerated Medicine Storage" name = "\improper Refrigerated Medicine Storage"
@@ -162,18 +226,17 @@
overlays += "drying_rack_drying" overlays += "drying_rack_drying"
/obj/machinery/smartfridge/drying_rack/proc/dry() /obj/machinery/smartfridge/drying_rack/proc/dry()
for(var/obj/item/weapon/reagent_containers/food/snacks/S in contents) for(var/datum/data/stored_item/I in item_records)
for(var/obj/item/weapon/reagent_containers/food/snacks/S in I.instances)
if(S.dry) continue if(S.dry) continue
if(S.dried_type == S.type) if(S.dried_type == S.type)
S.dry = 1 S.dry = 1
item_quants[S.name]--
S.name = "dried [S.name]" S.name = "dried [S.name]"
S.color = "#AAAAAA" S.color = "#AAAAAA"
S.loc = loc I.get_product(loc)
else else
var/D = S.dried_type var/D = S.dried_type
new D(loc) new D(loc)
item_quants[S.name]--
qdel(S) qdel(S)
return return
return return
@@ -222,16 +285,18 @@
return return
if(accept_check(O)) if(accept_check(O))
if(contents.len >= max_n_of_items)
user << "<span class='notice'>\The [src] is full.</span>"
return 1
else
user.remove_from_mob(O) user.remove_from_mob(O)
O.loc = src var/hasRecord = FALSE //Check to see if this passes or not.
if(item_quants[O.name]) for(var/datum/data/stored_item/I in item_records)
item_quants[O.name]++ if(istype(O, I.item_path))
else stock(O, I)
item_quants[O.name] = 1 qdel(O)
return
if(!hasRecord)
var/datum/data/stored_item/item = new/datum/data/stored_item(src, O.type)
stock(O, item)
item_records.Add(item)
user.visible_message("<span class='notice'>[user] has added \the [O] to \the [src].</span>", "<span class='notice'>You add \the [O] to \the [src].</span>") user.visible_message("<span class='notice'>[user] has added \the [O] to \the [src].</span>", "<span class='notice'>You add \the [O] to \the [src].</span>")
nanomanager.update_uis(src) nanomanager.update_uis(src)
@@ -241,16 +306,17 @@
var/plants_loaded = 0 var/plants_loaded = 0
for(var/obj/G in P.contents) for(var/obj/G in P.contents)
if(accept_check(G)) if(accept_check(G))
if(contents.len >= max_n_of_items)
user << "<span class='notice'>\The [src] is full.</span>"
return 1
else
P.remove_from_storage(G,src)
if(item_quants[G.name])
item_quants[G.name]++
else
item_quants[G.name] = 1
plants_loaded++ plants_loaded++
var/hasRecord = FALSE //Check to see if this passes or not.
for(var/datum/data/stored_item/I in item_records)
if(istype(G, I.item_path))
stock(G, I)
hasRecord = TRUE
if(!hasRecord)
var/datum/data/stored_item/item = new/datum/data/stored_item(src, G.type)
stock(G, item)
item_records.Add(item)
if(plants_loaded) if(plants_loaded)
user.visible_message("<span class='notice'>[user] loads \the [src] with \the [P].</span>", "<span class='notice'>You load \the [src] with \the [P].</span>") user.visible_message("<span class='notice'>[user] loads \the [src] with \the [P].</span>", "<span class='notice'>You load \the [src] with \the [P].</span>")
@@ -270,6 +336,14 @@
user << "You short out the product lock on [src]." user << "You short out the product lock on [src]."
return 1 return 1
/obj/machinery/smartfridge/proc/stock(obj/item/O, var/datum/data/stored_item/I)
I.add_product(O)
nanomanager.update_uis(src)
/obj/machinery/smartfridge/proc/vend(datum/data/stored_item/I)
I.get_product(get_turf(src))
nanomanager.update_uis(src)
/obj/machinery/smartfridge/attack_ai(mob/user as mob) /obj/machinery/smartfridge/attack_ai(mob/user as mob)
attack_hand(user) attack_hand(user)
@@ -294,11 +368,11 @@
data["secure"] = is_secure data["secure"] = is_secure
var/list/items[0] var/list/items[0]
for (var/i=1 to length(item_quants)) for (var/i=1 to length(item_records))
var/K = item_quants[i] var/datum/data/stored_item/I = item_records[i]
var/count = item_quants[K] var/count = I.get_amount()
if(count > 0) if(count > 0)
items.Add(list(list("display_name" = html_encode(capitalize(K)), "vend" = i, "quantity" = count))) items.Add(list(list("display_name" = html_encode(capitalize(I.item_name)), "vend" = i, "quantity" = count)))
if(items.len > 0) if(items.len > 0)
data["contents"] = items data["contents"] = items
@@ -325,20 +399,15 @@
if(href_list["vend"]) if(href_list["vend"])
var/index = text2num(href_list["vend"]) var/index = text2num(href_list["vend"])
var/amount = text2num(href_list["amount"]) var/amount = text2num(href_list["amount"])
var/K = item_quants[index] var/datum/data/stored_item/I = item_records[index]
var/count = item_quants[K] var/count = I.get_amount()
// Sanity check, there are probably ways to press the button when it shouldn't be possible. // Sanity check, there are probably ways to press the button when it shouldn't be possible.
if(count > 0) if(count > 0)
item_quants[K] = max(count - amount, 0) if((count - amount) < 0)
amount = count
var/i = amount for(var/i = 1 to amount)
for(var/obj/O in contents) vend(I)
if(O.name == K)
O.loc = loc
i--
if(i <= 0)
return 1
return 1 return 1
return 0 return 0
@@ -349,17 +418,12 @@
if(!target) if(!target)
return 0 return 0
for (var/O in item_quants) for(var/datum/data/stored_item/I in src.item_records)
if(item_quants[O] <= 0) //Try to use a record that actually has something to dump. throw_item = I.get_product(loc)
if (!throw_item)
continue continue
break
item_quants[O]--
for(var/obj/T in contents)
if(T.name == O)
T.loc = src.loc
throw_item = T
break
break
if(!throw_item) if(!throw_item)
return 0 return 0
spawn(0) spawn(0)