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;
#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

View file

@ -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;

View file

@ -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