diff --git a/code/__DEFINES/mobs.dm b/code/__DEFINES/mobs.dm
index 68b853f212a..bbead3e117b 100644
--- a/code/__DEFINES/mobs.dm
+++ b/code/__DEFINES/mobs.dm
@@ -391,3 +391,10 @@
///Define for spawning megafauna instead of a mob for cave gen
#define SPAWN_MEGAFAUNA "bluh bluh huge boss"
+
+///Squash flags. For squashable element
+
+///Whether or not the squashing requires the squashed mob to be lying down
+#define SQUASHED_SHOULD_BE_DOWN (1<<0)
+///Whether or not to gib when the squashed mob is moved over
+#define SQUASHED_SHOULD_BE_GIBBED (1<<0)
diff --git a/code/datums/elements/squashable.dm b/code/datums/elements/squashable.dm
new file mode 100644
index 00000000000..e419730a6c6
--- /dev/null
+++ b/code/datums/elements/squashable.dm
@@ -0,0 +1,68 @@
+///This element allows something to be when crossed, for example for cockroaches.
+/datum/element/squashable
+ element_flags = ELEMENT_BESPOKE | ELEMENT_DETACH
+ id_arg_index = 2
+ ///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
+
+
+/datum/element/squashable/Attach(mob/living/target, squash_chance, squash_damage, squash_flags, squash_callback)
+ . = ..()
+ if(!istype(target))
+ return ELEMENT_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(target, squash_callback)
+
+ RegisterSignal(target, COMSIG_MOVABLE_CROSSED, .proc/OnCrossed)
+
+/datum/element/squashable/Detach(mob/living/target)
+ UnregisterSignal(target, COMSIG_MOVABLE_CROSSED)
+
+///Handles the squashing of the mob
+/datum/element/squashable/proc/OnCrossed(mob/living/target, atom/movable/crossing_movable)
+ SIGNAL_HANDLER
+
+
+ if(squash_flags & SQUASHED_SHOULD_BE_DOWN && target.body_position != LYING_DOWN)
+ return
+
+ var/should_squash = prob(squash_chance)
+
+ if(should_squash && on_squash_callback)
+ if(on_squash_callback.Invoke(target, 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("[crossing_mob] carefully steps over [target].", "You carefully step over [target] to avoid hurting it.")
+ return
+ if(should_squash)
+ crossing_mob.visible_message("[crossing_mob] squashed [target].", "You squashed [target].")
+ Squish(target)
+ else
+ target.visible_message("[target] avoids getting crushed.")
+ else if(isstructure(crossing_movable))
+ if(should_squash)
+ crossing_movable.visible_message("[target] is crushed under [crossing_movable].")
+ Squish(target)
+ else
+ target.visible_message("[target] avoids getting crushed.")
+
+/datum/element/squashable/proc/Squish(mob/living/target)
+ if(squash_flags & SQUASHED_SHOULD_BE_GIBBED)
+ target.gib()
+ else
+ target.adjustBruteLoss(squash_damage)
diff --git a/code/modules/mob/living/simple_animal/hostile/cockroach.dm b/code/modules/mob/living/simple_animal/hostile/cockroach.dm
index 205ca08811b..6c0e4ef7b75 100644
--- a/code/modules/mob/living/simple_animal/hostile/cockroach.dm
+++ b/code/modules/mob/living/simple_animal/hostile/cockroach.dm
@@ -36,6 +36,10 @@
/mob/living/simple_animal/hostile/cockroach/Initialize()
. = ..()
add_cell_sample()
+ make_squashable()
+
+/mob/living/simple_animal/hostile/cockroach/proc/make_squashable()
+ AddElement(/datum/element/squashable, squash_chance = 50, squash_damage = 1)
/mob/living/simple_animal/hostile/cockroach/add_cell_sample()
AddElement(/datum/element/swabable, CELL_LINE_TABLE_COCKROACH, CELL_VIRUS_TABLE_GENERIC_MOB, 1, 7)
@@ -68,26 +72,6 @@
return
..()
-/mob/living/simple_animal/hostile/cockroach/Crossed(atom/movable/AM)
- . = ..()
- if(isliving(AM))
- var/mob/living/A = AM
- if(A.mob_size > MOB_SIZE_SMALL && !(A.movement_type & FLYING))
- if(HAS_TRAIT(A, TRAIT_PACIFISM))
- A.visible_message("[A] carefully steps over [src].", "You carefully step over [src] to avoid hurting it.")
- return
- if(prob(squish_chance))
- A.visible_message("[A] squashed [src].", "You squashed [src].")
- adjustBruteLoss(1) //kills a normal cockroach
- else
- visible_message("[src] avoids getting crushed.")
- else if(isstructure(AM))
- if(prob(squish_chance))
- AM.visible_message("[src] is crushed under [AM].")
- adjustBruteLoss(1)
- else
- visible_message("[src] avoids getting crushed.")
-
/mob/living/simple_animal/hostile/cockroach/ex_act() //Explosions are a terrible way to handle a cockroach.
return
@@ -110,16 +94,15 @@
. = ..()
AddComponent(/datum/component/caltrop, 10, 15, 100, (CALTROP_BYPASS_SHOES | CALTROP_SILENT))
-/mob/living/simple_animal/hostile/cockroach/hauberoach/Crossed(atom/movable/AM)
- var/mob/living/A = AM
- if(istype(A) && A.mob_size > MOB_SIZE_SMALL && !(A.movement_type & FLYING))
- if(!HAS_TRAIT(A, TRAIT_PIERCEIMMUNE))
- A.visible_message("[A] steps onto [src]'s spike!", "You step onto [src]'s spike!")
- else if(HAS_TRAIT(A, TRAIT_PACIFISM))
- A.visible_message("[A] carefully steps over [src].", "You carefully step over [src] to avoid hurting it.")
- else if(HAS_TRAIT(A, TRAIT_PIERCEIMMUNE))
- A.visible_message("[A] steps onto [src]'s spike, but is unfazed!", "You step onto [src]'s spike, but you're unaffected!")
- else
- A.visible_message("[A] squashes [src], not even noticing its spike.", "You squashed [src], not even noticing its spike.")
- adjustBruteLoss(1) //kills a normal cockroach
- ..()
+/mob/living/simple_animal/hostile/cockroach/hauberoach/make_squashable()
+ AddElement(/datum/element/squashable, squash_chance = 100, squash_damage = 1, squash_callback = /mob/living/simple_animal/hostile/cockroach/hauberoach/.proc/on_squish)
+
+///Proc used to override the squashing behavior of the normal cockroach.
+/mob/living/simple_animal/hostile/cockroach/hauberoach/proc/on_squish(mob/living/cockroach, mob/living/living_target)
+ if(!istype(living_target))
+ return FALSE //We failed to run the invoke. Might be because we're a structure. Let the squashable element handle it then!
+ if(!HAS_TRAIT(living_target, TRAIT_PIERCEIMMUNE))
+ living_target.visible_message("[living_target] steps onto [cockroach]'s spike!", "You step onto [cockroach]'s spike!")
+ return TRUE
+ living_target.visible_message("[living_target] squashes [cockroach], not even noticing its spike.", "You squashed [cockroach], not even noticing its spike.")
+ return FALSE
diff --git a/tgstation.dme b/tgstation.dme
index 8c45869f427..63e3ffaa0fd 100644
--- a/tgstation.dme
+++ b/tgstation.dme
@@ -628,6 +628,7 @@
#include "code\datums\elements\ridable.dm"
#include "code\datums\elements\selfknockback.dm"
#include "code\datums\elements\snail_crawl.dm"
+#include "code\datums\elements\squashable.dm"
#include "code\datums\elements\squish.dm"
#include "code\datums\elements\swabbable.dm"
#include "code\datums\elements\tool_flash.dm"