DEADSOFTWARE

optimized horizontal grid traces
[d2df-sdl.git] / src / game / g_holmes.pas
index 75e03bb30ec82a3a6509fc830fc790992a384103..d96d234503f202420944fdd9c4e7e840fb602745 100644 (file)
@@ -37,15 +37,15 @@ type
       WheelDown = $0010;
 
       // event types
-      Motion = 0;
+      Release = 0;
       Press = 1;
-      Release = 2;
+      Motion = 2;
 
   public
     kind: Byte; // motion, press, release
     x, y: Integer;
     dx, dy: Integer; // for wheel this is wheel motion, otherwise this is relative mouse motion
-    but: Word; // current pressed button or 0
+    but: Word; // current pressed/released button, or 0 for motion
     bstate: Word; // button state
     kstate: Word; // keyboard state (see THKeyEvent);
   end;
@@ -53,9 +53,21 @@ type
   THKeyEvent = record
   public
     const
+      // modifiers
       ModCtrl = $0001;
       ModAlt = $0002;
       ModShift = $0004;
+
+      // event types
+      Release = 0;
+      Press = 1;
+
+  public
+    kind: Byte;
+    scan: Word; // SDL_SCANCODE_XXX
+    sym: Word; // SDLK_XXX
+    bstate: Word; // button state
+    kstate: Word; // keyboard state
   end;
 
 
@@ -80,7 +92,7 @@ var
 implementation
 
 uses
-  SysUtils, GL,
+  SysUtils, GL, SDL2,
   MAPDEF, g_options;
 
 
@@ -90,11 +102,17 @@ var
   msY: Integer = -666;
   msB: Word = 0; // button state
   kbS: Word = 0; // keyboard modifiers state
+  showGrid: Boolean = true;
+  showMonsInfo: Boolean = false;
+  showMonsLOS2Plr: Boolean = false;
+  showAllMonsCells: Boolean = false;
+  showMapCurPos: Boolean = false;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
 {$INCLUDE g_holmes.inc}
 
+
 // ////////////////////////////////////////////////////////////////////////// //
 procedure g_Holmes_VidModeChanged ();
 begin
@@ -128,6 +146,7 @@ var
   laserX0, laserY0, laserX1, laserY1: Integer;
   monMarkedUID: Integer = -1;
 
+
 procedure g_Holmes_plrView (viewPortX, viewPortY, viewPortW, viewPortH: Integer);
 begin
   vpSet := true;
@@ -144,6 +163,7 @@ begin
   laserY0 := ay0;
   laserX1 := ax1;
   laserY1 := ay1;
+  laserSet := laserSet; // shut up, fpc!
 end;
 
 
@@ -156,7 +176,11 @@ procedure plrDebugMouse (var ev: THMouseEvent);
   function wallToggle (pan: TPanel; tag: Integer): Boolean;
   begin
     result := false; // don't stop
-    if pan.Enabled then g_Map_DisableWall(pan.arrIdx) else g_Map_EnableWall(pan.arrIdx);
+    e_WriteLog(Format('wall #%d(%d); enabled=%d (%d); (%d,%d)-(%d,%d)', [pan.arrIdx, pan.proxyId, Integer(pan.Enabled), Integer(mapGrid.proxyEnabled[pan.proxyId]), pan.X, pan.Y, pan.Width, pan.Height]), MSG_NOTIFY);
+    if ((kbS and THKeyEvent.ModAlt) <> 0) then
+    begin
+      if pan.Enabled then g_Map_DisableWall(pan.arrIdx) else g_Map_EnableWall(pan.arrIdx);
+    end;
   end;
 
   function monsAtDump (mon: TMonster; tag: Integer): Boolean;
@@ -167,14 +191,35 @@ procedure plrDebugMouse (var ev: THMouseEvent);
     //if pan.Enabled then g_Map_DisableWall(pan.arrIdx) else g_Map_EnableWall(pan.arrIdx);
   end;
 
+  function monsInCell (mon: TMonster; tag: Integer): Boolean;
+  begin
+    result := false; // don't stop
+    e_WriteLog(Format('monster #%d (UID:%u) (proxyid:%d)', [mon.arrIdx, mon.UID, mon.proxyId]), MSG_NOTIFY);
+  end;
+
 begin
   //e_WriteLog(Format('mouse: x=%d; y=%d; but=%d; bstate=%d', [msx, msy, but, bstate]), MSG_NOTIFY);
-  if (gPlayer1 = nil) then exit;
+  if (gPlayer1 = nil) or not vpSet then exit;
   if (ev.kind <> THMouseEvent.Press) then exit;
 
+  e_WriteLog(Format('mev: %d', [Integer(ev.kind)]), MSG_NOTIFY);
+
   if (ev.but = THMouseEvent.Left) then
   begin
-    mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
+    if ((kbS and THKeyEvent.ModShift) <> 0) then
+    begin
+      // dump monsters in cell
+      e_WriteLog('===========================', MSG_NOTIFY);
+      monsGrid.forEachInCell(pmsCurMapX, pmsCurMapY, monsInCell);
+      e_WriteLog('---------------------------', MSG_NOTIFY);
+    end
+    else
+    begin
+      // toggle wall
+      e_WriteLog('=== TOGGLE WALL ===', MSG_NOTIFY);
+      mapGrid.forEachAtPoint(pmsCurMapX, pmsCurMapY, wallToggle, (GridTagWall or GridTagDoor));
+      e_WriteLog('--- toggle wall ---', MSG_NOTIFY);
+    end;
     exit;
   end;
 
@@ -191,6 +236,47 @@ end;
 
 procedure plrDebugDraw ();
 
+  procedure drawTileGrid ();
+  var
+    x, y: Integer;
+  begin
+    y := mapGrid.gridY0;
+    while (y < mapGrid.gridY0+mapGrid.gridHeight) do
+    begin
+      x := mapGrid.gridX0;
+      while (x < mapGrid.gridX0+mapGrid.gridWidth) do
+      begin
+        if (x+mapGrid.tileSize > vpx) and (y+mapGrid.tileSize > vpy) and
+           (x < vpx+vpw) and (y < vpy+vph) then
+        begin
+          //e_DrawQuad(x, y, x+mapGrid.tileSize-1, y+mapGrid.tileSize-1, 96, 96, 96, 96);
+          drawRect(x, y, mapGrid.tileSize, mapGrid.tileSize, 96, 96, 96, 255);
+        end;
+        Inc(x, mapGrid.tileSize);
+      end;
+      Inc(y, mapGrid.tileSize);
+    end;
+  end;
+
+  procedure hilightCell (cx, cy: Integer);
+  begin
+    fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 0, 128, 0, 64);
+  end;
+
+  procedure hilightCell1 (cx, cy: Integer);
+  begin
+    //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
+    fillRect(cx, cy, monsGrid.tileSize, monsGrid.tileSize, 255, 255, 0, 92);
+  end;
+
+  function hilightWallTrc (pan: TPanel; tag: Integer; x, y, prevx, prevy: Integer): Boolean;
+  begin
+    result := false; // don't stop
+    if (pan = nil) then exit; // cell completion, ignore
+    //e_WriteLog(Format('h1: (%d,%d)', [cx, cy]), MSG_NOTIFY);
+    fillRect(pan.X, pan.Y, pan.Width, pan.Height, 0, 128, 128, 64);
+  end;
+
   function monsCollector (mon: TMonster; tag: Integer): Boolean;
   var
     ex, ey: Integer;
@@ -208,53 +294,91 @@ procedure plrDebugDraw ();
   procedure drawMonsterInfo (mon: TMonster);
   var
     mx, my, mw, mh: Integer;
-    emx, emy, emw, emh: Integer;
-    enemy: TMonster;
-    eplr: TPlayer;
-  begin
-    if (mon = nil) then exit;
-    mon.getMapBox(mx, my, mw, mh);
-    //mx += mw div 2;
 
-    //fillRect(mx-4, my-7*8-6, 110, 7*8+6, 0, 0, 94, 250);
-    shadeRect(mx-4, my-7*8-6, 110, 7*8+6, 128);
-    my -= 8;
-    my -= 2;
-
-    // type
-    drawText6(mx, my, Format('%s(U:%u)', [monsTypeToString(mon.MonsterType), mon.UID]), 255, 127, 0); my -= 8;
-    // beh
-    drawText6(mx, my, Format('Beh: %s', [monsBehToString(mon.MonsterBehaviour)]), 255, 127, 0); my -= 8;
-    // state
-    drawText6(mx, my, Format('State:%s (%d)', [monsStateToString(mon.MonsterState), mon.MonsterSleep]), 255, 127, 0); my -= 8;
-    // health
-    drawText6(mx, my, Format('Health:%d', [mon.MonsterHealth]), 255, 127, 0); my -= 8;
-    // ammo
-    drawText6(mx, my, Format('Ammo:%d', [mon.MonsterAmmo]), 255, 127, 0); my -= 8;
-    // target
-    drawText6(mx, my, Format('TgtUID:%u', [mon.MonsterTargetUID]), 255, 127, 0); my -= 8;
-    drawText6(mx, my, Format('TgtTime:%d', [mon.MonsterTargetTime]), 255, 127, 0); my -= 8;
-
-    mon.getMapBox(mx, my, mw, mh);
-    if (g_GetUIDType(mon.MonsterTargetUID) = UID_PLAYER) then
+    procedure drawMonsterTargetLine ();
+    var
+      emx, emy, emw, emh: Integer;
+      enemy: TMonster;
+      eplr: TPlayer;
+      ex, ey: Integer;
     begin
-      eplr := g_Player_Get(mon.MonsterTargetUID);
-      if (eplr <> nil) then
+      if (g_GetUIDType(mon.MonsterTargetUID) = UID_PLAYER) then
+      begin
+        eplr := g_Player_Get(mon.MonsterTargetUID);
+        if (eplr <> nil) then eplr.getMapBox(emx, emy, emw, emh) else exit;
+      end
+      else if (g_GetUIDType(mon.MonsterTargetUID) = UID_MONSTER) then
       begin
-        eplr.getMapBox(emx, emy, emw, emh);
-        drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 128);
+        enemy := g_Monsters_ByUID(mon.MonsterTargetUID);
+        if (enemy <> nil) then enemy.getMapBox(emx, emy, emw, emh) else exit;
+      end
+      else
+      begin
+        exit;
       end;
-    end
-    else if (g_GetUIDType(mon.MonsterTargetUID) = UID_MONSTER) then
+      mon.getMapBox(mx, my, mw, mh);
+      drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
+      if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
+      begin
+        drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
+      end;
+    end;
+
+    procedure drawLOS2Plr ();
+    var
+      emx, emy, emw, emh: Integer;
+      eplr: TPlayer;
+      ex, ey: Integer;
     begin
-      enemy := g_Monsters_ByUID(mon.MonsterTargetUID);
-      if (enemy <> nil) then
+      eplr := gPlayers[0];
+      if (eplr = nil) then exit;
+      eplr.getMapBox(emx, emy, emw, emh);
+      mon.getMapBox(mx, my, mw, mh);
+      drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 255);
+      {$IF DEFINED(D2F_DEBUG)}
+      //mapGrid.dbgRayTraceTileHitCB := hilightCell1;
+      {$ENDIF}
+      if (g_Map_traceToNearestWall(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, @ex, @ey) <> nil) then
+      //if (mapGrid.traceRay(ex, ey, mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, hilightWallTrc, (GridTagWall or GridTagDoor)) <> nil) then
       begin
-        enemy.getMapBox(emx, emy, emw, emh);
-        drawLine(mx+mw div 2, my+mh div 2, emx+emw div 2, emy+emh div 2, 255, 0, 0, 128);
+        drawLine(mx+mw div 2, my+mh div 2, ex, ey, 0, 255, 0, 255);
       end;
+      {$IF DEFINED(D2F_DEBUG)}
+      //mapGrid.dbgRayTraceTileHitCB := nil;
+      {$ENDIF}
     end;
 
+  begin
+    if (mon = nil) then exit;
+    mon.getMapBox(mx, my, mw, mh);
+    //mx += mw div 2;
+
+    monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
+
+    if showMonsInfo then
+    begin
+      //fillRect(mx-4, my-7*8-6, 110, 7*8+6, 0, 0, 94, 250);
+      shadeRect(mx-4, my-7*8-6, 110, 7*8+6, 128);
+      my -= 8;
+      my -= 2;
+
+      // type
+      drawText6(mx, my, Format('%s(U:%u)', [monsTypeToString(mon.MonsterType), mon.UID]), 255, 127, 0); my -= 8;
+      // beh
+      drawText6(mx, my, Format('Beh: %s', [monsBehToString(mon.MonsterBehaviour)]), 255, 127, 0); my -= 8;
+      // state
+      drawText6(mx, my, Format('State:%s (%d)', [monsStateToString(mon.MonsterState), mon.MonsterSleep]), 255, 127, 0); my -= 8;
+      // health
+      drawText6(mx, my, Format('Health:%d', [mon.MonsterHealth]), 255, 127, 0); my -= 8;
+      // ammo
+      drawText6(mx, my, Format('Ammo:%d', [mon.MonsterAmmo]), 255, 127, 0); my -= 8;
+      // target
+      drawText6(mx, my, Format('TgtUID:%u', [mon.MonsterTargetUID]), 255, 127, 0); my -= 8;
+      drawText6(mx, my, Format('TgtTime:%d', [mon.MonsterTargetTime]), 255, 127, 0); my -= 8;
+    end;
+
+    drawMonsterTargetLine();
+    if showMonsLOS2Plr then drawLOS2Plr();
     {
     property MonsterRemoved: Boolean read FRemoved write FRemoved;
     property MonsterPain: Integer read FPain write FPain;
@@ -262,6 +386,12 @@ procedure plrDebugDraw ();
     }
   end;
 
+  function highlightAllMonsterCells (mon: TMonster): Boolean;
+  begin
+    result := false; // don't stop
+    monsGrid.forEachBodyCell(mon.proxyId, hilightCell);
+  end;
+
 var
   mon: TMonster;
   mx, my, mw, mh: Integer;
@@ -274,7 +404,9 @@ begin
   glPushMatrix();
   glTranslatef(-vpx, -vpy, 0);
 
-  g_Mons_AlongLine(laserX0, laserY0, laserX1, laserY1, monsCollector, true);
+  if (showGrid) then drawTileGrid();
+
+  if (laserSet) then g_Mons_AlongLine(laserX0, laserY0, laserX1, laserY1, monsCollector, true);
 
   if (monMarkedUID <> -1) then
   begin
@@ -287,34 +419,12 @@ begin
     end;
   end;
 
-  //e_DrawPoint(16, laserX0, laserY0, 255, 255, 255);
+  if showAllMonsCells then g_Mons_ForEach(highlightAllMonsterCells);
 
   glPopMatrix();
-end;
-
 
-{
-    procedure drawTileGrid ();
-    var
-      x, y: Integer;
-    begin
-      y := mapGrid.gridY0;
-      while (y < mapGrid.gridY0+mapGrid.gridHeight) do
-      begin
-        x := mapGrid.gridX0;
-        while (x < mapGrid.gridX0+mapGrid.gridWidth) do
-        begin
-          if (x+mapGrid.tileSize > vpx) and (y+mapGrid.tileSize > vpy) and
-             (x < vpx+vpw) and (y < vpy+vph) then
-          begin
-            e_DrawQuad(x, y, x+mapGrid.tileSize-1, y+mapGrid.tileSize-1, 96, 96, 96, 96);
-          end;
-          Inc(x, mapGrid.tileSize);
-        end;
-        Inc(y, mapGrid.tileSize);
-      end;
-    end;
-}
+  if showMapCurPos then drawText8(4, gWinSizeY-10, Format('mappos:(%d,%d)', [pmsCurMapX, pmsCurMapY]), 255, 255, 0);
+end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -325,13 +435,128 @@ begin
   msY := ev.y;
   msB := ev.bstate;
   kbS := ev.kstate;
+  msB := msB;
   plrDebugMouse(ev);
 end;
 
 
 function g_Holmes_KeyEvent (var ev: THKeyEvent): Boolean;
+var
+  mon: TMonster;
+  pan: TPanel;
+  x, y, w, h: Integer;
+  ex, ey: Integer;
+  dx, dy: Integer;
+
+  procedure dummyWallTrc (cx, cy: Integer);
+  begin
+  end;
+
 begin
   result := false;
+  msB := ev.bstate;
+  kbS := ev.kstate;
+  case ev.scan of
+    SDL_SCANCODE_LCTRL, SDL_SCANCODE_RCTRL,
+    SDL_SCANCODE_LALT, SDL_SCANCODE_RALT,
+    SDL_SCANCODE_LSHIFT, SDL_SCANCODE_RSHIFT:
+      result := true;
+  end;
+  // press
+  if (ev.kind = THKeyEvent.Press) then
+  begin
+    // M-M: one monster think step
+    if (ev.scan = SDL_SCANCODE_M) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
+    begin
+      result := true;
+      gmon_debug_think := false;
+      gmon_debug_one_think_step := true; // do one step
+      exit;
+    end;
+    // M-I: toggle monster info
+    if (ev.scan = SDL_SCANCODE_I) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
+    begin
+      result := true;
+      showMonsInfo := not showMonsInfo;
+      exit;
+    end;
+    // M-L: toggle monster LOS to player
+    if (ev.scan = SDL_SCANCODE_L) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
+    begin
+      result := true;
+      showMonsLOS2Plr := not showMonsLOS2Plr;
+      exit;
+    end;
+    // M-G: toggle "show all cells occupied by monsters"
+    if (ev.scan = SDL_SCANCODE_G) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
+    begin
+      result := true;
+      showAllMonsCells := not showAllMonsCells;
+      exit;
+    end;
+    // M-A: wake up monster
+    if (ev.scan = SDL_SCANCODE_A) and ((ev.kstate and THKeyEvent.ModAlt) <> 0) then
+    begin
+      result := true;
+      if (monMarkedUID <> -1) then
+      begin
+        mon := g_Monsters_ByUID(monMarkedUID);
+        if (mon <> nil) then mon.WakeUp();
+      end;
+      exit;
+    end;
+    // C-T: teleport player
+    if (ev.scan = SDL_SCANCODE_T) and ((ev.kstate and THKeyEvent.ModCtrl) <> 0) then
+    begin
+      result := true;
+      //e_WriteLog(Format('TELEPORT: (%d,%d)', [pmsCurMapX, pmsCurMapY]), MSG_NOTIFY);
+      if (gPlayers[0] <> nil) then
+      begin
+        gPlayers[0].getMapBox(x, y, w, h);
+        gPlayers[0].TeleportTo(pmsCurMapX-w div 2, pmsCurMapY-h div 2, true, 69); // 69: don't change dir
+      end;
+      exit;
+    end;
+    // C-P: show cursor position on the map
+    if (ev.scan = SDL_SCANCODE_P) and ((ev.kstate and THKeyEvent.ModCtrl) <> 0) then
+    begin
+      result := true;
+      showMapCurPos := not showMapCurPos;
+      exit;
+    end;
+    // C-G: toggle grid
+    if (ev.scan = SDL_SCANCODE_G) and ((ev.kstate and THKeyEvent.ModCtrl) <> 0) then
+    begin
+      result := true;
+      showGrid := not showGrid;
+      exit;
+    end;
+    // C-UP, C-DOWN, C-LEFT, C-RIGHT: trace 10 pixels from cursor in the respective direction
+    if ((ev.scan = SDL_SCANCODE_UP) or (ev.scan = SDL_SCANCODE_DOWN) or (ev.scan = SDL_SCANCODE_LEFT) or (ev.scan = SDL_SCANCODE_RIGHT)) and
+       ((ev.kstate and THKeyEvent.ModCtrl) <> 0) then
+    begin
+      result := true;
+      dx := pmsCurMapX;
+      dy := pmsCurMapY;
+      case ev.scan of
+        SDL_SCANCODE_UP: dy -= 120;
+        SDL_SCANCODE_DOWN: dy += 120;
+        SDL_SCANCODE_LEFT: dx -= 120;
+        SDL_SCANCODE_RIGHT: dx += 120;
+      end;
+      {$IF DEFINED(D2F_DEBUG)}
+      //mapGrid.dbgRayTraceTileHitCB := dummyWallTrc;
+      mapGrid.dbgShowTraceLog := true;
+      {$ENDIF}
+      pan := g_Map_traceToNearest(pmsCurMapX, pmsCurMapY, dx, dy, (GridTagWall or GridTagDoor or GridTagStep or GridTagAcid1 or GridTagAcid2 or GridTagWater), @ex, @ey);
+      {$IF DEFINED(D2F_DEBUG)}
+      //mapGrid.dbgRayTraceTileHitCB := nil;
+      mapGrid.dbgShowTraceLog := false;
+      {$ENDIF}
+      e_LogWritefln('v-trace: (%d,%d)-(%d,%d); end=(%d,%d); hit=%d', [pmsCurMapX, pmsCurMapY, dx, dy, ex, ey, (pan <> nil)]);
+      exit;
+    end;
+  end;
 end;
 
 
@@ -353,6 +578,8 @@ begin
   //drawText8Prop(10, 20, 'Hi there, I''m Holmes!', 255, 255, 0);
 
   drawCursor();
+
+  laserSet := false;
 end;