DEADSOFTWARE

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