author | unc0rr |
Mon, 03 Nov 2008 09:49:25 +0000 | |
changeset 1464 | 693db7cd6f25 |
parent 1066 | 1f1b3686a2b0 |
child 2599 | c7153d2348f3 |
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 |
||
19 |
unit uRandom; |
|
20 |
interface |
|
351 | 21 |
uses uFloat; |
124 | 22 |
{$INCLUDE options.inc} |
527 | 23 |
{$INCLUDE proto.inc} |
4 | 24 |
|
102 | 25 |
procedure SetRandomSeed(Seed: shortstring); |
351 | 26 |
function GetRandom: hwFloat; overload; |
4 | 27 |
function GetRandom(m: LongWord): LongWord; overload; |
915 | 28 |
function rndSign(num: hwFloat): hwFloat; |
29 |
{$IFDEF DEBUGFILE} |
|
30 |
procedure DumpBuffer; |
|
31 |
{$ENDIF} |
|
4 | 32 |
|
33 |
implementation |
|
915 | 34 |
{$IFDEF DEBUGFILE} |
35 |
uses uMisc; |
|
36 |
{$ENDIF} |
|
102 | 37 |
var cirbuf: array[0..63] of Longword; |
124 | 38 |
n: byte = 54; |
4 | 39 |
|
102 | 40 |
function GetNext: Longword; |
4 | 41 |
begin |
102 | 42 |
n:= (n + 1) and $3F; |
43 |
cirbuf[n]:= |
|
105 | 44 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
45 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
46 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 47 |
|
351 | 48 |
GetNext:= cirbuf[n] |
102 | 49 |
end; |
50 |
||
51 |
procedure SetRandomSeed(Seed: shortstring); |
|
52 |
var i: Longword; |
|
53 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
54 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
55 |
|
124 | 56 |
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 57 |
|
527 | 58 |
for i:= 0 to Pred(Length(Seed)) do |
59 |
cirbuf[i]:= byte(Seed[i + 1]); |
|
102 | 60 |
|
124 | 61 |
for i:= Length(Seed) to 54 do |
527 | 62 |
cirbuf[i]:= $A98765 + (cNetProtoVersion * 2); // odd number |
130 | 63 |
|
64 |
for i:= 0 to 1023 do GetNext |
|
4 | 65 |
end; |
66 |
||
351 | 67 |
function GetRandom: hwFloat; |
4 | 68 |
begin |
351 | 69 |
GetNext; |
918 | 70 |
GetRandom.isNegative:= false; |
71 |
GetRandom.QWordValue:= GetNext |
|
4 | 72 |
end; |
73 |
||
102 | 74 |
function GetRandom(m: LongWord): LongWord; |
4 | 75 |
begin |
105 | 76 |
GetNext; |
351 | 77 |
GetRandom:= GetNext mod m |
4 | 78 |
end; |
79 |
||
915 | 80 |
function rndSign(num: hwFloat): hwFloat; |
81 |
begin |
|
82 |
num.isNegative:= odd(GetNext); |
|
83 |
rndSign:= num |
|
84 |
end; |
|
85 |
||
86 |
{$IFDEF DEBUGFILE} |
|
87 |
procedure DumpBuffer; |
|
88 |
var i: LongInt; |
|
89 |
begin |
|
90 |
for i:= 0 to 63 do |
|
91 |
AddFileLog('[' + inttostr(i) + '] = ' + inttostr(cirbuf[i])) |
|
92 |
end; |
|
93 |
{$ENDIF} |
|
94 |
||
4 | 95 |
end. |