Debian Codegraph @

similar/main/paging.cpp

raw
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 * Routines for paging in/out textures.
23 *
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include "pstypes.h"
32 #include "inferno.h"
33 #include "segment.h"
34 #include "textures.h"
35 #include "wall.h"
36 #include "object.h"
37 #include "console.h"
38 #include "game.h"
39 #include "piggy.h"
40 #include "texmerge.h"
41 #include "paging.h"
42 #include "robot.h"
43 #include "vclip.h"
44 #include "effects.h"
45 #include "fireball.h"
46 #include "weapon.h"
47 #include "palette.h"
48 #include "text.h"
49 #include "gauges.h"
50 #include "powerup.h"
51 #include "fuelcen.h"
52 #include "ai.h"
53
54 #include "compiler-range_for.h"
55 #include "d_levelstate.h"
56 #include "partial_range.h"
57 #include "segiter.h"
58
59 namespace {
60
61 static void paging_touch_vclip(const vclip &vc, const unsigned line
62 #if DXX_HAVE_CXX_BUILTIN_FILE_LINE
63 = __builtin_LINE()
64 #else
65 = 0
66 #endif
67 )
68 {
69 using range_type = partial_range_t<const bitmap_index*>;
70 union {
71 uint8_t storage[1];
72 range_type r;
73 } u{};
74 try
75 {
76 new(&u.r) range_type(partial_const_range<partial_range_detail::required_buffer_size<sizeof(__FILE__), sizeof("vc.frames")>>(__FILE__, line, "vc.frames", vc.frames, vc.num_frames));
77 static_assert(std::is_trivially_destructible<range_type>::value, "partial_range destructor not called");
78 }
79 catch (const range_type::partial_range_error &e)
80 {
81 const auto w = e.what();
82 con_puts(CON_URGENT, {w, strlen(w)});
83 return;
84 }
85 range_for (auto &i, u.r)
86 {
87 PIGGY_PAGE_IN(i);
88 }
89 }
90
91 static void paging_touch_vclip(const d_vclip_array &Vclip, const vclip_index vclip_id, const unsigned line
92 #if DXX_HAVE_CXX_BUILTIN_FILE_LINE
93 = __builtin_LINE()
94 #else
95 = 0
96 #endif
97 )
98 {
99 if (Vclip.valid_index(vclip_id))
100 paging_touch_vclip(Vclip[vclip_id], line);
101 }
102
103 static void paging_touch_vclip(const d_eclip_array &Effects, const int eclip_id, const unsigned line
104 #if DXX_HAVE_CXX_BUILTIN_FILE_LINE
105 = __builtin_LINE()
106 #else
107 = 0
108 #endif
109 )
110 {
111 if (eclip_id > -1)
112 paging_touch_vclip(Effects[eclip_id].vc, line);
113 }
114
115 static void paging_touch_wall_effects(const d_eclip_array &Effects, const Textures_array &Textures, const d_vclip_array &Vclip, const int tmap_num)
116 {
117 range_for (auto &i, partial_const_range(Effects, Num_effects))
118 {
119 if ( i.changing_wall_texture == tmap_num ) {
120 paging_touch_vclip(i.vc);
121
122 if (i.dest_bm_num < Textures.size())
123 PIGGY_PAGE_IN( Textures[i.dest_bm_num] ); //use this bitmap when monitor destroyed
124 paging_touch_vclip(Vclip, i.dest_vclip); //what vclip to play when exploding
125 paging_touch_vclip(Effects, i.dest_eclip); //what eclip to play when exploding
126 paging_touch_vclip(Effects, i.crit_clip); //what eclip to play when mine critical
127 break;
128 }
129 }
130 }
131
132 static void paging_touch_object_effects(const d_eclip_array &Effects, const object_bitmap_index tmap_num)
133 {
134 range_for (auto &i, partial_const_range(Effects, Num_effects))
135 {
136 if ( i.changing_object_texture == tmap_num ) {
137 paging_touch_vclip(i.vc);
138 break;
139 }
140 }
141 }
142
143 static void paging_touch_model(const polygon_model_index modelnum)
144 {
145 auto &Effects = LevelUniqueEffectsClipState.Effects;
146 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
147 const auto &pm = Polygon_models[modelnum];
148 const uint_fast32_t b = pm.first_texture;
149 const uint_fast32_t e = b + pm.n_textures;
150 range_for (const auto p, partial_range(ObjBitmapPtrs, b, e))
151 {
152 PIGGY_PAGE_IN(ObjBitmaps[p]);
153 paging_touch_object_effects(Effects, p);
154 }
155 }
156
157 static void paging_touch_weapon(const d_vclip_array &Vclip, const weapon_info &weapon)
158 {
159 // Page in the robot's weapons.
160
161 if (weapon.picture != bitmap_index{})
162 {
163 PIGGY_PAGE_IN(weapon.picture);
164 }
165
166 paging_touch_vclip(Vclip, weapon.flash_vclip);
167 paging_touch_vclip(Vclip, weapon.wall_hit_vclip);
168 if (weapon.damage_radius)
169 {
170 // Robot_hit_vclips are actually badass_vclips
171 paging_touch_vclip(Vclip, weapon.robot_hit_vclip);
172 }
173
174 switch(weapon.render)
175 {
176 case WEAPON_RENDER_VCLIP:
177 paging_touch_vclip(Vclip, weapon.weapon_vclip);
178 break;
179 case WEAPON_RENDER_NONE:
180 case weapon_info::render_type::laser:
181 break;
182 case WEAPON_RENDER_POLYMODEL:
183 paging_touch_model(weapon.model_num);
184 break;
185 case WEAPON_RENDER_BLOB:
186 PIGGY_PAGE_IN(weapon.bitmap);
187 break;
188 }
189 }
190
191 static void paging_touch_weapon(const d_vclip_array &Vclip, const weapon_info_array &Weapon_info, const uint_fast32_t weapon_type)
192 {
193 if (weapon_type < N_weapon_types)
194 paging_touch_weapon(Vclip, Weapon_info[weapon_type]);
195 }
196
197 const std::array<robot_id, 13> super_boss_gate_type_list{{
198 robot_id{0},
199 robot_id{1},
200 robot_id{8},
201 robot_id{9},
202 robot_id{10},
203 robot_id{11},
204 robot_id{12},
205 robot_id{15},
206 robot_id{16},
207 robot_id{18},
208 robot_id{19},
209 robot_id{20},
210 robot_id{22},
211 }};
212
213 static void paging_touch_robot(const d_robot_info_array &Robot_info, const d_vclip_array &Vclip, const weapon_info_array &Weapon_info, const robot_id ridx)
214 {
215 auto &ri = Robot_info[ridx];
216 // Page in robot_index
217 paging_touch_model(ri.model_num);
218 paging_touch_vclip(Vclip, ri.exp1_vclip_num);
219 paging_touch_vclip(Vclip, ri.exp2_vclip_num);
220
221 // Page in his weapons
222 paging_touch_weapon(Vclip, Weapon_info, ri.weapon_type);
223
224 // A super-boss can gate in robots...
225 if (ri.boss_flag == boss_robot_id::d1_superboss)
226 {
227 range_for (const auto i, super_boss_gate_type_list)
228 paging_touch_robot(Robot_info, Vclip, Weapon_info, i);
229 paging_touch_vclip(Vclip[vclip_index::morphing_robot]);
230 }
231 }
232
233 static void paging_touch_object(const d_robot_info_array &Robot_info, const Textures_array &Textures, const d_vclip_array &Vclip, const weapon_info_array &Weapon_info, const object_base &obj)
234 {
235 switch (obj.render_type) {
236
237 case render_type::RT_NONE: break; //doesn't render, like the player
238
239 case render_type::RT_POLYOBJ:
240 if (obj.rtype.pobj_info.tmap_override != -1)
241 PIGGY_PAGE_IN(Textures[obj.rtype.pobj_info.tmap_override]);
242 else
243 paging_touch_model(obj.rtype.pobj_info.model_num);
244 break;
245
246 case render_type::RT_POWERUP:
247 paging_touch_vclip(Vclip, obj.rtype.vclip_info.vclip_num);
248 break;
249
250 case render_type::RT_MORPH: break;
251
252 case render_type::RT_FIREBALL: break;
253
254 case render_type::RT_WEAPON_VCLIP: break;
255
256 case render_type::RT_HOSTAGE:
257 paging_touch_vclip(Vclip[obj.rtype.vclip_info.vclip_num]);
258 break;
259
260 case render_type::RT_LASER: break;
261 }
262
263 switch (obj.type) {
264 default:
265 break;
266 case OBJ_PLAYER:
267 paging_touch_vclip(Vclip, get_explosion_vclip(LevelSharedRobotInfoState.Robot_info, obj, explosion_vclip_stage::s0));
268 break;
269 case OBJ_ROBOT:
270 paging_touch_robot(Robot_info, Vclip, Weapon_info, get_robot_id(obj));
271 break;
272 case OBJ_CNTRLCEN:
273 paging_touch_weapon(Vclip, Weapon_info, weapon_id_type::CONTROLCEN_WEAPON_NUM);
274 if (const auto m = Dead_modelnums[obj.rtype.pobj_info.model_num]; m != polygon_model_index::None)
275 paging_touch_model(m);
276 break;
277 }
278 }
279
280 static void paging_touch_side(const d_eclip_array &Effects, const Textures_array &Textures, const d_vclip_array &Vclip, const cscusegment segp, const sidenum_t sidenum)
281 {
282 auto &Walls = LevelUniqueWallSubsystemState.Walls;
283 auto &vcwallptr = Walls.vcptr;
284 if (!(WALL_IS_DOORWAY(GameBitmaps, Textures, vcwallptr, segp, sidenum) & WALL_IS_DOORWAY_FLAG::render))
285 return;
286 auto &uside = segp.u.sides[sidenum];
287 const auto tmap1 = uside.tmap_num;
288 paging_touch_wall_effects(Effects, Textures, Vclip, get_texture_index(tmap1));
289 if (const auto tmap2 = uside.tmap_num2; tmap2 != texture2_value::None)
290 {
291 texmerge_get_cached_bitmap( tmap1, tmap2 );
292 paging_touch_wall_effects(Effects, Textures, Vclip, get_texture_index(tmap2));
293 } else {
294 PIGGY_PAGE_IN(Textures[get_texture_index(tmap1)]);
295 }
296 }
297
298 static void paging_touch_robot_maker(const d_robot_info_array &Robot_info, const d_vclip_array &Vclip, const weapon_info_array &Weapon_info, const shared_segment &segp)
299 {
300 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
301 paging_touch_vclip(Vclip[vclip_index::morphing_robot]);
302 const auto &robot_flags = RobotCenters[segp.matcen_num].robot_flags;
303 constexpr std::size_t bits_per_robot_flags = 8 * sizeof(robot_flags[0]);
304 for (uint_fast32_t i = 0; i != robot_flags.size(); ++i)
305 {
306 uint8_t robot_index = i * bits_per_robot_flags;
307 /* As an optimization, if uint_fast32_t can represent two
308 * elements of robot_flags[] and a second element exists, then
309 * merge in the second element, so that the inner while loop
310 * can handle two elements of robot_flags[] at a time.
311 */
312 uint_fast32_t flags = robot_flags[i];
313 if constexpr (sizeof(flags) >= 2 * sizeof(robot_flags[0]))
314 {
315 if (i + 1 != robot_flags.size())
316 flags |= static_cast<uint64_t>(robot_flags[++i]) << bits_per_robot_flags;
317 }
318 while (flags) {
319 if (flags & 1) {
320 // Page in robot_index
321 paging_touch_robot(Robot_info, Vclip, Weapon_info, robot_id{robot_index});
322 }
323 flags >>= 1;
324 robot_index++;
325 }
326 }
327 }
328
329 static void paging_touch_segment(const d_eclip_array &Effects, const d_robot_info_array &Robot_info, const Textures_array &Textures, const d_vclip_array &Vclip, const weapon_info_array &Weapon_info, const fvcobjptridx &vcobjptridx, const fvcsegptr &vcsegptr, const cscusegment segp)
330 {
331 if (segp.s.special == segment_special::robotmaker)
332 paging_touch_robot_maker(Robot_info, Vclip, Weapon_info, segp);
333
334 for (const auto sn : MAX_SIDES_PER_SEGMENT)
335 {
336 paging_touch_side(Effects, Textures, Vclip, segp, sn);
337 }
338
339 for (auto &objp : objects_in<const object_base>(segp, vcobjptridx, vcsegptr))
340 paging_touch_object(Robot_info, Textures, Vclip, Weapon_info, objp);
341 }
342
343 static void paging_touch_walls(const Textures_array &Textures, const wall_animations_array &WallAnims, const fvcwallptr &vcwallptr)
344 {
345 for (auto &w : vcwallptr)
346 {
347 if (w.clip_num > -1)
348 {
349 const auto &anim = WallAnims[w.clip_num];
350 range_for (auto &j, partial_range(anim.frames, anim.num_frames))
351 PIGGY_PAGE_IN(Textures[j]);
352 }
353 }
354 }
355 }
356
357 namespace dsx {
358 void paging_touch_all(const d_vclip_array &Vclip)
359 {
360 auto &Effects = LevelUniqueEffectsClipState.Effects;
361 auto &Objects = LevelUniqueObjectState.Objects;
362 auto &WallAnims = GameSharedState.WallAnims;
363 auto &vcobjptridx = Objects.vcptridx;
364 pause_game_world_time p;
365
366 #if DXX_BUILD_DESCENT == 1
367 gr_set_default_canvas();
368 show_boxed_message(*grd_curcanv, TXT_LOADING);
369 gr_flip();
370 #endif
371 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
372 for (const cscusegment segp : vcsegptr)
373 {
374 paging_touch_segment(Effects, Robot_info, Textures, Vclip, Weapon_info, vcobjptridx, vcsegptr, segp);
375 }
376 auto &Walls = LevelUniqueWallSubsystemState.Walls;
377 auto &vcwallptr = Walls.vcptr;
378 paging_touch_walls(Textures, WallAnims, vcwallptr);
379
380 range_for (auto &s, partial_const_range(Powerup_info, N_powerup_types))
381 paging_touch_vclip(Vclip, s.vclip_num);
382
383 range_for (auto &w, partial_const_range(Weapon_info, N_weapon_types))
384 {
385 paging_touch_weapon(Vclip, w);
386 }
387
388 range_for (auto &s, partial_const_range(Powerup_info, N_powerup_types))
389 paging_touch_vclip(Vclip, s.vclip_num);
390
391 range_for (auto &s, Gauges)
392 {
393 if (s != bitmap_index{})
394 PIGGY_PAGE_IN( s );
395 }
396 paging_touch_vclip(Vclip[vclip_index::player_appearance]);
397 paging_touch_vclip(Vclip[vclip_index::powerup_disappearance]);
398
399 reset_cockpit(); //force cockpit redraw next time
400 }
401 }