saveload: fix read/write unexisting value
[d2df-sdl.git] / src / game / g_console.pas
blobd5816a7e7f51d135fa927d760e08fadfbc76f550
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);
694 // FIXME: hack for improper ConcatPaths(); see commit.
695 if s = ''
696 then s := p[1]
697 else s := ConcatPaths([s, p[1]]);
699 g_Console_WriteConfig(s);
701 else
702 begin
703 g_Console_Add('writeconfig <file>');
705 end;
707 if (cmd = 'ver') or (cmd = 'version') then
708 begin
709 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
710 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
711 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
712 end;
714 if cmd = 'alias' then
715 begin
716 // alias [alias_name] [commands]
717 if Length(p) > 1 then
718 begin
719 for a := 0 to High(Aliases) do
720 if Aliases[a].name = p[1] then
721 begin
722 if Length(p) > 2 then
723 Aliases[a].commands := ParseAlias(p[2])
724 else
725 for b := 0 to High(Aliases[a].commands) do
726 g_Console_Add(Aliases[a].commands[b]);
727 Exit;
728 end;
729 SetLength(Aliases, Length(Aliases)+1);
730 a := High(Aliases);
731 Aliases[a].name := p[1];
732 if Length(p) > 2 then
733 Aliases[a].commands := ParseAlias(p[2])
734 else
735 for b := 0 to High(Aliases[a].commands) do
736 g_Console_Add(Aliases[a].commands[b]);
737 end else
738 for a := 0 to High(Aliases) do
739 if Aliases[a].commands <> nil then
740 g_Console_Add(Aliases[a].name);
741 end;
743 if cmd = 'call' then
744 begin
745 // call <alias_name>
746 if Length(p) > 1 then
747 begin
748 if Aliases = nil then
749 Exit;
750 for a := 0 to High(Aliases) do
751 if Aliases[a].name = p[1] then
752 begin
753 if Aliases[a].commands <> nil then
754 begin
755 // with this system proper endless loop detection seems either impossible
756 // or very dirty to implement, so let's have this instead
757 // prevents endless loops
758 for b := 0 to High(Aliases[a].commands) do
759 begin
760 Inc(RecursionDepth);
761 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
762 if not RecursionLimitHit then
763 g_Console_Process(Aliases[a].commands[b], True);
764 Dec(RecursionDepth);
765 end;
766 if (RecursionDepth = 0) and RecursionLimitHit then
767 begin
768 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
769 RecursionLimitHit := False;
770 end;
771 end;
772 Exit;
773 end;
775 else
776 g_Console_Add('call <alias name>');
777 end;
778 end;
780 procedure WhitelistCommand(cmd: AnsiString);
782 a: Integer;
783 begin
784 SetLength(Whitelist, Length(Whitelist)+1);
785 a := High(Whitelist);
786 Whitelist[a] := LowerCase(cmd);
787 end;
789 procedure segfault (p: SSArray);
791 pp: PByte = nil;
792 begin
793 pp^ := 0;
794 end;
796 function GetCommandString (p: SSArray): AnsiString;
797 var i: Integer;
798 begin
799 result := '';
800 if Length(p) >= 1 then
801 begin
802 result := p[0];
803 for i := 1 to High(p) do
804 result := result + '; ' + p[i]
806 end;
808 function QuoteStr(str: String): String;
809 begin
810 if Pos(' ', str) > 0 then
811 Result := '"' + str + '"'
812 else
813 Result := str;
814 end;
816 procedure BindCommands (p: SSArray);
817 var cmd, key: AnsiString; i: Integer;
818 begin
819 cmd := LowerCase(p[0]);
820 case cmd of
821 'bind':
822 // bind <key> [down [up]]
823 if (Length(p) >= 2) and (Length(p) <= 4) then
824 begin
825 i := 0;
826 key := LowerCase(p[1]);
827 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
828 if i < e_MaxInputKeys then
829 begin
830 if Length(p) = 2 then
831 g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
832 else if Length(p) = 3 then
833 g_Console_BindKey(i, p[2], '')
834 else (* len = 4 *)
835 g_Console_BindKey(i, p[2], p[3])
837 else
838 g_Console_Add('bind: "' + p[1] + '" is not a key')
840 else
841 begin
842 g_Console_Add('bind <key> <down action> [up action]')
843 end;
844 'bindrep':
845 // bindrep <key>
846 if Length(p) = 2 then
847 begin
848 key := LowerCase(p[1]);
849 i := 0;
850 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
851 if i < e_MaxInputKeys then
852 gInputBinds[i].rep := True
853 else
854 g_Console_Add('bindrep: "' + p[1] + '" is not a key')
856 else
857 g_Console_Add('bindrep <key>');
858 'bindunrep':
859 // bindunrep <key>
860 if Length(p) = 2 then
861 begin
862 key := LowerCase(p[1]);
863 i := 0;
864 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
865 if i < e_MaxInputKeys then
866 gInputBinds[i].rep := False
867 else
868 g_Console_Add('bindunrep: "' + p[1] + '" is not a key')
870 else
871 g_Console_Add('bindunrep <key>');
872 'bindlist':
873 for i := 0 to e_MaxInputKeys - 1 do
874 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
875 g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
876 'unbind':
877 // unbind <key>
878 if Length(p) = 2 then
879 begin
880 key := LowerCase(p[1]);
881 i := 0;
882 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
883 if i < e_MaxInputKeys then
884 g_Console_BindKey(i, '')
885 else
886 g_Console_Add('unbind: "' + p[1] + '" is not a key')
888 else
889 g_Console_Add('unbind <key>');
890 'unbindall':
891 for i := 0 to e_MaxInputKeys - 1 do
892 g_Console_BindKey(i, '');
893 'showkeyboard':
894 g_Touch_ShowKeyboard(True);
895 'hidekeyboard':
896 g_Touch_ShowKeyboard(False);
897 'togglemenu':
898 begin
899 if gConsoleShow then
900 g_Console_Switch
901 else if gChatShow then
902 g_Console_Chat_Switch
903 else
904 KeyPress(VK_ESCAPE);
905 menu_toggled := True
906 end;
907 'toggleconsole':
908 g_Console_Switch;
909 'togglechat':
910 g_Console_Chat_Switch;
911 'toggleteamchat':
912 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
913 g_Console_Chat_Switch(True);
915 end;
917 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
919 a: Integer;
920 cp: PCommand;
921 begin
922 SetLength(commands, Length(commands)+1);
923 a := High(commands);
924 cp := @commands[a];
925 cp.cmd := LowerCase(cmd);
926 cp.proc := proc;
927 cp.procEx := nil;
928 cp.help := ahelp;
929 cp.hidden := ahidden;
930 cp.ptr := nil;
931 cp.msg := '';
932 cp.cheat := acheat;
933 cp.action := -1;
934 cp.player := -1;
935 end;
937 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
938 const
939 PrefixList: array [0..1] of AnsiString = ('+', '-');
940 PlayerList: array [0..1] of Integer = (1, 2);
942 s: AnsiString;
943 i: Integer;
945 procedure NewAction (cmd: AnsiString; player: Integer);
946 var cp: PCommand;
947 begin
948 SetLength(commands, Length(commands) + 1);
949 cp := @commands[High(commands)];
950 cp.cmd := LowerCase(cmd);
951 cp.proc := nil;
952 cp.procEx := nil;
953 cp.help := help;
954 cp.hidden := hidden;
955 cp.ptr := nil;
956 cp.msg := '';
957 cp.cheat := cheat;
958 cp.action := action;
959 cp.player := player;
960 end;
962 begin
963 ASSERT(action >= FIRST_ACTION);
964 ASSERT(action <= LAST_ACTION);
965 for s in PrefixList do
966 begin
967 NewAction(s + cmd, 0);
968 for i in PlayerList do
969 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
971 end;
973 procedure ReadStdIn;
975 K: Char;
976 KEv: TKeyEvent;
977 begin
978 gConsoleShow := True;
979 InputReady := True;
980 // one key per frame
981 KEv := PollKeyEvent();
982 if KEv <> 0 then
983 begin
984 K := GetKeyEventChar(TranslateKeyEvent(GetKeyEvent()));
985 Write(K);
986 case K of
987 #8: g_Console_Control(IK_BACKSPACE);
988 #10, #13: g_Console_Control(IK_RETURN);
989 #32..#126: g_Console_Char(K);
990 // arrow keys and DEL all return 0 for some reason, so fuck em
991 end;
992 end;
993 end;
995 procedure g_Console_SysInit;
996 var a: Integer;
997 begin
998 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
999 gConsoleShow := False;
1000 gChatShow := False;
1001 Cons_Shown := False;
1002 InputReady := False;
1003 CPos := 1;
1005 for a := 0 to High(MsgArray) do
1006 with MsgArray[a] do
1007 begin
1008 Msg := '';
1009 Time := 0;
1010 end;
1012 AddCommand('segfault', segfault, 'make segfault');
1014 AddCommand('quit', SystemCommands);
1015 AddCommand('exit', SystemCommands);
1016 AddCommand('r_reset', SystemCommands);
1017 AddCommand('r_maxfps', SystemCommands);
1018 AddCommand('g_language', SystemCommands);
1020 AddCommand('bind', BindCommands);
1021 AddCommand('bindrep', BindCommands);
1022 AddCommand('bindunrep', BindCommands);
1023 AddCommand('bindlist', BindCommands);
1024 AddCommand('unbind', BindCommands);
1025 AddCommand('unbindall', BindCommands);
1026 AddCommand('showkeyboard', BindCommands);
1027 AddCommand('hidekeyboard', BindCommands);
1028 AddCommand('togglemenu', BindCommands);
1029 AddCommand('toggleconsole', BindCommands);
1030 AddCommand('togglechat', BindCommands);
1031 AddCommand('toggleteamchat', BindCommands);
1033 AddCommand('clear', ConsoleCommands, 'clear console');
1034 AddCommand('clearhistory', ConsoleCommands);
1035 AddCommand('showhistory', ConsoleCommands);
1036 AddCommand('commands', ConsoleCommands);
1037 AddCommand('time', ConsoleCommands);
1038 AddCommand('date', ConsoleCommands);
1039 AddCommand('echo', ConsoleCommands);
1040 AddCommand('dump', ConsoleCommands);
1041 AddCommand('exec', ConsoleCommands);
1042 AddCommand('writeconfig', ConsoleCommands);
1043 AddCommand('alias', ConsoleCommands);
1044 AddCommand('call', ConsoleCommands);
1045 AddCommand('ver', ConsoleCommands);
1046 AddCommand('version', ConsoleCommands);
1048 AddCommand('d_window', DebugCommands);
1049 AddCommand('d_sounds', DebugCommands);
1050 AddCommand('d_frames', DebugCommands);
1051 AddCommand('d_winmsg', DebugCommands);
1052 AddCommand('d_monoff', DebugCommands);
1053 AddCommand('d_botoff', DebugCommands);
1054 AddCommand('d_monster', DebugCommands);
1055 AddCommand('d_health', DebugCommands);
1056 AddCommand('d_player', DebugCommands);
1057 AddCommand('d_joy', DebugCommands);
1058 AddCommand('d_mem', DebugCommands);
1060 AddCommand('p1_name', PlayerSettingsCVars);
1061 AddCommand('p2_name', PlayerSettingsCVars);
1062 AddCommand('p1_color', PlayerSettingsCVars);
1063 AddCommand('p2_color', PlayerSettingsCVars);
1064 AddCommand('p1_model', PlayerSettingsCVars);
1065 AddCommand('p2_model', PlayerSettingsCVars);
1066 AddCommand('p1_team', PlayerSettingsCVars);
1067 AddCommand('p2_team', PlayerSettingsCVars);
1068 AddCommand('p1_autoswitch', PlayerSettingsCVars);
1069 AddCommand('p2_autoswitch', PlayerSettingsCVars);
1070 AddCommand('p1_switch_empty', PlayerSettingsCVars);
1071 AddCommand('p2_switch_empty', PlayerSettingsCVars);
1072 AddCommand('p1_skip_ironfist', PlayerSettingsCVars);
1073 AddCommand('p2_skip_ironfist', PlayerSettingsCVars);
1074 AddCommand('p1_priority_ironfist', PlayerSettingsCVars);
1075 AddCommand('p2_priority_ironfist', PlayerSettingsCVars);
1076 AddCommand('p1_priority_saw', PlayerSettingsCVars);
1077 AddCommand('p2_priority_saw', PlayerSettingsCVars);
1078 AddCommand('p1_priority_pistol', PlayerSettingsCVars);
1079 AddCommand('p2_priority_pistol', PlayerSettingsCVars);
1080 AddCommand('p1_priority_shotgun1', PlayerSettingsCVars);
1081 AddCommand('p2_priority_shotgun1', PlayerSettingsCVars);
1082 AddCommand('p1_priority_shotgun2', PlayerSettingsCVars);
1083 AddCommand('p2_priority_shotgun2', PlayerSettingsCVars);
1084 AddCommand('p1_priority_chaingun', PlayerSettingsCVars);
1085 AddCommand('p2_priority_chaingun', PlayerSettingsCVars);
1086 AddCommand('p1_priority_rocketlauncher', PlayerSettingsCVars);
1087 AddCommand('p2_priority_rocketlauncher', PlayerSettingsCVars);
1088 AddCommand('p1_priority_plasma', PlayerSettingsCVars);
1089 AddCommand('p2_priority_plasma', PlayerSettingsCVars);
1090 AddCommand('p1_priority_bfg', PlayerSettingsCVars);
1091 AddCommand('p2_priority_bfg', PlayerSettingsCVars);
1092 AddCommand('p1_priority_superchaingun', PlayerSettingsCVars);
1093 AddCommand('p2_priority_superchaingun', PlayerSettingsCVars);
1094 AddCommand('p1_priority_flamethrower', PlayerSettingsCVars);
1095 AddCommand('p2_priority_flamethrower', PlayerSettingsCVars);
1096 AddCommand('p1_priority_berserk', PlayerSettingsCVars);
1097 AddCommand('p2_priority_berserk', PlayerSettingsCVars);
1099 AddCommand('g_max_particles', GameCVars);
1100 AddCommand('g_max_shells', GameCVars);
1101 AddCommand('g_max_gibs', GameCVars);
1102 AddCommand('g_max_corpses', GameCVars);
1103 AddCommand('g_force_model', GameCVars);
1104 AddCommand('g_force_model_name', GameCVars);
1105 AddCommand('g_gamemode', GameCVars);
1106 AddCommand('g_friendlyfire', GameCVars);
1107 AddCommand('g_friendly_hit_trace', GameCVars);
1108 AddCommand('g_friendly_hit_projectile', GameCVars);
1109 AddCommand('g_friendly_absorb_damage', GameCVars);
1110 AddCommand('g_weaponstay', GameCVars);
1111 AddCommand('g_allow_exit', GameCVars);
1112 AddCommand('g_dm_keys', GameCVars);
1113 AddCommand('g_allow_monsters', GameCVars);
1114 AddCommand('g_allow_dropflag', GameCVars);
1115 AddCommand('g_throw_flag', GameCVars);
1116 AddCommand('g_bot_vsmonsters', GameCVars);
1117 AddCommand('g_bot_vsplayers', GameCVars);
1118 AddCommand('g_max_bots', GameCVars); // intentionally not whitelisted
1119 AddCommand('g_scorelimit', GameCVars);
1120 AddCommand('g_timelimit', GameCVars);
1121 AddCommand('g_maxlives', GameCVars);
1122 AddCommand('g_warmup_time', GameCVars);
1123 AddCommand('g_spawn_invul', GameCVars);
1124 AddCommand('g_item_respawn_time', GameCVars);
1125 AddCommand('g_item_time_random', GameCVars);
1126 AddCommand('g_items_all_respawn_random', GameCVars);
1127 AddCommand('g_items_help_respawn_random', GameCVars);
1128 AddCommand('g_items_ammo_respawn_random', GameCVars);
1129 AddCommand('g_items_weapon_respawn_random', GameCVars);
1130 AddCommand('g_powerup_randomize_respawn', GameCVars);
1131 AddCommand('g_powerup_respawn_time', GameCVars);
1132 AddCommand('g_powerup_time_random', GameCVars);
1133 AddCommand('sv_intertime', GameCVars);
1135 AddCommand('sv_name', NetServerCVars);
1136 AddCommand('sv_passwd', NetServerCVars);
1137 AddCommand('sv_maxplrs', NetServerCVars);
1138 AddCommand('sv_public', NetServerCVars);
1139 AddCommand('sv_port', NetServerCVars);
1141 AddCommand('pause', GameCommands);
1142 AddCommand('endgame', GameCommands);
1143 AddCommand('restart', GameCommands);
1144 AddCommand('addbot', GameCommands);
1145 AddCommand('bot_add', GameCommands);
1146 AddCommand('bot_addlist', GameCommands);
1147 AddCommand('bot_addred', GameCommands);
1148 AddCommand('bot_addblue', GameCommands);
1149 AddCommand('bot_removeall', GameCommands);
1150 AddCommand('chat', GameCommands);
1151 AddCommand('teamchat', GameCommands);
1152 AddCommand('announce', GameCommands);
1153 AddCommand('an', GameCommands);
1154 AddCommand('game', GameCommands);
1155 AddCommand('host', GameCommands);
1156 AddCommand('map', GameCommands);
1157 AddCommand('nextmap', GameCommands);
1158 AddCommand('endmap', GameCommands);
1159 AddCommand('goodbye', GameCommands);
1160 AddCommand('suicide', GameCommands);
1161 AddCommand('spectate', GameCommands);
1162 AddCommand('ready', GameCommands);
1163 AddCommand('kick', GameCommands);
1164 AddCommand('kick_id', GameCommands);
1165 AddCommand('kick_pid', GameCommands);
1166 AddCommand('ban', GameCommands);
1167 AddCommand('ban_id', GameCommands);
1168 AddCommand('ban_pid', GameCommands);
1169 AddCommand('permban', GameCommands);
1170 AddCommand('permban_id', GameCommands);
1171 AddCommand('permban_pid', GameCommands);
1172 AddCommand('permban_ip', GameCommands);
1173 AddCommand('unban', GameCommands);
1174 AddCommand('connect', GameCommands);
1175 AddCommand('disconnect', GameCommands);
1176 AddCommand('reconnect', GameCommands);
1177 AddCommand('say', GameCommands);
1178 AddCommand('tell', GameCommands);
1179 AddCommand('centerprint', GameCommands);
1180 AddCommand('overtime', GameCommands);
1181 AddCommand('rcon_password', GameCommands);
1182 AddCommand('rcon', GameCommands);
1183 AddCommand('callvote', GameCommands);
1184 AddCommand('vote', GameCommands);
1185 AddCommand('clientlist', GameCommands);
1186 AddCommand('event', GameCommands);
1187 AddCommand('screenshot', GameCommands);
1188 AddCommand('weapnext', GameCommands);
1189 AddCommand('weapprev', GameCommands);
1190 AddCommand('weapon', GameCommands);
1191 AddCommand('dropflag', GameCommands);
1192 AddCommand('p1_weapnext', GameCommands);
1193 AddCommand('p1_weapprev', GameCommands);
1194 AddCommand('p1_weapon', GameCommands);
1195 AddCommand('p1_weapbest', GameCommands);
1196 AddCommand('p1_dropflag', GameCommands);
1197 AddCommand('p2_weapnext', GameCommands);
1198 AddCommand('p2_weapprev', GameCommands);
1199 AddCommand('p2_weapon', GameCommands);
1200 AddCommand('p2_weapbest', GameCommands);
1201 AddCommand('p2_dropflag', GameCommands);
1203 AddCommand('god', GameCheats);
1204 AddCommand('notarget', GameCheats);
1205 AddCommand('give', GameCheats); // "exit" too ;-)
1206 AddCommand('open', GameCheats);
1207 AddCommand('fly', GameCheats);
1208 AddCommand('noclip', GameCheats);
1209 AddCommand('speedy', GameCheats);
1210 AddCommand('jumpy', GameCheats);
1211 AddCommand('noreload', GameCheats);
1212 AddCommand('aimline', GameCheats);
1213 AddCommand('automap', GameCheats);
1215 AddAction('jump', ACTION_JUMP);
1216 AddAction('moveleft', ACTION_MOVELEFT);
1217 AddAction('moveright', ACTION_MOVERIGHT);
1218 AddAction('lookup', ACTION_LOOKUP);
1219 AddAction('lookdown', ACTION_LOOKDOWN);
1220 AddAction('attack', ACTION_ATTACK);
1221 AddAction('scores', ACTION_SCORES);
1222 AddAction('activate', ACTION_ACTIVATE);
1223 AddAction('strafe', ACTION_STRAFE);
1225 WhitelistCommand('say');
1226 WhitelistCommand('tell');
1227 WhitelistCommand('overtime');
1228 WhitelistCommand('ready');
1229 WhitelistCommand('map');
1230 WhitelistCommand('nextmap');
1231 WhitelistCommand('endmap');
1232 WhitelistCommand('restart');
1233 WhitelistCommand('kick');
1234 WhitelistCommand('kick_pid');
1235 WhitelistCommand('ban');
1236 WhitelistCommand('ban_pid');
1237 WhitelistCommand('centerprint');
1239 WhitelistCommand('addbot');
1240 WhitelistCommand('bot_add');
1241 WhitelistCommand('bot_addred');
1242 WhitelistCommand('bot_addblue');
1243 WhitelistCommand('bot_removeall');
1245 WhitelistCommand('g_gamemode');
1246 WhitelistCommand('g_friendlyfire');
1247 WhitelistCommand('g_friendly_hit_trace');
1248 WhitelistCommand('g_friendly_hit_projectile');
1249 WhitelistCommand('g_friendly_absorb_damage');
1250 WhitelistCommand('g_weaponstay');
1251 WhitelistCommand('g_allow_exit');
1252 WhitelistCommand('g_dm_keys');
1253 WhitelistCommand('g_allow_monsters');
1254 WhitelistCommand('g_bot_vsmonsters');
1255 WhitelistCommand('g_bot_vsplayers');
1256 WhitelistCommand('g_scorelimit');
1257 WhitelistCommand('g_timelimit');
1258 WhitelistCommand('g_maxlives');
1259 WhitelistCommand('g_warmup_time');
1260 WhitelistCommand('g_spawn_invul');
1261 WhitelistCommand('g_item_respawn_time');
1262 WhitelistCommand('g_item_time_random');
1263 WhitelistCommand('g_items_all_respawn_random');
1264 WhitelistCommand('g_items_help_respawn_random');
1265 WhitelistCommand('g_items_ammo_respawn_random');
1266 WhitelistCommand('g_items_weapon_respawn_random');
1267 WhitelistCommand('g_powerup_randomize_respawn');
1268 WhitelistCommand('g_powerup_respawn_time');
1269 WhitelistCommand('g_powerup_time_random');
1271 g_Console_ResetBinds;
1272 g_Console_ReadConfig(gConfigScript);
1273 g_Console_ReadConfig(autoexecScript);
1274 gParsingBinds := False;
1275 end;
1277 procedure g_Console_Init;
1278 begin
1279 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
1280 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
1281 g_Console_Add('');
1282 {$IFDEF HEADLESS}
1283 if ConsoleStdIn then
1284 begin
1285 InitKeyboard();
1286 conbufStdOutRawMode := true;
1287 end;
1288 {$ENDIF}
1289 end;
1291 procedure g_Console_Update;
1293 a, b, Step: Integer;
1294 begin
1295 {$IFDEF HEADLESS}
1296 if ConsoleStdIn then
1297 ReadStdIn();
1298 {$ENDIF}
1300 if Cons_Shown then
1301 begin
1302 Step := Max(1, Round(Floor(gScreenHeight * ConsoleHeight) * ConsoleStep));
1303 if gConsoleShow then
1304 begin
1305 (* Open animation *)
1306 Cons_Y := Min(Cons_Y + Step, 0);
1307 InputReady := True
1309 else
1310 begin
1311 (* Close animation *)
1312 Cons_Y := Max(Cons_Y - Step, -Floor(gScreenHeight * ConsoleHeight));
1313 Cons_Shown := Cons_Y > -Floor(gScreenHeight * ConsoleHeight);
1314 InputReady := False
1315 end;
1317 if gChatShow then
1318 InputReady := True
1319 end;
1321 a := 0;
1322 while a <= High(MsgArray) do
1323 begin
1324 if MsgArray[a].Time > 0 then
1325 begin
1326 if MsgArray[a].Time = 1 then
1327 begin
1328 if a < High(MsgArray) then
1329 begin
1330 for b := a to High(MsgArray)-1 do
1331 MsgArray[b] := MsgArray[b+1];
1333 MsgArray[High(MsgArray)].Time := 0;
1335 a := a - 1;
1336 end;
1338 else
1339 Dec(MsgArray[a].Time);
1340 end;
1342 a := a + 1;
1343 end;
1344 end;
1347 procedure drawConsoleText ();
1349 CWidth, CHeight: Byte;
1350 ty: Integer;
1351 sp, ep: LongWord;
1352 skip: Integer;
1354 procedure putLine (sp, ep: LongWord);
1356 p: LongWord;
1357 wdt, cw: Integer;
1358 begin
1359 p := sp;
1360 wdt := 0;
1361 while p <> ep do
1362 begin
1363 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1364 if wdt+cw > gScreenWidth-8 then break;
1365 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1366 Inc(wdt, cw);
1367 cbufNext(p);
1368 end;
1369 if p <> ep then putLine(p, ep); // do rest of the line first
1370 // now print our part
1371 if skip = 0 then
1372 begin
1373 ep := p;
1374 p := sp;
1375 wdt := 2;
1376 while p <> ep do
1377 begin
1378 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1379 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1380 Inc(wdt, cw);
1381 cbufNext(p);
1382 end;
1383 Dec(ty, CHeight);
1385 else
1386 begin
1387 Dec(skip);
1388 end;
1389 end;
1391 begin
1392 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1393 ty := Floor(gScreenHeight * ConsoleHeight) - 4 - 2 * CHeight - Abs(Cons_Y);
1394 skip := conSkipLines;
1395 cbufLastLine(sp, ep);
1396 repeat
1397 putLine(sp, ep);
1398 if ty+CHeight <= 0 then break;
1399 until not cbufLineUp(sp, ep);
1400 end;
1402 procedure g_Console_Draw(MessagesOnly: Boolean = False);
1404 CWidth, CHeight: Byte;
1405 mfW, mfH: Word;
1406 a, b, offset_y: Integer;
1407 begin
1408 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1410 if ChatTop and gChatShow then
1411 offset_y := CHeight
1412 else
1413 offset_y := 0;
1415 for a := 0 to High(MsgArray) do
1416 if MsgArray[a].Time > 0 then
1417 e_TextureFontPrintFmt(0, offset_y + CHeight * a, MsgArray[a].Msg, gStdFont, True);
1419 if MessagesOnly then Exit;
1421 if gChatShow then
1422 begin
1423 if ChatTop then
1424 offset_y := 0
1425 else
1426 offset_y := gScreenHeight - CHeight - 1;
1427 if gChatTeam then
1428 begin
1429 e_TextureFontPrintEx(0, offset_y, 'say team> ' + Line, gStdFont, 255, 255, 255, 1, True);
1430 e_TextureFontPrintEx((CPos + 9) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1432 else
1433 begin
1434 e_TextureFontPrintEx(0, offset_y, 'say> ' + Line, gStdFont, 255, 255, 255, 1, True);
1435 e_TextureFontPrintEx((CPos + 4) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1437 end;
1439 if not Cons_Shown then
1440 Exit;
1442 if gDebugMode then
1443 begin
1444 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1445 a := (gScreenWidth - 2*mfW) div 2;
1446 b := Cons_Y + (Floor(gScreenHeight * ConsoleHeight) - 2 * mfH) div 2;
1447 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1448 _RGB(128, 0, 0), 2.0);
1449 end;
1451 e_DrawSize(ID, 0, Cons_Y, Round(ConsoleTrans * 255), False, False, gScreenWidth, Floor(gScreenHeight * ConsoleHeight));
1452 e_TextureFontPrint(0, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - CHeight - 4, '> ' + Line, gStdFont);
1454 drawConsoleText();
1456 if ConsoleHistory <> nil then
1457 begin
1458 b := 0;
1459 if CHeight > 0 then
1460 if Length(ConsoleHistory) > (Floor(gScreenHeight * ConsoleHeight) div CHeight) - 1 then
1461 b := Length(ConsoleHistory) - (Floor(gScreenHeight * ConsoleHeight) div CHeight) + 1;
1463 b := Max(b-Offset, 0);
1464 d := Max(High(ConsoleHistory)-Offset, 0);
1466 c := 2;
1467 for a := d downto b do
1468 begin
1469 e_TextureFontPrintFmt(0, Floor(gScreenHeight * ConsoleHeight) - 4 - c * CHeight - Abs(Cons_Y), ConsoleHistory[a], gStdFont, True);
1470 c := c + 1;
1471 end;
1472 end;
1475 e_TextureFontPrint((CPos + 1) * CWidth, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - 21, '_', gStdFont);
1476 end;
1478 procedure g_Console_Char(C: AnsiChar);
1479 begin
1480 if InputReady and (gConsoleShow or gChatShow) then
1481 begin
1482 Insert(C, Line, CPos);
1483 CPos := CPos + 1;
1485 end;
1489 tcomplist: array of AnsiString = nil;
1490 tcompidx: array of Integer = nil;
1492 procedure Complete ();
1494 i, c: Integer;
1495 tused: Integer;
1496 ll, lpfx, cmd: AnsiString;
1497 begin
1498 if (Length(Line) = 0) then
1499 begin
1500 g_Console_Add('');
1501 for i := 0 to High(commands) do
1502 begin
1503 // hidden commands are hidden when cheats aren't enabled
1504 if commands[i].hidden and not conIsCheatsEnabled then continue;
1505 if (Length(commands[i].help) > 0) then
1506 begin
1507 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1509 else
1510 begin
1511 g_Console_Add(' '+commands[i].cmd);
1512 end;
1513 end;
1514 exit;
1515 end;
1517 ll := LowerCase(Line);
1518 lpfx := '';
1520 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1521 begin
1522 ll := Copy(ll, 0, Length(ll)-1);
1523 for i := 0 to High(commands) do
1524 begin
1525 // hidden commands are hidden when cheats aren't enabled
1526 if commands[i].hidden and not conIsCheatsEnabled then continue;
1527 if (commands[i].cmd = ll) then
1528 begin
1529 if (Length(commands[i].help) > 0) then
1530 begin
1531 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1532 end;
1533 end;
1534 end;
1535 exit;
1536 end;
1538 // build completion list
1539 tused := 0;
1540 for i := 0 to High(commands) do
1541 begin
1542 // hidden commands are hidden when cheats aren't enabled
1543 if commands[i].hidden and not conIsCheatsEnabled then continue;
1544 cmd := commands[i].cmd;
1545 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1546 begin
1547 if (tused = Length(tcomplist)) then
1548 begin
1549 SetLength(tcomplist, Length(tcomplist)+128);
1550 SetLength(tcompidx, Length(tcompidx)+128);
1551 end;
1552 tcomplist[tused] := cmd;
1553 tcompidx[tused] := i;
1554 Inc(tused);
1555 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1556 end;
1557 end;
1559 // get longest prefix
1560 for i := 0 to tused-1 do
1561 begin
1562 cmd := tcomplist[i];
1563 for c := 1 to Length(lpfx) do
1564 begin
1565 if (c > Length(cmd)) then break;
1566 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1567 end;
1568 end;
1570 if (tused = 0) then exit;
1572 if (tused = 1) then
1573 begin
1574 Line := tcomplist[0]+' ';
1575 CPos := Length(Line)+1;
1577 else
1578 begin
1579 // has longest prefix?
1580 if (Length(lpfx) > Length(ll)) then
1581 begin
1582 Line := lpfx;
1583 CPos:= Length(Line)+1;
1585 else
1586 begin
1587 g_Console_Add('');
1588 for i := 0 to tused-1 do
1589 begin
1590 if (Length(commands[tcompidx[i]].help) > 0) then
1591 begin
1592 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1594 else
1595 begin
1596 g_Console_Add(' '+tcomplist[i]);
1597 end;
1598 end;
1599 end;
1600 end;
1601 end;
1604 procedure g_Console_Control(K: Word);
1605 begin
1606 case K of
1607 IK_BACKSPACE:
1608 if (Length(Line) > 0) and (CPos > 1) then
1609 begin
1610 Delete(Line, CPos-1, 1);
1611 CPos := CPos-1;
1612 end;
1613 IK_DELETE:
1614 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1615 Delete(Line, CPos, 1);
1616 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1617 if CPos > 1 then
1618 CPos := CPos - 1;
1619 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1620 if CPos <= Length(Line) then
1621 CPos := CPos + 1;
1622 IK_RETURN, IK_KPRETURN, IK_SELECT,
1623 VK_OPEN, VK_FIRE,
1624 JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1625 begin
1626 if gConsoleShow then
1627 g_Console_Process(Line)
1628 else
1629 if gChatShow then
1630 begin
1631 if (Length(Line) > 0) and g_Game_IsNet then
1632 begin
1633 if gChatTeam then
1634 begin
1635 if g_Game_IsClient then
1636 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1637 else
1638 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1639 NET_CHAT_TEAM, gPlayer1Settings.Team);
1641 else
1642 begin
1643 if g_Game_IsClient then
1644 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1645 else
1646 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1647 NET_CHAT_PLAYER);
1648 end;
1649 end;
1651 Line := '';
1652 CPos := 1;
1653 gJustChatted := True;
1654 g_Console_Chat_Switch;
1655 InputReady := False;
1656 end;
1657 end;
1658 IK_TAB:
1659 if not gChatShow then
1660 Complete();
1661 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1662 if not gChatShow then
1663 if (CommandHistory <> nil) and
1664 (CmdIndex < Length(CommandHistory)) then
1665 begin
1666 if CmdIndex < Length(CommandHistory)-1 then
1667 CmdIndex := CmdIndex + 1;
1668 Line := CommandHistory[CmdIndex];
1669 CPos := Length(Line) + 1;
1670 end;
1671 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1672 if not gChatShow then
1673 if (CommandHistory <> nil) and
1674 (CmdIndex <= Length(CommandHistory)) then
1675 begin
1676 if CmdIndex > 0 then
1677 CmdIndex := CmdIndex - 1;
1678 Line := CommandHistory[CmdIndex];
1679 Cpos := Length(Line) + 1;
1680 end;
1681 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1682 if not gChatShow then Inc(conSkipLines);
1683 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1684 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1685 IK_HOME, IK_KPHOME:
1686 CPos := 1;
1687 IK_END, IK_KPEND:
1688 CPos := Length(Line) + 1;
1689 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1690 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, (*IK_EQUALS,*)
1691 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1692 (* see TEXTINPUT event *)
1694 end;
1696 function GetStr(var Str: AnsiString): AnsiString;
1698 a, b: Integer;
1699 begin
1700 Result := '';
1701 if Str[1] = '"' then
1702 begin
1703 for b := 1 to Length(Str) do
1704 if (b = Length(Str)) or (Str[b+1] = '"') then
1705 begin
1706 Result := Copy(Str, 2, b-1);
1707 Delete(Str, 1, b+1);
1708 Str := Trim(Str);
1709 Exit;
1710 end;
1711 end;
1713 for a := 1 to Length(Str) do
1714 if (a = Length(Str)) or (Str[a+1] = ' ') then
1715 begin
1716 Result := Copy(Str, 1, a);
1717 Delete(Str, 1, a+1);
1718 Str := Trim(Str);
1719 Exit;
1720 end;
1721 end;
1723 function ParseString(Str: AnsiString): SSArray;
1724 begin
1725 Result := nil;
1727 Str := Trim(Str);
1729 if Str = '' then
1730 Exit;
1732 while Str <> '' do
1733 begin
1734 SetLength(Result, Length(Result)+1);
1735 Result[High(Result)] := GetStr(Str);
1736 end;
1737 end;
1739 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1741 procedure conmsg (s: AnsiString);
1743 a: Integer;
1744 begin
1745 if length(s) = 0 then exit;
1746 for a := 0 to High(MsgArray) do
1747 begin
1748 with MsgArray[a] do
1749 begin
1750 if Time = 0 then
1751 begin
1752 Msg := s;
1753 Time := MsgTime;
1754 exit;
1755 end;
1756 end;
1757 end;
1758 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1759 with MsgArray[High(MsgArray)] do
1760 begin
1761 Msg := L;
1762 Time := MsgTime;
1763 end;
1764 end;
1767 f: Integer;
1768 begin
1769 // put it to console
1770 cbufPut(L);
1771 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1773 // now show 'em out of console too
1774 show := show and gAllowConsoleMessages;
1775 if show and gShowMessages then
1776 begin
1777 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1778 while length(L) > 0 do
1779 begin
1780 f := Pos(#10, L);
1781 if f <= 0 then f := length(L)+1;
1782 conmsg(Copy(L, 1, f-1));
1783 Delete(L, 1, f);
1784 end;
1785 end;
1787 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1788 //ConsoleHistory[High(ConsoleHistory)] := L;
1791 {$IFDEF HEADLESS}
1792 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1793 {$ENDIF}
1795 end;
1799 consolewriterLastWasEOL: Boolean = false;
1801 procedure consolewriter (constref buf; len: SizeUInt);
1803 b: PByte;
1804 begin
1805 if (len < 1) then exit;
1806 b := PByte(@buf);
1807 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1808 while (len > 0) do
1809 begin
1810 if (b[0] <> 13) and (b[0] <> 10) then
1811 begin
1812 cbufPut(AnsiChar(b[0]));
1814 else
1815 begin
1816 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1817 cbufPut(#10);
1818 end;
1819 len -= 1;
1820 b += 1;
1821 end;
1822 end;
1825 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1826 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1827 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1828 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1829 begin
1830 g_Console_Add(s, show);
1831 end;
1834 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1835 begin
1836 if show then
1837 begin
1838 g_Console_Add(formatstrf(s, args), true);
1840 else
1841 begin
1842 consolewriterLastWasEOL := false;
1843 formatstrf(s, args, consolewriter);
1844 if not consolewriterLastWasEOL then cbufPut(#10);
1845 end;
1846 end;
1849 procedure g_Console_Clear();
1850 begin
1851 //ConsoleHistory := nil;
1852 cbufClear();
1853 conSkipLines := 0;
1854 end;
1856 procedure AddToHistory(L: AnsiString);
1858 len: Integer;
1859 begin
1860 len := Length(CommandHistory);
1862 if (len = 0) or
1863 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1864 begin
1865 SetLength(CommandHistory, len+1);
1866 CommandHistory[len] := L;
1867 end;
1869 CmdIndex := Length(CommandHistory);
1870 end;
1872 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1874 Arr: SSArray;
1875 i: Integer;
1876 begin
1877 Result := True;
1879 Arr := nil;
1881 if Trim(C) = '' then
1882 Exit;
1884 Arr := ParseString(C);
1885 if Arr = nil then
1886 Exit;
1888 for i := 0 to High(Whitelist) do
1889 if Whitelist[i] = LowerCase(Arr[0]) then
1890 Result := False;
1891 end;
1893 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1895 Arr: SSArray;
1896 i: Integer;
1897 begin
1898 Arr := nil;
1900 if Trim(L) = '' then
1901 Exit;
1903 conSkipLines := 0; // "unscroll"
1905 if L = 'goobers' then
1906 begin
1907 Line := '';
1908 CPos := 1;
1909 gCheats := true;
1910 g_Console_Add('Your memory serves you well.');
1911 exit;
1912 end;
1914 if not quiet then
1915 begin
1916 g_Console_Add('> '+L);
1917 Line := '';
1918 CPos := 1;
1919 end;
1921 Arr := ParseString(L);
1922 if Arr = nil then
1923 Exit;
1925 if commands = nil then
1926 Exit;
1928 if not quiet then
1929 AddToHistory(L);
1931 for i := 0 to High(commands) do
1932 begin
1933 if commands[i].cmd = LowerCase(Arr[0]) then
1934 begin
1935 if commands[i].action >= 0 then
1936 begin
1937 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1938 exit
1939 end;
1940 if assigned(commands[i].procEx) then
1941 begin
1942 commands[i].procEx(@commands[i], Arr);
1943 exit
1944 end;
1945 if assigned(commands[i].proc) then
1946 begin
1947 commands[i].proc(Arr);
1948 exit
1951 end;
1953 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1954 end;
1957 function g_Console_Interactive: Boolean;
1958 begin
1959 Result := gConsoleShow
1960 end;
1962 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = ''; rep: Boolean = False);
1963 begin
1964 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1965 ASSERT(key >= 0);
1966 ASSERT(key < e_MaxInputKeys);
1967 if key > 0 then
1968 begin
1969 gInputBinds[key].rep := rep;
1970 gInputBinds[key].down := ParseAlias(down);
1971 gInputBinds[key].up := ParseAlias(up);
1972 end;
1973 g_Console_WriteGameConfig();
1974 end;
1976 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1978 function EqualsCommandLists (a, b: SSArray): Boolean;
1979 var i, len: Integer;
1980 begin
1981 result := False;
1982 len := Length(a);
1983 if len = Length(b) then
1984 begin
1985 i := 0;
1986 while (i < len) and (a[i] = b[i]) do inc(i);
1987 if i >= len then
1988 result := True
1990 end;
1992 begin
1993 ASSERT(key >= 0);
1994 ASSERT(key < e_MaxInputKeys);
1995 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1996 end;
1998 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1999 var i: Integer;
2000 begin
2001 ASSERT(n >= 1);
2002 result := 0;
2003 if commands = nil then Exit;
2004 i := 0;
2005 while (n >= 1) and (i < e_MaxInputKeys) do
2006 begin
2007 if (i < VK_FIRSTKEY) or (i > VK_LASTKEY) then (* never show virtual keys in gui *)
2008 begin
2009 if g_Console_MatchBind(i, down, up) then
2010 begin
2011 result := i;
2012 dec(n)
2013 end;
2014 end;
2015 inc(i)
2016 end;
2017 if n >= 1 then
2018 result := 0
2019 end;
2021 function g_Console_Action (action: Integer): Boolean;
2022 var i, len: Integer;
2023 begin
2024 ASSERT(action >= FIRST_ACTION);
2025 ASSERT(action <= LAST_ACTION);
2026 i := 0;
2027 len := Length(gPlayerAction);
2028 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
2029 Result := i < len
2030 end;
2032 function BindsAllowed (key: Integer): Boolean;
2033 begin
2034 Result := False;
2035 if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
2036 begin
2037 if gChatShow then
2038 Result := g_Console_MatchBind(key, 'togglemenu') or
2039 g_Console_MatchBind(key, 'showkeyboard') or
2040 g_Console_MatchBind(key, 'hidekeyboard')
2041 else if gConsoleShow or (g_ActiveWindow <> nil) or (gGameSettings.GameType = GT_NONE) then
2042 Result := g_Console_MatchBind(key, 'togglemenu') or
2043 g_Console_MatchBind(key, 'toggleconsole') or
2044 g_Console_MatchBind(key, 'showkeyboard') or
2045 g_Console_MatchBind(key, 'hidekeyboard')
2046 else (* in game *)
2047 Result := True
2049 end;
2051 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
2052 var i: Integer;
2053 begin
2054 if BindsAllowed(key) then
2055 begin
2056 if down then
2057 for i := 0 to High(gInputBinds[key].down) do
2058 g_Console_Process(gInputBinds[key].down[i], True)
2059 else
2060 for i := 0 to High(gInputBinds[key].up) do
2061 g_Console_Process(gInputBinds[key].up[i], True)
2062 end;
2063 if down and not menu_toggled then
2064 KeyPress(key);
2065 menu_toggled := False
2066 end;
2068 procedure g_Console_ProcessBindRepeat (key: Integer);
2069 var i: Integer;
2070 begin
2071 if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
2072 begin
2073 KeyPress(key); // key repeat in menus and shit
2074 Exit;
2075 end;
2076 if BindsAllowed(key) and gInputBinds[key].rep then
2077 begin
2078 for i := 0 to High(gInputBinds[key].down) do
2079 g_Console_Process(gInputBinds[key].down[i], True);
2080 end;
2081 end;
2083 procedure g_Console_ResetBinds;
2084 var i: Integer;
2085 begin
2086 for i := 0 to e_MaxInputKeys - 1 do
2087 g_Console_BindKey(i, '', '');
2089 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
2090 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
2091 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
2092 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
2093 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
2094 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
2095 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
2096 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
2097 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
2098 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
2099 g_Console_BindKey(IK_E, 'p1_weapnext', '', True);
2100 g_Console_BindKey(IK_Q, 'p1_weapprev', '', True);
2101 g_Console_BindKey(IK_R, 'p1_dropflag', '');
2102 g_Console_BindKey(IK_1, 'p1_weapon 1');
2103 g_Console_BindKey(IK_2, 'p1_weapon 2');
2104 g_Console_BindKey(IK_3, 'p1_weapon 3');
2105 g_Console_BindKey(IK_4, 'p1_weapon 4');
2106 g_Console_BindKey(IK_5, 'p1_weapon 5');
2107 g_Console_BindKey(IK_6, 'p1_weapon 6');
2108 g_Console_BindKey(IK_7, 'p1_weapon 7');
2109 g_Console_BindKey(IK_8, 'p1_weapon 8');
2110 g_Console_BindKey(IK_9, 'p1_weapon 9');
2111 g_Console_BindKey(IK_0, 'p1_weapon 10');
2112 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
2113 g_Console_BindKey(IK_T, 'togglechat');
2114 g_Console_BindKey(IK_Y, 'toggleteamchat');
2115 g_Console_BindKey(IK_F11, 'screenshot');
2116 g_Console_BindKey(IK_TAB, '+scores', '-scores');
2117 g_Console_BindKey(IK_PAUSE, 'pause');
2118 g_Console_BindKey(IK_F1, 'vote');
2120 (* for i := 0 to e_MaxJoys - 1 do *)
2121 for i := 0 to 1 do
2122 begin
2123 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
2124 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
2125 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
2126 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
2127 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
2128 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
2129 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
2130 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
2131 g_Console_BindKey(e_JoyButtonToKey(i, 1), 'p' + IntToStr(i mod 2 + 1) + '_weapnext', '', True);
2132 g_Console_BindKey(e_JoyButtonToKey(i, 4), 'p' + IntToStr(i mod 2 + 1) + '_weapprev', '', True);
2133 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
2134 end;
2136 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
2137 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
2138 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
2139 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
2140 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
2141 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
2142 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
2143 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
2144 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
2145 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
2146 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
2147 g_Console_BindKey(VK_NEXT, 'weapnext', '', True);
2148 g_Console_BindKey(VK_PREV, 'weapprev', '', True);
2149 g_Console_BindKey(VK_0, 'weapon 1');
2150 g_Console_BindKey(VK_1, 'weapon 2');
2151 g_Console_BindKey(VK_2, 'weapon 3');
2152 g_Console_BindKey(VK_3, 'weapon 4');
2153 g_Console_BindKey(VK_4, 'weapon 5');
2154 g_Console_BindKey(VK_5, 'weapon 6');
2155 g_Console_BindKey(VK_6, 'weapon 7');
2156 g_Console_BindKey(VK_7, 'weapon 8');
2157 g_Console_BindKey(VK_8, 'weapon 9');
2158 g_Console_BindKey(VK_9, 'weapon 10');
2159 g_Console_BindKey(VK_A, 'weapon 11');
2160 g_Console_BindKey(VK_CHAT, 'togglechat');
2161 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
2162 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
2163 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
2164 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
2165 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
2166 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
2167 end;
2169 procedure g_Console_ReadConfig (filename: String);
2170 var f: TextFile; s: AnsiString; i, len: Integer;
2171 begin
2172 e_LogWritefln('g_Console_ReadConfig (1) "%s"', [filename]);
2173 if e_FindResource(ConfigDirs, filename, false) = true then
2174 begin
2175 e_LogWritefln('g_Console_ReadConfig (2) "%s"', [filename]);
2176 AssignFile(f, filename);
2177 Reset(f);
2178 while not EOF(f) do
2179 begin
2180 ReadLn(f, s);
2181 len := Length(s);
2182 if len > 0 then
2183 begin
2184 i := 1;
2185 (* skip spaces *)
2186 while (i <= len) and (s[i] <= ' ') do inc(i);
2187 (* skip comments *)
2188 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
2189 g_Console_Process(s, True);
2191 end;
2192 CloseFile(f);
2194 end;
2196 procedure g_Console_WriteConfig (filename: String);
2197 var f: TextFile; i, j: Integer;
2199 procedure WriteFlag(name: string; flag: TGameOption);
2200 begin
2201 WriteLn(f, name, Ord(flag in gsGameFlags));
2202 end;
2204 function FormatTeam(team: Byte): string;
2205 begin
2206 if team = TEAM_BLUE then
2207 result := 'blue'
2208 else
2209 result := 'red';
2210 end;
2212 begin
2213 AssignFile(f, filename);
2214 Rewrite(f);
2215 WriteLn(f, '// ' + configComment);
2217 // binds
2218 WriteLn(f, 'unbindall');
2219 for i := 0 to e_MaxInputKeys - 1 do
2220 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
2221 begin
2222 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
2223 if Length(gInputBinds[i].down) = 0 then
2224 Write(f, '""');
2225 if Length(gInputBinds[i].up) > 0 then
2226 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
2227 WriteLn(f, '');
2228 if gInputBinds[i].rep then
2229 WriteLn(f, 'bindrep ', e_KeyNames[i]);
2230 end;
2232 // lang
2233 if gAskLanguage then
2234 WriteLn(f, 'g_language ask')
2235 else
2236 WriteLn(f, 'g_language ', gLanguage);
2238 // net server
2239 WriteLn(f, 'sv_name ', QuoteStr(NetServerName));
2240 WriteLn(f, 'sv_passwd ', QuoteStr(NetPassword));
2241 WriteLn(f, 'sv_maxplrs ', NetMaxClients);
2242 WriteLn(f, 'sv_port ', NetPort);
2243 WriteLn(f, 'sv_public ', IfThen(NetUseMaster, 1, 0));
2245 // game settings
2246 WriteLn(f, 'g_max_particles ', g_GFX_GetMax());
2247 WriteLn(f, 'g_max_shells ', g_Shells_GetMax());
2248 WriteLn(f, 'g_max_gibs ', g_Gibs_GetMax());
2249 WriteLn(f, 'g_max_corpses ', g_Corpses_GetMax());
2250 WriteLn(f, 'g_force_model ', g_Force_Model_Get());
2251 WriteLn(f, 'g_force_model_name ', g_Forced_Model_GetName());
2252 WriteLn(f, 'sv_intertime ', gDefInterTime);
2254 // gameplay settings
2255 WriteLn(f, 'g_gamemode ', gsGameMode);
2256 WriteLn(f, 'g_scorelimit ', gsScoreLimit);
2257 WriteLn(f, 'g_timelimit ', gsTimeLimit);
2258 WriteLn(f, 'g_maxlives ', gsMaxLives);
2259 WriteLn(f, 'g_item_respawn_time ', gsItemRespawnTime);
2260 WriteLn(f, 'g_item_time_random ', gsItemRespawnRandom);
2261 WriteLn(f, 'g_powerup_respawn_time ', gsPowerupRespawnTime);
2262 WriteLn(f, 'g_powerup_time_random ', gsPowerupRespawnRandom);
2263 WriteLn(f, 'g_spawn_invul ', gsSpawnInvul);
2264 WriteLn(f, 'g_warmup_time ', gsWarmupTime);
2266 WriteFlag('g_friendlyfire ', TGameOption.TEAM_DAMAGE);
2267 WriteFlag('g_friendly_hit_trace ', TGameOption.TEAM_HIT_TRACE);
2268 WriteFlag('g_friendly_hit_projectile ', TGameOption.TEAM_HIT_PROJECTILE);
2269 WriteFlag('g_powerup_randomize_respawn ', TGameOption.POWERUP_RANDOM);
2270 WriteFlag('g_items_all_respawn_random ', TGameOption.ITEM_ALL_RANDOM);
2271 WriteFlag('g_items_help_respawn_random ', TGameOption.ITEM_LIFE_RANDOM);
2272 WriteFlag('g_items_ammo_respawn_random ', TGameOption.ITEM_AMMO_RANDOM);
2273 WriteFlag('g_items_weapon_respawn_random ', TGameOption.ITEM_WEAPON_RANDOM);
2274 WriteFlag('g_allow_exit ', TGameOption.ALLOW_EXIT);
2275 WriteFlag('g_allow_monsters ', TGameOption.MONSTERS);
2276 WriteFlag('g_allow_dropflag ', TGameOption.ALLOW_DROP_FLAG);
2277 WriteFlag('g_throw_flag ', TGameOption.THROW_FLAG);
2278 WriteFlag('g_dm_keys ', TGameOption.DM_KEYS);
2279 WriteFlag('g_weaponstay ', TGameOption.WEAPONS_STAY);
2280 WriteFlag('g_bot_vsmonsters ', TGameOption.BOTS_VS_MONSTERS);
2281 WriteFlag('g_bot_vsplayers ', TGameOption.BOTS_VS_PLAYERS);
2283 // players
2284 with gPlayer1Settings do
2285 begin
2286 WriteLn(f, 'p1_name ', QuoteStr(Name));
2287 WriteLn(f, 'p1_color ', Color.R, ' ', Color.G, ' ', Color.B);
2288 WriteLn(f, 'p1_model ', QuoteStr(Model));
2289 WriteLn(f, 'p1_team ', FormatTeam(Team));
2290 WriteLn(f, 'p1_autoswitch ', WeaponSwitch);
2291 WriteLn(f, 'p1_switch_empty ', SwitchToEmpty);
2292 WriteLn(f, 'p1_priority_ironfist ', Max(0, WeaponPreferences[WEAPON_IRONFIST]));
2293 WriteLn(f, 'p1_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2294 WriteLn(f, 'p1_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2295 WriteLn(f, 'p1_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2296 WriteLn(f, 'p1_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN2]));
2297 WriteLn(f, 'p1_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2298 WriteLn(f, 'p1_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2299 WriteLn(f, 'p1_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2300 WriteLn(f, 'p1_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2301 WriteLn(f, 'p1_priority_superchaingun ', Max(0, WeaponPreferences[WEAPON_SUPERCHAINGUN]));
2302 WriteLn(f, 'p1_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2303 WriteLn(f, 'p1_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2304 end;
2305 with gPlayer2Settings do
2306 begin
2307 WriteLn(f, 'p2_name ', QuoteStr(Name));
2308 WriteLn(f, 'p2_color ', Color.R, ' ', Color.G, ' ', Color.B);
2309 WriteLn(f, 'p2_model ', QuoteStr(Model));
2310 WriteLn(f, 'p2_team ', FormatTeam(Team));
2311 WriteLn(f, 'p2_autoswitch ', WeaponSwitch);
2312 WriteLn(f, 'p2_switch_empty ', SwitchToEmpty);
2313 WriteLn(f, 'p2_priority_ironfist ', Max(0, WeaponPreferences[WEAPON_IRONFIST]));
2314 WriteLn(f, 'p2_priority_saw ', Max(0, WeaponPreferences[WEAPON_SAW]));
2315 WriteLn(f, 'p2_priority_pistol ', Max(0, WeaponPreferences[WEAPON_PISTOL]));
2316 WriteLn(f, 'p2_priority_shotgun1 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN1]));
2317 WriteLn(f, 'p2_priority_shotgun2 ', Max(0, WeaponPreferences[WEAPON_SHOTGUN2]));
2318 WriteLn(f, 'p2_priority_chaingun ', Max(0, WeaponPreferences[WEAPON_CHAINGUN]));
2319 WriteLn(f, 'p2_priority_rocketlauncher ', Max(0, WeaponPreferences[WEAPON_ROCKETLAUNCHER]));
2320 WriteLn(f, 'p2_priority_plasma ', Max(0, WeaponPreferences[WEAPON_PLASMA]));
2321 WriteLn(f, 'p2_priority_bfg ', Max(0, WeaponPreferences[WEAPON_BFG]));
2322 WriteLn(f, 'p2_priority_superchaingun ', Max(0, WeaponPreferences[WEAPON_SUPERCHAINGUN]));
2323 WriteLn(f, 'p2_priority_flamethrower ', Max(0, WeaponPreferences[WEAPON_FLAMETHROWER]));
2324 WriteLn(f, 'p2_priority_berserk ', Max(0, WeaponPreferences[WP_LAST+1]));
2325 end;
2327 // all cvars
2328 for i := 0 to High(commands) do
2329 begin
2330 if not commands[i].cheat then
2331 begin
2332 if @commands[i].procEx = @boolVarHandler then
2333 begin
2334 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
2335 WriteLn(f, commands[i].cmd, ' ', j)
2337 else if @commands[i].procEx = @intVarHandler then
2338 begin
2339 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
2341 else if @commands[i].procEx = @wordVarHandler then
2342 begin
2343 WriteLn(f, commands[i].cmd, ' ', PWord(commands[i].ptr)^)
2345 else if @commands[i].procEx = @dwordVarHandler then
2346 begin
2347 WriteLn(f, commands[i].cmd, ' ', PCardinal(commands[i].ptr)^)
2349 else if @commands[i].procEx = @singleVarHandler then
2350 begin
2351 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
2353 else if @commands[i].procEx = @strVarHandler then
2354 begin
2355 if Length(PAnsiString(commands[i].ptr)^) = 0 then
2356 WriteLn(f, commands[i].cmd, ' ""')
2357 else
2358 WriteLn(f, commands[i].cmd, ' ', QuoteStr(PAnsiString(commands[i].ptr)^))
2361 end;
2363 WriteLn(f, 'r_maxfps ', gMaxFPS);
2364 WriteLn(f, 'r_reset');
2365 CloseFile(f)
2366 end;
2368 procedure g_Console_WriteGameConfig();
2370 s: AnsiString;
2371 begin
2372 if gParsingBinds then
2373 Exit;
2375 s := e_GetWriteableDir(ConfigDirs);
2377 // FIXME: hack for improper ConcatPaths(); see commit.
2378 if s = ''
2379 then s := gConfigScript
2380 else s := ConcatPaths([s, gConfigScript]);
2382 g_Console_WriteConfig(s);
2383 end;
2385 procedure Init();
2387 i: Integer;
2388 begin
2389 conRegVar('chat_at_top', @ChatTop, 'draw chat at top border', 'draw chat at top border');
2390 conRegVar('console_height', @ConsoleHeight, 0.0, 1.0, 'set console size', 'set console size');
2391 conRegVar('console_trans', @ConsoleTrans, 0.0, 1.0, 'set console transparency', 'set console transparency');
2392 conRegVar('console_step', @ConsoleStep, 0.0, 1.0, 'set console animation speed', 'set console animation speed');
2393 conRegVar('console_stdin', @ConsoleStdIn, 'enable reading commands from stdin', 'enable reading commands from stdin');
2394 {$IFDEF ANDROID}
2395 ChatTop := True;
2396 ConsoleHeight := 0.35;
2397 {$ELSE}
2398 ChatTop := False;
2399 ConsoleHeight := 0.5;
2400 {$ENDIF}
2401 ConsoleTrans := 0.1;
2402 ConsoleStep := 0.07;
2403 {$IFDEF HEADLESS}
2404 ConsoleStdIn := True;
2405 {$ELSE}
2406 ConsoleStdIn := False;
2407 {$ENDIF}
2408 conRegVar('d_eres', @debug_e_res, '', '');
2409 for i := 1 to e_MaxJoys do
2410 conRegVar('joy' + IntToStr(i) + '_deadzone', @e_JoystickDeadzones[i - 1], '', '')
2411 end;
2413 procedure Cleanup();
2415 C: TCommand;
2416 begin
2417 for C in commands do
2418 if @C.procEx = @singleVarHandler then
2419 FreeMem(C.ptr);
2420 end;
2422 initialization
2423 Init();
2425 finalization
2426 {$IFDEF HEADLESS}
2427 DoneKeyboard;
2428 conbufStdOutRawMode := false;
2429 {$ENDIF}
2430 Cleanup();
2432 end.