DEADSOFTWARE

0269a4c9315d2d700320061cd60f4b02774c4354
[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, sdlcarcass, fui_ctls, fui_wadread, fui_style, fui_gfx_gl,
42 {$ENDIF}
43 wadreader, e_log, g_window,
44 e_graphics, e_input, g_game, g_console, g_gui,
45 e_sound, g_options, g_sound, g_player, g_basic,
46 g_weapons, SysUtils, g_triggers, MAPDEF, g_map,
47 g_menu, g_language, g_net, g_touch, g_system, g_res_downloader,
48 utils, conbuf, envvars,
49 xparser;
52 var
53 charbuff: packed array [0..15] of AnsiChar;
55 procedure Main();
56 {$IFDEF ENABLE_HOLMES}
57 var flexloaded: Boolean;
58 {$ENDIF}
59 begin
60 e_InitWritelnDriver();
62 GetDir(0, GameDir);
63 MapsDir := GameDir + '/maps/';
64 DataDir := GameDir + '/data/';
65 ModelsDir := DataDir + 'models/';
66 GameWAD := DataDir + 'Game.wad';
68 e_InitLog(GameDir + '/' + LogFileName, TWriteMode.WM_NEWFILE);
70 e_WriteLog(
71 'Doom 2D: Forever version ' + GAME_VERSION +
72 ' proto ' + IntToStr(NET_PROTOCOL_VER),
73 TMsgType.Notify
74 );
75 e_WriteLog(
76 'Build date: ' + GAME_BUILDDATE + ' ' + GAME_BUILDTIME,
77 TMsgType.Notify
78 );
80 {$IFDEF HEADLESS}
81 conbufDumpToStdOut := true;
82 {$ENDIF}
83 e_WriteToStdOut := False; //{$IFDEF HEADLESS}True;{$ELSE}False;{$ENDIF}
85 e_InitInput;
87 sys_Init;
88 g_Options_Read(GameDir + '/' + CONFIG_FILENAME);
89 if sys_SetDisplayMode(gScreenWidth, gScreenHeight, gBPP, gFullScreen) = False then
90 raise Exception.Create('Failed to set videomode on startup.');
92 g_Console_SysInit;
93 e_WriteLog(gLanguage, TMsgType.Notify);
94 g_Language_Set(gLanguage);
96 {$IF not DEFINED(HEADLESS) and DEFINED(ENABLE_HOLMES)}
97 flexloaded := true;
98 if not fuiAddWad('flexui.wad') then
99 begin
100 if not fuiAddWad('./data/flexui.wad') then fuiAddWad('./flexui.wad');
101 end;
102 try
103 fuiGfxLoadFont('win8', 'flexui/fonts/win8.fuifont');
104 fuiGfxLoadFont('win14', 'flexui/fonts/win14.fuifont');
105 fuiGfxLoadFont('win16', 'flexui/fonts/win16.fuifont');
106 fuiGfxLoadFont('dos8', 'flexui/fonts/dos8.fuifont');
107 fuiGfxLoadFont('msx6', 'flexui/fonts/msx6.fuifont');
108 except on e: Exception do
109 begin
110 writeln('ERROR loading FlexUI fonts');
111 flexloaded := false;
112 //raise;
113 end;
114 else
115 begin
116 flexloaded := false;
117 //raise;
118 end;
119 end;
120 if (flexloaded) then
121 begin
122 try
123 e_LogWriteln('FlexUI: loading stylesheet...');
124 uiLoadStyles('flexui/widgets.wgs');
125 except on e: TParserException do
126 begin
127 writeln('ERROR at (', e.tokLine, ',', e.tokCol, '): ', e.message);
128 //raise;
129 flexloaded := false;
130 end;
131 else
132 begin
133 //raise;
134 flexloaded := false;
135 end;
136 end;
137 end;
138 g_holmes_imfunctional := not flexloaded;
140 if (not g_holmes_imfunctional) then
141 begin
142 uiInitialize();
143 uiContext.font := 'win14';
144 end;
146 if assigned(oglInitCB) then oglInitCB;
147 {$ENDIF}
149 //g_Res_CreateDatabases(); // it will be done before connecting to the server for the first time
151 e_WriteLog('Entering SDLMain', TMsgType.Notify);
153 {$WARNINGS OFF}
154 SDLMain();
155 {$WARNINGS ON}
157 {$IFDEF ENABLE_HOLMES}
158 if assigned(oglDeinitCB) then oglDeinitCB;
159 {$ENDIF}
161 sys_Final;
162 end;
164 procedure Init();
165 var
166 NoSound: Boolean;
167 begin
168 Randomize;
170 {$IFDEF HEADLESS}
171 {$IFDEF USE_SDLMIXER}
172 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
173 {$ELSE}
174 NoSound := True; // FMOD backend will sort it out
175 {$ENDIF}
176 {$ELSE}
177 NoSound := False;
178 {$ENDIF}
180 g_Touch_Init;
182 (*
183 if (e_JoysticksAvailable > 0) then
184 e_WriteLog('Input: Joysticks available.', TMsgType.Notify)
185 else
186 e_WriteLog('Input: No Joysticks.', TMsgType.Notify);
187 *)
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 := 'MAP'+s2;
378 g_Game_ExitLevel(c);
379 end;
380 goto Cheated;
381 end;
382 //
383 if CheckCheat(I_GAME_CHEAT_FLY) then
384 begin
385 gFly := not gFly;
386 goto Cheated;
387 end;
388 // BULLFROG
389 if CheckCheat(I_GAME_CHEAT_JUMPS) then
390 begin
391 VEL_JUMP := 30-VEL_JUMP;
392 goto Cheated;
393 end;
394 // FORMULA1
395 if CheckCheat(I_GAME_CHEAT_SPEED) then
396 begin
397 MAX_RUNVEL := 32-MAX_RUNVEL;
398 goto Cheated;
399 end;
400 // CONDOM
401 if CheckCheat(I_GAME_CHEAT_SUIT) then
402 begin
403 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
404 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
405 goto Cheated;
406 end;
407 //
408 if CheckCheat(I_GAME_CHEAT_AIR) then
409 begin
410 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
411 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
412 goto Cheated;
413 end;
414 // PURELOVE
415 if CheckCheat(I_GAME_CHEAT_BERSERK) then
416 begin
417 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
418 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
419 goto Cheated;
420 end;
421 //
422 if CheckCheat(I_GAME_CHEAT_JETPACK) then
423 begin
424 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
425 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
426 goto Cheated;
427 end;
428 // CASPER
429 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
430 begin
431 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
432 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
433 goto Cheated;
434 end;
435 //
436 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
437 begin
438 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
439 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
440 goto Cheated;
441 end;
442 // INFERNO
443 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
444 begin
445 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
446 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
447 goto Cheated;
448 end;
449 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
450 begin
451 gAimLine := not gAimLine;
452 goto Cheated;
453 end;
454 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
455 begin
456 gShowMap := not gShowMap;
457 goto Cheated;
458 end;
459 Exit;
461 Cheated:
462 g_Sound_PlayEx(s);
463 end;
466 procedure KeyPress (K: Word);
467 {$IFNDEF HEADLESS}
468 var
469 Msg: g_gui.TMessage;
470 {$ENDIF}
471 begin
472 {$IFNDEF HEADLESS}
473 case K of
474 VK_ESCAPE: // <Esc>:
475 begin
476 if (g_ActiveWindow <> nil) then
477 begin
478 Msg.Msg := WM_KEYDOWN;
479 Msg.WParam := VK_ESCAPE;
480 g_ActiveWindow.OnMessage(Msg);
481 if (not g_Game_IsNet) and (g_ActiveWindow = nil) then g_Game_Pause(false); //Fn loves to do this
482 end
483 else if (gState <> STATE_FOLD) then
484 begin
485 if gGameOn or (gState = STATE_INTERSINGLE) or (gState = STATE_INTERCUSTOM) then
486 begin
487 g_Game_InGameMenu(True);
488 end
489 else if (gExit = 0) and (gState <> STATE_SLIST) then
490 begin
491 if (gState <> STATE_MENU) then
492 begin
493 if (NetMode <> NET_NONE) then
494 begin
495 g_Game_StopAllSounds(True);
496 g_Game_Free;
497 gState := STATE_MENU;
498 Exit;
499 end;
500 end;
501 g_GUI_ShowWindow('MainMenu');
502 g_Sound_PlayEx('MENU_OPEN');
503 end;
504 end;
505 end;
507 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
508 begin // <F2> .. <F6> � <F12>
509 if gGameOn and (not gConsoleShow) and (not gChatShow) then
510 begin
511 while (g_ActiveWindow <> nil) do g_GUI_HideWindow(False);
512 if (not g_Game_IsNet) then g_Game_Pause(True);
513 case K of
514 IK_F2: g_Menu_Show_SaveMenu();
515 IK_F3: g_Menu_Show_LoadMenu();
516 IK_F4: g_Menu_Show_GameSetGame();
517 IK_F5: g_Menu_Show_OptionsVideo();
518 IK_F6: g_Menu_Show_OptionsSound();
519 IK_F7: g_Menu_Show_EndGameMenu();
520 IK_F10: g_Menu_Show_QuitGameMenu();
521 end;
522 end;
523 end;
525 else
526 begin
527 gJustChatted := False;
528 if gConsoleShow or gChatShow then
529 begin
530 g_Console_Control(K);
531 end
532 else if (g_ActiveWindow <> nil) then
533 begin
534 Msg.Msg := WM_KEYDOWN;
535 Msg.WParam := K;
536 g_ActiveWindow.OnMessage(Msg);
537 end
538 else if (gState = STATE_MENU) then
539 begin
540 g_GUI_ShowWindow('MainMenu');
541 g_Sound_PlayEx('MENU_OPEN');
542 end;
543 end;
544 end;
545 {$ENDIF}
546 end;
549 procedure CharPress (C: AnsiChar);
550 var
551 Msg: g_gui.TMessage;
552 a: Integer;
553 begin
554 if gConsoleShow or gChatShow then
555 begin
556 g_Console_Char(C)
557 end
558 else if (g_ActiveWindow <> nil) then
559 begin
560 Msg.Msg := WM_CHAR;
561 Msg.WParam := Ord(C);
562 g_ActiveWindow.OnMessage(Msg);
563 end
564 else
565 begin
566 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
567 charbuff[15] := upcase1251(C);
568 Cheat();
569 end;
570 end;
573 end.