DEADSOFTWARE

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