]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CGame.cpp
c371ad9ee6340fe8bf25499f33ba9944fca192da
[quix0rs-blobwars.git] / src / CGame.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 Game::Game()
24 {
25         musicVol = 100;
26         soundVol = 128;
27         output = 2;
28         brightness = 10;
29
30         gore = 1;
31         skill = 1;
32 #ifdef SDL_FRAMEWORK
33         float r,g,b;
34         if (SDL_GetGamma(&r, &g, &b) != -1) {
35                 if (r != g || g != b) {
36                         brightness = -1;
37                 } else {
38                         brightness = 10 * r;
39                 }
40         }
41 #endif
42
43         clear();
44 }
45
46 void Game::clear()
47 {
48         hasAquaLung = hasJetPack = continueFromCheckPoint = false;
49         score = stagesCleared = 0;
50         totalHours = totalMinutes = totalSeconds = 0;
51         currentMissionHours = currentMissionMinutes = currentMissionSeconds = 0;
52         totalEnemiesDefeated = totalItemsCollected = totalBonusesCollected = 0;
53         totalObjectivesCompleted = totalMIAsRescued = 0;
54         currentMissionEnemiesDefeated = currentMissionItemsCollected = 0;
55         stagesCleared = 0;
56
57         lastComboTime = 0;
58         currentComboHits = maxComboHits = 0;
59         
60         autoSave = 1;
61         autoSaveSlot = 0;
62
63         currentWeapon = 0;
64         for (int i = 0 ; i < 5 ; i++)
65         {
66                 bulletsHit[i] = bulletsFired[i] = 0;
67         }
68
69         strlcpy(mapName, "data/grasslands1", sizeof mapName);
70         strlcpy(stageName, "Grasslands", sizeof stageName);
71
72         continuesUsed = 0;
73         levelsStarted = 0;
74         escapes = 0;
75
76         canContinue = 0;
77 }
78
79 void Game::destroy()
80 {
81         clear();
82 }
83
84 void Game::incrementMissionTime()
85 {
86         currentMissionSeconds++;
87         if (currentMissionSeconds == 60)
88         {
89                 currentMissionSeconds = 0;
90                 currentMissionMinutes++;
91                 if (currentMissionMinutes == 60)
92                 {
93                         currentMissionMinutes = 0;
94                         currentMissionHours++;
95                 }
96         }
97 }
98
99 void Game::setObjectiveCheckPoint()
100 {
101         objectiveCheckPointX = checkPointX;
102         objectiveCheckPointY = checkPointY;
103         canContinue = 3;
104 }
105
106 void Game::useObjectiveCheckPoint()
107 {
108         checkPointX = objectiveCheckPointX;
109         checkPointY = objectiveCheckPointY;
110         continuesUsed++;
111         canContinue--;
112 }
113
114 void Game::setCheckPoint(float x, float y)
115 {
116         checkPointX = (int)x;
117         checkPointY = (int)y;
118 }
119
120 void Game::getCheckPoint(float *x, float *y) const
121 {
122         *x = checkPointX;
123         *y = checkPointY;
124 }
125
126 void Game::doCombo()
127 {
128         if (lastComboTime == 0)
129                 currentComboHits = 0;
130
131         currentComboHits++;
132
133         if (currentComboHits > maxComboHits)
134                 maxComboHits = currentComboHits;
135
136         lastComboTime = 25;
137
138         Math::limitChar(&maxComboHits, 0, 99);
139         Math::limitChar(&currentComboHits, 0, 99);
140 }
141
142 void Game::incBulletsFired()
143 {
144         bulletsFired[currentWeapon]++;
145 }
146
147 void Game::incBulletsHit()
148 {
149         bulletsHit[currentWeapon]++;
150 }
151
152 int Game::getWeaponAccuracy(int weapon)
153 {
154         if (bulletsHit[weapon])
155         {
156                 return (int)(((0.0 + bulletsHit[weapon]) / (0.0 + bulletsFired[weapon])) * 100.0);
157         }
158
159         return 0;
160 }
161
162 int Game::getTotalBulletsFired() const
163 {
164         return bulletsFired[0] + bulletsFired[1] + bulletsFired[2] + bulletsFired[3] + bulletsFired[4];
165 }
166
167 int Game::getTotalAccuracy()
168 {
169         int fired = getTotalBulletsFired();
170         int hits = bulletsHit[0] + bulletsHit[1] + bulletsHit[2] + bulletsHit[3] + bulletsHit[4];
171
172         if (hits)
173         {
174                 return (int)(((0.0 + hits) / (0.0 + fired)) * 100.0);
175         }
176
177         return 0;
178 }
179
180 int Game::getMostUsedWeapon()
181 {
182         unsigned int mostUsed = 0;
183         unsigned int mostFired = 0;
184
185         for (int i = 0 ; i < 5 ; i++)
186         {
187                 if (bulletsFired[i] > mostFired)
188                 {
189                         mostUsed = i;
190                         mostFired = bulletsFired[i];
191                 }
192         }
193
194         return mostUsed;
195 }
196
197 void Game::totalUpStats()
198 {
199         totalEnemiesDefeated += currentMissionEnemiesDefeated;
200         totalItemsCollected += currentMissionItemsCollected;
201
202         totalSeconds += currentMissionSeconds;
203         totalMinutes += currentMissionMinutes;
204         totalHours += currentMissionHours;
205
206         while (totalSeconds > 59)
207         {
208                 totalSeconds -= 60;
209                 totalMinutes++;
210         }
211
212         while (totalMinutes > 59)
213         {
214                 totalMinutes -= 60;
215                 totalHours++;
216         }
217
218         currentMissionEnemiesDefeated = currentMissionItemsCollected = 0;
219         currentMissionSeconds = currentMissionMinutes = currentMissionHours = 0;
220 }
221
222 void Game::setStageName(const char *name)
223 {
224         strlcpy(stageName, name, sizeof stageName);
225 }
226
227 void Game::setMapName(const char *name)
228 {
229         strlcpy(mapName, name, sizeof mapName);
230 }
231
232 void Game::setMissionOver(int reason)
233 {
234         if ((missionOver == 0) || (reason == MIS_TIMEUP))
235         {
236                 switch (reason)
237                 {
238                         case MIS_COMPLETE:
239                                 missionOver = MAX_FPS;
240                                 break;
241                         case MIS_PLAYEROUT:
242                                 missionOver = (int)(MAX_FPS * 2.5);
243                                 break;
244                         case MIS_TIMEUP:
245                         case MIS_PLAYERDEAD:
246                                 missionOver = MAX_FPS * 5;
247                                 break;
248                         case MIS_PLAYERESCAPE:
249                                 missionOver = MAX_FPS * 2;
250                                 break;
251                         case MIS_GAMECOMPLETE:
252                                 missionOver = MAX_FPS * 8;
253                                 break;
254                         default:
255                                 missionOver = MAX_FPS;
256                                 break;
257                 }
258         }
259
260         missionOverReason = reason;
261 }
262
263 void Game::resetMissionOver()
264 {
265         missionOver = missionOverReason = 0;
266 }