DEADSOFTWARE

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