DEADSOFTWARE

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