Refactor modular computer (and application) attackby into item_interaction (#84245)

## About The Pull Request

Sooooooooo I was recently notified of an issue (#84185) that popped up
from me replacing the `attackby(...)` chain on id cards, where it's no
longer possible to put money into IDs inside of PDAs by slapping it
against the PDA.
As I expected, this is because modular computers both still use
`attackby(...)`, and would call `attackby(...)` on the ID they contained
if hit with cash.

https://github.com/tgstation/tgstation/blob/24a23009e8ee4d056b6671c70c41feab1a18590b/code/modules/modular_computers/computers/item/computer.dm#L799
Now this could've been an easy one line no-gbp fix where I just replace
it with a direct call to `insert_money(...)` on the ID and call it a
day!
But hey. Might as well get rid of the `attackby(...)` altogether while
we're at it.


First off, because the `attackby(...)` proc was getting quite bloated,
we split off all the specific item behaviours into `[X]_act(...)` type
procs to clean it up.
We then make those return item interaction flags, so we can call them on
`item_interaction(...)` after the right typecheck passes and immediately
return their results.
This also involves replacing the `application_attackby(...)` on
applications with an `application_item_interaction(...)`, and making it
return the item interaction flags too.

The code of each subsection isn't significantly different, though
reorganized a bit in some cases.
Like inserting a computer disks now tries to move it into the computer
_first_ before swapping out whichever disk is already in there, so it
doesn't swap out the disk if putting the new one in fails.
## Why It's Good For The Game

Fixes #84185.
Having more stuff be updated to the proper `item_interaction(...)`
system is cool and good.
This commit is contained in:
_0Steven
2024-06-24 16:20:14 -05:00
committed by GitHub
parent 4e7446e1cb
commit 476973ea6b
8 changed files with 151 additions and 132 deletions
@@ -787,87 +787,6 @@
return
name = "[saved_identification] ([saved_job])"
/obj/item/modular_computer/attackby(obj/item/attacking_item, mob/user, params)
// Check for ID first
if(isidcard(attacking_item) && InsertID(attacking_item, user))
return
// Check for cash next
if(computer_id_slot && iscash(attacking_item))
var/obj/item/card/id/inserted_id = computer_id_slot.GetID()
if(inserted_id)
inserted_id.attackby(attacking_item, user) // If we do, try and put that attacking object in
return
// Inserting a pAI
if(istype(attacking_item, /obj/item/pai_card) && insert_pai(user, attacking_item))
return
if(istype(attacking_item, /obj/item/stock_parts/cell))
if(ismachinery(physical))
return
if(internal_cell)
to_chat(user, span_warning("You try to connect \the [attacking_item] to \the [src], but its connectors are occupied."))
return
if(user && !user.transferItemToLoc(attacking_item, src))
return
internal_cell = attacking_item
to_chat(user, span_notice("You plug \the [attacking_item] to \the [src]."))
return
if(istype(attacking_item, /obj/item/photo))
var/obj/item/photo/attacking_photo = attacking_item
if(store_file(new /datum/computer_file/picture(attacking_photo.picture)))
balloon_alert(user, "photo scanned")
else
balloon_alert(user, "no space!")
return
// Check if any Applications need it
for(var/datum/computer_file/item_holding_app as anything in stored_files)
if(item_holding_app.application_attackby(attacking_item, user))
return
if(istype(attacking_item, /obj/item/paper))
if(stored_paper >= max_paper)
balloon_alert(user, "no more room!")
return
if(!user.temporarilyRemoveItemFromInventory(attacking_item))
return FALSE
balloon_alert(user, "inserted paper")
qdel(attacking_item)
stored_paper++
return
if(istype(attacking_item, /obj/item/paper_bin))
var/obj/item/paper_bin/bin = attacking_item
if(bin.total_paper <= 0)
balloon_alert(user, "empty bin!")
return
var/papers_added //just to keep track
while((bin.total_paper > 0) && (stored_paper < max_paper))
papers_added++
stored_paper++
bin.remove_paper()
if(!papers_added)
return
balloon_alert(user, "inserted paper")
to_chat(user, span_notice("Added in [papers_added] new sheets. You now have [stored_paper] / [max_paper] printing paper stored."))
bin.update_appearance()
return
// Insert a data disk
if(istype(attacking_item, /obj/item/computer_disk))
if(inserted_disk)
user.put_in_hands(inserted_disk)
balloon_alert(user, "disks swapped")
if(!user.transferItemToLoc(attacking_item, src))
return
inserted_disk = attacking_item
playsound(src, 'sound/machines/card_slide.ogg', 50)
return
return ..()
/obj/item/modular_computer/screwdriver_act_secondary(mob/living/user, obj/item/tool)
. = ..()
if(internal_cell)
@@ -902,6 +821,112 @@
update_appearance()
return ITEM_INTERACT_SUCCESS
/obj/item/modular_computer/item_interaction(mob/living/user, obj/item/tool, list/modifiers)
if(isidcard(tool))
return InsertID(tool, user) ? ITEM_INTERACT_SUCCESS : ITEM_INTERACT_BLOCKING
if(iscash(tool))
return money_act(user, tool)
if(istype(tool, /obj/item/pai_card))
return pai_act(user, tool)
if(istype(tool, /obj/item/stock_parts/cell))
return cell_act(user, tool)
if(istype(tool, /obj/item/photo))
return photo_act(user, tool)
// Check if any Applications need our item
for(var/datum/computer_file/item_holding_app as anything in stored_files)
var/app_return = item_holding_app.application_item_interaction(user, tool, modifiers)
if(app_return)
return app_return
if(istype(tool, /obj/item/paper))
return paper_act(user, tool)
if(istype(tool, /obj/item/paper_bin))
return paper_bin_act(user, tool)
if(istype(tool, /obj/item/computer_disk))
return computer_disk_act(user, tool)
/obj/item/modular_computer/proc/money_act(mob/user, obj/item/money)
var/obj/item/card/id/inserted_id = computer_id_slot?.GetID()
if(!inserted_id)
balloon_alert(user, "no ID!")
return ITEM_INTERACT_BLOCKING
return inserted_id.insert_money(money, user) ? ITEM_INTERACT_SUCCESS : ITEM_INTERACT_BLOCKING
/obj/item/modular_computer/proc/pai_act(mob/user, obj/item/pai_card/card)
if(inserted_pai)
return ITEM_INTERACT_BLOCKING
if(!user.transferItemToLoc(card, src))
return ITEM_INTERACT_BLOCKING
inserted_pai = card
balloon_alert(user, "inserted pai")
if(inserted_pai.pai)
inserted_pai.pai.give_messenger_ability()
update_appearance(UPDATE_ICON)
return ITEM_INTERACT_SUCCESS
/obj/item/modular_computer/proc/cell_act(mob/user, obj/item/stock_parts/cell/new_cell)
if(ismachinery(physical))
return ITEM_INTERACT_BLOCKING
if(internal_cell)
to_chat(user, span_warning("You try to connect \the [new_cell] to \the [src], but its connectors are occupied."))
return ITEM_INTERACT_BLOCKING
if(!user.transferItemToLoc(new_cell, src))
return ITEM_INTERACT_BLOCKING
internal_cell = new_cell
to_chat(user, span_notice("You plug \the [new_cell] to \the [src]."))
return ITEM_INTERACT_SUCCESS
/obj/item/modular_computer/proc/photo_act(mob/user, obj/item/photo/scanned_photo)
if(!store_file(new /datum/computer_file/picture(scanned_photo.picture)))
balloon_alert(user, "no space!")
return ITEM_INTERACT_BLOCKING
balloon_alert(user, "photo scanned")
return ITEM_INTERACT_SUCCESS
/obj/item/modular_computer/proc/paper_act(mob/user, obj/item/paper/new_paper)
if(stored_paper >= max_paper)
balloon_alert(user, "no more room!")
return ITEM_INTERACT_BLOCKING
if(!user.temporarilyRemoveItemFromInventory(new_paper))
return ITEM_INTERACT_BLOCKING
balloon_alert(user, "inserted paper")
qdel(new_paper)
stored_paper++
return ITEM_INTERACT_SUCCESS
/obj/item/modular_computer/proc/paper_bin_act(mob/user, obj/item/paper_bin/bin)
if(bin.total_paper <= 0)
balloon_alert(user, "empty bin!")
return ITEM_INTERACT_BLOCKING
var/papers_added //just to keep track
while((bin.total_paper > 0) && (stored_paper < max_paper))
papers_added++
stored_paper++
bin.remove_paper()
if(!papers_added)
return ITEM_INTERACT_BLOCKING
balloon_alert(user, "inserted paper")
to_chat(user, span_notice("Added in [papers_added] new sheets. You now have [stored_paper] / [max_paper] printing paper stored."))
bin.update_appearance()
return ITEM_INTERACT_SUCCESS
/obj/item/modular_computer/proc/computer_disk_act(mob/user, obj/item/computer_disk/disk)
if(!user.transferItemToLoc(disk, src))
return ITEM_INTERACT_BLOCKING
if(inserted_disk)
user.put_in_hands(inserted_disk)
balloon_alert(user, "disks swapped")
inserted_disk = disk
playsound(src, 'sound/machines/card_slide.ogg', 50)
return ITEM_INTERACT_SUCCESS
/obj/item/modular_computer/atom_deconstruct(disassembled = TRUE)
remove_pai()
eject_aicard()
@@ -936,18 +961,6 @@
/obj/item/modular_computer/proc/get_messenger_ending()
return "Sent from my PDA"
/obj/item/modular_computer/proc/insert_pai(mob/user, obj/item/pai_card/card)
if(inserted_pai)
return FALSE
if(!user.transferItemToLoc(card, src))
return FALSE
inserted_pai = card
balloon_alert(user, "inserted pai")
if(inserted_pai.pai)
inserted_pai.pai.give_messenger_ability()
update_appearance(UPDATE_ICON)
return TRUE
/obj/item/modular_computer/proc/remove_pai(mob/user)
if(!inserted_pai)
return FALSE
@@ -141,7 +141,9 @@
return . || NONE
/obj/item/modular_computer/pda/item_interaction(mob/living/user, obj/item/tool, list/modifiers)
. = ..()
if(.)
return .
if(!is_type_in_list(tool, contained_item))
return NONE
if(tool.w_class >= WEIGHT_CLASS_SMALL) // Anything equal to or larger than small won't work
@@ -154,8 +154,8 @@
/obj/machinery/modular_computer/welder_act(mob/user, obj/item/tool)
return CPU_INTERACTABLE(user) ? cpu.welder_act(user, tool) : ..()
/obj/machinery/modular_computer/attackby(obj/item/weapon, mob/living/user)
return (CPU_INTERACTABLE(user) && !user.combat_mode) ? cpu.attackby(weapon, user) : ..()
/obj/machinery/modular_computer/item_interaction(mob/living/user, obj/item/tool, list/modifiers)
return (CPU_INTERACTABLE(user) && !user.combat_mode) ? cpu.item_interaction(user, tool, modifiers) : ..()
/obj/machinery/modular_computer/attacked_by(obj/item/attacking_item, mob/living/user)
return CPU_INTERACTABLE(user) ? cpu.attacked_by(attacking_item, user) : ..()
@@ -66,9 +66,9 @@
/datum/computer_file/proc/on_examine(obj/item/modular_computer/source, mob/user)
return null
/// Called when attacking a tablet with an item, checking if any application uses it. Return TRUE to cancel the attack chain.
/datum/computer_file/proc/application_attackby(obj/item/attacking_item, mob/living/user)
return FALSE
/// Called on modular computer item_interaction, checking if any application uses the given item. Uses the item interaction chain flags.
/datum/computer_file/proc/application_item_interaction(mob/living/user, obj/item/tool, list/modifiers)
return NONE
/**
* Implement this when your program has an object that the user can eject.
@@ -57,22 +57,22 @@
return TRUE
/datum/computer_file/program/ai_restorer/application_attackby(obj/item/attacking_item, mob/living/user)
/datum/computer_file/program/ai_restorer/application_item_interaction(mob/living/user, obj/item/tool, list/modifiers)
if(istype(tool, /obj/item/aicard))
return aicard_act(user, tool)
/datum/computer_file/program/ai_restorer/proc/aicard_act(mob/living/user, obj/item/aicard/used_aicard)
if(!computer)
return FALSE
if(!istype(attacking_item, /obj/item/aicard))
return FALSE
return NONE
if(stored_card)
to_chat(user, span_warning("You try to insert \the [attacking_item] into \the [computer.name], but the slot is occupied."))
return FALSE
if(user && !user.transferItemToLoc(attacking_item, computer))
return FALSE
to_chat(user, span_warning("You try to insert \the [used_aicard] into \the [computer.name], but the slot is occupied."))
return ITEM_INTERACT_BLOCKING
if(!user.transferItemToLoc(used_aicard, computer))
return ITEM_INTERACT_BLOCKING
stored_card = attacking_item
to_chat(user, span_notice("You insert \the [attacking_item] into \the [computer.name]."))
return TRUE
stored_card = used_aicard
to_chat(user, span_notice("You insert \the [used_aicard] into \the [computer.name]."))
return ITEM_INTERACT_SUCCESS
/datum/computer_file/program/ai_restorer/try_eject(mob/living/user, forced = FALSE)
if(!stored_card)
@@ -24,13 +24,16 @@
if(!CONFIG_GET(flag/no_default_techweb_link) && !linked_techweb)
CONNECT_TO_RND_SERVER_ROUNDSTART(linked_techweb, computer)
/datum/computer_file/program/scipaper_program/application_attackby(obj/item/attacking_item, mob/living/user)
if(!istype(attacking_item, /obj/item/multitool))
return FALSE
var/obj/item/multitool/attacking_tool = attacking_item
if(!QDELETED(attacking_tool.buffer) && istype(attacking_tool.buffer, /datum/techweb))
linked_techweb = attacking_tool.buffer
return TRUE
/datum/computer_file/program/scipaper_program/application_item_interaction(mob/living/user, obj/item/tool, list/modifiers)
if(istype(tool, /obj/item/multitool))
return multitool_act(user, tool)
/datum/computer_file/program/scipaper_program/proc/multitool_act(mob/living/user, obj/item/multitool/used_multitool)
if(QDELETED(used_multitool.buffer) || !istype(used_multitool.buffer, /datum/techweb))
return ITEM_INTERACT_BLOCKING
linked_techweb = used_multitool.buffer
computer.balloon_alert(user, "buffer linked!")
return ITEM_INTERACT_SUCCESS
/datum/computer_file/program/scipaper_program/proc/recheck_file_presence()
if(selected_file in computer.get_files(include_disk_files = TRUE))
@@ -17,12 +17,10 @@
unsync_modsuit()
return ..()
/datum/computer_file/program/maintenance/modsuit_control/application_attackby(obj/item/attacking_item, mob/living/user)
. = ..()
if(!istype(attacking_item, /obj/item/mod/control))
return FALSE
sync_modsuit(attacking_item, user)
return TRUE
/datum/computer_file/program/maintenance/modsuit_control/application_item_interaction(mob/living/user, obj/item/tool, list/modifiers)
if(istype(tool, /obj/item/mod/control))
sync_modsuit(tool, user)
return ITEM_INTERACT_SUCCESS
/datum/computer_file/program/maintenance/modsuit_control/proc/sync_modsuit(obj/item/mod/control/new_modsuit, mob/living/user)
if(controlled_suit)
@@ -26,13 +26,16 @@
if(!CONFIG_GET(flag/no_default_techweb_link) && !stored_research)
CONNECT_TO_RND_SERVER_ROUNDSTART(stored_research, computer)
/datum/computer_file/program/science/application_attackby(obj/item/attacking_item, mob/living/user)
if(!istype(attacking_item, /obj/item/multitool))
return FALSE
var/obj/item/multitool/attacking_tool = attacking_item
if(!QDELETED(attacking_tool.buffer) && istype(attacking_tool.buffer, /datum/techweb))
stored_research = attacking_tool.buffer
return TRUE
/datum/computer_file/program/science/application_item_interaction(mob/living/user, obj/item/tool, list/modifiers)
if(istype(tool, /obj/item/multitool))
return multitool_act(user, tool)
/datum/computer_file/program/science/proc/multitool_act(mob/living/user, obj/item/multitool/used_multitool)
if(QDELETED(used_multitool.buffer) || !istype(used_multitool.buffer, /datum/techweb))
return ITEM_INTERACT_BLOCKING
stored_research = used_multitool.buffer
computer.balloon_alert(user, "buffer linked!")
return ITEM_INTERACT_SUCCESS
/datum/computer_file/program/science/ui_assets(mob/user)
return list(