author | nemo |
Fri, 25 Mar 2011 19:25:50 -0400 | |
changeset 5049 | 96e51a62fb7f |
parent 4976 | 088d40d8aba2 |
child 5123 | b0b57f247e02 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
4976 | 3 |
* Copyright (c) 2004-2011 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 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; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
2630 | 19 |
{$INCLUDE "options.inc"} |
20 |
||
4 | 21 |
unit uRandom; |
22 |
interface |
|
351 | 23 |
uses uFloat; |
2924 | 24 |
{$INCLUDE "config.inc"} |
4 | 25 |
|
3038 | 26 |
procedure initModule; |
27 |
procedure freeModule; |
|
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
28 |
|
102 | 29 |
procedure SetRandomSeed(Seed: shortstring); |
351 | 30 |
function GetRandom: hwFloat; overload; |
4 | 31 |
function GetRandom(m: LongWord): LongWord; overload; |
915 | 32 |
function rndSign(num: hwFloat): hwFloat; |
4 | 33 |
|
34 |
implementation |
|
3295 | 35 |
|
102 | 36 |
var cirbuf: array[0..63] of Longword; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
37 |
n: byte; |
4 | 38 |
|
102 | 39 |
function GetNext: Longword; |
4 | 40 |
begin |
102 | 41 |
n:= (n + 1) and $3F; |
42 |
cirbuf[n]:= |
|
105 | 43 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
44 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
45 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 46 |
|
351 | 47 |
GetNext:= cirbuf[n] |
102 | 48 |
end; |
49 |
||
50 |
procedure SetRandomSeed(Seed: shortstring); |
|
51 |
var i: Longword; |
|
52 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
53 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
54 |
|
124 | 55 |
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 56 |
|
527 | 57 |
for i:= 0 to Pred(Length(Seed)) do |
58 |
cirbuf[i]:= byte(Seed[i + 1]); |
|
102 | 59 |
|
124 | 60 |
for i:= Length(Seed) to 54 do |
4665
fa7ad5f3725f
Make basic training solvable again. Freeze RNG at current version for less of this kind of issue in future, and a bit more savable of seeds. Disable offsets in preparation for release.
nemo
parents:
4363
diff
changeset
|
61 |
cirbuf[i]:= $A98765 + 68; // odd number |
130 | 62 |
|
63 |
for i:= 0 to 1023 do GetNext |
|
4 | 64 |
end; |
65 |
||
351 | 66 |
function GetRandom: hwFloat; |
4 | 67 |
begin |
351 | 68 |
GetNext; |
918 | 69 |
GetRandom.isNegative:= false; |
70 |
GetRandom.QWordValue:= GetNext |
|
4 | 71 |
end; |
72 |
||
102 | 73 |
function GetRandom(m: LongWord): LongWord; |
4 | 74 |
begin |
105 | 75 |
GetNext; |
351 | 76 |
GetRandom:= GetNext mod m |
4 | 77 |
end; |
78 |
||
915 | 79 |
function rndSign(num: hwFloat): hwFloat; |
80 |
begin |
|
81 |
num.isNegative:= odd(GetNext); |
|
82 |
rndSign:= num |
|
83 |
end; |
|
84 |
||
3038 | 85 |
procedure initModule; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
86 |
begin |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2924
diff
changeset
|
87 |
n:= 54; |
3369
c7289e42f0ee
add other controls for map preview, also fix a bug in digest
koda
parents:
3295
diff
changeset
|
88 |
FillChar(cirbuf, 64*sizeof(Longword), 0); |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
89 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
90 |
|
3038 | 91 |
procedure freeModule; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
92 |
begin |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
93 |
|
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
94 |
end; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
95 |
|
4 | 96 |
end. |