DEADSOFTWARE

sdlmixer: more midi configuration
[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 date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME, TMsgType.Notify);
457 e_WriteLog('Build hash: ' + g_GetBuildHash(), TMsgType.Notify);
458 e_WriteLog('Build by: ' + g_GetBuilderName(), TMsgType.Notify);
460 e_LogWritefln('Force bin dir: %s', [forceBinDir], TMsgType.Notify);
461 e_LogWritefln('BINARY PATH: [%s]', [binPath], TMsgType.Notify);
463 PrintDirs('DataDirs', DataDirs);
464 PrintDirs('ModelDirs', ModelDirs);
465 PrintDirs('MegawadDirs', MegawadDirs);
466 PrintDirs('MapDirs', MapDirs);
467 PrintDirs('WadDirs', WadDirs);
469 PrintDirs('LogDirs', LogDirs);
470 PrintDirs('SaveDirs', SaveDirs);
471 PrintDirs('CacheDirs', CacheDirs);
472 PrintDirs('ConfigDirs', ConfigDirs);
473 PrintDirs('ScreenshotDirs', ScreenshotDirs);
474 PrintDirs('StatsDirs', StatsDirs);
475 PrintDirs('MapDownloadDirs', MapDownloadDirs);
476 PrintDirs('WadDownloadDirs', WadDownloadDirs);
478 GameWAD := e_FindWad(DataDirs, GameWADName);
479 if GameWad = '' then
480 begin
481 e_WriteLog('WAD ' + GameWADName + ' not found in data directories.', TMsgType.Fatal);
482 {$IF DEFINED(USE_SDL2) AND NOT DEFINED(HEADLESS)}
483 if forceBinDir = false then
484 SDL_ShowSimpleMessageBox(
485 SDL_MESSAGEBOX_ERROR,
486 'Doom 2D Forever',
487 PChar('WAD ' + GameWADName + ' not found in data directories.'),
488 nil
489 );
490 {$ENDIF}
491 e_DeinitLog;
492 Halt(1);
493 end;
494 end;
496 procedure Main();
497 {$IFDEF ENABLE_HOLMES}
498 var flexloaded: Boolean;
499 {$ENDIF}
500 begin
501 InitPath;
502 InitPrep;
503 e_InitInput;
504 sys_Init;
506 g_Options_SetDefault;
507 g_Options_SetDefaultVideo;
508 g_Console_SysInit;
509 if sys_SetDisplayMode(gRC_Width, gRC_Height, gBPP, gRC_FullScreen, gRC_Maximized) = False then
510 raise Exception.Create('Failed to set videomode on startup.');
512 e_WriteLog(gLanguage, TMsgType.Notify);
513 g_Language_Set(gLanguage);
515 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
516 flexloaded := true;
517 if not fuiAddWad('flexui.wad') then
518 begin
519 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
520 end;
521 try
522 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
523 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
524 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
525 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
526 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
527 except on e: Exception do
528 begin
529 writeln('ERROR loading FlexUI fonts');
530 flexloaded := false;
531 //raise;
532 end;
533 else
534 begin
535 flexloaded := false;
536 //raise;
537 end;
538 end;
539 if (flexloaded) then
540 begin
541 try
542 e_LogWriteln('FlexUI: loading stylesheet...');
543 uiLoadStyles('flexui/widgets.wgs');
544 except on e: TParserException do
545 begin
546 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
547 //raise;
548 flexloaded := false;
549 end;
550 else
551 begin
552 //raise;
553 flexloaded := false;
554 end;
555 end;
556 end;
557 g_holmes_imfunctional := not flexloaded;
559 if (not g_holmes_imfunctional) then
560 begin
561 uiInitialize();
562 uiContext.font := 'win14';
563 end;
565 if assigned(oglInitCB) then oglInitCB;
566 {$ENDIF}
568 //g_Res_CreateDatabases(true); // it will be done before connecting to the server for the first time
570 e_WriteLog('Entering SDLMain', TMsgType.Notify);
572 {$WARNINGS OFF}
573 SDLMain();
574 {$WARNINGS ON}
576 {$IFDEF ENABLE_HOLMES}
577 if assigned(oglDeinitCB) then oglDeinitCB;
578 {$ENDIF}
580 g_Console_WriteGameConfig;
581 sys_Final;
582 end;
584 procedure Init();
585 {$IFDEF USE_SDLMIXER}
586 var timiditycfg: AnsiString;
587 var oldcwd, newcwd: RawByteString;
588 {$ENDIF}
589 var NoSound: Boolean;
590 begin
591 Randomize;
593 {$IFDEF HEADLESS}
594 {$IFDEF USE_SDLMIXER}
595 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
596 {$ELSE}
597 NoSound := True; // FMOD backend will sort it out
598 {$ENDIF}
599 {$ELSE}
600 NoSound := False;
601 {$ENDIF}
603 g_Touch_Init;
605 (*
606 if (e_JoysticksAvailable > 0) then
607 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
608 else
609 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
610 *)
612 if gNoSound = false then
613 begin
614 e_WriteLog('Initializing sound system', TMsgType.Notify);
615 {$IFDEF USE_SDLMIXER}
616 newcwd := '';
617 if UseNativeMusic then
618 SetEnvVar('SDL_NATIVE_MUSIC', '1');
619 timiditycfg := GetEnvironmentVariable('TIMIDITY_CFG');
620 if timiditycfg = '' then
621 begin
622 timiditycfg := 'timidity.cfg';
623 if e_FindResource(ConfigDirs, timiditycfg) OR e_FindResource(DataDirs, timiditycfg) then
624 begin
625 timiditycfg := ExpandFileName(timiditycfg);
626 newcwd := ExtractFileDir(timiditycfg);
627 SetEnvVar('TIMIDITY_CFG', timiditycfg);
628 end
629 else
630 timiditycfg := '';
631 end;
632 e_LogWritefln('TIMIDITY_CFG = "%s"', [timiditycfg]);
633 e_LogWritefln('SDL_NATIVE_MUSIC = "%s"', [GetEnvironmentVariable('SDL_NATIVE_MUSIC')]);
634 {$ENDIF}
635 e_InitSoundSystem(NoSound);
636 {$IFDEF USE_SDLMIXER}
637 if e_TimidityDecoder and (newcwd <> '') then
638 begin
639 (* HACK: Set CWD to load GUS patches relatively to cfg file. *)
640 (* CWD not restored after sound init because timidity *)
641 (* store relative pathes internally and load patches *)
642 (* later. I hope game never relies on CWD. *)
643 oldcwd := '';
644 GetDir(0, oldcwd);
645 ChDir(newcwd);
646 e_logwritefln('WARNING: USED TIMIDITY CONFIG HACK, CWD SWITCHED "%s" -> "%s"', [oldcwd, newcwd]);
647 end;
648 {$ENDIF}
649 end;
651 e_WriteLog('Init game', TMsgType.Notify);
652 g_Game_Init();
654 FillChar(charbuff, sizeof(charbuff), ' ');
655 end;
658 procedure Release();
659 begin
660 e_WriteLog('Releasing engine', TMsgType.Notify);
661 e_ReleaseEngine();
663 e_WriteLog('Releasing input', TMsgType.Notify);
664 e_ReleaseInput();
666 if not gNoSound then
667 begin
668 e_WriteLog('Releasing sound', TMsgType.Notify);
669 e_ReleaseSoundSystem();
670 end;
671 end;
674 procedure Update ();
675 begin
676 // remember old mobj positions, prepare for update
677 g_Game_PreUpdate();
678 // server: receive client commands for new frame
679 // client: receive game state changes from server
680 if (NetMode = NET_SERVER) then g_Net_Host_Update()
681 else if (NetMode = NET_CLIENT) then g_Net_Client_Update();
682 // think
683 g_Game_Update();
684 // server: send any accumulated outgoing data to clients
685 if NetMode = NET_SERVER then g_Net_Flush();
686 end;
689 procedure Draw ();
690 begin
691 g_Game_Draw();
692 end;
695 function Translit (const S: AnsiString): AnsiString;
696 var
697 i: Integer;
698 begin
699 Result := S;
700 for i := 1 to Length(Result) do
701 begin
702 case Result[i] of
703 'É': Result[i] := 'Q';
704 'Ö': Result[i] := 'W';
705 'Ó': Result[i] := 'E';
706 'Ê': Result[i] := 'R';
707 'Å': Result[i] := 'T';
708 'Í': Result[i] := 'Y';
709 'Ã': Result[i] := 'U';
710 'Ø': Result[i] := 'I';
711 'Ù': Result[i] := 'O';
712 'Ç': Result[i] := 'P';
713 'Õ': Result[i] := '['; //Chr(219);
714 'Ú': Result[i] := ']'; //Chr(221);
715 'Ô': Result[i] := 'A';
716 'Û': Result[i] := 'S';
717 'Â': Result[i] := 'D';
718 'À': Result[i] := 'F';
719 'Ï': Result[i] := 'G';
720 'Ð': Result[i] := 'H';
721 'Î': Result[i] := 'J';
722 'Ë': Result[i] := 'K';
723 'Ä': Result[i] := 'L';
724 'Æ': Result[i] := ';'; //Chr(186);
725 'Ý': Result[i] := #39; //Chr(222);
726 'ß': Result[i] := 'Z';
727 '×': Result[i] := 'X';
728 'Ñ': Result[i] := 'C';
729 'Ì': Result[i] := 'V';
730 'È': Result[i] := 'B';
731 'Ò': Result[i] := 'N';
732 'Ü': Result[i] := 'M';
733 'Á': Result[i] := ','; //Chr(188);
734 'Þ': Result[i] := '.'; //Chr(190);
735 end;
736 end;
737 end;
740 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
741 var
742 ls1, ls2: string;
743 begin
744 ls1 := CheatEng[ct];
745 ls2 := Translit(CheatRus[ct]);
746 if length(ls1) = 0 then ls1 := '~';
747 if length(ls2) = 0 then ls2 := '~';
748 result :=
749 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
750 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
751 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
752 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
754 if ct = I_GAME_CHEAT_JETPACK then
755 begin
756 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
757 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
758 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
759 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
760 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
761 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
762 end;
764 end;
767 procedure Cheat ();
768 const
769 CHEAT_DAMAGE = 500;
770 label
771 Cheated;
772 var
773 s, s2: string;
774 c: ShortString;
775 a: Integer;
776 begin
778 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
779 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
780 or g_Game_IsNet then Exit;
782 if not gGameOn then exit;
783 if not conIsCheatsEnabled then exit;
785 s := 'SOUND_GAME_RADIO';
787 //
788 if CheckCheat(I_GAME_CHEAT_GODMODE) then
789 begin
790 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
791 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
792 goto Cheated;
793 end;
794 // RAMBO
795 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
796 begin
797 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
798 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
799 goto Cheated;
800 end;
801 // TANK
802 if CheckCheat(I_GAME_CHEAT_HEALTH) then
803 begin
804 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
805 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
806 goto Cheated;
807 end;
808 // IDDQD
809 if CheckCheat(I_GAME_CHEAT_DEATH) then
810 begin
811 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
812 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
813 s := 'SOUND_MONSTER_HAHA';
814 goto Cheated;
815 end;
816 //
817 if CheckCheat(I_GAME_CHEAT_DOORS) then
818 begin
819 g_Triggers_OpenAll();
820 goto Cheated;
821 end;
822 // GOODBYE
823 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
824 begin
825 if gTriggers <> nil then
826 for a := 0 to High(gTriggers) do
827 if gTriggers[a].TriggerType = TRIGGER_EXIT then
828 begin
829 gExitByTrigger := True;
830 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
831 g_Game_ExitLevel(gTriggers[a].tgcMap);
832 Break;
833 end;
834 goto Cheated;
835 end;
836 //
837 s2 := Copy(charbuff, 15, 2);
838 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
839 begin
840 if g_Map_Exist(gGameSettings.WAD + ':\MAP' + s2) then
841 begin
842 c := 'MAP' + s2;
843 g_Game_ExitLevel(c);
844 end;
845 goto Cheated;
846 end;
847 //
848 if CheckCheat(I_GAME_CHEAT_FLY) then
849 begin
850 gFly := not gFly;
851 goto Cheated;
852 end;
853 // BULLFROG
854 if CheckCheat(I_GAME_CHEAT_JUMPS) then
855 begin
856 VEL_JUMP := 30-VEL_JUMP;
857 goto Cheated;
858 end;
859 // FORMULA1
860 if CheckCheat(I_GAME_CHEAT_SPEED) then
861 begin
862 MAX_RUNVEL := 32-MAX_RUNVEL;
863 goto Cheated;
864 end;
865 // CONDOM
866 if CheckCheat(I_GAME_CHEAT_SUIT) then
867 begin
868 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
869 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
870 goto Cheated;
871 end;
872 //
873 if CheckCheat(I_GAME_CHEAT_AIR) then
874 begin
875 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
876 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
877 goto Cheated;
878 end;
879 // PURELOVE
880 if CheckCheat(I_GAME_CHEAT_BERSERK) then
881 begin
882 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
883 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
884 goto Cheated;
885 end;
886 //
887 if CheckCheat(I_GAME_CHEAT_JETPACK) then
888 begin
889 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
890 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
891 goto Cheated;
892 end;
893 // CASPER
894 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
895 begin
896 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
897 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
898 goto Cheated;
899 end;
900 //
901 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
902 begin
903 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
904 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
905 goto Cheated;
906 end;
907 // INFERNO
908 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
909 begin
910 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
911 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
912 goto Cheated;
913 end;
914 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
915 begin
916 gAimLine := not gAimLine;
917 goto Cheated;
918 end;
919 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
920 begin
921 gShowMap := not gShowMap;
922 goto Cheated;
923 end;
924 Exit;
926 Cheated:
927 g_Sound_PlayEx(s);
928 end;
931 procedure KeyPress (K: Word);
932 {$IFNDEF HEADLESS}
933 var
934 Msg: g_gui.TMessage;
935 {$ENDIF}
936 begin
937 {$IFNDEF HEADLESS}
938 case K of
939 VK_ESCAPE: // <Esc>:
940 begin
941 if (g_ActiveWindow <> nil) then
942 begin
943 Msg.Msg := WM_KEYDOWN;
944 Msg.WParam := VK_ESCAPE;
945 g_ActiveWindow.OnMessage(Msg);
946 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
947 end
948 else if (gState <> STATE_FOLD) then
949 begin
950 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
951 begin
952 g_Game_InGameMenu(True);
953 end
954 else if (gExit = 0) and (gState <> STATE_SLIST) then
955 begin
956 if (gState <> STATE_MENU) then
957 begin
958 if (NetMode <> NET_NONE) then
959 begin
960 g_Game_StopAllSounds(True);
961 g_Game_Free;
962 gState := STATE_MENU;
963 Exit;
964 end;
965 end;
966 g_GUI_ShowWindow('MainMenu');
967 g_Sound_PlayEx('MENU_OPEN');
968 end;
969 end;
970 end;
972 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
973 begin // <F2> .. <F6> � <F12>
974 if gGameOn and (not gConsoleShow) and (not gChatShow) then
975 begin
976 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
977 if (not g_Game_IsNet) then g_Game_Pause(True);
978 case K of
979 IK_F2: g_Menu_Show_SaveMenu();
980 IK_F3: g_Menu_Show_LoadMenu();
981 IK_F4: g_Menu_Show_GameSetGame();
982 IK_F5: g_Menu_Show_OptionsVideo();
983 IK_F6: g_Menu_Show_OptionsSound();
984 IK_F7: g_Menu_Show_EndGameMenu();
985 IK_F10: g_Menu_Show_QuitGameMenu();
986 end;
987 end;
988 end;
990 else
991 begin
992 gJustChatted := False;
993 if gConsoleShow or gChatShow then
994 begin
995 g_Console_Control(K);
996 end
997 else if (g_ActiveWindow <> nil) then
998 begin
999 Msg.Msg := WM_KEYDOWN;
1000 Msg.WParam := K;
1001 g_ActiveWindow.OnMessage(Msg);
1002 end
1003 else if (gState = STATE_MENU) then
1004 begin
1005 g_GUI_ShowWindow('MainMenu');
1006 g_Sound_PlayEx('MENU_OPEN');
1007 end;
1008 end;
1009 end;
1010 {$ENDIF}
1011 end;
1014 procedure CharPress (C: AnsiChar);
1015 var
1016 Msg: g_gui.TMessage;
1017 a: Integer;
1018 begin
1019 if gConsoleShow or gChatShow then
1020 begin
1021 g_Console_Char(C)
1022 end
1023 else if (g_ActiveWindow <> nil) then
1024 begin
1025 Msg.Msg := WM_CHAR;
1026 Msg.WParam := Ord(C);
1027 g_ActiveWindow.OnMessage(Msg);
1028 end
1029 else
1030 begin
1031 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
1032 charbuff[15] := upcase1251(C);
1033 Cheat();
1034 end;
1035 end;
1037 initialization
1038 {$IFDEF USE_SDLMIXER}
1039 conRegVar('sdl_native_music', @UseNativeMusic, 'use native midi music output when possible', 'use native midi');
1040 {$IFDEF DARWIN}
1041 UseNativeMusic := true; (* OSX have a good midi support, so why not? *)
1042 {$ELSE}
1043 UseNativeMusic := false;
1044 {$ENDIF}
1045 {$ENDIF}
1046 end.