summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2013-04-17 13:22:58 -0700
committerBen Longbons <b.r.longbons@gmail.com>2013-04-27 14:09:22 -0700
commitd18f5bdb682a1d9c6e3a191926bfd46d36e813c1 (patch)
treec987d53ea924b761e5445572a438e0c4bc825d48
parentda6b990ca1f553a017003f32a436304c66c62b9e (diff)
downloadtmwa-d18f5bdb682a1d9c6e3a191926bfd46d36e813c1.tar.gz
tmwa-d18f5bdb682a1d9c6e3a191926bfd46d36e813c1.tar.bz2
tmwa-d18f5bdb682a1d9c6e3a191926bfd46d36e813c1.tar.xz
tmwa-d18f5bdb682a1d9c6e3a191926bfd46d36e813c1.zip
Force timers to be managed
-rw-r--r--src/char/char.cpp24
-rw-r--r--src/common/timer.cpp83
-rw-r--r--src/common/timer.hpp25
-rw-r--r--src/common/timer.t.hpp42
-rw-r--r--src/login/login.cpp14
-rw-r--r--src/map/atcommand.cpp10
-rw-r--r--src/map/chrif.cpp10
-rw-r--r--src/map/clif.cpp19
-rw-r--r--src/map/magic-expr.cpp5
-rw-r--r--src/map/magic-interpreter-base.cpp4
-rw-r--r--src/map/magic-interpreter.hpp2
-rw-r--r--src/map/magic-stmt.cpp27
-rw-r--r--src/map/map.cpp7
-rw-r--r--src/map/map.hpp24
-rw-r--r--src/map/mob.cpp90
-rw-r--r--src/map/npc.cpp28
-rw-r--r--src/map/party.cpp5
-rw-r--r--src/map/pc.cpp127
-rw-r--r--src/map/script.cpp13
-rw-r--r--src/map/skill.cpp32
20 files changed, 307 insertions, 284 deletions
diff --git a/src/char/char.cpp b/src/char/char.cpp
index 793035c..a03687d 100644
--- a/src/char/char.cpp
+++ b/src/char/char.cpp
@@ -3308,17 +3308,25 @@ int do_init(int argc, char **argv)
char_fd = make_listen_port(char_port);
- add_timer_interval(gettick() + std::chrono::seconds(1),
- check_connect_login_server, std::chrono::seconds(10));
- add_timer_interval(gettick() + std::chrono::seconds(1),
- send_users_tologin, std::chrono::seconds(5));
- add_timer_interval(gettick() + autosave_interval,
- mmo_char_sync_timer, autosave_interval);
+ Timer(gettick() + std::chrono::seconds(1),
+ check_connect_login_server,
+ std::chrono::seconds(10)
+ ).detach();
+ Timer(gettick() + std::chrono::seconds(1),
+ send_users_tologin,
+ std::chrono::seconds(5)
+ ).detach();
+ Timer(gettick() + autosave_interval,
+ mmo_char_sync_timer,
+ autosave_interval
+ ).detach();
if (anti_freeze_enable > 0)
{
- add_timer_interval(gettick() + std::chrono::seconds(1),
- map_anti_freeze_system, ANTI_FREEZE_INTERVAL);
+ Timer(gettick() + std::chrono::seconds(1),
+ map_anti_freeze_system,
+ ANTI_FREEZE_INTERVAL
+ ).detach();
}
CHAR_LOG("The char-server is ready (Server is listening on the port %d).\n",
diff --git a/src/common/timer.cpp b/src/common/timer.cpp
index 219efd9..7b115d9 100644
--- a/src/common/timer.cpp
+++ b/src/common/timer.cpp
@@ -15,12 +15,22 @@
struct TimerData
{
+ /// This will be reset on call, to avoid problems.
+ Timer *owner;
+
/// When it will be triggered
tick_t tick;
/// What will be done
timer_func func;
/// Repeat rate - 0 for oneshot
interval_t interval;
+
+ TimerData(Timer *o, tick_t t, timer_func f, interval_t i)
+ : owner(o)
+ , tick(t)
+ , func(std::move(f))
+ , interval(i)
+ {}
};
struct TimerCompare
@@ -52,6 +62,30 @@ tick_t milli_clock::now(void) noexcept
}
static
+void do_nothing(TimerData *, tick_t)
+{
+}
+
+void Timer::cancel()
+{
+ if (!td)
+ return;
+
+ assert (this == td->owner);
+ td->owner = nullptr;
+ td->func = do_nothing;
+ td->interval = interval_t::zero();
+ td = nullptr;
+}
+
+void Timer::detach()
+{
+ assert (this == td->owner);
+ td->owner = nullptr;
+ td = nullptr;
+}
+
+static
void push_timer_heap(TimerData *td)
{
timer_heap.push(td);
@@ -71,34 +105,39 @@ void pop_timer_heap(void)
timer_heap.pop();
}
-TimerData *add_timer(tick_t tick, timer_func func)
-{
- return add_timer_interval(tick, std::move(func), interval_t::zero());
-}
-
-TimerData *add_timer_interval(tick_t tick, timer_func func, interval_t interval)
+Timer::Timer(tick_t tick, timer_func func, interval_t interval)
+: td(new TimerData(this, tick, std::move(func), interval))
{
assert (interval >= interval_t::zero());
- TimerData *td = new TimerData();
- td->tick = tick;
- td->func = std::move(func);
- td->interval = interval;
push_timer_heap(td);
- return td;
}
-static
-void do_nothing(TimerData *, tick_t)
+Timer::Timer(Timer&& t)
+: td(t.td)
{
+ t.td = nullptr;
+ if (td)
+ {
+ assert (td->owner == &t);
+ td->owner = this;
+ }
}
-void delete_timer(TimerData *td)
+Timer& Timer::operator = (Timer&& t)
{
- assert (td != nullptr);
-
- td->func = do_nothing;
- td->interval = interval_t::zero();
+ std::swap(td, t.td);
+ if (td)
+ {
+ assert (td->owner == &t);
+ td->owner = this;
+ }
+ if (t.td)
+ {
+ assert (t.td->owner == this);
+ t.td->owner = &t;
+ }
+ return *this;
}
interval_t do_timer(tick_t tick)
@@ -118,7 +157,13 @@ interval_t do_timer(tick_t tick)
}
pop_timer_heap();
- // If we are too far past the requested tick, call with the current tick instead to fix reregistering problems
+ // Prevent destroying the object we're in.
+ // Note: this would be surprising in an interval timer,
+ // but all interval timers do an immediate explicit detach().
+ if (td->owner)
+ td->owner->detach();
+ // If we are too far past the requested tick, call with
+ // the current tick instead to fix reregistration problems
if (td->tick + std::chrono::seconds(1) < tick)
td->func(td, tick);
else
diff --git a/src/common/timer.hpp b/src/common/timer.hpp
index 876d519..c581377 100644
--- a/src/common/timer.hpp
+++ b/src/common/timer.hpp
@@ -5,12 +5,6 @@
# include "sanity.hpp"
-# include <chrono>
-# include <functional>
-
-/// (to get additional arguments, use std::bind or a lambda).
-typedef std::function<void (TimerData *, tick_t)> timer_func;
-
// updated automatically when using milli_clock::now()
// which is done only by core.cpp
extern tick_t gettick_cache;
@@ -21,23 +15,8 @@ tick_t gettick(void)
return gettick_cache;
}
-/// Schedule a one-shot timer at the given tick.
-/// The timer will automatically be freed after it is called
-/// (during a do_timer).
-TimerData *add_timer(tick_t t, timer_func f);
-
-/// Schedule a recurring timer initially at the given tick.
-/// The timer will automatically reregister itself, with the same
-/// opaque handle, every interval after the tick.
-/// It will never be freed unless you use delete_timer.
-TimerData *add_timer_interval(tick_t, timer_func, interval_t);
-
-/// Cancel the given timer.
-/// This doesn't actually remove it, it just resets the functor.
-/// and waits for the the tick to arrive in do_timer.
-void delete_timer(TimerData *);
-
-/// Do all timers scheduled before tick, and return the number of milliseconds until the next timer happens
+/// Do all timers scheduled before tick, and return the number of
+/// milliseconds until the next timer happens
interval_t do_timer(tick_t tick);
/// Stat a file, and return its modification time, truncated to seconds.
diff --git a/src/common/timer.t.hpp b/src/common/timer.t.hpp
index 67d7450..ee9b5d2 100644
--- a/src/common/timer.t.hpp
+++ b/src/common/timer.t.hpp
@@ -2,6 +2,9 @@
#define TIMER_T_HPP
# include <chrono>
+# include <functional>
+
+struct TimerData;
/// An implementation of the C++ "clock" concept, exposing
/// durations in milliseconds.
@@ -21,8 +24,43 @@ public:
typedef milli_clock::time_point tick_t;
/// The difference between two points in time.
typedef milli_clock::duration interval_t;
+/// (to get additional arguments, use std::bind or a lambda).
+typedef std::function<void (TimerData *, tick_t)> timer_func;
-/// Opaque type representing an active timer.
-struct TimerData;
+class Timer
+{
+ friend struct TimerData;
+ TimerData *td;
+
+ Timer(const Timer&) = delete;
+ Timer& operator = (const Timer&) = delete;
+public:
+ /// Don't own anything yet.
+ Timer() : td(nullptr) {}
+ /// Schedule a timer for the given tick.
+ /// If you do not wish to keep track of it, call disconnect().
+ /// Otherwise, you may cancel() or replace (operator =) it later.
+ ///
+ /// If the interval argument is given, the timer will reschedule
+ /// itself again forever. Otherwise, it will disconnect() itself
+ /// just BEFORE it is called.
+ Timer(tick_t tick, timer_func func, interval_t interval=interval_t::zero());
+
+ Timer(Timer&& t);
+ Timer& operator = (Timer&& t);
+ ~Timer() { cancel(); }
+
+ /// Cancel the delivery of this timer's function, and make it falsy.
+ /// Implementation note: this doesn't actually remove it, just sets
+ /// the functor to do_nothing, and waits for the tick before removing.
+ void cancel();
+ /// Make it falsy without cancelling the timer,
+ void detach();
+
+ /// Check if there is a timer connected.
+ explicit operator bool() { return td; }
+ /// Check if there is no connected timer.
+ bool operator !() { return !td; }
+};
#endif // TIMER_T_HPP
diff --git a/src/login/login.cpp b/src/login/login.cpp
index 583cff9..fea673e 100644
--- a/src/login/login.cpp
+++ b/src/login/login.cpp
@@ -4273,18 +4273,26 @@ int do_init(int argc, char **argv)
login_fd = make_listen_port(login_port);
- add_timer_interval(gettick() + std::chrono::minutes(5), check_auth_sync, std::chrono::minutes(5));
+ Timer(gettick() + std::chrono::minutes(5),
+ check_auth_sync,
+ std::chrono::minutes(5)
+ ).detach();
if (anti_freeze_enable > 0)
{
- add_timer_interval(gettick() + std::chrono::seconds(1), char_anti_freeze_system, ANTI_FREEZE_INTERVAL);
+ Timer(gettick() + std::chrono::seconds(1),
+ char_anti_freeze_system,
+ ANTI_FREEZE_INTERVAL
+ ).detach();
}
// add timer to check GM accounts file modification
std::chrono::seconds j = gm_account_filename_check_timer;
if (j == interval_t::zero())
j = std::chrono::minutes(1);
- add_timer_interval(gettick() + j, check_GM_file, j);
+ Timer(gettick() + j,
+ check_GM_file,
+ j).detach();
LOGIN_LOG("The login-server is ready (Server is listening on the port %d).\n",
login_port);
diff --git a/src/map/atcommand.cpp b/src/map/atcommand.cpp
index ea13a04..5fffaa3 100644
--- a/src/map/atcommand.cpp
+++ b/src/map/atcommand.cpp
@@ -1898,11 +1898,7 @@ int atcommand_pvpoff(const int fd, struct map_session_data *sd,
{
if (sd->bl.m == pl_sd->bl.m)
{
- if (pl_sd->pvp_timer)
- {
- delete_timer(pl_sd->pvp_timer);
- pl_sd->pvp_timer = nullptr;
- }
+ pl_sd->pvp_timer.cancel();
}
}
}
@@ -1943,7 +1939,7 @@ int atcommand_pvpon(const int fd, struct map_session_data *sd,
{
if (sd->bl.m == pl_sd->bl.m && !pl_sd->pvp_timer)
{
- pl_sd->pvp_timer = add_timer(gettick() + std::chrono::milliseconds(200),
+ pl_sd->pvp_timer = Timer(gettick() + std::chrono::milliseconds(200),
std::bind(pc_calc_pvprank_timer, ph::_1, ph::_2, pl_sd->bl.id));
pl_sd->pvp_rank = 0;
pl_sd->pvp_lastusers = 0;
@@ -6049,7 +6045,7 @@ int atcommand_summon(const int, struct map_session_data *sd,
md->master_id = sd->bl.id;
md->state.special_mob_ai = 1;
md->mode = mob_db[md->mob_class].mode | MobMode::AGGRESSIVE;
- md->deletetimer = add_timer(tick + std::chrono::minutes(1),
+ md->deletetimer = Timer(tick + std::chrono::minutes(1),
std::bind(mob_timer_delete, ph::_1, ph::_2,
id));
clif_misceffect(&md->bl, 344);
diff --git a/src/map/chrif.cpp b/src/map/chrif.cpp
index 47eef1a..8926780 100644
--- a/src/map/chrif.cpp
+++ b/src/map/chrif.cpp
@@ -1211,10 +1211,14 @@ void check_connect_char_server(TimerData *, tick_t)
*/
int do_init_chrif (void)
{
- add_timer_interval(gettick() + std::chrono::seconds(1),
+ Timer(gettick() + std::chrono::seconds(1),
check_connect_char_server,
- std::chrono::seconds(10));
- add_timer_interval(gettick() + std::chrono::seconds(1), send_users_tochar, std::chrono::seconds(5));
+ std::chrono::seconds(10)
+ ).detach();
+ Timer(gettick() + std::chrono::seconds(1),
+ send_users_tochar,
+ std::chrono::seconds(5)
+ ).detach();
return 0;
}
diff --git a/src/map/clif.cpp b/src/map/clif.cpp
index ca8e487..f009a23 100644
--- a/src/map/clif.cpp
+++ b/src/map/clif.cpp
@@ -652,9 +652,10 @@ int clif_clearchar_delay(tick_t tick,
CREATE(tmpbl, struct block_list, 1);
memcpy(tmpbl, bl, sizeof(struct block_list));
- add_timer(tick,
+ Timer(tick,
std::bind(clif_clearchar_delay_sub, ph::_1, ph::_2,
- tmpbl, type));
+ tmpbl, type)
+ ).detach();
return 0;
}
@@ -1138,9 +1139,10 @@ void clif_waitclose(TimerData *, tick_t, int id)
*/
void clif_setwaitclose(int fd)
{
- add_timer(gettick() + std::chrono::seconds(5),
+ Timer(gettick() + std::chrono::seconds(5),
std::bind(clif_waitclose, ph::_1, ph::_2,
- fd));
+ fd)
+ ).detach();
}
/*==========================================
@@ -3803,14 +3805,15 @@ void clif_parse_LoadEndAck(int, struct map_session_data *sd)
clif_updatestatus(sd, SP::WEIGHT);
// pvp
- if (sd->pvp_timer && !battle_config.pk_mode)
- delete_timer(sd->pvp_timer);
+ if (!battle_config.pk_mode)
+ sd->pvp_timer.cancel();
+
if (map[sd->bl.m].flag.pvp)
{
if (!battle_config.pk_mode)
{
// remove pvp stuff for pk_mode [Valaris]
- sd->pvp_timer = add_timer(gettick() + std::chrono::milliseconds(200),
+ sd->pvp_timer = Timer(gettick() + std::chrono::milliseconds(200),
std::bind(pc_calc_pvprank_timer, ph::_1, ph::_2,
sd->bl.id));
sd->pvp_rank = 0;
@@ -3820,7 +3823,7 @@ void clif_parse_LoadEndAck(int, struct map_session_data *sd)
}
else
{
- sd->pvp_timer = nullptr;
+ // sd->pvp_timer = nullptr;
}
sd->state.connect_new = 0;
diff --git a/src/map/magic-expr.cpp b/src/map/magic-expr.cpp
index 0acc50a..4a72a38 100644
--- a/src/map/magic-expr.cpp
+++ b/src/map/magic-expr.cpp
@@ -1019,14 +1019,13 @@ int fun_rbox(env_t *, int, val_t *result, val_t *args)
}
static
-int fun_running_status_update(env_t *, int, val_t *result,
- val_t *args)
+int fun_running_status_update(env_t *, int, val_t *result, val_t *args)
{
if (ENTITY_TYPE(0) != BL::PC && ENTITY_TYPE(0) != BL::MOB)
return 1;
StatusChange sc = StatusChange(ARGINT(1));
- RESULTINT = battle_get_sc_data(ARGENTITY(0))[sc].timer != nullptr;
+ RESULTINT = bool(battle_get_sc_data(ARGENTITY(0))[sc].timer);
return 0;
}
diff --git a/src/map/magic-interpreter-base.cpp b/src/map/magic-interpreter-base.cpp
index 5c68d26..26d75af 100644
--- a/src/map/magic-interpreter-base.cpp
+++ b/src/map/magic-interpreter-base.cpp
@@ -506,7 +506,7 @@ invocation_t *spell_instantiate(effect_set_t *effect_set, env_t *env)
invocation_t *spell_clone_effect(invocation_t *base)
{
- invocation_t *retval = (invocation_t *) malloc(sizeof(invocation_t));
+ invocation_t *retval = (invocation_t *) calloc(1, sizeof(invocation_t));
env_t *env;
memcpy(retval, base, sizeof(invocation_t));
@@ -518,7 +518,7 @@ invocation_t *spell_clone_effect(invocation_t *base)
retval->end_effect = NULL;
retval->script_pos = 0;
retval->stack_size = 0;
- retval->timer = 0;
+ // retval->timer = 0;
retval->subject = 0;
retval->status_change_refs_nr = 0;
retval->status_change_refs = NULL;
diff --git a/src/map/magic-interpreter.hpp b/src/map/magic-interpreter.hpp
index 77197f0..22223ec 100644
--- a/src/map/magic-interpreter.hpp
+++ b/src/map/magic-interpreter.hpp
@@ -320,7 +320,7 @@ typedef struct invocation
int caster; /* this is the person who originally invoked the spell */
int subject; /* when this person dies, the spell dies with it */
- TimerData *timer; /* spell timer, if any */
+ Timer timer; /* spell timer, if any */
int stack_size;
cont_activation_record_t stack[MAX_STACK_SIZE];
diff --git a/src/map/magic-stmt.cpp b/src/map/magic-stmt.cpp
index c36e334..0bef054 100644
--- a/src/map/magic-stmt.cpp
+++ b/src/map/magic-stmt.cpp
@@ -84,9 +84,8 @@ void invocation_timer_callback(TimerData *, tick_t, int id)
{
invocation_t *invocation = (invocation_t *) map_id2bl(id);
- if (invocation)
+ assert (invocation != NULL);
{
- invocation->timer = 0;
spell_execute(invocation);
}
}
@@ -121,8 +120,7 @@ void spell_free_invocation(invocation_t *invocation)
clear_stack(invocation);
- if (invocation->timer)
- delete_timer(invocation->timer);
+ invocation->timer.cancel();
magic_free_env(invocation->env);
@@ -255,9 +253,10 @@ void timer_callback_effect(TimerData *, tick_t, int id, int data)
static
void entity_effect(entity_t *entity, int effect_nr, interval_t delay)
{
- add_timer(gettick() + delay,
+ Timer(gettick() + delay,
std::bind(&timer_callback_effect, ph::_1, ph::_2,
- entity->id, effect_nr));
+ entity->id, effect_nr)
+ ).detach();
}
void magic_unshroud(character_t *other_char)
@@ -287,9 +286,10 @@ struct npc_data *local_spell_effect(int m, int x, int y, int effect,
int effect_npc_id = effect_npc->bl.id;
entity_effect(&effect_npc->bl, effect, tdelay);
- add_timer(gettick() + delay,
+ Timer(gettick() + delay,
std::bind(timer_callback_effect_npc_delete, ph::_1, ph::_2,
- effect_npc_id));
+ effect_npc_id)
+ ).detach();
return effect_npc;
}
@@ -424,9 +424,10 @@ int op_messenger_npc(env_t *, int, val_t *args)
npc = npc_spawn_text(loc->m, loc->x, loc->y,
ARGINT(1), ARGSTR(2), ARGSTR(3));
- add_timer(gettick() + static_cast<interval_t>(ARGINT(4)),
+ Timer(gettick() + static_cast<interval_t>(ARGINT(4)),
std::bind(timer_callback_kill_npc, ph::_1, ph::_2,
- npc->bl.id));
+ npc->bl.id)
+ ).detach();
return 0;
}
@@ -737,7 +738,7 @@ int op_spawn(env_t *, int, val_t *args)
mob->mode |=
MobMode::SUMMONED | MobMode::TURNS_AGAINST_BAD_MASTER;
- mob->deletetimer = add_timer(gettick() + monster_lifetime,
+ mob->deletetimer = Timer(gettick() + monster_lifetime,
std::bind(mob_timer_delete, ph::_1, ph::_2,
mob_id));
@@ -1576,8 +1577,8 @@ void spell_execute_d(invocation_t *invocation, int allow_deletion)
if (delta > interval_t::zero())
{
- assert (invocation->timer == nullptr);
- invocation->timer = add_timer(gettick() + delta,
+ assert (!invocation->timer);
+ invocation->timer = Timer(gettick() + delta,
std::bind(invocation_timer_callback, ph::_1, ph::_2,
invocation->bl.id));
}
diff --git a/src/map/map.cpp b/src/map/map.cpp
index 63fc8c9..152b3c8 100644
--- a/src/map/map.cpp
+++ b/src/map/map.cpp
@@ -759,15 +759,14 @@ void map_clearflooritem_timer(TimerData *tid, tick_t, int id)
struct flooritem_data *fitem = NULL;
fitem = (struct flooritem_data *) object[id];
- if (fitem == NULL || fitem->bl.type != BL::ITEM
- || (tid && fitem->cleartimer != tid))
+ if (fitem == NULL || fitem->bl.type != BL::ITEM)
{
if (battle_config.error_log)
PRINTF("map_clearflooritem_timer : error\n");
return;
}
if (!tid)
- delete_timer(fitem->cleartimer);
+ fitem->cleartimer.cancel();
clif_clearflooritem(fitem, 0);
map_delobject(fitem->bl.id, BL::ITEM);
}
@@ -853,7 +852,7 @@ int map_addflooritem_any(struct item *item_data, int amount,
// Currently, it yields the numbers {3 6 9 12}.
fitem->subx = random_::in(1, 4) * 3;
fitem->suby = random_::in(1, 4) * 3;
- fitem->cleartimer = add_timer(gettick() + lifetime,
+ fitem->cleartimer = Timer(gettick() + lifetime,
std::bind(map_clearflooritem_timer, ph::_1, ph::_2,
fitem->bl.id));
diff --git a/src/map/map.hpp b/src/map/map.hpp
index ab9b4c8..caffab0 100644
--- a/src/map/map.hpp
+++ b/src/map/map.hpp
@@ -60,7 +60,7 @@ struct script_regstr
};
struct status_change
{
- TimerData *timer;
+ Timer timer;
int val1;
int spell_invocation; /* [Fate] If triggered by a spell, record here */
};
@@ -129,7 +129,7 @@ struct map_session_data
DIR dir, head_dir;
tick_t client_tick, server_tick;
struct walkpath_data walkpath;
- TimerData *walktimer;
+ Timer walktimer;
int npc_id, areanpc_id, npc_shopid;
int npc_pos;
int npc_menu;
@@ -145,7 +145,7 @@ struct map_session_data
} npc_flags;
unsigned int chatID;
- TimerData *attacktimer;
+ Timer attacktimer;
int attacktarget;
ATK attacktarget_lv;
tick_t attackabletime;
@@ -173,7 +173,7 @@ struct map_session_data
// [Fate] XP that can be extracted from this player by healing
int heal_xp; // i.e., OTHER players (healers) can partake in this player's XP
- TimerData *invincible_timer;
+ Timer invincible_timer;
tick_t canact_tick;
tick_t canmove_tick;
tick_t canlog_tick;
@@ -233,11 +233,11 @@ struct map_session_data
int catch_target_class;
int pvp_point, pvp_rank;
- TimerData *pvp_timer;
+ Timer pvp_timer;
int pvp_lastusers;
char eventqueue[MAX_EVENTQUEUE][50];
- TimerData *eventtimer[MAX_EVENTTIMER];
+ Timer eventtimer[MAX_EVENTTIMER];
struct
{
@@ -300,7 +300,7 @@ struct npc_data
const ScriptCode *script;
short xs, ys;
interval_t timer;
- TimerData *timerid;
+ Timer timerid;
int timeramount, nexttimer;
tick_t timertick;
struct npc_timerevent_list *timer_event;
@@ -320,7 +320,7 @@ struct npc_data
// ここにメンバを追加してはならない(shop_itemが可変長の為)
char eventqueue[MAX_EVENTQUEUE][50];
- TimerData *eventtimer[MAX_EVENTTIMER];
+ Timer eventtimer[MAX_EVENTTIMER];
short arenaflag;
};
@@ -350,7 +350,7 @@ struct mob_data
unsigned walk_easy:1;
unsigned special_mob_ai:3;
} state;
- TimerData *timer;
+ Timer timer;
short to_x, to_y;
int hp;
int target_id, attacked_id;
@@ -377,9 +377,9 @@ struct mob_data
Option option;
short min_chase;
short sg_count;
- TimerData *deletetimer;
+ Timer deletetimer;
- TimerData *skilltimer;
+ Timer skilltimer;
int skilltarget;
short skillx, skilly;
SkillID skillid;
@@ -470,7 +470,7 @@ struct flooritem_data
{
struct block_list bl;
short subx, suby;
- TimerData *cleartimer;
+ Timer cleartimer;
int first_get_id, second_get_id, third_get_id;
tick_t first_get_tick, second_get_tick, third_get_tick;
struct item item_data;
diff --git a/src/map/mob.cpp b/src/map/mob.cpp
index ba2528c..a6070f0 100644
--- a/src/map/mob.cpp
+++ b/src/map/mob.cpp
@@ -107,7 +107,7 @@ int mob_spawn_dataset(struct mob_data *md, const char *mobname, int mob_class)
md->bl.id = npc_get_new_npc_id();
memset(&md->state, 0, sizeof(md->state));
- md->timer = nullptr;
+ // md->timer = nullptr;
md->target_id = 0;
md->attacked_id = 0;
@@ -646,7 +646,7 @@ int mob_walk(struct mob_data *md, tick_t tick, unsigned char data)
i = i / 2;
if (md->walkpath.path_half == 0)
i = std::max(i, std::chrono::milliseconds(1));
- md->timer = add_timer(tick + i,
+ md->timer = Timer(tick + i,
std::bind(mob_timer, ph::_1, ph::_2,
md->bl.id, md->walkpath.path_pos));
md->state.state = MS::WALK;
@@ -796,7 +796,7 @@ int mob_attack(struct mob_data *md, tick_t tick)
md->attackabletime = tick + battle_get_adelay(&md->bl);
- md->timer = add_timer(md->attackabletime,
+ md->timer = Timer(md->attackabletime,
std::bind(mob_timer, ph::_1, ph::_2,
md->bl.id, 0));
md->state.state = MS::ATTACK;
@@ -827,9 +827,7 @@ int mob_changestate(struct mob_data *md, MS state, bool type)
{
nullpo_ret(md);
- if (md->timer)
- delete_timer(md->timer);
- md->timer = nullptr;
+ md->timer.cancel();
md->state.state = state;
switch (state)
@@ -840,7 +838,7 @@ int mob_changestate(struct mob_data *md, MS state, bool type)
if (i > interval_t::zero())
{
i = i / 4;
- md->timer = add_timer(gettick() + i,
+ md->timer = Timer(gettick() + i,
std::bind(mob_timer, ph::_1, ph::_2,
md->bl.id, 0));
}
@@ -853,20 +851,20 @@ int mob_changestate(struct mob_data *md, MS state, bool type)
tick_t tick = gettick();
interval_t i = md->attackabletime - tick;
if (i > interval_t::zero() && i < std::chrono::seconds(2))
- md->timer = add_timer(md->attackabletime,
+ md->timer = Timer(md->attackabletime,
std::bind(mob_timer, ph::_1, ph::_2,
md->bl.id, 0));
else if (type)
{
md->attackabletime = tick + battle_get_amotion(&md->bl);
- md->timer = add_timer(md->attackabletime,
+ md->timer = Timer(md->attackabletime,
std::bind(mob_timer, ph::_1, ph::_2,
md->bl.id, 0));
}
else
{
md->attackabletime = tick + std::chrono::milliseconds(1);
- md->timer = add_timer(md->attackabletime,
+ md->timer = Timer(md->attackabletime,
std::bind(mob_timer, ph::_1, ph::_2,
md->bl.id, 0));
}
@@ -880,9 +878,7 @@ int mob_changestate(struct mob_data *md, MS state, bool type)
// Since it died, all aggressors' attack to this mob is stopped.
clif_foreachclient(std::bind(mob_stopattacked, ph::_1, md->bl.id));
skill_status_change_clear(&md->bl, 2); // The abnormalities in status are canceled.
- if (md->deletetimer)
- delete_timer(md->deletetimer);
- md->deletetimer = nullptr;
+ md->deletetimer.cancel();
md->hp = md->target_id = md->attacked_id = 0;
md->state.attackable = false;
}
@@ -898,7 +894,7 @@ int mob_changestate(struct mob_data *md, MS state, bool type)
*------------------------------------------
*/
static
-void mob_timer(TimerData *tid, tick_t tick, int id, unsigned char data)
+void mob_timer(TimerData *, tick_t tick, int id, unsigned char data)
{
struct mob_data *md;
struct block_list *bl;
@@ -913,8 +909,6 @@ void mob_timer(TimerData *tid, tick_t tick, int id, unsigned char data)
md = (struct mob_data *) bl;
- assert (md->timer == tid);
- md->timer = nullptr;
if (md->bl.prev == NULL || md->state.state == MS::DEAD)
return;
@@ -1043,9 +1037,10 @@ int mob_setdelayspawn(int id)
tick_t spawntime3 = gettick() + std::chrono::seconds(5);
tick_t spawntime = std::max({spawntime1, spawntime2, spawntime3});
- add_timer(spawntime,
+ Timer(spawntime,
std::bind(mob_delayspawn, ph::_1, ph::_2,
- id));
+ id)
+ ).detach();
return 0;
}
@@ -1104,9 +1099,10 @@ int mob_spawn(int id)
{
// if(battle_config.error_log==1)
// PRINTF("MOB spawn error %d @ %s\n",id,map[md->bl.m].name);
- add_timer(tick + std::chrono::seconds(5),
+ Timer(tick + std::chrono::seconds(5),
std::bind(mob_delayspawn, ph::_1, ph::_2,
- id));
+ id)
+ ).detach();
return 1;
}
}
@@ -1131,16 +1127,16 @@ int mob_spawn(int id)
md->state.state = MS::IDLE;
md->state.skillstate = MobSkillState::MSS_IDLE;
- md->timer = nullptr;
+ assert (!md->timer);
md->last_thinktime = tick;
md->next_walktime = tick + std::chrono::seconds(5) + std::chrono::milliseconds(random_::to(50));
md->attackabletime = tick;
md->canmove_tick = tick;
md->sg_count = 0;
- md->deletetimer = nullptr;
+ // md->deletetimer = nullptr;
- md->skilltimer = nullptr;
+ // md->skilltimer = nullptr;
for (int i = 0; i < MAX_MOBSKILL; i++)
md->skilldelay[i] = tick - std::chrono::hours(10);
md->skillid = SkillID();
@@ -1153,7 +1149,7 @@ int mob_spawn(int id)
for (StatusChange i : erange(StatusChange(), StatusChange::MAX_STATUSCHANGE))
{
- md->sc_data[i].timer = nullptr;
+ assert (!md->sc_data[i].timer);
md->sc_data[i].val1 = 0;
}
md->sc_count = 0;
@@ -2497,6 +2493,7 @@ int mob_damage(struct block_list *src, struct mob_data *md, int damage,
// ----- ここから死亡処理 -----
map_freeblock_lock();
+ // cancels timers
mob_changestate(md, MS::DEAD, 0);
mobskill_use(md, tick, MobSkillCondition::ANY);
@@ -2657,9 +2654,10 @@ int mob_damage(struct block_list *src, struct mob_data *md, int damage,
ditem->first_sd = mvp_sd;
ditem->second_sd = second_sd;
ditem->third_sd = third_sd;
- add_timer(tick + std::chrono::milliseconds(500) + static_cast<interval_t>(i),
+ Timer(tick + std::chrono::milliseconds(500) + static_cast<interval_t>(i),
std::bind(mob_delay_item_drop, ph::_1, ph::_2,
- ditem));
+ ditem)
+ ).detach();
}
if (md->lootitem)
{
@@ -2678,9 +2676,10 @@ int mob_damage(struct block_list *src, struct mob_data *md, int damage,
ditem->second_sd = second_sd;
ditem->third_sd = third_sd;
// ?
- add_timer(tick + std::chrono::milliseconds(540) + static_cast<interval_t>(i),
+ Timer(tick + std::chrono::milliseconds(540) + static_cast<interval_t>(i),
std::bind(mob_delay_item_drop2, ph::_1, ph::_2,
- ditem));
+ ditem)
+ ).detach();
}
}
}
@@ -3023,7 +3022,7 @@ int mob_counttargeted(struct mob_data *md, struct block_list *src,
* スキル使用(詠唱完了、ID指定)
*------------------------------------------
*/
-void mobskill_castend_id(TimerData *tid, tick_t tick, int id)
+void mobskill_castend_id(TimerData *, tick_t tick, int id)
{
struct mob_data *md = NULL;
struct block_list *bl;
@@ -3039,10 +3038,6 @@ void mobskill_castend_id(TimerData *tid, tick_t tick, int id)
}
if (md->bl.type != BL::MOB || md->bl.prev == NULL)
return;
- if (md->skilltimer != tid) // タイマIDの確認
- return;
-
- md->skilltimer = nullptr;
if (bool(md->opt1))
return;
@@ -3095,7 +3090,7 @@ void mobskill_castend_id(TimerData *tid, tick_t tick, int id)
* スキル使用(詠唱完了、場所指定)
*------------------------------------------
*/
-void mobskill_castend_pos(TimerData *tid, tick_t tick, int id)
+void mobskill_castend_pos(TimerData *, tick_t tick, int id)
{
struct mob_data *md = NULL;
struct block_list *bl;
@@ -3111,11 +3106,6 @@ void mobskill_castend_pos(TimerData *tid, tick_t tick, int id)
if (md->bl.type != BL::MOB || md->bl.prev == NULL)
return;
- if (md->skilltimer != tid) // タイマIDの確認
- return;
-
- md->skilltimer = nullptr;
-
if (bool(md->opt1))
return;
@@ -3195,14 +3185,14 @@ int mobskill_use_id(struct mob_data *md, struct block_list *target,
if (casttime > interval_t::zero())
{
- md->skilltimer = add_timer(gettick() + casttime,
+ md->skilltimer = Timer(gettick() + casttime,
std::bind(mobskill_castend_id, ph::_1, ph::_2,
md->bl.id));
}
else
{
- md->skilltimer = nullptr;
- mobskill_castend_id(md->skilltimer, gettick(), md->bl.id);
+ assert (!md->skilltimer);
+ mobskill_castend_id(nullptr, gettick(), md->bl.id);
}
return 1;
@@ -3267,14 +3257,14 @@ int mobskill_use_pos(struct mob_data *md,
md->skillidx = skill_idx;
if (casttime > interval_t::zero())
{
- md->skilltimer = add_timer(gettick() + casttime,
+ md->skilltimer = Timer(gettick() + casttime,
std::bind(mobskill_castend_pos, ph::_1, ph::_2,
md->bl.id));
}
else
{
- md->skilltimer = nullptr;
- mobskill_castend_pos(md->skilltimer, gettick(), md->bl.id);
+ assert (!md->skilltimer);
+ mobskill_castend_pos(nullptr, gettick(), md->bl.id);
}
return 1;
@@ -3751,12 +3741,14 @@ int do_init_mob(void)
mob_readskilldb();
- add_timer_interval(gettick() + MIN_MOBTHINKTIME,
+ Timer(gettick() + MIN_MOBTHINKTIME,
mob_ai_hard,
- MIN_MOBTHINKTIME);
- add_timer_interval(gettick() + MIN_MOBTHINKTIME * 10,
+ MIN_MOBTHINKTIME
+ ).detach();
+ Timer(gettick() + MIN_MOBTHINKTIME * 10,
mob_ai_lazy,
- MIN_MOBTHINKTIME * 10);
+ MIN_MOBTHINKTIME * 10
+ ).detach();
return 0;
}
diff --git a/src/map/npc.cpp b/src/map/npc.cpp
index 7b88705..fb4f79e 100644
--- a/src/map/npc.cpp
+++ b/src/map/npc.cpp
@@ -1,5 +1,6 @@
#include "npc.hpp"
+#include <cassert>
#include <cstdlib>
#include <cstring>
#include <ctime>
@@ -275,9 +276,10 @@ int npc_event_do_oninit(void)
int c = npc_event_doall("OnInit");
PRINTF("npc: OnInit Event done. (%d npc)\n", c);
- add_timer_interval(gettick() + std::chrono::milliseconds(100),
+ Timer(gettick() + std::chrono::milliseconds(100),
npc_event_do_clock,
- std::chrono::seconds(1));
+ std::chrono::seconds(1)
+ ).detach();
return 0;
}
@@ -291,21 +293,19 @@ void npc_timerevent(TimerData *, tick_t tick, int id, interval_t data)
{
struct npc_data *nd = (struct npc_data *) map_id2bl(id);
struct npc_timerevent_list *te;
- if (nd == NULL || nd->u.scr.nexttimer < 0)
- {
- PRINTF("npc_timerevent: ??\n");
- return;
- }
+ assert (nd != NULL);
+ assert (nd->u.scr.nexttimer >= 0);
+
nd->u.scr.timertick = tick;
te = nd->u.scr.timer_event + nd->u.scr.nexttimer;
- nd->u.scr.timerid = nullptr;
+ // nd->u.scr.timerid = nullptr;
interval_t t = nd->u.scr.timer += data;
nd->u.scr.nexttimer++;
if (nd->u.scr.timeramount > nd->u.scr.nexttimer)
{
interval_t next = nd->u.scr.timer_event[nd->u.scr.nexttimer].timer - t;
- nd->u.scr.timerid = add_timer(tick + next,
+ nd->u.scr.timerid = Timer(tick + next,
std::bind(npc_timerevent, ph::_1, ph::_2,
id, next));
}
@@ -339,7 +339,7 @@ int npc_timerevent_start(struct npc_data *nd)
return 0;
interval_t next = nd->u.scr.timer_event[j].timer - nd->u.scr.timer;
- nd->u.scr.timerid = add_timer(gettick() + next,
+ nd->u.scr.timerid = Timer(gettick() + next,
std::bind(npc_timerevent, ph::_1, ph::_2,
nd->bl.id, next));
return 0;
@@ -357,9 +357,7 @@ int npc_timerevent_stop(struct npc_data *nd)
{
nd->u.scr.nexttimer = -1;
nd->u.scr.timer += gettick() - nd->u.scr.timertick;
- if (nd->u.scr.timerid)
- delete_timer(nd->u.scr.timerid);
- nd->u.scr.timerid = nullptr;
+ nd->u.scr.timerid.cancel();
}
return 0;
}
@@ -1476,7 +1474,7 @@ int npc_parse_script(char *w1, char *w2, char *w3, char *w4,
}
}
nd->u.scr.nexttimer = -1;
- nd->u.scr.timerid = nullptr;
+ // nd->u.scr.timerid = nullptr;
return 0;
}
@@ -1611,7 +1609,7 @@ int npc_parse_mob(const char *w1, const char *, const char *w3, const char *w4)
md->spawndelay2 = delay2;
memset(&md->state, 0, sizeof(md->state));
- md->timer = nullptr;
+ // md->timer = nullptr;
md->target_id = 0;
md->attacked_id = 0;
diff --git a/src/map/party.cpp b/src/map/party.cpp
index 277d0c8..5f04b58 100644
--- a/src/map/party.cpp
+++ b/src/map/party.cpp
@@ -31,9 +31,10 @@ void party_send_xyhp_timer(TimerData *tid, tick_t tick);
// 初期化
void do_init_party(void)
{
- add_timer_interval(gettick() + PARTY_SEND_XYHP_INVERVAL,
+ Timer(gettick() + PARTY_SEND_XYHP_INVERVAL,
party_send_xyhp_timer,
- PARTY_SEND_XYHP_INVERVAL);
+ PARTY_SEND_XYHP_INVERVAL
+ ).detach();
}
// 検索
diff --git a/src/map/pc.cpp b/src/map/pc.cpp
index 39ac711..772c673 100644
--- a/src/map/pc.cpp
+++ b/src/map/pc.cpp
@@ -296,25 +296,19 @@ int distance(int x0, int y0, int x1, int y1)
}
static
-void pc_invincible_timer(TimerData *tid, tick_t, int id)
+void pc_invincible_timer(TimerData *, tick_t, int id)
{
- struct map_session_data *sd;
-
- if ((sd = map_id2sd(id)) == NULL
- || sd->bl.type != BL::PC)
- return;
+ struct map_session_data *sd = map_id2sd(id);
- assert (sd->invincible_timer == tid);
- sd->invincible_timer = nullptr;
+ assert (sd != NULL);
+ assert (sd->bl.type == BL::PC);
}
int pc_setinvincibletimer(struct map_session_data *sd, interval_t val)
{
nullpo_ret(sd);
- if (sd->invincible_timer != nullptr)
- delete_timer(sd->invincible_timer);
- sd->invincible_timer = add_timer(gettick() + val,
+ sd->invincible_timer = Timer(gettick() + val,
std::bind(pc_invincible_timer, ph::_1, ph::_2,
sd->bl.id));
return 0;
@@ -324,11 +318,7 @@ int pc_delinvincibletimer(struct map_session_data *sd)
{
nullpo_ret(sd);
- if (sd->invincible_timer)
- {
- delete_timer(sd->invincible_timer);
- sd->invincible_timer = nullptr;
- }
+ sd->invincible_timer.cancel();
return 0;
}
@@ -655,9 +645,9 @@ int pc_authok(int id, int login_id2, TimeT connect_until_time,
sd->dir = DIR::S;
sd->head_dir = DIR::S;
sd->state.auth = 1;
- sd->walktimer = nullptr;
- sd->attacktimer = nullptr;
- sd->invincible_timer = nullptr;
+ // sd->walktimer = nullptr;
+ // sd->attacktimer = nullptr;
+ // sd->invincible_timer = nullptr;
sd->sg_count = 0;
sd->deal_locked = 0;
@@ -700,7 +690,7 @@ int pc_authok(int id, int login_id2, TimeT connect_until_time,
// ステータス異常の初期化
for (StatusChange i : erange(StatusChange(), StatusChange::MAX_STATUSCHANGE))
{
- sd->sc_data[i].timer = nullptr;
+ // sd->sc_data[i].timer = nullptr;
sd->sc_data[i].val1 = 0;
}
sd->sc_count = 0;
@@ -719,7 +709,10 @@ int pc_authok(int id, int login_id2, TimeT connect_until_time,
// イベント関係の初期化
memset(sd->eventqueue, 0, sizeof(sd->eventqueue));
for (int i = 0; i < MAX_EVENTTIMER; i++)
- sd->eventtimer[i] = nullptr;
+ {
+ // sd->eventtimer[i] = nullptr;
+ }
+
// 位置の設定
pc_setpos(sd, sd->status.last_point.map, sd->status.last_point.x,
@@ -733,7 +726,7 @@ int pc_authok(int id, int login_id2, TimeT connect_until_time,
// pvpの設定
sd->pvp_rank = 0;
sd->pvp_point = 0;
- sd->pvp_timer = nullptr;
+ // sd->pvp_timer = nullptr;
// 通知
@@ -2530,7 +2523,7 @@ interval_t calc_next_walk_step(struct map_session_data *sd)
*------------------------------------------
*/
static
-void pc_walk(TimerData *tid, tick_t tick, int id, unsigned char data)
+void pc_walk(TimerData *, tick_t tick, int id, unsigned char data)
{
struct map_session_data *sd;
int moveblock;
@@ -2540,8 +2533,6 @@ void pc_walk(TimerData *tid, tick_t tick, int id, unsigned char data)
if (sd == NULL)
return;
- assert (sd->walktimer == tid);
- sd->walktimer = nullptr;
if (sd->walkpath.path_pos >= sd->walkpath.path_len
|| sd->walkpath.path_pos != data)
return;
@@ -2640,8 +2631,8 @@ void pc_walk(TimerData *tid, tick_t tick, int id, unsigned char data)
i = i / 2;
if (sd->walkpath.path_half == 0)
i = std::max(i, std::chrono::milliseconds(1));
- assert (!sd->walktimer);
- sd->walktimer = add_timer(tick + i,
+
+ sd->walktimer = Timer(tick + i,
std::bind(pc_walk, ph::_1, ph::_2,
id, sd->walkpath.path_pos));
}
@@ -2669,9 +2660,7 @@ int pc_walktoxy_sub(struct map_session_data *sd)
if (i > interval_t::zero())
{
i = i / 4;
- if (sd->walktimer)
- delete_timer(sd->walktimer);
- sd->walktimer = add_timer(gettick() + i,
+ sd->walktimer = Timer(gettick() + i,
std::bind(pc_walk, ph::_1, ph::_2,
sd->bl.id, 0));
}
@@ -2717,11 +2706,8 @@ int pc_stop_walking(struct map_session_data *sd, int type)
{
nullpo_ret(sd);
- if (sd->walktimer)
- {
- delete_timer(sd->walktimer);
- sd->walktimer = nullptr;
- }
+ sd->walktimer.cancel();
+
sd->walkpath.path_len = 0;
sd->to_x = sd->bl.x;
sd->to_y = sd->bl.y;
@@ -2846,7 +2832,7 @@ int pc_checkequip(struct map_session_data *sd, EPOS pos)
*------------------------------------------
*/
static
-void pc_attack_timer(TimerData *tid, tick_t tick, int id)
+void pc_attack_timer(TimerData *, tick_t tick, int id)
{
struct map_session_data *sd;
struct block_list *bl;
@@ -2856,8 +2842,6 @@ void pc_attack_timer(TimerData *tid, tick_t tick, int id)
sd = map_id2sd(id);
if (sd == NULL)
return;
- assert (sd->attacktimer == tid);
- sd->attacktimer = nullptr;
if (sd->bl.prev == NULL)
return;
@@ -2946,7 +2930,7 @@ void pc_attack_timer(TimerData *tid, tick_t tick, int id)
if (sd->state.attack_continue)
{
- sd->attacktimer = add_timer(sd->attackabletime,
+ sd->attacktimer = Timer(sd->attackabletime,
std::bind(pc_attack_timer, ph::_1, ph::_2,
sd->bl.id));
}
@@ -2983,7 +2967,7 @@ int pc_attack(struct map_session_data *sd, int target_id, int type)
interval_t d = sd->attackabletime - gettick();
if (d > interval_t::zero() && d < std::chrono::seconds(2))
{ // 攻撃delay中
- sd->attacktimer = add_timer(sd->attackabletime,
+ sd->attacktimer = Timer(sd->attackabletime,
std::bind(pc_attack_timer, ph::_1, ph::_2,
sd->bl.id));
}
@@ -3004,11 +2988,8 @@ int pc_stopattack(struct map_session_data *sd)
{
nullpo_ret(sd);
- if (sd->attacktimer)
- {
- delete_timer(sd->attacktimer);
- sd->attacktimer = nullptr;
- }
+ sd->attacktimer.cancel();
+
sd->attacktarget = 0;
sd->state.attack_continue = 0;
@@ -4492,28 +4473,14 @@ int pc_setaccountreg2(struct map_session_data *sd, const char *reg, int val)
*------------------------------------------
*/
static
-void pc_eventtimer(TimerData *tid, tick_t, int id, const char *data)
+void pc_eventtimer(TimerData *, tick_t, int id, const char *data)
{
struct map_session_data *sd = map_id2sd(id);
- int i;
- if (sd == NULL)
- return;
+ assert (sd != NULL);
+
+ npc_event(sd, data, 0);
- for (i = 0; i < MAX_EVENTTIMER; i++)
- {
- if (sd->eventtimer[i] == tid)
- {
- sd->eventtimer[i] = nullptr;
- npc_event(sd, data, 0);
- break;
- }
- }
free(const_cast<char *>(data));
- if (i == MAX_EVENTTIMER)
- {
- if (battle_config.error_log)
- PRINTF("pc_eventtimer: no such event timer\n");
- }
}
/*==========================================
@@ -4533,9 +4500,8 @@ int pc_addeventtimer(struct map_session_data *sd, interval_t tick, const char *n
if (i < MAX_EVENTTIMER)
{
char *evname = (char *) calloc(24, 1);
- strncpy(evname, name, 24);
- evname[23] = '\0';
- sd->eventtimer[i] = add_timer(gettick() + tick,
+ strzcpy(evname, name, 24);
+ sd->eventtimer[i] = Timer(gettick() + tick,
std::bind(pc_eventtimer, ph::_1, ph::_2,
sd->bl.id, evname));
return 1;
@@ -4550,16 +4516,10 @@ int pc_addeventtimer(struct map_session_data *sd, interval_t tick, const char *n
*/
int pc_cleareventtimer(struct map_session_data *sd)
{
- int i;
-
nullpo_ret(sd);
- for (i = 0; i < MAX_EVENTTIMER; i++)
- if (sd->eventtimer[i])
- {
- delete_timer(sd->eventtimer[i]);
- sd->eventtimer[i] = nullptr;
- }
+ for (int i = 0; i < MAX_EVENTTIMER; i++)
+ sd->eventtimer[i].cancel();
return 0;
}
@@ -4629,6 +4589,7 @@ int pc_equipitem(struct map_session_data *sd, int n, EPOS)
}
#warning "TODO: make this code do what it's supposed to do, instead of what it does"
+ // https://trac.paradoxsystems.net/changeset/7550/trunk/src/map/pc.c
arrow = pc_search_inventory(sd, pc_checkequip(sd, EPOS::LEGS | EPOS::CAPE)); // Added by RoVeRT
for (EQUIP i : EQUIPs)
{
@@ -4992,9 +4953,9 @@ void pc_calc_pvprank_timer(TimerData *, tick_t, int id)
sd = map_id2sd(id);
if (sd == NULL)
return;
- sd->pvp_timer = nullptr;
+ sd->pvp_timer.cancel();
if (pc_calc_pvprank(sd) > 0)
- sd->pvp_timer = add_timer(gettick() + PVP_CALCRANK_INTERVAL,
+ sd->pvp_timer = Timer(gettick() + PVP_CALCRANK_INTERVAL,
std::bind(pc_calc_pvprank_timer, ph::_1, ph::_2,
id));
}
@@ -5417,7 +5378,9 @@ void pc_autosave(TimerData *, tick_t)
interval_t interval = autosave_interval / (clif_countusers() + 1);
if (interval <= interval_t::zero())
interval = std::chrono::milliseconds(1);
- add_timer(gettick() + interval, pc_autosave);
+ Timer(gettick() + interval,
+ pc_autosave
+ ).detach();
}
int pc_read_gm_account(int fd)
@@ -5470,11 +5433,13 @@ int do_init_pc(void)
{
pc_calc_sigma();
natural_heal_prev_tick = gettick() + NATURAL_HEAL_INTERVAL;
- add_timer_interval(natural_heal_prev_tick,
+ Timer(natural_heal_prev_tick,
pc_natural_heal,
- NATURAL_HEAL_INTERVAL);
- add_timer(gettick() + autosave_interval,
- pc_autosave);
+ NATURAL_HEAL_INTERVAL
+ ).detach();
+ Timer(gettick() + autosave_interval,
+ pc_autosave
+ ).detach();
return 0;
}
diff --git a/src/map/script.cpp b/src/map/script.cpp
index 151175b..55c7d42 100644
--- a/src/map/script.cpp
+++ b/src/map/script.cpp
@@ -3570,7 +3570,7 @@ void builtin_pvpon(ScriptState *st)
{
if (m == pl_sd->bl.m && !pl_sd->pvp_timer)
{
- pl_sd->pvp_timer = add_timer(gettick() + std::chrono::milliseconds(200),
+ pl_sd->pvp_timer = Timer(gettick() + std::chrono::milliseconds(200),
std::bind(pc_calc_pvprank_timer, ph::_1, ph::_2,
pl_sd->bl.id));
pl_sd->pvp_rank = 0;
@@ -3605,11 +3605,7 @@ void builtin_pvpoff(ScriptState *st)
{
if (m == pl_sd->bl.m)
{
- if (pl_sd->pvp_timer)
- {
- delete_timer(pl_sd->pvp_timer);
- pl_sd->pvp_timer = nullptr;
- }
+ pl_sd->pvp_timer.cancel();
}
}
}
@@ -5109,9 +5105,10 @@ void do_init_script(void)
{
script_load_mapreg();
- add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL,
+ Timer(gettick() + MAPREG_AUTOSAVE_INTERVAL,
script_autosave_mapreg,
- MAPREG_AUTOSAVE_INTERVAL);
+ MAPREG_AUTOSAVE_INTERVAL
+ ).detach();
}
#define BUILTIN(func, args) \
diff --git a/src/map/skill.cpp b/src/map/skill.cpp
index df6ee13..ff15de8 100644
--- a/src/map/skill.cpp
+++ b/src/map/skill.cpp
@@ -658,8 +658,7 @@ int skill_castcancel(struct block_list *bl, int)
struct mob_data *md = (struct mob_data *) bl;
if (md->skilltimer)
{
- delete_timer(md->skilltimer);
- md->skilltimer = nullptr;
+ md->skilltimer.cancel();
clif_skillcastcancel(bl);
}
return 0;
@@ -758,7 +757,7 @@ int skill_status_change_active(struct block_list *bl, StatusChange type)
if (not sc_data)
return 0;
- return sc_data[type].timer != nullptr;
+ return bool(sc_data[type].timer);
}
void skill_status_change_end(struct block_list *bl, StatusChange type, TimerData *tid)
@@ -792,19 +791,14 @@ void skill_status_change_end(struct block_list *bl, StatusChange type, TimerData
opt3 = battle_get_opt3(bl);
nullpo_retv(opt3);
- if (!sc_data[type].timer)
- return;
assert ((*sc_count) > 0);
- assert (sc_data[type].timer == tid || !tid);
{
if (!tid)
- // タイマから呼ばれていないならタイマ削除をする
- delete_timer(sc_data[type].timer);
+ sc_data[type].timer.cancel();
- /* 該当の異常を正常に戻す */
- sc_data[type].timer = nullptr;
+ assert (!sc_data[type].timer);
(*sc_count)--;
switch (type)
@@ -901,8 +895,6 @@ void skill_status_change_timer(TimerData *tid, tick_t tick, int id, StatusChange
//sc_count=battle_get_sc_count(bl); //使ってない?
- assert (sc_data[type].timer == tid);
-
if (sc_data[type].spell_invocation)
{ // Must report termination
spell_effect_report_termination(sc_data[type].spell_invocation,
@@ -940,13 +932,13 @@ void skill_status_change_timer(TimerData *tid, tick_t tick, int id, StatusChange
md->hp -= hp;
}
}
- sc_data[type].timer = add_timer(tick + std::chrono::seconds(1),
+ sc_data[type].timer = Timer(tick + std::chrono::seconds(1),
std::bind(skill_status_change_timer, ph::_1, ph::_2,
bl->id, type));
}
}
else
- sc_data[type].timer = add_timer(tick + std::chrono::seconds(2),
+ sc_data[type].timer = Timer(tick + std::chrono::seconds(2),
std::bind(skill_status_change_timer, ph::_1, ph::_2,
bl->id, type));
break;
@@ -956,10 +948,9 @@ void skill_status_change_timer(TimerData *tid, tick_t tick, int id, StatusChange
case StatusChange::SC_WEIGHT90:
case StatusChange::SC_BROKNWEAPON:
case StatusChange::SC_BROKNARMOR:
- if (sc_data[type].timer == tid)
- sc_data[type].timer = add_timer(tick + std::chrono::minutes(10),
- std::bind(skill_status_change_timer, ph::_1, ph::_2,
- bl->id, type));
+ sc_data[type].timer = Timer(tick + std::chrono::minutes(10),
+ std::bind(skill_status_change_timer, ph::_1, ph::_2,
+ bl->id, type));
return;
case StatusChange::SC_FLYING_BACKPACK:
@@ -1047,8 +1038,7 @@ int skill_status_effect(struct block_list *bl, StatusChange type,
/* 継ぎ足しができない状態異常である時は状態異常を行わない */
{
(*sc_count)--;
- delete_timer(sc_data[type].timer);
- sc_data[type].timer = nullptr;
+ sc_data[type].timer.cancel();
}
}
@@ -1145,7 +1135,7 @@ int skill_status_effect(struct block_list *bl, StatusChange type,
sc_data[type].spell_invocation = spell_invocation;
/* タイマー設定 */
- sc_data[type].timer = add_timer(gettick() + tick,
+ sc_data[type].timer = Timer(gettick() + tick,
std::bind(skill_status_change_timer, ph::_1, ph::_2,
bl->id, type));