DEADSOFTWARE

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