src/core/random_func.cpp
author rubidium
Sun, 25 May 2008 19:17:03 +0000
changeset 9354 845e07db4549
parent 9111 48ce04029fe4
child 10207 c291a21b304e
permissions -rw-r--r--
(svn r13251) -Codechange: rename _patches to _settings as that is more logic.
-Codechange: move all Settings into substructs of _settings in a way that they are logically grouped.
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     1
/* $Id$ */
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     2
9111
48ce04029fe4 (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: 8792
diff changeset
     3
/** @file random_func.cpp Implementation of the pseudo random generator. */
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     4
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     5
#include "../stdafx.h"
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     6
#include "random_func.hpp"
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8079
diff changeset
     7
#include "bitmath_func.hpp"
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
     8
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
     9
Randomizer _random, _interactive_random;
8142
837f47089a7c (svn r11704) -Codechange: remove another bunch of useless includes.
rubidium
parents: 8113
diff changeset
    10
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    11
uint32 Randomizer::Next()
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    12
{
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    13
	const uint32 s = this->state[0];
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    14
	const uint32 t = this->state[1];
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    15
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    16
	this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    17
	return this->state[1] = ROR(s, 3) - 1;
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    18
}
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    19
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    20
uint32 Randomizer::Next(uint16 max)
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    21
{
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    22
	return GB(this->Next(), 0, 16) * max >> 16;
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    23
}
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    24
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    25
void Randomizer::SetSeed(uint32 seed)
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    26
{
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    27
	this->state[0] = seed;
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    28
	this->state[1] = seed;
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    29
}
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    30
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    31
void SetRandomSeed(uint32 seed)
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    32
{
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    33
	_random.SetSeed(seed);
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    34
	_interactive_random.SetSeed(seed * 0x1234567);
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    35
}
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    36
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    37
#ifdef RANDOM_DEBUG
8079
8ed263cb705a (svn r11640) -Fix: missed change of include when moving stuff to random_func.cpp
rubidium
parents: 7936
diff changeset
    38
#include "../network/network_data.h"
8477
a434fd630863 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8434
diff changeset
    39
#include "../variables.h" /* _frame_counter */
a434fd630863 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8434
diff changeset
    40
#include "../player_func.h"
a434fd630863 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8434
diff changeset
    41
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    42
uint32 DoRandom(int line, const char *file)
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    43
{
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    44
	if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) {
7935
c2d1b2f4ecd6 (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);
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    46
	}
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    47
8477
a434fd630863 (svn r12050) -Fix: random_func broke for desync debug.
rubidium
parents: 8434
diff changeset
    48
	return _random.Next();
8434
558c39956ba2 (svn r12004) -Codechange: refactor the random functions to reduce code duplication.
rubidium
parents: 8142
diff changeset
    49
}
7935
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    50
c2d1b2f4ecd6 (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)
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    52
{
c2d1b2f4ecd6 (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;
c2d1b2f4ecd6 (svn r11488) -Codechange: Spilt the random functions out to seperate file
skidd13
parents:
diff changeset
    54
}
8792
fab648592d6e (svn r12516) -Revert r2583: Removed mersenne PRNG cause it is not used and won't be used in the future
skidd13
parents: 8477
diff changeset
    55
#endif /* RANDOM_DEBUG */