DEADSOFTWARE

fix chat/console input on android
[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 const
25 ACTION_JUMP = 0;
26 ACTION_MOVELEFT = 1;
27 ACTION_MOVERIGHT = 2;
28 ACTION_LOOKDOWN = 3;
29 ACTION_LOOKUP = 4;
30 ACTION_ATTACK = 5;
31 ACTION_SCORES = 6;
32 ACTION_ACTIVATE = 7;
33 ACTION_STRAFE = 8;
34 ACTION_WEAPNEXT = 9;
35 ACTION_WEAPPREV = 10;
37 FIRST_ACTION = ACTION_JUMP;
38 LAST_ACTION = ACTION_WEAPPREV;
40 procedure g_Console_Init;
41 procedure g_Console_Update;
42 procedure g_Console_Draw;
43 procedure g_Console_Char (C: AnsiChar);
44 procedure g_Console_Control (K: Word);
45 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
46 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
47 procedure g_Console_Clear;
48 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
49 procedure g_Console_ReadConfig (filename: String);
50 procedure g_Console_WriteConfig (filename: String);
51 procedure g_Console_WriteGameConfig;
53 function g_Console_Interactive: Boolean;
54 function g_Console_Action (action: Integer): Boolean;
55 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
56 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
57 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
58 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
59 procedure g_Console_ResetBinds;
61 procedure conwriteln (const s: AnsiString; show: Boolean=false);
62 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
64 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
65 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
66 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
68 // <0: no arg; 0/1: true/false
69 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
71 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
72 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
75 var
76 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
77 gChatShow: Boolean = false;
78 gChatTeam: Boolean = false;
79 gAllowConsoleMessages: Boolean = true;
80 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
81 gParsingBinds: Boolean = true; // íå ïåðåñîõðàíÿòü êîíôèã âî âðåìÿ ïàðñèíãà
82 gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
84 implementation
86 uses
87 g_textures, g_main, e_graphics, e_input, g_game,
88 SysUtils, g_basic, g_options, Math, g_touch,
89 g_menu, g_gui, g_language, g_net, g_netmsg, e_log, conbuf;
92 type
93 PCommand = ^TCommand;
95 TCmdProc = procedure (p: SSArray);
96 TCmdProcEx = procedure (me: PCommand; p: SSArray);
98 TCommand = record
99 cmd: AnsiString;
100 proc: TCmdProc;
101 procEx: TCmdProcEx;
102 help: AnsiString;
103 hidden: Boolean;
104 ptr: Pointer; // various data
105 msg: AnsiString; // message for var changes
106 cheat: Boolean;
107 action: Integer; // >= 0 for action commands
108 player: Integer; // used for action commands
109 end;
111 TAlias = record
112 name: AnsiString;
113 commands: SSArray;
114 end;
117 const
118 Step = 32;
119 Alpha = 25;
120 MsgTime = 144;
121 MaxScriptRecursion = 16;
123 DEBUG_STRING = 'DEBUG MODE';
125 var
126 ID: DWORD;
127 RecursionDepth: Word = 0;
128 RecursionLimitHit: Boolean = False;
129 Cons_Y: SmallInt;
130 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
131 Line: AnsiString;
132 CPos: Word;
133 //ConsoleHistory: SSArray;
134 CommandHistory: SSArray;
135 Whitelist: SSArray;
136 commands: Array of TCommand = nil;
137 Aliases: Array of TAlias = nil;
138 CmdIndex: Word;
139 conSkipLines: Integer = 0;
140 MsgArray: Array [0..4] of record
141 Msg: AnsiString;
142 Time: Word;
143 end;
145 gInputBinds: Array [0..e_MaxInputKeys - 1] of record
146 down, up: SSArray;
147 end;
148 menu_toggled: BOOLEAN; (* hack for menu controls *)
151 procedure g_Console_Switch;
152 begin
153 gChatShow := False;
154 gConsoleShow := not gConsoleShow;
155 Cons_Shown := True;
156 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
157 end;
159 procedure g_Console_Chat_Switch (Team: Boolean = False);
160 begin
161 if not g_Game_IsNet then Exit;
162 gConsoleShow := False;
163 gChatShow := not gChatShow;
164 gChatTeam := Team;
165 Line := '';
166 CPos := 1;
167 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
168 end;
170 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
171 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
172 var
173 pos: Integer = 1;
174 frac: Single = 1;
175 slen: Integer;
176 begin
177 result := false;
178 res := 0;
179 slen := Length(s);
180 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
181 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
182 if (pos > slen) then exit;
183 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
184 // integral part
185 while (pos <= slen) do
186 begin
187 if (s[pos] < '0') or (s[pos] > '9') then break;
188 res := res*10+Byte(s[pos])-48;
189 Inc(pos);
190 end;
191 if (pos <= slen) then
192 begin
193 // must be a dot
194 if (s[pos] <> '.') then exit;
195 Inc(pos);
196 while (pos <= slen) do
197 begin
198 if (s[pos] < '0') or (s[pos] > '9') then break;
199 frac := frac/10;
200 res += frac*(Byte(s[pos])-48);
201 Inc(pos);
202 end;
203 end;
204 if (pos <= slen) then exit; // oops
205 result := true;
206 end;
209 // ////////////////////////////////////////////////////////////////////////// //
210 // <0: no arg; 0/1: true/false; 666: toggle
211 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
212 begin
213 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
214 result := 0;
215 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
216 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
217 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
218 (CompareText(p[idx], 't') = 0) then result := 666;
219 end;
222 procedure boolVarHandler (me: PCommand; p: SSArray);
223 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
224 var
225 old: Boolean;
226 begin
227 if (Length(p) > 2) then
228 begin
229 conwritefln('too many arguments to ''%s''', [p[0]]);
230 end
231 else
232 begin
233 old := flag;
234 case conGetBoolArg(p, 1) of
235 -1: begin end;
236 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
237 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
238 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
239 end;
240 if flag <> old then
241 g_Console_WriteGameConfig();
242 if (Length(msg) = 0) then msg := p[0] else msg += ':';
243 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
244 end;
245 end;
246 begin
247 binaryFlag(PBoolean(me.ptr)^, me.msg);
248 end;
251 procedure intVarHandler (me: PCommand; p: SSArray);
252 var
253 old: Integer;
254 begin
255 if (Length(p) <> 2) then
256 begin
257 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
258 end
259 else
260 begin
261 try
262 old := PInteger(me.ptr)^;
263 PInteger(me.ptr)^ := StrToInt(p[1]);
264 if PInteger(me.ptr)^ <> old then
265 g_Console_WriteGameConfig();
266 except
267 conwritefln('invalid integer value: "%s"', [p[1]]);
268 end;
269 end;
270 end;
273 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
274 var
275 f: Integer;
276 cp: PCommand;
277 begin
278 f := Length(commands);
279 SetLength(commands, f+1);
280 cp := @commands[f];
281 cp.cmd := LowerCase(conname);
282 cp.proc := nil;
283 cp.procEx := boolVarHandler;
284 cp.help := ahelp;
285 cp.hidden := ahidden;
286 cp.ptr := pvar;
287 cp.msg := amsg;
288 cp.cheat := acheat;
289 cp.action := -1;
290 cp.player := -1;
291 end;
294 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
295 var
296 f: Integer;
297 cp: PCommand;
298 begin
299 f := Length(commands);
300 SetLength(commands, f+1);
301 cp := @commands[f];
302 cp.cmd := LowerCase(conname);
303 cp.proc := nil;
304 cp.procEx := intVarHandler;
305 cp.help := ahelp;
306 cp.hidden := ahidden;
307 cp.ptr := pvar;
308 cp.msg := amsg;
309 cp.cheat := acheat;
310 cp.action := -1;
311 cp.player := -1;
312 end;
315 // ////////////////////////////////////////////////////////////////////////// //
316 type
317 PVarSingle = ^TVarSingle;
318 TVarSingle = record
319 val: PSingle;
320 min, max, def: Single; // default will be starting value
321 end;
324 procedure singleVarHandler (me: PCommand; p: SSArray);
325 var
326 pv: PVarSingle;
327 nv, old: Single;
328 msg: AnsiString;
329 begin
330 if (Length(p) > 2) then
331 begin
332 conwritefln('too many arguments to ''%s''', [me.cmd]);
333 exit;
334 end;
335 pv := PVarSingle(me.ptr);
336 old := pv.val^;
337 if (Length(p) = 2) then
338 begin
339 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
340 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
341 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
342 (CompareText(p[1], 'ona') = 0) then
343 begin
344 pv.val^ := pv.def;
345 end
346 else
347 begin
348 if not conParseFloat(nv, p[1]) then
349 begin
350 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
351 exit;
352 end;
353 if (nv < pv.min) then nv := pv.min;
354 if (nv > pv.max) then nv := pv.max;
355 pv.val^ := nv;
356 end;
357 end;
358 if pv.val^ <> old then
359 g_Console_WriteGameConfig();
360 msg := me.msg;
361 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
362 conwritefln('%s %s', [msg, pv.val^]);
363 end;
366 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
367 var
368 f: Integer;
369 cp: PCommand;
370 pv: PVarSingle;
371 begin
372 GetMem(pv, sizeof(TVarSingle));
373 pv.val := pvar;
374 pv.min := amin;
375 pv.max := amax;
376 pv.def := pvar^;
377 f := Length(commands);
378 SetLength(commands, f+1);
379 cp := @commands[f];
380 cp.cmd := LowerCase(conname);
381 cp.proc := nil;
382 cp.procEx := singleVarHandler;
383 cp.help := ahelp;
384 cp.hidden := ahidden;
385 cp.ptr := pv;
386 cp.msg := amsg;
387 cp.cheat := acheat;
388 cp.action := -1;
389 cp.player := -1;
390 end;
393 // ////////////////////////////////////////////////////////////////////////// //
394 function GetStrACmd(var Str: AnsiString): AnsiString;
395 var
396 a: Integer;
397 begin
398 Result := '';
399 for a := 1 to Length(Str) do
400 if (a = Length(Str)) or (Str[a+1] = ';') then
401 begin
402 Result := Copy(Str, 1, a);
403 Delete(Str, 1, a+1);
404 Str := Trim(Str);
405 Exit;
406 end;
407 end;
409 function ParseAlias(Str: AnsiString): SSArray;
410 begin
411 Result := nil;
413 Str := Trim(Str);
415 if Str = '' then
416 Exit;
418 while Str <> '' do
419 begin
420 SetLength(Result, Length(Result)+1);
421 Result[High(Result)] := GetStrACmd(Str);
422 end;
423 end;
425 procedure ConsoleCommands(p: SSArray);
426 var
427 cmd, s: AnsiString;
428 a, b: Integer;
429 (* F: TextFile; *)
430 begin
431 cmd := LowerCase(p[0]);
432 s := '';
434 if cmd = 'clear' then
435 begin
436 //ConsoleHistory := nil;
437 cbufClear();
438 conSkipLines := 0;
440 for a := 0 to High(MsgArray) do
441 with MsgArray[a] do
442 begin
443 Msg := '';
444 Time := 0;
445 end;
446 end;
448 if cmd = 'clearhistory' then
449 CommandHistory := nil;
451 if cmd = 'showhistory' then
452 if CommandHistory <> nil then
453 begin
454 g_Console_Add('');
455 for a := 0 to High(CommandHistory) do
456 g_Console_Add(' '+CommandHistory[a]);
457 end;
459 if cmd = 'commands' then
460 begin
461 g_Console_Add('');
462 g_Console_Add('commands list:');
463 for a := High(commands) downto 0 do
464 begin
465 if (Length(commands[a].help) > 0) then
466 begin
467 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
468 end
469 else
470 begin
471 g_Console_Add(' '+commands[a].cmd);
472 end;
473 end;
474 end;
476 if cmd = 'time' then
477 g_Console_Add(TimeToStr(Now), True);
479 if cmd = 'date' then
480 g_Console_Add(DateToStr(Now), True);
482 if cmd = 'echo' then
483 if Length(p) > 1 then
484 begin
485 if p[1] = 'ololo' then
486 gCheats := True
487 else
488 begin
489 s := '';
490 for a := 1 to High(p) do
491 s := s + p[a] + ' ';
492 g_Console_Add(b_Text_Format(s), True);
493 end;
494 end
495 else
496 g_Console_Add('');
498 if cmd = 'dump' then
499 begin
500 (*
501 if ConsoleHistory <> nil then
502 begin
503 if Length(P) > 1 then
504 s := P[1]
505 else
506 s := GameDir+'/console.txt';
508 {$I-}
509 AssignFile(F, s);
510 Rewrite(F);
511 if IOResult <> 0 then
512 begin
513 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
514 CloseFile(F);
515 Exit;
516 end;
518 for a := 0 to High(ConsoleHistory) do
519 WriteLn(F, ConsoleHistory[a]);
521 CloseFile(F);
522 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
523 {$I+}
524 end;
525 *)
526 end;
528 if cmd = 'exec' then
529 begin
530 // exec <filename>
531 if Length(p) = 2 then
532 g_Console_ReadConfig(GameDir + '/' + p[1])
533 else
534 g_Console_Add('exec <script file>');
535 end;
537 if cmd = 'writeconfig' then
538 begin
539 // writeconfig <filename>
540 if Length(p) = 2 then
541 g_Console_WriteConfig(GameDir + '/' + p[1])
542 else
543 g_Console_Add('writeconfig <file>');
544 end;
546 if (cmd = 'ver') or (cmd = 'version') then
547 begin
548 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
549 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
550 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
551 end;
553 if cmd = 'alias' then
554 begin
555 // alias [alias_name] [commands]
556 if Length(p) > 1 then
557 begin
558 for a := 0 to High(Aliases) do
559 if Aliases[a].name = p[1] then
560 begin
561 if Length(p) > 2 then
562 Aliases[a].commands := ParseAlias(p[2])
563 else
564 for b := 0 to High(Aliases[a].commands) do
565 g_Console_Add(Aliases[a].commands[b]);
566 Exit;
567 end;
568 SetLength(Aliases, Length(Aliases)+1);
569 a := High(Aliases);
570 Aliases[a].name := p[1];
571 if Length(p) > 2 then
572 Aliases[a].commands := ParseAlias(p[2])
573 else
574 for b := 0 to High(Aliases[a].commands) do
575 g_Console_Add(Aliases[a].commands[b]);
576 end else
577 for a := 0 to High(Aliases) do
578 if Aliases[a].commands <> nil then
579 g_Console_Add(Aliases[a].name);
580 end;
582 if cmd = 'call' then
583 begin
584 // call <alias_name>
585 if Length(p) > 1 then
586 begin
587 if Aliases = nil then
588 Exit;
589 for a := 0 to High(Aliases) do
590 if Aliases[a].name = p[1] then
591 begin
592 if Aliases[a].commands <> nil then
593 begin
594 // with this system proper endless loop detection seems either impossible
595 // or very dirty to implement, so let's have this instead
596 // prevents endless loops
597 for b := 0 to High(Aliases[a].commands) do
598 begin
599 Inc(RecursionDepth);
600 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
601 if not RecursionLimitHit then
602 g_Console_Process(Aliases[a].commands[b], True);
603 Dec(RecursionDepth);
604 end;
605 if (RecursionDepth = 0) and RecursionLimitHit then
606 begin
607 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
608 RecursionLimitHit := False;
609 end;
610 end;
611 Exit;
612 end;
613 end
614 else
615 g_Console_Add('call <alias name>');
616 end;
617 end;
619 procedure WhitelistCommand(cmd: AnsiString);
620 var
621 a: Integer;
622 begin
623 SetLength(Whitelist, Length(Whitelist)+1);
624 a := High(Whitelist);
625 Whitelist[a] := LowerCase(cmd);
626 end;
628 procedure segfault (p: SSArray);
629 var
630 pp: PByte = nil;
631 begin
632 pp^ := 0;
633 end;
635 function GetCommandString (p: SSArray): AnsiString;
636 var i: Integer;
637 begin
638 result := '';
639 if Length(p) >= 1 then
640 begin
641 result := p[0];
642 for i := 1 to High(p) do
643 result := result + '; ' + p[i]
644 end
645 end;
647 function QuoteStr(str: String): String;
648 begin
649 if Pos(' ', str) > 0 then
650 Result := '"' + str + '"'
651 else
652 Result := str;
653 end;
655 procedure BindCommands (p: SSArray);
656 var cmd, key: AnsiString; i: Integer;
657 begin
658 cmd := LowerCase(p[0]);
659 case cmd of
660 'bind':
661 // bind <key> [down [up]]
662 if (Length(p) >= 2) and (Length(p) <= 4) then
663 begin
664 i := 0;
665 key := LowerCase(p[1]);
666 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
667 if i < e_MaxInputKeys then
668 begin
669 if Length(p) = 2 then
670 g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
671 else if Length(p) = 3 then
672 g_Console_BindKey(i, p[2], '')
673 else (* len = 4 *)
674 g_Console_BindKey(i, p[2], p[3])
675 end
676 else
677 g_Console_Add('bind: "' + p[1] + '" is not a key')
678 end
679 else
680 begin
681 g_Console_Add('bind <key> <down action> [up action]')
682 end;
683 'bindlist':
684 for i := 0 to e_MaxInputKeys - 1 do
685 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
686 g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
687 'unbind':
688 // unbind <key>
689 if Length(p) = 2 then
690 begin
691 key := LowerCase(p[1]);
692 i := 0;
693 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
694 if i < e_MaxInputKeys then
695 g_Console_BindKey(i, '')
696 else
697 g_Console_Add('unbind: "' + p[1] + '" is not a key')
698 end
699 else
700 g_Console_Add('unbind <key>');
701 'unbindall':
702 for i := 0 to e_MaxInputKeys - 1 do
703 g_Console_BindKey(i, '');
704 'showkeyboard':
705 g_Touch_ShowKeyboard(True);
706 'hidekeyboard':
707 g_Touch_ShowKeyboard(False);
708 'togglemenu':
709 if gConsoleShow then
710 begin
711 g_Console_Switch;
712 menu_toggled := True
713 end
714 else
715 begin
716 KeyPress(VK_ESCAPE);
717 menu_toggled := True
718 end;
719 'toggleconsole':
720 g_Console_Switch;
721 'togglechat':
722 if not gConsoleShow and (g_ActiveWindow = nil) then
723 g_Console_Chat_Switch;
724 'toggleteamchat':
725 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
726 if not gConsoleShow and (g_ActiveWindow = nil) then
727 g_Console_Chat_Switch(True);
728 end
729 end;
731 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
732 var
733 a: Integer;
734 cp: PCommand;
735 begin
736 SetLength(commands, Length(commands)+1);
737 a := High(commands);
738 cp := @commands[a];
739 cp.cmd := LowerCase(cmd);
740 cp.proc := proc;
741 cp.procEx := nil;
742 cp.help := ahelp;
743 cp.hidden := ahidden;
744 cp.ptr := nil;
745 cp.msg := '';
746 cp.cheat := acheat;
747 cp.action := -1;
748 cp.player := -1;
749 end;
751 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
752 const
753 PrefixList: array [0..1] of AnsiString = ('+', '-');
754 PlayerList: array [0..1] of Integer = (1, 2);
755 var
756 s: AnsiString;
757 i: Integer;
759 procedure NewAction (cmd: AnsiString; player: Integer);
760 var cp: PCommand;
761 begin
762 SetLength(commands, Length(commands) + 1);
763 cp := @commands[High(commands)];
764 cp.cmd := LowerCase(cmd);
765 cp.proc := nil;
766 cp.procEx := nil;
767 cp.help := help;
768 cp.hidden := hidden;
769 cp.ptr := nil;
770 cp.msg := '';
771 cp.cheat := cheat;
772 cp.action := action;
773 cp.player := player;
774 end;
776 begin
777 ASSERT(action >= FIRST_ACTION);
778 ASSERT(action <= LAST_ACTION);
779 for s in PrefixList do
780 begin
781 NewAction(s + cmd, 0);
782 for i in PlayerList do
783 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
784 end
785 end;
787 procedure g_Console_Init();
788 var
789 a: Integer;
790 begin
791 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
792 Cons_Y := -(gScreenHeight div 2);
793 gConsoleShow := False;
794 gChatShow := False;
795 Cons_Shown := False;
796 CPos := 1;
798 for a := 0 to High(MsgArray) do
799 with MsgArray[a] do
800 begin
801 Msg := '';
802 Time := 0;
803 end;
805 AddCommand('segfault', segfault, 'make segfault');
807 AddCommand('bind', BindCommands);
808 AddCommand('bindlist', BindCommands);
809 AddCommand('unbind', BindCommands);
810 AddCommand('unbindall', BindCommands);
811 AddCommand('showkeyboard', BindCommands);
812 AddCommand('hidekeyboard', BindCommands);
813 AddCommand('togglemenu', BindCommands);
814 AddCommand('toggleconsole', BindCommands);
815 AddCommand('togglechat', BindCommands);
816 AddCommand('toggleteamchat', BindCommands);
818 AddCommand('clear', ConsoleCommands, 'clear console');
819 AddCommand('clearhistory', ConsoleCommands);
820 AddCommand('showhistory', ConsoleCommands);
821 AddCommand('commands', ConsoleCommands);
822 AddCommand('time', ConsoleCommands);
823 AddCommand('date', ConsoleCommands);
824 AddCommand('echo', ConsoleCommands);
825 AddCommand('dump', ConsoleCommands);
826 AddCommand('exec', ConsoleCommands);
827 AddCommand('writeconfig', ConsoleCommands);
828 AddCommand('alias', ConsoleCommands);
829 AddCommand('call', ConsoleCommands);
830 AddCommand('ver', ConsoleCommands);
831 AddCommand('version', ConsoleCommands);
833 AddCommand('d_window', DebugCommands);
834 AddCommand('d_sounds', DebugCommands);
835 AddCommand('d_frames', DebugCommands);
836 AddCommand('d_winmsg', DebugCommands);
837 AddCommand('d_monoff', DebugCommands);
838 AddCommand('d_botoff', DebugCommands);
839 AddCommand('d_monster', DebugCommands);
840 AddCommand('d_health', DebugCommands);
841 AddCommand('d_player', DebugCommands);
842 AddCommand('d_joy', DebugCommands);
843 AddCommand('d_mem', DebugCommands);
845 AddCommand('p1_name', GameCVars);
846 AddCommand('p2_name', GameCVars);
847 AddCommand('p1_color', GameCVars);
848 AddCommand('p2_color', GameCVars);
849 AddCommand('r_showscore', GameCVars);
850 AddCommand('r_showlives', GameCVars);
851 AddCommand('r_showstat', GameCVars);
852 AddCommand('r_showkillmsg', GameCVars);
853 AddCommand('r_showspect', GameCVars);
854 AddCommand('r_showping', GameCVars);
855 AddCommand('g_gamemode', GameCVars);
856 AddCommand('g_friendlyfire', GameCVars);
857 AddCommand('g_weaponstay', GameCVars);
858 AddCommand('g_allow_exit', GameCVars);
859 AddCommand('g_allow_monsters', GameCVars);
860 AddCommand('g_bot_vsmonsters', GameCVars);
861 AddCommand('g_bot_vsplayers', GameCVars);
862 AddCommand('g_scorelimit', GameCVars);
863 AddCommand('g_timelimit', GameCVars);
864 AddCommand('g_maxlives', GameCVars);
865 AddCommand('g_warmuptime', GameCVars);
866 AddCommand('net_interp', GameCVars);
867 AddCommand('net_forceplayerupdate', GameCVars);
868 AddCommand('net_predictself', GameCVars);
869 AddCommand('sv_name', GameCVars);
870 AddCommand('sv_passwd', GameCVars);
871 AddCommand('sv_maxplrs', GameCVars);
872 AddCommand('sv_public', GameCVars);
873 AddCommand('sv_intertime', GameCVars);
875 AddCommand('quit', GameCommands);
876 AddCommand('exit', GameCommands);
877 AddCommand('pause', GameCommands);
878 AddCommand('endgame', GameCommands);
879 AddCommand('restart', GameCommands);
880 AddCommand('addbot', GameCommands);
881 AddCommand('bot_add', GameCommands);
882 AddCommand('bot_addlist', GameCommands);
883 AddCommand('bot_addred', GameCommands);
884 AddCommand('bot_addblue', GameCommands);
885 AddCommand('bot_removeall', GameCommands);
886 AddCommand('chat', GameCommands);
887 AddCommand('teamchat', GameCommands);
888 AddCommand('game', GameCommands);
889 AddCommand('host', GameCommands);
890 AddCommand('map', GameCommands);
891 AddCommand('nextmap', GameCommands);
892 AddCommand('endmap', GameCommands);
893 AddCommand('goodbye', GameCommands);
894 AddCommand('suicide', GameCommands);
895 AddCommand('spectate', GameCommands);
896 AddCommand('ready', GameCommands);
897 AddCommand('kick', GameCommands);
898 AddCommand('kick_id', GameCommands);
899 AddCommand('ban', GameCommands);
900 AddCommand('permban', GameCommands);
901 AddCommand('ban_id', GameCommands);
902 AddCommand('permban_id', GameCommands);
903 AddCommand('unban', GameCommands);
904 AddCommand('connect', GameCommands);
905 AddCommand('disconnect', GameCommands);
906 AddCommand('reconnect', GameCommands);
907 AddCommand('say', GameCommands);
908 AddCommand('tell', GameCommands);
909 AddCommand('overtime', GameCommands);
910 AddCommand('rcon_password', GameCommands);
911 AddCommand('rcon', GameCommands);
912 AddCommand('callvote', GameCommands);
913 AddCommand('vote', GameCommands);
914 AddCommand('clientlist', GameCommands);
915 AddCommand('event', GameCommands);
916 AddCommand('screenshot', GameCommands);
917 AddCommand('weapon', GameCommands);
918 AddCommand('p1_weapon', GameCommands);
919 AddCommand('p2_weapon', GameCommands);
921 AddCommand('god', GameCheats);
922 AddCommand('notarget', GameCheats);
923 AddCommand('give', GameCheats); // "exit" too ;-)
924 AddCommand('open', GameCheats);
925 AddCommand('fly', GameCheats);
926 AddCommand('noclip', GameCheats);
927 AddCommand('speedy', GameCheats);
928 AddCommand('jumpy', GameCheats);
929 AddCommand('noreload', GameCheats);
930 AddCommand('aimline', GameCheats);
931 AddCommand('automap', GameCheats);
933 AddAction('jump', ACTION_JUMP);
934 AddAction('moveleft', ACTION_MOVELEFT);
935 AddAction('moveright', ACTION_MOVERIGHT);
936 AddAction('lookup', ACTION_LOOKUP);
937 AddAction('lookdown', ACTION_LOOKDOWN);
938 AddAction('attack', ACTION_ATTACK);
939 AddAction('scores', ACTION_SCORES);
940 AddAction('activate', ACTION_ACTIVATE);
941 AddAction('strafe', ACTION_STRAFE);
942 AddAction('weapnext', ACTION_WEAPNEXT);
943 AddAction('weapprev', ACTION_WEAPPREV);
945 WhitelistCommand('say');
946 WhitelistCommand('tell');
947 WhitelistCommand('overtime');
948 WhitelistCommand('ready');
949 WhitelistCommand('map');
950 WhitelistCommand('nextmap');
951 WhitelistCommand('endmap');
952 WhitelistCommand('restart');
953 WhitelistCommand('kick');
954 WhitelistCommand('ban');
956 WhitelistCommand('addbot');
957 WhitelistCommand('bot_add');
958 WhitelistCommand('bot_addred');
959 WhitelistCommand('bot_addblue');
960 WhitelistCommand('bot_removeall');
962 WhitelistCommand('g_gamemode');
963 WhitelistCommand('g_friendlyfire');
964 WhitelistCommand('g_weaponstay');
965 WhitelistCommand('g_allow_exit');
966 WhitelistCommand('g_allow_monsters');
967 WhitelistCommand('g_scorelimit');
968 WhitelistCommand('g_timelimit');
970 g_Console_ResetBinds;
971 g_Console_ReadConfig(GameDir + '/dfconfig.cfg');
972 g_Console_ReadConfig(GameDir + '/autoexec.cfg');
973 gParsingBinds := False;
975 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
976 g_Console_Add('');
977 end;
979 procedure g_Console_Update();
980 var
981 a, b: Integer;
982 begin
983 if Cons_Shown then
984 begin
985 // Â ïðîöåññå îòêðûòèÿ:
986 if gConsoleShow and (Cons_Y < 0) then
987 begin
988 Cons_Y := Cons_Y+Step;
989 end;
991 // Â ïðîöåññå çàêðûòèÿ:
992 if (not gConsoleShow) and
993 (Cons_Y > -(gScreenHeight div 2)) then
994 Cons_Y := Cons_Y-Step;
996 // Îêîí÷àòåëüíî îòêðûëàñü:
997 if Cons_Y > 0 then
998 Cons_Y := 0;
1000 // Îêîí÷àòåëüíî çàêðûëàñü:
1001 if Cons_Y <= (-(gScreenHeight div 2)) then
1002 begin
1003 Cons_Y := -(gScreenHeight div 2);
1004 Cons_Shown := False;
1005 end;
1006 end;
1008 a := 0;
1009 while a <= High(MsgArray) do
1010 begin
1011 if MsgArray[a].Time > 0 then
1012 begin
1013 if MsgArray[a].Time = 1 then
1014 begin
1015 if a < High(MsgArray) then
1016 begin
1017 for b := a to High(MsgArray)-1 do
1018 MsgArray[b] := MsgArray[b+1];
1020 MsgArray[High(MsgArray)].Time := 0;
1022 a := a - 1;
1023 end;
1024 end
1025 else
1026 Dec(MsgArray[a].Time);
1027 end;
1029 a := a + 1;
1030 end;
1031 end;
1034 procedure drawConsoleText ();
1035 var
1036 CWidth, CHeight: Byte;
1037 ty: Integer;
1038 sp, ep: LongWord;
1039 skip: Integer;
1041 procedure putLine (sp, ep: LongWord);
1042 var
1043 p: LongWord;
1044 wdt, cw: Integer;
1045 begin
1046 p := sp;
1047 wdt := 0;
1048 while p <> ep do
1049 begin
1050 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1051 if wdt+cw > gScreenWidth-8 then break;
1052 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1053 Inc(wdt, cw);
1054 cbufNext(p);
1055 end;
1056 if p <> ep then putLine(p, ep); // do rest of the line first
1057 // now print our part
1058 if skip = 0 then
1059 begin
1060 ep := p;
1061 p := sp;
1062 wdt := 2;
1063 while p <> ep do
1064 begin
1065 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1066 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1067 Inc(wdt, cw);
1068 cbufNext(p);
1069 end;
1070 Dec(ty, CHeight);
1071 end
1072 else
1073 begin
1074 Dec(skip);
1075 end;
1076 end;
1078 begin
1079 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1080 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
1081 skip := conSkipLines;
1082 cbufLastLine(sp, ep);
1083 repeat
1084 putLine(sp, ep);
1085 if ty+CHeight <= 0 then break;
1086 until not cbufLineUp(sp, ep);
1087 end;
1089 procedure g_Console_Draw();
1090 var
1091 CWidth, CHeight: Byte;
1092 mfW, mfH: Word;
1093 a, b: Integer;
1094 begin
1095 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1097 for a := 0 to High(MsgArray) do
1098 if MsgArray[a].Time > 0 then
1099 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
1100 gStdFont, True);
1102 if not Cons_Shown then
1103 begin
1104 if gChatShow then
1105 begin
1106 if gChatTeam then
1107 begin
1108 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
1109 gStdFont, 255, 255, 255, 1, True);
1110 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
1111 gStdFont, 255, 255, 255, 1, True);
1112 end
1113 else
1114 begin
1115 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
1116 gStdFont, 255, 255, 255, 1, True);
1117 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
1118 gStdFont, 255, 255, 255, 1, True);
1119 end;
1120 end;
1121 Exit;
1122 end;
1124 if gDebugMode then
1125 begin
1126 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1127 a := (gScreenWidth - 2*mfW) div 2;
1128 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
1129 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1130 _RGB(128, 0, 0), 2.0);
1131 end;
1133 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
1134 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
1136 drawConsoleText();
1137 (*
1138 if ConsoleHistory <> nil then
1139 begin
1140 b := 0;
1141 if CHeight > 0 then
1142 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
1143 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
1145 b := Max(b-Offset, 0);
1146 d := Max(High(ConsoleHistory)-Offset, 0);
1148 c := 2;
1149 for a := d downto b do
1150 begin
1151 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
1152 gStdFont, True);
1153 c := c + 1;
1154 end;
1155 end;
1156 *)
1158 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
1159 end;
1161 procedure g_Console_Char(C: AnsiChar);
1162 begin
1163 Insert(C, Line, CPos);
1164 CPos := CPos + 1;
1165 end;
1168 var
1169 tcomplist: array of AnsiString = nil;
1170 tcompidx: array of Integer = nil;
1172 procedure Complete ();
1173 var
1174 i, c: Integer;
1175 tused: Integer;
1176 ll, lpfx, cmd: AnsiString;
1177 begin
1178 if (Length(Line) = 0) then
1179 begin
1180 g_Console_Add('');
1181 for i := 0 to High(commands) do
1182 begin
1183 // hidden commands are hidden when cheats aren't enabled
1184 if commands[i].hidden and not conIsCheatsEnabled then continue;
1185 if (Length(commands[i].help) > 0) then
1186 begin
1187 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1188 end
1189 else
1190 begin
1191 g_Console_Add(' '+commands[i].cmd);
1192 end;
1193 end;
1194 exit;
1195 end;
1197 ll := LowerCase(Line);
1198 lpfx := '';
1200 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1201 begin
1202 ll := Copy(ll, 0, Length(ll)-1);
1203 for i := 0 to High(commands) do
1204 begin
1205 // hidden commands are hidden when cheats aren't enabled
1206 if commands[i].hidden and not conIsCheatsEnabled then continue;
1207 if (commands[i].cmd = ll) then
1208 begin
1209 if (Length(commands[i].help) > 0) then
1210 begin
1211 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1212 end;
1213 end;
1214 end;
1215 exit;
1216 end;
1218 // build completion list
1219 tused := 0;
1220 for i := 0 to High(commands) do
1221 begin
1222 // hidden commands are hidden when cheats aren't enabled
1223 if commands[i].hidden and not conIsCheatsEnabled then continue;
1224 cmd := commands[i].cmd;
1225 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1226 begin
1227 if (tused = Length(tcomplist)) then
1228 begin
1229 SetLength(tcomplist, Length(tcomplist)+128);
1230 SetLength(tcompidx, Length(tcompidx)+128);
1231 end;
1232 tcomplist[tused] := cmd;
1233 tcompidx[tused] := i;
1234 Inc(tused);
1235 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1236 end;
1237 end;
1239 // get longest prefix
1240 for i := 0 to tused-1 do
1241 begin
1242 cmd := tcomplist[i];
1243 for c := 1 to Length(lpfx) do
1244 begin
1245 if (c > Length(cmd)) then break;
1246 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1247 end;
1248 end;
1250 if (tused = 0) then exit;
1252 if (tused = 1) then
1253 begin
1254 Line := tcomplist[0]+' ';
1255 CPos := Length(Line)+1;
1256 end
1257 else
1258 begin
1259 // has longest prefix?
1260 if (Length(lpfx) > Length(ll)) then
1261 begin
1262 Line := lpfx;
1263 CPos:= Length(Line)+1;
1264 end
1265 else
1266 begin
1267 g_Console_Add('');
1268 for i := 0 to tused-1 do
1269 begin
1270 if (Length(commands[tcompidx[i]].help) > 0) then
1271 begin
1272 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1273 end
1274 else
1275 begin
1276 g_Console_Add(' '+tcomplist[i]);
1277 end;
1278 end;
1279 end;
1280 end;
1281 end;
1284 procedure g_Console_Control(K: Word);
1285 begin
1286 case K of
1287 IK_BACKSPACE:
1288 if (Length(Line) > 0) and (CPos > 1) then
1289 begin
1290 Delete(Line, CPos-1, 1);
1291 CPos := CPos-1;
1292 end;
1293 IK_DELETE:
1294 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1295 Delete(Line, CPos, 1);
1296 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1297 if CPos > 1 then
1298 CPos := CPos - 1;
1299 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1300 if CPos <= Length(Line) then
1301 CPos := CPos + 1;
1302 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1303 begin
1304 if Cons_Shown then
1305 g_Console_Process(Line)
1306 else
1307 if gChatShow then
1308 begin
1309 if (Length(Line) > 0) and g_Game_IsNet then
1310 begin
1311 if gChatTeam then
1312 begin
1313 if g_Game_IsClient then
1314 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1315 else
1316 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1317 NET_CHAT_TEAM, gPlayer1Settings.Team);
1318 end
1319 else
1320 begin
1321 if g_Game_IsClient then
1322 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1323 else
1324 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1325 NET_CHAT_PLAYER);
1326 end;
1327 end;
1329 Line := '';
1330 CPos := 1;
1331 gChatShow := False;
1332 gJustChatted := True;
1333 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1334 end;
1335 end;
1336 IK_TAB:
1337 if not gChatShow then
1338 Complete();
1339 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1340 if not gChatShow then
1341 if (CommandHistory <> nil) and
1342 (CmdIndex < Length(CommandHistory)) then
1343 begin
1344 if CmdIndex < Length(CommandHistory)-1 then
1345 CmdIndex := CmdIndex + 1;
1346 Line := CommandHistory[CmdIndex];
1347 CPos := Length(Line) + 1;
1348 end;
1349 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1350 if not gChatShow then
1351 if (CommandHistory <> nil) and
1352 (CmdIndex <= Length(CommandHistory)) then
1353 begin
1354 if CmdIndex > 0 then
1355 CmdIndex := CmdIndex - 1;
1356 Line := CommandHistory[CmdIndex];
1357 Cpos := Length(Line) + 1;
1358 end;
1359 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1360 if not gChatShow then Inc(conSkipLines);
1361 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1362 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1363 IK_HOME, IK_KPHOME:
1364 CPos := 1;
1365 IK_END, IK_KPEND:
1366 CPos := Length(Line) + 1;
1367 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1368 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, IK_EQUALS,
1369 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1370 (* see TEXTINPUT event *)
1371 else
1372 if gConsoleShow and not Cons_Shown and g_Console_MatchBind(K, 'toggleconsole') then
1373 g_Console_Switch;
1374 if g_Console_MatchBind(K, 'togglemenu') then
1375 begin
1376 menu_toggled := True;
1377 if gChatShow then
1378 g_Console_Chat_Switch
1379 else if gConsoleShow and not Cons_Shown then
1380 g_Console_Switch
1381 end
1382 end
1383 end;
1385 function GetStr(var Str: AnsiString): AnsiString;
1386 var
1387 a, b: Integer;
1388 begin
1389 Result := '';
1390 if Str[1] = '"' then
1391 begin
1392 for b := 1 to Length(Str) do
1393 if (b = Length(Str)) or (Str[b+1] = '"') then
1394 begin
1395 Result := Copy(Str, 2, b-1);
1396 Delete(Str, 1, b+1);
1397 Str := Trim(Str);
1398 Exit;
1399 end;
1400 end;
1402 for a := 1 to Length(Str) do
1403 if (a = Length(Str)) or (Str[a+1] = ' ') then
1404 begin
1405 Result := Copy(Str, 1, a);
1406 Delete(Str, 1, a+1);
1407 Str := Trim(Str);
1408 Exit;
1409 end;
1410 end;
1412 function ParseString(Str: AnsiString): SSArray;
1413 begin
1414 Result := nil;
1416 Str := Trim(Str);
1418 if Str = '' then
1419 Exit;
1421 while Str <> '' do
1422 begin
1423 SetLength(Result, Length(Result)+1);
1424 Result[High(Result)] := GetStr(Str);
1425 end;
1426 end;
1428 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1430 procedure conmsg (s: AnsiString);
1431 var
1432 a: Integer;
1433 begin
1434 if length(s) = 0 then exit;
1435 for a := 0 to High(MsgArray) do
1436 begin
1437 with MsgArray[a] do
1438 begin
1439 if Time = 0 then
1440 begin
1441 Msg := s;
1442 Time := MsgTime;
1443 exit;
1444 end;
1445 end;
1446 end;
1447 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1448 with MsgArray[High(MsgArray)] do
1449 begin
1450 Msg := L;
1451 Time := MsgTime;
1452 end;
1453 end;
1455 var
1456 f: Integer;
1457 begin
1458 // put it to console
1459 cbufPut(L);
1460 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1462 // now show 'em out of console too
1463 show := show and gAllowConsoleMessages;
1464 if show and gShowMessages then
1465 begin
1466 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1467 while length(L) > 0 do
1468 begin
1469 f := Pos(#10, L);
1470 if f <= 0 then f := length(L)+1;
1471 conmsg(Copy(L, 1, f-1));
1472 Delete(L, 1, f);
1473 end;
1474 end;
1476 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1477 //ConsoleHistory[High(ConsoleHistory)] := L;
1479 (*
1480 {$IFDEF HEADLESS}
1481 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1482 {$ENDIF}
1483 *)
1484 end;
1487 var
1488 consolewriterLastWasEOL: Boolean = false;
1490 procedure consolewriter (constref buf; len: SizeUInt);
1491 var
1492 b: PByte;
1493 begin
1494 if (len < 1) then exit;
1495 b := PByte(@buf);
1496 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1497 while (len > 0) do
1498 begin
1499 if (b[0] <> 13) and (b[0] <> 10) then
1500 begin
1501 cbufPut(AnsiChar(b[0]));
1502 end
1503 else
1504 begin
1505 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1506 cbufPut(#10);
1507 end;
1508 len -= 1;
1509 b += 1;
1510 end;
1511 end;
1514 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1515 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1516 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1517 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1518 begin
1519 g_Console_Add(s, show);
1520 end;
1523 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1524 begin
1525 if show then
1526 begin
1527 g_Console_Add(formatstrf(s, args), true);
1528 end
1529 else
1530 begin
1531 consolewriterLastWasEOL := false;
1532 formatstrf(s, args, consolewriter);
1533 if not consolewriterLastWasEOL then cbufPut(#10);
1534 end;
1535 end;
1538 procedure g_Console_Clear();
1539 begin
1540 //ConsoleHistory := nil;
1541 cbufClear();
1542 conSkipLines := 0;
1543 end;
1545 procedure AddToHistory(L: AnsiString);
1546 var
1547 len: Integer;
1548 begin
1549 len := Length(CommandHistory);
1551 if (len = 0) or
1552 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1553 begin
1554 SetLength(CommandHistory, len+1);
1555 CommandHistory[len] := L;
1556 end;
1558 CmdIndex := Length(CommandHistory);
1559 end;
1561 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1562 var
1563 Arr: SSArray;
1564 i: Integer;
1565 begin
1566 Result := True;
1568 Arr := nil;
1570 if Trim(C) = '' then
1571 Exit;
1573 Arr := ParseString(C);
1574 if Arr = nil then
1575 Exit;
1577 for i := 0 to High(Whitelist) do
1578 if Whitelist[i] = LowerCase(Arr[0]) then
1579 Result := False;
1580 end;
1582 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1583 var
1584 Arr: SSArray;
1585 i: Integer;
1586 begin
1587 Arr := nil;
1589 if Trim(L) = '' then
1590 Exit;
1592 conSkipLines := 0; // "unscroll"
1594 if L = 'goobers' then
1595 begin
1596 Line := '';
1597 CPos := 1;
1598 gCheats := true;
1599 g_Console_Add('Your memory serves you well.');
1600 exit;
1601 end;
1603 if not quiet then
1604 begin
1605 g_Console_Add('> '+L);
1606 Line := '';
1607 CPos := 1;
1608 end;
1610 Arr := ParseString(L);
1611 if Arr = nil then
1612 Exit;
1614 if commands = nil then
1615 Exit;
1617 if not quiet then
1618 AddToHistory(L);
1620 for i := 0 to High(commands) do
1621 begin
1622 if commands[i].cmd = LowerCase(Arr[0]) then
1623 begin
1624 if commands[i].action >= 0 then
1625 begin
1626 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1627 exit
1628 end;
1629 if assigned(commands[i].procEx) then
1630 begin
1631 commands[i].procEx(@commands[i], Arr);
1632 exit
1633 end;
1634 if assigned(commands[i].proc) then
1635 begin
1636 commands[i].proc(Arr);
1637 exit
1638 end
1639 end
1640 end;
1642 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1643 end;
1646 function g_Console_Interactive: Boolean;
1647 begin
1648 Result := gConsoleShow
1649 end;
1651 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
1652 begin
1653 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1654 ASSERT(key >= 0);
1655 ASSERT(key < e_MaxInputKeys);
1656 if key > 0 then
1657 begin
1658 gInputBinds[key].down := ParseAlias(down);
1659 gInputBinds[key].up := ParseAlias(up);
1660 end;
1661 g_Console_WriteGameConfig();
1662 end;
1664 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1666 function EqualsCommandLists (a, b: SSArray): Boolean;
1667 var i, len: Integer;
1668 begin
1669 result := False;
1670 len := Length(a);
1671 if len = Length(b) then
1672 begin
1673 i := 0;
1674 while (i < len) and (a[i] = b[i]) do inc(i);
1675 if i >= len then
1676 result := True
1677 end
1678 end;
1680 begin
1681 ASSERT(key >= 0);
1682 ASSERT(key < e_MaxInputKeys);
1683 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1684 end;
1686 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1687 var i: Integer;
1688 begin
1689 ASSERT(n >= 1);
1690 result := 0;
1691 if commands = nil then Exit;
1692 i := 0;
1693 while (n >= 1) and (i < e_MaxInputKeys) do
1694 begin
1695 if g_Console_MatchBind(i, down, up) then
1696 begin
1697 result := i;
1698 dec(n)
1699 end;
1700 inc(i)
1701 end;
1702 if n >= 1 then
1703 result := 0
1704 end;
1706 function g_Console_Action (action: Integer): Boolean;
1707 var i, len: Integer;
1708 begin
1709 ASSERT(action >= FIRST_ACTION);
1710 ASSERT(action <= LAST_ACTION);
1711 i := 0;
1712 len := Length(gPlayerAction);
1713 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1714 Result := i < len
1715 end;
1717 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1718 var i: Integer;
1719 begin
1720 if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1721 begin
1722 if down then
1723 begin
1724 if not gChatShow then
1725 for i := 0 to High(gInputBinds[key].down) do
1726 g_Console_Process(gInputBinds[key].down[i], True);
1727 end
1728 else
1729 for i := 0 to High(gInputBinds[key].up) do
1730 g_Console_Process(gInputBinds[key].up[i], True)
1731 end;
1732 if down and not menu_toggled then
1733 KeyPress(key);
1734 menu_toggled := False
1735 end;
1737 procedure g_Console_ResetBinds;
1738 var i: Integer;
1739 begin
1740 for i := 0 to e_MaxInputKeys - 1 do
1741 g_Console_BindKey(i, '', '');
1743 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
1744 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
1745 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1746 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1747 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1748 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1749 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1750 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1751 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1752 g_Console_BindKey(IK_E, '+p1_weapnext', '-p1_weapnext');
1753 g_Console_BindKey(IK_Q, '+p1_weapprev', '-p1_weapprev');
1754 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1755 g_Console_BindKey(IK_1, 'p1_weapon 1');
1756 g_Console_BindKey(IK_2, 'p1_weapon 2');
1757 g_Console_BindKey(IK_3, 'p1_weapon 3');
1758 g_Console_BindKey(IK_4, 'p1_weapon 4');
1759 g_Console_BindKey(IK_5, 'p1_weapon 5');
1760 g_Console_BindKey(IK_6, 'p1_weapon 6');
1761 g_Console_BindKey(IK_7, 'p1_weapon 7');
1762 g_Console_BindKey(IK_8, 'p1_weapon 8');
1763 g_Console_BindKey(IK_9, 'p1_weapon 9');
1764 g_Console_BindKey(IK_0, 'p1_weapon 10');
1765 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1766 g_Console_BindKey(IK_T, 'togglechat');
1767 g_Console_BindKey(IK_Y, 'toggleteamchat');
1768 g_Console_BindKey(IK_F11, 'screenshot');
1769 g_Console_BindKey(IK_TAB, '+p1_scores', '-p1_scores');
1770 g_Console_BindKey(IK_PAUSE, 'pause');
1771 g_Console_BindKey(IK_F1, 'vote');
1773 (* for i := 0 to e_MaxJoys - 1 do *)
1774 for i := 0 to 1 do
1775 begin
1776 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
1777 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
1778 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
1779 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
1780 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
1781 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
1782 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
1783 g_Console_BindKey(e_JoyButtonToKey(i, 1), '+p' + IntToStr(i mod 2 + 1) + '_weapnext', '-p' + IntToStr(i mod 2 + 1) + '_weapnext');
1784 g_Console_BindKey(e_JoyButtonToKey(i, 4), '+p' + IntToStr(i mod 2 + 1) + '_weapprev', '-p' + IntToStr(i mod 2 + 1) + '_weapprev');
1785 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
1786 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
1787 end;
1789 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
1790 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
1791 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
1792 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
1793 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
1794 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
1795 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
1796 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
1797 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
1798 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
1799 g_Console_BindKey(VK_NEXT, '+weapnext', '-weapnext');
1800 g_Console_BindKey(VK_PREV, '+weapprev', '-weapprev');
1801 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
1802 g_Console_BindKey(VK_0, 'weapon 1');
1803 g_Console_BindKey(VK_1, 'weapon 2');
1804 g_Console_BindKey(VK_2, 'weapon 3');
1805 g_Console_BindKey(VK_3, 'weapon 4');
1806 g_Console_BindKey(VK_4, 'weapon 5');
1807 g_Console_BindKey(VK_5, 'weapon 6');
1808 g_Console_BindKey(VK_6, 'weapon 7');
1809 g_Console_BindKey(VK_7, 'weapon 8');
1810 g_Console_BindKey(VK_8, 'weapon 9');
1811 g_Console_BindKey(VK_9, 'weapon 10');
1812 g_Console_BindKey(VK_A, 'weapon 11');
1813 g_Console_BindKey(VK_CHAT, 'togglechat');
1814 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
1815 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
1816 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
1817 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
1818 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
1819 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
1820 end;
1822 procedure g_Console_ReadConfig (filename: String);
1823 var f: TextFile; s: AnsiString; i, len: Integer;
1824 begin
1825 if FileExists(filename) then
1826 begin
1827 AssignFile(f, filename);
1828 Reset(f);
1829 while not EOF(f) do
1830 begin
1831 ReadLn(f, s);
1832 len := Length(s);
1833 if len > 0 then
1834 begin
1835 i := 1;
1836 (* skip spaces *)
1837 while (i <= len) and (s[i] <= ' ') do inc(i);
1838 (* skip comments *)
1839 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
1840 g_Console_Process(s, True);
1841 end
1842 end;
1843 CloseFile(f);
1844 end
1845 end;
1847 procedure g_Console_WriteConfig (filename: String);
1848 var f: TextFile; i, j: Integer;
1849 begin
1850 AssignFile(f, filename);
1851 Rewrite(f);
1852 WriteLn(f, '// generated by doom2d, do not modify');
1853 WriteLn(f, 'unbindall');
1854 for i := 0 to e_MaxInputKeys - 1 do
1855 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
1856 begin
1857 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
1858 if Length(gInputBinds[i].down) = 0 then
1859 Write(f, '""');
1860 if Length(gInputBinds[i].up) > 0 then
1861 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
1862 WriteLn(f, '');
1863 end;
1864 for i := 0 to High(commands) do
1865 begin
1866 if not commands[i].cheat then
1867 begin
1868 if @commands[i].procEx = @boolVarHandler then
1869 begin
1870 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
1871 WriteLn(f, commands[i].cmd, ' ', j)
1872 end
1873 else if @commands[i].procEx = @intVarHandler then
1874 begin
1875 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
1876 end
1877 else if @commands[i].procEx = @singleVarHandler then
1878 begin
1879 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
1880 end
1881 end
1882 end;
1883 CloseFile(f)
1884 end;
1886 procedure g_Console_WriteGameConfig;
1887 begin
1888 if gParsingBinds then
1889 Exit;
1890 g_Console_WriteConfig(GameDir + '/dfconfig.cfg');
1891 end;
1893 end.