-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.cpp
110 lines (83 loc) · 2 KB
/
random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// random.c
#include "stdafx.h"
#include <stdlib.h>
#include "main.h"
#include "random.h"
unsigned int randomSeed[2], pieceCount[2], grenadeTimer[2];
int pieceMap[kBlobTypes], numPieces;
static unsigned int internalRandomSeed = 1;
static int internalRandom() // uses a generic rand() algorithm
{
internalRandomSeed = internalRandomSeed * 1103515245 + 12345;
return ((internalRandomSeed >> 16) & 0x7FFF);
}
void InitRandom( int inNumPieces )
{
int count, swap, swapWith;
numPieces = inNumPieces;
randomSeed[0] = randomSeed[1] = SDL_GetTicks();
pieceCount[0] = pieceCount[1] = 0;
grenadeTimer[0] = grenadeTimer[1] = 40;
for( count=0; count<kBlobTypes; count++ )
{
pieceMap[count] = count+1;
}
for( count=0; count<kBlobTypes; count++ )
{
swapWith = RandomBefore( kBlobTypes );
swap = pieceMap[swapWith];
pieceMap[swapWith] = pieceMap[count];
pieceMap[count] = swap;
}
}
void AddExtraPiece( void )
{
numPieces++;
}
int GetPiece( int player )
{
int result;
unsigned int realSeed;
realSeed = internalRandomSeed;
internalRandomSeed = randomSeed[player];
result = pieceMap[RandomBefore(numPieces)];
randomSeed[player] = internalRandomSeed;
internalRandomSeed = realSeed;
return result;
}
int GetMagic( int player )
{
int result;
int realSeed;
realSeed = internalRandomSeed;
internalRandomSeed = randomSeed[player];
result = (RandomBefore(19) == 0)? true: false;
randomSeed[player] = internalRandomSeed;
internalRandomSeed = realSeed;
return result;
}
int GetGrenade( int player )
{
pieceCount[player]++;
if( pieceCount[player] == grenadeTimer[player] )
{
grenadeTimer[player] += grenadeTimer[player] * 3 / 2;
return true;
}
else
{
return false;
}
}
static inline int RandomRange( double min, double max )
{
const double kMinRand = 0.0;
const double kMaxRand = 32767.0;
double x;
x = (internalRandom() - kMinRand) / (kMaxRand - kMinRand + 1.0);
return (int) (x * (max + 1.0 - min) + min);
}
int RandomBefore( int what )
{
return RandomRange( 0.0, what-1 );
}