Files
Bubberstation/code/datums/components/squashable.dm
Tim d6f79f4427 Refactor gib code to use bitflags and have documentation (#78754)
## About The Pull Request
This takes all the gib related procs:
- `gib()`
- `spawn_gibs()` 
- `spill_organs()`
- `spread_bodyparts()`

And adds heavy documentation that communicates what the procs are used
for and how the different bitflags affect them. The difference is
noticeable:

`gib(TRUE, FALSE, FALSE, null)` vs `gib(DROP_ORGANS|DROP_BODYPARTS)`

The code is now much more legible which is important considering it's
used in a lot of places!

Another robust change, is that we had several places in the code where
there were double negatives like so:

```
/mob/living/carbon/spill_organs(no_brain, no_organs, no_bodyparts)
	if(!no_bodyparts) // DOUBLE NEGATIVES ARE BAD M'KAY?!?
		// do stuff here
```

This is a mindfuck to untangle. I inverted a lot of these parts so we
don't lose our sanity.

Last thing that was changed was a big `if()` loop in the `spill_organ()`
proc. This was refactored to just be a simple `for` loop with `continue`
statements where we needed to skip enabled bitflags. It's now shorter
and cleaner than before.

The only slight gameplay change this affects is that gibbing a mob now
guarantees to drop all items unless the `DROP_ITEMS` bitflag is
deliberately omitted. Some places like admin gib self, we don't want
this to happen.

## Why It's Good For The Game
Gib code is very old. (~15 years) People kept adding more arguments to
the procs when it should have been a bitflag initially. By doing it this
way, there is more flexibility and readability when it comes to adding
new code in the future.

## Changelog
🆑
refactor: Refactor gib code to be more robust.
qol: Gibbing a mob will result in all items being dropped instead of
getting deleted. There are a few exceptions (like admin gib self) where
this will not take place.
/🆑
2023-10-06 13:12:22 +01:00

82 lines
3.1 KiB
Plaintext

///This component allows something to be when crossed, for example for cockroaches.
/datum/component/squashable
///Chance on crossed to be squashed
var/squash_chance = 50
///How much brute is applied when mob is squashed
var/squash_damage = 1
///Squash flags, for extra checks etcetera.
var/squash_flags = NONE
///Special callback to call on squash instead, for things like hauberoach
var/datum/callback/on_squash_callback
///signal list given to connect_loc
var/static/list/loc_connections = list(
COMSIG_ATOM_ENTERED = PROC_REF(on_entered),
)
/datum/component/squashable/Initialize(squash_chance, squash_damage, squash_flags, squash_callback)
. = ..()
if(!isliving(parent))
return COMPONENT_INCOMPATIBLE
if(squash_chance)
src.squash_chance = squash_chance
if(squash_damage)
src.squash_damage = squash_damage
if(squash_flags)
src.squash_flags = squash_flags
if(!src.on_squash_callback && squash_callback)
on_squash_callback = CALLBACK(parent, squash_callback)
AddComponent(/datum/component/connect_loc_behalf, parent, loc_connections)
/datum/component/squashable/Destroy(force, silent)
on_squash_callback = null
return ..()
///Handles the squashing of the mob
/datum/component/squashable/proc/on_entered(turf/source_turf, atom/movable/crossing_movable)
SIGNAL_HANDLER
if(parent == crossing_movable)
return
var/mob/living/parent_as_living = parent
if((squash_flags & SQUASHED_DONT_SQUASH_IN_CONTENTS) && !isturf(parent_as_living.loc))
return
if((squash_flags & SQUASHED_SHOULD_BE_DOWN) && parent_as_living.body_position != LYING_DOWN)
return
var/should_squash = ((squash_flags & SQUASHED_ALWAYS_IF_DEAD) && parent_as_living.stat == DEAD) || prob(squash_chance)
if(should_squash && on_squash_callback)
if(on_squash_callback.Invoke(parent_as_living, crossing_movable))
return //Everything worked, we're done!
if(isliving(crossing_movable))
var/mob/living/crossing_mob = crossing_movable
if(crossing_mob.mob_size > MOB_SIZE_SMALL && !(crossing_mob.movement_type & FLYING))
if(HAS_TRAIT(crossing_mob, TRAIT_PACIFISM))
crossing_mob.visible_message(span_notice("[crossing_mob] carefully steps over [parent_as_living]."), span_notice("You carefully step over [parent_as_living] to avoid hurting it."))
return
if(should_squash)
crossing_mob.visible_message(span_notice("[crossing_mob] squashed [parent_as_living]."), span_notice("You squashed [parent_as_living]."))
Squish(parent_as_living)
else
parent_as_living.visible_message(span_notice("[parent_as_living] avoids getting crushed."))
else if(isstructure(crossing_movable))
if(should_squash)
crossing_movable.visible_message(span_notice("[parent_as_living] is crushed under [crossing_movable]."))
Squish(parent_as_living)
else
parent_as_living.visible_message(span_notice("[parent_as_living] avoids getting crushed."))
/datum/component/squashable/proc/Squish(mob/living/target)
if(squash_flags & SQUASHED_SHOULD_BE_GIBBED)
target.gib(DROP_ALL_REMAINS)
else
target.adjustBruteLoss(squash_damage)
/datum/component/squashable/UnregisterFromParent()
. = ..()
qdel(GetComponent(/datum/component/connect_loc_behalf))