rnd-20060111-1-src
[rocksndiamonds.git] / src / game.c
index 1f4d8a01a378252d1d5e1b75f6d4edd756cc2668..052239346ffc65f545296b98070c120881393cf3 100644 (file)
 
 /* forward declaration for internal use */
 
+static void CreateField(int, int, int);
+
 static void SetPlayerWaiting(struct PlayerInfo *, boolean);
 static void AdvanceFrameAndPlayerCounters(int);
 
@@ -261,7 +263,7 @@ static void TestIfElementHitsCustomElement(int, int, int);
 static void TestIfElementSmashesCustomElement(int, int, int);
 #endif
 
-static void ChangeElement(int, int, int);
+static void HandleElementChange(int, int, int);
 
 static boolean CheckTriggeredElementChangeExt(int, int, int, int, int,int,int);
 #define CheckTriggeredElementChange(x, y, e, ev)                       \
@@ -302,19 +304,22 @@ static struct GadgetInfo *game_gadget[NUM_GAME_BUTTONS];
 /* ------------------------------------------------------------------------- */
 
 /* forward declaration for changer functions */
-static void InitBuggyBase(int x, int y);
-static void WarnBuggyBase(int x, int y);
+static void InitBuggyBase(int, int);
+static void WarnBuggyBase(int, int);
+
+static void InitTrap(int, int);
+static void ActivateTrap(int, int);
+static void ChangeActiveTrap(int, int);
 
-static void InitTrap(int x, int y);
-static void ActivateTrap(int x, int y);
-static void ChangeActiveTrap(int x, int y);
+static void InitRobotWheel(int, int);
+static void RunRobotWheel(int, int);
+static void StopRobotWheel(int, int);
 
-static void InitRobotWheel(int x, int y);
-static void RunRobotWheel(int x, int y);
-static void StopRobotWheel(int x, int y);
+static void InitTimegateWheel(int, int);
+static void RunTimegateWheel(int, int);
 
-static void InitTimegateWheel(int x, int y);
-static void RunTimegateWheel(int x, int y);
+static void InitMagicBallDelay(int, int);
+static void ActivateMagicBall(int, int);
 
 struct ChangingElementInfo
 {
@@ -481,6 +486,14 @@ static struct ChangingElementInfo change_delay_list[] =
     RunTimegateWheel,
     NULL
   },
+  {
+    EL_EMC_MAGIC_BALL_ACTIVE,
+    EL_EMC_MAGIC_BALL_ACTIVE,
+    0,
+    InitMagicBallDelay,
+    NULL,
+    ActivateMagicBall
+  },
 
   {
     EL_UNDEFINED,
@@ -935,6 +948,16 @@ static void InitField(int x, int y, boolean init_game)
        game.light_time_left = level.time_light * FRAMES_PER_SECOND;
       break;
 
+    case EL_EMC_MAGIC_BALL:
+      if (game.ball_state)
+       Feld[x][y] = EL_EMC_MAGIC_BALL_ACTIVE;
+      break;
+
+    case EL_EMC_MAGIC_BALL_SWITCH:
+      if (game.ball_state)
+       Feld[x][y] = EL_EMC_MAGIC_BALL_SWITCH_ACTIVE;
+      break;
+
     default:
 #if 1
       if (IS_CUSTOM_ELEMENT(element))
@@ -1796,6 +1819,9 @@ void InitGame()
   game.lenses_time_left = 0;
   game.magnify_time_left = 0;
 
+  game.ball_state = level.ball_state_initial;
+  game.ball_content_nr = 0;
+
   game.envelope_active = FALSE;
 
   for (i = 0; i < NUM_BELTS; i++)
@@ -6580,6 +6606,47 @@ static void RunTimegateWheel(int x, int y)
   PlayLevelSound(x, y, SND_TIMEGATE_SWITCH_ACTIVE);
 }
 
+static void InitMagicBallDelay(int x, int y)
+{
+#if 1
+  ChangeDelay[x][y] = (level.ball_time + 1) * 8 + 1;
+#else
+  ChangeDelay[x][y] = level.ball_time * FRAMES_PER_SECOND + 1;
+#endif
+}
+
+static void ActivateMagicBall(int bx, int by)
+{
+  int x, y;
+
+  if (level.ball_random)
+  {
+    int pos_border = RND(8);   /* select one of the eight border elements */
+    int pos_content = (pos_border > 3 ? pos_border + 1 : pos_border);
+    int xx = pos_content % 3;
+    int yy = pos_content / 3;
+
+    x = bx - 1 + xx;
+    y = by - 1 + yy;
+
+    if (IN_LEV_FIELD(x, y) && Feld[x][y] == EL_EMPTY)
+      CreateField(x, y, level.ball_content[game.ball_content_nr].e[xx][yy]);
+  }
+  else
+  {
+    for (y = by - 1; y <= by + 1; y++) for (x = bx - 1; x <= bx + 1; x++)
+    {
+      int xx = x - bx + 1;
+      int yy = y - by + 1;
+
+      if (IN_LEV_FIELD(x, y) && Feld[x][y] == EL_EMPTY)
+       CreateField(x, y, level.ball_content[game.ball_content_nr].e[xx][yy]);
+    }
+  }
+
+  game.ball_content_nr = (game.ball_content_nr + 1) % level.num_ball_contents;
+}
+
 void CheckExit(int x, int y)
 {
   if (local_player->gems_still_needed > 0 ||
@@ -7353,22 +7420,22 @@ static void ExecuteCustomElementAction(int x, int y, int element, int page)
   }
 }
 
-static void ChangeElementNowExt(struct ElementChangeInfo *change,
-                               int x, int y, int target_element)
+static void CreateFieldExt(int x, int y, int element, boolean is_change)
 {
   int previous_move_direction = MovDir[x][y];
 #if USE_NEW_CUSTOM_VALUE
   int last_ce_value = CustomValue[x][y];
 #endif
-  boolean add_player = (ELEM_IS_PLAYER(target_element) &&
+  boolean add_player = (ELEM_IS_PLAYER(element) &&
                        IS_WALKABLE(Feld[x][y]));
 
   /* check if element under player changes from accessible to unaccessible
      (needed for special case of dropping element which then changes) */
   if (IS_PLAYER(x, y) && !PLAYER_EXPLOSION_PROTECTED(x, y) &&
-      IS_ACCESSIBLE(Feld[x][y]) && !IS_ACCESSIBLE(target_element))
+      IS_ACCESSIBLE(Feld[x][y]) && !IS_ACCESSIBLE(element))
   {
     Bang(x, y);
+
     return;
   }
 
@@ -7379,7 +7446,7 @@ static void ChangeElementNowExt(struct ElementChangeInfo *change,
     else
       RemoveField(x, y);
 
-    Feld[x][y] = target_element;
+    Feld[x][y] = element;
 
     ResetGfxAnimation(x, y);
     ResetRandomAnimationValue(x, y);
@@ -7401,17 +7468,28 @@ static void ChangeElementNowExt(struct ElementChangeInfo *change,
   }
 
   /* "ChangeCount" not set yet to allow "entered by player" change one time */
-  if (ELEM_IS_PLAYER(target_element))
-    RelocatePlayer(x, y, target_element);
+  if (ELEM_IS_PLAYER(element))
+    RelocatePlayer(x, y, element);
 
-  ChangeCount[x][y]++;         /* count number of changes in the same frame */
+  if (is_change)
+    ChangeCount[x][y]++;       /* count number of changes in the same frame */
 
   TestIfBadThingTouchesPlayer(x, y);
   TestIfPlayerTouchesCustomElement(x, y);
   TestIfElementTouchesCustomElement(x, y);
 }
 
-static boolean ChangeElementNow(int x, int y, int element, int page)
+static void CreateField(int x, int y, int element)
+{
+  CreateFieldExt(x, y, element, FALSE);
+}
+
+static void CreateElementFromChange(int x, int y, int element)
+{
+  CreateFieldExt(x, y, element, TRUE);
+}
+
+static boolean ChangeElement(int x, int y, int element, int page)
 {
   struct ElementChangeInfo *change = &element_info[element].change_page[page];
   int target_element;
@@ -7534,7 +7612,7 @@ static boolean ChangeElementNow(int x, int y, int element, int page)
          content_element = change->target_content.e[xx][yy];
          target_element = GET_TARGET_ELEMENT(content_element, change);
 
-         ChangeElementNowExt(change, ex, ey, target_element);
+         CreateElementFromChange(ex, ey, target_element);
 
          something_has_changed = TRUE;
 
@@ -7555,7 +7633,7 @@ static boolean ChangeElementNow(int x, int y, int element, int page)
   {
     target_element = GET_TARGET_ELEMENT(change->target_element, change);
 
-    ChangeElementNowExt(change, x, y, target_element);
+    CreateElementFromChange(x, y, target_element);
 
     PlayLevelSoundElementAction(x, y, element, ACTION_CHANGING);
     PlayLevelSoundElementAction(x, y, element, ACTION_PAGE_1 + page);
@@ -7569,7 +7647,7 @@ static boolean ChangeElementNow(int x, int y, int element, int page)
 
 #if USE_NEW_DELAYED_ACTION
 
-static void ChangeElement(int x, int y, int page)
+static void HandleElementChange(int x, int y, int page)
 {
   int element = MovingOrBlocked2Element(x, y);
   struct ElementInfo *ei = &element_info[element];
@@ -7580,9 +7658,9 @@ static void ChangeElement(int x, int y, int page)
       !CAN_CHANGE_OR_HAS_ACTION(Back[x][y]))
   {
     printf("\n\n");
-    printf("ChangeElement(): %d,%d: element = %d ('%s')\n",
+    printf("HandleElementChange(): %d,%d: element = %d ('%s')\n",
           x, y, element, element_info[element].token_name);
-    printf("ChangeElement(): This should never happen!\n");
+    printf("HandleElementChange(): This should never happen!\n");
     printf("\n\n");
   }
 #endif
@@ -7647,7 +7725,7 @@ static void ChangeElement(int x, int y, int page)
 
     if (change->can_change)
     {
-      if (ChangeElementNow(x, y, element, page))
+      if (ChangeElement(x, y, element, page))
       {
        if (change->post_change_function)
          change->post_change_function(x, y);
@@ -7661,7 +7739,7 @@ static void ChangeElement(int x, int y, int page)
 
 #else
 
-static void ChangeElement(int x, int y, int page)
+static void HandleElementChange(int x, int y, int page)
 {
   int element = MovingOrBlocked2Element(x, y);
   struct ElementInfo *ei = &element_info[element];
@@ -7671,9 +7749,9 @@ static void ChangeElement(int x, int y, int page)
   if (!CAN_CHANGE(element) && !CAN_CHANGE(Back[x][y]))
   {
     printf("\n\n");
-    printf("ChangeElement(): %d,%d: element = %d ('%s')\n",
+    printf("HandleElementChange(): %d,%d: element = %d ('%s')\n",
           x, y, element, element_info[element].token_name);
-    printf("ChangeElement(): This should never happen!\n");
+    printf("HandleElementChange(): This should never happen!\n");
     printf("\n\n");
   }
 #endif
@@ -7730,7 +7808,7 @@ static void ChangeElement(int x, int y, int page)
       return;
     }
 
-    if (ChangeElementNow(x, y, element, page))
+    if (ChangeElement(x, y, element, page))
     {
       if (change->post_change_function)
        change->post_change_function(x, y);
@@ -7796,7 +7874,8 @@ static boolean CheckTriggeredElementChangeExt(int trigger_x, int trigger_y,
              {
                ChangeDelay[x][y] = 1;
                ChangeEvent[x][y] = trigger_event;
-               ChangeElement(x, y, p);
+
+               HandleElementChange(x, y, p);
              }
 #if USE_NEW_DELAYED_ACTION
              else if (change->has_action)
@@ -7898,7 +7977,8 @@ static boolean CheckElementChangeExt(int x, int y,
       {
        ChangeDelay[x][y] = 1;
        ChangeEvent[x][y] = trigger_event;
-       ChangeElement(x, y, p);
+
+       HandleElementChange(x, y, p);
 
        change_done = TRUE;
       }
@@ -8482,7 +8562,7 @@ void GameActions()
     {
       int page = element_info[element].event_page_nr[CE_DELAY];
 #if 0
-      ChangeElement(x, y, ChangePage[x][y] != -1 ? ChangePage[x][y] : page);
+      HandleElementChange(x, y, ChangePage[x][y] != -1 ? ChangePage[x][y] : page);
 #else
 
 #if 0
@@ -8495,10 +8575,10 @@ void GameActions()
 #endif
 
 #if 1
-      ChangeElement(x, y, page);
+      HandleElementChange(x, y, page);
 #else
       if (CAN_CHANGE(element))
-       ChangeElement(x, y, page);
+       HandleElementChange(x, y, page);
 
       if (HAS_ACTION(element))
        ExecuteCustomElementAction(x, y, element, page);
@@ -10642,6 +10722,37 @@ int DigField(struct PlayerInfo *player,
       ResetGfxAnimation(x, y);
       DrawLevelField(x, y);
     }
+    else if (element == EL_EMC_MAGIC_BALL_SWITCH ||
+            element == EL_EMC_MAGIC_BALL_SWITCH_ACTIVE)
+    {
+      int xx, yy;
+
+      game.ball_state = !game.ball_state;
+
+#if 1
+      SCAN_PLAYFIELD(xx, yy)
+#else
+      for (yy = 0; yy < lev_fieldy; yy++) for (xx = 0; xx < lev_fieldx; xx++)
+#endif
+      {
+       int e = Feld[xx][yy];
+
+       if (game.ball_state)
+       {
+         if (e == EL_EMC_MAGIC_BALL)
+           CreateField(xx, yy, EL_EMC_MAGIC_BALL_ACTIVE);
+         else if (e == EL_EMC_MAGIC_BALL_SWITCH)
+           CreateField(xx, yy, EL_EMC_MAGIC_BALL_SWITCH_ACTIVE);
+       }
+       else
+       {
+         if (e == EL_EMC_MAGIC_BALL_ACTIVE)
+           CreateField(xx, yy, EL_EMC_MAGIC_BALL);
+         else if (e == EL_EMC_MAGIC_BALL_SWITCH_ACTIVE)
+           CreateField(xx, yy, EL_EMC_MAGIC_BALL_SWITCH);
+       }
+      }
+    }
 
     CheckTriggeredElementChangeByPlayer(x, y, element, CE_SWITCH_OF_X,
                                        player->index_bit, dig_side);