Changeset ca44f82 in network-game for common


Ignore:
Timestamp:
Feb 24, 2013, 1:28:32 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
7b43385
Parents:
3a79253 (diff), 8f85180 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Updated files to correctly compile on Windows

Location:
common
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • common/Common.cpp

    r3a79253 rca44f82  
    11#include "Common.h"
     2
     3#include <iostream>
     4using namespace std;
     5
     6#if defined WINDOWS
     7   #include <Windows.h>
     8#elif defined LINUX
     9   #include <time.h>
     10#endif
    211
    312void set_nonblock(int sock)
     
    1322   #endif
    1423}
     24
     25unsigned long long getCurrentMillis()
     26{
     27   unsigned long long numMilliseconds;
     28
     29   #if defined WINDOWS
     30      numMilliseconds = GetTickCount();
     31   #elif defined LINUX
     32      timespec curTime;
     33      clock_gettime(CLOCK_REALTIME, &curTime);
     34
     35      numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
     36   #endif
     37
     38   return numMilliseconds;
     39}
  • common/Common.h

    r3a79253 rca44f82  
    1313
    1414void set_nonblock(int sock);
     15unsigned long long getCurrentMillis();
    1516
    1617typedef struct
     
    1819   int x;
    1920   int y;
    20 } PLAYER_POS;
     21} POSITION;
     22
     23typedef struct
     24{
     25   float x;
     26   float y;
     27} FLOAT_POSITION;
    2128
    2229#endif
  • common/Message.cpp

    r3a79253 rca44f82  
    1111#endif
    1212
     13#include <iostream>
     14
     15using namespace std;
     16
    1317int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest)
    1418{
    15    return sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
     19   int ret =  sendto(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, sizeof(struct sockaddr_in));
     20
     21   cout << "Sent message of type " << msg->type << endl;
     22
     23   return ret;
    1624}
    1725
     
    2129
    2230   // assume we don't care about the value of socklen
    23    return recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, &socklen);
     31   int ret =  recvfrom(sock, (char*)msg, sizeof(NETWORK_MSG), 0, (struct sockaddr *)dest, &socklen);
     32
     33   if (ret > -1)
     34      cout << "Received message of type " << msg->type << endl;
     35
     36   return ret;
    2437}
  • common/Message.h

    r3a79253 rca44f82  
    66#define MSG_TYPE_LOGOUT       3
    77#define MSG_TYPE_CHAT         4
    8 #define MSG_TYPE_PLAYER       5
     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
    910
    1011typedef struct
  • common/Player.cpp

    r3a79253 rca44f82  
    44#include <sstream>
    55#include <cstring>
     6#include <cmath>
    67
    78using namespace std;
     
    1213   this->name = "";
    1314   this->password = "";
    14    this->pos.x = 0;
    15    this->pos.y = 0;
     15   this->pos.x = this->target.x = 0;
     16   this->pos.y = this->target.y = 0;
     17   this->timeLastUpdated = 0;
    1618}
    1719
     
    2325   this->pos.x = p.pos.x;
    2426   this->pos.y = p.pos.y;
     27   this->target.x = p.target.x;
     28   this->target.y = p.target.y;
    2529   this->addr = p.addr;
    2630}
     
    3135   this->name = name;
    3236   this->password = password;
    33    this->pos.x = 200;
    34    this->pos.y = 200;
     37   this->pos.x = this->target.x = 200;
     38   this->pos.y = this->target.y = 200;
    3539}
    3640
     
    4044   this->name = name;
    4145   this->password = "";
    42    this->pos.x = 200;
    43    this->pos.y = 200;
     46   this->pos.x = this->target.x = 200;
     47   this->pos.y = this->target.y = 200;
    4448   this->addr = addr;
    4549}
     
    5155void Player::serialize(char* buffer)
    5256{
    53    ostringstream oss;
    54 
    55    oss.write((char*)&(this->id), sizeof(int));
    56    oss << this->name;
    57    oss.write("\0", 1);
    58    oss.write((char*)&(this->pos.x), sizeof(int));
    59    oss.write((char*)&(this->pos.y), sizeof(int));
    60 
    61    memcpy(buffer, oss.str().c_str(), this->name.length()+1+2*sizeof(int));
     57   memcpy(buffer, &this->id, 4);
     58   memcpy(buffer+4, &this->pos.x, 4);
     59   memcpy(buffer+8, &this->pos.y, 4);
     60   memcpy(buffer+12, &this->target.x, 4);
     61   memcpy(buffer+16, &this->target.y, 4);
     62   strcpy(buffer+20, this->name.c_str());
    6263}
    6364
    6465void Player::deserialize(char* buffer)
    6566{
    66    istringstream iss;
    67    iss.str(buffer);
     67   memcpy(&this->id, buffer, 4);
     68   memcpy(&this->pos.x, buffer+4, 4);
     69   memcpy(&this->pos.y, buffer+8, 4);
     70   memcpy(&this->target.x, buffer+12, 4);
     71   memcpy(&this->target.y, buffer+16, 4);
     72   this->name.assign(buffer+20);
    6873
    69    iss.read((char*)&(this->id), sizeof(int));
    70    iss >> this->name;
    71    iss.read((char*)&(this->pos.x), sizeof(int));
    72    iss.read((char*)&(this->pos.y), sizeof(int));
     74   cout << "id: " << this->id << endl;
     75   cout << "pos x: " << this->pos.x << endl;
     76   cout << "pos y: " << this->pos.y << endl;
     77   cout << "target x: " << this->target.x << endl;
     78   cout << "target y: " << this->target.y << endl;
     79   cout << "name: " << this->name << endl;
    7380}
    7481
     
    8289   this->addr = addr;
    8390}
     91
     92void Player::move(void) {
     93   int speed = 100; // pixels per second
     94   unsigned long long curTime = getCurrentMillis();
     95
     96   // if we're at our target, don't move
     97   if (pos.x == target.x && pos.y == target.y)
     98      cout << "We're already at our target" << endl;
     99   else {
     100      float pixels = speed * (curTime-timeLastUpdated) / 1000.0;
     101      cout << "We need to move " << pixels << " pixels" << endl;
     102
     103      double angle = atan2(target.y-pos.y, target.x-pos.x);
     104
     105      float dist = sqrt(pow(target.x-pos.x, 2) + pow(target.y-pos.y, 2));
     106      if (dist <= pixels) {
     107         pos.x = target.x;
     108         pos.y = target.y;
     109      }else {
     110         pos.x += cos(angle)*pixels;
     111         pos.y += sin(angle)*pixels;
     112      }
     113   }
     114
     115   timeLastUpdated = curTime;
     116}
  • common/Player.h

    r3a79253 rca44f82  
    3232   void setAddr(sockaddr_in addr);
    3333
     34   void move();
     35
    3436   int id;
    3537   string name;
    3638   string password;
    3739   sockaddr_in addr;
    38    PLAYER_POS pos;
     40   FLOAT_POSITION pos;
     41   POSITION target;
     42   unsigned long long timeLastUpdated;
    3943};
    4044
Note: See TracChangeset for help on using the changeset viewer.