Files
Bubberstation/code/modules/paperwork/paperbin.dm
SyncIt21 6dc40ca522 Standardizes object deconstruction throughout the codebase. (#82280)
## About The Pull Request
When it comes to deconstructing an object we have `proc/deconstruct()` &
`NO_DECONSTRUCT`

Lets talk about the flag first. 

**Problems with `NO_DECONSTRUCTION`**
I know what the comment says on what it should do

b5593bc693/code/__DEFINES/obj_flags.dm (L18)

But everywhere people have decided to give their own meaning/definition
to this flag. Here are some examples on how this flag is used

**1. Make the object just disappear(not drop anything) when
deconstructed**
This is by far the largest use case everywhere. If an object is
deconstructed(either via tools or smashed apart) then if it has this
flag it should not drop any of its contents but just disappear. You have
seen this code pattern used everywhere

b5593bc693/code/game/machinery/constructable_frame.dm (L26-L31)

This behaviour is then leveraged by 2 important components.

When an object is frozen, if it is deconstructed it should just
disappear without leaving any traces behind

b5593bc693/code/datums/elements/frozen.dm (L66-L67)

By hologram objects. Obviously if you destroy an hologram nothing real
should drop out

b5593bc693/code/modules/holodeck/computer.dm (L301-L304)

And there are other use cases as well but we won't go into them as they
aren't as significant as these.

**2. To stop an object from being wrenched ??**
Yeah this one is weird. Like why? I understand in some instances (chair,
table, rack etc) a wrench can be used to deconstruct a object so using
the flag there to stop it from happening makes sense but why can't we
even anchor an object just because of this flag?

b5593bc693/code/game/objects/objs.dm (L368-L369)
This is one of those instances where somebody just decided this
behaviour for their own convenience just like the above example with no
explanation as to why

**3. To stop using tools to deconstruct the object** 
This was the original intent of the flag but it is enforced in few
places far & between. One example is when deconstructing the a machine
via crowbar.

b5593bc693/code/game/machinery/_machinery.dm (L811)

But machines are a special dual use case for this flag. Because if you
look at its deconstruct proc the flag also prevents the machine from
spawning a frame.

b5593bc693/code/game/machinery/_machinery.dm (L820-L822)

How can 1 flag serve 2 purposes within the same type?

**4. Simply forget to check for this flag altogether**
Yup if you find this flag not doing its job for some objects don't be
surprised. People & sometimes even maintainers just forget that it even
exists

b5593bc693/code/game/objects/items/piggy_bank.dm (L66-L67)

**Solution**
These are the main examples i found. As you can see the same flag can
perform 2 different functions within the same type and do something else
in a different object & in some instances don't even work cause people
just forget, etc.

In order to bring consistency to this flag we need to move it to the
atom level where it means the same thing everywhere. Where in the atom
you may ask? .Well, I'll just post what MrMelbert said in
https://github.com/tgstation/tgstation/pull/81656#discussion_r1503086862

> ...Ideally the .deconstruct call would handle NO_DECONSTRUCTION
handling as it wants,

Yup that's the ideal case now. This flag is checked directly in
`deconstruct()`. Now like i said we want to give a universal definition
to this flag and as you have seen from my examples it is used in 3 cases
1) Make an object disappear(doesn't dropping anything) when
deconstructed
2) Stop it from being wrenched
3) Stop it from being deconstructed via tools

We can't enforce points 2 & 3 inside `deconstruct()` which leaves us
with only case 1) i.e. make the object disappear. And that's what i have
done. Therefore after more than a decade or since this flag got
introduced `NO_DECONSTRUCT` now has a new definition as of 2024

_"Make an object disappear(don't dropping anything) when deconstructed
either via tools or forcefully smashed apart"_

Now i very well understand this will open up bugs in places where cases
2 & 3 are required but its worth it. In fact they could even be qol
changes for all we know so who knows it might even benefit us but for
now we need to give a universal definition to this flag to bring some
consistency & that's what this PR does.

**Problem with deconstruct()**
This proc actually sends out a signal which is currently used by the
material container but could be used by other objects later on.

3e84c3e6da/code/game/objects/obj_defense.dm (L160)

So objects that override this proc should call its parent. Sadly that
isn't the case in many instances like such

3e84c3e6da/code/game/machinery/deployable.dm (L20-L23)

Instead of `return ..()` which would delete the object & send the signal
it deletes the object directly thus the signal never gets sent.

**Solution**
Make this proc non overridable. For objects to add their own custom
deconstruction behaviour a new proc has been introduced
`atom_deconstruct()` Subtypes should now override this proc to handle
object deconstruction.

If objects have certain important stuff inside them (like mobs in
machines for example) they want to drop by handling `NO_DECONSTRUCT`
flag in a more carefully customized way they can do this by overriding
`handle_deconstruct()` which by default delegates to
`atom_deconstruct()` if the `NO_DECONSTRUCT` flag is absent. This proc
will allow you to handle the flag in a more customized way if you ever
need to.

## Why It's Good For The Game
1) I'm goanna post the full comment from MrMelbert
https://github.com/tgstation/tgstation/pull/81656#discussion_r1503086862

> ...Ideally the .deconstruct call would handle NO_DECONSTRUCTION
handling as it wants, but there's a shocking lack of consistency around
NO_DECONSTRUCTION, where some objects treat it as "allow deconstruction,
but make it drop no parts" and others simply "disallow deconstruction at
all"

This PR now makes `NO_DECONSTRUCTION` handled by `deconstruct()` & gives
this flag the consistency it deserves. Not to mention as shown in case 4
there are objects that simply forgot to check for this flag. Now it
applies for those missing instances as well.

2) No more copying pasting the most overused code pattern in this code
base history `if(obj_flags & NO_DECONSTRUCTION)`. Just makes code
cleaner everywhere

3) All objects now send the `COMSIG_OBJ_DECONSTRUCT` signal on object
deconstruction which is now available for use should you need it

## Changelog
🆑
refactor: refactors how objects are deconstructed in relation to the
`NO_DECONSTRUCTION` flag. Certain objects & machinery may display
different tool interactions & behaviours when destroyed/deconstructed.
Report these changes if you feel like they are bugs
/🆑

---------

Co-authored-by: san7890 <the@san7890.com>
2024-04-04 18:55:51 -06:00

270 lines
8.4 KiB
Plaintext

#define PAPERS_PER_OVERLAY 8
#define PAPER_OVERLAY_PIXEL_SHIFT 2
/obj/item/paper_bin
name = "paper bin"
desc = "Contains all the paper you'll never need."
icon = 'icons/obj/service/bureaucracy.dmi'
icon_state = "paper_bin0"
inhand_icon_state = "sheet-metal"
lefthand_file = 'icons/mob/inhands/items/sheets_lefthand.dmi'
righthand_file = 'icons/mob/inhands/items/sheets_righthand.dmi'
throwforce = 0
w_class = WEIGHT_CLASS_NORMAL
throw_speed = 3
throw_range = 7
pressure_resistance = 8
var/papertype = /obj/item/paper
var/total_paper = 30
var/list/paper_stack = list()
var/obj/item/pen/bin_pen
///Overlay of the pen on top of the bin.
var/mutable_appearance/pen_overlay
///Name of icon that goes over the paper overlays.
var/bin_overlay_string = "paper_bin_overlay"
///Overlay that goes over the paper overlays.
var/mutable_appearance/bin_overlay
/obj/item/paper_bin/Initialize(mapload)
. = ..()
interaction_flags_item &= ~INTERACT_ITEM_ATTACK_HAND_PICKUP
AddElement(/datum/element/drag_pickup)
if(mapload)
var/obj/item/pen/pen = locate(/obj/item/pen) in loc
if(pen && !bin_pen)
pen.forceMove(src)
bin_pen = pen
update_appearance()
/obj/item/paper_bin/Destroy()
QDEL_LIST(paper_stack)
return ..()
/// Returns a fresh piece of paper
/obj/item/paper_bin/proc/generate_paper()
var/obj/item/paper/paper = new papertype
if(check_holidays(APRIL_FOOLS))
if(prob(30))
paper.add_raw_text("<font face=\"[CRAYON_FONT]\" color=\"red\"><b>HONK HONK HONK HONK HONK HONK HONK<br>HOOOOOOOOOOOOOOOOOOOOOONK<br>APRIL FOOLS</b></font>")
paper.AddElement(/datum/element/honkspam)
paper.update_appearance()
return paper
/obj/item/paper_bin/dump_contents(atom/droppoint, collapse = FALSE)
if(!droppoint)
droppoint = drop_location()
if(collapse)
visible_message(span_warning("The stack of paper collapses!"))
for(var/obj/item/paper/stacked_paper in paper_stack) //first, dump all of the paper that already exists
stacked_paper.forceMove(droppoint)
if(!stacked_paper.pixel_y)
stacked_paper.pixel_y = rand(-3,3)
if(!stacked_paper.pixel_x)
stacked_paper.pixel_x = rand(-3,3)
paper_stack -= stacked_paper
total_paper -= 1
for(var/i in 1 to total_paper) //second, generate new paper for the remainder
var/obj/item/paper/new_paper = generate_paper()
new_paper.forceMove(droppoint)
if(!new_paper.pixel_y)
new_paper.pixel_y = rand(-3,3)
if(!new_paper.pixel_x)
new_paper.pixel_x = rand(-3,3)
if(bin_pen)
var/obj/item/pen/pen = bin_pen
pen.forceMove(droppoint)
bin_pen = null
total_paper = 0
update_appearance()
/obj/item/paper_bin/fire_act(exposed_temperature, exposed_volume)
if(total_paper > 0)
total_paper = 0
QDEL_LIST(paper_stack)
update_appearance()
..()
/obj/item/paper_bin/attack_paw(mob/user, list/modifiers)
return attack_hand(user, modifiers)
//ATTACK HAND IGNORING PARENT RETURN VALUE
/obj/item/paper_bin/attack_hand(mob/user, list/modifiers)
if(isliving(user))
var/mob/living/living_mob = user
if(!(living_mob.mobility_flags & MOBILITY_PICKUP))
return
user.changeNext_move(CLICK_CD_RAPID)
if(at_overlay_limit())
dump_contents(drop_location(), TRUE)
return
if(bin_pen)
var/obj/item/pen/pen = bin_pen
pen.add_fingerprint(user)
pen.forceMove(user.loc)
user.put_in_hands(pen)
to_chat(user, span_notice("You take [pen] out of [src]."))
bin_pen = null
update_appearance()
else if(total_paper > 0)
var/obj/item/paper/top_paper = pop(paper_stack) || generate_paper()
total_paper -= 1
top_paper.add_fingerprint(user)
top_paper.forceMove(user.loc)
user.put_in_hands(top_paper)
to_chat(user, span_notice("You take [top_paper] out of [src]."))
update_appearance()
else
to_chat(user, span_warning("[src] is empty!"))
add_fingerprint(user)
return ..()
/obj/item/paper_bin/attackby(obj/item/I, mob/user, params)
if(at_overlay_limit())
dump_contents(drop_location(), TRUE)
return
if(istype(I, /obj/item/paper))
var/obj/item/paper/paper = I
if(!user.transferItemToLoc(paper, src, silent = FALSE))
return
to_chat(user, span_notice("You put [paper] in [src]."))
paper_stack += paper
total_paper += 1
update_appearance()
else if(istype(I, /obj/item/pen) && !bin_pen)
var/obj/item/pen/pen = I
if(!user.transferItemToLoc(pen, src, silent = FALSE))
return
to_chat(user, span_notice("You put [pen] in [src]."))
bin_pen = pen
update_appearance()
else
return ..()
/obj/item/paper_bin/proc/at_overlay_limit()
return overlays.len >= MAX_ATOM_OVERLAYS - 1
/obj/item/paper_bin/proc/remove_paper(amount = 1)
var/obj/item/paper/top_paper = pop(paper_stack)
if(top_paper)
qdel(top_paper)
total_paper -= amount
/obj/item/paper_bin/examine(mob/user)
. = ..()
if(total_paper)
. += "It contains [total_paper > 1 ? "[total_paper] papers" : "one paper"]."
else
. += "It doesn't contain anything."
/obj/item/paper_bin/update_icon_state()
if(total_paper < 1)
icon_state = "paper_bin0"
else
icon_state = "[initial(icon_state)]"
return ..()
/obj/item/paper_bin/update_overlays()
. = ..()
var/static/obj/item/paper/reference_paper
if (isnull(reference_paper))
reference_paper = new /obj/item/paper
if(bin_pen)
pen_overlay = mutable_appearance(bin_pen.icon, bin_pen.icon_state)
if(!bin_overlay)
bin_overlay = mutable_appearance(icon, bin_overlay_string)
if(total_paper > 0)
if(total_paper > length(paper_stack))
SET_PLANE_EXPLICIT(reference_paper, initial(reference_paper.plane), src)
reference_paper.update_appearance() // Ensures all our overlays are on the right plane
for(var/paper_number in 1 to total_paper)
if(paper_number != total_paper && paper_number % PAPERS_PER_OVERLAY != 0) //only top paper and every nth paper get overlays
continue
var/obj/item/paper/current_paper = paper_number > (total_paper - paper_stack.len) \
? paper_stack[paper_stack.len - (total_paper - paper_number + 1) + 1] \
: reference_paper
var/mutable_appearance/paper_overlay = mutable_appearance(current_paper.icon, current_paper.icon_state)
paper_overlay.color = current_paper.color
paper_overlay.pixel_z = paper_number/PAPERS_PER_OVERLAY - PAPER_OVERLAY_PIXEL_SHIFT //gives the illusion of stacking
. += paper_overlay
if(paper_number == total_paper) //this is our top paper
. += current_paper.overlays //add overlays only for top paper
if(istype(src, /obj/item/paper_bin/bundlenatural))
bin_overlay.pixel_z = paper_overlay.pixel_z //keeps binding centred on stack
if(bin_pen)
pen_overlay.pixel_z = paper_overlay.pixel_z //keeps pen on top of stack
. += bin_overlay
if(bin_pen)
. += pen_overlay
/obj/item/paper_bin/construction
name = "construction paper bin"
desc = "Contains all the paper you'll never need, IN COLOR!"
papertype = /obj/item/paper/construction
/obj/item/paper_bin/bundlenatural
name = "natural paper bundle"
desc = "A bundle of paper created using traditional methods."
icon_state = "paper_stack"
papertype = /obj/item/paper/natural
resistance_flags = FLAMMABLE
bin_overlay_string = "paper_bundle_overlay"
///Cable this bundle is held together with.
var/obj/item/stack/cable_coil/binding_cable
/obj/item/paper_bin/bundlenatural/Initialize(mapload)
binding_cable = new(src, 2)
binding_cable.set_cable_color(CABLE_COLOR_BROWN)
binding_cable.desc += " Non-natural."
return ..()
/obj/item/paper_bin/bundlenatural/dump_contents(atom/droppoint)
. = ..()
binding_cable.forceMove(droppoint)
binding_cable = null
qdel(src)
/obj/item/paper_bin/bundlenatural/update_overlays()
bin_overlay = mutable_appearance(icon, bin_overlay_string)
bin_overlay.color = binding_cable.color
return ..()
/obj/item/paper_bin/bundlenatural/attack_hand(mob/user, list/modifiers)
. = ..()
if(total_paper == 0)
deconstruct(FALSE)
/obj/item/paper_bin/bundlenatural/atom_deconstruct(disassembled)
dump_contents(drop_location())
return ..()
/obj/item/paper_bin/bundlenatural/fire_act(exposed_temperature, exposed_volume)
qdel(src)
/obj/item/paper_bin/bundlenatural/attackby(obj/item/W, mob/user)
if(istype(W, /obj/item/paper/carbon))
to_chat(user, span_warning("[W] won't fit into [src]."))
return
if(W.get_sharpness())
if(W.use_tool(src, user, 1 SECONDS))
to_chat(user, span_notice("You slice the cable from [src]."))
deconstruct(TRUE)
else
..()
/obj/item/paper_bin/carbon
name = "carbon paper bin"
desc = "Contains all the paper you'll ever need, in duplicate!"
icon_state = "paper_bin_carbon0"
papertype = /obj/item/paper/carbon
bin_overlay_string = "paper_bin_carbon_overlay"
#undef PAPERS_PER_OVERLAY
#undef PAPER_OVERLAY_PIXEL_SHIFT