Skip to content

Commit

Permalink
Added Time_queue::add and Time_queue::remove overloads that accept
Browse files Browse the repository at this point in the history
a 'void *' userdata, which is converted under the hood to uintptr.
Also added a Time_queue::add overload without a user data parameter
to handle the cases where userdata == 0.
  • Loading branch information
marzojr committed Nov 17, 2018
1 parent e64309a commit 8ddc2fc
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 47 deletions.
17 changes: 6 additions & 11 deletions actors.cc
Expand Up @@ -1282,8 +1282,7 @@ void Actor::start(
if (delay)
gwin->get_tqueue()->remove(this);
uint32 curtime = Game::get_ticks();
gwin->get_tqueue()->add(curtime + delay, this,
reinterpret_cast<uintptr>(gwin));
gwin->get_tqueue()->add(curtime + delay, this, gwin);
}
}

Expand Down Expand Up @@ -2481,8 +2480,7 @@ void Clear_casting::handle_event(unsigned long curtime, uintptr udata) {

void Actor::end_casting_mode(int delay) {
Clear_casting *c = new Clear_casting();
gwin->get_tqueue()->add(Game::get_ticks() + 2 * delay, c,
reinterpret_cast<uintptr>(this));
gwin->get_tqueue()->add(Game::get_ticks() + 2 * delay, c, this);
}

/*
Expand Down Expand Up @@ -2680,8 +2678,7 @@ int Actor::apply_damage(
hit = true;
add_dirty();
Clear_hit *c = new Clear_hit();
gwin->get_tqueue()->add(Game::get_ticks() + 200, c,
reinterpret_cast<uintptr>(this));
gwin->get_tqueue()->add(Game::get_ticks() + 200, c, this);
// Attack back.
fight_back(attacker);
return 0; // No damage == no powers (usually).
Expand Down Expand Up @@ -2747,8 +2744,7 @@ int Actor::reduce_health(
hit = true; // Flash red outline.
add_dirty();
Clear_hit *c = new Clear_hit();
gwin->get_tqueue()->add(Game::get_ticks() + 200, c,
reinterpret_cast<uintptr>(this));
gwin->get_tqueue()->add(Game::get_ticks() + 200, c, this);
}
if (oldhp >= maxhp / 2 && val < maxhp / 2 && rand() % 2 != 0) {
// A little oomph.
Expand Down Expand Up @@ -4689,8 +4685,7 @@ bool Actor::quake_on_walk(
if (get_info().quake_on_walk()) {
qsteps = (qsteps + 1) % 5;
if (!qsteps) // Time to roll?
gwin->get_tqueue()->add(Game::get_ticks() + 10,
new Earthquake(2), 0L);
gwin->get_tqueue()->add(Game::get_ticks() + 10, new Earthquake(2));
return true;
}
return false;
Expand Down Expand Up @@ -4935,7 +4930,7 @@ void Npc_actor::paint(
// Force schedule->now_what() in .5secs
// DO NOT call now_what here!!!
uint32 curtime = Game::get_ticks();
gwin->get_tqueue()->add(curtime + 500, this, reinterpret_cast<uintptr>(gwin));
gwin->get_tqueue()->add(curtime + 500, this, gwin);
}
if (!nearby) // Make sure we're in 'nearby' list.
gwin->add_nearby_npc(this);
Expand Down
19 changes: 8 additions & 11 deletions effects.cc
Expand Up @@ -342,7 +342,7 @@ Sprites_effect::Sprites_effect(
Game_window *gwin = Game_window::get_instance();
frames = sprite.get_num_frames();
// Start.
gwin->get_tqueue()->add(Game::get_ticks() + delay, this, 0L);
gwin->get_tqueue()->add(Game::get_ticks() + delay, this);
}

/**
Expand All @@ -362,7 +362,7 @@ Sprites_effect::Sprites_effect(
Game_window *gwin = Game_window::get_instance();
frames = sprite.get_num_frames();
// Start immediately.
gwin->get_tqueue()->add(Game::get_ticks(), this, 0L);
gwin->get_tqueue()->add(Game::get_ticks(), this);
}

/**
Expand Down Expand Up @@ -859,7 +859,7 @@ Homing_projectile::Homing_projectile( // A better name is welcome...
// Go for 20 seconds.
stop_time = Game::get_ticks() + 20 * 1000;
// Start immediately.
gwin->get_tqueue()->add(Game::get_ticks(), this, 0L);
gwin->get_tqueue()->add(Game::get_ticks(), this);
channel = Audio::get_ptr()->play_sound_effect(sfx, pos, -1);
}

Expand Down Expand Up @@ -1029,7 +1029,7 @@ void Text_effect::init(
height = 8 + sman->get_text_height(0);
add_dirty(); // Force first paint.
// Start immediately.
gwin->get_tqueue()->add(Game::get_ticks(), this, 0L);
gwin->get_tqueue()->add(Game::get_ticks(), this);
if (msg[0] == '@')
msg[0] = '"';
int len = msg.size();
Expand Down Expand Up @@ -1076,8 +1076,7 @@ void Text_effect::handle_event(
return;
}
// Back into queue.
gwin->get_tqueue()->add(Game::get_ticks() + gwin->get_std_delay(),
this, 0L);
gwin->get_tqueue()->add(Game::get_ticks() + gwin->get_std_delay(), this);

update_dirty();
}
Expand Down Expand Up @@ -1127,7 +1126,7 @@ Weather_effect::Weather_effect(
eggloc = Tile_coord(-1, -1, -1);
stop_time = Game::get_ticks() + delay + duration * gwin->get_std_delay() * ticks_per_minute;
// Start immediately.
gwin->get_tqueue()->add(Game::get_ticks() + delay, this, 0L);
gwin->get_tqueue()->add(Game::get_ticks() + delay, this);
}

/**
Expand Down Expand Up @@ -1761,8 +1760,7 @@ Fire_field_effect::Fire_field_effect(
field = gmap->create_ireg_object(895, 0);
field->set_flag(Obj_flags::is_temporary);
field->move(t.tx, t.ty, t.tz);
gwin->get_tqueue()->add(Game::get_ticks() + 3000 + rand() % 2000, this,
0L);
gwin->get_tqueue()->add(Game::get_ticks() + 3000 + rand() % 2000, this);
}

/**
Expand All @@ -1785,8 +1783,7 @@ void Fire_field_effect::handle_event(
frnum--;
gwin->add_dirty(field);
field->set_frame(frnum);
gwin->get_tqueue()->add(curtime + gwin->get_std_delay(),
this, udata);
gwin->get_tqueue()->add(curtime + gwin->get_std_delay(), this, udata);
}
}

7 changes: 3 additions & 4 deletions gamewin.cc
Expand Up @@ -131,7 +131,7 @@ class Background_noise : public Time_sensitive {
public:
Background_noise(Game_window *gw) : repeats(0), last_sound(-1),
gwin(gw), laststate(Invalid) {
gwin->get_tqueue()->add(5000, this, 0L);
gwin->get_tqueue()->add(5000, this);
}
virtual ~Background_noise() {
gwin->get_tqueue()->remove(this);
Expand Down Expand Up @@ -626,7 +626,7 @@ void Game_window::init_files(bool cycle) {
unsigned long timer = SDL_GetTicks();
srand(timer); // Use time to seed rand. generator.
// Force clock to start.
tqueue->add(timer, clock, reinterpret_cast<uintptr>(this));
tqueue->add(timer, clock, this);
// Go to starting chunk
scrolltx_lp = scrolltx_l = scrolltx = game->get_start_tile_x();
scrollty_lp = scrollty_l = scrollty = game->get_start_tile_y();
Expand Down Expand Up @@ -1465,8 +1465,7 @@ void Game_window::write_gwin(
void Game_window::read_gwin(
) {
if (!clock->in_queue()) // Be sure clock is running.
tqueue->add(Game::get_ticks(), clock,
reinterpret_cast<uintptr>(this));
tqueue->add(Game::get_ticks(), clock, this);
ifstream gin_stream;
try {
U7open(gin_stream, GWINDAT); // Gamewin.dat.
Expand Down
4 changes: 2 additions & 2 deletions npcnear.cc
Expand Up @@ -57,7 +57,7 @@ void Npc_proximity_handler::add(
msecs = (rand() % 4000) + 2000;
unsigned long newtime = curtime + msecs;
newtime += 1000 * additional_secs;
gwin->get_tqueue()->add(newtime, this, reinterpret_cast<uintptr>(npc));
gwin->get_tqueue()->add(newtime, this, npc);
}

/*
Expand All @@ -68,7 +68,7 @@ void Npc_proximity_handler::remove(
Npc_actor *npc
) {
npc->clear_nearby();
gwin->get_tqueue()->remove(this, reinterpret_cast<uintptr>(npc));
gwin->get_tqueue()->remove(this, npc);
}

/*
Expand Down
14 changes: 7 additions & 7 deletions npctime.cc
Expand Up @@ -271,7 +271,7 @@ Npc_timer::Npc_timer(
Npc_timer_list *l,
int start_delay // Time in msecs. before starting.
) : list(l) {
gwin->get_tqueue()->add(Game::get_ticks() + start_delay, this, 0L);
gwin->get_tqueue()->add(Game::get_ticks() + start_delay, this);
}

/*
Expand Down Expand Up @@ -325,7 +325,7 @@ void Npc_hunger_timer::handle_event(
}
last_time = minute;
}
gwin->get_tqueue()->add(curtime + 5000, this, 0L);
gwin->get_tqueue()->add(curtime + 5000, this);
}

/*
Expand Down Expand Up @@ -371,7 +371,7 @@ void Npc_poison_timer::handle_event(
// npc->set_property(static_cast<int>(Actor::health),
// npc->get_property(static_cast<int>(Actor::health)) - penalty);
// Check again in 10-20 secs.
gwin->get_tqueue()->add(curtime + 10000 + rand() % 10000, this, 0L);
gwin->get_tqueue()->add(curtime + 10000 + rand() % 10000, this);
}

/*
Expand Down Expand Up @@ -418,7 +418,7 @@ void Npc_sleep_timer::handle_event(
return;
}
// Check again every half a game minute.
gwin->get_tqueue()->add(curtime + (ticks_per_minute * gwin->get_std_delay()) / 2, this, 0L);
gwin->get_tqueue()->add(curtime + (ticks_per_minute * gwin->get_std_delay()) / 2, this);
}

/*
Expand Down Expand Up @@ -466,7 +466,7 @@ void Npc_invisibility_timer::handle_event(
return;
}
// Check again in 2 secs.
gwin->get_tqueue()->add(curtime + 2000, this, 0L);
gwin->get_tqueue()->add(curtime + 2000, this);
}

/*
Expand All @@ -493,7 +493,7 @@ void Npc_protection_timer::handle_event(
return;
}
// Check again in 2 secs.
gwin->get_tqueue()->add(curtime + 2000, this, 0L);
gwin->get_tqueue()->add(curtime + 2000, this);
}

/*
Expand All @@ -511,6 +511,6 @@ void Npc_flag_timer::handle_event(
npc->clear_flag(flag);
delete this;
} else // Check again in 10 secs.
gwin->get_tqueue()->add(curtime + 10000, this, 0L);
gwin->get_tqueue()->add(curtime + 10000, this);
}

6 changes: 2 additions & 4 deletions objs/animate.cc
Expand Up @@ -80,8 +80,7 @@ void Object_sfx::Play(Game_object *obj, int sfx, int delay) {
osfx->channel = Audio::get_ptr()->play_sound_effect(sfx, osfx->last_pos, volume, 0);
delay = 100;
}
gwin->get_tqueue()->add(Game::get_ticks() + delay, osfx,
reinterpret_cast<uintptr>(gwin));
gwin->get_tqueue()->add(Game::get_ticks() + delay, osfx, gwin);
}

Object_sfx::Object_sfx(Game_object *o, int s)
Expand Down Expand Up @@ -304,8 +303,7 @@ void Animator::start_animation(
) {
// Clean out old entry if there.
gwin->get_tqueue()->remove(this);
gwin->get_tqueue()->add(Game::get_ticks() + 20, this,
reinterpret_cast<uintptr>(gwin));
gwin->get_tqueue()->add(Game::get_ticks() + 20, this, gwin);
animating = 1;
}

Expand Down
2 changes: 1 addition & 1 deletion objs/barge.cc
Expand Up @@ -412,7 +412,7 @@ void Barge_object::travel_to_tile(
if (!ice_raft) // Ice-raft doesn't rotate.
face_direction(ndir);
if (!in_queue()) // Not already in queue?
gwin->get_tqueue()->add(Game::get_ticks(), this, 0L);
gwin->get_tqueue()->add(Game::get_ticks(), this);
} else
frame_time = 0; // Not moving.
}
Expand Down
4 changes: 2 additions & 2 deletions objs/egg.cc
Expand Up @@ -320,7 +320,7 @@ class Missile_egg : public Egg_object {
// Make sure launcher is active.
if (launcher && !launcher->in_queue() &&
(criteria == party_near || criteria == avatar_near))
gwin->get_tqueue()->add(0L, launcher, 0);
gwin->get_tqueue()->add(0L, launcher);
Egg_object::paint();
}
virtual void set(int crit, int dist) {
Expand All @@ -344,7 +344,7 @@ class Missile_egg : public Egg_object {
launcher = new Missile_launcher(this, weapon,
proj, dir, gwin->get_std_delay()*delay);
if (!launcher->in_queue())
gwin->get_tqueue()->add(0L, launcher, 0);
gwin->get_tqueue()->add(0L, launcher);
}
};

Expand Down
9 changes: 9 additions & 0 deletions tqueue.h
Expand Up @@ -94,9 +94,18 @@ class Time_queue {
{ }
void clear(); // Remove all entries.
// Add an entry.
void add(uint32 t, Time_sensitive *obj) {
add(t, obj, static_cast<uintptr>(0));
}
void add(uint32 t, Time_sensitive *obj, void *ud) {
add(t, obj, reinterpret_cast<uintptr>(ud));
}
void add(uint32 t, Time_sensitive *obj, uintptr ud);
// Remove object's entry.
int remove(Time_sensitive *obj);
void remove(Time_sensitive *obj, void *ud) {
remove(obj, reinterpret_cast<uintptr>(ud));
}
int remove(Time_sensitive *obj, uintptr udata);
int find(Time_sensitive const *obj) const; // Find an entry.
// Find delay when obj. is due.
Expand Down
3 changes: 1 addition & 2 deletions usecode/intrinsics.cc
Expand Up @@ -1755,8 +1755,7 @@ USECODE_INTRINSIC(get_barge) {
USECODE_INTRINSIC(earthquake) {
ignore_unused_variable_warning(num_parms);
int len = parms[0].get_int_value();
gwin->get_tqueue()->add(Game::get_ticks() + 10,
new Earthquake(len), reinterpret_cast<uintptr>(this));
gwin->get_tqueue()->add(Game::get_ticks() + 10, new Earthquake(len), this);
return(no_ret);
}

Expand Down
5 changes: 2 additions & 3 deletions usecode/ucsched.cc
Expand Up @@ -139,9 +139,8 @@ void Usecode_script::start(
first = this;
started = true;
//++++ Messes up Moonshade Trial.
// gwin->get_tqueue()->add(d + Game::get_ticks(), this,
gwin->get_tqueue()->add(d + SDL_GetTicks(), this,
reinterpret_cast<uintptr>(gwin->get_usecode()));
// gwin->get_tqueue()->add(d + Game::get_ticks(), this, gwin->get_usecode());
gwin->get_tqueue()->add(d + SDL_GetTicks(), this, gwin->get_usecode());
}

/*
Expand Down

0 comments on commit 8ddc2fc

Please sign in to comment.