DEADSOFTWARE

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