rnd-19981026-3
[rocksndiamonds.git] / src / events.c
index 9a2cdd41b6f6cce864ff645838e8572564bd3d9d..f360abd68334142d390b151a8aac28f8eac1fff2 100644 (file)
@@ -1,13 +1,12 @@
 /***********************************************************
 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
 *----------------------------------------------------------*
-*  ©1995 Artsoft Development                               *
-*        Holger Schemel                                    *
-*        33659 Bielefeld-Senne                             *
-*        Telefon: (0521) 493245                            *
-*        eMail: aeglos@valinor.owl.de                      *
-*               aeglos@uni-paderborn.de                    *
-*               q99492@pbhrzx.uni-paderborn.de             *
+*  (c) 1995-98 Artsoft Entertainment                       *
+*              Holger Schemel                              *
+*              Oststrasse 11a                              *
+*              33604 Bielefeld                             *
+*              phone: ++49 +521 290471                     *
+*              email: aeglos@valinor.owl.de                *
 *----------------------------------------------------------*
 *  events.c                                                *
 ***********************************************************/
 #include "misc.h"
 #include "tape.h"
 #include "joystick.h"
+#include "network.h"
 
 void EventLoop(void)
 {
   while(1)
   {
-    if (XPending(display))     /* got an event */
+    if (XPending(display))     /* got event from X server */
     {
       XEvent event;
 
@@ -34,34 +34,40 @@ void EventLoop(void)
 
       switch(event.type)
       {
-       case Expose:
-         HandleExposeEvent((XExposeEvent *) &event);
-         break;
-       case UnmapNotify:
-         SleepWhileUnmapped();
-         break;
        case ButtonPress:
        case ButtonRelease:
          HandleButtonEvent((XButtonEvent *) &event);
          break;
+
        case MotionNotify:
          HandleMotionEvent((XMotionEvent *) &event);
          break;
+
        case KeyPress:
        case KeyRelease:
          HandleKeyEvent((XKeyEvent *) &event);
          break;
-       case FocusIn:
-       case FocusOut:
-         HandleFocusEvent((XFocusChangeEvent *) &event);
-         break;
-        case ClientMessage:
-         HandleClientMessageEvent((XClientMessageEvent *) &event);
-         break;
+
        default:
+         HandleOtherEvents(&event);
          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,12 +81,41 @@ void EventLoop(void)
        Delay(10);
       }
     }
+#endif
+
+
 
     if (game_status == EXITGAME)
       return;
   }
 }
 
+void HandleOtherEvents(XEvent *event)
+{
+  switch(event->type)
+  {
+    case Expose:
+      HandleExposeEvent((XExposeEvent *) event);
+      break;
+
+    case UnmapNotify:
+      SleepWhileUnmapped();
+      break;
+
+    case FocusIn:
+    case FocusOut:
+      HandleFocusEvent((XFocusChangeEvent *) event);
+      break;
+
+    case ClientMessage:
+      HandleClientMessageEvent((XClientMessageEvent *) event);
+      break;
+
+    default:
+      break;
+  }
+}
+
 void ClearEventQueue()
 {
   while(XPending(display))
@@ -91,26 +126,16 @@ void ClearEventQueue()
 
     switch(event.type)
     {
-      case Expose:
-        HandleExposeEvent((XExposeEvent *) &event);
-       break;
-      case UnmapNotify:
-       SleepWhileUnmapped();
-       break;
       case ButtonRelease:
        button_status = MB_RELEASED;
        break;
+
       case KeyRelease:
        key_joystick_mapping = 0;
        break;
-      case FocusIn:
-      case FocusOut:
-       HandleFocusEvent((XFocusChangeEvent *) &event);
-       break;
-      case ClientMessage:
-       HandleClientMessageEvent((XClientMessageEvent *) &event);
-       break;
+
       default:
+       HandleOtherEvents(&event);
        break;
     }
   }
@@ -118,7 +143,7 @@ void ClearEventQueue()
 
 void SleepWhileUnmapped()
 {
-  BOOL window_unmapped = TRUE;
+  boolean window_unmapped = TRUE;
 
   XAutoRepeatOn(display);
 
@@ -130,22 +155,27 @@ void SleepWhileUnmapped()
 
     switch(event.type)
     {
-      case Expose:
-        HandleExposeEvent((XExposeEvent *) &event);
-       break;
       case ButtonRelease:
        button_status = MB_RELEASED;
        break;
+
       case KeyRelease:
        key_joystick_mapping = 0;
        break;
+
       case MapNotify:
        window_unmapped = FALSE;
        break;
-      case ClientMessage:
-       HandleClientMessageEvent((XClientMessageEvent *) &event);
+
+      case UnmapNotify:
+       /* this is only to surely prevent the 'should not happen' case
+        * of recursively looping between 'SleepWhileUnmapped()' and
+        * 'HandleOtherEvents()' which usually calls this funtion.
+        */
        break;
+
       default:
+       HandleOtherEvents(&event);
        break;
     }
   }
@@ -159,7 +189,7 @@ void HandleExposeEvent(XExposeEvent *event)
   int x = event->x, y = event->y;
   int width = event->width, height = event->height;
 
-  if (direct_draw_on && game_status==PLAYING)
+  if (setup.direct_draw_on && game_status==PLAYING)
   {
     int xx,yy;
     int x1 = (x-SX)/TILEX, y1 = (y-SY)/TILEY;
@@ -176,12 +206,12 @@ void HandleExposeEvent(XExposeEvent *event)
     SetDrawtoField(DRAW_DIRECT);
   }
 
-  if (soft_scrolling_on && game_status == PLAYING)
+  if (setup.soft_scrolling_on && game_status == PLAYING)
   {
     int fx = FX, fy = FY;
 
-    fx += (local_player->MovDir & (MV_LEFT|MV_RIGHT) ? ScreenMovPos : 0);
-    fy += (local_player->MovDir & (MV_UP|MV_DOWN)    ? ScreenMovPos : 0);
+    fx += (ScreenMovDir & (MV_LEFT|MV_RIGHT) ? ScreenGfxPos : 0);
+    fy += (ScreenMovDir & (MV_UP|MV_DOWN)    ? ScreenGfxPos : 0);
 
     XCopyArea(display,fieldbuffer,backbuffer,gc,
              fx,fy, SXSIZE,SYSIZE,
@@ -245,7 +275,7 @@ void HandleClientMessageEvent(XClientMessageEvent *event)
 {
   if ((event->window == window) &&
       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
-    CloseAll();
+    CloseAllAndExit(0);
 }
 
 void HandleButton(int mx, int my, int button)
@@ -273,7 +303,7 @@ void HandleButton(int mx, int my, int button)
     int sx = (mx - SX) / TILEX;
     int sy = (my - SY) / TILEY;
 
-    if (IN_SCR_FIELD(sx,sy))
+    if (IN_VIS_FIELD(sx,sy))
     {
       int x = LEVELX(sx);
       int y = LEVELY(sy);
@@ -295,27 +325,45 @@ void HandleButton(int mx, int my, int button)
     case MAINMENU:
       HandleMainMenu(mx,my,0,0,button);
       break;
+
     case TYPENAME:
       HandleTypeName(0,XK_Return);
       break;
+
     case CHOOSELEVEL:
       HandleChooseLevel(mx,my,0,0,button);
       break;
+
     case HALLOFFAME:
       HandleHallOfFame(button);
       break;
+
     case LEVELED:
       LevelEd(mx,my,button);
       break;
+
     case HELPSCREEN:
       HandleHelpScreen(button);
       break;
+
     case SETUP:
       HandleSetupScreen(mx,my,0,0,button);
       break;
+
+    case SETUPINPUT:
+      HandleSetupInputScreen(mx,my,0,0,button);
+      break;
+
     case PLAYING:
+
+      /* --> NoXEvent() will follow */
+
+      /*
       HandleGameActions(0);
+      */
+
       break;
+
     default:
       break;
   }
@@ -324,6 +372,68 @@ void HandleButton(int mx, int my, int button)
 void HandleKey(KeySym key, int key_status)
 {
   int joy = 0;
+  static struct SetupKeyboardInfo custom_key;
+  static struct
+  {
+    KeySym *keysym_custom;
+    KeySym keysym_default;
+    byte action;
+  } key_info[] =
+  {
+    { &custom_key.left,  DEFAULT_KEY_LEFT,  JOY_LEFT     },
+    { &custom_key.right, DEFAULT_KEY_RIGHT, JOY_RIGHT    },
+    { &custom_key.up,    DEFAULT_KEY_UP,    JOY_UP       },
+    { &custom_key.down,  DEFAULT_KEY_DOWN,  JOY_DOWN     },
+    { &custom_key.snap,  DEFAULT_KEY_SNAP,  JOY_BUTTON_1 },
+    { &custom_key.bomb,  DEFAULT_KEY_BOMB,  JOY_BUTTON_2 }
+  };
+
+  if (game_status == PLAYING)
+  {
+    int pnr;
+
+    for (pnr=0; pnr<MAX_PLAYERS; pnr++)
+    {
+      int i;
+      byte key_action = 0;
+
+      if (setup.input[pnr].use_joystick)
+       continue;
+
+      custom_key = setup.input[pnr].key;
+
+      for (i=0; i<6; i++)
+       if (key == *key_info[i].keysym_custom)
+         key_action |= key_info[i].action;
+
+      if (key_status == KEY_PRESSED)
+      {
+       if (network_playing)
+         local_player->potential_action |= key_action;
+       else
+         stored_player[pnr].action |= key_action;
+      }
+      else
+      {
+       if (network_playing)
+         local_player->potential_action &= ~key_action;
+       else
+         stored_player[pnr].action &= ~key_action;
+      }
+    }
+  }
+  else
+  {
+    int i;
+
+    for (i=0; i<6; i++)
+      if (key == key_info[i].keysym_default)
+       joy |= key_info[i].action;
+  }
+
+
+#if 0
+
 
   /* Map cursor keys to joystick directions */
 
@@ -340,6 +450,7 @@ void HandleKey(KeySym key, int key_status)
     case XK_j:
       joy |= JOY_LEFT;
       break;
+
     case XK_Right:
 #ifdef XK_KP_Right
     case XK_KP_Right:
@@ -351,6 +462,7 @@ void HandleKey(KeySym key, int key_status)
     case XK_k:
       joy |= JOY_RIGHT;
       break;
+
     case XK_Up:
 #ifdef XK_KP_Up
     case XK_KP_Up:
@@ -362,6 +474,7 @@ void HandleKey(KeySym key, int key_status)
     case XK_i:
       joy |= JOY_UP;
       break;
+
     case XK_Down:
 #ifdef XK_KP_Down
     case XK_KP_Down:
@@ -373,54 +486,63 @@ void HandleKey(KeySym key, int key_status)
     case XK_m:
       joy |= JOY_DOWN;
       break;
+
 #ifdef XK_KP_Home
     case XK_KP_Home:           /* Diagonalrichtungen */
 #endif
     case XK_KP_7:
       joy |= JOY_UP | JOY_LEFT;
       break;
+
 #ifdef XK_KP_Page_Up
     case XK_KP_Page_Up:
 #endif
     case XK_KP_9:
       joy = JOY_UP | JOY_RIGHT;
       break;
+
 #ifdef XK_KP_End
     case XK_KP_End:
 #endif
     case XK_KP_1:
       joy |= JOY_DOWN | JOY_LEFT;
       break;
+
 #ifdef XK_KP_Page_Down
     case XK_KP_Page_Down:
 #endif
     case XK_KP_3:
       joy |= JOY_DOWN | JOY_RIGHT;
       break;
+
 #ifndef MSDOS
     case XK_S:                 /* Feld entfernen */
 #endif
     case XK_s:
       joy |= JOY_BUTTON_1 | JOY_LEFT;
       break;
+
 #ifndef MSDOS
     case XK_D:
 #endif
     case XK_d:
       joy |= JOY_BUTTON_1 | JOY_RIGHT;
       break;
+
 #ifndef MSDOS
     case XK_E:
 #endif
     case XK_e:
       joy |= JOY_BUTTON_1 | JOY_UP;
       break;
+
 #ifndef MSDOS
     case XK_X:
 #endif
     case XK_x:
       joy |= JOY_BUTTON_1 | JOY_DOWN;
       break;
+
     case XK_Shift_L:           /* Linker Feuerknopf */
 #ifndef MSDOS
     case XK_Control_L:
@@ -429,6 +551,7 @@ void HandleKey(KeySym key, int key_status)
 #endif
       joy |= JOY_BUTTON_1;
       break;
+
     case XK_Shift_R:           /* Rechter Feuerknopf */
 #ifndef MSDOS
     case XK_Control_R:
@@ -441,10 +564,15 @@ void HandleKey(KeySym key, int key_status)
     case XK_b:
       joy |= JOY_BUTTON_2;
       break;
+
     default:
       break;
   }
 
+
+#endif
+
+
   if (joy)
   {
     if (key_status == KEY_PRESSED)
@@ -485,10 +613,11 @@ void HandleKey(KeySym key, int key_status)
     case TYPENAME:
       HandleTypeName(0,key);
       break;
+
     case MAINMENU:
     case CHOOSELEVEL:
     case SETUP:
-    {
+    case SETUPINPUT:
       switch(key)
       {
        case XK_Return:
@@ -498,15 +627,19 @@ void HandleKey(KeySym key, int key_status)
             HandleChooseLevel(0,0,0,0,MB_MENU_CHOICE);
          else if (game_status==SETUP)
            HandleSetupScreen(0,0,0,0,MB_MENU_CHOICE);
+         else if (game_status==SETUPINPUT)
+           HandleSetupInputScreen(0,0,0,0,MB_MENU_CHOICE);
          break;
+
        default:
          break;
       }
       break;
-    }
+
     case HELPSCREEN:
       HandleHelpScreen(MB_RELEASED);
       break;
+
     case HALLOFFAME:
       switch(key)
       {
@@ -515,13 +648,16 @@ void HandleKey(KeySym key, int key_status)
          DrawMainMenu();
          BackToFront();
          break;
+
        default:
          break;
       }
       break;
+
     case LEVELED:
       LevelNameTyping(key);
       break;
+
     case PLAYING:
     {
       switch(key)
@@ -546,6 +682,8 @@ void HandleKey(KeySym key, int key_status)
                 GAME_FRAME_DELAY * 100 / GameFrameDelay, GameFrameDelay);
          break;
 
+
+#if 0
        case XK_a:
          if (ScrollStepSize == TILEX/8)
            ScrollStepSize = TILEX/4;
@@ -553,19 +691,23 @@ void HandleKey(KeySym key, int key_status)
            ScrollStepSize = TILEX/8;
          printf("ScrollStepSize == %d\n", ScrollStepSize);
          break;
+#endif
 
        case XK_f:
          ScrollStepSize = TILEX/8;
          printf("ScrollStepSize == %d (1/8)\n", ScrollStepSize);
          break;
+
        case XK_g:
          ScrollStepSize = TILEX/4;
          printf("ScrollStepSize == %d (1/4)\n", ScrollStepSize);
          break;
+
        case XK_h:
          ScrollStepSize = TILEX/2;
          printf("ScrollStepSize == %d (1/2)\n", ScrollStepSize);
          break;
+
        case XK_l:
          ScrollStepSize = TILEX;
          printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
@@ -578,6 +720,10 @@ void HandleKey(KeySym key, int key_status)
          local_player->dynamite = 1000;
          break;
 
+
+
+#if 0
+
        case XK_x:
 
          {
@@ -648,7 +794,7 @@ void HandleKey(KeySym key, int key_status)
          }
          */
 
-         printf("direct_draw_on == %d\n", direct_draw_on);
+         printf("direct_draw_on == %d\n", setup.direct_draw_on);
 
          break;
 
@@ -668,6 +814,21 @@ void HandleKey(KeySym key, int key_status)
          }
 
          break;
+
+       case XK_t:
+         {
+           char *color[] = { "yellow", "red", "green", "blue" };
+
+           do
+             TestPlayer = (TestPlayer + 1) % MAX_PLAYERS;
+           while(!stored_player[TestPlayer].active);
+
+           printf("TestPlayer = %d (%s player)\n",
+                  TestPlayer, color[TestPlayer]);
+         }
+
+         break;
+#endif
 #endif
 
        default:
@@ -688,6 +849,9 @@ void HandleNoXEvent()
     return;
   }
 
+  if (options.network)
+    HandleNetworking();
+
   switch(game_status)
   {
     case MAINMENU:
@@ -695,8 +859,10 @@ void HandleNoXEvent()
     case HALLOFFAME:
     case HELPSCREEN:
     case SETUP:
+    case SETUPINPUT:
       HandleJoystick();
       break;
+
     case PLAYING:
       HandleJoystick();
 
@@ -705,6 +871,7 @@ void HandleNoXEvent()
       */
 
       break;
+
     default:
       break;
   }
@@ -714,7 +881,7 @@ 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;
   int up       = joy & JOY_UP;
@@ -729,10 +896,11 @@ void HandleJoystick()
     case MAINMENU:
     case CHOOSELEVEL:
     case SETUP:
+    case SETUPINPUT:
     {
       static long joystickmove_delay = 0;
 
-      if (joystick && !button && !DelayReached(&joystickmove_delay,15))
+      if (joystick && !button && !DelayReached(&joystickmove_delay,150))
        newbutton = dx = dy = 0;
 
       if (game_status==MAINMENU)
@@ -741,6 +909,9 @@ void HandleJoystick()
         HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       else if (game_status==SETUP)
        HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
+      else if (game_status==SETUPINPUT)
+       HandleSetupInputScreen(0,0,dx,dy,
+                              newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       break;
     }
 
@@ -764,10 +935,7 @@ void HandleJoystick()
        return;
       }
 
-      if (tape.pausing || AllPlayersGone)
-       joy = 0;
-
-      HandleGameActions(joy);
+      HandleGameActions();
       break;
 
     default: