Changeset 8dad966 in network-game


Ignore:
Timestamp:
Jun 3, 2013, 11:05:20 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
d03ec0f
Parents:
8a4ed74
Message:

Added server support for player attack animations and cooldowns and firing projectiles for ranged attacks

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • common/Message.h

    r8a4ed74 r8dad966  
    22#define _MESSAGE_H
    33
    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
    1719
    1820typedef struct
  • common/Player.cpp

    r8a4ed74 r8dad966  
    1616   this->pos.y = this->target.y = 0;
    1717   this->timeLastUpdated = 0;
     18   this->timeAttackStarted = 0;
     19   this->isAttacking = false;
    1820
    1921   this->playerClass = CLASS_NONE;
     
    2224   this->attackType = ATTACK_NONE;
    2325   this->damage = 0;
     26   this->attackCooldown = 0;
    2427   this->team = 0;   // blue team by default
    2528   this->hasBlueFlag = false;
     
    3841   this->target.y = p.target.y;
    3942   this->timeLastUpdated = p.timeLastUpdated;
     43   this->timeAttackStarted = p.timeAttackStarted;
     44   this->isAttacking = p.isAttacking;
    4045
    4146   this->playerClass = p.playerClass;
     
    4449   this->attackType = p.attackType;
    4550   this->damage = p.damage;
     51   this->attackCooldown = p.attackCooldown;
    4652   this->team = p.team;
    4753   this->hasBlueFlag = p.hasBlueFlag;
     
    5763   this->pos.x = this->target.x = 200;
    5864   this->pos.y = this->target.y = 200;
     65   this->timeLastUpdated = 0;
     66   this->timeAttackStarted = 0;
     67   this->isAttacking = false;
    5968
    6069   this->playerClass = CLASS_NONE;
     
    6372   this->attackType = ATTACK_NONE;
    6473   this->damage = 0;
     74   this->attackCooldown = 0;
    6575   this->team = 0;   // blue team by default
    6676   this->hasBlueFlag = false;
     
    8797      case CLASS_WARRIOR:
    8898         this->playerClass = CLASS_WARRIOR;
    89          this->maxHealth = this->health = 100;
     99         this->maxHealth = this->health = 120;
    90100         this->attackType = ATTACK_MELEE;
    91101         this->damage = 10;
     102         this->attackCooldown = 800;
    92103         break;
    93104      case CLASS_RANGER:
     
    96107         this->attackType = ATTACK_RANGED;
    97108         this->damage = 6;
     109         this->attackCooldown = 1000;
    98110         break;
    99111      case CLASS_NONE:
  • common/Player.h

    r8a4ed74 r8dad966  
    5858   POSITION target;
    5959   unsigned long long timeLastUpdated;
     60   unsigned long long timeAttackStarted;
     61   bool isAttacking;
     62   int targetPlayer;
    6063
    6164   int playerClass;
     
    6467   int attackType;
    6568   int damage;
     69   unsigned long long attackCooldown;
    6670   int team; // 0 is blue, 1 is red
    6771   bool hasBlueFlag;
  • server/makefile

    r8a4ed74 r8dad966  
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
    5 DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o
     5DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o Projectile.o
    66
    77server : server.cpp $(DEPENDENCIES)
     
    2020        $(CC) -c -o $@ $?
    2121
     22Projectile.o : $(COMMON_PATH)/Projectile.cpp
     23        $(CC) -c -o $@ $?
     24
    2225%.o : %.cpp
    2326        $(CC) -c -o $@ $?
  • server/server.cpp

    r8a4ed74 r8dad966  
    3131#include "../common/WorldMap.h"
    3232#include "../common/Player.h"
     33#include "../common/Projectile.h"
    3334
    3435#include "DataAccess.h"
     
    3839// from used to be const. Removed that so I could take a reference
    3940// 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);
     41bool 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
     43void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     44void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles);
    4345
    4446// this should probably go somewhere in the common folder
     
    8385   NETWORK_MSG clientMsg, serverMsg;
    8486   map<unsigned int, Player> mapPlayers;
    85    unsigned int unusedId = 1;
     87   map<unsigned int, Projectile> mapProjectiles;
     88   unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
    8689   int scoreBlue, scoreRed;
    8790
     
    142145         timeLastUpdated = curTime;
    143146
     147         // move all players
    144148         // maybe put this in a separate method
    145149         map<unsigned int, Player>::iterator it;
     
    317321               }
    318322            }
     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            }
    319406         }
    320407      }
     
    323410
    324411      if (n >= 0) {
    325          broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock, scoreBlue, scoreRed);
     412         broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed);
    326413
    327414         if (broadcastResponse)
     
    350437}
    351438
    352 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)
     439bool 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)
    353440{
    354441   DataAccess da;
     
    407494            serverMsg.type = MSG_TYPE_PLAYER;
    408495
    409             updateUnusedId(unusedId, mapPlayers);
    410             p->id = unusedId;
     496            updateUnusedPlayerId(unusedPlayerId, mapPlayers);
     497            p->id = unusedPlayerId;
    411498            cout << "new player id: " << p->id << endl;
    412499            p->setAddr(from);
     
    472559
    473560            serverMsg.type = MSG_TYPE_LOGIN;
    474             mapPlayers[unusedId] = *p;
     561            mapPlayers[unusedPlayerId] = *p;
    475562         }
    476563
     
    499586         else
    500587         {
    501             if (p->id < unusedId)
    502                unusedId = p->id;
     588            if (p->id < unusedPlayerId)
     589               unusedPlayerId = p->id;
    503590            mapPlayers.erase(p->id);
    504591            strcpy(serverMsg.buffer, "You have successfully logged out.");
     
    672759         mapPlayers[id].serialize(serverMsg.buffer);
    673760
    674          map<unsigned int, Player>::iterator it2;
    675761         broadcastResponse = true;
    676762
     
    686772         memcpy(&targetId, clientMsg.buffer+4, 4);
    687773
     774         Player* source = &mapPlayers[id];
     775         source->timeAttackStarted = getCurrentMillis();
     776         source->targetPlayer = targetId;
     777
    688778         serverMsg.type = MSG_TYPE_START_ATTACK;
     779         memcpy(serverMsg.buffer, &id, 4);
     780         memcpy(serverMsg.buffer+4, &targetId, 4);
    689781         broadcastResponse = true;
    690782
     
    694786      {
    695787         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;
    704789
    705790         break;
     
    718803}
    719804
    720 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
     805void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
    721806{
    722807   while (mapPlayers.find(id) != mapPlayers.end())
    723808      id++;
    724809}
     810
     811void 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.