DEADSOFTWARE

gibs and corpses now affected by mplats
[d2df-sdl.git] / src / game / g_gfx.pas
index d3cc071ad5f4a0a4c1598884ad276bb2176ac699..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
@@ -121,6 +122,8 @@ type
     procedure freeze (); inline; // remove velocities and acceleration
     procedure sleep (); inline; // switch to sleep mode
 
+    function checkAirStreams (): Boolean; // `true`: affected by air stream
+
     function alive (): Boolean; inline;
     procedure die (); inline;
     procedure think (); inline;
@@ -169,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;
@@ -228,11 +231,50 @@ begin
 end;
 
 
+// `true`: affected by air stream
+function TParticle.checkAirStreams (): Boolean;
+var
+  pan: TPanel;
+begin
+  pan := g_Map_PanelAtPoint(x, y, GridTagLift);
+  result := (pan <> nil);
+  if result then
+  begin
+    if ((pan.PanelType and PANEL_LIFTUP) <> 0) then
+    begin
+      if (velY > -4-Random(3)) then velY -= 0.8;
+      if (abs(velX) > 0.1) then velX -= velX/10.0;
+      velX += (Random-Random)*0.2;
+      accelY := 0.15;
+    end
+    else if ((pan.PanelType and PANEL_LIFTLEFT) <> 0) then
+    begin
+      if (velX > -8-Random(3)) then velX -= 0.8;
+      accelY := 0.15;
+    end
+    else if ((pan.PanelType and PANEL_LIFTRIGHT) <> 0) then
+    begin
+      if (velX < 8+Random(3)) then velX += 0.8;
+      accelY := 0.15;
+    end
+    else
+    begin
+      result := false;
+    end;
+    // awake
+    if result and (state = TPartState.Sleeping) then state := TPartState.Normal;
+  end;
+end;
+
+
 // switch to sleep mode
 procedure TParticle.sleep (); inline;
 begin
-  state := TPartState.Sleeping;
-  freeze();
+  if not checkAirStreams() then
+  begin
+    state := TPartState.Sleeping;
+    freeze();
+  end;
 end;
 
 
@@ -317,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
@@ -380,41 +427,12 @@ 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;
-  end;
-
-  // `true`: affected by air stream
-  function checkAirStreams (): Boolean;
-  var
-    pan: TPanel;
-  begin
-    pan := g_Map_PanelAtPoint(x, y, GridTagLift);
-    result := (pan <> nil);
     if result then
     begin
-      if ((pan.PanelType and PANEL_LIFTUP) <> 0) then
-      begin
-        if (velY > -4-Random(3)) then velY -= 0.8;
-        if (abs(velX) > 0.1) then velX -= velX/10.0;
-        velX += (Random-Random)*0.2;
-        accelY := 0.15;
-      end
-      else if ((pan.PanelType and PANEL_LIFTLEFT) <> 0) then
-      begin
-        if (velX > -8-Random(3)) then velX -= 0.8;
-        accelY := 0.15;
-      end
-      else if ((pan.PanelType and PANEL_LIFTRIGHT) <> 0) then
-      begin
-        if (velX < 8+Random(3)) then velX += 0.8;
-        accelY := 0.15;
-      end
-      else
-      begin
-        result := false;
-      end;
-      // awake
-      if result and (state = TPartState.Sleeping) then state := TPartState.Normal;
+      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;
 
@@ -429,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;
@@ -442,18 +460,23 @@ 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;
 
   if gAdvBlood then
   begin
-    // still check for air streams when sleeping
-    if (state = TPartState.Sleeping) then begin checkAirStreams(); goto _done; end; // so blood will dissolve
+    // still check for air streams when sleeping (no)
+    if (state = TPartState.Sleeping) then begin {checkAirStreams();} goto _done; end; // so blood will dissolve
 
     // process stuck particles
     if (state = TPartState.Stuck) then
@@ -480,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
@@ -488,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
@@ -524,17 +561,28 @@ begin
     dX := round(velX);
     dY := round(velY);
 
+    if (state = TPartState.Normal) then checkAirStreams();
+
     // 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
@@ -562,15 +610,21 @@ begin
     begin
       // has some horizontal velocity
       pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
-      if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
+      checkEnv := (x <> ex);
       x := ex;
       y := ey;
+      if checkEnv then
+      begin
+        // dunno yet
+        floorY := Unknown;
+        ceilingY := Unknown;
+        // check environment (air/liquid)
+        if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
+      end;
       if (pan <> nil) then
       begin
         // we stuck
         // the only case when we can have both ceiling and wall is corner; stick to wall in this case
-        // check environment (air/liquid)
-        if (g_Map_PanelAtPoint(x, y, GridTagLiquid) <> nil) then env := TEnvType.ELiquid else env := TEnvType.EAir;
         // check if we stuck to a wall
         if (dX < 0) then dX := -1 else dX := 1;
         if (g_Map_PanelAtPoint(x+dX, y, GridTagObstacle) <> nil) then
@@ -601,7 +655,9 @@ 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]);
           if (y >= floorY) then
@@ -613,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
@@ -670,16 +742,22 @@ _done:
       time += 1;
       if (liveTime <= 0) then begin die(); exit; end;
       ex := 255-trunc(255.0*time/liveTime);
-      if (ex >= 250) then begin die(); exit; end;
-      if (ex < 0) then ex := 0;
+      if (ex <= 10) then begin die(); exit; end;
+      if (ex > 250) then ex := 255;
       alpha := Byte(ex);
     end;
   end
   else
   begin
-    // water will disappear in water (?)
-    if (env = TEnvType.ELiquid) then die();
+    // water will disappear in any liquid
+    if (env = TEnvType.ELiquid) then begin die(); exit; end;
     time += 1;
+    // dry water
+    if (liveTime <= 0) then begin die(); exit; end;
+    ex := 255-trunc(255.0*time/liveTime);
+    if (ex <= 10) then begin die(); exit; end;
+    if (ex > 250) then ex := 255;
+    alpha := Byte(ex);
   end;
 end;
 
@@ -835,19 +913,19 @@ begin
       // check for level bounds
       if (x < g_Map_MinX) or (y < g_Map_MinY) or (x > g_Map_MaxX) or (y > g_Map_MaxY) then continue;
 
-      // in what environment we are starting in?
-      pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
-      if (pan <> nil) then
+      // this hack will allow water spawned in water to fly out
+      // it can happen when player fell from a huge height (see "DOOM2D.WAD:\MAP03", for example)
+      if (fVelY >= 0) then
       begin
-        // either in a wall, or in a liquid
-        //if ((pan.tag and GridTagObstacle) <> 0) then continue; // don't spawn in walls
-        //env := TEnvType.ELiquid;
-        continue;
+        // in what environment we are starting in?
+        pan := g_Map_PanelAtPoint(x, y, (GridTagObstacle or GridTagLiquid));
       end
       else
       begin
-        env := TEnvType.EAir;
+        pan := g_Map_PanelAtPoint(x, y, GridTagObstacle);
       end;
+      if (pan <> nil) then continue;
+      env := TEnvType.EAir;
 
       // color
       case color of
@@ -1045,12 +1123,13 @@ begin
   if (dX <> 0) then
   begin
     // has some horizontal velocity
-    pan := g_Map_traceToNearest(x, y, x+dX, y+dY, GridTagObstacle, @ex, @ey);
+    pan := g_Map_traceToNearest(x, y, x+dX, y+dY, (GridTagObstacle or GridTagLiquid), @ex, @ey);
     if (x <> ex) then begin floorY := Unknown; ceilingY := Unknown; end; // dunno yet
     x := ex;
     y := ey;
     if (pan <> nil) then
     begin
+      if ((pan.tag and GridTagLiquid) <> 0) then begin die(); exit; end; // die in liquid
       // hit the wall; falling down vertically
       velX := 0;
       accelX := 0;
@@ -1445,6 +1524,7 @@ end;
 procedure g_GFX_Draw ();
 var
   a, len: Integer;
+  scaled: Boolean;
 begin
   if not gpart_dbg_enabled then exit;
 
@@ -1458,15 +1538,18 @@ 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);
-          glVertex2i(x, y);
+          glVertex2f(x+0.37, y+0.37);
         end;
       end;
     end;