Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 78e1e3f

Browse files
removed find_amount and replaced with datums
1 parent df1e8f3 commit 78e1e3f

File tree

1 file changed

+70
-62
lines changed

1 file changed

+70
-62
lines changed

code/modules/food_and_drinks/kitchen_machinery/food_cart.dm

Lines changed: 70 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
///For sending images to the UI with base 64
2-
/datum/data/ui_image
1+
///Holding info for UI elements
2+
/datum/data/cart_item
3+
///Amount of item in contents
4+
var/amount = null
5+
///Item's type path
6+
var/type_path = null
37
///Image to be shown in UI
48
var/image = null
59

6-
//Thanks hedgehog1029 for the help on this
7-
/datum/data/ui_image/New(obj/item/I)
10+
//Thanks hedgehog1029 for the help on base64 icon
11+
/datum/data/cart_item/New(obj/item/I)
12+
name = I.name
13+
amount = 1
14+
type_path = I.type
815
var/icon/icon = icon(I.icon, I.icon_state, SOUTH, 1)
916
image = icon2base64(icon)
1017

@@ -57,42 +64,46 @@
5764
data["storage"] = list()
5865

5966
//Make sure food_ui_list has desired contents
60-
//This, combined with the find_amount() bellow, allow parmesan cheese to show in the UI after maturing
61-
for(var/obj/list_element in contents)
67+
//This, along with contents check for food_ui_list, allows parmesan cheese to show in the UI after maturing
68+
var/in_list
69+
for(var/obj/item/content_item in contents)
70+
in_list = FALSE
6271
//Only check food items
63-
if(istype(list_element, /obj/item/reagent_containers/food))
64-
//Add to list if not already in it
65-
if(!LAZYFIND(food_ui_list, list_element.type))
66-
LAZYADD(food_ui_list, list_element.type)
72+
if(istype(content_item, /obj/item/reagent_containers/food/snacks))
73+
//Loop through all datums in ui list to check if item is in the UI
74+
for(var/datum/data/cart_item/item in food_ui_list)
75+
if(item.type_path == content_item.type)
76+
in_list = TRUE
77+
break
78+
//If not already in food_ui_list, add to it
79+
if(!in_list)
80+
LAZYADD(food_ui_list, new /datum/data/cart_item(content_item))
6781

68-
6982
//Loop through food list for data to send to food tab
70-
for(var/item_detail in food_ui_list)
83+
for(var/datum/data/cart_item/item_detail in food_ui_list)
84+
in_list = FALSE
7185
//If none are found in contents, remove from list and move on to next element
72-
if(find_amount(item_detail) == 0)
86+
for(var/obj/content_item in contents)
87+
if(content_item.type == item_detail.type_path)
88+
in_list = TRUE
89+
if(!in_list)
7390
LAZYREMOVE(food_ui_list, item_detail)
74-
continue
91+
break
7592

7693
//Create needed list and variable for geting data for UI
7794
var/list/details = list()
78-
var/obj/item/reagent_containers/food/item = new item_detail
7995

8096
//Get information for UI
81-
details["name"] = item.name
82-
details["quantity"] = find_amount(item)
83-
details["type_path"] = item.type
97+
details["name"] = item_detail.name
98+
details["quantity"] = item_detail.amount
99+
details["type_path"] = item_detail.type_path
84100

85101
//Get an image for the UI
86-
var/datum/data/ui_image/ui_image = new /datum/data/ui_image(item)
87-
details["image"] = ui_image.image
102+
details["image"] = item_detail.image
88103

89104
//Add to food list
90105
data["food"] += list(details)
91106

92-
//Delete instances to prevent server being overrun by spoopy ghost items
93-
qdel(item)
94-
qdel(ui_image)
95-
96107
//Loop through drink list for data to send to cart's reagent storage tab
97108
for(var/datum/reagent/drink in reagents.reagent_list)
98109
var/list/details = list()
@@ -206,33 +217,47 @@
206217
..()
207218

208219
/obj/machinery/food_cart/proc/dispense_item(received_item, mob/user = usr)
209-
//Make a variable for checking amount of item
210-
var/obj/item/reagent_containers/food/ui_item = new received_item
211-
212-
//If the vat has some of the desired item, dispense it
213-
if(find_amount(ui_item) > 0)
214-
//Select the last(most recent) of desired item
215-
var/obj/item/reagent_containers/food/snacks/dispensed_item = LAZYACCESS(contents, last_index(ui_item))
216-
//Move it into the user's hands or drop it on the floor
217-
user.put_in_hands(dispensed_item)
218-
user.visible_message(span_notice("[user] dispenses [dispensed_item.name] from [src]."), span_notice("You dispense [dispensed_item.name] from [src]."))
219-
playsound(src, dispense_sound, 25, TRUE, extrarange = -3)
220-
//If the last one was dispenced, remove from UI
221-
if(!find_amount(ui_item))
222-
LAZYREMOVE(food_ui_list, received_item)
220+
//Get list item for recieved_item
221+
var/datum/data/cart_item/ui_item = null
222+
for(var/datum/data/cart_item/item in food_ui_list)
223+
if(received_item == item.type_path)
224+
ui_item = item
225+
break
226+
//Continue if ui_item is not null
227+
if(ui_item)
228+
//If the vat has some of the desired item, dispense it
229+
if(ui_item.amount > 0)
230+
//Select the last(most recent) of desired item
231+
var/obj/item/reagent_containers/food/snacks/dispensed_item = LAZYACCESS(contents, last_index(ui_item.type_path))
232+
//Decrease amount by 1
233+
ui_item.amount -= 1
234+
//Move it into the user's hands or drop it on the floor
235+
user.put_in_hands(dispensed_item)
236+
user.visible_message(span_notice("[user] dispenses [dispensed_item.name] from [src]."), span_notice("You dispense [dispensed_item.name] from [src]."))
237+
playsound(src, dispense_sound, 25, TRUE, extrarange = -3)
238+
//If the last one was dispenced, remove from UI
239+
if(ui_item.amount == 0)
240+
LAZYREMOVE(food_ui_list, ui_item)
241+
else
242+
//Incase the UI buttons are slow to disable themselves
243+
user.balloon_alert(user, "All out!")
223244
else
224-
//Incase the UI buttons are slow to disable themselves
225245
user.balloon_alert(user, "All out!")
226246

227-
//Delete instance
228-
qdel(ui_item)
229-
230247
/obj/machinery/food_cart/proc/storage_single(obj/item/target_item, mob/user = usr)
231248
//Check if there is room, subtract by 1 since the cart's reagent container is added to its contents
232249
if(contents.len - 1 < contents_capacity)
233-
//If item's typepath is not already in food_ui_list, add it
234-
if(!LAZYFIND(food_ui_list, target_item.type))
235-
LAZYADD(food_ui_list, target_item.type)
250+
//If item is not already in food_ui_list, add it
251+
var/in_list = FALSE
252+
for(var/datum/data/cart_item/item in food_ui_list)
253+
if(item.type_path == target_item.type)
254+
//Increment amount by 1
255+
item.amount += 1
256+
//Set in_list to true and end loop
257+
in_list = TRUE
258+
break
259+
if(!in_list)
260+
LAZYADD(food_ui_list, new /datum/data/cart_item(target_item))
236261
//Move item to content
237262
target_item.forceMove(src)
238263
user.visible_message(span_notice("[user] inserts [target_item] into [src]."), span_notice("You insert [target_item] into [src]."))
@@ -258,23 +283,6 @@
258283
else
259284
user.balloon_alert(user, "No Drinking Glasses!")
260285

261-
/obj/machinery/food_cart/proc/find_amount(obj/item/counting_item, target_name = null, list/target_list = null)
262-
var/amount = 0
263-
264-
//If target_list is null, search contents for type paths
265-
if(!target_list)
266-
//Loop through contents, counting every instance of the given target
267-
for(var/obj/item/list_item in contents)
268-
if(list_item.type == counting_item.type)
269-
amount += 1
270-
//Else, search target_list
271-
else
272-
for(var/list_item in target_list)
273-
if(list_item == target_name)
274-
amount += 1
275-
276-
return amount
277-
278286
/obj/machinery/food_cart/proc/last_index(obj/item/search_item)
279287
var/obj/item/reagent_containers/food/snacks/item_index = null
280288

0 commit comments

Comments
 (0)