Fix random() builtin to avoid the monsters-are-statues bug.

This commit is contained in:
Shpoike 2024-02-24 08:57:32 +00:00
parent 78b5a73ee6
commit 0af9796e5a

View file

@ -562,11 +562,17 @@ bug: vanilla could return 1, contrary to the (unchanged) comment just above.
*/
static void PF_random (void)
{
float num;
//don't return 1 (it would break array[random()*array.length];
//don't return 0 either, it would break the self.nextthink = time+random()*foo; lines in walkmonster_start, resulting rarely in statue-monsters.
G_FLOAT(OFS_RETURN) = (rand ()&0x7fff) / ((float)0x08000) + (0.5/0x08000);
num = (rand() & 0x7fff) / ((float)0x8000);
G_FLOAT(OFS_RETURN) = num;
if (qcvm->argc)
{
if (qcvm->argc == 1) //maximum value
G_FLOAT(OFS_RETURN) *= G_FLOAT(OFS_PARM0);
else // min and max.
G_FLOAT(OFS_RETURN) = G_FLOAT(OFS_PARM0) + G_FLOAT(OFS_RETURN)*(G_FLOAT(OFS_PARM1)-G_FLOAT(OFS_PARM0));
}
}
/*