DEADSOFTWARE

default to using FBO; can be disabled via command line
[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 {$INCLUDE ../nogl/noGLuses.inc}
57 {$IFDEF ENABLE_HOLMES}
58 g_holmes, sdlcarcass, fui_ctls, fui_wadread, fui_style, fui_gfx_gl,
59 {$ENDIF}
60 {$IFDEF LINUX}
61 BaseUnix,
62 {$ENDIF}
63 {$IFDEF DARWIN}
64 MacOSAll, CocoaAll,
65 {$ENDIF}
66 {$IFDEF USE_SDL2}
67 SDL2,
68 {$ENDIF}
69 wadreader, e_log, g_window,
70 e_graphics, e_input, g_game, g_console, g_gui,
71 e_sound, g_options, g_sound, g_player, g_basic,
72 g_weapons, SysUtils, g_triggers, MAPDEF, g_map, e_res,
73 g_menu, g_language, g_net, g_touch, g_system, g_res_downloader,
74 conbuf, envvars,
75 xparser;
78 var
79 charbuff: packed array [0..15] of AnsiChar;
80 binPath: AnsiString = '';
81 forceBinDir: Boolean;
83 function GetBinaryPath (): AnsiString;
84 {$IFDEF LINUX}
85 var
86 //cd: AnsiString;
87 sl: AnsiString;
88 {$ENDIF}
89 begin
90 result := ExtractFilePath(ParamStr(0));
91 {$IFDEF LINUX}
92 // it may be a symlink; do some guesswork here
93 sl := fpReadLink(ExtractFileName(ParamStr(0)));
94 if (sl = ParamStr(0)) then
95 begin
96 // use current directory, as we don't have anything better
97 //result := '.';
98 GetDir(0, result);
99 end;
100 {$ENDIF}
101 result := fixSlashes(result);
102 if (length(result) > 0) and (result[length(result)] <> '/') then result := result+'/';
103 end;
105 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
106 var dir: AnsiString;
107 begin
108 e_LogWriteln(msg + ':');
109 for dir in dirs do
110 e_LogWriteln(' ' + dir);
111 end;
113 {$IFDEF DARWIN}
114 function NSStringToAnsiString (s: NSString): AnsiString;
115 var i: Integer;
116 begin
117 result := '';
118 for i := 0 to s.length - 1 do
119 result := result + AnsiChar(s.characterAtIndex(i));
120 end;
122 function GetBundlePath (): AnsiString;
123 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
124 begin
125 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
126 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
127 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
128 CFRelease(pathRef);
129 CFRelease(pathCFStr);
130 Result := pathStr;
131 end;
132 {$ENDIF}
134 procedure InitPath;
135 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
137 procedure AddDir (var dirs: SSArray; append: AnsiString);
138 begin
139 SetLength(dirs, Length(dirs) + 1);
140 dirs[High(dirs)] := ExpandFileName(append)
141 end;
143 function IsSep (ch: Char): Boolean;
144 begin
145 {$IFDEF WINDOWS}
146 result := (ch = '/') or (ch = '\');
147 {$ELSE}
148 result := (ch = '/');
149 {$ENDIF}
150 end;
152 function OptimizePath (dir: AnsiString): AnsiString;
153 var i, len: Integer; s: AnsiString;
154 begin
155 i := 1; len := Length(dir); s := '';
156 while i <= len do
157 begin
158 if IsSep(dir[i]) then
159 begin
160 s := s + DirectorySeparator;
161 Inc(i);
162 while (i <= len) and IsSep(dir[i]) do Inc(i);
163 if (i <= len) and (dir[i] = '.') then
164 begin
165 if (i = len) or IsSep(dir[i + 1]) then
166 begin
167 Inc(i)
168 end
169 else if (i + 1 <= len) and (dir[i + 1] = '.') then
170 begin
171 if (i + 1 = len) or IsSep(dir[i + 2]) then
172 begin
173 s := e_UpperDir(s);
174 Inc(i, 2)
175 end
176 end
177 end
178 end
179 else
180 begin
181 s := s + dir[i];
182 Inc(i)
183 end
184 end;
185 result := s
186 end;
188 procedure OptimizeDirs (var dirs: SSArray);
189 var i, j, k: Integer;
190 begin
191 for i := 0 to High(dirs) do
192 dirs[i] := OptimizePath(dirs[i]);
193 // deduplicate
194 i := High(dirs);
195 while i >= 0 do
196 begin
197 j := 0;
198 while j < i do
199 begin
200 if dirs[j] = dirs[i] then
201 begin
202 for k := j + 1 to High(dirs) do
203 dirs[k - 1] := dirs[k];
204 Dec(i);
205 SetLength(dirs, High(dirs))
206 end
207 else
208 begin
209 Inc(j)
210 end
211 end;
212 Dec(i)
213 end
214 end;
216 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
217 var s: AnsiString;
218 begin
219 if Length(dirs) = 0 then
220 for s in base do
221 AddDir(dirs, e_CatPath(s, append));
222 OptimizeDirs(dirs)
223 end;
225 function GetDefaultRODirs (): SSArray;
226 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
227 var home: AnsiString;
228 {$ENDIF}
229 {$IFDEF WINDOWS}
230 var appdata: AnsiString;
231 {$ENDIF}
232 {$IFDEF DARWIN}
233 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
234 {$ENDIF}
235 begin
236 result := nil;
237 {$IFDEF DARWIN}
238 bundle := GetBundlePath();
239 if ExtractFileExt(bundle) <> '.app' then
240 AddDir(result, binpath);
241 {$ELSE}
242 AddDir(result, binPath);
243 {$ENDIF}
244 if forceBinDir = false then
245 begin
246 {$IFDEF USE_SDL2}
247 AddDir(result, SDL_GetBasePath());
248 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
249 {$ENDIF}
250 {$IFDEF WINDOWS}
251 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
252 if appdata <> '' then
253 AddDir(result, appdata);
254 {$ENDIF}
255 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
256 AddDir(result, '/usr/share/doom2df');
257 AddDir(result, '/usr/local/share/doom2df');
258 home := GetEnvironmentVariable('HOME');
259 if home <> '' then
260 AddDir(result, e_CatPath(home, '.doom2df'));
261 {$ENDIF}
262 {$IFDEF DARWIN}
263 bundle := GetBundlePath();
264 if bundle <> '' then
265 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
266 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
267 for i := 0 to dirArr.count - 1 do
268 begin
269 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
270 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
271 end;
272 {$ENDIF}
273 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
274 AddDir(result, SDL_AndroidGetInternalStoragePath());
275 if SDL_AndroidGetExternalStorageState() <> 0 then
276 AddDir(result, SDL_AndroidGetExternalStoragePath());
277 {$ENDIF}
278 end
279 end;
281 function GetDefaultRWDirs (): SSArray;
282 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
283 var home: AnsiString;
284 {$ENDIF}
285 {$IFDEF WINDOWS}
286 var appdata: AnsiString;
287 {$ENDIF}
288 {$IFDEF DARWIN}
289 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
290 {$ENDIF}
291 begin
292 result := nil;
293 {$IFDEF DARWIN}
294 bundle := GetBundlePath();
295 if ExtractFileExt(bundle) <> '.app' then
296 AddDir(result, binPath);
297 {$ELSE}
298 AddDir(result, binPath);
299 {$ENDIF}
300 if forceBinDir = false then
301 begin
302 {$IFDEF USE_SDL2}
303 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
304 {$ENDIF}
305 {$IFDEF WINDOWS}
306 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
307 if appdata <> '' then
308 AddDir(result, appdata);
309 {$ENDIF}
310 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
311 home := GetEnvironmentVariable('HOME');
312 if home <> '' then
313 AddDir(result, e_CatPath(home, '.doom2df'));
314 {$ENDIF}
315 {$IFDEF DARWIN}
316 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
317 for i := 0 to dirArr.count - 1 do
318 begin
319 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
320 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
321 end;
322 {$ENDIF}
323 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
324 if SDL_AndroidGetExternalStorageState() <> 0 then
325 AddDir(result, SDL_AndroidGetExternalStoragePath());
326 {$ENDIF}
327 end
328 end;
330 begin
331 forceBinDir := false;
332 binPath := GetBinaryPath();
334 i := 1;
335 while i < ParamCount do
336 begin
337 case ParamStr(i) of
338 '--like-windoze': forceBinDir := true;
339 '--rw-dir':
340 begin
341 Inc(i);
342 rwdir := ParamStr(i);
343 (* RW *)
344 AddDir(LogDirs, e_CatPath(rwdir, ''));
345 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
346 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
347 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
348 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
349 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
350 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
351 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
352 (* RO *)
353 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
354 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
355 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
356 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
357 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
358 end;
359 '--ro-dir':
360 begin
361 Inc(i);
362 rodir := ParamStr(i);
363 (* RO *)
364 AddDir(DataDirs, e_CatPath(rodir, 'data'));
365 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
366 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
367 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
368 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
369 end;
370 '--game-wad':
371 begin
372 Inc(i);
373 GameWADName := ParamStr(i);
374 end;
375 end;
376 Inc(i)
377 end;
379 // prefer bin dir if it writable and contains game.wad
380 if forceBinDir = false then
381 begin
382 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
383 if e_CanCreateFilesAt(binPath) then
384 forceBinDir := true
385 end;
387 (* RO *)
388 rodirs := GetDefaultRODirs();
389 AddDef(DataDirs, rodirs, 'data');
390 AddDef(ModelDirs, rodirs, 'data/models');
391 AddDef(MegawadDirs, rodirs, 'maps/megawads');
392 AddDef(MapDirs, rodirs, 'maps');
393 AddDef(WadDirs, rodirs, 'wads');
395 (* RW *)
396 rwdirs := GetDefaultRWDirs();
397 AddDef(LogDirs, rwdirs, '');
398 AddDef(SaveDirs, rwdirs, 'data');
399 AddDef(CacheDirs, rwdirs, 'data/cache');
400 AddDef(ConfigDirs, rwdirs, '');
401 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
402 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
403 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
404 AddDef(StatsDirs, rwdirs, 'stats');
406 for i := 0 to High(MapDirs) do
407 AddDir(AllMapDirs, MapDirs[i]);
408 for i := 0 to High(MegawadDirs) do
409 AddDir(AllMapDirs, MegawadDirs[i]);
410 OptimizeDirs(AllMapDirs);
412 if LogFileName = '' then
413 begin
414 rwdir := e_GetWriteableDir(LogDirs, false);
415 if rwdir <> '' then
416 begin
417 {$IFDEF HEADLESS}
418 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
419 {$ELSE}
420 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
421 {$ENDIF}
422 end
423 end;
425 // HACK: ensure the screenshots folder also has a stats subfolder in it
426 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
427 if rwdir <> '' then CreateDir(rwdir + '/stats');
428 end;
430 procedure InitPrep;
431 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
432 var timiditycfg: AnsiString;
433 {$ENDIF}
434 var i: Integer;
435 begin
436 {$IFDEF HEADLESS}
437 conbufDumpToStdOut := true;
438 {$ENDIF}
439 for i := 1 to ParamCount do
440 begin
441 case ParamStr(i) of
442 '--con-stdout': conbufDumpToStdOut := true;
443 '--no-fbo': glRenderToFBO := false;
444 end
445 end;
447 if LogFileName <> '' then
448 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
449 e_InitWritelnDriver();
450 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
451 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
452 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
453 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
455 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
456 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
458 PrintDirs('DataDirs', DataDirs);
459 PrintDirs('ModelDirs', ModelDirs);
460 PrintDirs('MegawadDirs', MegawadDirs);
461 PrintDirs('MapDirs', MapDirs);
462 PrintDirs('WadDirs', WadDirs);
464 PrintDirs('LogDirs', LogDirs);
465 PrintDirs('SaveDirs', SaveDirs);
466 PrintDirs('CacheDirs', CacheDirs);
467 PrintDirs('ConfigDirs', ConfigDirs);
468 PrintDirs('ScreenshotDirs', ScreenshotDirs);
469 PrintDirs('StatsDirs', StatsDirs);
470 PrintDirs('MapDownloadDirs', MapDownloadDirs);
471 PrintDirs('WadDownloadDirs', WadDownloadDirs);
473 GameWAD := e_FindWad(DataDirs, GameWADName);
474 if GameWad = '' then
475 begin
476 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
477 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
478 if forceBinDir = false then
479 SDL_ShowSimpleMessageBox(
480 SDL_MESSAGEBOX_ERROR,
481 'Doom 2D Forever',
482 PChar('WAD ' + GameWADName + ' not found in data directories.'),
483 nil
484 );
485 {$ENDIF}
486 e_DeinitLog;
487 Halt(1);
488 end;
490 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDLMIXER)}
491 timiditycfg := 'timidity.cfg';
492 if e_FindResource(ConfigDirs, timiditycfg) = true then
493 begin
494 timiditycfg := ExpandFileName(timiditycfg);
495 SetEnvVar('TIMIDITY_CFG', timiditycfg);
496 e_LogWritefln('Set TIMIDITY_CFG = "%s"', [timiditycfg]);
497 end;
498 {$ENDIF}
499 end;
501 procedure Main();
502 {$IFDEF ENABLE_HOLMES}
503 var flexloaded: Boolean;
504 {$ENDIF}
505 var s: AnsiString;
506 begin
507 InitPath;
508 InitPrep;
509 e_InitInput;
510 sys_Init;
512 g_Options_SetDefault;
513 g_Options_SetDefaultVideo;
514 s := CONFIG_FILENAME;
515 if e_FindResource(ConfigDirs, s) = true then
516 g_Options_Read(s);
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 g_Game_Update();
650 end;
653 procedure Draw ();
654 begin
655 g_Game_Draw();
656 end;
659 function Translit (const S: AnsiString): AnsiString;
660 var
661 i: Integer;
662 begin
663 Result := S;
664 for i := 1 to Length(Result) do
665 begin
666 case Result[i] of
667 'É': Result[i] := 'Q';
668 'Ö': Result[i] := 'W';
669 'Ó': Result[i] := 'E';
670 'Ê': Result[i] := 'R';
671 'Å': Result[i] := 'T';
672 'Í': Result[i] := 'Y';
673 'Ã': Result[i] := 'U';
674 'Ø': Result[i] := 'I';
675 'Ù': Result[i] := 'O';
676 'Ç': Result[i] := 'P';
677 'Õ': Result[i] := '['; //Chr(219);
678 'Ú': Result[i] := ']'; //Chr(221);
679 'Ô': Result[i] := 'A';
680 'Û': Result[i] := 'S';
681 'Â': Result[i] := 'D';
682 'À': Result[i] := 'F';
683 'Ï': Result[i] := 'G';
684 'Ð': Result[i] := 'H';
685 'Î': Result[i] := 'J';
686 'Ë': Result[i] := 'K';
687 'Ä': Result[i] := 'L';
688 'Æ': Result[i] := ';'; //Chr(186);
689 'Ý': Result[i] := #39; //Chr(222);
690 'ß': Result[i] := 'Z';
691 '×': Result[i] := 'X';
692 'Ñ': Result[i] := 'C';
693 'Ì': Result[i] := 'V';
694 'È': Result[i] := 'B';
695 'Ò': Result[i] := 'N';
696 'Ü': Result[i] := 'M';
697 'Á': Result[i] := ','; //Chr(188);
698 'Þ': Result[i] := '.'; //Chr(190);
699 end;
700 end;
701 end;
704 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
705 var
706 ls1, ls2: string;
707 begin
708 ls1 := CheatEng[ct];
709 ls2 := Translit(CheatRus[ct]);
710 if length(ls1) = 0 then ls1 := '~';
711 if length(ls2) = 0 then ls2 := '~';
712 result :=
713 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
714 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
715 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
716 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
718 if ct = I_GAME_CHEAT_JETPACK then
719 begin
720 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
721 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
722 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
723 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
724 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
725 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
726 end;
728 end;
731 procedure Cheat ();
732 const
733 CHEAT_DAMAGE = 500;
734 label
735 Cheated;
736 var
737 s, s2: string;
738 c: ShortString;
739 a: Integer;
740 begin
742 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
743 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
744 or g_Game_IsNet then Exit;
746 if not gGameOn then exit;
747 if not conIsCheatsEnabled then exit;
749 s := 'SOUND_GAME_RADIO';
751 //
752 if CheckCheat(I_GAME_CHEAT_GODMODE) then
753 begin
754 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
755 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
756 goto Cheated;
757 end;
758 // RAMBO
759 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
760 begin
761 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
762 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
763 goto Cheated;
764 end;
765 // TANK
766 if CheckCheat(I_GAME_CHEAT_HEALTH) then
767 begin
768 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
769 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
770 goto Cheated;
771 end;
772 // IDDQD
773 if CheckCheat(I_GAME_CHEAT_DEATH) then
774 begin
775 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
776 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
777 s := 'SOUND_MONSTER_HAHA';
778 goto Cheated;
779 end;
780 //
781 if CheckCheat(I_GAME_CHEAT_DOORS) then
782 begin
783 g_Triggers_OpenAll();
784 goto Cheated;
785 end;
786 // GOODBYE
787 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
788 begin
789 if gTriggers <> nil then
790 for a := 0 to High(gTriggers) do
791 if gTriggers[a].TriggerType = TRIGGER_EXIT then
792 begin
793 gExitByTrigger := True;
794 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
795 g_Game_ExitLevel(gTriggers[a].tgcMap);
796 Break;
797 end;
798 goto Cheated;
799 end;
800 //
801 s2 := Copy(charbuff, 15, 2);
802 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
803 begin
804 if g_Map_Exist(gGameSettings.WAD + ':\MAP' + s2) then
805 begin
806 c := 'MAP' + s2;
807 g_Game_ExitLevel(c);
808 end;
809 goto Cheated;
810 end;
811 //
812 if CheckCheat(I_GAME_CHEAT_FLY) then
813 begin
814 gFly := not gFly;
815 goto Cheated;
816 end;
817 // BULLFROG
818 if CheckCheat(I_GAME_CHEAT_JUMPS) then
819 begin
820 VEL_JUMP := 30-VEL_JUMP;
821 goto Cheated;
822 end;
823 // FORMULA1
824 if CheckCheat(I_GAME_CHEAT_SPEED) then
825 begin
826 MAX_RUNVEL := 32-MAX_RUNVEL;
827 goto Cheated;
828 end;
829 // CONDOM
830 if CheckCheat(I_GAME_CHEAT_SUIT) then
831 begin
832 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
833 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
834 goto Cheated;
835 end;
836 //
837 if CheckCheat(I_GAME_CHEAT_AIR) then
838 begin
839 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
840 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
841 goto Cheated;
842 end;
843 // PURELOVE
844 if CheckCheat(I_GAME_CHEAT_BERSERK) then
845 begin
846 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
847 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
848 goto Cheated;
849 end;
850 //
851 if CheckCheat(I_GAME_CHEAT_JETPACK) then
852 begin
853 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
854 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
855 goto Cheated;
856 end;
857 // CASPER
858 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
859 begin
860 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
861 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
862 goto Cheated;
863 end;
864 //
865 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
866 begin
867 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
868 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
869 goto Cheated;
870 end;
871 // INFERNO
872 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
873 begin
874 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
875 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
876 goto Cheated;
877 end;
878 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
879 begin
880 gAimLine := not gAimLine;
881 goto Cheated;
882 end;
883 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
884 begin
885 gShowMap := not gShowMap;
886 goto Cheated;
887 end;
888 Exit;
890 Cheated:
891 g_Sound_PlayEx(s);
892 end;
895 procedure KeyPress (K: Word);
896 {$IFNDEF HEADLESS}
897 var
898 Msg: g_gui.TMessage;
899 {$ENDIF}
900 begin
901 {$IFNDEF HEADLESS}
902 case K of
903 VK_ESCAPE: // <Esc>:
904 begin
905 if (g_ActiveWindow <> nil) then
906 begin
907 Msg.Msg := WM_KEYDOWN;
908 Msg.WParam := VK_ESCAPE;
909 g_ActiveWindow.OnMessage(Msg);
910 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
911 end
912 else if (gState <> STATE_FOLD) then
913 begin
914 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
915 begin
916 g_Game_InGameMenu(True);
917 end
918 else if (gExit = 0) and (gState <> STATE_SLIST) then
919 begin
920 if (gState <> STATE_MENU) then
921 begin
922 if (NetMode <> NET_NONE) then
923 begin
924 g_Game_StopAllSounds(True);
925 g_Game_Free;
926 gState := STATE_MENU;
927 Exit;
928 end;
929 end;
930 g_GUI_ShowWindow('MainMenu');
931 g_Sound_PlayEx('MENU_OPEN');
932 end;
933 end;
934 end;
936 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
937 begin // <F2> .. <F6> � <F12>
938 if gGameOn and (not gConsoleShow) and (not gChatShow) then
939 begin
940 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
941 if (not g_Game_IsNet) then g_Game_Pause(True);
942 case K of
943 IK_F2: g_Menu_Show_SaveMenu();
944 IK_F3: g_Menu_Show_LoadMenu();
945 IK_F4: g_Menu_Show_GameSetGame();
946 IK_F5: g_Menu_Show_OptionsVideo();
947 IK_F6: g_Menu_Show_OptionsSound();
948 IK_F7: g_Menu_Show_EndGameMenu();
949 IK_F10: g_Menu_Show_QuitGameMenu();
950 end;
951 end;
952 end;
954 else
955 begin
956 gJustChatted := False;
957 if gConsoleShow or gChatShow then
958 begin
959 g_Console_Control(K);
960 end
961 else if (g_ActiveWindow <> nil) then
962 begin
963 Msg.Msg := WM_KEYDOWN;
964 Msg.WParam := K;
965 g_ActiveWindow.OnMessage(Msg);
966 end
967 else if (gState = STATE_MENU) then
968 begin
969 g_GUI_ShowWindow('MainMenu');
970 g_Sound_PlayEx('MENU_OPEN');
971 end;
972 end;
973 end;
974 {$ENDIF}
975 end;
978 procedure CharPress (C: AnsiChar);
979 var
980 Msg: g_gui.TMessage;
981 a: Integer;
982 begin
983 if gConsoleShow or gChatShow then
984 begin
985 g_Console_Char(C)
986 end
987 else if (g_ActiveWindow <> nil) then
988 begin
989 Msg.Msg := WM_CHAR;
990 Msg.WParam := Ord(C);
991 g_ActiveWindow.OnMessage(Msg);
992 end
993 else
994 begin
995 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
996 charbuff[15] := upcase1251(C);
997 Cheat();
998 end;
999 end;
1001 end.