Debian Codegraph @

similar/main/gamemine.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 * Functions for loading mines in the game
23 *
24 */
25
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <string.h>
31
32 #include "pstypes.h"
33 #include "inferno.h"
34 #include "segment.h"
35 #include "textures.h"
36 #include "wall.h"
37 #include "object.h"
38 #include "gamemine.h"
39 #include "dxxerror.h"
40 #include "gameseg.h"
41 #include "physfsx.h"
42 #include "switch.h"
43 #include "game.h"
44 #include "fuelcen.h"
45 #include "hash.h"
46 #include "piggy.h"
47 #include "gamesave.h"
48 #include "compiler-poison.h"
49 #include "compiler-range_for.h"
50 #include "d_levelstate.h"
51 #include "partial_range.h"
52
53 namespace dcx {
54
55 namespace {
56
57 static uint8_t New_file_format_load{1}; // "new file format" is everything newer than d1 shareware
58
59 }
60
61 }
62
63 namespace dsx {
64
65 namespace {
66
67 static segment_special build_segment_special_from_untrusted(uint8_t untrusted)
68 {
69 switch (untrusted)
70 {
71 case static_cast<uint8_t>(segment_special::nothing):
72 case static_cast<uint8_t>(segment_special::fuelcen):
73 case static_cast<uint8_t>(segment_special::repaircen):
74 case static_cast<uint8_t>(segment_special::controlcen):
75 case static_cast<uint8_t>(segment_special::robotmaker):
76 #if DXX_BUILD_DESCENT == 2
77 case static_cast<uint8_t>(segment_special::goal_blue):
78 case static_cast<uint8_t>(segment_special::goal_red):
79 #endif
80 return segment_special{untrusted};
81 default:
82 return segment_special::nothing;
83 }
84 }
85
86 static materialization_center_number build_materialization_center_number_from_untrusted(uint8_t untrusted)
87 {
88 if (const auto o = decltype(d_level_shared_robotcenter_state::RobotCenters)::valid_index(untrusted))
89 return *o;
90 else
91 return materialization_center_number::None;
92 }
93
94 static station_number build_station_number_from_untrusted(uint8_t untrusted)
95 {
96 if (const auto o = decltype(d_level_unique_fuelcenter_state::Station)::valid_index(untrusted))
97 return *o;
98 else
99 return station_number::None;
100 }
101
102 /*
103 * reads a segment2 structure from a PHYSFS_File
104 */
105 static void segment2_read(const msmusegment s2, const NamedPHYSFS_File fp)
106 {
107 s2.s.special = build_segment_special_from_untrusted(PHYSFSX_readByte(fp));
108 s2.s.matcen_num = build_materialization_center_number_from_untrusted(PHYSFSX_readByte(fp));
109 /* station_idx is overwritten by the caller in some cases, but set
110 * it here for compatibility with how the game previously worked */
111 s2.s.station_idx = build_station_number_from_untrusted(PHYSFSX_readByte(fp));
112 const auto s2_flags = PHYSFSX_readByte(fp);
113 /* Ambient sounds are recomputed by the level load code. Ignore the value
114 * read from the file.
115 */
116 (void)s2_flags;
117 s2.s.s2_flags = {};
118 s2.u.static_light = PHYSFSX_readFix(fp);
119 }
120
121 }
122
123 #if DXX_BUILD_DESCENT == 1
124 #elif DXX_BUILD_DESCENT == 2
125
126 uint8_t d1_pig_present{0}; // can descent.pig from descent 1 be loaded?
127
128 namespace {
129
130 /* Converts descent 1 texture numbers to descent 2 texture numbers.
131 * Textures from d1 which are unique to d1 have extra spaces around "return".
132 * If we can load the original d1 pig, we make sure this function is bijective.
133 * This function was updated using the file config/convtabl.ini from devil 2.2.
134 */
135 static texture_index convert_d1_tmap_num_unrotated(const texture_index d1_tmap_num)
136 {
137 switch (d1_tmap_num) {
138 case 0: case 2: case 4: case 5:
139 // all refer to grey rock001 (exception to bijectivity rule)
140 return d1_pig_present ? 137 : 43; // (devil:95)
141 case 1: return 0;
142 case 3: return 1; // rock021
143 case 6: return 270; // blue rock002
144 case 7: return 271; // yellow rock265
145 case 8: return 2; // rock004
146 case 9: return d1_pig_present ? 138 : 62; // purple (devil:179)
147 case 10: return 272; // red rock006
148 case 11: return d1_pig_present ? 139 : 117;
149 case 12: return d1_pig_present ? 140 : 12; //devil:43
150 case 13: return 3; // rock014
151 case 14: return 4; // rock019
152 case 15: return 5; // rock020
153 case 16: return 6;
154 case 17: return d1_pig_present ? 141 : 52;
155 case 18: return 129;
156 case 19: return 7;
157 case 20: return d1_pig_present ? 142 : 22;
158 case 21: return d1_pig_present ? 143 : 9;
159 case 22: return 8;
160 case 23: return 9;
161 case 24: return 10;
162 case 25: return d1_pig_present ? 144 : 12; //devil:35
163 case 26: return 11;
164 case 27: return 12;
165 case 28: return d1_pig_present ? 145 : 11; //devil:43
166 //range handled by default case, returns 13..21 (- 16)
167 case 38: return 163; //devil:27
168 case 39: return 147; //31
169 case 40: return 22;
170 case 41: return 266;
171 case 42: return 23;
172 case 43: return 24;
173 case 44: return 136; //devil:135
174 case 45: return 25;
175 case 46: return 26;
176 case 47: return 27;
177 case 48: return 28;
178 case 49: return d1_pig_present ? 146 : 43; //devil:60
179 case 50: return 131; //devil:138
180 case 51: return 29;
181 case 52: return 30;
182 case 53: return 31;
183 case 54: return 32;
184 case 55: return 165; //devil:193
185 case 56: return 33;
186 case 57: return 132; //devil:119
187 // range handled by default case, returns 34..63 (- 24)
188 case 88: return 197; //devil:15
189 // range handled by default case, returns 64..106 (- 25)
190 case 132: return 167;
191 // range handled by default case, returns 107..114 (- 26)
192 case 141: return d1_pig_present ? 148 : 110; //devil:106
193 case 142: return 115;
194 case 143: return 116;
195 case 144: return 117;
196 case 145: return 118;
197 case 146: return 119;
198 case 147: return d1_pig_present ? 149 : 93;
199 case 148: return 120;
200 case 149: return 121;
201 case 150: return 122;
202 case 151: return 123;
203 case 152: return 124;
204 case 153: return 125; // rock263
205 case 154: return d1_pig_present ? 150 : 27;
206 case 155: return 126; // rock269
207 case 156: return 200; // metl002
208 case 157: return 201; // metl003
209 case 158: return 186; //devil:227
210 case 159: return 190; //devil:246
211 case 160: return d1_pig_present ? 151 : 206;
212 case 161: return d1_pig_present ? 152 : 114; //devil:206
213 case 162: return 202;
214 case 163: return 203;
215 case 164: return 204;
216 case 165: return 205;
217 case 166: return 206;
218 case 167: return d1_pig_present ? 153 : 206;
219 case 168: return d1_pig_present ? 154 : 206;
220 case 169: return d1_pig_present ? 155 : 206;
221 case 170: return d1_pig_present ? 156 : 227;//206;
222 case 171: return d1_pig_present ? 157 : 206;//227;
223 case 172: return 207;
224 case 173: return 208;
225 case 174: return d1_pig_present ? 158 : 202;
226 case 175: return d1_pig_present ? 159 : 206;
227 // range handled by default case, returns 209..217 (+ 33)
228 case 185: return d1_pig_present ? 160 : 217;
229 // range handled by default case, returns 218..224 (+ 32)
230 case 193: return d1_pig_present ? 161 : 206;
231 case 194: return d1_pig_present ? 162 : 203;//206;
232 case 195: return d1_pig_present ? 166 : 234;
233 case 196: return 225;
234 case 197: return 226;
235 case 198: return d1_pig_present ? 193 : 225;
236 case 199: return d1_pig_present ? 168 : 206; //devil:204
237 case 200: return d1_pig_present ? 169 : 206; //devil:204
238 case 201: return 227;
239 case 202: return d1_pig_present ? 170 : 206; //devil:227
240 // range handled by default case, returns 228..234 (+ 25)
241 case 210: return d1_pig_present ? 171 : 234; //devil:242
242 case 211: return d1_pig_present ? 172 : 206; //devil:240
243 // range handled by default case, returns 235..242 (+ 23)
244 case 220: return d1_pig_present ? 173 : 242; //devil:240
245 case 221: return 243;
246 case 222: return 244;
247 case 223: return d1_pig_present ? 174 : 313;
248 case 224: return 245;
249 case 225: return 246;
250 case 226: return 164;//247; matching names but not matching textures
251 case 227: return 179; //devil:181
252 case 228: return 196;//248; matching names but not matching textures
253 case 229: return d1_pig_present ? 175 : 15; //devil:66
254 case 230: return d1_pig_present ? 176 : 15; //devil:66
255 // range handled by default case, returns 249..257 (+ 18)
256 case 240: return d1_pig_present ? 177 : 6; //devil:132
257 case 241: return 130; //devil:131
258 case 242: return d1_pig_present ? 178 : 78; //devil:15
259 case 243: return d1_pig_present ? 180 : 33; //devil:38
260 case 244: return 258;
261 case 245: return 259;
262 case 246: return d1_pig_present ? 181 : 321; // grate metl127
263 case 247: return 260;
264 case 248: return 261;
265 case 249: return 262;
266 case 250: return 340; // white doorframe metl126
267 case 251: return 412; // red doorframe metl133
268 case 252: return 410; // blue doorframe metl134
269 case 253: return 411; // yellow doorframe metl135
270 case 254: return 263; // metl136
271 case 255: return 264; // metl139
272 case 256: return 265; // metl140
273 case 257: return d1_pig_present ? 182 : 249;//246; brig001
274 case 258: return d1_pig_present ? 183 : 251;//246; brig002
275 case 259: return d1_pig_present ? 184 : 252;//246; brig003
276 case 260: return d1_pig_present ? 185 : 256;//246; brig004
277 case 261: return 273; // exit01
278 case 262: return 274; // exit02
279 case 263: return d1_pig_present ? 187 : 281; // ceil001
280 case 264: return 275; // ceil002
281 case 265: return 276; // ceil003
282 case 266: return d1_pig_present ? 188 : 279; //devil:291
283 // range handled by default case, returns 277..291 (+ 10)
284 case 282: return 293;
285 case 283: return d1_pig_present ? 189 : 295;
286 case 284: return 295;
287 case 285: return 296;
288 case 286: return 298;
289 // range handled by default case, returns 300..310 (+ 13)
290 case 298: return d1_pig_present ? 191 : 364; // devil:374 misc010
291 // range handled by default case, returns 311..326 (+ 12)
292 case 315: return d1_pig_present ? 192 : 361; // bad producer misc044
293 // range handled by default case, returns 327..337 (+ 11)
294 case 327: return 352; // arw01
295 case 328: return 353; // misc17
296 case 329: return 354; // fan01
297 case 330: return 380; // mntr04
298 case 331: return 379;//373; matching names but not matching textures
299 case 332: return 355;//344; matching names but not matching textures
300 case 333: return 409; // lava misc11 //devil:404
301 case 334: return 356; // ctrl04
302 case 335: return 357; // ctrl01
303 case 336: return 358; // ctrl02
304 case 337: return 359; // ctrl03
305 case 338: return 360; // misc14
306 case 339: return 361; // producer misc16
307 case 340: return 362; // misc049
308 case 341: return 364; // misc060
309 case 342: return 363; // blown01
310 case 343: return 366; // misc061
311 case 344: return 365;
312 case 345: return 368;
313 case 346: return 376;
314 case 347: return 370;
315 case 348: return 367;
316 case 349: return 372;
317 case 350: return 369;
318 case 351: return 374;//429; matching names but not matching textures
319 case 352: return 375;//387; matching names but not matching textures
320 case 353: return 371;
321 case 354: return 377;//425; matching names but not matching textures
322 case 355: return 408;
323 case 356: return 378; // lava02
324 case 357: return 383;//384; matching names but not matching textures
325 case 358: return 384;//385; matching names but not matching textures
326 case 359: return 385;//386; matching names but not matching textures
327 case 360: return 386;
328 case 361: return 387;
329 case 362: return d1_pig_present ? 194 : 388; // mntr04b (devil: -1)
330 case 363: return 388;
331 case 364: return 391;
332 case 365: return 392;
333 case 366: return 393;
334 case 367: return 394;
335 case 368: return 395;
336 case 369: return 396;
337 case 370: return d1_pig_present ? 195 : 392; // mntr04d (devil: -1)
338 case 570: return 635;
339 // range 371..584 handled by default case (wall01 and door frames)
340 default:
341 // ranges:
342 if (d1_tmap_num >= 29 && d1_tmap_num <= 37)
343 return d1_tmap_num - 16;
344 if (d1_tmap_num >= 58 && d1_tmap_num <= 87)
345 return d1_tmap_num - 24;
346 if (d1_tmap_num >= 89 && d1_tmap_num <= 131)
347 return d1_tmap_num - 25;
348 if (d1_tmap_num >= 133 && d1_tmap_num <= 140)
349 return d1_tmap_num - 26;
350 if (d1_tmap_num >= 176 && d1_tmap_num <= 184)
351 return d1_tmap_num + 33;
352 if (d1_tmap_num >= 186 && d1_tmap_num <= 192)
353 return d1_tmap_num + 32;
354 if (d1_tmap_num >= 203 && d1_tmap_num <= 209)
355 return d1_tmap_num + 25;
356 if (d1_tmap_num >= 212 && d1_tmap_num <= 219)
357 return d1_tmap_num + 23;
358 if (d1_tmap_num >= 231 && d1_tmap_num <= 239)
359 return d1_tmap_num + 18;
360 if (d1_tmap_num >= 267 && d1_tmap_num <= 281)
361 return d1_tmap_num + 10;
362 if (d1_tmap_num >= 287 && d1_tmap_num <= 297)
363 return d1_tmap_num + 13;
364 if (d1_tmap_num >= 299 && d1_tmap_num <= 314)
365 return d1_tmap_num + 12;
366 if (d1_tmap_num >= 316 && d1_tmap_num <= 326)
367 return d1_tmap_num + 11; // matching names but not matching textures
368 // wall01 and door frames:
369 if (d1_tmap_num > 370 && d1_tmap_num < 584) {
370 if (New_file_format_load) return d1_tmap_num + 64;
371 // d1 shareware needs special treatment:
372 if (d1_tmap_num < 410) return d1_tmap_num + 68;
373 if (d1_tmap_num < 417) return d1_tmap_num + 73;
374 if (d1_tmap_num < 446) return d1_tmap_num + 91;
375 if (d1_tmap_num < 453) return d1_tmap_num + 104;
376 if (d1_tmap_num < 462) return d1_tmap_num + 111;
377 if (d1_tmap_num < 486) return d1_tmap_num + 117;
378 if (d1_tmap_num < 494) return d1_tmap_num + 141;
379 if (d1_tmap_num < 584) return d1_tmap_num + 147;
380 }
381 Warning("Failed to convert unknown Descent 1 texture #%d.", d1_tmap_num);
382 return d1_tmap_num;
383 }
384 }
385
386 }
387
388 texture_index convert_d1_tmap_num(const texture_index d1_tmap_num)
389 {
390 constexpr uint16_t TMAP_NUM_MASK{0x3fff};
391 /* Mask off the rotation bits, if any. Convert the base texture number.
392 * Restore the masked rotation bits.
393 */
394 return convert_d1_tmap_num_unrotated(d1_tmap_num & TMAP_NUM_MASK) | (d1_tmap_num & ~TMAP_NUM_MASK);
395 }
396 #endif
397
398 }
399
400 #if DXX_USE_EDITOR
401 namespace dsx {
402 tmap_xlate_table_array tmap_xlate_table;
403 }
404 struct mtfi mine_top_fileinfo; // Should be same as first two fields below...
405 struct mfi mine_fileinfo;
406 struct mh mine_header;
407 struct me mine_editor;
408
409 // -----------------------------------------------------------------------------
410 //loads from an already-open file
411 // returns 0=everything ok, 1=old version, -1=error
412 #endif
413
414 #define COMPILED_MINE_VERSION 0
415
416 namespace {
417
418 static void read_children(shared_segment &segp, const sidemask_t bit_mask, const NamedPHYSFS_File LoadFile)
419 {
420 for (const auto bit : MAX_SIDES_PER_SEGMENT)
421 {
422 if (bit_mask & build_sidemask(bit))
423 {
424 const segnum_t child_segment{PHYSFSX_readULE16(LoadFile)};
425 segp.children[bit] = unlikely(child_segment == segment_exit)
426 ? child_segment
427 : vmsegidx_t::check_nothrow_index(child_segment).value_or(segment_none);
428 } else
429 segp.children[bit] = segment_none;
430 }
431 }
432
433 static void read_verts(shared_segment &segp, const NamedPHYSFS_File LoadFile)
434 {
435 // Read short Segments[segnum].verts[MAX_VERTICES_PER_SEGMENT]
436 range_for (auto &v, segp.verts)
437 {
438 const std::size_t i{PHYSFSX_readULE16(LoadFile)};
439 if (i >= MAX_VERTICES)
440 throw std::invalid_argument("vertex number too large");
441 v = static_cast<vertnum_t>(i);
442 }
443 }
444
445 static void read_special(shared_segment &segp, const sidemask_t bit_mask, const NamedPHYSFS_File LoadFile)
446 {
447 if (bit_mask & build_sidemask(MAX_SIDES_PER_SEGMENT))
448 {
449 // Read ubyte Segments[segnum].special
450 segp.special = build_segment_special_from_untrusted(PHYSFSX_readByte(LoadFile));
451 // Read byte Segments[segnum].matcen_num
452 segp.matcen_num = build_materialization_center_number_from_untrusted(PHYSFSX_readByte(LoadFile));
453 // Read short Segments[segnum].value
454 segp.station_idx = build_station_number_from_untrusted(PHYSFSX_readSLE16(LoadFile));
455 } else {
456 segp.special = segment_special::nothing;
457 segp.matcen_num = materialization_center_number::None;
458 segp.station_idx = station_number::None;
459 }
460 }
461
462 }
463
464 namespace dsx {
465
466 int load_mine_data_compiled(const NamedPHYSFS_File LoadFile, const char *const Gamesave_current_filename)
467 {
468 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
469 auto &Vertices = LevelSharedVertexState.get_vertices();
470 ubyte compiled_version;
471 short temp_short;
472
473 #if DXX_BUILD_DESCENT == 2
474 LevelSharedSeismicState.Level_shake_frequency = 0;
475 LevelSharedSeismicState.Level_shake_duration = 0;
476 d1_pig_present = PHYSFSX_exists_ignorecase(descent_pig_basename);
477 #endif
478 if (!strcmp(strchr(Gamesave_current_filename, '.'), ".sdl"))
479 New_file_format_load = 0; // descent 1 shareware
480 else
481 New_file_format_load = 1;
482
483 // For compiled levels, textures map to themselves, prevent tmap_override always being gray,
484 // bug which Matt and John refused to acknowledge, so here is Mike, fixing it.
485 //
486 // Although in a cloud of arrogant glee, he forgot to ifdef it on EDITOR!
487 // (Matt told me to write that!)
488 #if DXX_USE_EDITOR
489 for (int i=0; i<MAX_TEXTURES; i++)
490 tmap_xlate_table[i] = i;
491 #endif
492
493 // memset( Segments, 0, sizeof(segment)*MAX_SEGMENTS );
494 fuelcen_reset();
495
496 //=============================== Reading part ==============================
497 compiled_version = PHYSFSX_readByte(LoadFile);
498 (void)compiled_version;
499
500 DXX_POISON_VAR(Vertices, 0xfc);
501 const unsigned Num_vertices = New_file_format_load
502 ? PHYSFSX_readSLE16(LoadFile)
503 : PHYSFSX_readInt(LoadFile);
504 assert(Num_vertices <= MAX_VERTICES);
505 #if DXX_USE_EDITOR
506 LevelSharedVertexState.Num_vertices = Num_vertices;
507 #endif
508
509 DXX_POISON_VAR(Segments, 0xfc);
510 if (New_file_format_load)
511 LevelSharedSegmentState.Num_segments = PHYSFSX_readSLE16(LoadFile);
512 else
513 LevelSharedSegmentState.Num_segments = PHYSFSX_readInt(LoadFile);
514 assert(LevelSharedSegmentState.Num_segments <= MAX_SEGMENTS);
515
516 range_for (auto &i, partial_range(Vertices, Num_vertices))
517 PHYSFSX_readVector(LoadFile, i);
518
519 const auto Num_segments = LevelSharedSegmentState.Num_segments;
520 /* Editor builds need both the segment index and segment pointer.
521 * Non-editor builds need only the segment pointer.
522 */
523 #if DXX_USE_EDITOR
524 #define dxx_segment_iteration_factory vmptridx
525 #else
526 #define dxx_segment_iteration_factory vmptr
527 #endif
528 for (auto &&segpi : partial_range(Segments.dxx_segment_iteration_factory, Num_segments))
529 #undef dxx_segment_iteration_factory
530 {
531 const msmusegment segp = segpi;
532
533 #if DXX_USE_EDITOR
534 segp.s.segnum = segpi;
535 segp.s.group = 0;
536 #endif
537
538 const sidemask_t children_mask = New_file_format_load
539 ? static_cast<sidemask_t>(PHYSFSX_readByte(LoadFile))
540 : sidemask_t{0x7f}; // read all six children and special stuff...
541
542 if (Gamesave_current_version == 5) { // d2 SHAREWARE level
543 read_special(segp, children_mask, LoadFile);
544 read_verts(segp,LoadFile);
545 read_children(segp, children_mask, LoadFile);
546 } else {
547 read_children(segp, children_mask, LoadFile);
548 read_verts(segp,LoadFile);
549 if (Gamesave_current_version <= 1) { // descent 1 level
550 read_special(segp, children_mask, LoadFile);
551 }
552 }
553
554 segp.u.objects = object_none;
555
556 if (Gamesave_current_version <= 5) { // descent 1 thru d2 SHAREWARE level
557 // Read fix Segments[segnum].static_light (shift down 5 bits, write as short)
558 const uint16_t temp_static_light = PHYSFSX_readSLE16(LoadFile);
559 segp.u.static_light = static_cast<fix>(temp_static_light) << 4;
560 }
561
562 // Read the walls as a 6 byte array
563 const sidemask_t wall_mask = New_file_format_load
564 ? static_cast<sidemask_t>(PHYSFSX_readByte(LoadFile))
565 : sidemask_t{0x3f}; // read all six sides
566 for (const auto sidenum : MAX_SIDES_PER_SEGMENT)
567 {
568 auto &sside = segp.s.sides[sidenum];
569 if (wall_mask & build_sidemask(sidenum))
570 {
571 const uint8_t byte_wallnum = PHYSFSX_readByte(LoadFile);
572 if ( byte_wallnum == 255 )
573 sside.wall_num = wall_none;
574 else
575 sside.wall_num = wallnum_t{byte_wallnum};
576 } else
577 sside.wall_num = wall_none;
578 }
579
580 for (const auto sidenum : MAX_SIDES_PER_SEGMENT)
581 {
582 auto &uside = segp.u.sides[sidenum];
583 if (segp.s.children[sidenum] == segment_none || segp.s.sides[sidenum].wall_num != wall_none) {
584 // Read short Segments[segnum].sides[sidenum].tmap_num;
585 const uint16_t temp_tmap1_num = PHYSFSX_readSLE16(LoadFile);
586 #if DXX_BUILD_DESCENT == 1
587 uside.tmap_num = build_texture1_value(convert_tmap(temp_tmap1_num & 0x7fff));
588
589 if (New_file_format_load && !(temp_tmap1_num & 0x8000))
590 uside.tmap_num2 = texture2_value::None;
591 else {
592 // Read short Segments[segnum].sides[sidenum].tmap_num2;
593 const auto tmap_num2 = texture2_value{static_cast<uint16_t>(PHYSFSX_readSLE16(LoadFile))};
594 uside.tmap_num2 = build_texture2_value(convert_tmap(get_texture_index(tmap_num2)), get_texture_rotation_high(tmap_num2));
595 }
596 #elif DXX_BUILD_DESCENT == 2
597 const uint16_t masked_temp_tmap1_num = New_file_format_load ? (temp_tmap1_num & 0x7fff) : temp_tmap1_num;
598 uside.tmap_num = build_texture1_value(masked_temp_tmap1_num);
599
600 if (Gamesave_current_version <= 1)
601 uside.tmap_num = build_texture1_value(convert_d1_tmap_num(get_texture_index(uside.tmap_num)));
602
603 if (New_file_format_load && !(temp_tmap1_num & 0x8000))
604 uside.tmap_num2 = texture2_value::None;
605 else {
606 // Read short Segments[segnum].sides[sidenum].tmap_num2;
607 const auto tmap_num2 = static_cast<texture2_value>(PHYSFSX_readSLE16(LoadFile));
608 uside.tmap_num2 = (Gamesave_current_version <= 1 && tmap_num2 != texture2_value::None)
609 ? build_texture2_value(convert_d1_tmap_num(get_texture_index(tmap_num2)), get_texture_rotation_high(tmap_num2))
610 : tmap_num2;
611 }
612 #endif
613
614 // Read uvl Segments[segnum].sides[sidenum].uvls[4] (u,v>>5, write as short, l>>1 write as short)
615 range_for (auto &i, uside.uvls) {
616 temp_short = PHYSFSX_readSLE16(LoadFile);
617 i.u = static_cast<fix>(temp_short) << 5;
618 temp_short = PHYSFSX_readSLE16(LoadFile);
619 i.v = static_cast<fix>(temp_short) << 5;
620 const uint16_t temp_light = PHYSFSX_readSLE16(LoadFile);
621 i.l = static_cast<fix>(temp_light) << 1;
622 }
623 } else {
624 uside.tmap_num = texture1_value::None;
625 uside.tmap_num2 = texture2_value::None;
626 uside.uvls = {};
627 }
628 }
629 }
630
631 Vertices.set_count(Num_vertices);
632 Segments.set_count(Num_segments);
633
634 validate_segment_all(LevelSharedSegmentState); // Fill in side type and normals.
635
636 range_for (const auto &&pi, vmsegptridx)
637 {
638 if (Gamesave_current_version > 5)
639 segment2_read(pi, LoadFile);
640 fuelcen_activate(pi);
641 }
642
643 reset_objects(LevelUniqueObjectState, 1); //one object, the player
644
645 return 0;
646 }
647 }