diff --git a/code/game/objects/buckling.dm b/code/game/objects/buckling.dm
index 40a8b9bb066..c3c6ac8e274 100644
--- a/code/game/objects/buckling.dm
+++ b/code/game/objects/buckling.dm
@@ -66,14 +66,9 @@
if(!buckled_mobs)
buckled_mobs = list()
- if(!istype(M))
+ if(!is_buckle_possible(M, force, check_loc))
return FALSE
- if(check_loc && M.loc != loc)
- return FALSE
-
- if((!can_buckle && !force) || M.buckled || (buckled_mobs.len >= max_buckled_mobs) || (buckle_requires_restraints && !M.restrained()) || M == src)
- return FALSE
M.buckling = src
if(!M.can_buckle() && !force)
if(M == usr)
@@ -138,18 +133,94 @@
//same but for unbuckle
/atom/movable/proc/post_unbuckle_mob(mob/living/M)
+/**
+ * Simple helper proc that runs a suite of checks to test whether it is possible or not to buckle the target mob to src.
+ *
+ * Returns FALSE if any conditions that should prevent buckling are satisfied. Returns TRUE otherwise.
+ * Arguments:
+ * * target - Target mob to check against buckling to src.
+ * * force - Whether or not the buckle should be forced. If TRUE, ignores src's can_buckle var.
+ * * check_loc - Whether to do a proximity check or not. The proximity check looks for target.loc == src.loc.
+ */
+/atom/movable/proc/is_buckle_possible(mob/living/target, force = FALSE, check_loc = TRUE)
+ // Make sure target is mob/living
+ if(!istype(target))
+ return FALSE
+
+ // No bucking you to yourself.
+ if(target == src)
+ return FALSE
+
+ // Check if this atom can have things buckled to it.
+ if(!can_buckle && !force)
+ return FALSE
+
+ // If we're checking the loc, make sure the target is on the thing we're bucking them to.
+ if(check_loc && target.loc != loc)
+ return FALSE
+
+ // Make sure the target isn't already buckled to something.
+ if(target.buckled)
+ return FALSE
+
+ // Make sure this atom can still have more things buckled to it.
+ if(LAZYLEN(buckled_mobs) >= max_buckled_mobs)
+ return FALSE
+
+ // If the buckle requires restraints, make sure the target is actually restrained while ignoring grab restraint.
+ if(buckle_requires_restraints && !target.restrained(TRUE))
+ return FALSE
+
+ return TRUE
+
+/**
+ * Simple helper proc that runs a suite of checks to test whether it is possible or not for user to buckle target mob to src.
+ *
+ * Returns FALSE if any conditions that should prevent buckling are satisfied. Returns TRUE otherwise.
+ * Arguments:
+ * * target - Target mob to check against buckling to src.
+ * * user - The mob who is attempting to buckle the target to src.
+ * * check_loc - Whether to do a proximity check or not when calling is_buckle_possible().
+ */
+/atom/movable/proc/is_user_buckle_possible(mob/living/target, mob/user, check_loc = TRUE)
+ // Standard adjacency and other checks.
+ if(!Adjacent(user) || !Adjacent(target) || !isturf(user.loc) || user.incapacitated() || target.anchored)
+ return FALSE
+
+ // In buckling even possible in the first place?
+ if(!is_buckle_possible(target, FALSE, check_loc))
+ return FALSE
+
+ // If the person attempting to buckle is stood on this atom's turf and they're not buckling themselves,
+ // buckling shouldn't be possible as they're blocking it.
+ if((target != user) && (get_turf(user) == get_turf(src)))
+ to_chat(target, "You are unable to buckle [target] to [src] while it is blocked!")
+ return FALSE
+
+ return TRUE
+
//Wrapper procs that handle sanity and user feedback
/atom/movable/proc/user_buckle_mob(mob/living/M, mob/user, check_loc = TRUE)
- if(!Adjacent(user) || !Adjacent(M) || !isturf(user.loc) || user.incapacitated() || M.anchored)
+ // Is buckling even possible? Do a full suite of checks.
+ if(!is_user_buckle_possible(M, user, check_loc))
return FALSE
add_fingerprint(user)
- if (M != user)
+
+ // If the mob we're attempting to buckle is not stood on this atom's turf and it isn't the user buckling themselves,
+ // we'll try it with a 2 second do_after delay.
+ if(M != user && (get_turf(M) != get_turf(src)))
M.visible_message("[user] starts buckling [M] to [src]!",\
- "[user] starts buckling you to [src]!",\
- "You hear metal clanking.")
- if(!do_after(user, 3 SECONDS, TRUE, M))
+ "[user] starts buckling you to [src]!",\
+ "You hear metal clanking.")
+ if(!do_after(user, 2 SECONDS, TRUE, M))
return FALSE
+
+ // Sanity check before we attempt to buckle. Is everything still in a kosher state for buckling after the 3 seconds have elapsed?
+ // Covers situations where, for example, the chair was moved or there's some other issue.
+ if(!is_user_buckle_possible(M, user, check_loc))
+ return FALSE
+
. = buckle_mob(M, check_loc = check_loc)
if(.)
if(M == user)
@@ -158,7 +229,8 @@
"You hear metal clanking.")
else
M.visible_message("[user] buckles [M] to [src]!",\
- "[user] buckles you to [src]!")
+ "[user] buckles you to [src]!",\
+ "You hear metal clanking.")
/atom/movable/proc/user_unbuckle_mob(mob/living/buckled_mob, mob/user)
var/mob/living/M = unbuckle_mob(buckled_mob)