source: network-game/common/Game.cpp@ 0678d60

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

All server warnings have been fixed and the WorldMap class has a new method to create flags (and, in the future, other objects) based on structures present on the map

  • Property mode set to 100644
File size: 13.0 KB
Line 
1#include "Game.h"
2
3#include <iostream>
4#include <cstring>
5
6#include "Common.h"
7
8using namespace std;
9
10Game::Game() {
11 this->id = 0;
12 this->name = "";
13 this->blueScore = 0;
14 this->redScore = 0;
15 this->worldMap = NULL;
16 this->msgProcessor = NULL;
17}
18
19Game::Game(string name, string filepath, MessageProcessor* msgProcessor) {
20 this->id = 0;
21 this->name = name;
22 this->blueScore = 0;
23 this->redScore = 0;
24 this->worldMap = WorldMap::loadMapFromFile(filepath);
25 this->msgProcessor = msgProcessor;
26}
27
28Game::~Game() {
29 delete this->worldMap;
30}
31
32string Game::getName() {
33 return this->name;
34}
35
36int Game::getNumPlayers() {
37 return this->players.size();
38}
39
40map<unsigned int, Player*>& Game::getPlayers() {
41 return this->players;
42}
43
44bool Game::addPlayer(Player* p) {
45 if (players.find(p->getId()) == players.end()) {
46 players[p->getId()] = p;
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
60map<unsigned int, Projectile>& Game::getProjectiles() {
61 return this->projectiles;
62}
63
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
80unsigned int Game::getRedScore() {
81 return this->redScore;
82}
83
84unsigned int Game::getBlueScore() {
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;
94}
95
96void Game::setRedScore(unsigned int score) {
97 this->redScore = score;
98}
99
100void Game::setBlueScore(unsigned int score) {
101 this->blueScore = score;
102}
103
104void Game::addObjectToMap(WorldMap::ObjectType objectType, int x, int y) {
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
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 &&
122 this->worldMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
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
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 {
142 case WorldMap::TERRAIN_NONE:
143 case WorldMap::TERRAIN_OCEAN:
144 case WorldMap::TERRAIN_ROCK:
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
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;
164 int itemId = -1;
165
166 for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
167 if (posDistance(p->pos, it->pos.toFloat()) < 10) {
168 switch (it->type) {
169 case WorldMap::OBJECT_BLUE_FLAG:
170 if (p->team == 1) {
171 p->hasBlueFlag = true;
172 itemId = it->id;
173 }
174 break;
175 case WorldMap::OBJECT_RED_FLAG:
176 if (p->team == 0) {
177 p->hasRedFlag = true;
178 itemId = it->id;
179 }
180 break;
181 case WorldMap::OBJECT_NONE:
182 break;
183 }
184
185 if (itemId > -1) {
186 vctObjects->erase(it);
187 return itemId;
188 }
189 }
190 }
191
192 return itemId;
193}
194
195void Game::dealDamageToPlayer(Player* p, int damage) {
196 p->takeDamage(damage);
197
198 if (p->isDead)
199 {
200 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
201 if (p->hasBlueFlag)
202 flagType = WorldMap::OBJECT_BLUE_FLAG;
203 else if (p->hasRedFlag)
204 flagType = WorldMap::OBJECT_RED_FLAG;
205
206 if (flagType != WorldMap::OBJECT_NONE)
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
254 WorldMap::ObjectType flagType;
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 {
262 case WorldMap::STRUCTURE_BLUE_FLAG:
263 {
264 if (p->team == 0 && p->hasRedFlag)
265 {
266 // check that your flag is at your base
267 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
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 {
274 if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
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;
287 flagType = WorldMap::OBJECT_RED_FLAG;
288 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
289 flagTurnedIn = true;
290 this->blueScore++;
291 }
292 }
293
294 break;
295 }
296 case WorldMap::STRUCTURE_RED_FLAG:
297 {
298 if (p->team == 1 && p->hasBlueFlag)
299 {
300 // check that your flag is at your base
301 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
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 {
308 if (itObjects->type == WorldMap::OBJECT_RED_FLAG)
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;
321 flagType = WorldMap::OBJECT_BLUE_FLAG;
322 pos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
323 flagTurnedIn = true;
324 this->redScore++;
325 }
326 }
327
328 break;
329 }
330 default:
331 {
332 break;
333 }
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 &&
387 itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
388 {
389 structPos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
390 flagReturned = true;
391 break;
392 }
393 else if (p->team == 1 &&
394 itObjects->type == WorldMap::OBJECT_RED_FLAG)
395 {
396 structPos = this->worldMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
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
436 Player* target = players[p->getTargetPlayer()];
437 this->dealDamageToPlayer(target, p->damage);
438 }
439 else if (p->attackType == Player::ATTACK_RANGED)
440 {
441 cout << "Ranged attack" << endl;
442
443 Projectile proj(p->pos.x, p->pos.y, p->getTargetPlayer(), p->damage);
444 this->assignProjectileId(&proj);
445 this->addProjectile(proj);
446
447 int x = p->pos.x;
448 int y = p->pos.y;
449 unsigned int targetId = p->getTargetPlayer();
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);
455 memcpy(serverMsg.buffer+12, &targetId, 4);
456 msgProcessor->broadcastMessage(serverMsg, players);
457 }
458 else
459 cout << "Invalid attack type: " << p->attackType << endl;
460 }
461
462 return gameFinished;
463}
464
465void Game::assignProjectileId(Projectile* p) {
466 p->id = unusedProjectileId;
467 updateUnusedProjectileId();
468}
469
470void Game::updateUnusedProjectileId() {
471 while (projectiles.find(unusedProjectileId) != projectiles.end())
472 unusedProjectileId++;
473}
Note: See TracBrowser for help on using the repository browser.