author | nemo |
Wed, 24 Feb 2010 03:03:43 +0000 | |
changeset 2856 | cc70797a0b75 |
parent 2716 | b9ca1bfca24f |
child 2924 | 908851e59958 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
393 | 3 |
* Copyright (c) 2004-2007 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; |
2599 | 24 |
{$INCLUDE "proto.inc"} |
4 | 25 |
|
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
26 |
procedure init_uRandom; |
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
27 |
procedure free_uRandom; |
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 |
|
915 | 38 |
{$IFDEF DEBUGFILE} |
39 |
uses uMisc; |
|
40 |
{$ENDIF} |
|
102 | 41 |
var cirbuf: array[0..63] of Longword; |
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
42 |
n: byte; |
4 | 43 |
|
102 | 44 |
function GetNext: Longword; |
4 | 45 |
begin |
102 | 46 |
n:= (n + 1) and $3F; |
47 |
cirbuf[n]:= |
|
105 | 48 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
49 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
50 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 51 |
|
351 | 52 |
GetNext:= cirbuf[n] |
102 | 53 |
end; |
54 |
||
55 |
procedure SetRandomSeed(Seed: shortstring); |
|
56 |
var i: Longword; |
|
57 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
58 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
59 |
|
124 | 60 |
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 61 |
|
527 | 62 |
for i:= 0 to Pred(Length(Seed)) do |
63 |
cirbuf[i]:= byte(Seed[i + 1]); |
|
102 | 64 |
|
124 | 65 |
for i:= Length(Seed) to 54 do |
527 | 66 |
cirbuf[i]:= $A98765 + (cNetProtoVersion * 2); // odd number |
130 | 67 |
|
68 |
for i:= 0 to 1023 do GetNext |
|
4 | 69 |
end; |
70 |
||
351 | 71 |
function GetRandom: hwFloat; |
4 | 72 |
begin |
351 | 73 |
GetNext; |
918 | 74 |
GetRandom.isNegative:= false; |
75 |
GetRandom.QWordValue:= GetNext |
|
4 | 76 |
end; |
77 |
||
102 | 78 |
function GetRandom(m: LongWord): LongWord; |
4 | 79 |
begin |
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 |
|
95 |
AddFileLog('[' + inttostr(i) + '] = ' + inttostr(cirbuf[i])) |
|
96 |
end; |
|
97 |
{$ENDIF} |
|
98 |
||
2699
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
99 |
procedure init_uRandom; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
100 |
begin |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
101 |
n:= 54; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
102 |
end; |
249adefa9c1c
replace initialization/finalization statements with custom init functions
koda
parents:
2630
diff
changeset
|
103 |
|
2716
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
104 |
procedure free_uRandom; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
105 |
begin |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
106 |
|
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
107 |
end; |
b9ca1bfca24f
complete the replacement of init/free wrappers for every unit
koda
parents:
2699
diff
changeset
|
108 |
|
4 | 109 |
end. |