Changeset 8dad966 in network-game for server


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

Location:
server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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.