ref: e4c3f92c163dec741abeadeca80d6a938561dfb3
parent: 34ae4649cc91bf32cd602558a4e953bc3fb8777a
author: cinap_lenrek <[email protected]>
date: Fri Jun 12 13:28:09 EDT 2015
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.
--- a/sys/src/games/doom/m_random.c
+++ b/sys/src/games/doom/m_random.c
@@ -60,6 +60,12 @@
return rndtable[prndindex];
}
+int P_Random2 (void)
+{
+ int tmp = P_Random();
+ return tmp - P_Random();
+}
+
int M_Random (void)
{
rndindex = (rndindex+1)&0xff;
--- a/sys/src/games/doom/m_random.h
+++ b/sys/src/games/doom/m_random.h
@@ -34,6 +34,7 @@
// As M_Random, but used only by the play simulation.
int P_Random (void);
+int P_Random2 (void); /* P_Raomdom() - P_Random() */
// Fix randoms for demos.
void M_ClearRandom (void);
--- a/sys/src/games/doom/p_enemy.c
+++ b/sys/src/games/doom/p_enemy.c
@@ -789,7 +789,7 @@
actor->target->y);
if (actor->target->flags & MF_SHADOW)
- actor->angle += (P_Random()-P_Random())<<21;
+ actor->angle += P_Random2()<<21;
}
@@ -811,7 +811,7 @@
slope = P_AimLineAttack (actor, angle, MISSILERANGE);
S_StartSound (actor, sfx_pistol);
- angle += (P_Random()-P_Random())<<20;
+ angle += P_Random2()<<20;
damage = ((P_Random()%5)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, slope, damage);
}
@@ -835,7 +835,7 @@
for (i=0 ; i<3 ; i++)
{
- angle = bangle + ((P_Random()-P_Random())<<20);
+ angle = bangle + (P_Random2()<<20);
damage = ((P_Random()%5)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, slope, damage);
}
@@ -857,7 +857,7 @@
bangle = actor->angle;
slope = P_AimLineAttack (actor, bangle, MISSILERANGE);
- angle = bangle + ((P_Random()-P_Random())<<20);
+ angle = bangle + (P_Random2()<<20);
damage = ((P_Random()%5)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, slope, damage);
}
@@ -1908,7 +1908,7 @@
int z;
mobj_t* th;
- x = mo->x + (P_Random () - P_Random ())*2048;
+ x = mo->x + P_Random2()*2048;
y = mo->y;
z = 128 + P_Random()*2*FRACUNIT;
th = P_SpawnMobj (x,y,z, MT_ROCKET);
--- a/sys/src/games/doom/p_map.c
+++ b/sys/src/games/doom/p_map.c
@@ -1302,8 +1302,8 @@
thing->y,
thing->z + thing->height/2, MT_BLOOD);
- mo->momx = (P_Random() - P_Random ())<<12;
- mo->momy = (P_Random() - P_Random ())<<12;
+ mo->momx = P_Random2()<<12;
+ mo->momy = P_Random2()<<12;
}
// keep checking (crush other things)
--- a/sys/src/games/doom/p_mobj.c
+++ b/sys/src/games/doom/p_mobj.c
@@ -818,7 +818,7 @@
{
mobj_t* th;
- z += ((P_Random()-P_Random())<<10);
+ z += P_Random2()<<10;
th = P_SpawnMobj (x,y,z, MT_PUFF);
th->momz = FRACUNIT;
@@ -846,7 +846,7 @@
{
mobj_t* th;
- z += ((P_Random()-P_Random())<<10);
+ z += P_Random2()<<10;
th = P_SpawnMobj (x,y,z, MT_BLOOD);
th->momz = FRACUNIT*2;
th->tics -= P_Random()&3;
@@ -909,7 +909,7 @@
// fuzzy player
if (dest->flags & MF_SHADOW)
- an += (P_Random()-P_Random())<<20;
+ an += P_Random2()<<20;
th->angle = an;
an >>= ANGLETOFINESHIFT;
--- a/sys/src/games/doom/p_pspr.c
+++ b/sys/src/games/doom/p_pspr.c
@@ -471,7 +471,7 @@
damage *= 10;
angle = player->mo->angle;
- angle += (P_Random()-P_Random())<<18;
+ angle += P_Random2()<<18;
slope = P_AimLineAttack (player->mo, angle, MELEERANGE);
P_LineAttack (player->mo, angle, MELEERANGE, slope, damage);
@@ -500,7 +500,7 @@
damage = 2*(P_Random ()%10+1);
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
slope = P_AimLineAttack (player->mo, angle, MELEERANGE+1);
@@ -622,7 +622,7 @@
angle = mo->angle;
if (!accurate)
- angle += (P_Random()-P_Random())<<18;
+ angle += P_Random2()<<18;
P_LineAttack (mo, angle, MISSILERANGE, bulletslope, damage);
}
@@ -703,11 +703,11 @@
{
damage = 5*(P_Random ()%3+1);
angle = player->mo->angle;
- angle += (P_Random()-P_Random())<<19;
+ angle += P_Random2()<<19;
P_LineAttack (player->mo,
angle,
MISSILERANGE,
- bulletslope + ((P_Random()-P_Random())<<5), damage);
+ bulletslope + (P_Random2()<<5), damage);
}
}