DEADSOFTWARE

profiler
[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] := 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 := 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('dpp', ProfilerCommands);
392 AddCommand('dpu', ProfilerCommands);
394 AddCommand('p1_name', GameCVars);
395 AddCommand('p2_name', GameCVars);
396 AddCommand('p1_color', GameCVars);
397 AddCommand('p2_color', GameCVars);
398 AddCommand('r_showfps', GameCVars);
399 AddCommand('r_showtime', GameCVars);
400 AddCommand('r_showscore', GameCVars);
401 AddCommand('r_showlives', GameCVars);
402 AddCommand('r_showstat', GameCVars);
403 AddCommand('r_showkillmsg', GameCVars);
404 AddCommand('r_showspect', GameCVars);
405 AddCommand('r_showping', GameCVars);
406 AddCommand('g_gamemode', GameCVars);
407 AddCommand('g_friendlyfire', GameCVars);
408 AddCommand('g_weaponstay', GameCVars);
409 AddCommand('g_allow_exit', GameCVars);
410 AddCommand('g_allow_monsters', GameCVars);
411 AddCommand('g_bot_vsmonsters', GameCVars);
412 AddCommand('g_bot_vsplayers', GameCVars);
413 AddCommand('g_scorelimit', GameCVars);
414 AddCommand('g_timelimit', GameCVars);
415 AddCommand('g_maxlives', GameCVars);
416 AddCommand('g_warmuptime', GameCVars);
417 AddCommand('net_interp', GameCVars);
418 AddCommand('net_forceplayerupdate', GameCVars);
419 AddCommand('net_predictself', GameCVars);
420 AddCommand('sv_name', GameCVars);
421 AddCommand('sv_passwd', GameCVars);
422 AddCommand('sv_maxplrs', GameCVars);
423 AddCommand('sv_public', GameCVars);
424 AddCommand('sv_intertime', GameCVars);
426 AddCommand('quit', GameCommands);
427 AddCommand('exit', GameCommands);
428 AddCommand('pause', GameCommands);
429 AddCommand('endgame', GameCommands);
430 AddCommand('restart', GameCommands);
431 AddCommand('addbot', GameCommands);
432 AddCommand('bot_add', GameCommands);
433 AddCommand('bot_addlist', GameCommands);
434 AddCommand('bot_addred', GameCommands);
435 AddCommand('bot_addblue', GameCommands);
436 AddCommand('bot_removeall', GameCommands);
437 AddCommand('chat', GameCommands);
438 AddCommand('teamchat', GameCommands);
439 AddCommand('game', GameCommands);
440 AddCommand('host', GameCommands);
441 AddCommand('map', GameCommands);
442 AddCommand('nextmap', GameCommands);
443 AddCommand('endmap', GameCommands);
444 AddCommand('goodbye', GameCommands);
445 AddCommand('suicide', GameCommands);
446 AddCommand('spectate', GameCommands);
447 AddCommand('ready', GameCommands);
448 AddCommand('kick', GameCommands);
449 AddCommand('kick_id', GameCommands);
450 AddCommand('ban', GameCommands);
451 AddCommand('permban', GameCommands);
452 AddCommand('ban_id', GameCommands);
453 AddCommand('permban_id', GameCommands);
454 AddCommand('unban', GameCommands);
455 AddCommand('connect', GameCommands);
456 AddCommand('disconnect', GameCommands);
457 AddCommand('reconnect', GameCommands);
458 AddCommand('say', GameCommands);
459 AddCommand('tell', GameCommands);
460 AddCommand('overtime', GameCommands);
461 AddCommand('rcon_password', GameCommands);
462 AddCommand('rcon', GameCommands);
463 AddCommand('callvote', GameCommands);
464 AddCommand('vote', GameCommands);
465 AddCommand('clientlist', GameCommands);
466 AddCommand('event', GameCommands);
468 AddCommand('god', GameCheats);
469 AddCommand('notarget', GameCheats);
470 AddCommand('give', GameCheats); // "exit" too ;-)
471 AddCommand('open', GameCheats);
472 AddCommand('fly', GameCheats);
473 AddCommand('noclip', GameCheats);
474 AddCommand('speedy', GameCheats);
475 AddCommand('jumpy', GameCheats);
476 AddCommand('noreload', GameCheats);
477 AddCommand('aimline', GameCheats);
478 AddCommand('automap', GameCheats);
480 WhitelistCommand('say');
481 WhitelistCommand('tell');
482 WhitelistCommand('overtime');
483 WhitelistCommand('ready');
484 WhitelistCommand('map');
485 WhitelistCommand('nextmap');
486 WhitelistCommand('endmap');
487 WhitelistCommand('restart');
488 WhitelistCommand('kick');
489 WhitelistCommand('ban');
491 WhitelistCommand('addbot');
492 WhitelistCommand('bot_add');
493 WhitelistCommand('bot_addred');
494 WhitelistCommand('bot_addblue');
495 WhitelistCommand('bot_removeall');
497 WhitelistCommand('g_gamemode');
498 WhitelistCommand('g_friendlyfire');
499 WhitelistCommand('g_weaponstay');
500 WhitelistCommand('g_allow_exit');
501 WhitelistCommand('g_allow_monsters');
502 WhitelistCommand('g_scorelimit');
503 WhitelistCommand('g_timelimit');
505 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
506 g_Console_Add('');
507 end;
509 procedure g_Console_Update();
510 var
511 a, b: Integer;
512 begin
513 if Cons_Shown then
514 begin
515 // Â ïðîöåññå îòêðûòèÿ:
516 if gConsoleShow and (Cons_Y < 0) then
517 begin
518 Cons_Y := Cons_Y+Step;
519 end;
521 // Â ïðîöåññå çàêðûòèÿ:
522 if (not gConsoleShow) and
523 (Cons_Y > -(gScreenHeight div 2)) then
524 Cons_Y := Cons_Y-Step;
526 // Îêîí÷àòåëüíî îòêðûëàñü:
527 if Cons_Y > 0 then
528 Cons_Y := 0;
530 // Îêîí÷àòåëüíî çàêðûëàñü:
531 if Cons_Y <= (-(gScreenHeight div 2)) then
532 begin
533 Cons_Y := -(gScreenHeight div 2);
534 Cons_Shown := False;
535 end;
536 end;
538 a := 0;
539 while a <= High(MsgArray) do
540 begin
541 if MsgArray[a].Time > 0 then
542 begin
543 if MsgArray[a].Time = 1 then
544 begin
545 if a < High(MsgArray) then
546 begin
547 for b := a to High(MsgArray)-1 do
548 MsgArray[b] := MsgArray[b+1];
550 MsgArray[High(MsgArray)].Time := 0;
552 a := a - 1;
553 end;
554 end
555 else
556 Dec(MsgArray[a].Time);
557 end;
559 a := a + 1;
560 end;
561 end;
564 procedure drawConsoleText ();
565 var
566 CWidth, CHeight: Byte;
567 ty: Integer;
568 sp, ep: LongWord;
569 skip: Integer;
571 procedure putLine (sp, ep: LongWord);
572 var
573 p: LongWord;
574 wdt, cw: Integer;
575 begin
576 p := sp;
577 wdt := 0;
578 while p <> ep do
579 begin
580 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
581 if wdt+cw > gScreenWidth-8 then break;
582 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
583 Inc(wdt, cw);
584 cbufNext(p);
585 end;
586 if p <> ep then putLine(p, ep); // do rest of the line first
587 // now print our part
588 if skip = 0 then
589 begin
590 ep := p;
591 p := sp;
592 wdt := 2;
593 while p <> ep do
594 begin
595 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
596 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
597 Inc(wdt, cw);
598 cbufNext(p);
599 end;
600 Dec(ty, CHeight);
601 end
602 else
603 begin
604 Dec(skip);
605 end;
606 end;
608 begin
609 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
610 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
611 skip := conSkipLines;
612 cbufLastLine(sp, ep);
613 repeat
614 putLine(sp, ep);
615 if ty+CHeight <= 0 then break;
616 until not cbufLineUp(sp, ep);
617 end;
619 procedure g_Console_Draw();
620 var
621 CWidth, CHeight: Byte;
622 mfW, mfH: Word;
623 a, b: Integer;
624 begin
625 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
627 for a := 0 to High(MsgArray) do
628 if MsgArray[a].Time > 0 then
629 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
630 gStdFont, True);
632 if not Cons_Shown then
633 begin
634 if gChatShow then
635 begin
636 if gChatTeam then
637 begin
638 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
639 gStdFont, 255, 255, 255, 1, True);
640 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
641 gStdFont, 255, 255, 255, 1, True);
642 end
643 else
644 begin
645 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
646 gStdFont, 255, 255, 255, 1, True);
647 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
648 gStdFont, 255, 255, 255, 1, True);
649 end;
650 end;
651 Exit;
652 end;
654 if gDebugMode then
655 begin
656 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
657 a := (gScreenWidth - 2*mfW) div 2;
658 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
659 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
660 _RGB(128, 0, 0), 2.0);
661 end;
663 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
664 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
666 drawConsoleText();
667 (*
668 if ConsoleHistory <> nil then
669 begin
670 b := 0;
671 if CHeight > 0 then
672 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
673 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
675 b := Max(b-Offset, 0);
676 d := Max(High(ConsoleHistory)-Offset, 0);
678 c := 2;
679 for a := d downto b do
680 begin
681 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
682 gStdFont, True);
683 c := c + 1;
684 end;
685 end;
686 *)
688 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
689 end;
691 procedure g_Console_Switch();
692 begin
693 if gChatShow then Exit;
694 gConsoleShow := not gConsoleShow;
695 Cons_Shown := True;
696 end;
698 procedure g_Console_Chat_Switch(Team: Boolean = False);
699 begin
700 if gConsoleShow then Exit;
701 if not g_Game_IsNet then Exit;
702 gChatShow := not gChatShow;
703 gChatTeam := Team;
704 if gChatShow then
705 gChatEnter := False;
706 Line := '';
707 CPos := 1;
708 end;
710 procedure g_Console_Char(C: Char);
711 begin
712 if gChatShow and (not gChatEnter) then
713 Exit;
714 Insert(C, Line, CPos);
715 CPos := CPos + 1;
716 end;
718 procedure Complete();
719 var
720 i: Integer;
721 t: Array of String;
722 begin
723 if Line = '' then
724 Exit;
726 t := nil;
728 for i := 0 to High(Commands) do
729 if LowerCase(Line) = LowerCase(Copy(Commands[i].Cmd, 0, Length(Line))) then
730 begin
731 SetLength(t, Length(t) + 1);
732 t[Length(t)-1] := Commands[i].Cmd;
733 end;
735 if t = nil then
736 Exit;
738 if Length(t) = 1 then
739 begin
740 Line := t[0]+' ';
741 CPos := Length(Line)+1;
742 end
743 else
744 begin
745 g_Console_Add('');
746 for i := 0 to High(t) do
747 g_Console_Add(' '+t[i]);
748 end;
749 end;
751 procedure g_Console_Control(K: Word);
752 begin
753 case K of
754 IK_BACKSPACE:
755 if (Length(Line) > 0) and (CPos > 1) then
756 begin
757 Delete(Line, CPos-1, 1);
758 CPos := CPos-1;
759 end;
760 IK_DELETE:
761 if (Length(Line) > 0) and (CPos <= Length(Line)) then
762 Delete(Line, CPos, 1);
763 IK_LEFT, IK_KPLEFT:
764 if CPos > 1 then
765 CPos := CPos - 1;
766 IK_RIGHT, IK_KPRIGHT:
767 if CPos <= Length(Line) then
768 CPos := CPos + 1;
769 IK_RETURN, IK_KPRETURN:
770 begin
771 if Cons_Shown then
772 g_Console_Process(Line)
773 else
774 if gChatShow then
775 begin
776 if (Length(Line) > 0) and g_Game_IsNet then
777 begin
778 if gChatTeam then
779 begin
780 if g_Game_IsClient then
781 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
782 else
783 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
784 NET_CHAT_TEAM, gPlayer1Settings.Team);
785 end
786 else
787 begin
788 if g_Game_IsClient then
789 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
790 else
791 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
792 NET_CHAT_PLAYER);
793 end;
794 end;
796 Line := '';
797 CPos := 1;
798 gChatShow := False;
799 gJustChatted := True;
800 end;
801 end;
802 IK_TAB:
803 if not gChatShow then
804 Complete();
805 IK_DOWN, IK_KPDOWN:
806 if not gChatShow then
807 if (CommandHistory <> nil) and
808 (CmdIndex < Length(CommandHistory)) then
809 begin
810 if CmdIndex < Length(CommandHistory)-1 then
811 CmdIndex := CmdIndex + 1;
812 Line := CommandHistory[CmdIndex];
813 CPos := Length(Line) + 1;
814 end;
815 IK_UP, IK_KPUP:
816 if not gChatShow then
817 if (CommandHistory <> nil) and
818 (CmdIndex <= Length(CommandHistory)) then
819 begin
820 if CmdIndex > 0 then
821 CmdIndex := CmdIndex - 1;
822 Line := CommandHistory[CmdIndex];
823 Cpos := Length(Line) + 1;
824 end;
825 IK_PAGEUP, IK_KPPAGEUP: // PgUp
826 if not gChatShow then Inc(conSkipLines);
827 IK_PAGEDN, IK_KPPAGEDN: // PgDown
828 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
829 IK_HOME, IK_KPHOME:
830 CPos := 1;
831 IK_END, IK_KPEND:
832 CPos := Length(Line) + 1;
833 end;
834 end;
836 function GetStr(var Str: String): String;
837 var
838 a, b: Integer;
839 begin
840 Result := '';
841 if Str[1] = '"' then
842 begin
843 for b := 1 to Length(Str) do
844 if (b = Length(Str)) or (Str[b+1] = '"') then
845 begin
846 Result := Copy(Str, 2, b-1);
847 Delete(Str, 1, b+1);
848 Str := Trim(Str);
849 Exit;
850 end;
851 end;
853 for a := 1 to Length(Str) do
854 if (a = Length(Str)) or (Str[a+1] = ' ') then
855 begin
856 Result := Copy(Str, 1, a);
857 Delete(Str, 1, a+1);
858 Str := Trim(Str);
859 Exit;
860 end;
861 end;
863 function ParseString(Str: String): SArray;
864 begin
865 Result := nil;
867 Str := Trim(Str);
869 if Str = '' then
870 Exit;
872 while Str <> '' do
873 begin
874 SetLength(Result, Length(Result)+1);
875 Result[High(Result)] := GetStr(Str);
876 end;
877 end;
879 procedure g_Console_Add (L: string; Show: Boolean=false);
881 procedure conmsg (s: AnsiString);
882 var
883 a: Integer;
884 begin
885 if length(s) = 0 then exit;
886 for a := 0 to High(MsgArray) do
887 begin
888 with MsgArray[a] do
889 begin
890 if Time = 0 then
891 begin
892 Msg := s;
893 Time := MsgTime;
894 exit;
895 end;
896 end;
897 end;
898 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
899 with MsgArray[High(MsgArray)] do
900 begin
901 Msg := L;
902 Time := MsgTime;
903 end;
904 end;
906 var
907 f: Integer;
908 begin
909 // put it to console
910 cbufPut(L);
911 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
913 // now show 'em out of console too
914 Show := Show and gAllowConsoleMessages;
915 if Show and gShowMessages then
916 begin
917 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
918 while length(L) > 0 do
919 begin
920 f := Pos(#10, L);
921 if f <= 0 then f := length(L)+1;
922 conmsg(Copy(L, 1, f-1));
923 Delete(L, 1, f);
924 end;
925 end;
927 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
928 //ConsoleHistory[High(ConsoleHistory)] := L;
930 (*
931 {$IFDEF HEADLESS}
932 e_WriteLog('CON: ' + L, MSG_NOTIFY);
933 {$ENDIF}
934 *)
935 end;
937 procedure g_Console_Clear();
938 begin
939 //ConsoleHistory := nil;
940 cbufClear();
941 conSkipLines := 0;
942 end;
944 procedure AddToHistory(L: String);
945 var
946 len: Integer;
947 begin
948 len := Length(CommandHistory);
950 if (len = 0) or
951 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
952 begin
953 SetLength(CommandHistory, len+1);
954 CommandHistory[len] := L;
955 end;
957 CmdIndex := Length(CommandHistory);
958 end;
960 function g_Console_CommandBlacklisted(C: String): Boolean;
961 var
962 Arr: SArray;
963 i: Integer;
964 begin
965 Result := True;
967 Arr := nil;
969 if Trim(C) = '' then
970 Exit;
972 Arr := ParseString(C);
973 if Arr = nil then
974 Exit;
976 for i := 0 to High(Whitelist) do
977 if Whitelist[i] = LowerCase(Arr[0]) then
978 Result := False;
979 end;
981 procedure g_Console_Process(L: String; Quiet: Boolean = False);
982 var
983 Arr: SArray;
984 i: Integer;
985 begin
986 Arr := nil;
988 if Trim(L) = '' then
989 Exit;
991 conSkipLines := 0; // "unscroll"
993 if L = 'goobers' then
994 begin
995 Line := '';
996 CPos := 1;
997 gCheats := true;
998 g_Console_Add('Your memory serves you well.');
999 exit;
1000 end;
1002 if not Quiet then
1003 begin
1004 g_Console_Add('> '+L);
1005 Line := '';
1006 CPos := 1;
1007 end;
1009 Arr := ParseString(L);
1010 if Arr = nil then
1011 Exit;
1013 if Commands = nil then
1014 Exit;
1016 if not Quiet then
1017 AddToHistory(L);
1019 for i := 0 to High(Commands) do
1020 if Commands[i].Cmd = LowerCase(Arr[0]) then
1021 if @Commands[i].Proc <> nil then
1022 begin
1023 Commands[i].Proc(Arr);
1024 Exit;
1025 end;
1027 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1028 end;
1030 end.