rnd-19981006-1
authorHolger Schemel <info@artsoft.org>
Mon, 5 Oct 1998 22:50:24 +0000 (00:50 +0200)
committerHolger Schemel <info@artsoft.org>
Sat, 30 Aug 2014 08:31:01 +0000 (10:31 +0200)
src/events.c
src/game.c
src/init.c
src/main.c
src/main.h
src/misc.c
src/network.c
src/network.h
src/screens.c
src/tools.c

index 71d03604c22eb6081e1632232d3ffb852a5c9e41..92bdeef6c4b2f5c04e576cc439a18e0f501d96f9 100644 (file)
@@ -62,6 +62,21 @@ void EventLoop(void)
          break;
       }
     }
+
+    HandleNoXEvent();
+
+    /* don't use all CPU time when idle; the main loop while playing
+       has its own synchronization and is CPU friendly, too */
+
+    if (game_status != PLAYING)
+    {
+      XSync(display, FALSE);
+      Delay(10);
+    }
+
+
+
+#if 0
     else                       /* got no event, but don't be lazy... */
     {
       HandleNoXEvent();
@@ -75,6 +90,9 @@ void EventLoop(void)
        Delay(10);
       }
     }
+#endif
+
+
 
     if (game_status == EXITGAME)
       return;
@@ -314,7 +332,13 @@ void HandleButton(int mx, int my, int button)
       HandleSetupScreen(mx,my,0,0,button);
       break;
     case PLAYING:
+
+      /* --> NoXEvent() will follow */
+
+      /*
       HandleGameActions(0);
+      */
+
       break;
     default:
       break;
@@ -730,11 +754,6 @@ void HandleJoystick()
 {
   int joystick = Joystick();
   int keyboard = key_joystick_mapping;
-
-  /*
-  int joy      = (tape.playing ? TapePlayAction() : (joystick | keyboard));
-  */
-
   int joy      = (joystick | keyboard);
   int left     = joy & JOY_LEFT;
   int right    = joy & JOY_RIGHT;
@@ -788,11 +807,6 @@ void HandleJoystick()
       if (tape.pausing || AllPlayersGone)
        joy = 0;
 
-      /*
-      if (!network_player_action_stored)
-       SendToServer_MovePlayer(joy, 0);
-      */
-
       HandleGameActions((byte)joy);
       break;
 
index c8519b390c7500c97d1d3b6507c04b63bd55e94d..d79e9e13bddcca4a7e933b301d5343f70d71bd72 100644 (file)
@@ -129,12 +129,12 @@ void InitGame()
   local_player->active = TRUE;
   local_player->local = TRUE;
 
-  network_player_action_stored = FALSE;
+  network_player_action_received = FALSE;
 
 
 
   /* initial null action */
-  SendToServer_MovePlayer(0,0);
+  SendToServer_MovePlayer(MV_NO_MOVING);
 
 
 
@@ -2874,11 +2874,9 @@ void GameActions(byte player_action)
   int i, x,y, element;
   int *recorded_player_action;
 
-
   if (game_status != PLAYING)
     return;
 
-
 #ifdef DEBUG
   action_delay_value =
     (tape.playing && tape.fast_forward ? FFWD_FRAME_DELAY : GameFrameDelay);
@@ -2887,21 +2885,16 @@ void GameActions(byte player_action)
     (tape.playing && tape.fast_forward ? FFWD_FRAME_DELAY : GAME_FRAME_DELAY);
 #endif
 
-  /*
-  HandleNetworking();
-
-  if (game_status != PLAYING)
-    return;
-  */
-
   /* main game synchronization point */
   WaitUntilDelayReached(&action_delay, action_delay_value);
 
-  if (!network_player_action_stored)
+  if (!standalone && !network_player_action_received)
   {
+    /*
 #ifdef DEBUG
     printf("DEBUG: try to get network player actions in time\n");
 #endif
+    */
 
     /* last chance to get network player actions without main loop delay */
     HandleNetworking();
@@ -2909,11 +2902,13 @@ void GameActions(byte player_action)
     if (game_status != PLAYING)
       return;
 
-    if (!network_player_action_stored)
+    if (!network_player_action_received)
     {
+      /*
 #ifdef DEBUG
       printf("DEBUG: failed to get network player actions in time\n");
 #endif
+      */
       return;
     }
   }
@@ -2923,13 +2918,17 @@ void GameActions(byte player_action)
   else
     recorded_player_action = NULL;
 
-
-  SendToServer_MovePlayer(player_action, FrameCounter);
-
+  if (!standalone)
+    SendToServer_MovePlayer(player_action);
 
   for(i=0; i<MAX_PLAYERS; i++)
   {
+    int actual_player_action =
+      (standalone ? player_action : network_player_action[i]);
+
+    /*
     int actual_player_action = network_player_action[i];
+    */
 
     /*
     int actual_player_action = player_action;
@@ -2942,6 +2941,9 @@ void GameActions(byte player_action)
       actual_player_action = 0;
     */
 
+    if (standalone && i != TestPlayer)
+      actual_player_action = 0;
+
     /* TEST TEST TEST */
 
     if (recorded_player_action)
@@ -2953,7 +2955,7 @@ void GameActions(byte player_action)
     network_player_action[i] = 0;
   }
 
-  network_player_action_stored = FALSE;
+  network_player_action_received = FALSE;
 
   ScrollScreen(NULL, SCROLL_GO_ON);
 
@@ -2965,11 +2967,12 @@ void GameActions(byte player_action)
   FrameCounter++;
   TimeFrames++;
 
+
   /*
-  printf("advancing FrameCounter to %d\n",
-        FrameCounter);
+  printf("FrameCounter == %d, RND(100) == %d\n", FrameCounter, RND(100));
   */
 
+
   for(y=0;y<lev_fieldy;y++) for(x=0;x<lev_fieldx;x++)
   {
     Stop[x][y] = FALSE;
index 0a0c7580c460bd184622b944a91fb6e14d09b052..76ff5941d1545618b35b4f9d48c1d37d9d5be89d 100644 (file)
@@ -96,8 +96,6 @@ void InitNetworkServer()
 {
   int nr_wanted;
 
-  standalone = FALSE;
-
   if (standalone)
     return;
 
index 61039723459de7e82fd6f203e5a0a1c49c03957e..3962a9bd7c8959c65cab4df294b13eb73d213e93 100644 (file)
@@ -106,7 +106,7 @@ int         FrameCounter, TimeFrames, TimeLeft;
 int            MampferNr, SiebAktiv;
 
 byte           network_player_action[MAX_PLAYERS];
-BOOL           network_player_action_stored = FALSE;
+BOOL           network_player_action_received = FALSE;
 int            TestPlayer = 0;
 
 struct LevelDirInfo    leveldir[MAX_LEVDIR_ENTRIES];
index 354b23eca1c7bf2938e42c844bda537ad5a8a5bc..af0d9a9cf5b71ce01051d0c1868965eb0f055e77 100644 (file)
@@ -273,7 +273,7 @@ struct LevelDirInfo
 struct RecordingInfo
 {
   int level_nr;
-  unsigned int random_seed;
+  unsigned long random_seed;
   unsigned long date;
   unsigned long counter;
   unsigned long length;
@@ -377,7 +377,7 @@ extern int          FrameCounter, TimeFrames, TimeLeft;
 extern int             MampferNr, SiebAktiv;
 
 extern byte            network_player_action[];
-extern BOOL            network_player_action_stored;
+extern BOOL            network_player_action_received;
 extern int             TestPlayer;
 
 extern struct LevelDirInfo     leveldir[];
index e412be85952f6febbb39cba65c5311b400dfc850..fdbc27259763f84121e4d5894c74919e35eb7752 100644 (file)
@@ -260,6 +260,12 @@ void GetOptions(char *argv[])
 
       printf("--levels == '%s'\n", level_directory);
     }
+    else if (strncmp(option, "-network", option_len) == 0)
+    {
+      printf("--network\n");
+
+      standalone = FALSE;
+    }
     else if (strncmp(option, "-verbose", option_len) == 0)
     {
       printf("--verbose\n");
index 17ca60d1c637b2540a276d69b35a09d602a83242..81de0d5c011817b7cec175a1e4bd246a4d57d6ba 100644 (file)
@@ -150,6 +150,11 @@ void sendbuf(int len)
       fatal("Internal error: send buffer overflow");
     memcpy(writbuf + nwrite, realbuf, 4 + len);
     nwrite += 4 + len;
+
+
+    flushbuf();
+
+
   }
 }
 
@@ -296,6 +301,7 @@ void SendToServer_ProtocolVersion()
   buf[2] = PROT_VERS_1;
   buf[3] = PROT_VERS_2;
   buf[4] = PROT_VERS_3;
+
   sendbuf(5);
 }
 
@@ -303,46 +309,36 @@ void SendToServer_NrWanted(int nr_wanted)
 {
   buf[1] = OP_NRWANTED;
   buf[2] = nr_wanted;
+
   sendbuf(3);
 }
 
 void SendToServer_StartPlaying()
 {
+  unsigned long new_random_seed = InitRND(NEW_RANDOMIZE);
+
   buf[1] = OP_PLAY;
   buf[2] = (byte)(level_nr >> 8);
   buf[3] = (byte)(level_nr & 0xff);
   buf[4] = (byte)(leveldir_nr >> 8);
   buf[5] = (byte)(leveldir_nr & 0xff);
-  strcpy(&buf[6], leveldir[leveldir_nr].name);
-  sendbuf(strlen(leveldir[leveldir_nr].name)+1 + 6);
+
+  buf[6] = (unsigned char)((new_random_seed >> 24) & 0xff);
+  buf[7] = (unsigned char)((new_random_seed >> 16) & 0xff);
+  buf[8] = (unsigned char)((new_random_seed >>  8) & 0xff);
+  buf[9] = (unsigned char)((new_random_seed >>  0) & 0xff);
+
+  strcpy(&buf[10], leveldir[leveldir_nr].name);
+
+  sendbuf(10 + strlen(leveldir[leveldir_nr].name)+1);
 }
 
-void SendToServer_MovePlayer(byte player_action, unsigned long frame_nr)
+void SendToServer_MovePlayer(byte player_action)
 {
   buf[1] = OP_MOVE;
   buf[2] = player_action;
 
   sendbuf(3);
-
-  /*
-  buf[3] = (byte)((frame_nr >> 24) & 0xff);
-  buf[4] = (byte)((frame_nr >> 16) & 0xff);
-  buf[5] = (byte)((frame_nr >>  8) & 0xff);
-  buf[6] = (byte)((frame_nr >>  0) & 0xff);
-
-  sendbuf(7);
-  */
-
-  /*
-  printf("%d: %x, %x, %x, %x\n", frame_nr, buf[3], buf[4], buf[5], buf[6]);
-  */
-
-
-
-  flushbuf();
-
-
-
 }
 
 void handlemessages()
@@ -474,43 +470,59 @@ void handlemessages()
        break;
 
       case OP_PLAY:
+      {
+       int new_level_nr, new_leveldir_nr;
+       unsigned long new_random_seed;
+       unsigned char *new_leveldir_name;
+
+       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_PLAY: %d\n", 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]);
+               new_level_nr,
+               new_leveldir_nr,
+               new_leveldir_name);
        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);
+       if (strcmp(leveldir[new_leveldir_nr].name, new_leveldir_name) != 0)
+         Error(ERR_RETURN, "no such level directory: '%s'",new_leveldir_name);
 
-         level_nr = (buf[2] << 8) + buf[3];
+       leveldir_nr = new_leveldir_nr;
 
-         TapeErase();
-         LoadLevelTape(level_nr);
+       local_player->leveldir_nr = leveldir_nr;
+       LoadPlayerInfo(PLAYER_LEVEL);
+       SavePlayerInfo(PLAYER_SETUP);
 
-         GetPlayerConfig();
-         LoadLevel(level_nr);
+       level_nr = new_level_nr;
 
-         {
-           if (autorecord_on)
-             TapeStartRecording();
+       TapeErase();
+       LoadLevelTape(level_nr);
+
+       GetPlayerConfig();
+       LoadLevel(level_nr);
+
+       if (autorecord_on)
+         TapeStartRecording();
+
+       tape.random_seed = new_random_seed;
+
+       InitRND(tape.random_seed);
+
+       /*
+       printf("tape.random_seed == %d\n", tape.random_seed);
+       */
+
+       game_status = PLAYING;
+       InitGame();
 
-           game_status = PLAYING;
-           InitGame();
-         }
-       }
-       else
-       {
-         Error(ERR_RETURN, "no such level directory: '%s'", &buf[6]);
-       }
        break;
+      }
 
       case OP_MOVE:
       {
@@ -520,27 +532,21 @@ void handlemessages()
        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<MAX_PLAYERS; i++)
        {
          if (stored_player[i].active)
            network_player_action[i] = buf[6 + i];
        }
 
-       network_player_action_stored = TRUE;
-
-       /*
-       printf("FrameCounter == %d, frame_nr = %d\n",
-              FrameCounter, frame_nr);
-       */
-
-       /*
-       if (buf[2])
-       */
-
-
-       /*
-       printf("OP_MOVE: %d\n", buf[0]);
-       */
+       network_player_action_received = TRUE;
 
        sprintf(msgbuf, "frame %d: client %d moves player [0x%02x]",
                FrameCounter, buf[0], buf[2]);
index 893f4c5a769ed9178ee79dd8fd1048feeae067f6..f6f9d638f87848c51639684f8d1732d003bf8425 100644 (file)
@@ -21,7 +21,7 @@ void SendToServer_Nickname(char *);
 void SendToServer_ProtocolVersion(void);
 void SendToServer_NrWanted(int);
 void SendToServer_StartPlaying(void);
-void SendToServer_MovePlayer(byte, unsigned long);
+void SendToServer_MovePlayer(byte);
 void HandleNetworking(void);
 
 #endif
index cc78f0d8c52714197f368159db3fd0d58bc52775..7ada948031b64f49761b25a0022ef27c0236454f 100644 (file)
@@ -685,7 +685,9 @@ void CheckCheat()
 {
   int old_handicap = local_player->handicap;
 
+#if 0
   if (!strcmp(local_player->alias_name,"Artsoft"))
+#endif
     local_player->handicap = leveldir[leveldir_nr].levels-1;
 
   if (local_player->handicap != old_handicap)
index f987d323a2f550c0b778f3eda9e4fd1ec268daa9..1327e4e58acc2a057f47c1e544e61cb41f72250b 100644 (file)
@@ -613,7 +613,7 @@ void DrawGraphicThruMask(int x, int y, int graphic)
 #if DEBUG
   if (!IN_SCR_FIELD(x,y))
   {
-    printf("DrawGraphicThruMask(): x = %d, y = %d\n",x,y);
+    printf("DrawGraphicThruMask(): x = %d,y = %d, graphic = %d\n",x,y,graphic);
     printf("DrawGraphicThruMask(): This should never happen!\n");
     return;
   }