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
This commit is contained in:
helixhorned 2011-11-05 12:13:50 +00:00
parent e94243d7db
commit 3690f25944
3 changed files with 30 additions and 25 deletions

View file

@ -419,25 +419,30 @@ int32_t Bclosedir(BDIR *dir);
typedef int32_t bssize_t; typedef int32_t bssize_t;
#endif #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__ #ifdef __compat_h_macrodef__
# define Brand rand # define Brand rand
# define Balloca alloca # define Balloca alloca
#ifdef NEDMALLOC # ifdef NEDMALLOC
# define Bmalloc nedmalloc # define Bmalloc nedmalloc
# define Bcalloc nedcalloc # define Bcalloc nedcalloc
# define Brealloc nedrealloc # define Brealloc nedrealloc
# define Bfree nedfree # define Bfree nedfree
# define Bstrdup nedstrdup # define Bstrdup nedstrdup
# define Bmemalign nedmemalign # define Bmemalign nedmemalign
#else # else
# define Bmalloc malloc # define Bmalloc(x) ({if (x<=0) *(int *)123=234; malloc(x);})
# define Bcalloc calloc # define Bcalloc(x,y) ({if (x<=0||y<=0) *(int *)123=234; calloc(x,y);})
# define Brealloc realloc # define Brealloc(p,x) ({if (x<=0) *(int *)123=234; realloc(p,x);})
# define Bfree free # define Bfree free
# define Bstrdup strdup # define Bstrdup strdup
# define Bmemalign memalign # define Bmemalign memalign
#endif # endif
# define Bopen open # define Bopen open
# define Bclose close # define Bclose close
# define Bwrite write # define Bwrite write

View file

@ -644,8 +644,8 @@ dead:
} }
else 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.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 = 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.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) 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: case CON_DISPLAYRAND:
insptr++; insptr++;
Gv_SetVarX(*insptr++, rand()); Gv_SetVarX(*insptr++, system_15bit_rand());
continue; continue;
case CON_DRAGPOINT: case CON_DRAGPOINT:
@ -4137,7 +4137,7 @@ nullquote:
case CON_DISPLAYRANDVAR: case CON_DISPLAYRANDVAR:
insptr++; insptr++;
Gv_SetVarX(*insptr, mulscale15((uint16_t)rand(), *(insptr+1)+1)); Gv_SetVarX(*insptr, mulscale15(system_15bit_rand(), *(insptr+1)+1));
insptr += 2; insptr += 2;
continue; continue;
@ -4219,7 +4219,7 @@ nullquote:
insptr++; insptr++;
{ {
int32_t j=*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; continue;
@ -4227,7 +4227,7 @@ nullquote:
insptr++; insptr++;
{ {
int32_t j=*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; continue;

View file

@ -618,7 +618,7 @@ skip_check:
case CON_DISPLAYRANDVAR: case CON_DISPLAYRANDVAR:
insptr++; insptr++;
Gv_SetVarX(*insptr, mulscale15((uint16_t)rand(), *(insptr+1)+1)); Gv_SetVarX(*insptr, mulscale15(system_15bit_rand(), *(insptr+1)+1));
insptr += 2; insptr += 2;
continue; continue;
@ -688,7 +688,7 @@ skip_check:
insptr++; insptr++;
{ {
int32_t j=*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; continue;
@ -696,7 +696,7 @@ skip_check:
insptr++; insptr++;
{ {
int32_t j=*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; continue;
@ -833,7 +833,7 @@ skip_check:
case CON_DISPLAYRAND: case CON_DISPLAYRAND:
insptr++; insptr++;
Gv_SetVarX(*insptr++, rand()); Gv_SetVarX(*insptr++, system_15bit_rand());
continue; continue;
// *** other math // *** other math