DEADSOFTWARE

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