1 | #include "Game.h"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 | #include <cstring>
|
---|
5 | #include <cstdlib>
|
---|
6 |
|
---|
7 | #include "Common.h"
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | Game::Game() {
|
---|
12 | this->id = 0;
|
---|
13 | this->name = "";
|
---|
14 | this->blueScore = 0;
|
---|
15 | this->redScore = 0;
|
---|
16 | this->worldMap = NULL;
|
---|
17 | this->msgProcessor = NULL;
|
---|
18 | }
|
---|
19 |
|
---|
20 | Game::Game(string name, string filepath, MessageProcessor* msgProcessor) {
|
---|
21 | this->id = 0;
|
---|
22 | this->name = name;
|
---|
23 | this->blueScore = 0;
|
---|
24 | this->redScore = 0;
|
---|
25 | this->worldMap = WorldMap::loadMapFromFile(filepath);
|
---|
26 | this->msgProcessor = msgProcessor;
|
---|
27 | }
|
---|
28 |
|
---|
29 | Game::~Game() {
|
---|
30 | delete this->worldMap;
|
---|
31 | }
|
---|
32 |
|
---|
33 | string Game::getName() {
|
---|
34 | return this->name;
|
---|
35 | }
|
---|
36 |
|
---|
37 | int Game::getNumPlayers() {
|
---|
38 | return this->players.size();
|
---|
39 | }
|
---|
40 |
|
---|
41 | map<unsigned int, Player*>& Game::getPlayers() {
|
---|
42 | return this->players;
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool Game::addPlayer(Player* p, bool serverSide) {
|
---|
46 | if (players.find(p->getId()) == players.end()) {
|
---|
47 | players[p->getId()] = p;
|
---|
48 |
|
---|
49 | // reset player stats, location, etc.
|
---|
50 | p->pos.x = p->target.x = 200;
|
---|
51 | p->pos.y = p->target.y = 200;
|
---|
52 | p->setTargetPlayer(0);
|
---|
53 | p->isChasing = false;
|
---|
54 | p->isAttacking = false;
|
---|
55 | p->isDead = false;
|
---|
56 | p->health = p->maxHealth;
|
---|
57 | p->hasBlueFlag = false;
|
---|
58 | p->hasRedFlag = false;
|
---|
59 |
|
---|
60 | if (serverSide) {
|
---|
61 | // choose a random team (either 0 or 1)
|
---|
62 | p->team = rand() % 2;
|
---|
63 | }
|
---|
64 |
|
---|
65 | p->currentGame = this;
|
---|
66 |
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 | else
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | bool Game::removePlayer(unsigned int id) {
|
---|
74 | if (players.erase(id) == 1)
|
---|
75 | return true;
|
---|
76 | else
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | map<unsigned int, Projectile>& Game::getProjectiles() {
|
---|
81 | return this->projectiles;
|
---|
82 | }
|
---|
83 |
|
---|
84 | bool Game::addProjectile(Projectile p) {
|
---|
85 | if (projectiles.find(p.id) == projectiles.end()) {
|
---|
86 | projectiles[p.id] = p;
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 | else
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | bool Game::removeProjectile(unsigned int id) {
|
---|
94 | if (projectiles.erase(id) == 1)
|
---|
95 | return true;
|
---|
96 | else
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | unsigned int Game::getRedScore() {
|
---|
101 | return this->redScore;
|
---|
102 | }
|
---|
103 |
|
---|
104 | unsigned int Game::getBlueScore() {
|
---|
105 | return this->blueScore;
|
---|
106 | }
|
---|
107 |
|
---|
108 | WorldMap* Game::getMap() {
|
---|
109 | return this->worldMap;
|
---|
110 | }
|
---|
111 |
|
---|
112 | void Game::setId(unsigned int id) {
|
---|
113 | this->id = id;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void Game::setRedScore(unsigned int score) {
|
---|
117 | this->redScore = score;
|
---|
118 | }
|
---|
119 |
|
---|
120 | void Game::setBlueScore(unsigned int score) {
|
---|
121 | this->blueScore = score;
|
---|
122 | }
|
---|
123 |
|
---|
124 | void Game::addObjectToMap(ObjectType objectType, int x, int y) {
|
---|
125 | NETWORK_MSG serverMsg;
|
---|
126 |
|
---|
127 | this->getMap()->addObject(objectType, x, y);
|
---|
128 |
|
---|
129 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
130 | this->worldMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
131 |
|
---|
132 | this->msgProcessor->broadcastMessage(serverMsg, this->players);
|
---|
133 | }
|
---|
134 |
|
---|
135 | bool Game::startPlayerMovement(unsigned int id, int x, int y) {
|
---|
136 | // need to check if players actually contains the id
|
---|
137 | Player* p = players[id];
|
---|
138 |
|
---|
139 | // we need to make sure the player can move here
|
---|
140 | if (0 <= x && x < this->worldMap->width*25 &&
|
---|
141 | 0 <= y && y < this->worldMap->height*25 &&
|
---|
142 | this->worldMap->getElement(x/25, y/25) == TERRAIN_GRASS)
|
---|
143 | {
|
---|
144 | p->target.x = x;
|
---|
145 | p->target.y = y;
|
---|
146 |
|
---|
147 | p->isChasing = false;
|
---|
148 | p->isAttacking = false;
|
---|
149 | p->setTargetPlayer(0);
|
---|
150 |
|
---|
151 | return true;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | return false;
|
---|
155 | }
|
---|
156 |
|
---|
157 | // returns true if the movement should be canceled
|
---|
158 | bool Game::processPlayerMovement(Player* p, FLOAT_POSITION oldPos) {
|
---|
159 |
|
---|
160 | // check if the move needs to be canceled
|
---|
161 | switch(this->worldMap->getElement(p->pos.x/25, p->pos.y/25))
|
---|
162 | {
|
---|
163 | case TERRAIN_NONE:
|
---|
164 | case TERRAIN_OCEAN:
|
---|
165 | case TERRAIN_ROCK:
|
---|
166 | {
|
---|
167 | p->pos = oldPos;
|
---|
168 | p->target.x = p->pos.x;
|
---|
169 | p->target.y = p->pos.y;
|
---|
170 | p->isChasing = false;
|
---|
171 | return true;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | default:
|
---|
175 | // if there are no obstacles, don't cancel movement
|
---|
176 | return false;
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | // returns the id of the picked-up flag or -1 if none was picked up
|
---|
182 | int Game::processFlagPickupRequest(Player* p) {
|
---|
183 | vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
|
---|
184 | vector<WorldMap::Object>::iterator it;
|
---|
185 | int itemId = -1;
|
---|
186 |
|
---|
187 | for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
|
---|
188 | if (posDistance(p->pos, it->pos.toFloat()) < 10) {
|
---|
189 | switch (it->type) {
|
---|
190 | case OBJECT_BLUE_FLAG:
|
---|
191 | if (p->team == 1) {
|
---|
192 | p->hasBlueFlag = true;
|
---|
193 | itemId = it->id;
|
---|
194 | }
|
---|
195 | break;
|
---|
196 | case OBJECT_RED_FLAG:
|
---|
197 | if (p->team == 0) {
|
---|
198 | p->hasRedFlag = true;
|
---|
199 | itemId = it->id;
|
---|
200 | }
|
---|
201 | break;
|
---|
202 | case OBJECT_NONE:
|
---|
203 | break;
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (itemId > -1) {
|
---|
207 | vctObjects->erase(it);
|
---|
208 | return itemId;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | return itemId;
|
---|
214 | }
|
---|
215 |
|
---|
216 | void Game::dealDamageToPlayer(Player* p, int damage) {
|
---|
217 | p->takeDamage(damage);
|
---|
218 |
|
---|
219 | if (p->isDead) {
|
---|
220 | ObjectType flagType = OBJECT_NONE;
|
---|
221 | if (p->hasBlueFlag)
|
---|
222 | flagType = OBJECT_BLUE_FLAG;
|
---|
223 | else if (p->hasRedFlag)
|
---|
224 | flagType = OBJECT_RED_FLAG;
|
---|
225 |
|
---|
226 | if (flagType != OBJECT_NONE)
|
---|
227 | this->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
228 | }
|
---|
229 |
|
---|
230 | // send a PLAYER message after dealing damage
|
---|
231 | NETWORK_MSG serverMsg;
|
---|
232 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
233 | p->serialize(serverMsg.buffer);
|
---|
234 | msgProcessor->broadcastMessage(serverMsg, this->players);
|
---|
235 | }
|
---|
236 |
|
---|
237 | bool Game::handleGameEvents() {
|
---|
238 | map<unsigned int, Player*>::iterator it;
|
---|
239 | bool gameFinished = false;
|
---|
240 |
|
---|
241 | for (it = this->getPlayers().begin(); it != this->getPlayers().end(); it++) {
|
---|
242 | gameFinished = gameFinished || this->handlePlayerEvents(it->second);
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (gameFinished) {
|
---|
246 | for (it = this->players.begin(); it != this->players.end(); it++) {
|
---|
247 | it->second->currentGame = NULL;
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | return gameFinished;
|
---|
252 | }
|
---|
253 |
|
---|
254 | bool Game::handlePlayerEvents(Player* p) {
|
---|
255 | NETWORK_MSG serverMsg;
|
---|
256 | FLOAT_POSITION oldPos;
|
---|
257 | bool gameFinished = false;
|
---|
258 | bool broadcastMove = false;
|
---|
259 |
|
---|
260 | cout << "moving player" << endl;
|
---|
261 |
|
---|
262 | // move player and perform associated tasks
|
---|
263 | oldPos = p->pos;
|
---|
264 | if (p->move(this->worldMap)) {
|
---|
265 |
|
---|
266 | cout << "player moved" << endl;
|
---|
267 | if (this->processPlayerMovement(p, oldPos))
|
---|
268 | broadcastMove = true;
|
---|
269 | cout << "player move processed" << endl;
|
---|
270 |
|
---|
271 | ObjectType flagType;
|
---|
272 | POSITION pos;
|
---|
273 | bool flagTurnedIn = false;
|
---|
274 | bool flagReturned = false;
|
---|
275 | bool ownFlagAtBase = false;
|
---|
276 |
|
---|
277 | switch(this->worldMap->getStructure(p->pos.x/25, p->pos.y/25)) {
|
---|
278 | case STRUCTURE_BLUE_FLAG:
|
---|
279 | {
|
---|
280 | if (p->team == 0 && p->hasRedFlag) {
|
---|
281 | // check that your flag is at your base
|
---|
282 | pos = this->worldMap->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
283 |
|
---|
284 | vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
|
---|
285 | vector<WorldMap::Object>::iterator itObjects;
|
---|
286 |
|
---|
287 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
288 | if (itObjects->type == OBJECT_BLUE_FLAG) {
|
---|
289 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
290 | ownFlagAtBase = true;
|
---|
291 | break;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (ownFlagAtBase) {
|
---|
297 | p->hasRedFlag = false;
|
---|
298 | flagType = OBJECT_RED_FLAG;
|
---|
299 | pos = this->worldMap->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
300 | flagTurnedIn = true;
|
---|
301 | this->blueScore++;
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | break;
|
---|
306 | }
|
---|
307 | case STRUCTURE_RED_FLAG:
|
---|
308 | {
|
---|
309 | if (p->team == 1 && p->hasBlueFlag) {
|
---|
310 | // check that your flag is at your base
|
---|
311 | pos = this->worldMap->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
312 |
|
---|
313 | vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
|
---|
314 | vector<WorldMap::Object>::iterator itObjects;
|
---|
315 |
|
---|
316 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
317 | if (itObjects->type == OBJECT_RED_FLAG) {
|
---|
318 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
319 | ownFlagAtBase = true;
|
---|
320 | break;
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | if (ownFlagAtBase) {
|
---|
326 | p->hasBlueFlag = false;
|
---|
327 | flagType = OBJECT_BLUE_FLAG;
|
---|
328 | pos = this->worldMap->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
329 | flagTurnedIn = true;
|
---|
330 | this->redScore++;
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | break;
|
---|
335 | }
|
---|
336 | default:
|
---|
337 | {
|
---|
338 | break;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (flagTurnedIn) {
|
---|
343 | unsigned int blueScore = this->blueScore;
|
---|
344 | unsigned int redScore = this->redScore;
|
---|
345 |
|
---|
346 | pos.x = pos.x*25+12;
|
---|
347 | pos.y = pos.y*25+12;
|
---|
348 | this->addObjectToMap(flagType, pos.x, pos.y);
|
---|
349 |
|
---|
350 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
351 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
352 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
353 | msgProcessor->broadcastMessage(serverMsg, this->players);
|
---|
354 |
|
---|
355 | // check to see if the game should end
|
---|
356 | // move to its own method
|
---|
357 | if (this->blueScore == 3 || this->redScore == 3) {
|
---|
358 | gameFinished = true;
|
---|
359 |
|
---|
360 | unsigned int winningTeam;
|
---|
361 | if (this->blueScore == 3)
|
---|
362 | winningTeam = 0;
|
---|
363 | else if (this->redScore == 3)
|
---|
364 | winningTeam = 1;
|
---|
365 |
|
---|
366 | serverMsg.type = MSG_TYPE_FINISH_GAME;
|
---|
367 |
|
---|
368 | // I should create an instance of the GameSummary object here and just serialize it into this message
|
---|
369 | memcpy(serverMsg.buffer, &winningTeam, 4);
|
---|
370 | memcpy(serverMsg.buffer+4, &blueScore, 4);
|
---|
371 | memcpy(serverMsg.buffer+8, &redScore, 4);
|
---|
372 | strcpy(serverMsg.buffer+12, this->getName().c_str());
|
---|
373 | msgProcessor->broadcastMessage(serverMsg, this->players);
|
---|
374 | }
|
---|
375 |
|
---|
376 | // this means a PLAYER message will be sent
|
---|
377 | broadcastMove = true;
|
---|
378 | }
|
---|
379 |
|
---|
380 | // go through all objects and check if the player is close to one and if its their flag
|
---|
381 | vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
|
---|
382 | vector<WorldMap::Object>::iterator itObjects;
|
---|
383 | POSITION structPos;
|
---|
384 |
|
---|
385 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
386 | POSITION pos = itObjects->pos;
|
---|
387 |
|
---|
388 | if (posDistance(p->pos, pos.toFloat()) < 10) {
|
---|
389 | if (p->team == 0 && itObjects->type == OBJECT_BLUE_FLAG) {
|
---|
390 | structPos = this->worldMap->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
391 | flagReturned = true;
|
---|
392 | break;
|
---|
393 | } else if (p->team == 1 && itObjects->type == OBJECT_RED_FLAG) {
|
---|
394 | structPos = this->worldMap->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
395 | flagReturned = true;
|
---|
396 | break;
|
---|
397 | }
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | if (flagReturned) {
|
---|
402 | itObjects->pos.x = structPos.x*25+12;
|
---|
403 | itObjects->pos.y = structPos.y*25+12;
|
---|
404 |
|
---|
405 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
406 | itObjects->serialize(serverMsg.buffer);
|
---|
407 | msgProcessor->broadcastMessage(serverMsg, this->players);
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (broadcastMove) {
|
---|
411 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
412 | p->serialize(serverMsg.buffer);
|
---|
413 | msgProcessor->broadcastMessage(serverMsg, this->players);
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | cout << "processing player attack" << endl;
|
---|
418 |
|
---|
419 | // check if the player's attack animation is complete
|
---|
420 | if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis()) {
|
---|
421 | p->isAttacking = false;
|
---|
422 | cout << "Attack animation is complete" << endl;
|
---|
423 |
|
---|
424 | //send everyone an ATTACK message
|
---|
425 | cout << "about to broadcast attack" << endl;
|
---|
426 |
|
---|
427 | if (p->attackType == Player::ATTACK_MELEE) {
|
---|
428 | cout << "Melee attack" << endl;
|
---|
429 |
|
---|
430 | Player* target = players[p->getTargetPlayer()];
|
---|
431 | this->dealDamageToPlayer(target, p->damage);
|
---|
432 | } else if (p->attackType == Player::ATTACK_RANGED) {
|
---|
433 | cout << "Ranged attack" << endl;
|
---|
434 |
|
---|
435 | Projectile proj(p->pos.x, p->pos.y, p->getTargetPlayer(), p->damage);
|
---|
436 | this->assignProjectileId(&proj);
|
---|
437 | this->addProjectile(proj);
|
---|
438 |
|
---|
439 | int x = p->pos.x;
|
---|
440 | int y = p->pos.y;
|
---|
441 | unsigned int targetId = p->getTargetPlayer();
|
---|
442 |
|
---|
443 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
444 | memcpy(serverMsg.buffer, &proj.id, 4);
|
---|
445 | memcpy(serverMsg.buffer+4, &x, 4);
|
---|
446 | memcpy(serverMsg.buffer+8, &y, 4);
|
---|
447 | memcpy(serverMsg.buffer+12, &targetId, 4);
|
---|
448 | msgProcessor->broadcastMessage(serverMsg, players);
|
---|
449 | } else
|
---|
450 | cout << "Invalid attack type: " << p->attackType << endl;
|
---|
451 | }
|
---|
452 |
|
---|
453 | return gameFinished;
|
---|
454 | }
|
---|
455 |
|
---|
456 | void Game::assignProjectileId(Projectile* p) {
|
---|
457 | p->id = unusedProjectileId;
|
---|
458 | updateUnusedProjectileId();
|
---|
459 | }
|
---|
460 |
|
---|
461 | void Game::updateUnusedProjectileId() {
|
---|
462 | while (projectiles.find(unusedProjectileId) != projectiles.end())
|
---|
463 | unusedProjectileId++;
|
---|
464 | }
|
---|