Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
rand.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "rand.h"
20#include <cmath>
21
22static const double theMultFactor = 1.0 / (pow(2, 8 * sizeof(unsigned int)) - 1.0);
23
25{
26 seed[0] = 0xf22d0e56L;
27 seed[1] = 0x883126e9L;
28 seed[2] = 0xc624dd2fL;
29 seed[3] = 0x702c49cL;
30 seed[4] = 0x9e353f7dL;
31 seed[5] = 0x6fdf3b64L;
32
33 unsigned int ax;
34
35 ax = start; /* mov eax,SEED */
36 ax += 0xf22d0e56; /* add eax,0f22d0e56h */
37 seed[0] = ax; /* mov seed,eax */
38 ax += 0x883126e9 - 0xf22d0e56; /* add eax,0883126e9h-0f22d0e56h */
39 seed[1] = ax; /* mov seed+4,eax */
40 ax += 0xc624dd2f - 0x883126e9; /* add eax,0c624dd2fh-0883126e9h */
41 seed[2] = ax; /* mov seed+8,eax */
42 ax += 0x0702c49c - 0xc624dd2f; /* add eax,00702c49ch-0c624dd2fh */
43 seed[3] = ax; /* mov seed+12,eax */
44 ax += 0x9e353f7d - 0x0702c49c; /* add eax,09e353f7dh-00702c49ch */
45 seed[4] = ax; /* mov seed+16,eax */
46 ax += 0x6fdf3b64 - 0x9e353f7d; /* add eax,06fdf3b64h-09e353f7dh */
47 seed[5] = ax; /* mov seed+20,eax */
48}
49
50// Add with carry. SUM is replaced with A + B + C, C is replaced with 1 if there was a carry, 0 if there wasn't. A carry occurred if the sum is less than one of the inputs. This is addition, so carry can never be more than one.
51#define ADC(SUM, A, B, C) SUM = (A) + (B) + (C); C = ((SUM < (A)) || (SUM < (B)))
52
53unsigned int RandClass::randomValue( void )
54{
55 unsigned int ax;
56 unsigned int c = 0;
57
58
59 ADC(ax, seed[5], seed[4], c); /* mov ax,seed+20 */
60 /* add ax,seed+16 */
61 seed[4] = ax; /* mov seed+8,ax */
62
63 ADC(ax, ax, seed[3], c); /* adc ax,seed+12 */
64 seed[3] = ax; /* mov seed+12,ax */
65
66 ADC(ax, ax, seed[2], c); /* adc ax,seed+8 */
67 seed[2] = ax; /* mov seed+8,ax */
68
69 ADC(ax, ax, seed[1], c); /* adc ax,seed+4 */
70 seed[1] = ax; /* mov seed+4,ax */
71
72 ADC(ax, ax, seed[0], c); /* adc ax,seed+0 */
73 seed[0] = ax; /* mov seed+0,ax */
74
75 /* Increment seed array, bubbling up the carries. */
76 if (!++seed[5])
77 {
78 if (!++seed[4])
79 {
80 if (!++seed[3])
81 {
82 if (!++seed[2])
83 {
84 if (!++seed[1])
85 {
86 ++seed[0];
87 ++ax;
88 }
89 }
90 }
91 }
92 }
93 return(ax);
94}
95
96
98{
99 return (int)randomValue();
100}
101
102int RandClass::Int(int low, int high)
103{
104 unsigned int delta = high - low + 1;
105 int rval;
106
107 if (delta == 0)
108 return high;
109
110 rval = ((int)(randomValue()%delta)) + low;
111
112 return rval;
113}
114
116{
117 return Double(0.0, 1.0);
118}
119
120double RandClass::Double(double low, double high)
121{
122 double delta = high - low;
123 double rval;
124
125 if (delta <= 0.0)
126 return high;
127
128 rval = ((double)(randomValue()) * theMultFactor) * delta + low;
129 return rval;
130}
131
132
#define ADC(SUM, A, B, C)
RandClass(int start=0)
Definition rand.cpp:24
double Double(void)
Definition rand.cpp:115
int Int(void)
Definition rand.cpp:97