DEADSOFTWARE

render: completely remove opengl calls form game code
[d2df-sdl.git] / src / game / g_main.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_main;
18 interface
20 uses Utils;
22 procedure Main ();
23 procedure Init ();
24 procedure Release ();
25 procedure Update ();
26 procedure Draw ();
27 procedure KeyPress (K: Word);
28 procedure CharPress (C: AnsiChar);
30 var
31 {--- Read-only dirs ---}
32 GameWAD: string;
33 DataDirs: SSArray;
34 ModelDirs: SSArray;
35 MegawadDirs: SSArray;
36 MapDirs: SSArray;
37 WadDirs: SSArray;
38 AllMapDirs: SSArray; // Maps + Megawads
40 {--- Read-Write dirs ---}
41 LogFileName: string;
42 LogDirs: SSArray;
43 SaveDirs: SSArray;
44 CacheDirs: SSArray;
45 ConfigDirs: SSArray;
46 ScreenshotDirs: SSArray;
47 StatsDirs: SSArray;
48 MapDownloadDirs: SSArray;
49 WadDownloadDirs: SSArray;
51 GameWADName: string = 'GAME';
53 implementation
55 uses
56 {$IFDEF ENABLE_HOLMES}
57 g_holmes, sdlcarcass, fui_ctls, fui_wadread, fui_style, fui_gfx_gl,
58 {$ENDIF}
59 {$IFDEF LINUX}
60 BaseUnix,
61 {$ENDIF}
62 {$IFDEF DARWIN}
63 MacOSAll, CocoaAll,
64 {$ENDIF}
65 {$IFDEF USE_SDL2}
66 SDL2,
67 {$ENDIF}
68 wadreader, e_log, g_window,
69 r_graphics, e_input, g_game, g_console, g_gui,
70 e_sound, g_options, g_sound, g_player, g_basic,
71 g_weapons, SysUtils, g_triggers, MAPDEF, g_map, e_res,
72 g_menu, g_language, g_net, g_touch, g_system, g_res_downloader,
73 conbuf, envvars, r_game,
74 xparser;
77 var
78 charbuff: packed array [0..15] of AnsiChar;
79 binPath: AnsiString = '';
80 forceBinDir: Boolean;
82 function GetBinaryPath (): AnsiString;
83 {$IFDEF LINUX}
84 var
85 //cd: AnsiString;
86 sl: AnsiString;
87 {$ENDIF}
88 begin
89 result := ExtractFilePath(ParamStr(0));
90 {$IFDEF LINUX}
91 // it may be a symlink; do some guesswork here
92 sl := fpReadLink(ExtractFileName(ParamStr(0)));
93 if (sl = ParamStr(0)) then
94 begin
95 // use current directory, as we don't have anything better
96 //result := '.';
97 GetDir(0, result);
98 end;
99 {$ENDIF}
100 result := fixSlashes(result);
101 if (length(result) > 0) and (result[length(result)] <> '/') then result := result+'/';
102 end;
104 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
105 var dir: AnsiString;
106 begin
107 e_LogWriteln(msg + ':');
108 for dir in dirs do
109 e_LogWriteln(' ' + dir);
110 end;
112 {$IFDEF DARWIN}
113 function NSStringToAnsiString (s: NSString): AnsiString;
114 var i: Integer;
115 begin
116 result := '';
117 for i := 0 to s.length - 1 do
118 result := result + AnsiChar(s.characterAtIndex(i));
119 end;
121 function GetBundlePath (): AnsiString;
122 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
123 begin
124 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
125 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
126 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
127 CFRelease(pathRef);
128 CFRelease(pathCFStr);
129 Result := pathStr;
130 end;
131 {$ENDIF}
133 procedure InitPath;
134 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
136 procedure AddDir (var dirs: SSArray; append: AnsiString);
137 begin
138 SetLength(dirs, Length(dirs) + 1);
139 dirs[High(dirs)] := ExpandFileName(append)
140 end;
142 function IsSep (ch: Char): Boolean;
143 begin
144 {$IFDEF WINDOWS}
145 result := (ch = '/') or (ch = '\');
146 {$ELSE}
147 result := (ch = '/');
148 {$ENDIF}
149 end;
151 function OptimizePath (dir: AnsiString): AnsiString;
152 var i, len: Integer; s: AnsiString;
153 begin
154 i := 1; len := Length(dir); s := '';
155 while i <= len do
156 begin
157 if IsSep(dir[i]) then
158 begin
159 s := s + DirectorySeparator;
160 Inc(i);
161 while (i <= len) and IsSep(dir[i]) do Inc(i);
162 if (i <= len) and (dir[i] = '.') then
163 begin
164 if (i = len) or IsSep(dir[i + 1]) then
165 begin
166 Inc(i)
167 end
168 else if (i + 1 <= len) and (dir[i + 1] = '.') then
169 begin
170 if (i + 1 = len) or IsSep(dir[i + 2]) then
171 begin
172 s := e_UpperDir(s);
173 Inc(i, 2)
174 end
175 end
176 end
177 end
178 else
179 begin
180 s := s + dir[i];
181 Inc(i)
182 end
183 end;
184 result := s
185 end;
187 procedure OptimizeDirs (var dirs: SSArray);
188 var i, j, k: Integer;
189 begin
190 for i := 0 to High(dirs) do
191 dirs[i] := OptimizePath(dirs[i]);
192 // deduplicate
193 i := High(dirs);
194 while i >= 0 do
195 begin
196 j := 0;
197 while j < i do
198 begin
199 if dirs[j] = dirs[i] then
200 begin
201 for k := j + 1 to High(dirs) do
202 dirs[k - 1] := dirs[k];
203 Dec(i);
204 SetLength(dirs, High(dirs))
205 end
206 else
207 begin
208 Inc(j)
209 end
210 end;
211 Dec(i)
212 end
213 end;
215 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
216 var s: AnsiString;
217 begin
218 if Length(dirs) = 0 then
219 for s in base do
220 AddDir(dirs, e_CatPath(s, append));
221 OptimizeDirs(dirs)
222 end;
224 function GetDefaultRODirs (): SSArray;
225 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
226 var home: AnsiString;
227 {$ENDIF}
228 {$IFDEF WINDOWS}
229 var appdata: AnsiString;
230 {$ENDIF}
231 {$IFDEF DARWIN}
232 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
233 {$ENDIF}
234 begin
235 result := nil;
236 {$IFDEF DARWIN}
237 bundle := GetBundlePath();
238 if ExtractFileExt(bundle) <> '.app' then
239 AddDir(result, binpath);
240 {$ELSE}
241 AddDir(result, binPath);
242 {$ENDIF}
243 if forceBinDir = false then
244 begin
245 {$IFDEF USE_SDL2}
246 AddDir(result, SDL_GetBasePath());
247 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
248 {$ENDIF}
249 {$IFDEF WINDOWS}
250 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
251 if appdata <> '' then
252 AddDir(result, appdata);
253 {$ENDIF}
254 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
255 AddDir(result, '/usr/share/doom2df');
256 AddDir(result, '/usr/local/share/doom2df');
257 home := GetEnvironmentVariable('HOME');
258 if home <> '' then
259 AddDir(result, e_CatPath(home, '.doom2df'));
260 {$ENDIF}
261 {$IFDEF DARWIN}
262 bundle := GetBundlePath();
263 if bundle <> '' then
264 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
265 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
266 for i := 0 to dirArr.count - 1 do
267 begin
268 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
269 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
270 end;
271 {$ENDIF}
272 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
273 AddDir(result, SDL_AndroidGetInternalStoragePath());
274 if SDL_AndroidGetExternalStorageState() <> 0 then
275 AddDir(result, SDL_AndroidGetExternalStoragePath());
276 {$ENDIF}
277 end
278 end;
280 function GetDefaultRWDirs (): SSArray;
281 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
282 var home: AnsiString;
283 {$ENDIF}
284 {$IFDEF WINDOWS}
285 var appdata: AnsiString;
286 {$ENDIF}
287 {$IFDEF DARWIN}
288 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
289 {$ENDIF}
290 begin
291 result := nil;
292 {$IFDEF DARWIN}
293 bundle := GetBundlePath();
294 if ExtractFileExt(bundle) <> '.app' then
295 AddDir(result, binPath);
296 {$ELSE}
297 AddDir(result, binPath);
298 {$ENDIF}
299 if forceBinDir = false then
300 begin
301 {$IFDEF USE_SDL2}
302 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
303 {$ENDIF}
304 {$IFDEF WINDOWS}
305 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
306 if appdata <> '' then
307 AddDir(result, appdata);
308 {$ENDIF}
309 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
310 home := GetEnvironmentVariable('HOME');
311 if home <> '' then
312 AddDir(result, e_CatPath(home, '.doom2df'));
313 {$ENDIF}
314 {$IFDEF DARWIN}
315 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
316 for i := 0 to dirArr.count - 1 do
317 begin
318 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
319 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
320 end;
321 {$ENDIF}
322 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
323 if SDL_AndroidGetExternalStorageState() <> 0 then
324 AddDir(result, SDL_AndroidGetExternalStoragePath());
325 {$ENDIF}
326 end
327 end;
329 begin
330 forceBinDir := false;
331 binPath := GetBinaryPath();
333 i := 1;
334 while i < ParamCount do
335 begin
336 case ParamStr(i) of
337 '--like-windoze': forceBinDir := true;
338 '--rw-dir':
339 begin
340 Inc(i);
341 rwdir := ParamStr(i);
342 (* RW *)
343 AddDir(LogDirs, e_CatPath(rwdir, ''));
344 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
345 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
346 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
347 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
348 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
349 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
350 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
351 (* RO *)
352 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
353 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
354 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
355 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
356 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
357 end;
358 '--ro-dir':
359 begin
360 Inc(i);
361 rodir := ParamStr(i);
362 (* RO *)
363 AddDir(DataDirs, e_CatPath(rodir, 'data'));
364 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
365 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
366 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
367 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
368 end;
369 '--game-wad':
370 begin
371 Inc(i);
372 GameWADName := ParamStr(i);
373 end;
374 '--config':
375 begin
376 Inc(i);
377 gConfigScript := ParamStr(i);
378 end;
379 end;
380 Inc(i)
381 end;
383 // prefer bin dir if it writable and contains game.wad
384 if forceBinDir = false then
385 begin
386 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
387 if e_CanCreateFilesAt(binPath) then
388 forceBinDir := true
389 end;
391 (* RO *)
392 rodirs := GetDefaultRODirs();
393 AddDef(DataDirs, rodirs, 'data');
394 AddDef(ModelDirs, rodirs, 'data/models');
395 AddDef(MegawadDirs, rodirs, 'maps/megawads');
396 AddDef(MapDirs, rodirs, 'maps');
397 AddDef(WadDirs, rodirs, 'wads');
399 (* RW *)
400 rwdirs := GetDefaultRWDirs();
401 AddDef(LogDirs, rwdirs, '');
402 AddDef(SaveDirs, rwdirs, 'data');
403 AddDef(CacheDirs, rwdirs, 'data/cache');
404 AddDef(ConfigDirs, rwdirs, '');
405 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
406 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
407 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
408 AddDef(StatsDirs, rwdirs, 'stats');
410 for i := 0 to High(MapDirs) do
411 AddDir(AllMapDirs, MapDirs[i]);
412 for i := 0 to High(MegawadDirs) do
413 AddDir(AllMapDirs, MegawadDirs[i]);
414 OptimizeDirs(AllMapDirs);
416 if LogFileName = '' then
417 begin
418 rwdir := e_GetWriteableDir(LogDirs, false);
419 if rwdir <> '' then
420 begin
421 {$IFDEF HEADLESS}
422 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
423 {$ELSE}
424 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
425 {$ENDIF}
426 end
427 end;
429 // HACK: ensure the screenshots folder also has a stats subfolder in it
430 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
431 if rwdir <> '' then CreateDir(rwdir + '/stats');
432 end;
434 procedure InitPrep;
435 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
436 var timiditycfg: AnsiString;
437 {$ENDIF}
438 var i: Integer;
439 begin
440 {$IFDEF HEADLESS}
441 conbufDumpToStdOut := true;
442 {$ENDIF}
443 for i := 1 to ParamCount do
444 begin
445 case ParamStr(i) of
446 '--con-stdout': conbufDumpToStdOut := true;
447 '--no-fbo': glRenderToFBO := false;
448 end
449 end;
451 if LogFileName <> '' then
452 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
453 e_InitWritelnDriver();
454 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
455 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
456 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
457 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
459 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
460 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
462 PrintDirs('DataDirs', DataDirs);
463 PrintDirs('ModelDirs', ModelDirs);
464 PrintDirs('MegawadDirs', MegawadDirs);
465 PrintDirs('MapDirs', MapDirs);
466 PrintDirs('WadDirs', WadDirs);
468 PrintDirs('LogDirs', LogDirs);
469 PrintDirs('SaveDirs', SaveDirs);
470 PrintDirs('CacheDirs', CacheDirs);
471 PrintDirs('ConfigDirs', ConfigDirs);
472 PrintDirs('ScreenshotDirs', ScreenshotDirs);
473 PrintDirs('StatsDirs', StatsDirs);
474 PrintDirs('MapDownloadDirs', MapDownloadDirs);
475 PrintDirs('WadDownloadDirs', WadDownloadDirs);
477 GameWAD := e_FindWad(DataDirs, GameWADName);
478 if GameWad = '' then
479 begin
480 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
481 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
482 if forceBinDir = false then
483 SDL_ShowSimpleMessageBox(
484 SDL_MESSAGEBOX_ERROR,
485 'Doom 2D Forever',
486 PChar('WAD ' + GameWADName + ' not found in data directories.'),
487 nil
488 );
489 {$ENDIF}
490 e_DeinitLog;
491 Halt(1);
492 end;
494 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
495 timiditycfg := 'timidity.cfg';
496 if e_FindResource(ConfigDirs, timiditycfg) = true then
497 begin
498 timiditycfg := ExpandFileName(timiditycfg);
499 SetEnvVar('TIMIDITY_CFG', timiditycfg);
500 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
501 end;
502 {$ENDIF}
503 end;
505 procedure Main();
506 {$IFDEF ENABLE_HOLMES}
507 var flexloaded: Boolean;
508 {$ENDIF}
509 begin
510 InitPath;
511 InitPrep;
512 e_InitInput;
513 sys_Init;
515 g_Options_SetDefault;
516 g_Options_SetDefaultVideo;
517 g_Console_SysInit;
518 if sys_SetDisplayMode(gRC_Width, gRC_Height, gBPP, gRC_FullScreen, gRC_Maximized) = False then
519 raise Exception.Create('Failed to set videomode on startup.');
521 e_WriteLog(gLanguage, TMsgType.Notify);
522 g_Language_Set(gLanguage);
524 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
525 flexloaded := true;
526 if not fuiAddWad('flexui.wad') then
527 begin
528 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
529 end;
530 try
531 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
532 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
533 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
534 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
535 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
536 except on e: Exception do
537 begin
538 writeln('ERROR loading FlexUI fonts');
539 flexloaded := false;
540 //raise;
541 end;
542 else
543 begin
544 flexloaded := false;
545 //raise;
546 end;
547 end;
548 if (flexloaded) then
549 begin
550 try
551 e_LogWriteln('FlexUI: loading stylesheet...');
552 uiLoadStyles('flexui/widgets.wgs');
553 except on e: TParserException do
554 begin
555 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
556 //raise;
557 flexloaded := false;
558 end;
559 else
560 begin
561 //raise;
562 flexloaded := false;
563 end;
564 end;
565 end;
566 g_holmes_imfunctional := not flexloaded;
568 if (not g_holmes_imfunctional) then
569 begin
570 uiInitialize();
571 uiContext.font := 'win14';
572 end;
574 if assigned(oglInitCB) then oglInitCB;
575 {$ENDIF}
577 //g_Res_CreateDatabases(true); // it will be done before connecting to the server for the first time
579 e_WriteLog('Entering SDLMain', TMsgType.Notify);
581 {$WARNINGS OFF}
582 SDLMain();
583 {$WARNINGS ON}
585 {$IFDEF ENABLE_HOLMES}
586 if assigned(oglDeinitCB) then oglDeinitCB;
587 {$ENDIF}
589 g_Console_WriteGameConfig;
590 sys_Final;
591 end;
593 procedure Init();
594 var
595 NoSound: Boolean;
596 begin
597 Randomize;
599 {$IFDEF HEADLESS}
600 {$IFDEF USE_SDLMIXER}
601 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
602 {$ELSE}
603 NoSound := True; // FMOD backend will sort it out
604 {$ENDIF}
605 {$ELSE}
606 NoSound := False;
607 {$ENDIF}
609 g_Touch_Init;
611 (*
612 if (e_JoysticksAvailable > 0) then
613 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
614 else
615 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
616 *)
618 if (not gNoSound) then
619 begin
620 e_WriteLog('Initializing sound system', TMsgType.Notify);
621 e_InitSoundSystem(NoSound);
622 end;
624 e_WriteLog('Init game', TMsgType.Notify);
625 g_Game_Init();
627 FillChar(charbuff, sizeof(charbuff), ' ');
628 end;
631 procedure Release();
632 begin
633 e_WriteLog('Releasing engine', TMsgType.Notify);
634 e_ReleaseEngine();
636 e_WriteLog('Releasing input', TMsgType.Notify);
637 e_ReleaseInput();
639 if not gNoSound then
640 begin
641 e_WriteLog('Releasing sound', TMsgType.Notify);
642 e_ReleaseSoundSystem();
643 end;
644 end;
647 procedure Update ();
648 begin
649 // remember old mobj positions, prepare for update
650 g_Game_PreUpdate();
651 // server: receive client commands for new frame
652 // client: receive game state changes from server
653 if (NetMode = NET_SERVER) then g_Net_Host_Update()
654 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
655 // think
656 g_Game_Update();
657 // server: send any accumulated outgoing data to clients
658 if NetMode = NET_SERVER then g_Net_Flush();
659 end;
662 procedure Draw ();
663 begin
664 r_Game_Draw();
665 end;
668 function Translit (const S: AnsiString): AnsiString;
669 var
670 i: Integer;
671 begin
672 Result := S;
673 for i := 1 to Length(Result) do
674 begin
675 case Result[i] of
676 'É': Result[i] := 'Q';
677 'Ö': Result[i] := 'W';
678 'Ó': Result[i] := 'E';
679 'Ê': Result[i] := 'R';
680 'Å': Result[i] := 'T';
681 'Í': Result[i] := 'Y';
682 'Ã': Result[i] := 'U';
683 'Ø': Result[i] := 'I';
684 'Ù': Result[i] := 'O';
685 'Ç': Result[i] := 'P';
686 'Õ': Result[i] := '['; //Chr(219);
687 'Ú': Result[i] := ']'; //Chr(221);
688 'Ô': Result[i] := 'A';
689 'Û': Result[i] := 'S';
690 'Â': Result[i] := 'D';
691 'À': Result[i] := 'F';
692 'Ï': Result[i] := 'G';
693 'Ð': Result[i] := 'H';
694 'Î': Result[i] := 'J';
695 'Ë': Result[i] := 'K';
696 'Ä': Result[i] := 'L';
697 'Æ': Result[i] := ';'; //Chr(186);
698 'Ý': Result[i] := #39; //Chr(222);
699 'ß': Result[i] := 'Z';
700 '×': Result[i] := 'X';
701 'Ñ': Result[i] := 'C';
702 'Ì': Result[i] := 'V';
703 'È': Result[i] := 'B';
704 'Ò': Result[i] := 'N';
705 'Ü': Result[i] := 'M';
706 'Á': Result[i] := ','; //Chr(188);
707 'Þ': Result[i] := '.'; //Chr(190);
708 end;
709 end;
710 end;
713 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
714 var
715 ls1, ls2: string;
716 begin
717 ls1 := CheatEng[ct];
718 ls2 := Translit(CheatRus[ct]);
719 if length(ls1) = 0 then ls1 := '~';
720 if length(ls2) = 0 then ls2 := '~';
721 result :=
722 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
723 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
724 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
725 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
727 if ct = I_GAME_CHEAT_JETPACK then
728 begin
729 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
730 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
731 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
732 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
733 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
734 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
735 end;
737 end;
740 procedure Cheat ();
741 const
742 CHEAT_DAMAGE = 500;
743 label
744 Cheated;
745 var
746 s, s2: string;
747 c: ShortString;
748 a: Integer;
749 begin
751 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
752 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
753 or g_Game_IsNet then Exit;
755 if not gGameOn then exit;
756 if not conIsCheatsEnabled then exit;
758 s := 'SOUND_GAME_RADIO';
760 //
761 if CheckCheat(I_GAME_CHEAT_GODMODE) then
762 begin
763 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
764 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
765 goto Cheated;
766 end;
767 // RAMBO
768 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
769 begin
770 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
771 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
772 goto Cheated;
773 end;
774 // TANK
775 if CheckCheat(I_GAME_CHEAT_HEALTH) then
776 begin
777 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
778 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
779 goto Cheated;
780 end;
781 // IDDQD
782 if CheckCheat(I_GAME_CHEAT_DEATH) then
783 begin
784 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
785 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
786 s := 'SOUND_MONSTER_HAHA';
787 goto Cheated;
788 end;
789 //
790 if CheckCheat(I_GAME_CHEAT_DOORS) then
791 begin
792 g_Triggers_OpenAll();
793 goto Cheated;
794 end;
795 // GOODBYE
796 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
797 begin
798 if gTriggers <> nil then
799 for a := 0 to High(gTriggers) do
800 if gTriggers[a].TriggerType = TRIGGER_EXIT then
801 begin
802 gExitByTrigger := True;
803 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
804 g_Game_ExitLevel(gTriggers[a].tgcMap);
805 Break;
806 end;
807 goto Cheated;
808 end;
809 //
810 s2 := Copy(charbuff, 15, 2);
811 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
812 begin
813 if g_Map_Exist(gGameSettings.WAD + ':\MAP' + s2) then
814 begin
815 c := 'MAP' + s2;
816 g_Game_ExitLevel(c);
817 end;
818 goto Cheated;
819 end;
820 //
821 if CheckCheat(I_GAME_CHEAT_FLY) then
822 begin
823 gFly := not gFly;
824 goto Cheated;
825 end;
826 // BULLFROG
827 if CheckCheat(I_GAME_CHEAT_JUMPS) then
828 begin
829 VEL_JUMP := 30-VEL_JUMP;
830 goto Cheated;
831 end;
832 // FORMULA1
833 if CheckCheat(I_GAME_CHEAT_SPEED) then
834 begin
835 MAX_RUNVEL := 32-MAX_RUNVEL;
836 goto Cheated;
837 end;
838 // CONDOM
839 if CheckCheat(I_GAME_CHEAT_SUIT) then
840 begin
841 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
842 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
843 goto Cheated;
844 end;
845 //
846 if CheckCheat(I_GAME_CHEAT_AIR) then
847 begin
848 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
849 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
850 goto Cheated;
851 end;
852 // PURELOVE
853 if CheckCheat(I_GAME_CHEAT_BERSERK) then
854 begin
855 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
856 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
857 goto Cheated;
858 end;
859 //
860 if CheckCheat(I_GAME_CHEAT_JETPACK) then
861 begin
862 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
863 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
864 goto Cheated;
865 end;
866 // CASPER
867 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
868 begin
869 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
870 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
871 goto Cheated;
872 end;
873 //
874 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
875 begin
876 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
877 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
878 goto Cheated;
879 end;
880 // INFERNO
881 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
882 begin
883 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
884 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
885 goto Cheated;
886 end;
887 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
888 begin
889 gAimLine := not gAimLine;
890 goto Cheated;
891 end;
892 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
893 begin
894 gShowMap := not gShowMap;
895 goto Cheated;
896 end;
897 Exit;
899 Cheated:
900 g_Sound_PlayEx(s);
901 end;
904 procedure KeyPress (K: Word);
905 {$IFNDEF HEADLESS}
906 var
907 Msg: g_gui.TMessage;
908 {$ENDIF}
909 begin
910 {$IFNDEF HEADLESS}
911 case K of
912 VK_ESCAPE: // <Esc>:
913 begin
914 if (g_ActiveWindow <> nil) then
915 begin
916 Msg.Msg := WM_KEYDOWN;
917 Msg.WParam := VK_ESCAPE;
918 g_ActiveWindow.OnMessage(Msg);
919 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
920 end
921 else if (gState <> STATE_FOLD) then
922 begin
923 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
924 begin
925 g_Game_InGameMenu(True);
926 end
927 else if (gExit = 0) and (gState <> STATE_SLIST) then
928 begin
929 if (gState <> STATE_MENU) then
930 begin
931 if (NetMode <> NET_NONE) then
932 begin
933 g_Game_StopAllSounds(True);
934 g_Game_Free;
935 gState := STATE_MENU;
936 Exit;
937 end;
938 end;
939 g_GUI_ShowWindow('MainMenu');
940 g_Sound_PlayEx('MENU_OPEN');
941 end;
942 end;
943 end;
945 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
946 begin // <F2> .. <F6> � <F12>
947 if gGameOn and (not gConsoleShow) and (not gChatShow) then
948 begin
949 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
950 if (not g_Game_IsNet) then g_Game_Pause(True);
951 case K of
952 IK_F2: g_Menu_Show_SaveMenu();
953 IK_F3: g_Menu_Show_LoadMenu();
954 IK_F4: g_Menu_Show_GameSetGame();
955 IK_F5: g_Menu_Show_OptionsVideo();
956 IK_F6: g_Menu_Show_OptionsSound();
957 IK_F7: g_Menu_Show_EndGameMenu();
958 IK_F10: g_Menu_Show_QuitGameMenu();
959 end;
960 end;
961 end;
963 else
964 begin
965 gJustChatted := False;
966 if gConsoleShow or gChatShow then
967 begin
968 g_Console_Control(K);
969 end
970 else if (g_ActiveWindow <> nil) then
971 begin
972 Msg.Msg := WM_KEYDOWN;
973 Msg.WParam := K;
974 g_ActiveWindow.OnMessage(Msg);
975 end
976 else if (gState = STATE_MENU) then
977 begin
978 g_GUI_ShowWindow('MainMenu');
979 g_Sound_PlayEx('MENU_OPEN');
980 end;
981 end;
982 end;
983 {$ENDIF}
984 end;
987 procedure CharPress (C: AnsiChar);
988 var
989 Msg: g_gui.TMessage;
990 a: Integer;
991 begin
992 if gConsoleShow or gChatShow then
993 begin
994 g_Console_Char(C)
995 end
996 else if (g_ActiveWindow <> nil) then
997 begin
998 Msg.Msg := WM_CHAR;
999 Msg.WParam := Ord(C);
1000 g_ActiveWindow.OnMessage(Msg);
1001 end
1002 else
1003 begin
1004 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
1005 charbuff[15] := upcase1251(C);
1006 Cheat();
1007 end;
1008 end;
1010 end.