DEADSOFTWARE

started `conRegVar()` API (only for booleans for now)
[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 uses
22 wadreader; // for SArray
24 procedure g_Console_Init();
25 procedure g_Console_Update();
26 procedure g_Console_Draw();
27 procedure g_Console_Switch();
28 procedure g_Console_Char(C: Char);
29 procedure g_Console_Control(K: Word);
30 procedure g_Console_Process(L: String; Quiet: Boolean = False);
31 procedure g_Console_Add(L: String; Show: Boolean = False);
32 procedure g_Console_Clear();
33 function g_Console_CommandBlacklisted(C: String): Boolean;
35 procedure conwriteln (const s: AnsiString; show: Boolean=false);
36 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
38 // <0: no arg; 0/1: true/false
39 function conGetBoolArg (P: SArray; idx: Integer): Integer;
41 procedure g_Console_Chat_Switch(Team: Boolean = False);
43 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false);
46 var
47 gConsoleShow: Boolean; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
48 gChatShow: Boolean;
49 gChatTeam: Boolean = False;
50 gAllowConsoleMessages: Boolean = True;
51 gChatEnter: Boolean = True;
52 gJustChatted: Boolean = False; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
54 implementation
56 uses
57 g_textures, g_main, e_graphics, e_input, g_game,
58 SysUtils, g_basic, g_options, Math,
59 g_menu, g_language, g_net, g_netmsg, e_log, conbuf, utils;
61 type
62 PCommand = ^TCommand;
64 TCmdProc = procedure (P: SArray);
65 TCmdProcEx = procedure (me: PCommand; P: SArray);
67 TCommand = record
68 Cmd: String;
69 Proc: TCmdProc;
70 ProcEx: TCmdProcEx;
71 help: String;
72 hidden: Boolean;
73 ptr: Pointer; // various data
74 msg: AnsiString; // message for var changes
75 cheat: Boolean;
76 end;
78 TAlias = record
79 Name: String;
80 Commands: SArray;
81 end;
84 const
85 Step = 32;
86 Alpha = 25;
87 MsgTime = 144;
88 MaxScriptRecursion = 16;
90 DEBUG_STRING = 'DEBUG MODE';
92 var
93 ID: DWORD;
94 RecursionDepth: Word = 0;
95 RecursionLimitHit: Boolean = False;
96 Cons_Y: SmallInt;
97 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
98 Line: String;
99 CPos: Word;
100 //ConsoleHistory: SArray;
101 CommandHistory: SArray;
102 Whitelist: SArray;
103 Commands: Array of TCommand = nil;
104 Aliases: Array of TAlias = nil;
105 CmdIndex: Word;
106 conSkipLines: Integer = 0;
107 MsgArray: Array [0..4] of record
108 Msg: String;
109 Time: Word;
110 end;
113 // <0: no arg; 0/1: true/false
114 function conGetBoolArg (P: SArray; idx: Integer): Integer;
115 begin
116 if (idx < 0) or (idx > High(P)) then begin result := -1; exit; end;
117 result := 0;
118 if (P[idx] = '1') or (CompareText(P[idx], 'on') = 0) or (CompareText(P[idx], 'true') = 0) or
119 (CompareText(P[idx], 'tan') = 0) or (CompareText(P[idx], 'yes') = 0) then result := 1;
120 end;
123 procedure boolVarHandler (me: PCommand; P: SArray);
125 procedure binaryFlag (var flag: Boolean; msg: string);
126 begin
127 if (Length(p) > 2) then
128 begin
129 conwritefln('too many arguments to ''%s''', [P[0]]);
130 end
131 else
132 begin
133 case conGetBoolArg(P, 1) of
134 -1: begin end;
135 0: if conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
136 1: if conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
137 end;
138 if flag then conwritefln('%s: tan', [msg]) else conwritefln('%s: ona', [msg]);
139 end;
140 end;
142 begin
143 binaryFlag(PBoolean(me.ptr)^, me.msg);
144 end;
147 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false);
148 var
149 f: Integer;
150 cp: PCommand;
151 begin
152 f := Length(Commands);
153 SetLength(Commands, f+1);
154 cp := @Commands[f];
155 cp.Cmd := LowerCase(conname);
156 cp.Proc := nil;
157 cp.ProcEx := boolVarHandler;
158 cp.help := ahelp;
159 cp.hidden := false;
160 cp.ptr := pvar;
161 cp.msg := amsg;
162 cp.cheat := acheat;
163 end;
166 function GetStrACmd(var Str: String): String;
167 var
168 a: Integer;
169 begin
170 Result := '';
171 for a := 1 to Length(Str) do
172 if (a = Length(Str)) or (Str[a+1] = ';') then
173 begin
174 Result := Copy(Str, 1, a);
175 Delete(Str, 1, a+1);
176 Str := Trim(Str);
177 Exit;
178 end;
179 end;
181 function ParseAlias(Str: String): SArray;
182 begin
183 Result := nil;
185 Str := Trim(Str);
187 if Str = '' then
188 Exit;
190 while Str <> '' do
191 begin
192 SetLength(Result, Length(Result)+1);
193 Result[High(Result)] := GetStrACmd(Str);
194 end;
195 end;
197 procedure ConsoleCommands(P: SArray);
198 var
199 Cmd, s: String;
200 a, b: Integer;
201 F: TextFile;
202 begin
203 Cmd := LowerCase(P[0]);
204 s := '';
206 if Cmd = 'clear' then
207 begin
208 //ConsoleHistory := nil;
209 cbufClear();
210 conSkipLines := 0;
212 for a := 0 to High(MsgArray) do
213 with MsgArray[a] do
214 begin
215 Msg := '';
216 Time := 0;
217 end;
218 end;
220 if Cmd = 'clearhistory' then
221 CommandHistory := nil;
223 if Cmd = 'showhistory' then
224 if CommandHistory <> nil then
225 begin
226 g_Console_Add('');
227 for a := 0 to High(CommandHistory) do
228 g_Console_Add(' '+CommandHistory[a]);
229 end;
231 if Cmd = 'commands' then
232 begin
233 g_Console_Add('');
234 g_Console_Add('Commands list:');
235 for a := High(Commands) downto 0 do
236 begin
237 if (Length(Commands[a].help) > 0) then
238 begin
239 g_Console_Add(' '+Commands[a].Cmd+' -- '+Commands[a].help);
240 end
241 else
242 begin
243 g_Console_Add(' '+Commands[a].Cmd);
244 end;
245 end;
246 end;
248 if Cmd = 'time' then
249 g_Console_Add(TimeToStr(Now), True);
251 if Cmd = 'date' then
252 g_Console_Add(DateToStr(Now), True);
254 if Cmd = 'echo' then
255 if Length(P) > 1 then
256 begin
257 if P[1] = 'ololo' then
258 gCheats := True
259 else
260 begin
261 s := '';
262 for a := 1 to High(P) do
263 s := s + P[a] + ' ';
264 g_Console_Add(b_Text_Format(s), True);
265 end;
266 end
267 else
268 g_Console_Add('');
270 if Cmd = 'dump' then
271 begin
272 (*
273 if ConsoleHistory <> nil then
274 begin
275 if Length(P) > 1 then
276 s := P[1]
277 else
278 s := GameDir+'/console.txt';
280 {$I-}
281 AssignFile(F, s);
282 Rewrite(F);
283 if IOResult <> 0 then
284 begin
285 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
286 CloseFile(F);
287 Exit;
288 end;
290 for a := 0 to High(ConsoleHistory) do
291 WriteLn(F, ConsoleHistory[a]);
293 CloseFile(F);
294 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
295 {$I+}
296 end;
297 *)
298 end;
300 if Cmd = 'exec' then
301 begin
302 // exec <filename>
303 if Length(P) > 1 then
304 begin
305 s := GameDir+'/'+P[1];
307 {$I-}
308 AssignFile(F, s);
309 Reset(F);
310 if IOResult <> 0 then
311 begin
312 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
313 CloseFile(F);
314 Exit;
315 end;
316 g_Console_Add(Format(_lc[I_CONSOLE_EXEC], [s]));
318 while not EOF(F) do
319 begin
320 ReadLn(F, s);
321 if IOResult <> 0 then
322 begin
323 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
324 CloseFile(F);
325 Exit;
326 end;
327 if Pos('#', s) <> 1 then // script comment
328 begin
329 // prevents endless loops
330 Inc(RecursionDepth);
331 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
332 if not RecursionLimitHit then
333 g_Console_Process(s, True);
334 Dec(RecursionDepth);
335 end;
336 end;
337 if (RecursionDepth = 0) and RecursionLimitHit then
338 begin
339 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
340 RecursionLimitHit := False;
341 end;
343 CloseFile(F);
344 {$I+}
345 end
346 else
347 g_Console_Add('exec <script file>');
348 end;
350 if Cmd = 'alias' then
351 begin
352 // alias [alias_name] [commands]
353 if Length(P) > 1 then
354 begin
355 for a := 0 to High(Aliases) do
356 if Aliases[a].Name = P[1] then
357 begin
358 if Length(P) > 2 then
359 Aliases[a].Commands := ParseAlias(P[2])
360 else
361 for b := 0 to High(Aliases[a].Commands) do
362 g_Console_Add(Aliases[a].Commands[b]);
363 Exit;
364 end;
365 SetLength(Aliases, Length(Aliases)+1);
366 a := High(Aliases);
367 Aliases[a].Name := P[1];
368 if Length(P) > 2 then
369 Aliases[a].Commands := ParseAlias(P[2])
370 else
371 for b := 0 to High(Aliases[a].Commands) do
372 g_Console_Add(Aliases[a].Commands[b]);
373 end else
374 for a := 0 to High(Aliases) do
375 if Aliases[a].Commands <> nil then
376 g_Console_Add(Aliases[a].Name);
377 end;
379 if Cmd = 'call' then
380 begin
381 // call <alias_name>
382 if Length(P) > 1 then
383 begin
384 if Aliases = nil then
385 Exit;
386 for a := 0 to High(Aliases) do
387 if Aliases[a].Name = P[1] then
388 begin
389 if Aliases[a].Commands <> nil then
390 begin
391 // with this system proper endless loop detection seems either impossible
392 // or very dirty to implement, so let's have this instead
393 // prevents endless loops
394 for b := 0 to High(Aliases[a].Commands) do
395 begin
396 Inc(RecursionDepth);
397 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
398 if not RecursionLimitHit then
399 g_Console_Process(Aliases[a].Commands[b], True);
400 Dec(RecursionDepth);
401 end;
402 if (RecursionDepth = 0) and RecursionLimitHit then
403 begin
404 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
405 RecursionLimitHit := False;
406 end;
407 end;
408 Exit;
409 end;
410 end
411 else
412 g_Console_Add('call <alias name>');
413 end;
414 end;
416 procedure WhitelistCommand(Cmd: string);
417 var
418 a: Integer;
419 begin
420 SetLength(Whitelist, Length(Whitelist)+1);
421 a := High(Whitelist);
422 Whitelist[a] := LowerCase(Cmd);
423 end;
425 procedure AddCommand(Cmd: String; Proc: TCmdProc; ahelp: String=''; ahidden: Boolean=false; acheat: Boolean=false);
426 var
427 a: Integer;
428 cp: PCommand;
429 begin
430 SetLength(Commands, Length(Commands)+1);
431 a := High(Commands);
432 cp := @Commands[a];
433 cp.Cmd := LowerCase(Cmd);
434 cp.Proc := Proc;
435 cp.ProcEx := nil;
436 cp.help := ahelp;
437 cp.hidden := ahidden;
438 cp.ptr := nil;
439 cp.msg := '';
440 cp.cheat := acheat;
441 end;
443 procedure g_Console_Init();
444 var
445 a: Integer;
446 begin
447 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
448 Cons_Y := -(gScreenHeight div 2);
449 gConsoleShow := False;
450 gChatShow := False;
451 Cons_Shown := False;
452 CPos := 1;
454 for a := 0 to High(MsgArray) do
455 with MsgArray[a] do
456 begin
457 Msg := '';
458 Time := 0;
459 end;
461 AddCommand('clear', ConsoleCommands, 'clear console');
462 AddCommand('clearhistory', ConsoleCommands);
463 AddCommand('showhistory', ConsoleCommands);
464 AddCommand('commands', ConsoleCommands);
465 AddCommand('time', ConsoleCommands);
466 AddCommand('date', ConsoleCommands);
467 AddCommand('echo', ConsoleCommands);
468 AddCommand('dump', ConsoleCommands);
469 AddCommand('exec', ConsoleCommands);
470 AddCommand('alias', ConsoleCommands);
471 AddCommand('call', ConsoleCommands);
473 AddCommand('d_window', DebugCommands);
474 AddCommand('d_sounds', DebugCommands);
475 AddCommand('d_frames', DebugCommands);
476 AddCommand('d_winmsg', DebugCommands);
477 AddCommand('d_monoff', DebugCommands);
478 AddCommand('d_botoff', DebugCommands);
479 AddCommand('d_monster', DebugCommands);
480 AddCommand('d_health', DebugCommands);
481 AddCommand('d_player', DebugCommands);
482 AddCommand('d_joy', DebugCommands);
484 AddCommand('p1_name', GameCVars);
485 AddCommand('p2_name', GameCVars);
486 AddCommand('p1_color', GameCVars);
487 AddCommand('p2_color', GameCVars);
488 AddCommand('r_showfps', GameCVars);
489 AddCommand('r_showtime', GameCVars);
490 AddCommand('r_showscore', GameCVars);
491 AddCommand('r_showlives', GameCVars);
492 AddCommand('r_showstat', GameCVars);
493 AddCommand('r_showkillmsg', GameCVars);
494 AddCommand('r_showspect', GameCVars);
495 AddCommand('r_showping', GameCVars);
496 AddCommand('g_gamemode', GameCVars);
497 AddCommand('g_friendlyfire', GameCVars);
498 AddCommand('g_weaponstay', GameCVars);
499 AddCommand('g_allow_exit', GameCVars);
500 AddCommand('g_allow_monsters', GameCVars);
501 AddCommand('g_bot_vsmonsters', GameCVars);
502 AddCommand('g_bot_vsplayers', GameCVars);
503 AddCommand('g_scorelimit', GameCVars);
504 AddCommand('g_timelimit', GameCVars);
505 AddCommand('g_maxlives', GameCVars);
506 AddCommand('g_warmuptime', GameCVars);
507 AddCommand('net_interp', GameCVars);
508 AddCommand('net_forceplayerupdate', GameCVars);
509 AddCommand('net_predictself', GameCVars);
510 AddCommand('sv_name', GameCVars);
511 AddCommand('sv_passwd', GameCVars);
512 AddCommand('sv_maxplrs', GameCVars);
513 AddCommand('sv_public', GameCVars);
514 AddCommand('sv_intertime', GameCVars);
516 AddCommand('quit', GameCommands);
517 AddCommand('exit', GameCommands);
518 AddCommand('pause', GameCommands);
519 AddCommand('endgame', GameCommands);
520 AddCommand('restart', GameCommands);
521 AddCommand('addbot', GameCommands);
522 AddCommand('bot_add', GameCommands);
523 AddCommand('bot_addlist', GameCommands);
524 AddCommand('bot_addred', GameCommands);
525 AddCommand('bot_addblue', GameCommands);
526 AddCommand('bot_removeall', GameCommands);
527 AddCommand('chat', GameCommands);
528 AddCommand('teamchat', GameCommands);
529 AddCommand('game', GameCommands);
530 AddCommand('host', GameCommands);
531 AddCommand('map', GameCommands);
532 AddCommand('nextmap', GameCommands);
533 AddCommand('endmap', GameCommands);
534 AddCommand('goodbye', GameCommands);
535 AddCommand('suicide', GameCommands);
536 AddCommand('spectate', GameCommands);
537 AddCommand('ready', GameCommands);
538 AddCommand('kick', GameCommands);
539 AddCommand('kick_id', GameCommands);
540 AddCommand('ban', GameCommands);
541 AddCommand('permban', GameCommands);
542 AddCommand('ban_id', GameCommands);
543 AddCommand('permban_id', GameCommands);
544 AddCommand('unban', GameCommands);
545 AddCommand('connect', GameCommands);
546 AddCommand('disconnect', GameCommands);
547 AddCommand('reconnect', GameCommands);
548 AddCommand('say', GameCommands);
549 AddCommand('tell', GameCommands);
550 AddCommand('overtime', GameCommands);
551 AddCommand('rcon_password', GameCommands);
552 AddCommand('rcon', GameCommands);
553 AddCommand('callvote', GameCommands);
554 AddCommand('vote', GameCommands);
555 AddCommand('clientlist', GameCommands);
556 AddCommand('event', GameCommands);
558 AddCommand('god', GameCheats);
559 AddCommand('notarget', GameCheats);
560 AddCommand('give', GameCheats); // "exit" too ;-)
561 AddCommand('open', GameCheats);
562 AddCommand('fly', GameCheats);
563 AddCommand('noclip', GameCheats);
564 AddCommand('speedy', GameCheats);
565 AddCommand('jumpy', GameCheats);
566 AddCommand('noreload', GameCheats);
567 AddCommand('aimline', GameCheats);
568 AddCommand('automap', GameCheats);
570 WhitelistCommand('say');
571 WhitelistCommand('tell');
572 WhitelistCommand('overtime');
573 WhitelistCommand('ready');
574 WhitelistCommand('map');
575 WhitelistCommand('nextmap');
576 WhitelistCommand('endmap');
577 WhitelistCommand('restart');
578 WhitelistCommand('kick');
579 WhitelistCommand('ban');
581 WhitelistCommand('addbot');
582 WhitelistCommand('bot_add');
583 WhitelistCommand('bot_addred');
584 WhitelistCommand('bot_addblue');
585 WhitelistCommand('bot_removeall');
587 WhitelistCommand('g_gamemode');
588 WhitelistCommand('g_friendlyfire');
589 WhitelistCommand('g_weaponstay');
590 WhitelistCommand('g_allow_exit');
591 WhitelistCommand('g_allow_monsters');
592 WhitelistCommand('g_scorelimit');
593 WhitelistCommand('g_timelimit');
595 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
596 g_Console_Add('');
597 end;
599 procedure g_Console_Update();
600 var
601 a, b: Integer;
602 begin
603 if Cons_Shown then
604 begin
605 // Â ïðîöåññå îòêðûòèÿ:
606 if gConsoleShow and (Cons_Y < 0) then
607 begin
608 Cons_Y := Cons_Y+Step;
609 end;
611 // Â ïðîöåññå çàêðûòèÿ:
612 if (not gConsoleShow) and
613 (Cons_Y > -(gScreenHeight div 2)) then
614 Cons_Y := Cons_Y-Step;
616 // Îêîí÷àòåëüíî îòêðûëàñü:
617 if Cons_Y > 0 then
618 Cons_Y := 0;
620 // Îêîí÷àòåëüíî çàêðûëàñü:
621 if Cons_Y <= (-(gScreenHeight div 2)) then
622 begin
623 Cons_Y := -(gScreenHeight div 2);
624 Cons_Shown := False;
625 end;
626 end;
628 a := 0;
629 while a <= High(MsgArray) do
630 begin
631 if MsgArray[a].Time > 0 then
632 begin
633 if MsgArray[a].Time = 1 then
634 begin
635 if a < High(MsgArray) then
636 begin
637 for b := a to High(MsgArray)-1 do
638 MsgArray[b] := MsgArray[b+1];
640 MsgArray[High(MsgArray)].Time := 0;
642 a := a - 1;
643 end;
644 end
645 else
646 Dec(MsgArray[a].Time);
647 end;
649 a := a + 1;
650 end;
651 end;
654 procedure drawConsoleText ();
655 var
656 CWidth, CHeight: Byte;
657 ty: Integer;
658 sp, ep: LongWord;
659 skip: Integer;
661 procedure putLine (sp, ep: LongWord);
662 var
663 p: LongWord;
664 wdt, cw: Integer;
665 begin
666 p := sp;
667 wdt := 0;
668 while p <> ep do
669 begin
670 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
671 if wdt+cw > gScreenWidth-8 then break;
672 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
673 Inc(wdt, cw);
674 cbufNext(p);
675 end;
676 if p <> ep then putLine(p, ep); // do rest of the line first
677 // now print our part
678 if skip = 0 then
679 begin
680 ep := p;
681 p := sp;
682 wdt := 2;
683 while p <> ep do
684 begin
685 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
686 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
687 Inc(wdt, cw);
688 cbufNext(p);
689 end;
690 Dec(ty, CHeight);
691 end
692 else
693 begin
694 Dec(skip);
695 end;
696 end;
698 begin
699 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
700 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
701 skip := conSkipLines;
702 cbufLastLine(sp, ep);
703 repeat
704 putLine(sp, ep);
705 if ty+CHeight <= 0 then break;
706 until not cbufLineUp(sp, ep);
707 end;
709 procedure g_Console_Draw();
710 var
711 CWidth, CHeight: Byte;
712 mfW, mfH: Word;
713 a, b: Integer;
714 begin
715 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
717 for a := 0 to High(MsgArray) do
718 if MsgArray[a].Time > 0 then
719 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
720 gStdFont, True);
722 if not Cons_Shown then
723 begin
724 if gChatShow then
725 begin
726 if gChatTeam then
727 begin
728 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
729 gStdFont, 255, 255, 255, 1, True);
730 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
731 gStdFont, 255, 255, 255, 1, True);
732 end
733 else
734 begin
735 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
736 gStdFont, 255, 255, 255, 1, True);
737 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
738 gStdFont, 255, 255, 255, 1, True);
739 end;
740 end;
741 Exit;
742 end;
744 if gDebugMode then
745 begin
746 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
747 a := (gScreenWidth - 2*mfW) div 2;
748 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
749 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
750 _RGB(128, 0, 0), 2.0);
751 end;
753 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
754 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
756 drawConsoleText();
757 (*
758 if ConsoleHistory <> nil then
759 begin
760 b := 0;
761 if CHeight > 0 then
762 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
763 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
765 b := Max(b-Offset, 0);
766 d := Max(High(ConsoleHistory)-Offset, 0);
768 c := 2;
769 for a := d downto b do
770 begin
771 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
772 gStdFont, True);
773 c := c + 1;
774 end;
775 end;
776 *)
778 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
779 end;
781 procedure g_Console_Switch();
782 begin
783 if gChatShow then Exit;
784 gConsoleShow := not gConsoleShow;
785 Cons_Shown := True;
786 end;
788 procedure g_Console_Chat_Switch(Team: Boolean = False);
789 begin
790 if gConsoleShow then Exit;
791 if not g_Game_IsNet then Exit;
792 gChatShow := not gChatShow;
793 gChatTeam := Team;
794 if gChatShow then
795 gChatEnter := False;
796 Line := '';
797 CPos := 1;
798 end;
800 procedure g_Console_Char(C: Char);
801 begin
802 if gChatShow and (not gChatEnter) then
803 Exit;
804 Insert(C, Line, CPos);
805 CPos := CPos + 1;
806 end;
809 var
810 tcomplist: array of string = nil;
811 tcompidx: array of Integer = nil;
813 procedure Complete ();
814 var
815 i, c: Integer;
816 tused: Integer;
817 ll, lpfx, cmd: string;
818 begin
819 if (Length(Line) = 0) then
820 begin
821 g_Console_Add('');
822 for i := 0 to High(Commands) do
823 begin
824 if not Commands[i].hidden then
825 begin
826 if (Length(Commands[i].help) > 0) then
827 begin
828 g_Console_Add(' '+Commands[i].Cmd+' -- '+Commands[i].help);
829 end
830 else
831 begin
832 g_Console_Add(' '+Commands[i].Cmd);
833 end;
834 end;
835 end;
836 exit;
837 end;
839 ll := LowerCase(Line);
840 lpfx := '';
842 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
843 begin
844 ll := Copy(ll, 0, Length(ll)-1);
845 for i := 0 to High(Commands) do
846 begin
847 if Commands[i].hidden then continue;
848 if (Commands[i].Cmd = ll) then
849 begin
850 if (Length(Commands[i].help) > 0) then
851 begin
852 g_Console_Add(' '+Commands[i].Cmd+' -- '+Commands[i].help);
853 end;
854 end;
855 end;
856 exit;
857 end;
859 // build completion list
860 tused := 0;
861 for i := 0 to High(Commands) do
862 begin
863 if Commands[i].hidden then continue;
864 cmd := Commands[i].Cmd;
865 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
866 begin
867 if (tused = Length(tcomplist)) then
868 begin
869 SetLength(tcomplist, Length(tcomplist)+128);
870 SetLength(tcompidx, Length(tcompidx)+128);
871 end;
872 tcomplist[tused] := cmd;
873 tcompidx[tused] := i;
874 Inc(tused);
875 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
876 end;
877 end;
879 // get longest prefix
880 for i := 0 to tused-1 do
881 begin
882 cmd := tcomplist[i];
883 for c := 1 to Length(lpfx) do
884 begin
885 if (c > Length(cmd)) then break;
886 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
887 end;
888 end;
890 if (tused = 0) then exit;
892 if (tused = 1) then
893 begin
894 Line := tcomplist[0]+' ';
895 CPos := Length(Line)+1;
896 end
897 else
898 begin
899 // has longest prefix?
900 if (Length(lpfx) > Length(ll)) then
901 begin
902 Line := lpfx;
903 CPos:= Length(Line)+1;
904 end
905 else
906 begin
907 g_Console_Add('');
908 for i := 0 to tused-1 do
909 begin
910 if (Length(Commands[tcompidx[i]].help) > 0) then
911 begin
912 g_Console_Add(' '+tcomplist[i]+' -- '+Commands[tcompidx[i]].help);
913 end
914 else
915 begin
916 g_Console_Add(' '+tcomplist[i]);
917 end;
918 end;
919 end;
920 end;
921 end;
924 procedure g_Console_Control(K: Word);
925 begin
926 case K of
927 IK_BACKSPACE:
928 if (Length(Line) > 0) and (CPos > 1) then
929 begin
930 Delete(Line, CPos-1, 1);
931 CPos := CPos-1;
932 end;
933 IK_DELETE:
934 if (Length(Line) > 0) and (CPos <= Length(Line)) then
935 Delete(Line, CPos, 1);
936 IK_LEFT, IK_KPLEFT:
937 if CPos > 1 then
938 CPos := CPos - 1;
939 IK_RIGHT, IK_KPRIGHT:
940 if CPos <= Length(Line) then
941 CPos := CPos + 1;
942 IK_RETURN, IK_KPRETURN:
943 begin
944 if Cons_Shown then
945 g_Console_Process(Line)
946 else
947 if gChatShow then
948 begin
949 if (Length(Line) > 0) and g_Game_IsNet then
950 begin
951 if gChatTeam then
952 begin
953 if g_Game_IsClient then
954 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
955 else
956 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
957 NET_CHAT_TEAM, gPlayer1Settings.Team);
958 end
959 else
960 begin
961 if g_Game_IsClient then
962 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
963 else
964 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
965 NET_CHAT_PLAYER);
966 end;
967 end;
969 Line := '';
970 CPos := 1;
971 gChatShow := False;
972 gJustChatted := True;
973 end;
974 end;
975 IK_TAB:
976 if not gChatShow then
977 Complete();
978 IK_DOWN, IK_KPDOWN:
979 if not gChatShow then
980 if (CommandHistory <> nil) and
981 (CmdIndex < Length(CommandHistory)) then
982 begin
983 if CmdIndex < Length(CommandHistory)-1 then
984 CmdIndex := CmdIndex + 1;
985 Line := CommandHistory[CmdIndex];
986 CPos := Length(Line) + 1;
987 end;
988 IK_UP, IK_KPUP:
989 if not gChatShow then
990 if (CommandHistory <> nil) and
991 (CmdIndex <= Length(CommandHistory)) then
992 begin
993 if CmdIndex > 0 then
994 CmdIndex := CmdIndex - 1;
995 Line := CommandHistory[CmdIndex];
996 Cpos := Length(Line) + 1;
997 end;
998 IK_PAGEUP, IK_KPPAGEUP: // PgUp
999 if not gChatShow then Inc(conSkipLines);
1000 IK_PAGEDN, IK_KPPAGEDN: // PgDown
1001 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1002 IK_HOME, IK_KPHOME:
1003 CPos := 1;
1004 IK_END, IK_KPEND:
1005 CPos := Length(Line) + 1;
1006 end;
1007 end;
1009 function GetStr(var Str: String): String;
1010 var
1011 a, b: Integer;
1012 begin
1013 Result := '';
1014 if Str[1] = '"' then
1015 begin
1016 for b := 1 to Length(Str) do
1017 if (b = Length(Str)) or (Str[b+1] = '"') then
1018 begin
1019 Result := Copy(Str, 2, b-1);
1020 Delete(Str, 1, b+1);
1021 Str := Trim(Str);
1022 Exit;
1023 end;
1024 end;
1026 for a := 1 to Length(Str) do
1027 if (a = Length(Str)) or (Str[a+1] = ' ') then
1028 begin
1029 Result := Copy(Str, 1, a);
1030 Delete(Str, 1, a+1);
1031 Str := Trim(Str);
1032 Exit;
1033 end;
1034 end;
1036 function ParseString(Str: String): SArray;
1037 begin
1038 Result := nil;
1040 Str := Trim(Str);
1042 if Str = '' then
1043 Exit;
1045 while Str <> '' do
1046 begin
1047 SetLength(Result, Length(Result)+1);
1048 Result[High(Result)] := GetStr(Str);
1049 end;
1050 end;
1052 procedure g_Console_Add (L: string; Show: Boolean=false);
1054 procedure conmsg (s: AnsiString);
1055 var
1056 a: Integer;
1057 begin
1058 if length(s) = 0 then exit;
1059 for a := 0 to High(MsgArray) do
1060 begin
1061 with MsgArray[a] do
1062 begin
1063 if Time = 0 then
1064 begin
1065 Msg := s;
1066 Time := MsgTime;
1067 exit;
1068 end;
1069 end;
1070 end;
1071 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1072 with MsgArray[High(MsgArray)] do
1073 begin
1074 Msg := L;
1075 Time := MsgTime;
1076 end;
1077 end;
1079 var
1080 f: Integer;
1081 begin
1082 // put it to console
1083 cbufPut(L);
1084 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1086 // now show 'em out of console too
1087 Show := Show and gAllowConsoleMessages;
1088 if Show and gShowMessages then
1089 begin
1090 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1091 while length(L) > 0 do
1092 begin
1093 f := Pos(#10, L);
1094 if f <= 0 then f := length(L)+1;
1095 conmsg(Copy(L, 1, f-1));
1096 Delete(L, 1, f);
1097 end;
1098 end;
1100 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1101 //ConsoleHistory[High(ConsoleHistory)] := L;
1103 (*
1104 {$IFDEF HEADLESS}
1105 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1106 {$ENDIF}
1107 *)
1108 end;
1111 var
1112 consolewriterLastWasEOL: Boolean = false;
1114 procedure consolewriter (constref buf; len: SizeUInt);
1115 var
1116 b: PByte;
1117 begin
1118 if (len < 1) then exit;
1119 b := PByte(@buf);
1120 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1121 while (len > 0) do
1122 begin
1123 if (b[0] <> 13) and (b[0] <> 10) then
1124 begin
1125 cbufPut(Char(b[0]));
1126 end
1127 else
1128 begin
1129 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1130 cbufPut(#10);
1131 end;
1132 len -= 1;
1133 b += 1;
1134 end;
1135 end;
1138 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1139 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1140 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1141 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1142 begin
1143 g_Console_Add(s, show);
1144 end;
1147 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1148 begin
1149 if show then
1150 begin
1151 g_Console_Add(formatstrf(s, args), true);
1152 end
1153 else
1154 begin
1155 consolewriterLastWasEOL := false;
1156 formatstrf(s, args, consolewriter);
1157 if not consolewriterLastWasEOL then cbufPut(#10);
1158 end;
1159 end;
1162 procedure g_Console_Clear();
1163 begin
1164 //ConsoleHistory := nil;
1165 cbufClear();
1166 conSkipLines := 0;
1167 end;
1169 procedure AddToHistory(L: String);
1170 var
1171 len: Integer;
1172 begin
1173 len := Length(CommandHistory);
1175 if (len = 0) or
1176 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1177 begin
1178 SetLength(CommandHistory, len+1);
1179 CommandHistory[len] := L;
1180 end;
1182 CmdIndex := Length(CommandHistory);
1183 end;
1185 function g_Console_CommandBlacklisted(C: String): Boolean;
1186 var
1187 Arr: SArray;
1188 i: Integer;
1189 begin
1190 Result := True;
1192 Arr := nil;
1194 if Trim(C) = '' then
1195 Exit;
1197 Arr := ParseString(C);
1198 if Arr = nil then
1199 Exit;
1201 for i := 0 to High(Whitelist) do
1202 if Whitelist[i] = LowerCase(Arr[0]) then
1203 Result := False;
1204 end;
1206 procedure g_Console_Process(L: String; Quiet: Boolean = False);
1207 var
1208 Arr: SArray;
1209 i: Integer;
1210 begin
1211 Arr := nil;
1213 if Trim(L) = '' then
1214 Exit;
1216 conSkipLines := 0; // "unscroll"
1218 if L = 'goobers' then
1219 begin
1220 Line := '';
1221 CPos := 1;
1222 gCheats := true;
1223 g_Console_Add('Your memory serves you well.');
1224 exit;
1225 end;
1227 if not Quiet then
1228 begin
1229 g_Console_Add('> '+L);
1230 Line := '';
1231 CPos := 1;
1232 end;
1234 Arr := ParseString(L);
1235 if Arr = nil then
1236 Exit;
1238 if Commands = nil then
1239 Exit;
1241 if not Quiet then
1242 AddToHistory(L);
1244 for i := 0 to High(Commands) do
1245 begin
1246 if Commands[i].Cmd = LowerCase(Arr[0]) then
1247 begin
1248 if assigned(Commands[i].ProcEx) then
1249 begin
1250 Commands[i].ProcEx(@Commands[i], Arr);
1251 exit;
1252 end;
1253 if assigned(Commands[i].Proc) then
1254 begin
1255 Commands[i].Proc(Arr);
1256 exit;
1257 end;
1258 end;
1259 end;
1261 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1262 end;
1264 end.