DEADSOFTWARE

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