rnd-19980827
[rocksndiamonds.git] / src / game.c
index 521cc1a8e549fcb3889e1ad86a6f055005d3bec5..2ed2996640d37d969c134f6d6975ecb351eddf72 100644 (file)
 #include "tape.h"
 #include "joystick.h"
 
-extern int Gamespeed;
-extern int Movemethod;
-extern int Movespeed[2];
+
+
+int tst = 0;
+int tst2 = 0;
+
+
+
+extern int GameSpeed;
+extern int MoveSpeed;
 
 void GetPlayerConfig()
 {
@@ -78,6 +84,7 @@ void InitGame()
   ScreenMovPos = 0;
   PlayerMovDir = MV_NO_MOVING;
   PlayerMovPos = 0;
+  PlayerGfxPos = 0;
   PlayerFrame = 0;
   PlayerPushing = FALSE;
   PlayerGone = LevelSolved = GameOver = SiebAktiv = FALSE;
@@ -107,8 +114,8 @@ void InitGame()
       case EL_SPIELFIGUR:
       case EL_SPIELER1:
        Feld[x][y] = EL_LEERRAUM;
-       JX = x;
-       JY = y;
+       JX = lastJX = x;
+       JY = lastJY = y;
        break;
       case EL_SPIELER2:
        Feld[x][y] = EL_LEERRAUM;
@@ -1392,6 +1399,15 @@ void StartMoving(int x, int y)
 
   if (CAN_FALL(element) && y<lev_fieldy-1)
   {
+    if (PlayerPushing && PlayerMovPos)
+    {
+      int nextJX = JX + (JX - lastJX);
+      int nextJY = JY + (JY - lastJY);
+
+      if (x == nextJX && y == nextJY)
+       return;
+    }
+
     if (element==EL_MORAST_VOLL)
     {
       if (IS_FREE(x,y+1))
@@ -1796,7 +1812,7 @@ void ContinueMoving(int x, int y)
   int dy = (direction==MV_UP   ? -1 : direction==MV_DOWN  ? +1 : 0);
   int horiz_move = (dx!=0);
   int newx = x + dx, newy = y + dy;
-  int step = (horiz_move ? dx : dy)*TILEX/4;
+  int step = (horiz_move ? dx : dy) * TILEX/8;
 
   if (CAN_FALL(element) && horiz_move)
     step*=2;
@@ -2332,7 +2348,11 @@ void AusgangstuerPruefen(int x, int y)
   if (!Gems && !SokobanFields && !Lights)
   {
     Feld[x][y] = EL_AUSGANG_ACT;
-    PlaySoundLevel(x,y,SND_OEFFNEN);
+
+    if (IN_SCR_FIELD(SCROLLX(x),SCROLLY(y)))
+      PlaySoundLevel(x,y,SND_OEFFNEN);
+    else
+      PlaySoundLevel(JX,JY,SND_OEFFNEN);
   }
 }
 
@@ -2372,7 +2392,7 @@ void EdelsteinFunkeln(int x, int y)
 
   if (Feld[x][y] == EL_EDELSTEIN_BD)
   {
-    const int delay = 2;
+    const int delay = 4; /* war: 2 */
     const int frames = 4;
     int phase = (FrameCounter % (delay*frames)) / delay;
 
@@ -2553,7 +2573,7 @@ void CheckForDragon(int x, int y)
 
 void GameActions()
 {
-  static long action_delay=0;
+  static long action_delay = 0;
   long action_delay_value;
 
   if (game_status != PLAYING)
@@ -2565,7 +2585,7 @@ void GameActions()
 */
 
   action_delay_value =
-    (tape.playing && tape.fast_forward ? FFWD_FRAME_DELAY : Gamespeed);
+    (tape.playing && tape.fast_forward ? FFWD_FRAME_DELAY : GameSpeed);
 
   /*
   if (DelayReached(&action_delay, action_delay_value))
@@ -2581,8 +2601,75 @@ void GameActions()
 
 
 
+  tst++;
+
+  if (0)
+  {
+    static long last_Counter = 0;
+    long new_Counter = Counter();
+
+    printf("--> %ld / %ld [%d]\n",
+          new_Counter - last_Counter,
+          new_Counter,
+          FrameCounter);
+    last_Counter = new_Counter;
+  }
+
+
+
+
+  /*
   if (!DelayReached(&action_delay, action_delay_value))
     return;
+    */
+
+
+  while(!DelayReached(&action_delay, action_delay_value))
+    Delay(1000);
+
+
+
+
+  /*
+  printf("-----------\n");
+  */
+
+
+
+  FrameCounter++;
+
+
+
+  /*
+  if (PlayerMovPos)
+    ScrollFigure(0);
+
+  DrawPlayerField();
+  */
+
+
+  tst2 = tst;
+  tst = 0;
+
+
+
+  if (0)
+  {
+    static long last_Counter = 0;
+    long new_Counter = Counter();
+
+    printf("--> %ld / %ld [%d]\n",
+          new_Counter - last_Counter,
+          new_Counter,
+          FrameCounter);
+    last_Counter = new_Counter;
+  }
+
+
+  /*
+  printf("--> %ld / ", Counter());
+  */
+
 
   {
     int x,y,element;
@@ -2593,7 +2680,12 @@ void GameActions()
     else if (tape.recording)
       TapeRecordDelay();
 
+
+    /*
     FrameCounter++;
+    */
+
+
     TimeFrames++;
 
     for(y=0;y<lev_fieldy;y++) for(x=0;x<lev_fieldx;x++)
@@ -2715,17 +2807,15 @@ void GameActions()
        }
       }
     }
+  }
 
-    /*
-    if (PlayerMovPos)
-      ScrollFigure(0);
 
-    DrawPlayerField();
-    */
+  /*
+  printf("%ld\n", Counter());
+  */
 
-  }
 
-  if (TimeLeft>0 && TimeFrames>=25 && !tape.pausing)
+  if (TimeLeft>0 && TimeFrames>=(100/GameSpeed) && !tape.pausing)
   {
     TimeFrames = 0;
     TimeLeft--;
@@ -2742,43 +2832,17 @@ void GameActions()
       KillHero();
   }
 
-  /*
-  if (PlayerMovPos)
-    ScrollFigure(0);
-    */
-
-
-  /*
   DrawPlayerField();
-  */
-
-
-  DrawPlayerField();
-
 
   BackToFront();
 }
 
 void ScrollLevel(int dx, int dy)
 {
+  int softscroll_offset = (soft_scrolling_on ? TILEX : 0);
   int x,y;
-  int softscroll_offset = (FX == TILEX ? TILEX : 0);
-
-  /*
-  if (soft_scrolling_on)
-  {
-    ScreenMovPos = PlayerMovPos;
-    redraw_mask |= REDRAW_FIELD;
-  }
-  */
-
-
-  ScreenMovPos = PlayerMovPos;
-  if (soft_scrolling_on)
-  {
-    redraw_mask |= REDRAW_FIELD;
-  }
 
+  ScreenMovPos = PlayerGfxPos;
 
   XCopyArea(display,drawto_field,drawto_field,gc,
            FX + TILEX*(dx==-1) - softscroll_offset,
@@ -2806,14 +2870,10 @@ void ScrollLevel(int dx, int dy)
 
 BOOL MoveFigureOneStep(int dx, int dy, int real_dx, int real_dy)
 {
-  int oldJX,oldJY, newJX = JX+dx,newJY = JY+dy;
+  int newJX = JX+dx, newJY = JY+dy;
   int element;
   int can_move;
 
-/*
-  int old_move_dir = PlayerMovDir;
-*/
-
   if (PlayerGone || (!dx && !dy))
     return(MF_NO_ACTION);
 
@@ -2821,12 +2881,6 @@ BOOL MoveFigureOneStep(int dx, int dy, int real_dx, int real_dy)
                  dx > 0 ? MV_RIGHT :
                  dy < 0 ? MV_UP :
                  dy > 0 ? MV_DOWN :    MV_NO_MOVING);
-/*
-  if (old_move_dir != PlayerMovDir)
-    PlayerFrame = 0;
-  else
-    PlayerFrame = (PlayerFrame + 1) % 4;
-*/
 
   if (!IN_LEV_FIELD(newJX,newJY))
     return(MF_NO_ACTION);
@@ -2854,30 +2908,15 @@ BOOL MoveFigureOneStep(int dx, int dy, int real_dx, int real_dy)
   if (can_move != MF_MOVING)
     return(can_move);
 
-  oldJX = JX;
-  oldJY = JY;
+  lastJX = JX;
+  lastJY = JY;
   JX = newJX;
   JY = newJY;
 
-
-  lastJX = oldJX;
-  lastJY = oldJY;
-
-  PlayerMovPos = (dx > 0 || dy > 0 ? -1 : 1) * 3*TILEX/4;
+  PlayerMovPos = (dx > 0 || dy > 0 ? -1 : 1) * 7*TILEX/8;
 
   ScrollFigure(-1);
 
-  if (Store[oldJX][oldJY])
-  {
-    DrawGraphic(SCROLLX(oldJX),SCROLLY(oldJY),el2gfx(Store[oldJX][oldJY]));
-    DrawGraphicThruMask(SCROLLX(oldJX),SCROLLY(oldJY),
-                       el2gfx(Feld[oldJX][oldJY]));
-  }
-  else if (Feld[oldJX][oldJY]==EL_DYNAMIT)
-    DrawDynamite(oldJX,oldJY);
-  else
-    DrawLevelField(oldJX,oldJY);
-
   return(MF_MOVING);
 }
 
@@ -2891,32 +2930,8 @@ BOOL MoveFigure(int dx, int dy)
   if (PlayerGone || (!dx && !dy))
     return(FALSE);
 
-/*
-  if (!DelayReached(&move_delay,8) && !tape.playing)
+  if (!FrameReached(&move_delay,MoveSpeed) && !tape.playing)
     return(FALSE);
-*/
-
-/*
-  if (!DelayReached(&move_delay,10) && !tape.playing)
-    return(FALSE);
-*/
-
-/*
-  if (!FrameReached(&move_delay,2) && !tape.playing)
-    return(FALSE);
-*/
-
-  if (Movemethod == 0)
-  {
-    if (!DelayReached(&move_delay,Movespeed[0]) && !tape.playing)
-      return(FALSE);
-  }
-  else
-  {
-    if (!FrameReached(&move_delay,Movespeed[1]) && !tape.playing)
-      return(FALSE);
-  }
-
 
   if (last_move_dir & (MV_LEFT | MV_RIGHT))
   {
@@ -2931,31 +2946,20 @@ BOOL MoveFigure(int dx, int dy)
 
   last_move_dir = MV_NO_MOVING;
 
-
-  /*
-  if (moved |= MoveFigureOneStep(dx,0, dx,dy))
-    moved |= MoveFigureOneStep(0,dy, dx,dy);
-  else
-  {
-    moved |= MoveFigureOneStep(0,dy, dx,dy);
-    moved |= MoveFigureOneStep(dx,0, dx,dy);
-  }
-  */
-
   if (moved & MF_MOVING)
   {
-    int old_scroll_x=scroll_x, old_scroll_y=scroll_y;
+    int old_scroll_x = scroll_x, old_scroll_y = scroll_y;
     int offset = (scroll_delay_on ? 3 : 0);
 
     if ((scroll_x < JX-MIDPOSX-offset || scroll_x > JX-MIDPOSX+offset) &&
-       JX>=MIDPOSX-1-offset && JX<=lev_fieldx-(MIDPOSX-offset))
+       JX >= MIDPOSX-1-offset && JX <= lev_fieldx-(MIDPOSX-offset))
       scroll_x = JX-MIDPOSX + (scroll_x < JX-MIDPOSX ? -offset : offset);
     if ((scroll_y < JY-MIDPOSY-offset || scroll_y > JY-MIDPOSY+offset) &&
-       JY>=MIDPOSY-1-offset && JY<=lev_fieldy-(MIDPOSY-offset))
+       JY >= MIDPOSY-1-offset && JY <= lev_fieldy-(MIDPOSY-offset))
       scroll_y = JY-MIDPOSY + (scroll_y < JY-MIDPOSY ? -offset : offset);
 
-    if (scroll_x!=old_scroll_x || scroll_y!=old_scroll_y)
-      ScrollLevel(old_scroll_x-scroll_x,old_scroll_y-scroll_y);
+    if (scroll_x != old_scroll_x || scroll_y != old_scroll_y)
+      ScrollLevel(old_scroll_x - scroll_x, old_scroll_y - scroll_y);
   }
 
   if (!(moved & MF_MOVING) && !PlayerPushing)
@@ -2977,10 +2981,6 @@ BOOL MoveFigure(int dx, int dy)
 
   TestIfHeroHitsBadThing();
 
-  /*
-  BackToFront();
-  */
-
   if (PlayerGone)
     RemoveHero();
 
@@ -2989,35 +2989,39 @@ BOOL MoveFigure(int dx, int dy)
 
 void ScrollFigure(int init)
 {
-  static long actual_frame_counter;
+  static long actual_frame_counter = 0;
   static int oldJX = -1, oldJY = -1;
 
   if (init)
   {
-    /*
-    if (PlayerMovPos && oldJX != -1 && oldJY != -1)
+
+    PlayerGfxPos =
+      (TILEX/ScrollSteps) * (PlayerMovPos / (TILEX/ScrollSteps));
+
+
+
+    if (0)
     {
-      if (Feld[lastJX][lastJY] == EL_LEERRAUM)
-       Feld[lastJX][lastJY] = EL_PLAYER_IS_LEAVING;
-      DrawLevelElement(oldJX,oldJY, Feld[oldJX][oldJY]);
-      DrawPlayerField();
+      static long last_Counter = 0;
+      long new_Counter = Counter();
+
+      printf("--> %ld / %ld [%d, %d]\n",
+            new_Counter - last_Counter,
+            new_Counter,
+            FrameCounter,
+            tst2);
+      last_Counter = new_Counter;
     }
-    */
+
+
+
 
     if (oldJX != -1 && oldJY != -1)
-    {
-      /*
-      if (Feld[lastJX][lastJY] == EL_LEERRAUM)
-       Feld[lastJX][lastJY] = EL_PLAYER_IS_LEAVING;
-       */
       DrawLevelElement(oldJX,oldJY, Feld[oldJX][oldJY]);
-      /*
-      DrawLevelElement(lastJX,lastJY, Feld[lastJX][lastJY]);
-      DrawPlayerField();
-      */
-    }
 
-    if (Feld[lastJX][lastJY] == EL_LEERRAUM)
+    if (Feld[lastJX][lastJY] == EL_LEERRAUM &&
+       IN_LEV_FIELD(lastJX,lastJY-1) &&
+       CAN_FALL(Feld[lastJX][lastJY-1]))
       Feld[lastJX][lastJY] = EL_PLAYER_IS_LEAVING;
     DrawLevelElement(lastJX,lastJY, Feld[lastJX][lastJY]);
     DrawPlayerField();
@@ -3026,27 +3030,45 @@ void ScrollFigure(int init)
     oldJY = lastJY;
     actual_frame_counter = FrameCounter;
 
-    /*
-    redraw[redraw_x1 + oldJX][redraw_y1 + oldJY] = 1;
-    redraw_tiles++;
-    */
+    if (PlayerPushing)
+    {
+      int nextJX = JX + (JX - lastJX);
+      int nextJY = JY + (JY - lastJY);
 
-    /*
-    DrawLevelElement(oldJX,oldJY, Feld[oldJX][oldJY]);
-    */
+      if (Feld[nextJX][nextJY] == EL_SOKOBAN_FELD_VOLL)
+       DrawLevelElement(nextJX,nextJY, EL_SOKOBAN_FELD_LEER);
+      else
+       DrawLevelElement(nextJX,nextJY, EL_LEERRAUM);
+    }
 
     DrawPlayerField();
 
+    if (Store[lastJX][lastJY])
+    {
+      DrawGraphic(SCROLLX(lastJX),SCROLLY(lastJY),
+                 el2gfx(Store[lastJX][lastJY]));
+      DrawGraphicThruMask(SCROLLX(lastJX),SCROLLY(lastJY),
+                         el2gfx(Feld[lastJX][lastJY]));
+    }
+    else if (Feld[lastJX][lastJY]==EL_DYNAMIT)
+      DrawDynamite(lastJX,lastJY);
+    else
+      DrawLevelField(lastJX,lastJY);
+
     return;
   }
   else if (!FrameReached(&actual_frame_counter,1))
     return;
 
-  PlayerMovPos += (PlayerMovPos > 0 ? -1 : 1) * TILEX/4;
+  PlayerMovPos += (PlayerMovPos > 0 ? -1 : 1) * TILEX/8;
+
+
+  PlayerGfxPos =
+    (TILEX/ScrollSteps) * (PlayerMovPos / (TILEX/ScrollSteps));
 
   if (ScreenMovPos)
   {
-    ScreenMovPos = PlayerMovPos;
+    ScreenMovPos = PlayerGfxPos;
     redraw_mask |= REDRAW_FIELD;
   }
 
@@ -3069,7 +3091,21 @@ void ScrollFigure(int init)
   else
     DrawLevelField(oldJX,oldJY);
 
+  if (PlayerPushing)
+  {
+    int nextJX = JX + (JX - lastJX);
+    int nextJY = JY + (JY - lastJY);
 
+    if (PlayerGfxPos)
+    {
+      if (Feld[nextJX][nextJY] == EL_SOKOBAN_FELD_VOLL)
+       DrawLevelElement(nextJX,nextJY, EL_SOKOBAN_FELD_LEER);
+      else
+       DrawLevelElement(nextJX,nextJY, EL_LEERRAUM);
+    }
+    else
+      DrawLevelElement(nextJX,nextJY, Feld[nextJX][nextJY]);
+  }
 
   if (!PlayerMovPos)
   {
@@ -3268,7 +3304,8 @@ int DigField(int x, int y, int real_dx, int real_dy, int mode)
   static long push_delay = 0;
   static int push_delay_value = 5;
 
-  PlayerPushing = FALSE;
+  if (!PlayerMovPos)
+    PlayerPushing = FALSE;
 
   if (mode == DF_NO_PUSH)
   {