DEADSOFTWARE

gibs and corpses now affected by mplats
[d2df-sdl.git] / src / game / g_gfx.pas
index 47c638c8e3bf79d92738398d24e1719f936549c7..a719ce69b0c6c21ccaa77bd025ea080e61b3ba1a 100644 (file)
@@ -14,6 +14,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
 {$INCLUDE ../shared/a_modes.inc}
+{.$DEFINE D2F_DEBUG_FALL_MPLAT}
 unit g_gfx;
 
 interface
@@ -171,9 +172,9 @@ begin
   awakeMinX := mapGrid.gridX0;
   awakeMinY := mapGrid.gridY0;
   SetLength(awakeMap, awakeMapW*awakeMapH);
-  {$IF DEFINED(D2F_DEBUG)}
+  //{$IF DEFINED(D2F_DEBUG)}
   e_LogWritefln('particle awake map: %sx%s (for grid of size %sx%s)', [awakeMapW, awakeMapH, mapGrid.gridWidth, mapGrid.gridHeight]);
-  {$ENDIF}
+  //{$ENDIF}
   awakeDirty := true;
   awmClear();
 end;
@@ -358,20 +359,25 @@ end;
 
 
 procedure TParticle.think (); inline;
+  procedure awake ();
+  begin
+    state := TPartState.Normal;
+    floorY := Unknown;
+    ceilingY := Unknown;
+    wallEndY := Unknown;
+    if (velY = 0) then velY := 0.1;
+    if (accelY = 0) then accelY := 0.5;
+  end;
+
 begin
   // awake sleeping particle, if necessary
   if awakeDirty then
   begin
     case state of
       TPartState.Sleeping, TPartState.Stuck:
-        if awmIsSet(x, y) then
-        begin
-          state := TPartState.Normal;
-          floorY := Unknown;
-          ceilingY := Unknown;
-          if (velY = 0) then velY := 0.1;
-          if (accelY = 0) then accelY := 0.5;
-        end;
+        if awmIsSet(x, y) then awake();
+      else
+        if (env = TEnvType.EWall) and awmIsSet(x, y) then awake();
     end;
   end;
   case particleType of
@@ -421,7 +427,13 @@ procedure TParticle.thinkerBloodAndWater ();
       TPartType.Water: result := (Random(30) = 15);
       else raise Exception.Create('internal error in particle engine: drip');
     end;
-    if result then begin velY := 0.5; accelY := 0.15; end;
+    if result then
+    begin
+      velY := 0.5;
+      accelY := 0.15;
+      // if we're falling from ceiling, switch to normal mode
+      if (state = TPartState.Stuck) and (stickDX = 0) then state := TPartState.Normal;
+    end;
   end;
 
   // switch to freefall mode
@@ -435,7 +447,7 @@ procedure TParticle.thinkerBloodAndWater ();
   procedure applyGravity (inLiquid: Boolean);
   begin
     state := TPartState.Normal;
-    if (inLiquid) then
+    if inLiquid then
     begin
       velY := 0.5;
       accelY := 0.15;
@@ -448,12 +460,16 @@ procedure TParticle.thinkerBloodAndWater ();
   end;
 
 label
-  _done;
+  _done, _gravityagain, _stuckagain;
 var
   pan: TPanel;
   dX, dY: SmallInt;
   ex, ey: Integer;
   checkEnv: Boolean;
+  floorJustTraced: Boolean;
+  {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
+  oldFloorY: Integer;
+  {$ENDIF}
 begin
   if not gpart_dbg_phys_enabled then goto _done;
 
@@ -487,7 +503,14 @@ begin
       else
       begin
         // stuck to a wall
-        assert(wallEndY <> Unknown);
+        if (wallEndY = Unknown) then
+        begin
+          // this can happen if mplat was moved out; find new `wallEndY`
+          findFloor(true); // force trace, just in case
+          if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
+          mapGrid.traceOrthoRayWhileIn(ex, wallEndY, x+stickDX, y, x+stickDX, floorY+1, (GridTagWall or GridTagDoor or GridTagStep));
+        end;
+       _stuckagain:
         // floor transition?
         if (wallEndY <= floorY) and (y >= floorY) then
         begin
@@ -495,8 +518,15 @@ begin
           case floorType of
             TFloorType.Wall: // hit the ground
               begin
-                sleep();
-                goto _done; // nothing to do anymore
+                // check if our ground wasn't moved since the last scan
+                findFloor(true); // force trace
+                if (y = floorY) then
+                begin
+                  sleep();
+                  goto _done; // nothing to do anymore
+                end;
+                // otherwise, do it again
+                goto _stuckagain;
               end;
             TFloorType.LiquidIn: // entering the liquid
               begin
@@ -536,14 +566,23 @@ begin
     // gravity, if not stuck
     if (state <> TPartState.Stuck) and (abs(velX) < 0.1) and (abs(velY) < 0.1) then
     begin
-      if (floorY = Unknown) then findFloor();
+      floorJustTraced := (floorY = Unknown);
+      if floorJustTraced then findFloor();
+     _gravityagain:
       // floor transition?
       if (y = floorY) then
       begin
         case floorType of
           TFloorType.Wall: // hit the ground
             begin
-              // nothing to do
+              // check if our ground wasn't moved since the last scan
+              if not floorJustTraced then
+              begin
+                findFloor(true); // force trace
+                if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
+                if (y <> floorY) then goto _gravityagain;
+              end;
+              // otherwise, nothing to do
             end;
           TFloorType.LiquidIn: // entering the liquid
             begin
@@ -616,7 +655,8 @@ begin
         while (dY > 0) do
         begin
           // falling down
-          if (floorY = Unknown) then findFloor(); // need to do this anyway
+          floorJustTraced := (floorY = Unknown);
+          if floorJustTraced then findFloor();
           if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
           y += dY;
           //e_LogWritefln('floorY=%s; newy=%s; dY=%s; floorType=%s', [floorY, y, dY, floorType]);
@@ -629,6 +669,22 @@ begin
             case floorType of
               TFloorType.Wall: // hit the ground
                 begin
+                  // check if our ground wasn't moved since the last scan
+                  if not floorJustTraced then
+                  begin
+                    {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
+                    oldFloorY := floorY;
+                    {$ENDIF}
+                    findFloor(true); // force trace
+                    {$IF DEFINED(D2F_DEBUG_FALL_MPLAT)}
+                    if (floorY <> oldFloorY) then
+                    begin
+                      e_LogWritefln('force rescanning vpart at (%s,%s); oldFloorY=%s; floorY=%s', [x, y, oldFloorY, floorY]);
+                    end;
+                    {$ENDIF}
+                    if (floorType = TFloorType.LiquidOut) then env := TEnvType.ELiquid else env := TEnvType.EAir;
+                    if (y <> floorY) then continue;
+                  end;
                   // environment didn't changed
                   hitAFloor();
                   break; // done with vertical movement
@@ -868,6 +924,7 @@ begin
       begin
         pan := g_Map_PanelAtPoint(x, y, GridTagObstacle);
       end;
+      if (pan <> nil) then continue;
       env := TEnvType.EAir;
 
       // color
@@ -1467,6 +1524,7 @@ end;
 procedure g_GFX_Draw ();
 var
   a, len: Integer;
+  scaled: Boolean;
 begin
   if not gpart_dbg_enabled then exit;
 
@@ -1480,12 +1538,15 @@ begin
 
     glBegin(GL_POINTS);
 
+    scaled := (g_dbg_scale <> 1.0);
+
     len := High(Particles);
     for a := 0 to len do
     begin
       with Particles[a] do
       begin
-        if alive and (x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight) then
+        if not alive then continue;
+        if scaled or ((x >= sX) and (y >= sY) and (x <= sX+sWidth) and (sY <= sY+sHeight)) then
         begin
           glColor4ub(red, green, blue, alpha);
           glVertex2f(x+0.37, y+0.37);