rnd-19981016-2
[rocksndiamonds.git] / src / netserv.c
index b9dad16248cc83e0dcf4e6cbe2660f10c1a8e82f..bafb1fe8c1599508e894ceb3b2db275bd2660031 100644 (file)
 #include <netdb.h>
 
 #include "netserv.h"
-
-
-
-extern int verbose;
-
-extern void copydown(char *, char *, int);
-extern void fatal(char *);
-
-
+#include "misc.h"
 
 static int clients = 0;
 static int bots = 0;
 static int onceonly = 0;
 static int timetoplay = 0;
 static int is_daemon = 0;
-static int level = 5;
+static int levelnr = 5;
 static int mode = -1;
 static int paused = 0;
 
@@ -91,7 +83,8 @@ static void syserr(char *s)
 static void addtobuffer(struct user *u, unsigned char *b, int len)
 {
   if (u->nwrite + len >= MAX_BUFFER_SIZE)
-    fatal("Internal error: send buffer overflow");
+    Error(ERR_EXIT, "internal error: network send buffer overflow");
+
   memcpy(u->writbuf + u->nwrite, b, len);
   u->nwrite += len;
 }
@@ -188,7 +181,7 @@ static void dropuser(struct user *u)
   if (clients == 0)
   {
     mode = -1;
-    level = 5;
+    levelnr = 5;
     timetoplay = 0;
   }
 }
@@ -198,9 +191,8 @@ static void new_connect(int fd)
   struct user *u, *v;
   unsigned char nxn;
 
-  u = malloc(sizeof (struct user));
-  if (!u)
-    fatal("Out of memory");
+  u = checked_malloc(sizeof (struct user));
+
   u->fd = fd;
   u->nick[0] = 0;
   u->next = user0;
@@ -270,6 +262,7 @@ static void Handle_OP_PROTOCOL_VERSION(struct user *u, unsigned int len)
 static void Handle_OP_NUMBER_WANTED(struct user *u)
 {
   struct user *v;
+  int client_nr = u->number;
   int nr_wanted = buf[2];
   int nr_is_free = 1;
 
@@ -302,11 +295,16 @@ static void Handle_OP_NUMBER_WANTED(struct user *u)
   if (nr_is_free)
     u->number = nr_wanted;
 
-  buf[0] = 0;
+  buf[0] = client_nr;
   buf[1] = OP_NUMBER_WANTED;
   buf[2] = nr_wanted;
   buf[3] = u->number;
+
+  /*
   sendtoone(u, 4);
+  */
+
+  broadcast(NULL, 4, 0);
 }
 
 static void Handle_OP_NICKNAME(struct user *u, unsigned int len)
@@ -356,11 +354,11 @@ static void Handle_OP_NICKNAME(struct user *u, unsigned int len)
        sendtoone(u, 2+strlen(v->nick));
       }
     }
-    if (level != 5)
+    if (levelnr != 5)
     {
       buf[0] = 0;
       buf[1] = OP_LEVEL;
-      buf[2] = level;
+      buf[2] = levelnr;
       sendtoone(u, 3);
     }
     if (mode >= 0)
@@ -455,8 +453,8 @@ static void Handle_OP_STOP_PLAYING(struct user *u)
 static void Handle_OP_MOVE_FIGURE(struct user *u)
 {
   struct user *v;
-  int actions_complete = 1;
   int last_client_nr = 0;
+  int i;
 
   /* store player action */
   for (v=user0; v; v=v->next)
@@ -472,14 +470,15 @@ static void Handle_OP_MOVE_FIGURE(struct user *u)
   for (v=user0; v; v=v->next)
   {
     if (!v->action_received)
-    {
-      actions_complete = 0;
-      break;
-    }
+      return;
+
+    if (v->number > last_client_nr)
+      last_client_nr = v->number;
   }
 
-  if (!actions_complete)
-    return;
+  /* initialize all player actions to zero */
+  for (i=0; i<last_client_nr; i++)
+    buf[6 + i] = 0;
 
   /* broadcast actions of all players to all players */
   for (v=user0; v; v=v->next)
@@ -487,9 +486,6 @@ static void Handle_OP_MOVE_FIGURE(struct user *u)
     buf[6 + v->number-1] = v->action;
     v->action = 0;
     v->action_received = 0;
-
-    if (v->number > last_client_nr)
-      last_client_nr = v->number;
   }
 
   buf[2] = (unsigned char)((frame_counter >> 24) & 0xff);
@@ -693,7 +689,7 @@ void NetworkServer(int port, int serveronly)
          }
          memcpy(buf, &u->readbuf[4], len);
          u->nread -= 4 + len;
-         copydown(u->readbuf, u->readbuf + 4 + len, u->nread);
+         memmove(u->readbuf, u->readbuf + 4 + len, u->nread);
 
          buf[0] = u->number;
          if (!u->introduced && buf[1] != OP_NICKNAME)
@@ -781,7 +777,7 @@ void NetworkServer(int port, int serveronly)
              break;
            
            case OP_LEVEL:
-             level = buf[2];
+             levelnr = buf[2];
              if (verbose)
                printf("RND_SERVER: client %d (%s) sets level %d\n", u->number, u->nick, buf[2]);
              broadcast(NULL, 3, 0);