Index: server/server.cpp
===================================================================
--- server/server.cpp	(revision b128109e36df4199568551e736a76e5acca16912)
+++ server/server.cpp	(revision 60017fc77171d89da05e3eaab5eb4a9c3d0a0f6e)
@@ -6,4 +6,6 @@
 #include <sstream>
 #include <cstring>
+#include <cmath>
+#include <sys/time.h>
 
 #include <vector>
@@ -26,4 +28,5 @@
 #include "../common/Common.h"
 #include "../common/Message.h"
+#include "../common/WorldMap.h"
 #include "../common/Player.h"
 
@@ -32,5 +35,5 @@
 using namespace std;
 
-bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);
+bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
 
 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
@@ -106,4 +109,6 @@
       exit(1);
    }
+
+   WorldMap* gameMap = WorldMap::createDefaultMap();
  
    sock = socket(AF_INET, SOCK_DGRAM, 0);
@@ -119,4 +124,15 @@
    set_nonblock(sock);
 
+   Player testP;
+   clock_gettime(CLOCK_REALTIME, &testP.timeLastUpdated);
+
+   cout << "Before sleep" << endl;
+   // wait some time
+   sleep(3);
+   cout << "After sleep" << endl;
+
+   testP.move();
+
+/*
    bool broadcastResponse;
    while (true) {
@@ -129,5 +145,5 @@
          cout << "Got a message" << endl;
 
-         broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg);
+         broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
 
          // probably replace this with a function that prints based on the
@@ -158,9 +174,10 @@
       }
    }
+*/
 
    return 0;
 }
 
-bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)
+bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
 {
    DataAccess da;
@@ -199,4 +216,6 @@
       case MSG_TYPE_LOGIN:
       {
+         cout << "Got login message" << endl;
+
          string username(clientMsg.buffer);
          string password(strchr(clientMsg.buffer, '\0')+1);
@@ -287,4 +306,6 @@
       case MSG_TYPE_PLAYER_MOVE:
       {
+         cout << "Got a move message" << endl;
+
          istringstream iss;
          iss.str(clientMsg.buffer);
@@ -305,11 +326,39 @@
               mapPlayers[id].addr.sin_port == from.sin_port )
          {
-            memcpy(&mapPlayers[id].pos.x, clientMsg.buffer+4, 4);
-            memcpy(&mapPlayers[id].pos.y, clientMsg.buffer+8, 4);
-
-            serverMsg.type = MSG_TYPE_PLAYER_MOVE;
-            memcpy(serverMsg.buffer, clientMsg.buffer, 12);
-
-            broadcastResponse = true;
+            // we need to make sure the player can move here
+            if (0 <= x && x < 300 && 0 <= y && y < 300 &&
+               gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
+            {
+               // first we get the correct vector 
+               mapPlayers[id].target.x = x;
+               mapPlayers[id].target.y = y;
+               int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x;
+               int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y;
+               cout << "xDiff: " << xDiff << endl;               
+               cout << "yDiff: " << yDiff << endl;               
+
+               // then we get the correct angle
+               double angle = atan2(yDiff, xDiff);
+               cout << "angle: " << angle << endl;               
+
+               // finally we use the angle to determine
+               // how much the player moves
+               // the player will move 50 pixels in the correct direction
+               mapPlayers[id].pos.x += cos(angle)*50;
+               mapPlayers[id].pos.y += sin(angle)*50;
+               cout << "new x: " << mapPlayers[id].pos.x << endl;               
+               cout << "new y: " << mapPlayers[id].pos.y << endl;               
+
+               serverMsg.type = MSG_TYPE_PLAYER_MOVE;
+               
+               memcpy(serverMsg.buffer, &id, 4);
+               memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4);
+               memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4);
+               //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
+
+               broadcastResponse = true;
+            }
+            else
+               cout << "Bad terrain detected" << endl;
          }
          else  // nned to send back a message indicating failure
@@ -328,4 +377,6 @@
    }
 
+   cout << "Got to the end of the switch" << endl;
+
    return broadcastResponse;
 }
