author | unc0rr |
Sun, 20 Jan 2008 14:40:36 +0000 | |
changeset 727 | da975a75af09 |
parent 527 | e23490ce1f06 |
child 915 | 33040b7695c0 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like 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; |
28 |
||
29 |
implementation |
|
102 | 30 |
var cirbuf: array[0..63] of Longword; |
124 | 31 |
n: byte = 54; |
4 | 32 |
|
102 | 33 |
function GetNext: Longword; |
4 | 34 |
begin |
102 | 35 |
n:= (n + 1) and $3F; |
36 |
cirbuf[n]:= |
|
105 | 37 |
(cirbuf[(n + 40) and $3F] + {n - 24 mod 64} |
38 |
cirbuf[(n + 9) and $3F]) {n - 55 mod 64} |
|
39 |
and $7FFFFFFF; {mod 2^31} |
|
136 | 40 |
|
351 | 41 |
GetNext:= cirbuf[n] |
102 | 42 |
end; |
43 |
||
44 |
procedure SetRandomSeed(Seed: shortstring); |
|
45 |
var i: Longword; |
|
46 |
begin |
|
155
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
47 |
n:= 54; |
401f4ea24715
Engine can generate land preview and send it via IPC
unc0rr
parents:
136
diff
changeset
|
48 |
|
124 | 49 |
if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf |
102 | 50 |
|
527 | 51 |
for i:= 0 to Pred(Length(Seed)) do |
52 |
cirbuf[i]:= byte(Seed[i + 1]); |
|
102 | 53 |
|
124 | 54 |
for i:= Length(Seed) to 54 do |
527 | 55 |
cirbuf[i]:= $A98765 + (cNetProtoVersion * 2); // odd number |
130 | 56 |
|
57 |
for i:= 0 to 1023 do GetNext |
|
4 | 58 |
end; |
59 |
||
351 | 60 |
function GetRandom: hwFloat; |
61 |
var r: hwFloat; |
|
4 | 62 |
begin |
351 | 63 |
GetNext; |
64 |
r.isNegative:= false; |
|
65 |
r.QWordValue:= GetNext; |
|
66 |
GetRandom:= r |
|
4 | 67 |
end; |
68 |
||
102 | 69 |
function GetRandom(m: LongWord): LongWord; |
4 | 70 |
begin |
105 | 71 |
GetNext; |
351 | 72 |
GetRandom:= GetNext mod m |
4 | 73 |
end; |
74 |
||
75 |
end. |