games/doom: fix desyncing demo (thanks qwx)

the code used P_Random()-P_Random() in some places which has
undefined evaluation order resulting in the wrong pseudo random
numbers being returned causing demo playback to desync.

this change adds P_Random2() function which returns the right
delta-random number and uses it in place of P_Random()-P_Random()
expression.
This commit is contained in:
cinap_lenrek 2015-06-12 17:28:09 +02:00
parent 34ae4649cc
commit e4c3f92c16
6 changed files with 22 additions and 15 deletions

View file

@ -60,6 +60,12 @@ int P_Random (void)
return rndtable[prndindex]; return rndtable[prndindex];
} }
int P_Random2 (void)
{
int tmp = P_Random();
return tmp - P_Random();
}
int M_Random (void) int M_Random (void)
{ {
rndindex = (rndindex+1)&0xff; rndindex = (rndindex+1)&0xff;

View file

@ -34,6 +34,7 @@ int M_Random (void);
// As M_Random, but used only by the play simulation. // As M_Random, but used only by the play simulation.
int P_Random (void); int P_Random (void);
int P_Random2 (void); /* P_Raomdom() - P_Random() */
// Fix randoms for demos. // Fix randoms for demos.
void M_ClearRandom (void); void M_ClearRandom (void);

View file

@ -789,7 +789,7 @@ void A_FaceTarget (void *_actor, void*)
actor->target->y); actor->target->y);
if (actor->target->flags & MF_SHADOW) if (actor->target->flags & MF_SHADOW)
actor->angle += (P_Random()-P_Random())<<21; actor->angle += P_Random2()<<21;
} }
@ -811,7 +811,7 @@ void A_PosAttack (void *_actor, void*)
slope = P_AimLineAttack (actor, angle, MISSILERANGE); slope = P_AimLineAttack (actor, angle, MISSILERANGE);
S_StartSound (actor, sfx_pistol); S_StartSound (actor, sfx_pistol);
angle += (P_Random()-P_Random())<<20; angle += P_Random2()<<20;
damage = ((P_Random()%5)+1)*3; damage = ((P_Random()%5)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, slope, damage); P_LineAttack (actor, angle, MISSILERANGE, slope, damage);
} }
@ -835,7 +835,7 @@ void A_SPosAttack (void *_actor, void*)
for (i=0 ; i<3 ; i++) for (i=0 ; i<3 ; i++)
{ {
angle = bangle + ((P_Random()-P_Random())<<20); angle = bangle + (P_Random2()<<20);
damage = ((P_Random()%5)+1)*3; damage = ((P_Random()%5)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, slope, damage); P_LineAttack (actor, angle, MISSILERANGE, slope, damage);
} }
@ -857,7 +857,7 @@ void A_CPosAttack (void *_actor, void*)
bangle = actor->angle; bangle = actor->angle;
slope = P_AimLineAttack (actor, bangle, MISSILERANGE); slope = P_AimLineAttack (actor, bangle, MISSILERANGE);
angle = bangle + ((P_Random()-P_Random())<<20); angle = bangle + (P_Random2()<<20);
damage = ((P_Random()%5)+1)*3; damage = ((P_Random()%5)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, slope, damage); P_LineAttack (actor, angle, MISSILERANGE, slope, damage);
} }
@ -1908,7 +1908,7 @@ void A_BrainExplode (void *_mo, void*)
int z; int z;
mobj_t* th; mobj_t* th;
x = mo->x + (P_Random () - P_Random ())*2048; x = mo->x + P_Random2()*2048;
y = mo->y; y = mo->y;
z = 128 + P_Random()*2*FRACUNIT; z = 128 + P_Random()*2*FRACUNIT;
th = P_SpawnMobj (x,y,z, MT_ROCKET); th = P_SpawnMobj (x,y,z, MT_ROCKET);

View file

@ -1302,8 +1302,8 @@ boolean PIT_ChangeSector (mobj_t* thing)
thing->y, thing->y,
thing->z + thing->height/2, MT_BLOOD); thing->z + thing->height/2, MT_BLOOD);
mo->momx = (P_Random() - P_Random ())<<12; mo->momx = P_Random2()<<12;
mo->momy = (P_Random() - P_Random ())<<12; mo->momy = P_Random2()<<12;
} }
// keep checking (crush other things) // keep checking (crush other things)

View file

@ -818,7 +818,7 @@ P_SpawnPuff
{ {
mobj_t* th; mobj_t* th;
z += ((P_Random()-P_Random())<<10); z += P_Random2()<<10;
th = P_SpawnMobj (x,y,z, MT_PUFF); th = P_SpawnMobj (x,y,z, MT_PUFF);
th->momz = FRACUNIT; th->momz = FRACUNIT;
@ -846,7 +846,7 @@ P_SpawnBlood
{ {
mobj_t* th; mobj_t* th;
z += ((P_Random()-P_Random())<<10); z += P_Random2()<<10;
th = P_SpawnMobj (x,y,z, MT_BLOOD); th = P_SpawnMobj (x,y,z, MT_BLOOD);
th->momz = FRACUNIT*2; th->momz = FRACUNIT*2;
th->tics -= P_Random()&3; th->tics -= P_Random()&3;
@ -909,7 +909,7 @@ P_SpawnMissile
// fuzzy player // fuzzy player
if (dest->flags & MF_SHADOW) if (dest->flags & MF_SHADOW)
an += (P_Random()-P_Random())<<20; an += P_Random2()<<20;
th->angle = an; th->angle = an;
an >>= ANGLETOFINESHIFT; an >>= ANGLETOFINESHIFT;

View file

@ -471,7 +471,7 @@ A_Punch(void *_player, void* /*psp*/)
damage *= 10; damage *= 10;
angle = player->mo->angle; angle = player->mo->angle;
angle += (P_Random()-P_Random())<<18; angle += P_Random2()<<18;
slope = P_AimLineAttack (player->mo, angle, MELEERANGE); slope = P_AimLineAttack (player->mo, angle, MELEERANGE);
P_LineAttack (player->mo, angle, MELEERANGE, slope, damage); P_LineAttack (player->mo, angle, MELEERANGE, slope, damage);
@ -500,7 +500,7 @@ A_Saw(void *_player, void * /*psp*/)
damage = 2*(P_Random ()%10+1); damage = 2*(P_Random ()%10+1);
angle = player->mo->angle; angle = player->mo->angle;
angle += (P_Random()-P_Random())<<18; angle += P_Random2()<<18;
// use meleerange + 1 se the puff doesn't skip the flash // use meleerange + 1 se the puff doesn't skip the flash
slope = P_AimLineAttack (player->mo, angle, MELEERANGE+1); slope = P_AimLineAttack (player->mo, angle, MELEERANGE+1);
@ -622,7 +622,7 @@ P_GunShot
angle = mo->angle; angle = mo->angle;
if (!accurate) if (!accurate)
angle += (P_Random()-P_Random())<<18; angle += P_Random2()<<18;
P_LineAttack (mo, angle, MISSILERANGE, bulletslope, damage); P_LineAttack (mo, angle, MISSILERANGE, bulletslope, damage);
} }
@ -703,11 +703,11 @@ A_FireShotgun2(void *_player, void* /*psp*/)
{ {
damage = 5*(P_Random ()%3+1); damage = 5*(P_Random ()%3+1);
angle = player->mo->angle; angle = player->mo->angle;
angle += (P_Random()-P_Random())<<19; angle += P_Random2()<<19;
P_LineAttack (player->mo, P_LineAttack (player->mo,
angle, angle,
MISSILERANGE, MISSILERANGE,
bulletslope + ((P_Random()-P_Random())<<5), damage); bulletslope + (P_Random2()<<5), damage);
} }
} }