DEADSOFTWARE

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