DEADSOFTWARE

buffer network messages (broke a lot of shit)
[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 g_amodes.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: Char);
29 var
30 GameDir: string;
31 DataDir: string;
32 MapsDir: string;
33 ModelsDir: string;
34 GameWAD: string;
36 implementation
38 uses
39 SDL2, GL, GLExt, wadreader, e_log, g_window,
40 e_graphics, e_input, g_game, g_console, g_gui,
41 e_sound, g_options, g_sound, g_player,
42 g_weapons, SysUtils, g_triggers, MAPDEF, g_map,
43 MAPSTRUCT, g_menu, g_language, g_net,
44 utils, conbuf, envvars;
46 var
47 charbuff: Array [0..15] of Char;
49 procedure Main();
50 var
51 sdlflags: LongWord;
52 begin
53 e_InitWritelnDriver();
55 GetDir(0, GameDir);
56 MapsDir := GameDir + '/maps/';
57 DataDir := GameDir + '/data/';
58 ModelsDir := DataDir + 'models/';
59 GameWAD := DataDir + 'Game.wad';
61 e_InitLog(GameDir + '/' + LOG_FILENAME, WM_NEWFILE);
63 e_WriteLog('Read config file', MSG_NOTIFY);
64 g_Options_Read(GameDir + '/' + CONFIG_FILENAME);
66 {$IFDEF HEADLESS}
67 conbufDumpToStdOut := true;
68 {$ENDIF}
69 e_WriteToStdOut := False; //{$IFDEF HEADLESS}True;{$ELSE}False;{$ENDIF}
71 //GetSystemDefaultLCID()
73 //e_WriteLog('Read language file', MSG_NOTIFY);
74 //g_Language_Load(DataDir + gLanguage + '.txt');
75 e_WriteLog(gLanguage, MSG_NOTIFY);
76 g_Language_Set(gLanguage);
78 {$IFDEF HEADLESS}
79 {$IFDEF USE_SDLMIXER}
80 sdlflags := SDL_INIT_TIMER or SDL_INIT_AUDIO or $00004000;
81 // HACK: shit this into env and hope for the best
82 SetEnvVar('SDL_AUDIODRIVER', 'dummy');
83 {$ELSE}
84 sdlflags := SDL_INIT_TIMER or $00004000;
85 {$ENDIF}
86 {$ELSE}
87 {$IFDEF USE_SDLMIXER}
88 sdlflags := SDL_INIT_EVERYTHING;
89 {$ELSE}
90 sdlflags := SDL_INIT_JOYSTICK or SDL_INIT_TIMER or SDL_INIT_VIDEO;
91 {$ENDIF}
92 {$ENDIF}
93 if SDL_Init(sdlflags) < 0 then
94 raise Exception.Create('SDL: Init failed: ' + SDL_GetError());
96 {$IFDEF HEADLESS}
97 SDL_StartTextInput();
98 {$ENDIF}
100 e_WriteLog('Entering SDLMain', MSG_NOTIFY);
102 {$WARNINGS OFF}
103 SDLMain();
104 {$WARNINGS ON}
106 {$IFDEF HEADLESS}
107 SDL_StopTextInput();
108 {$ENDIF}
110 e_WriteLog('Releasing SDL', MSG_NOTIFY);
111 SDL_Quit();
112 end;
114 procedure Init();
115 var
116 a: Integer;
117 NoSound: Boolean;
118 begin
119 Randomize;
121 {$IFDEF HEADLESS}
122 {$IFDEF USE_SDLMIXER}
123 NoSound := False; // hope env has set SDL_AUDIODRIVER to dummy
124 {$ELSE}
125 NoSound := True; // FMOD backend will sort it out
126 {$ENDIF}
127 {$ELSE}
128 NoSound := False;
129 {$ENDIF}
131 e_WriteLog('Init Input', MSG_NOTIFY);
132 e_InitInput();
134 if (e_JoysticksAvailable > 0) then
135 e_WriteLog('Input: Joysticks available.', MSG_NOTIFY)
136 else
137 e_WriteLog('Input: No Joysticks.', MSG_NOTIFY);
139 if (not gNoSound) then
140 begin
141 e_WriteLog('Initializing sound system', MSG_NOTIFY);
142 e_InitSoundSystem(NoSound);
143 end;
145 e_WriteLog('Init game', MSG_NOTIFY);
146 g_Game_Init();
148 for a := 0 to 15 do charbuff[a] := ' ';
149 end;
151 procedure Release();
152 begin
153 e_WriteLog('Releasing engine', MSG_NOTIFY);
154 e_ReleaseEngine();
156 e_WriteLog('Releasing Input', MSG_NOTIFY);
157 e_ReleaseInput();
159 if not gNoSound then
160 begin
161 e_WriteLog('Releasing FMOD', MSG_NOTIFY);
162 e_ReleaseSoundSystem();
163 end;
164 end;
166 procedure Update();
167 begin
168 g_Game_Update();
169 end;
171 procedure Draw();
172 begin
173 g_Game_Draw();
174 end;
176 function Translit(S: String): String;
177 var
178 i: Integer;
179 begin
180 Result := S;
181 for i := 1 to Length(Result) do
182 case Result[i] of
183 'É': Result[i] := 'Q';
184 'Ö': Result[i] := 'W';
185 'Ó': Result[i] := 'E';
186 'Ê': Result[i] := 'R';
187 'Å': Result[i] := 'T';
188 'Í': Result[i] := 'Y';
189 'Ã': Result[i] := 'U';
190 'Ø': Result[i] := 'I';
191 'Ù': Result[i] := 'O';
192 'Ç': Result[i] := 'P';
193 'Õ': Result[i] := '['; //Chr(219);
194 'Ú': Result[i] := ']'; //Chr(221);
195 'Ô': Result[i] := 'A';
196 'Û': Result[i] := 'S';
197 'Â': Result[i] := 'D';
198 'À': Result[i] := 'F';
199 'Ï': Result[i] := 'G';
200 'Ð': Result[i] := 'H';
201 'Î': Result[i] := 'J';
202 'Ë': Result[i] := 'K';
203 'Ä': Result[i] := 'L';
204 'Æ': Result[i] := ';'; //Chr(186);
205 'Ý': Result[i] := #39; //Chr(222);
206 'ß': Result[i] := 'Z';
207 '×': Result[i] := 'X';
208 'Ñ': Result[i] := 'C';
209 'Ì': Result[i] := 'V';
210 'È': Result[i] := 'B';
211 'Ò': Result[i] := 'N';
212 'Ü': Result[i] := 'M';
213 'Á': Result[i] := ','; //Chr(188);
214 'Þ': Result[i] := '.'; //Chr(190);
215 end;
216 end;
219 function CheckCheat (ct: TStrings_Locale; eofs: Integer=0): Boolean;
220 var
221 ls1, ls2: string;
222 begin
223 ls1 := CheatEng[ct];
224 ls2 := Translit(CheatRus[ct]);
225 if length(ls1) = 0 then ls1 := '~';
226 if length(ls2) = 0 then ls2 := '~';
227 result :=
228 (Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)) = ls1) or
229 (Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))) = ls1) or
230 (Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)) = ls2) or
231 (Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))) = ls2);
233 if ct = I_GAME_CHEAT_JETPACK then
234 begin
235 e_WriteLog('ls1: ['+ls1+']', MSG_NOTIFY);
236 e_WriteLog('ls2: ['+ls2+']', MSG_NOTIFY);
237 e_WriteLog('bf0: ['+Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1))+']', MSG_NOTIFY);
238 e_WriteLog('bf1: ['+Translit(Copy(charbuff, 17-Length(ls1)-eofs, Length(ls1)))+']', MSG_NOTIFY);
239 e_WriteLog('bf2: ['+Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2))+']', MSG_NOTIFY);
240 e_WriteLog('bf3: ['+Translit(Copy(charbuff, 17-Length(ls2)-eofs, Length(ls2)))+']', MSG_NOTIFY);
241 end;
243 end;
246 procedure Cheat();
247 const
248 CHEAT_DAMAGE = 500;
249 label
250 Cheated;
251 var
252 s, s2: string;
253 c: Char16;
254 a: Integer;
255 begin
256 if (not gGameOn) or (not gCheats) or ((gGameSettings.GameType <> GT_SINGLE) and
257 (gGameSettings.GameMode <> GM_COOP) and (not gDebugMode))
258 or g_Game_IsNet then Exit;
260 s := 'SOUND_GAME_RADIO';
262 //
263 if CheckCheat(I_GAME_CHEAT_GODMODE) then
264 begin
265 if gPlayer1 <> nil then gPlayer1.GodMode := not gPlayer1.GodMode;
266 if gPlayer2 <> nil then gPlayer2.GodMode := not gPlayer2.GodMode;
267 goto Cheated;
268 end;
269 // RAMBO
270 if CheckCheat(I_GAME_CHEAT_WEAPONS) then
271 begin
272 if gPlayer1 <> nil then gPlayer1.AllRulez(False);
273 if gPlayer2 <> nil then gPlayer2.AllRulez(False);
274 goto Cheated;
275 end;
276 // TANK
277 if CheckCheat(I_GAME_CHEAT_HEALTH) then
278 begin
279 if gPlayer1 <> nil then gPlayer1.AllRulez(True);
280 if gPlayer2 <> nil then gPlayer2.AllRulez(True);
281 goto Cheated;
282 end;
283 // IDDQD
284 if CheckCheat(I_GAME_CHEAT_DEATH) then
285 begin
286 if gPlayer1 <> nil then gPlayer1.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
287 if gPlayer2 <> nil then gPlayer2.Damage(CHEAT_DAMAGE, 0, 0, 0, HIT_TRAP);
288 s := 'SOUND_MONSTER_HAHA';
289 goto Cheated;
290 end;
291 //
292 if CheckCheat(I_GAME_CHEAT_DOORS) then
293 begin
294 g_Triggers_OpenAll();
295 goto Cheated;
296 end;
297 // GOODBYE
298 if CheckCheat(I_GAME_CHEAT_NEXTMAP) then
299 begin
300 if gTriggers <> nil then
301 for a := 0 to High(gTriggers) do
302 if gTriggers[a].TriggerType = TRIGGER_EXIT then
303 begin
304 gExitByTrigger := True;
305 g_Game_ExitLevel(gTriggers[a].Data.MapName);
306 Break;
307 end;
308 goto Cheated;
309 end;
310 //
311 s2 := Copy(charbuff, 15, 2);
312 if CheckCheat(I_GAME_CHEAT_CHANGEMAP, 2) and (s2[1] >= '0') and (s2[1] <= '9') and (s2[2] >= '0') and (s2[2] <= '9') then
313 begin
314 if g_Map_Exist(MapsDir+gGameSettings.WAD+':\MAP'+s2) then
315 begin
316 c := 'MAP00';
317 c[3] := s2[1];
318 c[4] := s2[2];
319 g_Game_ExitLevel(c);
320 end;
321 goto Cheated;
322 end;
323 //
324 if CheckCheat(I_GAME_CHEAT_FLY) then
325 begin
326 gFly := not gFly;
327 goto Cheated;
328 end;
329 // BULLFROG
330 if CheckCheat(I_GAME_CHEAT_JUMPS) then
331 begin
332 VEL_JUMP := 30-VEL_JUMP;
333 goto Cheated;
334 end;
335 // FORMULA1
336 if CheckCheat(I_GAME_CHEAT_SPEED) then
337 begin
338 MAX_RUNVEL := 32-MAX_RUNVEL;
339 goto Cheated;
340 end;
341 // CONDOM
342 if CheckCheat(I_GAME_CHEAT_SUIT) then
343 begin
344 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_SUIT);
345 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_SUIT);
346 goto Cheated;
347 end;
348 //
349 if CheckCheat(I_GAME_CHEAT_AIR) then
350 begin
351 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_OXYGEN);
352 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_OXYGEN);
353 goto Cheated;
354 end;
355 // PURELOVE
356 if CheckCheat(I_GAME_CHEAT_BERSERK) then
357 begin
358 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_MEDKIT_BLACK);
359 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_MEDKIT_BLACK);
360 goto Cheated;
361 end;
362 //
363 if CheckCheat(I_GAME_CHEAT_JETPACK) then
364 begin
365 if gPlayer1 <> nil then gPlayer1.GiveItem(ITEM_JETPACK);
366 if gPlayer2 <> nil then gPlayer2.GiveItem(ITEM_JETPACK);
367 goto Cheated;
368 end;
369 // CASPER
370 if CheckCheat(I_GAME_CHEAT_NOCLIP) then
371 begin
372 if gPlayer1 <> nil then gPlayer1.SwitchNoClip;
373 if gPlayer2 <> nil then gPlayer2.SwitchNoClip;
374 goto Cheated;
375 end;
376 //
377 if CheckCheat(I_GAME_CHEAT_NOTARGET) then
378 begin
379 if gPlayer1 <> nil then gPlayer1.NoTarget := not gPlayer1.NoTarget;
380 if gPlayer2 <> nil then gPlayer2.NoTarget := not gPlayer2.NoTarget;
381 goto Cheated;
382 end;
383 // INFERNO
384 if CheckCheat(I_GAME_CHEAT_NORELOAD) then
385 begin
386 if gPlayer1 <> nil then gPlayer1.NoReload := not gPlayer1.NoReload;
387 if gPlayer2 <> nil then gPlayer2.NoReload := not gPlayer2.NoReload;
388 goto Cheated;
389 end;
390 if CheckCheat(I_GAME_CHEAT_AIMLINE) then
391 begin
392 gAimLine := not gAimLine;
393 goto Cheated;
394 end;
395 if CheckCheat(I_GAME_CHEAT_AUTOMAP) then
396 begin
397 gShowMap := not gShowMap;
398 goto Cheated;
399 end;
400 Exit;
402 Cheated:
403 g_Sound_PlayEx(s);
404 end;
406 procedure KeyPress(K: Word);
407 var
408 Msg: g_gui.TMessage;
409 begin
410 case K of
411 IK_PAUSE: // <Pause/Break>:
412 begin
413 if (g_ActiveWindow = nil) then
414 g_Game_Pause(not gPause);
415 end;
417 IK_BACKQUOTE: // <`/~/¨/¸>:
418 begin
419 g_Console_Switch();
420 end;
422 IK_ESCAPE: // <Esc>:
423 begin
424 if gChatShow then
425 begin
426 g_Console_Chat_Switch();
427 Exit;
428 end;
430 if gConsoleShow then
431 g_Console_Switch()
432 else
433 if g_ActiveWindow <> nil then
434 begin
435 Msg.Msg := WM_KEYDOWN;
436 Msg.WParam := IK_ESCAPE;
437 g_ActiveWindow.OnMessage(Msg);
438 end
439 else
440 if gState <> STATE_FOLD then
441 if gGameOn
442 or (gState = STATE_INTERSINGLE)
443 or (gState = STATE_INTERCUSTOM)
444 then
445 g_Game_InGameMenu(True)
446 else
447 if (gExit = 0) and (gState <> STATE_SLIST) then
448 begin
449 if gState <> STATE_MENU then
450 if NetMode <> NET_NONE then
451 begin
452 g_Game_StopAllSounds(True);
453 g_Game_Free;
454 gState := STATE_MENU;
455 Exit;
456 end;
458 g_GUI_ShowWindow('MainMenu');
459 g_Sound_PlayEx('MENU_OPEN');
460 end;
461 end;
463 IK_F2, IK_F3, IK_F4, IK_F5, IK_F6, IK_F7, IK_F10:
464 begin // <F2> .. <F6> � <F12>
465 if gGameOn and (not gConsoleShow) and (not gChatShow) then
466 begin
467 while g_ActiveWindow <> nil do
468 g_GUI_HideWindow(False);
470 if (not g_Game_IsNet) then
471 g_Game_Pause(True);
473 case K of
474 IK_F2:
475 g_Menu_Show_SaveMenu();
476 IK_F3:
477 g_Menu_Show_LoadMenu();
478 IK_F4:
479 g_Menu_Show_GameSetGame();
480 IK_F5:
481 g_Menu_Show_OptionsVideo();
482 IK_F6:
483 g_Menu_Show_OptionsSound();
484 IK_F7:
485 g_Menu_Show_EndGameMenu();
486 IK_F10:
487 g_Menu_Show_QuitGameMenu();
488 end;
489 end;
490 end;
492 else
493 begin
494 gJustChatted := False;
495 if gConsoleShow or gChatShow then
496 g_Console_Control(K)
497 else
498 if g_ActiveWindow <> nil then
499 begin
500 Msg.Msg := WM_KEYDOWN;
501 Msg.WParam := K;
502 g_ActiveWindow.OnMessage(Msg);
503 end
504 else
505 begin
506 if (gState = STATE_MENU) then
507 begin
508 g_GUI_ShowWindow('MainMenu');
509 g_Sound_PlayEx('MENU_OPEN');
510 end;
511 end;
512 end;
513 end;
514 end;
516 procedure CharPress(C: Char);
517 var
518 Msg: g_gui.TMessage;
519 a: Integer;
520 begin
521 if (not gChatShow) and ((C = '`') or (C = '~') or (C = '¸') or (C = '¨')) then
522 Exit;
524 if gConsoleShow or gChatShow then
525 g_Console_Char(C)
526 else
527 if g_ActiveWindow <> nil then
528 begin
529 Msg.Msg := WM_CHAR;
530 Msg.WParam := Ord(C);
531 g_ActiveWindow.OnMessage(Msg);
532 end
533 else
534 begin
535 for a := 0 to 14 do charbuff[a] := charbuff[a+1];
536 charbuff[15] := UpCase1251(C);
537 Cheat();
538 end;
539 end;
541 end.