-
Notifications
You must be signed in to change notification settings - Fork 4
/
Util.cpp
120 lines (99 loc) · 1.93 KB
/
Util.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
110
111
112
113
114
115
116
117
118
119
120
//
// Util.cpp - common utilities library
//
// Created 5/24/2 Jimbo S. Harris
//
#ifdef __WIN32__
#include <windows.h>
#endif //__WIN32__
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "JMDefs.h"
#include "Util.h"
namespace Util
{
// The RNG in all its glory
float GetRandom( float lo, float hi )
{
float mult = hi - lo;
float num;
num = ((float)rand() / RAND_MAX);
num = lo + mult * num;
return( num );
}
// In case we ever need a random int
int GetRandom( int lo, int hi )
{
// deal with coinflip "getrandom(0,1)"
if( (hi - lo) == 1 )
{
return( (rand() & 1) ? hi : lo );
}
int num;
num = rand();
num %= ( hi - lo + 1 );
num += lo;
return( num );
}
// Roll some dice
float Roll( int dice, int sides )
{
if( sides <= 0 )
{
return 0.0f;
}
float total = 0.0f;
int die;
for( die = 0; die < dice; die++ )
{
total += GetRandom( 1.0f, (float)sides );
}
return total;
}
// function overload; pass in "2d5"
float Roll( const char *szFormat )
{
if( !szFormat || *szFormat == nul )
{
// You passed in an empty or NULL string!
return 0.0f;
}
char *szToken;
int dice;
int sides;
char *c;
szToken = new char[strlen(szFormat)+1];
strcpy( szToken, szFormat );
c = strtok( szToken, "d" );
if( c == NULL )
{
// You passed in a malformed string ( no 'd' )
return 0.0f;
}
dice = atoi( c );
c = strtok( NULL, "d" );
if( c == NULL )
{
// You passed in a malformed string (nothing after the 'd')
return 0.0f;
}
sides = atoi( c );
return Roll( dice, sides );
}
bool IsInWorld( JFVector vIn )
{
return vIn.IsInWorld();
}
bool IsInWorld( JRect rcIn )
{
return rcIn.IsInWorld();
}
unsigned int GetTickCount()
{
struct timeval tv;
if(gettimeofday(&tv, NULL) != 0)
return 0;
return (unsigned int)((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
}
}