DEADSOFTWARE

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