Debian Codegraph @

similar/editor/mine.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-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Mine specific editing functions, such as load_mine, save_mine
23 *
24 */
25
26 #include <cinttypes>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <string.h>
31 #include "gr.h"
32 #include "bm.h" // for MAX_TEXTURES
33 #include "inferno.h"
34 #include "segment.h"
35 #include "editor.h"
36 #include "editor/esegment.h"
37 #include "wall.h"
38 #include "dxxerror.h"
39 #include "textures.h"
40 #include "physfsx.h"
41 #include "gamemine.h"
42 #include "gamesave.h"
43 #include "switch.h"
44 #include "fuelcen.h"
45
46 #include "compiler-range_for.h"
47 #include "d_enumerate.h"
48 #include "d_range.h"
49 #include "d_underlying_value.h"
50 #include "partial_range.h"
51
52 int New_file_format_save{1};
53
54 #if DXX_BUILD_DESCENT == 2
55 namespace dsx {
56
57 namespace {
58
59 // Converts descent 2 texture numbers back to descent 1 texture numbers.
60 // Only works properly when the full Descent 1 texture set (descent.pig) is available.
61 static texture_index convert_to_d1_tmap_num(const texture_index tmap_num)
62 {
63 switch (tmap_num)
64 {
65 case 137: return 0; // grey rock001
66 case 0: return 1;
67 case 1: return 3; // rock021
68 case 270: return 6; // blue rock002
69 case 271: return 7; // yellow rock265
70 case 2: return 8; // rock004
71 case 138: return 9; // purple (devil:179)
72 case 272: return 10; // red rock006
73 case 139: return 11;
74 case 140: return 12; //devil:43
75 case 3: return 13; // rock014
76 case 4: return 14; // rock019
77 case 5: return 15; // rock020
78 case 6: return 16;
79 case 141: return 17;
80 case 129: return 18;
81 case 7: return 19;
82 case 142: return 20;
83 case 143: return 21;
84 case 8: return 22;
85 case 9: return 23;
86 case 10: return 24;
87 case 144: return 25; //devil:35
88 case 11: return 26;
89 case 12: return 27;
90 case 145: return 28; //devil:43
91 //range handled by default case, returns 13..21 (- 16)
92 case 163: return 38; //devil:27
93 case 147: return 39; //31
94 case 22: return 40;
95 case 266: return 41;
96 case 23: return 42;
97 case 24: return 43;
98 case 136: return 44; //devil:135
99 case 25: return 45;
100 case 26: return 46;
101 case 27: return 47;
102 case 28: return 48;
103 case 146: return 49; //devil:60
104 case 131: return 50; //devil:138
105 case 29: return 51;
106 case 30: return 52;
107 case 31: return 53;
108 case 32: return 54;
109 case 165: return 55; //devil:193
110 case 33: return 56;
111 case 132: return 57; //devil:119
112 // range handled by default case, returns 58..88 (+ 24)
113 case 197: return 88; //devil:15
114 // range handled by default case, returns 89..106 (- 25)
115 case 167: return 132;
116 // range handled by default case, returns 107..114 (- 26)
117 case 148: return 141; //devil:106
118 case 115: return 142;
119 case 116: return 143;
120 case 117: return 144;
121 case 118: return 145;
122 case 119: return 146;
123 case 149: return 147;
124 case 120: return 148;
125 case 121: return 149;
126 case 122: return 150;
127 case 123: return 151;
128 case 124: return 152;
129 case 125: return 153; // rock263
130 case 150: return 154;
131 case 126: return 155; // rock269
132 case 200: return 156; // metl002
133 case 201: return 157; // metl003
134 case 186: return 158; //devil:227
135 case 190: return 159; //devil:246
136 case 151: return 160;
137 case 152: return 161; //devil:206
138 case 202: return 162;
139 case 203: return 163;
140 case 204: return 164;
141 case 205: return 165;
142 case 206: return 166;
143 case 153: return 167;
144 case 154: return 168;
145 case 155: return 169;
146 case 156: return 170;//206;
147 case 157: return 171;//227;
148 case 207: return 172;
149 case 208: return 173;
150 case 158: return 174;
151 case 159: return 175;
152 // range handled by default case, returns 209..217 (+ 33)
153 case 160: return 185;
154 // range handled by default case, returns 218..224 (+ 32)
155 case 161: return 193;
156 case 162: return 194;//206;
157 case 166: return 195;
158 case 225: return 196;
159 case 226: return 197;
160 case 193: return 198;
161 case 168: return 199; //devil:204
162 case 169: return 200; //devil:204
163 case 227: return 201;
164 case 170: return 202; //devil:227
165 // range handled by default case, returns 228..234 (+ 25)
166 case 171: return 210; //devil:242
167 case 172: return 211; //devil:240
168 // range handled by default case, returns 235..242 (+ 23)
169 case 173: return 220; //devil:240
170 case 243: return 221;
171 case 244: return 222;
172 case 174: return 223;
173 case 245: return 224;
174 case 246: return 225;
175 case 164: return 226;//247; matching names but not matching textures
176 case 179: return 227; //devil:181
177 case 196: return 228;//248; matching names but not matching textures
178 case 175: return 229; //devil:66
179 case 176: return 230; //devil:66
180 // range handled by default case, returns 249..257 (+ 18)
181 case 177: return 240; //devil:132
182 case 130: return 241; //devil:131
183 case 178: return 242; //devil:15
184 case 180: return 243; //devil:38
185 case 258: return 244;
186 case 259: return 245;
187 case 181: return 246; // grate metl127
188 case 260: return 247;
189 case 261: return 248;
190 case 262: return 249;
191 case 340: return 250; // white doorframe metl126
192 case 412: return 251; // red doorframe metl133
193 case 410: return 252; // blue doorframe metl134
194 case 411: return 253; // yellow doorframe metl135
195 case 263: return 254; // metl136
196 case 264: return 255; // metl139
197 case 265: return 256; // metl140
198 case 182: return 257;//246; brig001
199 case 183: return 258;//246; brig002
200 case 184: return 259;//246; brig003
201 case 185: return 260;//246; brig004
202 case 273: return 261; // exit01
203 case 274: return 262; // exit02
204 case 187: return 263; // ceil001
205 case 275: return 264; // ceil002
206 case 276: return 265; // ceil003
207 case 188: return 266; //devil:291
208 // range handled by default case, returns 277..291 (+ 10)
209 case 293: return 282;
210 case 189: return 283;
211 case 295: return 284;
212 case 296: return 285;
213 case 298: return 286;
214 // range handled by default case, returns 300..310 (+ 13)
215 case 191: return 298; // devil:374 misc010
216 // range handled by default case, returns 311..326 (+ 12)
217 case 192: return 315; // bad producer misc044
218 // range handled by default case, returns 327..337 (+ 11)
219 case 352: return 327; // arw01
220 case 353: return 328; // misc17
221 case 354: return 329; // fan01
222 case 380: return 330; // mntr04
223 case 379: return 331;//373; matching names but not matching textures
224 case 355: return 332;//344; matching names but not matching textures
225 case 409: return 333; // lava misc11 //devil:404
226 case 356: return 334; // ctrl04
227 case 357: return 335; // ctrl01
228 case 358: return 336; // ctrl02
229 case 359: return 337; // ctrl03
230 case 360: return 338; // misc14
231 case 361: return 339; // producer misc16
232 case 362: return 340; // misc049
233 case 364: return 341; // misc060
234 case 363: return 342; // blown01
235 case 366: return 343; // misc061
236 case 365: return 344;
237 case 368: return 345;
238 case 376: return 346;
239 case 370: return 347;
240 case 367: return 348;
241 case 372: return 349;
242 case 369: return 350;
243 case 374: return 351;//429; matching names but not matching textures
244 case 375: return 352;//387; matching names but not matching textures
245 case 371: return 353;
246 case 377: return 354;//425; matching names but not matching textures
247 case 408: return 355;
248 case 378: return 356; // lava02
249 case 383: return 357;//384; matching names but not matching textures
250 case 384: return 358;//385; matching names but not matching textures
251 case 385: return 359;//386; matching names but not matching textures
252 case 386: return 360;
253 case 387: return 361;
254 case 194: return 362; // mntr04b (devil: -1)
255 case 388: return 363;
256 case 391: return 364;
257 case 392: return 365;
258 case 393: return 366;
259 case 394: return 367;
260 case 395: return 368;
261 case 396: return 369;
262 case 195: return 370; // mntr04d (devil: -1)
263 // range 371..584 handled by default case (wall01 and door frames)
264 default:
265 // ranges:
266 if (tmap_num >= 13 && tmap_num <= 21)
267 return tmap_num + 16;
268 if (tmap_num >= 34 && tmap_num <= 63)
269 return tmap_num + 24;
270 if (tmap_num >= 64 && tmap_num <= 106)
271 return tmap_num + 25;
272 if (tmap_num >= 107 && tmap_num <= 114)
273 return tmap_num + 26;
274 if (tmap_num >= 209 && tmap_num <= 217)
275 return tmap_num - 33;
276 if (tmap_num >= 218 && tmap_num <= 224)
277 return tmap_num - 32;
278 if (tmap_num >= 228 && tmap_num <= 234)
279 return tmap_num - 25;
280 if (tmap_num >= 235 && tmap_num <= 242)
281 return tmap_num - 23;
282 if (tmap_num >= 249 && tmap_num <= 257)
283 return tmap_num - 18;
284 if (tmap_num >= 277 && tmap_num <= 291)
285 return tmap_num - 10;
286 if (tmap_num >= 300 && tmap_num <= 310)
287 return tmap_num - 13;
288 if (tmap_num >= 311 && tmap_num <= 326)
289 return tmap_num - 12;
290 if (tmap_num >= 327 && tmap_num <= 337)
291 return tmap_num - 11; // matching names but not matching textures
292 // wall01 and door frames:
293 if (tmap_num > 434 && tmap_num < 731)
294 {
295 if (New_file_format_save) return tmap_num - 64;
296 // d1 shareware needs special treatment:
297 if (tmap_num < 478) return tmap_num - 68;
298 if (tmap_num < 490) return tmap_num - 73;
299 if (tmap_num < 537) return tmap_num - 91;
300 if (tmap_num < 557) return tmap_num - 104;
301 if (tmap_num < 573) return tmap_num - 111;
302 if (tmap_num < 603) return tmap_num - 117;
303 if (tmap_num < 635) return tmap_num - 141;
304 if (tmap_num < 731) return tmap_num - 147;
305 }
306 Warning("Failed to convert unknown texture #%hu to Descent 1 texture.", tmap_num);
307 return tmap_num;
308 }
309 }
310
311 }
312
313 }
314 #endif
315
316 namespace {
317
318 static int save_mine_data(PHYSFS_File * SaveFile);
319
320 // -----------------------------------------------------------------------------
321 // Save mine will:
322 // 1. Write file info, header info, editor info, vertex data, segment data,
323 // and new_segment in that order, marking their file offset.
324 // 2. Go through all the fields and fill in the offset, size, and sizeof
325 // values in the headers.
326
327 static int med_save_mine(const char * filename)
328 {
329 char ErrorMessage[256];
330
331 auto &&[SaveFile, physfserr] = PHYSFSX_openWriteBuffered(filename);
332 if (!SaveFile)
333 {
334 snprintf(ErrorMessage, sizeof(ErrorMessage), "ERROR: Unable to open %.200s: %s\n", filename, PHYSFS_getErrorByCode(physfserr));
335 ui_messagebox( -2, -2, 1, ErrorMessage, "Ok" );
336 return 1;
337 }
338
339 save_mine_data(SaveFile);
340
341 //==================== CLOSE THE FILE =============================
342 return 0;
343 }
344
345 // -----------------------------------------------------------------------------
346 // saves to an already-open file
347 static int save_mine_data(PHYSFS_File * SaveFile)
348 {
349 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
350 int header_offset, editor_offset, vertex_offset, segment_offset, texture_offset, walls_offset, triggers_offset; //, links_offset;
351 int newseg_verts_offset;
352 int newsegment_offset;
353 med_compress_mine();
354 warn_if_concave_segments();
355
356 std::array<d_fname, MAX_TEXTURES> current_tmap_list;
357 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
358 for (int i=0;i<NumTextures;i++)
359 current_tmap_list[i] = TmapInfo[i].filename;
360
361 //=================== Calculate offsets into file ==================
362
363 header_offset = PHYSFS_tell(SaveFile) + sizeof(mine_fileinfo);
364 editor_offset = header_offset + sizeof(mine_header);
365 texture_offset = editor_offset + sizeof(mine_editor);
366 vertex_offset = texture_offset + (13*NumTextures);
367 segment_offset = vertex_offset + (sizeof(vms_vector) * LevelSharedVertexState.Num_vertices);
368 newsegment_offset = segment_offset + (sizeof(segment) * LevelSharedSegmentState.Num_segments);
369 newseg_verts_offset = newsegment_offset + sizeof(segment);
370 walls_offset = newseg_verts_offset + (sizeof(vms_vector)*8);
371 auto &Triggers = LevelUniqueWallSubsystemState.Triggers;
372 auto &Walls = LevelUniqueWallSubsystemState.Walls;
373 triggers_offset = walls_offset + (sizeof(wall)*Walls.get_count());
374
375 //===================== SAVE FILE INFO ========================
376
377 mine_fileinfo.fileinfo_signature= 0x2884;
378 mine_fileinfo.fileinfo_version = MINE_VERSION;
379 mine_fileinfo.fileinfo_sizeof = sizeof(mine_fileinfo);
380 mine_fileinfo.header_offset = header_offset;
381 mine_fileinfo.header_size = sizeof(mine_header);
382 mine_fileinfo.editor_offset = editor_offset;
383 mine_fileinfo.editor_size = sizeof(mine_editor);
384 mine_fileinfo.vertex_offset = vertex_offset;
385 mine_fileinfo.vertex_howmany = LevelSharedVertexState.Num_vertices;
386 mine_fileinfo.vertex_sizeof = sizeof(vms_vector);
387 mine_fileinfo.segment_offset = segment_offset;
388 mine_fileinfo.segment_howmany = LevelSharedSegmentState.Num_segments;
389 mine_fileinfo.segment_sizeof = sizeof(segment);
390 mine_fileinfo.newseg_verts_offset = newseg_verts_offset;
391 mine_fileinfo.newseg_verts_howmany = 8;
392 mine_fileinfo.newseg_verts_sizeof = sizeof(vms_vector);
393 mine_fileinfo.texture_offset = texture_offset;
394 mine_fileinfo.texture_howmany = NumTextures;
395 mine_fileinfo.texture_sizeof = 13; // num characters in a name
396 mine_fileinfo.walls_offset = walls_offset;
397 mine_fileinfo.walls_howmany = Walls.get_count();
398 mine_fileinfo.walls_sizeof = sizeof(wall);
399 mine_fileinfo.triggers_offset = triggers_offset;
400 mine_fileinfo.triggers_howmany = Triggers.get_count();
401 mine_fileinfo.triggers_sizeof = sizeof(trigger);
402
403 // Write the fileinfo
404 PHYSFSX_writeBytes(SaveFile, &mine_fileinfo, sizeof(mine_fileinfo));
405
406 //===================== SAVE HEADER INFO ========================
407
408 mine_header.num_vertices = LevelSharedVertexState.Num_vertices;
409 mine_header.num_segments = LevelSharedSegmentState.Num_segments;
410
411 // Write the editor info
412 if (header_offset != PHYSFS_tell(SaveFile))
413 Error( "OFFSETS WRONG IN MINE.C!" );
414
415 PHYSFSX_writeBytes(SaveFile, &mine_header, sizeof(mine_header));
416
417 //===================== SAVE EDITOR INFO ==========================
418 mine_editor.current_seg = Cursegp;
419 mine_editor.newsegment_offset = newsegment_offset;
420 mine_editor.newsegment_size = sizeof(segment);
421
422 // Next 3 vars added 10/07 by JAS
423 mine_editor.Curside = underlying_value(Curside);
424 if (Markedsegp)
425 mine_editor.Markedsegp = Markedsegp;
426 else
427 mine_editor.Markedsegp = -1;
428 mine_editor.Markedside = underlying_value(Markedside);
429 range_for (const int i, xrange(10u))
430 mine_editor.Groupsegp[i] = vmsegptridx(Groupsegp[i]);
431 range_for (const int i, xrange(10u))
432 mine_editor.Groupside[i] = underlying_value(Groupside[i]);
433
434 if (editor_offset != PHYSFS_tell(SaveFile))
435 Error( "OFFSETS WRONG IN MINE.C!" );
436 PHYSFSX_writeBytes(SaveFile, &mine_editor, sizeof(mine_editor));
437
438 //===================== SAVE TEXTURE INFO ==========================
439
440 if (texture_offset != PHYSFS_tell(SaveFile))
441 Error( "OFFSETS WRONG IN MINE.C!" );
442 range_for (auto &i, partial_const_range(current_tmap_list, NumTextures))
443 PHYSFSX_writeBytes(SaveFile, i.data(), i.size());
444
445 //===================== SAVE VERTEX INFO ==========================
446
447 if (vertex_offset != PHYSFS_tell(SaveFile))
448 Error( "OFFSETS WRONG IN MINE.C!" );
449 auto &Vertices = LevelSharedVertexState.get_vertices();
450 PHYSFSX_writeBytes(SaveFile, Vertices, sizeof(vms_vector) * LevelSharedVertexState.Num_vertices);
451
452 //===================== SAVE SEGMENT INFO =========================
453
454 if (segment_offset != PHYSFS_tell(SaveFile))
455 Error( "OFFSETS WRONG IN MINE.C!" );
456 Error("Sorry, v20 segment support is broken.");
457 #if 0
458 PHYSFS_write(SaveFile, &Segments.front(), sizeof(segment), LevelSharedSegmentState.Num_segments);
459
460 //===================== SAVE NEWSEGMENT INFO ======================
461
462 if (newsegment_offset != PHYSFS_tell(SaveFile))
463 Error( "OFFSETS WRONG IN MINE.C!" );
464 PHYSFSX_writeBytes(SaveFile, &New_segment, sizeof(segment));
465 #endif
466
467 if (newseg_verts_offset != PHYSFS_tell(SaveFile))
468 Error( "OFFSETS WRONG IN MINE.C!" );
469 PHYSFSX_writeBytes(SaveFile, &Vertices[New_segment.verts.front()], sizeof(vms_vector) * 8);
470
471 //==================== CLOSE THE FILE =============================
472
473 return 0;
474
475 }
476
477
478
479 #define COMPILED_MINE_VERSION 0
480
481 static void dump_fix_as_short( fix value, int nbits, PHYSFS_File *SaveFile )
482 {
483 short short_value;
484
485 auto int_value = value >> nbits;
486 if (int_value > INT16_MAX)
487 {
488 short_value = INT16_MAX;
489 }
490 else if( int_value < -0x7fff ) {
491 short_value = -0x7fff;
492 }
493 else
494 short_value = static_cast<short>(int_value);
495
496 PHYSFS_writeSLE16(SaveFile, short_value);
497 }
498
499 //version of dump for unsigned values
500 static void dump_fix_as_ushort( fix value, int nbits, PHYSFS_File *SaveFile )
501 {
502 uint int_value{0};
503 ushort short_value;
504
505 if (value < 0) {
506 Int3(); //hey---show this to Matt
507 value = 0;
508 }
509 else
510 int_value = value >> nbits;
511
512 if( int_value > 0xffff ) {
513 short_value = 0xffff;
514 }
515 else
516 short_value = int_value;
517
518 PHYSFS_writeULE16(SaveFile, short_value);
519 }
520
521 static void write_children(const shared_segment &seg, const sidemask_t bit_mask, PHYSFS_File *const SaveFile)
522 {
523 for (const auto &&[bit, child] : enumerate(seg.children))
524 {
525 if (bit_mask & build_sidemask(bit))
526 PHYSFS_writeSLE16(SaveFile, child);
527 }
528 }
529
530 static void write_verts(const shared_segment &seg, PHYSFS_File *const SaveFile)
531 {
532 range_for (auto &i, seg.verts)
533 PHYSFS_writeSLE16(SaveFile, static_cast<uint16_t>(i));
534 }
535
536 static void write_special(const shared_segment &seg, const sidemask_t bit_mask, PHYSFS_File *const SaveFile)
537 {
538 if (bit_mask & build_sidemask(MAX_SIDES_PER_SEGMENT))
539 {
540 PHYSFSX_writeU8(SaveFile, underlying_value(seg.special));
541 PHYSFSX_writeU8(SaveFile, underlying_value(seg.matcen_num));
542 PHYSFS_writeULE16(SaveFile, underlying_value(seg.station_idx));
543 }
544 }
545
546 }
547
548 int med_save_mine(const mine_filename_type &filename)
549 {
550 return med_save_mine(filename.data());
551 }
552
553 // -----------------------------------------------------------------------------
554 // saves compiled mine data to an already-open file...
555 namespace dsx {
556 int save_mine_data_compiled(PHYSFS_File *SaveFile)
557 {
558 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
559 ubyte version = COMPILED_MINE_VERSION;
560
561 med_compress_mine();
562 warn_if_concave_segments();
563
564 if (Highest_segment_index >= MAX_SEGMENTS) {
565 char message[128];
566 snprintf(message, sizeof(message), "Error: Too many segments (%i > %" PRIuFAST32 ") for game (not editor)", Highest_segment_index+1, static_cast<uint_fast32_t>(MAX_SEGMENTS));
567 ui_messagebox( -2, -2, 1, message, "Ok" );
568 }
569
570 auto &Vertices = LevelSharedVertexState.get_vertices();
571 if (Vertices.get_count() > MAX_VERTICES)
572 {
573 char message[128];
574 snprintf(message, sizeof(message), "Error: Too many vertices (%i > %" PRIuFAST32 ") for game (not editor)", Vertices.get_count(), static_cast<uint_fast32_t>(MAX_VERTICES));
575 ui_messagebox( -2, -2, 1, message, "Ok" );
576 }
577
578 //=============================== Writing part ==============================
579 PHYSFSX_writeU8(SaveFile, version); // 1 byte = compiled version
580 if (New_file_format_save)
581 {
582 PHYSFS_writeSLE16(SaveFile, LevelSharedVertexState.Num_vertices); // 2 bytes = Num_vertices
583 PHYSFS_writeSLE16(SaveFile, LevelSharedSegmentState.Num_segments); // 2 bytes = Num_segments
584 }
585 else
586 {
587 PHYSFS_writeSLE32(SaveFile, LevelSharedVertexState.Num_vertices); // 4 bytes = Num_vertices
588 PHYSFS_writeSLE32(SaveFile, LevelSharedSegmentState.Num_segments); // 4 bytes = Num_segments
589 }
590
591 range_for (auto &i, partial_const_range(Vertices, LevelSharedVertexState.Num_vertices))
592 PHYSFSX_writeVector(SaveFile, i);
593
594 const auto Num_segments = LevelSharedSegmentState.Num_segments;
595 auto &&segment_range = partial_const_range(Segments, Num_segments);
596 for (const cscusegment seg : segment_range)
597 {
598 {
599 sidemask_t bit_mask{};
600 for (const auto &&[sidenum, child] : enumerate(seg.s.children))
601 {
602 if (child != segment_none)
603 bit_mask |= build_sidemask(sidenum);
604 }
605
606 if (seg.s.special != segment_special::nothing || seg.s.matcen_num != materialization_center_number::None || seg.s.station_idx != station_number::None)
607 bit_mask |= build_sidemask(MAX_SIDES_PER_SEGMENT);
608
609 if (New_file_format_save)
610 PHYSFSX_writeU8(SaveFile, underlying_value(bit_mask));
611 else
612 bit_mask = sidemask_t{0x7f};
613
614 if (Gamesave_current_version == 5) // d2 SHAREWARE level
615 {
616 write_special(seg, bit_mask, SaveFile);
617 write_verts(seg, SaveFile);
618 write_children(seg, bit_mask, SaveFile);
619 }
620 else
621 {
622 write_children(seg, bit_mask, SaveFile);
623 write_verts(seg, SaveFile);
624 if (Gamesave_current_version <= 1) // descent 1 level
625 write_special(seg, bit_mask, SaveFile);
626 }
627
628 if (Gamesave_current_version <= 5) // descent 1 thru d2 SHAREWARE level
629 dump_fix_as_ushort(seg.u.static_light, 4, SaveFile);
630 }
631
632 // Write the walls as a 6 byte array
633 {
634 sidemask_t bit_mask{};
635 for (const auto &&[sidenum, side] : enumerate(seg.s.sides))
636 {
637 if (side.wall_num != wall_none)
638 bit_mask |= build_sidemask(sidenum);
639 }
640 if (New_file_format_save)
641 PHYSFSX_writeU8(SaveFile, underlying_value(bit_mask));
642 else
643 bit_mask = sidemask_t{0x3f};
644
645 for (const auto &&[sidenum, side] : enumerate(seg.s.sides))
646 {
647 if (bit_mask & build_sidemask(sidenum))
648 PHYSFSX_writeU8(SaveFile, underlying_value(side.wall_num));
649 }
650 }
651
652 for (const auto sidenum : MAX_SIDES_PER_SEGMENT)
653 {
654 if (seg.s.children[sidenum] == segment_none || seg.s.sides[sidenum].wall_num != wall_none)
655 {
656 auto tmap_num = seg.u.sides[sidenum].tmap_num;
657 auto tmap_num2 = seg.u.sides[sidenum].tmap_num2;
658
659 #if DXX_BUILD_DESCENT == 2
660 if (Gamesave_current_version <= 3) // convert texture numbers back to d1
661 {
662 tmap_num = build_texture1_value(convert_to_d1_tmap_num(get_texture_index(tmap_num)));
663 if (tmap_num2 != texture2_value::None)
664 {
665 tmap_num2 = build_texture2_value(convert_to_d1_tmap_num(get_texture_index(tmap_num2)), get_texture_rotation_high(tmap_num2));
666 }
667 }
668 #endif
669
670 uint16_t write_tmap_num = static_cast<uint16_t>(tmap_num);
671 if (tmap_num2 != texture2_value::None && New_file_format_save)
672 write_tmap_num |= 0x8000;
673
674 PHYSFS_writeSLE16(SaveFile, write_tmap_num);
675 if (tmap_num2 != texture2_value::None || !New_file_format_save)
676 PHYSFS_writeSLE16(SaveFile, static_cast<uint16_t>(tmap_num2));
677
678 range_for (auto &i, seg.u.sides[sidenum].uvls)
679 {
680 dump_fix_as_short(i.u, 5, SaveFile);
681 dump_fix_as_short(i.v, 5, SaveFile);
682 dump_fix_as_ushort(i.l, 1, SaveFile);
683 }
684 }
685 }
686
687 }
688
689 #if DXX_BUILD_DESCENT == 2
690 if (Gamesave_current_version > 5)
691 for (auto &s : segment_range)
692 segment2_write(s, SaveFile);
693 #endif
694
695 return 0;
696 }
697 }
698
699