From: Holger Schemel Date: Sat, 10 Oct 1998 01:12:25 +0000 (+0200) Subject: rnd-19981010-2 X-Git-Tag: 1.2.0^2~61 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=3d236f777bca55fef0f6b23122b6affd0f424249;p=rocksndiamonds.git rnd-19981010-2 --- diff --git a/src/game.c b/src/game.c index 640c2486..f6e226bc 100644 --- a/src/game.c +++ b/src/game.c @@ -2913,6 +2913,13 @@ void GameActions(byte player_action) } } + + if (tape.pausing || (tape.playing && !TapePlayDelay())) + return; + else if (tape.recording) + TapeRecordDelay(); + + if (tape.playing) recorded_player_action = TapePlayAction(); else @@ -2959,10 +2966,12 @@ void GameActions(byte player_action) ScrollScreen(NULL, SCROLL_GO_ON); + /* if (tape.pausing || (tape.playing && !TapePlayDelay())) return; else if (tape.recording) TapeRecordDelay(); + */ FrameCounter++; TimeFrames++; diff --git a/src/init.c b/src/init.c index ecafc3b8..5fecebf5 100644 --- a/src/init.c +++ b/src/init.c @@ -23,6 +23,7 @@ #include "gfxload.h" #include "gifload.h" #include "network.h" +#include "netserv.h" #ifdef DEBUG /* @@ -56,6 +57,14 @@ static void InitElementProperties(void); void OpenAll(int argc, char *argv[]) { + if (serveronly) + { + NetworkServer(server_port, serveronly); + + /* never reached */ + exit(0); + } + InitLevelAndPlayerInfo(); InitCounter(); diff --git a/src/netserv.c b/src/netserv.c index e81d8816..221c8ff9 100644 --- a/src/netserv.c +++ b/src/netserv.c @@ -41,7 +41,7 @@ static int clients = 0; static int bots = 0; static int onceonly = 0; static int timetoplay = 0; -static int is_daemon = 0; +static int daemon = 0; static int level = 5; static int mode = -1; static int paused = 0; @@ -81,14 +81,14 @@ static unsigned long frame_counter = 0; static fd_set fds; -void syserr(char *s) +static void syserr(char *s) { - if (!is_daemon) + if (!daemon) fprintf(stderr, "fatal: %s failed.\n", s); exit(1); } -void addtobuffer(struct user *u, unsigned char *b, int len) +static void addtobuffer(struct user *u, unsigned char *b, int len) { if (u->nwrite + len >= MAX_BUFFER_SIZE) fatal("Internal error: send buffer overflow"); @@ -96,7 +96,7 @@ void addtobuffer(struct user *u, unsigned char *b, int len) u->nwrite += len; } -void flushuser(struct user *u) +static void flushuser(struct user *u) { if (u->nwrite) { @@ -105,7 +105,7 @@ void flushuser(struct user *u) } } -void broadcast(struct user *except, int len, int activeonly) +static void broadcast(struct user *except, int len, int activeonly) { struct user *u; @@ -116,14 +116,14 @@ void broadcast(struct user *except, int len, int activeonly) addtobuffer(u, realbuf, 4 + len); } -void sendtoone(struct user *to, int len) +static void sendtoone(struct user *to, int len) { realbuf[0] = realbuf[1] = realbuf[2] = 0; realbuf[3] = (unsigned char)len; addtobuffer(to, realbuf, 4 + len); } -void dropuser(struct user *u) +static void dropuser(struct user *u) { struct user *v, *w; @@ -193,10 +193,199 @@ void dropuser(struct user *u) } } -void do_play() +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->fd = fd; + u->nick[0] = 0; + u->next = user0; + u->nextvictim = NULL; + u->active = 0; + u->nread = 0; + u->nwrite = 0; + u->playing = 0; + u->isbot = 0; + u->introduced = 0; + u->games = 0; + u->action = 0; + u->action_received = 0; + + user0 = u; + + nxn = 1; + + again: + v = u->next; + while(v) + { + if (v->number == nxn) + { + nxn++; + goto again; + } + v = v->next; + } + + u->number = nxn; + if (verbose) + printf("RND_SERVER: client %d connecting from %s\n", nxn, inet_ntoa(saddr.sin_addr)); + clients++; + + buf[0] = 0; + buf[1] = OP_YOUR_NUMBER; + buf[2] = u->number; + sendtoone(u, 3); +} + +static void Handle_OP_PROTOCOL_VERSION(struct user *u, unsigned int len) +{ + if (len != 5 || buf[2] != PROT_VERS_1 || buf[3] != PROT_VERS_2) + { + if (verbose) + printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]); + + buf[0] = 0; + buf[1] = OP_BADVERS; + buf[2] = PROT_VERS_1; + buf[3] = PROT_VERS_2; + buf[4] = PROT_VERS_3; + sendtoone(u, 5); + flushuser(u); + + dropuser(u); + interrupt = 1; + } + else + { + if (verbose) + printf("RND_SERVER: client %d (%s) uses protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]); + } +} + +static void Handle_OP_NUMBER_WANTED(struct user *u) +{ + struct user *v; + int nr_wanted = buf[2]; + int nr_is_free = 1; + + if (verbose) + printf("RND_SERVER: client %d (%s) wants to switch to # %d\n", + u->number, u->nick, nr_wanted); + + for (v=user0; v; v=v->next) + { + if (v->number == nr_wanted) + { + nr_is_free = 0; + break; + } + } + + if (verbose) + { + if (nr_is_free) + printf("RND_SERVER: client %d (%s) switches to # %d\n", + u->number, u->nick, nr_wanted); + else if (u->number == nr_wanted) + printf("RND_SERVER: client %d (%s) still has # %d\n", + u->number, u->nick, nr_wanted); + else + printf("RND_SERVER: client %d (%s) cannot switch (client %d still exists)\n", + u->number, u->nick, nr_wanted); + } + + if (nr_is_free) + u->number = nr_wanted; + + buf[0] = 0; + buf[1] = OP_NUMBER_WANTED; + buf[2] = nr_wanted; + buf[3] = u->number; + sendtoone(u, 4); +} + +static void Handle_OP_NICKNAME(struct user *u, unsigned int len) +{ + struct user *v; + int i; + + if (len>16) + len=16; + memcpy(u->nick, &buf[2], len-2); + u->nick[len-2] = 0; + for (i=0; inick[i] < ' ' || + (u->nick[i] > 0x7e && u->nick[i] <= 0xa0)) + { + u->nick[i] = 0; + break; + } + } + + if (!u->introduced) + { + buf[0] = u->number; + buf[1] = OP_PLAYER_CONNECTED; + broadcast(u, 2, 0); + } + + if (verbose) + printf("RND_SERVER: client %d calls itself \"%s\"\n", u->number, u->nick); + buf[1] = OP_NICKNAME; + broadcast(u, len, 0); + + if (!u->introduced) + { + for (v=user0; v; v=v->next) + { + if (v != u && v->introduced) + { + buf[0] = v->number; + buf[1] = OP_PLAYER_CONNECTED; + buf[2] = (v->games >> 8); + buf[3] = (v->games & 0xff); + sendtoone(u, 4); + buf[1] = OP_NICKNAME; + memcpy(&buf[2], v->nick, 14); + sendtoone(u, 2+strlen(v->nick)); + } + } + if (level != 5) + { + buf[0] = 0; + buf[1] = OP_LEVEL; + buf[2] = level; + sendtoone(u, 3); + } + if (mode >= 0) + { + buf[1] = OP_MODE; + buf[2] = mode; + sendtoone(u, 3); + } + } + + u->introduced = 1; +} + +static void Handle_OP_START_PLAYING(struct user *u) { struct user *v, *w; + if (verbose) + printf("RND_SERVER: client %d (%s) starts game [level %d from levedir %d (%s)]\n", + u->number, u->nick, + (buf[2] << 8) + buf[3], + (buf[4] << 8) + buf[5], + &buf[6]); + timetoplay = 0; + for (w=user0; w; w=w->next) { if (w->introduced) @@ -238,59 +427,89 @@ void do_play() } broadcast(NULL, 10 + strlen(&buf[10])+1, 0); +} +static void Handle_OP_PAUSE_PLAYING(struct user *u) +{ + if (verbose) + printf("RND_SERVER: client %d (%s) pauses game\n", u->number, u->nick); + broadcast(NULL, 2, 0); + paused = 1; } -void new_connect(int fd) +static void Handle_OP_CONTINUE_PLAYING(struct user *u) { - struct user *u, *v; - unsigned char nxn; + if (verbose) + printf("RND_SERVER: client %d (%s) continues game\n", u->number, u->nick); + broadcast(NULL, 2, 0); + paused = 0; +} - u = malloc(sizeof (struct user)); - if (!u) - fatal("Out of memory"); - u->fd = fd; - u->nick[0] = 0; - u->next = user0; - u->nextvictim = NULL; - u->active = 0; - u->nread = 0; - u->nwrite = 0; - u->playing = 0; - u->isbot = 0; - u->introduced = 0; - u->games = 0; - u->action = 0; - u->action_received = 0; +static void Handle_OP_STOP_PLAYING(struct user *u) +{ + if (verbose) + printf("RND_SERVER: client %d (%s) stops game\n", u->number, u->nick); + broadcast(NULL, 2, 0); +} - user0 = u; +static void Handle_OP_MOVE_FIGURE(struct user *u) +{ + struct user *v; + int actions_complete = 1; + int last_client_nr = 0; - nxn = 1; + /* store player action */ + for (v=user0; v; v=v->next) + { + if (v->number == u->number) + { + v->action = buf[2]; + v->action_received = 1; + } + } -again: - v = u->next; - while(v) + /* check if server received action from each player */ + for (v=user0; v; v=v->next) { - if (v->number == nxn) + if (!v->action_received) { - nxn++; - goto again; + actions_complete = 0; + break; } - v = v->next; } - u->number = nxn; - if (verbose) - printf("RND_SERVER: client %d connecting from %s\n", nxn, inet_ntoa(saddr.sin_addr)); - clients++; + if (!actions_complete) + return; - buf[0] = 0; - buf[1] = OP_YOUR_NUMBER; - buf[2] = u->number; - sendtoone(u, 3); + /* broadcast actions of all players to all players */ + for (v=user0; v; v=v->next) + { + 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); + buf[3] = (unsigned char)((frame_counter >> 16) & 0xff); + buf[4] = (unsigned char)((frame_counter >> 8) & 0xff); + buf[5] = (unsigned char)((frame_counter >> 0) & 0xff); + + broadcast(NULL, 6 + last_client_nr, 0); + + frame_counter++; + + /* + if (verbose) + printf("RND_SERVER: frame %d: client %d (%s) moves player [0x%02x]\n", + frame_counter, + u->number, u->nick, buf[2]); + */ } -void NetworkServer(int port, int onceonly) +void NetworkServer(int port, int serveronly) { int i, sl, on; struct user *u, *v, *w; @@ -299,7 +518,7 @@ void NetworkServer(int port, int onceonly) unsigned int len; struct protoent *tcpproto; struct timeval tv; - int is_daemon = 0; + int daemon = 0; #ifndef NeXT struct sigaction sact; @@ -308,6 +527,9 @@ void NetworkServer(int port, int onceonly) if (port == 0) port = DEFAULTPORT; + if (!serveronly) + onceonly = 1; + if ((tcpproto = getprotobyname("tcp")) != NULL) tcp = tcpproto->p_proto; @@ -336,7 +558,7 @@ void NetworkServer(int port, int onceonly) listen(lfd, 5); - if (is_daemon) + if (daemon) { /* become a daemon, breaking all ties with the controlling terminal */ verbose = 0; @@ -345,12 +567,14 @@ void NetworkServer(int port, int onceonly) if (i != lfd) close(i); } + if (fork()) exit(0); setsid(); if (fork()) exit(0); chdir("/"); + /* open a fake stdin, stdout, stderr, just in case */ open("/dev/null", O_RDONLY); open("/dev/null", O_WRONLY); @@ -369,6 +593,7 @@ void NetworkServer(int port, int onceonly) { interrupt = 0; + /* if (timetoplay && time(NULL) >= timetoplay) { buf[0] = 0; @@ -377,6 +602,7 @@ void NetworkServer(int port, int onceonly) printf("RND_SERVER: everyone lost... restarting game\n"); timetoplay = 0; } + */ for (u=user0; u; u=u->next) flushuser(u); @@ -483,64 +709,35 @@ void NetworkServer(int port, int onceonly) switch(buf[1]) { case OP_NICKNAME: - if (len>16) - len=16; - memcpy(u->nick, &buf[2], len-2); - u->nick[len-2] = 0; - for (i=0; inick[i] < ' ' || - (u->nick[i] > 0x7e && u->nick[i] <= 0xa0)) - { - u->nick[i] = 0; - break; - } - } + Handle_OP_NICKNAME(u, len); + break; - if (!u->introduced) - { - buf[0] = u->number; - buf[1] = OP_PLAYER_CONNECTED; - broadcast(u, 2, 0); - } - - if (verbose) - printf("RND_SERVER: client %d calls itself \"%s\"\n", u->number, u->nick); - buf[1] = OP_NICKNAME; - broadcast(u, len, 0); + case OP_PROTOCOL_VERSION: + Handle_OP_PROTOCOL_VERSION(u, len); + break; - if (!u->introduced) - { - for (v=user0; v; v=v->next) - { - if (v != u && v->introduced) - { - buf[0] = v->number; - buf[1] = OP_PLAYER_CONNECTED; - buf[2] = (v->games >> 8); - buf[3] = (v->games & 0xff); - sendtoone(u, 4); - buf[1] = OP_NICKNAME; - memcpy(&buf[2], v->nick, 14); - sendtoone(u, 2+strlen(v->nick)); - } - } - if (level != 5) - { - buf[0] = 0; - buf[1] = OP_LEVEL; - buf[2] = level; - sendtoone(u, 3); - } - if (mode >= 0) - { - buf[1] = OP_MODE; - buf[2] = mode; - sendtoone(u, 3); - } - } + case OP_NUMBER_WANTED: + Handle_OP_NUMBER_WANTED(u); + break; + + case OP_START_PLAYING: + Handle_OP_START_PLAYING(u); + break; + + case OP_PAUSE_PLAYING: + Handle_OP_PAUSE_PLAYING(u); + break; - u->introduced = 1; + case OP_CONTINUE_PLAYING: + Handle_OP_CONTINUE_PLAYING(u); + break; + + case OP_STOP_PLAYING: + Handle_OP_STOP_PLAYING(u); + break; + + case OP_MOVE_FIGURE: + Handle_OP_MOVE_FIGURE(u); break; case OP_KILL: @@ -568,75 +765,6 @@ void NetworkServer(int port, int onceonly) } break; - case OP_START_PLAYING: - if (verbose) - printf("RND_SERVER: client %d (%s) starts game [level %d from levedir %d (%s)]\n", - u->number, u->nick, - (buf[2] << 8) + buf[3], - (buf[4] << 8) + buf[5], - &buf[6]); - timetoplay = 0; - do_play(); - break; - - case OP_MOVE_FIGURE: - { - int actions_complete = 1; - int last_client_nr = 0; - - /* store player action */ - for (v=user0; v; v=v->next) - { - if (v->number == u->number) - { - v->action = buf[2]; - v->action_received = 1; - } - } - - /* check if server received action from each player */ - for (v=user0; v; v=v->next) - { - if (!v->action_received) - { - actions_complete = 0; - break; - } - } - - if (!actions_complete) - break; - - /* broadcast actions of all players to all players */ - for (v=user0; v; v=v->next) - { - 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); - buf[3] = (unsigned char)((frame_counter >> 16) & 0xff); - buf[4] = (unsigned char)((frame_counter >> 8) & 0xff); - buf[5] = (unsigned char)((frame_counter >> 0) & 0xff); - - broadcast(NULL, 6 + last_client_nr, 0); - - frame_counter++; - - /* - if (verbose) - printf("RND_SERVER: frame %d: client %d (%s) moves player [0x%02x]\n", - frame_counter, - u->number, u->nick, buf[2]); - */ - - break; - } - case OP_MODE: mode = buf[2]; if (verbose) @@ -644,20 +772,6 @@ void NetworkServer(int port, int onceonly) broadcast(NULL, 3, 0); break; - case OP_PAUSE: - if (verbose) - printf("RND_SERVER: client %d (%s) pauses game\n", u->number, u->nick); - broadcast(NULL, 2, 0); - paused = 1; - break; - - case OP_CONT: - if (verbose) - printf("RND_SERVER: client %d (%s) continues game\n", u->number, u->nick); - broadcast(NULL, 2, 0); - paused = 0; - break; - case OP_BOT: if (!u->isbot) bots++; @@ -745,72 +859,6 @@ void NetworkServer(int port, int onceonly) broadcast(u, len, 0); break; - case OP_NUMBER_WANTED: - { - int nr_wanted = buf[2]; - int nr_is_free = 1; - - if (verbose) - printf("RND_SERVER: client %d (%s) wants to switch to # %d\n", - u->number, u->nick, nr_wanted); - - for (v=user0; v; v=v->next) - { - if (v->number == nr_wanted) - { - nr_is_free = 0; - break; - } - } - - if (verbose) - { - if (nr_is_free) - printf("RND_SERVER: client %d (%s) switches to # %d\n", - u->number, u->nick, nr_wanted); - else if (u->number == nr_wanted) - printf("RND_SERVER: client %d (%s) still has # %d\n", - u->number, u->nick, nr_wanted); - else - printf("RND_SERVER: client %d (%s) cannot switch (client %d still exists)\n", - u->number, u->nick, nr_wanted); - } - - if (nr_is_free) - u->number = nr_wanted; - - buf[0] = 0; - buf[1] = OP_NUMBER_WANTED; - buf[2] = nr_wanted; - buf[3] = u->number; - sendtoone(u, 4); - break; - } - - case OP_PROTOCOL_VERSION: - if (len != 5 || buf[2] != PROT_VERS_1 || buf[3] != PROT_VERS_2) - { - if (verbose) - printf("RND_SERVER: client %d (%s) has wrong protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]); - - buf[0] = 0; - buf[1] = OP_BADVERS; - buf[2] = PROT_VERS_1; - buf[3] = PROT_VERS_2; - buf[4] = PROT_VERS_3; - sendtoone(u, 5); - flushuser(u); - - dropuser(u); - interrupt = 1; - } - else - { - if (verbose) - printf("RND_SERVER: client %d (%s) uses protocol version %d.%d.%d\n", u->number, u->nick, buf[2], buf[3], buf[4]); - } - break; - case OP_LINES: if (len != 3) { diff --git a/src/netserv.h b/src/netserv.h index 537b3002..55ebf8e9 100644 --- a/src/netserv.h +++ b/src/netserv.h @@ -22,25 +22,25 @@ #define OP_PROTOCOL_VERSION 1 #define OP_BAD_PROTOCOL_VERSION 2 -#define OP_PLAYER_CONNECTED 3 -#define OP_PLAYER_DISCONNECTED 4 -#define OP_YOUR_NUMBER 5 -#define OP_NUMBER_WANTED 6 -#define OP_NICKNAME 7 +#define OP_YOUR_NUMBER 3 +#define OP_NUMBER_WANTED 4 +#define OP_NICKNAME 5 +#define OP_PLAYER_CONNECTED 6 +#define OP_PLAYER_DISCONNECTED 7 #define OP_START_PLAYING 8 -#define OP_MOVE_FIGURE 9 - -#define OP_LOST 10 -#define OP_GONE 11 -#define OP_CLEAR 12 -#define OP_LINES 13 -#define OP_GROW 14 -#define OP_MODE 15 -#define OP_LEVEL 16 -#define OP_BOT 17 -#define OP_KILL 18 -#define OP_PAUSE 19 -#define OP_CONT 20 +#define OP_PAUSE_PLAYING 9 +#define OP_CONTINUE_PLAYING 10 +#define OP_STOP_PLAYING 11 +#define OP_MOVE_FIGURE 12 + +#define OP_LOST 13 +#define OP_CLEAR 14 +#define OP_LINES 15 +#define OP_GROW 16 +#define OP_MODE 17 +#define OP_LEVEL 18 +#define OP_BOT 19 +#define OP_KILL 20 #define OP_BADVERS 21 #define OP_MSG 22 #define OP_YOUARE 23 diff --git a/src/network.c b/src/network.c index bb4ea67e..2d4e0e64 100644 --- a/src/network.c +++ b/src/network.c @@ -31,6 +31,8 @@ #include "tape.h" #include "files.h" #include "tools.h" +#include "buttons.h" +#include "screens.h" #include "misc.h" #define MAXNICKLEN 14 @@ -49,6 +51,8 @@ struct user me = NULL }; +static char msgbuf[300]; + /* server stuff */ int sfd; @@ -56,7 +60,7 @@ unsigned char realbuf[512], readbuf[MAX_BUFFER_SIZE], writbuf[MAX_BUFFER_SIZE]; unsigned char *buf = realbuf + 4; int nread = 0, nwrite = 0; -void sysmsg(char *s) +static void sysmsg(char *s) { if (verbose) { @@ -65,7 +69,7 @@ void sysmsg(char *s) } } -void *mmalloc(int n) +static void *mmalloc(int n) { void *r; @@ -75,7 +79,7 @@ void *mmalloc(int n) return r; } -void u_sleep(int i) +static void u_sleep(int i) { struct timeval tm; tm.tv_sec = i / 1000000; @@ -83,7 +87,7 @@ void u_sleep(int i) select(0, NULL, NULL, NULL, &tm); } -void flushbuf() +static void flushbuf() { if (nwrite) { @@ -92,7 +96,7 @@ void flushbuf() } } -void sendbuf(int len) +static void sendbuf(int len) { if (!standalone) { @@ -139,12 +143,12 @@ char *get_user_name(unsigned char c) return("no name"); } -void InitNetworkServer(int port) +static void StartNetworkServer(int port) { switch (fork()) { case 0: - NetworkServer(port, !serveronly); + NetworkServer(port, serveronly); /* never reached */ exit(0); @@ -198,7 +202,7 @@ BOOL ConnectToServer(char *host, int port) { printf("No rocksndiamonds server on localhost - starting up one ...\n"); - InitNetworkServer(port); + StartNetworkServer(port); for (i=0; i<6; i++) { @@ -270,6 +274,27 @@ void SendToServer_StartPlaying() sendbuf(10 + strlen(leveldir[leveldir_nr].name)+1); } +void SendToServer_PausePlaying() +{ + buf[1] = OP_PAUSE_PLAYING; + + sendbuf(2); +} + +void SendToServer_ContinuePlaying() +{ + buf[1] = OP_CONTINUE_PLAYING; + + sendbuf(2); +} + +void SendToServer_StopPlaying() +{ + buf[1] = OP_STOP_PLAYING; + + sendbuf(2); +} + void SendToServer_MovePlayer(byte player_action) { buf[1] = OP_MOVE_FIGURE; @@ -278,239 +303,300 @@ void SendToServer_MovePlayer(byte player_action) sendbuf(3); } -void handlemessages() +static void Handle_OP_BAD_PROTOCOL_VERSION() +{ + Error(ERR_RETURN, "protocol version mismatch"); + Error(ERR_EXIT, "server expects %d.%d.x instead of %d.%d.%d", + buf[2], buf[3], PROT_VERS_1, PROT_VERS_2, PROT_VERS_3); +} + +static void Handle_OP_YOUR_NUMBER() +{ + int new_client_nr = buf[2]; + int new_index_nr = new_client_nr - 1; + + printf("OP_YOUR_NUMBER: %d\n", buf[0]); + me.nr = new_client_nr; + + stored_player[new_index_nr] = *local_player; + local_player = &stored_player[new_index_nr]; + + TestPlayer = new_index_nr; + + if (me.nr > MAX_PLAYERS) + Error(ERR_EXIT, "sorry - no more than %d players", MAX_PLAYERS); + + sprintf(msgbuf, "you get client # %d", new_client_nr); + sysmsg(msgbuf); +} + +static void Handle_OP_NUMBER_WANTED() +{ + int client_nr_wanted = buf[2]; + int new_client_nr = buf[3]; + int new_index_nr = new_client_nr - 1; + + printf("OP_NUMBER_WANTED: %d\n", buf[0]); + + if (new_client_nr != client_nr_wanted) + { + char *color[] = { "yellow", "red", "green", "blue" }; + + sprintf(msgbuf, "Sorry ! You are %s player !", + color[new_index_nr]); + Request(msgbuf, REQ_CONFIRM); + + sprintf(msgbuf, "cannot switch -- you keep client # %d", + new_client_nr); + sysmsg(msgbuf); + } + else + { + if (me.nr != client_nr_wanted) + sprintf(msgbuf, "switching to client # %d", new_client_nr); + else + sprintf(msgbuf, "keeping client # %d", new_client_nr); + sysmsg(msgbuf); + + me.nr = new_client_nr; + + stored_player[new_index_nr] = *local_player; + local_player = &stored_player[new_index_nr]; + + TestPlayer = new_index_nr; + } +} + +static void Handle_OP_NICKNAME(unsigned int len) +{ + struct user *u; + + printf("OP_NICKNAME: %d\n", buf[0]); + u = finduser(buf[0]); + buf[len] = 0; + sprintf(msgbuf, "client %d calls itself \"%s\"", buf[0], &buf[2]); + sysmsg(msgbuf); + strncpy(u->name, &buf[2], MAXNICKLEN); +} + +static void Handle_OP_PLAYER_CONNECTED() { - unsigned int len; struct user *u, *v = NULL; - static char msgbuf[300]; - while (nread >= 4 && nread >= 4 + readbuf[3]) + printf("OP_PLAYER_CONNECTED: %d\n", buf[0]); + sprintf(msgbuf, "new client %d connected", buf[0]); + sysmsg(msgbuf); + + for (u = &me; u; u = u->next) { - len = readbuf[3]; - if (readbuf[0] || readbuf[1] || readbuf[2]) - fatal("Wrong server line length"); + if (u->nr == buf[0]) + Error(ERR_EXIT, "multiplayer server sent duplicate player id"); + else + v = u; + } - memcpy(buf, &readbuf[4], len); - nread -= 4 + len; - copydown(readbuf, readbuf + 4 + len, nread); + v->next = u = mmalloc(sizeof(struct user)); + u->nr = buf[0]; + u->name[0] = '\0'; + u->next = NULL; +} - switch(buf[1]) - { - case OP_YOUR_NUMBER: - { - int new_client_nr = buf[2]; - int new_index_nr = new_client_nr - 1; +static void Handle_OP_PLAYER_DISCONNECTED() +{ + struct user *u, *v; - printf("OP_YOUR_NUMBER: %d\n", buf[0]); - me.nr = new_client_nr; + printf("OP_PLAYER_DISCONNECTED: %d\n", buf[0]); + u = finduser(buf[0]); + sprintf(msgbuf, "client %d (%s) disconnected", + buf[0], get_user_name(buf[0])); + sysmsg(msgbuf); - stored_player[new_index_nr] = *local_player; - local_player = &stored_player[new_index_nr]; + for (v = &me; v; v = v->next) + if (v->next == u) + v->next = u->next; + free(u); +} - TestPlayer = new_index_nr; +static void Handle_OP_START_PLAYING() +{ + int new_level_nr, new_leveldir_nr; + unsigned long new_random_seed; + unsigned char *new_leveldir_name; + + /* + if (game_status == PLAYING) + break; + */ + + new_level_nr = (buf[2] << 8) + buf[3]; + new_leveldir_nr = (buf[4] << 8) + buf[5]; + new_random_seed = + (buf[6] << 24) | (buf[7] << 16) | (buf[8] << 8) | (buf[9]); + new_leveldir_name = &buf[10]; + + printf("OP_START_PLAYING: %d\n", buf[0]); + sprintf(msgbuf, "client %d starts game [level %d from levedir %d (%s)]\n", + buf[0], + new_level_nr, + new_leveldir_nr, + new_leveldir_name); + sysmsg(msgbuf); - if (me.nr > MAX_PLAYERS) - Error(ERR_EXIT, "sorry - no more than %d players", MAX_PLAYERS); - sprintf(msgbuf, "you get client # %d", new_client_nr); - sysmsg(msgbuf); + if (strcmp(leveldir[new_leveldir_nr].name, new_leveldir_name) != 0) + Error(ERR_RETURN, "no such level directory: '%s'",new_leveldir_name); - break; - } + leveldir_nr = new_leveldir_nr; - case OP_NUMBER_WANTED: - { - int client_nr_wanted = buf[2]; - int new_client_nr = buf[3]; - int new_index_nr = new_client_nr - 1; + local_player->leveldir_nr = leveldir_nr; + LoadPlayerInfo(PLAYER_LEVEL); + SavePlayerInfo(PLAYER_SETUP); - printf("OP_NUMBER_WANTED: %d\n", buf[0]); + level_nr = new_level_nr; - if (new_client_nr != client_nr_wanted) - { - char *color[] = { "yellow", "red", "green", "blue" }; + TapeErase(); + LoadLevelTape(level_nr); - sprintf(msgbuf, "Sorry ! You are %s player !", - color[new_index_nr]); - Request(msgbuf, REQ_CONFIRM); + GetPlayerConfig(); + LoadLevel(level_nr); - sprintf(msgbuf, "cannot switch -- you keep client # %d", - new_client_nr); - sysmsg(msgbuf); - } - else - { - if (me.nr != client_nr_wanted) - sprintf(msgbuf, "switching to client # %d", new_client_nr); - else - sprintf(msgbuf, "keeping client # %d", new_client_nr); - sysmsg(msgbuf); - me.nr = new_client_nr; - stored_player[new_index_nr] = *local_player; - local_player = &stored_player[new_index_nr]; + if (autorecord_on) + TapeStartRecording(); - TestPlayer = new_index_nr; - } - break; - } - case OP_PLAYER_CONNECTED: - printf("OP_PLAYER_CONNECTED: %d\n", buf[0]); - sprintf(msgbuf, "new client %d connected", buf[0]); - sysmsg(msgbuf); + if (tape.recording) + tape.random_seed = new_random_seed; - for (u = &me; u; u = u->next) - { - if (u->nr == buf[0]) - Error(ERR_EXIT, "multiplayer server sent duplicate player id"); - else - v = u; - } + InitRND(new_random_seed); - v->next = u = mmalloc(sizeof(struct user)); - u->nr = buf[0]; - u->name[0] = '\0'; - u->next = NULL; - break; + /* + printf("tape.random_seed == %d\n", tape.random_seed); + */ - case OP_NICKNAME: - printf("OP_NICKNAME: %d\n", buf[0]); - u = finduser(buf[0]); - buf[len] = 0; - sprintf(msgbuf, "client %d calls itself \"%s\"", buf[0], &buf[2]); - sysmsg(msgbuf); - strncpy(u->name, &buf[2], MAXNICKLEN); - break; - - case OP_GONE: - printf("OP_GONE: %d\n", buf[0]); - u = finduser(buf[0]); - sprintf(msgbuf, "client %d (%s) disconnected", - buf[0], get_user_name(buf[0])); - sysmsg(msgbuf); + game_status = PLAYING; + InitGame(); +} - for (v = &me; v; v = v->next) - if (v->next == u) - v->next = u->next; - free(u); +static void Handle_OP_PAUSE_PLAYING() +{ + printf("OP_PAUSE_PLAYING: %d\n", buf[0]); + sprintf(msgbuf, "client %d pauses game", buf[0]); + sysmsg(msgbuf); - break; + tape.pausing = TRUE; + DrawVideoDisplay(VIDEO_STATE_PAUSE_ON,0); +} - case OP_BAD_PROTOCOL_VERSION: - Error(ERR_RETURN, "protocol version mismatch"); - Error(ERR_EXIT, "server expects %d.%d.x instead of %d.%d.%d", - buf[2], buf[3], PROT_VERS_1, PROT_VERS_2, PROT_VERS_3); - break; +static void Handle_OP_CONTINUE_PLAYING() +{ + printf("OP_CONTINUE_PLAYING: %d\n", buf[0]); + sprintf(msgbuf, "client %d continues game", buf[0]); + sysmsg(msgbuf); - case OP_START_PLAYING: - { - int new_level_nr, new_leveldir_nr; - unsigned long new_random_seed; - unsigned char *new_leveldir_name; + tape.pausing = FALSE; + DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0); +} - /* - if (game_status == PLAYING) - break; - */ - - new_level_nr = (buf[2] << 8) + buf[3]; - new_leveldir_nr = (buf[4] << 8) + buf[5]; - new_random_seed = - (buf[6] << 24) | (buf[7] << 16) | (buf[8] << 8) | (buf[9]); - new_leveldir_name = &buf[10]; - - printf("OP_START_PLAYING: %d\n", buf[0]); - sprintf(msgbuf, "client %d starts game [level %d from levedir %d (%s)]\n", - buf[0], - new_level_nr, - new_leveldir_nr, - new_leveldir_name); - sysmsg(msgbuf); +static void Handle_OP_STOP_PLAYING() +{ + printf("OP_STOP_PLAYING: %d\n", buf[0]); + sprintf(msgbuf, "client %d stops game", buf[0]); + sysmsg(msgbuf); +} +static void Handle_OP_MOVE_FIGURE() +{ + int frame_nr; + int i; - if (strcmp(leveldir[new_leveldir_nr].name, new_leveldir_name) != 0) - Error(ERR_RETURN, "no such level directory: '%s'",new_leveldir_name); + frame_nr = + (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | (buf[5]); - leveldir_nr = new_leveldir_nr; + if (frame_nr != FrameCounter) + { + Error(ERR_RETURN, "client and servers frame counters out of sync"); + Error(ERR_RETURN, "frame counter of client is %d", FrameCounter); + Error(ERR_RETURN, "frame counter of server is %d", frame_nr); + Error(ERR_EXIT, "this should not happen -- please debug"); + } + + for (i=0; ileveldir_nr = leveldir_nr; - LoadPlayerInfo(PLAYER_LEVEL); - SavePlayerInfo(PLAYER_SETUP); + network_player_action_received = TRUE; - level_nr = new_level_nr; + /* + sprintf(msgbuf, "frame %d: client %d moves player [0x%02x]", + FrameCounter, buf[0], buf[2]); + sysmsg(msgbuf); + */ +} - TapeErase(); - LoadLevelTape(level_nr); +static void handlemessages() +{ + unsigned int len; - GetPlayerConfig(); - LoadLevel(level_nr); + while (nread >= 4 && nread >= 4 + readbuf[3]) + { + len = readbuf[3]; + if (readbuf[0] || readbuf[1] || readbuf[2]) + fatal("Wrong server line length"); - /* - if (autorecord_on) - TapeStartRecording(); - */ + memcpy(buf, &readbuf[4], len); + nread -= 4 + len; + copydown(readbuf, readbuf + 4 + len, nread); - if (tape.recording) - tape.random_seed = new_random_seed; + switch(buf[1]) + { + case OP_BAD_PROTOCOL_VERSION: + Handle_OP_BAD_PROTOCOL_VERSION(); + break; - InitRND(new_random_seed); + case OP_YOUR_NUMBER: + Handle_OP_YOUR_NUMBER(); + break; + case OP_NUMBER_WANTED: + Handle_OP_NUMBER_WANTED(); + break; - /* - printf("tape.random_seed == %d\n", tape.random_seed); - */ + case OP_NICKNAME: + Handle_OP_NICKNAME(len); + break; - game_status = PLAYING; - InitGame(); + case OP_PLAYER_CONNECTED: + Handle_OP_PLAYER_CONNECTED(); + break; + + case OP_PLAYER_DISCONNECTED: + Handle_OP_PLAYER_DISCONNECTED(); + break; + case OP_START_PLAYING: + Handle_OP_START_PLAYING(); break; - } - case OP_MOVE_FIGURE: - { - int frame_nr; - int i; - - frame_nr = - (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | (buf[5]); - - if (frame_nr != FrameCounter) - { - Error(ERR_RETURN, "client and servers frame counters out of sync"); - Error(ERR_RETURN, "frame counter of client is %d", FrameCounter); - Error(ERR_RETURN, "frame counter of server is %d", frame_nr); - Error(ERR_EXIT, "this should not happen -- please debug"); - } - - for (i=0; i