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