DEADSOFTWARE

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