Debian Codegraph @

similar/main/bm.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 * Bitmap and palette loading functions.
23 *
24 */
25
26 #include <algorithm>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "pstypes.h"
32 #include "inferno.h"
33 #include "gr.h"
34 #include "bm.h"
35 #include "u_mem.h"
36 #include "dxxerror.h"
37 #include "object.h"
38 #include "vclip.h"
39 #include "effects.h"
40 #include "polyobj.h"
41 #include "wall.h"
42 #include "textures.h"
43 #include "game.h"
44 #include "multi.h"
45 #include "iff.h"
46 #include "powerup.h"
47 #include "sounds.h"
48 #include "piggy.h"
49 #include "aistruct.h"
50 #include "robot.h"
51 #include "weapon.h"
52 #include "gauges.h"
53 #include "player.h"
54 #include "endlevel.h"
55 #include "cntrlcen.h"
56 #include "makesig.h"
57 #include "mission.h"
58 #include "interp.h"
59 #include "console.h"
60 #include "rle.h"
61 #include "physfsx.h"
62 #include "strutil.h"
63
64 #if DXX_USE_EDITOR
65 #include "editor/texpage.h"
66 #endif
67
68 #include "compiler-range_for.h"
69 #include "d_range.h"
70 #include "partial_range.h"
71 #include <memory>
72
73 unsigned NumTextures;
74
75 namespace dsx {
76 #if DXX_BUILD_DESCENT == 1
77 int Num_total_object_types;
78
79 sbyte ObjType[MAX_OBJTYPE];
80 std::array<polygon_model_index, MAX_OBJTYPE> ObjId;
81 fix ObjStrength[MAX_OBJTYPE];
82 #elif DXX_BUILD_DESCENT == 2
83 //the polygon model number to use for the marker
84 unsigned N_ObjBitmaps;
85 namespace {
86 static int extra_bitmap_num;
87 static void bm_free_extra_objbitmaps();
88 }
89 #endif
90
91 Textures_array Textures; // All textures.
92 //for each model, a model number for dying & dead variants, or -1 if none
93 enumerated_array<polygon_model_index, MAX_POLYGON_MODELS, polygon_model_index> Dying_modelnums, Dead_modelnums;
94 }
95
96 //right now there's only one player ship, but we can have another by
97 //adding an array and setting the pointer to the active ship.
98 namespace dcx {
99
100 #if DXX_USE_EDITOR
101 int Num_object_subtypes{1};
102 #endif
103
104 std::array<sound_effect, ::d2x::MAX_SOUNDS> Sounds, AltSounds;
105 player_ship only_player_ship;
106
107 //----------------- Miscellaneous bitmap pointers ---------------
108 unsigned Num_cockpits;
109 }
110
111 //---------------- Variables for wall textures ------------------
112
113 //---------------- Variables for object textures ----------------
114
115 int First_multi_bitmap_num=-1;
116
117 namespace dsx {
118
119 enumerated_array<bitmap_index, N_COCKPIT_BITMAPS, cockpit_bitmap_index> cockpit_bitmap;
120 enumerated_array<bitmap_index, MAX_OBJ_BITMAPS, object_bitmap_index> ObjBitmaps;
121 std::array<object_bitmap_index, MAX_OBJ_BITMAPS> ObjBitmapPtrs; // These point back into ObjBitmaps, since some are used twice.
122
123 void gamedata_close()
124 {
125 free_polygon_models(LevelSharedPolygonModelState);
126 #if DXX_BUILD_DESCENT == 2
127 bm_free_extra_objbitmaps();
128 #endif
129 free_endlevel_data();
130 rle_cache_close();
131 piggy_close();
132 }
133
134 /*
135 * reads n tmap_info structs from a PHYSFS_File
136 */
137 #if DXX_BUILD_DESCENT == 1
138 namespace {
139
140 static void tmap_info_read(tmap_info &ti, const NamedPHYSFS_File fp)
141 {
142 PHYSFSX_readBytes(fp.fp, ti.filename, 13);
143 uint8_t flags;
144 PHYSFSX_readBytes(fp.fp, &flags, 1);
145 ti.flags = tmapinfo_flags{flags};
146 ti.lighting = PHYSFSX_readFix(fp);
147 ti.damage = PHYSFSX_readFix(fp);
148 ti.eclip_num = PHYSFSX_readInt(fp);
149 }
150 }
151
152 //-----------------------------------------------------------------
153 // Initializes game properties data (including texture caching system) and sound data.
154 int gamedata_init(d_level_shared_robot_info_state &LevelSharedRobotInfoState)
155 {
156 init_polygon_models(LevelSharedPolygonModelState);
157 const auto retval = properties_init(LevelSharedRobotInfoState); // This calls properties_read_cmp if appropriate
158 if (retval != properties_init_result::skip_gamedata_read_tbl)
159 gamedata_read_tbl(LevelSharedRobotInfoState, Vclip, retval == properties_init_result::shareware);
160 piggy_read_sounds(retval == properties_init_result::shareware);
161
162 return 0;
163 }
164
165 // Read compiled properties data from descent.pig
166 // (currently only ever called if D1)
167 void properties_read_cmp(d_level_shared_robot_info_state &LevelSharedRobotInfoState, d_vclip_array &Vclip, const NamedPHYSFS_File fp)
168 {
169 auto &Effects = LevelUniqueEffectsClipState.Effects;
170 auto &Robot_joints = LevelSharedRobotJointState.Robot_joints;
171 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
172 auto &WallAnims = GameSharedState.WallAnims;
173 // bitmap_index is a short
174
175 NumTextures = PHYSFSX_readInt(fp);
176 bitmap_index_read_n(fp, Textures);
177 range_for (tmap_info &ti, TmapInfo)
178 tmap_info_read(ti, fp);
179
180 PHYSFSX_readBytes(fp, Sounds, MAX_SOUNDS);
181 PHYSFSX_readBytes(fp, AltSounds, MAX_SOUNDS);
182
183 Num_vclips = PHYSFSX_readInt(fp);
184 range_for (vclip &vc, Vclip)
185 vclip_read(fp, vc);
186
187 Num_effects = PHYSFSX_readInt(fp);
188 range_for (eclip &ec, Effects)
189 eclip_read(fp, ec);
190
191 Num_wall_anims = PHYSFSX_readInt(fp);
192 range_for (auto &w, WallAnims)
193 wclip_read(fp, w);
194
195 LevelSharedRobotInfoState.N_robot_types = PHYSFSX_readInt(fp);
196 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
197 range_for (auto &r, Robot_info)
198 robot_info_read(fp, r);
199
200 LevelSharedRobotJointState.N_robot_joints = PHYSFSX_readInt(fp);
201 range_for (auto &r, Robot_joints)
202 jointpos_read(fp, r);
203
204 N_weapon_types = PHYSFSX_readInt(fp);
205 weapon_info_read_n(Weapon_info, MAX_WEAPON_TYPES, fp, 0);
206
207 N_powerup_types = PHYSFSX_readInt(fp);
208 range_for (auto &p, Powerup_info)
209 powerup_type_info_read(fp, p);
210
211 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
212 const auto N_polygon_models = LevelSharedPolygonModelState.N_polygon_models = PHYSFSX_readInt(fp);
213 {
214 const auto &&r = partial_range(Polygon_models, N_polygon_models);
215 range_for (auto &p, r)
216 polymodel_read(p, fp);
217
218 range_for (auto &p, r)
219 polygon_model_data_read(&p, fp);
220 }
221
222 bitmap_index_read_n(fp, partial_range(Gauges, MAX_GAUGE_BMS));
223
224 range_for (auto &i, Dying_modelnums)
225 i = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
226 range_for (auto &i, Dead_modelnums)
227 i = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
228
229 bitmap_index_read_n(fp, ObjBitmaps);
230 range_for (auto &i, ObjBitmapPtrs)
231 {
232 if (const auto oi = ObjBitmaps.valid_index(PHYSFSX_readSLE16(fp)))
233 i = {*oi};
234 else
235 i = {};
236 }
237
238 player_ship_read(&only_player_ship, fp);
239
240 Num_cockpits = PHYSFSX_readInt(fp);
241 bitmap_index_read_n(fp, cockpit_bitmap);
242
243 PHYSFSX_readBytes(fp, Sounds, MAX_SOUNDS);
244 PHYSFSX_readBytes(fp, AltSounds, MAX_SOUNDS);
245
246 Num_total_object_types = PHYSFSX_readInt(fp);
247 PHYSFSX_readBytes(fp, ObjType, MAX_OBJTYPE);
248 {
249 std::array<uint8_t, std::size(ObjId)> o;
250 PHYSFSX_readBytes(fp, o, std::size(o));
251 std::transform(o.begin(), o.end(), ObjId.begin(), build_polygon_model_index_from_untrusted);
252 }
253 range_for (auto &i, ObjStrength)
254 i = PHYSFSX_readFix(fp);
255
256 First_multi_bitmap_num = PHYSFSX_readInt(fp);
257 Reactors[0].n_guns = PHYSFSX_readInt(fp);
258
259 range_for (auto &i, Reactors[0].gun_points)
260 PHYSFSX_readVector(fp, i);
261 range_for (auto &i, Reactors[0].gun_dirs)
262 PHYSFSX_readVector(fp, i);
263
264 exit_modelnum = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
265 destroyed_exit_modelnum = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
266
267 #if DXX_USE_EDITOR
268 //Build tmaplist
269 auto &&effect_range = partial_const_range(Effects, Num_effects);
270 LevelUniqueTmapInfoState.Num_tmaps = TextureEffects + std::count_if(effect_range.begin(), effect_range.end(), [](const eclip &e) { return e.changing_wall_texture >= 0; });
271 #endif
272 }
273 #elif DXX_BUILD_DESCENT == 2
274 namespace {
275 static void tmap_info_read(tmap_info &ti, const NamedPHYSFS_File fp)
276 {
277 uint8_t flags;
278 PHYSFSX_readBytes(fp, &flags, 1);
279 ti.flags = tmapinfo_flags{flags};
280 PHYSFSX_skipBytes<3>(fp);
281 ti.lighting = PHYSFSX_readFix(fp);
282 ti.damage = PHYSFSX_readFix(fp);
283 ti.eclip_num = PHYSFSX_readSLE16(fp);
284 ti.destroyed = PHYSFSX_readSLE16(fp);
285 ti.slide_u = PHYSFSX_readSLE16(fp);
286 ti.slide_v = PHYSFSX_readSLE16(fp);
287 }
288 }
289
290 //-----------------------------------------------------------------
291 // Initializes game properties data (including texture caching system) and sound data.
292 int gamedata_init(d_level_shared_robot_info_state &LevelSharedRobotInfoState)
293 {
294 init_polygon_models(LevelSharedPolygonModelState);
295
296 #if DXX_USE_EDITOR
297 // The pc_shareware argument is currently unused for Descent 2,
298 // but *may* be useful for loading Descent 1 Shareware texture properties.
299 if (!gamedata_read_tbl(LevelSharedRobotInfoState, Vclip, 0))
300 #endif
301 if (properties_init(LevelSharedRobotInfoState) == properties_init_result::failure) // This calls properties_read_cmp
302 Error("Cannot open ham file\n");
303
304 piggy_read_sounds();
305
306 return 0;
307 }
308
309 void bm_read_all(d_level_shared_robot_info_state &LevelSharedRobotInfoState, d_vclip_array &Vclip, const NamedPHYSFS_File fp)
310 {
311 auto &Effects = LevelUniqueEffectsClipState.Effects;
312 auto &Robot_joints = LevelSharedRobotJointState.Robot_joints;
313 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
314 auto &WallAnims = GameSharedState.WallAnims;
315 unsigned t;
316
317 NumTextures = PHYSFSX_readInt(fp);
318 bitmap_index_read_n(fp, partial_range(Textures, NumTextures));
319 range_for (tmap_info &ti, partial_range(TmapInfo, NumTextures))
320 tmap_info_read(ti, fp);
321
322 {
323 const auto t{PHYSFSX_readInt(fp)};
324 PHYSFSX_readBytes(fp, Sounds, t);
325 PHYSFSX_readBytes(fp, AltSounds, t);
326 }
327
328 Num_vclips = PHYSFSX_readInt(fp);
329 range_for (vclip &vc, partial_range(Vclip, Num_vclips))
330 vclip_read(fp, vc);
331
332 Num_effects = PHYSFSX_readInt(fp);
333 range_for (eclip &ec, partial_range(Effects, Num_effects))
334 eclip_read(fp, ec);
335
336 Num_wall_anims = PHYSFSX_readInt(fp);
337 range_for (auto &w, partial_range(WallAnims, Num_wall_anims))
338 wclip_read(fp, w);
339
340 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
341 LevelSharedRobotInfoState.N_robot_types = PHYSFSX_readInt(fp);
342 range_for (auto &r, partial_range(Robot_info, LevelSharedRobotInfoState.N_robot_types))
343 robot_info_read(fp, r);
344
345 const auto N_robot_joints = LevelSharedRobotJointState.N_robot_joints = PHYSFSX_readInt(fp);
346 range_for (auto &r, partial_range(Robot_joints, N_robot_joints))
347 jointpos_read(fp, r);
348
349 N_weapon_types = PHYSFSX_readInt(fp);
350 weapon_info_read_n(Weapon_info, N_weapon_types, fp, Piggy_hamfile_version, 0);
351
352 N_powerup_types = PHYSFSX_readInt(fp);
353 range_for (auto &p, partial_range(Powerup_info, N_powerup_types))
354 powerup_type_info_read(fp, p);
355
356 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
357 const auto N_polygon_models = LevelSharedPolygonModelState.N_polygon_models = PHYSFSX_readInt(fp);
358 {
359 const auto &&r = partial_range(Polygon_models, N_polygon_models);
360 range_for (auto &p, r)
361 polymodel_read(p, fp);
362
363 range_for (auto &p, r)
364 polygon_model_data_read(&p, fp);
365 }
366
367 range_for (auto &i, partial_range(Dying_modelnums, N_polygon_models))
368 i = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
369 range_for (auto &i, partial_range(Dead_modelnums, N_polygon_models))
370 i = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
371
372 t = PHYSFSX_readInt(fp);
373 bitmap_index_read_n(fp, partial_range(Gauges, t));
374 bitmap_index_read_n(fp, partial_range(Gauges_hires, t));
375
376 N_ObjBitmaps = PHYSFSX_readInt(fp);
377 bitmap_index_read_n(fp, partial_range(ObjBitmaps, N_ObjBitmaps));
378 range_for (auto &i, partial_range(ObjBitmapPtrs, N_ObjBitmaps))
379 {
380 if (const auto oi = ObjBitmaps.valid_index(PHYSFSX_readSLE16(fp)))
381 i = {*oi};
382 else
383 i = {};
384 }
385
386 player_ship_read(&only_player_ship, fp);
387
388 Num_cockpits = PHYSFSX_readInt(fp);
389 bitmap_index_read_n(fp, partial_range(cockpit_bitmap, Num_cockpits));
390
391 First_multi_bitmap_num = PHYSFSX_readInt(fp);
392
393 Num_reactors = PHYSFSX_readInt(fp);
394 reactor_read_n(fp, partial_range(Reactors, Num_reactors));
395
396 LevelSharedPolygonModelState.Marker_model_num = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
397
398 if (Piggy_hamfile_version < pig_hamfile_version::_3) { // D1
399 exit_modelnum = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
400 destroyed_exit_modelnum = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
401 }
402 else // D2: to be loaded later
403 exit_modelnum = destroyed_exit_modelnum = polygon_model_index::None;
404 }
405
406 namespace {
407
408 // this and below only really used for D2
409 bool Exit_bitmaps_loaded;
410 unsigned Exit_bitmap_index;
411
412 static void bm_free_extra_objbitmaps()
413 {
414 int i;
415
416 if (!extra_bitmap_num)
417 extra_bitmap_num = Num_bitmap_files;
418
419 for (i = Num_bitmap_files; i < extra_bitmap_num; i++)
420 {
421 N_ObjBitmaps--;
422 d_free(GameBitmaps[(bitmap_index{static_cast<uint16_t>(i)})].bm_mdata);
423 }
424 extra_bitmap_num = Num_bitmap_files;
425 Exit_bitmaps_loaded = false;
426 }
427
428 static void bm_free_extra_models(d_level_shared_polygon_model_state &LevelSharedPolygonModelState)
429 {
430 LevelSharedPolygonModelState.Exit_models_loaded = false;
431 const auto base = std::min<unsigned>(N_D2_POLYGON_MODELS.value, underlying_value(exit_modelnum));
432 for (auto &p : partial_range(LevelSharedPolygonModelState.Polygon_models, base, std::exchange(LevelSharedPolygonModelState.N_polygon_models, base)))
433 free_model(p);
434 }
435
436 }
437
438 //type==1 means 1.1, type==2 means 1.2 (with weapons)
439 void bm_read_extra_robots(const char *fname, const Mission::descent_version_type type)
440 {
441 auto &Robot_joints = LevelSharedRobotJointState.Robot_joints;
442 int t,version;
443
444 auto &&[fp, physfserr] = PHYSFSX_openReadBuffered(fname);
445 if (!fp)
446 {
447 Error("Failed to open HAM file \"%s\": %s", fname, PHYSFS_getErrorByCode(physfserr));
448 return;
449 }
450
451 if (type == Mission::descent_version_type::descent2z)
452 {
453 int sig;
454
455 sig = PHYSFSX_readInt(fp);
456 if (sig != MAKE_SIG('X','H','A','M'))
457 return;
458 version = PHYSFSX_readInt(fp);
459 }
460 else
461 version = 0;
462 (void)version; // NOTE: we do not need it, but keep it for possible further use
463
464 bm_free_extra_models(LevelSharedPolygonModelState);
465 bm_free_extra_objbitmaps();
466
467 //read extra weapons
468
469 t = PHYSFSX_readInt(fp);
470 N_weapon_types = N_D2_WEAPON_TYPES+t;
471 weapon_info_read_n(Weapon_info, N_weapon_types, fp, pig_hamfile_version::_3, N_D2_WEAPON_TYPES);
472
473 //now read robot info
474
475 t = PHYSFSX_readInt(fp);
476 const auto N_robot_types = LevelSharedRobotInfoState.N_robot_types = N_D2_ROBOT_TYPES + t;
477 if (N_robot_types >= MAX_ROBOT_TYPES)
478 Error("Too many robots (%d) in <%s>. Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
479 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
480 range_for (auto &r, partial_range(Robot_info, N_D2_ROBOT_TYPES.value, N_robot_types))
481 robot_info_read(fp, r);
482
483 t = PHYSFSX_readInt(fp);
484 const auto N_robot_joints = LevelSharedRobotJointState.N_robot_joints = N_D2_ROBOT_JOINTS+t;
485 if (N_robot_joints >= MAX_ROBOT_JOINTS)
486 Error("Too many robot joints (%d) in <%s>. Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
487 range_for (auto &r, partial_range(Robot_joints, N_D2_ROBOT_JOINTS.value, N_robot_joints))
488 jointpos_read(fp, r);
489
490 unsigned u = PHYSFSX_readInt(fp);
491 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
492 const auto N_polygon_models = LevelSharedPolygonModelState.N_polygon_models = N_D2_POLYGON_MODELS+u;
493 if (N_polygon_models >= MAX_POLYGON_MODELS)
494 Error("Too many polygon models (%d) in <%s>. Max is %" DXX_PRI_size_type ".", u, fname, MAX_POLYGON_MODELS - N_D2_POLYGON_MODELS);
495 {
496 const auto &&r = partial_range(Polygon_models, N_D2_POLYGON_MODELS.value, N_polygon_models);
497 range_for (auto &p, r)
498 polymodel_read(p, fp);
499
500 range_for (auto &p, r)
501 polygon_model_data_read(&p, fp);
502 }
503
504 range_for (auto &i, partial_range(Dying_modelnums, N_D2_POLYGON_MODELS.value, N_polygon_models))
505 i = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
506 range_for (auto &i, partial_range(Dead_modelnums, N_D2_POLYGON_MODELS.value, N_polygon_models))
507 i = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
508
509 t = PHYSFSX_readInt(fp);
510 if (N_D2_OBJBITMAPS+t >= ObjBitmaps.size())
511 Error("Too many object bitmaps (%d) in <%s>. Max is %" DXX_PRI_size_type ".", t, fname, ObjBitmaps.size() - N_D2_OBJBITMAPS);
512 bitmap_index_read_n(fp, partial_range(ObjBitmaps, N_D2_OBJBITMAPS.value, N_D2_OBJBITMAPS + t));
513
514 t = PHYSFSX_readInt(fp);
515 if (N_D2_OBJBITMAPPTRS+t >= ObjBitmapPtrs.size())
516 Error("Too many object bitmap pointers (%d) in <%s>. Max is %" DXX_PRI_size_type ".", t, fname, ObjBitmapPtrs.size() - N_D2_OBJBITMAPPTRS);
517 range_for (auto &i, partial_range(ObjBitmapPtrs, N_D2_OBJBITMAPPTRS.value, N_D2_OBJBITMAPPTRS + t))
518 {
519 if (const auto oi = ObjBitmaps.valid_index(PHYSFSX_readSLE16(fp)))
520 i = {*oi};
521 else
522 i = {};
523 }
524 }
525
526 int Robot_replacements_loaded{0};
527
528 void load_robot_replacements(const d_fname &level_name)
529 {
530 auto &Robot_joints = LevelSharedRobotJointState.Robot_joints;
531 int t,j;
532 std::array<char, FILENAME_LEN> ifile_name;
533 if (!change_filename_extension(ifile_name, level_name, "HXM"))
534 {
535 con_printf(CON_URGENT, "Failed to generate HXM name from level name \"%s\"", level_name.data());
536 return;
537 }
538
539 auto fp{PHYSFSX_openReadBuffered_updateCase(ifile_name.data()).first};
540 if (!fp) //no robot replacement file
541 return;
542
543 t = PHYSFSX_readInt(fp); //read id "HXM!"
544 if (t!= 0x21584d48)
545 Error("ID of HXM! file incorrect");
546
547 t = PHYSFSX_readInt(fp); //read version
548 if (t<1)
549 Error("HXM! version too old (%d)",t);
550
551 t = PHYSFSX_readInt(fp); //read number of robots
552 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
553 const auto N_robot_types = LevelSharedRobotInfoState.N_robot_types;
554 for (j=0;j<t;j++) {
555 const unsigned i = PHYSFSX_readInt(fp); //read robot number
556 if (i >= N_robot_types)
557 Error("Robots number (%u) out of range in (%s). Range = [0..%u].", i, static_cast<const char *>(level_name), N_robot_types- 1);
558 robot_info_read(fp, Robot_info[static_cast<robot_id>(i)]);
559 }
560
561 t = PHYSFSX_readInt(fp); //read number of joints
562 const auto N_robot_joints = LevelSharedRobotJointState.N_robot_joints;
563 for (j=0;j<t;j++) {
564 const unsigned i = PHYSFSX_readInt(fp); //read joint number
565 if (i >= N_robot_joints)
566 Error("Robots joint (%u) out of range in (%s). Range = [0..%u].", i, static_cast<const char *>(level_name), N_robot_joints - 1);
567 jointpos_read(fp, Robot_joints[i]);
568 }
569
570 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
571 const auto N_polygon_models = LevelSharedPolygonModelState.N_polygon_models;
572 t = PHYSFSX_readInt(fp); //read number of polygon models
573 for (j=0;j<t;j++)
574 {
575 const unsigned i = PHYSFSX_readInt(fp); //read model number
576 const auto pmi = build_polygon_model_index_from_untrusted(i);
577 if (underlying_value(pmi) >= N_polygon_models)
578 Error("Polygon model (%u) out of range in (%s). Range = [0..%u].", i, static_cast<const char *>(level_name), N_polygon_models - 1);
579
580 auto &m = Polygon_models[pmi];
581 polymodel_read(m, fp);
582 polygon_model_data_read(&m, fp);
583
584 Dying_modelnums[pmi] = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
585 Dead_modelnums[pmi] = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
586 }
587
588 t = PHYSFSX_readInt(fp); //read number of objbitmaps
589 for (j=0;j<t;j++) {
590 const auto foi = PHYSFSX_readInt(fp); //read objbitmap number
591 if (const auto oi = ObjBitmaps.valid_index(foi))
592 bitmap_index_read(fp, ObjBitmaps[*oi]);
593 else
594 Error("Object bitmap number (%u) out of range in (%s). Range = [0..%" DXX_PRI_size_type "].", foi, static_cast<const char *>(level_name), ObjBitmaps.size() - 1);
595 }
596
597 t = PHYSFSX_readInt(fp); //read number of objbitmapptrs
598 for (j=0;j<t;j++) {
599 const unsigned i = PHYSFSX_readInt(fp); //read objbitmapptr number
600 if (i >= ObjBitmapPtrs.size())
601 Error("Object bitmap pointer (%u) out of range in (%s). Range = [0..%" DXX_PRI_size_type "].", i, static_cast<const char *>(level_name), ObjBitmapPtrs.size() - 1);
602 const auto foi = PHYSFSX_readSLE16(fp);
603 if (const auto oi = ObjBitmaps.valid_index(foi))
604 ObjBitmapPtrs[i] = *oi;
605 else
606 Error("Object bitmap number (%u) out of range in (%s). Range = [0..%" DXX_PRI_size_type "].", foi, static_cast<const char *>(level_name), ObjBitmaps.size() - 1);
607 }
608 Robot_replacements_loaded = 1;
609 }
610
611 namespace {
612
613 /*
614 * Routines for loading exit models
615 *
616 * Used by d1 levels (including some add-ons), and by d2 shareware.
617 * Could potentially be used by d2 add-on levels, but only if they
618 * don't use "extra" robots... or maybe they do
619 */
620
621 // formerly exitmodel_bm_load_sub
622 static grs_bitmap *read_extra_bitmap_iff(const char * filename, grs_bitmap &n)
623 {
624 palette_array_t newpal;
625 if (const auto iff_error{iff_read_bitmap(filename, n, &newpal)}; iff_error != iff_status_code::no_error)
626 {
627 con_printf(CON_DEBUG, "Error loading exit model bitmap <%s> - IFF error: %s", filename, iff_errormsg(iff_error));
628 return nullptr;
629 }
630
631 gr_remap_bitmap_good(n, newpal, iff_has_transparency ? iff_transparent_color : -1, 254);
632
633 #if !DXX_USE_OGL
634 n.avg_color = 0; //compute_average_pixel(new);
635 #endif
636 return &n;
637 }
638
639 // formerly load_exit_model_bitmap
640 static grs_bitmap *bm_load_extra_objbitmap(const char *name)
641 {
642 if (const auto ooi = ObjBitmaps.valid_index(N_ObjBitmaps); !ooi)
643 return nullptr;
644 else
645 {
646 const auto oi = *ooi;
647 auto &bitmap_idx = ObjBitmaps[oi];
648 const auto bitmap_store_index = bitmap_index{static_cast<uint16_t>(extra_bitmap_num)};
649 grs_bitmap &n = GameBitmaps[bitmap_store_index];
650 if (!read_extra_bitmap_iff(name, n))
651 {
652 const char *const dot = strrchr(name, '.');
653 if (const auto r = read_extra_bitmap_d1_pig(std::span<const char>(name, dot ? std::distance(name, dot) : 8), n); !r)
654 return r;
655 }
656 bitmap_idx = bitmap_store_index;
657 ++ extra_bitmap_num;
658
659 if (n.bm_w != 64 || n.bm_h != 64)
660 Error("Bitmap <%s> is not 64x64",name);
661 ObjBitmapPtrs[N_ObjBitmaps] = oi;
662 N_ObjBitmaps++;
663 assert(N_ObjBitmaps < ObjBitmaps.size());
664 return &n;
665 }
666 }
667
668 static void bm_unload_last_objbitmaps(unsigned count)
669 {
670 assert(N_ObjBitmaps >= count);
671
672 unsigned new_N_ObjBitmaps = N_ObjBitmaps - count;
673 range_for (auto &o, partial_range(ObjBitmaps, new_N_ObjBitmaps, N_ObjBitmaps))
674 d_free(GameBitmaps[o].bm_mdata);
675 N_ObjBitmaps = new_N_ObjBitmaps;
676 }
677
678 }
679
680 // only called for D2 registered, but there is a D1 check anyway for
681 // possible later use
682 int load_exit_models()
683 {
684 int start_num;
685
686 /*
687 don't free extra models in native D2 mode -- ziplantil. it's our
688 responsibility to make sure the exit stuff is already loaded rather than
689 loading it all again.
690
691 however, in D1 mode, we always need to reload everything due to how
692 the exit data is loaded (which is different from D2 native mode)
693 */
694 if (EMULATING_D1) // D1?
695 {
696 bm_free_extra_models(LevelSharedPolygonModelState);
697 bm_free_extra_objbitmaps();
698 }
699 else if (!Exit_bitmaps_loaded)
700 {
701 extra_bitmap_num = Num_bitmap_files;
702 }
703
704 // make sure there is enough space to load textures and models
705 if (!Exit_bitmaps_loaded && N_ObjBitmaps > ObjBitmaps.size() - 6)
706 {
707 return 0;
708 }
709 if (!LevelSharedPolygonModelState.Exit_models_loaded && LevelSharedPolygonModelState.N_polygon_models > MAX_POLYGON_MODELS - 2)
710 {
711 return 0;
712 }
713
714 start_num = N_ObjBitmaps;
715 if (!Exit_bitmaps_loaded)
716 {
717 if (!bm_load_extra_objbitmap("steel1.bbm") ||
718 !bm_load_extra_objbitmap("rbot061.bbm") ||
719 !bm_load_extra_objbitmap("rbot062.bbm") ||
720 !bm_load_extra_objbitmap("steel1.bbm") ||
721 !bm_load_extra_objbitmap("rbot061.bbm") ||
722 !bm_load_extra_objbitmap("rbot063.bbm"))
723 {
724 // unload the textures that we already loaded
725 bm_unload_last_objbitmaps(N_ObjBitmaps - start_num);
726 con_puts(CON_NORMAL, "Can't load exit models!");
727 return 0;
728 }
729 Exit_bitmap_index = start_num;
730 }
731
732 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
733 const auto N_polygon_models = LevelSharedPolygonModelState.N_polygon_models;
734 if (LevelSharedPolygonModelState.Exit_models_loaded && underlying_value(exit_modelnum) < N_polygon_models && underlying_value(destroyed_exit_modelnum) < N_polygon_models)
735 {
736 // already loaded, just adjust texture indexes
737 Polygon_models[exit_modelnum].first_texture = Exit_bitmap_index;
738 Polygon_models[destroyed_exit_modelnum].first_texture = Exit_bitmap_index+3;
739 return 1;
740 }
741
742 static char exit_ham_basename[]{"exit.ham"};
743 if (auto exit_hamfile{PHYSFSX_openReadBuffered(exit_ham_basename).first})
744 {
745 const auto em = static_cast<polygon_model_index>(N_polygon_models);
746 const auto dem = static_cast<polygon_model_index>(N_polygon_models + 1);
747 LevelSharedPolygonModelState.N_polygon_models = N_polygon_models + 2;
748 exit_modelnum = em;
749 destroyed_exit_modelnum = dem;
750 auto &pem = Polygon_models[em];
751 auto &pdem = Polygon_models[dem];
752 polymodel_read(pem, exit_hamfile);
753 polymodel_read(pdem, exit_hamfile);
754 pem.first_texture = start_num;
755 pdem.first_texture = start_num+3;
756
757 polygon_model_data_read(&pem, exit_hamfile);
758 polygon_model_data_read(&pdem, exit_hamfile);
759 }
760 else if (PHYSFS_exists("exit01.pof") && PHYSFS_exists("exit01d.pof"))
761 {
762 exit_modelnum = load_polygon_model("exit01.pof", 3, start_num, NULL);
763 destroyed_exit_modelnum = load_polygon_model("exit01d.pof", 3, start_num + 3, NULL);
764
765 #if DXX_USE_OGL
766 ogl_cache_polymodel_textures(exit_modelnum);
767 ogl_cache_polymodel_textures(destroyed_exit_modelnum);
768 #endif
769 }
770 else if ((exit_hamfile = PHYSFSX_openReadBuffered_updateCase(descent_pig_basename).first))
771 {
772 int offset, offset2;
773 switch (descent1_pig_size{PHYSFS_fileLength(exit_hamfile)})
774 { //total hack for loading models
775 case descent1_pig_size::d1_pigsize:
776 offset = 91848; /* and 92582 */
777 offset2 = 383390; /* and 394022 */
778 break;
779 default:
780 case descent1_pig_size::d1_share_big_pigsize:
781 case descent1_pig_size::d1_share_10_pigsize:
782 case descent1_pig_size::d1_share_pigsize:
783 case descent1_pig_size::d1_10_big_pigsize:
784 case descent1_pig_size::d1_10_pigsize:
785 Int3(); /* exit models should be in .pofs */
786 [[fallthrough]];
787 case descent1_pig_size::d1_oem_pigsize:
788 case descent1_pig_size::d1_mac_pigsize:
789 case descent1_pig_size::d1_mac_share_pigsize:
790 // unload the textures that we already loaded
791 bm_unload_last_objbitmaps(N_ObjBitmaps - start_num);
792 con_puts(CON_NORMAL, "Can't load exit models!");
793 return 0;
794 }
795 PHYSFS_seek(exit_hamfile, offset);
796 const auto em = static_cast<polygon_model_index>(N_polygon_models);
797 const auto dem = static_cast<polygon_model_index>(N_polygon_models + 1);
798 LevelSharedPolygonModelState.N_polygon_models = N_polygon_models + 2;
799 exit_modelnum = em;
800 destroyed_exit_modelnum = dem;
801 auto &pem = Polygon_models[em];
802 auto &pdem = Polygon_models[dem];
803 polymodel_read(pem, exit_hamfile);
804 polymodel_read(pdem, exit_hamfile);
805 pem.first_texture = start_num;
806 pdem.first_texture = start_num+3;
807
808 PHYSFS_seek(exit_hamfile, offset2);
809 polygon_model_data_read(&pem, exit_hamfile);
810 polygon_model_data_read(&pdem, exit_hamfile);
811 } else {
812 // unload the textures that we already loaded
813 bm_unload_last_objbitmaps(N_ObjBitmaps - start_num);
814 con_puts(CON_NORMAL, "Can't load exit models!");
815 return 0;
816 }
817
818 // set to be loaded, but only on D2 - always reload the data on D1
819 LevelSharedPolygonModelState.Exit_models_loaded = Exit_bitmaps_loaded = !EMULATING_D1;
820 return 1;
821 }
822 #endif
823
824 }
825
826 void compute_average_rgb(grs_bitmap *bm, std::array<fix, 3> &rgb)
827 {
828 rgb = {};
829 if (unlikely(!bm->get_bitmap_data()))
830 return;
831 const uint_fast32_t bm_h = bm->bm_h;
832 const uint_fast32_t bm_w = bm->bm_w;
833 if (unlikely(!bm_h) || unlikely(!bm_w))
834 return;
835
836 const auto process_one = [&rgb](uint8_t color) {
837 if (color == TRANSPARENCY_COLOR)
838 return;
839 auto &t_rgb = gr_palette[color];
840 if (t_rgb.r == t_rgb.g && t_rgb.r == t_rgb.b)
841 return;
842 rgb[0] += t_rgb.r;
843 rgb[1] += t_rgb.g;
844 rgb[2] += t_rgb.b;
845 };
846 if (bm->get_flag_mask(BM_FLAG_RLE))
847 {
848 bm_rle_expand expander(*bm);
849 const auto &&buf = std::make_unique<uint8_t[]>(bm_w);
850 range_for (const uint_fast32_t i, xrange(bm_h))
851 {
852 (void)i;
853 const auto &&range = unchecked_partial_range(buf.get(), bm_w);
854 if (expander.step(bm_rle_expand_range(range.begin(), range.end())) != bm_rle_expand::again)
855 break;
856 range_for (const auto color, range)
857 process_one(color);
858 }
859 }
860 else
861 {
862 range_for (const auto color, unchecked_partial_range(bm->bm_data, bm_w * bm_h))
863 process_one(color);
864 }
865 }