gzdoom/code/M_random.c
1999-02-17 00:00:00 +00:00

126 lines
3.8 KiB
C

// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id: m_random.c,v 1.6 1998/05/03 23:13:18 killough Exp $
//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
//
// DESCRIPTION:
// Random number LUT.
//
// 1/19/98 killough: Rewrote random number generator for better randomness,
// while at the same time maintaining demo sync and backward compatibility.
//
// 2/16/98 killough: Made each RNG local to each control-equivalent block,
// to reduce the chances of demo sync problems.
//
//-----------------------------------------------------------------------------
#include "doomstat.h"
#include "m_random.h"
//
// M_Random
// Returns a 0-255 number
//
rng_t rng; // the random number state
unsigned long rngseed = 1993; // killough 3/26/98: The seed
int P_Random(pr_class_t pr_class)
{
// killough 2/16/98: We always update both sets of random number
// generators, to ensure repeatability if the demo_compatibility
// flag is changed while the program is running. Changing the
// demo_compatibility flag does not change the sequences generated,
// only which one is selected from.
//
// All of this RNG stuff is tricky as far as demo sync goes --
// it's like playing with explosives :) Lee
int compat = pr_class == pr_misc ?
(rng.prndindex = (rng.prndindex + 1) & 255) :
(rng. rndindex = (rng. rndindex + 1) & 255) ;
unsigned long boom;
#if 0 // [RH]
// killough 3/31/98:
// If demo sync insurance is not requested, use
// much more unstable method by putting everything
// except pr_misc into pr_all_in_one
if (pr_class != pr_misc && !demo_insurance) // killough 3/31/98
pr_class = pr_all_in_one;
#endif
boom = rng.seed[pr_class];
// killough 3/26/98: add pr_class*2 to addend
rng.seed[pr_class] = boom * 1664525ul + 221297ul + pr_class*2;
boom >>= 20;
// killough 3/30/98: use gametic-levelstarttic to shuffle RNG
// killough 3/31/98: but only if demo insurance requested,
// since it's unnecessary for random shuffling otherwise
// [RH] And it also seems to screws up netgame sync.
//if (demo_insurance)
// boom += (gametic-level.starttime)*7;
return boom & 255;
}
// Initialize all the seeds
//
// This initialization method is critical to maintaining demo sync.
// Each seed is initialized according to its class, so if new classes
// are added they must be added to end of pr_class_t list. killough
//
void M_ClearRandom (void)
{
int i;
unsigned long seed = rngseed*2+1; // add 3/26/98: add rngseed
for (i=0; i<NUMPRCLASS; i++) // go through each pr_class and set
rng.seed[i] = seed *= 69069ul; // each starting seed differently
rng.prndindex = rng.rndindex = 0; // clear two compatibility indices
}
//----------------------------------------------------------------------------
//
// $Log: m_random.c,v $
// Revision 1.6 1998/05/03 23:13:18 killough
// Fix #include
//
// Revision 1.5 1998/03/31 10:43:05 killough
// Fix (supposed) RNG problems, add new demo_insurance
//
// Revision 1.4 1998/03/28 17:56:05 killough
// Improve RNG by adding external seed
//
// Revision 1.3 1998/02/17 05:40:08 killough
// Make RNGs local to each calling block, for demo sync
//
// Revision 1.2 1998/01/26 19:23:51 phares
// First rev with no ^Ms
//
// Revision 1.1.1.1 1998/01/19 14:02:58 rand
// Lee's Jan 19 sources
//
//----------------------------------------------------------------------------