Merge pull request #15999 from SandPoot/do_after
Implements timed_action_flags for do_after-like procs
This commit is contained in:
+79
-240
@@ -1,168 +1,6 @@
|
||||
/**
|
||||
* Higher overhead "advanced" version of do_after.
|
||||
* @params
|
||||
* - atom/user is the atom doing the action or the "physical" user
|
||||
* - delay is time in deciseconds
|
||||
* - atom/target is the atom the action is being done to, defaults to user
|
||||
* - do_after_flags see __DEFINES/flags/do_after.dm for details.
|
||||
* - datum/callback/extra_checks - Every time this ticks, extra_checks() is invoked with (user, delay, target, time_left, do_after_flags, required_mobility_flags, required_combat_flags, mob_redirect, stage, initially_held_item, tool, passed_in).
|
||||
* Stage can be DO_AFTER_STARTING, DO_AFTER_PROGRESSING, DO_AFTER_FINISHING
|
||||
* If it returns DO_AFTER_STOP, this breaks.
|
||||
* If it returns nothing, all other checks are done.
|
||||
* If it returns DO_AFTER_PROCEED, all other checks are ignored.
|
||||
* passed_in is a list[PROGRESS_MULTIPLIER], for modification.
|
||||
* - required_mobility_flags is checked with CHECK_ALL_MOBILITY. Will immediately fail if the user isn't a mob.
|
||||
* - requried_combat_flags is checked with CHECK_MULTIPLE_BITFIELDS. Will immediately fail if the user isn't a mob.
|
||||
* - mob/living/mob_redirect - advanced option: If this is specified, movement and mobility/combat flag checks will use this instead of user. Progressbars will also go to this.
|
||||
* - obj/item/tool - The tool we're using. See do_after flags for details.
|
||||
*/
|
||||
#define INVOKE_CALLBACK cb_return = extra_checks?.Invoke(user, delay, target, timeleft, do_after_flags, required_mobility_flags, required_combat_flags, mob_redirect, stage, initially_held_item, tool, passed_in)
|
||||
#define CHECK_FLAG_FAILURE ((required_mobility_flags || required_combat_flags) && (!living_user || (required_mobility_flags && !CHECK_ALL_MOBILITY(living_user, required_mobility_flags)) || (required_combat_flags && !CHECK_MULTIPLE_BITFIELDS(living_user.combat_flags, required_combat_flags))))
|
||||
#define TIMELEFT (timeleft)
|
||||
/proc/do_after_advanced(atom/user, delay, atom/target, do_after_flags, datum/callback/extra_checks, required_mobility_flags, required_combat_flags, mob/living/mob_redirect, obj/item/tool)
|
||||
// CHECK AND SET VARIABLES
|
||||
if(!user)
|
||||
return FALSE
|
||||
if(!target)
|
||||
target = user
|
||||
if((user.loc == null) || (target.loc == null))
|
||||
return FALSE
|
||||
var/mob/living/living_user = mob_redirect
|
||||
if(!living_user && isliving(user))
|
||||
living_user = user
|
||||
var/stage = DO_AFTER_STARTING
|
||||
var/startlocuser = user.loc
|
||||
var/startloctarget = target.loc
|
||||
var/turf/userturf = get_turf(user)
|
||||
var/turf/targetturf = get_turf(target)
|
||||
if(!userturf || !targetturf)
|
||||
return FALSE
|
||||
if((do_after_flags & DO_AFTER_REQUIRES_USER_ON_TURF) && !isturf(user.loc))
|
||||
return FALSE
|
||||
if(!(do_after_flags & DO_AFTER_NO_COEFFICIENT) && living_user)
|
||||
delay *= living_user.cached_multiplicative_actions_slowdown
|
||||
var/timeleft = delay
|
||||
var/obj/item/initially_held_item = mob_redirect?.get_active_held_item()
|
||||
var/atom/movable/AM_user = ismovable(user) && user
|
||||
var/drifting = AM_user?.Process_Spacemove(NONE) && AM_user.inertia_dir
|
||||
var/initial_dx = targetturf.x - userturf.x
|
||||
var/initial_dy = targetturf.y - userturf.y
|
||||
var/dx = initial_dx
|
||||
var/dy = initial_dy
|
||||
// DO OUR STARTING CHECKS
|
||||
var/cb_return
|
||||
var/list/passed_in = list(1)
|
||||
INVOKE_CALLBACK
|
||||
if(cb_return == DO_AFTER_STOP)
|
||||
return FALSE
|
||||
else if(cb_return != DO_AFTER_PROCEED)
|
||||
if(CHECK_FLAG_FAILURE)
|
||||
return FALSE
|
||||
// SETUP LOOP
|
||||
var/datum/progressbar/progbar
|
||||
if(living_user)
|
||||
if(!(do_after_flags & DO_AFTER_NO_PROGRESSBAR))
|
||||
progbar = new(living_user, delay, target)
|
||||
// MAIN LOOP
|
||||
. = TRUE
|
||||
if(!delay)
|
||||
return
|
||||
var/obj/item/held
|
||||
var/locchanged
|
||||
var/ctu
|
||||
var/ctt
|
||||
var/tick_time = world.time
|
||||
while(timeleft > 0)
|
||||
stoplag(1)
|
||||
var/timepassed = world.time - tick_time
|
||||
tick_time = world.time
|
||||
progbar?.update(TIMELEFT)
|
||||
if(QDELETED(user) || QDELETED(target) || (user.loc == null) || (target.loc == null))
|
||||
. = FALSE
|
||||
break
|
||||
INVOKE_CALLBACK
|
||||
timeleft -= timepassed * passed_in[1]
|
||||
if(cb_return == DO_AFTER_STOP)
|
||||
. = FALSE
|
||||
break
|
||||
else if(cb_return == DO_AFTER_PROCEED)
|
||||
continue
|
||||
// otherwise, go through our normal checks.
|
||||
if(((do_after_flags & DO_AFTER_DISALLOW_MOVING_ABSOLUTE_USER) && (user.loc != startlocuser)) || ((do_after_flags & DO_AFTER_DISALLOW_MOVING_ABSOLUTE_TARGET) && (target.loc != startloctarget)))
|
||||
. = FALSE
|
||||
break
|
||||
else if(do_after_flags & DO_AFTER_DISALLOW_MOVING_RELATIVE)
|
||||
ctu = get_turf(user)
|
||||
ctt = get_turf(target)
|
||||
locchanged = (userturf != ctu) || (targetturf != ctt)
|
||||
userturf = ctu
|
||||
targetturf = ctt
|
||||
dx = targetturf.x - userturf.x
|
||||
dy = targetturf.y - userturf.y
|
||||
if((dx != initial_dx) || (dy != initial_dy))
|
||||
. = FALSE
|
||||
break
|
||||
if(locchanged && !drifting && !(do_after_flags & DO_AFTER_ALLOW_NONSPACEDRIFT_RELATIVITY))
|
||||
. = FALSE
|
||||
break
|
||||
if(!AM_user.inertia_dir)
|
||||
drifting = FALSE
|
||||
if((do_after_flags & DO_AFTER_REQUIRES_USER_ON_TURF) && !isturf(user.loc))
|
||||
return FALSE
|
||||
if(CHECK_FLAG_FAILURE)
|
||||
. = FALSE
|
||||
break
|
||||
held = living_user?.get_active_held_item()
|
||||
if((do_after_flags & DO_AFTER_DISALLOW_ACTIVE_ITEM_CHANGE) && (held != (tool || initially_held_item)))
|
||||
. = FALSE
|
||||
break
|
||||
if((do_after_flags & DO_AFTER_REQUIRE_FREE_HAND_OR_TOOL) && (!living_user?.is_holding(tool) && !length(living_user?.get_empty_held_indexes())))
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
// CLEANUP
|
||||
progbar.end_progress()
|
||||
// If we failed, just return.
|
||||
if(!.)
|
||||
return FALSE
|
||||
// DO FINISHING CHECKS
|
||||
if(QDELETED(user) || QDELETED(target))
|
||||
return FALSE
|
||||
INVOKE_CALLBACK
|
||||
if(cb_return == DO_AFTER_STOP)
|
||||
return FALSE
|
||||
else if(cb_return == DO_AFTER_PROCEED)
|
||||
return TRUE
|
||||
if(CHECK_FLAG_FAILURE)
|
||||
return FALSE
|
||||
if(((do_after_flags & DO_AFTER_DISALLOW_MOVING_ABSOLUTE_USER) && (user.loc != startlocuser)) || ((do_after_flags & DO_AFTER_DISALLOW_MOVING_ABSOLUTE_TARGET) && (target.loc != startloctarget)))
|
||||
return FALSE
|
||||
else if(do_after_flags & DO_AFTER_DISALLOW_MOVING_RELATIVE)
|
||||
ctu = get_turf(user)
|
||||
ctt = get_turf(target)
|
||||
locchanged = (userturf != ctu) || (targetturf != ctt)
|
||||
userturf = ctu
|
||||
targetturf = ctt
|
||||
dx = targetturf.x - userturf.x
|
||||
dy = targetturf.y - userturf.y
|
||||
if((dx != initial_dx) || (dy != initial_dy))
|
||||
return FALSE
|
||||
if(locchanged && !drifting && !(do_after_flags & DO_AFTER_ALLOW_NONSPACEDRIFT_RELATIVITY))
|
||||
return FALSE
|
||||
if((do_after_flags & DO_AFTER_REQUIRES_USER_ON_TURF) && !isturf(user.loc))
|
||||
return FALSE
|
||||
held = living_user?.get_active_held_item()
|
||||
if((do_after_flags & DO_AFTER_DISALLOW_ACTIVE_ITEM_CHANGE) && (held != (tool || initially_held_item)))
|
||||
return FALSE
|
||||
if((do_after_flags & DO_AFTER_REQUIRE_FREE_HAND_OR_TOOL) && (!living_user?.is_holding(tool) && !length(living_user?.get_empty_held_indexes())))
|
||||
return FALSE
|
||||
|
||||
#undef INVOKE_CALLBACK
|
||||
#undef CHECK_FLAG_FAILURE
|
||||
|
||||
/proc/do_mob(mob/user , mob/target, time = 30, uninterruptible = 0, progress = 1, datum/callback/extra_checks = null, ignorehelditem = FALSE, resume_time = 0 SECONDS)
|
||||
/proc/do_mob(mob/user, mob/target, time = 3 SECONDS, timed_action_flags = NONE, progress = TRUE, datum/callback/extra_checks, resume_time = 0 SECONDS)
|
||||
if(!user || !target)
|
||||
return 0
|
||||
return FALSE
|
||||
var/user_loc = user.loc
|
||||
|
||||
var/drifting = 0
|
||||
@@ -181,30 +19,26 @@
|
||||
|
||||
var/endtime = world.time+time
|
||||
var/starttime = world.time
|
||||
. = 1
|
||||
. = TRUE
|
||||
while (world.time + resume_time < endtime)
|
||||
stoplag(1)
|
||||
if (progress)
|
||||
progbar.update(world.time - starttime + resume_time)
|
||||
if(QDELETED(user) || QDELETED(target))
|
||||
. = 0
|
||||
break
|
||||
if(uninterruptible)
|
||||
continue
|
||||
if(!(target in user.do_afters))
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
if(!(target in user.do_afters))
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
if(drifting && !user.inertia_dir)
|
||||
drifting = 0
|
||||
drifting = FALSE
|
||||
user_loc = user.loc
|
||||
|
||||
if((!drifting && user.loc != user_loc) || target.loc != target_loc || (!ignorehelditem && user.get_active_held_item() != holding) || user.incapacitated() || user.lying || (extra_checks && !extra_checks.Invoke()))
|
||||
. = 0
|
||||
if(
|
||||
QDELETED(user) || QDELETED(target) \
|
||||
|| (!(timed_action_flags & IGNORE_TARGET_IN_DOAFTERS) && !(target in user.do_afters)) \
|
||||
|| (!(timed_action_flags & IGNORE_USER_LOC_CHANGE) && !drifting && user.loc != user_loc) \
|
||||
|| (!(timed_action_flags & IGNORE_TARGET_LOC_CHANGE) && target.loc != target_loc) \
|
||||
|| (!(timed_action_flags & IGNORE_HELD_ITEM) && user.get_active_held_item() != holding) \
|
||||
|| (!(timed_action_flags & IGNORE_INCAPACITATED) && user.incapacitated()) \
|
||||
|| (extra_checks && !extra_checks.Invoke()) \
|
||||
)
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
if(!QDELETED(progbar))
|
||||
@@ -228,29 +62,25 @@
|
||||
checked_health["health"] = health
|
||||
return ..()
|
||||
|
||||
/proc/do_after(mob/user, var/delay, needhand = 1, atom/target = null, progress = 1, datum/callback/extra_checks = null, required_mobility_flags = (MOBILITY_USE|MOBILITY_MOVE), resume_time = 0 SECONDS)
|
||||
/proc/do_after(mob/user, delay, atom/target, timed_action_flags = NONE, progress = TRUE, datum/callback/extra_checks, resume_time = 0 SECONDS)
|
||||
if(!user)
|
||||
return 0
|
||||
var/atom/Tloc = null
|
||||
return FALSE
|
||||
var/atom/target_loc = null
|
||||
if(target && !isturf(target))
|
||||
Tloc = target.loc
|
||||
target_loc = target.loc
|
||||
|
||||
if(target)
|
||||
LAZYADD(user.do_afters, target)
|
||||
LAZYADD(target.targeted_by, user)
|
||||
|
||||
var/atom/Uloc = user.loc
|
||||
var/atom/user_loc = user.loc
|
||||
|
||||
var/drifting = 0
|
||||
var/drifting = FALSE
|
||||
if(!user.Process_Spacemove(0) && user.inertia_dir)
|
||||
drifting = 1
|
||||
drifting = TRUE
|
||||
|
||||
var/holding = user.get_active_held_item()
|
||||
|
||||
var/holdingnull = 1 //User's hand started out empty, check for an empty hand
|
||||
if(holding)
|
||||
holdingnull = 0 //Users hand started holding something, check to see if it's still holding that
|
||||
|
||||
delay *= user.cached_multiplicative_actions_slowdown
|
||||
|
||||
var/datum/progressbar/progbar
|
||||
@@ -259,44 +89,39 @@
|
||||
|
||||
var/endtime = world.time + delay
|
||||
var/starttime = world.time
|
||||
. = 1
|
||||
var/mob/living/L = isliving(user) && user //evals to last thing eval'd
|
||||
. = TRUE
|
||||
while (world.time + resume_time < endtime)
|
||||
stoplag(1)
|
||||
if (progress)
|
||||
progbar.update(world.time - starttime + resume_time)
|
||||
|
||||
if(drifting && !user.inertia_dir)
|
||||
drifting = 0
|
||||
Uloc = user.loc
|
||||
drifting = FALSE
|
||||
user_loc = user.loc
|
||||
|
||||
if(L && !CHECK_ALL_MOBILITY(L, required_mobility_flags))
|
||||
. = 0
|
||||
break
|
||||
|
||||
if(QDELETED(user) || user.stat || (!drifting && user.loc != Uloc) || (extra_checks && !extra_checks.Invoke()))
|
||||
. = 0
|
||||
break
|
||||
|
||||
if(!QDELETED(Tloc) && (QDELETED(target) || Tloc != target.loc))
|
||||
if((Uloc != Tloc || Tloc != user) && !drifting)
|
||||
. = 0
|
||||
break
|
||||
|
||||
if(target && !(target in user.do_afters))
|
||||
if(
|
||||
QDELETED(user) \
|
||||
|| (!(timed_action_flags & IGNORE_USER_LOC_CHANGE) && !drifting && user.loc != user_loc) \
|
||||
|| (!(timed_action_flags & IGNORE_HELD_ITEM) && user.get_active_held_item() != holding) \
|
||||
|| (!(timed_action_flags & IGNORE_INCAPACITATED) && user.incapacitated()) \
|
||||
|| (extra_checks && !extra_checks.Invoke()) \
|
||||
)
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
if(needhand)
|
||||
//This might seem like an odd check, but you can still need a hand even when it's empty
|
||||
//i.e the hand is used to pull some item/tool out of the construction
|
||||
if(!holdingnull)
|
||||
if(!holding)
|
||||
. = 0
|
||||
break
|
||||
if(user.get_active_held_item() != holding)
|
||||
. = 0
|
||||
break
|
||||
if(
|
||||
!(timed_action_flags & IGNORE_TARGET_LOC_CHANGE) \
|
||||
&& !drifting \
|
||||
&& !QDELETED(target_loc) \
|
||||
&& (QDELETED(target) || target_loc != target.loc) \
|
||||
&& ((user_loc != target_loc || target_loc != user)) \
|
||||
)
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
if(target && !(timed_action_flags & IGNORE_TARGET_IN_DOAFTERS) && !(target in user.do_afters))
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
if(!QDELETED(progbar))
|
||||
progbar.end_progress()
|
||||
@@ -308,16 +133,16 @@
|
||||
LAZYREMOVE(user.do_afters, target)
|
||||
LAZYREMOVE(target.targeted_by, user)
|
||||
|
||||
/proc/do_after_mob(mob/user, var/list/targets, time = 30, uninterruptible = 0, progress = 1, datum/callback/extra_checks)
|
||||
/proc/do_after_mob(mob/user, list/targets, time = 3 SECONDS, timed_action_flags = NONE, progress = TRUE, datum/callback/extra_checks)
|
||||
if(!user || !targets)
|
||||
return 0
|
||||
return FALSE
|
||||
if(!islist(targets))
|
||||
targets = list(targets)
|
||||
var/user_loc = user.loc
|
||||
|
||||
var/drifting = 0
|
||||
var/drifting = FALSE
|
||||
if(!user.Process_Spacemove(0) && user.inertia_dir)
|
||||
drifting = 1
|
||||
drifting = TRUE
|
||||
|
||||
var/list/originalloc = list()
|
||||
for(var/atom/target in targets)
|
||||
@@ -335,25 +160,39 @@
|
||||
var/endtime = world.time + time
|
||||
var/starttime = world.time
|
||||
. = 1
|
||||
mainloop:
|
||||
while(world.time < endtime)
|
||||
stoplag(1)
|
||||
if(progress)
|
||||
progbar.update(world.time - starttime)
|
||||
if(QDELETED(user) || !targets)
|
||||
. = 0
|
||||
while(world.time < endtime)
|
||||
stoplag(1)
|
||||
|
||||
if(!QDELETED(progbar))
|
||||
progbar.update(world.time - starttime)
|
||||
if(QDELETED(user) || !length(targets))
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
if(drifting && !user.inertia_dir)
|
||||
drifting = FALSE
|
||||
user_loc = user.loc
|
||||
|
||||
if(
|
||||
!((timed_action_flags & IGNORE_USER_LOC_CHANGE) && !drifting && user_loc != user.loc) \
|
||||
|| (!(timed_action_flags & IGNORE_HELD_ITEM) && user.get_active_held_item() != holding) \
|
||||
|| (!(timed_action_flags & IGNORE_INCAPACITATED) && user.incapacitated()) \
|
||||
|| (extra_checks && !extra_checks.Invoke()) \
|
||||
)
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
for(var/t in targets)
|
||||
var/atom/target = t
|
||||
if(
|
||||
(QDELETED(target)) \
|
||||
|| (!(timed_action_flags & IGNORE_TARGET_LOC_CHANGE) && originalloc[target] != target.loc) \
|
||||
)
|
||||
. = FALSE
|
||||
break
|
||||
if(uninterruptible)
|
||||
continue
|
||||
|
||||
if(drifting && !user.inertia_dir)
|
||||
drifting = 0
|
||||
user_loc = user.loc
|
||||
|
||||
for(var/atom/target in targets)
|
||||
if((!drifting && user_loc != user.loc) || QDELETED(target) || originalloc[target] != target.loc || user.get_active_held_item() != holding || user.incapacitated() || user.lying || (extra_checks && !extra_checks.Invoke()))
|
||||
. = 0
|
||||
break mainloop
|
||||
if(!.) // In case the for-loop found a reason to break out of the while.
|
||||
break
|
||||
|
||||
if(!QDELETED(progbar))
|
||||
progbar.end_progress()
|
||||
|
||||
@@ -1340,7 +1340,7 @@ GLOBAL_DATUM_INIT(dview_mob, /mob/dview, new)
|
||||
return temp
|
||||
|
||||
//same as do_mob except for movables and it allows both to drift and doesn't draw progressbar
|
||||
/proc/do_atom(atom/movable/user , atom/movable/target, time = 30, uninterruptible = 0,datum/callback/extra_checks = null)
|
||||
/proc/do_atom(atom/movable/user, atom/movable/target, time = 3 SECONDS, timed_action_flags = NONE, datum/callback/extra_checks)
|
||||
if(!user || !target)
|
||||
return TRUE
|
||||
var/user_loc = user.loc
|
||||
@@ -1359,11 +1359,10 @@ GLOBAL_DATUM_INIT(dview_mob, /mob/dview, new)
|
||||
. = TRUE
|
||||
while (world.time < endtime)
|
||||
stoplag(1)
|
||||
|
||||
if(QDELETED(user) || QDELETED(target))
|
||||
. = 0
|
||||
. = FALSE
|
||||
break
|
||||
if(uninterruptible)
|
||||
continue
|
||||
|
||||
if(drifting && !user.inertia_dir)
|
||||
drifting = FALSE
|
||||
@@ -1373,7 +1372,11 @@ GLOBAL_DATUM_INIT(dview_mob, /mob/dview, new)
|
||||
target_drifting = FALSE
|
||||
target_loc = target.loc
|
||||
|
||||
if((!drifting && user.loc != user_loc) || (!target_drifting && target.loc != target_loc) || (extra_checks && !extra_checks.Invoke()))
|
||||
if(
|
||||
(!(timed_action_flags & IGNORE_USER_LOC_CHANGE) && !drifting && user.loc != user_loc) \
|
||||
|| (!(timed_action_flags& IGNORE_TARGET_LOC_CHANGE) && !target_drifting && target.loc != target_loc) \
|
||||
|| (extra_checks && !extra_checks.Invoke()) \
|
||||
)
|
||||
. = FALSE
|
||||
break
|
||||
|
||||
|
||||
Reference in New Issue
Block a user