DEADSOFTWARE

"dbg_scale" debug variable
[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 wadreader; // for SArray
24 procedure g_Console_Init ();
25 procedure g_Console_Update ();
26 procedure g_Console_Draw ();
27 procedure g_Console_Switch ();
28 procedure g_Console_Char (C: AnsiChar);
29 procedure g_Console_Control (K: Word);
30 procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
31 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
32 procedure g_Console_Clear ();
33 function g_Console_CommandBlacklisted (C: AnsiString): Boolean;
35 procedure conwriteln (const s: AnsiString; show: Boolean=false);
36 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
38 // <0: no arg; 0/1: true/false
39 function conGetBoolArg (p: SArray; idx: Integer): Integer;
41 procedure g_Console_Chat_Switch (team: Boolean=false);
43 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
44 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
47 var
48 gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
49 gChatShow: Boolean = false;
50 gChatTeam: Boolean = false;
51 gAllowConsoleMessages: Boolean = true;
52 gChatEnter: Boolean = true;
53 gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
56 implementation
58 uses
59 g_textures, g_main, e_graphics, e_input, g_game,
60 SysUtils, g_basic, g_options, Math,
61 g_menu, g_language, g_net, g_netmsg, e_log, conbuf, utils;
64 type
65 PCommand = ^TCommand;
67 TCmdProc = procedure (p: SArray);
68 TCmdProcEx = procedure (me: PCommand; p: SArray);
70 TCommand = record
71 cmd: AnsiString;
72 proc: TCmdProc;
73 procEx: TCmdProcEx;
74 help: AnsiString;
75 hidden: Boolean;
76 ptr: Pointer; // various data
77 msg: AnsiString; // message for var changes
78 cheat: Boolean;
79 end;
81 TAlias = record
82 name: AnsiString;
83 commands: SArray;
84 end;
87 const
88 Step = 32;
89 Alpha = 25;
90 MsgTime = 144;
91 MaxScriptRecursion = 16;
93 DEBUG_STRING = 'DEBUG MODE';
95 var
96 ID: DWORD;
97 RecursionDepth: Word = 0;
98 RecursionLimitHit: Boolean = False;
99 Cons_Y: SmallInt;
100 Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
101 Line: AnsiString;
102 CPos: Word;
103 //ConsoleHistory: SArray;
104 CommandHistory: SArray;
105 Whitelist: SArray;
106 commands: Array of TCommand = nil;
107 Aliases: Array of TAlias = nil;
108 CmdIndex: Word;
109 conSkipLines: Integer = 0;
110 MsgArray: Array [0..4] of record
111 Msg: AnsiString;
112 Time: Word;
113 end;
116 // ////////////////////////////////////////////////////////////////////////// //
117 // <0: no arg; 0/1: true/false; 666: toggle
118 function conGetBoolArg (p: SArray; idx: Integer): Integer;
119 begin
120 if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
121 result := 0;
122 if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
123 (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
124 else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
125 (CompareText(p[idx], 't') = 0) then result := 666;
126 end;
129 procedure boolVarHandler (me: PCommand; p: SArray);
130 procedure binaryFlag (var flag: Boolean; msg: AnsiString);
131 begin
132 if (Length(p) > 2) then
133 begin
134 conwritefln('too many arguments to ''%s''', [p[0]]);
135 end
136 else
137 begin
138 case conGetBoolArg(p, 1) of
139 -1: begin end;
140 0: if conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
141 1: if conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
142 666: if conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
143 end;
144 if flag then conwritefln('%s: tan', [msg]) else conwritefln('%s: ona', [msg]);
145 end;
146 end;
147 begin
148 binaryFlag(PBoolean(me.ptr)^, me.msg);
149 end;
152 procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
153 var
154 f: Integer;
155 cp: PCommand;
156 begin
157 f := Length(commands);
158 SetLength(commands, f+1);
159 cp := @commands[f];
160 cp.cmd := LowerCase(conname);
161 cp.proc := nil;
162 cp.procEx := boolVarHandler;
163 cp.help := ahelp;
164 cp.hidden := false;
165 cp.ptr := pvar;
166 cp.msg := amsg;
167 cp.cheat := acheat;
168 end;
171 // ////////////////////////////////////////////////////////////////////////// //
172 type
173 PVarSingle = ^TVarSingle;
174 TVarSingle = record
175 val: PSingle;
176 min, max, def: Single; // default will be starting value
177 end;
180 procedure singleVarHandler (me: PCommand; p: SArray);
181 // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
182 function parseFloat (var res: Single; const s: AnsiString): Boolean;
183 var
184 pos: Integer = 1;
185 frac: Single = 1;
186 slen: Integer;
187 begin
188 result := false;
189 res := 0;
190 slen := Length(s);
191 while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
192 while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
193 if (pos > slen) then exit;
194 if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
195 // integral part
196 while (pos <= slen) do
197 begin
198 if (s[pos] < '0') or (s[pos] > '9') then break;
199 res := res*10+Byte(s[pos])-48;
200 Inc(pos);
201 end;
202 if (pos <= slen) then
203 begin
204 // must be a dot
205 if (s[pos] <> '.') then exit;
206 Inc(pos);
207 while (pos <= slen) do
208 begin
209 if (s[pos] < '0') or (s[pos] > '9') then break;
210 frac := frac/10;
211 res += frac*(Byte(s[pos])-48);
212 Inc(pos);
213 end;
214 end;
215 if (pos <= slen) then exit; // oops
216 result := true;
217 end;
218 var
219 pv: PVarSingle;
220 nv: Single;
221 msg: AnsiString;
222 begin
223 if (Length(p) > 2) then
224 begin
225 conwritefln('too many arguments to ''%s''', [p[0]]);
226 exit;
227 end;
228 pv := PVarSingle(me.ptr);
229 if (Length(p) = 2) then
230 begin
231 if not conIsCheatsEnabled then begin conwriteln('not available'); exit; end;
232 if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
233 (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
234 (CompareText(p[1], 'ona') = 0) then
235 begin
236 pv.val^ := pv.def;
237 end
238 else
239 begin
240 if not parseFloat(nv, p[1]) then
241 begin
242 conwritefln('%s: ''%s'' doesn''t look like a floating number', [p[0], p[1]]);
243 exit;
244 end;
245 if (nv < pv.min) then nv := pv.min;
246 if (nv > pv.max) then nv := pv.max;
247 pv.val^ := nv;
248 end;
249 end;
250 msg := me.msg;
251 if (Length(msg) = 0) then msg := p[0] else msg += ':';
252 conwritefln('%s %s', [msg, pv.val^]);
253 end;
256 procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
257 var
258 f: Integer;
259 cp: PCommand;
260 pv: PVarSingle;
261 begin
262 GetMem(pv, sizeof(pv^));
263 pv.val := pvar;
264 pv.min := amin;
265 pv.max := amax;
266 pv.def := pvar^;
267 f := Length(commands);
268 SetLength(commands, f+1);
269 cp := @commands[f];
270 cp.cmd := LowerCase(conname);
271 cp.proc := nil;
272 cp.procEx := singleVarHandler;
273 cp.help := ahelp;
274 cp.hidden := false;
275 cp.ptr := pv;
276 cp.msg := amsg;
277 cp.cheat := acheat;
278 end;
281 // ////////////////////////////////////////////////////////////////////////// //
282 function GetStrACmd(var Str: AnsiString): AnsiString;
283 var
284 a: Integer;
285 begin
286 Result := '';
287 for a := 1 to Length(Str) do
288 if (a = Length(Str)) or (Str[a+1] = ';') then
289 begin
290 Result := Copy(Str, 1, a);
291 Delete(Str, 1, a+1);
292 Str := Trim(Str);
293 Exit;
294 end;
295 end;
297 function ParseAlias(Str: AnsiString): SArray;
298 begin
299 Result := nil;
301 Str := Trim(Str);
303 if Str = '' then
304 Exit;
306 while Str <> '' do
307 begin
308 SetLength(Result, Length(Result)+1);
309 Result[High(Result)] := GetStrACmd(Str);
310 end;
311 end;
313 procedure ConsoleCommands(p: SArray);
314 var
315 cmd, s: AnsiString;
316 a, b: Integer;
317 F: TextFile;
318 begin
319 cmd := LowerCase(p[0]);
320 s := '';
322 if cmd = 'clear' then
323 begin
324 //ConsoleHistory := nil;
325 cbufClear();
326 conSkipLines := 0;
328 for a := 0 to High(MsgArray) do
329 with MsgArray[a] do
330 begin
331 Msg := '';
332 Time := 0;
333 end;
334 end;
336 if cmd = 'clearhistory' then
337 CommandHistory := nil;
339 if cmd = 'showhistory' then
340 if CommandHistory <> nil then
341 begin
342 g_Console_Add('');
343 for a := 0 to High(CommandHistory) do
344 g_Console_Add(' '+CommandHistory[a]);
345 end;
347 if cmd = 'commands' then
348 begin
349 g_Console_Add('');
350 g_Console_Add('commands list:');
351 for a := High(commands) downto 0 do
352 begin
353 if (Length(commands[a].help) > 0) then
354 begin
355 g_Console_Add(' '+commands[a].cmd+' -- '+commands[a].help);
356 end
357 else
358 begin
359 g_Console_Add(' '+commands[a].cmd);
360 end;
361 end;
362 end;
364 if cmd = 'time' then
365 g_Console_Add(TimeToStr(Now), True);
367 if cmd = 'date' then
368 g_Console_Add(DateToStr(Now), True);
370 if cmd = 'echo' then
371 if Length(p) > 1 then
372 begin
373 if p[1] = 'ololo' then
374 gCheats := True
375 else
376 begin
377 s := '';
378 for a := 1 to High(p) do
379 s := s + p[a] + ' ';
380 g_Console_Add(b_Text_Format(s), True);
381 end;
382 end
383 else
384 g_Console_Add('');
386 if cmd = 'dump' then
387 begin
388 (*
389 if ConsoleHistory <> nil then
390 begin
391 if Length(P) > 1 then
392 s := P[1]
393 else
394 s := GameDir+'/console.txt';
396 {$I-}
397 AssignFile(F, s);
398 Rewrite(F);
399 if IOResult <> 0 then
400 begin
401 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [s]));
402 CloseFile(F);
403 Exit;
404 end;
406 for a := 0 to High(ConsoleHistory) do
407 WriteLn(F, ConsoleHistory[a]);
409 CloseFile(F);
410 g_Console_Add(Format(_lc[I_CONSOLE_DUMPED], [s]));
411 {$I+}
412 end;
413 *)
414 end;
416 if cmd = 'exec' then
417 begin
418 // exec <filename>
419 if Length(p) > 1 then
420 begin
421 s := GameDir+'/'+p[1];
423 {$I-}
424 AssignFile(F, s);
425 Reset(F);
426 if IOResult <> 0 then
427 begin
428 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
429 CloseFile(F);
430 Exit;
431 end;
432 g_Console_Add(Format(_lc[I_CONSOLE_EXEC], [s]));
434 while not EOF(F) do
435 begin
436 ReadLn(F, s);
437 if IOResult <> 0 then
438 begin
439 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_READ], [s]));
440 CloseFile(F);
441 Exit;
442 end;
443 if Pos('#', s) <> 1 then // script comment
444 begin
445 // prevents endless loops
446 Inc(RecursionDepth);
447 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
448 if not RecursionLimitHit then
449 g_Console_Process(s, True);
450 Dec(RecursionDepth);
451 end;
452 end;
453 if (RecursionDepth = 0) and RecursionLimitHit then
454 begin
455 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
456 RecursionLimitHit := False;
457 end;
459 CloseFile(F);
460 {$I+}
461 end
462 else
463 g_Console_Add('exec <script file>');
464 end;
466 if cmd = 'alias' then
467 begin
468 // alias [alias_name] [commands]
469 if Length(p) > 1 then
470 begin
471 for a := 0 to High(Aliases) do
472 if Aliases[a].name = p[1] then
473 begin
474 if Length(p) > 2 then
475 Aliases[a].commands := ParseAlias(p[2])
476 else
477 for b := 0 to High(Aliases[a].commands) do
478 g_Console_Add(Aliases[a].commands[b]);
479 Exit;
480 end;
481 SetLength(Aliases, Length(Aliases)+1);
482 a := High(Aliases);
483 Aliases[a].name := p[1];
484 if Length(p) > 2 then
485 Aliases[a].commands := ParseAlias(p[2])
486 else
487 for b := 0 to High(Aliases[a].commands) do
488 g_Console_Add(Aliases[a].commands[b]);
489 end else
490 for a := 0 to High(Aliases) do
491 if Aliases[a].commands <> nil then
492 g_Console_Add(Aliases[a].name);
493 end;
495 if cmd = 'call' then
496 begin
497 // call <alias_name>
498 if Length(p) > 1 then
499 begin
500 if Aliases = nil then
501 Exit;
502 for a := 0 to High(Aliases) do
503 if Aliases[a].name = p[1] then
504 begin
505 if Aliases[a].commands <> nil then
506 begin
507 // with this system proper endless loop detection seems either impossible
508 // or very dirty to implement, so let's have this instead
509 // prevents endless loops
510 for b := 0 to High(Aliases[a].commands) do
511 begin
512 Inc(RecursionDepth);
513 RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
514 if not RecursionLimitHit then
515 g_Console_Process(Aliases[a].commands[b], True);
516 Dec(RecursionDepth);
517 end;
518 if (RecursionDepth = 0) and RecursionLimitHit then
519 begin
520 g_Console_Add(Format(_lc[I_CONSOLE_ERROR_CALL], [s]));
521 RecursionLimitHit := False;
522 end;
523 end;
524 Exit;
525 end;
526 end
527 else
528 g_Console_Add('call <alias name>');
529 end;
530 end;
532 procedure WhitelistCommand(cmd: AnsiString);
533 var
534 a: Integer;
535 begin
536 SetLength(Whitelist, Length(Whitelist)+1);
537 a := High(Whitelist);
538 Whitelist[a] := LowerCase(cmd);
539 end;
541 procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
542 var
543 a: Integer;
544 cp: PCommand;
545 begin
546 SetLength(commands, Length(commands)+1);
547 a := High(commands);
548 cp := @commands[a];
549 cp.cmd := LowerCase(cmd);
550 cp.proc := proc;
551 cp.procEx := nil;
552 cp.help := ahelp;
553 cp.hidden := ahidden;
554 cp.ptr := nil;
555 cp.msg := '';
556 cp.cheat := acheat;
557 end;
559 procedure g_Console_Init();
560 var
561 a: Integer;
562 begin
563 g_Texture_CreateWAD(ID, GameWAD+':TEXTURES\CONSOLE');
564 Cons_Y := -(gScreenHeight div 2);
565 gConsoleShow := False;
566 gChatShow := False;
567 Cons_Shown := False;
568 CPos := 1;
570 for a := 0 to High(MsgArray) do
571 with MsgArray[a] do
572 begin
573 Msg := '';
574 Time := 0;
575 end;
577 AddCommand('clear', ConsoleCommands, 'clear console');
578 AddCommand('clearhistory', ConsoleCommands);
579 AddCommand('showhistory', ConsoleCommands);
580 AddCommand('commands', ConsoleCommands);
581 AddCommand('time', ConsoleCommands);
582 AddCommand('date', ConsoleCommands);
583 AddCommand('echo', ConsoleCommands);
584 AddCommand('dump', ConsoleCommands);
585 AddCommand('exec', ConsoleCommands);
586 AddCommand('alias', ConsoleCommands);
587 AddCommand('call', ConsoleCommands);
589 AddCommand('d_window', DebugCommands);
590 AddCommand('d_sounds', DebugCommands);
591 AddCommand('d_frames', DebugCommands);
592 AddCommand('d_winmsg', DebugCommands);
593 AddCommand('d_monoff', DebugCommands);
594 AddCommand('d_botoff', DebugCommands);
595 AddCommand('d_monster', DebugCommands);
596 AddCommand('d_health', DebugCommands);
597 AddCommand('d_player', DebugCommands);
598 AddCommand('d_joy', DebugCommands);
600 AddCommand('p1_name', GameCVars);
601 AddCommand('p2_name', GameCVars);
602 AddCommand('p1_color', GameCVars);
603 AddCommand('p2_color', GameCVars);
604 AddCommand('r_showfps', GameCVars);
605 AddCommand('r_showtime', GameCVars);
606 AddCommand('r_showscore', GameCVars);
607 AddCommand('r_showlives', GameCVars);
608 AddCommand('r_showstat', GameCVars);
609 AddCommand('r_showkillmsg', GameCVars);
610 AddCommand('r_showspect', GameCVars);
611 AddCommand('r_showping', GameCVars);
612 AddCommand('g_gamemode', GameCVars);
613 AddCommand('g_friendlyfire', GameCVars);
614 AddCommand('g_weaponstay', GameCVars);
615 AddCommand('g_allow_exit', GameCVars);
616 AddCommand('g_allow_monsters', GameCVars);
617 AddCommand('g_bot_vsmonsters', GameCVars);
618 AddCommand('g_bot_vsplayers', GameCVars);
619 AddCommand('g_scorelimit', GameCVars);
620 AddCommand('g_timelimit', GameCVars);
621 AddCommand('g_maxlives', GameCVars);
622 AddCommand('g_warmuptime', GameCVars);
623 AddCommand('net_interp', GameCVars);
624 AddCommand('net_forceplayerupdate', GameCVars);
625 AddCommand('net_predictself', GameCVars);
626 AddCommand('sv_name', GameCVars);
627 AddCommand('sv_passwd', GameCVars);
628 AddCommand('sv_maxplrs', GameCVars);
629 AddCommand('sv_public', GameCVars);
630 AddCommand('sv_intertime', GameCVars);
632 AddCommand('quit', GameCommands);
633 AddCommand('exit', GameCommands);
634 AddCommand('pause', GameCommands);
635 AddCommand('endgame', GameCommands);
636 AddCommand('restart', GameCommands);
637 AddCommand('addbot', GameCommands);
638 AddCommand('bot_add', GameCommands);
639 AddCommand('bot_addlist', GameCommands);
640 AddCommand('bot_addred', GameCommands);
641 AddCommand('bot_addblue', GameCommands);
642 AddCommand('bot_removeall', GameCommands);
643 AddCommand('chat', GameCommands);
644 AddCommand('teamchat', GameCommands);
645 AddCommand('game', GameCommands);
646 AddCommand('host', GameCommands);
647 AddCommand('map', GameCommands);
648 AddCommand('nextmap', GameCommands);
649 AddCommand('endmap', GameCommands);
650 AddCommand('goodbye', GameCommands);
651 AddCommand('suicide', GameCommands);
652 AddCommand('spectate', GameCommands);
653 AddCommand('ready', GameCommands);
654 AddCommand('kick', GameCommands);
655 AddCommand('kick_id', GameCommands);
656 AddCommand('ban', GameCommands);
657 AddCommand('permban', GameCommands);
658 AddCommand('ban_id', GameCommands);
659 AddCommand('permban_id', GameCommands);
660 AddCommand('unban', GameCommands);
661 AddCommand('connect', GameCommands);
662 AddCommand('disconnect', GameCommands);
663 AddCommand('reconnect', GameCommands);
664 AddCommand('say', GameCommands);
665 AddCommand('tell', GameCommands);
666 AddCommand('overtime', GameCommands);
667 AddCommand('rcon_password', GameCommands);
668 AddCommand('rcon', GameCommands);
669 AddCommand('callvote', GameCommands);
670 AddCommand('vote', GameCommands);
671 AddCommand('clientlist', GameCommands);
672 AddCommand('event', GameCommands);
674 AddCommand('god', GameCheats);
675 AddCommand('notarget', GameCheats);
676 AddCommand('give', GameCheats); // "exit" too ;-)
677 AddCommand('open', GameCheats);
678 AddCommand('fly', GameCheats);
679 AddCommand('noclip', GameCheats);
680 AddCommand('speedy', GameCheats);
681 AddCommand('jumpy', GameCheats);
682 AddCommand('noreload', GameCheats);
683 AddCommand('aimline', GameCheats);
684 AddCommand('automap', GameCheats);
686 WhitelistCommand('say');
687 WhitelistCommand('tell');
688 WhitelistCommand('overtime');
689 WhitelistCommand('ready');
690 WhitelistCommand('map');
691 WhitelistCommand('nextmap');
692 WhitelistCommand('endmap');
693 WhitelistCommand('restart');
694 WhitelistCommand('kick');
695 WhitelistCommand('ban');
697 WhitelistCommand('addbot');
698 WhitelistCommand('bot_add');
699 WhitelistCommand('bot_addred');
700 WhitelistCommand('bot_addblue');
701 WhitelistCommand('bot_removeall');
703 WhitelistCommand('g_gamemode');
704 WhitelistCommand('g_friendlyfire');
705 WhitelistCommand('g_weaponstay');
706 WhitelistCommand('g_allow_exit');
707 WhitelistCommand('g_allow_monsters');
708 WhitelistCommand('g_scorelimit');
709 WhitelistCommand('g_timelimit');
711 g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
712 g_Console_Add('');
713 end;
715 procedure g_Console_Update();
716 var
717 a, b: Integer;
718 begin
719 if Cons_Shown then
720 begin
721 // Â ïðîöåññå îòêðûòèÿ:
722 if gConsoleShow and (Cons_Y < 0) then
723 begin
724 Cons_Y := Cons_Y+Step;
725 end;
727 // Â ïðîöåññå çàêðûòèÿ:
728 if (not gConsoleShow) and
729 (Cons_Y > -(gScreenHeight div 2)) then
730 Cons_Y := Cons_Y-Step;
732 // Îêîí÷àòåëüíî îòêðûëàñü:
733 if Cons_Y > 0 then
734 Cons_Y := 0;
736 // Îêîí÷àòåëüíî çàêðûëàñü:
737 if Cons_Y <= (-(gScreenHeight div 2)) then
738 begin
739 Cons_Y := -(gScreenHeight div 2);
740 Cons_Shown := False;
741 end;
742 end;
744 a := 0;
745 while a <= High(MsgArray) do
746 begin
747 if MsgArray[a].Time > 0 then
748 begin
749 if MsgArray[a].Time = 1 then
750 begin
751 if a < High(MsgArray) then
752 begin
753 for b := a to High(MsgArray)-1 do
754 MsgArray[b] := MsgArray[b+1];
756 MsgArray[High(MsgArray)].Time := 0;
758 a := a - 1;
759 end;
760 end
761 else
762 Dec(MsgArray[a].Time);
763 end;
765 a := a + 1;
766 end;
767 end;
770 procedure drawConsoleText ();
771 var
772 CWidth, CHeight: Byte;
773 ty: Integer;
774 sp, ep: LongWord;
775 skip: Integer;
777 procedure putLine (sp, ep: LongWord);
778 var
779 p: LongWord;
780 wdt, cw: Integer;
781 begin
782 p := sp;
783 wdt := 0;
784 while p <> ep do
785 begin
786 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
787 if wdt+cw > gScreenWidth-8 then break;
788 //e_TextureFontPrintChar(X, Y: Integer; Ch: Char; FontID: DWORD; Shadow: Boolean = False);
789 Inc(wdt, cw);
790 cbufNext(p);
791 end;
792 if p <> ep then putLine(p, ep); // do rest of the line first
793 // now print our part
794 if skip = 0 then
795 begin
796 ep := p;
797 p := sp;
798 wdt := 2;
799 while p <> ep do
800 begin
801 cw := e_TextureFontCharWidth(cbufAt(p), gStdFont);
802 e_TextureFontPrintCharEx(wdt, ty, cbufAt(p), gStdFont);
803 Inc(wdt, cw);
804 cbufNext(p);
805 end;
806 Dec(ty, CHeight);
807 end
808 else
809 begin
810 Dec(skip);
811 end;
812 end;
814 begin
815 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
816 ty := (gScreenHeight div 2)-4-2*CHeight-Abs(Cons_Y);
817 skip := conSkipLines;
818 cbufLastLine(sp, ep);
819 repeat
820 putLine(sp, ep);
821 if ty+CHeight <= 0 then break;
822 until not cbufLineUp(sp, ep);
823 end;
825 procedure g_Console_Draw();
826 var
827 CWidth, CHeight: Byte;
828 mfW, mfH: Word;
829 a, b: Integer;
830 begin
831 e_TextureFontGetSize(gStdFont, CWidth, CHeight);
833 for a := 0 to High(MsgArray) do
834 if MsgArray[a].Time > 0 then
835 e_TextureFontPrintFmt(0, CHeight*a, MsgArray[a].Msg,
836 gStdFont, True);
838 if not Cons_Shown then
839 begin
840 if gChatShow then
841 begin
842 if gChatTeam then
843 begin
844 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say team> ' + Line,
845 gStdFont, 255, 255, 255, 1, True);
846 e_TextureFontPrintEx((CPos + 9)*CWidth, gScreenHeight - CHeight - 1, '_',
847 gStdFont, 255, 255, 255, 1, True);
848 end
849 else
850 begin
851 e_TextureFontPrintEx(0, gScreenHeight - CHeight - 1, 'say> ' + Line,
852 gStdFont, 255, 255, 255, 1, True);
853 e_TextureFontPrintEx((CPos + 4)*CWidth, gScreenHeight - CHeight - 1, '_',
854 gStdFont, 255, 255, 255, 1, True);
855 end;
856 end;
857 Exit;
858 end;
860 if gDebugMode then
861 begin
862 e_CharFont_GetSize(gMenuFont, DEBUG_STRING, mfW, mfH);
863 a := (gScreenWidth - 2*mfW) div 2;
864 b := Cons_Y + ((gScreenHeight div 2) - 2*mfH) div 2;
865 e_CharFont_PrintEx(gMenuFont, a div 2, b div 2, DEBUG_STRING,
866 _RGB(128, 0, 0), 2.0);
867 end;
869 e_DrawSize(ID, 0, Cons_Y, Alpha, False, False, gScreenWidth, gScreenHeight div 2);
870 e_TextureFontPrint(0, Cons_Y+(gScreenHeight div 2)-CHeight-4, '> '+Line, gStdFont);
872 drawConsoleText();
873 (*
874 if ConsoleHistory <> nil then
875 begin
876 b := 0;
877 if CHeight > 0 then
878 if Length(ConsoleHistory) > ((gScreenHeight div 2) div CHeight)-1 then
879 b := Length(ConsoleHistory)-((gScreenHeight div 2) div CHeight)+1;
881 b := Max(b-Offset, 0);
882 d := Max(High(ConsoleHistory)-Offset, 0);
884 c := 2;
885 for a := d downto b do
886 begin
887 e_TextureFontPrintFmt(0, (gScreenHeight div 2)-4-c*CHeight-Abs(Cons_Y), ConsoleHistory[a],
888 gStdFont, True);
889 c := c + 1;
890 end;
891 end;
892 *)
894 e_TextureFontPrint((CPos+1)*CWidth, Cons_Y+(gScreenHeight div 2)-21, '_', gStdFont);
895 end;
897 procedure g_Console_Switch();
898 begin
899 if gChatShow then Exit;
900 gConsoleShow := not gConsoleShow;
901 Cons_Shown := True;
902 end;
904 procedure g_Console_Chat_Switch(Team: Boolean = False);
905 begin
906 if gConsoleShow then Exit;
907 if not g_Game_IsNet then Exit;
908 gChatShow := not gChatShow;
909 gChatTeam := Team;
910 if gChatShow then
911 gChatEnter := False;
912 Line := '';
913 CPos := 1;
914 end;
916 procedure g_Console_Char(C: AnsiChar);
917 begin
918 if gChatShow and (not gChatEnter) then
919 Exit;
920 Insert(C, Line, CPos);
921 CPos := CPos + 1;
922 end;
925 var
926 tcomplist: array of AnsiString = nil;
927 tcompidx: array of Integer = nil;
929 procedure Complete ();
930 var
931 i, c: Integer;
932 tused: Integer;
933 ll, lpfx, cmd: AnsiString;
934 begin
935 if (Length(Line) = 0) then
936 begin
937 g_Console_Add('');
938 for i := 0 to High(commands) do
939 begin
940 if not commands[i].hidden then
941 begin
942 if (Length(commands[i].help) > 0) then
943 begin
944 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
945 end
946 else
947 begin
948 g_Console_Add(' '+commands[i].cmd);
949 end;
950 end;
951 end;
952 exit;
953 end;
955 ll := LowerCase(Line);
956 lpfx := '';
958 if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
959 begin
960 ll := Copy(ll, 0, Length(ll)-1);
961 for i := 0 to High(commands) do
962 begin
963 if commands[i].hidden then continue;
964 if (commands[i].cmd = ll) then
965 begin
966 if (Length(commands[i].help) > 0) then
967 begin
968 g_Console_Add(' '+commands[i].cmd+' -- '+commands[i].help);
969 end;
970 end;
971 end;
972 exit;
973 end;
975 // build completion list
976 tused := 0;
977 for i := 0 to High(commands) do
978 begin
979 if commands[i].hidden then continue;
980 cmd := commands[i].cmd;
981 if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
982 begin
983 if (tused = Length(tcomplist)) then
984 begin
985 SetLength(tcomplist, Length(tcomplist)+128);
986 SetLength(tcompidx, Length(tcompidx)+128);
987 end;
988 tcomplist[tused] := cmd;
989 tcompidx[tused] := i;
990 Inc(tused);
991 if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
992 end;
993 end;
995 // get longest prefix
996 for i := 0 to tused-1 do
997 begin
998 cmd := tcomplist[i];
999 for c := 1 to Length(lpfx) do
1000 begin
1001 if (c > Length(cmd)) then break;
1002 if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
1003 end;
1004 end;
1006 if (tused = 0) then exit;
1008 if (tused = 1) then
1009 begin
1010 Line := tcomplist[0]+' ';
1011 CPos := Length(Line)+1;
1012 end
1013 else
1014 begin
1015 // has longest prefix?
1016 if (Length(lpfx) > Length(ll)) then
1017 begin
1018 Line := lpfx;
1019 CPos:= Length(Line)+1;
1020 end
1021 else
1022 begin
1023 g_Console_Add('');
1024 for i := 0 to tused-1 do
1025 begin
1026 if (Length(commands[tcompidx[i]].help) > 0) then
1027 begin
1028 g_Console_Add(' '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
1029 end
1030 else
1031 begin
1032 g_Console_Add(' '+tcomplist[i]);
1033 end;
1034 end;
1035 end;
1036 end;
1037 end;
1040 procedure g_Console_Control(K: Word);
1041 begin
1042 case K of
1043 IK_BACKSPACE:
1044 if (Length(Line) > 0) and (CPos > 1) then
1045 begin
1046 Delete(Line, CPos-1, 1);
1047 CPos := CPos-1;
1048 end;
1049 IK_DELETE:
1050 if (Length(Line) > 0) and (CPos <= Length(Line)) then
1051 Delete(Line, CPos, 1);
1052 IK_LEFT, IK_KPLEFT:
1053 if CPos > 1 then
1054 CPos := CPos - 1;
1055 IK_RIGHT, IK_KPRIGHT:
1056 if CPos <= Length(Line) then
1057 CPos := CPos + 1;
1058 IK_RETURN, IK_KPRETURN:
1059 begin
1060 if Cons_Shown then
1061 g_Console_Process(Line)
1062 else
1063 if gChatShow then
1064 begin
1065 if (Length(Line) > 0) and g_Game_IsNet then
1066 begin
1067 if gChatTeam then
1068 begin
1069 if g_Game_IsClient then
1070 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
1071 else
1072 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1073 NET_CHAT_TEAM, gPlayer1Settings.Team);
1074 end
1075 else
1076 begin
1077 if g_Game_IsClient then
1078 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
1079 else
1080 MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
1081 NET_CHAT_PLAYER);
1082 end;
1083 end;
1085 Line := '';
1086 CPos := 1;
1087 gChatShow := False;
1088 gJustChatted := True;
1089 end;
1090 end;
1091 IK_TAB:
1092 if not gChatShow then
1093 Complete();
1094 IK_DOWN, IK_KPDOWN:
1095 if not gChatShow then
1096 if (CommandHistory <> nil) and
1097 (CmdIndex < Length(CommandHistory)) then
1098 begin
1099 if CmdIndex < Length(CommandHistory)-1 then
1100 CmdIndex := CmdIndex + 1;
1101 Line := CommandHistory[CmdIndex];
1102 CPos := Length(Line) + 1;
1103 end;
1104 IK_UP, IK_KPUP:
1105 if not gChatShow then
1106 if (CommandHistory <> nil) and
1107 (CmdIndex <= Length(CommandHistory)) then
1108 begin
1109 if CmdIndex > 0 then
1110 CmdIndex := CmdIndex - 1;
1111 Line := CommandHistory[CmdIndex];
1112 Cpos := Length(Line) + 1;
1113 end;
1114 IK_PAGEUP, IK_KPPAGEUP: // PgUp
1115 if not gChatShow then Inc(conSkipLines);
1116 IK_PAGEDN, IK_KPPAGEDN: // PgDown
1117 if not gChatShow and (conSkipLines > 0) then Dec(conSkipLines);
1118 IK_HOME, IK_KPHOME:
1119 CPos := 1;
1120 IK_END, IK_KPEND:
1121 CPos := Length(Line) + 1;
1122 end;
1123 end;
1125 function GetStr(var Str: AnsiString): AnsiString;
1126 var
1127 a, b: Integer;
1128 begin
1129 Result := '';
1130 if Str[1] = '"' then
1131 begin
1132 for b := 1 to Length(Str) do
1133 if (b = Length(Str)) or (Str[b+1] = '"') then
1134 begin
1135 Result := Copy(Str, 2, b-1);
1136 Delete(Str, 1, b+1);
1137 Str := Trim(Str);
1138 Exit;
1139 end;
1140 end;
1142 for a := 1 to Length(Str) do
1143 if (a = Length(Str)) or (Str[a+1] = ' ') then
1144 begin
1145 Result := Copy(Str, 1, a);
1146 Delete(Str, 1, a+1);
1147 Str := Trim(Str);
1148 Exit;
1149 end;
1150 end;
1152 function ParseString(Str: AnsiString): SArray;
1153 begin
1154 Result := nil;
1156 Str := Trim(Str);
1158 if Str = '' then
1159 Exit;
1161 while Str <> '' do
1162 begin
1163 SetLength(Result, Length(Result)+1);
1164 Result[High(Result)] := GetStr(Str);
1165 end;
1166 end;
1168 procedure g_Console_Add (L: AnsiString; show: Boolean=false);
1170 procedure conmsg (s: AnsiString);
1171 var
1172 a: Integer;
1173 begin
1174 if length(s) = 0 then exit;
1175 for a := 0 to High(MsgArray) do
1176 begin
1177 with MsgArray[a] do
1178 begin
1179 if Time = 0 then
1180 begin
1181 Msg := s;
1182 Time := MsgTime;
1183 exit;
1184 end;
1185 end;
1186 end;
1187 for a := 0 to High(MsgArray)-1 do MsgArray[a] := MsgArray[a+1];
1188 with MsgArray[High(MsgArray)] do
1189 begin
1190 Msg := L;
1191 Time := MsgTime;
1192 end;
1193 end;
1195 var
1196 f: Integer;
1197 begin
1198 // put it to console
1199 cbufPut(L);
1200 if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
1202 // now show 'em out of console too
1203 show := show and gAllowConsoleMessages;
1204 if show and gShowMessages then
1205 begin
1206 // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
1207 while length(L) > 0 do
1208 begin
1209 f := Pos(#10, L);
1210 if f <= 0 then f := length(L)+1;
1211 conmsg(Copy(L, 1, f-1));
1212 Delete(L, 1, f);
1213 end;
1214 end;
1216 //SetLength(ConsoleHistory, Length(ConsoleHistory)+1);
1217 //ConsoleHistory[High(ConsoleHistory)] := L;
1219 (*
1220 {$IFDEF HEADLESS}
1221 e_WriteLog('CON: ' + L, MSG_NOTIFY);
1222 {$ENDIF}
1223 *)
1224 end;
1227 var
1228 consolewriterLastWasEOL: Boolean = false;
1230 procedure consolewriter (constref buf; len: SizeUInt);
1231 var
1232 b: PByte;
1233 begin
1234 if (len < 1) then exit;
1235 b := PByte(@buf);
1236 consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
1237 while (len > 0) do
1238 begin
1239 if (b[0] <> 13) and (b[0] <> 10) then
1240 begin
1241 cbufPut(AnsiChar(b[0]));
1242 end
1243 else
1244 begin
1245 if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
1246 cbufPut(#10);
1247 end;
1248 len -= 1;
1249 b += 1;
1250 end;
1251 end;
1254 // returns formatted string if `writerCB` is `nil`, empty string otherwise
1255 //function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1256 //TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
1257 procedure conwriteln (const s: AnsiString; show: Boolean=false);
1258 begin
1259 g_Console_Add(s, show);
1260 end;
1263 procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
1264 begin
1265 if show then
1266 begin
1267 g_Console_Add(formatstrf(s, args), true);
1268 end
1269 else
1270 begin
1271 consolewriterLastWasEOL := false;
1272 formatstrf(s, args, consolewriter);
1273 if not consolewriterLastWasEOL then cbufPut(#10);
1274 end;
1275 end;
1278 procedure g_Console_Clear();
1279 begin
1280 //ConsoleHistory := nil;
1281 cbufClear();
1282 conSkipLines := 0;
1283 end;
1285 procedure AddToHistory(L: AnsiString);
1286 var
1287 len: Integer;
1288 begin
1289 len := Length(CommandHistory);
1291 if (len = 0) or
1292 (LowerCase(CommandHistory[len-1]) <> LowerCase(L)) then
1293 begin
1294 SetLength(CommandHistory, len+1);
1295 CommandHistory[len] := L;
1296 end;
1298 CmdIndex := Length(CommandHistory);
1299 end;
1301 function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
1302 var
1303 Arr: SArray;
1304 i: Integer;
1305 begin
1306 Result := True;
1308 Arr := nil;
1310 if Trim(C) = '' then
1311 Exit;
1313 Arr := ParseString(C);
1314 if Arr = nil then
1315 Exit;
1317 for i := 0 to High(Whitelist) do
1318 if Whitelist[i] = LowerCase(Arr[0]) then
1319 Result := False;
1320 end;
1322 procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
1323 var
1324 Arr: SArray;
1325 i: Integer;
1326 begin
1327 Arr := nil;
1329 if Trim(L) = '' then
1330 Exit;
1332 conSkipLines := 0; // "unscroll"
1334 if L = 'goobers' then
1335 begin
1336 Line := '';
1337 CPos := 1;
1338 gCheats := true;
1339 g_Console_Add('Your memory serves you well.');
1340 exit;
1341 end;
1343 if not quiet then
1344 begin
1345 g_Console_Add('> '+L);
1346 Line := '';
1347 CPos := 1;
1348 end;
1350 Arr := ParseString(L);
1351 if Arr = nil then
1352 Exit;
1354 if commands = nil then
1355 Exit;
1357 if not quiet then
1358 AddToHistory(L);
1360 for i := 0 to High(commands) do
1361 begin
1362 if commands[i].cmd = LowerCase(Arr[0]) then
1363 begin
1364 if assigned(commands[i].procEx) then
1365 begin
1366 commands[i].procEx(@commands[i], Arr);
1367 exit;
1368 end;
1369 if assigned(commands[i].proc) then
1370 begin
1371 commands[i].proc(Arr);
1372 exit;
1373 end;
1374 end;
1375 end;
1377 g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
1378 end;
1380 end.