DEADSOFTWARE

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