From 3690f2594406729f4e9ea419127ef08a8b8ae3d1 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sat, 5 Nov 2011 12:13:50 +0000 Subject: [PATCH] Fix the 'displayrandvar{var}' CON and m32script commands for environments with RAND_MAX > 32767 (everything except Windows?). The 'displayrand' command now returns values from 0 to 32767 inclusive, across all platforms. git-svn-id: https://svn.eduke32.com/eduke32@2105 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/compat.h | 35 +++++++++++++++----------- polymer/eduke32/source/gameexec.c | 12 ++++----- polymer/eduke32/source/m32exec.c | 8 +++--- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/polymer/eduke32/build/include/compat.h b/polymer/eduke32/build/include/compat.h index c679007db..f08472993 100644 --- a/polymer/eduke32/build/include/compat.h +++ b/polymer/eduke32/build/include/compat.h @@ -419,25 +419,30 @@ int32_t Bclosedir(BDIR *dir); typedef int32_t bssize_t; #endif +#if RAND_MAX == 32767 +static inline uint16_t system_15bit_rand(void) { return (uint16_t)rand(); } +#else // RAND_MAX > 32767, assumed to be of the form 2^k - 1 +static inline uint16_t system_15bit_rand(void) { return ((uint16_t)rand())&0x7fff; } +#endif #ifdef __compat_h_macrodef__ # define Brand rand # define Balloca alloca -#ifdef NEDMALLOC -# define Bmalloc nedmalloc -# define Bcalloc nedcalloc -# define Brealloc nedrealloc -# define Bfree nedfree -# define Bstrdup nedstrdup -# define Bmemalign nedmemalign -#else -# define Bmalloc malloc -# define Bcalloc calloc -# define Brealloc realloc -# define Bfree free -# define Bstrdup strdup -# define Bmemalign memalign -#endif +# ifdef NEDMALLOC +# define Bmalloc nedmalloc +# define Bcalloc nedcalloc +# define Brealloc nedrealloc +# define Bfree nedfree +# define Bstrdup nedstrdup +# define Bmemalign nedmemalign +# else +# define Bmalloc(x) ({if (x<=0) *(int *)123=234; malloc(x);}) +# define Bcalloc(x,y) ({if (x<=0||y<=0) *(int *)123=234; calloc(x,y);}) +# define Brealloc(p,x) ({if (x<=0) *(int *)123=234; realloc(p,x);}) +# define Bfree free +# define Bstrdup strdup +# define Bmemalign memalign +# endif # define Bopen open # define Bclose close # define Bwrite write diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index 2856898b4..bea263201 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -644,8 +644,8 @@ dead: } else { - g_player[vm.g_p].ps->vel.x = mulscale(g_player[vm.g_p].ps->vel.x,g_player[vm.g_p].ps->runspeed-0x2000,16); - g_player[vm.g_p].ps->vel.y = mulscale(g_player[vm.g_p].ps->vel.y,g_player[vm.g_p].ps->runspeed-0x2000,16); + g_player[vm.g_p].ps->vel.x = mulscale16(g_player[vm.g_p].ps->vel.x,g_player[vm.g_p].ps->runspeed-0x2000); + g_player[vm.g_p].ps->vel.y = mulscale16(g_player[vm.g_p].ps->vel.y,g_player[vm.g_p].ps->runspeed-0x2000); } } else if (vm.g_sp->picnum != DRONE && vm.g_sp->picnum != SHARK && vm.g_sp->picnum != COMMANDER) @@ -1979,7 +1979,7 @@ nullquote: case CON_DISPLAYRAND: insptr++; - Gv_SetVarX(*insptr++, rand()); + Gv_SetVarX(*insptr++, system_15bit_rand()); continue; case CON_DRAGPOINT: @@ -4137,7 +4137,7 @@ nullquote: case CON_DISPLAYRANDVAR: insptr++; - Gv_SetVarX(*insptr, mulscale15((uint16_t)rand(), *(insptr+1)+1)); + Gv_SetVarX(*insptr, mulscale15(system_15bit_rand(), *(insptr+1)+1)); insptr += 2; continue; @@ -4219,7 +4219,7 @@ nullquote: insptr++; { int32_t j=*insptr++; - Gv_SetVarX(j,mulscale(krand(), Gv_GetVarX(*insptr++)+1, 16)); + Gv_SetVarX(j,mulscale16(krand(), Gv_GetVarX(*insptr++)+1)); } continue; @@ -4227,7 +4227,7 @@ nullquote: insptr++; { int32_t j=*insptr++; - Gv_SetVarX(j,mulscale((uint16_t)rand(), Gv_GetVarX(*insptr++)+1, 15)); + Gv_SetVarX(j,mulscale15(system_15bit_rand(), Gv_GetVarX(*insptr++)+1)); } continue; diff --git a/polymer/eduke32/source/m32exec.c b/polymer/eduke32/source/m32exec.c index faefdd720..0f9950165 100644 --- a/polymer/eduke32/source/m32exec.c +++ b/polymer/eduke32/source/m32exec.c @@ -618,7 +618,7 @@ skip_check: case CON_DISPLAYRANDVAR: insptr++; - Gv_SetVarX(*insptr, mulscale15((uint16_t)rand(), *(insptr+1)+1)); + Gv_SetVarX(*insptr, mulscale15(system_15bit_rand(), *(insptr+1)+1)); insptr += 2; continue; @@ -688,7 +688,7 @@ skip_check: insptr++; { int32_t j=*insptr++; - Gv_SetVarX(j,mulscale(krand(), Gv_GetVarX(*insptr++)+1, 16)); + Gv_SetVarX(j,mulscale16(krand(), Gv_GetVarX(*insptr++)+1)); } continue; @@ -696,7 +696,7 @@ skip_check: insptr++; { int32_t j=*insptr++; - Gv_SetVarX(j,mulscale((uint16_t)rand(), Gv_GetVarX(*insptr++)+1, 15)); + Gv_SetVarX(j,mulscale15(system_15bit_rand(), Gv_GetVarX(*insptr++)+1)); } continue; @@ -833,7 +833,7 @@ skip_check: case CON_DISPLAYRAND: insptr++; - Gv_SetVarX(*insptr++, rand()); + Gv_SetVarX(*insptr++, system_15bit_rand()); continue; // *** other math