From acaa918403bd6941dd554fe94524190106d37f49 Mon Sep 17 00:00:00 2001 From: Marco Cawthorne Date: Thu, 25 Aug 2022 18:51:51 -0700 Subject: [PATCH] NSTimer: initial implementation of an object timer. Handles temporary (self garbgage collecting) timers as well. --- src/shared/NSTimer.h | 31 +++++++++++++ src/shared/NSTimer.qc | 102 +++++++++++++++++++++++++++++++++++++++++ src/shared/defs.h | 1 + src/shared/include.src | 1 + 4 files changed, 135 insertions(+) create mode 100644 src/shared/NSTimer.h create mode 100644 src/shared/NSTimer.qc diff --git a/src/shared/NSTimer.h b/src/shared/NSTimer.h new file mode 100644 index 00000000..29a0de53 --- /dev/null +++ b/src/shared/NSTimer.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Vera Visions LLC. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +class +NSTimer:NSEntity +{ + entity m_eReceiver; + virtual void(void) m_vFunc = 0; + float m_flTime; + bool m_bRepeats; + + void(void) NSTimer; + + virtual void(void) _TimerThink; + virtual void(void) _TempTimerThink; + static NSTimer(entity, void(), float, bool) ScheduleTimer; + static NSTimer(entity, void(), float, bool) TemporaryTimer; +}; \ No newline at end of file diff --git a/src/shared/NSTimer.qc b/src/shared/NSTimer.qc new file mode 100644 index 00000000..81381d91 --- /dev/null +++ b/src/shared/NSTimer.qc @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2022 Vera Visions LLC. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +void +NSTimer::NSTimer(void) +{ + m_eReceiver = __NULL__; + m_vFunc = __NULL__; + m_flTime = 0.0f; + m_bRepeats = false; +} + +/* wrapper to execute our trigger */ +static void +_NSTimerWrapper(entity receiver, void() func) +{ + entity oldself = self; + self = receiver; + func(); + self = oldself; +} + +void +NSTimer::_TimerThink(void) +{ + _NSTimerWrapper(m_eReceiver, m_vFunc); + + if (m_bRepeats == true) + nextthink = time + m_flTime; +} + +void +NSTimer::_TempTimerThink(void) +{ + _NSTimerWrapper(m_eReceiver, m_vFunc); + + if (m_bRepeats == true) + nextthink = time + m_flTime; + else { + think = Destroy; + nextthink = time; + } +} + +NSTimer +NSTimer::ScheduleTimer(entity receiver, void() call, float interval, bool repeats) +{ + NSTimer ret; + + if (this) + ret = this; + else { + ret = spawn(NSTimer); + this = ret; + } + + ret.m_eReceiver = receiver; + ret.m_flTime = interval; + ret.m_vFunc = call; + ret.m_bRepeats = repeats; + + /* time to start running */ + ret.think = NSTimer::_TimerThink; + ret.nextthink = time + interval; + return ret; +} + +NSTimer +NSTimer::TemporaryTimer(entity receiver, void() call, float interval, bool repeats) +{ + NSTimer ret; + + if (this) + ret = this; + else { + ret = spawn(NSTimer); + this = ret; + } + + ret.m_eReceiver = receiver; + ret.m_flTime = interval; + ret.m_vFunc = call; + ret.m_bRepeats = repeats; + + /* time to start running */ + ret.think = NSTimer::_TempTimerThink; + ret.nextthink = time + interval; + return ret; +} \ No newline at end of file diff --git a/src/shared/defs.h b/src/shared/defs.h index acc6a4e8..fb9e3b6e 100644 --- a/src/shared/defs.h +++ b/src/shared/defs.h @@ -49,6 +49,7 @@ string __fullspawndata; #include "NSIO.h" #include "NSTrigger.h" #include "NSEntity.h" +#include "NSTimer.h" #include "NSRenderableEntity.h" #include "NSSurfacePropEntity.h" #include "NSPhysicsEntity.h" diff --git a/src/shared/include.src b/src/shared/include.src index 4577d9e6..00a46b8e 100644 --- a/src/shared/include.src +++ b/src/shared/include.src @@ -3,6 +3,7 @@ NSIO.qc NSTrigger.qc NSEntity.qc +NSTimer.qc NSRenderableEntity.qc NSSurfacePropEntity.qc NSPhysicsEntity.qc