DEADSOFTWARE

console: read commands from stdin in headless mode
[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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_console;
18 interface
20 uses
21 utils; // for SSArray
23 const
24 ACTION_JUMP = 0;
25 ACTION_MOVELEFT = 1;
26 ACTION_MOVERIGHT = 2;
27 ACTION_LOOKDOWN = 3;
28 ACTION_LOOKUP = 4;
29 ACTION_ATTACK = 5;
30 ACTION_SCORES = 6;
31 ACTION_ACTIVATE = 7;
32 ACTION_STRAFE = 8;
34 FIRST_ACTION = ACTION_JUMP;
35 LAST_ACTION = ACTION_STRAFE;
37 procedure g_Console_Init;
38 procedure g_Console_SysInit;
39 procedure g_Console_Update;
40 procedure g_Console_Draw (MessagesOnly: Boolean = False);
41 procedure g_Console_Char (C: AnsiChar);
42 procedure g_Console_Control (K: Word);
43 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
44 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
45 procedure g_Console_Clear;
46 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
47 procedure g_Console_ReadConfig (filename: String);
48 procedure g_Console_WriteConfig (filename: String);
49 procedure g_Console_WriteGameConfig;
51 function g_Console_Interactive: Boolean;
52 function g_Console_Action (action: Integer): Boolean;
53 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
54 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
55 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = ''; rep: Boolean = False);
56 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
57 procedure g_Console_ProcessBindRepeat (key: Integer);
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;
66 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
67 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
68 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
70 // <0: no arg; 0/1: true/false
71 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
73 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
74 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
76 const
77 {$IFDEF HEADLESS}
78 defaultConfigScript = 'dfserver.cfg';
79 {$ELSE}
80 defaultConfigScript = 'dfconfig.cfg';
81 {$ENDIF}
83 var
84 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
85 gChatShow: Boolean = false;
86 gChatTeam: Boolean = false;
87 gAllowConsoleMessages: Boolean = true;
88 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
89 gParsingBinds: Boolean = true; // íå ïåðåñîõðàíÿòü êîíôèã âî âðåìÿ ïàðñèíãà
90 gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
91 gConfigScript: string = defaultConfigScript;
93 implementation
95 uses
96 g_textures, g_main, e_graphics, e_input, g_game, g_gfx, g_player, g_items,
97 SysUtils, g_basic, g_options, Math, g_touch, e_res,
98 g_menu, g_gui, g_language, g_net, g_netmsg, e_log, conbuf, g_weapons,
99 Keyboard;
101 const
102 autoexecScript = 'autoexec.cfg';
103 configComment = 'generated by doom2d, do not modify';
105 type
106 PCommand = ^TCommand;
108 TCmdProc = procedure (p: SSArray);
109 TCmdProcEx = procedure (me: PCommand; p: SSArray);
111 TCommand = record
112 cmd: AnsiString;
113 proc: TCmdProc;
114 procEx: TCmdProcEx;
115 help: AnsiString;
116 hidden: Boolean;
117 ptr: Pointer; // various data
118 msg: AnsiString; // message for var changes
119 cheat: Boolean;
120 action: Integer; // >= 0 for action commands
121 player: Integer; // used for action commands
122 end;
124 TAlias = record
125 name: AnsiString;
126 commands: SSArray;
127 end;
130 const
131 MsgTime = 144;
132 MaxScriptRecursion = 16;
134 DEBUG_STRING = 'DEBUG MODE';
136 var
137 ID: DWORD;
138 RecursionDepth: Word = 0;
139 RecursionLimitHit: Boolean = False;
140 Cons_Y: SmallInt;
141 ConsoleHeight: Single;
142 Cons_Shown: Boolean; // draw console
143 InputReady: Boolean; // allow text input in console/chat
144 Line: AnsiString;
145 CPos: Word;
146 //ConsoleHistory: SSArray;
147 CommandHistory: SSArray;
148 Whitelist: SSArray;
149 commands: Array of TCommand = nil;
150 Aliases: Array of TAlias = nil;
151 CmdIndex: Word;
152 conSkipLines: Integer = 0;
153 MsgArray: Array [0..4] of record
154 Msg: AnsiString;
155 Time: Word;
156 end;
158 gInputBinds: Array [0..e_MaxInputKeys - 1] of record
159 rep: Boolean;
160 down, up: SSArray;
161 end;
162 menu_toggled: BOOLEAN; (* hack for menu controls *)
163 ChatTop: BOOLEAN;
164 ConsoleStep: Single;
165 ConsoleTrans: Single;
166 ConsoleStdIn: Boolean;
169 procedure g_Console_Switch;
170 begin
171 Cons_Y := Min(0, Max(Cons_Y, -Floor(gScreenHeight * ConsoleHeight)));
172 if Cons_Shown = False then
173 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
174 gChatShow := False;
175 gConsoleShow := not gConsoleShow;
176 Cons_Shown := True;
177 InputReady := False;
178 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
179 end;
181 procedure g_Console_Chat_Switch (Team: Boolean = False);
182 begin
183 if not g_Game_IsNet then Exit;
184 Cons_Y := Min(0, Max(Cons_Y, -Floor(gScreenHeight * ConsoleHeight)));
185 if Cons_Shown = False then
186 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
187 gConsoleShow := False;
188 gChatShow := not gChatShow;
189 gChatTeam := Team;
190 Cons_Shown := True;
191 InputReady := False;
192 Line := '';
193 CPos := 1;
194 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
195 end;
197 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
198 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
199 var
200 pos: Integer = 1;
201 frac: Single = 1;
202 slen: Integer;
203 begin
204 result := false;
205 res := 0;
206 slen := Length(s);
207 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
208 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
209 if (pos > slen) then exit;
210 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
211 // integral part
212 while (pos <= slen) do
213 begin
214 if (s[pos] < '0') or (s[pos] > '9') then break;
215 res := res*10+Byte(s[pos])-48;
216 Inc(pos);
217 end;
218 if (pos <= slen) then
219 begin
220 // must be a dot
221 if (s[pos] <> '.') then exit;
222 Inc(pos);
223 while (pos <= slen) do
224 begin
225 if (s[pos] < '0') or (s[pos] > '9') then break;
226 frac := frac/10;
227 res += frac*(Byte(s[pos])-48);
228 Inc(pos);
229 end;
230 end;
231 if (pos <= slen) then exit; // oops
232 result := true;
233 end;
236 // ////////////////////////////////////////////////////////////////////////// //
237 // <0: no arg; 0/1: true/false; 666: toggle
238 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
239 begin
240 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
241 result := 0;
242 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
243 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
244 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
245 (CompareText(p[idx], 't') = 0) then result := 666;
246 end;
249 procedure boolVarHandler (me: PCommand; p: SSArray);
250 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
251 var
252 old: Boolean;
253 begin
254 if (Length(p) > 2) then
255 begin
256 conwritefln('too many arguments to ''%s''', [p[0]]);
257 end
258 else
259 begin
260 old := flag;
261 case conGetBoolArg(p, 1) of
262 -1: begin end;
263 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
264 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
265 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
266 end;
267 if flag <> old then
268 g_Console_WriteGameConfig();
269 if (Length(msg) = 0) then msg := p[0] else msg += ':';
270 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
271 end;
272 end;
273 begin
274 binaryFlag(PBoolean(me.ptr)^, me.msg);
275 end;
278 procedure intVarHandler (me: PCommand; p: SSArray);
279 var
280 old: Integer;
281 begin
282 if (Length(p) <> 2) then
283 begin
284 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
285 end
286 else
287 begin
288 try
289 old := PInteger(me.ptr)^;
290 PInteger(me.ptr)^ := StrToInt(p[1]);
291 if PInteger(me.ptr)^ <> old then
292 g_Console_WriteGameConfig();
293 except
294 conwritefln('invalid integer value: "%s"', [p[1]]);
295 end;
296 end;
297 end;
300 procedure wordVarHandler (me: PCommand; p: SSArray);
301 var
302 old: Integer;
303 begin
304 if (Length(p) <> 2) then
305 begin
306 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
307 end
308 else
309 begin
310 try
311 old := PWord(me.ptr)^;
312 PWord(me.ptr)^ := min($FFFF, StrToDWord(p[1]));
313 if PWord(me.ptr)^ <> old then
314 g_Console_WriteGameConfig();
315 except
316 conwritefln('invalid word value: "%s"', [p[1]]);
317 end;
318 end;
319 end;
322 procedure dwordVarHandler (me: PCommand; p: SSArray);
323 var
324 old: Integer;
325 begin
326 if (Length(p) <> 2) then
327 begin
328 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
329 end
330 else
331 begin
332 try
333 old := PCardinal(me.ptr)^;
334 PCardinal(me.ptr)^ := StrToDWord(p[1]);
335 if PCardinal(me.ptr)^ <> old then
336 g_Console_WriteGameConfig();
337 except
338 conwritefln('invalid dword value: "%s"', [p[1]]);
339 end;
340 end;
341 end;
344 procedure strVarHandler (me: PCommand; p: SSArray);
345 var
346 old: AnsiString;
347 begin
348 if (Length(p) <> 2) then
349 begin
350 conwritefln('%s %s', [me.cmd, QuoteStr(PAnsiString(me.ptr)^)]);
351 end
352 else
353 begin
354 old := PAnsiString(me.ptr)^;
355 PAnsiString(me.ptr)^ := p[1];
356 if PAnsiString(me.ptr)^ <> old then
357 g_Console_WriteGameConfig();
358 end;
359 end;
362 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
363 var
364 f: Integer;
365 cp: PCommand;
366 begin
367 f := Length(commands);
368 SetLength(commands, f+1);
369 cp := @commands[f];
370 cp.cmd := LowerCase(conname);
371 cp.proc := nil;
372 cp.procEx := boolVarHandler;
373 cp.help := ahelp;
374 cp.hidden := ahidden;
375 cp.ptr := pvar;
376 cp.msg := amsg;
377 cp.cheat := acheat;
378 cp.action := -1;
379 cp.player := -1;
380 end;
383 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
384 var
385 f: Integer;
386 cp: PCommand;
387 begin
388 f := Length(commands);
389 SetLength(commands, f+1);
390 cp := @commands[f];
391 cp.cmd := LowerCase(conname);
392 cp.proc := nil;
393 cp.procEx := intVarHandler;
394 cp.help := ahelp;
395 cp.hidden := ahidden;
396 cp.ptr := pvar;
397 cp.msg := amsg;
398 cp.cheat := acheat;
399 cp.action := -1;
400 cp.player := -1;
401 end;
404 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
405 var
406 f: Integer;
407 cp: PCommand;
408 begin
409 f := Length(commands);
410 SetLength(commands, f+1);
411 cp := @commands[f];
412 cp.cmd := LowerCase(conname);
413 cp.proc := nil;
414 cp.procEx := wordVarHandler;
415 cp.help := ahelp;
416 cp.hidden := ahidden;
417 cp.ptr := pvar;
418 cp.msg := amsg;
419 cp.cheat := acheat;
420 cp.action := -1;
421 cp.player := -1;
422 end;
425 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
426 var
427 f: Integer;
428 cp: PCommand;
429 begin
430 f := Length(commands);
431 SetLength(commands, f+1);
432 cp := @commands[f];
433 cp.cmd := LowerCase(conname);
434 cp.proc := nil;
435 cp.procEx := dwordVarHandler;
436 cp.help := ahelp;
437 cp.hidden := ahidden;
438 cp.ptr := pvar;
439 cp.msg := amsg;
440 cp.cheat := acheat;
441 cp.action := -1;
442 cp.player := -1;
443 end;
446 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
447 var
448 f: Integer;
449 cp: PCommand;
450 begin
451 f := Length(commands);
452 SetLength(commands, f+1);
453 cp := @commands[f];
454 cp.cmd := LowerCase(conname);
455 cp.proc := nil;
456 cp.procEx := strVarHandler;
457 cp.help := ahelp;
458 cp.hidden := ahidden;
459 cp.ptr := pvar;
460 cp.msg := amsg;
461 cp.cheat := acheat;
462 cp.action := -1;
463 cp.player := -1;
464 end;
466 // ////////////////////////////////////////////////////////////////////////// //
467 type
468 PVarSingle = ^TVarSingle;
469 TVarSingle = record
470 val: PSingle;
471 min, max, def: Single; // default will be starting value
472 end;
475 procedure singleVarHandler (me: PCommand; p: SSArray);
476 var
477 pv: PVarSingle;
478 nv, old: Single;
479 msg: AnsiString;
480 begin
481 if (Length(p) > 2) then
482 begin
483 conwritefln('too many arguments to ''%s''', [me.cmd]);
484 exit;
485 end;
486 pv := PVarSingle(me.ptr);
487 old := pv.val^;
488 if (Length(p) = 2) then
489 begin
490 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
491 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
492 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
493 (CompareText(p[1], 'ona') = 0) then
494 begin
495 pv.val^ := pv.def;
496 end
497 else
498 begin
499 if not conParseFloat(nv, p[1]) then
500 begin
501 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
502 exit;
503 end;
504 if (nv < pv.min) then nv := pv.min;
505 if (nv > pv.max) then nv := pv.max;
506 pv.val^ := nv;
507 end;
508 end;
509 if pv.val^ <> old then
510 g_Console_WriteGameConfig();
511 msg := me.msg;
512 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
513 conwritefln('%s %s', [msg, pv.val^]);
514 end;
517 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
518 var
519 f: Integer;
520 cp: PCommand;
521 pv: PVarSingle;
522 begin
523 GetMem(pv, sizeof(TVarSingle));
524 pv.val := pvar;
525 pv.min := amin;
526 pv.max := amax;
527 pv.def := pvar^;
528 f := Length(commands);
529 SetLength(commands, f+1);
530 cp := @commands[f];
531 cp.cmd := LowerCase(conname);
532 cp.proc := nil;
533 cp.procEx := singleVarHandler;
534 cp.help := ahelp;
535 cp.hidden := ahidden;
536 cp.ptr := pv;
537 cp.msg := amsg;
538 cp.cheat := acheat;
539 cp.action := -1;
540 cp.player := -1;
541 end;
544 // ////////////////////////////////////////////////////////////////////////// //
545 function GetStrACmd(var Str: AnsiString): AnsiString;
546 var
547 a: Integer;
548 begin
549 Result := '';
550 for a := 1 to Length(Str) do
551 if (a = Length(Str)) or (Str[a+1] = ';') then
552 begin
553 Result := Copy(Str, 1, a);
554 Delete(Str, 1, a+1);
555 Str := Trim(Str);
556 Exit;
557 end;
558 end;
560 function ParseAlias(Str: AnsiString): SSArray;
561 begin
562 Result := nil;
564 Str := Trim(Str);
566 if Str = '' then
567 Exit;
569 while Str <> '' do
570 begin
571 SetLength(Result, Length(Result)+1);
572 Result[High(Result)] := GetStrACmd(Str);
573 end;
574 end;
576 procedure ConsoleCommands(p: SSArray);
577 var
578 cmd, s: AnsiString;
579 a, b: Integer;
580 (* F: TextFile; *)
581 begin
582 cmd := LowerCase(p[0]);
583 s := '';
585 if cmd = 'clear' then
586 begin
587 //ConsoleHistory := nil;
588 cbufClear();
589 conSkipLines := 0;
591 for a := 0 to High(MsgArray) do
592 with MsgArray[a] do
593 begin
594 Msg := '';
595 Time := 0;
596 end;
597 end;
599 if cmd = 'clearhistory' then
600 CommandHistory := nil;
602 if cmd = 'showhistory' then
603 if CommandHistory <> nil then
604 begin
605 g_Console_Add('');
606 for a := 0 to High(CommandHistory) do
607 g_Console_Add(' '+CommandHistory[a]);
608 end;
610 if cmd = 'commands' then
611 begin
612 g_Console_Add('');
613 g_Console_Add('commands list:');
614 for a := High(commands) downto 0 do
615 begin
616 if (Length(commands[a].help) > 0) then
617 begin
618 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
619 end
620 else
621 begin
622 g_Console_Add(' '+commands[a].cmd);
623 end;
624 end;
625 end;
627 if cmd = 'time' then
628 g_Console_Add(TimeToStr(Now), True);
630 if cmd = 'date' then
631 g_Console_Add(DateToStr(Now), True);
633 if cmd = 'echo' then
634 if Length(p) > 1 then
635 begin
636 if p[1] = 'ololo' then
637 gCheats := True
638 else
639 begin
640 s := '';
641 for a := 1 to High(p) do
642 s := s + p[a] + ' ';
643 g_Console_Add(b_Text_Format(s), True);
644 end;
645 end
646 else
647 g_Console_Add('');
649 if cmd = 'dump' then
650 begin
651 (*
652 if ConsoleHistory <> nil then
653 begin
654 if Length(P) > 1 then
655 s := P[1]
656 else
657 s := GameDir+'/console.txt';
659 {$I-}
660 AssignFile(F, s);
661 Rewrite(F);
662 if IOResult <> 0 then
663 begin
664 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
665 CloseFile(F);
666 Exit;
667 end;
669 for a := 0 to High(ConsoleHistory) do
670 WriteLn(F, ConsoleHistory[a]);
672 CloseFile(F);
673 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
674 {$I+}
675 end;
676 *)
677 end;
679 if cmd = 'exec' then
680 begin
681 // exec <filename>
682 if Length(p) = 2 then
683 g_Console_ReadConfig(p[1])
684 else
685 g_Console_Add('exec <script file>');
686 end;
688 if cmd = 'writeconfig' then
689 begin
690 // writeconfig <filename>
691 if Length(p) = 2 then
692 begin
693 s := e_GetWriteableDir(ConfigDirs);
694 g_Console_WriteConfig(e_CatPath(s, p[1]))
695 end
696 else
697 begin
698 g_Console_Add('writeconfig <file>')
699 end
700 end;
702 if (cmd = 'ver') or (cmd = 'version') then
703 begin
704 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
705 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
706 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
707 end;
709 if cmd = 'alias' then
710 begin
711 // alias [alias_name] [commands]
712 if Length(p) > 1 then
713 begin
714 for a := 0 to High(Aliases) do
715 if Aliases[a].name = p[1] then
716 begin
717 if Length(p) > 2 then
718 Aliases[a].commands := ParseAlias(p[2])
719 else
720 for b := 0 to High(Aliases[a].commands) do
721 g_Console_Add(Aliases[a].commands[b]);
722 Exit;
723 end;
724 SetLength(Aliases, Length(Aliases)+1);
725 a := High(Aliases);
726 Aliases[a].name := p[1];
727 if Length(p) > 2 then
728 Aliases[a].commands := ParseAlias(p[2])
729 else
730 for b := 0 to High(Aliases[a].commands) do
731 g_Console_Add(Aliases[a].commands[b]);
732 end else
733 for a := 0 to High(Aliases) do
734 if Aliases[a].commands <> nil then
735 g_Console_Add(Aliases[a].name);
736 end;
738 if cmd = 'call' then
739 begin
740 // call <alias_name>
741 if Length(p) > 1 then
742 begin
743 if Aliases = nil then
744 Exit;
745 for a := 0 to High(Aliases) do
746 if Aliases[a].name = p[1] then
747 begin
748 if Aliases[a].commands <> nil then
749 begin
750 // with this system proper endless loop detection seems either impossible
751 // or very dirty to implement, so let's have this instead
752 // prevents endless loops
753 for b := 0 to High(Aliases[a].commands) do
754 begin
755 Inc(RecursionDepth);
756 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
757 if not RecursionLimitHit then
758 g_Console_Process(Aliases[a].commands[b], True);
759 Dec(RecursionDepth);
760 end;
761 if (RecursionDepth = 0) and RecursionLimitHit then
762 begin
763 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
764 RecursionLimitHit := False;
765 end;
766 end;
767 Exit;
768 end;
769 end
770 else
771 g_Console_Add('call <alias name>');
772 end;
773 end;
775 procedure WhitelistCommand(cmd: AnsiString);
776 var
777 a: Integer;
778 begin
779 SetLength(Whitelist, Length(Whitelist)+1);
780 a := High(Whitelist);
781 Whitelist[a] := LowerCase(cmd);
782 end;
784 procedure segfault (p: SSArray);
785 var
786 pp: PByte = nil;
787 begin
788 pp^ := 0;
789 end;
791 function GetCommandString (p: SSArray): AnsiString;
792 var i: Integer;
793 begin
794 result := '';
795 if Length(p) >= 1 then
796 begin
797 result := p[0];
798 for i := 1 to High(p) do
799 result := result + '; ' + p[i]
800 end
801 end;
803 function QuoteStr(str: String): String;
804 begin
805 if Pos(' ', str) > 0 then
806 Result := '"' + str + '"'
807 else
808 Result := str;
809 end;
811 procedure BindCommands (p: SSArray);
812 var cmd, key: AnsiString; i: Integer;
813 begin
814 cmd := LowerCase(p[0]);
815 case cmd of
816 'bind':
817 // bind <key> [down [up]]
818 if (Length(p) >= 2) and (Length(p) <= 4) then
819 begin
820 i := 0;
821 key := LowerCase(p[1]);
822 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
823 if i < e_MaxInputKeys then
824 begin
825 if Length(p) = 2 then
826 g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
827 else if Length(p) = 3 then
828 g_Console_BindKey(i, p[2], '')
829 else (* len = 4 *)
830 g_Console_BindKey(i, p[2], p[3])
831 end
832 else
833 g_Console_Add('bind: "' + p[1] + '" is not a key')
834 end
835 else
836 begin
837 g_Console_Add('bind <key> <down action> [up action]')
838 end;
839 'bindrep':
840 // bindrep <key>
841 if Length(p) = 2 then
842 begin
843 key := LowerCase(p[1]);
844 i := 0;
845 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
846 if i < e_MaxInputKeys then
847 gInputBinds[i].rep := True
848 else
849 g_Console_Add('bindrep: "' + p[1] + '" is not a key')
850 end
851 else
852 g_Console_Add('bindrep <key>');
853 'bindunrep':
854 // bindunrep <key>
855 if Length(p) = 2 then
856 begin
857 key := LowerCase(p[1]);
858 i := 0;
859 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
860 if i < e_MaxInputKeys then
861 gInputBinds[i].rep := False
862 else
863 g_Console_Add('bindunrep: "' + p[1] + '" is not a key')
864 end
865 else
866 g_Console_Add('bindunrep <key>');
867 'bindlist':
868 for i := 0 to e_MaxInputKeys - 1 do
869 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
870 g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
871 'unbind':
872 // unbind <key>
873 if Length(p) = 2 then
874 begin
875 key := LowerCase(p[1]);
876 i := 0;
877 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
878 if i < e_MaxInputKeys then
879 g_Console_BindKey(i, '')
880 else
881 g_Console_Add('unbind: "' + p[1] + '" is not a key')
882 end
883 else
884 g_Console_Add('unbind <key>');
885 'unbindall':
886 for i := 0 to e_MaxInputKeys - 1 do
887 g_Console_BindKey(i, '');
888 'showkeyboard':
889 g_Touch_ShowKeyboard(True);
890 'hidekeyboard':
891 g_Touch_ShowKeyboard(False);
892 'togglemenu':
893 begin
894 if gConsoleShow then
895 g_Console_Switch
896 else if gChatShow then
897 g_Console_Chat_Switch
898 else
899 KeyPress(VK_ESCAPE);
900 menu_toggled := True
901 end;
902 'toggleconsole':
903 g_Console_Switch;
904 'togglechat':
905 g_Console_Chat_Switch;
906 'toggleteamchat':
907 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
908 g_Console_Chat_Switch(True);
909 end
910 end;
912 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
913 var
914 a: Integer;
915 cp: PCommand;
916 begin
917 SetLength(commands, Length(commands)+1);
918 a := High(commands);
919 cp := @commands[a];
920 cp.cmd := LowerCase(cmd);
921 cp.proc := proc;
922 cp.procEx := nil;
923 cp.help := ahelp;
924 cp.hidden := ahidden;
925 cp.ptr := nil;
926 cp.msg := '';
927 cp.cheat := acheat;
928 cp.action := -1;
929 cp.player := -1;
930 end;
932 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
933 const
934 PrefixList: array [0..1] of AnsiString = ('+', '-');
935 PlayerList: array [0..1] of Integer = (1, 2);
936 var
937 s: AnsiString;
938 i: Integer;
940 procedure NewAction (cmd: AnsiString; player: Integer);
941 var cp: PCommand;
942 begin
943 SetLength(commands, Length(commands) + 1);
944 cp := @commands[High(commands)];
945 cp.cmd := LowerCase(cmd);
946 cp.proc := nil;
947 cp.procEx := nil;
948 cp.help := help;
949 cp.hidden := hidden;
950 cp.ptr := nil;
951 cp.msg := '';
952 cp.cheat := cheat;
953 cp.action := action;
954 cp.player := player;
955 end;
957 begin
958 ASSERT(action >= FIRST_ACTION);
959 ASSERT(action <= LAST_ACTION);
960 for s in PrefixList do
961 begin
962 NewAction(s + cmd, 0);
963 for i in PlayerList do
964 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
965 end
966 end;
968 procedure ReadStdIn;
969 var
970 K: Char;
971 KEv: TKeyEvent;
972 begin
973 gConsoleShow := True;
974 InputReady := True;
975 // one key per frame
976 KEv := PollKeyEvent();
977 if KEv <> 0 then
978 begin
979 K := GetKeyEventChar(TranslateKeyEvent(GetKeyEvent()));
980 Write(K);
981 case K of
982 #8: g_Console_Control(IK_BACKSPACE);
983 #10, #13: g_Console_Control(IK_RETURN);
984 #32..#126: g_Console_Char(K);
985 // arrow keys and DEL all return 0 for some reason, so fuck em
986 end;
987 end;
988 end;
990 procedure g_Console_SysInit;
991 var a: Integer;
992 begin
993 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
994 gConsoleShow := False;
995 gChatShow := False;
996 Cons_Shown := False;
997 InputReady := False;
998 CPos := 1;
1000 for a := 0 to High(MsgArray) do
1001 with MsgArray[a] do
1002 begin
1003 Msg := '';
1004 Time := 0;
1005 end;
1007 AddCommand('segfault', segfault, 'make segfault');
1009 AddCommand('quit', SystemCommands);
1010 AddCommand('exit', SystemCommands);
1011 AddCommand('r_reset', SystemCommands);
1012 AddCommand('r_maxfps', SystemCommands);
1013 AddCommand('g_language', SystemCommands);
1015 AddCommand('bind', BindCommands);
1016 AddCommand('bindrep', BindCommands);
1017 AddCommand('bindunrep', BindCommands);
1018 AddCommand('bindlist', BindCommands);
1019 AddCommand('unbind', BindCommands);
1020 AddCommand('unbindall', BindCommands);
1021 AddCommand('showkeyboard', BindCommands);
1022 AddCommand('hidekeyboard', BindCommands);
1023 AddCommand('togglemenu', BindCommands);
1024 AddCommand('toggleconsole', BindCommands);
1025 AddCommand('togglechat', BindCommands);
1026 AddCommand('toggleteamchat', BindCommands);
1028 AddCommand('clear', ConsoleCommands, 'clear console');
1029 AddCommand('clearhistory', ConsoleCommands);
1030 AddCommand('showhistory', ConsoleCommands);
1031 AddCommand('commands', ConsoleCommands);
1032 AddCommand('time', ConsoleCommands);
1033 AddCommand('date', ConsoleCommands);
1034 AddCommand('echo', ConsoleCommands);
1035 AddCommand('dump', ConsoleCommands);
1036 AddCommand('exec', ConsoleCommands);
1037 AddCommand('writeconfig', ConsoleCommands);
1038 AddCommand('alias', ConsoleCommands);
1039 AddCommand('call', ConsoleCommands);
1040 AddCommand('ver', ConsoleCommands);
1041 AddCommand('version', ConsoleCommands);
1043 AddCommand('d_window', DebugCommands);
1044 AddCommand('d_sounds', DebugCommands);
1045 AddCommand('d_frames', DebugCommands);
1046 AddCommand('d_winmsg', DebugCommands);
1047 AddCommand('d_monoff', DebugCommands);
1048 AddCommand('d_botoff', DebugCommands);
1049 AddCommand('d_monster', DebugCommands);
1050 AddCommand('d_health', DebugCommands);
1051 AddCommand('d_player', DebugCommands);
1052 AddCommand('d_joy', DebugCommands);
1053 AddCommand('d_mem', DebugCommands);
1055 AddCommand('p1_name', PlayerSettingsCVars);
1056 AddCommand('p2_name', PlayerSettingsCVars);
1057 AddCommand('p1_color', PlayerSettingsCVars);
1058 AddCommand('p2_color', PlayerSettingsCVars);
1059 AddCommand('p1_model', PlayerSettingsCVars);
1060 AddCommand('p2_model', PlayerSettingsCVars);
1061 AddCommand('p1_team', PlayerSettingsCVars);
1062 AddCommand('p2_team', PlayerSettingsCVars);
1063 AddCommand('p1_autoswitch', PlayerSettingsCVars);
1064 AddCommand('p2_autoswitch', PlayerSettingsCVars);
1065 AddCommand('p1_switch_empty', PlayerSettingsCVars);
1066 AddCommand('p2_switch_empty', PlayerSettingsCVars);
1067 AddCommand('p1_skip_fist', PlayerSettingsCVars);
1068 AddCommand('p2_skip_fist', PlayerSettingsCVars);
1069 AddCommand('p1_priority_kastet', PlayerSettingsCVars);
1070 AddCommand('p2_priority_kastet', PlayerSettingsCVars);
1071 AddCommand('p1_priority_saw', PlayerSettingsCVars);
1072 AddCommand('p2_priority_saw', PlayerSettingsCVars);
1073 AddCommand('p1_priority_pistol', PlayerSettingsCVars);
1074 AddCommand('p2_priority_pistol', PlayerSettingsCVars);
1075 AddCommand('p1_priority_shotgun1', PlayerSettingsCVars);
1076 AddCommand('p2_priority_shotgun1', PlayerSettingsCVars);
1077 AddCommand('p1_priority_shotgun2', PlayerSettingsCVars);
1078 AddCommand('p2_priority_shotgun2', PlayerSettingsCVars);
1079 AddCommand('p1_priority_chaingun', PlayerSettingsCVars);
1080 AddCommand('p2_priority_chaingun', PlayerSettingsCVars);
1081 AddCommand('p1_priority_rocketlauncher', PlayerSettingsCVars);
1082 AddCommand('p2_priority_rocketlauncher', PlayerSettingsCVars);
1083 AddCommand('p1_priority_plasma', PlayerSettingsCVars);
1084 AddCommand('p2_priority_plasma', PlayerSettingsCVars);
1085 AddCommand('p1_priority_bfg', PlayerSettingsCVars);
1086 AddCommand('p2_priority_bfg', PlayerSettingsCVars);
1087 AddCommand('p1_priority_super', PlayerSettingsCVars);
1088 AddCommand('p2_priority_super', PlayerSettingsCVars);
1089 AddCommand('p1_priority_flamethrower', PlayerSettingsCVars);
1090 AddCommand('p2_priority_flamethrower', PlayerSettingsCVars);
1091 AddCommand('p1_priority_berserk', PlayerSettingsCVars);
1092 AddCommand('p2_priority_berserk', PlayerSettingsCVars);
1094 AddCommand('g_max_particles', GameCVars);
1095 AddCommand('g_max_shells', GameCVars);
1096 AddCommand('g_max_gibs', GameCVars);
1097 AddCommand('g_max_corpses', GameCVars);
1098 AddCommand('g_force_model', GameCVars);
1099 AddCommand('g_force_model_name', GameCVars);
1100 AddCommand('g_gamemode', GameCVars);
1101 AddCommand('g_friendlyfire', GameCVars);
1102 AddCommand('g_friendly_hit_trace', GameCVars);
1103 AddCommand('g_friendly_hit_projectile', GameCVars);
1104 AddCommand('g_friendly_absorb_damage', GameCVars);
1105 AddCommand('g_weaponstay', GameCVars);
1106 AddCommand('g_allow_exit', GameCVars);
1107 AddCommand('g_dm_keys', GameCVars);
1108 AddCommand('g_allow_monsters', GameCVars);
1109 AddCommand('g_allow_dropflag', GameCVars);
1110 AddCommand('g_throw_flag', GameCVars);
1111 AddCommand('g_bot_vsmonsters', GameCVars);
1112 AddCommand('g_bot_vsplayers', GameCVars);
1113 AddCommand('g_max_bots', GameCVars); // intentionally not whitelisted
1114 AddCommand('g_scorelimit', GameCVars);
1115 AddCommand('g_timelimit', GameCVars);
1116 AddCommand('g_maxlives', GameCVars);
1117 AddCommand('g_warmup_time', GameCVars);
1118 AddCommand('g_spawn_invul', GameCVars);
1119 AddCommand('g_item_respawn_time', GameCVars);
1120 AddCommand('sv_intertime', GameCVars);
1122 AddCommand('sv_name', NetServerCVars);
1123 AddCommand('sv_passwd', NetServerCVars);
1124 AddCommand('sv_maxplrs', NetServerCVars);
1125 AddCommand('sv_public', NetServerCVars);
1126 AddCommand('sv_port', NetServerCVars);
1128 AddCommand('pause', GameCommands);
1129 AddCommand('endgame', GameCommands);
1130 AddCommand('restart', GameCommands);
1131 AddCommand('addbot', GameCommands);
1132 AddCommand('bot_add', GameCommands);
1133 AddCommand('bot_addlist', GameCommands);
1134 AddCommand('bot_addred', GameCommands);
1135 AddCommand('bot_addblue', GameCommands);
1136 AddCommand('bot_removeall', GameCommands);
1137 AddCommand('chat', GameCommands);
1138 AddCommand('teamchat', GameCommands);
1139 AddCommand('announce', GameCommands);
1140 AddCommand('an', GameCommands);
1141 AddCommand('game', GameCommands);
1142 AddCommand('host', GameCommands);
1143 AddCommand('map', GameCommands);
1144 AddCommand('nextmap', GameCommands);
1145 AddCommand('endmap', GameCommands);
1146 AddCommand('goodbye', GameCommands);
1147 AddCommand('suicide', GameCommands);
1148 AddCommand('spectate', GameCommands);
1149 AddCommand('ready', GameCommands);
1150 AddCommand('kick', GameCommands);
1151 AddCommand('kick_id', GameCommands);
1152 AddCommand('kick_pid', GameCommands);
1153 AddCommand('ban', GameCommands);
1154 AddCommand('ban_id', GameCommands);
1155 AddCommand('ban_pid', GameCommands);
1156 AddCommand('permban', GameCommands);
1157 AddCommand('permban_id', GameCommands);
1158 AddCommand('permban_pid', GameCommands);
1159 AddCommand('permban_ip', GameCommands);
1160 AddCommand('unban', GameCommands);
1161 AddCommand('connect', GameCommands);
1162 AddCommand('disconnect', GameCommands);
1163 AddCommand('reconnect', GameCommands);
1164 AddCommand('say', GameCommands);
1165 AddCommand('tell', GameCommands);
1166 AddCommand('centerprint', GameCommands);
1167 AddCommand('overtime', GameCommands);
1168 AddCommand('rcon_password', GameCommands);
1169 AddCommand('rcon', GameCommands);
1170 AddCommand('callvote', GameCommands);
1171 AddCommand('vote', GameCommands);
1172 AddCommand('clientlist', GameCommands);
1173 AddCommand('event', GameCommands);
1174 AddCommand('screenshot', GameCommands);
1175 AddCommand('weapnext', GameCommands);
1176 AddCommand('weapprev', GameCommands);
1177 AddCommand('weapon', GameCommands);
1178 AddCommand('dropflag', GameCommands);
1179 AddCommand('p1_weapnext', GameCommands);
1180 AddCommand('p1_weapprev', GameCommands);
1181 AddCommand('p1_weapon', GameCommands);
1182 AddCommand('p1_weapbest', GameCommands);
1183 AddCommand('p1_dropflag', GameCommands);
1184 AddCommand('p2_weapnext', GameCommands);
1185 AddCommand('p2_weapprev', GameCommands);
1186 AddCommand('p2_weapon', GameCommands);
1187 AddCommand('p2_weapbest', GameCommands);
1188 AddCommand('p2_dropflag', GameCommands);
1190 AddCommand('god', GameCheats);
1191 AddCommand('notarget', GameCheats);
1192 AddCommand('give', GameCheats); // "exit" too ;-)
1193 AddCommand('open', GameCheats);
1194 AddCommand('fly', GameCheats);
1195 AddCommand('noclip', GameCheats);
1196 AddCommand('speedy', GameCheats);
1197 AddCommand('jumpy', GameCheats);
1198 AddCommand('noreload', GameCheats);
1199 AddCommand('aimline', GameCheats);
1200 AddCommand('automap', GameCheats);
1202 AddAction('jump', ACTION_JUMP);
1203 AddAction('moveleft', ACTION_MOVELEFT);
1204 AddAction('moveright', ACTION_MOVERIGHT);
1205 AddAction('lookup', ACTION_LOOKUP);
1206 AddAction('lookdown', ACTION_LOOKDOWN);
1207 AddAction('attack', ACTION_ATTACK);
1208 AddAction('scores', ACTION_SCORES);
1209 AddAction('activate', ACTION_ACTIVATE);
1210 AddAction('strafe', ACTION_STRAFE);
1212 WhitelistCommand('say');
1213 WhitelistCommand('tell');
1214 WhitelistCommand('overtime');
1215 WhitelistCommand('ready');
1216 WhitelistCommand('map');
1217 WhitelistCommand('nextmap');
1218 WhitelistCommand('endmap');
1219 WhitelistCommand('restart');
1220 WhitelistCommand('kick');
1221 WhitelistCommand('kick_pid');
1222 WhitelistCommand('ban');
1223 WhitelistCommand('ban_pid');
1224 WhitelistCommand('centerprint');
1226 WhitelistCommand('addbot');
1227 WhitelistCommand('bot_add');
1228 WhitelistCommand('bot_addred');
1229 WhitelistCommand('bot_addblue');
1230 WhitelistCommand('bot_removeall');
1232 WhitelistCommand('g_gamemode');
1233 WhitelistCommand('g_friendlyfire');
1234 WhitelistCommand('g_friendly_hit_trace');
1235 WhitelistCommand('g_friendly_hit_projectile');
1236 WhitelistCommand('g_friendly_absorb_damage');
1237 WhitelistCommand('g_weaponstay');
1238 WhitelistCommand('g_allow_exit');
1239 WhitelistCommand('g_dm_keys');
1240 WhitelistCommand('g_allow_monsters');
1241 WhitelistCommand('g_bot_vsmonsters');
1242 WhitelistCommand('g_bot_vsplayers');
1243 WhitelistCommand('g_scorelimit');
1244 WhitelistCommand('g_timelimit');
1245 WhitelistCommand('g_maxlives');
1246 WhitelistCommand('g_warmup_time');
1247 WhitelistCommand('g_spawn_invul');
1248 WhitelistCommand('g_item_respawn_time');
1250 g_Console_ResetBinds;
1251 g_Console_ReadConfig(gConfigScript);
1252 g_Console_ReadConfig(autoexecScript);
1253 gParsingBinds := False;
1254 end;
1256 procedure g_Console_Init;
1257 begin
1258 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
1259 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
1260 g_Console_Add('');
1261 {$IFDEF HEADLESS}
1262 if ConsoleStdIn then
1263 begin
1264 InitKeyboard();
1265 conbufStdOutRawMode := true;
1266 end;
1267 {$ENDIF}
1268 end;
1270 procedure g_Console_Update;
1271 var
1272 a, b, Step: Integer;
1273 begin
1274 {$IFDEF HEADLESS}
1275 if ConsoleStdIn then
1276 ReadStdIn();
1277 {$ENDIF}
1279 if Cons_Shown then
1280 begin
1281 Step := Max(1, Round(Floor(gScreenHeight * ConsoleHeight) * ConsoleStep));
1282 if gConsoleShow then
1283 begin
1284 (* Open animation *)
1285 Cons_Y := Min(Cons_Y + Step, 0);
1286 InputReady := True
1287 end
1288 else
1289 begin
1290 (* Close animation *)
1291 Cons_Y := Max(Cons_Y - Step, -Floor(gScreenHeight * ConsoleHeight));
1292 Cons_Shown := Cons_Y > -Floor(gScreenHeight * ConsoleHeight);
1293 InputReady := False
1294 end;
1296 if gChatShow then
1297 InputReady := True
1298 end;
1300 a := 0;
1301 while a <= High(MsgArray) do
1302 begin
1303 if MsgArray[a].Time > 0 then
1304 begin
1305 if MsgArray[a].Time = 1 then
1306 begin
1307 if a < High(MsgArray) then
1308 begin
1309 for b := a to High(MsgArray)-1 do
1310 MsgArray[b] := MsgArray[b+1];
1312 MsgArray[High(MsgArray)].Time := 0;
1314 a := a - 1;
1315 end;
1316 end
1317 else
1318 Dec(MsgArray[a].Time);
1319 end;
1321 a := a + 1;
1322 end;
1323 end;
1326 procedure drawConsoleText ();
1327 var
1328 CWidth, CHeight: Byte;
1329 ty: Integer;
1330 sp, ep: LongWord;
1331 skip: Integer;
1333 procedure putLine (sp, ep: LongWord);
1334 var
1335 p: LongWord;
1336 wdt, cw: Integer;
1337 begin
1338 p := sp;
1339 wdt := 0;
1340 while p <> ep do
1341 begin
1342 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1343 if wdt+cw > gScreenWidth-8 then break;
1344 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1345 Inc(wdt, cw);
1346 cbufNext(p);
1347 end;
1348 if p <> ep then putLine(p, ep); // do rest of the line first
1349 // now print our part
1350 if skip = 0 then
1351 begin
1352 ep := p;
1353 p := sp;
1354 wdt := 2;
1355 while p <> ep do
1356 begin
1357 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1358 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1359 Inc(wdt, cw);
1360 cbufNext(p);
1361 end;
1362 Dec(ty, CHeight);
1363 end
1364 else
1365 begin
1366 Dec(skip);
1367 end;
1368 end;
1370 begin
1371 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1372 ty := Floor(gScreenHeight * ConsoleHeight) - 4 - 2 * CHeight - Abs(Cons_Y);
1373 skip := conSkipLines;
1374 cbufLastLine(sp, ep);
1375 repeat
1376 putLine(sp, ep);
1377 if ty+CHeight <= 0 then break;
1378 until not cbufLineUp(sp, ep);
1379 end;
1381 procedure g_Console_Draw(MessagesOnly: Boolean = False);
1382 var
1383 CWidth, CHeight: Byte;
1384 mfW, mfH: Word;
1385 a, b, offset_y: Integer;
1386 begin
1387 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1389 if ChatTop and gChatShow then
1390 offset_y := CHeight
1391 else
1392 offset_y := 0;
1394 for a := 0 to High(MsgArray) do
1395 if MsgArray[a].Time > 0 then
1396 e_TextureFontPrintFmt(0, offset_y + CHeight * a, MsgArray[a].Msg, gStdFont, True);
1398 if MessagesOnly then Exit;
1400 if gChatShow then
1401 begin
1402 if ChatTop then
1403 offset_y := 0
1404 else
1405 offset_y := gScreenHeight - CHeight - 1;
1406 if gChatTeam then
1407 begin
1408 e_TextureFontPrintEx(0, offset_y, 'say team> ' + Line, gStdFont, 255, 255, 255, 1, True);
1409 e_TextureFontPrintEx((CPos + 9) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1410 end
1411 else
1412 begin
1413 e_TextureFontPrintEx(0, offset_y, 'say> ' + Line, gStdFont, 255, 255, 255, 1, True);
1414 e_TextureFontPrintEx((CPos + 4) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1415 end
1416 end;
1418 if not Cons_Shown then
1419 Exit;
1421 if gDebugMode then
1422 begin
1423 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1424 a := (gScreenWidth - 2*mfW) div 2;
1425 b := Cons_Y + (Floor(gScreenHeight * ConsoleHeight) - 2 * mfH) div 2;
1426 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1427 _RGB(128, 0, 0), 2.0);
1428 end;
1430 e_DrawSize(ID, 0, Cons_Y, Round(ConsoleTrans * 255), False, False, gScreenWidth, Floor(gScreenHeight * ConsoleHeight));
1431 e_TextureFontPrint(0, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - CHeight - 4, '> ' + Line, gStdFont);
1433 drawConsoleText();
1434 (*
1435 if ConsoleHistory <> nil then
1436 begin
1437 b := 0;
1438 if CHeight > 0 then
1439 if Length(ConsoleHistory) > (Floor(gScreenHeight * ConsoleHeight) div CHeight) - 1 then
1440 b := Length(ConsoleHistory) - (Floor(gScreenHeight * ConsoleHeight) div CHeight) + 1;
1442 b := Max(b-Offset, 0);
1443 d := Max(High(ConsoleHistory)-Offset, 0);
1445 c := 2;
1446 for a := d downto b do
1447 begin
1448 e_TextureFontPrintFmt(0, Floor(gScreenHeight * ConsoleHeight) - 4 - c * CHeight - Abs(Cons_Y), ConsoleHistory[a], gStdFont, True);
1449 c := c + 1;
1450 end;
1451 end;
1452 *)
1454 e_TextureFontPrint((CPos + 1) * CWidth, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - 21, '_', gStdFont);
1455 end;
1457 procedure g_Console_Char(C: AnsiChar);
1458 begin
1459 if InputReady and (gConsoleShow or gChatShow) then
1460 begin
1461 Insert(C, Line, CPos);
1462 CPos := CPos + 1;
1463 end
1464 end;
1467 var
1468 tcomplist: array of AnsiString = nil;
1469 tcompidx: array of Integer = nil;
1471 procedure Complete ();
1472 var
1473 i, c: Integer;
1474 tused: Integer;
1475 ll, lpfx, cmd: AnsiString;
1476 begin
1477 if (Length(Line) = 0) then
1478 begin
1479 g_Console_Add('');
1480 for i := 0 to High(commands) do
1481 begin
1482 // hidden commands are hidden when cheats aren't enabled
1483 if commands[i].hidden and not conIsCheatsEnabled then continue;
1484 if (Length(commands[i].help) > 0) then
1485 begin
1486 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1487 end
1488 else
1489 begin
1490 g_Console_Add(' '+commands[i].cmd);
1491 end;
1492 end;
1493 exit;
1494 end;
1496 ll := LowerCase(Line);
1497 lpfx := '';
1499 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1500 begin
1501 ll := Copy(ll, 0, Length(ll)-1);
1502 for i := 0 to High(commands) do
1503 begin
1504 // hidden commands are hidden when cheats aren't enabled
1505 if commands[i].hidden and not conIsCheatsEnabled then continue;
1506 if (commands[i].cmd = ll) then
1507 begin
1508 if (Length(commands[i].help) > 0) then
1509 begin
1510 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1511 end;
1512 end;
1513 end;
1514 exit;
1515 end;
1517 // build completion list
1518 tused := 0;
1519 for i := 0 to High(commands) do
1520 begin
1521 // hidden commands are hidden when cheats aren't enabled
1522 if commands[i].hidden and not conIsCheatsEnabled then continue;
1523 cmd := commands[i].cmd;
1524 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1525 begin
1526 if (tused = Length(tcomplist)) then
1527 begin
1528 SetLength(tcomplist, Length(tcomplist)+128);
1529 SetLength(tcompidx, Length(tcompidx)+128);
1530 end;
1531 tcomplist[tused] := cmd;
1532 tcompidx[tused] := i;
1533 Inc(tused);
1534 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1535 end;
1536 end;
1538 // get longest prefix
1539 for i := 0 to tused-1 do
1540 begin
1541 cmd := tcomplist[i];
1542 for c := 1 to Length(lpfx) do
1543 begin
1544 if (c > Length(cmd)) then break;
1545 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1546 end;
1547 end;
1549 if (tused = 0) then exit;
1551 if (tused = 1) then
1552 begin
1553 Line := tcomplist[0]+' ';
1554 CPos := Length(Line)+1;
1555 end
1556 else
1557 begin
1558 // has longest prefix?
1559 if (Length(lpfx) > Length(ll)) then
1560 begin
1561 Line := lpfx;
1562 CPos:= Length(Line)+1;
1563 end
1564 else
1565 begin
1566 g_Console_Add('');
1567 for i := 0 to tused-1 do
1568 begin
1569 if (Length(commands[tcompidx[i]].help) > 0) then
1570 begin
1571 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1572 end
1573 else
1574 begin
1575 g_Console_Add(' '+tcomplist[i]);
1576 end;
1577 end;
1578 end;
1579 end;
1580 end;
1583 procedure g_Console_Control(K: Word);
1584 begin
1585 case K of
1586 IK_BACKSPACE:
1587 if (Length(Line) > 0) and (CPos > 1) then
1588 begin
1589 Delete(Line, CPos-1, 1);
1590 CPos := CPos-1;
1591 end;
1592 IK_DELETE:
1593 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1594 Delete(Line, CPos, 1);
1595 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1596 if CPos > 1 then
1597 CPos := CPos - 1;
1598 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1599 if CPos <= Length(Line) then
1600 CPos := CPos + 1;
1601 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1602 begin
1603 if gConsoleShow then
1604 g_Console_Process(Line)
1605 else
1606 if gChatShow then
1607 begin
1608 if (Length(Line) > 0) and g_Game_IsNet then
1609 begin
1610 if gChatTeam then
1611 begin
1612 if g_Game_IsClient then
1613 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1614 else
1615 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1616 NET_CHAT_TEAM, gPlayer1Settings.Team);
1617 end
1618 else
1619 begin
1620 if g_Game_IsClient then
1621 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1622 else
1623 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1624 NET_CHAT_PLAYER);
1625 end;
1626 end;
1628 Line := '';
1629 CPos := 1;
1630 gJustChatted := True;
1631 g_Console_Chat_Switch;
1632 InputReady := False;
1633 end;
1634 end;
1635 IK_TAB:
1636 if not gChatShow then
1637 Complete();
1638 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1639 if not gChatShow then
1640 if (CommandHistory <> nil) and
1641 (CmdIndex < Length(CommandHistory)) then
1642 begin
1643 if CmdIndex < Length(CommandHistory)-1 then
1644 CmdIndex := CmdIndex + 1;
1645 Line := CommandHistory[CmdIndex];
1646 CPos := Length(Line) + 1;
1647 end;
1648 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1649 if not gChatShow then
1650 if (CommandHistory <> nil) and
1651 (CmdIndex <= Length(CommandHistory)) then
1652 begin
1653 if CmdIndex > 0 then
1654 CmdIndex := CmdIndex - 1;
1655 Line := CommandHistory[CmdIndex];
1656 Cpos := Length(Line) + 1;
1657 end;
1658 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1659 if not gChatShow then Inc(conSkipLines);
1660 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1661 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1662 IK_HOME, IK_KPHOME:
1663 CPos := 1;
1664 IK_END, IK_KPEND:
1665 CPos := Length(Line) + 1;
1666 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1667 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, (*IK_EQUALS,*)
1668 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1669 (* see TEXTINPUT event *)
1670 end
1671 end;
1673 function GetStr(var Str: AnsiString): AnsiString;
1674 var
1675 a, b: Integer;
1676 begin
1677 Result := '';
1678 if Str[1] = '"' then
1679 begin
1680 for b := 1 to Length(Str) do
1681 if (b = Length(Str)) or (Str[b+1] = '"') then
1682 begin
1683 Result := Copy(Str, 2, b-1);
1684 Delete(Str, 1, b+1);
1685 Str := Trim(Str);
1686 Exit;
1687 end;
1688 end;
1690 for a := 1 to Length(Str) do
1691 if (a = Length(Str)) or (Str[a+1] = ' ') then
1692 begin
1693 Result := Copy(Str, 1, a);
1694 Delete(Str, 1, a+1);
1695 Str := Trim(Str);
1696 Exit;
1697 end;
1698 end;
1700 function ParseString(Str: AnsiString): SSArray;
1701 begin
1702 Result := nil;
1704 Str := Trim(Str);
1706 if Str = '' then
1707 Exit;
1709 while Str <> '' do
1710 begin
1711 SetLength(Result, Length(Result)+1);
1712 Result[High(Result)] := GetStr(Str);
1713 end;
1714 end;
1716 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1718 procedure conmsg (s: AnsiString);
1719 var
1720 a: Integer;
1721 begin
1722 if length(s) = 0 then exit;
1723 for a := 0 to High(MsgArray) do
1724 begin
1725 with MsgArray[a] do
1726 begin
1727 if Time = 0 then
1728 begin
1729 Msg := s;
1730 Time := MsgTime;
1731 exit;
1732 end;
1733 end;
1734 end;
1735 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1736 with MsgArray[High(MsgArray)] do
1737 begin
1738 Msg := L;
1739 Time := MsgTime;
1740 end;
1741 end;
1743 var
1744 f: Integer;
1745 begin
1746 // put it to console
1747 cbufPut(L);
1748 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1750 // now show 'em out of console too
1751 show := show and gAllowConsoleMessages;
1752 if show and gShowMessages then
1753 begin
1754 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1755 while length(L) > 0 do
1756 begin
1757 f := Pos(#10, L);
1758 if f <= 0 then f := length(L)+1;
1759 conmsg(Copy(L, 1, f-1));
1760 Delete(L, 1, f);
1761 end;
1762 end;
1764 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1765 //ConsoleHistory[High(ConsoleHistory)] := L;
1767 (*
1768 {$IFDEF HEADLESS}
1769 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1770 {$ENDIF}
1771 *)
1772 end;
1775 var
1776 consolewriterLastWasEOL: Boolean = false;
1778 procedure consolewriter (constref buf; len: SizeUInt);
1779 var
1780 b: PByte;
1781 begin
1782 if (len < 1) then exit;
1783 b := PByte(@buf);
1784 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1785 while (len > 0) do
1786 begin
1787 if (b[0] <> 13) and (b[0] <> 10) then
1788 begin
1789 cbufPut(AnsiChar(b[0]));
1790 end
1791 else
1792 begin
1793 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1794 cbufPut(#10);
1795 end;
1796 len -= 1;
1797 b += 1;
1798 end;
1799 end;
1802 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1803 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1804 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1805 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1806 begin
1807 g_Console_Add(s, show);
1808 end;
1811 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1812 begin
1813 if show then
1814 begin
1815 g_Console_Add(formatstrf(s, args), true);
1816 end
1817 else
1818 begin
1819 consolewriterLastWasEOL := false;
1820 formatstrf(s, args, consolewriter);
1821 if not consolewriterLastWasEOL then cbufPut(#10);
1822 end;
1823 end;
1826 procedure g_Console_Clear();
1827 begin
1828 //ConsoleHistory := nil;
1829 cbufClear();
1830 conSkipLines := 0;
1831 end;
1833 procedure AddToHistory(L: AnsiString);
1834 var
1835 len: Integer;
1836 begin
1837 len := Length(CommandHistory);
1839 if (len = 0) or
1840 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1841 begin
1842 SetLength(CommandHistory, len+1);
1843 CommandHistory[len] := L;
1844 end;
1846 CmdIndex := Length(CommandHistory);
1847 end;
1849 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1850 var
1851 Arr: SSArray;
1852 i: Integer;
1853 begin
1854 Result := True;
1856 Arr := nil;
1858 if Trim(C) = '' then
1859 Exit;
1861 Arr := ParseString(C);
1862 if Arr = nil then
1863 Exit;
1865 for i := 0 to High(Whitelist) do
1866 if Whitelist[i] = LowerCase(Arr[0]) then
1867 Result := False;
1868 end;
1870 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1871 var
1872 Arr: SSArray;
1873 i: Integer;
1874 begin
1875 Arr := nil;
1877 if Trim(L) = '' then
1878 Exit;
1880 conSkipLines := 0; // "unscroll"
1882 if L = 'goobers' then
1883 begin
1884 Line := '';
1885 CPos := 1;
1886 gCheats := true;
1887 g_Console_Add('Your memory serves you well.');
1888 exit;
1889 end;
1891 if not quiet then
1892 begin
1893 g_Console_Add('> '+L);
1894 Line := '';
1895 CPos := 1;
1896 end;
1898 Arr := ParseString(L);
1899 if Arr = nil then
1900 Exit;
1902 if commands = nil then
1903 Exit;
1905 if not quiet then
1906 AddToHistory(L);
1908 for i := 0 to High(commands) do
1909 begin
1910 if commands[i].cmd = LowerCase(Arr[0]) then
1911 begin
1912 if commands[i].action >= 0 then
1913 begin
1914 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1915 exit
1916 end;
1917 if assigned(commands[i].procEx) then
1918 begin
1919 commands[i].procEx(@commands[i], Arr);
1920 exit
1921 end;
1922 if assigned(commands[i].proc) then
1923 begin
1924 commands[i].proc(Arr);
1925 exit
1926 end
1927 end
1928 end;
1930 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1931 end;
1934 function g_Console_Interactive: Boolean;
1935 begin
1936 Result := gConsoleShow
1937 end;
1939 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = ''; rep: Boolean = False);
1940 begin
1941 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1942 ASSERT(key >= 0);
1943 ASSERT(key < e_MaxInputKeys);
1944 if key > 0 then
1945 begin
1946 gInputBinds[key].rep := rep;
1947 gInputBinds[key].down := ParseAlias(down);
1948 gInputBinds[key].up := ParseAlias(up);
1949 end;
1950 g_Console_WriteGameConfig();
1951 end;
1953 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1955 function EqualsCommandLists (a, b: SSArray): Boolean;
1956 var i, len: Integer;
1957 begin
1958 result := False;
1959 len := Length(a);
1960 if len = Length(b) then
1961 begin
1962 i := 0;
1963 while (i < len) and (a[i] = b[i]) do inc(i);
1964 if i >= len then
1965 result := True
1966 end
1967 end;
1969 begin
1970 ASSERT(key >= 0);
1971 ASSERT(key < e_MaxInputKeys);
1972 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1973 end;
1975 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1976 var i: Integer;
1977 begin
1978 ASSERT(n >= 1);
1979 result := 0;
1980 if commands = nil then Exit;
1981 i := 0;
1982 while (n >= 1) and (i < e_MaxInputKeys) do
1983 begin
1984 if (i < VK_FIRSTKEY) or (i > VK_LASTKEY) then (* never show virtual keys in gui *)
1985 begin
1986 if g_Console_MatchBind(i, down, up) then
1987 begin
1988 result := i;
1989 dec(n)
1990 end;
1991 end;
1992 inc(i)
1993 end;
1994 if n >= 1 then
1995 result := 0
1996 end;
1998 function g_Console_Action (action: Integer): Boolean;
1999 var i, len: Integer;
2000 begin
2001 ASSERT(action >= FIRST_ACTION);
2002 ASSERT(action <= LAST_ACTION);
2003 i := 0;
2004 len := Length(gPlayerAction);
2005 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
2006 Result := i < len
2007 end;
2009 function BindsAllowed (key: Integer): Boolean;
2010 begin
2011 Result := False;
2012 if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
2013 begin
2014 if gChatShow then
2015 Result := g_Console_MatchBind(key, 'togglemenu') or
2016 g_Console_MatchBind(key, 'showkeyboard') or
2017 g_Console_MatchBind(key, 'hidekeyboard')
2018 else if gConsoleShow or (g_ActiveWindow <> nil) or (gGameSettings.GameType = GT_NONE) then
2019 Result := g_Console_MatchBind(key, 'togglemenu') or
2020 g_Console_MatchBind(key, 'toggleconsole') or
2021 g_Console_MatchBind(key, 'showkeyboard') or
2022 g_Console_MatchBind(key, 'hidekeyboard')
2023 else (* in game *)
2024 Result := True
2025 end
2026 end;
2028 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
2029 var i: Integer;
2030 begin
2031 if BindsAllowed(key) then
2032 begin
2033 if down then
2034 for i := 0 to High(gInputBinds[key].down) do
2035 g_Console_Process(gInputBinds[key].down[i], True)
2036 else
2037 for i := 0 to High(gInputBinds[key].up) do
2038 g_Console_Process(gInputBinds[key].up[i], True)
2039 end;
2040 if down and not menu_toggled then
2041 KeyPress(key);
2042 menu_toggled := False
2043 end;
2045 procedure g_Console_ProcessBindRepeat (key: Integer);
2046 var i: Integer;
2047 begin
2048 if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
2049 begin
2050 KeyPress(key); // key repeat in menus and shit
2051 Exit;
2052 end;
2053 if BindsAllowed(key) and gInputBinds[key].rep then
2054 begin
2055 for i := 0 to High(gInputBinds[key].down) do
2056 g_Console_Process(gInputBinds[key].down[i], True);
2057 end;
2058 end;
2060 procedure g_Console_ResetBinds;
2061 var i: Integer;
2062 begin
2063 for i := 0 to e_MaxInputKeys - 1 do
2064 g_Console_BindKey(i, '', '');
2066 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
2067 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
2068 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
2069 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
2070 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
2071 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
2072 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
2073 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
2074 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
2075 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
2076 g_Console_BindKey(IK_E, 'p1_weapnext', '', True);
2077 g_Console_BindKey(IK_Q, 'p1_weapprev', '', True);
2078 g_Console_BindKey(IK_R, 'p1_dropflag', '');
2079 g_Console_BindKey(IK_1, 'p1_weapon 1');
2080 g_Console_BindKey(IK_2, 'p1_weapon 2');
2081 g_Console_BindKey(IK_3, 'p1_weapon 3');
2082 g_Console_BindKey(IK_4, 'p1_weapon 4');
2083 g_Console_BindKey(IK_5, 'p1_weapon 5');
2084 g_Console_BindKey(IK_6, 'p1_weapon 6');
2085 g_Console_BindKey(IK_7, 'p1_weapon 7');
2086 g_Console_BindKey(IK_8, 'p1_weapon 8');
2087 g_Console_BindKey(IK_9, 'p1_weapon 9');
2088 g_Console_BindKey(IK_0, 'p1_weapon 10');
2089 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
2090 g_Console_BindKey(IK_T, 'togglechat');
2091 g_Console_BindKey(IK_Y, 'toggleteamchat');
2092 g_Console_BindKey(IK_F11, 'screenshot');
2093 g_Console_BindKey(IK_TAB, '+scores', '-scores');
2094 g_Console_BindKey(IK_PAUSE, 'pause');
2095 g_Console_BindKey(IK_F1, 'vote');
2097 (* for i := 0 to e_MaxJoys - 1 do *)
2098 for i := 0 to 1 do
2099 begin
2100 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
2101 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
2102 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
2103 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
2104 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
2105 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
2106 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
2107 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
2108 g_Console_BindKey(e_JoyButtonToKey(i, 1), 'p' + IntToStr(i mod 2 + 1) + '_weapnext', '', True);
2109 g_Console_BindKey(e_JoyButtonToKey(i, 4), 'p' + IntToStr(i mod 2 + 1) + '_weapprev', '', True);
2110 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
2111 end;
2113 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
2114 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
2115 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
2116 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
2117 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
2118 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
2119 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
2120 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
2121 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
2122 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
2123 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
2124 g_Console_BindKey(VK_NEXT, 'weapnext', '', True);
2125 g_Console_BindKey(VK_PREV, 'weapprev', '', True);
2126 g_Console_BindKey(VK_0, 'weapon 1');
2127 g_Console_BindKey(VK_1, 'weapon 2');
2128 g_Console_BindKey(VK_2, 'weapon 3');
2129 g_Console_BindKey(VK_3, 'weapon 4');
2130 g_Console_BindKey(VK_4, 'weapon 5');
2131 g_Console_BindKey(VK_5, 'weapon 6');
2132 g_Console_BindKey(VK_6, 'weapon 7');
2133 g_Console_BindKey(VK_7, 'weapon 8');
2134 g_Console_BindKey(VK_8, 'weapon 9');
2135 g_Console_BindKey(VK_9, 'weapon 10');
2136 g_Console_BindKey(VK_A, 'weapon 11');
2137 g_Console_BindKey(VK_CHAT, 'togglechat');
2138 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
2139 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
2140 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
2141 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
2142 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
2143 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
2144 end;
2146 procedure g_Console_ReadConfig (filename: String);
2147 var f: TextFile; s: AnsiString; i, len: Integer;
2148 begin
2149 e_LogWritefln('g_Console_ReadConfig (1) "%s"', [filename]);
2150 if e_FindResource(ConfigDirs, filename, false) = true then
2151 begin
2152 e_LogWritefln('g_Console_ReadConfig (2) "%s"', [filename]);
2153 AssignFile(f, filename);
2154 Reset(f);
2155 while not EOF(f) do
2156 begin
2157 ReadLn(f, s);
2158 len := Length(s);
2159 if len > 0 then
2160 begin
2161 i := 1;
2162 (* skip spaces *)
2163 while (i <= len) and (s[i] <= ' ') do inc(i);
2164 (* skip comments *)
2165 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
2166 g_Console_Process(s, True);
2167 end
2168 end;
2169 CloseFile(f);
2170 end
2171 end;
2173 procedure g_Console_WriteConfig (filename: String);
2174 var f: TextFile; i, j: Integer;
2176 procedure WriteFlag(name: string; flag: LongWord);
2177 begin
2178 WriteLn(f, name, IfThen(LongBool(gsGameFlags and flag), 1, 0));
2179 end;
2181 function FormatTeam(team: Byte): string;
2182 begin
2183 if team = TEAM_BLUE then
2184 result := 'blue'
2185 else
2186 result := 'red';
2187 end;
2189 begin
2190 AssignFile(f, filename);
2191 Rewrite(f);
2192 WriteLn(f, '// ' + configComment);
2194 // binds
2195 WriteLn(f, 'unbindall');
2196 for i := 0 to e_MaxInputKeys - 1 do
2197 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
2198 begin
2199 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
2200 if Length(gInputBinds[i].down) = 0 then
2201 Write(f, '""');
2202 if Length(gInputBinds[i].up) > 0 then
2203 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
2204 WriteLn(f, '');
2205 if gInputBinds[i].rep then
2206 WriteLn(f, 'bindrep ', e_KeyNames[i]);
2207 end;
2209 // lang
2210 if gAskLanguage then
2211 WriteLn(f, 'g_language ask')
2212 else
2213 WriteLn(f, 'g_language ', gLanguage);
2215 // net server
2216 WriteLn(f, 'sv_name ', QuoteStr(NetServerName));
2217 WriteLn(f, 'sv_passwd ', QuoteStr(NetPassword));
2218 WriteLn(f, 'sv_maxplrs ', NetMaxClients);
2219 WriteLn(f, 'sv_port ', NetPort);
2220 WriteLn(f, 'sv_public ', IfThen(NetUseMaster, 1, 0));
2222 // game settings
2223 WriteLn(f, 'g_max_particles ', g_GFX_GetMax());
2224 WriteLn(f, 'g_max_shells ', g_Shells_GetMax());
2225 WriteLn(f, 'g_max_gibs ', g_Gibs_GetMax());
2226 WriteLn(f, 'g_max_corpses ', g_Corpses_GetMax());
2227 WriteLn(f, 'g_force_model ', g_Force_Model_Get());
2228 WriteLn(f, 'g_force_model_name ', g_Forced_Model_GetName());
2229 WriteLn(f, 'sv_intertime ', gDefInterTime);
2231 // gameplay settings
2232 WriteLn(f, 'g_gamemode ', gsGameMode);
2233 WriteLn(f, 'g_scorelimit ', gsScoreLimit);
2234 WriteLn(f, 'g_timelimit ', gsTimeLimit);
2235 WriteLn(f, 'g_maxlives ', gsMaxLives);
2236 WriteLn(f, 'g_item_respawn_time ', gsItemRespawnTime);
2237 WriteLn(f, 'g_spawn_invul ', gsSpawnInvul);
2238 WriteLn(f, 'g_warmup_time ', gsWarmupTime);
2240 WriteFlag('g_friendlyfire ', GAME_OPTION_TEAMDAMAGE);
2241 WriteFlag('g_friendly_hit_trace ', GAME_OPTION_TEAMHITTRACE);
2242 WriteFlag('g_friendly_hit_projectile ', GAME_OPTION_TEAMHITPROJECTILE);
2243 WriteFlag('g_allow_exit ', GAME_OPTION_ALLOWEXIT);
2244 WriteFlag('g_allow_monsters ', GAME_OPTION_MONSTERS);
2245 WriteFlag('g_allow_dropflag ', GAME_OPTION_ALLOWDROPFLAG);
2246 WriteFlag('g_throw_flag ', GAME_OPTION_THROWFLAG);
2247 WriteFlag('g_dm_keys ', GAME_OPTION_DMKEYS);
2248 WriteFlag('g_weaponstay ', GAME_OPTION_WEAPONSTAY);
2249 WriteFlag('g_bot_vsmonsters ', GAME_OPTION_BOTVSMONSTER);
2250 WriteFlag('g_bot_vsplayers ', GAME_OPTION_BOTVSPLAYER);
2252 // players
2253 with gPlayer1Settings do
2254 begin
2255 WriteLn(f, 'p1_name ', QuoteStr(Name));
2256 WriteLn(f, 'p1_color ', Color.R, ' ', Color.G, ' ', Color.B);
2257 WriteLn(f, 'p1_model ', QuoteStr(Model));
2258 WriteLn(f, 'p1_team ', FormatTeam(Team));
2259 WriteLn(f, 'p1_autoswitch ', WeaponSwitch);
2260 WriteLn(f, 'p1_switch_empty ', SwitchToEmpty);
2261 WriteLn(f, 'p1_priority_kastet ', Max(0, WeaponPreferences[WEAPON_KASTET]));
2262 WriteLn(f, 'p1_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2263 WriteLn(f, 'p1_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2264 WriteLn(f, 'p1_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2265 WriteLn(f, 'p1_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN2] ));
2266 WriteLn(f, 'p1_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2267 WriteLn(f, 'p1_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2268 WriteLn(f, 'p1_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2269 WriteLn(f, 'p1_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2270 WriteLn(f, 'p1_priority_super ', Max(0, WeaponPreferences[WEAPON_SUPERPULEMET]));
2271 WriteLn(f, 'p1_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2272 WriteLn(f, 'p1_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2273 //
2274 end;
2275 with gPlayer2Settings do
2276 begin
2277 WriteLn(f, 'p2_name ', QuoteStr(Name));
2278 WriteLn(f, 'p2_color ', Color.R, ' ', Color.G, ' ', Color.B);
2279 WriteLn(f, 'p2_model ', QuoteStr(Model));
2280 WriteLn(f, 'p2_team ', FormatTeam(Team));
2281 WriteLn(f, 'p2_autoswitch ', WeaponSwitch);
2282 WriteLn(f, 'p2_switch_empty ', SwitchToEmpty);
2283 WriteLn(f, 'p2_priority_kastet ', Max(0, WeaponPreferences[WEAPON_KASTET]));
2284 WriteLn(f, 'p2_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2285 WriteLn(f, 'p2_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2286 WriteLn(f, 'p2_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2287 WriteLn(f, 'p2_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2288 WriteLn(f, 'p2_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2289 WriteLn(f, 'p2_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2290 WriteLn(f, 'p2_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2291 WriteLn(f, 'p2_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2292 WriteLn(f, 'p2_priority_super ', Max(0, WeaponPreferences[WEAPON_SUPERPULEMET]));
2293 WriteLn(f, 'p2_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2294 WriteLn(f, 'p2_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2295 end;
2297 // all cvars
2298 for i := 0 to High(commands) do
2299 begin
2300 if not commands[i].cheat then
2301 begin
2302 if @commands[i].procEx = @boolVarHandler then
2303 begin
2304 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
2305 WriteLn(f, commands[i].cmd, ' ', j)
2306 end
2307 else if @commands[i].procEx = @intVarHandler then
2308 begin
2309 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
2310 end
2311 else if @commands[i].procEx = @wordVarHandler then
2312 begin
2313 WriteLn(f, commands[i].cmd, ' ', PWord(commands[i].ptr)^)
2314 end
2315 else if @commands[i].procEx = @dwordVarHandler then
2316 begin
2317 WriteLn(f, commands[i].cmd, ' ', PCardinal(commands[i].ptr)^)
2318 end
2319 else if @commands[i].procEx = @singleVarHandler then
2320 begin
2321 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
2322 end
2323 else if @commands[i].procEx = @strVarHandler then
2324 begin
2325 if Length(PAnsiString(commands[i].ptr)^) = 0 then
2326 WriteLn(f, commands[i].cmd, ' ""')
2327 else
2328 WriteLn(f, commands[i].cmd, ' ', QuoteStr(PAnsiString(commands[i].ptr)^))
2329 end
2330 end
2331 end;
2333 WriteLn(f, 'r_maxfps ', gMaxFPS);
2334 WriteLn(f, 'r_reset');
2335 CloseFile(f)
2336 end;
2338 procedure g_Console_WriteGameConfig;
2339 var s: AnsiString;
2340 begin
2341 if gParsingBinds = false then
2342 begin
2343 s := e_GetWriteableDir(ConfigDirs);
2344 g_Console_WriteConfig(e_CatPath(s, gConfigScript))
2345 end
2346 end;
2348 procedure Init;
2349 var i: Integer;
2350 begin
2351 conRegVar('chat_at_top', @ChatTop, 'draw chat at top border', 'draw chat at top border');
2352 conRegVar('console_height', @ConsoleHeight, 0.0, 1.0, 'set console size', 'set console size');
2353 conRegVar('console_trans', @ConsoleTrans, 0.0, 1.0, 'set console transparency', 'set console transparency');
2354 conRegVar('console_step', @ConsoleStep, 0.0, 1.0, 'set console animation speed', 'set console animation speed');
2355 conRegVar('console_stdin', @ConsoleStdIn, 'enable reading commands from stdin', 'enable reading commands from stdin');
2356 {$IFDEF ANDROID}
2357 ChatTop := True;
2358 ConsoleHeight := 0.35;
2359 {$ELSE}
2360 ChatTop := False;
2361 ConsoleHeight := 0.5;
2362 {$ENDIF}
2363 ConsoleTrans := 0.1;
2364 ConsoleStep := 0.07;
2365 {$IFDEF HEADLESS}
2366 ConsoleStdIn := True;
2367 {$ELSE}
2368 ConsoleStdIn := False;
2369 {$ENDIF}
2370 conRegVar('d_eres', @debug_e_res, '', '');
2371 for i := 1 to e_MaxJoys do
2372 conRegVar('joy' + IntToStr(i) + '_deadzone', @e_JoystickDeadzones[i - 1], '', '')
2373 end;
2375 initialization
2376 Init
2378 {$IFDEF HEADLESS}
2379 finalization
2380 DoneKeyboard;
2381 conbufStdOutRawMode := false;
2382 {$ENDIF}
2384 end.