src/core/random_func.cpp
author rubidium
Sun, 11 May 2008 14:09:38 +0000
changeset 10501 10f2642f41dc
parent 10429 1b99254f9607
permissions -rw-r--r--
(svn r13044) -Fix: deleting an already deleted window.
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     1
/* $Id$ */
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     2
10429
1b99254f9607 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9288
diff changeset
     3
/** @file random_func.cpp Implementation of the pseudo random generator. */
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     4
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     5
#include "../stdafx.h"
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     6
#include "random_func.hpp"
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8575
diff changeset
     7
#include "bitmath_func.hpp"
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     8
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
     9
Randomizer _random, _interactive_random;
8638
d997e3a75b36 (svn r11704) -Codechange: remove another bunch of useless includes.
rubidium
parents: 8609
diff changeset
    10
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    11
uint32 Randomizer::Next()
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    12
{
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    13
	const uint32 s = this->state[0];
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    14
	const uint32 t = this->state[1];
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    15
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    16
	this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    17
	return this->state[1] = ROR(s, 3) - 1;
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    18
}
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    19
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    20
uint32 Randomizer::Next(uint16 max)
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    21
{
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    22
	return GB(this->Next(), 0, 16) * max >> 16;
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    23
}
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    24
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    25
void Randomizer::SetSeed(uint32 seed)
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    26
{
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    27
	this->state[0] = seed;
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    28
	this->state[1] = seed;
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    29
}
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    30
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    31
void SetRandomSeed(uint32 seed)
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    32
{
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    33
	_random.SetSeed(seed);
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    34
	_interactive_random.SetSeed(seed * 0x1234567);
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    35
}
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    36
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    37
#ifdef RANDOM_DEBUG
8575
dfc40de58c04 (svn r11640) -Fix: missed change of include when moving stuff to random_func.cpp
rubidium
parents: 8432
diff changeset
    38
#include "../network/network_data.h"
8973
25542dd165b2 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8930
diff changeset
    39
#include "../variables.h" /* _frame_counter */
25542dd165b2 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8930
diff changeset
    40
#include "../player_func.h"
25542dd165b2 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8930
diff changeset
    41
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    42
uint32 DoRandom(int line, const char *file)
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    43
{
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    44
	if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) {
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    45
		printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line);
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    46
	}
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    47
8973
25542dd165b2 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8930
diff changeset
    48
	return _random.Next();
8930
361433723616 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8638
diff changeset
    49
}
8431
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    50
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    51
uint DoRandomRange(uint max, int line, const char *file)
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    52
{
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    53
	return GB(DoRandom(line, file), 0, 16) * max >> 16;
68fb2ccbce06 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    54
}
9288
bf11c6775f84 (svn r12516) -Revert r2583: Removed mersenne PRNG cause it is not used and won't be used in the future
skidd13
parents: 8973
diff changeset
    55
#endif /* RANDOM_DEBUG */