author | koda |
Fri, 17 Feb 2012 18:23:36 +0100 | |
changeset 6700 | e04da46ee43c |
parent 6580 | 6155187bf599 |
child 7001 | 89488f5b99ca |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
6700 | 3 |
* Copyright (c) 2004-2012 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; |
5123 | 22 |
(* |
23 |
* This unit supplies platform-independent functions for getting various |
|
24 |
* pseudo-random values based on a shared seed. |
|
25 |
* |
|
26 |
* This is necessary for accomplishing pseudo-random behavior in the game |
|
27 |
* without causing a desynchronisation of different clients when playing over |
|
28 |
* a network. |
|
29 |
*) |
|
4 | 30 |
interface |
351 | 31 |
uses uFloat; |
2924 | 32 |
{$INCLUDE "config.inc"} |
4 | 33 |
|
3038 | 34 |
procedure initModule; |
35 |
procedure freeModule; |
|
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
36 |
|
5123 | 37 |
procedure SetRandomSeed(Seed: shortstring); // Sets the seed that should be used for generating pseudo-random values. |
38 |
function GetRandom: hwFloat; overload; // Returns a pseudo-random hwFloat. |
|
39 |
function GetRandom(m: LongWord): LongWord; overload; // Returns a positive pseudo-random integer smaller than m. |
|
40 |
function rndSign(num: hwFloat): hwFloat; // Returns num with a random chance of having a inverted sign. |
|
4 | 41 |
|
42 |
implementation |
|
3295 | 43 |
|
102 | 44 |
var cirbuf: array[0..63] of Longword; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
45 |
n: byte; |
4 | 46 |
|
102 | 47 |
function GetNext: Longword; |
4 | 48 |
begin |
102 | 49 |
n:= (n + 1) and $3F; |
50 |
cirbuf[n]:= |
|
105 | 51 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
52 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
53 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 54 |
|
351 | 55 |
GetNext:= cirbuf[n] |
102 | 56 |
end; |
57 |
||
58 |
procedure SetRandomSeed(Seed: shortstring); |
|
59 |
var i: Longword; |
|
60 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
61 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
62 |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
5123
diff
changeset
|
63 |
if Length(Seed) > 54 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
5123
diff
changeset
|
64 |
Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 65 |
|
527 | 66 |
for i:= 0 to Pred(Length(Seed)) do |
67 |
cirbuf[i]:= byte(Seed[i + 1]); |
|
102 | 68 |
|
124 | 69 |
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
|
70 |
cirbuf[i]:= $A98765 + 68; // odd number |
130 | 71 |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
5123
diff
changeset
|
72 |
for i:= 0 to 1023 do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
5123
diff
changeset
|
73 |
GetNext |
4 | 74 |
end; |
75 |
||
351 | 76 |
function GetRandom: hwFloat; |
4 | 77 |
begin |
351 | 78 |
GetNext; |
918 | 79 |
GetRandom.isNegative:= false; |
80 |
GetRandom.QWordValue:= GetNext |
|
4 | 81 |
end; |
82 |
||
102 | 83 |
function GetRandom(m: LongWord): LongWord; |
4 | 84 |
begin |
105 | 85 |
GetNext; |
351 | 86 |
GetRandom:= GetNext mod m |
4 | 87 |
end; |
88 |
||
915 | 89 |
function rndSign(num: hwFloat): hwFloat; |
90 |
begin |
|
91 |
num.isNegative:= odd(GetNext); |
|
92 |
rndSign:= num |
|
93 |
end; |
|
94 |
||
3038 | 95 |
procedure initModule; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
96 |
begin |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2924
diff
changeset
|
97 |
n:= 54; |
3369
c7289e42f0ee
add other controls for map preview, also fix a bug in digest
koda
parents:
3295
diff
changeset
|
98 |
FillChar(cirbuf, 64*sizeof(Longword), 0); |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
99 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
100 |
|
3038 | 101 |
procedure freeModule; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
102 |
begin |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
103 |
|
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
104 |
end; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
105 |
|
4 | 106 |
end. |