DEADSOFTWARE

`conwriteln()` and `conwritefln()` API
[d2df-sdl.git] / src / game / g_console.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_console;
19 interface
21 procedure g_Console_Init();
22 procedure g_Console_Update();
23 procedure g_Console_Draw();
24 procedure g_Console_Switch();
25 procedure g_Console_Char(C: Char);
26 procedure g_Console_Control(K: Word);
27 procedure g_Console_Process(L: String; Quiet: Boolean = False);
28 procedure g_Console_Add(L: String; Show: Boolean = False);
29 procedure g_Console_Clear();
30 function g_Console_CommandBlacklisted(C: String): Boolean;
32 procedure conwriteln (const s: AnsiString; show: Boolean=false);
33 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
35 procedure g_Console_Chat_Switch(Team: Boolean = False);
37 var
38 gConsoleShow: Boolean; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
39 gChatShow: Boolean;
40 gChatTeam: Boolean = False;
41 gAllowConsoleMessages: Boolean = True;
42 gChatEnter: Boolean = True;
43 gJustChatted: Boolean = False; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
45 implementation
47 uses
48 g_textures, g_main, e_graphics, e_input, g_game,
49 SysUtils, g_basic, g_options, wadreader, Math,
50 g_menu, g_language, g_net, g_netmsg, e_log, conbuf, utils;
52 type
53 TCmdProc = procedure (P: SArray);
55 TCommand = record
56 Cmd: String;
57 Proc: TCmdProc;
58 help: String;
59 hidden: Boolean;
60 end;
62 TAlias = record
63 Name: String;
64 Commands: SArray;
65 end;
67 const
68 Step = 32;
69 Alpha = 25;
70 MsgTime = 144;
71 MaxScriptRecursion = 16;
73 DEBUG_STRING = 'DEBUG MODE';
75 var
76 ID: DWORD;
77 RecursionDepth: Word = 0;
78 RecursionLimitHit: Boolean = False;
79 Cons_Y: SmallInt;
80 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
81 Line: String;
82 CPos: Word;
83 //ConsoleHistory: SArray;
84 CommandHistory: SArray;
85 Whitelist: SArray;
86 Commands: Array of TCommand;
87 Aliases: Array of TAlias;
88 CmdIndex: Word;
89 conSkipLines: Integer = 0;
90 MsgArray: Array [0..4] of record
91 Msg: String;
92 Time: Word;
93 end;
95 function GetStrACmd(var Str: String): String;
96 var
97 a: Integer;
98 begin
99 Result := '';
100 for a := 1 to Length(Str) do
101 if (a = Length(Str)) or (Str[a+1] = ';') then
102 begin
103 Result := Copy(Str, 1, a);
104 Delete(Str, 1, a+1);
105 Str := Trim(Str);
106 Exit;
107 end;
108 end;
110 function ParseAlias(Str: String): SArray;
111 begin
112 Result := nil;
114 Str := Trim(Str);
116 if Str = '' then
117 Exit;
119 while Str <> '' do
120 begin
121 SetLength(Result, Length(Result)+1);
122 Result[High(Result)] := GetStrACmd(Str);
123 end;
124 end;
126 procedure ConsoleCommands(P: SArray);
127 var
128 Cmd, s: String;
129 a, b: Integer;
130 F: TextFile;
131 begin
132 Cmd := LowerCase(P[0]);
133 s := '';
135 if Cmd = 'clear' then
136 begin
137 //ConsoleHistory := nil;
138 cbufClear();
139 conSkipLines := 0;
141 for a := 0 to High(MsgArray) do
142 with MsgArray[a] do
143 begin
144 Msg := '';
145 Time := 0;
146 end;
147 end;
149 if Cmd = 'clearhistory' then
150 CommandHistory := nil;
152 if Cmd = 'showhistory' then
153 if CommandHistory <> nil then
154 begin
155 g_Console_Add('');
156 for a := 0 to High(CommandHistory) do
157 g_Console_Add(' '+CommandHistory[a]);
158 end;
160 if Cmd = 'commands' then
161 begin
162 g_Console_Add('');
163 g_Console_Add('Commands list:');
164 for a := High(Commands) downto 0 do
165 begin
166 if (Length(Commands[a].help) > 0) then
167 begin
168 g_Console_Add(' '+Commands[a].Cmd+' -- '+Commands[a].help);
169 end
170 else
171 begin
172 g_Console_Add(' '+Commands[a].Cmd);
173 end;
174 end;
175 end;
177 if Cmd = 'time' then
178 g_Console_Add(TimeToStr(Now), True);
180 if Cmd = 'date' then
181 g_Console_Add(DateToStr(Now), True);
183 if Cmd = 'echo' then
184 if Length(P) > 1 then
185 begin
186 if P[1] = 'ololo' then
187 gCheats := True
188 else
189 begin
190 s := '';
191 for a := 1 to High(P) do
192 s := s + P[a] + ' ';
193 g_Console_Add(b_Text_Format(s), True);
194 end;
195 end
196 else
197 g_Console_Add('');
199 if Cmd = 'dump' then
200 begin
201 (*
202 if ConsoleHistory <> nil then
203 begin
204 if Length(P) > 1 then
205 s := P[1]
206 else
207 s := GameDir+'/console.txt';
209 {$I-}
210 AssignFile(F, s);
211 Rewrite(F);
212 if IOResult <> 0 then
213 begin
214 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
215 CloseFile(F);
216 Exit;
217 end;
219 for a := 0 to High(ConsoleHistory) do
220 WriteLn(F, ConsoleHistory[a]);
222 CloseFile(F);
223 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
224 {$I+}
225 end;
226 *)
227 end;
229 if Cmd = 'exec' then
230 begin
231 // exec <filename>
232 if Length(P) > 1 then
233 begin
234 s := GameDir+'/'+P[1];
236 {$I-}
237 AssignFile(F, s);
238 Reset(F);
239 if IOResult <> 0 then
240 begin
241 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
242 CloseFile(F);
243 Exit;
244 end;
245 g_Console_Add(Format(_lc[I_CONSOLE_EXEC], [s]));
247 while not EOF(F) do
248 begin
249 ReadLn(F, s);
250 if IOResult <> 0 then
251 begin
252 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
253 CloseFile(F);
254 Exit;
255 end;
256 if Pos('#', s) <> 1 then // script comment
257 begin
258 // prevents endless loops
259 Inc(RecursionDepth);
260 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
261 if not RecursionLimitHit then
262 g_Console_Process(s, True);
263 Dec(RecursionDepth);
264 end;
265 end;
266 if (RecursionDepth = 0) and RecursionLimitHit then
267 begin
268 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
269 RecursionLimitHit := False;
270 end;
272 CloseFile(F);
273 {$I+}
274 end
275 else
276 g_Console_Add('exec <script file>');
277 end;
279 if Cmd = 'alias' then
280 begin
281 // alias [alias_name] [commands]
282 if Length(P) > 1 then
283 begin
284 for a := 0 to High(Aliases) do
285 if Aliases[a].Name = P[1] then
286 begin
287 if Length(P) > 2 then
288 Aliases[a].Commands := ParseAlias(P[2])
289 else
290 for b := 0 to High(Aliases[a].Commands) do
291 g_Console_Add(Aliases[a].Commands[b]);
292 Exit;
293 end;
294 SetLength(Aliases, Length(Aliases)+1);
295 a := High(Aliases);
296 Aliases[a].Name := P[1];
297 if Length(P) > 2 then
298 Aliases[a].Commands := ParseAlias(P[2])
299 else
300 for b := 0 to High(Aliases[a].Commands) do
301 g_Console_Add(Aliases[a].Commands[b]);
302 end else
303 for a := 0 to High(Aliases) do
304 if Aliases[a].Commands <> nil then
305 g_Console_Add(Aliases[a].Name);
306 end;
308 if Cmd = 'call' then
309 begin
310 // call <alias_name>
311 if Length(P) > 1 then
312 begin
313 if Aliases = nil then
314 Exit;
315 for a := 0 to High(Aliases) do
316 if Aliases[a].Name = P[1] then
317 begin
318 if Aliases[a].Commands <> nil then
319 begin
320 // with this system proper endless loop detection seems either impossible
321 // or very dirty to implement, so let's have this instead
322 // prevents endless loops
323 for b := 0 to High(Aliases[a].Commands) do
324 begin
325 Inc(RecursionDepth);
326 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
327 if not RecursionLimitHit then
328 g_Console_Process(Aliases[a].Commands[b], True);
329 Dec(RecursionDepth);
330 end;
331 if (RecursionDepth = 0) and RecursionLimitHit then
332 begin
333 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
334 RecursionLimitHit := False;
335 end;
336 end;
337 Exit;
338 end;
339 end
340 else
341 g_Console_Add('call <alias name>');
342 end;
343 end;
345 procedure WhitelistCommand(Cmd: string);
346 var
347 a: Integer;
348 begin
349 SetLength(Whitelist, Length(Whitelist)+1);
350 a := High(Whitelist);
351 Whitelist[a] := LowerCase(Cmd);
352 end;
354 procedure AddCommand(Cmd: String; Proc: TCmdProc; ahelp: String=''; ahidden: Boolean=false);
355 var
356 a: Integer;
357 begin
358 SetLength(Commands, Length(Commands)+1);
359 a := High(Commands);
360 Commands[a].Cmd := LowerCase(Cmd);
361 Commands[a].Proc := Proc;
362 Commands[a].hidden := ahidden;
363 Commands[a].help := ahelp;
364 end;
366 procedure g_Console_Init();
367 var
368 a: Integer;
369 begin
370 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
371 Cons_Y := -(gScreenHeight div 2);
372 gConsoleShow := False;
373 gChatShow := False;
374 Cons_Shown := False;
375 CPos := 1;
377 for a := 0 to High(MsgArray) do
378 with MsgArray[a] do
379 begin
380 Msg := '';
381 Time := 0;
382 end;
384 AddCommand('clear', ConsoleCommands, 'clear console');
385 AddCommand('clearhistory', ConsoleCommands);
386 AddCommand('showhistory', ConsoleCommands);
387 AddCommand('commands', ConsoleCommands);
388 AddCommand('time', ConsoleCommands);
389 AddCommand('date', ConsoleCommands);
390 AddCommand('echo', ConsoleCommands);
391 AddCommand('dump', ConsoleCommands);
392 AddCommand('exec', ConsoleCommands);
393 AddCommand('alias', ConsoleCommands);
394 AddCommand('call', ConsoleCommands);
396 AddCommand('d_window', DebugCommands);
397 AddCommand('d_sounds', DebugCommands);
398 AddCommand('d_frames', DebugCommands);
399 AddCommand('d_winmsg', DebugCommands);
400 AddCommand('d_monoff', DebugCommands);
401 AddCommand('d_botoff', DebugCommands);
402 AddCommand('d_monster', DebugCommands);
403 AddCommand('d_health', DebugCommands);
404 AddCommand('d_player', DebugCommands);
405 AddCommand('d_joy', DebugCommands);
407 AddCommand('pf_draw_frame', ProfilerCommands, 'draw frame rendering profiles');
408 //AddCommand('pf_update_frame', ProfilerCommands);
409 AddCommand('pf_coldet', ProfilerCommands, 'draw collision detection profiles');
410 AddCommand('pf_los', ProfilerCommands, 'draw monster LOS profiles');
411 AddCommand('r_sq_draw', ProfilerCommands, 'accelerated spatial queries in rendering');
412 AddCommand('cd_sq_enabled', ProfilerCommands, 'accelerated spatial queries in map coldet');
413 AddCommand('mon_sq_enabled', ProfilerCommands, 'use accelerated spatial queries for monsters');
414 AddCommand('wtrace_sq_enabled', ProfilerCommands, 'use accelerated weapon hitscan trace');
416 AddCommand('pr_enabled', ProfilerCommands, 'enable/disable particles');
417 AddCommand('pr_phys_enabled', ProfilerCommands, 'enable/disable particle physics');
418 AddCommand('los_enabled', ProfilerCommands, 'enable/disable LOS calculations');
420 AddCommand('mon_think', ProfilerCommands, 'enable/disable monster thinking');
421 AddCommand('dbg_holmes', ProfilerCommands, 'turn Holmes on/off');
423 AddCommand('p1_name', GameCVars);
424 AddCommand('p2_name', GameCVars);
425 AddCommand('p1_color', GameCVars);
426 AddCommand('p2_color', GameCVars);
427 AddCommand('r_showfps', GameCVars);
428 AddCommand('r_showtime', GameCVars);
429 AddCommand('r_showscore', GameCVars);
430 AddCommand('r_showlives', GameCVars);
431 AddCommand('r_showstat', GameCVars);
432 AddCommand('r_showkillmsg', GameCVars);
433 AddCommand('r_showspect', GameCVars);
434 AddCommand('r_showping', GameCVars);
435 AddCommand('g_gamemode', GameCVars);
436 AddCommand('g_friendlyfire', GameCVars);
437 AddCommand('g_weaponstay', GameCVars);
438 AddCommand('g_allow_exit', GameCVars);
439 AddCommand('g_allow_monsters', GameCVars);
440 AddCommand('g_bot_vsmonsters', GameCVars);
441 AddCommand('g_bot_vsplayers', GameCVars);
442 AddCommand('g_scorelimit', GameCVars);
443 AddCommand('g_timelimit', GameCVars);
444 AddCommand('g_maxlives', GameCVars);
445 AddCommand('g_warmuptime', GameCVars);
446 AddCommand('net_interp', GameCVars);
447 AddCommand('net_forceplayerupdate', GameCVars);
448 AddCommand('net_predictself', GameCVars);
449 AddCommand('sv_name', GameCVars);
450 AddCommand('sv_passwd', GameCVars);
451 AddCommand('sv_maxplrs', GameCVars);
452 AddCommand('sv_public', GameCVars);
453 AddCommand('sv_intertime', GameCVars);
455 AddCommand('quit', GameCommands);
456 AddCommand('exit', GameCommands);
457 AddCommand('pause', GameCommands);
458 AddCommand('endgame', GameCommands);
459 AddCommand('restart', GameCommands);
460 AddCommand('addbot', GameCommands);
461 AddCommand('bot_add', GameCommands);
462 AddCommand('bot_addlist', GameCommands);
463 AddCommand('bot_addred', GameCommands);
464 AddCommand('bot_addblue', GameCommands);
465 AddCommand('bot_removeall', GameCommands);
466 AddCommand('chat', GameCommands);
467 AddCommand('teamchat', GameCommands);
468 AddCommand('game', GameCommands);
469 AddCommand('host', GameCommands);
470 AddCommand('map', GameCommands);
471 AddCommand('nextmap', GameCommands);
472 AddCommand('endmap', GameCommands);
473 AddCommand('goodbye', GameCommands);
474 AddCommand('suicide', GameCommands);
475 AddCommand('spectate', GameCommands);
476 AddCommand('ready', GameCommands);
477 AddCommand('kick', GameCommands);
478 AddCommand('kick_id', GameCommands);
479 AddCommand('ban', GameCommands);
480 AddCommand('permban', GameCommands);
481 AddCommand('ban_id', GameCommands);
482 AddCommand('permban_id', GameCommands);
483 AddCommand('unban', GameCommands);
484 AddCommand('connect', GameCommands);
485 AddCommand('disconnect', GameCommands);
486 AddCommand('reconnect', GameCommands);
487 AddCommand('say', GameCommands);
488 AddCommand('tell', GameCommands);
489 AddCommand('overtime', GameCommands);
490 AddCommand('rcon_password', GameCommands);
491 AddCommand('rcon', GameCommands);
492 AddCommand('callvote', GameCommands);
493 AddCommand('vote', GameCommands);
494 AddCommand('clientlist', GameCommands);
495 AddCommand('event', GameCommands);
497 AddCommand('god', GameCheats);
498 AddCommand('notarget', GameCheats);
499 AddCommand('give', GameCheats); // "exit" too ;-)
500 AddCommand('open', GameCheats);
501 AddCommand('fly', GameCheats);
502 AddCommand('noclip', GameCheats);
503 AddCommand('speedy', GameCheats);
504 AddCommand('jumpy', GameCheats);
505 AddCommand('noreload', GameCheats);
506 AddCommand('aimline', GameCheats);
507 AddCommand('automap', GameCheats);
509 WhitelistCommand('say');
510 WhitelistCommand('tell');
511 WhitelistCommand('overtime');
512 WhitelistCommand('ready');
513 WhitelistCommand('map');
514 WhitelistCommand('nextmap');
515 WhitelistCommand('endmap');
516 WhitelistCommand('restart');
517 WhitelistCommand('kick');
518 WhitelistCommand('ban');
520 WhitelistCommand('addbot');
521 WhitelistCommand('bot_add');
522 WhitelistCommand('bot_addred');
523 WhitelistCommand('bot_addblue');
524 WhitelistCommand('bot_removeall');
526 WhitelistCommand('g_gamemode');
527 WhitelistCommand('g_friendlyfire');
528 WhitelistCommand('g_weaponstay');
529 WhitelistCommand('g_allow_exit');
530 WhitelistCommand('g_allow_monsters');
531 WhitelistCommand('g_scorelimit');
532 WhitelistCommand('g_timelimit');
534 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
535 g_Console_Add('');
536 end;
538 procedure g_Console_Update();
539 var
540 a, b: Integer;
541 begin
542 if Cons_Shown then
543 begin
544 // Â ïðîöåññå îòêðûòèÿ:
545 if gConsoleShow and (Cons_Y < 0) then
546 begin
547 Cons_Y := Cons_Y+Step;
548 end;
550 // Â ïðîöåññå çàêðûòèÿ:
551 if (not gConsoleShow) and
552 (Cons_Y > -(gScreenHeight div 2)) then
553 Cons_Y := Cons_Y-Step;
555 // Îêîí÷àòåëüíî îòêðûëàñü:
556 if Cons_Y > 0 then
557 Cons_Y := 0;
559 // Îêîí÷àòåëüíî çàêðûëàñü:
560 if Cons_Y <= (-(gScreenHeight div 2)) then
561 begin
562 Cons_Y := -(gScreenHeight div 2);
563 Cons_Shown := False;
564 end;
565 end;
567 a := 0;
568 while a <= High(MsgArray) do
569 begin
570 if MsgArray[a].Time > 0 then
571 begin
572 if MsgArray[a].Time = 1 then
573 begin
574 if a < High(MsgArray) then
575 begin
576 for b := a to High(MsgArray)-1 do
577 MsgArray[b] := MsgArray[b+1];
579 MsgArray[High(MsgArray)].Time := 0;
581 a := a - 1;
582 end;
583 end
584 else
585 Dec(MsgArray[a].Time);
586 end;
588 a := a + 1;
589 end;
590 end;
593 procedure drawConsoleText ();
594 var
595 CWidth, CHeight: Byte;
596 ty: Integer;
597 sp, ep: LongWord;
598 skip: Integer;
600 procedure putLine (sp, ep: LongWord);
601 var
602 p: LongWord;
603 wdt, cw: Integer;
604 begin
605 p := sp;
606 wdt := 0;
607 while p <> ep do
608 begin
609 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
610 if wdt+cw > gScreenWidth-8 then break;
611 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
612 Inc(wdt, cw);
613 cbufNext(p);
614 end;
615 if p <> ep then putLine(p, ep); // do rest of the line first
616 // now print our part
617 if skip = 0 then
618 begin
619 ep := p;
620 p := sp;
621 wdt := 2;
622 while p <> ep do
623 begin
624 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
625 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
626 Inc(wdt, cw);
627 cbufNext(p);
628 end;
629 Dec(ty, CHeight);
630 end
631 else
632 begin
633 Dec(skip);
634 end;
635 end;
637 begin
638 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
639 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
640 skip := conSkipLines;
641 cbufLastLine(sp, ep);
642 repeat
643 putLine(sp, ep);
644 if ty+CHeight <= 0 then break;
645 until not cbufLineUp(sp, ep);
646 end;
648 procedure g_Console_Draw();
649 var
650 CWidth, CHeight: Byte;
651 mfW, mfH: Word;
652 a, b: Integer;
653 begin
654 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
656 for a := 0 to High(MsgArray) do
657 if MsgArray[a].Time > 0 then
658 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
659 gStdFont, True);
661 if not Cons_Shown then
662 begin
663 if gChatShow then
664 begin
665 if gChatTeam then
666 begin
667 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
668 gStdFont, 255, 255, 255, 1, True);
669 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
670 gStdFont, 255, 255, 255, 1, True);
671 end
672 else
673 begin
674 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
675 gStdFont, 255, 255, 255, 1, True);
676 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
677 gStdFont, 255, 255, 255, 1, True);
678 end;
679 end;
680 Exit;
681 end;
683 if gDebugMode then
684 begin
685 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
686 a := (gScreenWidth - 2*mfW) div 2;
687 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
688 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
689 _RGB(128, 0, 0), 2.0);
690 end;
692 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
693 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
695 drawConsoleText();
696 (*
697 if ConsoleHistory <> nil then
698 begin
699 b := 0;
700 if CHeight > 0 then
701 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
702 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
704 b := Max(b-Offset, 0);
705 d := Max(High(ConsoleHistory)-Offset, 0);
707 c := 2;
708 for a := d downto b do
709 begin
710 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
711 gStdFont, True);
712 c := c + 1;
713 end;
714 end;
715 *)
717 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
718 end;
720 procedure g_Console_Switch();
721 begin
722 if gChatShow then Exit;
723 gConsoleShow := not gConsoleShow;
724 Cons_Shown := True;
725 end;
727 procedure g_Console_Chat_Switch(Team: Boolean = False);
728 begin
729 if gConsoleShow then Exit;
730 if not g_Game_IsNet then Exit;
731 gChatShow := not gChatShow;
732 gChatTeam := Team;
733 if gChatShow then
734 gChatEnter := False;
735 Line := '';
736 CPos := 1;
737 end;
739 procedure g_Console_Char(C: Char);
740 begin
741 if gChatShow and (not gChatEnter) then
742 Exit;
743 Insert(C, Line, CPos);
744 CPos := CPos + 1;
745 end;
748 var
749 tcomplist: array of string = nil;
750 tcompidx: array of Integer = nil;
752 procedure Complete ();
753 var
754 i, c: Integer;
755 tused: Integer;
756 ll, lpfx, cmd: string;
757 begin
758 if (Length(Line) = 0) then
759 begin
760 g_Console_Add('');
761 for i := 0 to High(Commands) do
762 begin
763 if not Commands[i].hidden then
764 begin
765 if (Length(Commands[i].help) > 0) then
766 begin
767 g_Console_Add(' '+Commands[i].Cmd+' -- '+Commands[i].help);
768 end
769 else
770 begin
771 g_Console_Add(' '+Commands[i].Cmd);
772 end;
773 end;
774 end;
775 exit;
776 end;
778 ll := LowerCase(Line);
779 lpfx := '';
781 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
782 begin
783 ll := Copy(ll, 0, Length(ll)-1);
784 for i := 0 to High(Commands) do
785 begin
786 if Commands[i].hidden then continue;
787 if (Commands[i].Cmd = ll) then
788 begin
789 if (Length(Commands[i].help) > 0) then
790 begin
791 g_Console_Add(' '+Commands[i].Cmd+' -- '+Commands[i].help);
792 end;
793 end;
794 end;
795 exit;
796 end;
798 // build completion list
799 tused := 0;
800 for i := 0 to High(Commands) do
801 begin
802 if Commands[i].hidden then continue;
803 cmd := Commands[i].Cmd;
804 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
805 begin
806 if (tused = Length(tcomplist)) then
807 begin
808 SetLength(tcomplist, Length(tcomplist)+128);
809 SetLength(tcompidx, Length(tcompidx)+128);
810 end;
811 tcomplist[tused] := cmd;
812 tcompidx[tused] := i;
813 Inc(tused);
814 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
815 end;
816 end;
818 // get longest prefix
819 for i := 0 to tused-1 do
820 begin
821 cmd := tcomplist[i];
822 for c := 1 to Length(lpfx) do
823 begin
824 if (c > Length(cmd)) then break;
825 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
826 end;
827 end;
829 if (tused = 0) then exit;
831 if (tused = 1) then
832 begin
833 Line := tcomplist[0]+' ';
834 CPos := Length(Line)+1;
835 end
836 else
837 begin
838 // has longest prefix?
839 if (Length(lpfx) > Length(ll)) then
840 begin
841 Line := lpfx;
842 CPos:= Length(Line)+1;
843 end
844 else
845 begin
846 g_Console_Add('');
847 for i := 0 to tused-1 do
848 begin
849 if (Length(Commands[tcompidx[i]].help) > 0) then
850 begin
851 g_Console_Add(' '+tcomplist[i]+' -- '+Commands[tcompidx[i]].help);
852 end
853 else
854 begin
855 g_Console_Add(' '+tcomplist[i]);
856 end;
857 end;
858 end;
859 end;
860 end;
863 procedure g_Console_Control(K: Word);
864 begin
865 case K of
866 IK_BACKSPACE:
867 if (Length(Line) > 0) and (CPos > 1) then
868 begin
869 Delete(Line, CPos-1, 1);
870 CPos := CPos-1;
871 end;
872 IK_DELETE:
873 if (Length(Line) > 0) and (CPos <= Length(Line)) then
874 Delete(Line, CPos, 1);
875 IK_LEFT, IK_KPLEFT:
876 if CPos > 1 then
877 CPos := CPos - 1;
878 IK_RIGHT, IK_KPRIGHT:
879 if CPos <= Length(Line) then
880 CPos := CPos + 1;
881 IK_RETURN, IK_KPRETURN:
882 begin
883 if Cons_Shown then
884 g_Console_Process(Line)
885 else
886 if gChatShow then
887 begin
888 if (Length(Line) > 0) and g_Game_IsNet then
889 begin
890 if gChatTeam then
891 begin
892 if g_Game_IsClient then
893 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
894 else
895 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
896 NET_CHAT_TEAM, gPlayer1Settings.Team);
897 end
898 else
899 begin
900 if g_Game_IsClient then
901 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
902 else
903 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
904 NET_CHAT_PLAYER);
905 end;
906 end;
908 Line := '';
909 CPos := 1;
910 gChatShow := False;
911 gJustChatted := True;
912 end;
913 end;
914 IK_TAB:
915 if not gChatShow then
916 Complete();
917 IK_DOWN, IK_KPDOWN:
918 if not gChatShow then
919 if (CommandHistory <> nil) and
920 (CmdIndex < Length(CommandHistory)) then
921 begin
922 if CmdIndex < Length(CommandHistory)-1 then
923 CmdIndex := CmdIndex + 1;
924 Line := CommandHistory[CmdIndex];
925 CPos := Length(Line) + 1;
926 end;
927 IK_UP, IK_KPUP:
928 if not gChatShow then
929 if (CommandHistory <> nil) and
930 (CmdIndex <= Length(CommandHistory)) then
931 begin
932 if CmdIndex > 0 then
933 CmdIndex := CmdIndex - 1;
934 Line := CommandHistory[CmdIndex];
935 Cpos := Length(Line) + 1;
936 end;
937 IK_PAGEUP, IK_KPPAGEUP: // PgUp
938 if not gChatShow then Inc(conSkipLines);
939 IK_PAGEDN, IK_KPPAGEDN: // PgDown
940 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
941 IK_HOME, IK_KPHOME:
942 CPos := 1;
943 IK_END, IK_KPEND:
944 CPos := Length(Line) + 1;
945 end;
946 end;
948 function GetStr(var Str: String): String;
949 var
950 a, b: Integer;
951 begin
952 Result := '';
953 if Str[1] = '"' then
954 begin
955 for b := 1 to Length(Str) do
956 if (b = Length(Str)) or (Str[b+1] = '"') then
957 begin
958 Result := Copy(Str, 2, b-1);
959 Delete(Str, 1, b+1);
960 Str := Trim(Str);
961 Exit;
962 end;
963 end;
965 for a := 1 to Length(Str) do
966 if (a = Length(Str)) or (Str[a+1] = ' ') then
967 begin
968 Result := Copy(Str, 1, a);
969 Delete(Str, 1, a+1);
970 Str := Trim(Str);
971 Exit;
972 end;
973 end;
975 function ParseString(Str: String): SArray;
976 begin
977 Result := nil;
979 Str := Trim(Str);
981 if Str = '' then
982 Exit;
984 while Str <> '' do
985 begin
986 SetLength(Result, Length(Result)+1);
987 Result[High(Result)] := GetStr(Str);
988 end;
989 end;
991 procedure g_Console_Add (L: string; Show: Boolean=false);
993 procedure conmsg (s: AnsiString);
994 var
995 a: Integer;
996 begin
997 if length(s) = 0 then exit;
998 for a := 0 to High(MsgArray) do
999 begin
1000 with MsgArray[a] do
1001 begin
1002 if Time = 0 then
1003 begin
1004 Msg := s;
1005 Time := MsgTime;
1006 exit;
1007 end;
1008 end;
1009 end;
1010 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1011 with MsgArray[High(MsgArray)] do
1012 begin
1013 Msg := L;
1014 Time := MsgTime;
1015 end;
1016 end;
1018 var
1019 f: Integer;
1020 begin
1021 // put it to console
1022 cbufPut(L);
1023 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1025 // now show 'em out of console too
1026 Show := Show and gAllowConsoleMessages;
1027 if Show and gShowMessages then
1028 begin
1029 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1030 while length(L) > 0 do
1031 begin
1032 f := Pos(#10, L);
1033 if f <= 0 then f := length(L)+1;
1034 conmsg(Copy(L, 1, f-1));
1035 Delete(L, 1, f);
1036 end;
1037 end;
1039 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1040 //ConsoleHistory[High(ConsoleHistory)] := L;
1042 (*
1043 {$IFDEF HEADLESS}
1044 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1045 {$ENDIF}
1046 *)
1047 end;
1050 var
1051 consolewriterLastWasEOL: Boolean = false;
1053 procedure consolewriter (constref buf; len: SizeUInt);
1054 var
1055 b: PByte;
1056 begin
1057 if (len < 1) then exit;
1058 b := PByte(@buf);
1059 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1060 while (len > 0) do
1061 begin
1062 if (b[0] <> 13) and (b[0] <> 10) then
1063 begin
1064 cbufPut(Char(b[0]));
1065 end
1066 else
1067 begin
1068 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1069 cbufPut(#10);
1070 end;
1071 len -= 1;
1072 b += 1;
1073 end;
1074 end;
1077 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1078 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1079 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1080 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1081 begin
1082 g_Console_Add(s, show);
1083 end;
1086 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1087 begin
1088 if show then
1089 begin
1090 g_Console_Add(formatstrf(s, args), true);
1091 end
1092 else
1093 begin
1094 consolewriterLastWasEOL := false;
1095 formatstrf(s, args, consolewriter);
1096 if not consolewriterLastWasEOL then cbufPut(#10);
1097 end;
1098 end;
1101 procedure g_Console_Clear();
1102 begin
1103 //ConsoleHistory := nil;
1104 cbufClear();
1105 conSkipLines := 0;
1106 end;
1108 procedure AddToHistory(L: String);
1109 var
1110 len: Integer;
1111 begin
1112 len := Length(CommandHistory);
1114 if (len = 0) or
1115 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1116 begin
1117 SetLength(CommandHistory, len+1);
1118 CommandHistory[len] := L;
1119 end;
1121 CmdIndex := Length(CommandHistory);
1122 end;
1124 function g_Console_CommandBlacklisted(C: String): Boolean;
1125 var
1126 Arr: SArray;
1127 i: Integer;
1128 begin
1129 Result := True;
1131 Arr := nil;
1133 if Trim(C) = '' then
1134 Exit;
1136 Arr := ParseString(C);
1137 if Arr = nil then
1138 Exit;
1140 for i := 0 to High(Whitelist) do
1141 if Whitelist[i] = LowerCase(Arr[0]) then
1142 Result := False;
1143 end;
1145 procedure g_Console_Process(L: String; Quiet: Boolean = False);
1146 var
1147 Arr: SArray;
1148 i: Integer;
1149 begin
1150 Arr := nil;
1152 if Trim(L) = '' then
1153 Exit;
1155 conSkipLines := 0; // "unscroll"
1157 if L = 'goobers' then
1158 begin
1159 Line := '';
1160 CPos := 1;
1161 gCheats := true;
1162 g_Console_Add('Your memory serves you well.');
1163 exit;
1164 end;
1166 if not Quiet then
1167 begin
1168 g_Console_Add('> '+L);
1169 Line := '';
1170 CPos := 1;
1171 end;
1173 Arr := ParseString(L);
1174 if Arr = nil then
1175 Exit;
1177 if Commands = nil then
1178 Exit;
1180 if not Quiet then
1181 AddToHistory(L);
1183 for i := 0 to High(Commands) do
1184 if Commands[i].Cmd = LowerCase(Arr[0]) then
1185 if @Commands[i].Proc <> nil then
1186 begin
1187 Commands[i].Proc(Arr);
1188 Exit;
1189 end;
1191 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1192 end;
1194 end.