From 37814ef3839d2234e25c3cf9ae88fdcc372dc510 Mon Sep 17 00:00:00 2001 From: Fluffy <65877598+FluffyGhoster@users.noreply.github.com> Date: Fri, 31 May 2024 23:25:02 +0200 Subject: [PATCH] Refactored item equipement observable away (#19241) Refactored item equipement observable away, turned them into signals. Partially ported on_equipped / equipped procs from TG, updated logic, added signals for them. --- aurorastation.dme | 1 - .../signals/signals_object/signals_object.dm | 8 +++ code/datums/observation/equipped.dm | 38 ------------ code/game/objects/items.dm | 58 +++++++++++++----- code/modules/cooking/trays.dm | 7 ++- .../mob/living/carbon/human/inventory.dm | 38 ++++++------ ...luffyghost-itemequipmentobservableaway.yml | 59 +++++++++++++++++++ 7 files changed, 134 insertions(+), 75 deletions(-) delete mode 100644 code/datums/observation/equipped.dm create mode 100644 html/changelogs/fluffyghost-itemequipmentobservableaway.yml diff --git a/aurorastation.dme b/aurorastation.dme index 9086ab58fea..112de1ae8b3 100644 --- a/aurorastation.dme +++ b/aurorastation.dme @@ -455,7 +455,6 @@ #include "code\datums\observation\destroyed.dm" #include "code\datums\observation\dir_set.dm" #include "code\datums\observation\entered.dm" -#include "code\datums\observation\equipped.dm" #include "code\datums\observation\exited.dm" #include "code\datums\observation\helpers.dm" #include "code\datums\observation\instruments.dm" diff --git a/code/__DEFINES/dcs/signals/signals_object/signals_object.dm b/code/__DEFINES/dcs/signals/signals_object/signals_object.dm index 6ceb286e458..e0b2007c016 100644 --- a/code/__DEFINES/dcs/signals/signals_object/signals_object.dm +++ b/code/__DEFINES/dcs/signals/signals_object/signals_object.dm @@ -5,6 +5,14 @@ // /obj/item signals +///from base of obj/item/equipped(): (mob/equipper, slot) +#define COMSIG_ITEM_EQUIPPED "item_equip" +///From base of obj/item/on_equipped() (mob/equipped, slot) +#define COMSIG_ITEM_POST_EQUIPPED "item_post_equipped" + /// This will make the on_equipped proc return FALSE. + #define COMPONENT_EQUIPPED_FAILED (1<<0) +/// A mob has just equipped an item. Called on [/mob] from base of [/obj/item/equipped()]: (/obj/item/equipped_item, slot) +#define COMSIG_MOB_EQUIPPED_ITEM "mob_equipped_item" ///from base of obj/item/dropped(): (mob/user) #define COMSIG_ITEM_DROPPED "item_drop" ///from base of obj/item/pickup(): (mob/user) diff --git a/code/datums/observation/equipped.dm b/code/datums/observation/equipped.dm deleted file mode 100644 index 0e291de025e..00000000000 --- a/code/datums/observation/equipped.dm +++ /dev/null @@ -1,38 +0,0 @@ -// Observer Pattern Implementation: Equipped -// Registration type: /mob -// -// Raised when: A mob equips an item. -// -// Arguments that the called proc should expect: -// /mob/equipper: The mob that equipped the item. -// /obj/item/item: The equipped item. -// slot: The slot equipped to. - -GLOBAL_DATUM_INIT(mob_equipped_event, /singleton/observ/mob_equipped, new) - -/singleton/observ/mob_equipped - name = "Mob Equipped" - expected_type = /mob - -// Observer Pattern Implementation: Equipped -// Registration type: /obj/item -// -// Raised when: A mob equips an item. -// -// Arguments that the called proc should expect: -// /obj/item/item: The equipped item. -// /mob/equipper: The mob that equipped the item. -// slot: The slot equipped to. - -var/singleton/observ/item_equipped/item_equipped_event = new() - -/singleton/observ/item_equipped - name = "Item Equipped" - expected_type = /obj/item - -/******************** -* Equipped Handling * -********************/ - -/obj/item/proc/check_equipped(var/mob/user, var/slot, var/assisted_equip = FALSE) - return TRUE diff --git a/code/game/objects/items.dm b/code/game/objects/items.dm index 2b0e3d679a2..9f8d41fe9e9 100644 --- a/code/game/objects/items.dm +++ b/code/game/objects/items.dm @@ -544,24 +544,53 @@ /obj/item/proc/on_found(mob/finder as mob) return -// called after an item is placed in an equipment slot -// user is mob that equipped it -// slot uses the slot_X defines found in setup.dm -// for items that can be placed in multiple slots -/obj/item/proc/equipped(var/mob/user, var/slot) +/** + * Called after an item is placed in an equipment slot. Runs equipped(), then sends a signal. + * This should be called last or near-to-last, after all other inventory code stuff is handled. + * + * Arguments: + * * user is mob that equipped it + * * slot uses the slot_X defines found in setup.dm for items that can be placed in multiple slots + * * initial is used to indicate whether or not this is the initial equipment (job datums etc) or just a player doing it + */ +/obj/item/proc/on_equipped(mob/user, slot, initial = FALSE) + SHOULD_NOT_OVERRIDE(TRUE) + equipped(user, slot, initial) + if(SEND_SIGNAL(src, COMSIG_ITEM_POST_EQUIPPED, user, slot) && COMPONENT_EQUIPPED_FAILED) + return FALSE + return TRUE + + +/** + * Called by on_equipped. Don't call this directly, we want the ITEM_POST_EQUIPPED signal to be sent after everything else. + * + * Note that hands count as slots. + * + * Arguments: + * * user is mob that equipped it + * * slot uses the slot_X defines found in setup.dm for items that can be placed in multiple slots + * * initial is used to indicate whether or not this is the initial equipment (job datums etc) or just a player doing it + */ +/obj/item/proc/equipped(mob/user, slot, initial = FALSE) SHOULD_CALL_PARENT(TRUE) + PROTECTED_PROC(TRUE) + SEND_SIGNAL(src, COMSIG_ITEM_EQUIPPED, user, slot) + SEND_SIGNAL(user, COMSIG_MOB_EQUIPPED_ITEM, src, slot) hud_layerise() equip_slot = slot if(user.client) user.client.screen |= src if(user.pulling == src) user.stop_pulling() in_inventory = TRUE - if(slot == slot_l_hand || slot == slot_r_hand) - playsound(src, pickup_sound, PICKUP_SOUND_VOLUME) - else if(slot_flags && slot) - if(equip_sound) - playsound(src, equip_sound, EQUIP_SOUND_VOLUME, TRUE, ignore_walls = FALSE) - else - playsound(src, drop_sound, DROP_SOUND_VOLUME, ignore_walls = FALSE) + + if(!initial) + if(slot == slot_l_hand || slot == slot_r_hand) + playsound(src, pickup_sound, PICKUP_SOUND_VOLUME) + else if(slot_flags && slot) + if(equip_sound) + playsound(src, equip_sound, EQUIP_SOUND_VOLUME, TRUE, ignore_walls = FALSE) + else + playsound(src, drop_sound, DROP_SOUND_VOLUME, ignore_walls = FALSE) + if(item_action_slot_check(user, slot)) add_verb(user, verbs) for(var/v in verbs) @@ -570,13 +599,14 @@ remove_item_verbs(user) //Ěent for observable - GLOB.mob_equipped_event.raise_event(user, src, slot) - item_equipped_event.raise_event(src, user, slot) SEND_SIGNAL(src, COMSIG_ITEM_REMOVE, src) if(user && (z_flags & ZMM_MANGLE_PLANES)) addtimer(CALLBACK(user, /mob/proc/check_emissive_equipment), 0, TIMER_UNIQUE) +/obj/item/proc/check_equipped(var/mob/user, var/slot, var/assisted_equip = FALSE) + return TRUE + //sometimes we only want to grant the item's action if it's equipped in a specific slot. /obj/item/proc/item_action_slot_check(mob/user, slot) return TRUE diff --git a/code/modules/cooking/trays.dm b/code/modules/cooking/trays.dm index fd358e35ed2..63b19efe9f1 100644 --- a/code/modules/cooking/trays.dm +++ b/code/modules/cooking/trays.dm @@ -106,8 +106,8 @@ current_weight += I.w_class add_vis_contents(I) I.vis_flags |= VIS_INHERIT_LAYER | VIS_INHERIT_PLANE - item_equipped_event.register(I, src, PROC_REF(pick_up)) - GLOB.destroyed_event.register(I, src, PROC_REF(unload_item)) + RegisterSignal(I, COMSIG_ITEM_EQUIPPED, PROC_REF(pick_up)) + RegisterSignal(I, COMSIG_QDELETING, PROC_REF(unload_item)) /obj/item/tray/verb/unload() set name = "Unload Tray" @@ -144,7 +144,8 @@ unload_item(contained, moved_to) /obj/item/tray/proc/unload_item(var/obj/item/contained, var/atom/dropspot = null) - item_equipped_event.unregister(contained, src) + UnregisterSignal(contained, COMSIG_ITEM_EQUIPPED) + UnregisterSignal(contained, COMSIG_QDELETING) if(dropspot) contained.forceMove(dropspot) vis_contents.Remove(contained) diff --git a/code/modules/mob/living/carbon/human/inventory.dm b/code/modules/mob/living/carbon/human/inventory.dm index fb8ff156431..6c7b009f41a 100644 --- a/code/modules/mob/living/carbon/human/inventory.dm +++ b/code/modules/mob/living/carbon/human/inventory.dm @@ -243,7 +243,7 @@ This saves us from having to call add_fingerprint() any time something is put in switch(slot) if(slot_back) src.back = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_back(redraw_mob) if(slot_wear_mask) src.wear_mask = W @@ -251,32 +251,32 @@ This saves us from having to call add_fingerprint() any time something is put in update_hair(redraw_mob) //rebuild hair update_inv_l_ear(0) update_inv_r_ear(0) - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_wear_mask(redraw_mob) if(slot_handcuffed) src.handcuffed = W update_inv_handcuffed(redraw_mob) if(slot_legcuffed) src.legcuffed = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_legcuffed(redraw_mob) if(slot_l_hand) src.l_hand = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) W.screen_loc = ui_lhand update_inv_l_hand(redraw_mob) if(slot_r_hand) src.r_hand = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) W.screen_loc = ui_rhand update_inv_r_hand(redraw_mob) if(slot_belt) src.belt = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_belt(redraw_mob) if(slot_wear_id) src.wear_id = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_wear_id(redraw_mob) if(slot_l_ear) src.l_ear = W @@ -288,7 +288,7 @@ This saves us from having to call add_fingerprint() any time something is put in var/obj/item/clothing/ears/offear/O = new /obj/item/clothing/ears/offear(src) O.copy_ear(W) src.r_ear = O - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_l_ear(redraw_mob) if(slot_r_ear) src.r_ear = W @@ -300,19 +300,19 @@ This saves us from having to call add_fingerprint() any time something is put in var/obj/item/clothing/ears/offear/O = new /obj/item/clothing/ears/offear(src) O.copy_ear(W) src.l_ear = O - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_r_ear(redraw_mob) if(slot_glasses) src.glasses = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_glasses(redraw_mob) if(slot_gloves) src.gloves = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_gloves(redraw_mob) if(slot_wrists) src.wrists = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_wrists(redraw_mob) if(slot_head) src.head = W @@ -321,34 +321,34 @@ This saves us from having to call add_fingerprint() any time something is put in update_inv_l_ear(0) update_inv_r_ear(0) update_inv_wear_mask(0) - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_head(redraw_mob) if(slot_shoes) src.shoes = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_shoes(redraw_mob) update_noise_level() if(slot_wear_suit) src.wear_suit = W if(wear_suit.flags_inv & HIDESHOES) update_inv_shoes(0) - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_wear_suit(redraw_mob) if(slot_w_uniform) src.w_uniform = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_w_uniform(redraw_mob) if(slot_l_store) src.l_store = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_pockets(redraw_mob) if(slot_r_store) src.r_store = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_pockets(redraw_mob) if(slot_s_store) src.s_store = W - W.equipped(src, slot, assisted_equip) + W.on_equipped(src, slot, assisted_equip) update_inv_s_store(redraw_mob) if(slot_in_backpack) if(src.get_active_hand() == W) diff --git a/html/changelogs/fluffyghost-itemequipmentobservableaway.yml b/html/changelogs/fluffyghost-itemequipmentobservableaway.yml new file mode 100644 index 00000000000..07c4c8c1a82 --- /dev/null +++ b/html/changelogs/fluffyghost-itemequipmentobservableaway.yml @@ -0,0 +1,59 @@ +################################ +# Example Changelog File +# +# Note: This file, and files beginning with ".", and files that don't end in ".yml" will not be read. If you change this file, you will look really dumb. +# +# Your changelog will be merged with a master changelog. (New stuff added only, and only on the date entry for the day it was merged.) +# When it is, any changes listed below will disappear. +# +# Valid Prefixes: +# bugfix +# - (fixes bugs) +# wip +# - (work in progress) +# qol +# - (quality of life) +# soundadd +# - (adds a sound) +# sounddel +# - (removes a sound) +# rscadd +# - (adds a feature) +# rscdel +# - (removes a feature) +# imageadd +# - (adds an image or sprite) +# imagedel +# - (removes an image or sprite) +# spellcheck +# - (fixes spelling or grammar) +# experiment +# - (experimental change) +# balance +# - (balance changes) +# code_imp +# - (misc internal code change) +# refactor +# - (refactors code) +# config +# - (makes a change to the config files) +# admin +# - (makes changes to administrator tools) +# server +# - (miscellaneous changes to server) +################################# + +# Your name. +author: FluffyGhost + +# Optional: Remove this file after generating master changelog. Useful for PR changelogs that won't get used again. +delete-after: True + +# Any changes you've made. See valid prefix list above. +# INDENT WITH TWO SPACES. NOT TABS. SPACES. +# SCREW THIS UP AND IT WON'T WORK. +# Also, this gets changed to [] after reading. Just remove the brackets when you add new shit. +# Please surround your changes in double quotes ("). It works without them, but if you use certain characters it screws up compiling. The quotes will not show up in the changelog. +changes: + - refactor: "Refactored item equipement observable away, turned them into signals." + - code_imp: "Partially ported on_equipped / equipped procs from TG, updated logic, added signals for them."