DEADSOFTWARE

`conwriteln()` and `conwritefln()` API
[d2df-sdl.git] / src / game / g_console.pas
index 6373d0dc8d04bb3f44e9cca29b230d2be8e1a2b9..0668507b333bf73cd52bfb3e763705adddbc115d 100644 (file)
@@ -29,6 +29,9 @@ procedure g_Console_Add(L: String; Show: Boolean = False);
 procedure g_Console_Clear();
 function  g_Console_CommandBlacklisted(C: String): Boolean;
 
+procedure conwriteln (const s: AnsiString; show: Boolean=false);
+procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
+
 procedure g_Console_Chat_Switch(Team: Boolean = False);
 
 var
@@ -44,7 +47,7 @@ implementation
 uses
   g_textures, g_main, e_graphics, e_input, g_game,
   SysUtils, g_basic, g_options, wadreader, Math,
-  g_menu, g_language, g_net, g_netmsg, e_log, conbuf;
+  g_menu, g_language, g_net, g_netmsg, e_log, conbuf, utils;
 
 type
   TCmdProc = procedure (P: SArray);
@@ -52,6 +55,8 @@ type
   TCommand = record
     Cmd: String;
     Proc: TCmdProc;
+    help: String;
+    hidden: Boolean;
   end;
 
   TAlias = record
@@ -157,7 +162,16 @@ begin
     g_Console_Add('');
     g_Console_Add('Commands list:');
     for a := High(Commands) downto 0 do
-      g_Console_Add('  '+Commands[a].Cmd);
+    begin
+      if (Length(Commands[a].help) > 0) then
+      begin
+        g_Console_Add('  '+Commands[a].Cmd+' -- '+Commands[a].help);
+      end
+      else
+      begin
+        g_Console_Add('  '+Commands[a].Cmd);
+      end;
+    end;
   end;
 
   if Cmd = 'time' then
@@ -337,7 +351,7 @@ begin
   Whitelist[a] := LowerCase(Cmd);
 end;
 
-procedure AddCommand(Cmd: String; Proc: TCmdProc);
+procedure AddCommand(Cmd: String; Proc: TCmdProc; ahelp: String=''; ahidden: Boolean=false);
 var
   a: Integer;
 begin
@@ -345,6 +359,8 @@ begin
   a := High(Commands);
   Commands[a].Cmd := LowerCase(Cmd);
   Commands[a].Proc := Proc;
+  Commands[a].hidden := ahidden;
+  Commands[a].help := ahelp;
 end;
 
 procedure g_Console_Init();
@@ -365,7 +381,7 @@ begin
       Time := 0;
     end;
 
-  AddCommand('clear', ConsoleCommands);
+  AddCommand('clear', ConsoleCommands, 'clear console');
   AddCommand('clearhistory', ConsoleCommands);
   AddCommand('showhistory', ConsoleCommands);
   AddCommand('commands', ConsoleCommands);
@@ -388,20 +404,21 @@ begin
   AddCommand('d_player', DebugCommands);
   AddCommand('d_joy', DebugCommands);
 
-  AddCommand('pf_draw_frame', ProfilerCommands);
-  AddCommand('pf_update_frame', ProfilerCommands);
-  AddCommand('pf_coldet', ProfilerCommands);
-  AddCommand('r_sq_draw', ProfilerCommands);
-  AddCommand('r_sq_use_grid', ProfilerCommands);
-  AddCommand('r_sq_use_tree', ProfilerCommands);
-  AddCommand('dbg_sq_coldet', ProfilerCommands);
+  AddCommand('pf_draw_frame', ProfilerCommands, 'draw frame rendering profiles');
+  //AddCommand('pf_update_frame', ProfilerCommands);
+  AddCommand('pf_coldet', ProfilerCommands, 'draw collision detection profiles');
+  AddCommand('pf_los', ProfilerCommands, 'draw monster LOS profiles');
+  AddCommand('r_sq_draw', ProfilerCommands, 'accelerated spatial queries in rendering');
+  AddCommand('cd_sq_enabled', ProfilerCommands, 'accelerated spatial queries in map coldet');
+  AddCommand('mon_sq_enabled', ProfilerCommands, 'use accelerated spatial queries for monsters');
+  AddCommand('wtrace_sq_enabled', ProfilerCommands, 'use accelerated weapon hitscan trace');
 
-  AddCommand('t_dump_node_queries', ProfilerCommands);
+  AddCommand('pr_enabled', ProfilerCommands, 'enable/disable particles');
+  AddCommand('pr_phys_enabled', ProfilerCommands, 'enable/disable particle physics');
+  AddCommand('los_enabled', ProfilerCommands, 'enable/disable LOS calculations');
 
-  AddCommand('sq_use_grid', ProfilerCommands);
-  AddCommand('sq_use_tree', ProfilerCommands);
-
-  AddCommand('mon_sq_enabled', ProfilerCommands);
+  AddCommand('mon_think', ProfilerCommands, 'enable/disable monster thinking');
+  AddCommand('dbg_holmes', ProfilerCommands, 'turn Holmes on/off');
 
   AddCommand('p1_name', GameCVars);
   AddCommand('p2_name', GameCVars);
@@ -730,6 +747,7 @@ end;
 
 var
   tcomplist: array of string = nil;
+  tcompidx: array of Integer = nil;
 
 procedure Complete ();
 var
@@ -742,9 +760,16 @@ begin
     g_Console_Add('');
     for i := 0 to High(Commands) do
     begin
-      if (Commands[i].Cmd <> 'goobers') then
+      if not Commands[i].hidden then
       begin
-        g_Console_Add('  '+Commands[i].Cmd);
+        if (Length(Commands[i].help) > 0) then
+        begin
+          g_Console_Add('  '+Commands[i].Cmd+' -- '+Commands[i].help);
+        end
+        else
+        begin
+          g_Console_Add('  '+Commands[i].Cmd);
+        end;
       end;
     end;
     exit;
@@ -753,16 +778,38 @@ begin
   ll := LowerCase(Line);
   lpfx := '';
 
+  if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
+  begin
+    ll := Copy(ll, 0, Length(ll)-1);
+    for i := 0 to High(Commands) do
+    begin
+      if Commands[i].hidden then continue;
+      if (Commands[i].Cmd = ll) then
+      begin
+        if (Length(Commands[i].help) > 0) then
+        begin
+          g_Console_Add('  '+Commands[i].Cmd+' -- '+Commands[i].help);
+        end;
+      end;
+    end;
+    exit;
+  end;
+
   // build completion list
   tused := 0;
   for i := 0 to High(Commands) do
   begin
+    if Commands[i].hidden then continue;
     cmd := Commands[i].Cmd;
-    if (cmd = 'goobers') then continue;
     if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
     begin
-      if (tused = Length(tcomplist)) then SetLength(tcomplist, Length(tcomplist)+128);
+      if (tused = Length(tcomplist)) then
+      begin
+        SetLength(tcomplist, Length(tcomplist)+128);
+        SetLength(tcompidx, Length(tcompidx)+128);
+      end;
       tcomplist[tused] := cmd;
+      tcompidx[tused] := i;
       Inc(tused);
       if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
     end;
@@ -797,7 +844,17 @@ begin
     else
     begin
       g_Console_Add('');
-      for i := 0 to tused-1 do g_Console_Add('  '+tcomplist[i]);
+      for i := 0 to tused-1 do
+      begin
+        if (Length(Commands[tcompidx[i]].help) > 0) then
+        begin
+          g_Console_Add('  '+tcomplist[i]+' -- '+Commands[tcompidx[i]].help);
+        end
+        else
+        begin
+          g_Console_Add('  '+tcomplist[i]);
+        end;
+      end;
     end;
   end;
 end;
@@ -989,6 +1046,58 @@ begin
   *)
 end;
 
+
+var
+  consolewriterLastWasEOL: Boolean = false;
+
+procedure consolewriter (constref buf; len: SizeUInt);
+var
+  b: PByte;
+begin
+  if (len < 1) then exit;
+  b := PByte(@buf);
+  consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
+  while (len > 0) do
+  begin
+    if (b[0] <> 13) and (b[0] <> 10) then
+    begin
+      cbufPut(Char(b[0]));
+    end
+    else
+    begin
+      if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
+      cbufPut(#10);
+    end;
+    len -= 1;
+    b += 1;
+  end;
+end;
+
+
+// returns formatted string if `writerCB` is `nil`, empty string otherwise
+//function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
+//TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
+procedure conwriteln (const s: AnsiString; show: Boolean=false);
+begin
+  g_Console_Add(s, show);
+end;
+
+
+procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
+begin
+  if show then
+  begin
+    g_Console_Add(formatstrf(s, args), true);
+  end
+  else
+  begin
+    consolewriterLastWasEOL := false;
+    formatstrf(s, args, consolewriter);
+    if not consolewriterLastWasEOL then cbufPut(#10);
+  end;
+end;
+
+
 procedure g_Console_Clear();
 begin
   //ConsoleHistory := nil;