hedgewars/uRandom.pas
author Wuzzy <Wuzzy2@mail.ru>
Fri, 03 Aug 2018 00:39:50 +0200
changeset 13612 212036414957
parent 13363 a79c1206bacd
permissions -rw-r--r--
Make cake bounce off bounce edge, stop cake at wrap edge to prevent other bug The "other bug" is that the cake just walks through terrain when it hits the wrap world edge. This behaviour is even worse.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     1
(*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 920
diff changeset
     2
 * Hedgewars, a free turn based strategy game
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10108
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     4
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     8
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    12
 * GNU General Public License for more details.
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    13
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    14
 * You should have received a copy of the GNU General Public License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 155
diff changeset
    15
 * along with this program; if not, write to the Free Software
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 10015
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    17
 *)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    18
2630
079ef82eac75 revamped file access and debug display
koda
parents: 2599
diff changeset
    19
{$INCLUDE "options.inc"}
079ef82eac75 revamped file access and debug display
koda
parents: 2599
diff changeset
    20
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    21
unit uRandom;
5123
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    22
(*
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    23
 * This unit supplies platform-independent functions for getting various
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    24
 * pseudo-random values based on a shared seed.
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    25
 *
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    26
 * This is necessary for accomplishing pseudo-random behavior in the game
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    27
 * without causing a desynchronisation of different clients when playing over
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    28
 * a network.
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    29
 *)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    30
interface
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 320
diff changeset
    31
uses uFloat;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    32
8912
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    33
procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean); // Sets the seed that should be used for generating pseudo-random values.
9168
20ff80421736 Some fixes to make pas2c+clang compile all engine files
unc0rr
parents: 9127
diff changeset
    34
function  GetRandomf: hwFloat; // Returns a pseudo-random hwFloat.
20ff80421736 Some fixes to make pas2c+clang compile all engine files
unc0rr
parents: 9127
diff changeset
    35
function  GetRandom(m: LongWord): LongWord; inline; // Returns a positive pseudo-random integer smaller than m.
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
    36
procedure AddRandomness(r: LongWord); inline;
5123
b0b57f247e02 guess what. comments. yay for burning commits
sheepluva
parents: 4976
diff changeset
    37
function  rndSign(num: hwFloat): hwFloat; // Returns num with a random chance of having a inverted sign.
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    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
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    40
implementation
3295
ced0ee8280ad fix building without DEBUGFILE
koda
parents: 3284
diff changeset
    41
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    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
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    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;
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7579
diff changeset
    48
   cirbuf[n]:= cirbuf[n] xor r;
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
    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
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    52
begin
11827
4600dcb9db4f - Use IntToStr() in preference to Str()
unc0rr
parents: 11046
diff changeset
    53
    n:= (n + 1) and $3F;
4600dcb9db4f - Use IntToStr() in preference to Str()
unc0rr
parents: 11046
diff changeset
    54
    cirbuf[n]:=
105
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    55
           (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    56
            cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    57
            and $7FFFFFFF;                       {mod 2^31}
136
89970b70b076 Implement bot levels
unc0rr
parents: 130
diff changeset
    58
11827
4600dcb9db4f - Use IntToStr() in preference to Str()
unc0rr
parents: 11046
diff changeset
    59
    GetNext:= cirbuf[n];
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    60
end;
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    61
8912
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    62
procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean);
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    63
var i, t, l: Longword;
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    64
begin
7577
bdbb072b38b9 initModule really not needed for uRandom
nemo
parents: 7575
diff changeset
    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
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    69
8912
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    70
t:= 0;
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    71
l:= Length(Seed);
102
c45643d3fd78 New faster random generator
unc0rr
parents: 22
diff changeset
    72
8912
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    73
while (t < l) and ((not dropAdditionalPart) or (Seed[t + 1] <> '|')) do
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    74
    begin
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    75
    cirbuf[t]:= byte(Seed[t + 1]);
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    76
    inc(t)
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    77
    end;
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    78
78ea1839aac9 Implement issue 308 with '|' as separator
unc0rr
parents: 7579
diff changeset
    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
19e3c16fb9f0 Fix engine PRNG (accidentally deleted a line)
unc0rr
parents: 124
diff changeset
    81
13363
a79c1206bacd Call GetNext twice as many times to distribute seed across the whole buffer even for single byte seeds
unc0rr
parents: 11827
diff changeset
    82
for i:= 0 to 2047 do
8026
4a4f21070479 merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents: 7579
diff changeset
    83
   GetNext;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    84
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    85
7001
89488f5b99ca GetRandom -> GetRandomf
unc0rr
parents: 6700
diff changeset
    86
function GetRandomf: hwFloat;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    87
begin
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 320
diff changeset
    88
GetNext;
7001
89488f5b99ca GetRandom -> GetRandomf
unc0rr
parents: 6700
diff changeset
    89
GetRandomf.isNegative:= false;
89488f5b99ca GetRandom -> GetRandomf
unc0rr
parents: 6700
diff changeset
    90
GetRandomf.QWordValue:= GetNext
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    91
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    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
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    94
begin
105
e7cb9bb4a9de - Fixed integer->longint
unc0rr
parents: 102
diff changeset
    95
GetNext;
351
29bc9c36ad5f Fixed-point arithmetics in engine.
unc0rr
parents: 320
diff changeset
    96
GetRandom:= GetNext mod m
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    97
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    98
915
33040b7695c0 - Some changes in clusters initialization
unc0rr
parents: 527
diff changeset
    99
function rndSign(num: hwFloat): hwFloat;
33040b7695c0 - Some changes in clusters initialization
unc0rr
parents: 527
diff changeset
   100
begin
33040b7695c0 - Some changes in clusters initialization
unc0rr
parents: 527
diff changeset
   101
num.isNegative:= odd(GetNext);
33040b7695c0 - Some changes in clusters initialization
unc0rr
parents: 527
diff changeset
   102
rndSign:= num
33040b7695c0 - Some changes in clusters initialization
unc0rr
parents: 527
diff changeset
   103
end;
33040b7695c0 - Some changes in clusters initialization
unc0rr
parents: 527
diff changeset
   104
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   105
end.