DEADSOFTWARE

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