DEADSOFTWARE

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