DEADSOFTWARE

debug: write build arch to log
[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;
82 {$IFDEF USE_SDLMIXER}
83 UseNativeMusic: Boolean;
84 {$ENDIF}
86 function GetBinaryPath (): AnsiString;
87 {$IFDEF LINUX}
88 var
89 //cd: AnsiString;
90 sl: AnsiString;
91 {$ENDIF}
92 begin
93 result := ExtractFilePath(ParamStr(0));
94 {$IFDEF LINUX}
95 // it may be a symlink; do some guesswork here
96 sl := fpReadLink(ExtractFileName(ParamStr(0)));
97 if (sl = ParamStr(0)) then
98 begin
99 // use current directory, as we don't have anything better
100 //result := '.';
101 GetDir(0, result);
102 end;
103 {$ENDIF}
104 result := fixSlashes(result);
105 if (length(result) > 0) and (result[length(result)] <> '/') then result := result+'/';
106 end;
108 procedure PrintDirs (msg: AnsiString; dirs: SSArray);
109 var dir: AnsiString;
110 begin
111 e_LogWriteln(msg + ':');
112 for dir in dirs do
113 e_LogWriteln(' ' + dir);
114 end;
116 {$IFDEF DARWIN}
117 function NSStringToAnsiString (s: NSString): AnsiString;
118 var i: Integer;
119 begin
120 result := '';
121 for i := 0 to s.length - 1 do
122 result := result + AnsiChar(s.characterAtIndex(i));
123 end;
125 function GetBundlePath (): AnsiString;
126 var pathRef: CFURLRef; pathCFStr: CFStringRef; pathStr: ShortString;
127 begin
128 pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
129 pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
130 CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
131 CFRelease(pathRef);
132 CFRelease(pathCFStr);
133 Result := pathStr;
134 end;
135 {$ENDIF}
137 procedure InitPath;
138 var i: Integer; rwdir, rodir: AnsiString; rwdirs, rodirs: SSArray;
140 procedure AddDir (var dirs: SSArray; append: AnsiString);
141 begin
142 SetLength(dirs, Length(dirs) + 1);
143 dirs[High(dirs)] := ExpandFileName(append)
144 end;
146 function IsSep (ch: Char): Boolean;
147 begin
148 {$IFDEF WINDOWS}
149 result := (ch = '/') or (ch = '\');
150 {$ELSE}
151 result := (ch = '/');
152 {$ENDIF}
153 end;
155 function OptimizePath (dir: AnsiString): AnsiString;
156 var i, len: Integer; s: AnsiString;
157 begin
158 i := 1; len := Length(dir); s := '';
159 while i <= len do
160 begin
161 if IsSep(dir[i]) then
162 begin
163 s := s + DirectorySeparator;
164 Inc(i);
165 while (i <= len) and IsSep(dir[i]) do Inc(i);
166 if (i <= len) and (dir[i] = '.') then
167 begin
168 if (i = len) or IsSep(dir[i + 1]) then
169 begin
170 Inc(i)
171 end
172 else if (i + 1 <= len) and (dir[i + 1] = '.') then
173 begin
174 if (i + 1 = len) or IsSep(dir[i + 2]) then
175 begin
176 s := e_UpperDir(s);
177 Inc(i, 2)
178 end
179 end
180 end
181 end
182 else
183 begin
184 s := s + dir[i];
185 Inc(i)
186 end
187 end;
188 result := s
189 end;
191 procedure OptimizeDirs (var dirs: SSArray);
192 var i, j, k: Integer;
193 begin
194 for i := 0 to High(dirs) do
195 dirs[i] := OptimizePath(dirs[i]);
196 // deduplicate
197 i := High(dirs);
198 while i >= 0 do
199 begin
200 j := 0;
201 while j < i do
202 begin
203 if dirs[j] = dirs[i] then
204 begin
205 for k := j + 1 to High(dirs) do
206 dirs[k - 1] := dirs[k];
207 Dec(i);
208 SetLength(dirs, High(dirs))
209 end
210 else
211 begin
212 Inc(j)
213 end
214 end;
215 Dec(i)
216 end
217 end;
219 procedure AddDef (var dirs: SSArray; base: SSArray; append: AnsiString);
220 var s: AnsiString;
221 begin
222 if Length(dirs) = 0 then
223 for s in base do
224 AddDir(dirs, e_CatPath(s, append));
225 OptimizeDirs(dirs)
226 end;
228 function GetDefaultRODirs (): SSArray;
229 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
230 var home: AnsiString;
231 {$ENDIF}
232 {$IFDEF WINDOWS}
233 var appdata: AnsiString;
234 {$ENDIF}
235 {$IFDEF DARWIN}
236 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
237 {$ENDIF}
238 begin
239 result := nil;
240 {$IFDEF DARWIN}
241 bundle := GetBundlePath();
242 if ExtractFileExt(bundle) <> '.app' then
243 AddDir(result, binpath);
244 {$ELSE}
245 AddDir(result, binPath);
246 {$ENDIF}
247 if forceBinDir = false then
248 begin
249 {$IFDEF USE_SDL2}
250 AddDir(result, SDL_GetBasePath());
251 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
252 {$ENDIF}
253 {$IFDEF WINDOWS}
254 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
255 if appdata <> '' then
256 AddDir(result, appdata);
257 {$ENDIF}
258 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
259 AddDir(result, '/usr/share/doom2df');
260 AddDir(result, '/usr/local/share/doom2df');
261 home := GetEnvironmentVariable('HOME');
262 if home <> '' then
263 AddDir(result, e_CatPath(home, '.doom2df'));
264 {$ENDIF}
265 {$IFDEF DARWIN}
266 bundle := GetBundlePath();
267 if bundle <> '' then
268 AddDir(result, e_CatPath(bundle, 'Contents/Resources'));
269 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
270 for i := 0 to dirArr.count - 1 do
271 begin
272 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
273 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
274 end;
275 {$ENDIF}
276 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
277 AddDir(result, SDL_AndroidGetInternalStoragePath());
278 if SDL_AndroidGetExternalStorageState() <> 0 then
279 AddDir(result, SDL_AndroidGetExternalStoragePath());
280 {$ENDIF}
281 end
282 end;
284 function GetDefaultRWDirs (): SSArray;
285 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
286 var home: AnsiString;
287 {$ENDIF}
288 {$IFDEF WINDOWS}
289 var appdata: AnsiString;
290 {$ENDIF}
291 {$IFDEF DARWIN}
292 var bundle, s: AnsiString; dirArr: NSArray; i: Integer;
293 {$ENDIF}
294 begin
295 result := nil;
296 {$IFDEF DARWIN}
297 bundle := GetBundlePath();
298 if ExtractFileExt(bundle) <> '.app' then
299 AddDir(result, binPath);
300 {$ELSE}
301 AddDir(result, binPath);
302 {$ENDIF}
303 if forceBinDir = false then
304 begin
305 {$IFDEF USE_SDL2}
306 AddDir(result, SDL_GetPrefPath('', 'doom2df'));
307 {$ENDIF}
308 {$IFDEF WINDOWS}
309 appdata := GetEnvironmentVariable('APPDATA') + '\doom2df';
310 if appdata <> '' then
311 AddDir(result, appdata);
312 {$ENDIF}
313 {$IF DEFINED(UNIX) AND NOT DEFINED(DARWIN) AND NOT DEFINED(ANDROID)}
314 home := GetEnvironmentVariable('HOME');
315 if home <> '' then
316 AddDir(result, e_CatPath(home, '.doom2df'));
317 {$ENDIF}
318 {$IFDEF DARWIN}
319 dirArr := NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true);
320 for i := 0 to dirArr.count - 1 do
321 begin
322 s := NSStringToAnsiString(dirArr.objectAtIndex(i));
323 AddDir(result, e_CatPath(s, 'Doom 2D Forever'))
324 end;
325 {$ENDIF}
326 {$IF DEFINED(ANDROID) AND DEFINED(USE_SDL2)}
327 if SDL_AndroidGetExternalStorageState() <> 0 then
328 AddDir(result, SDL_AndroidGetExternalStoragePath());
329 {$ENDIF}
330 end
331 end;
333 begin
334 forceBinDir := false;
335 binPath := GetBinaryPath();
337 i := 1;
338 while i < ParamCount do
339 begin
340 case ParamStr(i) of
341 '--like-windoze': forceBinDir := true;
342 '--rw-dir':
343 begin
344 Inc(i);
345 rwdir := ParamStr(i);
346 (* RW *)
347 AddDir(LogDirs, e_CatPath(rwdir, ''));
348 AddDir(SaveDirs, e_CatPath(rwdir, 'data'));
349 AddDir(CacheDirs, e_CatPath(rwdir, 'data/cache'));
350 AddDir(ConfigDirs, e_CatPath(rwdir, ''));
351 AddDir(MapDownloadDirs, e_CatPath(rwdir, 'maps/downloads'));
352 AddDir(WadDownloadDirs, e_CatPath(rwdir, 'wads/downloads'));
353 AddDir(ScreenshotDirs, e_CatPath(rwdir, 'screenshots'));
354 AddDir(StatsDirs, e_CatPath(rwdir, 'stats'));
355 (* RO *)
356 AddDir(DataDirs, e_CatPath(rwdir, 'data'));
357 AddDir(ModelDirs, e_CatPath(rwdir, 'data/models'));
358 AddDir(MegawadDirs, e_CatPath(rwdir, 'maps/megawads'));
359 AddDir(MapDirs, e_CatPath(rwdir, 'maps'));
360 AddDir(WadDirs, e_CatPath(rwdir, 'wads'));
361 end;
362 '--ro-dir':
363 begin
364 Inc(i);
365 rodir := ParamStr(i);
366 (* RO *)
367 AddDir(DataDirs, e_CatPath(rodir, 'data'));
368 AddDir(ModelDirs, e_CatPath(rodir, 'data/models'));
369 AddDir(MegawadDirs, e_CatPath(rodir, 'maps/megawads'));
370 AddDir(MapDirs, e_CatPath(rodir, 'maps'));
371 AddDir(WadDirs, e_CatPath(rodir, 'wads'));
372 end;
373 '--game-wad':
374 begin
375 Inc(i);
376 GameWADName := ParamStr(i);
377 end;
378 '--config':
379 begin
380 Inc(i);
381 gConfigScript := ParamStr(i);
382 end;
383 end;
384 Inc(i)
385 end;
387 // prefer bin dir if it writable and contains game.wad
388 if forceBinDir = false then
389 begin
390 if findDiskWad(binPath + 'data' + '/' + GameWADName) <> '' then
391 if e_CanCreateFilesAt(binPath) then
392 forceBinDir := true
393 end;
395 (* RO *)
396 rodirs := GetDefaultRODirs();
397 AddDef(DataDirs, rodirs, 'data');
398 AddDef(ModelDirs, rodirs, 'data/models');
399 AddDef(MegawadDirs, rodirs, 'maps/megawads');
400 AddDef(MapDirs, rodirs, 'maps');
401 AddDef(WadDirs, rodirs, 'wads');
403 (* RW *)
404 rwdirs := GetDefaultRWDirs();
405 AddDef(LogDirs, rwdirs, '');
406 AddDef(SaveDirs, rwdirs, 'data');
407 AddDef(CacheDirs, rwdirs, 'data/cache');
408 AddDef(ConfigDirs, rwdirs, '');
409 AddDef(MapDownloadDirs, rwdirs, 'maps/downloads');
410 AddDef(WadDownloadDirs, rwdirs, 'wads/downloads');
411 AddDef(ScreenshotDirs, rwdirs, 'screenshots');
412 AddDef(StatsDirs, rwdirs, 'stats');
414 for i := 0 to High(MapDirs) do
415 AddDir(AllMapDirs, MapDirs[i]);
416 for i := 0 to High(MegawadDirs) do
417 AddDir(AllMapDirs, MegawadDirs[i]);
418 OptimizeDirs(AllMapDirs);
420 if LogFileName = '' then
421 begin
422 rwdir := e_GetWriteableDir(LogDirs, false);
423 if rwdir <> '' then
424 begin
425 {$IFDEF HEADLESS}
426 LogFileName := e_CatPath(rwdir, 'Doom2DF_H.log');
427 {$ELSE}
428 LogFileName := e_CatPath(rwdir, 'Doom2DF.log');
429 {$ENDIF}
430 end
431 end;
433 // HACK: ensure the screenshots folder also has a stats subfolder in it
434 rwdir := e_GetWriteableDir(ScreenshotDirs, false);
435 if rwdir <> '' then CreateDir(rwdir + '/stats');
436 end;
438 procedure InitPrep;
439 var i: Integer;
440 begin
441 {$IFDEF HEADLESS}
442 conbufDumpToStdOut := true;
443 {$ENDIF}
444 for i := 1 to ParamCount do
445 begin
446 case ParamStr(i) of
447 '--con-stdout': conbufDumpToStdOut := true;
448 '--no-fbo': glRenderToFBO := false;
449 end
450 end;
452 if LogFileName <> '' then
453 e_InitLog(LogFileName, TWriteMode.WM_NEWFILE);
454 e_InitWritelnDriver();
455 e_WriteLog('Doom 2D: Forever version ' + GAME_VERSION + ' proto ' + IntToStr(NET_PROTOCOL_VER), TMsgType.Notify);
456 e_WriteLog('Build arch: ' + g_GetBuildArch(), TMsgType.Notify);
457 e_WriteLog('Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
458 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
459 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
461 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
462 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
464 PrintDirs('DataDirs', DataDirs);
465 PrintDirs('ModelDirs', ModelDirs);
466 PrintDirs('MegawadDirs', MegawadDirs);
467 PrintDirs('MapDirs', MapDirs);
468 PrintDirs('WadDirs', WadDirs);
470 PrintDirs('LogDirs', LogDirs);
471 PrintDirs('SaveDirs', SaveDirs);
472 PrintDirs('CacheDirs', CacheDirs);
473 PrintDirs('ConfigDirs', ConfigDirs);
474 PrintDirs('ScreenshotDirs', ScreenshotDirs);
475 PrintDirs('StatsDirs', StatsDirs);
476 PrintDirs('MapDownloadDirs', MapDownloadDirs);
477 PrintDirs('WadDownloadDirs', WadDownloadDirs);
479 GameWAD := e_FindWad(DataDirs, GameWADName);
480 if GameWad = '' then
481 begin
482 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
483 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
484 if forceBinDir = false then
485 SDL_ShowSimpleMessageBox(
486 SDL_MESSAGEBOX_ERROR,
487 'Doom 2D Forever',
488 PChar('WAD ' + GameWADName + ' not found in data directories.'),
489 nil
490 );
491 {$ENDIF}
492 e_DeinitLog;
493 Halt(1);
494 end;
495 end;
497 procedure Main();
498 {$IFDEF ENABLE_HOLMES}
499 var flexloaded: Boolean;
500 {$ENDIF}
501 begin
502 InitPath;
503 InitPrep;
504 e_InitInput;
505 sys_Init;
507 g_Options_SetDefault;
508 g_Options_SetDefaultVideo;
509 g_Console_SysInit;
510 if sys_SetDisplayMode(gRC_Width, gRC_Height, gBPP, gRC_FullScreen, gRC_Maximized) = False then
511 raise Exception.Create('Failed to set videomode on startup.');
513 e_WriteLog(gLanguage, TMsgType.Notify);
514 g_Language_Set(gLanguage);
516 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
517 flexloaded := true;
518 if not fuiAddWad('flexui.wad') then
519 begin
520 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
521 end;
522 try
523 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
524 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
525 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
526 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
527 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
528 except on e: Exception do
529 begin
530 writeln('ERROR loading FlexUI fonts');
531 flexloaded := false;
532 //raise;
533 end;
534 else
535 begin
536 flexloaded := false;
537 //raise;
538 end;
539 end;
540 if (flexloaded) then
541 begin
542 try
543 e_LogWriteln('FlexUI: loading stylesheet...');
544 uiLoadStyles('flexui/widgets.wgs');
545 except on e: TParserException do
546 begin
547 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
548 //raise;
549 flexloaded := false;
550 end;
551 else
552 begin
553 //raise;
554 flexloaded := false;
555 end;
556 end;
557 end;
558 g_holmes_imfunctional := not flexloaded;
560 if (not g_holmes_imfunctional) then
561 begin
562 uiInitialize();
563 uiContext.font := 'win14';
564 end;
566 if assigned(oglInitCB) then oglInitCB;
567 {$ENDIF}
569 //g_Res_CreateDatabases(true); // it will be done before connecting to the server for the first time
571 e_WriteLog('Entering SDLMain', TMsgType.Notify);
573 {$WARNINGS OFF}
574 SDLMain();
575 {$WARNINGS ON}
577 {$IFDEF ENABLE_HOLMES}
578 if assigned(oglDeinitCB) then oglDeinitCB;
579 {$ENDIF}
581 g_Console_WriteGameConfig;
582 sys_Final;
583 end;
585 procedure Init();
586 {$IFDEF USE_SDLMIXER}
587 var timiditycfg: AnsiString;
588 var oldcwd, newcwd: RawByteString;
589 {$ENDIF}
590 var NoSound: Boolean;
591 begin
592 Randomize;
594 {$IFDEF HEADLESS}
595 {$IFDEF USE_SDLMIXER}
596 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
597 {$ELSE}
598 NoSound := True; // FMOD backend will sort it out
599 {$ENDIF}
600 {$ELSE}
601 NoSound := False;
602 {$ENDIF}
604 g_Touch_Init;
606 (*
607 if (e_JoysticksAvailable > 0) then
608 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
609 else
610 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
611 *)
613 if gNoSound = false then
614 begin
615 e_WriteLog('Initializing sound system', TMsgType.Notify);
616 {$IFDEF USE_SDLMIXER}
617 newcwd := '';
618 if UseNativeMusic then
619 SetEnvVar('SDL_NATIVE_MUSIC', '1');
620 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
621 if timiditycfg = '' then
622 begin
623 timiditycfg := 'timidity.cfg';
624 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
625 begin
626 timiditycfg := ExpandFileName(timiditycfg);
627 newcwd := ExtractFileDir(timiditycfg);
628 SetEnvVar('TIMIDITY_CFG', timiditycfg);
629 end
630 else
631 timiditycfg := '';
632 end;
633 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
634 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
635 {$ENDIF}
636 e_InitSoundSystem(NoSound);
637 {$IFDEF USE_SDLMIXER}
638 if e_TimidityDecoder and (newcwd <> '') then
639 begin
640 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
641 (* CWD not restored after sound init because timidity *)
642 (* store relative pathes internally and load patches *)
643 (* later. I hope game never relies on CWD. *)
644 oldcwd := '';
645 GetDir(0, oldcwd);
646 ChDir(newcwd);
647 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
648 end;
649 {$ENDIF}
650 end;
652 e_WriteLog('Init game', TMsgType.Notify);
653 g_Game_Init();
655 FillChar(charbuff, sizeof(charbuff), ' ');
656 end;
659 procedure Release();
660 begin
661 e_WriteLog('Releasing engine', TMsgType.Notify);
662 e_ReleaseEngine();
664 e_WriteLog('Releasing input', TMsgType.Notify);
665 e_ReleaseInput();
667 if not gNoSound then
668 begin
669 e_WriteLog('Releasing sound', TMsgType.Notify);
670 e_ReleaseSoundSystem();
671 end;
672 end;
675 procedure Update ();
676 begin
677 // remember old mobj positions, prepare for update
678 g_Game_PreUpdate();
679 // server: receive client commands for new frame
680 // client: receive game state changes from server
681 if (NetMode = NET_SERVER) then g_Net_Host_Update()
682 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
683 // think
684 g_Game_Update();
685 // server: send any accumulated outgoing data to clients
686 if NetMode = NET_SERVER then g_Net_Flush();
687 end;
690 procedure Draw ();
691 begin
692 g_Game_Draw();
693 end;
696 function Translit (const S: AnsiString): AnsiString;
697 var
698 i: Integer;
699 begin
700 Result := S;
701 for i := 1 to Length(Result) do
702 begin
703 case Result[i] of
704 'É': Result[i] := 'Q';
705 'Ö': Result[i] := 'W';
706 'Ó': Result[i] := 'E';
707 'Ê': Result[i] := 'R';
708 'Å': Result[i] := 'T';
709 'Í': Result[i] := 'Y';
710 'Ã': Result[i] := 'U';
711 'Ø': Result[i] := 'I';
712 'Ù': Result[i] := 'O';
713 'Ç': Result[i] := 'P';
714 'Õ': Result[i] := '['; //Chr(219);
715 'Ú': Result[i] := ']'; //Chr(221);
716 'Ô': Result[i] := 'A';
717 'Û': Result[i] := 'S';
718 'Â': Result[i] := 'D';
719 'À': Result[i] := 'F';
720 'Ï': Result[i] := 'G';
721 'Ð': Result[i] := 'H';
722 'Î': Result[i] := 'J';
723 'Ë': Result[i] := 'K';
724 'Ä': Result[i] := 'L';
725 'Æ': Result[i] := ';'; //Chr(186);
726 'Ý': Result[i] := #39; //Chr(222);
727 'ß': Result[i] := 'Z';
728 '×': Result[i] := 'X';
729 'Ñ': Result[i] := 'C';
730 'Ì': Result[i] := 'V';
731 'È': Result[i] := 'B';
732 'Ò': Result[i] := 'N';
733 'Ü': Result[i] := 'M';
734 'Á': Result[i] := ','; //Chr(188);
735 'Þ': Result[i] := '.'; //Chr(190);
736 end;
737 end;
738 end;
741 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
742 var
743 ls1, ls2: string;
744 begin
745 ls1 := CheatEng[ct];
746 ls2 := Translit(CheatRus[ct]);
747 if length(ls1) = 0 then ls1 := '~';
748 if length(ls2) = 0 then ls2 := '~';
749 result :=
750 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
751 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
752 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
753 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
755 if ct = I_GAME_CHEAT_JETPACK then
756 begin
757 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
758 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
759 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
760 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
761 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
762 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
763 end;
765 end;
768 procedure Cheat ();
769 const
770 CHEAT_DAMAGE = 500;
771 label
772 Cheated;
773 var
774 s, s2: string;
775 c: ShortString;
776 a: Integer;
777 begin
779 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
780 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
781 or g_Game_IsNet then Exit;
783 if not gGameOn then exit;
784 if not conIsCheatsEnabled then exit;
786 s := 'SOUND_GAME_RADIO';
788 //
789 if CheckCheat(I_GAME_CHEAT_GODMODE) then
790 begin
791 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
792 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
793 goto Cheated;
794 end;
795 // RAMBO
796 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
797 begin
798 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
799 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
800 goto Cheated;
801 end;
802 // TANK
803 if CheckCheat(I_GAME_CHEAT_HEALTH) then
804 begin
805 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
806 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
807 goto Cheated;
808 end;
809 // IDDQD
810 if CheckCheat(I_GAME_CHEAT_DEATH) then
811 begin
812 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
813 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
814 s := 'SOUND_MONSTER_HAHA';
815 goto Cheated;
816 end;
817 //
818 if CheckCheat(I_GAME_CHEAT_DOORS) then
819 begin
820 g_Triggers_OpenAll();
821 goto Cheated;
822 end;
823 // GOODBYE
824 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
825 begin
826 if gTriggers <> nil then
827 for a := 0 to High(gTriggers) do
828 if gTriggers[a].TriggerType = TRIGGER_EXIT then
829 begin
830 gExitByTrigger := True;
831 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
832 g_Game_ExitLevel(gTriggers[a].tgcMap);
833 Break;
834 end;
835 goto Cheated;
836 end;
837 //
838 s2 := Copy(charbuff, 15, 2);
839 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
840 begin
841 if g_Map_Exist(gGameSettings.WAD + ':\MAP' + s2) then
842 begin
843 c := 'MAP' + s2;
844 g_Game_ExitLevel(c);
845 end;
846 goto Cheated;
847 end;
848 //
849 if CheckCheat(I_GAME_CHEAT_FLY) then
850 begin
851 gFly := not gFly;
852 goto Cheated;
853 end;
854 // BULLFROG
855 if CheckCheat(I_GAME_CHEAT_JUMPS) then
856 begin
857 VEL_JUMP := 30-VEL_JUMP;
858 goto Cheated;
859 end;
860 // FORMULA1
861 if CheckCheat(I_GAME_CHEAT_SPEED) then
862 begin
863 MAX_RUNVEL := 32-MAX_RUNVEL;
864 goto Cheated;
865 end;
866 // CONDOM
867 if CheckCheat(I_GAME_CHEAT_SUIT) then
868 begin
869 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
870 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
871 goto Cheated;
872 end;
873 //
874 if CheckCheat(I_GAME_CHEAT_AIR) then
875 begin
876 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
877 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
878 goto Cheated;
879 end;
880 // PURELOVE
881 if CheckCheat(I_GAME_CHEAT_BERSERK) then
882 begin
883 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
884 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
885 goto Cheated;
886 end;
887 //
888 if CheckCheat(I_GAME_CHEAT_JETPACK) then
889 begin
890 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
891 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
892 goto Cheated;
893 end;
894 // CASPER
895 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
896 begin
897 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
898 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
899 goto Cheated;
900 end;
901 //
902 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
903 begin
904 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
905 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
906 goto Cheated;
907 end;
908 // INFERNO
909 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
910 begin
911 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
912 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
913 goto Cheated;
914 end;
915 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
916 begin
917 gAimLine := not gAimLine;
918 goto Cheated;
919 end;
920 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
921 begin
922 gShowMap := not gShowMap;
923 goto Cheated;
924 end;
925 Exit;
927 Cheated:
928 g_Sound_PlayEx(s);
929 end;
932 procedure KeyPress (K: Word);
933 {$IFNDEF HEADLESS}
934 var
935 Msg: g_gui.TMessage;
936 {$ENDIF}
937 begin
938 {$IFNDEF HEADLESS}
939 case K of
940 VK_ESCAPE: // <Esc>:
941 begin
942 if (g_ActiveWindow <> nil) then
943 begin
944 Msg.Msg := WM_KEYDOWN;
945 Msg.WParam := VK_ESCAPE;
946 g_ActiveWindow.OnMessage(Msg);
947 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
948 end
949 else if (gState <> STATE_FOLD) then
950 begin
951 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
952 begin
953 g_Game_InGameMenu(True);
954 end
955 else if (gExit = 0) and (gState <> STATE_SLIST) then
956 begin
957 if (gState <> STATE_MENU) then
958 begin
959 if (NetMode <> NET_NONE) then
960 begin
961 g_Game_StopAllSounds(True);
962 g_Game_Free;
963 gState := STATE_MENU;
964 Exit;
965 end;
966 end;
967 g_GUI_ShowWindow('MainMenu');
968 g_Sound_PlayEx('MENU_OPEN');
969 end;
970 end;
971 end;
973 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
974 begin // <F2> .. <F6> � <F12>
975 if gGameOn and (not gConsoleShow) and (not gChatShow) then
976 begin
977 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
978 if (not g_Game_IsNet) then g_Game_Pause(True);
979 case K of
980 IK_F2: g_Menu_Show_SaveMenu();
981 IK_F3: g_Menu_Show_LoadMenu();
982 IK_F4: g_Menu_Show_GameSetGame();
983 IK_F5: g_Menu_Show_OptionsVideo();
984 IK_F6: g_Menu_Show_OptionsSound();
985 IK_F7: g_Menu_Show_EndGameMenu();
986 IK_F10: g_Menu_Show_QuitGameMenu();
987 end;
988 end;
989 end;
991 else
992 begin
993 gJustChatted := False;
994 if gConsoleShow or gChatShow then
995 begin
996 g_Console_Control(K);
997 end
998 else if (g_ActiveWindow <> nil) then
999 begin
1000 Msg.Msg := WM_KEYDOWN;
1001 Msg.WParam := K;
1002 g_ActiveWindow.OnMessage(Msg);
1003 end
1004 else if (gState = STATE_MENU) then
1005 begin
1006 g_GUI_ShowWindow('MainMenu');
1007 g_Sound_PlayEx('MENU_OPEN');
1008 end;
1009 end;
1010 end;
1011 {$ENDIF}
1012 end;
1015 procedure CharPress (C: AnsiChar);
1016 var
1017 Msg: g_gui.TMessage;
1018 a: Integer;
1019 begin
1020 if gConsoleShow or gChatShow then
1021 begin
1022 g_Console_Char(C)
1023 end
1024 else if (g_ActiveWindow <> nil) then
1025 begin
1026 Msg.Msg := WM_CHAR;
1027 Msg.WParam := Ord(C);
1028 g_ActiveWindow.OnMessage(Msg);
1029 end
1030 else
1031 begin
1032 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
1033 charbuff[15] := upcase1251(C);
1034 Cheat();
1035 end;
1036 end;
1038 initialization
1039 {$IFDEF USE_SDLMIXER}
1040 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1041 {$IFDEF DARWIN}
1042 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1043 {$ELSE}
1044 UseNativeMusic := false;
1045 {$ENDIF}
1046 {$ENDIF}
1047 end.