Index: server/server.cpp
===================================================================
--- server/server.cpp	(revision 7b43385375bd02e506e07071412b3b79737213c2)
+++ server/server.cpp	(revision db582279cc218ed5cc315e4cc309861ff3fc8b98)
@@ -11,4 +11,6 @@
 #include <map>
 
+#include <sys/time.h>
+
 #include <sys/socket.h>
 #include <netdb.h>
@@ -34,5 +36,7 @@
 using namespace std;
 
-bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg);
+// from used to be const. Removed that so I could take a reference
+// and use it to send messages
+bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock);
 
 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
@@ -124,7 +128,33 @@
 
    bool broadcastResponse;
+   timespec ts;
+   long timeLastUpdated = 0, curTime = 0;
    while (true) {
 
       usleep(5000);
+
+      clock_gettime(CLOCK_REALTIME, &ts);
+      curTime = ts.tv_sec + ts.tv_nsec*1000000000;
+
+      if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50000) {
+         timeLastUpdated = curTime;
+
+         // maybe put this in a separate method
+         map<unsigned int, Player>::iterator it, it2;
+         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
+            if (!it->second.move(gameMap)) {
+               cout << "Cenceling move" << endl;
+               //serverMsg.type = MSG_TYPE_PLAYER;
+               //it->second.serialize(serverMsg.buffer);
+
+               cout << "about to send move cencellation" << endl;
+               for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
+               {
+                  //if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
+                    // error("sendMessage");
+               }
+            }
+         }
+      }
 
       n = receiveMessage(&clientMsg, sock, &from);
@@ -133,5 +163,5 @@
          cout << "Got a message" << endl;
 
-         broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg);
+         broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg, sock);
 
          // probably replace this with a function that prints based on the
@@ -146,4 +176,5 @@
             for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
             {
+               cout << "Sent message back to " << it->second.name << endl;
                if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
                   error("sendMessage");
@@ -159,5 +190,10 @@
       }
 
+      // we don't want to update and broadcast player positions here
+      // when a player sends a position update, we want to check if
+      // it's reasonable and send it out to all other players
+
       // update player positions
+      /*
       map<unsigned int, Player>::iterator it;
       for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
@@ -167,4 +203,5 @@
 
       broadcastPlayerPositions(mapPlayers, sock);
+      */
    }
 
@@ -172,5 +209,5 @@
 }
 
-bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg)
+bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg, int sock)
 {
    DataAccess da;
@@ -209,4 +246,6 @@
          cout << "Got login message" << endl;
 
+         serverMsg.type = MSG_TYPE_LOGIN;
+         
          string username(clientMsg.buffer);
          string password(strchr(clientMsg.buffer, '\0')+1);
@@ -224,15 +263,39 @@
          else
          {
+            serverMsg.type = MSG_TYPE_PLAYER;
+
             p->setAddr(from);
             updateUnusedId(unusedId, mapPlayers);
             p->id = unusedId;
+            cout << "new player id: " << p->id << endl;
+
+            // tell the new player about all the existing players
+            cout << "Sending other players to new player" << endl;
+
+            map<unsigned int, Player>::iterator it;
+            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
+            {
+               it->second.serialize(serverMsg.buffer);
+
+               cout << "sending info about " << it->second.name  << endl;
+               cout << "sending ind " << it->second.id  << endl;
+               if ( sendMessage(&serverMsg, sock, &from) < 0 )
+                  error("sendMessage");
+            }
+
+            p->serialize(serverMsg.buffer);
+            cout << "Should be broadcasting the message" << endl;
+
+            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
+            {
+               cout << "Sent message back to " << it->second.name << endl;
+               if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
+                  error("sendMessage");
+            }
+
+            serverMsg.type = MSG_TYPE_LOGIN;
             mapPlayers[unusedId] = *p;
-
-            // sendd back the new player info to the user
-            p->serialize(serverMsg.buffer);
-         }
-
-         serverMsg.type = MSG_TYPE_LOGIN;
-   
+         }
+
          delete(p);
 
@@ -266,6 +329,5 @@
          }
 
-         // should really be serverMsg.type = MSG_TYPE_LOGOUT;
-         serverMsg.type = MSG_TYPE_LOGIN;
+         serverMsg.type = MSG_TYPE_LOGOUT;
 
          break;
@@ -321,6 +383,6 @@
                cout << "valid terrain" << endl;
 
-               cout << "orig x: " << mapPlayers[id].pos.x << endl;
-               cout << "orig y: " << mapPlayers[id].pos.y << endl;
+               //cout << "orig x: " << mapPlayers[id].pos.x << endl;
+               //cout << "orig y: " << mapPlayers[id].pos.y << endl;
                // first we get the correct vector
                mapPlayers[id].target.x = x;
@@ -328,6 +390,6 @@
                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;
+               //cout << "xDiff: " << xDiff << endl;
+               //cout << "yDiff: " << yDiff << endl;
 
                // then we get the correct angle
@@ -344,6 +406,6 @@
                
                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+4, &mapPlayers[id].target.x, 4);
+               memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
                //memcpy(serverMsg.buffer, clientMsg.buffer, 12);
 
