DEADSOFTWARE

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