DEADSOFTWARE

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