DEADSOFTWARE

Refactor: Define constants for LiftType
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE ../shared/a_modes.inc}
17 unit g_main;
19 interface
21 procedure Main ();
22 procedure Init ();
23 procedure Release ();
24 procedure Update ();
25 procedure Draw ();
26 procedure KeyPress (K: Word);
27 procedure CharPress (C: AnsiChar);
29 var
30 GameDir: string;
31 DataDir: string;
32 MapsDir: string;
33 ModelsDir: string;
34 GameWAD: string;
37 implementation
39 uses
40 {$INCLUDE ../nogl/noGLuses.inc}
41 {$IFDEF ENABLE_HOLMES}
42 g_holmes, fui_wadread, fui_style, fui_gfx_gl,
43 {$ENDIF}
44 SDL2, wadreader, e_log, g_window,
45 e_graphics, e_input, g_game, g_console, g_gui,
46 e_sound, g_options, g_sound, g_player, g_basic,
47 g_weapons, SysUtils, g_triggers, MAPDEF, g_map,
48 g_menu, g_language, g_net, g_touch,
49 utils, conbuf, envvars,
50 xparser;
53 var
54 charbuff: packed array [0..15] of AnsiChar;
56 procedure Main();
57 var
58 sdlflags: LongWord;
59 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
60 flexloaded: Boolean;
61 {$ENDIF}
62 begin
63 e_InitWritelnDriver();
65 GetDir(0, GameDir);
66 MapsDir := GameDir + '/maps/';
67 DataDir := GameDir + '/data/';
68 ModelsDir := DataDir + 'models/';
69 GameWAD := DataDir + 'Game.wad';
71 e_InitLog(GameDir + '/' + LOG_FILENAME, TWriteMode.WM_NEWFILE);
73 e_WriteLog(
74 'Doom 2D: Forever version ' + GAME_VERSION +
75 ' proto ' + IntToStr(NET_PROTOCOL_VER),
76 TMsgType.Notify
77 );
78 e_WriteLog(
79 'Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME,
80 TMsgType.Notify
81 );
83 {$IFDEF HEADLESS}
84 conbufDumpToStdOut := true;
85 {$ENDIF}
86 e_WriteToStdOut := False; //{$IFDEF HEADLESS}True;{$ELSE}False;{$ENDIF}
88 {$IFDEF HEADLESS}
89 {$IFDEF USE_SDLMIXER}
90 sdlflags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
91 // HACK: shit this into env and hope for the best
92 SetEnvVar('SDL_AUDIODRIVER', 'dummy');
93 {$ELSE}
94 sdlflags := SDL_INIT_TIMER or $00004000;
95 {$ENDIF}
96 {$ELSE}
97 {$IFDEF USE_SDLMIXER}
98 {*sdlflags := SDL_INIT_EVERYTHING;*}
99 sdlflags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
100 {$ELSE}
101 sdlflags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
102 {$ENDIF}
103 {$ENDIF}
105 SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, '0');
107 if SDL_Init(sdlflags) < 0 then
108 raise Exception.Create('SDL: Init failed: ' + SDL_GetError());
110 e_WriteLog('Read config file', TMsgType.Notify);
111 g_Options_Read(GameDir + '/' + CONFIG_FILENAME);
113 //GetSystemDefaultLCID()
115 //e_WriteLog('Read language file', MSG_NOTIFY);
116 //g_Language_Load(DataDir + gLanguage + '.txt');
117 e_WriteLog(gLanguage, TMsgType.Notify);
118 g_Language_Set(gLanguage);
120 {$IFNDEF HEADLESS}
121 {$IFNDEF ANDROID}
122 SDL_StartTextInput();
123 {$ENDIF}
124 {$ENDIF}
126 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
127 flexloaded := true;
128 if not fuiAddWad('flexui.wad') then
129 begin
130 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
131 end;
132 try
133 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
134 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
135 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
136 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
137 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
138 except on e: Exception do
139 begin
140 writeln('ERROR loading FlexUI fonts');
141 flexloaded := false;
142 //raise;
143 end;
144 else
145 begin
146 flexloaded := false;
147 //raise;
148 end;
149 end;
150 if (flexloaded) then
151 begin
152 try
153 e_LogWriteln('FlexUI: loading stylesheet...');
154 uiLoadStyles('flexui/widgets.wgs');
155 except on e: TParserException do
156 begin
157 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
158 //raise;
159 flexloaded := false;
160 end;
161 else
162 begin
163 //raise;
164 flexloaded := false;
165 end;
166 end;
167 end;
168 g_holmes_imfunctional := not flexloaded;
169 {$ENDIF}
171 e_WriteLog('Entering SDLMain', TMsgType.Notify);
173 {$WARNINGS OFF}
174 SDLMain();
175 {$WARNINGS ON}
177 {$IFNDEF HEADLESS}
178 SDL_StopTextInput();
179 {$ENDIF}
181 e_WriteLog('Releasing SDL', TMsgType.Notify);
182 SDL_Quit();
183 end;
185 procedure Init();
186 var
187 NoSound: Boolean;
188 begin
189 Randomize;
191 {$IFDEF HEADLESS}
192 {$IFDEF USE_SDLMIXER}
193 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
194 {$ELSE}
195 NoSound := True; // FMOD backend will sort it out
196 {$ENDIF}
197 {$ELSE}
198 NoSound := False;
199 {$ENDIF}
201 e_WriteLog('Init Input', TMsgType.Notify);
202 e_InitInput();
203 g_Touch_Init;
205 if (e_JoysticksAvailable > 0) then
206 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
207 else
208 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
210 if (not gNoSound) then
211 begin
212 e_WriteLog('Initializing sound system', TMsgType.Notify);
213 e_InitSoundSystem(NoSound);
214 end;
216 e_WriteLog('Init game', TMsgType.Notify);
217 g_Game_Init();
219 FillChar(charbuff, sizeof(charbuff), ' ');
220 end;
223 procedure Release();
224 begin
225 e_WriteLog('Releasing engine', TMsgType.Notify);
226 e_ReleaseEngine();
228 e_WriteLog('Releasing Input', TMsgType.Notify);
229 e_ReleaseInput();
231 if not gNoSound then
232 begin
233 e_WriteLog('Releasing FMOD', TMsgType.Notify);
234 e_ReleaseSoundSystem();
235 end;
236 end;
239 procedure Update ();
240 begin
241 g_Game_Update();
242 end;
245 procedure Draw ();
246 begin
247 g_Game_Draw();
248 end;
251 function Translit (const S: AnsiString): AnsiString;
252 var
253 i: Integer;
254 begin
255 Result := S;
256 for i := 1 to Length(Result) do
257 begin
258 case Result[i] of
259 'É': Result[i] := 'Q';
260 'Ö': Result[i] := 'W';
261 'Ó': Result[i] := 'E';
262 'Ê': Result[i] := 'R';
263 'Å': Result[i] := 'T';
264 'Í': Result[i] := 'Y';
265 'Ã': Result[i] := 'U';
266 'Ø': Result[i] := 'I';
267 'Ù': Result[i] := 'O';
268 'Ç': Result[i] := 'P';
269 'Õ': Result[i] := '['; //Chr(219);
270 'Ú': Result[i] := ']'; //Chr(221);
271 'Ô': Result[i] := 'A';
272 'Û': Result[i] := 'S';
273 'Â': Result[i] := 'D';
274 'À': Result[i] := 'F';
275 'Ï': Result[i] := 'G';
276 'Ð': Result[i] := 'H';
277 'Î': Result[i] := 'J';
278 'Ë': Result[i] := 'K';
279 'Ä': Result[i] := 'L';
280 'Æ': Result[i] := ';'; //Chr(186);
281 'Ý': Result[i] := #39; //Chr(222);
282 'ß': Result[i] := 'Z';
283 '×': Result[i] := 'X';
284 'Ñ': Result[i] := 'C';
285 'Ì': Result[i] := 'V';
286 'È': Result[i] := 'B';
287 'Ò': Result[i] := 'N';
288 'Ü': Result[i] := 'M';
289 'Á': Result[i] := ','; //Chr(188);
290 'Þ': Result[i] := '.'; //Chr(190);
291 end;
292 end;
293 end;
296 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
297 var
298 ls1, ls2: string;
299 begin
300 ls1 := CheatEng[ct];
301 ls2 := Translit(CheatRus[ct]);
302 if length(ls1) = 0 then ls1 := '~';
303 if length(ls2) = 0 then ls2 := '~';
304 result :=
305 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
306 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
307 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
308 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
310 if ct = I_GAME_CHEAT_JETPACK then
311 begin
312 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
313 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
314 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
315 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
316 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
317 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
318 end;
320 end;
323 procedure Cheat ();
324 const
325 CHEAT_DAMAGE = 500;
326 label
327 Cheated;
328 var
329 s, s2: string;
330 c: ShortString;
331 a: Integer;
332 begin
334 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
335 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
336 or g_Game_IsNet then Exit;
338 if not gGameOn then exit;
339 if not conIsCheatsEnabled then exit;
341 s := 'SOUND_GAME_RADIO';
343 //
344 if CheckCheat(I_GAME_CHEAT_GODMODE) then
345 begin
346 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
347 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
348 goto Cheated;
349 end;
350 // RAMBO
351 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
352 begin
353 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
354 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
355 goto Cheated;
356 end;
357 // TANK
358 if CheckCheat(I_GAME_CHEAT_HEALTH) then
359 begin
360 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
361 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
362 goto Cheated;
363 end;
364 // IDDQD
365 if CheckCheat(I_GAME_CHEAT_DEATH) then
366 begin
367 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
368 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
369 s := 'SOUND_MONSTER_HAHA';
370 goto Cheated;
371 end;
372 //
373 if CheckCheat(I_GAME_CHEAT_DOORS) then
374 begin
375 g_Triggers_OpenAll();
376 goto Cheated;
377 end;
378 // GOODBYE
379 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
380 begin
381 if gTriggers <> nil then
382 for a := 0 to High(gTriggers) do
383 if gTriggers[a].TriggerType = TRIGGER_EXIT then
384 begin
385 gExitByTrigger := True;
386 //g_Game_ExitLevel(gTriggers[a].Data.MapName);
387 g_Game_ExitLevel(gTriggers[a].tgcMap);
388 Break;
389 end;
390 goto Cheated;
391 end;
392 //
393 s2 := Copy(charbuff, 15, 2);
394 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
395 begin
396 if g_Map_Exist(MapsDir+gGameSettings.WAD+':\MAP'+s2) then
397 begin
398 c := 'MAP'+s2;
399 g_Game_ExitLevel(c);
400 end;
401 goto Cheated;
402 end;
403 //
404 if CheckCheat(I_GAME_CHEAT_FLY) then
405 begin
406 gFly := not gFly;
407 goto Cheated;
408 end;
409 // BULLFROG
410 if CheckCheat(I_GAME_CHEAT_JUMPS) then
411 begin
412 VEL_JUMP := 30-VEL_JUMP;
413 goto Cheated;
414 end;
415 // FORMULA1
416 if CheckCheat(I_GAME_CHEAT_SPEED) then
417 begin
418 MAX_RUNVEL := 32-MAX_RUNVEL;
419 goto Cheated;
420 end;
421 // CONDOM
422 if CheckCheat(I_GAME_CHEAT_SUIT) then
423 begin
424 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
425 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
426 goto Cheated;
427 end;
428 //
429 if CheckCheat(I_GAME_CHEAT_AIR) then
430 begin
431 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
432 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
433 goto Cheated;
434 end;
435 // PURELOVE
436 if CheckCheat(I_GAME_CHEAT_BERSERK) then
437 begin
438 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
439 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
440 goto Cheated;
441 end;
442 //
443 if CheckCheat(I_GAME_CHEAT_JETPACK) then
444 begin
445 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
446 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
447 goto Cheated;
448 end;
449 // CASPER
450 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
451 begin
452 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
453 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
454 goto Cheated;
455 end;
456 //
457 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
458 begin
459 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
460 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
461 goto Cheated;
462 end;
463 // INFERNO
464 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
465 begin
466 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
467 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
468 goto Cheated;
469 end;
470 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
471 begin
472 gAimLine := not gAimLine;
473 goto Cheated;
474 end;
475 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
476 begin
477 gShowMap := not gShowMap;
478 goto Cheated;
479 end;
480 Exit;
482 Cheated:
483 g_Sound_PlayEx(s);
484 end;
487 procedure KeyPress (K: Word);
488 var
489 Msg: g_gui.TMessage;
490 begin
491 case K of
492 IK_PAUSE: // <Pause/Break>:
493 begin
494 if (g_ActiveWindow = nil) then g_Game_Pause(not gPause);
495 end;
497 IK_BACKQUOTE, VK_CONSOLE: // <`/~/¨/¸>:
498 begin
499 g_Console_Switch();
500 end;
502 IK_ESCAPE, VK_ESCAPE: // <Esc>:
503 begin
504 if gChatShow then
505 begin
506 g_Console_Chat_Switch();
507 Exit;
508 end;
510 if gConsoleShow then
511 begin
512 g_Console_Switch();
513 end
514 else if (g_ActiveWindow <> nil) then
515 begin
516 Msg.Msg := WM_KEYDOWN;
517 Msg.WParam := IK_ESCAPE;
518 g_ActiveWindow.OnMessage(Msg);
519 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
520 end
521 else if (gState <> STATE_FOLD) then
522 begin
523 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
524 begin
525 g_Game_InGameMenu(True);
526 end
527 else if (gExit = 0) and (gState <> STATE_SLIST) then
528 begin
529 if (gState <> STATE_MENU) then
530 begin
531 if (NetMode <> NET_NONE) then
532 begin
533 g_Game_StopAllSounds(True);
534 g_Game_Free;
535 gState := STATE_MENU;
536 Exit;
537 end;
538 end;
539 g_GUI_ShowWindow('MainMenu');
540 g_Sound_PlayEx('MENU_OPEN');
541 end;
542 end;
543 end;
545 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
546 begin // <F2> .. <F6> � <F12>
547 if gGameOn and (not gConsoleShow) and (not gChatShow) then
548 begin
549 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
550 if (not g_Game_IsNet) then g_Game_Pause(True);
551 case K of
552 IK_F2: g_Menu_Show_SaveMenu();
553 IK_F3: g_Menu_Show_LoadMenu();
554 IK_F4: g_Menu_Show_GameSetGame();
555 IK_F5: g_Menu_Show_OptionsVideo();
556 IK_F6: g_Menu_Show_OptionsSound();
557 IK_F7: g_Menu_Show_EndGameMenu();
558 IK_F10: g_Menu_Show_QuitGameMenu();
559 end;
560 end;
561 end;
563 else
564 begin
565 gJustChatted := False;
566 if gConsoleShow or gChatShow then
567 begin
568 g_Console_Control(K);
569 end
570 else if (g_ActiveWindow <> nil) then
571 begin
572 Msg.Msg := WM_KEYDOWN;
573 Msg.WParam := K;
574 g_ActiveWindow.OnMessage(Msg);
575 end
576 else if (gState = STATE_MENU) then
577 begin
578 g_GUI_ShowWindow('MainMenu');
579 g_Sound_PlayEx('MENU_OPEN');
580 end;
581 end;
582 end;
583 end;
586 procedure CharPress (C: AnsiChar);
587 var
588 Msg: g_gui.TMessage;
589 a: Integer;
590 begin
591 if (not gChatShow) and ((C = '`') or (C = '~') or (C = '¸') or (C = '¨')) then Exit;
593 if gConsoleShow or gChatShow then
594 begin
595 g_Console_Char(C);
596 end
597 else if (g_ActiveWindow <> nil) then
598 begin
599 Msg.Msg := WM_CHAR;
600 Msg.WParam := Ord(C);
601 g_ActiveWindow.OnMessage(Msg);
602 end
603 else
604 begin
605 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
606 charbuff[15] := upcase1251(C);
607 Cheat();
608 end;
609 end;
612 end.