Debian Codegraph @

Descent3/descent.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/descent.cpp $
21 * $Revision: 106 $
22 * $Date: 3/20/00 12:05p $
23 * $Author: Matt $
24 *
25 * The Descent III main loop
26 *
27 * $Log: /DescentIII/main/descent.cpp $
28 *
29 * 106 3/20/00 12:05p Matt
30 * Merge of Duane's post-1.3 changes.
31 * Misc Mac stuff, esp. GameRanger (Mac only)
32 *
33 * 105 11/04/99 12:35a Chris
34 * Added support for Merc
35 *
36 * 104 10/27/99 10:26a Matt
37 * Fixed bug from Mac merge
38 *
39 * 103 10/22/99 10:40p Matt
40 * Fixed editing error
41 *
42 * 102 10/22/99 1:21p Matt
43 * Mac merge
44 *
45 * 101 10/12/99 11:00a Jeff
46 * added -intro command line parameter to override intro movie filename
47 *
48 * 100 8/23/99 5:12p Kevin
49 * Proxy support
50 *
51 * 99 7/27/99 9:50a Kevin
52 * More DVD goodies
53 *
54 * 98 7/20/99 12:59p Jason
55 * added auto katmai support
56 *
57 * 97 7/12/99 6:43p Jeff
58 * delete lock file in temp directory after when exiting
59 *
60 * 96 6/03/99 8:48a Kevin
61 * fixes for new OEM version....
62 *
63 * 95 5/21/99 10:05a Kevin
64 * Added stuff for a future DVD version
65 *
66 * 94 5/13/99 9:46a Kevin
67 * duh... not .cpp, .h!
68 *
69 * 93 5/13/99 9:34a Kevin
70 * call FreeMultiDLL() on quit to delete temp dll files.
71 *
72 * 92 5/12/99 2:23p Jeff
73 * Descent3 now has a setable temp directory for all temp files
74 *
75 * 91 5/10/99 10:21p Ardussi
76 * changes to compile on Mac
77 *
78 * 90 5/02/99 7:27p Samir
79 * a debug option, make it so -moviedir works for the dolby and intro too.
80 *
81 * 89 5/01/99 8:17p Jeff
82 * ifdef some windows specific code for Linux
83 *
84 * 88 4/27/99 9:28p Jason
85 * FROM SAMIR: Undid some stuff I broke in the last rev
86 *
87 * 87 4/27/99 6:30p Samir
88 * play dolby and intro in progression, changed some of the code a bit, it
89 * doesn't seem to break anything.
90 *
91 * 86 4/27/99 11:38a Kevin
92 * added code to play dolby1.mve
93 *
94 * 85 4/26/99 11:45a Kevin
95 * render a blank screen if prompting for a CD and there is no callback
96 * currently
97 *
98 * 84 4/25/99 6:14p Kevin
99 * added "-timetest file.dem" to behave like gamegauge does
100 *
101 * 83 4/25/99 2:19p Kevin
102 * main mission voice moved
103 *
104 * 82 4/21/99 2:15p Samir
105 * table file filter fixes.
106 *
107 * 81 4/18/99 4:29p Jeff
108 * fixed comparison of string with '\0' instead of NULL
109 *
110 * 80 4/18/99 1:56p Kevin
111 * Movie player fixes to avoid crashes running under the editor
112 *
113 * 79 4/17/99 6:15p Samir
114 * replaced gr.h with grdefs.h and fixed resulting compile bugs.
115 *
116 * 78 4/17/99 3:44p Kevin
117 * Demo2 changes & fixes
118 *
119 * 77 4/15/99 9:25a Kevin
120 * CD check fixes and updates
121 *
122 * 76 4/14/99 3:07p Kevin
123 * Force main mission to list even on small installations
124 *
125 * 75 4/14/99 2:50a Jeff
126 * fixed some case mismatched #includes
127 *
128 * 74 4/13/99 3:30p Samir
129 * changed call SetScreenMode(SM_CINEMATIC) to SM_MENU so UI works when
130 * the game starts.
131 *
132 * 73 4/09/99 2:38p Kevin
133 * Changed where on the CD D3 will look for files
134 *
135 * 72 4/02/99 7:04p Kevin
136 * Added splash screens do release builds
137 *
138 * 71 4/02/99 5:08p Matt
139 * Added intro movie.
140 *
141 * 70 3/31/99 3:04p Jeff
142 * added _real_ temporary credits system...
143 *
144 * 69 3/29/99 7:29p Jason
145 * made renderer handle default resolution more gracefully
146 *
147 * 68 3/22/99 6:26p Matt
148 * Cleaned up error handling in cfile and editor level loads.
149 *
150 * 67 3/19/99 4:08p Kevin
151 * Multiple CD installation support
152 *
153 * 66 3/18/99 2:33p Jeff
154 * fixed bug clearing the Current pilot stuff before we were done using it
155 *
156 * 65 3/15/99 4:32p Jeff
157 * fixed 'fake' memory leak with Current pilot
158 *
159 * 64 3/11/99 5:02p Samir
160 * set ui screen mode after showing static screen.
161 *
162 * 63 3/08/99 3:59p Kevin
163 * Idle processing of network packets fixed
164 *
165 * 62 3/01/99 9:51p Jeff
166 * fixed bad prototype
167 *
168 * 61 3/01/99 9:03p Kevin
169 * OEM Beta 4
170 *
171 * 60 2/26/99 2:11a Samir
172 * do render GameFrame with app not active.
173 *
174 * 59 2/25/99 8:54p Jeff
175 * Inventory supports level change persistant items. Inventory supports
176 * time-out objects. Inventory Reset changed (takes a level of reset
177 * now). Quad lasers stay across level change (single player). Guidebot
178 * bug fixed (now back in ship on level start). Quads time out when
179 * spewed. Invulnerability and cloak powerups no longer use game
180 * event/ms, so they can be saved in game saves (moved to
181 * MakePlayerInvulnerable and MakeObjectInvisible)
182 *
183 * 58 2/21/99 6:38p Samir
184 * mouse and key input better. buffered mouse.
185 *
186 * 57 2/20/99 2:31p Kevin
187 * Made multiplayer DLLs return to the game list after a game.
188 *
189 * 56 2/13/99 12:32a Jeff
190 * inventory reset takes a different parameter
191 *
192 * 55 2/10/99 4:38p Matt
193 * Misc. changes for table file parsing
194 *
195 * 54 2/09/99 1:29a Jeff
196 * added code to let D3 process multiplayer games in the background if
197 * game is not in focus.
198 *
199 * 53 2/05/99 7:23p Kevin
200 * OEM Changes
201 *
202 * 52 2/04/99 9:46a Kevin
203 * OEM version changes
204 *
205 * 51 1/15/99 7:16p Kevin
206 * Added GameGauge Configuration & code
207 *
208 * 50 12/03/98 10:23a Jason
209 * fixed black background problem with D3D
210 *
211 * 49 10/28/98 4:48p Kevin
212 * Cleaned up inventory usage
213 *
214 * 48 10/28/98 12:27p Kevin
215 * Shutdown sound system when quiting
216 *
217 * 47 10/24/98 2:16p Samir
218 * moved pollcontrols to controls.cpp, readplayercontrols.
219 *
220 * 46 10/23/98 1:09p Samir
221 * Demo Release 1.0
222 *
223 * 45 10/20/98 6:34p Jeff
224 * changes made to get dedicated server working
225 *
226 * 44 10/18/98 11:40p Matt
227 * Generalized the function that puts up the upsell screen so the credits
228 * could use it.
229 *
230 * 43 10/16/98 1:54p Kevin
231 * Changes for Demo Beta 4
232 *
233 * 42 10/16/98 12:17p Samir
234 * took out ddio_KeyFrame.
235 *
236 * 41 10/14/98 4:37p Matt
237 * Made InitD3System() exit with error if there's a problem instead of
238 * returning a status value. Also moved some editor-specific code from
239 * init.cpp to mainfrm.cpp, and cleaned up some other initialization and
240 * error-handling code.
241 *
242 * 40 10/12/98 4:40p Samir
243 * mouse click to leave upsell screen.
244 *
245 * 39 10/12/98 1:46p Samir
246 * new Program_version parameters.
247 *
248 * 38 10/08/98 7:52p Samir
249 * added upsell screen.
250 *
251 * 37 10/08/98 7:46p Samir
252 * added demo upsell screen.
253 *
254 * 36 10/08/98 7:26p Samir
255 * changed the prototype for the defer handler callback.
256 *
257 * 35 10/08/98 12:00p Kevin
258 * Demo system work
259 *
260 * 34 10/02/98 12:30p Samir
261 * added version information for beta/demo.
262 *
263 * 33 9/18/98 1:27p Jason
264 * cleaned up renderer initting
265 *
266 * 32 9/15/98 4:31p Jason
267 * added more functionality for the dedicated server
268 *
269 * 31 9/15/98 12:42p Jason
270 * got dedicated server actually working
271 *
272 * 30 9/14/98 6:28p Jason
273 * first pass at getting dedicated server working
274 *
275 * 29 8/20/98 10:51a Samir
276 * added the RESTORE_GAME_MODE function mode,.
277 *
278 * 28 7/21/98 6:50p Jeff
279 * don't initialize or close renderer if set screen mode hasn't been
280 * called yet in debug handlers.
281 *
282 * 27 7/01/98 4:56p Samir
283 * redid init code.
284 *
285 * 26 6/16/98 10:38a Jeff
286 * localization, strings pulled out to stringtable.h and d3.str
287 *
288 * 25 4/27/98 7:24p Samir
289 * poll controller at specified rate.
290 *
291 * 24 3/23/98 8:03p Samir
292 * A bunch of changes to allow for ALT-TAB to work.
293 *
294 * 23 3/19/98 11:27a Samir
295 * Better error checking.
296 *
297 * 22 3/10/98 5:16p Samir
298 * Got debug callbacks working when you hit an Int3.
299 *
300 * 21 2/26/98 1:00p Samir
301 * input only when app is active.
302 *
303 * 20 2/23/98 4:46p Jeff
304 * Moved serialization check to Winmain
305 *
306 * 19 2/15/98 7:44p Matt
307 * Added groovy try/catch/throw error checking for cfile functions
308 *
309 * 18 2/08/98 5:01p Samir
310 * New game sequencing changes.
311 *
312 * 17 2/07/98 6:33p Jeff
313 * Added in serialization check
314 *
315 * 16 2/02/98 7:35p Samir
316 * Call QuickPlayGame to play game using quick mission system.
317 *
318 * 15 1/10/98 2:55p Jason
319 * changed some things for multiplayer
320 *
321 * 14 1/07/98 3:49p Jason
322 * only call StartNewGame if not in multiplayer
323 *
324 * 13 12/29/97 5:44p Samir
325 * Took out references to grViewport and old 2d library.
326 *
327 * 12 12/03/97 7:34p Samir
328 * Run controller poll in idle loop.
329 *
330 * 11 10/27/97 4:30p Samir
331 * Set screen mode to SM_NULL when done with game.
332 *
333 * 10 10/16/97 2:30p Samir
334 * Added defer handler.
335 *
336 * 9 10/02/97 12:36p Samir
337 * Redid game sequencing flow.
338 *
339 * 8 9/17/97 11:16a Samir
340 * BIG SEGMENT RIPOUT.
341 *
342 * 7 9/12/97 4:08p Samir
343 * Made sure main game program version is DEVELOPMENT, not FULL
344 * DEVELOPMENT.
345 *
346 * 6 8/28/97 6:13p Matt
347 * Set the room engine to be always on
348 *
349 * 5 8/12/97 3:21p Samir
350 * When entering game from editor, call the QuickStart functions to load
351 * scripts, etc.
352 *
353 * 4 7/24/97 2:55p Matt
354 * Save game settings to registry on game exit
355 *
356 * 10 6/11/97 1:07p Samir
357 * The removal of gameos and replaced with oeApplication, oeDatabase
358 *
359 * 9 5/15/97 2:04p Samir
360 * Created functions to create game viewport.
361 *
362 * 8 3/10/97 11:30a Samir
363 * Took out osDebug global
364 *
365 * 7 3/03/97 5:23p Samir
366 * Added OS_database, and OS_debug objects pointers.
367 *
368 * 6 2/26/97 6:10 PM Jeremy
369 * removed #include of <windows.h>
370 *
371 * 5 2/04/97 5:28p Samir
372 * Added EDITOR_GAME_MODE mode
373 *
374 * 4 2/04/97 2:32p Samir
375 * Added a menu mode
376 *
377 * 3 1/30/97 6:20p Samir
378 * Mainloop was altered a little, and some changes reflecting osObject
379 * fix.
380 *
381 * $NoKeywords: $
382 */
383
384 #include <algorithm>
385 #include <cstdlib>
386 #include <filesystem>
387 #include <vector>
388
389 #include "pserror.h"
390 #include "grdefs.h"
391 #include "mono.h"
392 #include "cfile.h"
393
394 #include "init.h"
395 #include "game.h"
396 #include "program.h"
397 #include "descent.h"
398 #include "menu.h"
399 #include "ddio.h"
400 #include "gamesequence.h"
401 #include "stringtable.h"
402 #include "dedicated_server.h"
403 #include "networking.h"
404 #include "hlsoundlib.h"
405 #include "player.h"
406 #include "newui.h"
407 #include "credits.h"
408 #include "cinematics.h"
409 #include "args.h"
410 #include "multi_dll_mgr.h"
411 #include "localization.h"
412
413 // ---------------------------------------------------------------------------
414 // Variables
415 // ---------------------------------------------------------------------------
416 #ifdef EDITOR
417 static function_mode Function_mode = EDITOR_MODE; // Game function mode
418 #else
419 static function_mode Function_mode = INIT_MODE; // Game function mode
420 #endif
421 static function_mode Last_function_mode = INIT_MODE;
422
423 grScreen *Game_screen = NULL; // The one and only video screen
424 oeApplication *Descent = NULL; // The Main application
425 oeAppDatabase *Database = NULL; // Application database.
426
427 bool Descent_overrided_intro = false;
428
429 bool Katmai = true;
430
431 char Descent3_temp_directory[_MAX_PATH]; // temp directory to put temp files
432 // ---------------------------------------------------------------------------
433 // Descent3: Choke 1
434 // Initializes game elements and invokes the MainLoop
435 // ---------------------------------------------------------------------------
436 // #define BETA
437
438 #if (defined(OEM) || defined(DEMO))
439 void ShowStaticScreen(char *bitmap_filename, bool timed = false, float delay_time = 0.0f);
440 #endif
441
442 char Proxy_server[200] = "";
443 int16_t Proxy_port = 80;
444
445 void Descent3() {
446 int type;
447
448 #ifdef _DEBUG
449 type = DEVELOPMENT_VERSION;
450 #else
451 type = RELEASE_VERSION;
452 #endif
453 #ifdef BETA
454 type |= BETA_VERSION;
455 #endif
456 #ifdef DEMO
457 type |= DEMO_VERSION;
458 #endif
459
460 ProgramVersion(type, D3_MAJORVER, D3_MINORVER, D3_BUILD);
461
462 // Catch cfile errors
463 try {
464 // Init a bunch of stuff
465 InitD3Systems1(false);
466
467 int proxyarg = FindArg("-httpproxy");
468 if (proxyarg) {
469 strcpy(Proxy_server, GameArgs[proxyarg + 1]);
470
471 char *port = strchr(Proxy_server, ':');
472 if (port) {
473 // terminate the hostname
474 *port = 0;
475 // Increment to the first character of the port name
476 port++;
477 // get the port number
478 Proxy_port = atoi(port);
479 }
480 }
481 // Show intro & loading screens if not dedicated server
482 if (!Dedicated_server) {
483 SetScreenMode(SM_CINEMATIC);
484
485 // Show the intro movie
486 if (!FindArg("-nointro")) {
487 // Intros to be played
488 const std::vector<std::filesystem::path> intros = {
489 "dolby1.mv8",
490 "intro.mve",
491 };
492
493 for (auto const &intro : intros) {
494 PlayMovie(std::filesystem::path(Base_directory) / "movies" / intro);
495 }
496 }
497
498 SetScreenMode(SM_MENU);
499 // Show the intro screen
500 IntroScreen();
501 }
502
503 // Init a bunch more stuff
504 InitD3Systems2(false);
505
506 SetFunctionMode(MENU_MODE);
507
508 MainLoop();
509
510 // delete the lock file in the temp directory (as long as it belongs to us)
511 if (!ddio_DeleteLockFile(std::filesystem::path(Descent3_temp_directory))) {
512 mprintf(0, "Cannot delete lock file in temp dir %s!\n", Descent3_temp_directory);
513 };
514
515 // Save settings to registry
516 SaveGameSettings();
517 } catch (cfile_error *cfe) {
518 Error(TXT_D3ERROR1, (cfe->read_write == CFE_READING) ? TXT_READING : TXT_WRITING, cfe->file->name, cfe->msg);
519 }
520 }
521
522 // ---------------------------------------------------------------------------
523 // Main Loop: Choke 1 from editor Choke 2 from Game
524 // If called from the editor, this is the best way to start up the game.
525 // Otherwise this should be called from the main function.
526 // ---------------------------------------------------------------------------
527
528 extern int frames_one_second;
529 extern int min_one_second;
530 extern int max_one_second;
531
532 // #endif
533
534 void MainLoop() {
535 int exit_game = 0;
536
537 while (!exit_game) {
538 if (Dedicated_server && !(Function_mode == GAME_MODE || Function_mode == QUIT_MODE))
539 SetFunctionMode(GAME_MODE);
540
541 switch (Function_mode) {
542 case QUIT_MODE:
543 exit_game = 1;
544 break;
545 case MENU_MODE:
546 exit_game = MainMenu();
547 break;
548 case RESTORE_GAME_MODE: // do special sequencing for load games.
549 SetGameState(GAMESTATE_LOADGAME);
550 PlayGame();
551 break;
552
553 case GAME_MODE:
554 SetGameState(GAMESTATE_NEW);
555 PlayGame(); // Does what is says.
556 break;
557 case LOADDEMO_MODE:
558 SetGameState(GAMESTATE_LOADDEMO);
559 PlayGame();
560 break;
561 case CREDITS_MODE:
562 Credits_Display();
563 Function_mode = MENU_MODE;
564 break;
565 case GAMEGAUGE_MODE:
566 break;
567 #ifdef EDITOR
568 case EDITOR_GAME_MODE: // run level and then instead of menus, go to editor.
569 QuickPlayGame();
570 Function_mode = EDITOR_MODE;
571 break;
572 case EDITOR_MODE:
573 SetScreenMode(SM_NULL);
574 exit_game = 1; // this MainLoop call should be issued from editor, so
575 // this should just return to the editor
576 break;
577 #endif
578
579 default:
580 Int3(); // Bogus function mode
581 }
582 }
583 // Clean up these items so we don't report them as leaked memory
584 Sound_system.KillSoundLib(true);
585 for (int a = 0; a < MAX_PLAYERS; a++) {
586 Players[a].inventory.Reset(false, INVRESET_ALL);
587 Players[a].counter_measures.Reset(false, INVRESET_ALL);
588 }
589
590 #if defined(OEM)
591 if (!Dedicated_server)
592 ShowStaticScreen("oemupsell.ogf");
593 #elif defined(DEMO)
594 if (!Dedicated_server)
595 ShowStaticScreen("upsell.ogf");
596 #endif
597 FreeMultiDLL();
598 SetScreenMode(SM_NULL);
599 }
600
601 #if (defined(OEM) || defined(DEMO) || defined(RELEASE))
602 // Shows a fullscreen static bitmap
603 void ShowStaticScreen(char *bitmap_filename, bool timed, float delay_time) {
604 extern void ui_SetScreenMode(int w, int h);
605 chunked_bitmap splash_bm;
606
607 // do splash screen on release
608 int bm_handle = bm_AllocLoadFileBitmap(IGNORE_TABLE(bitmap_filename), 0);
609
610 ddio_KeyFlush();
611 ddio_MouseReset();
612
613 if (bm_handle > -1) {
614 if (!bm_CreateChunkedBitmap(bm_handle, &splash_bm))
615 Error("Failed to slice up %s.", bitmap_filename);
616
617 bm_FreeBitmap(bm_handle);
618 float start_time = timer_GetTime();
619 while (1) {
620 StartFrame();
621
622 rend_DrawChunkedBitmap(&splash_bm, 0, 0, 255);
623 rend_Flip();
624 EndFrame();
625
626 Descent->defer();
627
628 if (!timed) {
629 int key = ddio_KeyInKey();
630 if (key == KEY_ENTER || key == KEY_SPACEBAR || key == KEY_ESC)
631 break;
632
633 int x, y, dx, dy;
634 if (ddio_MouseGetState(&x, &y, &dx, &dy))
635 break;
636 } else {
637 if ((timer_GetTime() - start_time) > delay_time) {
638 break;
639 }
640 }
641 }
642
643 bm_DestroyChunkedBitmap(&splash_bm);
644 } else {
645 mprintf(0, "Couldn't load %s.\n", bitmap_filename);
646 }
647
648 ui_SetScreenMode(Max_window_w, Max_window_h);
649 }
650 #endif
651
652 // ---------------------------------------------------------------------------
653 // Accessor functions
654 // ---------------------------------------------------------------------------
655
656 void SetFunctionMode(function_mode mode) {
657 Last_function_mode = Function_mode;
658 Function_mode = mode;
659 }
660
661 function_mode GetFunctionMode() { return Function_mode; }
662
663 // ---------------------------------------------------------------------------
664 // The game's defer handler (For Win32, it happens during idle processing)
665 // ---------------------------------------------------------------------------
666 extern bool Skip_render_game_frame;
667 extern void GameFrame(void);
668 void D3DeferHandler(bool is_active) {
669 if (is_active) {
670 // perform any needed io system processing in the background
671 ddio_Frame();
672 } else {
673 // JEFF - If the game is in idle loop and we are in multiplayer game
674 // then process the game frame so we stay alive in the game.
675 if (Game_mode & GM_MULTI) {
676 Skip_render_game_frame = true;
677 GameFrame();
678 Skip_render_game_frame = false;
679 }
680 }
681
682 // JEFF - Commented out due to new idle processing
683 nw_DoNetworkIdle();
684 }
685
686 // ---------------------------------------------------------------------------
687 // this is called when you hit a debug break!
688 // ---------------------------------------------------------------------------
689 #ifndef RELEASE
690 #include "debug.h"
691 #include "renderer.h"
692 extern int rend_initted; // from game.cpp
693
694 void D3DebugStopHandler() {
695 // close off all systems for debugging.
696 if (rend_initted) {
697 rend_Close();
698 }
699 }
700
701 void D3DebugResumeHandler() {
702 // reopen all systems for gameplay.
703 if (rend_initted)
704 rend_initted = rend_Init(PreferredRenderer, Descent, &Render_preferred_state);
705
706 if (rend_initted != 0)
707 rend_initted = 1;
708 }
709
710 #endif
711
712 void RenderBlankScreen();