DEADSOFTWARE

better console completion
[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 g_Console_Chat_Switch(Team: Boolean = False);
34 var
35 gConsoleShow: Boolean; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
36 gChatShow: Boolean;
37 gChatTeam: Boolean = False;
38 gAllowConsoleMessages: Boolean = True;
39 gChatEnter: Boolean = True;
40 gJustChatted: Boolean = False; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
42 implementation
44 uses
45 g_textures, g_main, e_graphics, e_input, g_game,
46 SysUtils, g_basic, g_options, wadreader, Math,
47 g_menu, g_language, g_net, g_netmsg, e_log, conbuf;
49 type
50 TCmdProc = procedure (P: SArray);
52 TCommand = record
53 Cmd: String;
54 Proc: TCmdProc;
55 end;
57 TAlias = record
58 Name: String;
59 Commands: SArray;
60 end;
62 const
63 Step = 32;
64 Alpha = 25;
65 MsgTime = 144;
66 MaxScriptRecursion = 16;
68 DEBUG_STRING = 'DEBUG MODE';
70 var
71 ID: DWORD;
72 RecursionDepth: Word = 0;
73 RecursionLimitHit: Boolean = False;
74 Cons_Y: SmallInt;
75 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
76 Line: String;
77 CPos: Word;
78 //ConsoleHistory: SArray;
79 CommandHistory: SArray;
80 Whitelist: SArray;
81 Commands: Array of TCommand;
82 Aliases: Array of TAlias;
83 CmdIndex: Word;
84 conSkipLines: Integer = 0;
85 MsgArray: Array [0..4] of record
86 Msg: String;
87 Time: Word;
88 end;
90 function GetStrACmd(var Str: String): String;
91 var
92 a: Integer;
93 begin
94 Result := '';
95 for a := 1 to Length(Str) do
96 if (a = Length(Str)) or (Str[a+1] = ';') then
97 begin
98 Result := Copy(Str, 1, a);
99 Delete(Str, 1, a+1);
100 Str := Trim(Str);
101 Exit;
102 end;
103 end;
105 function ParseAlias(Str: String): SArray;
106 begin
107 Result := nil;
109 Str := Trim(Str);
111 if Str = '' then
112 Exit;
114 while Str <> '' do
115 begin
116 SetLength(Result, Length(Result)+1);
117 Result[High(Result)] := GetStrACmd(Str);
118 end;
119 end;
121 procedure ConsoleCommands(P: SArray);
122 var
123 Cmd, s: String;
124 a, b: Integer;
125 F: TextFile;
126 begin
127 Cmd := LowerCase(P[0]);
128 s := '';
130 if Cmd = 'clear' then
131 begin
132 //ConsoleHistory := nil;
133 cbufClear();
134 conSkipLines := 0;
136 for a := 0 to High(MsgArray) do
137 with MsgArray[a] do
138 begin
139 Msg := '';
140 Time := 0;
141 end;
142 end;
144 if Cmd = 'clearhistory' then
145 CommandHistory := nil;
147 if Cmd = 'showhistory' then
148 if CommandHistory <> nil then
149 begin
150 g_Console_Add('');
151 for a := 0 to High(CommandHistory) do
152 g_Console_Add(' '+CommandHistory[a]);
153 end;
155 if Cmd = 'commands' then
156 begin
157 g_Console_Add('');
158 g_Console_Add('Commands list:');
159 for a := High(Commands) downto 0 do
160 g_Console_Add(' '+Commands[a].Cmd);
161 end;
163 if Cmd = 'time' then
164 g_Console_Add(TimeToStr(Now), True);
166 if Cmd = 'date' then
167 g_Console_Add(DateToStr(Now), True);
169 if Cmd = 'echo' then
170 if Length(P) > 1 then
171 begin
172 if P[1] = 'ololo' then
173 gCheats := True
174 else
175 begin
176 s := '';
177 for a := 1 to High(P) do
178 s := s + P[a] + ' ';
179 g_Console_Add(b_Text_Format(s), True);
180 end;
181 end
182 else
183 g_Console_Add('');
185 if Cmd = 'dump' then
186 begin
187 (*
188 if ConsoleHistory <> nil then
189 begin
190 if Length(P) > 1 then
191 s := P[1]
192 else
193 s := GameDir+'/console.txt';
195 {$I-}
196 AssignFile(F, s);
197 Rewrite(F);
198 if IOResult <> 0 then
199 begin
200 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
201 CloseFile(F);
202 Exit;
203 end;
205 for a := 0 to High(ConsoleHistory) do
206 WriteLn(F, ConsoleHistory[a]);
208 CloseFile(F);
209 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
210 {$I+}
211 end;
212 *)
213 end;
215 if Cmd = 'exec' then
216 begin
217 // exec <filename>
218 if Length(P) > 1 then
219 begin
220 s := GameDir+'/'+P[1];
222 {$I-}
223 AssignFile(F, s);
224 Reset(F);
225 if IOResult <> 0 then
226 begin
227 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
228 CloseFile(F);
229 Exit;
230 end;
231 g_Console_Add(Format(_lc[I_CONSOLE_EXEC], [s]));
233 while not EOF(F) do
234 begin
235 ReadLn(F, s);
236 if IOResult <> 0 then
237 begin
238 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
239 CloseFile(F);
240 Exit;
241 end;
242 if Pos('#', s) <> 1 then // script comment
243 begin
244 // prevents endless loops
245 Inc(RecursionDepth);
246 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
247 if not RecursionLimitHit then
248 g_Console_Process(s, True);
249 Dec(RecursionDepth);
250 end;
251 end;
252 if (RecursionDepth = 0) and RecursionLimitHit then
253 begin
254 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
255 RecursionLimitHit := False;
256 end;
258 CloseFile(F);
259 {$I+}
260 end
261 else
262 g_Console_Add('exec <script file>');
263 end;
265 if Cmd = 'alias' then
266 begin
267 // alias [alias_name] [commands]
268 if Length(P) > 1 then
269 begin
270 for a := 0 to High(Aliases) do
271 if Aliases[a].Name = P[1] then
272 begin
273 if Length(P) > 2 then
274 Aliases[a].Commands := ParseAlias(P[2])
275 else
276 for b := 0 to High(Aliases[a].Commands) do
277 g_Console_Add(Aliases[a].Commands[b]);
278 Exit;
279 end;
280 SetLength(Aliases, Length(Aliases)+1);
281 a := High(Aliases);
282 Aliases[a].Name := P[1];
283 if Length(P) > 2 then
284 Aliases[a].Commands := ParseAlias(P[2])
285 else
286 for b := 0 to High(Aliases[a].Commands) do
287 g_Console_Add(Aliases[a].Commands[b]);
288 end else
289 for a := 0 to High(Aliases) do
290 if Aliases[a].Commands <> nil then
291 g_Console_Add(Aliases[a].Name);
292 end;
294 if Cmd = 'call' then
295 begin
296 // call <alias_name>
297 if Length(P) > 1 then
298 begin
299 if Aliases = nil then
300 Exit;
301 for a := 0 to High(Aliases) do
302 if Aliases[a].Name = P[1] then
303 begin
304 if Aliases[a].Commands <> nil then
305 begin
306 // with this system proper endless loop detection seems either impossible
307 // or very dirty to implement, so let's have this instead
308 // prevents endless loops
309 for b := 0 to High(Aliases[a].Commands) do
310 begin
311 Inc(RecursionDepth);
312 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
313 if not RecursionLimitHit then
314 g_Console_Process(Aliases[a].Commands[b], True);
315 Dec(RecursionDepth);
316 end;
317 if (RecursionDepth = 0) and RecursionLimitHit then
318 begin
319 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
320 RecursionLimitHit := False;
321 end;
322 end;
323 Exit;
324 end;
325 end
326 else
327 g_Console_Add('call <alias name>');
328 end;
329 end;
331 procedure WhitelistCommand(Cmd: string);
332 var
333 a: Integer;
334 begin
335 SetLength(Whitelist, Length(Whitelist)+1);
336 a := High(Whitelist);
337 Whitelist[a] := LowerCase(Cmd);
338 end;
340 procedure AddCommand(Cmd: String; Proc: TCmdProc);
341 var
342 a: Integer;
343 begin
344 SetLength(Commands, Length(Commands)+1);
345 a := High(Commands);
346 Commands[a].Cmd := LowerCase(Cmd);
347 Commands[a].Proc := Proc;
348 end;
350 procedure g_Console_Init();
351 var
352 a: Integer;
353 begin
354 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
355 Cons_Y := -(gScreenHeight div 2);
356 gConsoleShow := False;
357 gChatShow := False;
358 Cons_Shown := False;
359 CPos := 1;
361 for a := 0 to High(MsgArray) do
362 with MsgArray[a] do
363 begin
364 Msg := '';
365 Time := 0;
366 end;
368 AddCommand('clear', ConsoleCommands);
369 AddCommand('clearhistory', ConsoleCommands);
370 AddCommand('showhistory', ConsoleCommands);
371 AddCommand('commands', ConsoleCommands);
372 AddCommand('time', ConsoleCommands);
373 AddCommand('date', ConsoleCommands);
374 AddCommand('echo', ConsoleCommands);
375 AddCommand('dump', ConsoleCommands);
376 AddCommand('exec', ConsoleCommands);
377 AddCommand('alias', ConsoleCommands);
378 AddCommand('call', ConsoleCommands);
380 AddCommand('d_window', DebugCommands);
381 AddCommand('d_sounds', DebugCommands);
382 AddCommand('d_frames', DebugCommands);
383 AddCommand('d_winmsg', DebugCommands);
384 AddCommand('d_monoff', DebugCommands);
385 AddCommand('d_botoff', DebugCommands);
386 AddCommand('d_monster', DebugCommands);
387 AddCommand('d_health', DebugCommands);
388 AddCommand('d_player', DebugCommands);
389 AddCommand('d_joy', DebugCommands);
391 AddCommand('pf_draw_frame', ProfilerCommands);
392 AddCommand('pf_update_frame', ProfilerCommands);
393 AddCommand('pf_coldet', ProfilerCommands);
394 AddCommand('r_sq_draw', ProfilerCommands);
395 AddCommand('r_sq_use_grid', ProfilerCommands);
396 AddCommand('r_sq_use_tree', ProfilerCommands);
397 AddCommand('dbg_sq_coldet', ProfilerCommands);
399 AddCommand('t_dump_node_queries', ProfilerCommands);
401 AddCommand('sq_use_grid', ProfilerCommands);
402 AddCommand('sq_use_tree', ProfilerCommands);
404 AddCommand('mon_sq_enabled', ProfilerCommands);
406 AddCommand('p1_name', GameCVars);
407 AddCommand('p2_name', GameCVars);
408 AddCommand('p1_color', GameCVars);
409 AddCommand('p2_color', GameCVars);
410 AddCommand('r_showfps', GameCVars);
411 AddCommand('r_showtime', GameCVars);
412 AddCommand('r_showscore', GameCVars);
413 AddCommand('r_showlives', GameCVars);
414 AddCommand('r_showstat', GameCVars);
415 AddCommand('r_showkillmsg', GameCVars);
416 AddCommand('r_showspect', GameCVars);
417 AddCommand('r_showping', GameCVars);
418 AddCommand('g_gamemode', GameCVars);
419 AddCommand('g_friendlyfire', GameCVars);
420 AddCommand('g_weaponstay', GameCVars);
421 AddCommand('g_allow_exit', GameCVars);
422 AddCommand('g_allow_monsters', GameCVars);
423 AddCommand('g_bot_vsmonsters', GameCVars);
424 AddCommand('g_bot_vsplayers', GameCVars);
425 AddCommand('g_scorelimit', GameCVars);
426 AddCommand('g_timelimit', GameCVars);
427 AddCommand('g_maxlives', GameCVars);
428 AddCommand('g_warmuptime', GameCVars);
429 AddCommand('net_interp', GameCVars);
430 AddCommand('net_forceplayerupdate', GameCVars);
431 AddCommand('net_predictself', GameCVars);
432 AddCommand('sv_name', GameCVars);
433 AddCommand('sv_passwd', GameCVars);
434 AddCommand('sv_maxplrs', GameCVars);
435 AddCommand('sv_public', GameCVars);
436 AddCommand('sv_intertime', GameCVars);
438 AddCommand('quit', GameCommands);
439 AddCommand('exit', GameCommands);
440 AddCommand('pause', GameCommands);
441 AddCommand('endgame', GameCommands);
442 AddCommand('restart', GameCommands);
443 AddCommand('addbot', GameCommands);
444 AddCommand('bot_add', GameCommands);
445 AddCommand('bot_addlist', GameCommands);
446 AddCommand('bot_addred', GameCommands);
447 AddCommand('bot_addblue', GameCommands);
448 AddCommand('bot_removeall', GameCommands);
449 AddCommand('chat', GameCommands);
450 AddCommand('teamchat', GameCommands);
451 AddCommand('game', GameCommands);
452 AddCommand('host', GameCommands);
453 AddCommand('map', GameCommands);
454 AddCommand('nextmap', GameCommands);
455 AddCommand('endmap', GameCommands);
456 AddCommand('goodbye', GameCommands);
457 AddCommand('suicide', GameCommands);
458 AddCommand('spectate', GameCommands);
459 AddCommand('ready', GameCommands);
460 AddCommand('kick', GameCommands);
461 AddCommand('kick_id', GameCommands);
462 AddCommand('ban', GameCommands);
463 AddCommand('permban', GameCommands);
464 AddCommand('ban_id', GameCommands);
465 AddCommand('permban_id', GameCommands);
466 AddCommand('unban', GameCommands);
467 AddCommand('connect', GameCommands);
468 AddCommand('disconnect', GameCommands);
469 AddCommand('reconnect', GameCommands);
470 AddCommand('say', GameCommands);
471 AddCommand('tell', GameCommands);
472 AddCommand('overtime', GameCommands);
473 AddCommand('rcon_password', GameCommands);
474 AddCommand('rcon', GameCommands);
475 AddCommand('callvote', GameCommands);
476 AddCommand('vote', GameCommands);
477 AddCommand('clientlist', GameCommands);
478 AddCommand('event', GameCommands);
480 AddCommand('god', GameCheats);
481 AddCommand('notarget', GameCheats);
482 AddCommand('give', GameCheats); // "exit" too ;-)
483 AddCommand('open', GameCheats);
484 AddCommand('fly', GameCheats);
485 AddCommand('noclip', GameCheats);
486 AddCommand('speedy', GameCheats);
487 AddCommand('jumpy', GameCheats);
488 AddCommand('noreload', GameCheats);
489 AddCommand('aimline', GameCheats);
490 AddCommand('automap', GameCheats);
492 WhitelistCommand('say');
493 WhitelistCommand('tell');
494 WhitelistCommand('overtime');
495 WhitelistCommand('ready');
496 WhitelistCommand('map');
497 WhitelistCommand('nextmap');
498 WhitelistCommand('endmap');
499 WhitelistCommand('restart');
500 WhitelistCommand('kick');
501 WhitelistCommand('ban');
503 WhitelistCommand('addbot');
504 WhitelistCommand('bot_add');
505 WhitelistCommand('bot_addred');
506 WhitelistCommand('bot_addblue');
507 WhitelistCommand('bot_removeall');
509 WhitelistCommand('g_gamemode');
510 WhitelistCommand('g_friendlyfire');
511 WhitelistCommand('g_weaponstay');
512 WhitelistCommand('g_allow_exit');
513 WhitelistCommand('g_allow_monsters');
514 WhitelistCommand('g_scorelimit');
515 WhitelistCommand('g_timelimit');
517 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
518 g_Console_Add('');
519 end;
521 procedure g_Console_Update();
522 var
523 a, b: Integer;
524 begin
525 if Cons_Shown then
526 begin
527 // Â ïðîöåññå îòêðûòèÿ:
528 if gConsoleShow and (Cons_Y < 0) then
529 begin
530 Cons_Y := Cons_Y+Step;
531 end;
533 // Â ïðîöåññå çàêðûòèÿ:
534 if (not gConsoleShow) and
535 (Cons_Y > -(gScreenHeight div 2)) then
536 Cons_Y := Cons_Y-Step;
538 // Îêîí÷àòåëüíî îòêðûëàñü:
539 if Cons_Y > 0 then
540 Cons_Y := 0;
542 // Îêîí÷àòåëüíî çàêðûëàñü:
543 if Cons_Y <= (-(gScreenHeight div 2)) then
544 begin
545 Cons_Y := -(gScreenHeight div 2);
546 Cons_Shown := False;
547 end;
548 end;
550 a := 0;
551 while a <= High(MsgArray) do
552 begin
553 if MsgArray[a].Time > 0 then
554 begin
555 if MsgArray[a].Time = 1 then
556 begin
557 if a < High(MsgArray) then
558 begin
559 for b := a to High(MsgArray)-1 do
560 MsgArray[b] := MsgArray[b+1];
562 MsgArray[High(MsgArray)].Time := 0;
564 a := a - 1;
565 end;
566 end
567 else
568 Dec(MsgArray[a].Time);
569 end;
571 a := a + 1;
572 end;
573 end;
576 procedure drawConsoleText ();
577 var
578 CWidth, CHeight: Byte;
579 ty: Integer;
580 sp, ep: LongWord;
581 skip: Integer;
583 procedure putLine (sp, ep: LongWord);
584 var
585 p: LongWord;
586 wdt, cw: Integer;
587 begin
588 p := sp;
589 wdt := 0;
590 while p <> ep do
591 begin
592 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
593 if wdt+cw > gScreenWidth-8 then break;
594 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
595 Inc(wdt, cw);
596 cbufNext(p);
597 end;
598 if p <> ep then putLine(p, ep); // do rest of the line first
599 // now print our part
600 if skip = 0 then
601 begin
602 ep := p;
603 p := sp;
604 wdt := 2;
605 while p <> ep do
606 begin
607 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
608 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
609 Inc(wdt, cw);
610 cbufNext(p);
611 end;
612 Dec(ty, CHeight);
613 end
614 else
615 begin
616 Dec(skip);
617 end;
618 end;
620 begin
621 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
622 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
623 skip := conSkipLines;
624 cbufLastLine(sp, ep);
625 repeat
626 putLine(sp, ep);
627 if ty+CHeight <= 0 then break;
628 until not cbufLineUp(sp, ep);
629 end;
631 procedure g_Console_Draw();
632 var
633 CWidth, CHeight: Byte;
634 mfW, mfH: Word;
635 a, b: Integer;
636 begin
637 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
639 for a := 0 to High(MsgArray) do
640 if MsgArray[a].Time > 0 then
641 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
642 gStdFont, True);
644 if not Cons_Shown then
645 begin
646 if gChatShow then
647 begin
648 if gChatTeam then
649 begin
650 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
651 gStdFont, 255, 255, 255, 1, True);
652 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
653 gStdFont, 255, 255, 255, 1, True);
654 end
655 else
656 begin
657 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
658 gStdFont, 255, 255, 255, 1, True);
659 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
660 gStdFont, 255, 255, 255, 1, True);
661 end;
662 end;
663 Exit;
664 end;
666 if gDebugMode then
667 begin
668 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
669 a := (gScreenWidth - 2*mfW) div 2;
670 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
671 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
672 _RGB(128, 0, 0), 2.0);
673 end;
675 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
676 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
678 drawConsoleText();
679 (*
680 if ConsoleHistory <> nil then
681 begin
682 b := 0;
683 if CHeight > 0 then
684 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
685 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
687 b := Max(b-Offset, 0);
688 d := Max(High(ConsoleHistory)-Offset, 0);
690 c := 2;
691 for a := d downto b do
692 begin
693 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
694 gStdFont, True);
695 c := c + 1;
696 end;
697 end;
698 *)
700 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
701 end;
703 procedure g_Console_Switch();
704 begin
705 if gChatShow then Exit;
706 gConsoleShow := not gConsoleShow;
707 Cons_Shown := True;
708 end;
710 procedure g_Console_Chat_Switch(Team: Boolean = False);
711 begin
712 if gConsoleShow then Exit;
713 if not g_Game_IsNet then Exit;
714 gChatShow := not gChatShow;
715 gChatTeam := Team;
716 if gChatShow then
717 gChatEnter := False;
718 Line := '';
719 CPos := 1;
720 end;
722 procedure g_Console_Char(C: Char);
723 begin
724 if gChatShow and (not gChatEnter) then
725 Exit;
726 Insert(C, Line, CPos);
727 CPos := CPos + 1;
728 end;
731 var
732 tcomplist: array of string = nil;
734 procedure Complete ();
735 var
736 i, c: Integer;
737 tused: Integer;
738 ll, lpfx, cmd: string;
739 begin
740 if (Length(Line) = 0) then
741 begin
742 g_Console_Add('');
743 for i := 0 to High(Commands) do
744 begin
745 if (Commands[i].Cmd <> 'goobers') then
746 begin
747 g_Console_Add(' '+Commands[i].Cmd);
748 end;
749 end;
750 exit;
751 end;
753 ll := LowerCase(Line);
754 lpfx := '';
756 // build completion list
757 tused := 0;
758 for i := 0 to High(Commands) do
759 begin
760 cmd := Commands[i].Cmd;
761 if (cmd = 'goobers') then continue;
762 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
763 begin
764 if (tused = Length(tcomplist)) then SetLength(tcomplist, Length(tcomplist)+128);
765 tcomplist[tused] := cmd;
766 Inc(tused);
767 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
768 end;
769 end;
771 // get longest prefix
772 for i := 0 to tused-1 do
773 begin
774 cmd := tcomplist[i];
775 for c := 1 to Length(lpfx) do
776 begin
777 if (c > Length(cmd)) then break;
778 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
779 end;
780 end;
782 if (tused = 0) then exit;
784 if (tused = 1) then
785 begin
786 Line := tcomplist[0]+' ';
787 CPos := Length(Line)+1;
788 end
789 else
790 begin
791 // has longest prefix?
792 if (Length(lpfx) > Length(ll)) then
793 begin
794 Line := lpfx;
795 CPos:= Length(Line)+1;
796 end
797 else
798 begin
799 g_Console_Add('');
800 for i := 0 to tused-1 do g_Console_Add(' '+tcomplist[i]);
801 end;
802 end;
803 end;
806 procedure g_Console_Control(K: Word);
807 begin
808 case K of
809 IK_BACKSPACE:
810 if (Length(Line) > 0) and (CPos > 1) then
811 begin
812 Delete(Line, CPos-1, 1);
813 CPos := CPos-1;
814 end;
815 IK_DELETE:
816 if (Length(Line) > 0) and (CPos <= Length(Line)) then
817 Delete(Line, CPos, 1);
818 IK_LEFT, IK_KPLEFT:
819 if CPos > 1 then
820 CPos := CPos - 1;
821 IK_RIGHT, IK_KPRIGHT:
822 if CPos <= Length(Line) then
823 CPos := CPos + 1;
824 IK_RETURN, IK_KPRETURN:
825 begin
826 if Cons_Shown then
827 g_Console_Process(Line)
828 else
829 if gChatShow then
830 begin
831 if (Length(Line) > 0) and g_Game_IsNet then
832 begin
833 if gChatTeam then
834 begin
835 if g_Game_IsClient then
836 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
837 else
838 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
839 NET_CHAT_TEAM, gPlayer1Settings.Team);
840 end
841 else
842 begin
843 if g_Game_IsClient then
844 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
845 else
846 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
847 NET_CHAT_PLAYER);
848 end;
849 end;
851 Line := '';
852 CPos := 1;
853 gChatShow := False;
854 gJustChatted := True;
855 end;
856 end;
857 IK_TAB:
858 if not gChatShow then
859 Complete();
860 IK_DOWN, IK_KPDOWN:
861 if not gChatShow then
862 if (CommandHistory <> nil) and
863 (CmdIndex < Length(CommandHistory)) then
864 begin
865 if CmdIndex < Length(CommandHistory)-1 then
866 CmdIndex := CmdIndex + 1;
867 Line := CommandHistory[CmdIndex];
868 CPos := Length(Line) + 1;
869 end;
870 IK_UP, IK_KPUP:
871 if not gChatShow then
872 if (CommandHistory <> nil) and
873 (CmdIndex <= Length(CommandHistory)) then
874 begin
875 if CmdIndex > 0 then
876 CmdIndex := CmdIndex - 1;
877 Line := CommandHistory[CmdIndex];
878 Cpos := Length(Line) + 1;
879 end;
880 IK_PAGEUP, IK_KPPAGEUP: // PgUp
881 if not gChatShow then Inc(conSkipLines);
882 IK_PAGEDN, IK_KPPAGEDN: // PgDown
883 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
884 IK_HOME, IK_KPHOME:
885 CPos := 1;
886 IK_END, IK_KPEND:
887 CPos := Length(Line) + 1;
888 end;
889 end;
891 function GetStr(var Str: String): String;
892 var
893 a, b: Integer;
894 begin
895 Result := '';
896 if Str[1] = '"' then
897 begin
898 for b := 1 to Length(Str) do
899 if (b = Length(Str)) or (Str[b+1] = '"') then
900 begin
901 Result := Copy(Str, 2, b-1);
902 Delete(Str, 1, b+1);
903 Str := Trim(Str);
904 Exit;
905 end;
906 end;
908 for a := 1 to Length(Str) do
909 if (a = Length(Str)) or (Str[a+1] = ' ') then
910 begin
911 Result := Copy(Str, 1, a);
912 Delete(Str, 1, a+1);
913 Str := Trim(Str);
914 Exit;
915 end;
916 end;
918 function ParseString(Str: String): SArray;
919 begin
920 Result := nil;
922 Str := Trim(Str);
924 if Str = '' then
925 Exit;
927 while Str <> '' do
928 begin
929 SetLength(Result, Length(Result)+1);
930 Result[High(Result)] := GetStr(Str);
931 end;
932 end;
934 procedure g_Console_Add (L: string; Show: Boolean=false);
936 procedure conmsg (s: AnsiString);
937 var
938 a: Integer;
939 begin
940 if length(s) = 0 then exit;
941 for a := 0 to High(MsgArray) do
942 begin
943 with MsgArray[a] do
944 begin
945 if Time = 0 then
946 begin
947 Msg := s;
948 Time := MsgTime;
949 exit;
950 end;
951 end;
952 end;
953 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
954 with MsgArray[High(MsgArray)] do
955 begin
956 Msg := L;
957 Time := MsgTime;
958 end;
959 end;
961 var
962 f: Integer;
963 begin
964 // put it to console
965 cbufPut(L);
966 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
968 // now show 'em out of console too
969 Show := Show and gAllowConsoleMessages;
970 if Show and gShowMessages then
971 begin
972 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
973 while length(L) > 0 do
974 begin
975 f := Pos(#10, L);
976 if f <= 0 then f := length(L)+1;
977 conmsg(Copy(L, 1, f-1));
978 Delete(L, 1, f);
979 end;
980 end;
982 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
983 //ConsoleHistory[High(ConsoleHistory)] := L;
985 (*
986 {$IFDEF HEADLESS}
987 e_WriteLog('CON: ' + L, MSG_NOTIFY);
988 {$ENDIF}
989 *)
990 end;
992 procedure g_Console_Clear();
993 begin
994 //ConsoleHistory := nil;
995 cbufClear();
996 conSkipLines := 0;
997 end;
999 procedure AddToHistory(L: String);
1000 var
1001 len: Integer;
1002 begin
1003 len := Length(CommandHistory);
1005 if (len = 0) or
1006 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1007 begin
1008 SetLength(CommandHistory, len+1);
1009 CommandHistory[len] := L;
1010 end;
1012 CmdIndex := Length(CommandHistory);
1013 end;
1015 function g_Console_CommandBlacklisted(C: String): Boolean;
1016 var
1017 Arr: SArray;
1018 i: Integer;
1019 begin
1020 Result := True;
1022 Arr := nil;
1024 if Trim(C) = '' then
1025 Exit;
1027 Arr := ParseString(C);
1028 if Arr = nil then
1029 Exit;
1031 for i := 0 to High(Whitelist) do
1032 if Whitelist[i] = LowerCase(Arr[0]) then
1033 Result := False;
1034 end;
1036 procedure g_Console_Process(L: String; Quiet: Boolean = False);
1037 var
1038 Arr: SArray;
1039 i: Integer;
1040 begin
1041 Arr := nil;
1043 if Trim(L) = '' then
1044 Exit;
1046 conSkipLines := 0; // "unscroll"
1048 if L = 'goobers' then
1049 begin
1050 Line := '';
1051 CPos := 1;
1052 gCheats := true;
1053 g_Console_Add('Your memory serves you well.');
1054 exit;
1055 end;
1057 if not Quiet then
1058 begin
1059 g_Console_Add('> '+L);
1060 Line := '';
1061 CPos := 1;
1062 end;
1064 Arr := ParseString(L);
1065 if Arr = nil then
1066 Exit;
1068 if Commands = nil then
1069 Exit;
1071 if not Quiet then
1072 AddToHistory(L);
1074 for i := 0 to High(Commands) do
1075 if Commands[i].Cmd = LowerCase(Arr[0]) then
1076 if @Commands[i].Proc <> nil then
1077 begin
1078 Commands[i].Proc(Arr);
1079 Exit;
1080 end;
1082 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1083 end;
1085 end.