Changeset d211210 in network-game


Ignore:
Timestamp:
May 9, 2013, 1:32:42 AM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
db58227
Parents:
227baaa
Message:

Add server-side checking of player movement

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • common/Player.cpp

    r227baaa rd211210  
    9595      POSITION newPos;
    9696
     97      //cout << "pos.x: " << pos.x << endl;
     98      //cout << "pos.y: " << pos.y << endl;
     99      //cout << "target.x: " << target.x << endl;
     100      //cout << "target.y: " << target.y << endl;
     101
    97102      if (dist <= pixels) {
    98          pos.x = target.x;
    99          pos.y = target.y;
     103         newPos.x = target.x;
     104         newPos.y = target.y;
    100105      }else {
    101106         newPos.x = int(pos.x + cos(angle)*pixels);
    102107         newPos.y = int(pos.y + sin(angle)*pixels);
     108      }
    103109
    104          switch(map->getElement(newPos.x/25, newPos.y/25)) {
    105          case WorldMap::TERRAIN_NONE:
    106          case WorldMap::TERRAIN_ROCK:
    107             target.x = pos.x;
    108             target.y = pos.y;
    109             moveCanceled = true;
    110             break;
    111          }
     110      //cout << "newPos.x: " << newPos.x << endl;
     111      //cout << "newPos.y: " << newPos.y << endl;
     112      //cout << "newPos.x/25: " << newPos.x/25 << endl;
     113      //cout << "newPos.y/25: " << newPos.y/25 << endl;
     114
     115      switch(map->getElement(newPos.x/25, newPos.y/25)) {
     116      case WorldMap::TERRAIN_NONE:
     117      case WorldMap::TERRAIN_ROCK:
     118         cout << "Encountered invalid terrain" << endl;
     119         target.x = pos.x;
     120         target.y = pos.y;
     121         moveCanceled = true;
     122         cout << "move canceled" << endl;
     123         break;
     124      default: // if there are no obstacles
     125         pos.x = newPos.x;
     126         pos.y = newPos.y;
     127         break;
    112128      }
    113129   }
    114130
    115131   timeLastUpdated = curTime;
     132
     133   if (moveCanceled)
     134      cout << "moveCancled == true" << endl;
     135
    116136   return !moveCanceled;
    117137}
  • server/server.cpp

    r227baaa rd211210  
    1111#include <map>
    1212
     13#include <sys/time.h>
     14
    1315#include <sys/socket.h>
    1416#include <netdb.h>
     
    3436using namespace std;
    3537
    36 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
     38// from used to be const. Removed that so I could take a reference
     39// and use it to send messages
     40bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock);
    3741
    3842void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     
    124128
    125129   bool broadcastResponse;
     130   timespec ts;
     131   long timeLastUpdated = 0, curTime = 0;
    126132   while (true) {
    127133
    128134      usleep(5000);
     135
     136      clock_gettime(CLOCK_REALTIME, &ts);
     137      curTime = ts.tv_sec + ts.tv_nsec*1000000000;
     138
     139      if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50000) {
     140         timeLastUpdated = curTime;
     141
     142         // maybe put this in a separate method
     143         map<unsigned int, Player>::iterator it, it2;
     144         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
     145            if (!it->second.move(gameMap)) {
     146               cout << "Cenceling move" << endl;
     147               //serverMsg.type = MSG_TYPE_PLAYER;
     148               //it->second.serialize(serverMsg.buffer);
     149
     150               cout << "about to send move cencellation" << endl;
     151               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
     152               {
     153                  //if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
     154                    // error("sendMessage");
     155               }
     156            }
     157         }
     158      }
    129159
    130160      n = receiveMessage(&clientMsg, sock, &from);
     
    133163         cout << "Got a message" << endl;
    134164
    135          broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
     165         broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
    136166
    137167         // probably replace this with a function that prints based on the
     
    146176            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    147177            {
     178               cout << "Sent message back to " << it->second.name << endl;
    148179               if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
    149180                  error("sendMessage");
     
    159190      }
    160191
     192      // we don't want to update and broadcast player positions here
     193      // when a player sends a position update, we want to check if
     194      // it's reasonable and send it out to all other players
     195
    161196      // update player positions
     197      /*
    162198      map<unsigned int, Player>::iterator it;
    163199      for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
     
    167203
    168204      broadcastPlayerPositions(mapPlayers, sock);
     205      */
    169206   }
    170207
     
    172209}
    173210
    174 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
     211bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg, int sock)
    175212{
    176213   DataAccess da;
     
    209246         cout << "Got login message" << endl;
    210247
     248         serverMsg.type = MSG_TYPE_LOGIN;
     249         
    211250         string username(clientMsg.buffer);
    212251         string password(strchr(clientMsg.buffer, '\0')+1);
     
    224263         else
    225264         {
     265            serverMsg.type = MSG_TYPE_PLAYER;
     266
    226267            p->setAddr(from);
    227268            updateUnusedId(unusedId, mapPlayers);
    228269            p->id = unusedId;
     270            cout << "new player id: " << p->id << endl;
     271
     272            // tell the new player about all the existing players
     273            cout << "Sending other players to new player" << endl;
     274
     275            map<unsigned int, Player>::iterator it;
     276            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
     277            {
     278               it->second.serialize(serverMsg.buffer);
     279
     280               cout << "sending info about " << it->second.name  << endl;
     281               cout << "sending ind " << it->second.id  << endl;
     282               if ( sendMessage(&serverMsg, sock, &from) < 0 )
     283                  error("sendMessage");
     284            }
     285
     286            p->serialize(serverMsg.buffer);
     287            cout << "Should be broadcasting the message" << endl;
     288
     289            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
     290            {
     291               cout << "Sent message back to " << it->second.name << endl;
     292               if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
     293                  error("sendMessage");
     294            }
     295
     296            serverMsg.type = MSG_TYPE_LOGIN;
    229297            mapPlayers[unusedId] = *p;
    230 
    231             // sendd back the new player info to the user
    232             p->serialize(serverMsg.buffer);
    233          }
    234 
    235          serverMsg.type = MSG_TYPE_LOGIN;
    236    
     298         }
     299
    237300         delete(p);
    238301
     
    266329         }
    267330
    268          // should really be serverMsg.type = MSG_TYPE_LOGOUT;
    269          serverMsg.type = MSG_TYPE_LOGIN;
     331         serverMsg.type = MSG_TYPE_LOGOUT;
    270332
    271333         break;
     
    321383               cout << "valid terrain" << endl;
    322384
    323                cout << "orig x: " << mapPlayers[id].pos.x << endl;
    324                cout << "orig y: " << mapPlayers[id].pos.y << endl;
     385               //cout << "orig x: " << mapPlayers[id].pos.x << endl;
     386               //cout << "orig y: " << mapPlayers[id].pos.y << endl;
    325387               // first we get the correct vector
    326388               mapPlayers[id].target.x = x;
     
    328390               int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;
    329391               int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;
    330                cout << "xDiff: " << xDiff << endl;
    331                cout << "yDiff: " << yDiff << endl;
     392               //cout << "xDiff: " << xDiff << endl;
     393               //cout << "yDiff: " << yDiff << endl;
    332394
    333395               // then we get the correct angle
     
    344406               
    345407               memcpy(serverMsg.buffer, &id, 4);
    346                memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4);
    347                memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4);
     408               memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
     409               memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
    348410               //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
    349411
Note: See TracChangeset for help on using the changeset viewer.