DEADSOFTWARE

added warnings for bind/unbind
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_console;
19 interface
21 uses
22 utils; // for SSArray
24 const
25 ACTION_JUMP = 0;
26 ACTION_MOVELEFT = 1;
27 ACTION_MOVERIGHT = 2;
28 ACTION_LOOKDOWN = 3;
29 ACTION_LOOKUP = 4;
30 ACTION_ATTACK = 5;
31 ACTION_SCORES = 6;
32 ACTION_ACTIVATE = 7;
33 ACTION_STRAFE = 8;
34 ACTION_WEAPNEXT = 9;
35 ACTION_WEAPPREV = 10;
37 FIRST_ACTION = ACTION_JUMP;
38 LAST_ACTION = ACTION_WEAPPREV;
40 procedure g_Console_Init ();
41 procedure g_Console_Update ();
42 procedure g_Console_Draw ();
43 procedure g_Console_Switch ();
44 procedure g_Console_Char (C: AnsiChar);
45 procedure g_Console_Control (K: Word);
46 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
47 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
48 procedure g_Console_Clear ();
49 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
50 procedure g_Console_ReadConfig (filename: String);
51 procedure g_Console_WriteConfig (filename: String);
53 function g_Console_Interactive: Boolean;
54 function g_Console_Action (action: Integer): Boolean;
55 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
56 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
57 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
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 // <0: no arg; 0/1: true/false
64 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
66 procedure g_Console_Chat_Switch (team: Boolean=false);
68 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
69 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
70 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
72 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
73 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
76 var
77 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
78 gChatShow: Boolean = false;
79 gChatTeam: Boolean = false;
80 gAllowConsoleMessages: Boolean = true;
81 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
82 gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
84 implementation
86 uses
87 g_textures, g_main, e_graphics, e_input, g_game,
88 SysUtils, g_basic, g_options, Math, g_touch,
89 g_menu, g_language, g_net, g_netmsg, e_log, conbuf;
92 type
93 PCommand = ^TCommand;
95 TCmdProc = procedure (p: SSArray);
96 TCmdProcEx = procedure (me: PCommand; p: SSArray);
98 TCommand = record
99 cmd: AnsiString;
100 proc: TCmdProc;
101 procEx: TCmdProcEx;
102 help: AnsiString;
103 hidden: Boolean;
104 ptr: Pointer; // various data
105 msg: AnsiString; // message for var changes
106 cheat: Boolean;
107 action: Integer; // >= 0 for action commands
108 player: Integer; // used for action commands
109 end;
111 TAlias = record
112 name: AnsiString;
113 commands: SSArray;
114 end;
117 const
118 Step = 32;
119 Alpha = 25;
120 MsgTime = 144;
121 MaxScriptRecursion = 16;
123 DEBUG_STRING = 'DEBUG MODE';
125 var
126 ID: DWORD;
127 RecursionDepth: Word = 0;
128 RecursionLimitHit: Boolean = False;
129 Cons_Y: SmallInt;
130 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
131 Line: AnsiString;
132 CPos: Word;
133 //ConsoleHistory: SSArray;
134 CommandHistory: SSArray;
135 Whitelist: SSArray;
136 commands: Array of TCommand = nil;
137 Aliases: Array of TAlias = nil;
138 CmdIndex: Word;
139 conSkipLines: Integer = 0;
140 MsgArray: Array [0..4] of record
141 Msg: AnsiString;
142 Time: Word;
143 end;
145 gInputBinds: Array [0..e_MaxInputKeys - 1] of record
146 down, up: SSArray;
147 end;
150 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
151 function conParseFloat (var res: Single; const s: AnsiString): Boolean;
152 var
153 pos: Integer = 1;
154 frac: Single = 1;
155 slen: Integer;
156 begin
157 result := false;
158 res := 0;
159 slen := Length(s);
160 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
161 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
162 if (pos > slen) then exit;
163 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
164 // integral part
165 while (pos <= slen) do
166 begin
167 if (s[pos] < '0') or (s[pos] > '9') then break;
168 res := res*10+Byte(s[pos])-48;
169 Inc(pos);
170 end;
171 if (pos <= slen) then
172 begin
173 // must be a dot
174 if (s[pos] <> '.') then exit;
175 Inc(pos);
176 while (pos <= slen) do
177 begin
178 if (s[pos] < '0') or (s[pos] > '9') then break;
179 frac := frac/10;
180 res += frac*(Byte(s[pos])-48);
181 Inc(pos);
182 end;
183 end;
184 if (pos <= slen) then exit; // oops
185 result := true;
186 end;
189 // ////////////////////////////////////////////////////////////////////////// //
190 // <0: no arg; 0/1: true/false; 666: toggle
191 function conGetBoolArg (p: SSArray; idx: Integer): Integer;
192 begin
193 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
194 result := 0;
195 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
196 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
197 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
198 (CompareText(p[idx], 't') = 0) then result := 666;
199 end;
202 procedure boolVarHandler (me: PCommand; p: SSArray);
203 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
204 begin
205 if (Length(p) > 2) then
206 begin
207 conwritefln('too many arguments to ''%s''', [p[0]]);
208 end
209 else
210 begin
211 case conGetBoolArg(p, 1) of
212 -1: begin end;
213 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
214 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
215 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
216 end;
217 if (Length(msg) = 0) then msg := p[0] else msg += ':';
218 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
219 end;
220 end;
221 begin
222 binaryFlag(PBoolean(me.ptr)^, me.msg);
223 end;
226 procedure intVarHandler (me: PCommand; p: SSArray);
227 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
228 begin
229 if (Length(p) > 2) then
230 begin
231 conwritefln('too many arguments to ''%s''', [p[0]]);
232 end
233 else
234 begin
235 case conGetBoolArg(p, 1) of
236 -1: begin end;
237 0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
238 1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
239 666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
240 end;
241 if (Length(msg) = 0) then msg := p[0] else msg += ':';
242 if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
243 end;
244 end;
245 begin
246 if (Length(p) <> 2) then
247 begin
248 conwritefln('%s %d', [me.cmd, PInteger(me.ptr)^]);
249 end
250 else
251 begin
252 try
253 PInteger(me.ptr)^ := StrToInt(p[1]);
254 except
255 conwritefln('invalid integer value: "%s"', [p[1]]);
256 end;
257 end;
258 end;
261 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
262 var
263 f: Integer;
264 cp: PCommand;
265 begin
266 f := Length(commands);
267 SetLength(commands, f+1);
268 cp := @commands[f];
269 cp.cmd := LowerCase(conname);
270 cp.proc := nil;
271 cp.procEx := boolVarHandler;
272 cp.help := ahelp;
273 cp.hidden := ahidden;
274 cp.ptr := pvar;
275 cp.msg := amsg;
276 cp.cheat := acheat;
277 cp.action := -1;
278 cp.player := -1;
279 end;
282 procedure conRegVar (const conname: AnsiString; pvar: PInteger; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
283 var
284 f: Integer;
285 cp: PCommand;
286 begin
287 f := Length(commands);
288 SetLength(commands, f+1);
289 cp := @commands[f];
290 cp.cmd := LowerCase(conname);
291 cp.proc := nil;
292 cp.procEx := intVarHandler;
293 cp.help := ahelp;
294 cp.hidden := ahidden;
295 cp.ptr := pvar;
296 cp.msg := amsg;
297 cp.cheat := acheat;
298 cp.action := -1;
299 cp.player := -1;
300 end;
303 // ////////////////////////////////////////////////////////////////////////// //
304 type
305 PVarSingle = ^TVarSingle;
306 TVarSingle = record
307 val: PSingle;
308 min, max, def: Single; // default will be starting value
309 end;
312 procedure singleVarHandler (me: PCommand; p: SSArray);
313 var
314 pv: PVarSingle;
315 nv: Single;
316 msg: AnsiString;
317 begin
318 if (Length(p) > 2) then
319 begin
320 conwritefln('too many arguments to ''%s''', [me.cmd]);
321 exit;
322 end;
323 pv := PVarSingle(me.ptr);
324 if (Length(p) = 2) then
325 begin
326 if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
327 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
328 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
329 (CompareText(p[1], 'ona') = 0) then
330 begin
331 pv.val^ := pv.def;
332 end
333 else
334 begin
335 if not conParseFloat(nv, p[1]) then
336 begin
337 conwritefln('%s: ''%s'' doesn''t look like a floating number', [me.cmd, p[1]]);
338 exit;
339 end;
340 if (nv < pv.min) then nv := pv.min;
341 if (nv > pv.max) then nv := pv.max;
342 pv.val^ := nv;
343 end;
344 end;
345 msg := me.msg;
346 if (Length(msg) = 0) then msg := me.cmd else msg += ':';
347 conwritefln('%s %s', [msg, pv.val^]);
348 end;
351 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false; ahidden: Boolean=false); overload;
352 var
353 f: Integer;
354 cp: PCommand;
355 pv: PVarSingle;
356 begin
357 GetMem(pv, sizeof(TVarSingle));
358 pv.val := pvar;
359 pv.min := amin;
360 pv.max := amax;
361 pv.def := pvar^;
362 f := Length(commands);
363 SetLength(commands, f+1);
364 cp := @commands[f];
365 cp.cmd := LowerCase(conname);
366 cp.proc := nil;
367 cp.procEx := singleVarHandler;
368 cp.help := ahelp;
369 cp.hidden := ahidden;
370 cp.ptr := pv;
371 cp.msg := amsg;
372 cp.cheat := acheat;
373 cp.action := -1;
374 cp.player := -1;
375 end;
378 // ////////////////////////////////////////////////////////////////////////// //
379 function GetStrACmd(var Str: AnsiString): AnsiString;
380 var
381 a: Integer;
382 begin
383 Result := '';
384 for a := 1 to Length(Str) do
385 if (a = Length(Str)) or (Str[a+1] = ';') then
386 begin
387 Result := Copy(Str, 1, a);
388 Delete(Str, 1, a+1);
389 Str := Trim(Str);
390 Exit;
391 end;
392 end;
394 function ParseAlias(Str: AnsiString): SSArray;
395 begin
396 Result := nil;
398 Str := Trim(Str);
400 if Str = '' then
401 Exit;
403 while Str <> '' do
404 begin
405 SetLength(Result, Length(Result)+1);
406 Result[High(Result)] := GetStrACmd(Str);
407 end;
408 end;
410 procedure ConsoleCommands(p: SSArray);
411 var
412 cmd, s: AnsiString;
413 a, b: Integer;
414 (* F: TextFile; *)
415 begin
416 cmd := LowerCase(p[0]);
417 s := '';
419 if cmd = 'clear' then
420 begin
421 //ConsoleHistory := nil;
422 cbufClear();
423 conSkipLines := 0;
425 for a := 0 to High(MsgArray) do
426 with MsgArray[a] do
427 begin
428 Msg := '';
429 Time := 0;
430 end;
431 end;
433 if cmd = 'clearhistory' then
434 CommandHistory := nil;
436 if cmd = 'showhistory' then
437 if CommandHistory <> nil then
438 begin
439 g_Console_Add('');
440 for a := 0 to High(CommandHistory) do
441 g_Console_Add(' '+CommandHistory[a]);
442 end;
444 if cmd = 'commands' then
445 begin
446 g_Console_Add('');
447 g_Console_Add('commands list:');
448 for a := High(commands) downto 0 do
449 begin
450 if (Length(commands[a].help) > 0) then
451 begin
452 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
453 end
454 else
455 begin
456 g_Console_Add(' '+commands[a].cmd);
457 end;
458 end;
459 end;
461 if cmd = 'time' then
462 g_Console_Add(TimeToStr(Now), True);
464 if cmd = 'date' then
465 g_Console_Add(DateToStr(Now), True);
467 if cmd = 'echo' then
468 if Length(p) > 1 then
469 begin
470 if p[1] = 'ololo' then
471 gCheats := True
472 else
473 begin
474 s := '';
475 for a := 1 to High(p) do
476 s := s + p[a] + ' ';
477 g_Console_Add(b_Text_Format(s), True);
478 end;
479 end
480 else
481 g_Console_Add('');
483 if cmd = 'dump' then
484 begin
485 (*
486 if ConsoleHistory <> nil then
487 begin
488 if Length(P) > 1 then
489 s := P[1]
490 else
491 s := GameDir+'/console.txt';
493 {$I-}
494 AssignFile(F, s);
495 Rewrite(F);
496 if IOResult <> 0 then
497 begin
498 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
499 CloseFile(F);
500 Exit;
501 end;
503 for a := 0 to High(ConsoleHistory) do
504 WriteLn(F, ConsoleHistory[a]);
506 CloseFile(F);
507 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
508 {$I+}
509 end;
510 *)
511 end;
513 if cmd = 'exec' then
514 begin
515 // exec <filename>
516 if Length(p) = 2 then
517 g_Console_ReadConfig(GameDir + '/' + p[1])
518 else
519 g_Console_Add('exec <script file>');
520 end;
522 if cmd = 'writeconfig' then
523 begin
524 // writeconfig <filename>
525 if Length(p) = 2 then
526 g_Console_WriteConfig(GameDir + '/' + p[1])
527 else
528 g_Console_Add('writeconfig <file>');
529 end;
531 if (cmd = 'ver') or (cmd = 'version') then
532 begin
533 conwriteln('Doom 2D: Forever v. ' + GAME_VERSION);
534 conwritefln('Net protocol v. %d', [NET_PROTOCOL_VER]);
535 conwritefln('Build date: %s at %s', [GAME_BUILDDATE, GAME_BUILDTIME]);
536 end;
538 if cmd = 'alias' then
539 begin
540 // alias [alias_name] [commands]
541 if Length(p) > 1 then
542 begin
543 for a := 0 to High(Aliases) do
544 if Aliases[a].name = p[1] then
545 begin
546 if Length(p) > 2 then
547 Aliases[a].commands := ParseAlias(p[2])
548 else
549 for b := 0 to High(Aliases[a].commands) do
550 g_Console_Add(Aliases[a].commands[b]);
551 Exit;
552 end;
553 SetLength(Aliases, Length(Aliases)+1);
554 a := High(Aliases);
555 Aliases[a].name := p[1];
556 if Length(p) > 2 then
557 Aliases[a].commands := ParseAlias(p[2])
558 else
559 for b := 0 to High(Aliases[a].commands) do
560 g_Console_Add(Aliases[a].commands[b]);
561 end else
562 for a := 0 to High(Aliases) do
563 if Aliases[a].commands <> nil then
564 g_Console_Add(Aliases[a].name);
565 end;
567 if cmd = 'call' then
568 begin
569 // call <alias_name>
570 if Length(p) > 1 then
571 begin
572 if Aliases = nil then
573 Exit;
574 for a := 0 to High(Aliases) do
575 if Aliases[a].name = p[1] then
576 begin
577 if Aliases[a].commands <> nil then
578 begin
579 // with this system proper endless loop detection seems either impossible
580 // or very dirty to implement, so let's have this instead
581 // prevents endless loops
582 for b := 0 to High(Aliases[a].commands) do
583 begin
584 Inc(RecursionDepth);
585 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
586 if not RecursionLimitHit then
587 g_Console_Process(Aliases[a].commands[b], True);
588 Dec(RecursionDepth);
589 end;
590 if (RecursionDepth = 0) and RecursionLimitHit then
591 begin
592 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
593 RecursionLimitHit := False;
594 end;
595 end;
596 Exit;
597 end;
598 end
599 else
600 g_Console_Add('call <alias name>');
601 end;
602 end;
604 procedure WhitelistCommand(cmd: AnsiString);
605 var
606 a: Integer;
607 begin
608 SetLength(Whitelist, Length(Whitelist)+1);
609 a := High(Whitelist);
610 Whitelist[a] := LowerCase(cmd);
611 end;
613 procedure segfault (p: SSArray);
614 var
615 pp: PByte = nil;
616 begin
617 pp^ := 0;
618 end;
620 function GetCommandString (p: SSArray): AnsiString;
621 var i: Integer;
622 begin
623 result := '';
624 if Length(p) >= 1 then
625 begin
626 result := p[0];
627 for i := 1 to High(p) do
628 result := result + '; ' + p[i]
629 end
630 end;
632 procedure BindCommands (p: SSArray);
633 var cmd, key: AnsiString; i: Integer;
634 begin
635 cmd := LowerCase(p[0]);
636 case cmd of
637 'bind':
638 // bind <key> [down [up]]
639 if (Length(p) >= 2) and (Length(p) <= 4) then
640 begin
641 i := 0;
642 key := LowerCase(p[1]);
643 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
644 if i < e_MaxInputKeys then
645 begin
646 if Length(p) = 2 then
647 g_Console_Add('"' + e_KeyNames[i] + '" = "' + GetCommandString(gInputBinds[i].down) + '" "' + GetCommandString(gInputBinds[i].up) + '"')
648 else if Length(p) = 3 then
649 g_Console_BindKey(i, p[2], '')
650 else (* len = 4 *)
651 g_Console_BindKey(i, p[2], p[3])
652 end
653 else
654 g_Console_Add('bind: "' + p[1] + '" is not a key')
655 end
656 else
657 begin
658 g_Console_Add('bind <key> <down action> [up action]')
659 end;
660 'bindlist':
661 for i := 0 to e_MaxInputKeys - 1 do
662 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
663 g_Console_Add(e_KeyNames[i] + ' "' + GetCommandString(gInputBinds[i].down) + '" "' + GetCommandString(gInputBinds[i].up) + '"');
664 'unbind':
665 // unbind <key>
666 if Length(p) = 2 then
667 begin
668 key := LowerCase(p[1]);
669 i := 0;
670 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
671 if i < e_MaxInputKeys then
672 g_Console_BindKey(i, '')
673 else
674 g_Console_Add('unbind: "' + p[1] + '" is not a key')
675 end
676 else
677 g_Console_Add('unbind <key>');
678 'unbindall':
679 for i := 0 to e_MaxInputKeys - 1 do
680 g_Console_BindKey(i, '');
681 'showkeyboard':
682 g_Touch_ShowKeyboard(True);
683 'hidekeyboard':
684 g_Touch_ShowKeyboard(False);
685 end
686 end;
688 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
689 var
690 a: Integer;
691 cp: PCommand;
692 begin
693 SetLength(commands, Length(commands)+1);
694 a := High(commands);
695 cp := @commands[a];
696 cp.cmd := LowerCase(cmd);
697 cp.proc := proc;
698 cp.procEx := nil;
699 cp.help := ahelp;
700 cp.hidden := ahidden;
701 cp.ptr := nil;
702 cp.msg := '';
703 cp.cheat := acheat;
704 cp.action := -1;
705 cp.player := -1;
706 end;
708 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
709 const
710 PrefixList: array [0..1] of AnsiString = ('+', '-');
711 PlayerList: array [0..1] of Integer = (1, 2);
712 var
713 s: AnsiString;
714 i: Integer;
716 procedure NewAction (cmd: AnsiString; player: Integer);
717 var cp: PCommand;
718 begin
719 SetLength(commands, Length(commands) + 1);
720 cp := @commands[High(commands)];
721 cp.cmd := LowerCase(cmd);
722 cp.proc := nil;
723 cp.procEx := nil;
724 cp.help := help;
725 cp.hidden := hidden;
726 cp.ptr := nil;
727 cp.msg := '';
728 cp.cheat := cheat;
729 cp.action := action;
730 cp.player := player;
731 end;
733 begin
734 ASSERT(action >= FIRST_ACTION);
735 ASSERT(action <= LAST_ACTION);
736 for s in PrefixList do
737 begin
738 NewAction(s + cmd, 0);
739 for i in PlayerList do
740 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
741 end
742 end;
744 procedure g_Console_Init();
745 var
746 a: Integer;
747 begin
748 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
749 Cons_Y := -(gScreenHeight div 2);
750 gConsoleShow := False;
751 gChatShow := False;
752 Cons_Shown := False;
753 CPos := 1;
755 for a := 0 to High(MsgArray) do
756 with MsgArray[a] do
757 begin
758 Msg := '';
759 Time := 0;
760 end;
762 AddCommand('segfault', segfault, 'make segfault');
764 AddCommand('bind', BindCommands);
765 AddCommand('bindlist', BindCommands);
766 AddCommand('unbind', BindCommands);
767 AddCommand('unbindall', BindCommands);
768 AddCommand('showkeyboard', BindCommands);
769 AddCommand('hidekeyboard', BindCommands);
771 AddCommand('clear', ConsoleCommands, 'clear console');
772 AddCommand('clearhistory', ConsoleCommands);
773 AddCommand('showhistory', ConsoleCommands);
774 AddCommand('commands', ConsoleCommands);
775 AddCommand('time', ConsoleCommands);
776 AddCommand('date', ConsoleCommands);
777 AddCommand('echo', ConsoleCommands);
778 AddCommand('dump', ConsoleCommands);
779 AddCommand('exec', ConsoleCommands);
780 AddCommand('writeconfig', ConsoleCommands);
781 AddCommand('alias', ConsoleCommands);
782 AddCommand('call', ConsoleCommands);
783 AddCommand('ver', ConsoleCommands);
784 AddCommand('version', ConsoleCommands);
786 AddCommand('d_window', DebugCommands);
787 AddCommand('d_sounds', DebugCommands);
788 AddCommand('d_frames', DebugCommands);
789 AddCommand('d_winmsg', DebugCommands);
790 AddCommand('d_monoff', DebugCommands);
791 AddCommand('d_botoff', DebugCommands);
792 AddCommand('d_monster', DebugCommands);
793 AddCommand('d_health', DebugCommands);
794 AddCommand('d_player', DebugCommands);
795 AddCommand('d_joy', DebugCommands);
796 AddCommand('d_mem', DebugCommands);
798 AddCommand('p1_name', GameCVars);
799 AddCommand('p2_name', GameCVars);
800 AddCommand('p1_color', GameCVars);
801 AddCommand('p2_color', GameCVars);
802 AddCommand('r_showtime', GameCVars);
803 AddCommand('r_showscore', GameCVars);
804 AddCommand('r_showlives', GameCVars);
805 AddCommand('r_showstat', GameCVars);
806 AddCommand('r_showkillmsg', GameCVars);
807 AddCommand('r_showspect', GameCVars);
808 AddCommand('r_showping', GameCVars);
809 AddCommand('g_gamemode', GameCVars);
810 AddCommand('g_friendlyfire', GameCVars);
811 AddCommand('g_weaponstay', GameCVars);
812 AddCommand('g_allow_exit', GameCVars);
813 AddCommand('g_allow_monsters', GameCVars);
814 AddCommand('g_bot_vsmonsters', GameCVars);
815 AddCommand('g_bot_vsplayers', GameCVars);
816 AddCommand('g_scorelimit', GameCVars);
817 AddCommand('g_timelimit', GameCVars);
818 AddCommand('g_maxlives', GameCVars);
819 AddCommand('g_warmuptime', GameCVars);
820 AddCommand('net_interp', GameCVars);
821 AddCommand('net_forceplayerupdate', GameCVars);
822 AddCommand('net_predictself', GameCVars);
823 AddCommand('sv_name', GameCVars);
824 AddCommand('sv_passwd', GameCVars);
825 AddCommand('sv_maxplrs', GameCVars);
826 AddCommand('sv_public', GameCVars);
827 AddCommand('sv_intertime', GameCVars);
829 AddCommand('quit', GameCommands);
830 AddCommand('exit', GameCommands);
831 AddCommand('pause', GameCommands);
832 AddCommand('endgame', GameCommands);
833 AddCommand('restart', GameCommands);
834 AddCommand('addbot', GameCommands);
835 AddCommand('bot_add', GameCommands);
836 AddCommand('bot_addlist', GameCommands);
837 AddCommand('bot_addred', GameCommands);
838 AddCommand('bot_addblue', GameCommands);
839 AddCommand('bot_removeall', GameCommands);
840 AddCommand('chat', GameCommands);
841 AddCommand('teamchat', GameCommands);
842 AddCommand('game', GameCommands);
843 AddCommand('host', GameCommands);
844 AddCommand('map', GameCommands);
845 AddCommand('nextmap', GameCommands);
846 AddCommand('endmap', GameCommands);
847 AddCommand('goodbye', GameCommands);
848 AddCommand('suicide', GameCommands);
849 AddCommand('spectate', GameCommands);
850 AddCommand('ready', GameCommands);
851 AddCommand('kick', GameCommands);
852 AddCommand('kick_id', GameCommands);
853 AddCommand('ban', GameCommands);
854 AddCommand('permban', GameCommands);
855 AddCommand('ban_id', GameCommands);
856 AddCommand('permban_id', GameCommands);
857 AddCommand('unban', GameCommands);
858 AddCommand('connect', GameCommands);
859 AddCommand('disconnect', GameCommands);
860 AddCommand('reconnect', GameCommands);
861 AddCommand('say', GameCommands);
862 AddCommand('tell', GameCommands);
863 AddCommand('overtime', GameCommands);
864 AddCommand('rcon_password', GameCommands);
865 AddCommand('rcon', GameCommands);
866 AddCommand('callvote', GameCommands);
867 AddCommand('vote', GameCommands);
868 AddCommand('clientlist', GameCommands);
869 AddCommand('event', GameCommands);
870 AddCommand('screenshot', GameCommands);
871 AddCommand('togglechat', GameCommands);
872 AddCommand('toggleteamchat', GameCommands);
873 AddCommand('weapon', GameCommands);
874 AddCommand('p1_weapon', GameCommands);
875 AddCommand('p2_weapon', GameCommands);
877 AddCommand('god', GameCheats);
878 AddCommand('notarget', GameCheats);
879 AddCommand('give', GameCheats); // "exit" too ;-)
880 AddCommand('open', GameCheats);
881 AddCommand('fly', GameCheats);
882 AddCommand('noclip', GameCheats);
883 AddCommand('speedy', GameCheats);
884 AddCommand('jumpy', GameCheats);
885 AddCommand('noreload', GameCheats);
886 AddCommand('aimline', GameCheats);
887 AddCommand('automap', GameCheats);
889 AddAction('jump', ACTION_JUMP);
890 AddAction('moveleft', ACTION_MOVELEFT);
891 AddAction('moveright', ACTION_MOVERIGHT);
892 AddAction('lookup', ACTION_LOOKUP);
893 AddAction('lookdown', ACTION_LOOKDOWN);
894 AddAction('attack', ACTION_ATTACK);
895 AddAction('scores', ACTION_SCORES);
896 AddAction('activate', ACTION_ACTIVATE);
897 AddAction('strafe', ACTION_STRAFE);
898 AddAction('weapnext', ACTION_WEAPNEXT);
899 AddAction('weapprev', ACTION_WEAPPREV);
901 WhitelistCommand('say');
902 WhitelistCommand('tell');
903 WhitelistCommand('overtime');
904 WhitelistCommand('ready');
905 WhitelistCommand('map');
906 WhitelistCommand('nextmap');
907 WhitelistCommand('endmap');
908 WhitelistCommand('restart');
909 WhitelistCommand('kick');
910 WhitelistCommand('ban');
912 WhitelistCommand('addbot');
913 WhitelistCommand('bot_add');
914 WhitelistCommand('bot_addred');
915 WhitelistCommand('bot_addblue');
916 WhitelistCommand('bot_removeall');
918 WhitelistCommand('g_gamemode');
919 WhitelistCommand('g_friendlyfire');
920 WhitelistCommand('g_weaponstay');
921 WhitelistCommand('g_allow_exit');
922 WhitelistCommand('g_allow_monsters');
923 WhitelistCommand('g_scorelimit');
924 WhitelistCommand('g_timelimit');
926 g_Console_ResetBinds;
927 g_Console_ReadConfig(GameDir + '/dfconfig.cfg');
928 g_Console_ReadConfig(GameDir + '/autoexec.cfg');
930 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
931 g_Console_Add('');
932 end;
934 procedure g_Console_Update();
935 var
936 a, b: Integer;
937 begin
938 if Cons_Shown then
939 begin
940 // Â ïðîöåññå îòêðûòèÿ:
941 if gConsoleShow and (Cons_Y < 0) then
942 begin
943 Cons_Y := Cons_Y+Step;
944 end;
946 // Â ïðîöåññå çàêðûòèÿ:
947 if (not gConsoleShow) and
948 (Cons_Y > -(gScreenHeight div 2)) then
949 Cons_Y := Cons_Y-Step;
951 // Îêîí÷àòåëüíî îòêðûëàñü:
952 if Cons_Y > 0 then
953 Cons_Y := 0;
955 // Îêîí÷àòåëüíî çàêðûëàñü:
956 if Cons_Y <= (-(gScreenHeight div 2)) then
957 begin
958 Cons_Y := -(gScreenHeight div 2);
959 Cons_Shown := False;
960 end;
961 end;
963 a := 0;
964 while a <= High(MsgArray) do
965 begin
966 if MsgArray[a].Time > 0 then
967 begin
968 if MsgArray[a].Time = 1 then
969 begin
970 if a < High(MsgArray) then
971 begin
972 for b := a to High(MsgArray)-1 do
973 MsgArray[b] := MsgArray[b+1];
975 MsgArray[High(MsgArray)].Time := 0;
977 a := a - 1;
978 end;
979 end
980 else
981 Dec(MsgArray[a].Time);
982 end;
984 a := a + 1;
985 end;
986 end;
989 procedure drawConsoleText ();
990 var
991 CWidth, CHeight: Byte;
992 ty: Integer;
993 sp, ep: LongWord;
994 skip: Integer;
996 procedure putLine (sp, ep: LongWord);
997 var
998 p: LongWord;
999 wdt, cw: Integer;
1000 begin
1001 p := sp;
1002 wdt := 0;
1003 while p <> ep do
1004 begin
1005 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1006 if wdt+cw > gScreenWidth-8 then break;
1007 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1008 Inc(wdt, cw);
1009 cbufNext(p);
1010 end;
1011 if p <> ep then putLine(p, ep); // do rest of the line first
1012 // now print our part
1013 if skip = 0 then
1014 begin
1015 ep := p;
1016 p := sp;
1017 wdt := 2;
1018 while p <> ep do
1019 begin
1020 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1021 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1022 Inc(wdt, cw);
1023 cbufNext(p);
1024 end;
1025 Dec(ty, CHeight);
1026 end
1027 else
1028 begin
1029 Dec(skip);
1030 end;
1031 end;
1033 begin
1034 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1035 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
1036 skip := conSkipLines;
1037 cbufLastLine(sp, ep);
1038 repeat
1039 putLine(sp, ep);
1040 if ty+CHeight <= 0 then break;
1041 until not cbufLineUp(sp, ep);
1042 end;
1044 procedure g_Console_Draw();
1045 var
1046 CWidth, CHeight: Byte;
1047 mfW, mfH: Word;
1048 a, b: Integer;
1049 begin
1050 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1052 for a := 0 to High(MsgArray) do
1053 if MsgArray[a].Time > 0 then
1054 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
1055 gStdFont, True);
1057 if not Cons_Shown then
1058 begin
1059 if gChatShow then
1060 begin
1061 if gChatTeam then
1062 begin
1063 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
1064 gStdFont, 255, 255, 255, 1, True);
1065 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
1066 gStdFont, 255, 255, 255, 1, True);
1067 end
1068 else
1069 begin
1070 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
1071 gStdFont, 255, 255, 255, 1, True);
1072 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
1073 gStdFont, 255, 255, 255, 1, True);
1074 end;
1075 end;
1076 Exit;
1077 end;
1079 if gDebugMode then
1080 begin
1081 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1082 a := (gScreenWidth - 2*mfW) div 2;
1083 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
1084 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1085 _RGB(128, 0, 0), 2.0);
1086 end;
1088 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
1089 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
1091 drawConsoleText();
1092 (*
1093 if ConsoleHistory <> nil then
1094 begin
1095 b := 0;
1096 if CHeight > 0 then
1097 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
1098 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
1100 b := Max(b-Offset, 0);
1101 d := Max(High(ConsoleHistory)-Offset, 0);
1103 c := 2;
1104 for a := d downto b do
1105 begin
1106 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
1107 gStdFont, True);
1108 c := c + 1;
1109 end;
1110 end;
1111 *)
1113 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
1114 end;
1116 procedure g_Console_Switch();
1117 begin
1118 gChatShow := False;
1119 gConsoleShow := not gConsoleShow;
1120 Cons_Shown := True;
1121 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1122 end;
1124 procedure g_Console_Chat_Switch(Team: Boolean = False);
1125 begin
1126 if not g_Game_IsNet then Exit;
1127 gConsoleShow := False;
1128 gChatShow := not gChatShow;
1129 gChatTeam := Team;
1130 Line := '';
1131 CPos := 1;
1132 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1133 end;
1135 procedure g_Console_Char(C: AnsiChar);
1136 begin
1137 // if gChatShow then
1138 // Exit;
1139 Insert(C, Line, CPos);
1140 CPos := CPos + 1;
1141 end;
1144 var
1145 tcomplist: array of AnsiString = nil;
1146 tcompidx: array of Integer = nil;
1148 procedure Complete ();
1149 var
1150 i, c: Integer;
1151 tused: Integer;
1152 ll, lpfx, cmd: AnsiString;
1153 begin
1154 if (Length(Line) = 0) then
1155 begin
1156 g_Console_Add('');
1157 for i := 0 to High(commands) do
1158 begin
1159 // hidden commands are hidden when cheats aren't enabled
1160 if commands[i].hidden and not conIsCheatsEnabled then continue;
1161 if (Length(commands[i].help) > 0) then
1162 begin
1163 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1164 end
1165 else
1166 begin
1167 g_Console_Add(' '+commands[i].cmd);
1168 end;
1169 end;
1170 exit;
1171 end;
1173 ll := LowerCase(Line);
1174 lpfx := '';
1176 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1177 begin
1178 ll := Copy(ll, 0, Length(ll)-1);
1179 for i := 0 to High(commands) do
1180 begin
1181 // hidden commands are hidden when cheats aren't enabled
1182 if commands[i].hidden and not conIsCheatsEnabled then continue;
1183 if (commands[i].cmd = ll) then
1184 begin
1185 if (Length(commands[i].help) > 0) then
1186 begin
1187 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1188 end;
1189 end;
1190 end;
1191 exit;
1192 end;
1194 // build completion list
1195 tused := 0;
1196 for i := 0 to High(commands) do
1197 begin
1198 // hidden commands are hidden when cheats aren't enabled
1199 if commands[i].hidden and not conIsCheatsEnabled then continue;
1200 cmd := commands[i].cmd;
1201 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1202 begin
1203 if (tused = Length(tcomplist)) then
1204 begin
1205 SetLength(tcomplist, Length(tcomplist)+128);
1206 SetLength(tcompidx, Length(tcompidx)+128);
1207 end;
1208 tcomplist[tused] := cmd;
1209 tcompidx[tused] := i;
1210 Inc(tused);
1211 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1212 end;
1213 end;
1215 // get longest prefix
1216 for i := 0 to tused-1 do
1217 begin
1218 cmd := tcomplist[i];
1219 for c := 1 to Length(lpfx) do
1220 begin
1221 if (c > Length(cmd)) then break;
1222 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1223 end;
1224 end;
1226 if (tused = 0) then exit;
1228 if (tused = 1) then
1229 begin
1230 Line := tcomplist[0]+' ';
1231 CPos := Length(Line)+1;
1232 end
1233 else
1234 begin
1235 // has longest prefix?
1236 if (Length(lpfx) > Length(ll)) then
1237 begin
1238 Line := lpfx;
1239 CPos:= Length(Line)+1;
1240 end
1241 else
1242 begin
1243 g_Console_Add('');
1244 for i := 0 to tused-1 do
1245 begin
1246 if (Length(commands[tcompidx[i]].help) > 0) then
1247 begin
1248 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1249 end
1250 else
1251 begin
1252 g_Console_Add(' '+tcomplist[i]);
1253 end;
1254 end;
1255 end;
1256 end;
1257 end;
1260 procedure g_Console_Control(K: Word);
1261 begin
1262 case K of
1263 IK_BACKSPACE:
1264 if (Length(Line) > 0) and (CPos > 1) then
1265 begin
1266 Delete(Line, CPos-1, 1);
1267 CPos := CPos-1;
1268 end;
1269 IK_DELETE:
1270 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1271 Delete(Line, CPos, 1);
1272 IK_LEFT, IK_KPLEFT, VK_LEFT, JOY0_LEFT, JOY1_LEFT, JOY2_LEFT, JOY3_LEFT:
1273 if CPos > 1 then
1274 CPos := CPos - 1;
1275 IK_RIGHT, IK_KPRIGHT, VK_RIGHT, JOY0_RIGHT, JOY1_RIGHT, JOY2_RIGHT, JOY3_RIGHT:
1276 if CPos <= Length(Line) then
1277 CPos := CPos + 1;
1278 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE, JOY0_ATTACK, JOY1_ATTACK, JOY2_ATTACK, JOY3_ATTACK:
1279 begin
1280 if Cons_Shown then
1281 g_Console_Process(Line)
1282 else
1283 if gChatShow then
1284 begin
1285 if (Length(Line) > 0) and g_Game_IsNet then
1286 begin
1287 if gChatTeam then
1288 begin
1289 if g_Game_IsClient then
1290 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1291 else
1292 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1293 NET_CHAT_TEAM, gPlayer1Settings.Team);
1294 end
1295 else
1296 begin
1297 if g_Game_IsClient then
1298 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1299 else
1300 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1301 NET_CHAT_PLAYER);
1302 end;
1303 end;
1305 Line := '';
1306 CPos := 1;
1307 gChatShow := False;
1308 gJustChatted := True;
1309 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1310 end;
1311 end;
1312 IK_TAB:
1313 if not gChatShow then
1314 Complete();
1315 IK_DOWN, IK_KPDOWN, VK_DOWN, JOY0_DOWN, JOY1_DOWN, JOY2_DOWN, JOY3_DOWN:
1316 if not gChatShow then
1317 if (CommandHistory <> nil) and
1318 (CmdIndex < Length(CommandHistory)) then
1319 begin
1320 if CmdIndex < Length(CommandHistory)-1 then
1321 CmdIndex := CmdIndex + 1;
1322 Line := CommandHistory[CmdIndex];
1323 CPos := Length(Line) + 1;
1324 end;
1325 IK_UP, IK_KPUP, VK_UP, JOY0_UP, JOY1_UP, JOY2_UP, JOY3_UP:
1326 if not gChatShow then
1327 if (CommandHistory <> nil) and
1328 (CmdIndex <= Length(CommandHistory)) then
1329 begin
1330 if CmdIndex > 0 then
1331 CmdIndex := CmdIndex - 1;
1332 Line := CommandHistory[CmdIndex];
1333 Cpos := Length(Line) + 1;
1334 end;
1335 IK_PAGEUP, IK_KPPAGEUP, VK_PREV, JOY0_PREV, JOY1_PREV, JOY2_PREV, JOY3_PREV: // PgUp
1336 if not gChatShow then Inc(conSkipLines);
1337 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT, JOY0_NEXT, JOY1_NEXT, JOY2_NEXT, JOY3_NEXT: // PgDown
1338 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1339 IK_HOME, IK_KPHOME:
1340 CPos := 1;
1341 IK_END, IK_KPEND:
1342 CPos := Length(Line) + 1;
1343 end;
1344 end;
1346 function GetStr(var Str: AnsiString): AnsiString;
1347 var
1348 a, b: Integer;
1349 begin
1350 Result := '';
1351 if Str[1] = '"' then
1352 begin
1353 for b := 1 to Length(Str) do
1354 if (b = Length(Str)) or (Str[b+1] = '"') then
1355 begin
1356 Result := Copy(Str, 2, b-1);
1357 Delete(Str, 1, b+1);
1358 Str := Trim(Str);
1359 Exit;
1360 end;
1361 end;
1363 for a := 1 to Length(Str) do
1364 if (a = Length(Str)) or (Str[a+1] = ' ') then
1365 begin
1366 Result := Copy(Str, 1, a);
1367 Delete(Str, 1, a+1);
1368 Str := Trim(Str);
1369 Exit;
1370 end;
1371 end;
1373 function ParseString(Str: AnsiString): SSArray;
1374 begin
1375 Result := nil;
1377 Str := Trim(Str);
1379 if Str = '' then
1380 Exit;
1382 while Str <> '' do
1383 begin
1384 SetLength(Result, Length(Result)+1);
1385 Result[High(Result)] := GetStr(Str);
1386 end;
1387 end;
1389 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1391 procedure conmsg (s: AnsiString);
1392 var
1393 a: Integer;
1394 begin
1395 if length(s) = 0 then exit;
1396 for a := 0 to High(MsgArray) do
1397 begin
1398 with MsgArray[a] do
1399 begin
1400 if Time = 0 then
1401 begin
1402 Msg := s;
1403 Time := MsgTime;
1404 exit;
1405 end;
1406 end;
1407 end;
1408 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1409 with MsgArray[High(MsgArray)] do
1410 begin
1411 Msg := L;
1412 Time := MsgTime;
1413 end;
1414 end;
1416 var
1417 f: Integer;
1418 begin
1419 // put it to console
1420 cbufPut(L);
1421 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1423 // now show 'em out of console too
1424 show := show and gAllowConsoleMessages;
1425 if show and gShowMessages then
1426 begin
1427 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1428 while length(L) > 0 do
1429 begin
1430 f := Pos(#10, L);
1431 if f <= 0 then f := length(L)+1;
1432 conmsg(Copy(L, 1, f-1));
1433 Delete(L, 1, f);
1434 end;
1435 end;
1437 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1438 //ConsoleHistory[High(ConsoleHistory)] := L;
1440 (*
1441 {$IFDEF HEADLESS}
1442 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1443 {$ENDIF}
1444 *)
1445 end;
1448 var
1449 consolewriterLastWasEOL: Boolean = false;
1451 procedure consolewriter (constref buf; len: SizeUInt);
1452 var
1453 b: PByte;
1454 begin
1455 if (len < 1) then exit;
1456 b := PByte(@buf);
1457 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1458 while (len > 0) do
1459 begin
1460 if (b[0] <> 13) and (b[0] <> 10) then
1461 begin
1462 cbufPut(AnsiChar(b[0]));
1463 end
1464 else
1465 begin
1466 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1467 cbufPut(#10);
1468 end;
1469 len -= 1;
1470 b += 1;
1471 end;
1472 end;
1475 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1476 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1477 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1478 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1479 begin
1480 g_Console_Add(s, show);
1481 end;
1484 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1485 begin
1486 if show then
1487 begin
1488 g_Console_Add(formatstrf(s, args), true);
1489 end
1490 else
1491 begin
1492 consolewriterLastWasEOL := false;
1493 formatstrf(s, args, consolewriter);
1494 if not consolewriterLastWasEOL then cbufPut(#10);
1495 end;
1496 end;
1499 procedure g_Console_Clear();
1500 begin
1501 //ConsoleHistory := nil;
1502 cbufClear();
1503 conSkipLines := 0;
1504 end;
1506 procedure AddToHistory(L: AnsiString);
1507 var
1508 len: Integer;
1509 begin
1510 len := Length(CommandHistory);
1512 if (len = 0) or
1513 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1514 begin
1515 SetLength(CommandHistory, len+1);
1516 CommandHistory[len] := L;
1517 end;
1519 CmdIndex := Length(CommandHistory);
1520 end;
1522 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1523 var
1524 Arr: SSArray;
1525 i: Integer;
1526 begin
1527 Result := True;
1529 Arr := nil;
1531 if Trim(C) = '' then
1532 Exit;
1534 Arr := ParseString(C);
1535 if Arr = nil then
1536 Exit;
1538 for i := 0 to High(Whitelist) do
1539 if Whitelist[i] = LowerCase(Arr[0]) then
1540 Result := False;
1541 end;
1543 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1544 var
1545 Arr: SSArray;
1546 i: Integer;
1547 begin
1548 Arr := nil;
1550 if Trim(L) = '' then
1551 Exit;
1553 conSkipLines := 0; // "unscroll"
1555 if L = 'goobers' then
1556 begin
1557 Line := '';
1558 CPos := 1;
1559 gCheats := true;
1560 g_Console_Add('Your memory serves you well.');
1561 exit;
1562 end;
1564 if not quiet then
1565 begin
1566 g_Console_Add('> '+L);
1567 Line := '';
1568 CPos := 1;
1569 end;
1571 Arr := ParseString(L);
1572 if Arr = nil then
1573 Exit;
1575 if commands = nil then
1576 Exit;
1578 if not quiet then
1579 AddToHistory(L);
1581 for i := 0 to High(commands) do
1582 begin
1583 if commands[i].cmd = LowerCase(Arr[0]) then
1584 begin
1585 if commands[i].action >= 0 then
1586 begin
1587 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1588 exit
1589 end;
1590 if assigned(commands[i].procEx) then
1591 begin
1592 commands[i].procEx(@commands[i], Arr);
1593 exit
1594 end;
1595 if assigned(commands[i].proc) then
1596 begin
1597 commands[i].proc(Arr);
1598 exit
1599 end
1600 end
1601 end;
1603 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1604 end;
1607 function g_Console_Interactive: Boolean;
1608 begin
1609 Result := gConsoleShow
1610 end;
1612 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
1613 begin
1614 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1615 ASSERT(key >= 0);
1616 ASSERT(key < e_MaxInputKeys);
1617 if key > 0 then
1618 begin
1619 gInputBinds[key].down := ParseAlias(down);
1620 gInputBinds[key].up := ParseAlias(up)
1621 end
1622 end;
1624 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1625 var i: Integer;
1627 function EqualsCommandLists (a, b: SSArray): Boolean;
1628 var i, len: Integer;
1629 begin
1630 result := False;
1631 len := Length(a);
1632 if len = Length(b) then
1633 begin
1634 i := 0;
1635 while (i < len) and (a[i] = b[i]) do inc(i);
1636 if i >= len then
1637 result := True
1638 end
1639 end;
1641 begin
1642 ASSERT(n >= 1);
1643 result := 0;
1644 if commands = nil then Exit;
1645 i := 0;
1646 while (n >= 1) and (i < e_MaxInputKeys) do
1647 begin
1648 if EqualsCommandLists(ParseAlias(down), gInputBinds[i].down) then
1649 begin
1650 if EqualsCommandLists(ParseAlias(up), gInputBinds[i].up) then
1651 begin
1652 result := i;
1653 dec(n)
1654 end
1655 end;
1656 inc(i)
1657 end;
1658 if n >= 1 then
1659 result := 0
1660 end;
1662 function g_Console_Action (action: Integer): Boolean;
1663 var i, len: Integer;
1664 begin
1665 ASSERT(action >= FIRST_ACTION);
1666 ASSERT(action <= LAST_ACTION);
1667 i := 0;
1668 len := Length(gPlayerAction);
1669 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1670 Result := i < len
1671 end;
1673 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1674 var i: Integer;
1675 begin
1676 if (not gChatShow) and (not gConsoleShow) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1677 begin
1678 if down then
1679 for i := 0 to High(gInputBinds[key].down) do
1680 g_Console_Process(gInputBinds[key].down[i], True)
1681 else
1682 for i := 0 to High(gInputBinds[key].up) do
1683 g_Console_Process(gInputBinds[key].up[i], True)
1684 end
1685 end;
1687 procedure g_Console_ResetBinds;
1688 var i: Integer;
1689 begin
1690 for i := 0 to e_MaxInputKeys - 1 do
1691 g_Console_BindKey(i, '', '');
1693 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1694 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1695 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1696 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1697 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1698 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1699 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1700 g_Console_BindKey(IK_E, '+p1_weapnext', '-p1_weapnext');
1701 g_Console_BindKey(IK_Q, '+p1_weapprev', '-p1_weapprev');
1702 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1703 g_Console_BindKey(IK_1, 'p1_weapon 1');
1704 g_Console_BindKey(IK_2, 'p1_weapon 2');
1705 g_Console_BindKey(IK_3, 'p1_weapon 3');
1706 g_Console_BindKey(IK_4, 'p1_weapon 4');
1707 g_Console_BindKey(IK_5, 'p1_weapon 5');
1708 g_Console_BindKey(IK_6, 'p1_weapon 6');
1709 g_Console_BindKey(IK_7, 'p1_weapon 7');
1710 g_Console_BindKey(IK_8, 'p1_weapon 8');
1711 g_Console_BindKey(IK_9, 'p1_weapon 9');
1712 g_Console_BindKey(IK_0, 'p1_weapon 10');
1713 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1714 g_Console_BindKey(IK_T, 'togglechat');
1715 g_Console_BindKey(IK_Y, 'toggleteamchat');
1716 g_Console_BindKey(IK_F11, 'screenshot');
1717 g_Console_BindKey(IK_TAB, '+p1_scores', '-p1_scores');
1718 g_Console_BindKey(IK_PAUSE, 'pause');
1719 g_Console_BindKey(IK_F1, 'vote');
1721 (* for i := 0 to e_MaxJoys - 1 do *)
1722 for i := 0 to 1 do
1723 begin
1724 g_Console_BindKey(e_JoyAxisToKey(i, 0, AX_MINUS), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
1725 g_Console_BindKey(e_JoyAxisToKey(i, 0, AX_PLUS), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
1726 g_Console_BindKey(e_JoyAxisToKey(i, 1, AX_MINUS), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
1727 g_Console_BindKey(e_JoyAxisToKey(i, 1, AX_PLUS), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
1728 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
1729 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
1730 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
1731 g_Console_BindKey(e_JoyButtonToKey(i, 1), '+p' + IntToStr(i mod 2 + 1) + '_weapnext', '-p' + IntToStr(i mod 2 + 1) + '_weapnext');
1732 g_Console_BindKey(e_JoyButtonToKey(i, 4), '+p' + IntToStr(i mod 2 + 1) + '_weapprev', '-p' + IntToStr(i mod 2 + 1) + '_weapprev');
1733 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
1734 end;
1736 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
1737 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
1738 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
1739 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
1740 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
1741 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
1742 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
1743 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
1744 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
1745 g_Console_BindKey(VK_NEXT, '+weapnext', '-weapnext');
1746 g_Console_BindKey(VK_PREV, '+weapprev', '-weapprev');
1747 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
1748 g_Console_BindKey(VK_0, 'weapon 1');
1749 g_Console_BindKey(VK_1, 'weapon 2');
1750 g_Console_BindKey(VK_2, 'weapon 3');
1751 g_Console_BindKey(VK_3, 'weapon 4');
1752 g_Console_BindKey(VK_4, 'weapon 5');
1753 g_Console_BindKey(VK_5, 'weapon 6');
1754 g_Console_BindKey(VK_6, 'weapon 7');
1755 g_Console_BindKey(VK_7, 'weapon 8');
1756 g_Console_BindKey(VK_8, 'weapon 9');
1757 g_Console_BindKey(VK_9, 'weapon 10');
1758 g_Console_BindKey(VK_A, 'weapon 11');
1759 g_Console_BindKey(VK_CHAT, 'togglechat');
1760 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
1761 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
1762 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
1763 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
1764 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
1766 // VK_CONSOLE
1767 // VK_ESCAPE
1768 end;
1770 procedure g_Console_ReadConfig (filename: String);
1771 var f: TextFile; s: AnsiString; i, len: Integer;
1772 begin
1773 if FileExists(filename) then
1774 begin
1775 AssignFile(f, filename);
1776 Reset(f);
1777 while not EOF(f) do
1778 begin
1779 ReadLn(f, s);
1780 len := Length(s);
1781 if len > 0 then
1782 begin
1783 i := 1;
1784 (* skip spaces *)
1785 while (i <= len) and (s[i] <= ' ') do inc(i);
1786 (* skip comments *)
1787 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
1788 g_Console_Process(s, True)
1789 end
1790 end;
1791 CloseFile(f)
1792 end
1793 end;
1795 procedure g_Console_WriteConfig (filename: String);
1796 var f: TextFile; i, j: Integer;
1797 begin
1798 AssignFile(f, filename);
1799 Rewrite(f);
1800 WriteLn(f, '// generated by doom2d, do not modify');
1801 WriteLn(f, 'unbindall');
1802 for i := 0 to e_MaxInputKeys - 1 do
1803 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
1804 WriteLn(f, 'bind ', e_KeyNames[i], ' "', GetCommandString(gInputBinds[i].down), '" "', GetCommandString(gInputBinds[i].up), '"');
1805 for i := 0 to High(commands) do
1806 begin
1807 if not commands[i].cheat then
1808 begin
1809 if @commands[i].procEx = @boolVarHandler then
1810 begin
1811 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
1812 WriteLn(f, commands[i].cmd, ' "', j, '"')
1813 end
1814 else if @commands[i].procEx = @intVarHandler then
1815 begin
1816 WriteLn(f, commands[i].cmd, ' "', PInteger(commands[i].ptr)^, '"')
1817 end
1818 else if @commands[i].procEx = @singleVarHandler then
1819 begin
1820 WriteLn(f, commands[i].cmd, ' "', PVarSingle(commands[i].ptr).val^:0:6, '"')
1821 end
1822 end
1823 end;
1824 CloseFile(f)
1825 end;
1828 end.