From ac04faaceacb58415904bc0ffccd310754b5f21c Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 15 Apr 2014 21:08:45 +0100 Subject: [PATCH] [Change] Removed all non dt timers from stuff that should be dt based. --- src/ai.c | 10 +++++----- src/pause.c | 53 ++-------------------------------------------------- src/pilot.c | 52 +++++++++++++++++++++++++-------------------------- src/pilot.h | 26 +++++++++++++------------- src/player.c | 8 +++----- src/space.c | 16 ++++++---------- 6 files changed, 55 insertions(+), 110 deletions(-) diff --git a/src/ai.c b/src/ai.c index a4ca665..0b779a6 100644 --- a/src/ai.c +++ b/src/ai.c @@ -527,10 +527,10 @@ void ai_think(Pilot* pilot) { cur_pilot->target = cur_pilot->id; /* Control function if pilot is idle or tick is up. */ - if((cur_pilot->tcontrol < SDL_GetTicks()) || (cur_pilot->task == NULL)) { + if((cur_pilot->tcontrol < 0.) || (cur_pilot->task == NULL)) { ai_run(L, "control"); /* Run control. */ lua_getglobal(L, "control_rate"); - cur_pilot->tcontrol = SDL_GetTicks() + (int)(1000.*lua_tonumber(L, -1)); + cur_pilot->tcontrol = lua_tonumber(L, -1); } if(cur_pilot->task) /* Pilot has a currently running task. */ @@ -1492,8 +1492,8 @@ static int ai_settimer(lua_State* L) { int n; /* Get the timer. */ if(lua_isnumber(L, 1)) n = lua_tonumber(L,1); - cur_pilot->timer[n] = (lua_isnumber(L,2)) ? - lua_tonumber(L,2) + SDL_GetTicks() : 0; + cur_pilot->timer[n] = (lua_isnumber(L,2)) ? lua_tonumber(L,2)/1000. : 0; + return 0; } @@ -1504,7 +1504,7 @@ static int ai_timeup(lua_State* L) { int n; /* Get the timer. */ if(lua_isnumber(L,1)) n = lua_tonumber(L,1); - lua_pushboolean(L, cur_pilot->timer[n] < SDL_GetTicks()); + lua_pushboolean(L, cur_pilot->timer[n] < 0.); return 1; } diff --git a/src/pause.c b/src/pause.c index 75b42e5..da39ace 100644 --- a/src/pause.c +++ b/src/pause.c @@ -15,36 +15,25 @@ int paused = 0; /* Are we paused. */ double dt_mod = 1.; /**< dt modifier. */ -/* From pilot.c */ -extern Pilot** pilot_stack; -extern int pilot_nstack; /* From main.c */ -extern unsigned int gtime; - -static void pilot_pause(void); -static void pilot_unpause(void); -static void pilot_delay(unsigned int delay); +extern unsigned int time; /* Pause the game. */ void pause_game(void) { if(paused) return; /* Well well.. We are paused already. */ - pilot_pause(); - paused = 1; /* We should unpause it. */ } void unpause_game(void) { if(!paused) return; /* We are unpaused already. */ - pilot_unpause(); - paused = 0; } /* Set the timers back. */ void pause_delay(unsigned int delay) { - pilot_delay(delay); + (void)delay; } /** @@ -54,41 +43,3 @@ void pause_setSpeed(double mod) { dt_mod = mod; } -/* Pilots pausing/unpausing. */ -static void pilot_pause(void) { - int i, j; - unsigned int t = SDL_GetTicks(); - for(i = 0; i < pilot_nstack; i++) { - pilot_stack[i]->ptimer -= t; - - /* Pause timers. */ - pilot_stack[i]->tcontrol -= t; - for(j = 0; j < MAX_AI_TIMERS; j++) - pilot_stack[i]->timer[j] -= t; - } -} - -static void pilot_unpause(void) { - int i, j; - unsigned int t = SDL_GetTicks(); - for(i = 0; i < pilot_nstack; i++) { - pilot_stack[i]->ptimer += t; - - /* Rerun timers. */ - pilot_stack[i]->tcontrol += t; - for(j = 0; j < MAX_AI_TIMERS; j++) - pilot_stack[i]->timer[j] += t; - } -} - -static void pilot_delay(unsigned int delay) { - int i, j; - for(i = 0; i < pilot_nstack; i++) { - pilot_stack[i]->ptimer += delay; - - pilot_stack[i]->tcontrol += delay; - for(j = 0; j < MAX_AI_TIMERS; j++) - pilot_stack[i]->timer[j] += delay; - } -} - diff --git a/src/pilot.c b/src/pilot.c index 82e2ec0..6248331 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -481,11 +481,12 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, */ void pilot_dead(Pilot* p) { if(pilot_isFlag(p, PILOT_DEAD)) return; /* She's already dead. */ + /* Basically just set the timers.. */ if(p->id == PLAYER_ID) player_dead(); - p->timer[0] = SDL_GetTicks(); /* No need for AI anymore. */ - p->ptimer = p->timer[0] + 1000 + (unsigned int)sqrt(10*p->armour_max*p->shield_max); - p->timer[1] = p->timer[0]; /* Explosion timer. */ + p->timer[0] = 0.; /* No need for AI anymore. */ + p->ptimer = 1. + sqrt(10*p->armour_max * p->shield_max) / 1000.; + p->timer[1] = 0.; /* Explosion timer. */ /* Flag cleanup - fixes some issues. */ if(pilot_isFlag(p, PILOT_HYP_PREP)) pilot_rmFlag(p, PILOT_HYP_PREP); @@ -709,16 +710,25 @@ void pilot_render(Pilot* p) { */ static void pilot_update(Pilot* pilot, const double dt) { int i; - unsigned int t, l; + unsigned int l; double a, px, py, vx, vy; char buf[16]; PilotOutfit* o; - /* She's dead D: */ - if(pilot_isFlag(pilot, PILOT_DEAD)) { - t = SDL_GetTicks(); + /* Update timers. */ + pilot->ptimer -= dt; + pilot->tcontrol -= dt; + for(i = 0; i < MAX_AI_TIMERS; i++) + pilot->timer[i] -= dt; + for(i = 0; i < pilot->noutfits; i++) { + o = &pilot->outfits[i]; + if(o->timer > 0.) + o->timer -= dt; + } - if(t > pilot->ptimer) { /* Completely destroyed with final explosion. */ + /* She's dead. */ + if(pilot_isFlag(pilot, PILOT_DEAD)) { + if(pilot->ptimer < 0.) { /* Completely destroyed with final explosion. */ if(pilot->id == PLAYER_ID) /* Player handled differently. */ player_destroyed(); pilot_setFlag(pilot, PILOT_DELETE); /* It'll get deleted next frame. */ @@ -726,7 +736,7 @@ static void pilot_update(Pilot* pilot, const double dt) { } /* Pilot death sound. */ - if(!pilot_isFlag(pilot, PILOT_DEATH_SOUND) && (t > pilot->ptimer - 50)) { + if(!pilot_isFlag(pilot, PILOT_DEATH_SOUND) && (pilot->ptimer < 0.050)) { /* Play random explosion sound. */ snprintf(buf, 16, "explosion%d", RNG(0,2)); sound_playPos(sound_get(buf), pilot->solid->pos.x, pilot->solid->pos.y); @@ -735,7 +745,7 @@ static void pilot_update(Pilot* pilot, const double dt) { } /* Final explosion. */ - else if(!pilot_isFlag(pilot, PILOT_EXPLODED) && (t > pilot->ptimer - 200)) { + else if(!pilot_isFlag(pilot, PILOT_EXPLODED) && (pilot->ptimer < 0.200)) { /* Damage from explosion. */ a = sqrt(pilot->solid->mass); expl_explode(pilot->solid->pos.x, pilot->solid->pos.y, @@ -751,10 +761,8 @@ static void pilot_update(Pilot* pilot, const double dt) { pilot->commodities[i].quantity); } /* Reset random explosion time. */ - else if(t > pilot->timer[1]) { - pilot->timer[1] = t + - (unsigned int)(100*(double)(pilot->ptimer - pilot->timer[1]) / - (double)(pilot->ptimer - pilot->timer[0])); + else if(pilot->timer[1] < 0.) { + pilot->timer[1] = 0.08 * (pilot->ptimer - pilot->timer[1]) / pilot->ptimer; /* Random position on ship. */ a = RNGF()*2.*M_PI; @@ -832,13 +840,6 @@ static void pilot_update(Pilot* pilot, const double dt) { } else /* Normal limit. */ limit_speed(&pilot->solid->vel, pilot->speed, dt); } - - /* Update outfits. */ - for(i = 0; i < pilot->noutfits; i++) { - o = &pilot->outfits[i]; - if(o->timer > 0.) - o->timer -= dt; - } } /* Pilot is getting ready or is in, hyperspace. */ @@ -849,7 +850,7 @@ static void pilot_hyperspace(Pilot* p) { if(pilot_isFlag(p, PILOT_HYPERSPACE)) { /* Has the jump happened? */ - if(SDL_GetTicks() > p->ptimer) { + if(p->ptimer < 0.) { if(p == player) { player_brokeHyperspace(); } else { @@ -863,9 +864,8 @@ static void pilot_hyperspace(Pilot* p) { } /* Engines getting ready for the jump. */ else if(pilot_isFlag(p, PILOT_HYP_BEGIN)) { - if(SDL_GetTicks() > p->ptimer) { - /* Engines are ready. */ - p->ptimer = SDL_GetTicks() + HYPERSPACE_FLY_DELAY; + if(p->ptimer < 0.) { /* Engines ready. */ + p->ptimer = HYPERSPACE_FLY_DELAY; pilot_setFlag(p, PILOT_HYPERSPACE); } } else { @@ -888,7 +888,7 @@ static void pilot_hyperspace(Pilot* p) { if(ABS(diff) < MAX_DIR_ERR) { /* We should prepare for the jump now. */ p->solid->dir_vel = 0.; - p->ptimer = SDL_GetTicks() + HYPERSPACE_ENGINE_DELAY; + p->ptimer = HYPERSPACE_ENGINE_DELAY; pilot_setFlag(p, PILOT_HYP_BEGIN); } } diff --git a/src/pilot.h b/src/pilot.h index b305504..f5c6ba9 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -7,17 +7,17 @@ #include "sound.h" #include "economy.h" -#define PLAYER_ID 1 /**< Player pilot ID. */ +#define PLAYER_ID 1 /**< Player pilot ID. */ /* Hyperspace parameters. */ -#define HYPERSPACE_ENGINE_DELAY 3000 /**< Warm up the engines. */ -#define HYPERSPACE_FLY_DELAY 5000 /**< Time taken to hyperspace. */ -#define HYPERSPACE_STARS_BLUR 2000 /**< Time stars blur. */ +#define HYPERSPACE_ENGINE_DELAY 3. /**< Warm up the engines. */ +#define HYPERSPACE_FLY_DELAY 5. /**< Time taken to hyperspace. */ +#define HYPERSPACE_STARS_BLUR 2. /**< Time stars blur. */ #define HYPERSPACE_STARS_LENGTH 1000 /**< Length the stars blur to at max. */ -#define HYPERSPACE_FADEOUT 1000 /**< Time fadeout. */ +#define HYPERSPACE_FADEOUT 1. /**< Time fadeout. */ #define HYPERSPACE_FUEL 100 /**< Amount of fuel taken. */ #define HYPERSPACE_THRUST 2000. /**< How much thrust you use in hyperspace. */ -#define HYPERSPACE_VEL HYPERSPACE_THRUST*(HYPERSPACE_FLY_DELAY/1000) /**< Vel at hyp.*/ +#define HYPERSPACE_VEL HYPERSPACE_THRUST*HYPERSPACE_FLY_DELAY /**< Vel at hyp.*/ #define HYPERSPACE_ENTER_MIN HYPERSPACE_VEL*0.5 /**< Min entering distance. */ #define HYPERSPACE_ENTER_MAX HYPERSPACE_VEL*0.6 /**< Max entering distance. */ #define HYPERSPACE_EXIT_MIN 1500. /**< Min distance to begin jumping. */ @@ -165,7 +165,7 @@ typedef struct Pilot_ { /* Misc. */ uint32_t flags; /**< Used for AI etc. */ - unsigned int ptimer; /**< Generic timer for internal pilot use. */ + double ptimer; /**< Generic timer for internal pilot use. */ int lockons; /**< Stores how many seeking weapons are targetting pilot. */ /* Hook attached to the pilot. */ @@ -174,15 +174,15 @@ typedef struct Pilot_ { /* Escort stuff. */ unsigned int parent; /**< Pilots parent. */ - unsigned int* escorts; /**< Pilots escorts. */ + unsigned int* escorts; /**< Pilots escorts. */ int nescorts; /**< Number of pilot escorts. */ /* AI. */ - unsigned int target; /**< AI target. */ - AI_Profile* ai; /**< Ai personality profile. */ - unsigned int tcontrol; /**< Timer for control tick. */ - unsigned int timer[MAX_AI_TIMERS]; /**< Timers for AI. */ - Task* task; /**< Current action. */ + unsigned int target; /**< AI target. */ + AI_Profile* ai; /**< Ai personality profile. */ + double tcontrol; /**< Timer for control tick. */ + double timer[MAX_AI_TIMERS]; /**< Timers for AI. */ + Task* task; /**< Current action. */ } Pilot; /** diff --git a/src/player.c b/src/player.c index 4949358..d196c73 100644 --- a/src/player.c +++ b/src/player.c @@ -1181,12 +1181,10 @@ void player_renderGUI(double dt) { } } /* Hyperspace FLASH BANG!!! */ - if(pilot_isFlag(player, PILOT_HYPERSPACE)) { - i = (int)player->ptimer - HYPERSPACE_FADEOUT; - if(paused) i += t; - j = (int) t; + if(pilot_isFlag(player, PILOT_HYPERSPACE) && + (player->ptimer < HYPERSPACE_FADEOUT)) { if(i < j) { - x = (double)(j-i) / HYPERSPACE_FADEOUT; + x = (HYPERSPACE_FADEOUT-player->ptimer) / HYPERSPACE_FADEOUT; glColor4d(1.,1.,1., x); /* We'll | I'll, make this more effiecent later. */ glBegin(GL_QUADS); glVertex2d(-SCREEN_W/2., -SCREEN_H/2.); diff --git a/src/space.c b/src/space.c index 5e60b67..6085ffb 100644 --- a/src/space.c +++ b/src/space.c @@ -1211,21 +1211,15 @@ void space_renderOverlay(const double dt) { /* Render stars. */ static void space_renderStars(const double dt) { int i; - unsigned int t, timer; double x, y, m, b; glMatrixMode(GL_MODELVIEW); glPushMatrix(); /* Translation matrix. */ glTranslated(-(double)SCREEN_W/2., -(double)SCREEN_H/2., 0); - t = SDL_GetTicks(); if(!player_isFlag(PLAYER_DESTROYED) && !player_isFlag(PLAYER_CREATING) && pilot_isFlag(player, PILOT_HYPERSPACE) && /* Hyperspace fancy effect. */ - ((!paused && (player->ptimer - HYPERSPACE_STARS_BLUR < t)) || - (paused && (player->ptimer < HYPERSPACE_STARS_BLUR)))) { - - timer = player->ptimer - HYPERSPACE_STARS_BLUR; - if(paused) timer += t; + (player->ptimer < HYPERSPACE_STARS_BLUR)) { /* Fancy hyperspace effects. */ glShadeModel(GL_SMOOTH); @@ -1237,9 +1231,11 @@ static void space_renderStars(const double dt) { glBegin(GL_LINES); /* Lines will be based on velocity. */ - m = HYPERSPACE_STARS_LENGTH * (double)(t-timer) / (HYPERSPACE_STARS_BLUR); - x = m*cos(VANGLE(player->solid->vel)+M_PI); - y = m*sin(VANGLE(player->solid->vel)+M_PI); + m = HYPERSPACE_STARS_BLUR-player->ptimer; + m /= HYPERSPACE_STARS_BLUR; + m *= HYPERSPACE_STARS_LENGTH; + x = m*cos(VANGLE(player->solid->vel)+M_PI); + y = m*sin(VANGLE(player->solid->vel)+M_PI); for(i = 0; i < nstars; i++) { glColor4d(1., 1., 1., stars[i].brightness);