1 /*
2  * Portions of this file are copyright Rebirth contributors and licensed as
3  * described in COPYING.txt.
4  * Portions of this file are copyright Parallax Software and licensed
5  * according to the Parallax license below.
6  * See COPYING.txt for license details.
7 
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
18 */
19 
20 /*
21  *
22  * Global variables for main directory
23  *
24  */
25 
26 #include "maths.h"
27 #include "vecmat.h"
28 #include "inferno.h"
29 #include "lighting.h"
30 #include "cntrlcen.h"
31 #include "effects.h"
32 #include "fuelcen.h"
33 #include "segment.h"
34 #include "switch.h"
35 #include "object.h"
36 #include "player.h"
37 #include "bm.h"
38 #include "robot.h"
39 #include "ai.h"
40 #include "3d.h"
41 #include "game.h"
42 #include "textures.h"
43 #include "valptridx.tcc"
44 #include "wall.h"
45 #include "d_levelstate.h"
46 
47 namespace dcx {
48 
49 template <typename T>
reconstruct_global_variable(T & t)50 static void reconstruct_global_variable(T &t)
51 {
52 	t.~T();
53 	new(&t) T();
54 }
55 
56 d_interface_unique_state InterfaceUniqueState;
57 d_game_view_unique_state GameViewUniqueState;
58 d_player_unique_endlevel_state PlayerUniqueEndlevelState;
59 d_level_unique_automap_state LevelUniqueAutomapState;
60 d_level_unique_fuelcenter_state LevelUniqueFuelcenterState;
61 d_level_unique_robot_awareness_state LevelUniqueRobotAwarenessState;
62 d_level_unique_segment_state LevelUniqueSegmentState;
63 // Global array of vertices, common to one mine.
64 valptridx<player>::array_managed_type Players;
65 valptridx<segment>::array_managed_type Segments;
66 }
67 enumerated_array<g3s_point, MAX_VERTICES, vertnum_t> Segment_points;
68 
69 namespace dcx {
70 fix FrameTime = 0x1000;	// Time since last frame, in seconds
71 fix64 GameTime64 = 0;			//	Time in game, in seconds
72 
73 int d_tick_count = 0; // increments every 33.33ms
74 int d_tick_step = 0;  // true once every 33.33ms
75 
76 //	This is the global mine which create_new_mine returns.
77 //lsegment	Lsegments[MAX_SEGMENTS];
78 
79 // Number of vertices in current mine (ie, Vertices, pointed to by Vp)
80 
81 //	Translate table to get opposite side of a face on a segment.
82 
83 const std::array<sidenum_t, MAX_SIDES_PER_SEGMENT> Side_opposite{{
84 	sidenum_t::WRIGHT,
85 	sidenum_t::WBOTTOM,
86 	sidenum_t::WLEFT,
87 	sidenum_t::WTOP,
88 	sidenum_t::WFRONT,
89 	sidenum_t::WBACK
90 }};
91 
92 #define TOLOWER(c) ((((c)>='A') && ((c)<='Z'))?((c)+('a'-'A')):(c))
93 
94 const std::array<std::array<unsigned, 4>, MAX_SIDES_PER_SEGMENT>  Side_to_verts{{
95 	{{7,6,2,3}},			// left
96 	{{0,4,7,3}},			// top
97 	{{0,1,5,4}},			// right
98 	{{2,6,5,1}},			// bottom
99 	{{4,5,6,7}},			// back
100 	{{3,2,1,0}},			// front
101 }};
102 
103 // Texture map stuff
104 
105 //--unused-- fix	Laser_delay_time = F1_0/6;		//	Delay between laser fires.
106 
reset_globals_for_new_game()107 static void reset_globals_for_new_game()
108 {
109 	reconstruct_global_variable(LevelSharedBossState);
110 	reconstruct_global_variable(LevelUniqueFuelcenterState);
111 	reconstruct_global_variable(LevelUniqueSegmentState);
112 	reconstruct_global_variable(Players);
113 	reconstruct_global_variable(Segments);
114 }
115 
116 }
117 
118 #if DXX_HAVE_POISON_UNDEFINED
119 template <typename managed_type>
array_managed_type()120 valptridx<managed_type>::array_managed_type::array_managed_type()
121 {
122 	DXX_MAKE_MEM_UNDEFINED(this->begin(), this->end());
123 }
124 #endif
125 
126 namespace dsx {
127 d_game_shared_state GameSharedState;
128 d_game_unique_state GameUniqueState;
129 d_level_shared_boss_state LevelSharedBossState;
130 #if defined(DXX_BUILD_DESCENT_II)
131 d_level_shared_control_center_state LevelSharedControlCenterState;
132 #endif
133 d_level_unique_effects_clip_state LevelUniqueEffectsClipState;
134 d_level_shared_segment_state LevelSharedSegmentState;
135 d_level_unique_object_state LevelUniqueObjectState;
136 d_level_unique_light_state LevelUniqueLightState;
137 d_level_shared_polygon_model_state LevelSharedPolygonModelState;
138 d_level_shared_robotcenter_state LevelSharedRobotcenterState;
139 d_level_shared_robot_info_state LevelSharedRobotInfoState;
140 d_level_shared_robot_joint_state LevelSharedRobotJointState;
141 d_level_unique_wall_subsystem_state LevelUniqueWallSubsystemState;
142 d_level_unique_tmap_info_state LevelUniqueTmapInfoState;
143 
144 #if defined(DXX_BUILD_DESCENT_II)
145 d_level_shared_seismic_state LevelSharedSeismicState;
146 d_level_unique_seismic_state LevelUniqueSeismicState;
147 #endif
148 
reset_globals_for_new_game()149 void reset_globals_for_new_game()
150 {
151 	::dcx::reset_globals_for_new_game();
152 	/* Skip LevelUniqueEffectsClipState because it contains some fields
153 	 * that are initialized from game data, and those fields should not
154 	 * be reconstructed.
155 	 */
156 	reconstruct_global_variable(LevelUniqueObjectState);
157 	reconstruct_global_variable(LevelUniqueLightState);
158 	reconstruct_global_variable(LevelUniqueWallSubsystemState);
159 	/* Same for LevelUniqueTmapInfoState */
160 }
161 }
162 
163 /*
164  * If not specified otherwise by the user, enable full instantiation if
165  * the compiler inliner is not enabled.  This is required because
166  * non-inline builds contain link time references to functions that are
167  * not used.
168  *
169  * Force full instantiation for non-inline builds so that these
170  * references are satisfied.  For inline-enabled builds, instantiate
171  * only the classes that are known to be used.
172  *
173  * Force full instantiation for AddressSanitizer builds.  In gcc-7 (and
174  * possibly other versions), AddressSanitizer inhibits an optimization
175  * that deletes an unnecessary nullptr check, causing references to
176  * class null_pointer_exception.
177  */
178 #ifndef DXX_VALPTRIDX_ENABLE_FULL_TEMPLATE_INSTANTIATION
179 #if (defined(__NO_INLINE__) && __NO_INLINE__ > 0) || defined(__SANITIZE_ADDRESS__)
180 #define DXX_VALPTRIDX_ENABLE_FULL_TEMPLATE_INSTANTIATION	1
181 #else
182 #define DXX_VALPTRIDX_ENABLE_FULL_TEMPLATE_INSTANTIATION	0
183 #endif
184 #endif
185 
186 #if DXX_VALPTRIDX_ENABLE_FULL_TEMPLATE_INSTANTIATION
187 template class valptridx<dcx::active_door>;
188 template class valptridx<dcx::vertex>;
189 #if defined(DXX_BUILD_DESCENT_II)
190 template class valptridx<dsx::cloaking_wall>;
191 #endif
192 template class valptridx<dsx::object>;
193 template class valptridx<dcx::player>;
194 template class valptridx<dcx::segment>;
195 template class valptridx<dsx::trigger>;
196 template class valptridx<dsx::wall>;
197 
198 #else
199 namespace {
200 
201 	/* Explicit instantiation cannot be conditional on a truth
202 	 * expression, but the exception types only need to be instantiated
203 	 * if they are used.  To reduce code bloat, use
204 	 * `instantiation_guard` to instantiate the real exception class if
205 	 * it is used (as reported by
206 	 * `valptridx<T>::report_error_uses_exception`), but otherwise
207 	 * instantiate a stub class with no members.  The stub class must be
208 	 * a member of a template that depends on `T` because duplicate
209 	 * explicit instantiations are not allowed.  If the stub did not
210 	 * depend on `T`, each type `T` that used the stubs would
211 	 * instantiate the same stub.
212 	 *
213 	 * Hide `instantiation_guard` in an anonymous namespace to encourage
214 	 * the compiler to optimize away any unused pieces of it.
215 	 */
216 template <typename T, bool = valptridx<T>::report_error_uses_exception::value>
217 struct instantiation_guard
218 {
219 	using type = valptridx<T>;
220 };
221 
222 template <typename T>
223 struct instantiation_guard<T, false>
224 {
225 	struct type
226 	{
227 		class index_mismatch_exception {};
228 		class index_range_exception {};
229 		class null_pointer_exception {};
230 	};
231 };
232 
233 }
234 
235 template class instantiation_guard<dcx::active_door>::type::index_range_exception;
236 template class instantiation_guard<dcx::vertex>::type::index_range_exception;
237 #if defined(DXX_BUILD_DESCENT_II)
238 template class instantiation_guard<dsx::cloaking_wall>::type::index_range_exception;
239 #endif
240 
241 template class instantiation_guard<dsx::object>::type::index_mismatch_exception;
242 template class instantiation_guard<dsx::object>::type::index_range_exception;
243 template class instantiation_guard<dsx::object>::type::null_pointer_exception;
244 
245 template class instantiation_guard<dcx::player>::type::index_range_exception;
246 
247 template class instantiation_guard<dcx::segment>::type::index_mismatch_exception;
248 template class instantiation_guard<dcx::segment>::type::index_range_exception;
249 template class instantiation_guard<dcx::segment>::type::null_pointer_exception;
250 
251 template class instantiation_guard<dsx::trigger>::type::index_range_exception;
252 template class instantiation_guard<dsx::wall>::type::index_range_exception;
253 
254 #endif
255