Changes in / [093c141:384b7e0] in network-game


Ignore:
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • common/Common.h

    r093c141 r384b7e0  
    2020} POSITION;
    2121
    22 typedef struct
    23 {
    24    float x;
    25    float y;
    26 } FLOAT_POSITION;
    27 
    2822#endif
  • common/Player.cpp

    r093c141 r384b7e0  
    44#include <sstream>
    55#include <cstring>
    6 #include <cmath>
    76
    87using namespace std;
     
    1312   this->name = "";
    1413   this->password = "";
    15    this->pos.x = this->target.x = 0;
    16    this->pos.y = this->target.y = 0;
     14   this->pos.x = 0;
     15   this->pos.y = 0;
    1716}
    1817
     
    2423   this->pos.x = p.pos.x;
    2524   this->pos.y = p.pos.y;
    26    this->target.x = p.target.x;
    27    this->target.y = p.target.y;
    2825   this->addr = p.addr;
    2926}
     
    3431   this->name = name;
    3532   this->password = password;
    36    this->pos.x = this->target.x = 200;
    37    this->pos.y = this->target.y = 200;
     33   this->pos.x = 200;
     34   this->pos.y = 200;
    3835}
    3936
     
    4340   this->name = name;
    4441   this->password = "";
    45    this->pos.x = this->target.x = 200;
    46    this->pos.y = this->target.y = 200;
     42   this->pos.x = 200;
     43   this->pos.y = 200;
    4744   this->addr = addr;
    4845}
     
    8279   this->addr = addr;
    8380}
    84 
    85 void Player::move(void) {
    86    // timeLastMoved
    87    // pos
    88    // target
    89    int speed = 100; // pixels per second
    90 
    91    timespec curTS, diffTS;
    92    clock_gettime(CLOCK_REALTIME, &curTS);
    93 
    94    // get time elapsed
    95    diffTS.tv_sec = curTS.tv_sec - timeLastUpdated.tv_sec;
    96    diffTS.tv_nsec = curTS.tv_nsec - timeLastUpdated.tv_nsec;
    97    if (diffTS.tv_nsec < 0) {
    98       diffTS.tv_sec -= 1;
    99       diffTS.tv_nsec += 1000000000;
    100    }
    101 
    102    cout << "elapsed secs: " << diffTS.tv_sec << endl;
    103    cout << "elapsed nsecs: " << diffTS.tv_nsec << endl;
    104 
    105    // here we move 100 pixels per second
    106    float pixels = 100 * (diffTS.tv_sec+diffTS.tv_nsec/1000000000.0);
    107    cout << "We need to move " << pixels << "pixels" << endl;
    108 
    109    double angle = atan2(target.y-pos.y, target.x-pos.x);
    110 
    111    // we just need to check that we don't overjump the target
    112    pos.x += cos(angle)*pixels;
    113    pos.y += sin(angle)*pixels;
    114 
    115    timeLastUpdated.tv_sec = curTS.tv_sec;
    116    timeLastUpdated.tv_nsec = curTS.tv_nsec;
    117 }
  • common/Player.h

    r093c141 r384b7e0  
    1212
    1313#include <string>
    14 #include <sys/time.h>
    1514
    1615#include "Common.h"
     
    3332   void setAddr(sockaddr_in addr);
    3433
    35    void move();
    36 
    3734   int id;
    3835   string name;
    3936   string password;
    4037   sockaddr_in addr;
    41    FLOAT_POSITION pos;
    42    POSITION target;
    43    timespec timeLastUpdated;
     38   POSITION pos;
    4439};
    4540
  • server/DataAccess.cpp

    r093c141 r384b7e0  
    8181   }
    8282
    83    if ( ( row = mysql_fetch_row(result)) != NULL ) {
    84       cout << "Creating a new player" << endl;
     83   if ( ( row = mysql_fetch_row(result)) != NULL )
    8584      p = new Player(string(row[1]), string(row[2]));
    86    }else {
     85   else {
    8786      cout << "Returned no results for some reason" << endl;
    8887      p = NULL;
  • server/makefile

    r093c141 r384b7e0  
    11CC = g++
    2 LIB_FLAGS = -lssl -lmysqlclient -lcrypt -lrt
     2LIB_FLAGS = -lssl -lmysqlclient -lcrypt
    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 Map.o DataAccess.o
    66
    77server : server.cpp $(DEPENDENCIES)
     
    1717        $(CC) -c -o $@ $?
    1818
    19 WorldMap.o : $(COMMON_PATH)/WorldMap.cpp
     19Map.o : $(COMMON_PATH)/Map.cpp
    2020        $(CC) -c -o $@ $?
    2121
  • server/server.cpp

    r093c141 r384b7e0  
    66#include <sstream>
    77#include <cstring>
    8 #include <cmath>
    9 #include <sys/time.h>
    108
    119#include <vector>
     
    2826#include "../common/Common.h"
    2927#include "../common/Message.h"
    30 #include "../common/WorldMap.h"
    3128#include "../common/Player.h"
    3229
     
    3532using namespace std;
    3633
    37 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
     34bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
    3835
    3936void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     
    109106      exit(1);
    110107   }
    111 
    112    WorldMap* gameMap = WorldMap::createDefaultMap();
    113108 
    114109   sock = socket(AF_INET, SOCK_DGRAM, 0);
     
    124119   set_nonblock(sock);
    125120
    126    Player testP;
    127    clock_gettime(CLOCK_REALTIME, &testP.timeLastUpdated);
    128 
    129    cout << "Before sleep" << endl;
    130    // wait some time
    131    sleep(3);
    132    cout << "After sleep" << endl;
    133 
    134    testP.move();
    135 
    136 /*
    137121   bool broadcastResponse;
    138122   while (true) {
     
    145129         cout << "Got a message" << endl;
    146130
    147          broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
     131         broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
    148132
    149133         // probably replace this with a function that prints based on the
     
    174158      }
    175159   }
    176 */
    177160
    178161   return 0;
    179162}
    180163
    181 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
     164bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)
    182165{
    183166   DataAccess da;
     
    216199      case MSG_TYPE_LOGIN:
    217200      {
    218          cout << "Got login message" << endl;
    219 
    220201         string username(clientMsg.buffer);
    221202         string password(strchr(clientMsg.buffer, '\0')+1);
     
    306287      case MSG_TYPE_PLAYER_MOVE:
    307288      {
    308          cout << "Got a move message" << endl;
    309 
    310289         istringstream iss;
    311290         iss.str(clientMsg.buffer);
     
    326305              mapPlayers[id].addr.sin_port == from.sin_port )
    327306         {
    328             // we need to make sure the player can move here
    329             if (0 <= x && x < 300 && 0 <= y && y < 300 &&
    330                gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
    331             {
    332                // first we get the correct vector
    333                mapPlayers[id].target.x = x;
    334                mapPlayers[id].target.y = y;
    335                int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;
    336                int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;
    337                cout << "xDiff: " << xDiff << endl;               
    338                cout << "yDiff: " << yDiff << endl;               
    339 
    340                // then we get the correct angle
    341                double angle = atan2(yDiff, xDiff);
    342                cout << "angle: " << angle << endl;               
    343 
    344                // finally we use the angle to determine
    345                // how much the player moves
    346                // the player will move 50 pixels in the correct direction
    347                mapPlayers[id].pos.x += cos(angle)*50;
    348                mapPlayers[id].pos.y += sin(angle)*50;
    349                cout << "new x: " << mapPlayers[id].pos.x << endl;               
    350                cout << "new y: " << mapPlayers[id].pos.y << endl;               
    351 
    352                serverMsg.type = MSG_TYPE_PLAYER_MOVE;
    353                
    354                memcpy(serverMsg.buffer, &id, 4);
    355                memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4);
    356                memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4);
    357                //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
    358 
    359                broadcastResponse = true;
    360             }
    361             else
    362                cout << "Bad terrain detected" << endl;
     307            memcpy(&mapPlayers[id].pos.x, clientMsg.buffer+4, 4);
     308            memcpy(&mapPlayers[id].pos.y, clientMsg.buffer+8, 4);
     309
     310            serverMsg.type = MSG_TYPE_PLAYER_MOVE;
     311            memcpy(serverMsg.buffer, clientMsg.buffer, 12);
     312
     313            broadcastResponse = true;
    363314         }
    364315         else  // nned to send back a message indicating failure
     
    377328   }
    378329
    379    cout << "Got to the end of the switch" << endl;
    380 
    381330   return broadcastResponse;
    382331}
Note: See TracChangeset for help on using the changeset viewer.