rnd-20031208-1-src
[rocksndiamonds.git] / src / events.c
index 2ba138ffd67661107ee06d30444a21ceb36f20b3..19c47763e05236c491659fbd15d9b772ecf204f1 100644 (file)
 #include "tape.h"
 #include "network.h"
 
-/* values for key_status */
-#define KEY_NOT_PRESSED                FALSE
-#define KEY_RELEASED           FALSE
-#define KEY_PRESSED            TRUE
-
 
 static boolean cursor_inside_playfield = FALSE;
 static boolean playfield_cursor_set = FALSE;
@@ -84,7 +79,7 @@ static boolean NextValidEvent(Event *event)
 
 void EventLoop(void)
 {
-  while(1)
+  while (1)
   {
     if (PendingEvent())                /* got event */
     {
@@ -224,7 +219,7 @@ void ClearPlayerAction()
 
   /* simulate key release events for still pressed keys */
   key_joystick_mapping = 0;
-  for (i=0; i<MAX_PLAYERS; i++)
+  for (i = 0; i < MAX_PLAYERS; i++)
     stored_player[i].action = 0;
 }
 
@@ -234,7 +229,7 @@ void SleepWhileUnmapped()
 
   KeyboardAutoRepeatOn();
 
-  while(window_unmapped)
+  while (window_unmapped)
   {
     Event event;
 
@@ -311,7 +306,9 @@ void HandleKeyEvent(KeyEvent *event)
   int key_status = (event->type==EVENT_KEYPRESS ? KEY_PRESSED : KEY_RELEASED);
   boolean with_modifiers = (game_status == GAME_MODE_PLAYING ? FALSE : TRUE);
   Key key = GetEventKey(event, with_modifiers);
+  Key keymod = (with_modifiers ? GetEventKey(event, FALSE) : key);
 
+  HandleKeyModState(keymod, key_status);
   HandleKey(key, key_status);
 }
 
@@ -377,7 +374,11 @@ void HandleButton(int mx, int my, int button)
     old_my = my;
   }
 
-  HandleGadgets(mx, my, button);
+  if (HandleGadgets(mx, my, button))
+  {
+    /* do not handle this button event anymore */
+    mx = my = -32;     /* force mouse event to be outside screen tiles */
+  }
 
   switch(game_status)
   {
@@ -401,7 +402,7 @@ void HandleButton(int mx, int my, int button)
       break;
 
     case GAME_MODE_INFO:
-      HandleHelpScreen(button);
+      HandleInfoScreen(mx,my, 0,0, button);
       break;
 
     case GAME_MODE_SETUP:
@@ -412,11 +413,10 @@ void HandleButton(int mx, int my, int button)
 #ifdef DEBUG
       if (button == MB_RELEASED)
       {
-       int sx = (mx - SX) / TILEX;
-       int sy = (my - SY) / TILEY;
-
-       if (IN_VIS_FIELD(sx,sy))
+       if (IN_GFX_SCREEN(mx, my))
        {
+         int sx = (mx - SX) / TILEX;
+         int sy = (my - SY) / TILEY;
          int x = LEVELX(sx);
          int y = LEVELY(sy);
 
@@ -425,14 +425,19 @@ void HandleButton(int mx, int my, int button)
          if (!IN_LEV_FIELD(x, y))
            break;
 
-         printf("      Feld[%d][%d] == %d\n", x,y, Feld[x][y]);
+         printf("      Feld[%d][%d] == %d ('%s')\n", x,y, Feld[x][y],
+                element_info[Feld[x][y]].token_name);
+         printf("      Back[%d][%d] == %d\n", x,y, Back[x][y]);
          printf("      Store[%d][%d] == %d\n", x,y, Store[x][y]);
          printf("      Store2[%d][%d] == %d\n", x,y, Store2[x][y]);
          printf("      StorePlayer[%d][%d] == %d\n", x,y, StorePlayer[x][y]);
          printf("      MovPos[%d][%d] == %d\n", x,y, MovPos[x][y]);
          printf("      MovDir[%d][%d] == %d\n", x,y, MovDir[x][y]);
          printf("      MovDelay[%d][%d] == %d\n", x,y, MovDelay[x][y]);
+         printf("      ChangeDelay[%d][%d] == %d\n", x,y, ChangeDelay[x][y]);
          printf("      GfxElement[%d][%d] == %d\n", x,y, GfxElement[x][y]);
+         printf("      GfxAction[%d][%d] == %d\n", x,y, GfxAction[x][y]);
+         printf("      GfxFrame[%d][%d] == %d\n", x,y, GfxFrame[x][y]);
          printf("\n");
        }
       }
@@ -444,6 +449,63 @@ void HandleButton(int mx, int my, int button)
   }
 }
 
+static boolean is_string_suffix(char *string, char *suffix)
+{
+  int string_len = strlen(string);
+  int suffix_len = strlen(suffix);
+
+  if (suffix_len > string_len)
+    return FALSE;
+
+  return (strcmp(&string[string_len - suffix_len], suffix) == 0);
+}
+
+#define MAX_CHEAT_INPUT_LEN    32
+
+static void HandleKeysCheating(Key key)
+{
+  static char cheat_input[2 * MAX_CHEAT_INPUT_LEN + 1] = "";
+  char letter = getCharFromKey(key);
+  int cheat_input_len = strlen(cheat_input);
+  int i;
+
+  if (letter == 0)
+    return;
+
+  if (cheat_input_len >= 2 * MAX_CHEAT_INPUT_LEN)
+  {
+    for (i = 0; i < MAX_CHEAT_INPUT_LEN + 1; i++)
+      cheat_input[i] = cheat_input[MAX_CHEAT_INPUT_LEN + i];
+
+    cheat_input_len = MAX_CHEAT_INPUT_LEN;
+  }
+
+  cheat_input[cheat_input_len++] = letter;
+  cheat_input[cheat_input_len] = '\0';
+
+#if 0
+  printf("::: '%s' [%d]\n", cheat_input, cheat_input_len);
+#endif
+
+#if 1
+  if (is_string_suffix(cheat_input, ":insert solution tape"))
+    InsertSolutionTape();
+#else
+  if (is_string_suffix(cheat_input, ":ist"))
+    InsertSolutionTape();
+#endif
+
+#ifdef DEBUG
+  else if (is_string_suffix(cheat_input, ":dump tape"))
+    DumpTape(&tape);
+  else if (is_string_suffix(cheat_input, ".q"))
+    for (i = 0; i < MAX_INVENTORY_SIZE; i++)
+      if (local_player->inventory_size < MAX_INVENTORY_SIZE)
+       local_player->inventory_element[local_player->inventory_size++] =
+         EL_DYNAMITE;
+#endif
+}
+
 void HandleKey(Key key, int key_status)
 {
   int joy = 0;
@@ -471,7 +533,7 @@ void HandleKey(Key key, int key_status)
     static boolean bomb_placed[MAX_PLAYERS] = { FALSE,FALSE,FALSE,FALSE };
     int pnr;
 
-    for (pnr=0; pnr<MAX_PLAYERS; pnr++)
+    for (pnr = 0; pnr < MAX_PLAYERS; pnr++)
     {
       int i;
       byte key_action = 0;
@@ -481,7 +543,7 @@ void HandleKey(Key key, int key_status)
 
       custom_key = setup.input[pnr].key;
 
-      for (i=0; i<6; i++)
+      for (i = 0; i < 6; i++)
        if (key == *key_info[i].key_custom)
          key_action |= key_info[i].action;
 
@@ -531,7 +593,7 @@ void HandleKey(Key key, int key_status)
   {
     int i;
 
-    for (i=0; i<6; i++)
+    for (i = 0; i < 6; i++)
       if (key == key_info[i].key_default)
        joy |= key_info[i].action;
   }
@@ -561,19 +623,6 @@ void HandleKey(Key key, int key_status)
     return;
   }
 
-  /* allow quick escape to the main menu with the Escape key */
-  if (key == KSYM_Escape &&
-      game_status != GAME_MODE_MAIN &&
-      game_status != GAME_MODE_PLAYING &&
-      game_status != GAME_MODE_EDITOR &&
-      game_status != GAME_MODE_LEVELS &&
-      game_status != GAME_MODE_SETUP)
-  {
-    game_status = GAME_MODE_MAIN;
-    DrawMainMenu();
-    return;
-  }
-
   /* special key shortcuts */
   if (game_status == GAME_MODE_MAIN || game_status == GAME_MODE_PLAYING)
   {
@@ -583,19 +632,15 @@ void HandleKey(Key key, int key_status)
       TapeQuickLoad();
     else if (key == setup.shortcut.toggle_pause)
       TapeTogglePause(TAPE_TOGGLE_MANUAL);
-  }
-
-#if 0
-#ifndef DEBUG
-
-  if (game_status == GAME_MODE_PLAYING && (tape.playing || tape.pausing))
-    return;
-
-#endif
-#endif
 
+    HandleKeysCheating(key);
+  }
 
-  HandleGadgetsKeyInput(key);
+  if (HandleGadgetsKeyInput(key))
+  {
+    if (key != KSYM_Escape)    /* always allow ESC key to be handled */
+      key = KSYM_UNDEFINED;
+  }
 
   switch(game_status)
   {
@@ -606,6 +651,7 @@ void HandleKey(Key key, int key_status)
     case GAME_MODE_MAIN:
     case GAME_MODE_LEVELS:
     case GAME_MODE_SETUP:
+    case GAME_MODE_INFO:
       switch(key)
       {
        case KSYM_Return:
@@ -615,6 +661,8 @@ void HandleKey(Key key, int key_status)
             HandleChooseLevel(0,0, 0,0, MB_MENU_CHOICE);
          else if (game_status == GAME_MODE_SETUP)
            HandleSetupScreen(0,0, 0,0, MB_MENU_CHOICE);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0,0, MB_MENU_CHOICE);
          break;
 
        case KSYM_Escape:
@@ -622,52 +670,48 @@ void HandleKey(Key key, int key_status)
             HandleChooseLevel(0,0, 0,0, MB_MENU_LEAVE);
          else if (game_status == GAME_MODE_SETUP)
            HandleSetupScreen(0,0, 0,0, MB_MENU_LEAVE);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0,0, MB_MENU_LEAVE);
          break;
 
         case KSYM_Page_Up:
           if (game_status == GAME_MODE_LEVELS)
-            HandleChooseLevel(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
+            HandleChooseLevel(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
          else if (game_status == GAME_MODE_SETUP)
-           HandleSetupScreen(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
+           HandleSetupScreen(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
         case KSYM_Page_Down:
           if (game_status == GAME_MODE_LEVELS)
-            HandleChooseLevel(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
+            HandleChooseLevel(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
          else if (game_status == GAME_MODE_SETUP)
-           HandleSetupScreen(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
+           HandleSetupScreen(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
+         else if (game_status == GAME_MODE_INFO)
+           HandleInfoScreen(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
-#ifdef DEBUG
-        case KSYM_t:
-         DumpTape(&tape);
-         break;
-#endif
-
        default:
          break;
       }
       break;
 
-    case GAME_MODE_INFO:
-      HandleHelpScreen(MB_RELEASED);
-      break;
-
     case GAME_MODE_SCORES:
       switch(key)
       {
        case KSYM_Return:
+       case KSYM_Escape:
          game_status = GAME_MODE_MAIN;
          DrawMainMenu();
-         BackToFront();
          break;
 
         case KSYM_Page_Up:
-         HandleHallOfFame(0,0, 0,-SCR_FIELDY, MB_MENU_MARK);
+         HandleHallOfFame(0,0, 0, -1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
         case KSYM_Page_Down:
-         HandleHallOfFame(0,0, 0,SCR_FIELDY, MB_MENU_MARK);
+         HandleHallOfFame(0,0, 0, +1 * SCROLL_PAGE, MB_MENU_MARK);
          break;
 
        default:
@@ -725,7 +769,7 @@ void HandleKey(Key key, int key_status)
          }
          break;
 
-       case KSYM_s:
+       case KSYM_S:
          if (!global.fps_slowdown)
          {
            global.fps_slowdown = TRUE;
@@ -791,20 +835,13 @@ void HandleKey(Key key, int key_status)
          printf("ScrollStepSize == %d (1/1)\n", ScrollStepSize);
          break;
 
-       case KSYM_Q:
-       case KSYM_q:
-         local_player->dynamite = 1000;
-         break;
-
-
-
 #if 0
 
        case KSYM_z:
          {
            int i;
 
-           for(i=0; i<MAX_PLAYERS; i++)
+           for (i = 0; i < MAX_PLAYERS; i++)
            {
              printf("Player %d:\n", i);
              printf("  jx == %d, jy == %d\n",
@@ -824,8 +861,15 @@ void HandleKey(Key key, int key_status)
       }
       break;
     }
+
     default:
-      break;
+      if (key == KSYM_Escape)
+      {
+       game_status = GAME_MODE_MAIN;
+       DrawMainMenu();
+
+       return;
+      }
   }
 }
 
@@ -850,7 +894,7 @@ static int HandleJoystickForAllPlayers()
   int i;
   int result = 0;
 
-  for (i=0; i<MAX_PLAYERS; i++)
+  for (i = 0; i < MAX_PLAYERS; i++)
   {
     byte joy_action = 0;
 
@@ -890,6 +934,7 @@ void HandleJoystick()
     case GAME_MODE_MAIN:
     case GAME_MODE_LEVELS:
     case GAME_MODE_SETUP:
+    case GAME_MODE_INFO:
     {
       static unsigned long joystickmove_delay = 0;
 
@@ -903,6 +948,8 @@ void HandleJoystick()
         HandleChooseLevel(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       else if (game_status == GAME_MODE_SETUP)
        HandleSetupScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
+      else if (game_status == GAME_MODE_INFO)
+       HandleInfoScreen(0,0,dx,dy,newbutton ? MB_MENU_CHOICE : MB_MENU_MARK);
       break;
     }
 
@@ -910,10 +957,6 @@ void HandleJoystick()
       HandleHallOfFame(0,0, dx,dy, !newbutton);
       break;
 
-    case GAME_MODE_INFO:
-      HandleHelpScreen(!newbutton);
-      break;
-
     case GAME_MODE_EDITOR:
       HandleLevelEditorIdle();
       break;