]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CMath.cpp
Prevent a segmentation fault when using the -map option without specifying a map.
[quix0rs-blobwars.git] / src / CMath.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "headers.h"
23
24 void Math::limitChar(signed char *in, int low, int high)
25 {
26         if (*in < low)
27                 *in = low;
28         if (*in > high)
29                 *in = high;
30 }
31
32 void Math::limitChar(unsigned char *in, int low, int high)
33 {
34         if (*in < low)
35                 *in = low;
36         if (*in > high)
37                 *in = high;
38 }
39
40 void Math::limitInt(int *in, int low, int high)
41 {
42         if (*in < low)
43                 *in = low;
44         if (*in > high)
45                 *in = high;
46 }
47
48 void Math::limitFloat(float *in, float low, float high)
49 {
50         if (*in < low)
51                 *in = low;
52         if (*in > high)
53                 *in = high;
54 }
55
56 void Math::wrapChar(signed char *in, signed char low, signed char high)
57 {
58         if (*in < low)
59                 *in = high;
60         if (*in > high)
61                 *in = low;
62 }
63
64 void Math::wrapInt(int *in, int low, int high)
65 {
66         if (*in < low)
67                 *in = high;
68         if (*in > high)
69                 *in = low;
70 }
71
72 void Math::wrapFloat(float *in, float low, float high)
73 {
74         if (*in < low)
75                 *in = high;
76         if (*in > high)
77                 *in = low;
78 }
79
80 long Math::prand()
81 {
82         long k1;
83         long ix = pSeed;
84         
85         k1 = ix / 127773;
86         ix = 16807 * (ix - k1 * 127773) - k1 * 2836;
87         
88         if (ix < 0)
89         {
90                 ix += 2147483647;
91         }
92         
93         pSeed = ix;
94         
95         return pSeed;
96 }
97
98 long Math::rrand(int min, int max)
99 {
100         int r = min;
101
102         max++;
103
104         if ((max - min) == 0)
105         {
106                 return min;
107         }
108
109         return r += prand() % (max - min);
110 }
111
112 void Math::addBit(long *currentBits, long newBits)
113 {
114         if (!(*currentBits & newBits))
115                 *currentBits += newBits;
116 }
117
118 void Math::removeBit(long *currentBits, long oldBits)
119 {
120         if (*currentBits & oldBits)
121                 *currentBits -= oldBits;
122 }
123
124 void Math::calculateSlope(float x, float y, float x2, float y2, float *dx, float *dy)
125 {
126         int steps = (int)max(fabs(x - x2), fabs(y - y2));
127         if (steps == 0)
128         {
129                 *dx = 0;
130                 *dy = 0;
131                 return;
132         }
133
134         *dx = (x - x2);
135         *dx /= steps;
136         *dy = (y - y2);
137         *dy /= steps;
138 }
139
140 char *Math::formatTime(int t)
141 {
142         static char time[1024];
143         
144         time[0] = 0;
145         
146         int hours = t / 3600;
147         int minutes = (t % 3600) / 60;
148         int seconds = (t % 60);
149         
150         snprintf(time, sizeof time, "%dh %dm %ds", hours, minutes, seconds);
151         
152         return time;
153 }
154
155 long Math::pSeed;