DEADSOFTWARE

game: add option to disable hits on friendly players
[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_friendly_hit_trace', GameCVars);
1043 AddCommand('g_friendly_hit_projectile', GameCVars);
1044 AddCommand('g_weaponstay', GameCVars);
1045 AddCommand('g_allow_exit', GameCVars);
1046 AddCommand('g_dm_keys', GameCVars);
1047 AddCommand('g_allow_monsters', GameCVars);
1048 AddCommand('g_bot_vsmonsters', GameCVars);
1049 AddCommand('g_bot_vsplayers', GameCVars);
1050 AddCommand('g_scorelimit', GameCVars);
1051 AddCommand('g_timelimit', GameCVars);
1052 AddCommand('g_maxlives', GameCVars);
1053 AddCommand('g_warmup_time', GameCVars);
1054 AddCommand('g_spawn_invul', GameCVars);
1055 AddCommand('g_item_respawn_time', GameCVars);
1056 AddCommand('sv_intertime', GameCVars);
1058 AddCommand('sv_name', NetServerCVars);
1059 AddCommand('sv_passwd', NetServerCVars);
1060 AddCommand('sv_maxplrs', NetServerCVars);
1061 AddCommand('sv_public', NetServerCVars);
1062 AddCommand('sv_port', NetServerCVars);
1064 AddCommand('pause', GameCommands);
1065 AddCommand('endgame', GameCommands);
1066 AddCommand('restart', GameCommands);
1067 AddCommand('addbot', GameCommands);
1068 AddCommand('bot_add', GameCommands);
1069 AddCommand('bot_addlist', GameCommands);
1070 AddCommand('bot_addred', GameCommands);
1071 AddCommand('bot_addblue', GameCommands);
1072 AddCommand('bot_removeall', GameCommands);
1073 AddCommand('chat', GameCommands);
1074 AddCommand('teamchat', GameCommands);
1075 AddCommand('game', GameCommands);
1076 AddCommand('host', GameCommands);
1077 AddCommand('map', GameCommands);
1078 AddCommand('nextmap', GameCommands);
1079 AddCommand('endmap', GameCommands);
1080 AddCommand('goodbye', GameCommands);
1081 AddCommand('suicide', GameCommands);
1082 AddCommand('spectate', GameCommands);
1083 AddCommand('ready', GameCommands);
1084 AddCommand('kick', GameCommands);
1085 AddCommand('kick_id', GameCommands);
1086 AddCommand('kick_pid', GameCommands);
1087 AddCommand('ban', GameCommands);
1088 AddCommand('ban_id', GameCommands);
1089 AddCommand('ban_pid', GameCommands);
1090 AddCommand('permban', GameCommands);
1091 AddCommand('permban_id', GameCommands);
1092 AddCommand('permban_pid', GameCommands);
1093 AddCommand('permban_ip', GameCommands);
1094 AddCommand('unban', GameCommands);
1095 AddCommand('connect', GameCommands);
1096 AddCommand('disconnect', GameCommands);
1097 AddCommand('reconnect', GameCommands);
1098 AddCommand('say', GameCommands);
1099 AddCommand('tell', GameCommands);
1100 AddCommand('centerprint', GameCommands);
1101 AddCommand('overtime', GameCommands);
1102 AddCommand('rcon_password', GameCommands);
1103 AddCommand('rcon', GameCommands);
1104 AddCommand('callvote', GameCommands);
1105 AddCommand('vote', GameCommands);
1106 AddCommand('clientlist', GameCommands);
1107 AddCommand('event', GameCommands);
1108 AddCommand('screenshot', GameCommands);
1109 AddCommand('weapon', GameCommands);
1110 AddCommand('p1_weapon', GameCommands);
1111 AddCommand('p2_weapon', GameCommands);
1113 AddCommand('god', GameCheats);
1114 AddCommand('notarget', GameCheats);
1115 AddCommand('give', GameCheats); // "exit" too ;-)
1116 AddCommand('open', GameCheats);
1117 AddCommand('fly', GameCheats);
1118 AddCommand('noclip', GameCheats);
1119 AddCommand('speedy', GameCheats);
1120 AddCommand('jumpy', GameCheats);
1121 AddCommand('noreload', GameCheats);
1122 AddCommand('aimline', GameCheats);
1123 AddCommand('automap', GameCheats);
1125 AddAction('jump', ACTION_JUMP);
1126 AddAction('moveleft', ACTION_MOVELEFT);
1127 AddAction('moveright', ACTION_MOVERIGHT);
1128 AddAction('lookup', ACTION_LOOKUP);
1129 AddAction('lookdown', ACTION_LOOKDOWN);
1130 AddAction('attack', ACTION_ATTACK);
1131 AddAction('scores', ACTION_SCORES);
1132 AddAction('activate', ACTION_ACTIVATE);
1133 AddAction('strafe', ACTION_STRAFE);
1134 AddAction('weapnext', ACTION_WEAPNEXT);
1135 AddAction('weapprev', ACTION_WEAPPREV);
1137 WhitelistCommand('say');
1138 WhitelistCommand('tell');
1139 WhitelistCommand('overtime');
1140 WhitelistCommand('ready');
1141 WhitelistCommand('map');
1142 WhitelistCommand('nextmap');
1143 WhitelistCommand('endmap');
1144 WhitelistCommand('restart');
1145 WhitelistCommand('kick');
1146 WhitelistCommand('kick_pid');
1147 WhitelistCommand('ban');
1148 WhitelistCommand('ban_pid');
1149 WhitelistCommand('centerprint');
1151 WhitelistCommand('addbot');
1152 WhitelistCommand('bot_add');
1153 WhitelistCommand('bot_addred');
1154 WhitelistCommand('bot_addblue');
1155 WhitelistCommand('bot_removeall');
1157 WhitelistCommand('g_gamemode');
1158 WhitelistCommand('g_friendlyfire');
1159 WhitelistCommand('g_friendly_hit_trace');
1160 WhitelistCommand('g_friendly_hit_projectile');
1161 WhitelistCommand('g_weaponstay');
1162 WhitelistCommand('g_allow_exit');
1163 WhitelistCommand('g_dm_keys');
1164 WhitelistCommand('g_allow_monsters');
1165 WhitelistCommand('g_bot_vsmonsters');
1166 WhitelistCommand('g_bot_vsplayers');
1167 WhitelistCommand('g_scorelimit');
1168 WhitelistCommand('g_timelimit');
1169 WhitelistCommand('g_maxlives');
1170 WhitelistCommand('g_warmup_time');
1171 WhitelistCommand('g_spawn_invul');
1172 WhitelistCommand('g_item_respawn_time');
1174 g_Console_ResetBinds;
1175 g_Console_ReadConfig(gConfigScript);
1176 g_Console_ReadConfig(autoexecScript);
1177 gParsingBinds := False;
1178 end;
1180 procedure g_Console_Init;
1181 begin
1182 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
1183 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
1184 g_Console_Add('');
1185 end;
1187 procedure g_Console_Update;
1188 var
1189 a, b, Step: Integer;
1190 begin
1191 if Cons_Shown then
1192 begin
1193 Step := Max(1, Round(Floor(gScreenHeight * ConsoleHeight) * ConsoleStep));
1194 if gConsoleShow then
1195 begin
1196 (* Open animation *)
1197 Cons_Y := Min(Cons_Y + Step, 0);
1198 InputReady := True
1199 end
1200 else
1201 begin
1202 (* Close animation *)
1203 Cons_Y := Max(Cons_Y - Step, -Floor(gScreenHeight * ConsoleHeight));
1204 Cons_Shown := Cons_Y > -Floor(gScreenHeight * ConsoleHeight);
1205 InputReady := False
1206 end;
1208 if gChatShow then
1209 InputReady := True
1210 end;
1212 a := 0;
1213 while a <= High(MsgArray) do
1214 begin
1215 if MsgArray[a].Time > 0 then
1216 begin
1217 if MsgArray[a].Time = 1 then
1218 begin
1219 if a < High(MsgArray) then
1220 begin
1221 for b := a to High(MsgArray)-1 do
1222 MsgArray[b] := MsgArray[b+1];
1224 MsgArray[High(MsgArray)].Time := 0;
1226 a := a - 1;
1227 end;
1228 end
1229 else
1230 Dec(MsgArray[a].Time);
1231 end;
1233 a := a + 1;
1234 end;
1235 end;
1238 procedure drawConsoleText ();
1239 var
1240 CWidth, CHeight: Byte;
1241 ty: Integer;
1242 sp, ep: LongWord;
1243 skip: Integer;
1245 procedure putLine (sp, ep: LongWord);
1246 var
1247 p: LongWord;
1248 wdt, cw: Integer;
1249 begin
1250 p := sp;
1251 wdt := 0;
1252 while p <> ep do
1253 begin
1254 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1255 if wdt+cw > gScreenWidth-8 then break;
1256 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1257 Inc(wdt, cw);
1258 cbufNext(p);
1259 end;
1260 if p <> ep then putLine(p, ep); // do rest of the line first
1261 // now print our part
1262 if skip = 0 then
1263 begin
1264 ep := p;
1265 p := sp;
1266 wdt := 2;
1267 while p <> ep do
1268 begin
1269 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1270 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1271 Inc(wdt, cw);
1272 cbufNext(p);
1273 end;
1274 Dec(ty, CHeight);
1275 end
1276 else
1277 begin
1278 Dec(skip);
1279 end;
1280 end;
1282 begin
1283 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1284 ty := Floor(gScreenHeight * ConsoleHeight) - 4 - 2 * CHeight - Abs(Cons_Y);
1285 skip := conSkipLines;
1286 cbufLastLine(sp, ep);
1287 repeat
1288 putLine(sp, ep);
1289 if ty+CHeight <= 0 then break;
1290 until not cbufLineUp(sp, ep);
1291 end;
1293 procedure g_Console_Draw(MessagesOnly: Boolean = False);
1294 var
1295 CWidth, CHeight: Byte;
1296 mfW, mfH: Word;
1297 a, b, offset_y: Integer;
1298 begin
1299 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1301 if ChatTop and gChatShow then
1302 offset_y := CHeight
1303 else
1304 offset_y := 0;
1306 for a := 0 to High(MsgArray) do
1307 if MsgArray[a].Time > 0 then
1308 e_TextureFontPrintFmt(0, offset_y + CHeight * a, MsgArray[a].Msg, gStdFont, True);
1310 if MessagesOnly then Exit;
1312 if gChatShow then
1313 begin
1314 if ChatTop then
1315 offset_y := 0
1316 else
1317 offset_y := gScreenHeight - CHeight - 1;
1318 if gChatTeam then
1319 begin
1320 e_TextureFontPrintEx(0, offset_y, 'say team> ' + Line, gStdFont, 255, 255, 255, 1, True);
1321 e_TextureFontPrintEx((CPos + 9) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1322 end
1323 else
1324 begin
1325 e_TextureFontPrintEx(0, offset_y, 'say> ' + Line, gStdFont, 255, 255, 255, 1, True);
1326 e_TextureFontPrintEx((CPos + 4) * CWidth, offset_y, '_', gStdFont, 255, 255, 255, 1, True);
1327 end
1328 end;
1330 if not Cons_Shown then
1331 Exit;
1333 if gDebugMode then
1334 begin
1335 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1336 a := (gScreenWidth - 2*mfW) div 2;
1337 b := Cons_Y + (Floor(gScreenHeight * ConsoleHeight) - 2 * mfH) div 2;
1338 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1339 _RGB(128, 0, 0), 2.0);
1340 end;
1342 e_DrawSize(ID, 0, Cons_Y, Round(ConsoleTrans * 255), False, False, gScreenWidth, Floor(gScreenHeight * ConsoleHeight));
1343 e_TextureFontPrint(0, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - CHeight - 4, '> ' + Line, gStdFont);
1345 drawConsoleText();
1346 (*
1347 if ConsoleHistory <> nil then
1348 begin
1349 b := 0;
1350 if CHeight > 0 then
1351 if Length(ConsoleHistory) > (Floor(gScreenHeight * ConsoleHeight) div CHeight) - 1 then
1352 b := Length(ConsoleHistory) - (Floor(gScreenHeight * ConsoleHeight) div CHeight) + 1;
1354 b := Max(b-Offset, 0);
1355 d := Max(High(ConsoleHistory)-Offset, 0);
1357 c := 2;
1358 for a := d downto b do
1359 begin
1360 e_TextureFontPrintFmt(0, Floor(gScreenHeight * ConsoleHeight) - 4 - c * CHeight - Abs(Cons_Y), ConsoleHistory[a], gStdFont, True);
1361 c := c + 1;
1362 end;
1363 end;
1364 *)
1366 e_TextureFontPrint((CPos + 1) * CWidth, Cons_Y + Floor(gScreenHeight * ConsoleHeight) - 21, '_', gStdFont);
1367 end;
1369 procedure g_Console_Char(C: AnsiChar);
1370 begin
1371 if InputReady and (gConsoleShow or gChatShow) then
1372 begin
1373 Insert(C, Line, CPos);
1374 CPos := CPos + 1;
1375 end
1376 end;
1379 var
1380 tcomplist: array of AnsiString = nil;
1381 tcompidx: array of Integer = nil;
1383 procedure Complete ();
1384 var
1385 i, c: Integer;
1386 tused: Integer;
1387 ll, lpfx, cmd: AnsiString;
1388 begin
1389 if (Length(Line) = 0) then
1390 begin
1391 g_Console_Add('');
1392 for i := 0 to High(commands) do
1393 begin
1394 // hidden commands are hidden when cheats aren't enabled
1395 if commands[i].hidden and not conIsCheatsEnabled then continue;
1396 if (Length(commands[i].help) > 0) then
1397 begin
1398 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1399 end
1400 else
1401 begin
1402 g_Console_Add(' '+commands[i].cmd);
1403 end;
1404 end;
1405 exit;
1406 end;
1408 ll := LowerCase(Line);
1409 lpfx := '';
1411 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1412 begin
1413 ll := Copy(ll, 0, Length(ll)-1);
1414 for i := 0 to High(commands) do
1415 begin
1416 // hidden commands are hidden when cheats aren't enabled
1417 if commands[i].hidden and not conIsCheatsEnabled then continue;
1418 if (commands[i].cmd = ll) then
1419 begin
1420 if (Length(commands[i].help) > 0) then
1421 begin
1422 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1423 end;
1424 end;
1425 end;
1426 exit;
1427 end;
1429 // build completion list
1430 tused := 0;
1431 for i := 0 to High(commands) do
1432 begin
1433 // hidden commands are hidden when cheats aren't enabled
1434 if commands[i].hidden and not conIsCheatsEnabled then continue;
1435 cmd := commands[i].cmd;
1436 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1437 begin
1438 if (tused = Length(tcomplist)) then
1439 begin
1440 SetLength(tcomplist, Length(tcomplist)+128);
1441 SetLength(tcompidx, Length(tcompidx)+128);
1442 end;
1443 tcomplist[tused] := cmd;
1444 tcompidx[tused] := i;
1445 Inc(tused);
1446 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1447 end;
1448 end;
1450 // get longest prefix
1451 for i := 0 to tused-1 do
1452 begin
1453 cmd := tcomplist[i];
1454 for c := 1 to Length(lpfx) do
1455 begin
1456 if (c > Length(cmd)) then break;
1457 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1458 end;
1459 end;
1461 if (tused = 0) then exit;
1463 if (tused = 1) then
1464 begin
1465 Line := tcomplist[0]+' ';
1466 CPos := Length(Line)+1;
1467 end
1468 else
1469 begin
1470 // has longest prefix?
1471 if (Length(lpfx) > Length(ll)) then
1472 begin
1473 Line := lpfx;
1474 CPos:= Length(Line)+1;
1475 end
1476 else
1477 begin
1478 g_Console_Add('');
1479 for i := 0 to tused-1 do
1480 begin
1481 if (Length(commands[tcompidx[i]].help) > 0) then
1482 begin
1483 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1484 end
1485 else
1486 begin
1487 g_Console_Add(' '+tcomplist[i]);
1488 end;
1489 end;
1490 end;
1491 end;
1492 end;
1495 procedure g_Console_Control(K: Word);
1496 begin
1497 case K of
1498 IK_BACKSPACE:
1499 if (Length(Line) > 0) and (CPos > 1) then
1500 begin
1501 Delete(Line, CPos-1, 1);
1502 CPos := CPos-1;
1503 end;
1504 IK_DELETE:
1505 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1506 Delete(Line, CPos, 1);
1507 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1508 if CPos > 1 then
1509 CPos := CPos - 1;
1510 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1511 if CPos <= Length(Line) then
1512 CPos := CPos + 1;
1513 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1514 begin
1515 if gConsoleShow then
1516 g_Console_Process(Line)
1517 else
1518 if gChatShow then
1519 begin
1520 if (Length(Line) > 0) and g_Game_IsNet then
1521 begin
1522 if gChatTeam then
1523 begin
1524 if g_Game_IsClient then
1525 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1526 else
1527 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1528 NET_CHAT_TEAM, gPlayer1Settings.Team);
1529 end
1530 else
1531 begin
1532 if g_Game_IsClient then
1533 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1534 else
1535 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1536 NET_CHAT_PLAYER);
1537 end;
1538 end;
1540 Line := '';
1541 CPos := 1;
1542 gJustChatted := True;
1543 g_Console_Chat_Switch;
1544 InputReady := False;
1545 end;
1546 end;
1547 IK_TAB:
1548 if not gChatShow then
1549 Complete();
1550 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1551 if not gChatShow then
1552 if (CommandHistory <> nil) and
1553 (CmdIndex < Length(CommandHistory)) then
1554 begin
1555 if CmdIndex < Length(CommandHistory)-1 then
1556 CmdIndex := CmdIndex + 1;
1557 Line := CommandHistory[CmdIndex];
1558 CPos := Length(Line) + 1;
1559 end;
1560 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1561 if not gChatShow then
1562 if (CommandHistory <> nil) and
1563 (CmdIndex <= Length(CommandHistory)) then
1564 begin
1565 if CmdIndex > 0 then
1566 CmdIndex := CmdIndex - 1;
1567 Line := CommandHistory[CmdIndex];
1568 Cpos := Length(Line) + 1;
1569 end;
1570 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1571 if not gChatShow then Inc(conSkipLines);
1572 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1573 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1574 IK_HOME, IK_KPHOME:
1575 CPos := 1;
1576 IK_END, IK_KPEND:
1577 CPos := Length(Line) + 1;
1578 IK_A..IK_Z, IK_SPACE, IK_SHIFT, IK_RSHIFT, IK_CAPSLOCK, IK_LBRACKET, IK_RBRACKET,
1579 IK_SEMICOLON, IK_QUOTE, IK_BACKSLASH, IK_SLASH, IK_COMMA, IK_DOT, (*IK_EQUALS,*)
1580 IK_0, IK_1, IK_2, IK_3, IK_4, IK_5, IK_6, IK_7, IK_8, IK_9, IK_MINUS, IK_EQUALS:
1581 (* see TEXTINPUT event *)
1582 end
1583 end;
1585 function GetStr(var Str: AnsiString): AnsiString;
1586 var
1587 a, b: Integer;
1588 begin
1589 Result := '';
1590 if Str[1] = '"' then
1591 begin
1592 for b := 1 to Length(Str) do
1593 if (b = Length(Str)) or (Str[b+1] = '"') then
1594 begin
1595 Result := Copy(Str, 2, b-1);
1596 Delete(Str, 1, b+1);
1597 Str := Trim(Str);
1598 Exit;
1599 end;
1600 end;
1602 for a := 1 to Length(Str) do
1603 if (a = Length(Str)) or (Str[a+1] = ' ') then
1604 begin
1605 Result := Copy(Str, 1, a);
1606 Delete(Str, 1, a+1);
1607 Str := Trim(Str);
1608 Exit;
1609 end;
1610 end;
1612 function ParseString(Str: AnsiString): SSArray;
1613 begin
1614 Result := nil;
1616 Str := Trim(Str);
1618 if Str = '' then
1619 Exit;
1621 while Str <> '' do
1622 begin
1623 SetLength(Result, Length(Result)+1);
1624 Result[High(Result)] := GetStr(Str);
1625 end;
1626 end;
1628 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1630 procedure conmsg (s: AnsiString);
1631 var
1632 a: Integer;
1633 begin
1634 if length(s) = 0 then exit;
1635 for a := 0 to High(MsgArray) do
1636 begin
1637 with MsgArray[a] do
1638 begin
1639 if Time = 0 then
1640 begin
1641 Msg := s;
1642 Time := MsgTime;
1643 exit;
1644 end;
1645 end;
1646 end;
1647 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1648 with MsgArray[High(MsgArray)] do
1649 begin
1650 Msg := L;
1651 Time := MsgTime;
1652 end;
1653 end;
1655 var
1656 f: Integer;
1657 begin
1658 // put it to console
1659 cbufPut(L);
1660 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1662 // now show 'em out of console too
1663 show := show and gAllowConsoleMessages;
1664 if show and gShowMessages then
1665 begin
1666 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1667 while length(L) > 0 do
1668 begin
1669 f := Pos(#10, L);
1670 if f <= 0 then f := length(L)+1;
1671 conmsg(Copy(L, 1, f-1));
1672 Delete(L, 1, f);
1673 end;
1674 end;
1676 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1677 //ConsoleHistory[High(ConsoleHistory)] := L;
1679 (*
1680 {$IFDEF HEADLESS}
1681 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1682 {$ENDIF}
1683 *)
1684 end;
1687 var
1688 consolewriterLastWasEOL: Boolean = false;
1690 procedure consolewriter (constref buf; len: SizeUInt);
1691 var
1692 b: PByte;
1693 begin
1694 if (len < 1) then exit;
1695 b := PByte(@buf);
1696 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1697 while (len > 0) do
1698 begin
1699 if (b[0] <> 13) and (b[0] <> 10) then
1700 begin
1701 cbufPut(AnsiChar(b[0]));
1702 end
1703 else
1704 begin
1705 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1706 cbufPut(#10);
1707 end;
1708 len -= 1;
1709 b += 1;
1710 end;
1711 end;
1714 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1715 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1716 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1717 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1718 begin
1719 g_Console_Add(s, show);
1720 end;
1723 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1724 begin
1725 if show then
1726 begin
1727 g_Console_Add(formatstrf(s, args), true);
1728 end
1729 else
1730 begin
1731 consolewriterLastWasEOL := false;
1732 formatstrf(s, args, consolewriter);
1733 if not consolewriterLastWasEOL then cbufPut(#10);
1734 end;
1735 end;
1738 procedure g_Console_Clear();
1739 begin
1740 //ConsoleHistory := nil;
1741 cbufClear();
1742 conSkipLines := 0;
1743 end;
1745 procedure AddToHistory(L: AnsiString);
1746 var
1747 len: Integer;
1748 begin
1749 len := Length(CommandHistory);
1751 if (len = 0) or
1752 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1753 begin
1754 SetLength(CommandHistory, len+1);
1755 CommandHistory[len] := L;
1756 end;
1758 CmdIndex := Length(CommandHistory);
1759 end;
1761 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1762 var
1763 Arr: SSArray;
1764 i: Integer;
1765 begin
1766 Result := True;
1768 Arr := nil;
1770 if Trim(C) = '' then
1771 Exit;
1773 Arr := ParseString(C);
1774 if Arr = nil then
1775 Exit;
1777 for i := 0 to High(Whitelist) do
1778 if Whitelist[i] = LowerCase(Arr[0]) then
1779 Result := False;
1780 end;
1782 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1783 var
1784 Arr: SSArray;
1785 i: Integer;
1786 begin
1787 Arr := nil;
1789 if Trim(L) = '' then
1790 Exit;
1792 conSkipLines := 0; // "unscroll"
1794 if L = 'goobers' then
1795 begin
1796 Line := '';
1797 CPos := 1;
1798 gCheats := true;
1799 g_Console_Add('Your memory serves you well.');
1800 exit;
1801 end;
1803 if not quiet then
1804 begin
1805 g_Console_Add('> '+L);
1806 Line := '';
1807 CPos := 1;
1808 end;
1810 Arr := ParseString(L);
1811 if Arr = nil then
1812 Exit;
1814 if commands = nil then
1815 Exit;
1817 if not quiet then
1818 AddToHistory(L);
1820 for i := 0 to High(commands) do
1821 begin
1822 if commands[i].cmd = LowerCase(Arr[0]) then
1823 begin
1824 if commands[i].action >= 0 then
1825 begin
1826 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1827 exit
1828 end;
1829 if assigned(commands[i].procEx) then
1830 begin
1831 commands[i].procEx(@commands[i], Arr);
1832 exit
1833 end;
1834 if assigned(commands[i].proc) then
1835 begin
1836 commands[i].proc(Arr);
1837 exit
1838 end
1839 end
1840 end;
1842 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1843 end;
1846 function g_Console_Interactive: Boolean;
1847 begin
1848 Result := gConsoleShow
1849 end;
1851 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
1852 begin
1853 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1854 ASSERT(key >= 0);
1855 ASSERT(key < e_MaxInputKeys);
1856 if key > 0 then
1857 begin
1858 gInputBinds[key].rep := False;
1859 gInputBinds[key].down := ParseAlias(down);
1860 gInputBinds[key].up := ParseAlias(up);
1861 end;
1862 g_Console_WriteGameConfig();
1863 end;
1865 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
1867 function EqualsCommandLists (a, b: SSArray): Boolean;
1868 var i, len: Integer;
1869 begin
1870 result := False;
1871 len := Length(a);
1872 if len = Length(b) then
1873 begin
1874 i := 0;
1875 while (i < len) and (a[i] = b[i]) do inc(i);
1876 if i >= len then
1877 result := True
1878 end
1879 end;
1881 begin
1882 ASSERT(key >= 0);
1883 ASSERT(key < e_MaxInputKeys);
1884 result := EqualsCommandLists(ParseAlias(down), gInputBinds[key].down) and EqualsCommandLists(ParseAlias(up), gInputBinds[key].up)
1885 end;
1887 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1888 var i: Integer;
1889 begin
1890 ASSERT(n >= 1);
1891 result := 0;
1892 if commands = nil then Exit;
1893 i := 0;
1894 while (n >= 1) and (i < e_MaxInputKeys) do
1895 begin
1896 if g_Console_MatchBind(i, down, up) then
1897 begin
1898 result := i;
1899 dec(n)
1900 end;
1901 inc(i)
1902 end;
1903 if n >= 1 then
1904 result := 0
1905 end;
1907 function g_Console_Action (action: Integer): Boolean;
1908 var i, len: Integer;
1909 begin
1910 ASSERT(action >= FIRST_ACTION);
1911 ASSERT(action <= LAST_ACTION);
1912 i := 0;
1913 len := Length(gPlayerAction);
1914 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1915 Result := i < len
1916 end;
1918 function BindsAllowed (key: Integer): Boolean;
1919 begin
1920 Result := False;
1921 if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1922 begin
1923 if gChatShow then
1924 Result := g_Console_MatchBind(key, 'togglemenu') or
1925 g_Console_MatchBind(key, 'showkeyboard') or
1926 g_Console_MatchBind(key, 'hidekeyboard')
1927 else if gConsoleShow or (g_ActiveWindow <> nil) or (gGameSettings.GameType = GT_NONE) then
1928 Result := g_Console_MatchBind(key, 'togglemenu') or
1929 g_Console_MatchBind(key, 'toggleconsole') or
1930 g_Console_MatchBind(key, 'showkeyboard') or
1931 g_Console_MatchBind(key, 'hidekeyboard')
1932 else (* in game *)
1933 Result := True
1934 end
1935 end;
1937 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1938 var i: Integer;
1939 begin
1940 if BindsAllowed(key) then
1941 begin
1942 if down then
1943 for i := 0 to High(gInputBinds[key].down) do
1944 g_Console_Process(gInputBinds[key].down[i], True)
1945 else
1946 for i := 0 to High(gInputBinds[key].up) do
1947 g_Console_Process(gInputBinds[key].up[i], True)
1948 end;
1949 if down and not menu_toggled then
1950 KeyPress(key);
1951 menu_toggled := False
1952 end;
1954 procedure g_Console_ProcessBindRepeat (key: Integer);
1955 var i: Integer;
1956 begin
1957 if gConsoleShow or gChatShow or (g_ActiveWindow <> nil) then
1958 begin
1959 KeyPress(key); // key repeat in menus and shit
1960 Exit;
1961 end;
1962 if BindsAllowed(key) and gInputBinds[key].rep then
1963 begin
1964 for i := 0 to High(gInputBinds[key].down) do
1965 g_Console_Process(gInputBinds[key].down[i], True);
1966 end;
1967 end;
1969 procedure g_Console_ResetBinds;
1970 var i: Integer;
1971 begin
1972 for i := 0 to e_MaxInputKeys - 1 do
1973 g_Console_BindKey(i, '', '');
1975 g_Console_BindKey(IK_GRAVE, 'toggleconsole');
1976 g_Console_BindKey(IK_ESCAPE, 'togglemenu');
1977 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1978 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1979 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1980 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1981 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1982 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1983 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1984 g_Console_BindKey(IK_E, '+p1_weapnext', '-p1_weapnext');
1985 g_Console_BindKey(IK_Q, '+p1_weapprev', '-p1_weapprev');
1986 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1987 g_Console_BindKey(IK_1, 'p1_weapon 1');
1988 g_Console_BindKey(IK_2, 'p1_weapon 2');
1989 g_Console_BindKey(IK_3, 'p1_weapon 3');
1990 g_Console_BindKey(IK_4, 'p1_weapon 4');
1991 g_Console_BindKey(IK_5, 'p1_weapon 5');
1992 g_Console_BindKey(IK_6, 'p1_weapon 6');
1993 g_Console_BindKey(IK_7, 'p1_weapon 7');
1994 g_Console_BindKey(IK_8, 'p1_weapon 8');
1995 g_Console_BindKey(IK_9, 'p1_weapon 9');
1996 g_Console_BindKey(IK_0, 'p1_weapon 10');
1997 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1998 g_Console_BindKey(IK_T, 'togglechat');
1999 g_Console_BindKey(IK_Y, 'toggleteamchat');
2000 g_Console_BindKey(IK_F11, 'screenshot');
2001 g_Console_BindKey(IK_TAB, '+p1_scores', '-p1_scores');
2002 g_Console_BindKey(IK_PAUSE, 'pause');
2003 g_Console_BindKey(IK_F1, 'vote');
2005 (* for i := 0 to e_MaxJoys - 1 do *)
2006 for i := 0 to 1 do
2007 begin
2008 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_LEFT), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
2009 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_RIGHT), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
2010 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_UP), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
2011 g_Console_BindKey(e_JoyHatToKey(i, 0, HAT_DOWN), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
2012 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
2013 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
2014 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
2015 g_Console_BindKey(e_JoyButtonToKey(i, 1), '+p' + IntToStr(i mod 2 + 1) + '_weapnext', '-p' + IntToStr(i mod 2 + 1) + '_weapnext');
2016 g_Console_BindKey(e_JoyButtonToKey(i, 4), '+p' + IntToStr(i mod 2 + 1) + '_weapprev', '-p' + IntToStr(i mod 2 + 1) + '_weapprev');
2017 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
2018 g_Console_BindKey(e_JoyButtonToKey(i, 10), 'togglemenu');
2019 end;
2021 g_Console_BindKey(VK_ESCAPE, 'togglemenu');
2022 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
2023 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
2024 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
2025 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
2026 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
2027 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
2028 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
2029 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
2030 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
2031 g_Console_BindKey(VK_NEXT, '+weapnext', '-weapnext');
2032 g_Console_BindKey(VK_PREV, '+weapprev', '-weapprev');
2033 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
2034 g_Console_BindKey(VK_0, 'weapon 1');
2035 g_Console_BindKey(VK_1, 'weapon 2');
2036 g_Console_BindKey(VK_2, 'weapon 3');
2037 g_Console_BindKey(VK_3, 'weapon 4');
2038 g_Console_BindKey(VK_4, 'weapon 5');
2039 g_Console_BindKey(VK_5, 'weapon 6');
2040 g_Console_BindKey(VK_6, 'weapon 7');
2041 g_Console_BindKey(VK_7, 'weapon 8');
2042 g_Console_BindKey(VK_8, 'weapon 9');
2043 g_Console_BindKey(VK_9, 'weapon 10');
2044 g_Console_BindKey(VK_A, 'weapon 11');
2045 g_Console_BindKey(VK_CHAT, 'togglechat');
2046 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
2047 g_Console_BindKey(VK_CONSOLE, 'toggleconsole');
2048 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
2049 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
2050 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
2051 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
2052 end;
2054 procedure g_Console_ReadConfig (filename: String);
2055 var f: TextFile; s: AnsiString; i, len: Integer;
2056 begin
2057 e_LogWritefln('g_Console_ReadConfig (1) "%s"', [filename]);
2058 if e_FindResource(ConfigDirs, filename, false) = true then
2059 begin
2060 e_LogWritefln('g_Console_ReadConfig (2) "%s"', [filename]);
2061 AssignFile(f, filename);
2062 Reset(f);
2063 while not EOF(f) do
2064 begin
2065 ReadLn(f, s);
2066 len := Length(s);
2067 if len > 0 then
2068 begin
2069 i := 1;
2070 (* skip spaces *)
2071 while (i <= len) and (s[i] <= ' ') do inc(i);
2072 (* skip comments *)
2073 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
2074 g_Console_Process(s, True);
2075 end
2076 end;
2077 CloseFile(f);
2078 end
2079 end;
2081 procedure g_Console_WriteConfig (filename: String);
2082 var f: TextFile; i, j: Integer;
2084 procedure WriteFlag(name: string; flag: LongWord);
2085 begin
2086 WriteLn(f, name, IfThen(LongBool(gsGameFlags and flag), 1, 0));
2087 end;
2089 begin
2090 AssignFile(f, filename);
2091 Rewrite(f);
2092 WriteLn(f, '// ' + configComment);
2094 // binds
2095 WriteLn(f, 'unbindall');
2096 for i := 0 to e_MaxInputKeys - 1 do
2097 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
2098 begin
2099 Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
2100 if Length(gInputBinds[i].down) = 0 then
2101 Write(f, '""');
2102 if Length(gInputBinds[i].up) > 0 then
2103 Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
2104 WriteLn(f, '');
2105 if gInputBinds[i].rep then
2106 WriteLn(f, 'bindrep ', e_KeyNames[i]);
2107 end;
2109 // lang
2110 if gAskLanguage then
2111 WriteLn(f, 'g_language ask')
2112 else
2113 WriteLn(f, 'g_language ', gLanguage);
2115 // net server
2116 WriteLn(f, 'sv_name ', QuoteStr(NetServerName));
2117 WriteLn(f, 'sv_passwd ', QuoteStr(NetPassword));
2118 WriteLn(f, 'sv_maxplrs ', NetMaxClients);
2119 WriteLn(f, 'sv_port ', NetPort);
2120 WriteLn(f, 'sv_public ', IfThen(NetUseMaster, 1, 0));
2122 // game settings
2123 WriteLn(f, 'g_max_particles ', g_GFX_GetMax());
2124 WriteLn(f, 'g_max_shells ', g_Shells_GetMax());
2125 WriteLn(f, 'g_max_gibs ', g_Gibs_GetMax());
2126 WriteLn(f, 'g_max_corpses ', g_Corpses_GetMax());
2127 WriteLn(f, 'sv_intertime ', gDefInterTime);
2129 // gameplay settings
2130 WriteLn(f, 'g_gamemode ', gsGameMode);
2131 WriteLn(f, 'g_scorelimit ', gsGoalLimit);
2132 WriteLn(f, 'g_timelimit ', gsTimeLimit);
2133 WriteLn(f, 'g_maxlives ', gsMaxLives);
2134 WriteLn(f, 'g_item_respawn_time ', gsItemRespawnTime);
2135 WriteLn(f, 'g_spawn_invul ', gsSpawnInvul);
2136 WriteLn(f, 'g_warmup_time ', gsWarmupTime);
2138 WriteFlag('g_friendlyfire ', GAME_OPTION_TEAMDAMAGE);
2139 WriteFlag('g_friendly_hit_trace ', GAME_OPTION_TEAMHITTRACE);
2140 WriteFlag('g_friendly_hit_projectile ', GAME_OPTION_TEAMHITPROJECTILE);
2141 WriteFlag('g_allow_exit ', GAME_OPTION_ALLOWEXIT);
2142 WriteFlag('g_allow_monsters ', GAME_OPTION_MONSTERS);
2143 WriteFlag('g_dm_keys ', GAME_OPTION_DMKEYS);
2144 WriteFlag('g_weaponstay ', GAME_OPTION_WEAPONSTAY);
2145 WriteFlag('g_bot_vsmonsters ', GAME_OPTION_BOTVSMONSTER);
2146 WriteFlag('g_bot_vsplayers ', GAME_OPTION_BOTVSPLAYER);
2148 // players
2149 with gPlayer1Settings do
2150 begin
2151 WriteLn(f, 'p1_name ', QuoteStr(Name));
2152 WriteLn(f, 'p1_color ', Color.R, ' ', Color.G, ' ', Color.B);
2153 WriteLn(f, 'p1_model ', QuoteStr(Model));
2154 end;
2155 with gPlayer2Settings do
2156 begin
2157 WriteLn(f, 'p2_name ', QuoteStr(Name));
2158 WriteLn(f, 'p2_color ', Color.R, ' ', Color.G, ' ', Color.B);
2159 WriteLn(f, 'p2_model ', QuoteStr(Model));
2160 end;
2162 // all cvars
2163 for i := 0 to High(commands) do
2164 begin
2165 if not commands[i].cheat then
2166 begin
2167 if @commands[i].procEx = @boolVarHandler then
2168 begin
2169 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
2170 WriteLn(f, commands[i].cmd, ' ', j)
2171 end
2172 else if @commands[i].procEx = @intVarHandler then
2173 begin
2174 WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
2175 end
2176 else if @commands[i].procEx = @wordVarHandler then
2177 begin
2178 WriteLn(f, commands[i].cmd, ' ', PWord(commands[i].ptr)^)
2179 end
2180 else if @commands[i].procEx = @dwordVarHandler then
2181 begin
2182 WriteLn(f, commands[i].cmd, ' ', PCardinal(commands[i].ptr)^)
2183 end
2184 else if @commands[i].procEx = @singleVarHandler then
2185 begin
2186 WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
2187 end
2188 else if @commands[i].procEx = @strVarHandler then
2189 begin
2190 if Length(PAnsiString(commands[i].ptr)^) = 0 then
2191 WriteLn(f, commands[i].cmd, ' ""')
2192 else
2193 WriteLn(f, commands[i].cmd, ' ', QuoteStr(PAnsiString(commands[i].ptr)^))
2194 end
2195 end
2196 end;
2198 WriteLn(f, 'r_maxfps ', gMaxFPS);
2199 WriteLn(f, 'r_reset');
2200 CloseFile(f)
2201 end;
2203 procedure g_Console_WriteGameConfig;
2204 var s: AnsiString;
2205 begin
2206 if gParsingBinds = false then
2207 begin
2208 s := e_GetWriteableDir(ConfigDirs);
2209 g_Console_WriteConfig(e_CatPath(s, gConfigScript))
2210 end
2211 end;
2213 procedure Init;
2214 var i: Integer;
2215 begin
2216 conRegVar('chat_at_top', @ChatTop, 'draw chat at top border', 'draw chat at top border');
2217 conRegVar('console_height', @ConsoleHeight, 0.0, 1.0, 'set console size', 'set console size');
2218 conRegVar('console_trans', @ConsoleTrans, 0.0, 1.0, 'set console transparency', 'set console transparency');
2219 conRegVar('console_step', @ConsoleStep, 0.0, 1.0, 'set console animation speed', 'set console animation speed');
2220 {$IFDEF ANDROID}
2221 ChatTop := True;
2222 ConsoleHeight := 0.35;
2223 {$ELSE}
2224 ChatTop := False;
2225 ConsoleHeight := 0.5;
2226 {$ENDIF}
2227 ConsoleTrans := 0.1;
2228 ConsoleStep := 0.07;
2229 conRegVar('d_eres', @debug_e_res, '', '');
2230 for i := 1 to e_MaxJoys do
2231 conRegVar('joy' + IntToStr(i) + '_deadzone', @e_JoystickDeadzones[i - 1], '', '')
2232 end;
2234 initialization
2235 Init
2236 end.