source: network-game/common/Game.cpp@ f66d04f

Last change on this file since f66d04f was 7f884ea, checked in by dportnoy <dmp1488@…>, 11 years ago

Map-related structs are now outside of the WorldMap class

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