DEADSOFTWARE

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