DEADSOFTWARE

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