diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dungeon.cc | 17 | ||||
-rw-r--r-- | src/game.hpp | 6 | ||||
-rw-r--r-- | src/loadsave.cc | 10 | ||||
-rw-r--r-- | src/lua_bind.cc | 2 | ||||
-rw-r--r-- | src/spells3.cc | 7 | ||||
-rw-r--r-- | src/timer_type.hpp | 73 | ||||
-rw-r--r-- | src/util.cc | 15 | ||||
-rw-r--r-- | src/variable.cc | 6 | ||||
-rw-r--r-- | src/variable.hpp | 1 |
9 files changed, 89 insertions, 48 deletions
diff --git a/src/dungeon.cc b/src/dungeon.cc index e7bdf34b..f8671387 100644 --- a/src/dungeon.cc +++ b/src/dungeon.cc @@ -1225,8 +1225,7 @@ static void process_world() auto const &d_info = game->edit_data.d_info; auto const &r_info = game->edit_data.r_info; auto const &f_info = game->edit_data.f_info; - - timer_type *t_ptr; + auto &timers = game->timers; int x, y, i, j; @@ -1269,18 +1268,10 @@ static void process_world() check_music(); } - /* Handle the timers */ - for (t_ptr = gl_timers; t_ptr != NULL; t_ptr = t_ptr->next) + /* Process timers */ + for (auto &&timer: timers) { - if (!t_ptr->enabled) continue; - - t_ptr->countdown--; - if (!t_ptr->countdown) - { - t_ptr->countdown = t_ptr->delay; - assert(t_ptr->callback != NULL); - t_ptr->callback(); - } + timer->count_down(); } /* Check the fate */ diff --git a/src/game.hpp b/src/game.hpp index 4421d85d..9acfc471 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -11,6 +11,7 @@ #include "player_defs.hpp" #include "random_artifact.hpp" #include "skill_type.hpp" +#include "timer_type_fwd.hpp" #include "wilderness_map.hpp" #include <boost/circular_buffer.hpp> @@ -77,4 +78,9 @@ struct Game { */ std::vector<skill_type> s_info; + /** + * Timers + */ + std::vector<timer_type *> timers; + }; diff --git a/src/loadsave.cc b/src/loadsave.cc index e98b832c..3d45bf0f 100644 --- a/src/loadsave.cc +++ b/src/loadsave.cc @@ -1838,13 +1838,11 @@ bool_ load_dungeon(char *ext) */ static void do_timers(ls_flag_t flag) { - timer_type *t_ptr; - - for (t_ptr = gl_timers; t_ptr != NULL; t_ptr = t_ptr->next) + for (auto &&t_ptr: game->timers) { - do_bool(&t_ptr->enabled, flag); - do_s32b(&t_ptr->delay, flag); - do_s32b(&t_ptr->countdown, flag); + do_std_bool(&t_ptr->m_enabled, flag); + do_s32b(&t_ptr->m_delay, flag); + do_s32b(&t_ptr->m_countdown, flag); } } diff --git a/src/lua_bind.cc b/src/lua_bind.cc index c2f924ad..3ca43716 100644 --- a/src/lua_bind.cc +++ b/src/lua_bind.cc @@ -249,7 +249,7 @@ timer_type *TIMER_AGGRAVATE_EVIL = 0; void timer_aggravate_evil_enable() { - TIMER_AGGRAVATE_EVIL->enabled = TRUE; + TIMER_AGGRAVATE_EVIL->enable(); } void timer_aggravate_evil_callback() diff --git a/src/spells3.cc b/src/spells3.cc index 113decf9..58d42cab 100644 --- a/src/spells3.cc +++ b/src/spells3.cc @@ -2447,7 +2447,7 @@ static void stop_inertia_controlled_spell() assert(TIMER_INERTIA_CONTROL != NULL); p_ptr->inertia_controlled_spell = -1; - TIMER_INERTIA_CONTROL->enabled = FALSE; + TIMER_INERTIA_CONTROL->disable(); p_ptr->update = p_ptr->update | PU_MANA; } @@ -2492,9 +2492,8 @@ casting_result meta_inertia_control() } p_ptr->inertia_controlled_spell = s; - TIMER_INERTIA_CONTROL->enabled = TRUE; - TIMER_INERTIA_CONTROL->delay = delay; - TIMER_INERTIA_CONTROL->countdown = delay; + TIMER_INERTIA_CONTROL->set_delay_and_reset(delay); + TIMER_INERTIA_CONTROL->enable(); p_ptr->update |= PU_MANA; msg_format("Inertia flow controlling spell %s.", spell_type_name(spell_at(s))); return CAST_OBVIOUS; diff --git a/src/timer_type.hpp b/src/timer_type.hpp index 0ce6b095..d682b3bd 100644 --- a/src/timer_type.hpp +++ b/src/timer_type.hpp @@ -2,17 +2,80 @@ #include "h-basic.h" +#include <functional> + /* * Timer descriptor and runtime data. */ struct timer_type { - timer_type *next; /* The next timer in the list */ +private: + std::function<void ()> m_callback; + +public: + // + // XXX Currently need public access for loading and saving. + // + bool m_enabled; + s32b m_delay = 0; + s32b m_countdown = 0; + +public: + /** + * Create a new timer + */ + timer_type(std::function<void()> callback, s32b delay) + : m_callback(callback) + , m_enabled(false) + , m_delay(delay) + , m_countdown(delay) + { + } + + timer_type(timer_type const &other) = delete; + timer_type &operator =(timer_type const &other) = delete; + + /** + * Enable the timer. + */ + void enable() + { + m_enabled = true; + } + + /** + * Disable the timer. + */ + void disable() + { + m_enabled = false; + } + + /** + * Change delay and reset. + */ + void set_delay_and_reset(s32b delay) + { + m_delay = delay; + m_countdown = delay; + } - bool_ enabled; /* Is it currently counting? */ + /** + * Count down. + */ + void count_down() + { + if (!m_enabled) + { + return; + } - s32b delay; /* Delay between activations */ - s32b countdown; /* The current number of turns passed, when it reaches delay it fires */ + m_countdown--; + if (m_countdown <= 0) + { + m_countdown = m_delay; + m_callback(); + } + } - void (*callback)(); /* The C function to call upon firing */ }; diff --git a/src/util.cc b/src/util.cc index fc8f4080..7d795828 100644 --- a/src/util.cc +++ b/src/util.cc @@ -3642,19 +3642,10 @@ char msg_box_auto(std::string const &text) */ timer_type *new_timer(void (*callback)(), s32b delay) { - timer_type *t_ptr = new timer_type(); - - static_assert(std::is_pod<timer_type>::value, "Cannot memset a non-POD type"); - memset(t_ptr, 0, sizeof(timer_type)); - - t_ptr->next = gl_timers; - gl_timers = t_ptr; - - t_ptr->callback = callback; - t_ptr->delay = delay; - t_ptr->countdown = delay; - t_ptr->enabled = FALSE; + auto &timers = game->timers; + timer_type *t_ptr = new timer_type(callback, delay); + timers.push_back(t_ptr); return t_ptr; } diff --git a/src/variable.cc b/src/variable.cc index 8a6a71c8..7734c26b 100644 --- a/src/variable.cc +++ b/src/variable.cc @@ -636,12 +636,6 @@ s32b DUNGEON_ASTRAL = 8; s32b DUNGEON_ASTRAL_WILD_X = 45; s32b DUNGEON_ASTRAL_WILD_Y = 19; -/* - * Timers - */ -timer_type *gl_timers = NULL; - - /** * Get the version string. */ diff --git a/src/variable.hpp b/src/variable.hpp index 734e7773..6dd45d0e 100644 --- a/src/variable.hpp +++ b/src/variable.hpp @@ -190,7 +190,6 @@ extern s32b DUNGEON_ASTRAL; extern s32b DUNGEON_ASTRAL_WILD_X; extern s32b DUNGEON_ASTRAL_WILD_Y; extern deity_type deity_info[MAX_GODS]; -extern timer_type *gl_timers; const char *get_version_string(); extern bool_ arg_wizard; extern bool_ arg_force_original; |