From 57562a0f29074feea1dc01ddf1b1d5e4e6d59730 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Sat, 3 Oct 1998 15:08:05 +0200 Subject: [PATCH] rnd-19981003-2 --- src/events.c | 2 +- src/game.c | 9 ++- src/game.h | 2 +- src/init.c | 7 +- src/main.h | 5 +- src/misc.c | 31 +++++---- src/misc.h | 3 +- src/network.c | 181 +++++++++++++++++++++++++++++++++++++++++++++----- src/network.h | 6 +- src/screens.c | 16 +++-- src/screens.h | 2 +- src/sound.c | 8 +-- src/tape.c | 2 +- src/tape.h | 2 +- 14 files changed, 220 insertions(+), 56 deletions(-) diff --git a/src/events.c b/src/events.c index f3a720de..e77b7510 100644 --- a/src/events.c +++ b/src/events.c @@ -788,7 +788,7 @@ void HandleJoystick() if (tape.pausing || AllPlayersGone) joy = 0; - HandleGameActions(joy); + HandleGameActions((byte)joy); break; default: diff --git a/src/game.c b/src/game.c index 67db98a9..b96fa7ad 100644 --- a/src/game.c +++ b/src/game.c @@ -21,6 +21,7 @@ #include "files.h" #include "tape.h" #include "joystick.h" +#include "network.h" void GetPlayerConfig() { @@ -2764,9 +2765,9 @@ void CheckForDragon(int x, int y) } } -void PlayerActions(struct PlayerInfo *player, int player_action) +void PlayerActions(struct PlayerInfo *player, byte player_action) { - static int stored_player_action[MAX_PLAYERS]; + static byte stored_player_action[MAX_PLAYERS]; static int num_stored_actions = 0; BOOL moved = FALSE, snapped = FALSE, bombed = FALSE; int jx = player->jx, jy = player->jy; @@ -2849,7 +2850,7 @@ void PlayerActions(struct PlayerInfo *player, int player_action) } } -void GameActions(int player_action) +void GameActions(byte player_action) { static long action_delay = 0; long action_delay_value; @@ -2876,6 +2877,8 @@ void GameActions(int player_action) else recorded_player_action = NULL; + SendToServer_MovePlayer(player_action); + for(i=0; ialias_name); - SendProtocolVersionToServer(); + SendToServer_Nickname(local_player->alias_name); + SendToServer_ProtocolVersion(); } void InitSound() diff --git a/src/main.h b/src/main.h index 0d3bfe5e..dc5da4ff 100644 --- a/src/main.h +++ b/src/main.h @@ -40,6 +40,7 @@ #endif /* #ifndef MSDOS */ typedef int BOOL; +typedef unsigned char byte; #ifndef FALSE #define FALSE 0 @@ -288,8 +289,8 @@ struct RecordingInfo BOOL changed; struct { - unsigned char joystickdata[MAX_PLAYERS]; - unsigned char delay; + byte joystickdata[MAX_PLAYERS]; + byte delay; } pos[MAX_TAPELEN]; }; diff --git a/src/misc.c b/src/misc.c index 6445ee28..8d36c27a 100644 --- a/src/misc.c +++ b/src/misc.c @@ -208,7 +208,7 @@ void GetOptions(char *argv[]) break; if (option_len >= MAX_OPTION_LEN) - Error(ERR_EXITHELP, "unrecognized option '%s'", option); + Error(ERR_EXIT_HELP, "unrecognized option '%s'", option); if (strncmp(option, "--", 2) == 0) /* treat '--' like '-' */ option++; @@ -220,13 +220,13 @@ void GetOptions(char *argv[]) { *option_arg++ = '\0'; /* cut argument from option */ if (*option_arg == '\0') /* no argument after '=' */ - Error(ERR_EXITHELP, "option '%s' has invalid argument", option_str); + Error(ERR_EXIT_HELP, "option '%s' has invalid argument", option_str); } option_len = strlen(option); if (strcmp(option, "-") == 0) - Error(ERR_EXITHELP, "unrecognized option '%s'", option); + Error(ERR_EXIT_HELP, "unrecognized option '%s'", option); else if (strncmp(option, "-help", option_len) == 0) { printf("Usage: %s [options] [server.name [port]]\n" @@ -240,7 +240,7 @@ void GetOptions(char *argv[]) else if (strncmp(option, "-display", option_len) == 0) { if (option_arg == NULL) - Error(ERR_EXITHELP, "option '%s' requires an argument", option_str); + Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str); display_name = option_arg; if (option_arg == next_option) @@ -251,7 +251,7 @@ void GetOptions(char *argv[]) else if (strncmp(option, "-levels", option_len) == 0) { if (option_arg == NULL) - Error(ERR_EXITHELP, "option '%s' requires an argument", option_str); + Error(ERR_EXIT_HELP, "option '%s' requires an argument", option_str); level_directory = option_arg; if (option_arg == next_option) @@ -266,7 +266,7 @@ void GetOptions(char *argv[]) verbose = TRUE; } else if (*option == '-') - Error(ERR_EXITHELP, "unrecognized option '%s'", option_str); + Error(ERR_EXIT_HELP, "unrecognized option '%s'", option_str); else if (server_host == NULL) { server_host = *options_left; @@ -277,12 +277,12 @@ void GetOptions(char *argv[]) { server_port = atoi(*options_left); if (server_port < 1024) - Error(ERR_EXITHELP, "bad port number '%d'", server_port); + Error(ERR_EXIT_HELP, "bad port number '%d'", server_port); printf("port == %d\n", server_port); } else - Error(ERR_EXITHELP, "too many arguments"); + Error(ERR_EXIT_HELP, "too many arguments"); options_left++; } @@ -291,6 +291,10 @@ void GetOptions(char *argv[]) void Error(int mode, char *format_str, ...) { FILE *output_stream = stderr; + char *process_name = ""; + + if (mode == ERR_EXIT_SOUNDSERVER) + process_name = " sound server"; if (format_str) { @@ -300,7 +304,7 @@ void Error(int mode, char *format_str, ...) int i_value; double d_value; - fprintf(output_stream, "%s: ", program_name); + fprintf(output_stream, "%s%s: ", program_name, process_name); va_start(ap, format_str); /* ap points to first unnamed argument */ @@ -330,7 +334,8 @@ void Error(int mode, char *format_str, ...) break; default: - fprintf(stderr, "\nError(): invalid format string: %s\n",format_str); + fprintf(stderr, "\n%s: Error(): invalid format string: %s\n", + program_name, format_str); CloseAllAndExit(10); } } @@ -340,13 +345,13 @@ void Error(int mode, char *format_str, ...) fprintf(output_stream, "\n"); } - if (mode == ERR_EXITHELP) + if (mode == ERR_EXIT_HELP) fprintf(output_stream, "%s: Try option '--help' for more information.\n", program_name); - if (mode == ERR_EXIT || mode == ERR_EXITHELP) + if (mode != ERR_RETURN) { - fprintf(output_stream, "%s: aborting\n", program_name); + fprintf(output_stream, "%s%s: aborting\n", program_name, process_name); CloseAllAndExit(1); } } diff --git a/src/misc.h b/src/misc.h index 8b483c52..260d7803 100644 --- a/src/misc.h +++ b/src/misc.h @@ -23,7 +23,8 @@ #define ERR_RETURN 0 #define ERR_EXIT 1 -#define ERR_EXITHELP 2 +#define ERR_EXIT_HELP 2 +#define ERR_EXIT_SOUNDSERVER 3 void InitCounter(void); unsigned long Counter(void); diff --git a/src/network.c b/src/network.c index 033bed0d..14ea8656 100644 --- a/src/network.c +++ b/src/network.c @@ -26,6 +26,10 @@ #include #include "network.h" +#include "game.h" +#include "tape.h" +#include "files.h" +#include "misc.h" int norestart = 0; int nospeedup = 0; @@ -38,7 +42,7 @@ int nospeedup = 0; #define OP_NICK 1 #define OP_PLAY 2 -#define OP_FALL 3 +#define OP_MOVE 3 #define OP_DRAW 4 #define OP_LOST 5 #define OP_GONE 6 @@ -60,6 +64,22 @@ int nospeedup = 0; #define OP_WON 22 #define OP_ZERO 23 +#define MAXNICKLEN 14 + +struct user +{ + byte nr; + char name[MAXNICKLEN+2]; + struct user *next; +}; + +struct user me = +{ + 0, + "no name", + NULL +}; + /* server stuff */ #define BUFLEN 4096 @@ -88,6 +108,16 @@ void fatal(char *s) exit(1); } +void *mmalloc(int n) +{ + void *r; + + r = malloc(n); + if (r == NULL) + fatal("Out of memory"); + return r; +} + void u_sleep(int i) { struct timeval tm; @@ -119,6 +149,34 @@ void sendbuf(int len) } } +struct user *finduser(unsigned char c) +{ + struct user *u; + + for (u = &me; u; u = u->next) + if (u->nr == c) + return u; + + fatal("Protocol error: reference to non-existing user"); + return NULL; /* so that gcc -Wall doesn't complain */ +} + +char *get_user_name(unsigned char c) +{ + struct user *u; + + if (c == 0) + return("the server"); + else if (c == me.nr) + return("you"); + else + for (u = &me; u; u = u->next) + if (u->nr == c && u->name && strlen(u->name)) + return(u->name); + + return("no name"); +} + void startserver() { char *options[2]; @@ -217,7 +275,7 @@ BOOL ConnectToServer(char *host, int port) return(TRUE); } -void SendNicknameToServer(char *nickname) +void SendToServer_Nickname(char *nickname) { static char msgbuf[300]; @@ -228,7 +286,7 @@ void SendNicknameToServer(char *nickname) sysmsg(msgbuf); } -void SendProtocolVersionToServer() +void SendToServer_ProtocolVersion() { buf[1] = OP_VERSION; buf[2] = PROT_VERS_1; @@ -237,9 +295,28 @@ void SendProtocolVersionToServer() sendbuf(5); } +void SendToServer_StartPlaying() +{ + buf[1] = OP_PLAY; + buf[2] = (unsigned char)(level_nr / 256); + buf[3] = (unsigned char)(level_nr % 256); + buf[4] = (unsigned char)(leveldir_nr / 256); + buf[5] = (unsigned char)(leveldir_nr % 256); + strcpy(&buf[6], leveldir[leveldir_nr].name); + sendbuf(strlen(leveldir[leveldir_nr].name)+1 + 6); +} + +void SendToServer_MovePlayer(byte player_action) +{ + buf[1] = OP_MOVE; + buf[2] = player_action; + sendbuf(3); +} + void handlemessages() { unsigned int len; + struct user *u, *v = NULL; static char msgbuf[300]; while (nread >= 4 && nread >= 4 + readbuf[3]) @@ -256,33 +333,107 @@ void handlemessages() { case OP_YOUARE: printf("OP_YOUARE: %d\n", buf[0]); + me.nr = buf[0]; + + TestPlayer = buf[0] - 1; + break; case OP_NEW: printf("OP_NEW: %d\n", buf[0]); sprintf(msgbuf, "new client %d connected", buf[0]); sysmsg(msgbuf); + + for (u = &me; u; u = u->next) + { + if (u->nr == buf[0]) + Error(ERR_EXIT, "multiplayer server sent duplicate player id"); + else + v = u; + } + + v->next = u = mmalloc(sizeof(struct user)); + u->nr = buf[0]; + u->name[0] = '\0'; + u->next = NULL; + + break; + + case OP_NICK: + printf("OP_NICK: %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]); - sprintf(msgbuf, "client %d disconnected", buf[0]); + u = finduser(buf[0]); + sprintf(msgbuf, "client %d (%s) disconnected", + buf[0], get_user_name(buf[0])); sysmsg(msgbuf); + + for (v = &me; v; v = v->next) + if (v->next == u) + v->next = u->next; + free(u); + break; case OP_BADVERS: - { - static char tmpbuf[128]; - - sprintf(tmpbuf, "Protocol version mismatch: server expects %d.%d.x instead of %d.%d.%d\n", buf[2], buf[3], PROT_VERS_1, PROT_VERS_2, PROT_VERS_3); - fatal(tmpbuf); - } + 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; - + case OP_PLAY: printf("OP_PLAY: %d\n", buf[0]); - sprintf(msgbuf, "client %d starts game", buf[0]); + sprintf(msgbuf, "client %d starts game [level %d from levedir %d (%s)]\n", + buf[0], + (buf[2] << 8) + buf[3], + (buf[4] << 8) + buf[5], + &buf[6]); sysmsg(msgbuf); + + if (strcmp(leveldir[(buf[4] << 8) + buf[5]].name, &buf[6]) == 0) + { + leveldir_nr = (buf[4] << 8) + buf[5]; + + local_player->leveldir_nr = leveldir_nr; + LoadPlayerInfo(PLAYER_LEVEL); + SavePlayerInfo(PLAYER_SETUP); + + level_nr = (buf[2] << 8) + buf[3]; + + TapeErase(); + LoadLevelTape(level_nr); + + GetPlayerConfig(); + LoadLevel(level_nr); + + { + if (autorecord_on) + TapeStartRecording(); + + game_status = PLAYING; + InitGame(); + } + } + else + { + Error(ERR_RETURN, "no such level directory: '%s'", &buf[6]); + } + break; + + case OP_MOVE: + if (buf[2]) + { + printf("OP_MOVE: %d\n", buf[0]); + sprintf(msgbuf, "client %d moves player [0x%02x]", buf[0], buf[2]); + sysmsg(msgbuf); + } break; case OP_PAUSE: @@ -309,12 +460,6 @@ void handlemessages() sysmsg(msgbuf); break; - case OP_NICK: - printf("OP_NICK: %d\n", buf[0]); - sprintf(msgbuf, "client %d calls itself \"%s\"", buf[0], &buf[2]); - sysmsg(msgbuf); - break; - case OP_MSG: printf("OP_MSG: %d\n", buf[0]); sprintf(msgbuf, "client %d sends message", buf[0]); diff --git a/src/network.h b/src/network.h index e8456b4c..5465cae1 100644 --- a/src/network.h +++ b/src/network.h @@ -17,8 +17,10 @@ #include "main.h" BOOL ConnectToServer(char *, int); -void SendNicknameToServer(char *); -void SendProtocolVersionToServer(void); +void SendToServer_Nickname(char *); +void SendToServer_ProtocolVersion(void); +void SendToServer_StartPlaying(void); +void SendToServer_MovePlayer(byte); void HandleNetworking(void); #endif diff --git a/src/screens.c b/src/screens.c index 86a1235d..baa76948 100644 --- a/src/screens.c +++ b/src/screens.c @@ -23,6 +23,7 @@ #include "tape.h" #include "joystick.h" #include "cartoons.h" +#include "network.h" #ifdef MSDOS extern unsigned char get_ascii(KeySym); @@ -214,11 +215,16 @@ void HandleMainMenu(int mx, int my, int dx, int dy, int button) } else if (y==8) { - if (autorecord_on) - TapeStartRecording(); + if (standalone) + { + if (autorecord_on) + TapeStartRecording(); - game_status = PLAYING; - InitGame(); + game_status = PLAYING; + InitGame(); + } + else + SendToServer_StartPlaying(); } else if (y==9) { @@ -1298,7 +1304,7 @@ void CalibrateJoystick() DrawSetupScreen(); } -void HandleGameActions(int player_action) +void HandleGameActions(byte player_action) { if (game_status != PLAYING) return; diff --git a/src/screens.h b/src/screens.h index bde71a06..4095a0a0 100644 --- a/src/screens.h +++ b/src/screens.h @@ -37,7 +37,7 @@ void HandleHallOfFame(int); void DrawSetupScreen(void); void HandleSetupScreen(int, int, int, int, int); void CalibrateJoystick(void); -void HandleGameActions(int); +void HandleGameActions(byte); void HandleVideoButtons(int, int, int); void HandleSoundButtons(int, int, int); void HandleGameButtons(int, int, int); diff --git a/src/sound.c b/src/sound.c index 4a323818..1858a8f3 100644 --- a/src/sound.c +++ b/src/sound.c @@ -73,7 +73,7 @@ void SoundServer() if (!FD_ISSET(sound_pipe[0], &sound_fdset)) continue; if (read(sound_pipe[0], &snd_ctrl, sizeof(snd_ctrl)) != sizeof(snd_ctrl)) - Error(ERR_EXIT, "broken pipe - no sounds"); + Error(ERR_EXIT_SOUNDSERVER, "broken pipe - no sounds"); #ifdef VOXWARE @@ -483,13 +483,13 @@ void HPUX_Audio_Control() audio_ctl = open("/dev/audioCtl", O_WRONLY | O_NDELAY); if (audio_ctl == -1) - Error(ERR_EXIT, "cannot open /dev/audioCtl - no sounds"); + Error(ERR_EXIT_SOUNDSERVER, "cannot open /dev/audioCtl - no sounds"); if (ioctl(audio_ctl, AUDIO_DESCRIBE, &ainfo) == -1) - Error(ERR_EXIT, "no audio info - no sounds"); + Error(ERR_EXIT_SOUNDSERVER, "no audio info - no sounds"); if (ioctl(audio_ctl, AUDIO_SET_DATA_FORMAT, AUDIO_FORMAT_ULAW) == -1) - Error(ERR_EXIT, "ulaw audio not available - no sounds"); + Error(ERR_EXIT_SOUNDSERVER, "ulaw audio not available - no sounds"); ioctl(audio_ctl, AUDIO_SET_CHANNELS, 1); ioctl(audio_ctl, AUDIO_SET_SAMPLE_RATE, 8000); diff --git a/src/tape.c b/src/tape.c index 53ce1d9e..6d6a8d5c 100644 --- a/src/tape.c +++ b/src/tape.c @@ -58,7 +58,7 @@ void TapeStopRecording() DrawVideoDisplay(VIDEO_STATE_REC_OFF,0); } -void TapeRecordAction(int joy[MAX_PLAYERS]) +void TapeRecordAction(byte joy[MAX_PLAYERS]) { int i; diff --git a/src/tape.h b/src/tape.h index aa1b5826..9ae74581 100644 --- a/src/tape.h +++ b/src/tape.h @@ -20,7 +20,7 @@ void TapeStartRecording(void); void TapeStopRecording(void); -void TapeRecordAction(int *); +void TapeRecordAction(byte *); void TapeRecordDelay(void); void TapeTogglePause(void); void TapeStartPlaying(void); -- 2.34.1