DEADSOFTWARE

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