DEADSOFTWARE

alot of debugging code
[d2df-sdl.git] / src / game / g_weapons.pas
index 148f87c33966595f8e761c3d3bf56b5d5c73bbbf..aa78ebd94e7ddae5fa7f224eada8b21f0cf3f79e 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
 {$INCLUDE ../shared/a_modes.inc}
+{.$DEFINE GWEP_HITSCAN_TRACE_BITMAP_CHECKER}
 unit g_weapons;
 
 interface
 
 uses
-  g_textures, g_basic, e_graphics, g_phys, BinEditor;
+  g_textures, g_basic, e_graphics, g_phys, BinEditor, xprofiler;
 
 const
   HIT_SOME    = 0;
@@ -62,7 +63,7 @@ function g_Weapon_Hit(obj: PObj; d: Integer; SpawnerUID: Word; t: Byte; HitCorps
 function g_Weapon_HitUID(UID: Word; d: Integer; SpawnerUID: Word; t: Byte): Boolean;
 function g_Weapon_CreateShot(I: Integer; ShotType: Byte; Spawner, TargetUID: Word; X, Y, XV, YV: Integer): LongWord;
 
-procedure g_Weapon_gun(x, y, xd, yd, v, dmg: Integer; SpawnerUID: Word; CheckTrigger: Boolean);
+procedure g_Weapon_gun(const x, y, xd, yd, v, dmg: Integer; SpawnerUID: Word; CheckTrigger: Boolean);
 procedure g_Weapon_punch(x, y: Integer; d, SpawnerUID: Word);
 function g_Weapon_chainsaw(x, y: Integer; d, SpawnerUID: Word): Integer;
 procedure g_Weapon_rocket(x, y, xd, yd: Integer; SpawnerUID: Word; WID: Integer = -1; Silent: Boolean = False);
@@ -116,13 +117,19 @@ const
   WP_FIRST          = WEAPON_KASTET;
   WP_LAST           = WEAPON_FLAMETHROWER;
 
+
+var
+  gwep_debug_fast_trace: Boolean = true;
+
+
 implementation
 
 uses
   Math, g_map, g_player, g_gfx, g_sound, g_main, g_panel,
   g_console, SysUtils, g_options, g_game,
   g_triggers, MAPDEF, e_log, g_monsters, g_saveload,
-  g_language, g_netmsg;
+  g_language, g_netmsg, g_grid,
+  binheap, hashtable;
 
 type
   TWaterPanel = record
@@ -152,8 +159,79 @@ const
 
   SHOT_SIGNATURE = $544F4853; // 'SHOT'
 
+type
+  PHitTime = ^THitTime;
+  THitTime = record
+    distSq: Integer;
+    mon: TMonster;
+    plridx: Integer; // if mon=nil
+    x, y: Integer;
+  end;
+
+  // indicies in `wgunHitTime` array
+  TBinaryHeapHitTimes = specialize TBinaryHeapBase<Integer>;
+
 var
   WaterMap: array of array of DWORD = nil;
+  //wgunMonHash: THashIntInt = nil;
+  wgunHitHeap: TBinaryHeapHitTimes = nil;
+  wgunHitTime: array of THitTime = nil;
+  wgunHitTimeUsed: Integer = 0;
+
+
+function hitTimeLess (a, b: Integer): Boolean;
+var
+  hta, htb: PHitTime;
+begin
+  hta := @wgunHitTime[a];
+  htb := @wgunHitTime[b];
+  if (hta.distSq <> htb.distSq) then begin result := (hta.distSq < htb.distSq); exit; end;
+  if (hta.mon <> nil) then
+  begin
+    // a is monster
+    if (htb.mon = nil) then begin result := false; exit; end; // players first
+    result := (hta.mon.UID < htb.mon.UID); // why not?
+  end
+  else
+  begin
+    // a is player
+    if (htb.mon <> nil) then begin result := true; exit; end; // players first
+    result := (hta.plridx < htb.plridx); // why not?
+  end;
+end;
+
+
+procedure appendHitTimeMon (adistSq: Integer; amon: TMonster; ax, ay: Integer);
+begin
+  if (wgunHitTimeUsed = Length(wgunHitTime)) then SetLength(wgunHitTime, wgunHitTimeUsed+128);
+  with wgunHitTime[wgunHitTimeUsed] do
+  begin
+    distSq := adistSq;
+    mon := amon;
+    plridx := -1;
+    x := ax;
+    y := ay;
+  end;
+  wgunHitHeap.insert(wgunHitTimeUsed);
+  Inc(wgunHitTimeUsed);
+end;
+
+
+procedure appendHitTimePlr (adistSq: Integer; aplridx: Integer; ax, ay: Integer);
+begin
+  if (wgunHitTimeUsed = Length(wgunHitTime)) then SetLength(wgunHitTime, wgunHitTimeUsed+128);
+  with wgunHitTime[wgunHitTimeUsed] do
+  begin
+    distSq := adistSq;
+    mon := nil;
+    plridx := aplridx;
+    x := ax;
+    y := ay;
+  end;
+  wgunHitHeap.insert(wgunHitTimeUsed);
+  Inc(wgunHitTimeUsed);
+end;
+
 
 function FindShot(): DWORD;
 var
@@ -257,7 +335,7 @@ var
   frameId: DWord;
 
   {
-  function monsWaterCheck (monidx: Integer; mon: TMonster): Boolean;
+  function monsWaterCheck (mon: TMonster): Boolean;
   begin
     result := false; // don't stop
     if mon.Live and mon.Collide(gWater[WaterMap[a][c]]) and (not InWArray(monidx, chkTrap_mn)) and (i2 < 1023) then //FIXME
@@ -268,7 +346,7 @@ var
   end;
   }
 
-  function monsWaterCheck (monidx: Integer; mon: TMonster): Boolean;
+  function monsWaterCheck (mon: TMonster): Boolean;
   begin
     result := false; // don't stop
     if (mon.trapCheckFrameId <> frameId) then
@@ -398,76 +476,27 @@ begin
     Result := True;
 end;
 
-function HitPlayer(p: TPlayer; d: Integer; vx, vy: Integer; SpawnerUID: Word; t: Byte): Boolean;
+
+function HitPlayer (p: TPlayer; d: Integer; vx, vy: Integer; SpawnerUID: Word; t: Byte): Boolean;
 begin
-  Result := False;
+  result := False;
 
-// Ñàì ñåáÿ ìîæåò ðàíèòü òîëüêî ðàêåòîé è òîêîì:
-  if (p.UID = SpawnerUID) and (t <> HIT_ROCKET) and (t <> HIT_ELECTRO) then
-    Exit;
+  // Ñàì ñåáÿ ìîæåò ðàíèòü òîëüêî ðàêåòîé è òîêîì
+  if (p.UID = SpawnerUID) and (t <> HIT_ROCKET) and (t <> HIT_ELECTRO) then exit;
 
   if g_Game_IsServer then
   begin
-    if (t <> HIT_FLAME) or (p.FFireTime = 0) or (vx <> 0) or (vy <> 0) then
-      p.Damage(d, SpawnerUID, vx, vy, t);
-    if (t = HIT_FLAME) then
-      p.CatchFire(SpawnerUID);
+    if (t <> HIT_FLAME) or (p.FFireTime = 0) or (vx <> 0) or (vy <> 0) then p.Damage(d, SpawnerUID, vx, vy, t);
+    if (t = HIT_FLAME) then p.CatchFire(SpawnerUID);
   end;
 
-  Result := True;
+  result := true;
 end;
 
-function GunHit(X, Y: Integer; vx, vy: Integer; dmg: Integer;
-  SpawnerUID: Word; AllowPush: Boolean): Byte;
-
-  {function monsCheck (monidx: Integer; mon: TMonster): Boolean;
-  begin
-    result := false; // don't stop
-    if mon.Live and mon.Collide(X, Y) then
-    begin
-      if HitMonster(mon, dmg, vx*10, vy*10-3, SpawnerUID, HIT_SOME) then
-      begin
-        if AllowPush then mon.Push(vx, vy);
-        result := true;
-      end;
-    end;
-  end;}
-
-  function monsCheck (monidx: Integer; mon: TMonster): Boolean;
-  begin
-    result := false; // don't stop
-    if HitMonster(mon, dmg, vx*10, vy*10-3, SpawnerUID, HIT_SOME) then
-    begin
-      if AllowPush then mon.Push(vx, vy);
-      result := true;
-    end;
-  end;
-
-var
-  i, h: Integer;
-begin
-  Result := 0;
-
-  h := High(gPlayers);
-
-  if h <> -1 then
-    for i := 0 to h do
-      if (gPlayers[i] <> nil) and gPlayers[i].Live and gPlayers[i].Collide(X, Y) then
-        if HitPlayer(gPlayers[i], dmg, vx*10, vy*10-3, SpawnerUID, HIT_SOME) then
-        begin
-          if AllowPush then gPlayers[i].Push(vx, vy);
-          Result := 1;
-        end;
-
-  if Result <> 0 then Exit;
-
-  //if g_Mons_ForEach(monsCheck) then result := 2;
-  if g_Mons_ForEachAliveAt(X, Y, 1, 1, monsCheck) then result := 2;
-end;
 
 procedure g_Weapon_BFG9000(X, Y: Integer; SpawnerUID: Word);
 
-  function monsCheck (monidx: Integer; mon: TMonster): Boolean;
+  function monsCheck (mon: TMonster): Boolean;
   begin
     result := false; // don't stop
     if (mon.Live) and (mon.UID <> SpawnerUID) then
@@ -534,12 +563,13 @@ begin
           end;
 
   //FIXME
-  g_Mons_ForEach(monsCheck);
+  g_Mons_ForEachAlive(monsCheck);
 end;
 
 function g_Weapon_CreateShot(I: Integer; ShotType: Byte; Spawner, TargetUID: Word; X, Y, XV, YV: Integer): LongWord;
 var
-  find_id, FramesID: DWORD;
+  find_id: DWord;
+  FramesID: DWORD = 0;
 begin
   if I < 0 then
     find_id := FindShot()
@@ -814,7 +844,7 @@ var
   end;
   }
 
-  function monsCheckHit (monidx: Integer; mon: TMonster): Boolean;
+  function monsCheckHit (mon: TMonster): Boolean;
   begin
     result := false; // don't stop
     if HitMonster(mon, d, obj.Vel.X, obj.Vel.Y, SpawnerUID, t) then
@@ -831,6 +861,7 @@ var
   function MonsterHit(): Boolean;
   begin
     //result := g_Mons_ForEach(monsCheckHit);
+    //FIXME: accelerate this!
     result := g_Mons_ForEachAliveAt(obj.X+obj.Rect.X, obj.Y+obj.Rect.Y, obj.Rect.Width, obj.Rect.Height, monsCheckHit);
   end;
 
@@ -931,7 +962,7 @@ function g_Weapon_Explode(X, Y: Integer; rad: Integer; SpawnerUID: Word): Boolea
 var
   r: Integer; // squared radius
 
-  function monsExCheck (monidx: Integer; mon: TMonster): Boolean;
+  function monsExCheck (mon: TMonster): Boolean;
   var
     dx, dy, mm: Integer;
   begin
@@ -1133,6 +1164,9 @@ begin
 
   g_Texture_CreateWADEx('TEXTURE_SHELL_BULLET', GameWAD+':TEXTURES\EBULLET');
   g_Texture_CreateWADEx('TEXTURE_SHELL_SHELL', GameWAD+':TEXTURES\ESHELL');
+
+  //wgunMonHash := hashNewIntInt();
+  wgunHitHeap := TBinaryHeapHitTimes.Create(hitTimeLess);
 end;
 
 procedure g_Weapon_FreeData();
@@ -1192,7 +1226,47 @@ begin
   g_Frames_DeleteByName('FRAMES_EXPLODE_BARONFIRE');
 end;
 
-procedure g_Weapon_gun(x, y, xd, yd, v, dmg: Integer; SpawnerUID: Word; CheckTrigger: Boolean);
+
+function GunHitPlayer (X, Y: Integer; vx, vy: Integer; dmg: Integer; SpawnerUID: Word; AllowPush: Boolean): Boolean;
+var
+  i: Integer;
+begin
+  result := false;
+  for i := 0 to High(gPlayers) do
+  begin
+    if (gPlayers[i] <> nil) and gPlayers[i].Live and gPlayers[i].Collide(X, Y) then
+    begin
+      if HitPlayer(gPlayers[i], dmg, vx*10, vy*10-3, SpawnerUID, HIT_SOME) then
+      begin
+        if AllowPush then gPlayers[i].Push(vx, vy);
+        result := true;
+      end;
+    end;
+  end;
+end;
+
+
+function GunHit (X, Y: Integer; vx, vy: Integer; dmg: Integer; SpawnerUID: Word; AllowPush: Boolean): Byte;
+
+  function monsCheck (mon: TMonster): Boolean;
+  begin
+    result := false; // don't stop
+    if HitMonster(mon, dmg, vx*10, vy*10-3, SpawnerUID, HIT_SOME) then
+    begin
+      if AllowPush then mon.Push(vx, vy);
+      result := true;
+    end;
+  end;
+
+begin
+  result := 0;
+       if GunHitPlayer(X, Y, vx, vy, dmg, SpawnerUID, AllowPush) then result := 1
+  else if g_Mons_ForEachAliveAt(X, Y, 1, 1, monsCheck) then result := 2;
+end;
+
+
+(*
+procedure g_Weapon_gunOld(const x, y, xd, yd, v, dmg: Integer; SpawnerUID: Word; CheckTrigger: Boolean);
 var
   a: Integer;
   x2, y2: Integer;
@@ -1202,10 +1276,13 @@ var
   s, c: Extended;
   //vx, vy: Integer;
   xx, yy, d: Integer;
-
   i: Integer;
   t1, _collide: Boolean;
   w, h: Word;
+  {$IF DEFINED(D2F_DEBUG)}
+  stt: UInt64;
+  showTime: Boolean = true;
+  {$ENDIF}
 begin
   a := GetAngle(x, y, xd, yd)+180;
 
@@ -1241,6 +1318,10 @@ begin
   //vx := (dx*10 div d)*xi;
   //vy := (dy*10 div d)*yi;
 
+  {$IF DEFINED(D2F_DEBUG)}
+  stt := curTimeMicro();
+  {$ENDIF}
+
   xx := x;
   yy := y;
 
@@ -1268,21 +1349,237 @@ begin
       if ByteBool(gCollideMap[yy, xx] and MARK_BLOCKED) then
       begin
         _collide := True;
+        {$IF DEFINED(D2F_DEBUG)}
+        stt := curTimeMicro()-stt;
+        e_WriteLog(Format('*** old trace time: %u microseconds', [LongWord(stt)]), MSG_NOTIFY);
+        showTime := false;
+        {$ENDIF}
         g_GFX_Spark(xx-xi, yy-yi, 2+Random(2), 180+a, 0, 0);
         if g_Game_IsServer and g_Game_IsNet then
           MH_SEND_Effect(xx-xi, yy-yi, 180+a, NET_GFX_SPARK);
       end;
 
     if not _collide then
+    begin
       _collide := GunHit(xx, yy, xi*v, yi*v, dmg, SpawnerUID, v <> 0) <> 0;
+    end;
+
+    if _collide then Break;
+  end;
 
-    if _collide then
-      Break;
+  {$IF DEFINED(D2F_DEBUG)}
+  if showTime then
+  begin
+    stt := curTimeMicro()-stt;
+    e_WriteLog(Format('*** old trace time: %u microseconds', [LongWord(stt)]), MSG_NOTIFY);
   end;
+  {$ENDIF}
 
   if CheckTrigger and g_Game_IsServer then
     g_Triggers_PressL(X, Y, xx-xi, yy-yi, SpawnerUID, ACTIVATE_SHOT);
 end;
+*)
+
+
+//!!!FIXME!!!
+procedure g_Weapon_gun (const x, y, xd, yd, v, dmg: Integer; SpawnerUID: Word; CheckTrigger: Boolean);
+var
+  x0, y0: Integer;
+  x2, y2: Integer;
+  xi, yi: Integer;
+  wallDistSq: Integer = $3fffffff;
+
+  function doPlayerHit (idx: Integer; hx, hy: Integer): Boolean;
+  begin
+    result := false;
+    if (idx < 0) or (idx > High(gPlayers)) then exit;
+    if (gPlayers[idx] = nil) or not gPlayers[idx].Live then exit;
+    result := HitPlayer(gPlayers[idx], dmg, (xi*v)*10, (yi*v)*10-3, SpawnerUID, HIT_SOME);
+    if result and (v <> 0) then gPlayers[idx].Push((xi*v), (yi*v));
+    {$IF DEFINED(D2F_DEBUG)}
+    //if result then e_WriteLog(Format('  PLAYER #%d HIT', [idx]), MSG_NOTIFY);
+    {$ENDIF}
+  end;
+
+  function doMonsterHit (mon: TMonster; hx, hy: Integer): Boolean;
+  begin
+    result := false;
+    if (mon = nil) then exit;
+    result := HitMonster(mon, dmg, (xi*v)*10, (yi*v)*10-3, SpawnerUID, HIT_SOME);
+    if result and (v <> 0) then mon.Push((xi*v), (yi*v));
+    {$IF DEFINED(D2F_DEBUG)}
+    //if result then e_WriteLog(Format('  MONSTER #%u HIT', [LongWord(mon.UID)]), MSG_NOTIFY);
+    {$ENDIF}
+  end;
+
+  // collect players along hitray
+  // return `true` if instant hit was detected
+  function playerPossibleHit (): Boolean;
+  var
+    i: Integer;
+    px, py, pw, ph: Integer;
+    inx, iny: Integer;
+    distSq: Integer;
+    plr: TPlayer;
+  begin
+    result := false;
+    for i := 0 to High(gPlayers) do
+    begin
+      plr := gPlayers[i];
+      if (plr <> nil) and plr.Live then
+      begin
+        plr.getMapBox(px, py, pw, ph);
+        if lineAABBIntersects(x, y, x2, y2, px, py, pw, ph, inx, iny) then
+        begin
+          distSq := distanceSq(x, y, inx, iny);
+          if (distSq = 0) then
+          begin
+            // contains
+            if doPlayerHit(i, x, y) then begin result := true; exit; end;
+          end
+          else if (distSq < wallDistSq) then
+          begin
+            appendHitTimePlr(distSq, i, inx, iny);
+          end;
+        end;
+      end;
+    end;
+  end;
+
+  function sqchecker (mon: TMonster; tag: Integer): Boolean;
+  var
+    mx, my, mw, mh: Integer;
+    inx, iny: Integer;
+    distSq: Integer;
+  begin
+    result := false; // don't stop
+    mon.getMapBox(mx, my, mw, mh);
+    if lineAABBIntersects(x0, y0, x2, y2, mx, my, mw, mh, inx, iny) then
+    begin
+      distSq := distanceSq(x0, y0, inx, iny);
+      if (distSq < wallDistSq) then appendHitTimeMon(distSq, mon, inx, iny);
+    end;
+  end;
+
+var
+  a: Integer;
+  dx, dy: Integer;
+  xe, ye: Integer;
+  s, c: Extended;
+  i: Integer;
+  wallHitFlag: Boolean = false;
+  wallHitX: Integer = 0;
+  wallHitY: Integer = 0;
+  didHit: Boolean = false;
+  {$IF DEFINED(D2F_DEBUG)}
+  stt: UInt64;
+  {$ENDIF}
+begin
+  (*
+  if not gwep_debug_fast_trace then
+  begin
+    g_Weapon_gunOld(x, y, xd, yd, v, dmg, SpawnerUID, CheckTrigger);
+    exit;
+  end;
+  *)
+
+  if (xd = 0) and (yd = 0) then exit;
+
+  //wgunMonHash.reset(); //FIXME: clear hash on level change
+  wgunHitHeap.clear();
+  wgunHitTimeUsed := 0;
+
+  a := GetAngle(x, y, xd, yd)+180;
+
+  SinCos(DegToRad(-a), s, c);
+
+  if Abs(s) < 0.01 then s := 0;
+  if Abs(c) < 0.01 then c := 0;
+
+  x0 := x;
+  y0 := y;
+  x2 := x+Round(c*gMapInfo.Width);
+  y2 := y+Round(s*gMapInfo.Width);
+
+  dx := x2-x;
+  dy := y2-y;
+
+  if (dx > 0) then xi := 1 else if (dx < 0) then xi := -1 else xi := 0;
+  if (dy > 0) then yi := 1 else if (dy < 0) then yi := -1 else yi := 0;
+
+  {$IF DEFINED(D2F_DEBUG)}
+  e_WriteLog(Format('GUN TRACE: (%d,%d) to (%d,%d)', [x, y, x2, y2]), MSG_NOTIFY);
+  stt := curTimeMicro();
+  {$ENDIF}
+
+  wallHitFlag := (g_Map_traceToNearestWall(x, y, x2, y2, @wallHitX, @wallHitY) <> nil);
+  if wallHitFlag then
+  begin
+    x2 := wallHitX;
+    y2 := wallHitY;
+    wallDistSq := distanceSq(x, y, wallHitX, wallHitY);
+  end
+  else
+  begin
+    wallHitX := x2;
+    wallHitY := y2;
+  end;
+
+  if playerPossibleHit() then exit; // instant hit
+
+  // collect monsters
+  g_Mons_AlongLine(x, y, x2, y2, sqchecker);
+
+  // here, we collected all monsters and players in `wgunHitHeap` and `wgunHitTime`
+  // also, if `wallWasHit` is `true`, then `wallHitX` and `wallHitY` contains spark coords
+  while (wgunHitHeap.count > 0) do
+  begin
+    // has some entities to check, do it
+    i := wgunHitHeap.front;
+    wgunHitHeap.popFront();
+    // hitpoint
+    xe := wgunHitTime[i].x;
+    ye := wgunHitTime[i].y;
+    // check if it is not behind the wall
+    if (wgunHitTime[i].mon <> nil) then
+    begin
+      didHit := doMonsterHit(wgunHitTime[i].mon, xe, ye);
+    end
+    else
+    begin
+      didHit := doPlayerHit(wgunHitTime[i].plridx, xe, ye);
+    end;
+    if didHit then
+    begin
+      // need new coords for trigger
+      wallHitX := xe;
+      wallHitY := ye;
+      wallHitFlag := false; // no sparks
+      break;
+    end;
+  end;
+
+  // need sparks?
+  if wallHitFlag then
+  begin
+    {$IF DEFINED(D2F_DEBUG)}
+    stt := curTimeMicro()-stt;
+    e_WriteLog(Format('*** new trace time: %u microseconds', [LongWord(stt)]), MSG_NOTIFY);
+    {$ENDIF}
+    g_GFX_Spark(wallHitX, wallHitY, 2+Random(2), 180+a, 0, 0);
+    if g_Game_IsServer and g_Game_IsNet then MH_SEND_Effect(wallHitX, wallHitY, 180+a, NET_GFX_SPARK);
+  end
+  else
+  begin
+    {$IF DEFINED(D2F_DEBUG)}
+    stt := curTimeMicro()-stt;
+    e_WriteLog(Format('*** new trace time: %u microseconds', [LongWord(stt)]), MSG_NOTIFY);
+    {$ENDIF}
+  end;
+
+  if CheckTrigger and g_Game_IsServer then g_Triggers_PressL(X, Y, wallHitX, wallHitY, SpawnerUID, ACTIVATE_SHOT);
+end;
+
 
 procedure g_Weapon_punch(x, y: Integer; d, SpawnerUID: Word);
 var
@@ -2057,7 +2354,7 @@ begin
             else
               tf := 3;
 
-            if (gTime mod tf = 0) then
+            if (gTime mod LongWord(tf) = 0) then
             begin
               Anim := TAnimation.Create(TextureID, False, 2 + Random(2));
               Anim.Alpha := 0;