DEADSOFTWARE

no more tree for map
[d2df-sdl.git] / src / game / g_game.pas
index 963f8aec27dfa4e0f4586e9ecfbbecd6c69f203c..e4f7afea5da959741fbb7a5b0af47bfaaf2a99b1 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
-{$MODE DELPHI}
+{$INCLUDE ../shared/a_modes.inc}
 unit g_game;
 
 interface
 
 uses
   g_basic, g_player, e_graphics, Classes, g_res_downloader,
-  SysUtils, g_sound, g_gui, MAPSTRUCT, wadreader, md5;
+  SysUtils, g_sound, g_gui, MAPSTRUCT, wadreader, md5, xprofiler;
 
 type
   TGameSettings = record
@@ -124,6 +124,7 @@ procedure GameCVars(P: SArray);
 procedure GameCommands(P: SArray);
 procedure GameCheats(P: SArray);
 procedure DebugCommands(P: SArray);
+procedure ProfilerCommands(P: SArray);
 procedure g_Game_Process_Params;
 procedure g_Game_SetLoadingText(Text: String; Max: Integer; reWrite: Boolean);
 procedure g_Game_StepLoading();
@@ -309,17 +310,146 @@ var
   P1MoveButton: Byte = 0;
   P2MoveButton: Byte = 0;
 
+  g_profile_frame_update: Boolean = false;
+  g_profile_frame_draw: Boolean = false;
+  g_profile_collision: Boolean = false;
+  g_profile_history_size: Integer = 1000;
+
+procedure g_ResetDynlights ();
+procedure g_AddDynLight (x, y, radius: Integer; r, g, b, a: Single);
+procedure g_DynLightExplosion (x, y, radius: Integer; r, g, b: Single);
+
 implementation
 
 uses
   g_textures, g_main, g_window, g_menu,
-  e_input, e_log, g_console, g_items, g_map,
+  e_input, e_log, g_console, g_items, g_map, g_panel,
   g_playermodel, g_gfx, g_options, g_weapons, Math,
   g_triggers, MAPDEF, g_monsters, e_sound, CONFIG,
   BinEditor, g_language, g_net, SDL,
-  ENet, e_fixedbuffer, g_netmsg, g_netmaster, GL, GLExt,
+  ENet, e_msg, g_netmsg, g_netmaster, GL, GLExt,
   utils, sfs;
 
+
+// ////////////////////////////////////////////////////////////////////////// //
+var
+  profileFrameDraw: TProfiler = nil;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+type
+  TDynLight = record
+    x, y, radius: Integer;
+    r, g, b, a: Single;
+    exploCount: Integer;
+    exploRadius: Integer;
+  end;
+
+var
+  g_dynLights: array of TDynLight = nil;
+  g_dynLightCount: Integer = 0;
+  g_playerLight: Boolean = false;
+
+procedure g_ResetDynlights ();
+var
+  lnum, idx: Integer;
+begin
+  if not gwin_has_stencil then begin g_dynLightCount := 0; exit; end;
+  lnum := 0;
+  for idx := 0 to g_dynLightCount-1 do
+  begin
+    if g_dynLights[idx].exploCount = -666 then
+    begin
+      // skip it
+    end
+    else
+    begin
+      // explosion
+      Inc(g_dynLights[idx].exploCount);
+      if (g_dynLights[idx].exploCount < 10) then
+      begin
+        g_dynLights[idx].radius := g_dynLights[idx].exploRadius+g_dynLights[idx].exploCount*8;
+        g_dynLights[idx].a := 0.4+g_dynLights[idx].exploCount/10;
+        if (g_dynLights[idx].a > 0.8) then g_dynLights[idx].a := 0.8;
+        if lnum <> idx then g_dynLights[lnum] := g_dynLights[idx];
+        Inc(lnum);
+      end;
+    end;
+  end;
+  g_dynLightCount := lnum;
+end;
+
+procedure g_AddDynLight (x, y, radius: Integer; r, g, b, a: Single);
+begin
+  if not gwin_has_stencil then exit;
+  if g_dynLightCount = length(g_dynLights) then SetLength(g_dynLights, g_dynLightCount+1024);
+  g_dynLights[g_dynLightCount].x := x;
+  g_dynLights[g_dynLightCount].y := y;
+  g_dynLights[g_dynLightCount].radius := radius;
+  g_dynLights[g_dynLightCount].r := r;
+  g_dynLights[g_dynLightCount].g := g;
+  g_dynLights[g_dynLightCount].b := b;
+  g_dynLights[g_dynLightCount].a := a;
+  g_dynLights[g_dynLightCount].exploCount := -666;
+  Inc(g_dynLightCount);
+end;
+
+procedure g_DynLightExplosion (x, y, radius: Integer; r, g, b: Single);
+begin
+  if not gwin_has_stencil then exit;
+  if g_dynLightCount = length(g_dynLights) then SetLength(g_dynLights, g_dynLightCount+1024);
+  g_dynLights[g_dynLightCount].x := x;
+  g_dynLights[g_dynLightCount].y := y;
+  g_dynLights[g_dynLightCount].radius := 0;
+  g_dynLights[g_dynLightCount].exploRadius := radius;
+  g_dynLights[g_dynLightCount].r := r;
+  g_dynLights[g_dynLightCount].g := g;
+  g_dynLights[g_dynLightCount].b := b;
+  g_dynLights[g_dynLightCount].a := 0;
+  g_dynLights[g_dynLightCount].exploCount := 0;
+  Inc(g_dynLightCount);
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+function calcProfilesHeight (prof: TProfiler): Integer;
+begin
+  result := 0;
+  if (prof = nil) then exit;
+  if (length(prof.bars) = 0) then exit;
+  result := length(prof.bars)*(16+2);
+end;
+
+// returns width
+function drawProfiles (x, y: Integer; prof: TProfiler): Integer;
+var
+  wdt, hgt: Integer;
+  yy: Integer;
+  ii: Integer;
+begin
+  result := 0;
+  if (prof = nil) then exit;
+  // gScreenWidth
+  if (length(prof.bars) = 0) then exit;
+  wdt := 192;
+  hgt := calcProfilesHeight(prof);
+  if (x < 0) then x := gScreenWidth-(wdt-1)+x;
+  if (y < 0) then y := gScreenHeight-(hgt-1)+y;
+  // background
+  //e_DrawFillQuad(x, y, x+wdt-1, y+hgt-1, 255, 255, 255, 200, B_BLEND);
+  e_DrawFillQuad(x, y, x+wdt-1, y+hgt-1, 20, 20, 20, 0, B_NONE);
+  // title
+  yy := y+2;
+  for ii := 0 to High(prof.bars) do
+  begin
+    e_TextureFontPrintEx(x+2+4*prof.bars[ii].level, yy, Format('%s: %d', [prof.bars[ii].name, prof.bars[ii].value]), gStdFont, 255, 255, 0, 1, false);
+    Inc(yy, 16+2);
+  end;
+  result := wdt;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
 type
   TEndCustomGameStat = record
     PlayerStat: TPlayerStatArray;
@@ -1285,6 +1415,21 @@ begin
       if isKeyPressed(KeyWeapon[i], KeyWeapon2[i]) then
         plr.QueueWeaponSwitch(i); // all choices are passed there, and god will take the best
   end;
+
+  // HACK: add dynlight here
+  if gwin_k8_enable_light_experiments then
+  begin
+    if e_KeyPressed(IK_F8) and gGameOn and (not gConsoleShow) and (g_ActiveWindow = nil) then
+    begin
+      g_playerLight := true;
+    end;
+    if e_KeyPressed(IK_F9) and gGameOn and (not gConsoleShow) and (g_ActiveWindow = nil) then
+    begin
+      g_playerLight := false;
+    end;
+  end;
+
+  if gwin_has_stencil and g_playerLight then g_AddDynLight(plr.GameX+32, plr.GameY+40, 128, 1, 1, 0, 0.6);
 end;
 
 procedure g_Game_Update();
@@ -1294,7 +1439,26 @@ var
   a: Byte;
   w: Word;
   i, b: Integer;
+
+  function sendMonsPos (mon: TMonster): Boolean;
+  begin
+    result := false; // don't stop
+    if (mon.MonsterType = MONSTER_BARREL) then
+    begin
+      if (mon.GameVelX <> 0) or (mon.GameVelY <> 0) then MH_SEND_MonsterPos(mon.UID);
+    end
+    else
+      if (mon.MonsterState <> MONSTATE_SLEEP) then
+      begin
+        if (mon.MonsterState <> MONSTATE_DEAD) or (mon.GameVelX <> 0) or (mon.GameVelY <> 0) then
+        begin
+          MH_SEND_MonsterPos(mon.UID);
+        end;
+      end;
+  end;
+
 begin
+  g_ResetDynlights();
 // Ïîðà âûêëþ÷àòü èãðó:
   if gExit = EXIT_QUIT then
     Exit;
@@ -1662,22 +1826,7 @@ begin
           if gPlayers[I] <> nil then
             MH_SEND_PlayerPos(True, gPlayers[I].UID);
 
-        if gMonsters <> nil then
-          for I := 0 to High(gMonsters) do
-            if gMonsters[I] <> nil then
-            begin
-              if (gMonsters[I].MonsterType = MONSTER_BARREL) then
-              begin
-                if (gMonsters[I].GameVelX <> 0) or (gMonsters[I].GameVelY <> 0) then
-                  MH_SEND_MonsterPos(gMonsters[I].UID);
-              end
-              else
-                if (gMonsters[I].MonsterState <> MONSTATE_SLEEP) then
-                  if (gMonsters[I].MonsterState <> MONSTATE_DEAD) or
-                     (gMonsters[I].GameVelX <> 0) or
-                     (gMonsters[I].GameVelY <> 0) then
-                  MH_SEND_MonsterPos(gMonsters[I].UID);
-            end;
+        g_Mons_ForEach(sendMonsPos);
 
         NetTimeToReliable := 0;
         NetTimeToUpdate := NetUpdateRate;
@@ -1689,22 +1838,7 @@ begin
             if gPlayers[I] <> nil then
               MH_SEND_PlayerPos(False, gPlayers[I].UID);
 
-        if gMonsters <> nil then
-          for I := 0 to High(gMonsters) do
-            if gMonsters[I] <> nil then
-            begin
-              if (gMonsters[I].MonsterType = MONSTER_BARREL) then
-              begin
-                if (gMonsters[I].GameVelX <> 0) or (gMonsters[I].GameVelY <> 0) then
-                  MH_SEND_MonsterPos(gMonsters[I].UID);
-              end
-              else
-                if (gMonsters[I].MonsterState <> MONSTATE_SLEEP) then
-                  if (gMonsters[I].MonsterState <> MONSTATE_DEAD) or
-                     (gMonsters[I].GameVelX <> 0) or
-                     (gMonsters[I].GameVelY <> 0) then
-                  MH_SEND_MonsterPos(gMonsters[I].UID);
-            end;
+        g_Mons_ForEach(sendMonsPos);
 
         NetTimeToUpdate := 0;
       end;
@@ -1813,6 +1947,8 @@ begin
     UPSCounter := 0;
     UPSTime := Time;
   end;
+
+  if gGameOn then g_Weapon_AddDynLights();
 end;
 
 procedure g_Game_LoadData();
@@ -2305,6 +2441,28 @@ end;
 procedure DrawMinimap(p: TPlayer; RenderRect: e_graphics.TRect);
 var
   a, aX, aY, aX2, aY2, Scale, ScaleSz: Integer;
+
+  function monDraw (mon: TMonster): Boolean;
+  begin
+    result := false; // don't stop
+    with mon do
+    begin
+      if Live then
+      begin
+        // Ëåâûé âåðõíèé óãîë
+        aX := Obj.X div ScaleSz + 1;
+        aY := Obj.Y div ScaleSz + 1;
+        // Ðàçìåðû
+        aX2 := max(Obj.Rect.Width div ScaleSz, 1);
+        aY2 := max(Obj.Rect.Height div ScaleSz, 1);
+        // Ïðàâûé íèæíèé óãîë
+        aX2 := aX + aX2 - 1;
+        aY2 := aY + aY2 - 1;
+        e_DrawFillQuad(aX, aY, aX2, aY2, 255, 255, 0, 0);
+      end;
+    end;
+  end;
+
 begin
   if (gMapInfo.Width > RenderRect.Right - RenderRect.Left) or
      (gMapInfo.Height > RenderRect.Bottom - RenderRect.Top) then
@@ -2473,29 +2631,173 @@ begin
               end;
           end;
     end;
-    if gMonsters <> nil then
-    begin
-    // Ðèñóåì ìîíñòðîâ:
-      for a := 0 to High(gMonsters) do
-        if gMonsters[a] <> nil then with gMonsters[a] do
-          if Live then begin
-          // Ëåâûé âåðõíèé óãîë:
-            aX := Obj.X div ScaleSz + 1;
-            aY := Obj.Y div ScaleSz + 1;
-          // Ðàçìåðû:
-            aX2 := max(Obj.Rect.Width div ScaleSz, 1);
-            aY2 := max(Obj.Rect.Height div ScaleSz, 1);
-          // Ïðàâûé íèæíèé óãîë:
-            aX2 := aX + aX2 - 1;
-            aY2 := aY + aY2 - 1;
+    // Ðèñóåì ìîíñòðîâ
+    g_Mons_ForEach(monDraw);
+  end;
+end;
 
-            e_DrawFillQuad(aX, aY, aX2, aY2, 255, 255, 0, 0);
-          end;
+
+// setup sX, sY, sWidth, sHeight, and transformation matrix before calling this!
+procedure renderDynLightsInternal ();
+var
+  lln: Integer;
+  lx, ly, lrad: Integer;
+begin
+  //TODO: lights should be in separate grid, i think
+  //      but on the other side: grid may be slower for dynlights, as their lifetime is short
+  if not gwin_has_stencil or (g_dynLightCount < 1) then exit;
+
+  // setup OpenGL parameters
+  glStencilMask($FFFFFFFF);
+  glStencilFunc(GL_ALWAYS, 0, $FFFFFFFF);
+  glEnable(GL_STENCIL_TEST);
+  glEnable(GL_SCISSOR_TEST);
+  glClear(GL_STENCIL_BUFFER_BIT);
+  glStencilFunc(GL_EQUAL, 0, $ff);
+
+  for lln := 0 to g_dynLightCount-1 do
+  begin
+    lx := g_dynLights[lln].x;
+    ly := g_dynLights[lln].y;
+    lrad := g_dynLights[lln].radius;
+    if lrad < 3 then continue;
+
+    if lx-sX+lrad < 0 then continue;
+    if ly-sY+lrad < 0 then continue;
+    if lx-sX-lrad >= gPlayerScreenSize.X then continue;
+    if ly-sY-lrad >= gPlayerScreenSize.Y then continue;
+
+    // set scissor to optimize drawing
+    glScissor((lx-sX)-lrad+2, gPlayerScreenSize.Y-(ly-sY)-lrad-1+2, lrad*2-4, lrad*2-4);
+    // no need to clear stencil buffer, light blitting will do it for us
+    glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
+    // draw extruded panels
+    glDisable(GL_TEXTURE_2D);
+    glDisable(GL_BLEND);
+    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // no need to modify color buffer
+    if (lrad > 4) then g_Map_DrawPanelShadowVolumes(lx, ly, lrad);
+    // render light texture
+    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
+    glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); // draw light, and clear stencil buffer
+    // blend it
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+    glEnable(GL_TEXTURE_2D);
+    // color and opacity
+    glColor4f(g_dynLights[lln].r, g_dynLights[lln].g, g_dynLights[lln].b, g_dynLights[lln].a);
+    glBindTexture(GL_TEXTURE_2D, g_Texture_Light());
+    glBegin(GL_QUADS);
+      glTexCoord2f(0.0, 0.0); glVertex2i(lx-lrad, ly-lrad); // top-left
+      glTexCoord2f(1.0, 0.0); glVertex2i(lx+lrad, ly-lrad); // top-right
+      glTexCoord2f(1.0, 1.0); glVertex2i(lx+lrad, ly+lrad); // bottom-right
+      glTexCoord2f(0.0, 1.0); glVertex2i(lx-lrad, ly+lrad); // bottom-left
+    glEnd();
+  end;
+
+  // done
+  glDisable(GL_STENCIL_TEST);
+  glDisable(GL_BLEND);
+  glDisable(GL_SCISSOR_TEST);
+  glScissor(0, 0, sWidth, sHeight);
+end;
+
+
+// setup sX, sY, sWidth, sHeight, and transformation matrix before calling this!
+// WARNING! this WILL CALL `glTranslatef()`, but won't restore matrices!
+procedure renderMapInternal (backXOfs, backYOfs: Integer; transX, transY: Integer; setTransMatrix: Boolean);
+type
+  TDrawCB = procedure ();
+
+  procedure drawPanelType (profname: AnsiString; panType: DWord);
+  var
+    tagmask: Integer;
+    pan: TPanel;
+  begin
+    profileFrameDraw.sectionBegin(profname);
+    if gdbg_map_use_accel_render then
+    begin
+      tagmask := panelTypeToTag(panType);
+      {$IF TRUE}
+      while (gDrawPanelList.count > 0) do
+      begin
+        pan := TPanel(gDrawPanelList.front());
+        if ((pan.tag and tagmask) = 0) then break;
+        pan.Draw();
+        gDrawPanelList.popFront();
+      end;
+      {$ELSE}
+      e_WriteLog(Format('=== PANELS: %d ===', [gDrawPanelList.count]), MSG_NOTIFY);
+      while (gDrawPanelList.count > 0) do
+      begin
+        pan := TPanel(gDrawPanelList.front());
+        e_WriteLog(Format('tagmask: 0x%04x; pan.tag: 0x%04x; pan.ArrIdx: %d', [tagmask, pan.tag, pan.ArrIdx]), MSG_NOTIFY);
+        pan.Draw();
+        gDrawPanelList.popFront();
+      end;
+      {$ENDIF}
+    end
+    else
+    begin
+      g_Map_DrawPanels(panType);
     end;
+    profileFrameDraw.sectionEnd();
+  end;
+
+  procedure drawOther (profname: AnsiString; cb: TDrawCB);
+  begin
+    profileFrameDraw.sectionBegin(profname);
+    if assigned(cb) then cb();
+    profileFrameDraw.sectionEnd();
   end;
+
+begin
+  profileFrameDraw.sectionBegin('total');
+
+  // our accelerated renderer will collect all panels to gDrawPanelList
+  // we can use panel tag to render level parts (see GridTagXXX in g_map.pas)
+  profileFrameDraw.sectionBegin('collect');
+  if gdbg_map_use_accel_render then
+  begin
+    g_Map_CollectDrawPanels(sX, sY, sWidth, sHeight);
+  end;
+  profileFrameDraw.sectionEnd();
+
+  profileFrameDraw.sectionBegin('skyback');
+  g_Map_DrawBack(backXOfs, backYOfs);
+  profileFrameDraw.sectionEnd();
+
+  if (setTransMatrix) then glTranslatef(transX, transY, 0);
+
+  drawPanelType('*back', PANEL_BACK);
+  drawPanelType('*step', PANEL_STEP);
+  drawOther('items', @g_Items_Draw);
+  drawOther('weapons', @g_Weapon_Draw);
+  drawOther('shells', @g_Player_DrawShells);
+  drawOther('drawall', @g_Player_DrawAll);
+  drawOther('corpses', @g_Player_DrawCorpses);
+  drawPanelType('*wall', PANEL_WALL);
+  drawOther('monsters', @g_Monsters_Draw);
+  drawPanelType('*door', PANEL_CLOSEDOOR);
+  drawOther('gfx', @g_GFX_Draw);
+  drawOther('flags', @g_Map_DrawFlags);
+  drawPanelType('*acid1', PANEL_ACID1);
+  drawPanelType('*acid2', PANEL_ACID2);
+  drawPanelType('*water', PANEL_WATER);
+  drawOther('dynlights', @renderDynLightsInternal);
+  drawPanelType('*fore', PANEL_FORE);
+
+  if g_debug_HealthBar then
+  begin
+    g_Monsters_DrawHealth();
+    g_Player_DrawHealth();
+  end;
+
+  profileFrameDraw.mainEnd(); // map rendering
 end;
 
+
 procedure DrawMapView(x, y, w, h: Integer);
+
 var
   bx, by: Integer;
 begin
@@ -2503,44 +2805,23 @@ begin
 
   bx := Round(x/(gMapInfo.Width - w)*(gBackSize.X - w));
   by := Round(y/(gMapInfo.Height - h)*(gBackSize.Y - h));
-  g_Map_DrawBack(-bx, -by);
 
   sX := x;
   sY := y;
   sWidth := w;
   sHeight := h;
 
-  glTranslatef(-x, -y, 0);
-
-  g_Map_DrawPanels(PANEL_BACK);
-  g_Map_DrawPanels(PANEL_STEP);
-  g_Items_Draw();
-  g_Weapon_Draw();
-  g_Player_DrawShells();
-  g_Player_DrawAll();
-  g_Player_DrawCorpses();
-  g_Map_DrawPanels(PANEL_WALL);
-  g_Monsters_Draw();
-  g_Map_DrawPanels(PANEL_CLOSEDOOR);
-  g_GFX_Draw();
-  g_Map_DrawFlags();
-  g_Map_DrawPanels(PANEL_ACID1);
-  g_Map_DrawPanels(PANEL_ACID2);
-  g_Map_DrawPanels(PANEL_WATER);
-  g_Map_DrawPanels(PANEL_FORE);
-  if g_debug_HealthBar then
-  begin
-    g_Monsters_DrawHealth();
-    g_Player_DrawHealth();
-  end;
+  renderMapInternal(-bx, -by, -x, -y, true);
 
   glPopMatrix();
 end;
 
+
 procedure DrawPlayer(p: TPlayer);
 var
   px, py, a, b, c, d: Integer;
   //R: TRect;
+
 begin
   if (p = nil) or (p.FDummy) then
   begin
@@ -2550,6 +2831,9 @@ begin
     Exit;
   end;
 
+  if (profileFrameDraw = nil) then profileFrameDraw := TProfiler.Create('RENDER', g_profile_history_size);
+  profileFrameDraw.mainBegin(g_profile_frame_draw);
+
   gPlayerDrawn := p;
 
   glPushMatrix();
@@ -2557,96 +2841,60 @@ begin
   px := p.GameX + PLAYER_RECT_CX;
   py := p.GameY + PLAYER_RECT_CY;
 
-  if px > (gPlayerScreenSize.X div 2) then
-    a := -px + (gPlayerScreenSize.X div 2)
-  else
-    a := 0;
-  if py > (gPlayerScreenSize.Y div 2) then
-    b := -py + (gPlayerScreenSize.Y div 2)
-  else
-    b := 0;
-  if px > (gMapInfo.Width - (gPlayerScreenSize.X div 2)) then
-    a := -gMapInfo.Width + gPlayerScreenSize.X;
-  if py > (gMapInfo.Height - (gPlayerScreenSize.Y div 2)) then
-    b := -gMapInfo.Height + gPlayerScreenSize.Y;
-  if gMapInfo.Width <= gPlayerScreenSize.X then
-    a := 0;
-  if gMapInfo.Height <= gPlayerScreenSize.Y then
-    b := 0;
+  if px > (gPlayerScreenSize.X div 2) then a := -px+(gPlayerScreenSize.X div 2) else a := 0;
+  if py > (gPlayerScreenSize.Y div 2) then b := -py+(gPlayerScreenSize.Y div 2) else b := 0;
+
+  if px > gMapInfo.Width-(gPlayerScreenSize.X div 2) then a := -gMapInfo.Width+gPlayerScreenSize.X;
+  if py > gMapInfo.Height-(gPlayerScreenSize.Y div 2) then b := -gMapInfo.Height+gPlayerScreenSize.Y;
+
+  if gMapInfo.Width <= gPlayerScreenSize.X then a := 0;
+  if gMapInfo.Height <= gPlayerScreenSize.Y then b := 0;
 
   if p.IncCam <> 0 then
   begin
-    if py > (gMapInfo.Height - (gPlayerScreenSize.Y div 2)) then
+    if py > gMapInfo.Height-(gPlayerScreenSize.Y div 2) then
     begin
       if p.IncCam > 120-(py-(gMapInfo.Height-(gPlayerScreenSize.Y div 2))) then
+      begin
         p.IncCam := 120-(py-(gMapInfo.Height-(gPlayerScreenSize.Y div 2)));
+      end;
     end;
 
-    if py < (gPlayerScreenSize.Y div 2) then
+    if py < gPlayerScreenSize.Y div 2 then
     begin
       if p.IncCam < -120+((gPlayerScreenSize.Y div 2)-py) then
+      begin
         p.IncCam := -120+((gPlayerScreenSize.Y div 2)-py);
+      end;
     end;
 
     if p.IncCam < 0 then
-      while (py+(gPlayerScreenSize.Y div 2)-p.IncCam > gMapInfo.Height) and
-            (p.IncCam < 0) do
-        p.IncCam := p.IncCam + 1;
+    begin
+      while (py+(gPlayerScreenSize.Y div 2)-p.IncCam > gMapInfo.Height) and (p.IncCam < 0) do p.IncCam := p.IncCam+1; //Inc(p.IncCam);
+    end;
 
     if p.IncCam > 0 then
-      while (py-(gPlayerScreenSize.Y div 2)-p.IncCam < 0) and
-            (p.IncCam > 0) do
-        p.IncCam := p.IncCam - 1;
+    begin
+      while (py-(gPlayerScreenSize.Y div 2)-p.IncCam < 0) and (p.IncCam > 0) do p.IncCam := p.IncCam-1; //Dec(p.IncCam);
+    end;
   end;
 
-  if (px< gPlayerScreenSize.X div 2) or
-     (gMapInfo.Width-gPlayerScreenSize.X <= 256) then
-    c := 0
-  else
-    if (px > gMapInfo.Width-(gPlayerScreenSize.X div 2)) then
-      c := gBackSize.X - gPlayerScreenSize.X
-    else
-      c := Round((px-(gPlayerScreenSize.X div 2))/(gMapInfo.Width-gPlayerScreenSize.X)*(gBackSize.X-gPlayerScreenSize.X));
+       if (px < gPlayerScreenSize.X div 2) or (gMapInfo.Width-gPlayerScreenSize.X <= 256) then c := 0
+  else if (px > gMapInfo.Width-(gPlayerScreenSize.X div 2)) then c := gBackSize.X-gPlayerScreenSize.X
+  else c := round((px-(gPlayerScreenSize.X div 2))/(gMapInfo.Width-gPlayerScreenSize.X)*(gBackSize.X-gPlayerScreenSize.X));
 
-  if (py-p.IncCam <= gPlayerScreenSize.Y div 2) or
-     (gMapInfo.Height-gPlayerScreenSize.Y <= 256) then
-    d := 0
-  else
-    if (py-p.IncCam >= gMapInfo.Height-(gPlayerScreenSize.Y div 2)) then
-      d := gBackSize.Y - gPlayerScreenSize.Y
-    else
-      d := Round((py-p.IncCam-(gPlayerScreenSize.Y div 2))/(gMapInfo.Height-gPlayerScreenSize.Y)*(gBackSize.Y-gPlayerScreenSize.Y));
-
-  g_Map_DrawBack(-c, -d);
+       if (py-p.IncCam <= gPlayerScreenSize.Y div 2) or (gMapInfo.Height-gPlayerScreenSize.Y <= 256) then d := 0
+  else if (py-p.IncCam >= gMapInfo.Height-(gPlayerScreenSize.Y div 2)) then d := gBackSize.Y-gPlayerScreenSize.Y
+  else d := round((py-p.IncCam-(gPlayerScreenSize.Y div 2))/(gMapInfo.Height-gPlayerScreenSize.Y)*(gBackSize.Y-gPlayerScreenSize.Y));
 
   sX := -a;
   sY := -(b+p.IncCam);
   sWidth := gPlayerScreenSize.X;
   sHeight := gPlayerScreenSize.Y;
 
-  glTranslatef(a, b+p.IncCam, 0);
-
-  g_Map_DrawPanels(PANEL_BACK);
-  g_Map_DrawPanels(PANEL_STEP);
-  g_Items_Draw();
-  g_Weapon_Draw();
-  g_Player_DrawShells();
-  g_Player_DrawAll();
-  g_Player_DrawCorpses();
-  g_Map_DrawPanels(PANEL_WALL);
-  g_Monsters_Draw();
-  g_Map_DrawPanels(PANEL_CLOSEDOOR);
-  g_GFX_Draw();
-  g_Map_DrawFlags();
-  g_Map_DrawPanels(PANEL_ACID1);
-  g_Map_DrawPanels(PANEL_ACID2);
-  g_Map_DrawPanels(PANEL_WATER);
-  g_Map_DrawPanels(PANEL_FORE);
-  if g_debug_HealthBar then
-  begin
-    g_Monsters_DrawHealth();
-    g_Player_DrawHealth();
-  end;
+  //glTranslatef(a, b+p.IncCam, 0);
+
+  renderMapInternal(-c, -d, a, b+p.IncCam, true);
 
   if p.FSpectator then
     e_TextureFontPrintEx(p.GameX + PLAYER_RECT_CX - 4,
@@ -2681,6 +2929,14 @@ begin
   p.DrawGUI();
 end;
 
+procedure drawProfilers ();
+var
+  px: Integer = -1;
+begin
+  if g_profile_frame_draw then px := px-drawProfiles(px, -1, profileFrameDraw);
+  if g_profile_collision then px := px-drawProfiles(px, -1, profMapCollision);
+end;
+
 procedure g_Game_Draw();
 var
   ID: DWORD;
@@ -3002,6 +3258,8 @@ begin
   e_TextureFontPrint(gScreenWidth-72, 0,
                      Format('%d:%.2d:%.2d', [gTime div 1000 div 3600, (gTime div 1000 div 60) mod 60, gTime div 1000 mod 60]),
                      gStdFont);
+
+  if gGameOn then drawProfilers();
 end;
 
 procedure g_Game_Quit();
@@ -3482,6 +3740,7 @@ var
   State: Byte;
   OuterLoop: Boolean;
   newResPath: string;
+  InMsg: TMsg;
 begin
   g_Game_Free();
 
@@ -3529,27 +3788,28 @@ begin
       if (NetEvent.kind = ENET_EVENT_TYPE_RECEIVE) then
       begin
         Ptr := NetEvent.packet^.data;
-        e_Raw_Seek(0);
+        if not InMsg.Init(Ptr, NetEvent.packet^.dataLength, True) then
+          continue;
 
-        MID := e_Raw_Read_Byte(Ptr);
+        MID := InMsg.ReadByte();
 
         if (MID = NET_MSG_INFO) and (State = 0) then
         begin
-          NetMyID := e_Raw_Read_Byte(Ptr);
-          NetPlrUID1 := e_Raw_Read_Word(Ptr);
+          NetMyID := InMsg.ReadByte();
+          NetPlrUID1 := InMsg.ReadWord();
 
-          WadName := e_Raw_Read_String(Ptr);
-          Map := e_Raw_Read_String(Ptr);
+          WadName := InMsg.ReadString();
+          Map := InMsg.ReadString();
 
-          gWADHash := e_Raw_Read_MD5(Ptr);
+          gWADHash := InMsg.ReadMD5();
 
-          gGameSettings.GameMode := e_Raw_Read_Byte(Ptr);
+          gGameSettings.GameMode := InMsg.ReadByte();
           gSwitchGameMode := gGameSettings.GameMode;
-          gGameSettings.GoalLimit := e_Raw_Read_Word(Ptr);
-          gGameSettings.TimeLimit := e_Raw_Read_Word(Ptr);
-          gGameSettings.MaxLives := e_Raw_Read_Byte(Ptr);
-          gGameSettings.Options := e_Raw_Read_LongWord(Ptr);
-          T := e_Raw_Read_LongWord(Ptr);
+          gGameSettings.GoalLimit := InMsg.ReadWord();
+          gGameSettings.TimeLimit := InMsg.ReadWord();
+          gGameSettings.MaxLives := InMsg.ReadByte();
+          gGameSettings.Options := InMsg.ReadLongWord();
+          T := InMsg.ReadLongWord();
 
           newResPath := g_Res_SearchSameWAD(MapsDir, WadName, gWADHash);
           if newResPath = '' then
@@ -3910,6 +4170,13 @@ end;
 procedure g_Game_RestartRound(NoMapRestart: Boolean = False);
 var
   i, n, nb, nr: Integer;
+
+  function monRespawn (mon: TMonster): Boolean;
+  begin
+    result := false; // don't stop
+    if not mon.FNoRespawn then mon.Respawn();
+  end;
+
 begin
   if not g_Game_IsServer then Exit;
   if gLMSRespawn = LMS_RESPAWN_NONE then Exit;
@@ -3976,25 +4243,10 @@ begin
       gPlayer2 := g_Player_Get(gLMSPID2);
   end;
 
-  for i := Low(gItems) to High(gItems) do
-  begin
-    if gItems[i].Respawnable then
-    begin
-      gItems[i].QuietRespawn := True;
-      gItems[i].RespawnTime := 0;
-    end
-    else
-    begin
-      g_Items_Remove(i);
-      if g_Game_IsNet then MH_SEND_ItemDestroy(True, i);
-    end;
-  end;
+  g_Items_RestartRound();
 
-  for i := Low(gMonsters) to High(gMonsters) do
-  begin
-    if (gMonsters[i] <> nil) and not gMonsters[i].FNoRespawn then
-      gMonsters[i].Respawn;
-  end;
+
+  g_Mons_ForEach(monRespawn);
 
   gLMSSoftSpawn := False;
 end;
@@ -4663,11 +4915,120 @@ begin
   end;
 end;
 
+// profiler console commands
+procedure ProfilerCommands (P: SArray);
+var
+  cmd: string;
+
+  function getBool (idx: Integer): Integer;
+  begin
+    if (idx < 0) or (idx > High(P)) then begin result := -1; exit; end;
+    result := 0;
+    if (P[idx] = '1') or (P[idx] = 'on') or (P[idx] = 'true') or (P[idx] = 'tan') then result := 1;
+  end;
+
+begin
+  //if not gDebugMode then exit;
+  cmd := LowerCase(P[0]);
+
+  if cmd = 'pf_draw_frame' then
+  begin
+    g_profile_frame_draw := not g_profile_frame_draw;
+    exit;
+  end;
+
+  if cmd = 'pf_update_frame' then
+  begin
+    g_profile_frame_update := not g_profile_frame_update;
+    exit;
+  end;
+
+  if cmd = 'pf_coldet' then
+  begin
+    g_profile_collision := not g_profile_collision;
+    exit;
+  end;
+
+  if cmd = 'r_sq_draw' then
+  begin
+    case getBool(1) of
+      -1: begin end;
+       0: gdbg_map_use_accel_render := false;
+       1: gdbg_map_use_accel_render := true;
+    end;
+    if gdbg_map_use_accel_render then g_Console_Add('accelerated rendering: tan') else g_Console_Add('accelerated rendering: ona');
+    exit;
+  end;
+
+  if cmd = 'cd_sq_enabled' then
+  begin
+    case getBool(1) of
+      -1: begin end;
+       0: gdbg_map_use_accel_coldet := false;
+       1: gdbg_map_use_accel_coldet := true;
+    end;
+    if gdbg_map_use_accel_coldet then g_Console_Add('accelerated coldet: tan') else g_Console_Add('accelerated coldet: ona');
+    exit;
+  end;
+
+  {
+  if (cmd = 'sq_use_grid') or (cmd = 'sq_use_tree') then
+  begin
+    gdbg_map_use_tree_coldet := (cmd = 'sq_use_tree');
+    if gdbg_map_use_tree_coldet then g_Console_Add('coldet acceleration: tree') else g_Console_Add('coldet acceleration: grid');
+    exit;
+  end;
+
+  if (cmd = 'r_sq_use_grid') or (cmd = 'r_sq_use_tree') then
+  begin
+    gdbg_map_use_tree_draw := (cmd = 'r_sq_use_tree');
+    if gdbg_map_use_tree_draw then g_Console_Add('render acceleration: tree') else g_Console_Add('render acceleration: grid');
+    exit;
+  end;
+  }
+
+  {
+  if (cmd = 't_dump_node_queries') then
+  begin
+    case getBool(1) of
+      -1: begin end;
+       0: gdbg_map_dump_coldet_tree_queries := false;
+       1: gdbg_map_dump_coldet_tree_queries := true;
+    end;
+    if gdbg_map_dump_coldet_tree_queries then g_Console_Add('grid coldet tree queries: tan') else g_Console_Add('grid coldet tree queries: ona');
+    exit;
+  end;
+  }
+
+  if (cmd = 'mon_sq_enabled') then
+  begin
+    case getBool(1) of
+      -1: begin end;
+       0: gmon_debug_use_sqaccel := false;
+       1: gmon_debug_use_sqaccel := true;
+    end;
+    if gmon_debug_use_sqaccel then g_Console_Add('accelerated monster coldet: tan') else g_Console_Add('accelerated monster coldet: ona');
+    exit;
+  end;
+
+  if (cmd = 'wtrace_sq_enabled') then
+  begin
+    case getBool(1) of
+      -1: begin end;
+       0: gwep_debug_fast_trace := false;
+       1: gwep_debug_fast_trace := true;
+    end;
+    if gwep_debug_fast_trace then g_Console_Add('accelerated weapon hitscan: tan') else g_Console_Add('accelerated weapon hitscan: ona');
+    exit;
+  end;
+end;
+
 procedure DebugCommands(P: SArray);
 var
   a, b: Integer;
   cmd: string;
   //pt: TPoint;
+  mon: TMonster;
 begin
 // Êîìàíäû îòëàäî÷íîãî ðåæèìà:
   if gDebugMode then
@@ -4745,12 +5106,14 @@ begin
           else
             begin
               with gPlayer1.Obj do
-                b := g_Monsters_Create(a,
+              begin
+                mon := g_Monsters_Create(a,
                      X + Rect.X + (Rect.Width div 2),
                      Y + Rect.Y + Rect.Height,
                      gPlayer1.Direction, True);
-              if (Length(P) > 2) and (b >= 0) then
-                gMonsters[b].MonsterBehaviour := Min(Max(StrToIntDef(P[2], BH_NORMAL), BH_NORMAL), BH_GOOD);
+              end;
+              if (Length(P) > 2) and (mon <> nil) then
+                mon.MonsterBehaviour := Min(Max(StrToIntDef(P[2], BH_NORMAL), BH_NORMAL), BH_GOOD);
             end;
         end;
     end
@@ -6485,6 +6848,12 @@ var
 begin
   Parse_Params(pars);
 
+  s := Find_Param_Value(pars, '--profile-render');
+  if (s <> '') then g_profile_frame_draw := true;
+
+  s := Find_Param_Value(pars, '--profile-coldet');
+  if (s <> '') then g_profile_collision := true;
+
 // Debug mode:
   s := Find_Param_Value(pars, '--debug');
   if (s <> '') then
@@ -6510,6 +6879,18 @@ begin
     Exit;
   end;
 
+  s := LowerCase(Find_Param_Value(pars, '-dbg-mainwad'));
+  if (s <> '') then
+  begin
+    gDefaultMegawadStart := s;
+  end;
+
+  if (Find_Param_Value(pars, '--dbg-mainwad-restore') <> '') or
+     (Find_Param_Value(pars, '--dbg-mainwad-default') <> '') then
+  begin
+    gDefaultMegawadStart := DF_Default_Megawad_Start;
+  end;
+
 // Start map when game loads:
   map := LowerCase(Find_Param_Value(pars, '-map'));
   if isWadPath(map) then