DEADSOFTWARE

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