DEADSOFTWARE

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