3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
20 static char rcsid[] = "$Id: gamemine.c,v 1.21 2003-03-19 22:44:15 btb Exp $";
45 #include "editor/editor.h"
58 #define REMOVE_EXT(s) (*(strchr( (s), '.' ))='\0')
60 fix Level_shake_frequency = 0, Level_shake_duration = 0;
61 int Secret_return_segment = 0;
62 vms_matrix Secret_return_orient;
64 struct mtfi mine_top_fileinfo; // Should be same as first two fields below...
65 struct mfi mine_fileinfo;
66 struct mh mine_header;
67 struct me mine_editor;
69 typedef struct v16_segment {
71 short segnum; // segment number, not sure what it means
73 side sides[MAX_SIDES_PER_SEGMENT]; // 6 sides
74 short children[MAX_SIDES_PER_SEGMENT]; // indices of 6 children segments, front, left, top, right, bottom, back
75 short verts[MAX_VERTICES_PER_SEGMENT]; // vertex ids of 4 front and 4 back vertices
77 short group; // group number to which the segment belongs.
79 short objects; // pointer to objects in this segment
80 ubyte special; // what type of center this is
81 byte matcen_num; // which center segment is associated with.
83 fix static_light; // average static light in segment
85 short pad; // make structure longword aligned
90 ushort fileinfo_signature;
91 ushort fileinfo_version;
93 int header_offset; // Stuff common to game & editor
95 int editor_offset; // Editor specific stuff
100 int newseg_verts_offset;
101 int newseg_verts_howmany;
102 int newseg_verts_sizeof;
116 int triggers_howmany;
121 int object_offset; // Object info
124 int unused_offset; // was: doors_offset
125 int unused_howmamy; // was: doors_howmany
126 int unused_sizeof; // was: doors_sizeof
127 short level_shake_frequency; // Shakes every level_shake_frequency seconds
128 short level_shake_duration; // for level_shake_duration seconds (on average, random). In 16ths second.
129 int secret_return_segment;
130 vms_matrix secret_return_orient;
132 int dl_indices_offset;
133 int dl_indices_howmany;
134 int dl_indices_sizeof;
136 int delta_light_offset;
137 int delta_light_howmany;
138 int delta_light_sizeof;
142 int CreateDefaultNewSegment();
144 int New_file_format_load = 1; // "new file format" is everything newer than d1 shareware
146 int d1_pig_loaded = 0; // can descent.pig from descent 1 be loaded?
148 #define TMAP_NUM_MASK 0x3FFF
150 /* converts descent 1 texture numbers to descent 2 texture numbers
151 * textures whose names don't match between versions have extra spaces around "return"
152 * updated using the file config/convtabl.ini from the devil 2.2 level editor
154 short convert_d1_tmap_num(short d1_tmap_num) {
155 if (d1_pig_loaded && d1_tmap_num < 324) // we use original d1 textures for non-animated textures
157 switch (d1_tmap_num) {
158 case 0: return 43; // grey (devil:95)
160 case 2: return 43; // grey
162 case 4: return 43; // grey
163 case 5: return 43; // grey
164 case 6: return 270; // blue
165 case 7: return 271; // yellow
167 case 9: return 62; // purple (devil:179)
168 case 10: return 272; // red
170 case 12: return 12; //devil:43
183 case 25: return 12; //devil:35
186 case 28: return 11; //devil:43
196 case 38: return 163; //devil:27
197 case 39: return 31; //devil:147
202 case 44: return 136; //devil:135
207 case 49: return 43; //devil:60
208 case 50: return 131; //devil:138
213 case 55: return 165; //devil:193
215 case 57: return 132; //devil:119
216 // range handled by default case
217 case 88: return 197; //devil:15
218 // range handled by default case
219 case 132: return 167;
220 case 133: return 107;
221 case 134: return 108;
222 case 135: return 109;
223 case 136: return 110;
224 case 137: return 111;
225 case 138: return 112;
226 case 139: return 113;
227 case 140: return 114;
228 case 141: return 110; //devil:106
229 case 142: return 115;
230 case 143: return 116;
231 case 144: return 117;
232 case 145: return 118;
233 case 146: return 119;
235 case 148: return 120;
236 case 149: return 121;
237 case 150: return 122;
238 case 151: return 123;
239 case 152: return 124;
240 case 153: return 125;
242 case 155: return 126; //was: 66
243 case 156: return 200;
244 case 157: return 201;
245 case 158: return 186; //devil:227
246 case 159: return 190; //devil:246
247 case 160: return 206;
248 case 161: return 114; //devil:206
249 case 162: return 202;
250 case 163: return 203;
251 case 164: return 204;
252 case 165: return 205;
253 case 166: return 206;
254 case 167: return 206;
255 case 168: return 206;
256 case 169: return 206;
257 case 170: return 227;//206;
258 case 171: return 206;//227;
259 case 172: return 207;
260 case 173: return 208;
261 case 174: return 202;
262 case 175: return 206;
263 case 176: return 209;
264 case 177: return 210;
265 case 178: return 211;
266 case 179: return 212;
267 case 180: return 213;
268 case 181: return 214;
269 case 182: return 215;
270 case 183: return 216;
271 case 184: return 217;
272 case 185: return 217;
273 case 186: return 218;
274 case 187: return 219;
275 case 188: return 220;
276 case 189: return 221;
277 case 190: return 222;
278 case 191: return 223;
279 case 192: return 224;
280 case 193: return 206;
281 case 194: return 203;//206;
282 case 195: return 234;
283 case 196: return 225;
284 case 197: return 226;
285 case 198: return 225;
286 case 199: return 206; //devil:204
287 case 200: return 206; //devil:204
288 case 201: return 227;
289 case 202: return 206; //devil:227
290 case 203: return 228;
291 case 204: return 229;
292 case 205: return 230;
293 case 206: return 231;
294 case 207: return 232;
295 case 208: return 233;
296 case 209: return 234;
297 case 210: return 234; //devil:242
298 case 211: return 206; //devil:240
299 case 212: return 235;
300 case 213: return 236;
301 case 214: return 237;
302 case 215: return 238;
303 case 216: return 239;
304 case 217: return 240;
305 case 218: return 241;
306 case 219: return 242;
307 case 220: return 242; //devil:240
308 case 221: return 243;
309 case 222: return 244;
310 case 223: return 313;
311 case 224: return 245;
312 case 225: return 246;
313 case 226: return 164;//247; matching names but not matching textures
314 case 227: return 179; //devil:181
315 case 228: return 196;//248; matching names but not matching textures
316 case 229: return 15; //devil:66
317 case 230: return 15; //devil:66
318 case 231: return 249;
319 case 232: return 250;
320 case 233: return 251;
321 case 234: return 252;
322 case 235: return 253;
323 case 236: return 254;
324 case 237: return 255;
325 case 238: return 256;
326 case 239: return 257;
327 case 240: return 6; //devil:132
328 case 241: return 130; //devil:131
329 case 242: return 78; //devil:15
330 case 243: return 33; //devil:38
331 case 244: return 258;
332 case 245: return 259;
333 case 246: return 321;
334 case 247: return 260;
335 case 248: return 261;
336 case 249: return 262;
337 case 250: return 340; // white entrance
338 case 251: return 412; // red entrance
339 case 252: return 410; // blue entrance
340 case 253: return 411; // yellow entrance
341 case 254: return 263;
342 case 255: return 264;
343 case 256: return 265;
344 case 257: return 249;//246;
345 case 258: return 251;//246;
346 case 259: return 252;//246;
347 case 260: return 256;//246;
348 case 261: return 273;
349 case 262: return 274;
350 case 263: return 281;
351 case 264: return 275;
352 case 265: return 276;
353 case 266: return 279; //devil:291
354 // range handled by default case
355 case 282: return 293;
356 case 283: return 295;
357 case 284: return 295;
358 case 285: return 296;
359 case 286: return 298;
360 // range handled by default case
361 case 298: return 364; //devil:374
362 // range handled by default case
363 case 315: return 361; // broken producer
364 // range handled by default case
365 case 327: return 352;
366 case 328: return 353;
367 case 329: return 354;
368 case 330: return 380;
369 case 331: return 379;//373; matching names but not matching textures;
370 case 332: return 355;//344; matching names but not matching textures
371 case 333: return 409; // lava //devil:404
372 case 334: return 356;
373 case 335: return 357;
374 case 336: return 358;
375 case 337: return 359;
376 case 338: return 360;
377 case 339: return 361;
378 case 340: return 362;
379 case 341: return 364;
380 case 342: return 363;
381 case 343: return 366;
382 case 344: return 365;
383 case 345: return 368;
384 case 346: return 376;
385 case 347: return 370;
386 case 348: return 367;
387 case 349: return 372;
388 case 350: return 369;
389 case 351: return 374;//429; matching names but not matching textures
390 case 352: return 375;//387; matching names but not matching textures
391 case 353: return 371;
392 case 354: return 377;//425; matching names but not matching textures
393 case 355: return 408;
394 case 356: return 378; // lava02
395 case 357: return 383;//384; matching names but not matching textures
396 case 358: return 384;//385; matching names but not matching textures
397 case 359: return 385;//386; matching names but not matching textures
398 case 360: return 386;
399 case 361: return 387;
400 case 362: return 388; // mntr04a (devil: -1)
401 case 363: return 388;
402 case 364: return 391;
403 case 365: return 392;
404 case 366: return 393;
405 case 367: return 394;
406 case 368: return 395;
407 case 369: return 396;
408 case 370: return 392; // mntr04b (devil: -1)
409 // range 371 - 584 handled by default
412 if (d1_tmap_num >= 58 && d1_tmap_num <= 87)
413 return d1_tmap_num - 24;
414 if (d1_tmap_num >= 89 && d1_tmap_num <= 131)
415 return d1_tmap_num - 25;
416 if (d1_tmap_num >= 267 && d1_tmap_num <= 281)
417 return d1_tmap_num + 10;
418 if (d1_tmap_num >= 287 && d1_tmap_num <= 297)
419 return d1_tmap_num + 13;
420 if (d1_tmap_num >= 299 && d1_tmap_num <= 314)
421 return d1_tmap_num + 12;
422 if (d1_tmap_num >= 316 && d1_tmap_num <= 326)
423 return d1_tmap_num + 11;
424 // wall01 and door frames:
425 if (d1_tmap_num > 370 && d1_tmap_num < 584) {
426 if (New_file_format_load) return d1_tmap_num + 64;
427 // d1 shareware needs special treatment:
428 if (d1_tmap_num < 410) return d1_tmap_num + 68;
429 if (d1_tmap_num < 417) return d1_tmap_num + 73;
430 if (d1_tmap_num < 446) return d1_tmap_num + 91;
431 if (d1_tmap_num < 453) return d1_tmap_num + 104;
432 if (d1_tmap_num < 462) return d1_tmap_num + 111;
433 if (d1_tmap_num < 486) return d1_tmap_num + 117;
434 if (d1_tmap_num < 494) return d1_tmap_num + 141;
435 if (d1_tmap_num < 584) return d1_tmap_num + 147;
437 { // handle rare case where orientation != 0
438 short tmap_num = d1_tmap_num & TMAP_NUM_MASK;
439 short orient = d1_tmap_num & ~TMAP_NUM_MASK;
441 return orient | convert_d1_tmap_num(tmap_num);
443 Warning("can't convert unknown descent 1 texture #%d.\n", tmap_num);
452 static char old_tmap_list[MAX_TEXTURES][FILENAME_LEN];
453 short tmap_xlate_table[MAX_TEXTURES];
454 static short tmap_times_used[MAX_TEXTURES];
456 // -----------------------------------------------------------------------------
457 //loads from an already-open file
458 // returns 0=everything ok, 1=old version, -1=error
459 int load_mine_data(CFILE *LoadFile)
461 int i, j,oldsizeadjust;
465 int mine_start = cftell(LoadFile);
467 oldsizeadjust=(sizeof(int)*2)+sizeof (vms_matrix);
470 for (i=0; i<MAX_TEXTURES; i++ )
471 tmap_times_used[i] = 0;
474 // Create a new mine to initialize things.
475 //texpage_goto_first();
479 //===================== READ FILE INFO ========================
481 // These are the default values... version and fileinfo_sizeof
482 // don't have defaults.
483 mine_fileinfo.header_offset = -1;
484 mine_fileinfo.header_size = sizeof(mine_header);
485 mine_fileinfo.editor_offset = -1;
486 mine_fileinfo.editor_size = sizeof(mine_editor);
487 mine_fileinfo.vertex_offset = -1;
488 mine_fileinfo.vertex_howmany = 0;
489 mine_fileinfo.vertex_sizeof = sizeof(vms_vector);
490 mine_fileinfo.segment_offset = -1;
491 mine_fileinfo.segment_howmany = 0;
492 mine_fileinfo.segment_sizeof = sizeof(segment);
493 mine_fileinfo.newseg_verts_offset = -1;
494 mine_fileinfo.newseg_verts_howmany = 0;
495 mine_fileinfo.newseg_verts_sizeof = sizeof(vms_vector);
496 mine_fileinfo.group_offset = -1;
497 mine_fileinfo.group_howmany = 0;
498 mine_fileinfo.group_sizeof = sizeof(group);
499 mine_fileinfo.texture_offset = -1;
500 mine_fileinfo.texture_howmany = 0;
501 mine_fileinfo.texture_sizeof = FILENAME_LEN; // num characters in a name
502 mine_fileinfo.walls_offset = -1;
503 mine_fileinfo.walls_howmany = 0;
504 mine_fileinfo.walls_sizeof = sizeof(wall);
505 mine_fileinfo.triggers_offset = -1;
506 mine_fileinfo.triggers_howmany = 0;
507 mine_fileinfo.triggers_sizeof = sizeof(trigger);
508 mine_fileinfo.object_offset = -1;
509 mine_fileinfo.object_howmany = 1;
510 mine_fileinfo.object_sizeof = sizeof(object);
512 mine_fileinfo.level_shake_frequency = 0;
513 mine_fileinfo.level_shake_duration = 0;
515 // Delta light stuff for blowing out light sources.
516 // if (mine_top_fileinfo.fileinfo_version >= 19) {
517 mine_fileinfo.dl_indices_offset = -1;
518 mine_fileinfo.dl_indices_howmany = 0;
519 mine_fileinfo.dl_indices_sizeof = sizeof(dl_index);
521 mine_fileinfo.delta_light_offset = -1;
522 mine_fileinfo.delta_light_howmany = 0;
523 mine_fileinfo.delta_light_sizeof = sizeof(delta_light);
527 mine_fileinfo.segment2_offset = -1;
528 mine_fileinfo.segment2_howmany = 0;
529 mine_fileinfo.segment2_sizeof = sizeof(segment2);
531 // Read in mine_top_fileinfo to get size of saved fileinfo.
533 memset( &mine_top_fileinfo, 0, sizeof(mine_top_fileinfo) );
535 if (cfseek( LoadFile, mine_start, SEEK_SET ))
536 Error( "Error moving to top of file in gamemine.c" );
538 if (cfread( &mine_top_fileinfo, sizeof(mine_top_fileinfo), 1, LoadFile )!=1)
539 Error( "Error reading mine_top_fileinfo in gamemine.c" );
541 if (mine_top_fileinfo.fileinfo_signature != 0x2884)
544 // Check version number
545 if (mine_top_fileinfo.fileinfo_version < COMPATIBLE_VERSION )
548 // Now, Read in the fileinfo
549 if (cfseek( LoadFile, mine_start, SEEK_SET ))
550 Error( "Error seeking to top of file in gamemine.c" );
552 if (cfread( &mine_fileinfo, mine_top_fileinfo.fileinfo_sizeof, 1, LoadFile )!=1)
553 Error( "Error reading mine_fileinfo in gamemine.c" );
555 if (mine_top_fileinfo.fileinfo_version < 18) {
556 mprintf((1, "Old version, setting shake intensity to 0.\n"));
557 Level_shake_frequency = 0;
558 Level_shake_duration = 0;
559 Secret_return_segment = 0;
560 Secret_return_orient = vmd_identity_matrix;
562 Level_shake_frequency = mine_fileinfo.level_shake_frequency << 12;
563 Level_shake_duration = mine_fileinfo.level_shake_duration << 12;
564 Secret_return_segment = mine_fileinfo.secret_return_segment;
565 Secret_return_orient = mine_fileinfo.secret_return_orient;
568 //===================== READ HEADER INFO ========================
570 // Set default values.
571 mine_header.num_vertices = 0;
572 mine_header.num_segments = 0;
574 if (mine_fileinfo.header_offset > -1 )
576 if (cfseek( LoadFile, mine_fileinfo.header_offset, SEEK_SET ))
577 Error( "Error seeking to header_offset in gamemine.c" );
579 if (cfread( &mine_header, mine_fileinfo.header_size, 1, LoadFile )!=1)
580 Error( "Error reading mine_header in gamemine.c" );
583 //===================== READ EDITOR INFO ==========================
585 // Set default values
586 mine_editor.current_seg = 0;
587 mine_editor.newsegment_offset = -1; // To be written
588 mine_editor.newsegment_size = sizeof(segment);
589 mine_editor.Curside = 0;
590 mine_editor.Markedsegp = -1;
591 mine_editor.Markedside = 0;
593 if (mine_fileinfo.editor_offset > -1 )
595 if (cfseek( LoadFile, mine_fileinfo.editor_offset, SEEK_SET ))
596 Error( "Error seeking to editor_offset in gamemine.c" );
598 if (cfread( &mine_editor, mine_fileinfo.editor_size, 1, LoadFile )!=1)
599 Error( "Error reading mine_editor in gamemine.c" );
602 //===================== READ TEXTURE INFO ==========================
604 if ( (mine_fileinfo.texture_offset > -1) && (mine_fileinfo.texture_howmany > 0))
606 if (cfseek( LoadFile, mine_fileinfo.texture_offset, SEEK_SET ))
607 Error( "Error seeking to texture_offset in gamemine.c" );
609 for (i=0; i< mine_fileinfo.texture_howmany; i++ )
611 if (cfread( &old_tmap_list[i], mine_fileinfo.texture_sizeof, 1, LoadFile )!=1)
612 Error( "Error reading old_tmap_list[i] in gamemine.c" );
616 //=============== GENERATE TEXTURE TRANSLATION TABLE ===============
620 Assert (NumTextures < MAX_TEXTURES);
625 hashtable_init( &ht, NumTextures );
627 // Remove all the file extensions in the textures list
629 for (i=0;i<NumTextures;i++) {
630 temptr = strchr(TmapInfo[i].filename, '.');
631 if (temptr) *temptr = '\0';
632 hashtable_insert( &ht, TmapInfo[i].filename, i );
635 // For every texture, search through the texture list
636 // to find a matching name.
637 for (j=0;j<mine_fileinfo.texture_howmany;j++) {
638 // Remove this texture name's extension
639 temptr = strchr(old_tmap_list[j], '.');
640 if (temptr) *temptr = '\0';
642 tmap_xlate_table[j] = hashtable_search( &ht,old_tmap_list[j]);
643 if (tmap_xlate_table[j] < 0 ) {
644 //tmap_xlate_table[j] = 0;
645 // mprintf( (0, "Couldn't find texture '%s'\n", old_tmap_list[j] ));
648 if (tmap_xlate_table[j] != j ) translate = 1;
649 if (tmap_xlate_table[j] >= 0)
650 tmap_times_used[tmap_xlate_table[j]]++;
655 for (i=0; i<MAX_TEXTURES; i++ )
656 if (tmap_times_used[i])
658 mprintf( (0, "This mine has %d unique textures in it (~%d KB)\n", count, (count*4096) /1024 ));
661 // -- mprintf( (0, "Translate=%d\n", translate ));
663 hashtable_free( &ht );
666 //====================== READ VERTEX INFO ==========================
668 // New check added to make sure we don't read in too many vertices.
669 if ( mine_fileinfo.vertex_howmany > MAX_VERTICES )
671 mprintf((0, "Num vertices exceeds maximum. Loading MAX %d vertices\n", MAX_VERTICES));
672 mine_fileinfo.vertex_howmany = MAX_VERTICES;
675 if ( (mine_fileinfo.vertex_offset > -1) && (mine_fileinfo.vertex_howmany > 0))
677 if (cfseek( LoadFile, mine_fileinfo.vertex_offset, SEEK_SET ))
678 Error( "Error seeking to vertex_offset in gamemine.c" );
680 for (i=0; i< mine_fileinfo.vertex_howmany; i++ )
682 // Set the default values for this vertex
687 if (cfread( &Vertices[i], mine_fileinfo.vertex_sizeof, 1, LoadFile )!=1)
688 Error( "Error reading Vertices[i] in gamemine.c" );
692 //==================== READ SEGMENT INFO ===========================
694 // New check added to make sure we don't read in too many segments.
695 if ( mine_fileinfo.segment_howmany > MAX_SEGMENTS ) {
696 mprintf((0, "Num segments exceeds maximum. Loading MAX %d segments\n", MAX_SEGMENTS));
697 mine_fileinfo.segment_howmany = MAX_SEGMENTS;
698 mine_fileinfo.segment2_howmany = MAX_SEGMENTS;
701 // [commented out by mk on 11/20/94 (weren't we supposed to hit final in October?) because it looks redundant. I think I'll test it now...] fuelcen_reset();
703 if ( (mine_fileinfo.segment_offset > -1) && (mine_fileinfo.segment_howmany > 0)) {
705 if (cfseek( LoadFile, mine_fileinfo.segment_offset,SEEK_SET ))
707 Error( "Error seeking to segment_offset in gamemine.c" );
709 Highest_segment_index = mine_fileinfo.segment_howmany-1;
711 for (i=0; i< mine_fileinfo.segment_howmany; i++ ) {
713 // Set the default values for this segment (clear to zero )
714 //memset( &Segments[i], 0, sizeof(segment) );
716 if (mine_top_fileinfo.fileinfo_version < 20) {
719 Assert(mine_fileinfo.segment_sizeof == sizeof(v16_seg));
721 if (cfread( &v16_seg, mine_fileinfo.segment_sizeof, 1, LoadFile )!=1)
722 Error( "Error reading segments in gamemine.c" );
725 Segments[i].segnum = v16_seg.segnum;
726 // -- Segments[i].pad = v16_seg.pad;
729 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
730 Segments[i].sides[j] = v16_seg.sides[j];
732 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
733 Segments[i].children[j] = v16_seg.children[j];
735 for (j=0; j<MAX_VERTICES_PER_SEGMENT; j++)
736 Segments[i].verts[j] = v16_seg.verts[j];
738 Segment2s[i].special = v16_seg.special;
739 Segment2s[i].value = v16_seg.value;
740 Segment2s[i].s2_flags = 0;
741 Segment2s[i].matcen_num = v16_seg.matcen_num;
742 Segment2s[i].static_light = v16_seg.static_light;
743 fuelcen_activate( &Segments[i], Segment2s[i].special );
746 if (cfread( &Segments[i], mine_fileinfo.segment_sizeof, 1, LoadFile )!=1)
747 Error("Unable to read segment %i\n", i);
750 Segments[i].objects = -1;
752 Segments[i].group = -1;
755 if (mine_top_fileinfo.fileinfo_version < 15) { //used old uvl ranges
758 for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++)
759 for (uvln=0;uvln<4;uvln++) {
760 Segments[i].sides[sn].uvls[uvln].u /= 64;
761 Segments[i].sides[sn].uvls[uvln].v /= 64;
762 Segments[i].sides[sn].uvls[uvln].l /= 32;
767 for (j=0;j<MAX_SIDES_PER_SEGMENT;j++) {
768 unsigned short orient;
769 tmap_xlate = Segments[i].sides[j].tmap_num;
770 Segments[i].sides[j].tmap_num = tmap_xlate_table[tmap_xlate];
771 if ((WALL_IS_DOORWAY(&Segments[i],j) & WID_RENDER_FLAG))
772 if (Segments[i].sides[j].tmap_num < 0) {
773 mprintf( (0, "Couldn't find texture '%s' for Segment %d, side %d\n", old_tmap_list[tmap_xlate],i,j));
775 Segments[i].sides[j].tmap_num = NumTextures-1;
777 tmap_xlate = Segments[i].sides[j].tmap_num2 & TMAP_NUM_MASK;
778 orient = Segments[i].sides[j].tmap_num2 & (~TMAP_NUM_MASK);
779 if (tmap_xlate != 0) {
780 int xlated_tmap = tmap_xlate_table[tmap_xlate];
782 if ((WALL_IS_DOORWAY(&Segments[i],j) & WID_RENDER_FLAG))
783 if (xlated_tmap <= 0) {
784 mprintf( (0, "Couldn't find texture '%s' for Segment %d, side %d\n", old_tmap_list[tmap_xlate],i,j));
786 Segments[i].sides[j].tmap_num2 = NumTextures-1;
788 Segments[i].sides[j].tmap_num2 = xlated_tmap | orient;
794 if (mine_top_fileinfo.fileinfo_version >= 20)
795 for (i=0; i<=Highest_segment_index; i++) {
796 cfread(&Segment2s[i], sizeof(segment2), 1, LoadFile);
797 fuelcen_activate( &Segments[i], Segment2s[i].special );
801 //===================== READ NEWSEGMENT INFO =====================
805 { // Default segment created.
807 med_create_new_segment(vm_vec_make(&sizevec,DEFAULT_X_SIZE,DEFAULT_Y_SIZE,DEFAULT_Z_SIZE)); // New_segment = Segments[0];
808 //memset( &New_segment, 0, sizeof(segment) );
811 if (mine_editor.newsegment_offset > -1)
813 if (cfseek( LoadFile, mine_editor.newsegment_offset,SEEK_SET ))
814 Error( "Error seeking to newsegment_offset in gamemine.c" );
815 if (cfread( &New_segment, mine_editor.newsegment_size,1,LoadFile )!=1)
816 Error( "Error reading new_segment in gamemine.c" );
819 if ( (mine_fileinfo.newseg_verts_offset > -1) && (mine_fileinfo.newseg_verts_howmany > 0))
821 if (cfseek( LoadFile, mine_fileinfo.newseg_verts_offset, SEEK_SET ))
822 Error( "Error seeking to newseg_verts_offset in gamemine.c" );
823 for (i=0; i< mine_fileinfo.newseg_verts_howmany; i++ )
825 // Set the default values for this vertex
826 Vertices[NEW_SEGMENT_VERTICES+i].x = 1;
827 Vertices[NEW_SEGMENT_VERTICES+i].y = 1;
828 Vertices[NEW_SEGMENT_VERTICES+i].z = 1;
830 if (cfread( &Vertices[NEW_SEGMENT_VERTICES+i], mine_fileinfo.newseg_verts_sizeof,1,LoadFile )!=1)
831 Error( "Error reading Vertices[NEW_SEGMENT_VERTICES+i] in gamemine.c" );
833 New_segment.verts[i] = NEW_SEGMENT_VERTICES+i;
839 //========================= UPDATE VARIABLES ======================
843 // Setting to Markedsegp to NULL ignores Curside and Markedside, which
844 // we want to do when reading in an old file.
846 Markedside = mine_editor.Markedside;
847 Curside = mine_editor.Curside;
849 Groupside[i] = mine_editor.Groupside[i];
851 if ( mine_editor.current_seg != -1 )
852 Cursegp = mine_editor.current_seg + Segments;
856 if (mine_editor.Markedsegp != -1 )
857 Markedsegp = mine_editor.Markedsegp + Segments;
866 Num_vertices = mine_fileinfo.vertex_howmany;
867 Num_segments = mine_fileinfo.segment_howmany;
868 Highest_vertex_index = Num_vertices-1;
869 Highest_segment_index = Num_segments-1;
871 reset_objects(1); //one object, the player
874 Highest_vertex_index = MAX_SEGMENT_VERTICES-1;
875 Highest_segment_index = MAX_SEGMENTS-1;
877 Highest_vertex_index = Num_vertices-1;
878 Highest_segment_index = Num_segments-1;
880 warn_if_concave_segments();
884 validate_segment_all();
887 //create_local_segment_data();
889 //gamemine_find_textures();
891 if (mine_top_fileinfo.fileinfo_version < MINE_VERSION )
892 return 1; //old version
899 #define COMPILED_MINE_VERSION 0
901 void read_children(int segnum,ubyte bit_mask,CFILE *LoadFile)
905 for (bit=0; bit<MAX_SIDES_PER_SEGMENT; bit++) {
906 if (bit_mask & (1 << bit)) {
907 Segments[segnum].children[bit] = cfile_read_short(LoadFile);
909 Segments[segnum].children[bit] = -1;
913 void read_verts(int segnum,CFILE *LoadFile)
916 // Read short Segments[segnum].verts[MAX_VERTICES_PER_SEGMENT]
917 for (i = 0; i < MAX_VERTICES_PER_SEGMENT; i++)
918 Segments[segnum].verts[i] = cfile_read_short(LoadFile);
921 void read_special(int segnum,ubyte bit_mask,CFILE *LoadFile)
923 if (bit_mask & (1 << MAX_SIDES_PER_SEGMENT)) {
924 // Read ubyte Segment2s[segnum].special
925 Segment2s[segnum].special = cfile_read_byte(LoadFile);
926 // Read byte Segment2s[segnum].matcen_num
927 Segment2s[segnum].matcen_num = cfile_read_byte(LoadFile);
928 // Read short Segment2s[segnum].value
929 Segment2s[segnum].value = cfile_read_short(LoadFile);
931 Segment2s[segnum].special = 0;
932 Segment2s[segnum].matcen_num = -1;
933 Segment2s[segnum].value = 0;
937 int load_mine_data_compiled(CFILE *LoadFile)
939 int i, segnum, sidenum;
940 ubyte compiled_version;
945 if (!strcmp(strchr(Gamesave_current_filename, '.'), ".sdl"))
946 New_file_format_load = 0; // descent 1 shareware
948 New_file_format_load = 1;
950 // For compiled levels, textures map to themselves, prevent tmap_override always being gray,
951 // bug which Matt and John refused to acknowledge, so here is Mike, fixing it.
953 for (i=0; i<MAX_TEXTURES; i++)
954 tmap_xlate_table[i] = i;
957 // memset( Segments, 0, sizeof(segment)*MAX_SEGMENTS );
960 //=============================== Reading part ==============================
961 compiled_version = cfile_read_byte(LoadFile);
962 //Assert( compiled_version==COMPILED_MINE_VERSION );
963 if (compiled_version!=COMPILED_MINE_VERSION)
964 mprintf((0,"compiled mine version=%i\n", compiled_version)); //many levels have "wrong" versions. Theres no point in aborting because of it, I think.
966 if (New_file_format_load)
967 Num_vertices = cfile_read_short(LoadFile);
969 Num_vertices = cfile_read_int(LoadFile);
970 Assert( Num_vertices <= MAX_VERTICES );
972 if (New_file_format_load)
973 Num_segments = cfile_read_short(LoadFile);
975 Num_segments = cfile_read_int(LoadFile);
976 Assert( Num_segments <= MAX_SEGMENTS );
978 for (i = 0; i < Num_vertices; i++)
979 cfile_read_vector( &(Vertices[i]), LoadFile);
981 for (segnum=0; segnum<Num_segments; segnum++ ) {
984 Segments[segnum].segnum = segnum;
985 Segments[segnum].group = 0;
988 if (New_file_format_load)
989 bit_mask = cfile_read_byte(LoadFile);
991 bit_mask = 0x7f; // read all six children and special stuff...
993 if (Gamesave_current_version == 5) { // d2 SHAREWARE level
994 read_special(segnum,bit_mask,LoadFile);
995 read_verts(segnum,LoadFile);
996 read_children(segnum,bit_mask,LoadFile);
998 read_children(segnum,bit_mask,LoadFile);
999 read_verts(segnum,LoadFile);
1000 if (Gamesave_current_version <= 1) { // descent 1 level
1001 read_special(segnum,bit_mask,LoadFile);
1005 Segments[segnum].objects = -1;
1007 if (Gamesave_current_version <= 5) { // descent 1 thru d2 SHAREWARE level
1008 // Read fix Segments[segnum].static_light (shift down 5 bits, write as short)
1009 temp_ushort = cfile_read_short(LoadFile);
1010 Segment2s[segnum].static_light = ((fix)temp_ushort) << 4;
1011 //cfread( &Segments[segnum].static_light, sizeof(fix), 1, LoadFile );
1014 // Read the walls as a 6 byte array
1015 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++ ) {
1016 Segments[segnum].sides[sidenum].pad = 0;
1019 if (New_file_format_load)
1020 bit_mask = cfile_read_byte(LoadFile);
1022 bit_mask = 0x3f; // read all six sides
1023 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
1026 if (bit_mask & (1 << sidenum)) {
1027 byte_wallnum = cfile_read_byte(LoadFile);
1028 if ( byte_wallnum == 255 )
1029 Segments[segnum].sides[sidenum].wall_num = -1;
1031 Segments[segnum].sides[sidenum].wall_num = byte_wallnum;
1033 Segments[segnum].sides[sidenum].wall_num = -1;
1036 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++ ) {
1038 if ( (Segments[segnum].children[sidenum]==-1) || (Segments[segnum].sides[sidenum].wall_num!=-1) ) {
1039 // Read short Segments[segnum].sides[sidenum].tmap_num;
1040 if (New_file_format_load) {
1041 temp_ushort = cfile_read_short(LoadFile);
1042 Segments[segnum].sides[sidenum].tmap_num = temp_ushort & 0x7fff;
1044 Segments[segnum].sides[sidenum].tmap_num = cfile_read_short(LoadFile);
1046 if (Gamesave_current_version <= 1)
1047 Segments[segnum].sides[sidenum].tmap_num = convert_d1_tmap_num(Segments[segnum].sides[sidenum].tmap_num);
1049 if (New_file_format_load && !(temp_ushort & 0x8000))
1050 Segments[segnum].sides[sidenum].tmap_num2 = 0;
1052 // Read short Segments[segnum].sides[sidenum].tmap_num2;
1053 Segments[segnum].sides[sidenum].tmap_num2 = cfile_read_short(LoadFile);
1054 if (Gamesave_current_version <= 1 && Segments[segnum].sides[sidenum].tmap_num2 != 0)
1055 Segments[segnum].sides[sidenum].tmap_num2 = convert_d1_tmap_num(Segments[segnum].sides[sidenum].tmap_num2);
1058 // Read uvl Segments[segnum].sides[sidenum].uvls[4] (u,v>>5, write as short, l>>1 write as short)
1059 for (i=0; i<4; i++ ) {
1060 temp_short = cfile_read_short(LoadFile);
1061 Segments[segnum].sides[sidenum].uvls[i].u = ((fix)temp_short) << 5;
1062 temp_short = cfile_read_short(LoadFile);
1063 Segments[segnum].sides[sidenum].uvls[i].v = ((fix)temp_short) << 5;
1064 temp_ushort = cfile_read_short(LoadFile);
1065 Segments[segnum].sides[sidenum].uvls[i].l = ((fix)temp_ushort) << 1;
1066 //cfread( &Segments[segnum].sides[sidenum].uvls[i].l, sizeof(fix), 1, LoadFile );
1069 Segments[segnum].sides[sidenum].tmap_num = 0;
1070 Segments[segnum].sides[sidenum].tmap_num2 = 0;
1071 for (i=0; i<4; i++ ) {
1072 Segments[segnum].sides[sidenum].uvls[i].u = 0;
1073 Segments[segnum].sides[sidenum].uvls[i].v = 0;
1074 Segments[segnum].sides[sidenum].uvls[i].l = 0;
1084 fp = fopen("segments.out", "wt");
1085 for (i = 0; i <= Highest_segment_index; i++) {
1086 side sides[MAX_SIDES_PER_SEGMENT]; // 6 sides
1087 short children[MAX_SIDES_PER_SEGMENT]; // indices of 6 children segments, front, left, top, right, bottom, back
1088 short verts[MAX_VERTICES_PER_SEGMENT]; // vertex ids of 4 front and 4 back vertices
1089 int objects; // pointer to objects in this segment
1091 for (j = 0; j < MAX_SIDES_PER_SEGMENT; j++) {
1092 byte type; // replaces num_faces and tri_edge, 1 = quad, 2 = 0:2 triangulation, 3 = 1:3 triangulation
1093 ubyte pad; //keep us longword alligned
1098 vms_vector normals[2]; // 2 normals, if quadrilateral, both the same.
1099 fprintf(fp, "%d\n", Segments[i].sides[j].type);
1100 fprintf(fp, "%d\n", Segments[i].sides[j].pad);
1101 fprintf(fp, "%d\n", Segments[i].sides[j].wall_num);
1102 fprintf(fp, "%d\n", Segments[i].tmap_num);
1110 Highest_vertex_index = Num_vertices-1;
1111 Highest_segment_index = Num_segments-1;
1113 validate_segment_all(); // Fill in side type and normals.
1115 for (i=0; i<Num_segments; i++) {
1116 if (Gamesave_current_version > 5)
1117 segment2_read(&Segment2s[i], LoadFile);
1118 fuelcen_activate( &Segments[i], Segment2s[i].special );
1121 reset_objects(1); //one object, the player