DEADSOFTWARE

d3059549107a05b8fa58b92df8bbb916880f7109
[d2df-sdl.git] / src / game / g_console.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_console;
18 interface
20 uses
21 utils; // for SSArray
23 const
24 ACTION_JUMP = 0;
25 ACTION_MOVELEFT = 1;
26 ACTION_MOVERIGHT = 2;
27 ACTION_LOOKDOWN = 3;
28 ACTION_LOOKUP = 4;
29 ACTION_ATTACK = 5;
30 ACTION_SCORES = 6;
31 ACTION_ACTIVATE = 7;
32 ACTION_STRAFE = 8;
33 ACTION_WEAPNEXT = 9;
34 ACTION_WEAPPREV = 10;
36 FIRST_ACTION = ACTION_JUMP;
37 LAST_ACTION = ACTION_WEAPPREV;
39 procedure g_Console_Init;
40 procedure g_Console_SysInit;
41 procedure g_Console_Update;
42 procedure g_Console_Draw (MessagesOnly: Boolean = False);
43 procedure g_Console_Char (C: AnsiChar);
44 procedure g_Console_Control (K: Word);
45 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
46 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
47 procedure g_Console_Clear;
48 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
49 procedure g_Console_ReadConfig (filename: String);
50 procedure g_Console_WriteConfig (filename: String);
51 procedure g_Console_WriteGameConfig;
53 function g_Console_Interactive: Boolean;
54 function g_Console_Action (action: Integer): Boolean;
55 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
56 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
57 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
58 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
59 procedure g_Console_ResetBinds;
61 procedure conwriteln (const s: AnsiString; show: Boolean=false);
62 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
64 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
65 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
66 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
67 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
68 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
69 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
71 // <0: no arg; 0/1: true/false
72 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
74 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
75 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
77 const
78 defaultConfigScript = 'dfconfig.cfg';
80 var
81 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
82 gChatShow: Boolean = false;
83 gChatTeam: Boolean = false;
84 gAllowConsoleMessages: Boolean = true;
85 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
86 gParsingBinds: Boolean = true; // íå ïåðåñîõðàíÿòü êîíôèã âî âðåìÿ ïàðñèíãà
87 gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
88 gConfigScript: string = defaultConfigScript;
90 implementation
92 uses
93 g_textures, g_main, e_graphics, e_input, g_game, g_gfx, g_player, g_items,
94 SysUtils, g_basic, g_options, Math, g_touch, e_res,
95 g_menu, g_gui, g_language, g_net, g_netmsg, e_log, conbuf;
97 const
98 autoexecScript = 'autoexec.cfg';
99 configComment = 'generated by doom2d, do not modify';
101 type
102 PCommand = ^TCommand;
104 TCmdProc = procedure (p: SSArray);
105 TCmdProcEx = procedure (me: PCommand; p: SSArray);
107 TCommand = record
108 cmd: AnsiString;
109 proc: TCmdProc;
110 procEx: TCmdProcEx;
111 help: AnsiString;
112 hidden: Boolean;
113 ptr: Pointer; // various data
114 msg: AnsiString; // message for var changes
115 cheat: Boolean;
116 action: Integer; // >= 0 for action commands
117 player: Integer; // used for action commands
118 end;
120 TAlias = record
121 name: AnsiString;
122 commands: SSArray;
123 end;
126 const
127 MsgTime = 144;
128 MaxScriptRecursion = 16;
130 DEBUG_STRING = 'DEBUG MODE';
132 var
133 ID: DWORD;
134 RecursionDepth: Word = 0;
135 RecursionLimitHit: Boolean = False;
136 Cons_Y: SmallInt;
137 ConsoleHeight: Single;
138 Cons_Shown: Boolean; // draw console
139 InputReady: Boolean; // allow text input in console/chat
140 Line: AnsiString;
141 CPos: Word;
142 //ConsoleHistory: SSArray;
143 CommandHistory: SSArray;
144 Whitelist: SSArray;
145 commands: Array of TCommand = nil;
146 Aliases: Array of TAlias = nil;
147 CmdIndex: Word;
148 conSkipLines: Integer = 0;
149 MsgArray: Array [0..4] of record
150 Msg: AnsiString;
151 Time: Word;
152 end;
154 gInputBinds: Array [0..e_MaxInputKeys - 1] of record
155 down, up: SSArray;
156 end;
157 menu_toggled: BOOLEAN; (* hack for menu controls *)
158 ChatTop: BOOLEAN;
159 ConsoleStep: Single;
160 ConsoleTrans: Single;
163 procedure g_Console_Switch;
164 begin
165 Cons_Y := Min(0, Max(Cons_Y, -Floor(gScreenHeight * ConsoleHeight)));
166 if Cons_Shown = False then
167 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
168 gChatShow := False;
169 gConsoleShow := not gConsoleShow;
170 Cons_Shown := True;
171 InputReady := False;
172 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
173 end;
175 procedure g_Console_Chat_Switch (Team: Boolean = False);
176 begin
177 if not g_Game_IsNet then Exit;
178 Cons_Y := Min(0, Max(Cons_Y, -Floor(gScreenHeight * ConsoleHeight)));
179 if Cons_Shown = False then
180 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
181 gConsoleShow := False;
182 gChatShow := not gChatShow;
183 gChatTeam := Team;
184 Cons_Shown := True;
185 InputReady := False;
186 Line := '';
187 CPos := 1;
188 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
189 end;
191 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
192 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
193 var
194 pos: Integer = 1;
195 frac: Single = 1;
196 slen: Integer;
197 begin
198 result := false;
199 res := 0;
200 slen := Length(s);
201 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
202 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
203 if (pos > slen) then exit;
204 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
205 // integral part
206 while (pos <= slen) do
207 begin
208 if (s[pos] < '0') or (s[pos] > '9') then break;
209 res := res*10+Byte(s[pos])-48;
210 Inc(pos);
211 end;
212 if (pos <= slen) then
213 begin
214 // must be a dot
215 if (s[pos] <> '.') then exit;
216 Inc(pos);
217 while (pos <= slen) do
218 begin
219 if (s[pos] < '0') or (s[pos] > '9') then break;
220 frac := frac/10;
221 res += frac*(Byte(s[pos])-48);
222 Inc(pos);
223 end;
224 end;
225 if (pos <= slen) then exit; // oops
226 result := true;
227 end;
230 // ////////////////////////////////////////////////////////////////////////// //
231 // <0: no arg; 0/1: true/false; 666: toggle
232 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
233 begin
234 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
235 result := 0;
236 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
237 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
238 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
239 (CompareText(p[idx], 't') = 0) then result := 666;
240 end;
243 procedure boolVarHandler (me: PCommand; p: SSArray);
244 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
245 var
246 old: Boolean;
247 begin
248 if (Length(p) > 2) then
249 begin
250 conwritefln('too many arguments to ''%s''', [p[0]]);
251 end
252 else
253 begin
254 old := flag;
255 case conGetBoolArg(p, 1) of
256 -1: begin end;
257 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
258 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
259 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
260 end;
261 if flag <> old then
262 g_Console_WriteGameConfig();
263 if (Length(msg) = 0) then msg := p[0] else msg += ':';
264 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
265 end;
266 end;
267 begin
268 binaryFlag(PBoolean(me.ptr)^, me.msg);
269 end;
272 procedure intVarHandler (me: PCommand; p: SSArray);
273 var
274 old: Integer;
275 begin
276 if (Length(p) <> 2) then
277 begin
278 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
279 end
280 else
281 begin
282 try
283 old := PInteger(me.ptr)^;
284 PInteger(me.ptr)^ := StrToInt(p[1]);
285 if PInteger(me.ptr)^ <> old then
286 g_Console_WriteGameConfig();
287 except
288 conwritefln('invalid integer value: "%s"', [p[1]]);
289 end;
290 end;
291 end;
294 procedure wordVarHandler (me: PCommand; p: SSArray);
295 var
296 old: Integer;
297 begin
298 if (Length(p) <> 2) then
299 begin
300 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
301 end
302 else
303 begin
304 try
305 old := PWord(me.ptr)^;
306 PWord(me.ptr)^ := min($FFFF, StrToDWord(p[1]));
307 if PWord(me.ptr)^ <> old then
308 g_Console_WriteGameConfig();
309 except
310 conwritefln('invalid word value: "%s"', [p[1]]);
311 end;
312 end;
313 end;
316 procedure dwordVarHandler (me: PCommand; p: SSArray);
317 var
318 old: Integer;
319 begin
320 if (Length(p) <> 2) then
321 begin
322 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
323 end
324 else
325 begin
326 try
327 old := PCardinal(me.ptr)^;
328 PCardinal(me.ptr)^ := StrToDWord(p[1]);
329 if PCardinal(me.ptr)^ <> old then
330 g_Console_WriteGameConfig();
331 except
332 conwritefln('invalid dword value: "%s"', [p[1]]);
333 end;
334 end;
335 end;
338 procedure strVarHandler (me: PCommand; p: SSArray);
339 var
340 old: AnsiString;
341 begin
342 if (Length(p) <> 2) then
343 begin
344 conwritefln('%s %s', [me.cmd, QuoteStr(PAnsiString(me.ptr)^)]);
345 end
346 else
347 begin
348 old := PAnsiString(me.ptr)^;
349 PAnsiString(me.ptr)^ := p[1];
350 if PAnsiString(me.ptr)^ <> old then
351 g_Console_WriteGameConfig();
352 end;
353 end;
356 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
357 var
358 f: Integer;
359 cp: PCommand;
360 begin
361 f := Length(commands);
362 SetLength(commands, f+1);
363 cp := @commands[f];
364 cp.cmd := LowerCase(conname);
365 cp.proc := nil;
366 cp.procEx := boolVarHandler;
367 cp.help := ahelp;
368 cp.hidden := ahidden;
369 cp.ptr := pvar;
370 cp.msg := amsg;
371 cp.cheat := acheat;
372 cp.action := -1;
373 cp.player := -1;
374 end;
377 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
378 var
379 f: Integer;
380 cp: PCommand;
381 begin
382 f := Length(commands);
383 SetLength(commands, f+1);
384 cp := @commands[f];
385 cp.cmd := LowerCase(conname);
386 cp.proc := nil;
387 cp.procEx := intVarHandler;
388 cp.help := ahelp;
389 cp.hidden := ahidden;
390 cp.ptr := pvar;
391 cp.msg := amsg;
392 cp.cheat := acheat;
393 cp.action := -1;
394 cp.player := -1;
395 end;
398 procedure conRegVar (const conname: AnsiString; pvar: PWord; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
399 var
400 f: Integer;
401 cp: PCommand;
402 begin
403 f := Length(commands);
404 SetLength(commands, f+1);
405 cp := @commands[f];
406 cp.cmd := LowerCase(conname);
407 cp.proc := nil;
408 cp.procEx := wordVarHandler;
409 cp.help := ahelp;
410 cp.hidden := ahidden;
411 cp.ptr := pvar;
412 cp.msg := amsg;
413 cp.cheat := acheat;
414 cp.action := -1;
415 cp.player := -1;
416 end;
419 procedure conRegVar (const conname: AnsiString; pvar: PCardinal; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
420 var
421 f: Integer;
422 cp: PCommand;
423 begin
424 f := Length(commands);
425 SetLength(commands, f+1);
426 cp := @commands[f];
427 cp.cmd := LowerCase(conname);
428 cp.proc := nil;
429 cp.procEx := dwordVarHandler;
430 cp.help := ahelp;
431 cp.hidden := ahidden;
432 cp.ptr := pvar;
433 cp.msg := amsg;
434 cp.cheat := acheat;
435 cp.action := -1;
436 cp.player := -1;
437 end;
440 procedure conRegVar (const conname: AnsiString; pvar: PAnsiString; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
441 var
442 f: Integer;
443 cp: PCommand;
444 begin
445 f := Length(commands);
446 SetLength(commands, f+1);
447 cp := @commands[f];
448 cp.cmd := LowerCase(conname);
449 cp.proc := nil;
450 cp.procEx := strVarHandler;
451 cp.help := ahelp;
452 cp.hidden := ahidden;
453 cp.ptr := pvar;
454 cp.msg := amsg;
455 cp.cheat := acheat;
456 cp.action := -1;
457 cp.player := -1;
458 end;
460 // ////////////////////////////////////////////////////////////////////////// //
461 type
462 PVarSingle = ^TVarSingle;
463 TVarSingle = record
464 val: PSingle;
465 min, max, def: Single; // default will be starting value
466 end;
469 procedure singleVarHandler (me: PCommand; p: SSArray);
470 var
471 pv: PVarSingle;
472 nv, old: Single;
473 msg: AnsiString;
474 begin
475 if (Length(p) > 2) then
476 begin
477 conwritefln('too many arguments to ''%s''', [me.cmd]);
478 exit;
479 end;
480 pv := PVarSingle(me.ptr);
481 old := pv.val^;
482 if (Length(p) = 2) then
483 begin
484 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
485 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
486 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
487 (CompareText(p[1], 'ona') = 0) then
488 begin
489 pv.val^ := pv.def;
490 end
491 else
492 begin
493 if not conParseFloat(nv, p[1]) then
494 begin
495 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
496 exit;
497 end;
498 if (nv < pv.min) then nv := pv.min;
499 if (nv > pv.max) then nv := pv.max;
500 pv.val^ := nv;
501 end;
502 end;
503 if pv.val^ <> old then
504 g_Console_WriteGameConfig();
505 msg := me.msg;
506 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
507 conwritefln('%s %s', [msg, pv.val^]);
508 end;
511 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
512 var
513 f: Integer;
514 cp: PCommand;
515 pv: PVarSingle;
516 begin
517 GetMem(pv, sizeof(TVarSingle));
518 pv.val := pvar;
519 pv.min := amin;
520 pv.max := amax;
521 pv.def := pvar^;
522 f := Length(commands);
523 SetLength(commands, f+1);
524 cp := @commands[f];
525 cp.cmd := LowerCase(conname);
526 cp.proc := nil;
527 cp.procEx := singleVarHandler;
528 cp.help := ahelp;
529 cp.hidden := ahidden;
530 cp.ptr := pv;
531 cp.msg := amsg;
532 cp.cheat := acheat;
533 cp.action := -1;
534 cp.player := -1;
535 end;
538 // ////////////////////////////////////////////////////////////////////////// //
539 function GetStrACmd(var Str: AnsiString): AnsiString;
540 var
541 a: Integer;
542 begin
543 Result := '';
544 for a := 1 to Length(Str) do
545 if (a = Length(Str)) or (Str[a+1] = ';') then
546 begin
547 Result := Copy(Str, 1, a);
548 Delete(Str, 1, a+1);
549 Str := Trim(Str);
550 Exit;
551 end;
552 end;
554 function ParseAlias(Str: AnsiString): SSArray;
555 begin
556 Result := nil;
558 Str := Trim(Str);
560 if Str = '' then
561 Exit;
563 while Str <> '' do
564 begin
565 SetLength(Result, Length(Result)+1);
566 Result[High(Result)] := GetStrACmd(Str);
567 end;
568 end;
570 procedure ConsoleCommands(p: SSArray);
571 var
572 cmd, s: AnsiString;
573 a, b: Integer;
574 (* F: TextFile; *)
575 begin
576 cmd := LowerCase(p[0]);
577 s := '';
579 if cmd = 'clear' then
580 begin
581 //ConsoleHistory := nil;
582 cbufClear();
583 conSkipLines := 0;
585 for a := 0 to High(MsgArray) do
586 with MsgArray[a] do
587 begin
588 Msg := '';
589 Time := 0;
590 end;
591 end;
593 if cmd = 'clearhistory' then
594 CommandHistory := nil;
596 if cmd = 'showhistory' then
597 if CommandHistory <> nil then
598 begin
599 g_Console_Add('');
600 for a := 0 to High(CommandHistory) do
601 g_Console_Add(' '+CommandHistory[a]);
602 end;
604 if cmd = 'commands' then
605 begin
606 g_Console_Add('');
607 g_Console_Add('commands list:');
608 for a := High(commands) downto 0 do
609 begin
610 if (Length(commands[a].help) > 0) then
611 begin
612 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
613 end
614 else
615 begin
616 g_Console_Add(' '+commands[a].cmd);
617 end;
618 end;
619 end;
621 if cmd = 'time' then
622 g_Console_Add(TimeToStr(Now), True);
624 if cmd = 'date' then
625 g_Console_Add(DateToStr(Now), True);
627 if cmd = 'echo' then
628 if Length(p) > 1 then
629 begin
630 if p[1] = 'ololo' then
631 gCheats := True
632 else
633 begin
634 s := '';
635 for a := 1 to High(p) do
636 s := s + p[a] + ' ';
637 g_Console_Add(b_Text_Format(s), True);
638 end;
639 end
640 else
641 g_Console_Add('');
643 if cmd = 'dump' then
644 begin
645 (*
646 if ConsoleHistory <> nil then
647 begin
648 if Length(P) > 1 then
649 s := P[1]
650 else
651 s := GameDir+'/console.txt';
653 {$I-}
654 AssignFile(F, s);
655 Rewrite(F);
656 if IOResult <> 0 then
657 begin
658 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
659 CloseFile(F);
660 Exit;
661 end;
663 for a := 0 to High(ConsoleHistory) do
664 WriteLn(F, ConsoleHistory[a]);
666 CloseFile(F);
667 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
668 {$I+}
669 end;
670 *)
671 end;
673 if cmd = 'exec' then
674 begin
675 // exec <filename>
676 if Length(p) = 2 then
677 g_Console_ReadConfig(p[1])
678 else
679 g_Console_Add('exec <script file>');
680 end;
682 if cmd = 'writeconfig' then
683 begin
684 // writeconfig <filename>
685 if Length(p) = 2 then
686 begin
687 s := e_GetWriteableDir(ConfigDirs);
688 g_Console_WriteConfig(e_CatPath(s, p[1]))
689 end
690 else
691 begin
692 g_Console_Add('writeconfig <file>')
693 end
694 end;
696 if (cmd = 'ver') or (cmd = 'version') then
697 begin
698 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
699 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
700 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
701 end;
703 if cmd = 'alias' then
704 begin
705 // alias [alias_name] [commands]
706 if Length(p) > 1 then
707 begin
708 for a := 0 to High(Aliases) do
709 if Aliases[a].name = p[1] then
710 begin
711 if Length(p) > 2 then
712 Aliases[a].commands := ParseAlias(p[2])
713 else
714 for b := 0 to High(Aliases[a].commands) do
715 g_Console_Add(Aliases[a].commands[b]);
716 Exit;
717 end;
718 SetLength(Aliases, Length(Aliases)+1);
719 a := High(Aliases);
720 Aliases[a].name := p[1];
721 if Length(p) > 2 then
722 Aliases[a].commands := ParseAlias(p[2])
723 else
724 for b := 0 to High(Aliases[a].commands) do
725 g_Console_Add(Aliases[a].commands[b]);
726 end else
727 for a := 0 to High(Aliases) do
728 if Aliases[a].commands <> nil then
729 g_Console_Add(Aliases[a].name);
730 end;
732 if cmd = 'call' then
733 begin
734 // call <alias_name>
735 if Length(p) > 1 then
736 begin
737 if Aliases = nil then
738 Exit;
739 for a := 0 to High(Aliases) do
740 if Aliases[a].name = p[1] then
741 begin
742 if Aliases[a].commands <> nil then
743 begin
744 // with this system proper endless loop detection seems either impossible
745 // or very dirty to implement, so let's have this instead
746 // prevents endless loops
747 for b := 0 to High(Aliases[a].commands) do
748 begin
749 Inc(RecursionDepth);
750 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
751 if not RecursionLimitHit then
752 g_Console_Process(Aliases[a].commands[b], True);
753 Dec(RecursionDepth);
754 end;
755 if (RecursionDepth = 0) and RecursionLimitHit then
756 begin
757 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
758 RecursionLimitHit := False;
759 end;
760 end;
761 Exit;
762 end;
763 end
764 else
765 g_Console_Add('call <alias name>');
766 end;
767 end;
769 procedure WhitelistCommand(cmd: AnsiString);
770 var
771 a: Integer;
772 begin
773 SetLength(Whitelist, Length(Whitelist)+1);
774 a := High(Whitelist);
775 Whitelist[a] := LowerCase(cmd);
776 end;
778 procedure segfault (p: SSArray);
779 var
780 pp: PByte = nil;
781 begin
782 pp^ := 0;
783 end;
785 function GetCommandString (p: SSArray): AnsiString;
786 var i: Integer;
787 begin
788 result := '';
789 if Length(p) >= 1 then
790 begin
791 result := p[0];
792 for i := 1 to High(p) do
793 result := result + '; ' + p[i]
794 end
795 end;
797 function QuoteStr(str: String): String;
798 begin
799 if Pos(' ', str) > 0 then
800 Result := '"' + str + '"'
801 else
802 Result := str;
803 end;
805 procedure BindCommands (p: SSArray);
806 var cmd, key: AnsiString; i: Integer;
807 begin
808 cmd := LowerCase(p[0]);
809 case cmd of
810 'bind':
811 // bind <key> [down [up]]
812 if (Length(p) >= 2) and (Length(p) <= 4) then
813 begin
814 i := 0;
815 key := LowerCase(p[1]);
816 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
817 if i < e_MaxInputKeys then
818 begin
819 if Length(p) = 2 then
820 g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
821 else if Length(p) = 3 then
822 g_Console_BindKey(i, p[2], '')
823 else (* len = 4 *)
824 g_Console_BindKey(i, p[2], p[3])
825 end
826 else
827 g_Console_Add('bind: "' + p[1] + '" is not a key')
828 end
829 else
830 begin
831 g_Console_Add('bind <key> <down action> [up action]')
832 end;
833 'bindlist':
834 for i := 0 to e_MaxInputKeys - 1 do
835 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
836 g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
837 'unbind':
838 // unbind <key>
839 if Length(p) = 2 then
840 begin
841 key := LowerCase(p[1]);
842 i := 0;
843 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
844 if i < e_MaxInputKeys then
845 g_Console_BindKey(i, '')
846 else
847 g_Console_Add('unbind: "' + p[1] + '" is not a key')
848 end
849 else
850 g_Console_Add('unbind <key>');
851 'unbindall':
852 for i := 0 to e_MaxInputKeys - 1 do
853 g_Console_BindKey(i, '');
854 'showkeyboard':
855 g_Touch_ShowKeyboard(True);
856 'hidekeyboard':
857 g_Touch_ShowKeyboard(False);
858 'togglemenu':
859 begin
860 if gConsoleShow then
861 g_Console_Switch
862 else if gChatShow then
863 g_Console_Chat_Switch
864 else
865 KeyPress(VK_ESCAPE);
866 menu_toggled := True
867 end;
868 'toggleconsole':
869 g_Console_Switch;
870 'togglechat':
871 g_Console_Chat_Switch;
872 'toggleteamchat':
873 if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
874 g_Console_Chat_Switch(True);
875 end
876 end;
878 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
879 var
880 a: Integer;
881 cp: PCommand;
882 begin
883 SetLength(commands, Length(commands)+1);
884 a := High(commands);
885 cp := @commands[a];
886 cp.cmd := LowerCase(cmd);
887 cp.proc := proc;
888 cp.procEx := nil;
889 cp.help := ahelp;
890 cp.hidden := ahidden;
891 cp.ptr := nil;
892 cp.msg := '';
893 cp.cheat := acheat;
894 cp.action := -1;
895 cp.player := -1;
896 end;
898 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
899 const
900 PrefixList: array [0..1] of AnsiString = ('+', '-');
901 PlayerList: array [0..1] of Integer = (1, 2);
902 var
903 s: AnsiString;
904 i: Integer;
906 procedure NewAction (cmd: AnsiString; player: Integer);
907 var cp: PCommand;
908 begin
909 SetLength(commands, Length(commands) + 1);
910 cp := @commands[High(commands)];
911 cp.cmd := LowerCase(cmd);
912 cp.proc := nil;
913 cp.procEx := nil;
914 cp.help := help;
915 cp.hidden := hidden;
916 cp.ptr := nil;
917 cp.msg := '';
918 cp.cheat := cheat;
919 cp.action := action;
920 cp.player := player;
921 end;
923 begin
924 ASSERT(action >= FIRST_ACTION);
925 ASSERT(action <= LAST_ACTION);
926 for s in PrefixList do
927 begin
928 NewAction(s + cmd, 0);
929 for i in PlayerList do
930 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
931 end
932 end;
934 procedure g_Console_SysInit;
935 var a: Integer;
936 begin
937 Cons_Y := -Floor(gScreenHeight * ConsoleHeight);
938 gConsoleShow := False;
939 gChatShow := False;
940 Cons_Shown := False;
941 InputReady := False;
942 CPos := 1;
944 for a := 0 to High(MsgArray) do
945 with MsgArray[a] do
946 begin
947 Msg := '';
948 Time := 0;
949 end;
951 AddCommand('segfault', segfault, 'make segfault');
953 AddCommand('quit', SystemCommands);
954 AddCommand('exit', SystemCommands);
955 AddCommand('r_reset', SystemCommands);
956 AddCommand('r_maxfps', SystemCommands);
957 AddCommand('g_language', SystemCommands);
959 AddCommand('bind', BindCommands);
960 AddCommand('bindlist', BindCommands);
961 AddCommand('unbind', BindCommands);
962 AddCommand('unbindall', BindCommands);
963 AddCommand('showkeyboard', BindCommands);
964 AddCommand('hidekeyboard', BindCommands);
965 AddCommand('togglemenu', BindCommands);
966 AddCommand('toggleconsole', BindCommands);
967 AddCommand('togglechat', BindCommands);
968 AddCommand('toggleteamchat', BindCommands);
970 AddCommand('clear', ConsoleCommands, 'clear console');
971 AddCommand('clearhistory', ConsoleCommands);
972 AddCommand('showhistory', ConsoleCommands);
973 AddCommand('commands', ConsoleCommands);
974 AddCommand('time', ConsoleCommands);
975 AddCommand('date', ConsoleCommands);
976 AddCommand('echo', ConsoleCommands);
977 AddCommand('dump', ConsoleCommands);
978 AddCommand('exec', ConsoleCommands);
979 AddCommand('writeconfig', ConsoleCommands);
980 AddCommand('alias', ConsoleCommands);
981 AddCommand('call', ConsoleCommands);
982 AddCommand('ver', ConsoleCommands);
983 AddCommand('version', ConsoleCommands);
985 AddCommand('d_window', DebugCommands);
986 AddCommand('d_sounds', DebugCommands);
987 AddCommand('d_frames', DebugCommands);
988 AddCommand('d_winmsg', DebugCommands);
989 AddCommand('d_monoff', DebugCommands);
990 AddCommand('d_botoff', DebugCommands);
991 AddCommand('d_monster', DebugCommands);
992 AddCommand('d_health', DebugCommands);
993 AddCommand('d_player', DebugCommands);
994 AddCommand('d_joy', DebugCommands);
995 AddCommand('d_mem', DebugCommands);
997 AddCommand('p1_name', PlayerSettingsCVars);
998 AddCommand('p2_name', PlayerSettingsCVars);
999 AddCommand('p1_color', PlayerSettingsCVars);
1000 AddCommand('p2_color', PlayerSettingsCVars);
1001 AddCommand('p1_model', PlayerSettingsCVars);
1002 AddCommand('p2_model', PlayerSettingsCVars);
1004 AddCommand('g_max_particles', GameCVars);
1005 AddCommand('g_max_shells', GameCVars);
1006 AddCommand('g_max_gibs', GameCVars);
1007 AddCommand('g_max_corpses', GameCVars);
1008 AddCommand('g_gamemode', GameCVars);
1009 AddCommand('g_friendlyfire', GameCVars);
1010 AddCommand('g_weaponstay', GameCVars);
1011 AddCommand('g_allow_exit', GameCVars);
1012 AddCommand('g_dm_keys', GameCVars);
1013 AddCommand('g_allow_monsters', GameCVars);
1014 AddCommand('g_bot_vsmonsters', GameCVars);
1015 AddCommand('g_bot_vsplayers', GameCVars);
1016 AddCommand('g_scorelimit', GameCVars);
1017 AddCommand('g_timelimit', GameCVars);
1018 AddCommand('g_maxlives', GameCVars);
1019 AddCommand('g_warmup_time', GameCVars);
1020 AddCommand('g_spawn_invul', GameCVars);
1021 AddCommand('g_item_respawn_time', GameCVars);
1022 AddCommand('sv_intertime', GameCVars);
1024 AddCommand('sv_name', NetServerCVars);
1025 AddCommand('sv_passwd', NetServerCVars);
1026 AddCommand('sv_maxplrs', NetServerCVars);
1027 AddCommand('sv_public', NetServerCVars);
1028 AddCommand('sv_port', NetServerCVars);
1030 AddCommand('pause', GameCommands);
1031 AddCommand('endgame', GameCommands);
1032 AddCommand('restart', GameCommands);
1033 AddCommand('addbot', GameCommands);
1034 AddCommand('bot_add', GameCommands);
1035 AddCommand('bot_addlist', GameCommands);
1036 AddCommand('bot_addred', GameCommands);
1037 AddCommand('bot_addblue', GameCommands);
1038 AddCommand('bot_removeall', GameCommands);
1039 AddCommand('chat', GameCommands);
1040 AddCommand('teamchat', GameCommands);
1041 AddCommand('game', GameCommands);
1042 AddCommand('host', GameCommands);
1043 AddCommand('map', GameCommands);
1044 AddCommand('nextmap', GameCommands);
1045 AddCommand('endmap', GameCommands);
1046 AddCommand('goodbye', GameCommands);
1047 AddCommand('suicide', GameCommands);
1048 AddCommand('spectate', GameCommands);
1049 AddCommand('ready', GameCommands);
1050 AddCommand('kick', GameCommands);
1051 AddCommand('kick_id', GameCommands);
1052 AddCommand('ban', GameCommands);
1053 AddCommand('permban', GameCommands);
1054 AddCommand('ban_id', GameCommands);
1055 AddCommand('permban_id', GameCommands);
1056 AddCommand('unban', GameCommands);
1057 AddCommand('connect', GameCommands);
1058 AddCommand('disconnect', GameCommands);
1059 AddCommand('reconnect', GameCommands);
1060 AddCommand('say', GameCommands);
1061 AddCommand('tell', GameCommands);
1062 AddCommand('centerprint', GameCommands);
1063 AddCommand('overtime', GameCommands);
1064 AddCommand('rcon_password', GameCommands);
1065 AddCommand('rcon', GameCommands);
1066 AddCommand('callvote', GameCommands);
1067 AddCommand('vote', GameCommands);
1068 AddCommand('clientlist', GameCommands);
1069 AddCommand('event', GameCommands);
1070 AddCommand('screenshot', GameCommands);
1071 AddCommand('weapon', GameCommands);
1072 AddCommand('p1_weapon', GameCommands);
1073 AddCommand('p2_weapon', GameCommands);
1075 AddCommand('god', GameCheats);
1076 AddCommand('notarget', GameCheats);
1077 AddCommand('give', GameCheats); // "exit" too ;-)
1078 AddCommand('open', GameCheats);
1079 AddCommand('fly', GameCheats);
1080 AddCommand('noclip', GameCheats);
1081 AddCommand('speedy', GameCheats);
1082 AddCommand('jumpy', GameCheats);
1083 AddCommand('noreload', GameCheats);
1084 AddCommand('aimline', GameCheats);
1085 AddCommand('automap', GameCheats);
1087 AddAction('jump', ACTION_JUMP);
1088 AddAction('moveleft', ACTION_MOVELEFT);
1089 AddAction('moveright', ACTION_MOVERIGHT);
1090 AddAction('lookup', ACTION_LOOKUP);
1091 AddAction('lookdown', ACTION_LOOKDOWN);
1092 AddAction('attack', ACTION_ATTACK);
1093 AddAction('scores', ACTION_SCORES);
1094 AddAction('activate', ACTION_ACTIVATE);
1095 AddAction('strafe', ACTION_STRAFE);
1096 AddAction('weapnext', ACTION_WEAPNEXT);
1097 AddAction('weapprev', ACTION_WEAPPREV);
1099 WhitelistCommand('say');
1100 WhitelistCommand('tell');
1101 WhitelistCommand('overtime');
1102 WhitelistCommand('ready');
1103 WhitelistCommand('map');
1104 WhitelistCommand('nextmap');
1105 WhitelistCommand('endmap');
1106 WhitelistCommand('restart');
1107 WhitelistCommand('kick');
1108 WhitelistCommand('ban');
1109 WhitelistCommand('centerprint');
1111 WhitelistCommand('addbot');
1112 WhitelistCommand('bot_add');
1113 WhitelistCommand('bot_addred');
1114 WhitelistCommand('bot_addblue');
1115 WhitelistCommand('bot_removeall');
1117 WhitelistCommand('g_gamemode');
1118 WhitelistCommand('g_friendlyfire');
1119 WhitelistCommand('g_weaponstay');
1120 WhitelistCommand('g_allow_exit');
1121 WhitelistCommand('g_dm_keys');
1122 WhitelistCommand('g_allow_monsters');
1123 WhitelistCommand('g_bot_vsmonsters');
1124 WhitelistCommand('g_bot_vsplayers');
1125 WhitelistCommand('g_scorelimit');
1126 WhitelistCommand('g_timelimit');
1127 WhitelistCommand('g_maxlives');
1128 WhitelistCommand('g_warmup_time');
1129 WhitelistCommand('g_spawn_invul');
1130 WhitelistCommand('g_item_respawn_time');
1132 g_Console_ResetBinds;
1133 g_Console_ReadConfig(gConfigScript);
1134 g_Console_ReadConfig(autoexecScript);
1135 gParsingBinds := False;
1136 end;
1138 procedure g_Console_Init;
1139 begin
1140 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
1141 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
1142 g_Console_Add('');
1143 end;
1145 procedure g_Console_Update;
1146 var
1147 a, b, Step: Integer;
1148 begin
1149 if Cons_Shown then
1150 begin
1151 Step := Max(1, Round(Floor(gScreenHeight * ConsoleHeight) * ConsoleStep));
1152 if gConsoleShow then
1153 begin
1154 (* Open animation *)
1155 Cons_Y := Min(Cons_Y + Step, 0);
1156 InputReady := True
1157 end
1158 else
1159 begin
1160 (* Close animation *)
1161 Cons_Y := Max(Cons_Y - Step, -Floor(gScreenHeight * ConsoleHeight));
1162 Cons_Shown := Cons_Y > -Floor(gScreenHeight * ConsoleHeight);
1163 InputReady := False
1164 end;
1166 if gChatShow then
1167 InputReady := True
1168 end;
1170 a := 0;
1171 while a <= High(MsgArray) do
1172 begin
1173 if MsgArray[a].Time > 0 then
1174 begin
1175 if MsgArray[a].Time = 1 then
1176 begin
1177 if a < High(MsgArray) then
1178 begin
1179 for b := a to High(MsgArray)-1 do
1180 MsgArray[b] := MsgArray[b+1];
1182 MsgArray[High(MsgArray)].Time := 0;
1184 a := a - 1;
1185 end;
1186 end
1187 else
1188 Dec(MsgArray[a].Time);
1189 end;
1191 a := a + 1;
1192 end;
1193 end;
1196 procedure drawConsoleText ();
1197 var
1198 CWidth, CHeight: Byte;
1199 ty: Integer;
1200 sp, ep: LongWord;
1201 skip: Integer;
1203 procedure putLine (sp, ep: LongWord);
1204 var
1205 p: LongWord;
1206 wdt, cw: Integer;
1207 begin
1208 p := sp;
1209 wdt := 0;
1210 while p <> ep do
1211 begin
1212 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1213 if wdt+cw > gScreenWidth-8 then break;
1214 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1215 Inc(wdt, cw);
1216 cbufNext(p);
1217 end;
1218 if p <> ep then putLine(p, ep); // do rest of the line first
1219 // now print our part
1220 if skip = 0 then
1221 begin
1222 ep := p;
1223 p := sp;
1224 wdt := 2;
1225 while p <> ep do
1226 begin
1227 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1228 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1229 Inc(wdt, cw);
1230 cbufNext(p);
1231 end;
1232 Dec(ty, CHeight);
1233 end
1234 else
1235 begin
1236 Dec(skip);
1237 end;
1238 end;
1240 begin
1241 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1242 ty := Floor(gScreenHeight * ConsoleHeight) - 4 - 2 * CHeight - Abs(Cons_Y);
1243 skip := conSkipLines;
1244 cbufLastLine(sp, ep);
1245 repeat
1246 putLine(sp, ep);
1247 if ty+CHeight <= 0 then break;
1248 until not cbufLineUp(sp, ep);
1249 end;
1251 procedure g_Console_Draw(MessagesOnly: Boolean = False);
1252 var
1253 CWidth, CHeight: Byte;
1254 mfW, mfH: Word;
1255 a, b, offset_y: Integer;
1256 begin
1257 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1259 if ChatTop and gChatShow then
1260 offset_y := CHeight
1261 else
1262 offset_y := 0;
1264 for a := 0 to High(MsgArray) do
1265 if MsgArray[a].Time > 0 then
1266 e_TextureFontPrintFmt(0, offset_y + CHeight * a, MsgArray[a].Msg, gStdFont, True);
1268 if MessagesOnly then Exit;
1270 if gChatShow then
1271 begin
1272 if ChatTop then
1273 offset_y := 0
1274 else
1275 offset_y := gScreenHeight - CHeight - 1;
1276 if gChatTeam then
1277 begin
1278 e_TextureFontPrintEx(0, offset_y, 'say team> ' + Line, gStdFont, 255, 255, 255, 1, True);
1279 e_TextureFontPrintEx((CPos + 9) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1280 end
1281 else
1282 begin
1283 e_TextureFontPrintEx(0, offset_y, 'say> ' + Line, gStdFont, 255, 255, 255, 1, True);
1284 e_TextureFontPrintEx((CPos + 4) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1285 end
1286 end;
1288 if not Cons_Shown then
1289 Exit;
1291 if gDebugMode then
1292 begin
1293 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1294 a := (gScreenWidth - 2*mfW) div 2;
1295 b := Cons_Y + (Floor(gScreenHeight * ConsoleHeight) - 2 * mfH) div 2;
1296 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1297 _RGB(128, 0, 0), 2.0);
1298 end;
1300 e_DrawSize(ID, 0, Cons_Y, Round(ConsoleTrans * 255), False, False, gScreenWidth, Floor(gScreenHeight * ConsoleHeight));
1301 e_TextureFontPrint(0, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - CHeight - 4, '> ' + Line, gStdFont);
1303 drawConsoleText();
1304 (*
1305 if ConsoleHistory <> nil then
1306 begin
1307 b := 0;
1308 if CHeight > 0 then
1309 if Length(ConsoleHistory) > (Floor(gScreenHeight * ConsoleHeight) div CHeight) - 1 then
1310 b := Length(ConsoleHistory) - (Floor(gScreenHeight * ConsoleHeight) div CHeight) + 1;
1312 b := Max(b-Offset, 0);
1313 d := Max(High(ConsoleHistory)-Offset, 0);
1315 c := 2;
1316 for a := d downto b do
1317 begin
1318 e_TextureFontPrintFmt(0, Floor(gScreenHeight * ConsoleHeight) - 4 - c * CHeight - Abs(Cons_Y), ConsoleHistory[a], gStdFont, True);
1319 c := c + 1;
1320 end;
1321 end;
1322 *)
1324 e_TextureFontPrint((CPos + 1) * CWidth, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - 21, '_', gStdFont);
1325 end;
1327 procedure g_Console_Char(C: AnsiChar);
1328 begin
1329 if InputReady and (gConsoleShow or gChatShow) then
1330 begin
1331 Insert(C, Line, CPos);
1332 CPos := CPos + 1;
1333 end
1334 end;
1337 var
1338 tcomplist: array of AnsiString = nil;
1339 tcompidx: array of Integer = nil;
1341 procedure Complete ();
1342 var
1343 i, c: Integer;
1344 tused: Integer;
1345 ll, lpfx, cmd: AnsiString;
1346 begin
1347 if (Length(Line) = 0) then
1348 begin
1349 g_Console_Add('');
1350 for i := 0 to High(commands) do
1351 begin
1352 // hidden commands are hidden when cheats aren't enabled
1353 if commands[i].hidden and not conIsCheatsEnabled then continue;
1354 if (Length(commands[i].help) > 0) then
1355 begin
1356 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1357 end
1358 else
1359 begin
1360 g_Console_Add(' '+commands[i].cmd);
1361 end;
1362 end;
1363 exit;
1364 end;
1366 ll := LowerCase(Line);
1367 lpfx := '';
1369 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1370 begin
1371 ll := Copy(ll, 0, Length(ll)-1);
1372 for i := 0 to High(commands) do
1373 begin
1374 // hidden commands are hidden when cheats aren't enabled
1375 if commands[i].hidden and not conIsCheatsEnabled then continue;
1376 if (commands[i].cmd = ll) then
1377 begin
1378 if (Length(commands[i].help) > 0) then
1379 begin
1380 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1381 end;
1382 end;
1383 end;
1384 exit;
1385 end;
1387 // build completion list
1388 tused := 0;
1389 for i := 0 to High(commands) do
1390 begin
1391 // hidden commands are hidden when cheats aren't enabled
1392 if commands[i].hidden and not conIsCheatsEnabled then continue;
1393 cmd := commands[i].cmd;
1394 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1395 begin
1396 if (tused = Length(tcomplist)) then
1397 begin
1398 SetLength(tcomplist, Length(tcomplist)+128);
1399 SetLength(tcompidx, Length(tcompidx)+128);
1400 end;
1401 tcomplist[tused] := cmd;
1402 tcompidx[tused] := i;
1403 Inc(tused);
1404 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1405 end;
1406 end;
1408 // get longest prefix
1409 for i := 0 to tused-1 do
1410 begin
1411 cmd := tcomplist[i];
1412 for c := 1 to Length(lpfx) do
1413 begin
1414 if (c > Length(cmd)) then break;
1415 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1416 end;
1417 end;
1419 if (tused = 0) then exit;
1421 if (tused = 1) then
1422 begin
1423 Line := tcomplist[0]+' ';
1424 CPos := Length(Line)+1;
1425 end
1426 else
1427 begin
1428 // has longest prefix?
1429 if (Length(lpfx) > Length(ll)) then
1430 begin
1431 Line := lpfx;
1432 CPos:= Length(Line)+1;
1433 end
1434 else
1435 begin
1436 g_Console_Add('');
1437 for i := 0 to tused-1 do
1438 begin
1439 if (Length(commands[tcompidx[i]].help) > 0) then
1440 begin
1441 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1442 end
1443 else
1444 begin
1445 g_Console_Add(' '+tcomplist[i]);
1446 end;
1447 end;
1448 end;
1449 end;
1450 end;
1453 procedure g_Console_Control(K: Word);
1454 begin
1455 case K of
1456 IK_BACKSPACE:
1457 if (Length(Line) > 0) and (CPos > 1) then
1458 begin
1459 Delete(Line, CPos-1, 1);
1460 CPos := CPos-1;
1461 end;
1462 IK_DELETE:
1463 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1464 Delete(Line, CPos, 1);
1465 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1466 if CPos > 1 then
1467 CPos := CPos - 1;
1468 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1469 if CPos <= Length(Line) then
1470 CPos := CPos + 1;
1471 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1472 begin
1473 if gConsoleShow then
1474 g_Console_Process(Line)
1475 else
1476 if gChatShow then
1477 begin
1478 if (Length(Line) > 0) and g_Game_IsNet then
1479 begin
1480 if gChatTeam then
1481 begin
1482 if g_Game_IsClient then
1483 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1484 else
1485 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1486 NET_CHAT_TEAM, gPlayer1Settings.Team);
1487 end
1488 else
1489 begin
1490 if g_Game_IsClient then
1491 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1492 else
1493 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1494 NET_CHAT_PLAYER);
1495 end;
1496 end;
1498 Line := '';
1499 CPos := 1;
1500 gJustChatted := True;
1501 g_Console_Chat_Switch;
1502 InputReady := False;
1503 end;
1504 end;
1505 IK_TAB:
1506 if not gChatShow then
1507 Complete();
1508 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1509 if not gChatShow then
1510 if (CommandHistory <> nil) and
1511 (CmdIndex < Length(CommandHistory)) then
1512 begin
1513 if CmdIndex < Length(CommandHistory)-1 then
1514 CmdIndex := CmdIndex + 1;
1515 Line := CommandHistory[CmdIndex];
1516 CPos := Length(Line) + 1;
1517 end;
1518 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1519 if not gChatShow then
1520 if (CommandHistory <> nil) and
1521 (CmdIndex <= Length(CommandHistory)) then
1522 begin
1523 if CmdIndex > 0 then
1524 CmdIndex := CmdIndex - 1;
1525 Line := CommandHistory[CmdIndex];
1526 Cpos := Length(Line) + 1;
1527 end;
1528 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1529 if not gChatShow then Inc(conSkipLines);
1530 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1531 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1532 IK_HOME, IK_KPHOME:
1533 CPos := 1;
1534 IK_END, IK_KPEND:
1535 CPos := Length(Line) + 1;
1536 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1537 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, (*IK_EQUALS,*)
1538 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1539 (* see TEXTINPUT event *)
1540 end
1541 end;
1543 function GetStr(var Str: AnsiString): AnsiString;
1544 var
1545 a, b: Integer;
1546 begin
1547 Result := '';
1548 if Str[1] = '"' then
1549 begin
1550 for b := 1 to Length(Str) do
1551 if (b = Length(Str)) or (Str[b+1] = '"') then
1552 begin
1553 Result := Copy(Str, 2, b-1);
1554 Delete(Str, 1, b+1);
1555 Str := Trim(Str);
1556 Exit;
1557 end;
1558 end;
1560 for a := 1 to Length(Str) do
1561 if (a = Length(Str)) or (Str[a+1] = ' ') then
1562 begin
1563 Result := Copy(Str, 1, a);
1564 Delete(Str, 1, a+1);
1565 Str := Trim(Str);
1566 Exit;
1567 end;
1568 end;
1570 function ParseString(Str: AnsiString): SSArray;
1571 begin
1572 Result := nil;
1574 Str := Trim(Str);
1576 if Str = '' then
1577 Exit;
1579 while Str <> '' do
1580 begin
1581 SetLength(Result, Length(Result)+1);
1582 Result[High(Result)] := GetStr(Str);
1583 end;
1584 end;
1586 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1588 procedure conmsg (s: AnsiString);
1589 var
1590 a: Integer;
1591 begin
1592 if length(s) = 0 then exit;
1593 for a := 0 to High(MsgArray) do
1594 begin
1595 with MsgArray[a] do
1596 begin
1597 if Time = 0 then
1598 begin
1599 Msg := s;
1600 Time := MsgTime;
1601 exit;
1602 end;
1603 end;
1604 end;
1605 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1606 with MsgArray[High(MsgArray)] do
1607 begin
1608 Msg := L;
1609 Time := MsgTime;
1610 end;
1611 end;
1613 var
1614 f: Integer;
1615 begin
1616 // put it to console
1617 cbufPut(L);
1618 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1620 // now show 'em out of console too
1621 show := show and gAllowConsoleMessages;
1622 if show and gShowMessages then
1623 begin
1624 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1625 while length(L) > 0 do
1626 begin
1627 f := Pos(#10, L);
1628 if f <= 0 then f := length(L)+1;
1629 conmsg(Copy(L, 1, f-1));
1630 Delete(L, 1, f);
1631 end;
1632 end;
1634 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1635 //ConsoleHistory[High(ConsoleHistory)] := L;
1637 (*
1638 {$IFDEF HEADLESS}
1639 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1640 {$ENDIF}
1641 *)
1642 end;
1645 var
1646 consolewriterLastWasEOL: Boolean = false;
1648 procedure consolewriter (constref buf; len: SizeUInt);
1649 var
1650 b: PByte;
1651 begin
1652 if (len < 1) then exit;
1653 b := PByte(@buf);
1654 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1655 while (len > 0) do
1656 begin
1657 if (b[0] <> 13) and (b[0] <> 10) then
1658 begin
1659 cbufPut(AnsiChar(b[0]));
1660 end
1661 else
1662 begin
1663 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1664 cbufPut(#10);
1665 end;
1666 len -= 1;
1667 b += 1;
1668 end;
1669 end;
1672 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1673 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1674 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1675 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1676 begin
1677 g_Console_Add(s, show);
1678 end;
1681 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1682 begin
1683 if show then
1684 begin
1685 g_Console_Add(formatstrf(s, args), true);
1686 end
1687 else
1688 begin
1689 consolewriterLastWasEOL := false;
1690 formatstrf(s, args, consolewriter);
1691 if not consolewriterLastWasEOL then cbufPut(#10);
1692 end;
1693 end;
1696 procedure g_Console_Clear();
1697 begin
1698 //ConsoleHistory := nil;
1699 cbufClear();
1700 conSkipLines := 0;
1701 end;
1703 procedure AddToHistory(L: AnsiString);
1704 var
1705 len: Integer;
1706 begin
1707 len := Length(CommandHistory);
1709 if (len = 0) or
1710 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1711 begin
1712 SetLength(CommandHistory, len+1);
1713 CommandHistory[len] := L;
1714 end;
1716 CmdIndex := Length(CommandHistory);
1717 end;
1719 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1720 var
1721 Arr: SSArray;
1722 i: Integer;
1723 begin
1724 Result := True;
1726 Arr := nil;
1728 if Trim(C) = '' then
1729 Exit;
1731 Arr := ParseString(C);
1732 if Arr = nil then
1733 Exit;
1735 for i := 0 to High(Whitelist) do
1736 if Whitelist[i] = LowerCase(Arr[0]) then
1737 Result := False;
1738 end;
1740 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1741 var
1742 Arr: SSArray;
1743 i: Integer;
1744 begin
1745 Arr := nil;
1747 if Trim(L) = '' then
1748 Exit;
1750 conSkipLines := 0; // "unscroll"
1752 if L = 'goobers' then
1753 begin
1754 Line := '';
1755 CPos := 1;
1756 gCheats := true;
1757 g_Console_Add('Your memory serves you well.');
1758 exit;
1759 end;
1761 if not quiet then
1762 begin
1763 g_Console_Add('> '+L);
1764 Line := '';
1765 CPos := 1;
1766 end;
1768 Arr := ParseString(L);
1769 if Arr = nil then
1770 Exit;
1772 if commands = nil then
1773 Exit;
1775 if not quiet then
1776 AddToHistory(L);
1778 for i := 0 to High(commands) do
1779 begin
1780 if commands[i].cmd = LowerCase(Arr[0]) then
1781 begin
1782 if commands[i].action >= 0 then
1783 begin
1784 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1785 exit
1786 end;
1787 if assigned(commands[i].procEx) then
1788 begin
1789 commands[i].procEx(@commands[i], Arr);
1790 exit
1791 end;
1792 if assigned(commands[i].proc) then
1793 begin
1794 commands[i].proc(Arr);
1795 exit
1796 end
1797 end
1798 end;
1800 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1801 end;
1804 function g_Console_Interactive: Boolean;
1805 begin
1806 Result := gConsoleShow
1807 end;
1809 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
1810 begin
1811 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1812 ASSERT(key >= 0);
1813 ASSERT(key < e_MaxInputKeys);
1814 if key > 0 then
1815 begin
1816 gInputBinds[key].down := ParseAlias(down);
1817 gInputBinds[key].up := ParseAlias(up);
1818 end;
1819 g_Console_WriteGameConfig();
1820 end;
1822 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1824 function EqualsCommandLists (a, b: SSArray): Boolean;
1825 var i, len: Integer;
1826 begin
1827 result := False;
1828 len := Length(a);
1829 if len = Length(b) then
1830 begin
1831 i := 0;
1832 while (i < len) and (a[i] = b[i]) do inc(i);
1833 if i >= len then
1834 result := True
1835 end
1836 end;
1838 begin
1839 ASSERT(key >= 0);
1840 ASSERT(key < e_MaxInputKeys);
1841 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1842 end;
1844 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1845 var i: Integer;
1846 begin
1847 ASSERT(n >= 1);
1848 result := 0;
1849 if commands = nil then Exit;
1850 i := 0;
1851 while (n >= 1) and (i < e_MaxInputKeys) do
1852 begin
1853 if g_Console_MatchBind(i, down, up) then
1854 begin
1855 result := i;
1856 dec(n)
1857 end;
1858 inc(i)
1859 end;
1860 if n >= 1 then
1861 result := 0
1862 end;
1864 function g_Console_Action (action: Integer): Boolean;
1865 var i, len: Integer;
1866 begin
1867 ASSERT(action >= FIRST_ACTION);
1868 ASSERT(action <= LAST_ACTION);
1869 i := 0;
1870 len := Length(gPlayerAction);
1871 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1872 Result := i < len
1873 end;
1875 function BindsAllowed (key: Integer): Boolean;
1876 begin
1877 Result := False;
1878 if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1879 begin
1880 if gChatShow then
1881 Result := g_Console_MatchBind(key, 'togglemenu') or
1882 g_Console_MatchBind(key, 'showkeyboard') or
1883 g_Console_MatchBind(key, 'hidekeyboard')
1884 else if gConsoleShow or (g_ActiveWindow <> nil) or (gGameSettings.GameType = GT_NONE) then
1885 Result := g_Console_MatchBind(key, 'togglemenu') or
1886 g_Console_MatchBind(key, 'toggleconsole') or
1887 g_Console_MatchBind(key, 'showkeyboard') or
1888 g_Console_MatchBind(key, 'hidekeyboard')
1889 else (* in game *)
1890 Result := True
1891 end
1892 end;
1894 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1895 var i: Integer;
1896 begin
1897 if BindsAllowed(key) then
1898 begin
1899 if down then
1900 for i := 0 to High(gInputBinds[key].down) do
1901 g_Console_Process(gInputBinds[key].down[i], True)
1902 else
1903 for i := 0 to High(gInputBinds[key].up) do
1904 g_Console_Process(gInputBinds[key].up[i], True)
1905 end;
1906 if down and not menu_toggled then
1907 KeyPress(key);
1908 menu_toggled := False
1909 end;
1911 procedure g_Console_ResetBinds;
1912 var i: Integer;
1913 begin
1914 for i := 0 to e_MaxInputKeys - 1 do
1915 g_Console_BindKey(i, '', '');
1917 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
1918 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
1919 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1920 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1921 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1922 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1923 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1924 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1925 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1926 g_Console_BindKey(IK_E, '+p1_weapnext', '-p1_weapnext');
1927 g_Console_BindKey(IK_Q, '+p1_weapprev', '-p1_weapprev');
1928 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1929 g_Console_BindKey(IK_1, 'p1_weapon 1');
1930 g_Console_BindKey(IK_2, 'p1_weapon 2');
1931 g_Console_BindKey(IK_3, 'p1_weapon 3');
1932 g_Console_BindKey(IK_4, 'p1_weapon 4');
1933 g_Console_BindKey(IK_5, 'p1_weapon 5');
1934 g_Console_BindKey(IK_6, 'p1_weapon 6');
1935 g_Console_BindKey(IK_7, 'p1_weapon 7');
1936 g_Console_BindKey(IK_8, 'p1_weapon 8');
1937 g_Console_BindKey(IK_9, 'p1_weapon 9');
1938 g_Console_BindKey(IK_0, 'p1_weapon 10');
1939 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1940 g_Console_BindKey(IK_T, 'togglechat');
1941 g_Console_BindKey(IK_Y, 'toggleteamchat');
1942 g_Console_BindKey(IK_F11, 'screenshot');
1943 g_Console_BindKey(IK_TAB, '+p1_scores', '-p1_scores');
1944 g_Console_BindKey(IK_PAUSE, 'pause');
1945 g_Console_BindKey(IK_F1, 'vote');
1947 (* for i := 0 to e_MaxJoys - 1 do *)
1948 for i := 0 to 1 do
1949 begin
1950 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
1951 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
1952 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
1953 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
1954 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
1955 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
1956 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
1957 g_Console_BindKey(e_JoyButtonToKey(i, 1), '+p' + IntToStr(i mod 2 + 1) + '_weapnext', '-p' + IntToStr(i mod 2 + 1) + '_weapnext');
1958 g_Console_BindKey(e_JoyButtonToKey(i, 4), '+p' + IntToStr(i mod 2 + 1) + '_weapprev', '-p' + IntToStr(i mod 2 + 1) + '_weapprev');
1959 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
1960 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
1961 end;
1963 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
1964 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
1965 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
1966 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
1967 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
1968 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
1969 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
1970 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
1971 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
1972 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
1973 g_Console_BindKey(VK_NEXT, '+weapnext', '-weapnext');
1974 g_Console_BindKey(VK_PREV, '+weapprev', '-weapprev');
1975 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
1976 g_Console_BindKey(VK_0, 'weapon 1');
1977 g_Console_BindKey(VK_1, 'weapon 2');
1978 g_Console_BindKey(VK_2, 'weapon 3');
1979 g_Console_BindKey(VK_3, 'weapon 4');
1980 g_Console_BindKey(VK_4, 'weapon 5');
1981 g_Console_BindKey(VK_5, 'weapon 6');
1982 g_Console_BindKey(VK_6, 'weapon 7');
1983 g_Console_BindKey(VK_7, 'weapon 8');
1984 g_Console_BindKey(VK_8, 'weapon 9');
1985 g_Console_BindKey(VK_9, 'weapon 10');
1986 g_Console_BindKey(VK_A, 'weapon 11');
1987 g_Console_BindKey(VK_CHAT, 'togglechat');
1988 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
1989 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
1990 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
1991 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
1992 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
1993 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
1994 end;
1996 procedure g_Console_ReadConfig (filename: String);
1997 var f: TextFile; s: AnsiString; i, len: Integer;
1998 begin
1999 e_LogWritefln('g_Console_ReadConfig (1) "%s"', [filename]);
2000 if e_FindResource(ConfigDirs, filename, false) = true then
2001 begin
2002 e_LogWritefln('g_Console_ReadConfig (2) "%s"', [filename]);
2003 AssignFile(f, filename);
2004 Reset(f);
2005 while not EOF(f) do
2006 begin
2007 ReadLn(f, s);
2008 len := Length(s);
2009 if len > 0 then
2010 begin
2011 i := 1;
2012 (* skip spaces *)
2013 while (i <= len) and (s[i] <= ' ') do inc(i);
2014 (* skip comments *)
2015 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
2016 g_Console_Process(s, True);
2017 end
2018 end;
2019 CloseFile(f);
2020 end
2021 end;
2023 procedure g_Console_WriteConfig (filename: String);
2024 var f: TextFile; i, j: Integer;
2026 procedure WriteFlag(name: string; flag: LongWord);
2027 begin
2028 WriteLn(f, name, IfThen(LongBool(gsGameFlags and flag), 1, 0));
2029 end;
2031 begin
2032 AssignFile(f, filename);
2033 Rewrite(f);
2034 WriteLn(f, '// ' + configComment);
2036 // binds
2037 WriteLn(f, 'unbindall');
2038 for i := 0 to e_MaxInputKeys - 1 do
2039 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
2040 begin
2041 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
2042 if Length(gInputBinds[i].down) = 0 then
2043 Write(f, '""');
2044 if Length(gInputBinds[i].up) > 0 then
2045 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
2046 WriteLn(f, '');
2047 end;
2049 // lang
2050 if gAskLanguage then
2051 WriteLn(f, 'g_language ask')
2052 else
2053 WriteLn(f, 'g_language ', gLanguage);
2055 // net server
2056 WriteLn(f, 'sv_name ', QuoteStr(NetServerName));
2057 WriteLn(f, 'sv_passwd ', QuoteStr(NetPassword));
2058 WriteLn(f, 'sv_maxplrs ', NetMaxClients);
2059 WriteLn(f, 'sv_port ', NetPort);
2060 WriteLn(f, 'sv_public ', IfThen(NetUseMaster, 1, 0));
2062 // game settings
2063 WriteLn(f, 'g_max_particles ', g_GFX_GetMax());
2064 WriteLn(f, 'g_max_shells ', g_Shells_GetMax());
2065 WriteLn(f, 'g_max_gibs ', g_Gibs_GetMax());
2066 WriteLn(f, 'g_max_corpses ', g_Corpses_GetMax());
2067 WriteLn(f, 'sv_intertime ', gDefInterTime);
2069 // gameplay settings
2070 WriteLn(f, 'g_gamemode ', gsGameMode);
2071 WriteLn(f, 'g_scorelimit ', gsGoalLimit);
2072 WriteLn(f, 'g_timelimit ', gsTimeLimit);
2073 WriteLn(f, 'g_maxlives ', gsMaxLives);
2074 WriteLn(f, 'g_item_respawn_time ', gsItemRespawnTime);
2075 WriteLn(f, 'g_spawn_invul ', gsSpawnInvul);
2076 WriteLn(f, 'g_warmup_time ', gsWarmupTime);
2078 WriteFlag('g_friendlyfire ', GAME_OPTION_TEAMDAMAGE);
2079 WriteFlag('g_allow_exit ', GAME_OPTION_ALLOWEXIT);
2080 WriteFlag('g_allow_monsters ', GAME_OPTION_MONSTERS);
2081 WriteFlag('g_dm_keys ', GAME_OPTION_DMKEYS);
2082 WriteFlag('g_weaponstay ', GAME_OPTION_WEAPONSTAY);
2083 WriteFlag('g_bot_vsmonsters ', GAME_OPTION_BOTVSMONSTER);
2084 WriteFlag('g_bot_vsplayers ', GAME_OPTION_BOTVSPLAYER);
2086 // players
2087 with gPlayer1Settings do
2088 begin
2089 WriteLn(f, 'p1_name ', QuoteStr(Name));
2090 WriteLn(f, 'p1_color ', Color.R, ' ', Color.G, ' ', Color.B);
2091 WriteLn(f, 'p1_model ', QuoteStr(Model));
2092 end;
2093 with gPlayer2Settings do
2094 begin
2095 WriteLn(f, 'p2_name ', QuoteStr(Name));
2096 WriteLn(f, 'p2_color ', Color.R, ' ', Color.G, ' ', Color.B);
2097 WriteLn(f, 'p2_model ', QuoteStr(Model));
2098 end;
2100 // all cvars
2101 for i := 0 to High(commands) do
2102 begin
2103 if not commands[i].cheat then
2104 begin
2105 if @commands[i].procEx = @boolVarHandler then
2106 begin
2107 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
2108 WriteLn(f, commands[i].cmd, ' ', j)
2109 end
2110 else if @commands[i].procEx = @intVarHandler then
2111 begin
2112 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
2113 end
2114 else if @commands[i].procEx = @wordVarHandler then
2115 begin
2116 WriteLn(f, commands[i].cmd, ' ', PWord(commands[i].ptr)^)
2117 end
2118 else if @commands[i].procEx = @dwordVarHandler then
2119 begin
2120 WriteLn(f, commands[i].cmd, ' ', PCardinal(commands[i].ptr)^)
2121 end
2122 else if @commands[i].procEx = @singleVarHandler then
2123 begin
2124 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
2125 end
2126 else if @commands[i].procEx = @strVarHandler then
2127 begin
2128 if Length(PAnsiString(commands[i].ptr)^) = 0 then
2129 WriteLn(f, commands[i].cmd, ' ""')
2130 else
2131 WriteLn(f, commands[i].cmd, ' ', QuoteStr(PAnsiString(commands[i].ptr)^))
2132 end
2133 end
2134 end;
2136 WriteLn(f, 'r_maxfps ', gMaxFPS);
2137 WriteLn(f, 'r_reset');
2138 CloseFile(f)
2139 end;
2141 procedure g_Console_WriteGameConfig;
2142 var s: AnsiString;
2143 begin
2144 if gParsingBinds = false then
2145 begin
2146 s := e_GetWriteableDir(ConfigDirs);
2147 g_Console_WriteConfig(e_CatPath(s, gConfigScript))
2148 end
2149 end;
2151 procedure Init;
2152 var i: Integer;
2153 begin
2154 conRegVar('chat_at_top', @ChatTop, 'draw chat at top border', 'draw chat at top border');
2155 conRegVar('console_height', @ConsoleHeight, 0.0, 1.0, 'set console size', 'set console size');
2156 conRegVar('console_trans', @ConsoleTrans, 0.0, 1.0, 'set console transparency', 'set console transparency');
2157 conRegVar('console_step', @ConsoleStep, 0.0, 1.0, 'set console animation speed', 'set console animation speed');
2158 {$IFDEF ANDROID}
2159 ChatTop := True;
2160 ConsoleHeight := 0.35;
2161 {$ELSE}
2162 ChatTop := False;
2163 ConsoleHeight := 0.5;
2164 {$ENDIF}
2165 ConsoleTrans := 0.1;
2166 ConsoleStep := 0.07;
2167 conRegVar('d_eres', @debug_e_res, '', '');
2168 for i := 1 to e_MaxJoys do
2169 conRegVar('joy' + IntToStr(i) + '_deadzone', @e_JoystickDeadzones[i - 1], '', '')
2170 end;
2172 initialization
2173 Init
2174 end.