rnd-20031128-1-src
[rocksndiamonds.git] / src / tools.c
index fbb3079302ec7e5a67aa8b7986827f3596112b00..c9d3e4baf4203d124152a7b8ae9eb2733c0e0efc 100644 (file)
@@ -529,6 +529,90 @@ void DrawLevelElementAnimationIfNeeded(int x, int y, int element)
     DrawLevelFieldCrumbledSand(x, y);
 }
 
+static int getPlayerAction(struct PlayerInfo *player, int move_dir)
+{
+  int action = (player->is_pushing    ? ACTION_PUSHING    :
+               player->is_digging    ? ACTION_DIGGING    :
+               player->is_collecting ? ACTION_COLLECTING :
+               player->is_moving     ? ACTION_MOVING     :
+               player->is_snapping   ? ACTION_SNAPPING   :
+               player->is_sleeping   ? ACTION_SLEEPING   :
+               player->is_bored      ? ACTION_BORING     :
+               player->is_waiting    ? ACTION_WAITING    : ACTION_DEFAULT);
+
+  if (player->is_sleeping)
+  {
+    if (player->num_special_action_sleeping > 0)
+    {
+      if (player->anim_delay_counter == 0 && player->post_delay_counter == 0)
+      {
+       int last_special_action = player->special_action_sleeping;
+       int num_special_action = player->num_special_action_sleeping;
+       int special_action =
+         (last_special_action == ACTION_DEFAULT ? ACTION_SLEEPING_1 :
+          last_special_action == ACTION_SLEEPING ? ACTION_SLEEPING :
+          last_special_action < ACTION_SLEEPING_1 + num_special_action - 1 ?
+          last_special_action + 1 : ACTION_SLEEPING);
+       int special_graphic =
+         el_act_dir2img(player->element_nr, special_action, move_dir);
+
+       player->anim_delay_counter =
+         graphic_info[special_graphic].anim_delay_fixed +
+         SimpleRND(graphic_info[special_graphic].anim_delay_random);
+       player->post_delay_counter =
+         graphic_info[special_graphic].post_delay_fixed +
+         SimpleRND(graphic_info[special_graphic].post_delay_random);
+
+       player->special_action_sleeping = special_action;
+      }
+
+      if (player->anim_delay_counter > 0)
+      {
+       action = player->special_action_sleeping;
+       player->anim_delay_counter--;
+      }
+      else if (player->post_delay_counter > 0)
+      {
+       player->post_delay_counter--;
+      }
+    }
+  }
+  else if (player->is_bored)
+  {
+    if (player->num_special_action_bored > 0)
+    {
+      if (player->anim_delay_counter == 0 && player->post_delay_counter == 0)
+      {
+       int special_action =
+         ACTION_BORING_1 + SimpleRND(player->num_special_action_bored);
+       int special_graphic =
+         el_act_dir2img(player->element_nr, special_action, move_dir);
+
+       player->anim_delay_counter =
+         graphic_info[special_graphic].anim_delay_fixed +
+         SimpleRND(graphic_info[special_graphic].anim_delay_random);
+       player->post_delay_counter =
+         graphic_info[special_graphic].post_delay_fixed +
+         SimpleRND(graphic_info[special_graphic].post_delay_random);
+
+       player->special_action_bored = special_action;
+      }
+
+      if (player->anim_delay_counter > 0)
+      {
+       action = player->special_action_bored;
+       player->anim_delay_counter--;
+      }
+      else if (player->post_delay_counter > 0)
+      {
+       player->post_delay_counter--;
+      }
+    }
+  }
+
+  return action;
+}
+
 static int getPlayerGraphic(struct PlayerInfo *player, int move_dir)
 {
   if (player->use_murphy_graphic)
@@ -553,6 +637,19 @@ static int getPlayerGraphic(struct PlayerInfo *player, int move_dir)
     return el_act_dir2img(player->element_nr, player->GfxAction, move_dir);
 }
 
+static boolean equalGraphics(int graphic1, int graphic2)
+{
+  struct GraphicInfo *g1 = &graphic_info[graphic1];
+  struct GraphicInfo *g2 = &graphic_info[graphic2];
+
+  return (g1->bitmap      == g2->bitmap &&
+         g1->src_x       == g2->src_x &&
+         g1->src_y       == g2->src_y &&
+         g1->anim_frames == g2->anim_frames &&
+         g1->anim_delay  == g2->anim_delay &&
+         g1->anim_mode   == g2->anim_mode);
+}
+
 void DrawAllPlayers()
 {
   int i;
@@ -615,11 +712,51 @@ void DrawPlayer(struct PlayerInfo *player)
   if (element == EL_EXPLOSION)
     return;
 
-  action = (player->is_pushing    ? ACTION_PUSHING    :
+#if 1
+
+  action = getPlayerAction(player, move_dir);
+
+#else
+
+  action = (player->is_pushing   ? ACTION_PUSHING    :
            player->is_digging    ? ACTION_DIGGING    :
            player->is_collecting ? ACTION_COLLECTING :
            player->is_moving     ? ACTION_MOVING     :
-           player->is_snapping   ? ACTION_SNAPPING   : ACTION_DEFAULT);
+           player->is_snapping   ? ACTION_SNAPPING   :
+           player->is_sleeping   ? ACTION_SLEEPING   :
+           player->is_bored      ? ACTION_BORING     :
+           player->is_waiting    ? ACTION_WAITING    : ACTION_DEFAULT);
+
+  if (player->is_bored && player->num_special_action_bored > 0)
+  {
+    if (player->anim_delay_counter == 0 && player->post_delay_counter == 0)
+    {
+      int graphic_waiting;
+
+      action = ACTION_BORING_1 + SimpleRND(player->num_special_action_bored);
+      special_graphic = el_act_dir2img(EL_SP_MURPHY, action, move_dir);
+
+      player->anim_delay_counter =
+       graphic_info[special_graphic].anim_delay_fixed +
+       SimpleRND(graphic_info[special_graphic].anim_delay_random);
+      player->post_delay_counter =
+       graphic_info[special_graphic].post_delay_fixed +
+       SimpleRND(graphic_info[special_graphic].post_delay_random);
+      player->special_action_bored = action;
+    }
+
+    if (player->anim_delay_counter > 0)
+    {
+      action = player->special_action_bored;
+      player->anim_delay_counter--;
+    }
+
+    if (player->post_delay_counter > 0)
+    {
+      player->post_delay_counter--;
+    }
+  }
+#endif
 
 #if 0
   printf("::: '%s'\n", element_action_info[action].suffix);
@@ -704,8 +841,7 @@ void DrawPlayer(struct PlayerInfo *player)
 
   /* in the case of changed player action or direction, prevent the current
      animation frame from being restarted for identical animations */
-  if (player->Frame == 0 &&
-      graphic_info[graphic].bitmap == graphic_info[last_player_graphic].bitmap)
+  if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
     player->Frame = last_player_frame;
 
 #else