Changeset 60017fc in network-game for server


Ignore:
Timestamp:
Feb 18, 2013, 6:58:40 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
093c141
Parents:
62ee2ce
Message:

Added code for player movement

Location:
server
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • server/DataAccess.cpp

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

    r62ee2ce r60017fc  
    11CC = g++
    2 LIB_FLAGS = -lssl -lmysqlclient -lcrypt
     2LIB_FLAGS = -lssl -lmysqlclient -lcrypt -lrt
    33FLAGS = $(LIB_FLAGS)
    44COMMON_PATH = ../common
    5 DEPENDENCIES = Common.o Message.o Player.o Map.o DataAccess.o
     5DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o
    66
    77server : server.cpp $(DEPENDENCIES)
     
    1717        $(CC) -c -o $@ $?
    1818
    19 Map.o : $(COMMON_PATH)/Map.cpp
     19WorldMap.o : $(COMMON_PATH)/WorldMap.cpp
    2020        $(CC) -c -o $@ $?
    2121
  • server/server.cpp

    r62ee2ce r60017fc  
    66#include <sstream>
    77#include <cstring>
     8#include <cmath>
     9#include <sys/time.h>
    810
    911#include <vector>
     
    2628#include "../common/Common.h"
    2729#include "../common/Message.h"
     30#include "../common/WorldMap.h"
    2831#include "../common/Player.h"
    2932
     
    3235using namespace std;
    3336
    34 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
     37bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
    3538
    3639void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
     
    106109      exit(1);
    107110   }
     111
     112   WorldMap* gameMap = WorldMap::createDefaultMap();
    108113 
    109114   sock = socket(AF_INET, SOCK_DGRAM, 0);
     
    119124   set_nonblock(sock);
    120125
     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/*
    121137   bool broadcastResponse;
    122138   while (true) {
     
    129145         cout << "Got a message" << endl;
    130146
    131          broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
     147         broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
    132148
    133149         // probably replace this with a function that prints based on the
     
    158174      }
    159175   }
     176*/
    160177
    161178   return 0;
    162179}
    163180
    164 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)
     181bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
    165182{
    166183   DataAccess da;
     
    199216      case MSG_TYPE_LOGIN:
    200217      {
     218         cout << "Got login message" << endl;
     219
    201220         string username(clientMsg.buffer);
    202221         string password(strchr(clientMsg.buffer, '\0')+1);
     
    287306      case MSG_TYPE_PLAYER_MOVE:
    288307      {
     308         cout << "Got a move message" << endl;
     309
    289310         istringstream iss;
    290311         iss.str(clientMsg.buffer);
     
    305326              mapPlayers[id].addr.sin_port == from.sin_port )
    306327         {
    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;
     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;
    314363         }
    315364         else  // nned to send back a message indicating failure
     
    328377   }
    329378
     379   cout << "Got to the end of the switch" << endl;
     380
    330381   return broadcastResponse;
    331382}
Note: See TracChangeset for help on using the changeset viewer.