DEADSOFTWARE

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