From d8683b9d861353d5a220bf566ebb18e2490f8d2d Mon Sep 17 00:00:00 2001 From: Iajret Creature <122297233+Steals-The-PRs@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:14:37 +0300 Subject: [PATCH] [MIRROR] Fix a few vendor restocking bugs (#2910) * Fix a few vendor restocking bugs (#82603) ## About The Pull Request So while working on fixing standard RPEDs working at all, I noticed they weren't displaying parts when used on a vendor. Looking into it, it seemed to be because the vendors just called `display_parts(user)` on its own: https://github.com/tgstation/tgstation/blob/1780fcef62e0413da65375a37f7c8030a0371419/code/modules/vending/_vending.dm#L1151 While other machinery would wrap it in a `to_chat(...)` call. https://github.com/tgstation/tgstation/blob/1780fcef62e0413da65375a37f7c8030a0371419/code/game/machinery/_machinery.dm#L971 So, we do that too! But then, during further testing, I noticed it wasn't actually restocking. Ever. Even though it's calling the `restock(...)` proc with the same inputs as you would do manually- oh. _oh._ https://github.com/tgstation/tgstation/blob/1780fcef62e0413da65375a37f7c8030a0371419/code/modules/vending/_vending.dm#L1152-L1154 It's checking the replacer for whether it's a refill canister, and not the contents of it. Has been doing this for about 8 months, apparently. No wonder it wasn't working. Great! This should work, right? One more round of testing! Aaaaand it doesn't actually pay out any credits when restocking this way. That's because it's *specifically* coded on `attackby(...)`. https://github.com/tgstation/tgstation/blob/1780fcef62e0413da65375a37f7c8030a0371419/code/modules/vending/_vending.dm#L707-L715 Alright, well, we move this to a new `post_restock(...)` proc, and call this whenever we're done doing restocks. ```dm /obj/machinery/vending/proc/post_restock(mob/living/user, restocked) if(!restocked) to_chat(user, span_warning("There's nothing to restock!")) return to_chat(user, span_notice("You loaded [restocked] items in [src][credits_contained > 0 ? ", and are rewarded [credits_contained] credits." : "."]")) var/datum/bank_account/cargo_account = SSeconomy.get_dep_account(ACCOUNT_CAR) cargo_account.adjust_money(round(credits_contained * 0.5), "Vending: Restock") var/obj/item/holochip/payday = new(src, credits_contained) try_put_in_hand(payday, user) ``` This is separate from the `restock(...)`, so we can call it when we're _done_ restocking and need a message, rather than sending one for each canister in our RPED. We can't just break the loop on the first fitting canister, because the items we need might be spread over multiple partially full canisters. Third round of testing anyone? Oh hey infinite money glitch. It seems like we never actually reset the `credits_contained` var after paying out with it. Soooo we just append this to our `post_restock(...)` proc, and be done with it. ```dm /obj/machinery/vending/proc/post_restock(mob/living/user, restocked) (...) try_put_in_hand(payday, user) credits_contained = 0 ``` At this point, I felt it better we also reorganized the vendor `exchange_parts(...)` to only have one `display_parts(...)` call and an early return if we can't exchange parts. Any further issues I could find I felt were outside of the scope, and better off atomized into a separate pr. ## Why It's Good For The Game Fixes RPEDs not working with vendors at all. Fixes RPEDs not displaying vendor parts. Fixes restocking vendors with an RPED not giving credits. Fixes vendors not resetting their contained credits when restocked. ## Changelog :cl: fix: RPEDs can be used on vendors again. Note that only bluespace RPEDs can carry vendor refills as of writing. fix: RPEDs can display vendor parts again. fix: Restocking vendors gives credits whether done manually or by RPEDs. fix: Vendors reset their contained credits when restocked. /:cl: --------- * Fix a few vendor restocking bugs --------- Co-authored-by: NovaBot <154629622+NovaBot13@users.noreply.github.com> Co-authored-by: _0Steven <42909981+00-Steven@users.noreply.github.com> Co-authored-by: san7890 --- code/modules/vending/_vending.dm | 56 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/code/modules/vending/_vending.dm b/code/modules/vending/_vending.dm index d4f385f7363..913f3f3fcb8 100644 --- a/code/modules/vending/_vending.dm +++ b/code/modules/vending/_vending.dm @@ -548,6 +548,28 @@ GLOBAL_LIST_EMPTY(vending_machines_to_restock) return . +/** + * After-effects of refilling a vending machine from a refill canister + * + * This takes the amount of products restocked and gives the user our contained credits if needed, + * sending the user a fitting message. + * + * Arguments: + * * user - the user restocking us + * * restocked - the amount of items we've been refilled with + */ +/obj/machinery/vending/proc/post_restock(mob/living/user, restocked) + if(!restocked) + to_chat(user, span_warning("There's nothing to restock!")) + return + + to_chat(user, span_notice("You loaded [restocked] items in [src][credits_contained > 0 ? ", and are rewarded [credits_contained] credits." : "."]")) + var/datum/bank_account/cargo_account = SSeconomy.get_dep_account(ACCOUNT_CAR) + cargo_account.adjust_money(round(credits_contained * 0.5), "Vending: Restock") + var/obj/item/holochip/payday = new(src, credits_contained) + try_put_in_hand(payday, user) + credits_contained = 0 + /** * Refill our inventory from the passed in product list into the record list * @@ -704,15 +726,8 @@ GLOBAL_LIST_EMPTY(vending_machines_to_restock) to_chat(user, span_warning("[canister] is empty!")) else // instantiate canister if needed - var/transferred = restock(canister) - if(transferred) - to_chat(user, span_notice("You loaded [transferred] items in [src][credits_contained > 0 ? ", and are rewarded [credits_contained] credits." : "."]")) - var/datum/bank_account/cargo_account = SSeconomy.get_dep_account(ACCOUNT_CAR) - cargo_account.adjust_money(round(credits_contained * 0.5), "Vending: Restock") - var/obj/item/holochip/payday = new(src, credits_contained) - try_put_in_hand(payday, user) - else - to_chat(user, span_warning("There's nothing to restock!")) + var/restocked = restock(canister) + post_restock(user, restocked) return if(compartmentLoadAccessCheck(user) && !user.combat_mode) @@ -1145,17 +1160,18 @@ GLOBAL_LIST_EMPTY(vending_machines_to_restock) if(!component_parts || !refill_canister) return FALSE - var/moved = 0 - if(panel_open || replacer.works_from_distance) - if(replacer.works_from_distance) - display_parts(user) - for(var/replacer_item in replacer) - if(istype(replacer, refill_canister)) - moved += restock(replacer_item) - else - display_parts(user) - if(moved) - to_chat(user, span_notice("[moved] items restocked.")) + if(!panel_open || replacer.works_from_distance) + to_chat(user, display_parts(user)) + + if(!panel_open && !replacer.works_from_distance) + return FALSE + + var/restocked = 0 + for(var/replacer_item in replacer) + if(istype(replacer_item, refill_canister)) + restocked += restock(replacer_item) + post_restock(user, restocked) + if(restocked > 0) replacer.play_rped_sound() return TRUE