Files
Bubberstation/code/modules/paperwork/desk_bell.dm
T
b6369a47b4 Mouse drag & drop refactored attack chain (#83690)
## About The Pull Request
Mouse drag & drop has been refactored into its own attack chain. The
flowchart below summarizes it

![Flowchart](https://github.com/tgstation/tgstation/assets/110812394/d92047ff-d94c-44a6-9e87-354c3d525021)

Brief summary of each proc is as follows

**1. `atom/MouseDrop()`**
- It is now non overridable. No subtype should ever touch this proc
because it performs 2 basic checks
  
a) Measures the time between mouse down & mouse release. If its less
than `LENIENCY_TIME`(0.1 seconds) then the operation is not considered a
drag but a simple click

b) Measures the distance squared between the drag start & end point. If
its less than `LENIENCY_DISTANCE`(16 pixels screen space) then the drag
is considered too small and is discarded

- These 2 sanity checks for drag & drop are applied across all
operations without fail
  
**2. `atom/base_mouse_drop_handler()`**
- This is where atoms handle mouse drag & drop inside the world. Ideally
it is non overridable in most cases because it also performs 2 checks
- Is the dragged object & the drop target adjacent to the player?.
Screen elements always return true for this case
  
- Additional checks can be enforced by `can_perform_action()` done only
on the dragged object. It uses the combined flags of
`interaction_flags_mouse_drop` for both the dragged object & drop target
to determine if the operation is feasible.
     
We do this only on the dragged object because if both the dragged object
& drop target are adjacent to the player then `can_perform_action()`
will return the same results when done on either object so it makes no
difference.

Checks can be bypassed via the `IGNORE_MOUSE_DROP_CHECKS` which is used
by huds & screen elements or in case you want to implement your own
unique checks

**3. `atom/mouse_drop_dragged()`**
- Called on the object that is being dragged, drop target passed here as
well, subtypes do their stuff here
- `COMSIG_MOUSEDROP_ONTO` is sent afterwards. It does not require
subtypes to call their parent proc

**4. `atom/mouse_drop_receive()`**
- Called on the drop target that is receiving the dragged object,
subtypes do their stuff here
- `COMSIG_MOUSEDROPPED_ONTO` is sent afterwards. It does not require
subtypes to call their parent proc

## Why It's Good For The Game
Implements basic sanity checks across all drag & drop operations. Allows
us to reduce code like this


https://github.com/tgstation/tgstation/blob/8c8311e624271a6f6decba8cd643b33b9904534a/code/game/machinery/dna_scanner.dm#L144-L145

Into this

```
if(!iscarbon(target))
	return
```

I'm tired of seeing this code pattern `!Adjacent(user) ||
!user.Adjacent(target)` copy pasted all over the place. Let's just write
that at the atom level & be done with it

## Changelog
🆑
refactor: Mouse drag & drop attack chain has been refactored. Report any
bugs on GitHub
fix: You cannot close the cryo tube on yourself with Alt click like
before
/🆑

---------

Co-authored-by: MrMelbert <51863163+MrMelbert@users.noreply.github.com>
Co-authored-by: Bloop <13398309+vinylspiders@users.noreply.github.com>
2024-06-13 13:28:41 -07:00

125 lines
4.4 KiB
Plaintext

// A receptionist's bell
/obj/structure/desk_bell
name = "desk bell"
desc = "The cornerstone of any customer service job. You feel an unending urge to ring it."
icon = 'icons/obj/service/bureaucracy.dmi'
icon_state = "desk_bell"
layer = OBJ_LAYER
anchored = FALSE
pass_flags = PASSTABLE // Able to place on tables
max_integrity = 5000 // To make attacking it not instantly break it
/// The amount of times this bell has been rang, used to check the chance it breaks
var/times_rang = 0
/// Is this bell broken?
var/broken_ringer = FALSE
/// The cooldown for ringing the bell
COOLDOWN_DECLARE(ring_cooldown)
/// The length of the cooldown. Setting it to 0 will skip all cooldowns alltogether.
var/ring_cooldown_length = 0.3 SECONDS // This is here to protect against tinnitus.
/// The sound the bell makes
var/ring_sound = 'sound/machines/microwave/microwave-end.ogg'
/obj/structure/desk_bell/Initialize(mapload)
. = ..()
register_context()
/obj/structure/desk_bell/add_context(atom/source, list/context, obj/item/held_item, mob/user)
. = ..()
if(held_item?.tool_behaviour == TOOL_WRENCH)
context[SCREENTIP_CONTEXT_RMB] = "Disassemble"
return CONTEXTUAL_SCREENTIP_SET
if(broken_ringer)
if(held_item?.tool_behaviour == TOOL_SCREWDRIVER)
context[SCREENTIP_CONTEXT_LMB] = "Fix"
else
var/click_context = "Ring"
if(prob(1))
click_context = "Annoy"
context[SCREENTIP_CONTEXT_LMB] = click_context
return CONTEXTUAL_SCREENTIP_SET
/obj/structure/desk_bell/attack_hand(mob/living/user, list/modifiers)
. = ..()
if(!COOLDOWN_FINISHED(src, ring_cooldown) && ring_cooldown_length)
return TRUE
if(!ring_bell(user))
to_chat(user, span_notice("[src] is silent. Some idiot broke it."))
if(ring_cooldown_length)
COOLDOWN_START(src, ring_cooldown, ring_cooldown_length)
return TRUE
/obj/structure/desk_bell/attack_paw(mob/user, list/modifiers)
return attack_hand(user, modifiers)
/obj/structure/desk_bell/attackby(obj/item/weapon, mob/living/user, params)
. = ..()
times_rang += weapon.force
ring_bell(user)
// Fix the clapper
/obj/structure/desk_bell/screwdriver_act(mob/living/user, obj/item/tool)
if(broken_ringer)
balloon_alert(user, "repairing...")
tool.play_tool_sound(src)
if(tool.use_tool(src, user, 5 SECONDS))
balloon_alert_to_viewers("repaired")
playsound(user, 'sound/items/change_drill.ogg', 50, vary = TRUE)
broken_ringer = FALSE
times_rang = 0
return ITEM_INTERACT_SUCCESS
return FALSE
return ..()
// Deconstruct
/obj/structure/desk_bell/wrench_act_secondary(mob/living/user, obj/item/tool)
balloon_alert(user, "taking apart...")
tool.play_tool_sound(src)
if(tool.use_tool(src, user, 5 SECONDS))
balloon_alert(user, "disassembled")
playsound(user, 'sound/items/deconstruct.ogg', 50, vary = TRUE)
if(!broken_ringer) // Drop 2 if it's not broken.
new/obj/item/stack/sheet/iron(drop_location())
new/obj/item/stack/sheet/iron(drop_location())
qdel(src)
return ITEM_INTERACT_SUCCESS
return ..()
/// Check if the clapper breaks, and if it does, break it
/obj/structure/desk_bell/proc/check_clapper(mob/living/user)
if(((times_rang >= 10000) || prob(times_rang/100)) && ring_cooldown_length)
to_chat(user, span_notice("You hear [src]'s clapper fall off of its hinge. Nice job, you broke it."))
broken_ringer = TRUE
/// Ring the bell
/obj/structure/desk_bell/proc/ring_bell(mob/living/user)
if(broken_ringer)
return FALSE
check_clapper(user)
// The lack of varying is intentional. The only variance occurs on the strike the bell breaks.
playsound(src, ring_sound, 70, vary = broken_ringer, extrarange = SHORT_RANGE_SOUND_EXTRARANGE)
flick("desk_bell_ring", src)
times_rang++
return TRUE
// A warning to all who enter; the ringing sound STACKS. It won't be deafening because it only goes every decisecond,
// but I did feel like my ears were going to start bleeding when I tested it with my autoclicker.
/obj/structure/desk_bell/speed_demon
desc = "The cornerstone of any customer service job. This one's been modified for hyper-performance."
ring_cooldown_length = 0
/obj/structure/desk_bell/mouse_drop_dragged(atom/over_object, mob/user)
if(!istype(over_object, /obj/vehicle/ridden/wheelchair))
return
var/obj/vehicle/ridden/wheelchair/target = over_object
if(target.bell_attached)
user.balloon_alert(user, "already has a bell!")
return
user.balloon_alert(user, "attaching bell...")
if(!do_after(user, 0.5 SECONDS))
return
target.attach_bell(src)