author | unc0rr |
Sat, 25 Sep 2010 23:01:52 +0400 | |
changeset 3901 | 124b4755914b |
parent 3407 | dcc129c4352e |
child 4363 | e944cc43f7a4 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
3236
4ab3917d7d44
Update (c) lines to 2010 as unc0rr requested - they all had varying values so I just took the first year mentioned, then tacked on -2010
nemo
parents:
3038
diff
changeset
|
3 |
* Copyright (c) 2004-2010 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; |
33 |
{$IFDEF DEBUGFILE} |
|
34 |
procedure DumpBuffer; |
|
35 |
{$ENDIF} |
|
4 | 36 |
|
37 |
implementation |
|
3407 | 38 |
uses uMisc; |
3295 | 39 |
|
102 | 40 |
var cirbuf: array[0..63] of Longword; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
41 |
n: byte; |
4 | 42 |
|
102 | 43 |
function GetNext: Longword; |
4 | 44 |
begin |
102 | 45 |
n:= (n + 1) and $3F; |
46 |
cirbuf[n]:= |
|
105 | 47 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
48 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
49 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 50 |
|
351 | 51 |
GetNext:= cirbuf[n] |
102 | 52 |
end; |
53 |
||
54 |
procedure SetRandomSeed(Seed: shortstring); |
|
55 |
var i: Longword; |
|
56 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
57 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
58 |
|
124 | 59 |
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 60 |
|
527 | 61 |
for i:= 0 to Pred(Length(Seed)) do |
62 |
cirbuf[i]:= byte(Seed[i + 1]); |
|
102 | 63 |
|
124 | 64 |
for i:= Length(Seed) to 54 do |
527 | 65 |
cirbuf[i]:= $A98765 + (cNetProtoVersion * 2); // odd number |
130 | 66 |
|
67 |
for i:= 0 to 1023 do GetNext |
|
4 | 68 |
end; |
69 |
||
351 | 70 |
function GetRandom: hwFloat; |
4 | 71 |
begin |
351 | 72 |
GetNext; |
918 | 73 |
GetRandom.isNegative:= false; |
74 |
GetRandom.QWordValue:= GetNext |
|
4 | 75 |
end; |
76 |
||
102 | 77 |
function GetRandom(m: LongWord): LongWord; |
4 | 78 |
begin |
3284 | 79 |
TryDo((m > 0),'GetRandom(0) called! Please report this to the developers!',true); |
105 | 80 |
GetNext; |
351 | 81 |
GetRandom:= GetNext mod m |
4 | 82 |
end; |
83 |
||
915 | 84 |
function rndSign(num: hwFloat): hwFloat; |
85 |
begin |
|
86 |
num.isNegative:= odd(GetNext); |
|
87 |
rndSign:= num |
|
88 |
end; |
|
89 |
||
90 |
{$IFDEF DEBUGFILE} |
|
91 |
procedure DumpBuffer; |
|
92 |
var i: LongInt; |
|
93 |
begin |
|
94 |
for i:= 0 to 63 do |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2924
diff
changeset
|
95 |
AddFileLog('[' + inttostr(i) + '] = ' + inttostr(cirbuf[i])) |
915 | 96 |
end; |
97 |
{$ENDIF} |
|
98 |
||
3038 | 99 |
procedure initModule; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
100 |
begin |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2924
diff
changeset
|
101 |
n:= 54; |
3369
c7289e42f0ee
add other controls for map preview, also fix a bug in digest
koda
parents:
3295
diff
changeset
|
102 |
FillChar(cirbuf, 64*sizeof(Longword), 0); |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
103 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
104 |
|
3038 | 105 |
procedure freeModule; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
106 |
begin |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
107 |
|
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
108 |
end; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
109 |
|
4 | 110 |
end. |