Add persistent smartfridges with lossiness

This commit is contained in:
Chompstation Bot
2021-05-15 17:15:26 +00:00
committed by Darlantan
parent faad3af89e
commit 9784331cdb
21 changed files with 682 additions and 183 deletions
+44 -28
View File
@@ -6,9 +6,9 @@
for(var/datum/stored_item/I in entry.item_records)
.[I.item_path] = I.get_amount()
/datum/persistent/storage/smartfridge/CreateEntryInstance(var/turf/creating, var/list/tokens)
/datum/persistent/storage/smartfridge/CreateEntryInstance(var/turf/creating, var/list/token)
var/obj/machinery/smartfridge/S = find_specific_instance(creating)
var/list/L = generate_items(tokens["items"])
var/list/L = generate_items(token["items"])
for(var/atom/A in L)
if(S.accept_check(A))
S.stock(A)
@@ -20,50 +20,64 @@
/datum/persistent/storage/smartfridge/sheet_storage
name = "sheet_storage"
name = "sheet storage"
max_storage = 50
store_per_type = TRUE
target_type = /obj/machinery/smartfridge/sheets
var/stacks_go_missing = FALSE // Variable rate depletion of stacks inter-round
/datum/persistent/storage/smartfridge/sheet_storage/lossy
name = "sheet storage lossy"
max_storage = 250
stacks_go_missing = TRUE
/datum/persistent/storage/smartfridge/sheet_storage/generate_items(var/list/L)
. = list()
for(var/obj/item/stack/material/S as anything in L)
if(!ispath(S, /obj/item/stack/material))
var/real_path = istext(S) ? text2path(S) : S
if(!ispath(real_path, /obj/item/stack/material))
log_debug("Warning: Sheet_storage persistent datum tried to create [S]")
continue
var/count = L[S]
while(count > 0)
S = new S
S.amount = min(count, S.get_max_amount())
count -= S.get_amount()
. += S
// Skip entire stack if we hit the chance
if(prob(go_missing_chance))
continue
var/count = L[S]
var/obj/item/stack/material/inst = real_path
var/max_amount = initial(inst.max_amount)
// Delete some stacks if we want
if(stacks_go_missing)
var/fuzzy = rand(-5,5)
switch(count / max_amount)
if(0 to 1)
count -= 10+fuzzy // 1 stack or less, lose 10
if(1 to 10)
count -= max_amount+fuzzy // 1 to 10 stacks, lose a stack
if(10 to INFINITY)
count -= max_amount*3+fuzzy // 10+ stacks, lose 3 stacks
if(count <= 0)
continue
while(count > 0)
inst = new real_path
inst.amount = min(count, max_amount)
count -= inst.get_amount()
. += inst
/datum/persistent/storage/smartfridge/produce
name = "fruit_storage"
name = "fruit storage"
max_storage = 50
store_per_type = FALSE
target_type = /obj/machinery/smartfridge/produce
/datum/persistent/storage/smartfridge/produce/assemble_token(var/T)
var/list/subtok = splittext(T, " ")
if(subtok.len != 2)
return null
if(!istype(SSplants)) // No seed controller means the fruit will come out all wonky if at all
return null
subtok[2] = text2num(subtok[2])
// Ensure we've found a token describing the quantity of a path
if(subtok.len != 2 || \
!istype(SSplants.seeds[subtok[1]], /datum/seed) || \
!isnum(subtok[2]))
return null
return subtok
/datum/persistent/storage/smartfridge/produce/lossy
name = "fruit storage lossy"
go_missing_chance = 12.5 // 10% loss between rounds
/datum/persistent/storage/smartfridge/produce/create_item(var/seedtype)
return new /obj/item/weapon/reagent_containers/food/snacks/grown(null, seedtype) // Smartfridge will be stock()ed with it, loc is unimportant
@@ -74,6 +88,8 @@
. = list()
for(var/datum/stored_item/I in entry.item_records)
if(prob(go_missing_chance))
continue
if(LAZYLEN(I.instances))
var/obj/item/weapon/reagent_containers/food/snacks/grown/G = I.instances[1]
if(!istype(G))
+26 -33
View File
@@ -1,40 +1,23 @@
/datum/persistent/storage
name = "storage"
entries_expire_at = 1
has_admin_data = TRUE
// Don't use these for storage persistence. If someone takes some sheets out and puts them back in mixed in with
// new sheets, how do you know the age of the stack? If you want sheets to 'decay', see go_missing_chance
entries_decay_at = 0
entry_decay_weight = 0
tokens_per_line = PERSISTENCE_VARIABLE_TOKEN_LENGTH
// // // //
var/max_storage = 0
var/store_per_type = FALSE // If true, will store up to max_storage for each type stored
var/target_type = null // Path of the thing that this expects to put stuff into
var/go_missing_chance = 0 // Chance an item will fail to be spawned in from persistence and need to be restocked
/datum/persistent/storage/SetFilename()
if(name)
filename = "data/persistent/storage/[lowertext(using_map.name)]-[lowertext(name)].txt"
/datum/persistent/storage/LabelTokens(var/list/tokens)
. = ..()
.["items"] = list()
for(var/T in tokens)
var/list/L = assemble_token(T)
if(LAZYLEN(L))
.["items"][L[1]] = text2num(L[2])
/datum/persistent/storage/proc/assemble_token(var/T)
var/list/subtok = splittext(T, " ")
if(subtok.len != 2)
return null
subtok[1] = text2path(subtok[1])
subtok[2] = text2num( subtok[2])
// Ensure we've found a token describing the quantity of a path
if(subtok.len != 2 || \
!ispath(subtok[1]) || \
!isnum(subtok[2]))
return null
return subtok
filename = "data/persistent/storage/[lowertext(using_map.name)]-[lowertext(name)].json"
/datum/persistent/storage/IsValidEntry(var/atom/entry)
return ..() && istype(entry, target_type)
@@ -52,9 +35,8 @@
if(!store_per_type)
stored = max(stored - item_list[item], 0)
for(var/item in storage_list)
. += "[item] [storage_list[item]]"
LAZYADDASSOC(., "items", storage_list)
// Usage: returns list with structure:
// list(
// [type1] = [stored_quantity],
@@ -66,16 +48,27 @@
/datum/persistent/storage/proc/find_specific_instance(var/turf/T)
return locate(target_type) in T
/datum/persistent/storage/CheckTurfContents(var/turf/T, var/list/tokens)
/datum/persistent/storage/CheckTurfContents(var/turf/T, var/list/token)
return istype(find_specific_instance(T), target_type)
/datum/persistent/storage/proc/generate_items(var/list/L)
. = list()
for(var/path in L)
// byond's json implementation is "questionable", and uses types as keys and values without quotes sometimes even though they aren't valid json
var/real_path = istext(path) ? text2path(path) : path
for(var/i in 1 to L[path])
var/atom/A = create_item(path)
if(prob(go_missing_chance))
continue
var/atom/A = create_item(real_path)
if(!QDELETED(A))
. += A
/datum/persistent/storage/proc/create_item(var/path)
return new path()
return new path()
/datum/persistent/storage/GetAdminDataStringFor(var/thing, var/can_modify, var/mob/user)
var/atom/T = thing
if(!istype(T))
return "<td><Missing entry><td>"
else
. = "<td colspan = 2>[T.name]</td><td>[T.x],[T.y],[T.z]</td><td>"