| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 1999 by |
| 8 |
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman |
| 9 |
* Copyright (C) 1999-2000 by |
| 10 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 11 |
* Copyright 2005, 2006 by |
| 12 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or |
| 15 |
* modify it under the terms of the GNU General Public License |
| 16 |
* as published by the Free Software Foundation; either version 2 |
| 17 |
* of the License, or (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
* 02111-1307, USA. |
| 28 |
* |
| 29 |
* DESCRIPTION: |
| 30 |
* Random number LUT. |
| 31 |
* |
| 32 |
* 1/19/98 killough: Rewrote random number generator for better randomness, |
| 33 |
* while at the same time maintaining demo sync and backward compatibility. |
| 34 |
* |
| 35 |
* 2/16/98 killough: Made each RNG local to each control-equivalent block, |
| 36 |
* to reduce the chances of demo sync problems. |
| 37 |
* |
| 38 |
*-----------------------------------------------------------------------------*/ |
| 39 |
|
| 40 |
|
| 41 |
#include "doomstat.h" |
| 42 |
#include "m_random.h" |
| 43 |
#include "lprintf.h" |
| 44 |
#include "tables.h" |
| 45 |
|
| 46 |
// |
| 47 |
// M_Random |
| 48 |
// Returns a 0-255 number |
| 49 |
// |
| 50 |
static const unsigned char doom_rndtable[256] = { // 1/19/98 killough -- made const |
| 51 |
0, 8, 109, 220, 222, 241, 149, 107, 75, 248, 254, 140, 16, 66 , |
| 52 |
74, 21, 211, 47, 80, 242, 154, 27, 205, 128, 161, 89, 77, 36 , |
| 53 |
95, 110, 85, 48, 212, 140, 211, 249, 22, 79, 200, 50, 28, 188 , |
| 54 |
52, 140, 202, 120, 68, 145, 62, 70, 184, 190, 91, 197, 152, 224 , |
| 55 |
149, 104, 25, 178, 252, 182, 202, 182, 141, 197, 4, 81, 181, 242 , |
| 56 |
145, 42, 39, 227, 156, 198, 225, 193, 219, 93, 122, 175, 249, 0 , |
| 57 |
175, 143, 70, 239, 46, 246, 163, 53, 163, 109, 168, 135, 2, 235 , |
| 58 |
25, 92, 20, 145, 138, 77, 69, 166, 78, 176, 173, 212, 166, 113 , |
| 59 |
94, 161, 41, 50, 239, 49, 111, 164, 70, 60, 2, 37, 171, 75 , |
| 60 |
136, 156, 11, 56, 42, 146, 138, 229, 73, 146, 77, 61, 98, 196 , |
| 61 |
135, 106, 63, 197, 195, 86, 96, 203, 113, 101, 170, 247, 181, 113 , |
| 62 |
80, 250, 108, 7, 255, 237, 129, 226, 79, 107, 112, 166, 103, 241 , |
| 63 |
24, 223, 239, 120, 198, 58, 60, 82, 128, 3, 184, 66, 143, 224 , |
| 64 |
145, 224, 81, 206, 163, 45, 63, 90, 168, 114, 59, 33, 159, 95 , |
| 65 |
28, 139, 123, 98, 125, 196, 15, 70, 194, 253, 54, 14, 109, 226 , |
| 66 |
71, 17, 161, 93, 186, 87, 244, 138, 20, 52, 123, 251, 26, 36 , |
| 67 |
17, 46, 52, 231, 232, 76, 31, 221, 84, 37, 216, 165, 212, 106 , |
| 68 |
197, 242, 98, 43, 39, 175, 254, 145, 190, 84, 118, 222, 187, 136 , |
| 69 |
120, 163, 236, 249 |
| 70 |
}; |
| 71 |
|
| 72 |
static const unsigned char hexen_rndtable[256] = { |
| 73 |
201, 1, 243, 19, 18, 42, 183, 203, 101, 123, 154, 137, 34, 118, 10, 216, |
| 74 |
135, 246, 0, 107, 133, 229, 35, 113, 177, 211, 110, 17, 139, 84, 251, 235, |
| 75 |
182, 166, 161, 230, 143, 91, 24, 81, 22, 94, 7, 51, 232, 104, 122, 248, |
| 76 |
175, 138, 127, 171, 222, 213, 44, 16, 9, 33, 88, 102, 170, 150, 136, 114, |
| 77 |
62, 3, 142, 237, 6, 252, 249, 56, 74, 30, 13, 21, 180, 199, 32, 132, |
| 78 |
187, 234, 78, 210, 46, 131, 197, 8, 206, 244, 73, 4, 236, 178, 195, 70, |
| 79 |
121, 97, 167, 217, 103, 40, 247, 186, 105, 39, 95, 163, 99, 149, 253, 29, |
| 80 |
119, 83, 254, 26, 202, 65, 130, 155, 60, 64, 184, 106, 221, 93, 164, 196, |
| 81 |
112, 108, 179, 141, 54, 109, 11, 126, 75, 165, 191, 227, 87, 225, 156, 15, |
| 82 |
98, 162, 116, 79, 169, 140, 190, 205, 168, 194, 41, 250, 27, 20, 14, 241, |
| 83 |
50, 214, 72, 192, 220, 233, 67, 148, 96, 185, 176, 181, 215, 207, 172, 85, |
| 84 |
89, 90, 209, 128, 124, 2, 55, 173, 66, 152, 47, 129, 59, 43, 159, 240, |
| 85 |
239, 12, 189, 212, 144, 28, 200, 77, 219, 198, 134, 228, 45, 92, 125, 151, |
| 86 |
5, 53, 255, 52, 68, 245, 160, 158, 61, 86, 58, 82, 117, 37, 242, 145, |
| 87 |
69, 188, 115, 76, 63, 100, 49, 111, 153, 80, 38, 57, 174, 224, 71, 231, |
| 88 |
23, 25, 48, 218, 120, 147, 208, 36, 226, 223, 193, 238, 157, 204, 146, 31 |
| 89 |
}; |
| 90 |
|
| 91 |
static const unsigned char *rndtable = doom_rndtable; |
| 92 |
|
| 93 |
rng_t rng; // the random number state |
| 94 |
|
| 95 |
unsigned int rngseed = 1993; // killough 3/26/98: The seed |
| 96 |
|
| 97 |
int (P_Random)(pr_class_t pr_class) |
| 98 |
{ |
| 99 |
// killough 2/16/98: We always update both sets of random number |
| 100 |
// generators, to ensure repeatability if the demo_compatibility |
| 101 |
// flag is changed while the program is running. Changing the |
| 102 |
// demo_compatibility flag does not change the sequences generated, |
| 103 |
// only which one is selected from. |
| 104 |
// |
| 105 |
// All of this RNG stuff is tricky as far as demo sync goes -- |
| 106 |
// it's like playing with explosives :) Lee |
| 107 |
|
| 108 |
int compat = pr_class == pr_misc ? |
| 109 |
(rng.prndindex = (rng.prndindex + 1) & 255) : |
| 110 |
(rng. rndindex = (rng. rndindex + 1) & 255) ; |
| 111 |
|
| 112 |
unsigned long boom; |
| 113 |
|
| 114 |
// killough 3/31/98: |
| 115 |
// If demo sync insurance is not requested, use |
| 116 |
// much more unstable method by putting everything |
| 117 |
// except pr_misc into pr_all_in_one |
| 118 |
|
| 119 |
if (pr_class != pr_misc && !demo_insurance) // killough 3/31/98 |
| 120 |
pr_class = pr_all_in_one; |
| 121 |
|
| 122 |
boom = rng.seed[pr_class]; |
| 123 |
|
| 124 |
// killough 3/26/98: add pr_class*2 to addend |
| 125 |
|
| 126 |
rng.seed[pr_class] = boom * 1664525ul + 221297ul + pr_class*2; |
| 127 |
|
| 128 |
if (demo_compatibility) |
| 129 |
return rndtable[compat]; |
| 130 |
|
| 131 |
boom >>= 20; |
| 132 |
|
| 133 |
/* killough 3/30/98: use gametic-levelstarttic to shuffle RNG |
| 134 |
* killough 3/31/98: but only if demo insurance requested, |
| 135 |
* since it's unnecessary for random shuffling otherwise |
| 136 |
* killough 9/29/98: but use basetic now instead of levelstarttic |
| 137 |
* cph - DEMOSYNC - this change makes MBF demos work, |
| 138 |
* but does it break Boom ones? |
| 139 |
*/ |
| 140 |
|
| 141 |
if (demo_insurance) |
| 142 |
boom += boom_logictic * 7; |
| 143 |
|
| 144 |
return boom & 255; |
| 145 |
} |
| 146 |
|
| 147 |
// Initialize all the seeds |
| 148 |
// |
| 149 |
// This initialization method is critical to maintaining demo sync. |
| 150 |
// Each seed is initialized according to its class, so if new classes |
| 151 |
// are added they must be added to end of pr_class_t list. killough |
| 152 |
// |
| 153 |
|
| 154 |
void M_ClearRandom (void) |
| 155 |
{ |
| 156 |
int i; |
| 157 |
unsigned int seed = rngseed*2+1; // add 3/26/98: add rngseed |
| 158 |
for (i=0; i<NUMPRCLASS; i++) // go through each pr_class and set |
| 159 |
rng.seed[i] = seed *= 69069ul; // each starting seed differently |
| 160 |
rng.prndindex = rng.rndindex = 0; // clear two compatibility indices |
| 161 |
} |
| 162 |
|
| 163 |
// [XA] Common random formulas used by codepointers |
| 164 |
|
| 165 |
// |
| 166 |
// P_RandomHitscanAngle |
| 167 |
// Outputs a random angle between (-spread, spread), as an int ('cause it can be negative). |
| 168 |
// spread: Maximum angle (degrees, in fixed point -- not BAM!) |
| 169 |
// |
| 170 |
int P_RandomHitscanAngle(pr_class_t pr_class, fixed_t spread) |
| 171 |
{ |
| 172 |
int t; |
| 173 |
int64_t spread_bam; |
| 174 |
|
| 175 |
// FixedToAngle doesn't work for negative numbers, |
| 176 |
// so for convenience take just the absolute value. |
| 177 |
spread_bam = (spread < 0 ? FixedToAngle(-spread) : FixedToAngle(spread)); |
| 178 |
t = P_Random(pr_class); |
| 179 |
return (int)((spread_bam * (t - P_Random(pr_class))) / 255); |
| 180 |
} |
| 181 |
|
| 182 |
// |
| 183 |
// P_RandomHitscanSlope |
| 184 |
// Outputs a random angle between (-spread, spread), converted to values used for slope |
| 185 |
// spread: Maximum vertical angle (degrees, in fixed point -- not BAM!) |
| 186 |
// |
| 187 |
int P_RandomHitscanSlope(pr_class_t pr_class, fixed_t spread) |
| 188 |
{ |
| 189 |
int angle; |
| 190 |
|
| 191 |
angle = P_RandomHitscanAngle(pr_class, spread); |
| 192 |
|
| 193 |
// clamp it, yo |
| 194 |
if (angle > ANG90) |
| 195 |
return finetangent[0]; |
| 196 |
else if (-angle > ANG90) |
| 197 |
return finetangent[FINEANGLES/2 - 1]; |
| 198 |
else |
| 199 |
return finetangent[(ANG90 - angle) >> ANGLETOFINESHIFT]; |
| 200 |
} |
| 201 |
|
| 202 |
// heretic |
| 203 |
|
| 204 |
int P_SubRandom (void) |
| 205 |
{ |
| 206 |
int r = P_Random(pr_heretic); |
| 207 |
return r - P_Random(pr_heretic); |
| 208 |
} |
| 209 |
|
| 210 |
// hexen |
| 211 |
|
| 212 |
void P_UseHexenRNG(void) |
| 213 |
{ |
| 214 |
rndtable = hexen_rndtable; |
| 215 |
} |