DEADSOFTWARE

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