From 2b9dd4210f6a0478ef56c994668f32901b6e8617 Mon Sep 17 00:00:00 2001 From: Taylor Richards Date: Mon, 8 Feb 2010 08:09:23 +0000 Subject: [PATCH] myrand() fixes Original code made various assumptions about RAND_MAX being SHRT_MAX like on Windows. So switch to using MY_RAND_MAX for anything dealing with myrand() and also ensure that myrand() uses 0x7fff as its max value. --- include/pstypes.h | 1 + src/debris/debris.cpp | 10 +++++----- src/freespace2/freespace.cpp | 12 ++++++------ src/globalincs/systemvars.cpp | 4 ++++ src/math/floating.cpp | 2 +- src/menuui/mainhallmenu.cpp | 6 +++--- src/mission/missionparse.cpp | 8 ++++---- src/model/modelinterp.cpp | 4 ++-- src/network/multilag.cpp | 14 +++++++------- src/parse/sexp.cpp | 2 +- src/particle/particle.cpp | 2 +- src/physics/physics.cpp | 4 ++-- src/ship/shipfx.cpp | 4 ++-- src/starfield/starfield.cpp | 6 +++--- src/starfield/supernova.cpp | 4 ++-- 15 files changed, 44 insertions(+), 39 deletions(-) diff --git a/include/pstypes.h b/include/pstypes.h index 8aa1fd3..8731b6a 100644 --- a/include/pstypes.h +++ b/include/pstypes.h @@ -631,6 +631,7 @@ float SWAPFLOAT( float *x ) #define TRUE 1 #define FALSE 0 +#define MY_RAND_MAX 0x7fff int myrand(); diff --git a/src/debris/debris.cpp b/src/debris/debris.cpp index d2a19ec..804c725 100644 --- a/src/debris/debris.cpp +++ b/src/debris/debris.cpp @@ -624,7 +624,7 @@ void debris_process_post(object * obj, float frame_time) } else { // Maybe move a vertex.... 20% of the time maybe? int mr = myrand(); - if ( mr < RAND_MAX/5 ) { + if ( mr < MY_RAND_MAX/5 ) { vector v1, v2; submodel_get_two_random_points( db->model_num, db->submodel_num, &v1, &v2 ); db->arc_pts[i][mr % 2] = v1; @@ -719,12 +719,12 @@ object *debris_create(object *source_obj, int model_num, int submodel_num, vecto // Create Debris piece n! if ( hull_flag ) { - if (rand() < RAND_MAX/6) // Make some pieces blow up shortly after explosion. - db->lifeleft = 2.0f * ((float) myrand()/(float) RAND_MAX) + 0.5f; + if (myrand() < MY_RAND_MAX/6) // Make some pieces blow up shortly after explosion. + db->lifeleft = 2.0f * ((float) myrand()/(float) MY_RAND_MAX) + 0.5f; else db->lifeleft = -1.0f; // large hull pieces stay around forever } else { - db->lifeleft = (i2fl(myrand())/i2fl(RAND_MAX))*2.0f+0.1f; + db->lifeleft = (i2fl(myrand())/i2fl(MY_RAND_MAX))*2.0f+0.1f; } // increase lifetime for vaporized debris @@ -749,7 +749,7 @@ object *debris_create(object *source_obj, int model_num, int submodel_num, vecto if ( db->is_hull ) { // Only make 1/2 of the pieces have arcs - if ( myrand() < RAND_MAX*2/3 ) { + if ( myrand() < (MY_RAND_MAX/3)*2 ) { db->arc_frequency = 1000; } else { db->arc_frequency = 0; diff --git a/src/freespace2/freespace.cpp b/src/freespace2/freespace.cpp index 0c8f076..8a3163b 100644 --- a/src/freespace2/freespace.cpp +++ b/src/freespace2/freespace.cpp @@ -3599,8 +3599,8 @@ void apply_hud_shake(matrix *eye_orient) int r1 = myrand(); int r2 = myrand(); - tangles.p += 0.07f * (float) (r1-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/ABURN_DECAY_TIME)); - tangles.h += 0.07f * (float) (r2-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/ABURN_DECAY_TIME)); + tangles.p += 0.07f * (float) (r1-MY_RAND_MAX/2)/MY_RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/ABURN_DECAY_TIME)); + tangles.h += 0.07f * (float) (r2-MY_RAND_MAX/2)/MY_RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/ABURN_DECAY_TIME)); } // Make eye shake due to engine wash @@ -3608,8 +3608,8 @@ void apply_hud_shake(matrix *eye_orient) if (Player_obj->type == OBJ_SHIP && (Ships[Player_obj->instance].wash_intensity > 0) && Wash_on ) { int r1 = myrand(); int r2 = myrand(); - tangles.p += 0.07f * Ships[Player_obj->instance].wash_intensity * (float) (r1-RAND_MAX/2)/RAND_MAX; - tangles.h += 0.07f * Ships[Player_obj->instance].wash_intensity * (float) (r2-RAND_MAX/2)/RAND_MAX; + tangles.p += 0.07f * Ships[Player_obj->instance].wash_intensity * (float) (r1-MY_RAND_MAX/2)/MY_RAND_MAX; + tangles.h += 0.07f * Ships[Player_obj->instance].wash_intensity * (float) (r2-MY_RAND_MAX/2)/MY_RAND_MAX; // get the intensity float intensity = FF_SCALE * Ships[Player_obj->instance].wash_intensity; @@ -3637,8 +3637,8 @@ void apply_hud_shake(matrix *eye_orient) int r1 = myrand(); int r2 = myrand(); - tangles.p += (Game_shudder_intensity / 200.0f) * (float) (r1-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/(float)Game_shudder_total)); - tangles.h += (Game_shudder_intensity / 200.0f) * (float) (r2-RAND_MAX/2)/RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/(float)Game_shudder_total)); + tangles.p += (Game_shudder_intensity / 200.0f) * (float) (r1-MY_RAND_MAX/2)/MY_RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/(float)Game_shudder_total)); + tangles.h += (Game_shudder_intensity / 200.0f) * (float) (r2-MY_RAND_MAX/2)/MY_RAND_MAX * (0.5f - fl_abs(0.5f - (float) dtime/(float)Game_shudder_total)); } } diff --git a/src/globalincs/systemvars.cpp b/src/globalincs/systemvars.cpp index 7c4de5c..8c9d0e7 100644 --- a/src/globalincs/systemvars.cpp +++ b/src/globalincs/systemvars.cpp @@ -267,7 +267,11 @@ float Noise[NOISE_NUM_FRAMES] = { int myrand() { int rval; +#if MY_RAND_MAX != RAND_MAX + rval = rand() % (MY_RAND_MAX+1); +#else rval = rand(); +#endif Rand_count++; // nprintf(("Alan","RAND: %d\n", rval)); return rval; diff --git a/src/math/floating.cpp b/src/math/floating.cpp index 97ca028..40e47d0 100644 --- a/src/math/floating.cpp +++ b/src/math/floating.cpp @@ -164,7 +164,7 @@ float fl_roundoff(float x, int multiple) float frand() { float rval; - rval = fabsf(((float) myrand()) / (RAND_MAX + 1)); + rval = fabsf(((float) myrand()) / (MY_RAND_MAX + 1)); return rval; } diff --git a/src/menuui/mainhallmenu.cpp b/src/menuui/mainhallmenu.cpp index 9b6472e..9cb75ca 100644 --- a/src/menuui/mainhallmenu.cpp +++ b/src/menuui/mainhallmenu.cpp @@ -1480,7 +1480,7 @@ void main_hall_handle_misc_anims() // if the timestamp is -1, then reset it to some random value (based on MIN and MAX) and continue if(Main_hall->misc_anim_delay[idx][0] == -1){ Main_hall->misc_anim_delay[idx][0] = timestamp(Main_hall->misc_anim_delay[idx][1] + - (int)(((float)rand()/(float)RAND_MAX) * (float)(Main_hall->misc_anim_delay[idx][2] - Main_hall->misc_anim_delay[idx][1]))); + (int)(((float)myrand()/(float)MY_RAND_MAX) * (float)(Main_hall->misc_anim_delay[idx][2] - Main_hall->misc_anim_delay[idx][1]))); // if the timestamp is not -1 and has popped, play the anim and make the timestap -1 } else if (timestamp_elapsed(Main_hall->misc_anim_delay[idx][0]) && Main_hall_misc_anim[idx]) { @@ -1789,7 +1789,7 @@ void main_hall_handle_random_intercom_sounds() { // if we have no timestamp for the next random sound, then set on if((Main_hall_next_intercom_sound_stamp == -1) && (Main_hall_intercom_sound_handle == -1)){ - Main_hall_next_intercom_sound_stamp = timestamp((int)(((float)rand()/(float)RAND_MAX) * + Main_hall_next_intercom_sound_stamp = timestamp((int)(((float)myrand()/(float)MY_RAND_MAX) * (float)(Main_hall->intercom_delay[Main_hall_next_intercom_sound][1] - Main_hall->intercom_delay[Main_hall_intercom_sound_handle][0])) ); } @@ -1817,7 +1817,7 @@ void main_hall_handle_random_intercom_sounds() } // set the timestamp - Main_hall_next_intercom_sound_stamp = timestamp((int)(((float)rand()/(float)RAND_MAX) * + Main_hall_next_intercom_sound_stamp = timestamp((int)(((float)myrand()/(float)MY_RAND_MAX) * (float)(Main_hall->intercom_delay[Main_hall_next_intercom_sound][1] - Main_hall->intercom_delay[Main_hall_next_intercom_sound][0])) ); diff --git a/src/mission/missionparse.cpp b/src/mission/missionparse.cpp index 0487659..18ce534 100644 --- a/src/mission/missionparse.cpp +++ b/src/mission/missionparse.cpp @@ -4239,13 +4239,13 @@ int mission_set_arrival_location(int anchor, int location, int dist, int objnum, //x = cos(angle) x = (float)cos(ANG_TO_RAD(45)); if ( Game_mode & GM_NORMAL ) { - r1 = rand() < RAND_MAX/2 ? -1 : 1; - r2 = rand() < RAND_MAX/2 ? -1 : 1; + r1 = myrand() < MY_RAND_MAX/2 ? -1 : 1; + r2 = myrand() < MY_RAND_MAX/2 ? -1 : 1; } else { // in multiplayer, use the static rand functions so that all clients can get the // same information. - r1 = static_rand(Objects[objnum].net_signature) < RAND_MAX/2 ? -1 : 1; - r2 = static_rand(Objects[objnum].net_signature+1) < RAND_MAX/2 ? -1 : 1; + r1 = static_rand(Objects[objnum].net_signature) < MY_RAND_MAX/2 ? -1 : 1; + r2 = static_rand(Objects[objnum].net_signature+1) < MY_RAND_MAX/2 ? -1 : 1; } vm_vec_copy_scale(&t1, &(Objects[anchor_objnum].orient.v.fvec), x); diff --git a/src/model/modelinterp.cpp b/src/model/modelinterp.cpp index a6689f9..72a3e38 100644 --- a/src/model/modelinterp.cpp +++ b/src/model/modelinterp.cpp @@ -1372,7 +1372,7 @@ void interp_render_lightning( polymodel *pm, bsp_info * sm ) switch(sm->arc_type[i]){ // "normal", Freespace 1 style arcs case MARC_TYPE_NORMAL: - if ( (rand()>>4) & 1 ) { + if ( (myrand()>>4) & 1 ) { gr_set_color( 64, 64, 255 ); } else { gr_set_color( 128, 128, 255 ); @@ -1381,7 +1381,7 @@ void interp_render_lightning( polymodel *pm, bsp_info * sm ) // "EMP" style arcs case MARC_TYPE_EMP: - if ( (rand()>>4) & 1 ) { + if ( (myrand()>>4) & 1 ) { gr_set_color( AR, AG, AB ); } else { gr_set_color( AR2, AG2, AB2 ); diff --git a/src/network/multilag.cpp b/src/network/multilag.cpp index 5272fae..15259a2 100644 --- a/src/network/multilag.cpp +++ b/src/network/multilag.cpp @@ -404,15 +404,15 @@ int multi_lag_get_random_lag() // pick a value // see if we should be going up or down (loss max/loss min) mod = 0; - if((float)rand()/(float)RAND_MAX < 0.5){ + if((float)myrand()/(float)MY_RAND_MAX < 0.5){ // down if(Multi_lag_min >= 0){ - mod = - (int)((float)(Multi_lag_base - Multi_lag_min) * ((float)rand()/(float)RAND_MAX)); + mod = - (int)((float)(Multi_lag_base - Multi_lag_min) * ((float)myrand()/(float)MY_RAND_MAX)); } } else { // up if(Multi_lag_max >= 0){ - mod = (int)((float)(Multi_lag_max - Multi_lag_base) * ((float)rand()/(float)RAND_MAX)); + mod = (int)((float)(Multi_lag_max - Multi_lag_base) * ((float)myrand()/(float)MY_RAND_MAX)); } } @@ -448,19 +448,19 @@ int multi_lag_should_be_lost() // see if we should be going up or down (loss max/loss min) mod = 0.0f; - if((float)rand()/(float)RAND_MAX < 0.5){ + if((float)myrand()/(float)MY_RAND_MAX < 0.5){ // down if(Multi_loss_min >= 0.0f){ - mod = - ((Multi_loss_base - Multi_loss_min) * ((float)rand()/(float)RAND_MAX)); + mod = - ((Multi_loss_base - Multi_loss_min) * ((float)myrand()/(float)MY_RAND_MAX)); } } else { // up if(Multi_loss_max >= 0.0f){ - mod = ((Multi_loss_max - Multi_loss_base) * ((float)rand()/(float)RAND_MAX)); + mod = ((Multi_loss_max - Multi_loss_base) * ((float)myrand()/(float)MY_RAND_MAX)); } } - if((float)rand()/(float)RAND_MAX <= Multi_loss_base + mod){ + if((float)myrand()/(float)MY_RAND_MAX <= Multi_loss_base + mod){ return 1; } diff --git a/src/parse/sexp.cpp b/src/parse/sexp.cpp index a78885a..085fa9f 100644 --- a/src/parse/sexp.cpp +++ b/src/parse/sexp.cpp @@ -2347,7 +2347,7 @@ int rand_internal(int low, int high) diff = 0; } - return (low + rand() % (diff + 1)); + return (low + myrand() % (diff + 1)); } diff --git a/src/particle/particle.cpp b/src/particle/particle.cpp index 96895af..58b0030 100644 --- a/src/particle/particle.cpp +++ b/src/particle/particle.cpp @@ -774,7 +774,7 @@ void particle_emit( particle_emitter *pe, int type, uint optional_data, float ra n2 = (pe->num_high*percent)/100; // How many to emit? - n = (rand() % (n2-n1+1)) + n1; + n = (myrand() % (n2-n1+1)) + n1; if ( n < 1 ) return; diff --git a/src/physics/physics.cpp b/src/physics/physics.cpp index e16afed..8f11814 100644 --- a/src/physics/physics.cpp +++ b/src/physics/physics.cpp @@ -605,8 +605,8 @@ void physics_sim_rot(matrix * orient, physics_info * pi, float sim_time ) // Make ship shake due to shockwave, decreasing in amplitude at the end of the shockwave if ( pi->flags & PF_IN_SHOCKWAVE ) { - tangles.p += (float) (myrand()-RAND_MAX/2)/RAND_MAX * shock_amplitude; - tangles.h += (float) (myrand()-RAND_MAX/2)/RAND_MAX * shock_amplitude; + tangles.p += (float) (myrand()-MY_RAND_MAX/2)/MY_RAND_MAX * shock_amplitude; + tangles.h += (float) (myrand()-MY_RAND_MAX/2)/MY_RAND_MAX * shock_amplitude; } diff --git a/src/ship/shipfx.cpp b/src/ship/shipfx.cpp index 01ab30e..713621d 100644 --- a/src/ship/shipfx.cpp +++ b/src/ship/shipfx.cpp @@ -2416,7 +2416,7 @@ void shipfx_do_damaged_arcs_frame( ship *shipp ) //mprintf(( "Creating new ship arc!\n" )); - int n, n_arcs = ((rand()>>5) % 3)+1; // Create 1-3 sparks + int n, n_arcs = ((myrand()>>5) % 3)+1; // Create 1-3 sparks vector v1, v2, v3, v4; submodel_get_two_random_points( shipp->modelnum, -1, &v1, &v2 ); @@ -2530,7 +2530,7 @@ void shipfx_do_damaged_arcs_frame( ship *shipp ) if ( !timestamp_elapsed( shipp->arc_timestamp[i] ) ) { // Maybe move a vertex.... 20% of the time maybe? int mr = myrand(); - if ( mr < RAND_MAX/5 ) { + if ( mr < MY_RAND_MAX/5 ) { vector v1, v2; submodel_get_two_random_points( shipp->modelnum, -1, &v1, &v2 ); diff --git a/src/starfield/starfield.cpp b/src/starfield/starfield.cpp index 9aade26..f23ab38 100644 --- a/src/starfield/starfield.cpp +++ b/src/starfield/starfield.cpp @@ -1298,9 +1298,9 @@ void stars_draw( int show_stars, int show_suns, int show_nebulas, int show_subsp vertex p; if (!d->active) { - d->pos.xyz.x = f2fl(myrand() - RAND_MAX/2); - d->pos.xyz.y = f2fl(myrand() - RAND_MAX/2); - d->pos.xyz.z = f2fl(myrand() - RAND_MAX/2); + d->pos.xyz.x = f2fl(myrand() - MY_RAND_MAX/2); + d->pos.xyz.y = f2fl(myrand() - MY_RAND_MAX/2); + d->pos.xyz.z = f2fl(myrand() - MY_RAND_MAX/2); vm_vec_normalize(&d->pos); diff --git a/src/starfield/supernova.cpp b/src/starfield/supernova.cpp index 79c025f..d6a7468 100644 --- a/src/starfield/supernova.cpp +++ b/src/starfield/supernova.cpp @@ -339,8 +339,8 @@ void supernova_apply_shake(matrix *eye_orient, float intensity) // Make eye shake due to engine wash int r1 = myrand(); int r2 = myrand(); - tangles.p += 0.07f * intensity * (float) (r1-RAND_MAX/2)/RAND_MAX; - tangles.h += 0.07f * intensity * (float) (r2-RAND_MAX/2)/RAND_MAX; + tangles.p += 0.07f * intensity * (float) (r1-MY_RAND_MAX/2)/MY_RAND_MAX; + tangles.h += 0.07f * intensity * (float) (r2-MY_RAND_MAX/2)/MY_RAND_MAX; matrix tm, tm2; vm_angles_2_matrix(&tm, &tangles); -- 2.39.2