Skip to content

Commit

Permalink
Refactors vendor global scope procs (#4918)
Browse files Browse the repository at this point in the history
# About the pull request

Refactors awful vendor global scope procs.

Really this entire file needs looking at why do vending machines need
1300 lines. This is a start at least.

Also removed a bunch of commented out code at the bottom.

# Explain why it's good for the game
# Testing Photographs and Procedure
<details>
<summary>Screenshots & Videos</summary>

Put screenshots and videos here with an empty line between the
screenshots and the `<details>` tags.

</details>


# Changelog
:cl:
code: Refactors vending machine global scope procs
/:cl:
  • Loading branch information
Birdtalon authored Nov 16, 2023
1 parent 674a093 commit b7dbcf9
Showing 1 changed file with 70 additions and 136 deletions.
206 changes: 70 additions & 136 deletions code/game/machinery/vending/cm_vending.dm
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,44 @@ GLOBAL_LIST_EMPTY(vending_products)
//------------TGUI PROCS---------------

/obj/structure/machinery/cm_vending/ui_data(mob/user)
return vendor_user_ui_data(src, user)
if(vend_flags & VEND_LIMITED_INVENTORY)
return vendor_inventory_ui_data(user)

. = list()
var/list/ui_listed_products = get_listed_products(user)
// list format
// (
// name: str
// cost
// item reference
// allowed to buy flag
// item priority (mandatory/recommended/regular)
// )

var/list/stock_values = list()

var/mob/living/carbon/human/marine = user
var/points = 0

if(instanced_vendor_points)
points = available_points_to_display
else
if(use_snowflake_points)
points = marine.marine_snowflake_points
else if(use_points)
points = marine.marine_points

for (var/i in 1 to length(ui_listed_products))
var/list/myprod = ui_listed_products[i] //we take one list from listed_products
var/prod_available = FALSE
var/p_cost = myprod[2]
var/category = myprod[4]
if(points >= p_cost && (!category || ((category in marine.marine_buyable_categories) && (marine.marine_buyable_categories[category]))))
prod_available = TRUE
stock_values += list(prod_available)

.["stock_listing"] = stock_values
.["current_m_points"] = points

/obj/structure/machinery/cm_vending/ui_act(action, list/params, datum/tgui/ui, datum/ui_state/state)
. = ..()
Expand Down Expand Up @@ -530,7 +567,7 @@ GLOBAL_LIST_EMPTY(vending_products)
vend_fail()
return FALSE

if(!handle_vend(src, itemspec, user))
if(!handle_vend(itemspec, user))
to_chat(user, SPAN_WARNING("You can't buy things from this category anymore."))
vend_fail()
return FALSE
Expand All @@ -548,7 +585,7 @@ GLOBAL_LIST_EMPTY(vending_products)
vend_fail()
return TRUE // one left and the player spam click during a lagspike.

vendor_successful_vend(src, itemspec, user)
vendor_successful_vend(itemspec, user)
return TRUE
add_fingerprint(user)

Expand Down Expand Up @@ -779,7 +816,7 @@ GLOBAL_LIST_EMPTY(vending_products)
/obj/structure/machinery/cm_vending/gear/ui_static_data(mob/user)
. = ..(user)
.["vendor_type"] = "gear"
.["displayed_categories"] = vendor_user_inventory_list(src, user)
.["displayed_categories"] = vendor_user_inventory_list(user)

//------------CLOTHING VENDORS---------------
//clothing vendors automatically put item on user. QoL at it's finest.
Expand All @@ -796,7 +833,7 @@ GLOBAL_LIST_EMPTY(vending_products)
/obj/structure/machinery/cm_vending/clothing/ui_static_data(mob/user)
. = ..(user)
.["vendor_type"] = "clothing"
.["displayed_categories"] = vendor_user_inventory_list(src, user)
.["displayed_categories"] = vendor_user_inventory_list(user)

//------------SORTED VENDORS---------------
//22.06.2019 Modified ex-"marine_selector" system that doesn't use points by Jeser. In theory, should replace all vendors.
Expand Down Expand Up @@ -855,7 +892,7 @@ GLOBAL_LIST_EMPTY(vending_products)
/obj/structure/machinery/cm_vending/sorted/ui_static_data(mob/user)
. = ..(user)
.["vendor_type"] = "sorted"
.["displayed_categories"] = vendor_user_inventory_list(src, user, null, 4)
.["displayed_categories"] = vendor_user_inventory_list(user, null, 4)

/obj/structure/machinery/cm_vending/sorted/MouseDrop_T(atom/movable/A, mob/user)

Expand Down Expand Up @@ -934,7 +971,7 @@ GLOBAL_LIST_EMPTY(vending_products)
/obj/structure/machinery/cm_vending/own_points/ui_static_data(mob/user)
. = ..(user)
.["vendor_type"] = "gear"
.["displayed_categories"] = vendor_user_inventory_list(src, user)
.["displayed_categories"] = vendor_user_inventory_list(user)

//------------ESSENTIALS SETS AND RANDOM GEAR SPAWNER---------------

Expand Down Expand Up @@ -1060,7 +1097,7 @@ GLOBAL_LIST_INIT(cm_vending_gear_corresponding_types_list, list(

//---helper procs

/proc/vendor_user_inventory_list(vendor, mob/user, cost_index=2, priority_index=5)
/obj/structure/machinery/cm_vending/proc/vendor_user_inventory_list(mob/user, cost_index=2, priority_index=5)
. = list()
// default list format
// (
Expand All @@ -1070,8 +1107,7 @@ GLOBAL_LIST_INIT(cm_vending_gear_corresponding_types_list, list(
// allowed to buy flag
// item priority (mandatory/recommended/regular)
// )
var/obj/structure/machinery/cm_vending/vending_machine = vendor
var/list/ui_listed_products = vending_machine.get_listed_products(user)
var/list/ui_listed_products = get_listed_products(user)

for (var/i in 1 to length(ui_listed_products))
var/list/myprod = ui_listed_products[i] //we take one list from listed_products
Expand Down Expand Up @@ -1113,10 +1149,9 @@ GLOBAL_LIST_INIT(cm_vending_gear_corresponding_types_list, list(
var/last_category = .[last_index]
last_category["items"] += list(display_item)

/proc/vendor_inventory_ui_data(vendor, mob/user)
/obj/structure/machinery/cm_vending/proc/vendor_inventory_ui_data(mob/user)
. = list()
var/obj/structure/machinery/cm_vending/vending_machine = vendor
var/list/ui_listed_products = vending_machine.get_listed_products(user)
var/list/ui_listed_products = get_listed_products(user)
var/list/ui_categories = list()

for (var/i in 1 to length(ui_listed_products))
Expand All @@ -1125,92 +1160,50 @@ GLOBAL_LIST_INIT(cm_vending_gear_corresponding_types_list, list(
ui_categories += list(p_amount)
.["stock_listing"] = ui_categories

/proc/vendor_user_ui_data(obj/structure/machinery/cm_vending/vending_machine, mob/user)
if(vending_machine.vend_flags & VEND_LIMITED_INVENTORY)
return vendor_inventory_ui_data(vending_machine, user)

. = list()
var/list/ui_listed_products = vending_machine.get_listed_products(user)
// list format
// (
// name: str
// cost
// item reference
// allowed to buy flag
// item priority (mandatory/recommended/regular)
// )

var/list/stock_values = list()

var/mob/living/carbon/human/marine = user
var/points = 0

if(vending_machine.instanced_vendor_points)
points = vending_machine.available_points_to_display
else
if(vending_machine.use_snowflake_points)
points = marine.marine_snowflake_points
else if(vending_machine.use_points)
points = marine.marine_points

for (var/i in 1 to length(ui_listed_products))
var/list/myprod = ui_listed_products[i] //we take one list from listed_products
var/prod_available = FALSE
var/p_cost = myprod[2]
var/category = myprod[4]
if(points >= p_cost && (!category || ((category in marine.marine_buyable_categories) && (marine.marine_buyable_categories[category]))))
prod_available = TRUE
stock_values += list(prod_available)

.["stock_listing"] = stock_values
.["current_m_points"] = points

/proc/vendor_successful_vend(obj/structure/machinery/cm_vending/vendor, list/itemspec, mob/living/carbon/human/user)
if(vendor.stat & IN_USE)
/obj/structure/machinery/cm_vending/proc/vendor_successful_vend(list/itemspec, mob/living/carbon/human/user)
if(stat & IN_USE)
return
vendor.stat |= IN_USE
stat |= IN_USE

var/vend_flags = vendor.vend_flags
var/turf/target_turf = vendor.get_appropriate_vend_turf(user)
var/turf/target_turf = get_appropriate_vend_turf(user)
if(LAZYLEN(itemspec)) //making sure it's not empty
if(vendor.vend_delay)
vendor.overlays.Cut()
vendor.icon_state = "[initial(vendor.icon_state)]_vend"
if(vendor.vend_sound)
playsound(vendor.loc, vendor.vend_sound, 25, 1, 2) //heard only near vendor
sleep(vendor.vend_delay)
if(vend_delay)
overlays.Cut()
icon_state = "[initial(icon_state)]_vend"
if(vend_sound)
playsound(loc, vend_sound, 25, 1, 2) //heard only near vendor
sleep(vend_delay)

var/prod_type = itemspec[3]
if(islist(prod_type))
for(var/each_type in prod_type)
vendor_successful_vend_one(vendor, each_type, user, target_turf, itemspec[4] == MARINE_CAN_BUY_UNIFORM)
vendor_successful_vend_one(each_type, user, target_turf, itemspec[4] == MARINE_CAN_BUY_UNIFORM)
else
vendor_successful_vend_one(vendor, prod_type, user, target_turf, itemspec[4] == MARINE_CAN_BUY_UNIFORM)
vendor_successful_vend_one(prod_type, user, target_turf, itemspec[4] == MARINE_CAN_BUY_UNIFORM)

if(vend_flags & VEND_LIMITED_INVENTORY)
itemspec[2]--
if(vend_flags & VEND_LOAD_AMMO_BOXES)
vendor.update_derived_ammo_and_boxes(itemspec)
update_derived_ammo_and_boxes(itemspec)

else
to_chat(user, SPAN_WARNING("ERROR: itemspec is missing. Please report this to admins."))
sleep(15)

vendor.stat &= ~IN_USE
vendor.icon_state = initial(vendor.icon_state)
vendor.update_icon()
stat &= ~IN_USE
icon_state = initial(icon_state)
update_icon()

/proc/vendor_successful_vend_one(obj/structure/machinery/cm_vending/vendor, prod_type, mob/living/carbon/human/user, turf/target_turf, insignas_override)
/obj/structure/machinery/cm_vending/proc/vendor_successful_vend_one(prod_type, mob/living/carbon/human/user, turf/target_turf, insignas_override)
var/obj/item/new_item
var/vend_flags = vendor.vend_flags
if(ispath(prod_type, /obj/item))
if(ispath(prod_type, /obj/item/weapon/gun))
new_item = new prod_type(target_turf, TRUE)
else
if(prod_type == /obj/item/device/radio/headset/almayer/marine)
prod_type = vendor.headset_type
prod_type = headset_type
else if(prod_type == /obj/item/clothing/gloves/marine)
prod_type = vendor.gloves_type
prod_type = gloves_type
new_item = new prod_type(target_turf)
new_item.add_fingerprint(user)
else
Expand Down Expand Up @@ -1239,13 +1232,13 @@ GLOBAL_LIST_INIT(cm_vending_gear_corresponding_types_list, list(

if(vend_flags & VEND_TO_HAND)
if(user.client?.prefs && (user.client?.prefs?.toggle_prefs & TOGGLE_VEND_ITEM_TO_HAND))
if(vendor.Adjacent(user))
if(Adjacent(user))
user.put_in_any_hand_if_possible(new_item, disable_warning = TRUE)

new_item.post_vendor_spawn_hook(user)

/proc/handle_vend(obj/structure/machinery/cm_vending/vendor, list/listed_products, mob/living/carbon/human/vending_human)
if(vendor.vend_flags & VEND_USE_VENDOR_FLAGS)
/obj/structure/machinery/cm_vending/proc/handle_vend(list/listed_products, mob/living/carbon/human/vending_human)
if(vend_flags & VEND_USE_VENDOR_FLAGS)
return TRUE
var/buying_category = listed_products[4]
if(buying_category)
Expand Down Expand Up @@ -1281,62 +1274,3 @@ GLOBAL_LIST_INIT(cm_vending_gear_corresponding_types_list, list(
stat &= ~IN_USE
if(destroy)
qdel(src)

//------------HACKING---------------

//Hacking code from old vendors, in case someone will actually would like to add complex hacking in future. For now, simple access hacking I believe sufficient.
/*
/obj/structure/machinery/vending/proc/get_wire_descriptions()
return list(
VENDING_WIRE_EXTEND = "Inventory control computer",
VENDING_WIRE_IDSCAN = "ID scanner",
VENDING_WIRE_SHOCK = "Ground safety",
VENDING_WIRE_SHOOT_INV = "Dispenser motor control"
)
/obj/structure/machinery/vending/proc/isWireCut(wire)
return !(wires & getWireFlag(wire))
/obj/structure/machinery/vending/proc/cut(wire)
wires ^= getWireFlag(wire)
switch(wire)
if(VENDING_WIRE_EXTEND)
src.extended_inventory = 0
visible_message(SPAN_NOTICE("A weak yellow light turns off underneath \the [src]."))
if(VENDING_WIRE_SHOCK)
src.seconds_electrified = -1
visible_message(SPAN_DANGER("Electric arcs shoot off from \the [src]!"))
if (VENDING_WIRE_SHOOT_INV)
if(!src.shoot_inventory)
src.shoot_inventory = TRUE
visible_message(SPAN_WARNING("\The [src] begins whirring noisily."))
/obj/structure/machinery/vending/proc/mend(wire)
wires |= getWireFlag(wire)
switch(wire)
if(VENDING_WIRE_EXTEND)
src.extended_inventory = 1
visible_message(SPAN_NOTICE("A weak yellow light turns on underneath \the [src]."))
if(VENDING_WIRE_SHOCK)
src.seconds_electrified = 0
if (VENDING_WIRE_SHOOT_INV)
src.shoot_inventory = FALSE
visible_message(SPAN_NOTICE("\The [src] stops whirring."))
/obj/structure/machinery/vending/proc/pulse(wire)
switch(wire)
if(VENDING_WIRE_EXTEND)
src.extended_inventory = !src.extended_inventory
visible_message(SPAN_NOTICE("A weak yellow light turns [extended_inventory ? "on" : "off"] underneath \the [src]."))
if (VENDING_WIRE_SHOCK)
src.seconds_electrified = 30
visible_message(SPAN_DANGER("Electric arcs shoot off from \the [src]!"))
if (VENDING_WIRE_SHOOT_INV)
src.shoot_inventory = !src.shoot_inventory
if(shoot_inventory)
visible_message(SPAN_WARNING("\The [src] begins whirring noisily."))
else
visible_message(SPAN_NOTICE("\The [src] stops whirring."))
*/

0 comments on commit b7dbcf9

Please sign in to comment.