DEADSOFTWARE

Reserved key range for virtual keyboard + alternative virtkbd layout
[d2df-sdl.git] / src / game / g_console.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_console;
19 interface
21 uses
22 utils; // for SSArray
24 procedure g_Console_Init ();
25 procedure g_Console_Update ();
26 procedure g_Console_Draw ();
27 procedure g_Console_Switch ();
28 procedure g_Console_Char (C: AnsiChar);
29 procedure g_Console_Control (K: Word);
30 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
31 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
32 procedure g_Console_Clear ();
33 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
35 procedure conwriteln (const s: AnsiString; show: Boolean=false);
36 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
38 // <0: no arg; 0/1: true/false
39 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
41 procedure g_Console_Chat_Switch (team: Boolean=false);
43 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
44 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
46 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
47 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
50 var
51 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
52 gChatShow: Boolean = false;
53 gChatTeam: Boolean = false;
54 gAllowConsoleMessages: Boolean = true;
55 gChatEnter: Boolean = true;
56 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
59 implementation
61 uses
62 g_textures, g_main, e_graphics, e_input, g_game,
63 SysUtils, g_basic, g_options, Math, g_touch,
64 g_menu, g_language, g_net, g_netmsg, e_log, conbuf;
67 type
68 PCommand = ^TCommand;
70 TCmdProc = procedure (p: SSArray);
71 TCmdProcEx = procedure (me: PCommand; p: SSArray);
73 TCommand = record
74 cmd: AnsiString;
75 proc: TCmdProc;
76 procEx: TCmdProcEx;
77 help: AnsiString;
78 hidden: Boolean;
79 ptr: Pointer; // various data
80 msg: AnsiString; // message for var changes
81 cheat: Boolean;
82 end;
84 TAlias = record
85 name: AnsiString;
86 commands: SSArray;
87 end;
90 const
91 Step = 32;
92 Alpha = 25;
93 MsgTime = 144;
94 MaxScriptRecursion = 16;
96 DEBUG_STRING = 'DEBUG MODE';
98 var
99 ID: DWORD;
100 RecursionDepth: Word = 0;
101 RecursionLimitHit: Boolean = False;
102 Cons_Y: SmallInt;
103 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
104 Line: AnsiString;
105 CPos: Word;
106 //ConsoleHistory: SSArray;
107 CommandHistory: SSArray;
108 Whitelist: SSArray;
109 commands: Array of TCommand = nil;
110 Aliases: Array of TAlias = nil;
111 CmdIndex: Word;
112 conSkipLines: Integer = 0;
113 MsgArray: Array [0..4] of record
114 Msg: AnsiString;
115 Time: Word;
116 end;
119 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
120 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
121 var
122 pos: Integer = 1;
123 frac: Single = 1;
124 slen: Integer;
125 begin
126 result := false;
127 res := 0;
128 slen := Length(s);
129 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
130 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
131 if (pos > slen) then exit;
132 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
133 // integral part
134 while (pos <= slen) do
135 begin
136 if (s[pos] < '0') or (s[pos] > '9') then break;
137 res := res*10+Byte(s[pos])-48;
138 Inc(pos);
139 end;
140 if (pos <= slen) then
141 begin
142 // must be a dot
143 if (s[pos] <> '.') then exit;
144 Inc(pos);
145 while (pos <= slen) do
146 begin
147 if (s[pos] < '0') or (s[pos] > '9') then break;
148 frac := frac/10;
149 res += frac*(Byte(s[pos])-48);
150 Inc(pos);
151 end;
152 end;
153 if (pos <= slen) then exit; // oops
154 result := true;
155 end;
158 // ////////////////////////////////////////////////////////////////////////// //
159 // <0: no arg; 0/1: true/false; 666: toggle
160 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
161 begin
162 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
163 result := 0;
164 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
165 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
166 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
167 (CompareText(p[idx], 't') = 0) then result := 666;
168 end;
171 procedure boolVarHandler (me: PCommand; p: SSArray);
172 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
173 begin
174 if (Length(p) > 2) then
175 begin
176 conwritefln('too many arguments to ''%s''', [p[0]]);
177 end
178 else
179 begin
180 case conGetBoolArg(p, 1) of
181 -1: begin end;
182 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
183 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
184 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
185 end;
186 if (Length(msg) = 0) then msg := p[0] else msg += ':';
187 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
188 end;
189 end;
190 begin
191 binaryFlag(PBoolean(me.ptr)^, me.msg);
192 end;
195 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
196 var
197 f: Integer;
198 cp: PCommand;
199 begin
200 f := Length(commands);
201 SetLength(commands, f+1);
202 cp := @commands[f];
203 cp.cmd := LowerCase(conname);
204 cp.proc := nil;
205 cp.procEx := boolVarHandler;
206 cp.help := ahelp;
207 cp.hidden := ahidden;
208 cp.ptr := pvar;
209 cp.msg := amsg;
210 cp.cheat := acheat;
211 end;
214 // ////////////////////////////////////////////////////////////////////////// //
215 type
216 PVarSingle = ^TVarSingle;
217 TVarSingle = record
218 val: PSingle;
219 min, max, def: Single; // default will be starting value
220 end;
223 procedure singleVarHandler (me: PCommand; p: SSArray);
224 var
225 pv: PVarSingle;
226 nv: Single;
227 msg: AnsiString;
228 begin
229 if (Length(p) > 2) then
230 begin
231 conwritefln('too many arguments to ''%s''', [me.cmd]);
232 exit;
233 end;
234 pv := PVarSingle(me.ptr);
235 if (Length(p) = 2) then
236 begin
237 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
238 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
239 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
240 (CompareText(p[1], 'ona') = 0) then
241 begin
242 pv.val^ := pv.def;
243 end
244 else
245 begin
246 if not conParseFloat(nv, p[1]) then
247 begin
248 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
249 exit;
250 end;
251 if (nv < pv.min) then nv := pv.min;
252 if (nv > pv.max) then nv := pv.max;
253 pv.val^ := nv;
254 end;
255 end;
256 msg := me.msg;
257 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
258 conwritefln('%s %s', [msg, pv.val^]);
259 end;
262 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
263 var
264 f: Integer;
265 cp: PCommand;
266 pv: PVarSingle;
267 begin
268 GetMem(pv, sizeof(TVarSingle));
269 pv.val := pvar;
270 pv.min := amin;
271 pv.max := amax;
272 pv.def := pvar^;
273 f := Length(commands);
274 SetLength(commands, f+1);
275 cp := @commands[f];
276 cp.cmd := LowerCase(conname);
277 cp.proc := nil;
278 cp.procEx := singleVarHandler;
279 cp.help := ahelp;
280 cp.hidden := ahidden;
281 cp.ptr := pv;
282 cp.msg := amsg;
283 cp.cheat := acheat;
284 end;
287 // ////////////////////////////////////////////////////////////////////////// //
288 function GetStrACmd(var Str: AnsiString): AnsiString;
289 var
290 a: Integer;
291 begin
292 Result := '';
293 for a := 1 to Length(Str) do
294 if (a = Length(Str)) or (Str[a+1] = ';') then
295 begin
296 Result := Copy(Str, 1, a);
297 Delete(Str, 1, a+1);
298 Str := Trim(Str);
299 Exit;
300 end;
301 end;
303 function ParseAlias(Str: AnsiString): SSArray;
304 begin
305 Result := nil;
307 Str := Trim(Str);
309 if Str = '' then
310 Exit;
312 while Str <> '' do
313 begin
314 SetLength(Result, Length(Result)+1);
315 Result[High(Result)] := GetStrACmd(Str);
316 end;
317 end;
319 procedure ConsoleCommands(p: SSArray);
320 var
321 cmd, s: AnsiString;
322 a, b: Integer;
323 F: TextFile;
324 begin
325 cmd := LowerCase(p[0]);
326 s := '';
328 if cmd = 'clear' then
329 begin
330 //ConsoleHistory := nil;
331 cbufClear();
332 conSkipLines := 0;
334 for a := 0 to High(MsgArray) do
335 with MsgArray[a] do
336 begin
337 Msg := '';
338 Time := 0;
339 end;
340 end;
342 if cmd = 'clearhistory' then
343 CommandHistory := nil;
345 if cmd = 'showhistory' then
346 if CommandHistory <> nil then
347 begin
348 g_Console_Add('');
349 for a := 0 to High(CommandHistory) do
350 g_Console_Add(' '+CommandHistory[a]);
351 end;
353 if cmd = 'commands' then
354 begin
355 g_Console_Add('');
356 g_Console_Add('commands list:');
357 for a := High(commands) downto 0 do
358 begin
359 if (Length(commands[a].help) > 0) then
360 begin
361 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
362 end
363 else
364 begin
365 g_Console_Add(' '+commands[a].cmd);
366 end;
367 end;
368 end;
370 if cmd = 'time' then
371 g_Console_Add(TimeToStr(Now), True);
373 if cmd = 'date' then
374 g_Console_Add(DateToStr(Now), True);
376 if cmd = 'echo' then
377 if Length(p) > 1 then
378 begin
379 if p[1] = 'ololo' then
380 gCheats := True
381 else
382 begin
383 s := '';
384 for a := 1 to High(p) do
385 s := s + p[a] + ' ';
386 g_Console_Add(b_Text_Format(s), True);
387 end;
388 end
389 else
390 g_Console_Add('');
392 if cmd = 'dump' then
393 begin
394 (*
395 if ConsoleHistory <> nil then
396 begin
397 if Length(P) > 1 then
398 s := P[1]
399 else
400 s := GameDir+'/console.txt';
402 {$I-}
403 AssignFile(F, s);
404 Rewrite(F);
405 if IOResult <> 0 then
406 begin
407 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
408 CloseFile(F);
409 Exit;
410 end;
412 for a := 0 to High(ConsoleHistory) do
413 WriteLn(F, ConsoleHistory[a]);
415 CloseFile(F);
416 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
417 {$I+}
418 end;
419 *)
420 end;
422 if cmd = 'exec' then
423 begin
424 // exec <filename>
425 if Length(p) > 1 then
426 begin
427 s := GameDir+'/'+p[1];
429 {$I-}
430 AssignFile(F, s);
431 Reset(F);
432 if IOResult <> 0 then
433 begin
434 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
435 CloseFile(F);
436 Exit;
437 end;
438 g_Console_Add(Format(_lc[I_CONSOLE_EXEC], [s]));
440 while not EOF(F) do
441 begin
442 ReadLn(F, s);
443 if IOResult <> 0 then
444 begin
445 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
446 CloseFile(F);
447 Exit;
448 end;
449 if Pos('#', s) <> 1 then // script comment
450 begin
451 // prevents endless loops
452 Inc(RecursionDepth);
453 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
454 if not RecursionLimitHit then
455 g_Console_Process(s, True);
456 Dec(RecursionDepth);
457 end;
458 end;
459 if (RecursionDepth = 0) and RecursionLimitHit then
460 begin
461 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
462 RecursionLimitHit := False;
463 end;
465 CloseFile(F);
466 {$I+}
467 end
468 else
469 g_Console_Add('exec <script file>');
470 end;
472 if (cmd = 'ver') or (cmd = 'version') then
473 begin
474 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
475 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
476 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
477 end;
479 if cmd = 'alias' then
480 begin
481 // alias [alias_name] [commands]
482 if Length(p) > 1 then
483 begin
484 for a := 0 to High(Aliases) do
485 if Aliases[a].name = p[1] then
486 begin
487 if Length(p) > 2 then
488 Aliases[a].commands := ParseAlias(p[2])
489 else
490 for b := 0 to High(Aliases[a].commands) do
491 g_Console_Add(Aliases[a].commands[b]);
492 Exit;
493 end;
494 SetLength(Aliases, Length(Aliases)+1);
495 a := High(Aliases);
496 Aliases[a].name := p[1];
497 if Length(p) > 2 then
498 Aliases[a].commands := ParseAlias(p[2])
499 else
500 for b := 0 to High(Aliases[a].commands) do
501 g_Console_Add(Aliases[a].commands[b]);
502 end else
503 for a := 0 to High(Aliases) do
504 if Aliases[a].commands <> nil then
505 g_Console_Add(Aliases[a].name);
506 end;
508 if cmd = 'call' then
509 begin
510 // call <alias_name>
511 if Length(p) > 1 then
512 begin
513 if Aliases = nil then
514 Exit;
515 for a := 0 to High(Aliases) do
516 if Aliases[a].name = p[1] then
517 begin
518 if Aliases[a].commands <> nil then
519 begin
520 // with this system proper endless loop detection seems either impossible
521 // or very dirty to implement, so let's have this instead
522 // prevents endless loops
523 for b := 0 to High(Aliases[a].commands) do
524 begin
525 Inc(RecursionDepth);
526 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
527 if not RecursionLimitHit then
528 g_Console_Process(Aliases[a].commands[b], True);
529 Dec(RecursionDepth);
530 end;
531 if (RecursionDepth = 0) and RecursionLimitHit then
532 begin
533 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
534 RecursionLimitHit := False;
535 end;
536 end;
537 Exit;
538 end;
539 end
540 else
541 g_Console_Add('call <alias name>');
542 end;
543 end;
545 procedure WhitelistCommand(cmd: AnsiString);
546 var
547 a: Integer;
548 begin
549 SetLength(Whitelist, Length(Whitelist)+1);
550 a := High(Whitelist);
551 Whitelist[a] := LowerCase(cmd);
552 end;
554 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
555 var
556 a: Integer;
557 cp: PCommand;
558 begin
559 SetLength(commands, Length(commands)+1);
560 a := High(commands);
561 cp := @commands[a];
562 cp.cmd := LowerCase(cmd);
563 cp.proc := proc;
564 cp.procEx := nil;
565 cp.help := ahelp;
566 cp.hidden := ahidden;
567 cp.ptr := nil;
568 cp.msg := '';
569 cp.cheat := acheat;
570 end;
573 procedure segfault (p: SSArray);
574 var
575 pp: PByte = nil;
576 begin
577 pp^ := 0;
578 end;
581 procedure g_Console_Init();
582 var
583 a: Integer;
584 begin
585 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
586 Cons_Y := -(gScreenHeight div 2);
587 gConsoleShow := False;
588 gChatShow := False;
589 Cons_Shown := False;
590 CPos := 1;
592 for a := 0 to High(MsgArray) do
593 with MsgArray[a] do
594 begin
595 Msg := '';
596 Time := 0;
597 end;
599 AddCommand('segfault', segfault, 'make segfault');
601 AddCommand('clear', ConsoleCommands, 'clear console');
602 AddCommand('clearhistory', ConsoleCommands);
603 AddCommand('showhistory', ConsoleCommands);
604 AddCommand('commands', ConsoleCommands);
605 AddCommand('time', ConsoleCommands);
606 AddCommand('date', ConsoleCommands);
607 AddCommand('echo', ConsoleCommands);
608 AddCommand('dump', ConsoleCommands);
609 AddCommand('exec', ConsoleCommands);
610 AddCommand('alias', ConsoleCommands);
611 AddCommand('call', ConsoleCommands);
612 AddCommand('ver', ConsoleCommands);
613 AddCommand('version', ConsoleCommands);
615 AddCommand('d_window', DebugCommands);
616 AddCommand('d_sounds', DebugCommands);
617 AddCommand('d_frames', DebugCommands);
618 AddCommand('d_winmsg', DebugCommands);
619 AddCommand('d_monoff', DebugCommands);
620 AddCommand('d_botoff', DebugCommands);
621 AddCommand('d_monster', DebugCommands);
622 AddCommand('d_health', DebugCommands);
623 AddCommand('d_player', DebugCommands);
624 AddCommand('d_joy', DebugCommands);
625 AddCommand('d_mem', DebugCommands);
627 AddCommand('p1_name', GameCVars);
628 AddCommand('p2_name', GameCVars);
629 AddCommand('p1_color', GameCVars);
630 AddCommand('p2_color', GameCVars);
631 AddCommand('r_showfps', GameCVars);
632 AddCommand('r_showtime', GameCVars);
633 AddCommand('r_showscore', GameCVars);
634 AddCommand('r_showlives', GameCVars);
635 AddCommand('r_showstat', GameCVars);
636 AddCommand('r_showkillmsg', GameCVars);
637 AddCommand('r_showspect', GameCVars);
638 AddCommand('r_showping', GameCVars);
639 AddCommand('g_gamemode', GameCVars);
640 AddCommand('g_friendlyfire', GameCVars);
641 AddCommand('g_weaponstay', GameCVars);
642 AddCommand('g_allow_exit', GameCVars);
643 AddCommand('g_allow_monsters', GameCVars);
644 AddCommand('g_bot_vsmonsters', GameCVars);
645 AddCommand('g_bot_vsplayers', GameCVars);
646 AddCommand('g_scorelimit', GameCVars);
647 AddCommand('g_timelimit', GameCVars);
648 AddCommand('g_maxlives', GameCVars);
649 AddCommand('g_warmuptime', GameCVars);
650 AddCommand('net_interp', GameCVars);
651 AddCommand('net_forceplayerupdate', GameCVars);
652 AddCommand('net_predictself', GameCVars);
653 AddCommand('sv_name', GameCVars);
654 AddCommand('sv_passwd', GameCVars);
655 AddCommand('sv_maxplrs', GameCVars);
656 AddCommand('sv_public', GameCVars);
657 AddCommand('sv_intertime', GameCVars);
659 AddCommand('quit', GameCommands);
660 AddCommand('exit', GameCommands);
661 AddCommand('pause', GameCommands);
662 AddCommand('endgame', GameCommands);
663 AddCommand('restart', GameCommands);
664 AddCommand('addbot', GameCommands);
665 AddCommand('bot_add', GameCommands);
666 AddCommand('bot_addlist', GameCommands);
667 AddCommand('bot_addred', GameCommands);
668 AddCommand('bot_addblue', GameCommands);
669 AddCommand('bot_removeall', GameCommands);
670 AddCommand('chat', GameCommands);
671 AddCommand('teamchat', GameCommands);
672 AddCommand('game', GameCommands);
673 AddCommand('host', GameCommands);
674 AddCommand('map', GameCommands);
675 AddCommand('nextmap', GameCommands);
676 AddCommand('endmap', GameCommands);
677 AddCommand('goodbye', GameCommands);
678 AddCommand('suicide', GameCommands);
679 AddCommand('spectate', GameCommands);
680 AddCommand('ready', GameCommands);
681 AddCommand('kick', GameCommands);
682 AddCommand('kick_id', GameCommands);
683 AddCommand('ban', GameCommands);
684 AddCommand('permban', GameCommands);
685 AddCommand('ban_id', GameCommands);
686 AddCommand('permban_id', GameCommands);
687 AddCommand('unban', GameCommands);
688 AddCommand('connect', GameCommands);
689 AddCommand('disconnect', GameCommands);
690 AddCommand('reconnect', GameCommands);
691 AddCommand('say', GameCommands);
692 AddCommand('tell', GameCommands);
693 AddCommand('overtime', GameCommands);
694 AddCommand('rcon_password', GameCommands);
695 AddCommand('rcon', GameCommands);
696 AddCommand('callvote', GameCommands);
697 AddCommand('vote', GameCommands);
698 AddCommand('clientlist', GameCommands);
699 AddCommand('event', GameCommands);
701 AddCommand('god', GameCheats);
702 AddCommand('notarget', GameCheats);
703 AddCommand('give', GameCheats); // "exit" too ;-)
704 AddCommand('open', GameCheats);
705 AddCommand('fly', GameCheats);
706 AddCommand('noclip', GameCheats);
707 AddCommand('speedy', GameCheats);
708 AddCommand('jumpy', GameCheats);
709 AddCommand('noreload', GameCheats);
710 AddCommand('aimline', GameCheats);
711 AddCommand('automap', GameCheats);
713 WhitelistCommand('say');
714 WhitelistCommand('tell');
715 WhitelistCommand('overtime');
716 WhitelistCommand('ready');
717 WhitelistCommand('map');
718 WhitelistCommand('nextmap');
719 WhitelistCommand('endmap');
720 WhitelistCommand('restart');
721 WhitelistCommand('kick');
722 WhitelistCommand('ban');
724 WhitelistCommand('addbot');
725 WhitelistCommand('bot_add');
726 WhitelistCommand('bot_addred');
727 WhitelistCommand('bot_addblue');
728 WhitelistCommand('bot_removeall');
730 WhitelistCommand('g_gamemode');
731 WhitelistCommand('g_friendlyfire');
732 WhitelistCommand('g_weaponstay');
733 WhitelistCommand('g_allow_exit');
734 WhitelistCommand('g_allow_monsters');
735 WhitelistCommand('g_scorelimit');
736 WhitelistCommand('g_timelimit');
738 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
739 g_Console_Add('');
740 end;
742 procedure g_Console_Update();
743 var
744 a, b: Integer;
745 begin
746 if Cons_Shown then
747 begin
748 // Â ïðîöåññå îòêðûòèÿ:
749 if gConsoleShow and (Cons_Y < 0) then
750 begin
751 Cons_Y := Cons_Y+Step;
752 end;
754 // Â ïðîöåññå çàêðûòèÿ:
755 if (not gConsoleShow) and
756 (Cons_Y > -(gScreenHeight div 2)) then
757 Cons_Y := Cons_Y-Step;
759 // Îêîí÷àòåëüíî îòêðûëàñü:
760 if Cons_Y > 0 then
761 Cons_Y := 0;
763 // Îêîí÷àòåëüíî çàêðûëàñü:
764 if Cons_Y <= (-(gScreenHeight div 2)) then
765 begin
766 Cons_Y := -(gScreenHeight div 2);
767 Cons_Shown := False;
768 end;
769 end;
771 a := 0;
772 while a <= High(MsgArray) do
773 begin
774 if MsgArray[a].Time > 0 then
775 begin
776 if MsgArray[a].Time = 1 then
777 begin
778 if a < High(MsgArray) then
779 begin
780 for b := a to High(MsgArray)-1 do
781 MsgArray[b] := MsgArray[b+1];
783 MsgArray[High(MsgArray)].Time := 0;
785 a := a - 1;
786 end;
787 end
788 else
789 Dec(MsgArray[a].Time);
790 end;
792 a := a + 1;
793 end;
794 end;
797 procedure drawConsoleText ();
798 var
799 CWidth, CHeight: Byte;
800 ty: Integer;
801 sp, ep: LongWord;
802 skip: Integer;
804 procedure putLine (sp, ep: LongWord);
805 var
806 p: LongWord;
807 wdt, cw: Integer;
808 begin
809 p := sp;
810 wdt := 0;
811 while p <> ep do
812 begin
813 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
814 if wdt+cw > gScreenWidth-8 then break;
815 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
816 Inc(wdt, cw);
817 cbufNext(p);
818 end;
819 if p <> ep then putLine(p, ep); // do rest of the line first
820 // now print our part
821 if skip = 0 then
822 begin
823 ep := p;
824 p := sp;
825 wdt := 2;
826 while p <> ep do
827 begin
828 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
829 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
830 Inc(wdt, cw);
831 cbufNext(p);
832 end;
833 Dec(ty, CHeight);
834 end
835 else
836 begin
837 Dec(skip);
838 end;
839 end;
841 begin
842 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
843 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
844 skip := conSkipLines;
845 cbufLastLine(sp, ep);
846 repeat
847 putLine(sp, ep);
848 if ty+CHeight <= 0 then break;
849 until not cbufLineUp(sp, ep);
850 end;
852 procedure g_Console_Draw();
853 var
854 CWidth, CHeight: Byte;
855 mfW, mfH: Word;
856 a, b: Integer;
857 begin
858 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
860 for a := 0 to High(MsgArray) do
861 if MsgArray[a].Time > 0 then
862 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
863 gStdFont, True);
865 if not Cons_Shown then
866 begin
867 if gChatShow then
868 begin
869 if gChatTeam then
870 begin
871 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
872 gStdFont, 255, 255, 255, 1, True);
873 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
874 gStdFont, 255, 255, 255, 1, True);
875 end
876 else
877 begin
878 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
879 gStdFont, 255, 255, 255, 1, True);
880 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
881 gStdFont, 255, 255, 255, 1, True);
882 end;
883 end;
884 Exit;
885 end;
887 if gDebugMode then
888 begin
889 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
890 a := (gScreenWidth - 2*mfW) div 2;
891 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
892 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
893 _RGB(128, 0, 0), 2.0);
894 end;
896 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
897 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
899 drawConsoleText();
900 (*
901 if ConsoleHistory <> nil then
902 begin
903 b := 0;
904 if CHeight > 0 then
905 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
906 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
908 b := Max(b-Offset, 0);
909 d := Max(High(ConsoleHistory)-Offset, 0);
911 c := 2;
912 for a := d downto b do
913 begin
914 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
915 gStdFont, True);
916 c := c + 1;
917 end;
918 end;
919 *)
921 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
922 end;
924 procedure g_Console_Switch();
925 begin
926 if gChatShow then Exit;
927 gConsoleShow := not gConsoleShow;
928 Cons_Shown := True;
929 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
930 end;
932 procedure g_Console_Chat_Switch(Team: Boolean = False);
933 begin
934 if gConsoleShow then Exit;
935 if not g_Game_IsNet then Exit;
936 gChatShow := not gChatShow;
937 gChatTeam := Team;
938 if gChatShow then
939 gChatEnter := False;
940 Line := '';
941 CPos := 1;
942 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
943 end;
945 procedure g_Console_Char(C: AnsiChar);
946 begin
947 if gChatShow and (not gChatEnter) then
948 Exit;
949 Insert(C, Line, CPos);
950 CPos := CPos + 1;
951 end;
954 var
955 tcomplist: array of AnsiString = nil;
956 tcompidx: array of Integer = nil;
958 procedure Complete ();
959 var
960 i, c: Integer;
961 tused: Integer;
962 ll, lpfx, cmd: AnsiString;
963 begin
964 if (Length(Line) = 0) then
965 begin
966 g_Console_Add('');
967 for i := 0 to High(commands) do
968 begin
969 // hidden commands are hidden when cheats aren't enabled
970 if commands[i].hidden and not conIsCheatsEnabled then continue;
971 if (Length(commands[i].help) > 0) then
972 begin
973 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
974 end
975 else
976 begin
977 g_Console_Add(' '+commands[i].cmd);
978 end;
979 end;
980 exit;
981 end;
983 ll := LowerCase(Line);
984 lpfx := '';
986 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
987 begin
988 ll := Copy(ll, 0, Length(ll)-1);
989 for i := 0 to High(commands) do
990 begin
991 // hidden commands are hidden when cheats aren't enabled
992 if commands[i].hidden and not conIsCheatsEnabled then continue;
993 if (commands[i].cmd = ll) then
994 begin
995 if (Length(commands[i].help) > 0) then
996 begin
997 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
998 end;
999 end;
1000 end;
1001 exit;
1002 end;
1004 // build completion list
1005 tused := 0;
1006 for i := 0 to High(commands) do
1007 begin
1008 // hidden commands are hidden when cheats aren't enabled
1009 if commands[i].hidden and not conIsCheatsEnabled then continue;
1010 cmd := commands[i].cmd;
1011 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1012 begin
1013 if (tused = Length(tcomplist)) then
1014 begin
1015 SetLength(tcomplist, Length(tcomplist)+128);
1016 SetLength(tcompidx, Length(tcompidx)+128);
1017 end;
1018 tcomplist[tused] := cmd;
1019 tcompidx[tused] := i;
1020 Inc(tused);
1021 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1022 end;
1023 end;
1025 // get longest prefix
1026 for i := 0 to tused-1 do
1027 begin
1028 cmd := tcomplist[i];
1029 for c := 1 to Length(lpfx) do
1030 begin
1031 if (c > Length(cmd)) then break;
1032 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1033 end;
1034 end;
1036 if (tused = 0) then exit;
1038 if (tused = 1) then
1039 begin
1040 Line := tcomplist[0]+' ';
1041 CPos := Length(Line)+1;
1042 end
1043 else
1044 begin
1045 // has longest prefix?
1046 if (Length(lpfx) > Length(ll)) then
1047 begin
1048 Line := lpfx;
1049 CPos:= Length(Line)+1;
1050 end
1051 else
1052 begin
1053 g_Console_Add('');
1054 for i := 0 to tused-1 do
1055 begin
1056 if (Length(commands[tcompidx[i]].help) > 0) then
1057 begin
1058 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1059 end
1060 else
1061 begin
1062 g_Console_Add(' '+tcomplist[i]);
1063 end;
1064 end;
1065 end;
1066 end;
1067 end;
1070 procedure g_Console_Control(K: Word);
1071 begin
1072 case K of
1073 IK_BACKSPACE:
1074 if (Length(Line) > 0) and (CPos > 1) then
1075 begin
1076 Delete(Line, CPos-1, 1);
1077 CPos := CPos-1;
1078 end;
1079 IK_DELETE:
1080 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1081 Delete(Line, CPos, 1);
1082 IK_LEFT, IK_KPLEFT, VK_LEFT:
1083 if CPos > 1 then
1084 CPos := CPos - 1;
1085 IK_RIGHT, IK_KPRIGHT, VK_RIGHT:
1086 if CPos <= Length(Line) then
1087 CPos := CPos + 1;
1088 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE:
1089 begin
1090 if Cons_Shown then
1091 g_Console_Process(Line)
1092 else
1093 if gChatShow then
1094 begin
1095 if (Length(Line) > 0) and g_Game_IsNet then
1096 begin
1097 if gChatTeam then
1098 begin
1099 if g_Game_IsClient then
1100 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1101 else
1102 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1103 NET_CHAT_TEAM, gPlayer1Settings.Team);
1104 end
1105 else
1106 begin
1107 if g_Game_IsClient then
1108 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1109 else
1110 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1111 NET_CHAT_PLAYER);
1112 end;
1113 end;
1115 Line := '';
1116 CPos := 1;
1117 gChatShow := False;
1118 gJustChatted := True;
1119 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1120 end;
1121 end;
1122 IK_TAB:
1123 if not gChatShow then
1124 Complete();
1125 IK_DOWN, IK_KPDOWN, VK_DOWN:
1126 if not gChatShow then
1127 if (CommandHistory <> nil) and
1128 (CmdIndex < Length(CommandHistory)) then
1129 begin
1130 if CmdIndex < Length(CommandHistory)-1 then
1131 CmdIndex := CmdIndex + 1;
1132 Line := CommandHistory[CmdIndex];
1133 CPos := Length(Line) + 1;
1134 end;
1135 IK_UP, IK_KPUP, VK_UP:
1136 if not gChatShow then
1137 if (CommandHistory <> nil) and
1138 (CmdIndex <= Length(CommandHistory)) then
1139 begin
1140 if CmdIndex > 0 then
1141 CmdIndex := CmdIndex - 1;
1142 Line := CommandHistory[CmdIndex];
1143 Cpos := Length(Line) + 1;
1144 end;
1145 IK_PAGEUP, IK_KPPAGEUP, VK_PREV: // PgUp
1146 if not gChatShow then Inc(conSkipLines);
1147 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT: // PgDown
1148 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1149 IK_HOME, IK_KPHOME:
1150 CPos := 1;
1151 IK_END, IK_KPEND:
1152 CPos := Length(Line) + 1;
1153 end;
1154 end;
1156 function GetStr(var Str: AnsiString): AnsiString;
1157 var
1158 a, b: Integer;
1159 begin
1160 Result := '';
1161 if Str[1] = '"' then
1162 begin
1163 for b := 1 to Length(Str) do
1164 if (b = Length(Str)) or (Str[b+1] = '"') then
1165 begin
1166 Result := Copy(Str, 2, b-1);
1167 Delete(Str, 1, b+1);
1168 Str := Trim(Str);
1169 Exit;
1170 end;
1171 end;
1173 for a := 1 to Length(Str) do
1174 if (a = Length(Str)) or (Str[a+1] = ' ') then
1175 begin
1176 Result := Copy(Str, 1, a);
1177 Delete(Str, 1, a+1);
1178 Str := Trim(Str);
1179 Exit;
1180 end;
1181 end;
1183 function ParseString(Str: AnsiString): SSArray;
1184 begin
1185 Result := nil;
1187 Str := Trim(Str);
1189 if Str = '' then
1190 Exit;
1192 while Str <> '' do
1193 begin
1194 SetLength(Result, Length(Result)+1);
1195 Result[High(Result)] := GetStr(Str);
1196 end;
1197 end;
1199 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1201 procedure conmsg (s: AnsiString);
1202 var
1203 a: Integer;
1204 begin
1205 if length(s) = 0 then exit;
1206 for a := 0 to High(MsgArray) do
1207 begin
1208 with MsgArray[a] do
1209 begin
1210 if Time = 0 then
1211 begin
1212 Msg := s;
1213 Time := MsgTime;
1214 exit;
1215 end;
1216 end;
1217 end;
1218 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1219 with MsgArray[High(MsgArray)] do
1220 begin
1221 Msg := L;
1222 Time := MsgTime;
1223 end;
1224 end;
1226 var
1227 f: Integer;
1228 begin
1229 // put it to console
1230 cbufPut(L);
1231 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1233 // now show 'em out of console too
1234 show := show and gAllowConsoleMessages;
1235 if show and gShowMessages then
1236 begin
1237 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1238 while length(L) > 0 do
1239 begin
1240 f := Pos(#10, L);
1241 if f <= 0 then f := length(L)+1;
1242 conmsg(Copy(L, 1, f-1));
1243 Delete(L, 1, f);
1244 end;
1245 end;
1247 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1248 //ConsoleHistory[High(ConsoleHistory)] := L;
1250 (*
1251 {$IFDEF HEADLESS}
1252 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1253 {$ENDIF}
1254 *)
1255 end;
1258 var
1259 consolewriterLastWasEOL: Boolean = false;
1261 procedure consolewriter (constref buf; len: SizeUInt);
1262 var
1263 b: PByte;
1264 begin
1265 if (len < 1) then exit;
1266 b := PByte(@buf);
1267 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1268 while (len > 0) do
1269 begin
1270 if (b[0] <> 13) and (b[0] <> 10) then
1271 begin
1272 cbufPut(AnsiChar(b[0]));
1273 end
1274 else
1275 begin
1276 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1277 cbufPut(#10);
1278 end;
1279 len -= 1;
1280 b += 1;
1281 end;
1282 end;
1285 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1286 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1287 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1288 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1289 begin
1290 g_Console_Add(s, show);
1291 end;
1294 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1295 begin
1296 if show then
1297 begin
1298 g_Console_Add(formatstrf(s, args), true);
1299 end
1300 else
1301 begin
1302 consolewriterLastWasEOL := false;
1303 formatstrf(s, args, consolewriter);
1304 if not consolewriterLastWasEOL then cbufPut(#10);
1305 end;
1306 end;
1309 procedure g_Console_Clear();
1310 begin
1311 //ConsoleHistory := nil;
1312 cbufClear();
1313 conSkipLines := 0;
1314 end;
1316 procedure AddToHistory(L: AnsiString);
1317 var
1318 len: Integer;
1319 begin
1320 len := Length(CommandHistory);
1322 if (len = 0) or
1323 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1324 begin
1325 SetLength(CommandHistory, len+1);
1326 CommandHistory[len] := L;
1327 end;
1329 CmdIndex := Length(CommandHistory);
1330 end;
1332 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1333 var
1334 Arr: SSArray;
1335 i: Integer;
1336 begin
1337 Result := True;
1339 Arr := nil;
1341 if Trim(C) = '' then
1342 Exit;
1344 Arr := ParseString(C);
1345 if Arr = nil then
1346 Exit;
1348 for i := 0 to High(Whitelist) do
1349 if Whitelist[i] = LowerCase(Arr[0]) then
1350 Result := False;
1351 end;
1353 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1354 var
1355 Arr: SSArray;
1356 i: Integer;
1357 begin
1358 Arr := nil;
1360 if Trim(L) = '' then
1361 Exit;
1363 conSkipLines := 0; // "unscroll"
1365 if L = 'goobers' then
1366 begin
1367 Line := '';
1368 CPos := 1;
1369 gCheats := true;
1370 g_Console_Add('Your memory serves you well.');
1371 exit;
1372 end;
1374 if not quiet then
1375 begin
1376 g_Console_Add('> '+L);
1377 Line := '';
1378 CPos := 1;
1379 end;
1381 Arr := ParseString(L);
1382 if Arr = nil then
1383 Exit;
1385 if commands = nil then
1386 Exit;
1388 if not quiet then
1389 AddToHistory(L);
1391 for i := 0 to High(commands) do
1392 begin
1393 if commands[i].cmd = LowerCase(Arr[0]) then
1394 begin
1395 if assigned(commands[i].procEx) then
1396 begin
1397 commands[i].procEx(@commands[i], Arr);
1398 exit;
1399 end;
1400 if assigned(commands[i].proc) then
1401 begin
1402 commands[i].proc(Arr);
1403 exit;
1404 end;
1405 end;
1406 end;
1408 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1409 end;
1412 end.