Changeset 8dad966 in network-game
- Timestamp:
- Jun 3, 2013, 11:05:20 PM (12 years ago)
- Branches:
- master
- Children:
- d03ec0f
- Parents:
- 8a4ed74
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
common/Message.h
r8a4ed74 r8dad966 2 2 #define _MESSAGE_H 3 3 4 #define MSG_TYPE_REGISTER 1 5 #define MSG_TYPE_LOGIN 2 6 #define MSG_TYPE_LOGOUT 3 7 #define MSG_TYPE_CHAT 4 8 #define MSG_TYPE_PLAYER 5 // server sends this to update player positions 9 #define MSG_TYPE_PLAYER_MOVE 6 // client sends this when a player wants to move 10 #define MSG_TYPE_OBJECT 7 11 #define MSG_TYPE_REMOVE_OBJECT 8 12 #define MSG_TYPE_PICKUP_FLAG 9 13 #define MSG_TYPE_DROP_FLAG 10 14 #define MSG_TYPE_SCORE 11 15 #define MSG_TYPE_START_ATTACK 12 16 #define MSG_TYPE_ATTACK 13 4 #define MSG_TYPE_REGISTER 1 5 #define MSG_TYPE_LOGIN 2 6 #define MSG_TYPE_LOGOUT 3 7 #define MSG_TYPE_CHAT 4 8 #define MSG_TYPE_PLAYER 5 // server sends this to update player positions 9 #define MSG_TYPE_PLAYER_MOVE 6 // client sends this when a player wants to move 10 #define MSG_TYPE_OBJECT 7 11 #define MSG_TYPE_REMOVE_OBJECT 8 12 #define MSG_TYPE_PICKUP_FLAG 9 13 #define MSG_TYPE_DROP_FLAG 10 14 #define MSG_TYPE_SCORE 11 15 #define MSG_TYPE_START_ATTACK 12 16 #define MSG_TYPE_ATTACK 13 17 #define MSG_TYPE_PROJECTILE 14 18 #define MSG_TYPE_REMOVE_PROJECTILE 14 17 19 18 20 typedef struct -
common/Player.cpp
r8a4ed74 r8dad966 16 16 this->pos.y = this->target.y = 0; 17 17 this->timeLastUpdated = 0; 18 this->timeAttackStarted = 0; 19 this->isAttacking = false; 18 20 19 21 this->playerClass = CLASS_NONE; … … 22 24 this->attackType = ATTACK_NONE; 23 25 this->damage = 0; 26 this->attackCooldown = 0; 24 27 this->team = 0; // blue team by default 25 28 this->hasBlueFlag = false; … … 38 41 this->target.y = p.target.y; 39 42 this->timeLastUpdated = p.timeLastUpdated; 43 this->timeAttackStarted = p.timeAttackStarted; 44 this->isAttacking = p.isAttacking; 40 45 41 46 this->playerClass = p.playerClass; … … 44 49 this->attackType = p.attackType; 45 50 this->damage = p.damage; 51 this->attackCooldown = p.attackCooldown; 46 52 this->team = p.team; 47 53 this->hasBlueFlag = p.hasBlueFlag; … … 57 63 this->pos.x = this->target.x = 200; 58 64 this->pos.y = this->target.y = 200; 65 this->timeLastUpdated = 0; 66 this->timeAttackStarted = 0; 67 this->isAttacking = false; 59 68 60 69 this->playerClass = CLASS_NONE; … … 63 72 this->attackType = ATTACK_NONE; 64 73 this->damage = 0; 74 this->attackCooldown = 0; 65 75 this->team = 0; // blue team by default 66 76 this->hasBlueFlag = false; … … 87 97 case CLASS_WARRIOR: 88 98 this->playerClass = CLASS_WARRIOR; 89 this->maxHealth = this->health = 1 00;99 this->maxHealth = this->health = 120; 90 100 this->attackType = ATTACK_MELEE; 91 101 this->damage = 10; 102 this->attackCooldown = 800; 92 103 break; 93 104 case CLASS_RANGER: … … 96 107 this->attackType = ATTACK_RANGED; 97 108 this->damage = 6; 109 this->attackCooldown = 1000; 98 110 break; 99 111 case CLASS_NONE: -
common/Player.h
r8a4ed74 r8dad966 58 58 POSITION target; 59 59 unsigned long long timeLastUpdated; 60 unsigned long long timeAttackStarted; 61 bool isAttacking; 62 int targetPlayer; 60 63 61 64 int playerClass; … … 64 67 int attackType; 65 68 int damage; 69 unsigned long long attackCooldown; 66 70 int team; // 0 is blue, 1 is red 67 71 bool hasBlueFlag; -
server/makefile
r8a4ed74 r8dad966 3 3 FLAGS = $(LIB_FLAGS) 4 4 COMMON_PATH = ../common 5 DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o 5 DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o Projectile.o 6 6 7 7 server : server.cpp $(DEPENDENCIES) … … 20 20 $(CC) -c -o $@ $? 21 21 22 Projectile.o : $(COMMON_PATH)/Projectile.cpp 23 $(CC) -c -o $@ $? 24 22 25 %.o : %.cpp 23 26 $(CC) -c -o $@ $? -
server/server.cpp
r8a4ed74 r8dad966 31 31 #include "../common/WorldMap.h" 32 32 #include "../common/Player.h" 33 #include "../common/Projectile.h" 33 34 34 35 #include "DataAccess.h" … … 38 39 // from used to be const. Removed that so I could take a reference 39 40 // and use it to send messages 40 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed); 41 42 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers); 41 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed); 42 43 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers); 44 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles); 43 45 44 46 // this should probably go somewhere in the common folder … … 83 85 NETWORK_MSG clientMsg, serverMsg; 84 86 map<unsigned int, Player> mapPlayers; 85 unsigned int unusedId = 1; 87 map<unsigned int, Projectile> mapProjectiles; 88 unsigned int unusedPlayerId = 1, unusedProjectileId = 1; 86 89 int scoreBlue, scoreRed; 87 90 … … 142 145 timeLastUpdated = curTime; 143 146 147 // move all players 144 148 // maybe put this in a separate method 145 149 map<unsigned int, Player>::iterator it; … … 317 321 } 318 322 } 323 324 // check if the player's attack animation is complete 325 if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) { 326 it->second.isAttacking = false; 327 328 //send everyone an ATTACK message 329 cout << "about to broadcast attack" << endl; 330 331 serverMsg.type = MSG_TYPE_ATTACK; 332 memcpy(serverMsg.buffer, &it->second.id, 4); 333 memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4); 334 335 map<unsigned int, Player>::iterator it2; 336 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 337 { 338 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 339 error("sendMessage"); 340 } 341 342 if (it->second.attackType == Player::ATTACK_MELEE) { 343 Player* target = &mapPlayers[it->second.targetPlayer]; 344 345 target->health -= it->second.damage; 346 if (target->health < 0) 347 target->health = 0; 348 349 serverMsg.type = MSG_TYPE_PLAYER; 350 target->serialize(serverMsg.buffer); 351 }else if (it->second.attackType == Player::ATTACK_RANGED) { 352 Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage); 353 proj.id = unusedProjectileId; 354 updateUnusedProjectileId(unusedProjectileId, mapProjectiles); 355 mapProjectiles[proj.id] = proj; 356 357 serverMsg.type = MSG_TYPE_PROJECTILE; 358 memcpy(serverMsg.buffer, &it->second.pos.x, 4); 359 memcpy(serverMsg.buffer+4, &it->second.pos.y, 4); 360 memcpy(serverMsg.buffer+8, &it->second.targetPlayer, 4); 361 }else { 362 cout << "Invalid attack type: " << it->second.attackType << endl; 363 } 364 365 // broadcast either a PLAYER or PROJECTILE message 366 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 367 { 368 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 369 error("sendMessage"); 370 } 371 } 372 } 373 374 // move all projectiles 375 map<unsigned int, Projectile>::iterator itProj; 376 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) { 377 if (itProj->second.move(mapPlayers)) { 378 // send a REMOVE_PROJECTILE message 379 serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE; 380 memcpy(serverMsg.buffer, &itProj->second.id, 4); 381 mapProjectiles.erase(itProj->second.id); 382 383 map<unsigned int, Player>::iterator it2; 384 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 385 { 386 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 387 error("sendMessage"); 388 } 389 390 // send a PLAYER message after dealing damage 391 Player* target = &mapPlayers[it->second.targetPlayer]; 392 393 target->health -= itProj->second.damage; 394 if (target->health < 0) 395 target->health = 0; 396 397 serverMsg.type = MSG_TYPE_PLAYER; 398 target->serialize(serverMsg.buffer); 399 400 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 401 { 402 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 403 error("sendMessage"); 404 } 405 } 319 406 } 320 407 } … … 323 410 324 411 if (n >= 0) { 325 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unused Id, serverMsg, sock, scoreBlue, scoreRed);412 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed); 326 413 327 414 if (broadcastResponse) … … 350 437 } 351 438 352 bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unused Id, NETWORK_MSG& serverMsg, int sock, int &scoreBlue, int &scoreRed)439 bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG& serverMsg, int sock, int &scoreBlue, int &scoreRed) 353 440 { 354 441 DataAccess da; … … 407 494 serverMsg.type = MSG_TYPE_PLAYER; 408 495 409 updateUnused Id(unusedId, mapPlayers);410 p->id = unused Id;496 updateUnusedPlayerId(unusedPlayerId, mapPlayers); 497 p->id = unusedPlayerId; 411 498 cout << "new player id: " << p->id << endl; 412 499 p->setAddr(from); … … 472 559 473 560 serverMsg.type = MSG_TYPE_LOGIN; 474 mapPlayers[unused Id] = *p;561 mapPlayers[unusedPlayerId] = *p; 475 562 } 476 563 … … 499 586 else 500 587 { 501 if (p->id < unused Id)502 unused Id = p->id;588 if (p->id < unusedPlayerId) 589 unusedPlayerId = p->id; 503 590 mapPlayers.erase(p->id); 504 591 strcpy(serverMsg.buffer, "You have successfully logged out."); … … 672 759 mapPlayers[id].serialize(serverMsg.buffer); 673 760 674 map<unsigned int, Player>::iterator it2;675 761 broadcastResponse = true; 676 762 … … 686 772 memcpy(&targetId, clientMsg.buffer+4, 4); 687 773 774 Player* source = &mapPlayers[id]; 775 source->timeAttackStarted = getCurrentMillis(); 776 source->targetPlayer = targetId; 777 688 778 serverMsg.type = MSG_TYPE_START_ATTACK; 779 memcpy(serverMsg.buffer, &id, 4); 780 memcpy(serverMsg.buffer+4, &targetId, 4); 689 781 broadcastResponse = true; 690 782 … … 694 786 { 695 787 cout << "Received am ATTACK message" << endl; 696 697 int id, targetId; 698 699 memcpy(&id, clientMsg.buffer, 4); 700 memcpy(&targetId, clientMsg.buffer+4, 4); 701 702 serverMsg.type = MSG_TYPE_ATTACK; 703 broadcastResponse = true; 788 cout << "ERROR: Clients should not send ATTACK messages" << endl; 704 789 705 790 break; … … 718 803 } 719 804 720 void updateUnused Id(unsigned int& id, map<unsigned int, Player>& mapPlayers)805 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers) 721 806 { 722 807 while (mapPlayers.find(id) != mapPlayers.end()) 723 808 id++; 724 809 } 810 811 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles) 812 { 813 while (mapProjectiles.find(id) != mapProjectiles.end()) 814 id++; 815 }
Note:
See TracChangeset
for help on using the changeset viewer.