DEADSOFTWARE

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