DEADSOFTWARE

more particle control options
[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('cd_sq_enabled', ProfilerCommands, 'accelerated spatial queries in map coldet');
409 AddCommand('mon_sq_enabled', ProfilerCommands, 'use accelerated spatial queries for monsters');
410 AddCommand('wtrace_sq_enabled', ProfilerCommands, 'use accelerated weapon hitscan trace');
412 AddCommand('pr_enabled', ProfilerCommands, 'enable/disable particles');
413 AddCommand('pr_phys_enabled', ProfilerCommands, 'enable/disable particle physics');
414 AddCommand('los_enabled', ProfilerCommands, 'enable/disable LOS calculations');
416 AddCommand('p1_name', GameCVars);
417 AddCommand('p2_name', GameCVars);
418 AddCommand('p1_color', GameCVars);
419 AddCommand('p2_color', GameCVars);
420 AddCommand('r_showfps', GameCVars);
421 AddCommand('r_showtime', GameCVars);
422 AddCommand('r_showscore', GameCVars);
423 AddCommand('r_showlives', GameCVars);
424 AddCommand('r_showstat', GameCVars);
425 AddCommand('r_showkillmsg', GameCVars);
426 AddCommand('r_showspect', GameCVars);
427 AddCommand('r_showping', GameCVars);
428 AddCommand('g_gamemode', GameCVars);
429 AddCommand('g_friendlyfire', GameCVars);
430 AddCommand('g_weaponstay', GameCVars);
431 AddCommand('g_allow_exit', GameCVars);
432 AddCommand('g_allow_monsters', GameCVars);
433 AddCommand('g_bot_vsmonsters', GameCVars);
434 AddCommand('g_bot_vsplayers', GameCVars);
435 AddCommand('g_scorelimit', GameCVars);
436 AddCommand('g_timelimit', GameCVars);
437 AddCommand('g_maxlives', GameCVars);
438 AddCommand('g_warmuptime', GameCVars);
439 AddCommand('net_interp', GameCVars);
440 AddCommand('net_forceplayerupdate', GameCVars);
441 AddCommand('net_predictself', GameCVars);
442 AddCommand('sv_name', GameCVars);
443 AddCommand('sv_passwd', GameCVars);
444 AddCommand('sv_maxplrs', GameCVars);
445 AddCommand('sv_public', GameCVars);
446 AddCommand('sv_intertime', GameCVars);
448 AddCommand('quit', GameCommands);
449 AddCommand('exit', GameCommands);
450 AddCommand('pause', GameCommands);
451 AddCommand('endgame', GameCommands);
452 AddCommand('restart', GameCommands);
453 AddCommand('addbot', GameCommands);
454 AddCommand('bot_add', GameCommands);
455 AddCommand('bot_addlist', GameCommands);
456 AddCommand('bot_addred', GameCommands);
457 AddCommand('bot_addblue', GameCommands);
458 AddCommand('bot_removeall', GameCommands);
459 AddCommand('chat', GameCommands);
460 AddCommand('teamchat', GameCommands);
461 AddCommand('game', GameCommands);
462 AddCommand('host', GameCommands);
463 AddCommand('map', GameCommands);
464 AddCommand('nextmap', GameCommands);
465 AddCommand('endmap', GameCommands);
466 AddCommand('goodbye', GameCommands);
467 AddCommand('suicide', GameCommands);
468 AddCommand('spectate', GameCommands);
469 AddCommand('ready', GameCommands);
470 AddCommand('kick', GameCommands);
471 AddCommand('kick_id', GameCommands);
472 AddCommand('ban', GameCommands);
473 AddCommand('permban', GameCommands);
474 AddCommand('ban_id', GameCommands);
475 AddCommand('permban_id', GameCommands);
476 AddCommand('unban', GameCommands);
477 AddCommand('connect', GameCommands);
478 AddCommand('disconnect', GameCommands);
479 AddCommand('reconnect', GameCommands);
480 AddCommand('say', GameCommands);
481 AddCommand('tell', GameCommands);
482 AddCommand('overtime', GameCommands);
483 AddCommand('rcon_password', GameCommands);
484 AddCommand('rcon', GameCommands);
485 AddCommand('callvote', GameCommands);
486 AddCommand('vote', GameCommands);
487 AddCommand('clientlist', GameCommands);
488 AddCommand('event', GameCommands);
490 AddCommand('god', GameCheats);
491 AddCommand('notarget', GameCheats);
492 AddCommand('give', GameCheats); // "exit" too ;-)
493 AddCommand('open', GameCheats);
494 AddCommand('fly', GameCheats);
495 AddCommand('noclip', GameCheats);
496 AddCommand('speedy', GameCheats);
497 AddCommand('jumpy', GameCheats);
498 AddCommand('noreload', GameCheats);
499 AddCommand('aimline', GameCheats);
500 AddCommand('automap', GameCheats);
502 WhitelistCommand('say');
503 WhitelistCommand('tell');
504 WhitelistCommand('overtime');
505 WhitelistCommand('ready');
506 WhitelistCommand('map');
507 WhitelistCommand('nextmap');
508 WhitelistCommand('endmap');
509 WhitelistCommand('restart');
510 WhitelistCommand('kick');
511 WhitelistCommand('ban');
513 WhitelistCommand('addbot');
514 WhitelistCommand('bot_add');
515 WhitelistCommand('bot_addred');
516 WhitelistCommand('bot_addblue');
517 WhitelistCommand('bot_removeall');
519 WhitelistCommand('g_gamemode');
520 WhitelistCommand('g_friendlyfire');
521 WhitelistCommand('g_weaponstay');
522 WhitelistCommand('g_allow_exit');
523 WhitelistCommand('g_allow_monsters');
524 WhitelistCommand('g_scorelimit');
525 WhitelistCommand('g_timelimit');
527 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
528 g_Console_Add('');
529 end;
531 procedure g_Console_Update();
532 var
533 a, b: Integer;
534 begin
535 if Cons_Shown then
536 begin
537 // Â ïðîöåññå îòêðûòèÿ:
538 if gConsoleShow and (Cons_Y < 0) then
539 begin
540 Cons_Y := Cons_Y+Step;
541 end;
543 // Â ïðîöåññå çàêðûòèÿ:
544 if (not gConsoleShow) and
545 (Cons_Y > -(gScreenHeight div 2)) then
546 Cons_Y := Cons_Y-Step;
548 // Îêîí÷àòåëüíî îòêðûëàñü:
549 if Cons_Y > 0 then
550 Cons_Y := 0;
552 // Îêîí÷àòåëüíî çàêðûëàñü:
553 if Cons_Y <= (-(gScreenHeight div 2)) then
554 begin
555 Cons_Y := -(gScreenHeight div 2);
556 Cons_Shown := False;
557 end;
558 end;
560 a := 0;
561 while a <= High(MsgArray) do
562 begin
563 if MsgArray[a].Time > 0 then
564 begin
565 if MsgArray[a].Time = 1 then
566 begin
567 if a < High(MsgArray) then
568 begin
569 for b := a to High(MsgArray)-1 do
570 MsgArray[b] := MsgArray[b+1];
572 MsgArray[High(MsgArray)].Time := 0;
574 a := a - 1;
575 end;
576 end
577 else
578 Dec(MsgArray[a].Time);
579 end;
581 a := a + 1;
582 end;
583 end;
586 procedure drawConsoleText ();
587 var
588 CWidth, CHeight: Byte;
589 ty: Integer;
590 sp, ep: LongWord;
591 skip: Integer;
593 procedure putLine (sp, ep: LongWord);
594 var
595 p: LongWord;
596 wdt, cw: Integer;
597 begin
598 p := sp;
599 wdt := 0;
600 while p <> ep do
601 begin
602 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
603 if wdt+cw > gScreenWidth-8 then break;
604 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
605 Inc(wdt, cw);
606 cbufNext(p);
607 end;
608 if p <> ep then putLine(p, ep); // do rest of the line first
609 // now print our part
610 if skip = 0 then
611 begin
612 ep := p;
613 p := sp;
614 wdt := 2;
615 while p <> ep do
616 begin
617 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
618 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
619 Inc(wdt, cw);
620 cbufNext(p);
621 end;
622 Dec(ty, CHeight);
623 end
624 else
625 begin
626 Dec(skip);
627 end;
628 end;
630 begin
631 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
632 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
633 skip := conSkipLines;
634 cbufLastLine(sp, ep);
635 repeat
636 putLine(sp, ep);
637 if ty+CHeight <= 0 then break;
638 until not cbufLineUp(sp, ep);
639 end;
641 procedure g_Console_Draw();
642 var
643 CWidth, CHeight: Byte;
644 mfW, mfH: Word;
645 a, b: Integer;
646 begin
647 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
649 for a := 0 to High(MsgArray) do
650 if MsgArray[a].Time > 0 then
651 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
652 gStdFont, True);
654 if not Cons_Shown then
655 begin
656 if gChatShow then
657 begin
658 if gChatTeam then
659 begin
660 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
661 gStdFont, 255, 255, 255, 1, True);
662 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
663 gStdFont, 255, 255, 255, 1, True);
664 end
665 else
666 begin
667 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
668 gStdFont, 255, 255, 255, 1, True);
669 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
670 gStdFont, 255, 255, 255, 1, True);
671 end;
672 end;
673 Exit;
674 end;
676 if gDebugMode then
677 begin
678 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
679 a := (gScreenWidth - 2*mfW) div 2;
680 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
681 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
682 _RGB(128, 0, 0), 2.0);
683 end;
685 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
686 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
688 drawConsoleText();
689 (*
690 if ConsoleHistory <> nil then
691 begin
692 b := 0;
693 if CHeight > 0 then
694 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
695 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
697 b := Max(b-Offset, 0);
698 d := Max(High(ConsoleHistory)-Offset, 0);
700 c := 2;
701 for a := d downto b do
702 begin
703 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
704 gStdFont, True);
705 c := c + 1;
706 end;
707 end;
708 *)
710 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
711 end;
713 procedure g_Console_Switch();
714 begin
715 if gChatShow then Exit;
716 gConsoleShow := not gConsoleShow;
717 Cons_Shown := True;
718 end;
720 procedure g_Console_Chat_Switch(Team: Boolean = False);
721 begin
722 if gConsoleShow then Exit;
723 if not g_Game_IsNet then Exit;
724 gChatShow := not gChatShow;
725 gChatTeam := Team;
726 if gChatShow then
727 gChatEnter := False;
728 Line := '';
729 CPos := 1;
730 end;
732 procedure g_Console_Char(C: Char);
733 begin
734 if gChatShow and (not gChatEnter) then
735 Exit;
736 Insert(C, Line, CPos);
737 CPos := CPos + 1;
738 end;
741 var
742 tcomplist: array of string = nil;
743 tcompidx: array of Integer = nil;
745 procedure Complete ();
746 var
747 i, c: Integer;
748 tused: Integer;
749 ll, lpfx, cmd: string;
750 begin
751 if (Length(Line) = 0) then
752 begin
753 g_Console_Add('');
754 for i := 0 to High(Commands) do
755 begin
756 if not Commands[i].hidden then
757 begin
758 if (Length(Commands[i].help) > 0) then
759 begin
760 g_Console_Add(' '+Commands[i].Cmd+' -- '+Commands[i].help);
761 end
762 else
763 begin
764 g_Console_Add(' '+Commands[i].Cmd);
765 end;
766 end;
767 end;
768 exit;
769 end;
771 ll := LowerCase(Line);
772 lpfx := '';
774 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
775 begin
776 ll := Copy(ll, 0, Length(ll)-1);
777 for i := 0 to High(Commands) do
778 begin
779 if Commands[i].hidden then continue;
780 if (Commands[i].Cmd = ll) then
781 begin
782 if (Length(Commands[i].help) > 0) then
783 begin
784 g_Console_Add(' '+Commands[i].Cmd+' -- '+Commands[i].help);
785 end;
786 end;
787 end;
788 exit;
789 end;
791 // build completion list
792 tused := 0;
793 for i := 0 to High(Commands) do
794 begin
795 if Commands[i].hidden then continue;
796 cmd := Commands[i].Cmd;
797 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
798 begin
799 if (tused = Length(tcomplist)) then
800 begin
801 SetLength(tcomplist, Length(tcomplist)+128);
802 SetLength(tcompidx, Length(tcompidx)+128);
803 end;
804 tcomplist[tused] := cmd;
805 tcompidx[tused] := i;
806 Inc(tused);
807 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
808 end;
809 end;
811 // get longest prefix
812 for i := 0 to tused-1 do
813 begin
814 cmd := tcomplist[i];
815 for c := 1 to Length(lpfx) do
816 begin
817 if (c > Length(cmd)) then break;
818 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
819 end;
820 end;
822 if (tused = 0) then exit;
824 if (tused = 1) then
825 begin
826 Line := tcomplist[0]+' ';
827 CPos := Length(Line)+1;
828 end
829 else
830 begin
831 // has longest prefix?
832 if (Length(lpfx) > Length(ll)) then
833 begin
834 Line := lpfx;
835 CPos:= Length(Line)+1;
836 end
837 else
838 begin
839 g_Console_Add('');
840 for i := 0 to tused-1 do
841 begin
842 if (Length(Commands[tcompidx[i]].help) > 0) then
843 begin
844 g_Console_Add(' '+tcomplist[i]+' -- '+Commands[tcompidx[i]].help);
845 end
846 else
847 begin
848 g_Console_Add(' '+tcomplist[i]);
849 end;
850 end;
851 end;
852 end;
853 end;
856 procedure g_Console_Control(K: Word);
857 begin
858 case K of
859 IK_BACKSPACE:
860 if (Length(Line) > 0) and (CPos > 1) then
861 begin
862 Delete(Line, CPos-1, 1);
863 CPos := CPos-1;
864 end;
865 IK_DELETE:
866 if (Length(Line) > 0) and (CPos <= Length(Line)) then
867 Delete(Line, CPos, 1);
868 IK_LEFT, IK_KPLEFT:
869 if CPos > 1 then
870 CPos := CPos - 1;
871 IK_RIGHT, IK_KPRIGHT:
872 if CPos <= Length(Line) then
873 CPos := CPos + 1;
874 IK_RETURN, IK_KPRETURN:
875 begin
876 if Cons_Shown then
877 g_Console_Process(Line)
878 else
879 if gChatShow then
880 begin
881 if (Length(Line) > 0) and g_Game_IsNet then
882 begin
883 if gChatTeam then
884 begin
885 if g_Game_IsClient then
886 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
887 else
888 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
889 NET_CHAT_TEAM, gPlayer1Settings.Team);
890 end
891 else
892 begin
893 if g_Game_IsClient then
894 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
895 else
896 MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
897 NET_CHAT_PLAYER);
898 end;
899 end;
901 Line := '';
902 CPos := 1;
903 gChatShow := False;
904 gJustChatted := True;
905 end;
906 end;
907 IK_TAB:
908 if not gChatShow then
909 Complete();
910 IK_DOWN, IK_KPDOWN:
911 if not gChatShow then
912 if (CommandHistory <> nil) and
913 (CmdIndex < Length(CommandHistory)) then
914 begin
915 if CmdIndex < Length(CommandHistory)-1 then
916 CmdIndex := CmdIndex + 1;
917 Line := CommandHistory[CmdIndex];
918 CPos := Length(Line) + 1;
919 end;
920 IK_UP, IK_KPUP:
921 if not gChatShow then
922 if (CommandHistory <> nil) and
923 (CmdIndex <= Length(CommandHistory)) then
924 begin
925 if CmdIndex > 0 then
926 CmdIndex := CmdIndex - 1;
927 Line := CommandHistory[CmdIndex];
928 Cpos := Length(Line) + 1;
929 end;
930 IK_PAGEUP, IK_KPPAGEUP: // PgUp
931 if not gChatShow then Inc(conSkipLines);
932 IK_PAGEDN, IK_KPPAGEDN: // PgDown
933 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
934 IK_HOME, IK_KPHOME:
935 CPos := 1;
936 IK_END, IK_KPEND:
937 CPos := Length(Line) + 1;
938 end;
939 end;
941 function GetStr(var Str: String): String;
942 var
943 a, b: Integer;
944 begin
945 Result := '';
946 if Str[1] = '"' then
947 begin
948 for b := 1 to Length(Str) do
949 if (b = Length(Str)) or (Str[b+1] = '"') then
950 begin
951 Result := Copy(Str, 2, b-1);
952 Delete(Str, 1, b+1);
953 Str := Trim(Str);
954 Exit;
955 end;
956 end;
958 for a := 1 to Length(Str) do
959 if (a = Length(Str)) or (Str[a+1] = ' ') then
960 begin
961 Result := Copy(Str, 1, a);
962 Delete(Str, 1, a+1);
963 Str := Trim(Str);
964 Exit;
965 end;
966 end;
968 function ParseString(Str: String): SArray;
969 begin
970 Result := nil;
972 Str := Trim(Str);
974 if Str = '' then
975 Exit;
977 while Str <> '' do
978 begin
979 SetLength(Result, Length(Result)+1);
980 Result[High(Result)] := GetStr(Str);
981 end;
982 end;
984 procedure g_Console_Add (L: string; Show: Boolean=false);
986 procedure conmsg (s: AnsiString);
987 var
988 a: Integer;
989 begin
990 if length(s) = 0 then exit;
991 for a := 0 to High(MsgArray) do
992 begin
993 with MsgArray[a] do
994 begin
995 if Time = 0 then
996 begin
997 Msg := s;
998 Time := MsgTime;
999 exit;
1000 end;
1001 end;
1002 end;
1003 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1004 with MsgArray[High(MsgArray)] do
1005 begin
1006 Msg := L;
1007 Time := MsgTime;
1008 end;
1009 end;
1011 var
1012 f: Integer;
1013 begin
1014 // put it to console
1015 cbufPut(L);
1016 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1018 // now show 'em out of console too
1019 Show := Show and gAllowConsoleMessages;
1020 if Show and gShowMessages then
1021 begin
1022 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1023 while length(L) > 0 do
1024 begin
1025 f := Pos(#10, L);
1026 if f <= 0 then f := length(L)+1;
1027 conmsg(Copy(L, 1, f-1));
1028 Delete(L, 1, f);
1029 end;
1030 end;
1032 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1033 //ConsoleHistory[High(ConsoleHistory)] := L;
1035 (*
1036 {$IFDEF HEADLESS}
1037 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1038 {$ENDIF}
1039 *)
1040 end;
1042 procedure g_Console_Clear();
1043 begin
1044 //ConsoleHistory := nil;
1045 cbufClear();
1046 conSkipLines := 0;
1047 end;
1049 procedure AddToHistory(L: String);
1050 var
1051 len: Integer;
1052 begin
1053 len := Length(CommandHistory);
1055 if (len = 0) or
1056 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1057 begin
1058 SetLength(CommandHistory, len+1);
1059 CommandHistory[len] := L;
1060 end;
1062 CmdIndex := Length(CommandHistory);
1063 end;
1065 function g_Console_CommandBlacklisted(C: String): Boolean;
1066 var
1067 Arr: SArray;
1068 i: Integer;
1069 begin
1070 Result := True;
1072 Arr := nil;
1074 if Trim(C) = '' then
1075 Exit;
1077 Arr := ParseString(C);
1078 if Arr = nil then
1079 Exit;
1081 for i := 0 to High(Whitelist) do
1082 if Whitelist[i] = LowerCase(Arr[0]) then
1083 Result := False;
1084 end;
1086 procedure g_Console_Process(L: String; Quiet: Boolean = False);
1087 var
1088 Arr: SArray;
1089 i: Integer;
1090 begin
1091 Arr := nil;
1093 if Trim(L) = '' then
1094 Exit;
1096 conSkipLines := 0; // "unscroll"
1098 if L = 'goobers' then
1099 begin
1100 Line := '';
1101 CPos := 1;
1102 gCheats := true;
1103 g_Console_Add('Your memory serves you well.');
1104 exit;
1105 end;
1107 if not Quiet then
1108 begin
1109 g_Console_Add('> '+L);
1110 Line := '';
1111 CPos := 1;
1112 end;
1114 Arr := ParseString(L);
1115 if Arr = nil then
1116 Exit;
1118 if Commands = nil then
1119 Exit;
1121 if not Quiet then
1122 AddToHistory(L);
1124 for i := 0 to High(Commands) do
1125 if Commands[i].Cmd = LowerCase(Arr[0]) then
1126 if @Commands[i].Proc <> nil then
1127 begin
1128 Commands[i].Proc(Arr);
1129 Exit;
1130 end;
1132 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1133 end;
1135 end.