DEADSOFTWARE

fix virtual keyboard
[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 procedure BindCommands (p: SSArray);
621 var cmd, key, act: AnsiString; i, j: Integer;
622 begin
623 cmd := LowerCase(p[0]);
624 case cmd of
625 'bind':
626 // bind <key> <down> [up]
627 if Length(p) >= 3 then
628 begin
629 i := 0;
630 key := LowerCase(p[1]);
631 if Length(p) = 4 then act := p[3] else act := '';
632 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
633 if i < e_MaxInputKeys then
634 g_Console_BindKey(i, p[2], act)
635 end
636 else
637 g_Console_Add('bind <key> <down action> [up action]');
638 'bindlist':
639 for i := 0 to e_MaxInputKeys - 1 do
640 begin
641 if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
642 begin
643 act := e_KeyNames[i] + ' "';
644 if (gInputBinds[i].down <> nil) then
645 begin
646 act := act + gInputBinds[i].down[0];
647 for j := 1 to High(gInputBinds[i].down) - 1 do
648 act := act + '; ' + gInputBinds[i].down[j];
649 end;
650 act := act + '" "';
651 if (gInputBinds[i].up <> nil) then
652 begin
653 act := act + gInputBinds[i].up[0];
654 for j := 1 to High(gInputBinds[i].up) do
655 act := act + '; ' + gInputBinds[i].up[j];
656 end;
657 act := act + '"';
658 g_Console_Add(act)
659 end
660 end;
661 'unbind':
662 // unbind <key>
663 if Length(p) = 2 then
664 begin
665 key := LowerCase(p[1]);
666 i := 0;
667 while (i < e_MaxInputKeys) and (key <> LowerCase(e_KeyNames[i])) do inc(i);
668 if i < e_MaxInputKeys then
669 g_Console_BindKey(i, '')
670 end
671 else
672 g_Console_Add('unbind <key>');
673 'unbindall':
674 for i := 0 to e_MaxInputKeys - 1 do
675 g_Console_BindKey(i, '');
676 'showkeyboard':
677 g_Touch_ShowKeyboard(True);
678 'hidekeyboard':
679 g_Touch_ShowKeyboard(False);
680 end
681 end;
683 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
684 var
685 a: Integer;
686 cp: PCommand;
687 begin
688 SetLength(commands, Length(commands)+1);
689 a := High(commands);
690 cp := @commands[a];
691 cp.cmd := LowerCase(cmd);
692 cp.proc := proc;
693 cp.procEx := nil;
694 cp.help := ahelp;
695 cp.hidden := ahidden;
696 cp.ptr := nil;
697 cp.msg := '';
698 cp.cheat := acheat;
699 cp.action := -1;
700 cp.player := -1;
701 end;
703 procedure AddAction (cmd: AnsiString; action: Integer; help: AnsiString = ''; hidden: Boolean = False; cheat: Boolean = False);
704 const
705 PrefixList: array [0..1] of AnsiString = ('+', '-');
706 PlayerList: array [0..1] of Integer = (1, 2);
707 var
708 s: AnsiString;
709 i: Integer;
711 procedure NewAction (cmd: AnsiString; player: Integer);
712 var cp: PCommand;
713 begin
714 SetLength(commands, Length(commands) + 1);
715 cp := @commands[High(commands)];
716 cp.cmd := LowerCase(cmd);
717 cp.proc := nil;
718 cp.procEx := nil;
719 cp.help := help;
720 cp.hidden := hidden;
721 cp.ptr := nil;
722 cp.msg := '';
723 cp.cheat := cheat;
724 cp.action := action;
725 cp.player := player;
726 end;
728 begin
729 ASSERT(action >= FIRST_ACTION);
730 ASSERT(action <= LAST_ACTION);
731 for s in PrefixList do
732 begin
733 NewAction(s + cmd, 0);
734 for i in PlayerList do
735 NewAction(s + 'p' + IntToStr(i) + '_' + cmd, i - 1)
736 end
737 end;
739 procedure g_Console_Init();
740 var
741 a: Integer;
742 begin
743 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
744 Cons_Y := -(gScreenHeight div 2);
745 gConsoleShow := False;
746 gChatShow := False;
747 Cons_Shown := False;
748 CPos := 1;
750 for a := 0 to High(MsgArray) do
751 with MsgArray[a] do
752 begin
753 Msg := '';
754 Time := 0;
755 end;
757 AddCommand('segfault', segfault, 'make segfault');
759 AddCommand('bind', BindCommands);
760 AddCommand('bindlist', BindCommands);
761 AddCommand('unbind', BindCommands);
762 AddCommand('unbindall', BindCommands);
763 AddCommand('showkeyboard', BindCommands);
764 AddCommand('hidekeyboard', BindCommands);
766 AddCommand('clear', ConsoleCommands, 'clear console');
767 AddCommand('clearhistory', ConsoleCommands);
768 AddCommand('showhistory', ConsoleCommands);
769 AddCommand('commands', ConsoleCommands);
770 AddCommand('time', ConsoleCommands);
771 AddCommand('date', ConsoleCommands);
772 AddCommand('echo', ConsoleCommands);
773 AddCommand('dump', ConsoleCommands);
774 AddCommand('exec', ConsoleCommands);
775 AddCommand('writeconfig', ConsoleCommands);
776 AddCommand('alias', ConsoleCommands);
777 AddCommand('call', ConsoleCommands);
778 AddCommand('ver', ConsoleCommands);
779 AddCommand('version', ConsoleCommands);
781 AddCommand('d_window', DebugCommands);
782 AddCommand('d_sounds', DebugCommands);
783 AddCommand('d_frames', DebugCommands);
784 AddCommand('d_winmsg', DebugCommands);
785 AddCommand('d_monoff', DebugCommands);
786 AddCommand('d_botoff', DebugCommands);
787 AddCommand('d_monster', DebugCommands);
788 AddCommand('d_health', DebugCommands);
789 AddCommand('d_player', DebugCommands);
790 AddCommand('d_joy', DebugCommands);
791 AddCommand('d_mem', DebugCommands);
793 AddCommand('p1_name', GameCVars);
794 AddCommand('p2_name', GameCVars);
795 AddCommand('p1_color', GameCVars);
796 AddCommand('p2_color', GameCVars);
797 AddCommand('r_showtime', GameCVars);
798 AddCommand('r_showscore', GameCVars);
799 AddCommand('r_showlives', GameCVars);
800 AddCommand('r_showstat', GameCVars);
801 AddCommand('r_showkillmsg', GameCVars);
802 AddCommand('r_showspect', GameCVars);
803 AddCommand('r_showping', GameCVars);
804 AddCommand('g_gamemode', GameCVars);
805 AddCommand('g_friendlyfire', GameCVars);
806 AddCommand('g_weaponstay', GameCVars);
807 AddCommand('g_allow_exit', GameCVars);
808 AddCommand('g_allow_monsters', GameCVars);
809 AddCommand('g_bot_vsmonsters', GameCVars);
810 AddCommand('g_bot_vsplayers', GameCVars);
811 AddCommand('g_scorelimit', GameCVars);
812 AddCommand('g_timelimit', GameCVars);
813 AddCommand('g_maxlives', GameCVars);
814 AddCommand('g_warmuptime', GameCVars);
815 AddCommand('net_interp', GameCVars);
816 AddCommand('net_forceplayerupdate', GameCVars);
817 AddCommand('net_predictself', GameCVars);
818 AddCommand('sv_name', GameCVars);
819 AddCommand('sv_passwd', GameCVars);
820 AddCommand('sv_maxplrs', GameCVars);
821 AddCommand('sv_public', GameCVars);
822 AddCommand('sv_intertime', GameCVars);
824 AddCommand('quit', GameCommands);
825 AddCommand('exit', GameCommands);
826 AddCommand('pause', GameCommands);
827 AddCommand('endgame', GameCommands);
828 AddCommand('restart', GameCommands);
829 AddCommand('addbot', GameCommands);
830 AddCommand('bot_add', GameCommands);
831 AddCommand('bot_addlist', GameCommands);
832 AddCommand('bot_addred', GameCommands);
833 AddCommand('bot_addblue', GameCommands);
834 AddCommand('bot_removeall', GameCommands);
835 AddCommand('chat', GameCommands);
836 AddCommand('teamchat', GameCommands);
837 AddCommand('game', GameCommands);
838 AddCommand('host', GameCommands);
839 AddCommand('map', GameCommands);
840 AddCommand('nextmap', GameCommands);
841 AddCommand('endmap', GameCommands);
842 AddCommand('goodbye', GameCommands);
843 AddCommand('suicide', GameCommands);
844 AddCommand('spectate', GameCommands);
845 AddCommand('ready', GameCommands);
846 AddCommand('kick', GameCommands);
847 AddCommand('kick_id', GameCommands);
848 AddCommand('ban', GameCommands);
849 AddCommand('permban', GameCommands);
850 AddCommand('ban_id', GameCommands);
851 AddCommand('permban_id', GameCommands);
852 AddCommand('unban', GameCommands);
853 AddCommand('connect', GameCommands);
854 AddCommand('disconnect', GameCommands);
855 AddCommand('reconnect', GameCommands);
856 AddCommand('say', GameCommands);
857 AddCommand('tell', GameCommands);
858 AddCommand('overtime', GameCommands);
859 AddCommand('rcon_password', GameCommands);
860 AddCommand('rcon', GameCommands);
861 AddCommand('callvote', GameCommands);
862 AddCommand('vote', GameCommands);
863 AddCommand('clientlist', GameCommands);
864 AddCommand('event', GameCommands);
865 AddCommand('screenshot', GameCommands);
866 AddCommand('togglechat', GameCommands);
867 AddCommand('toggleteamchat', GameCommands);
868 AddCommand('weapon', GameCommands);
869 AddCommand('p1_weapon', GameCommands);
870 AddCommand('p2_weapon', GameCommands);
872 AddCommand('god', GameCheats);
873 AddCommand('notarget', GameCheats);
874 AddCommand('give', GameCheats); // "exit" too ;-)
875 AddCommand('open', GameCheats);
876 AddCommand('fly', GameCheats);
877 AddCommand('noclip', GameCheats);
878 AddCommand('speedy', GameCheats);
879 AddCommand('jumpy', GameCheats);
880 AddCommand('noreload', GameCheats);
881 AddCommand('aimline', GameCheats);
882 AddCommand('automap', GameCheats);
884 AddAction('jump', ACTION_JUMP);
885 AddAction('moveleft', ACTION_MOVELEFT);
886 AddAction('moveright', ACTION_MOVERIGHT);
887 AddAction('lookup', ACTION_LOOKUP);
888 AddAction('lookdown', ACTION_LOOKDOWN);
889 AddAction('attack', ACTION_ATTACK);
890 AddAction('scores', ACTION_SCORES);
891 AddAction('activate', ACTION_ACTIVATE);
892 AddAction('strafe', ACTION_STRAFE);
893 AddAction('weapnext', ACTION_WEAPNEXT);
894 AddAction('weapprev', ACTION_WEAPPREV);
896 WhitelistCommand('say');
897 WhitelistCommand('tell');
898 WhitelistCommand('overtime');
899 WhitelistCommand('ready');
900 WhitelistCommand('map');
901 WhitelistCommand('nextmap');
902 WhitelistCommand('endmap');
903 WhitelistCommand('restart');
904 WhitelistCommand('kick');
905 WhitelistCommand('ban');
907 WhitelistCommand('addbot');
908 WhitelistCommand('bot_add');
909 WhitelistCommand('bot_addred');
910 WhitelistCommand('bot_addblue');
911 WhitelistCommand('bot_removeall');
913 WhitelistCommand('g_gamemode');
914 WhitelistCommand('g_friendlyfire');
915 WhitelistCommand('g_weaponstay');
916 WhitelistCommand('g_allow_exit');
917 WhitelistCommand('g_allow_monsters');
918 WhitelistCommand('g_scorelimit');
919 WhitelistCommand('g_timelimit');
921 g_Console_ResetBinds;
922 g_Console_ReadConfig(GameDir + '/dfconfig.cfg');
923 g_Console_ReadConfig(GameDir + '/autoexec.cfg');
925 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
926 g_Console_Add('');
927 end;
929 procedure g_Console_Update();
930 var
931 a, b: Integer;
932 begin
933 if Cons_Shown then
934 begin
935 // Â ïðîöåññå îòêðûòèÿ:
936 if gConsoleShow and (Cons_Y < 0) then
937 begin
938 Cons_Y := Cons_Y+Step;
939 end;
941 // Â ïðîöåññå çàêðûòèÿ:
942 if (not gConsoleShow) and
943 (Cons_Y > -(gScreenHeight div 2)) then
944 Cons_Y := Cons_Y-Step;
946 // Îêîí÷àòåëüíî îòêðûëàñü:
947 if Cons_Y > 0 then
948 Cons_Y := 0;
950 // Îêîí÷àòåëüíî çàêðûëàñü:
951 if Cons_Y <= (-(gScreenHeight div 2)) then
952 begin
953 Cons_Y := -(gScreenHeight div 2);
954 Cons_Shown := False;
955 end;
956 end;
958 a := 0;
959 while a <= High(MsgArray) do
960 begin
961 if MsgArray[a].Time > 0 then
962 begin
963 if MsgArray[a].Time = 1 then
964 begin
965 if a < High(MsgArray) then
966 begin
967 for b := a to High(MsgArray)-1 do
968 MsgArray[b] := MsgArray[b+1];
970 MsgArray[High(MsgArray)].Time := 0;
972 a := a - 1;
973 end;
974 end
975 else
976 Dec(MsgArray[a].Time);
977 end;
979 a := a + 1;
980 end;
981 end;
984 procedure drawConsoleText ();
985 var
986 CWidth, CHeight: Byte;
987 ty: Integer;
988 sp, ep: LongWord;
989 skip: Integer;
991 procedure putLine (sp, ep: LongWord);
992 var
993 p: LongWord;
994 wdt, cw: Integer;
995 begin
996 p := sp;
997 wdt := 0;
998 while p <> ep do
999 begin
1000 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1001 if wdt+cw > gScreenWidth-8 then break;
1002 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
1003 Inc(wdt, cw);
1004 cbufNext(p);
1005 end;
1006 if p <> ep then putLine(p, ep); // do rest of the line first
1007 // now print our part
1008 if skip = 0 then
1009 begin
1010 ep := p;
1011 p := sp;
1012 wdt := 2;
1013 while p <> ep do
1014 begin
1015 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
1016 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
1017 Inc(wdt, cw);
1018 cbufNext(p);
1019 end;
1020 Dec(ty, CHeight);
1021 end
1022 else
1023 begin
1024 Dec(skip);
1025 end;
1026 end;
1028 begin
1029 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1030 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
1031 skip := conSkipLines;
1032 cbufLastLine(sp, ep);
1033 repeat
1034 putLine(sp, ep);
1035 if ty+CHeight <= 0 then break;
1036 until not cbufLineUp(sp, ep);
1037 end;
1039 procedure g_Console_Draw();
1040 var
1041 CWidth, CHeight: Byte;
1042 mfW, mfH: Word;
1043 a, b: Integer;
1044 begin
1045 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
1047 for a := 0 to High(MsgArray) do
1048 if MsgArray[a].Time > 0 then
1049 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
1050 gStdFont, True);
1052 if not Cons_Shown then
1053 begin
1054 if gChatShow then
1055 begin
1056 if gChatTeam then
1057 begin
1058 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
1059 gStdFont, 255, 255, 255, 1, True);
1060 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
1061 gStdFont, 255, 255, 255, 1, True);
1062 end
1063 else
1064 begin
1065 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
1066 gStdFont, 255, 255, 255, 1, True);
1067 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
1068 gStdFont, 255, 255, 255, 1, True);
1069 end;
1070 end;
1071 Exit;
1072 end;
1074 if gDebugMode then
1075 begin
1076 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
1077 a := (gScreenWidth - 2*mfW) div 2;
1078 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
1079 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
1080 _RGB(128, 0, 0), 2.0);
1081 end;
1083 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
1084 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
1086 drawConsoleText();
1087 (*
1088 if ConsoleHistory <> nil then
1089 begin
1090 b := 0;
1091 if CHeight > 0 then
1092 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
1093 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
1095 b := Max(b-Offset, 0);
1096 d := Max(High(ConsoleHistory)-Offset, 0);
1098 c := 2;
1099 for a := d downto b do
1100 begin
1101 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
1102 gStdFont, True);
1103 c := c + 1;
1104 end;
1105 end;
1106 *)
1108 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
1109 end;
1111 procedure g_Console_Switch();
1112 begin
1113 gChatShow := False;
1114 gConsoleShow := not gConsoleShow;
1115 Cons_Shown := True;
1116 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1117 end;
1119 procedure g_Console_Chat_Switch(Team: Boolean = False);
1120 begin
1121 if not g_Game_IsNet then Exit;
1122 gConsoleShow := False;
1123 gChatShow := not gChatShow;
1124 gChatTeam := Team;
1125 Line := '';
1126 CPos := 1;
1127 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1128 end;
1130 procedure g_Console_Char(C: AnsiChar);
1131 begin
1132 // if gChatShow then
1133 // Exit;
1134 Insert(C, Line, CPos);
1135 CPos := CPos + 1;
1136 end;
1139 var
1140 tcomplist: array of AnsiString = nil;
1141 tcompidx: array of Integer = nil;
1143 procedure Complete ();
1144 var
1145 i, c: Integer;
1146 tused: Integer;
1147 ll, lpfx, cmd: AnsiString;
1148 begin
1149 if (Length(Line) = 0) then
1150 begin
1151 g_Console_Add('');
1152 for i := 0 to High(commands) do
1153 begin
1154 // hidden commands are hidden when cheats aren't enabled
1155 if commands[i].hidden and not conIsCheatsEnabled then continue;
1156 if (Length(commands[i].help) > 0) then
1157 begin
1158 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1159 end
1160 else
1161 begin
1162 g_Console_Add(' '+commands[i].cmd);
1163 end;
1164 end;
1165 exit;
1166 end;
1168 ll := LowerCase(Line);
1169 lpfx := '';
1171 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
1172 begin
1173 ll := Copy(ll, 0, Length(ll)-1);
1174 for i := 0 to High(commands) do
1175 begin
1176 // hidden commands are hidden when cheats aren't enabled
1177 if commands[i].hidden and not conIsCheatsEnabled then continue;
1178 if (commands[i].cmd = ll) then
1179 begin
1180 if (Length(commands[i].help) > 0) then
1181 begin
1182 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
1183 end;
1184 end;
1185 end;
1186 exit;
1187 end;
1189 // build completion list
1190 tused := 0;
1191 for i := 0 to High(commands) do
1192 begin
1193 // hidden commands are hidden when cheats aren't enabled
1194 if commands[i].hidden and not conIsCheatsEnabled then continue;
1195 cmd := commands[i].cmd;
1196 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
1197 begin
1198 if (tused = Length(tcomplist)) then
1199 begin
1200 SetLength(tcomplist, Length(tcomplist)+128);
1201 SetLength(tcompidx, Length(tcompidx)+128);
1202 end;
1203 tcomplist[tused] := cmd;
1204 tcompidx[tused] := i;
1205 Inc(tused);
1206 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
1207 end;
1208 end;
1210 // get longest prefix
1211 for i := 0 to tused-1 do
1212 begin
1213 cmd := tcomplist[i];
1214 for c := 1 to Length(lpfx) do
1215 begin
1216 if (c > Length(cmd)) then break;
1217 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1218 end;
1219 end;
1221 if (tused = 0) then exit;
1223 if (tused = 1) then
1224 begin
1225 Line := tcomplist[0]+' ';
1226 CPos := Length(Line)+1;
1227 end
1228 else
1229 begin
1230 // has longest prefix?
1231 if (Length(lpfx) > Length(ll)) then
1232 begin
1233 Line := lpfx;
1234 CPos:= Length(Line)+1;
1235 end
1236 else
1237 begin
1238 g_Console_Add('');
1239 for i := 0 to tused-1 do
1240 begin
1241 if (Length(commands[tcompidx[i]].help) > 0) then
1242 begin
1243 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1244 end
1245 else
1246 begin
1247 g_Console_Add(' '+tcomplist[i]);
1248 end;
1249 end;
1250 end;
1251 end;
1252 end;
1255 procedure g_Console_Control(K: Word);
1256 begin
1257 case K of
1258 IK_BACKSPACE:
1259 if (Length(Line) > 0) and (CPos > 1) then
1260 begin
1261 Delete(Line, CPos-1, 1);
1262 CPos := CPos-1;
1263 end;
1264 IK_DELETE:
1265 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1266 Delete(Line, CPos, 1);
1267 IK_LEFT, IK_KPLEFT, VK_LEFT:
1268 if CPos > 1 then
1269 CPos := CPos - 1;
1270 IK_RIGHT, IK_KPRIGHT, VK_RIGHT:
1271 if CPos <= Length(Line) then
1272 CPos := CPos + 1;
1273 IK_RETURN, IK_KPRETURN, VK_OPEN, VK_FIRE:
1274 begin
1275 if Cons_Shown then
1276 g_Console_Process(Line)
1277 else
1278 if gChatShow then
1279 begin
1280 if (Length(Line) > 0) and g_Game_IsNet then
1281 begin
1282 if gChatTeam then
1283 begin
1284 if g_Game_IsClient then
1285 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1286 else
1287 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1288 NET_CHAT_TEAM, gPlayer1Settings.Team);
1289 end
1290 else
1291 begin
1292 if g_Game_IsClient then
1293 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1294 else
1295 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1296 NET_CHAT_PLAYER);
1297 end;
1298 end;
1300 Line := '';
1301 CPos := 1;
1302 gChatShow := False;
1303 gJustChatted := True;
1304 g_Touch_ShowKeyboard(gConsoleShow or gChatShow);
1305 end;
1306 end;
1307 IK_TAB:
1308 if not gChatShow then
1309 Complete();
1310 IK_DOWN, IK_KPDOWN, VK_DOWN:
1311 if not gChatShow then
1312 if (CommandHistory <> nil) and
1313 (CmdIndex < Length(CommandHistory)) then
1314 begin
1315 if CmdIndex < Length(CommandHistory)-1 then
1316 CmdIndex := CmdIndex + 1;
1317 Line := CommandHistory[CmdIndex];
1318 CPos := Length(Line) + 1;
1319 end;
1320 IK_UP, IK_KPUP, VK_UP:
1321 if not gChatShow then
1322 if (CommandHistory <> nil) and
1323 (CmdIndex <= Length(CommandHistory)) then
1324 begin
1325 if CmdIndex > 0 then
1326 CmdIndex := CmdIndex - 1;
1327 Line := CommandHistory[CmdIndex];
1328 Cpos := Length(Line) + 1;
1329 end;
1330 IK_PAGEUP, IK_KPPAGEUP, VK_PREV: // PgUp
1331 if not gChatShow then Inc(conSkipLines);
1332 IK_PAGEDN, IK_KPPAGEDN, VK_NEXT: // PgDown
1333 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1334 IK_HOME, IK_KPHOME:
1335 CPos := 1;
1336 IK_END, IK_KPEND:
1337 CPos := Length(Line) + 1;
1338 end;
1339 end;
1341 function GetStr(var Str: AnsiString): AnsiString;
1342 var
1343 a, b: Integer;
1344 begin
1345 Result := '';
1346 if Str[1] = '"' then
1347 begin
1348 for b := 1 to Length(Str) do
1349 if (b = Length(Str)) or (Str[b+1] = '"') then
1350 begin
1351 Result := Copy(Str, 2, b-1);
1352 Delete(Str, 1, b+1);
1353 Str := Trim(Str);
1354 Exit;
1355 end;
1356 end;
1358 for a := 1 to Length(Str) do
1359 if (a = Length(Str)) or (Str[a+1] = ' ') then
1360 begin
1361 Result := Copy(Str, 1, a);
1362 Delete(Str, 1, a+1);
1363 Str := Trim(Str);
1364 Exit;
1365 end;
1366 end;
1368 function ParseString(Str: AnsiString): SSArray;
1369 begin
1370 Result := nil;
1372 Str := Trim(Str);
1374 if Str = '' then
1375 Exit;
1377 while Str <> '' do
1378 begin
1379 SetLength(Result, Length(Result)+1);
1380 Result[High(Result)] := GetStr(Str);
1381 end;
1382 end;
1384 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1386 procedure conmsg (s: AnsiString);
1387 var
1388 a: Integer;
1389 begin
1390 if length(s) = 0 then exit;
1391 for a := 0 to High(MsgArray) do
1392 begin
1393 with MsgArray[a] do
1394 begin
1395 if Time = 0 then
1396 begin
1397 Msg := s;
1398 Time := MsgTime;
1399 exit;
1400 end;
1401 end;
1402 end;
1403 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1404 with MsgArray[High(MsgArray)] do
1405 begin
1406 Msg := L;
1407 Time := MsgTime;
1408 end;
1409 end;
1411 var
1412 f: Integer;
1413 begin
1414 // put it to console
1415 cbufPut(L);
1416 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1418 // now show 'em out of console too
1419 show := show and gAllowConsoleMessages;
1420 if show and gShowMessages then
1421 begin
1422 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1423 while length(L) > 0 do
1424 begin
1425 f := Pos(#10, L);
1426 if f <= 0 then f := length(L)+1;
1427 conmsg(Copy(L, 1, f-1));
1428 Delete(L, 1, f);
1429 end;
1430 end;
1432 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1433 //ConsoleHistory[High(ConsoleHistory)] := L;
1435 (*
1436 {$IFDEF HEADLESS}
1437 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1438 {$ENDIF}
1439 *)
1440 end;
1443 var
1444 consolewriterLastWasEOL: Boolean = false;
1446 procedure consolewriter (constref buf; len: SizeUInt);
1447 var
1448 b: PByte;
1449 begin
1450 if (len < 1) then exit;
1451 b := PByte(@buf);
1452 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1453 while (len > 0) do
1454 begin
1455 if (b[0] <> 13) and (b[0] <> 10) then
1456 begin
1457 cbufPut(AnsiChar(b[0]));
1458 end
1459 else
1460 begin
1461 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1462 cbufPut(#10);
1463 end;
1464 len -= 1;
1465 b += 1;
1466 end;
1467 end;
1470 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1471 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1472 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1473 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1474 begin
1475 g_Console_Add(s, show);
1476 end;
1479 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1480 begin
1481 if show then
1482 begin
1483 g_Console_Add(formatstrf(s, args), true);
1484 end
1485 else
1486 begin
1487 consolewriterLastWasEOL := false;
1488 formatstrf(s, args, consolewriter);
1489 if not consolewriterLastWasEOL then cbufPut(#10);
1490 end;
1491 end;
1494 procedure g_Console_Clear();
1495 begin
1496 //ConsoleHistory := nil;
1497 cbufClear();
1498 conSkipLines := 0;
1499 end;
1501 procedure AddToHistory(L: AnsiString);
1502 var
1503 len: Integer;
1504 begin
1505 len := Length(CommandHistory);
1507 if (len = 0) or
1508 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1509 begin
1510 SetLength(CommandHistory, len+1);
1511 CommandHistory[len] := L;
1512 end;
1514 CmdIndex := Length(CommandHistory);
1515 end;
1517 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1518 var
1519 Arr: SSArray;
1520 i: Integer;
1521 begin
1522 Result := True;
1524 Arr := nil;
1526 if Trim(C) = '' then
1527 Exit;
1529 Arr := ParseString(C);
1530 if Arr = nil then
1531 Exit;
1533 for i := 0 to High(Whitelist) do
1534 if Whitelist[i] = LowerCase(Arr[0]) then
1535 Result := False;
1536 end;
1538 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1539 var
1540 Arr: SSArray;
1541 i: Integer;
1542 begin
1543 Arr := nil;
1545 if Trim(L) = '' then
1546 Exit;
1548 conSkipLines := 0; // "unscroll"
1550 if L = 'goobers' then
1551 begin
1552 Line := '';
1553 CPos := 1;
1554 gCheats := true;
1555 g_Console_Add('Your memory serves you well.');
1556 exit;
1557 end;
1559 if not quiet then
1560 begin
1561 g_Console_Add('> '+L);
1562 Line := '';
1563 CPos := 1;
1564 end;
1566 Arr := ParseString(L);
1567 if Arr = nil then
1568 Exit;
1570 if commands = nil then
1571 Exit;
1573 if not quiet then
1574 AddToHistory(L);
1576 for i := 0 to High(commands) do
1577 begin
1578 if commands[i].cmd = LowerCase(Arr[0]) then
1579 begin
1580 if commands[i].action >= 0 then
1581 begin
1582 gPlayerAction[commands[i].player, commands[i].action] := commands[i].cmd[1] = '+';
1583 exit
1584 end;
1585 if assigned(commands[i].procEx) then
1586 begin
1587 commands[i].procEx(@commands[i], Arr);
1588 exit
1589 end;
1590 if assigned(commands[i].proc) then
1591 begin
1592 commands[i].proc(Arr);
1593 exit
1594 end
1595 end
1596 end;
1598 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1599 end;
1602 function g_Console_Interactive: Boolean;
1603 begin
1604 Result := gConsoleShow
1605 end;
1607 procedure g_Console_BindKey (key: Integer; down: AnsiString; up: AnsiString = '');
1608 begin
1609 //e_LogWritefln('bind "%s" "%s" <%s>', [LowerCase(e_KeyNames[key]), cmd, key]);
1610 ASSERT(key >= 0);
1611 ASSERT(key < e_MaxInputKeys);
1612 if key > 0 then
1613 begin
1614 gInputBinds[key].down := ParseAlias(down);
1615 gInputBinds[key].up := ParseAlias(up)
1616 end
1617 end;
1619 function g_Console_FindBind (n: Integer; down: AnsiString; up: AnsiString = ''): Integer;
1620 var i: Integer;
1622 function EqualsCommandLists (a, b: SSArray): Boolean;
1623 var i, len: Integer;
1624 begin
1625 result := False;
1626 len := Length(a);
1627 if len = Length(b) then
1628 begin
1629 i := 0;
1630 while (i < len) and (a[i] = b[i]) do inc(i);
1631 if i >= len then
1632 result := True
1633 end
1634 end;
1636 begin
1637 ASSERT(n >= 1);
1638 result := 0;
1639 if commands = nil then Exit;
1640 i := 0;
1641 while (n >= 1) and (i < e_MaxInputKeys) do
1642 begin
1643 if EqualsCommandLists(ParseAlias(down), gInputBinds[i].down) then
1644 begin
1645 if EqualsCommandLists(ParseAlias(up), gInputBinds[i].up) then
1646 begin
1647 result := i;
1648 dec(n)
1649 end
1650 end;
1651 inc(i)
1652 end;
1653 if n >= 1 then
1654 result := 0
1655 end;
1657 function g_Console_Action (action: Integer): Boolean;
1658 var i, len: Integer;
1659 begin
1660 ASSERT(action >= FIRST_ACTION);
1661 ASSERT(action <= LAST_ACTION);
1662 i := 0;
1663 len := Length(gPlayerAction);
1664 while (i < len) and (not gPlayerAction[i, action]) do inc(i);
1665 Result := i < len
1666 end;
1668 procedure g_Console_ProcessBind (key: Integer; down: Boolean);
1669 var i: Integer;
1670 begin
1671 if (not gChatShow) and (not gConsoleShow) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
1672 begin
1673 if down then
1674 for i := 0 to High(gInputBinds[key].down) do
1675 g_Console_Process(gInputBinds[key].down[i], True)
1676 else
1677 for i := 0 to High(gInputBinds[key].up) do
1678 g_Console_Process(gInputBinds[key].up[i], True)
1679 end
1680 end;
1682 procedure g_Console_ResetBinds;
1683 var i: Integer;
1684 begin
1685 for i := 0 to e_MaxInputKeys - 1 do
1686 g_Console_BindKey(i, '', '');
1688 g_Console_BindKey(IK_A, '+p1_moveleft', '-p1_moveleft');
1689 g_Console_BindKey(IK_D, '+p1_moveright', '-p1_moveright');
1690 g_Console_BindKey(IK_W, '+p1_lookup', '-p1_lookup');
1691 g_Console_BindKey(IK_S, '+p1_lookdown', '-p1_lookdown');
1692 g_Console_BindKey(IK_SPACE, '+p1_jump', '-p1_jump');
1693 g_Console_BindKey(IK_H, '+p1_attack', '-p1_attack');
1694 g_Console_BindKey(IK_J, '+p1_activate', '-p1_activate');
1695 g_Console_BindKey(IK_E, '+p1_weapnext', '-p1_weapnext');
1696 g_Console_BindKey(IK_Q, '+p1_weapprev', '-p1_weapprev');
1697 g_Console_BindKey(IK_ALT, '+p1_strafe', '-p1_strafe');
1698 g_Console_BindKey(IK_1, 'p1_weapon 1');
1699 g_Console_BindKey(IK_2, 'p1_weapon 2');
1700 g_Console_BindKey(IK_3, 'p1_weapon 3');
1701 g_Console_BindKey(IK_4, 'p1_weapon 4');
1702 g_Console_BindKey(IK_5, 'p1_weapon 5');
1703 g_Console_BindKey(IK_6, 'p1_weapon 6');
1704 g_Console_BindKey(IK_7, 'p1_weapon 7');
1705 g_Console_BindKey(IK_8, 'p1_weapon 8');
1706 g_Console_BindKey(IK_9, 'p1_weapon 9');
1707 g_Console_BindKey(IK_0, 'p1_weapon 10');
1708 g_Console_BindKey(IK_MINUS, 'p1_weapon 11');
1709 g_Console_BindKey(IK_T, 'togglechat');
1710 g_Console_BindKey(IK_Y, 'toggleteamchat');
1711 g_Console_BindKey(IK_F11, 'screenshot');
1712 g_Console_BindKey(IK_TAB, '+p1_scores', '-p1_scores');
1713 g_Console_BindKey(IK_PAUSE, 'pause');
1714 g_Console_BindKey(IK_F1, 'vote');
1716 (* for i := 0 to e_MaxJoys - 1 do *)
1717 for i := 0 to 1 do
1718 begin
1719 g_Console_BindKey(e_JoyAxisToKey(i, 0, 0), '+p' + IntToStr(i mod 2 + 1) + '_moveleft', '-p' + IntToStr(i mod 2 + 1) + '_moveleft');
1720 g_Console_BindKey(e_JoyAxisToKey(i, 0, 1), '+p' + IntToStr(i mod 2 + 1) + '_moveright', '-p' + IntToStr(i mod 2 + 1) + '_moveright');
1721 g_Console_BindKey(e_JoyAxisToKey(i, 1, 0), '+p' + IntToStr(i mod 2 + 1) + '_lookup', '-p' + IntToStr(i mod 2 + 1) + '_lookup');
1722 g_Console_BindKey(e_JoyAxisToKey(i, 1, 1), '+p' + IntToStr(i mod 2 + 1) + '_lookdown', '-p' + IntToStr(i mod 2 + 1) + '_lookdown');
1723 g_Console_BindKey(e_JoyButtonToKey(i, 2), '+p' + IntToStr(i mod 2 + 1) + '_jump', '-p' + IntToStr(i mod 2 + 1) + '_jump');
1724 g_Console_BindKey(e_JoyButtonToKey(i, 0), '+p' + IntToStr(i mod 2 + 1) + '_attack', '-p' + IntToStr(i mod 2 + 1) + '_attack');
1725 g_Console_BindKey(e_JoyButtonToKey(i, 3), '+p' + IntToStr(i mod 2 + 1) + '_activate', '-p' + IntToStr(i mod 2 + 1) + '_activate');
1726 g_Console_BindKey(e_JoyButtonToKey(i, 1), '+p' + IntToStr(i mod 2 + 1) + '_weapnext', '-p' + IntToStr(i mod 2 + 1) + '_weapnext');
1727 g_Console_BindKey(e_JoyButtonToKey(i, 4), '+p' + IntToStr(i mod 2 + 1) + '_weapprev', '-p' + IntToStr(i mod 2 + 1) + '_weapprev');
1728 g_Console_BindKey(e_JoyButtonToKey(i, 7), '+p' + IntToStr(i mod 2 + 1) + '_strafe', '-p' + IntToStr(i mod 2 + 1) + '_strafe');
1729 end;
1731 g_Console_BindKey(VK_LSTRAFE, '+moveleft; +strafe', '-moveleft; -strafe');
1732 g_Console_BindKey(VK_RSTRAFE, '+moveright; +strafe', '-moveright; -strafe');
1733 g_Console_BindKey(VK_LEFT, '+moveleft', '-moveleft');
1734 g_Console_BindKey(VK_RIGHT, '+moveright', '-moveright');
1735 g_Console_BindKey(VK_UP, '+lookup', '-lookup');
1736 g_Console_BindKey(VK_DOWN, '+lookdown', '-lookdown');
1737 g_Console_BindKey(VK_JUMP, '+jump', '-jump');
1738 g_Console_BindKey(VK_FIRE, '+attack', '-attack');
1739 g_Console_BindKey(VK_OPEN, '+activate', '-activate');
1740 g_Console_BindKey(VK_NEXT, '+weapnext', '-weapnext');
1741 g_Console_BindKey(VK_PREV, '+weapprev', '-weapprev');
1742 g_Console_BindKey(VK_STRAFE, '+strafe', '-strafe');
1743 g_Console_BindKey(VK_0, 'weapon 1');
1744 g_Console_BindKey(VK_1, 'weapon 2');
1745 g_Console_BindKey(VK_2, 'weapon 3');
1746 g_Console_BindKey(VK_3, 'weapon 4');
1747 g_Console_BindKey(VK_4, 'weapon 5');
1748 g_Console_BindKey(VK_5, 'weapon 6');
1749 g_Console_BindKey(VK_6, 'weapon 7');
1750 g_Console_BindKey(VK_7, 'weapon 8');
1751 g_Console_BindKey(VK_8, 'weapon 9');
1752 g_Console_BindKey(VK_9, 'weapon 10');
1753 g_Console_BindKey(VK_A, 'weapon 11');
1754 g_Console_BindKey(VK_CHAT, 'togglechat');
1755 g_Console_BindKey(VK_TEAM, 'toggleteamchat');
1756 g_Console_BindKey(VK_PRINTSCR, 'screenshot');
1757 g_Console_BindKey(VK_STATUS, '+scores', '-scores');
1758 g_Console_BindKey(VK_SHOWKBD, 'showkeyboard');
1759 g_Console_BindKey(VK_HIDEKBD, 'hidekeyboard');
1761 // VK_CONSOLE
1762 // VK_ESCAPE
1763 end;
1765 procedure g_Console_ReadConfig (filename: String);
1766 var f: TextFile; s: AnsiString; i, len: Integer;
1767 begin
1768 if FileExists(filename) then
1769 begin
1770 AssignFile(f, filename);
1771 Reset(f);
1772 while not EOF(f) do
1773 begin
1774 ReadLn(f, s);
1775 len := Length(s);
1776 if len > 0 then
1777 begin
1778 i := 1;
1779 (* skip spaces *)
1780 while (i <= len) and (s[i] <= ' ') do inc(i);
1781 (* skip comments *)
1782 if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
1783 g_Console_Process(s, True)
1784 end
1785 end;
1786 CloseFile(f)
1787 end
1788 end;
1790 procedure g_Console_WriteConfig (filename: String);
1791 var f: TextFile; i, j: Integer;
1792 begin
1793 AssignFile(f, filename);
1794 Rewrite(f);
1795 WriteLn(f, '// generated by doom2d, do not modify');
1796 WriteLn(f, 'unbindall');
1797 for i := 0 to e_MaxInputKeys - 1 do
1798 begin
1799 if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
1800 begin
1801 Write(f, 'bind ', e_KeyNames[i], ' "');
1802 if Length(gInputBinds[i].down) > 0 then
1803 begin
1804 Write(f, gInputBinds[i].down[0]);
1805 for j := 1 to High(gInputBinds[i].down) do
1806 Write(f, '; ', gInputBinds[i].down[j])
1807 end;
1808 Write(f, '"');
1809 if Length(gInputBinds[i].up) > 0 then
1810 begin
1811 Write(f, ' "', gInputBinds[i].up[0]);
1812 for j := 1 to High(gInputBinds[i].up) do
1813 Write(f, '; ', gInputBinds[i].up[j]);
1814 Write(f, '"')
1815 end;
1816 WriteLn(f)
1817 end
1818 end;
1819 for i := 0 to High(commands) do
1820 begin
1821 if not commands[i].cheat then
1822 begin
1823 if @commands[i].procEx = @boolVarHandler then
1824 begin
1825 if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
1826 WriteLn(f, commands[i].cmd, ' "', j, '"')
1827 end
1828 else if @commands[i].procEx = @intVarHandler then
1829 begin
1830 WriteLn(f, commands[i].cmd, ' "', PInteger(commands[i].ptr)^, '"')
1831 end
1832 else if @commands[i].procEx = @singleVarHandler then
1833 begin
1834 WriteLn(f, commands[i].cmd, ' "', PVarSingle(commands[i].ptr).val^:0:6, '"')
1835 end
1836 end
1837 end;
1838 CloseFile(f)
1839 end;
1842 end.