author | Periklis Ntanasis <pntanasis@gmail.com> |
Sat, 10 Aug 2013 20:44:26 +0300 | |
branch | spacecampaign |
changeset 9512 | 9686c9116020 |
parent 9080 | 9b42757d7e71 |
child 9127 | e350500c4edb |
child 9998 | 736015b847e3 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
9080 | 3 |
* Copyright (c) 2004-2013 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; |
4 | 32 |
|
8912 | 33 |
procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean); // Sets the seed that should be used for generating pseudo-random values. |
7001 | 34 |
function GetRandomf: hwFloat; overload; // Returns a pseudo-random hwFloat. |
7389
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
35 |
function GetRandom(m: LongWord): LongWord; overload; inline; // Returns a positive pseudo-random integer smaller than m. |
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
36 |
procedure AddRandomness(r: LongWord); inline; |
5123 | 37 |
function rndSign(num: hwFloat): hwFloat; // Returns num with a random chance of having a inverted sign. |
4 | 38 |
|
7389
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
39 |
|
4 | 40 |
implementation |
3295 | 41 |
|
102 | 42 |
var cirbuf: array[0..63] of Longword; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
43 |
n: byte; |
4 | 44 |
|
7389
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
45 |
procedure AddRandomness(r: LongWord); inline; |
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
46 |
begin |
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
47 |
n:= (n + 1) and $3F; |
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
48 |
cirbuf[n]:= cirbuf[n] xor r |
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
49 |
end; |
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
50 |
|
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
51 |
function GetNext: Longword; inline; |
4 | 52 |
begin |
102 | 53 |
n:= (n + 1) and $3F; |
54 |
cirbuf[n]:= |
|
105 | 55 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
56 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
57 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 58 |
|
351 | 59 |
GetNext:= cirbuf[n] |
102 | 60 |
end; |
61 |
||
8912 | 62 |
procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean); |
63 |
var i, t, l: Longword; |
|
102 | 64 |
begin |
7577 | 65 |
n:= 54; |
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
66 |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
5123
diff
changeset
|
67 |
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
|
68 |
Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 69 |
|
8912 | 70 |
t:= 0; |
71 |
l:= Length(Seed); |
|
102 | 72 |
|
8912 | 73 |
while (t < l) and ((not dropAdditionalPart) or (Seed[t + 1] <> '|')) do |
74 |
begin |
|
75 |
cirbuf[t]:= byte(Seed[t + 1]); |
|
76 |
inc(t) |
|
77 |
end; |
|
78 |
||
79 |
for i:= t 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
|
80 |
cirbuf[i]:= $A98765 + 68; // odd number |
130 | 81 |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
5123
diff
changeset
|
82 |
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
|
83 |
GetNext |
4 | 84 |
end; |
85 |
||
7001 | 86 |
function GetRandomf: hwFloat; |
4 | 87 |
begin |
351 | 88 |
GetNext; |
7001 | 89 |
GetRandomf.isNegative:= false; |
90 |
GetRandomf.QWordValue:= GetNext |
|
4 | 91 |
end; |
92 |
||
7389
15c3fb4882df
Sorry about the slight delay in pickup. You can blame a few lame cheaters. This is to make their cheating a bit harder.
nemo
parents:
7043
diff
changeset
|
93 |
function GetRandom(m: LongWord): LongWord; inline; |
4 | 94 |
begin |
105 | 95 |
GetNext; |
351 | 96 |
GetRandom:= GetNext mod m |
4 | 97 |
end; |
98 |
||
915 | 99 |
function rndSign(num: hwFloat): hwFloat; |
100 |
begin |
|
101 |
num.isNegative:= odd(GetNext); |
|
102 |
rndSign:= num |
|
103 |
end; |
|
104 |
||
4 | 105 |
end. |