Fix some memory leaks (18 blocks) caused by initialization routines
[d2df-sdl.git] / src / game / g_console.pas
blob0ba93431e2f9e05c3ce6bf35b1e105d96d4aab40
1 (* Copyright (C) Doom 2D: Forever Developers
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.
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.
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/>.
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';
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;
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], '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);
252 old: Boolean;
253 begin
254 if (Length(p) > 2) then
255 begin
256 conwritefln('too many arguments to ''%s''', [p[0]]);
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 true', [msg]) else conwritefln('%s false', [msg]);
271 end;
272 end;
273 begin
274 binaryFlag(PBoolean(me.ptr)^, me.msg);
275 end;
278 procedure intVarHandler (me: PCommand; p: SSArray);
280 old: Integer;
281 begin
282 if (Length(p) <> 2) then
283 begin
284 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
286 else
287 begin
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);
302 old: Integer;
303 begin
304 if (Length(p) <> 2) then
305 begin
306 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
308 else
309 begin
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);
324 old: Integer;
325 begin
326 if (Length(p) <> 2) then
327 begin
328 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
330 else
331 begin
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);
346 old: AnsiString;
347 begin
348 if (Length(p) <> 2) then
349 begin
350 conwritefln('%s %s', [me.cmd, QuoteStr(PAnsiString(me.ptr)^)]);
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;
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;
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;
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;
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;
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);
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) then
493 begin
494 pv.val^ := pv.def;
496 else
497 begin
498 if not conParseFloat(nv, p[1]) then
499 begin
500 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
501 exit;
502 end;
503 if (nv < pv.min) then nv := pv.min;
504 if (nv > pv.max) then nv := pv.max;
505 pv.val^ := nv;
506 end;
507 end;
508 if pv.val^ <> old then
509 g_Console_WriteGameConfig();
510 msg := me.msg;
511 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
512 conwritefln('%s %s', [msg, pv.val^]);
513 end;
516 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
518 f: Integer;
519 cp: PCommand;
520 pv: PVarSingle;
521 begin
522 GetMem(pv, sizeof(TVarSingle));
523 pv.val := pvar;
524 pv.min := amin;
525 pv.max := amax;
526 pv.def := pvar^;
527 f := Length(commands);
528 SetLength(commands, f+1);
529 cp := @commands[f];
530 cp.cmd := LowerCase(conname);
531 cp.proc := nil;
532 cp.procEx := singleVarHandler;
533 cp.help := ahelp;
534 cp.hidden := ahidden;
535 cp.ptr := pv;
536 cp.msg := amsg;
537 cp.cheat := acheat;
538 cp.action := -1;
539 cp.player := -1;
540 end;
543 // ////////////////////////////////////////////////////////////////////////// //
544 function GetStrACmd(var Str: AnsiString): AnsiString;
546 a: Integer;
547 begin
548 Result := '';
549 for a := 1 to Length(Str) do
550 if (a = Length(Str)) or (Str[a+1] = ';') then
551 begin
552 Result := Copy(Str, 1, a);
553 Delete(Str, 1, a+1);
554 Str := Trim(Str);
555 Exit;
556 end;
557 end;
559 function ParseAlias(Str: AnsiString): SSArray;
560 begin
561 Result := nil;
563 Str := Trim(Str);
565 if Str = '' then
566 Exit;
568 while Str <> '' do
569 begin
570 SetLength(Result, Length(Result)+1);
571 Result[High(Result)] := GetStrACmd(Str);
572 end;
573 end;
575 procedure ConsoleCommands(p: SSArray);
577 cmd, s: AnsiString;
578 a, b: Integer;
579 (* F: TextFile; *)
580 begin
581 cmd := LowerCase(p[0]);
582 s := '';
584 if cmd = 'clear' then
585 begin
586 //ConsoleHistory := nil;
587 cbufClear();
588 conSkipLines := 0;
590 for a := 0 to High(MsgArray) do
591 with MsgArray[a] do
592 begin
593 Msg := '';
594 Time := 0;
595 end;
596 end;
598 if cmd = 'clearhistory' then
599 CommandHistory := nil;
601 if cmd = 'showhistory' then
602 if CommandHistory <> nil then
603 begin
604 g_Console_Add('');
605 for a := 0 to High(CommandHistory) do
606 g_Console_Add(' '+CommandHistory[a]);
607 end;
609 if cmd = 'commands' then
610 begin
611 g_Console_Add('');
612 g_Console_Add('commands list:');
613 for a := High(commands) downto 0 do
614 begin
615 if (Length(commands[a].help) > 0) then
616 begin
617 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
619 else
620 begin
621 g_Console_Add(' '+commands[a].cmd);
622 end;
623 end;
624 end;
626 if cmd = 'time' then
627 g_Console_Add(TimeToStr(Now), True);
629 if cmd = 'date' then
630 g_Console_Add(DateToStr(Now), True);
632 if cmd = 'echo' then
633 if Length(p) > 1 then
634 begin
635 if p[1] = 'ololo' then
636 gCheats := True
637 else
638 begin
639 s := '';
640 for a := 1 to High(p) do
641 s := s + p[a] + ' ';
642 g_Console_Add(b_Text_Format(s), True);
643 end;
645 else
646 g_Console_Add('');
648 if cmd = 'dump' then
649 begin
651 if ConsoleHistory <> nil then
652 begin
653 if Length(P) > 1 then
654 s := P[1]
655 else
656 s := GameDir+'/console.txt';
658 {$I-}
659 AssignFile(F, s);
660 Rewrite(F);
661 if IOResult <> 0 then
662 begin
663 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
664 CloseFile(F);
665 Exit;
666 end;
668 for a := 0 to High(ConsoleHistory) do
669 WriteLn(F, ConsoleHistory[a]);
671 CloseFile(F);
672 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
673 {$I+}
674 end;
676 end;
678 if cmd = 'exec' then
679 begin
680 // exec <filename>
681 if Length(p) = 2 then
682 g_Console_ReadConfig(p[1])
683 else
684 g_Console_Add('exec <script file>');
685 end;
687 if cmd = 'writeconfig' then
688 begin
689 // writeconfig <filename>
690 if Length(p) = 2 then
691 begin
692 s := e_GetWriteableDir(ConfigDirs);
693 g_Console_WriteConfig(e_CatPath(s, p[1]))
695 else
696 begin
697 g_Console_Add('writeconfig <file>')
699 end;
701 if (cmd = 'ver') or (cmd = 'version') then
702 begin
703 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
704 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
705 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
706 end;
708 if cmd = 'alias' then
709 begin
710 // alias [alias_name] [commands]
711 if Length(p) > 1 then
712 begin
713 for a := 0 to High(Aliases) do
714 if Aliases[a].name = p[1] then
715 begin
716 if Length(p) > 2 then
717 Aliases[a].commands := ParseAlias(p[2])
718 else
719 for b := 0 to High(Aliases[a].commands) do
720 g_Console_Add(Aliases[a].commands[b]);
721 Exit;
722 end;
723 SetLength(Aliases, Length(Aliases)+1);
724 a := High(Aliases);
725 Aliases[a].name := p[1];
726 if Length(p) > 2 then
727 Aliases[a].commands := ParseAlias(p[2])
728 else
729 for b := 0 to High(Aliases[a].commands) do
730 g_Console_Add(Aliases[a].commands[b]);
731 end else
732 for a := 0 to High(Aliases) do
733 if Aliases[a].commands <> nil then
734 g_Console_Add(Aliases[a].name);
735 end;
737 if cmd = 'call' then
738 begin
739 // call <alias_name>
740 if Length(p) > 1 then
741 begin
742 if Aliases = nil then
743 Exit;
744 for a := 0 to High(Aliases) do
745 if Aliases[a].name = p[1] then
746 begin
747 if Aliases[a].commands <> nil then
748 begin
749 // with this system proper endless loop detection seems either impossible
750 // or very dirty to implement, so let's have this instead
751 // prevents endless loops
752 for b := 0 to High(Aliases[a].commands) do
753 begin
754 Inc(RecursionDepth);
755 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
756 if not RecursionLimitHit then
757 g_Console_Process(Aliases[a].commands[b], True);
758 Dec(RecursionDepth);
759 end;
760 if (RecursionDepth = 0) and RecursionLimitHit then
761 begin
762 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
763 RecursionLimitHit := False;
764 end;
765 end;
766 Exit;
767 end;
769 else
770 g_Console_Add('call <alias name>');
771 end;
772 end;
774 procedure WhitelistCommand(cmd: AnsiString);
776 a: Integer;
777 begin
778 SetLength(Whitelist, Length(Whitelist)+1);
779 a := High(Whitelist);
780 Whitelist[a] := LowerCase(cmd);
781 end;
783 procedure segfault (p: SSArray);
785 pp: PByte = nil;
786 begin
787 pp^ := 0;
788 end;
790 function GetCommandString (p: SSArray): AnsiString;
791 var i: Integer;
792 begin
793 result := '';
794 if Length(p) >= 1 then
795 begin
796 result := p[0];
797 for i := 1 to High(p) do
798 result := result + '; ' + p[i]
800 end;
802 function QuoteStr(str: String): String;
803 begin
804 if Pos(' ', str) > 0 then
805 Result := '"' + str + '"'
806 else
807 Result := str;
808 end;
810 procedure BindCommands (p: SSArray);
811 var cmd, key: AnsiString; i: Integer;
812 begin
813 cmd := LowerCase(p[0]);
814 case cmd of
815 'bind':
816 // bind <key> [down [up]]
817 if (Length(p) >= 2) and (Length(p) <= 4) then
818 begin
819 i := 0;
820 key := LowerCase(p[1]);
821 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
822 if i < e_MaxInputKeys then
823 begin
824 if Length(p) = 2 then
825 g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
826 else if Length(p) = 3 then
827 g_Console_BindKey(i, p[2], '')
828 else (* len = 4 *)
829 g_Console_BindKey(i, p[2], p[3])
831 else
832 g_Console_Add('bind: "' + p[1] + '" is not a key')
834 else
835 begin
836 g_Console_Add('bind <key> <down action> [up action]')
837 end;
838 'bindrep':
839 // bindrep <key>
840 if Length(p) = 2 then
841 begin
842 key := LowerCase(p[1]);
843 i := 0;
844 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
845 if i < e_MaxInputKeys then
846 gInputBinds[i].rep := True
847 else
848 g_Console_Add('bindrep: "' + p[1] + '" is not a key')
850 else
851 g_Console_Add('bindrep <key>');
852 'bindunrep':
853 // bindunrep <key>
854 if Length(p) = 2 then
855 begin
856 key := LowerCase(p[1]);
857 i := 0;
858 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
859 if i < e_MaxInputKeys then
860 gInputBinds[i].rep := False
861 else
862 g_Console_Add('bindunrep: "' + p[1] + '" is not a key')
864 else
865 g_Console_Add('bindunrep <key>');
866 'bindlist':
867 for i := 0 to e_MaxInputKeys - 1 do
868 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
869 g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
870 'unbind':
871 // unbind <key>
872 if Length(p) = 2 then
873 begin
874 key := LowerCase(p[1]);
875 i := 0;
876 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
877 if i < e_MaxInputKeys then
878 g_Console_BindKey(i, '')
879 else
880 g_Console_Add('unbind: "' + p[1] + '" is not a key')
882 else
883 g_Console_Add('unbind <key>');
884 'unbindall':
885 for i := 0 to e_MaxInputKeys - 1 do
886 g_Console_BindKey(i, '');
887 'showkeyboard':
888 g_Touch_ShowKeyboard(True);
889 'hidekeyboard':
890 g_Touch_ShowKeyboard(False);
891 'togglemenu':
892 begin
893 if gConsoleShow then
894 g_Console_Switch
895 else if gChatShow then
896 g_Console_Chat_Switch
897 else
898 KeyPress(VK_ESCAPE);
899 menu_toggled := True
900 end;
901 'toggleconsole':
902 g_Console_Switch;
903 'togglechat':
904 g_Console_Chat_Switch;
905 'toggleteamchat':
906 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
907 g_Console_Chat_Switch(True);
909 end;
911 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
913 a: Integer;
914 cp: PCommand;
915 begin
916 SetLength(commands, Length(commands)+1);
917 a := High(commands);
918 cp := @commands[a];
919 cp.cmd := LowerCase(cmd);
920 cp.proc := proc;
921 cp.procEx := nil;
922 cp.help := ahelp;
923 cp.hidden := ahidden;
924 cp.ptr := nil;
925 cp.msg := '';
926 cp.cheat := acheat;
927 cp.action := -1;
928 cp.player := -1;
929 end;
931 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
932 const
933 PrefixList: array [0..1] of AnsiString = ('+', '-');
934 PlayerList: array [0..1] of Integer = (1, 2);
936 s: AnsiString;
937 i: Integer;
939 procedure NewAction (cmd: AnsiString; player: Integer);
940 var cp: PCommand;
941 begin
942 SetLength(commands, Length(commands) + 1);
943 cp := @commands[High(commands)];
944 cp.cmd := LowerCase(cmd);
945 cp.proc := nil;
946 cp.procEx := nil;
947 cp.help := help;
948 cp.hidden := hidden;
949 cp.ptr := nil;
950 cp.msg := '';
951 cp.cheat := cheat;
952 cp.action := action;
953 cp.player := player;
954 end;
956 begin
957 ASSERT(action >= FIRST_ACTION);
958 ASSERT(action <= LAST_ACTION);
959 for s in PrefixList do
960 begin
961 NewAction(s + cmd, 0);
962 for i in PlayerList do
963 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
965 end;
967 procedure ReadStdIn;
969 K: Char;
970 KEv: TKeyEvent;
971 begin
972 gConsoleShow := True;
973 InputReady := True;
974 // one key per frame
975 KEv := PollKeyEvent();
976 if KEv <> 0 then
977 begin
978 K := GetKeyEventChar(TranslateKeyEvent(GetKeyEvent()));
979 Write(K);
980 case K of
981 #8: g_Console_Control(IK_BACKSPACE);
982 #10, #13: g_Console_Control(IK_RETURN);
983 #32..#126: g_Console_Char(K);
984 // arrow keys and DEL all return 0 for some reason, so fuck em
985 end;
986 end;
987 end;
989 procedure g_Console_SysInit;
990 var a: Integer;
991 begin
992 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
993 gConsoleShow := False;
994 gChatShow := False;
995 Cons_Shown := False;
996 InputReady := False;
997 CPos := 1;
999 for a := 0 to High(MsgArray) do
1000 with MsgArray[a] do
1001 begin
1002 Msg := '';
1003 Time := 0;
1004 end;
1006 AddCommand('segfault', segfault, 'make segfault');
1008 AddCommand('quit', SystemCommands);
1009 AddCommand('exit', SystemCommands);
1010 AddCommand('r_reset', SystemCommands);
1011 AddCommand('r_maxfps', SystemCommands);
1012 AddCommand('g_language', SystemCommands);
1014 AddCommand('bind', BindCommands);
1015 AddCommand('bindrep', BindCommands);
1016 AddCommand('bindunrep', BindCommands);
1017 AddCommand('bindlist', BindCommands);
1018 AddCommand('unbind', BindCommands);
1019 AddCommand('unbindall', BindCommands);
1020 AddCommand('showkeyboard', BindCommands);
1021 AddCommand('hidekeyboard', BindCommands);
1022 AddCommand('togglemenu', BindCommands);
1023 AddCommand('toggleconsole', BindCommands);
1024 AddCommand('togglechat', BindCommands);
1025 AddCommand('toggleteamchat', BindCommands);
1027 AddCommand('clear', ConsoleCommands, 'clear console');
1028 AddCommand('clearhistory', ConsoleCommands);
1029 AddCommand('showhistory', ConsoleCommands);
1030 AddCommand('commands', ConsoleCommands);
1031 AddCommand('time', ConsoleCommands);
1032 AddCommand('date', ConsoleCommands);
1033 AddCommand('echo', ConsoleCommands);
1034 AddCommand('dump', ConsoleCommands);
1035 AddCommand('exec', ConsoleCommands);
1036 AddCommand('writeconfig', ConsoleCommands);
1037 AddCommand('alias', ConsoleCommands);
1038 AddCommand('call', ConsoleCommands);
1039 AddCommand('ver', ConsoleCommands);
1040 AddCommand('version', ConsoleCommands);
1042 AddCommand('d_window', DebugCommands);
1043 AddCommand('d_sounds', DebugCommands);
1044 AddCommand('d_frames', DebugCommands);
1045 AddCommand('d_winmsg', DebugCommands);
1046 AddCommand('d_monoff', DebugCommands);
1047 AddCommand('d_botoff', DebugCommands);
1048 AddCommand('d_monster', DebugCommands);
1049 AddCommand('d_health', DebugCommands);
1050 AddCommand('d_player', DebugCommands);
1051 AddCommand('d_joy', DebugCommands);
1052 AddCommand('d_mem', DebugCommands);
1054 AddCommand('p1_name', PlayerSettingsCVars);
1055 AddCommand('p2_name', PlayerSettingsCVars);
1056 AddCommand('p1_color', PlayerSettingsCVars);
1057 AddCommand('p2_color', PlayerSettingsCVars);
1058 AddCommand('p1_model', PlayerSettingsCVars);
1059 AddCommand('p2_model', PlayerSettingsCVars);
1060 AddCommand('p1_team', PlayerSettingsCVars);
1061 AddCommand('p2_team', PlayerSettingsCVars);
1062 AddCommand('p1_autoswitch', PlayerSettingsCVars);
1063 AddCommand('p2_autoswitch', PlayerSettingsCVars);
1064 AddCommand('p1_switch_empty', PlayerSettingsCVars);
1065 AddCommand('p2_switch_empty', PlayerSettingsCVars);
1066 AddCommand('p1_skip_ironfist', PlayerSettingsCVars);
1067 AddCommand('p2_skip_ironfist', PlayerSettingsCVars);
1068 AddCommand('p1_priority_ironfist', PlayerSettingsCVars);
1069 AddCommand('p2_priority_ironfist', PlayerSettingsCVars);
1070 AddCommand('p1_priority_saw', PlayerSettingsCVars);
1071 AddCommand('p2_priority_saw', PlayerSettingsCVars);
1072 AddCommand('p1_priority_pistol', PlayerSettingsCVars);
1073 AddCommand('p2_priority_pistol', PlayerSettingsCVars);
1074 AddCommand('p1_priority_shotgun1', PlayerSettingsCVars);
1075 AddCommand('p2_priority_shotgun1', PlayerSettingsCVars);
1076 AddCommand('p1_priority_shotgun2', PlayerSettingsCVars);
1077 AddCommand('p2_priority_shotgun2', PlayerSettingsCVars);
1078 AddCommand('p1_priority_chaingun', PlayerSettingsCVars);
1079 AddCommand('p2_priority_chaingun', PlayerSettingsCVars);
1080 AddCommand('p1_priority_rocketlauncher', PlayerSettingsCVars);
1081 AddCommand('p2_priority_rocketlauncher', PlayerSettingsCVars);
1082 AddCommand('p1_priority_plasma', PlayerSettingsCVars);
1083 AddCommand('p2_priority_plasma', PlayerSettingsCVars);
1084 AddCommand('p1_priority_bfg', PlayerSettingsCVars);
1085 AddCommand('p2_priority_bfg', PlayerSettingsCVars);
1086 AddCommand('p1_priority_superchaingun', PlayerSettingsCVars);
1087 AddCommand('p2_priority_superchaingun', PlayerSettingsCVars);
1088 AddCommand('p1_priority_flamethrower', PlayerSettingsCVars);
1089 AddCommand('p2_priority_flamethrower', PlayerSettingsCVars);
1090 AddCommand('p1_priority_berserk', PlayerSettingsCVars);
1091 AddCommand('p2_priority_berserk', PlayerSettingsCVars);
1093 AddCommand('g_max_particles', GameCVars);
1094 AddCommand('g_max_shells', GameCVars);
1095 AddCommand('g_max_gibs', GameCVars);
1096 AddCommand('g_max_corpses', GameCVars);
1097 AddCommand('g_force_model', GameCVars);
1098 AddCommand('g_force_model_name', GameCVars);
1099 AddCommand('g_gamemode', GameCVars);
1100 AddCommand('g_friendlyfire', GameCVars);
1101 AddCommand('g_friendly_hit_trace', GameCVars);
1102 AddCommand('g_friendly_hit_projectile', GameCVars);
1103 AddCommand('g_friendly_absorb_damage', GameCVars);
1104 AddCommand('g_weaponstay', GameCVars);
1105 AddCommand('g_allow_exit', GameCVars);
1106 AddCommand('g_dm_keys', GameCVars);
1107 AddCommand('g_allow_monsters', GameCVars);
1108 AddCommand('g_allow_dropflag', GameCVars);
1109 AddCommand('g_throw_flag', GameCVars);
1110 AddCommand('g_bot_vsmonsters', GameCVars);
1111 AddCommand('g_bot_vsplayers', GameCVars);
1112 AddCommand('g_max_bots', GameCVars); // intentionally not whitelisted
1113 AddCommand('g_scorelimit', GameCVars);
1114 AddCommand('g_timelimit', GameCVars);
1115 AddCommand('g_maxlives', GameCVars);
1116 AddCommand('g_warmup_time', GameCVars);
1117 AddCommand('g_spawn_invul', GameCVars);
1118 AddCommand('g_item_respawn_time', GameCVars);
1119 AddCommand('g_item_time_random', GameCVars);
1120 AddCommand('g_items_all_respawn_random', GameCVars);
1121 AddCommand('g_items_help_respawn_random', GameCVars);
1122 AddCommand('g_items_ammo_respawn_random', GameCVars);
1123 AddCommand('g_items_weapon_respawn_random', GameCVars);
1124 AddCommand('g_powerup_randomize_respawn', GameCVars);
1125 AddCommand('g_powerup_respawn_time', GameCVars);
1126 AddCommand('g_powerup_time_random', GameCVars);
1127 AddCommand('sv_intertime', GameCVars);
1129 AddCommand('sv_name', NetServerCVars);
1130 AddCommand('sv_passwd', NetServerCVars);
1131 AddCommand('sv_maxplrs', NetServerCVars);
1132 AddCommand('sv_public', NetServerCVars);
1133 AddCommand('sv_port', NetServerCVars);
1135 AddCommand('pause', GameCommands);
1136 AddCommand('endgame', GameCommands);
1137 AddCommand('restart', GameCommands);
1138 AddCommand('addbot', GameCommands);
1139 AddCommand('bot_add', GameCommands);
1140 AddCommand('bot_addlist', GameCommands);
1141 AddCommand('bot_addred', GameCommands);
1142 AddCommand('bot_addblue', GameCommands);
1143 AddCommand('bot_removeall', GameCommands);
1144 AddCommand('chat', GameCommands);
1145 AddCommand('teamchat', GameCommands);
1146 AddCommand('announce', GameCommands);
1147 AddCommand('an', GameCommands);
1148 AddCommand('game', GameCommands);
1149 AddCommand('host', GameCommands);
1150 AddCommand('map', GameCommands);
1151 AddCommand('nextmap', GameCommands);
1152 AddCommand('endmap', GameCommands);
1153 AddCommand('goodbye', GameCommands);
1154 AddCommand('suicide', GameCommands);
1155 AddCommand('spectate', GameCommands);
1156 AddCommand('ready', GameCommands);
1157 AddCommand('kick', GameCommands);
1158 AddCommand('kick_id', GameCommands);
1159 AddCommand('kick_pid', GameCommands);
1160 AddCommand('ban', GameCommands);
1161 AddCommand('ban_id', GameCommands);
1162 AddCommand('ban_pid', GameCommands);
1163 AddCommand('permban', GameCommands);
1164 AddCommand('permban_id', GameCommands);
1165 AddCommand('permban_pid', GameCommands);
1166 AddCommand('permban_ip', GameCommands);
1167 AddCommand('unban', GameCommands);
1168 AddCommand('connect', GameCommands);
1169 AddCommand('disconnect', GameCommands);
1170 AddCommand('reconnect', GameCommands);
1171 AddCommand('say', GameCommands);
1172 AddCommand('tell', GameCommands);
1173 AddCommand('centerprint', GameCommands);
1174 AddCommand('overtime', GameCommands);
1175 AddCommand('rcon_password', GameCommands);
1176 AddCommand('rcon', GameCommands);
1177 AddCommand('callvote', GameCommands);
1178 AddCommand('vote', GameCommands);
1179 AddCommand('clientlist', GameCommands);
1180 AddCommand('event', GameCommands);
1181 AddCommand('screenshot', GameCommands);
1182 AddCommand('weapnext', GameCommands);
1183 AddCommand('weapprev', GameCommands);
1184 AddCommand('weapon', GameCommands);
1185 AddCommand('dropflag', GameCommands);
1186 AddCommand('p1_weapnext', GameCommands);
1187 AddCommand('p1_weapprev', GameCommands);
1188 AddCommand('p1_weapon', GameCommands);
1189 AddCommand('p1_weapbest', GameCommands);
1190 AddCommand('p1_dropflag', GameCommands);
1191 AddCommand('p2_weapnext', GameCommands);
1192 AddCommand('p2_weapprev', GameCommands);
1193 AddCommand('p2_weapon', GameCommands);
1194 AddCommand('p2_weapbest', GameCommands);
1195 AddCommand('p2_dropflag', GameCommands);
1197 AddCommand('god', GameCheats);
1198 AddCommand('notarget', GameCheats);
1199 AddCommand('give', GameCheats); // "exit" too ;-)
1200 AddCommand('open', GameCheats);
1201 AddCommand('fly', GameCheats);
1202 AddCommand('noclip', GameCheats);
1203 AddCommand('speedy', GameCheats);
1204 AddCommand('jumpy', GameCheats);
1205 AddCommand('noreload', GameCheats);
1206 AddCommand('aimline', GameCheats);
1207 AddCommand('automap', GameCheats);
1209 AddAction('jump', ACTION_JUMP);
1210 AddAction('moveleft', ACTION_MOVELEFT);
1211 AddAction('moveright', ACTION_MOVERIGHT);
1212 AddAction('lookup', ACTION_LOOKUP);
1213 AddAction('lookdown', ACTION_LOOKDOWN);
1214 AddAction('attack', ACTION_ATTACK);
1215 AddAction('scores', ACTION_SCORES);
1216 AddAction('activate', ACTION_ACTIVATE);
1217 AddAction('strafe', ACTION_STRAFE);
1219 WhitelistCommand('say');
1220 WhitelistCommand('tell');
1221 WhitelistCommand('overtime');
1222 WhitelistCommand('ready');
1223 WhitelistCommand('map');
1224 WhitelistCommand('nextmap');
1225 WhitelistCommand('endmap');
1226 WhitelistCommand('restart');
1227 WhitelistCommand('kick');
1228 WhitelistCommand('kick_pid');
1229 WhitelistCommand('ban');
1230 WhitelistCommand('ban_pid');
1231 WhitelistCommand('centerprint');
1233 WhitelistCommand('addbot');
1234 WhitelistCommand('bot_add');
1235 WhitelistCommand('bot_addred');
1236 WhitelistCommand('bot_addblue');
1237 WhitelistCommand('bot_removeall');
1239 WhitelistCommand('g_gamemode');
1240 WhitelistCommand('g_friendlyfire');
1241 WhitelistCommand('g_friendly_hit_trace');
1242 WhitelistCommand('g_friendly_hit_projectile');
1243 WhitelistCommand('g_friendly_absorb_damage');
1244 WhitelistCommand('g_weaponstay');
1245 WhitelistCommand('g_allow_exit');
1246 WhitelistCommand('g_dm_keys');
1247 WhitelistCommand('g_allow_monsters');
1248 WhitelistCommand('g_bot_vsmonsters');
1249 WhitelistCommand('g_bot_vsplayers');
1250 WhitelistCommand('g_scorelimit');
1251 WhitelistCommand('g_timelimit');
1252 WhitelistCommand('g_maxlives');
1253 WhitelistCommand('g_warmup_time');
1254 WhitelistCommand('g_spawn_invul');
1255 WhitelistCommand('g_item_respawn_time');
1256 WhitelistCommand('g_item_time_random');
1257 WhitelistCommand('g_items_all_respawn_random');
1258 WhitelistCommand('g_items_help_respawn_random');
1259 WhitelistCommand('g_items_ammo_respawn_random');
1260 WhitelistCommand('g_items_weapon_respawn_random');
1261 WhitelistCommand('g_powerup_randomize_respawn');
1262 WhitelistCommand('g_powerup_respawn_time');
1263 WhitelistCommand('g_powerup_time_random');
1265 g_Console_ResetBinds;
1266 g_Console_ReadConfig(gConfigScript);
1267 g_Console_ReadConfig(autoexecScript);
1268 gParsingBinds := False;
1269 end;
1271 procedure g_Console_Init;
1272 begin
1273 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
1274 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
1275 g_Console_Add('');
1276 {$IFDEF HEADLESS}
1277 if ConsoleStdIn then
1278 begin
1279 InitKeyboard();
1280 conbufStdOutRawMode := true;
1281 end;
1282 {$ENDIF}
1283 end;
1285 procedure g_Console_Update;
1287 a, b, Step: Integer;
1288 begin
1289 {$IFDEF HEADLESS}
1290 if ConsoleStdIn then
1291 ReadStdIn();
1292 {$ENDIF}
1294 if Cons_Shown then
1295 begin
1296 Step := Max(1, Round(Floor(gScreenHeight * ConsoleHeight) * ConsoleStep));
1297 if gConsoleShow then
1298 begin
1299 (* Open animation *)
1300 Cons_Y := Min(Cons_Y + Step, 0);
1301 InputReady := True
1303 else
1304 begin
1305 (* Close animation *)
1306 Cons_Y := Max(Cons_Y - Step, -Floor(gScreenHeight * ConsoleHeight));
1307 Cons_Shown := Cons_Y > -Floor(gScreenHeight * ConsoleHeight);
1308 InputReady := False
1309 end;
1311 if gChatShow then
1312 InputReady := True
1313 end;
1315 a := 0;
1316 while a <= High(MsgArray) do
1317 begin
1318 if MsgArray[a].Time > 0 then
1319 begin
1320 if MsgArray[a].Time = 1 then
1321 begin
1322 if a < High(MsgArray) then
1323 begin
1324 for b := a to High(MsgArray)-1 do
1325 MsgArray[b] := MsgArray[b+1];
1327 MsgArray[High(MsgArray)].Time := 0;
1329 a := a - 1;
1330 end;
1332 else
1333 Dec(MsgArray[a].Time);
1334 end;
1336 a := a + 1;
1337 end;
1338 end;
1341 procedure drawConsoleText ();
1343 CWidth, CHeight: Byte;
1344 ty: Integer;
1345 sp, ep: LongWord;
1346 skip: Integer;
1348 procedure putLine (sp, ep: LongWord);
1350 p: LongWord;
1351 wdt, cw: Integer;
1352 begin
1353 p := sp;
1354 wdt := 0;
1355 while p <> ep do
1356 begin
1357 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1358 if wdt+cw > gScreenWidth-8 then break;
1359 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1360 Inc(wdt, cw);
1361 cbufNext(p);
1362 end;
1363 if p <> ep then putLine(p, ep); // do rest of the line first
1364 // now print our part
1365 if skip = 0 then
1366 begin
1367 ep := p;
1368 p := sp;
1369 wdt := 2;
1370 while p <> ep do
1371 begin
1372 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1373 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1374 Inc(wdt, cw);
1375 cbufNext(p);
1376 end;
1377 Dec(ty, CHeight);
1379 else
1380 begin
1381 Dec(skip);
1382 end;
1383 end;
1385 begin
1386 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1387 ty := Floor(gScreenHeight * ConsoleHeight) - 4 - 2 * CHeight - Abs(Cons_Y);
1388 skip := conSkipLines;
1389 cbufLastLine(sp, ep);
1390 repeat
1391 putLine(sp, ep);
1392 if ty+CHeight <= 0 then break;
1393 until not cbufLineUp(sp, ep);
1394 end;
1396 procedure g_Console_Draw(MessagesOnly: Boolean = False);
1398 CWidth, CHeight: Byte;
1399 mfW, mfH: Word;
1400 a, b, offset_y: Integer;
1401 begin
1402 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1404 if ChatTop and gChatShow then
1405 offset_y := CHeight
1406 else
1407 offset_y := 0;
1409 for a := 0 to High(MsgArray) do
1410 if MsgArray[a].Time > 0 then
1411 e_TextureFontPrintFmt(0, offset_y + CHeight * a, MsgArray[a].Msg, gStdFont, True);
1413 if MessagesOnly then Exit;
1415 if gChatShow then
1416 begin
1417 if ChatTop then
1418 offset_y := 0
1419 else
1420 offset_y := gScreenHeight - CHeight - 1;
1421 if gChatTeam then
1422 begin
1423 e_TextureFontPrintEx(0, offset_y, 'say team> ' + Line, gStdFont, 255, 255, 255, 1, True);
1424 e_TextureFontPrintEx((CPos + 9) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1426 else
1427 begin
1428 e_TextureFontPrintEx(0, offset_y, 'say> ' + Line, gStdFont, 255, 255, 255, 1, True);
1429 e_TextureFontPrintEx((CPos + 4) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1431 end;
1433 if not Cons_Shown then
1434 Exit;
1436 if gDebugMode then
1437 begin
1438 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1439 a := (gScreenWidth - 2*mfW) div 2;
1440 b := Cons_Y + (Floor(gScreenHeight * ConsoleHeight) - 2 * mfH) div 2;
1441 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1442 _RGB(128, 0, 0), 2.0);
1443 end;
1445 e_DrawSize(ID, 0, Cons_Y, Round(ConsoleTrans * 255), False, False, gScreenWidth, Floor(gScreenHeight * ConsoleHeight));
1446 e_TextureFontPrint(0, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - CHeight - 4, '> ' + Line, gStdFont);
1448 drawConsoleText();
1450 if ConsoleHistory <> nil then
1451 begin
1452 b := 0;
1453 if CHeight > 0 then
1454 if Length(ConsoleHistory) > (Floor(gScreenHeight * ConsoleHeight) div CHeight) - 1 then
1455 b := Length(ConsoleHistory) - (Floor(gScreenHeight * ConsoleHeight) div CHeight) + 1;
1457 b := Max(b-Offset, 0);
1458 d := Max(High(ConsoleHistory)-Offset, 0);
1460 c := 2;
1461 for a := d downto b do
1462 begin
1463 e_TextureFontPrintFmt(0, Floor(gScreenHeight * ConsoleHeight) - 4 - c * CHeight - Abs(Cons_Y), ConsoleHistory[a], gStdFont, True);
1464 c := c + 1;
1465 end;
1466 end;
1469 e_TextureFontPrint((CPos + 1) * CWidth, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - 21, '_', gStdFont);
1470 end;
1472 procedure g_Console_Char(C: AnsiChar);
1473 begin
1474 if InputReady and (gConsoleShow or gChatShow) then
1475 begin
1476 Insert(C, Line, CPos);
1477 CPos := CPos + 1;
1479 end;
1483 tcomplist: array of AnsiString = nil;
1484 tcompidx: array of Integer = nil;
1486 procedure Complete ();
1488 i, c: Integer;
1489 tused: Integer;
1490 ll, lpfx, cmd: AnsiString;
1491 begin
1492 if (Length(Line) = 0) then
1493 begin
1494 g_Console_Add('');
1495 for i := 0 to High(commands) do
1496 begin
1497 // hidden commands are hidden when cheats aren't enabled
1498 if commands[i].hidden and not conIsCheatsEnabled then continue;
1499 if (Length(commands[i].help) > 0) then
1500 begin
1501 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1503 else
1504 begin
1505 g_Console_Add(' '+commands[i].cmd);
1506 end;
1507 end;
1508 exit;
1509 end;
1511 ll := LowerCase(Line);
1512 lpfx := '';
1514 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1515 begin
1516 ll := Copy(ll, 0, Length(ll)-1);
1517 for i := 0 to High(commands) do
1518 begin
1519 // hidden commands are hidden when cheats aren't enabled
1520 if commands[i].hidden and not conIsCheatsEnabled then continue;
1521 if (commands[i].cmd = ll) then
1522 begin
1523 if (Length(commands[i].help) > 0) then
1524 begin
1525 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1526 end;
1527 end;
1528 end;
1529 exit;
1530 end;
1532 // build completion list
1533 tused := 0;
1534 for i := 0 to High(commands) do
1535 begin
1536 // hidden commands are hidden when cheats aren't enabled
1537 if commands[i].hidden and not conIsCheatsEnabled then continue;
1538 cmd := commands[i].cmd;
1539 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1540 begin
1541 if (tused = Length(tcomplist)) then
1542 begin
1543 SetLength(tcomplist, Length(tcomplist)+128);
1544 SetLength(tcompidx, Length(tcompidx)+128);
1545 end;
1546 tcomplist[tused] := cmd;
1547 tcompidx[tused] := i;
1548 Inc(tused);
1549 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1550 end;
1551 end;
1553 // get longest prefix
1554 for i := 0 to tused-1 do
1555 begin
1556 cmd := tcomplist[i];
1557 for c := 1 to Length(lpfx) do
1558 begin
1559 if (c > Length(cmd)) then break;
1560 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1561 end;
1562 end;
1564 if (tused = 0) then exit;
1566 if (tused = 1) then
1567 begin
1568 Line := tcomplist[0]+' ';
1569 CPos := Length(Line)+1;
1571 else
1572 begin
1573 // has longest prefix?
1574 if (Length(lpfx) > Length(ll)) then
1575 begin
1576 Line := lpfx;
1577 CPos:= Length(Line)+1;
1579 else
1580 begin
1581 g_Console_Add('');
1582 for i := 0 to tused-1 do
1583 begin
1584 if (Length(commands[tcompidx[i]].help) > 0) then
1585 begin
1586 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1588 else
1589 begin
1590 g_Console_Add(' '+tcomplist[i]);
1591 end;
1592 end;
1593 end;
1594 end;
1595 end;
1598 procedure g_Console_Control(K: Word);
1599 begin
1600 case K of
1601 IK_BACKSPACE:
1602 if (Length(Line) > 0) and (CPos > 1) then
1603 begin
1604 Delete(Line, CPos-1, 1);
1605 CPos := CPos-1;
1606 end;
1607 IK_DELETE:
1608 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1609 Delete(Line, CPos, 1);
1610 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1611 if CPos > 1 then
1612 CPos := CPos - 1;
1613 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1614 if CPos <= Length(Line) then
1615 CPos := CPos + 1;
1616 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1617 begin
1618 if gConsoleShow then
1619 g_Console_Process(Line)
1620 else
1621 if gChatShow then
1622 begin
1623 if (Length(Line) > 0) and g_Game_IsNet then
1624 begin
1625 if gChatTeam then
1626 begin
1627 if g_Game_IsClient then
1628 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1629 else
1630 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1631 NET_CHAT_TEAM, gPlayer1Settings.Team);
1633 else
1634 begin
1635 if g_Game_IsClient then
1636 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1637 else
1638 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1639 NET_CHAT_PLAYER);
1640 end;
1641 end;
1643 Line := '';
1644 CPos := 1;
1645 gJustChatted := True;
1646 g_Console_Chat_Switch;
1647 InputReady := False;
1648 end;
1649 end;
1650 IK_TAB:
1651 if not gChatShow then
1652 Complete();
1653 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1654 if not gChatShow then
1655 if (CommandHistory <> nil) and
1656 (CmdIndex < Length(CommandHistory)) then
1657 begin
1658 if CmdIndex < Length(CommandHistory)-1 then
1659 CmdIndex := CmdIndex + 1;
1660 Line := CommandHistory[CmdIndex];
1661 CPos := Length(Line) + 1;
1662 end;
1663 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1664 if not gChatShow then
1665 if (CommandHistory <> nil) and
1666 (CmdIndex <= Length(CommandHistory)) then
1667 begin
1668 if CmdIndex > 0 then
1669 CmdIndex := CmdIndex - 1;
1670 Line := CommandHistory[CmdIndex];
1671 Cpos := Length(Line) + 1;
1672 end;
1673 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1674 if not gChatShow then Inc(conSkipLines);
1675 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1676 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1677 IK_HOME, IK_KPHOME:
1678 CPos := 1;
1679 IK_END, IK_KPEND:
1680 CPos := Length(Line) + 1;
1681 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1682 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, (*IK_EQUALS,*)
1683 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1684 (* see TEXTINPUT event *)
1686 end;
1688 function GetStr(var Str: AnsiString): AnsiString;
1690 a, b: Integer;
1691 begin
1692 Result := '';
1693 if Str[1] = '"' then
1694 begin
1695 for b := 1 to Length(Str) do
1696 if (b = Length(Str)) or (Str[b+1] = '"') then
1697 begin
1698 Result := Copy(Str, 2, b-1);
1699 Delete(Str, 1, b+1);
1700 Str := Trim(Str);
1701 Exit;
1702 end;
1703 end;
1705 for a := 1 to Length(Str) do
1706 if (a = Length(Str)) or (Str[a+1] = ' ') then
1707 begin
1708 Result := Copy(Str, 1, a);
1709 Delete(Str, 1, a+1);
1710 Str := Trim(Str);
1711 Exit;
1712 end;
1713 end;
1715 function ParseString(Str: AnsiString): SSArray;
1716 begin
1717 Result := nil;
1719 Str := Trim(Str);
1721 if Str = '' then
1722 Exit;
1724 while Str <> '' do
1725 begin
1726 SetLength(Result, Length(Result)+1);
1727 Result[High(Result)] := GetStr(Str);
1728 end;
1729 end;
1731 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1733 procedure conmsg (s: AnsiString);
1735 a: Integer;
1736 begin
1737 if length(s) = 0 then exit;
1738 for a := 0 to High(MsgArray) do
1739 begin
1740 with MsgArray[a] do
1741 begin
1742 if Time = 0 then
1743 begin
1744 Msg := s;
1745 Time := MsgTime;
1746 exit;
1747 end;
1748 end;
1749 end;
1750 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1751 with MsgArray[High(MsgArray)] do
1752 begin
1753 Msg := L;
1754 Time := MsgTime;
1755 end;
1756 end;
1759 f: Integer;
1760 begin
1761 // put it to console
1762 cbufPut(L);
1763 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1765 // now show 'em out of console too
1766 show := show and gAllowConsoleMessages;
1767 if show and gShowMessages then
1768 begin
1769 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1770 while length(L) > 0 do
1771 begin
1772 f := Pos(#10, L);
1773 if f <= 0 then f := length(L)+1;
1774 conmsg(Copy(L, 1, f-1));
1775 Delete(L, 1, f);
1776 end;
1777 end;
1779 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1780 //ConsoleHistory[High(ConsoleHistory)] := L;
1783 {$IFDEF HEADLESS}
1784 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1785 {$ENDIF}
1787 end;
1791 consolewriterLastWasEOL: Boolean = false;
1793 procedure consolewriter (constref buf; len: SizeUInt);
1795 b: PByte;
1796 begin
1797 if (len < 1) then exit;
1798 b := PByte(@buf);
1799 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1800 while (len > 0) do
1801 begin
1802 if (b[0] <> 13) and (b[0] <> 10) then
1803 begin
1804 cbufPut(AnsiChar(b[0]));
1806 else
1807 begin
1808 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1809 cbufPut(#10);
1810 end;
1811 len -= 1;
1812 b += 1;
1813 end;
1814 end;
1817 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1818 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1819 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1820 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1821 begin
1822 g_Console_Add(s, show);
1823 end;
1826 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1827 begin
1828 if show then
1829 begin
1830 g_Console_Add(formatstrf(s, args), true);
1832 else
1833 begin
1834 consolewriterLastWasEOL := false;
1835 formatstrf(s, args, consolewriter);
1836 if not consolewriterLastWasEOL then cbufPut(#10);
1837 end;
1838 end;
1841 procedure g_Console_Clear();
1842 begin
1843 //ConsoleHistory := nil;
1844 cbufClear();
1845 conSkipLines := 0;
1846 end;
1848 procedure AddToHistory(L: AnsiString);
1850 len: Integer;
1851 begin
1852 len := Length(CommandHistory);
1854 if (len = 0) or
1855 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1856 begin
1857 SetLength(CommandHistory, len+1);
1858 CommandHistory[len] := L;
1859 end;
1861 CmdIndex := Length(CommandHistory);
1862 end;
1864 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1866 Arr: SSArray;
1867 i: Integer;
1868 begin
1869 Result := True;
1871 Arr := nil;
1873 if Trim(C) = '' then
1874 Exit;
1876 Arr := ParseString(C);
1877 if Arr = nil then
1878 Exit;
1880 for i := 0 to High(Whitelist) do
1881 if Whitelist[i] = LowerCase(Arr[0]) then
1882 Result := False;
1883 end;
1885 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1887 Arr: SSArray;
1888 i: Integer;
1889 begin
1890 Arr := nil;
1892 if Trim(L) = '' then
1893 Exit;
1895 conSkipLines := 0; // "unscroll"
1897 if L = 'goobers' then
1898 begin
1899 Line := '';
1900 CPos := 1;
1901 gCheats := true;
1902 g_Console_Add('Your memory serves you well.');
1903 exit;
1904 end;
1906 if not quiet then
1907 begin
1908 g_Console_Add('> '+L);
1909 Line := '';
1910 CPos := 1;
1911 end;
1913 Arr := ParseString(L);
1914 if Arr = nil then
1915 Exit;
1917 if commands = nil then
1918 Exit;
1920 if not quiet then
1921 AddToHistory(L);
1923 for i := 0 to High(commands) do
1924 begin
1925 if commands[i].cmd = LowerCase(Arr[0]) then
1926 begin
1927 if commands[i].action >= 0 then
1928 begin
1929 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1930 exit
1931 end;
1932 if assigned(commands[i].procEx) then
1933 begin
1934 commands[i].procEx(@commands[i], Arr);
1935 exit
1936 end;
1937 if assigned(commands[i].proc) then
1938 begin
1939 commands[i].proc(Arr);
1940 exit
1943 end;
1945 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1946 end;
1949 function g_Console_Interactive: Boolean;
1950 begin
1951 Result := gConsoleShow
1952 end;
1954 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = ''; rep: Boolean = False);
1955 begin
1956 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1957 ASSERT(key >= 0);
1958 ASSERT(key < e_MaxInputKeys);
1959 if key > 0 then
1960 begin
1961 gInputBinds[key].rep := rep;
1962 gInputBinds[key].down := ParseAlias(down);
1963 gInputBinds[key].up := ParseAlias(up);
1964 end;
1965 g_Console_WriteGameConfig();
1966 end;
1968 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1970 function EqualsCommandLists (a, b: SSArray): Boolean;
1971 var i, len: Integer;
1972 begin
1973 result := False;
1974 len := Length(a);
1975 if len = Length(b) then
1976 begin
1977 i := 0;
1978 while (i < len) and (a[i] = b[i]) do inc(i);
1979 if i >= len then
1980 result := True
1982 end;
1984 begin
1985 ASSERT(key >= 0);
1986 ASSERT(key < e_MaxInputKeys);
1987 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1988 end;
1990 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1991 var i: Integer;
1992 begin
1993 ASSERT(n >= 1);
1994 result := 0;
1995 if commands = nil then Exit;
1996 i := 0;
1997 while (n >= 1) and (i < e_MaxInputKeys) do
1998 begin
1999 if (i < VK_FIRSTKEY) or (i > VK_LASTKEY) then (* never show virtual keys in gui *)
2000 begin
2001 if g_Console_MatchBind(i, down, up) then
2002 begin
2003 result := i;
2004 dec(n)
2005 end;
2006 end;
2007 inc(i)
2008 end;
2009 if n >= 1 then
2010 result := 0
2011 end;
2013 function g_Console_Action (action: Integer): Boolean;
2014 var i, len: Integer;
2015 begin
2016 ASSERT(action >= FIRST_ACTION);
2017 ASSERT(action <= LAST_ACTION);
2018 i := 0;
2019 len := Length(gPlayerAction);
2020 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
2021 Result := i < len
2022 end;
2024 function BindsAllowed (key: Integer): Boolean;
2025 begin
2026 Result := False;
2027 if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
2028 begin
2029 if gChatShow then
2030 Result := g_Console_MatchBind(key, 'togglemenu') or
2031 g_Console_MatchBind(key, 'showkeyboard') or
2032 g_Console_MatchBind(key, 'hidekeyboard')
2033 else if gConsoleShow or (g_ActiveWindow <> nil) or (gGameSettings.GameType = GT_NONE) then
2034 Result := g_Console_MatchBind(key, 'togglemenu') or
2035 g_Console_MatchBind(key, 'toggleconsole') or
2036 g_Console_MatchBind(key, 'showkeyboard') or
2037 g_Console_MatchBind(key, 'hidekeyboard')
2038 else (* in game *)
2039 Result := True
2041 end;
2043 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
2044 var i: Integer;
2045 begin
2046 if BindsAllowed(key) then
2047 begin
2048 if down then
2049 for i := 0 to High(gInputBinds[key].down) do
2050 g_Console_Process(gInputBinds[key].down[i], True)
2051 else
2052 for i := 0 to High(gInputBinds[key].up) do
2053 g_Console_Process(gInputBinds[key].up[i], True)
2054 end;
2055 if down and not menu_toggled then
2056 KeyPress(key);
2057 menu_toggled := False
2058 end;
2060 procedure g_Console_ProcessBindRepeat (key: Integer);
2061 var i: Integer;
2062 begin
2063 if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
2064 begin
2065 KeyPress(key); // key repeat in menus and shit
2066 Exit;
2067 end;
2068 if BindsAllowed(key) and gInputBinds[key].rep then
2069 begin
2070 for i := 0 to High(gInputBinds[key].down) do
2071 g_Console_Process(gInputBinds[key].down[i], True);
2072 end;
2073 end;
2075 procedure g_Console_ResetBinds;
2076 var i: Integer;
2077 begin
2078 for i := 0 to e_MaxInputKeys - 1 do
2079 g_Console_BindKey(i, '', '');
2081 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
2082 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
2083 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
2084 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
2085 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
2086 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
2087 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
2088 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
2089 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
2090 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
2091 g_Console_BindKey(IK_E, 'p1_weapnext', '', True);
2092 g_Console_BindKey(IK_Q, 'p1_weapprev', '', True);
2093 g_Console_BindKey(IK_R, 'p1_dropflag', '');
2094 g_Console_BindKey(IK_1, 'p1_weapon 1');
2095 g_Console_BindKey(IK_2, 'p1_weapon 2');
2096 g_Console_BindKey(IK_3, 'p1_weapon 3');
2097 g_Console_BindKey(IK_4, 'p1_weapon 4');
2098 g_Console_BindKey(IK_5, 'p1_weapon 5');
2099 g_Console_BindKey(IK_6, 'p1_weapon 6');
2100 g_Console_BindKey(IK_7, 'p1_weapon 7');
2101 g_Console_BindKey(IK_8, 'p1_weapon 8');
2102 g_Console_BindKey(IK_9, 'p1_weapon 9');
2103 g_Console_BindKey(IK_0, 'p1_weapon 10');
2104 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
2105 g_Console_BindKey(IK_T, 'togglechat');
2106 g_Console_BindKey(IK_Y, 'toggleteamchat');
2107 g_Console_BindKey(IK_F11, 'screenshot');
2108 g_Console_BindKey(IK_TAB, '+scores', '-scores');
2109 g_Console_BindKey(IK_PAUSE, 'pause');
2110 g_Console_BindKey(IK_F1, 'vote');
2112 (* for i := 0 to e_MaxJoys - 1 do *)
2113 for i := 0 to 1 do
2114 begin
2115 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
2116 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
2117 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
2118 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
2119 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
2120 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
2121 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
2122 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
2123 g_Console_BindKey(e_JoyButtonToKey(i, 1), 'p' + IntToStr(i mod 2 + 1) + '_weapnext', '', True);
2124 g_Console_BindKey(e_JoyButtonToKey(i, 4), 'p' + IntToStr(i mod 2 + 1) + '_weapprev', '', True);
2125 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
2126 end;
2128 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
2129 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
2130 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
2131 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
2132 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
2133 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
2134 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
2135 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
2136 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
2137 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
2138 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
2139 g_Console_BindKey(VK_NEXT, 'weapnext', '', True);
2140 g_Console_BindKey(VK_PREV, 'weapprev', '', True);
2141 g_Console_BindKey(VK_0, 'weapon 1');
2142 g_Console_BindKey(VK_1, 'weapon 2');
2143 g_Console_BindKey(VK_2, 'weapon 3');
2144 g_Console_BindKey(VK_3, 'weapon 4');
2145 g_Console_BindKey(VK_4, 'weapon 5');
2146 g_Console_BindKey(VK_5, 'weapon 6');
2147 g_Console_BindKey(VK_6, 'weapon 7');
2148 g_Console_BindKey(VK_7, 'weapon 8');
2149 g_Console_BindKey(VK_8, 'weapon 9');
2150 g_Console_BindKey(VK_9, 'weapon 10');
2151 g_Console_BindKey(VK_A, 'weapon 11');
2152 g_Console_BindKey(VK_CHAT, 'togglechat');
2153 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
2154 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
2155 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
2156 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
2157 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
2158 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
2159 end;
2161 procedure g_Console_ReadConfig (filename: String);
2162 var f: TextFile; s: AnsiString; i, len: Integer;
2163 begin
2164 e_LogWritefln('g_Console_ReadConfig (1) "%s"', [filename]);
2165 if e_FindResource(ConfigDirs, filename, false) = true then
2166 begin
2167 e_LogWritefln('g_Console_ReadConfig (2) "%s"', [filename]);
2168 AssignFile(f, filename);
2169 Reset(f);
2170 while not EOF(f) do
2171 begin
2172 ReadLn(f, s);
2173 len := Length(s);
2174 if len > 0 then
2175 begin
2176 i := 1;
2177 (* skip spaces *)
2178 while (i <= len) and (s[i] <= ' ') do inc(i);
2179 (* skip comments *)
2180 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
2181 g_Console_Process(s, True);
2183 end;
2184 CloseFile(f);
2186 end;
2188 procedure g_Console_WriteConfig (filename: String);
2189 var f: TextFile; i, j: Integer;
2191 procedure WriteFlag(name: string; flag: TGameOption);
2192 begin
2193 WriteLn(f, name, Ord(flag in gsGameFlags));
2194 end;
2196 function FormatTeam(team: Byte): string;
2197 begin
2198 if team = TEAM_BLUE then
2199 result := 'blue'
2200 else
2201 result := 'red';
2202 end;
2204 begin
2205 AssignFile(f, filename);
2206 Rewrite(f);
2207 WriteLn(f, '// ' + configComment);
2209 // binds
2210 WriteLn(f, 'unbindall');
2211 for i := 0 to e_MaxInputKeys - 1 do
2212 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
2213 begin
2214 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
2215 if Length(gInputBinds[i].down) = 0 then
2216 Write(f, '""');
2217 if Length(gInputBinds[i].up) > 0 then
2218 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
2219 WriteLn(f, '');
2220 if gInputBinds[i].rep then
2221 WriteLn(f, 'bindrep ', e_KeyNames[i]);
2222 end;
2224 // lang
2225 if gAskLanguage then
2226 WriteLn(f, 'g_language ask')
2227 else
2228 WriteLn(f, 'g_language ', gLanguage);
2230 // net server
2231 WriteLn(f, 'sv_name ', QuoteStr(NetServerName));
2232 WriteLn(f, 'sv_passwd ', QuoteStr(NetPassword));
2233 WriteLn(f, 'sv_maxplrs ', NetMaxClients);
2234 WriteLn(f, 'sv_port ', NetPort);
2235 WriteLn(f, 'sv_public ', IfThen(NetUseMaster, 1, 0));
2237 // game settings
2238 WriteLn(f, 'g_max_particles ', g_GFX_GetMax());
2239 WriteLn(f, 'g_max_shells ', g_Shells_GetMax());
2240 WriteLn(f, 'g_max_gibs ', g_Gibs_GetMax());
2241 WriteLn(f, 'g_max_corpses ', g_Corpses_GetMax());
2242 WriteLn(f, 'g_force_model ', g_Force_Model_Get());
2243 WriteLn(f, 'g_force_model_name ', g_Forced_Model_GetName());
2244 WriteLn(f, 'sv_intertime ', gDefInterTime);
2246 // gameplay settings
2247 WriteLn(f, 'g_gamemode ', gsGameMode);
2248 WriteLn(f, 'g_scorelimit ', gsScoreLimit);
2249 WriteLn(f, 'g_timelimit ', gsTimeLimit);
2250 WriteLn(f, 'g_maxlives ', gsMaxLives);
2251 WriteLn(f, 'g_item_respawn_time ', gsItemRespawnTime);
2252 WriteLn(f, 'g_item_time_random ', gsItemRespawnRandom);
2253 WriteLn(f, 'g_powerup_respawn_time ', gsPowerupRespawnTime);
2254 WriteLn(f, 'g_powerup_time_random ', gsPowerupRespawnRandom);
2255 WriteLn(f, 'g_spawn_invul ', gsSpawnInvul);
2256 WriteLn(f, 'g_warmup_time ', gsWarmupTime);
2258 WriteFlag('g_friendlyfire ', TGameOption.TEAM_DAMAGE);
2259 WriteFlag('g_friendly_hit_trace ', TGameOption.TEAM_HIT_TRACE);
2260 WriteFlag('g_friendly_hit_projectile ', TGameOption.TEAM_HIT_PROJECTILE);
2261 WriteFlag('g_powerup_randomize_respawn ', TGameOption.POWERUP_RANDOM);
2262 WriteFlag('g_items_all_respawn_random ', TGameOption.ITEM_ALL_RANDOM);
2263 WriteFlag('g_items_help_respawn_random ', TGameOption.ITEM_LIFE_RANDOM);
2264 WriteFlag('g_items_ammo_respawn_random ', TGameOption.ITEM_AMMO_RANDOM);
2265 WriteFlag('g_items_weapon_respawn_random ', TGameOption.ITEM_WEAPON_RANDOM);
2266 WriteFlag('g_allow_exit ', TGameOption.ALLOW_EXIT);
2267 WriteFlag('g_allow_monsters ', TGameOption.MONSTERS);
2268 WriteFlag('g_allow_dropflag ', TGameOption.ALLOW_DROP_FLAG);
2269 WriteFlag('g_throw_flag ', TGameOption.THROW_FLAG);
2270 WriteFlag('g_dm_keys ', TGameOption.DM_KEYS);
2271 WriteFlag('g_weaponstay ', TGameOption.WEAPONS_STAY);
2272 WriteFlag('g_bot_vsmonsters ', TGameOption.BOTS_VS_MONSTERS);
2273 WriteFlag('g_bot_vsplayers ', TGameOption.BOTS_VS_PLAYERS);
2275 // players
2276 with gPlayer1Settings do
2277 begin
2278 WriteLn(f, 'p1_name ', QuoteStr(Name));
2279 WriteLn(f, 'p1_color ', Color.R, ' ', Color.G, ' ', Color.B);
2280 WriteLn(f, 'p1_model ', QuoteStr(Model));
2281 WriteLn(f, 'p1_team ', FormatTeam(Team));
2282 WriteLn(f, 'p1_autoswitch ', WeaponSwitch);
2283 WriteLn(f, 'p1_switch_empty ', SwitchToEmpty);
2284 WriteLn(f, 'p1_priority_ironfist ', Max(0, WeaponPreferences[WEAPON_IRONFIST]));
2285 WriteLn(f, 'p1_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2286 WriteLn(f, 'p1_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2287 WriteLn(f, 'p1_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2288 WriteLn(f, 'p1_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN2]));
2289 WriteLn(f, 'p1_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2290 WriteLn(f, 'p1_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2291 WriteLn(f, 'p1_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2292 WriteLn(f, 'p1_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2293 WriteLn(f, 'p1_priority_superchaingun ', Max(0, WeaponPreferences[WEAPON_SUPERCHAINGUN]));
2294 WriteLn(f, 'p1_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2295 WriteLn(f, 'p1_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2296 end;
2297 with gPlayer2Settings do
2298 begin
2299 WriteLn(f, 'p2_name ', QuoteStr(Name));
2300 WriteLn(f, 'p2_color ', Color.R, ' ', Color.G, ' ', Color.B);
2301 WriteLn(f, 'p2_model ', QuoteStr(Model));
2302 WriteLn(f, 'p2_team ', FormatTeam(Team));
2303 WriteLn(f, 'p2_autoswitch ', WeaponSwitch);
2304 WriteLn(f, 'p2_switch_empty ', SwitchToEmpty);
2305 WriteLn(f, 'p2_priority_ironfist ', Max(0, WeaponPreferences[WEAPON_IRONFIST]));
2306 WriteLn(f, 'p2_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2307 WriteLn(f, 'p2_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2308 WriteLn(f, 'p2_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2309 WriteLn(f, 'p2_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN2]));
2310 WriteLn(f, 'p2_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2311 WriteLn(f, 'p2_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2312 WriteLn(f, 'p2_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2313 WriteLn(f, 'p2_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2314 WriteLn(f, 'p2_priority_superchaingun ', Max(0, WeaponPreferences[WEAPON_SUPERCHAINGUN]));
2315 WriteLn(f, 'p2_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2316 WriteLn(f, 'p2_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2317 end;
2319 // all cvars
2320 for i := 0 to High(commands) do
2321 begin
2322 if not commands[i].cheat then
2323 begin
2324 if @commands[i].procEx = @boolVarHandler then
2325 begin
2326 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
2327 WriteLn(f, commands[i].cmd, ' ', j)
2329 else if @commands[i].procEx = @intVarHandler then
2330 begin
2331 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
2333 else if @commands[i].procEx = @wordVarHandler then
2334 begin
2335 WriteLn(f, commands[i].cmd, ' ', PWord(commands[i].ptr)^)
2337 else if @commands[i].procEx = @dwordVarHandler then
2338 begin
2339 WriteLn(f, commands[i].cmd, ' ', PCardinal(commands[i].ptr)^)
2341 else if @commands[i].procEx = @singleVarHandler then
2342 begin
2343 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
2345 else if @commands[i].procEx = @strVarHandler then
2346 begin
2347 if Length(PAnsiString(commands[i].ptr)^) = 0 then
2348 WriteLn(f, commands[i].cmd, ' ""')
2349 else
2350 WriteLn(f, commands[i].cmd, ' ', QuoteStr(PAnsiString(commands[i].ptr)^))
2353 end;
2355 WriteLn(f, 'r_maxfps ', gMaxFPS);
2356 WriteLn(f, 'r_reset');
2357 CloseFile(f)
2358 end;
2360 procedure g_Console_WriteGameConfig;
2361 var s: AnsiString;
2362 begin
2363 if gParsingBinds = false then
2364 begin
2365 s := e_GetWriteableDir(ConfigDirs);
2366 g_Console_WriteConfig(e_CatPath(s, gConfigScript))
2368 end;
2370 procedure Init();
2372 i: Integer;
2373 begin
2374 conRegVar('chat_at_top', @ChatTop, 'draw chat at top border', 'draw chat at top border');
2375 conRegVar('console_height', @ConsoleHeight, 0.0, 1.0, 'set console size', 'set console size');
2376 conRegVar('console_trans', @ConsoleTrans, 0.0, 1.0, 'set console transparency', 'set console transparency');
2377 conRegVar('console_step', @ConsoleStep, 0.0, 1.0, 'set console animation speed', 'set console animation speed');
2378 conRegVar('console_stdin', @ConsoleStdIn, 'enable reading commands from stdin', 'enable reading commands from stdin');
2379 {$IFDEF ANDROID}
2380 ChatTop := True;
2381 ConsoleHeight := 0.35;
2382 {$ELSE}
2383 ChatTop := False;
2384 ConsoleHeight := 0.5;
2385 {$ENDIF}
2386 ConsoleTrans := 0.1;
2387 ConsoleStep := 0.07;
2388 {$IFDEF HEADLESS}
2389 ConsoleStdIn := True;
2390 {$ELSE}
2391 ConsoleStdIn := False;
2392 {$ENDIF}
2393 conRegVar('d_eres', @debug_e_res, '', '');
2394 for i := 1 to e_MaxJoys do
2395 conRegVar('joy' + IntToStr(i) + '_deadzone', @e_JoystickDeadzones[i - 1], '', '')
2396 end;
2398 procedure Cleanup();
2400 C: TCommand;
2401 begin
2402 for C in commands do
2403 if @C.procEx = @singleVarHandler then
2404 FreeMem(C.ptr);
2405 end;
2407 initialization
2408 Init();
2410 finalization
2411 {$IFDEF HEADLESS}
2412 DoneKeyboard;
2413 conbufStdOutRawMode := false;
2414 {$ENDIF}
2415 Cleanup();
2417 end.