]> icculus.org git repositories - btb/d2x.git/blob - main/gamemine.c
Add RCS ID tag
[btb/d2x.git] / main / gamemine.c
1 /* $Id: gamemine.c,v 1.32 2004-12-20 06:34:42 btb Exp $ */
2 /*
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.
13 */
14
15 /*
16  *
17  * Functions for loading mines in the game
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #ifdef RCS
26 static char rcsid[] = "$Id: gamemine.c,v 1.32 2004-12-20 06:34:42 btb Exp $";
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <string.h>
33
34 #include "pstypes.h"
35 #include "mono.h"
36
37 #include "inferno.h"
38 #include "segment.h"
39 #include "textures.h"
40 #include "wall.h"
41 #include "object.h"
42 #include "gamemine.h"
43 #include "error.h"
44 #include "gameseg.h"
45 #include "switch.h"
46
47 #include "game.h"
48 #include "newmenu.h"
49
50 #ifdef EDITOR
51 #include "editor/editor.h"
52 #endif
53
54 #include "cfile.h"
55 #include "fuelcen.h"
56
57 #include "hash.h"
58 #include "key.h"
59 #include "piggy.h"
60
61 #include "byteswap.h"
62 #include "gamesave.h"
63
64 #define REMOVE_EXT(s)  (*(strchr( (s), '.' ))='\0')
65
66 fix Level_shake_frequency = 0, Level_shake_duration = 0;
67 int Secret_return_segment = 0;
68 vms_matrix Secret_return_orient;
69
70 #ifdef EDITOR
71 struct mtfi mine_top_fileinfo; // Should be same as first two fields below...
72 struct mfi mine_fileinfo;
73 struct mh mine_header;
74 struct me mine_editor;
75 #endif // EDITOR
76
77 typedef struct v16_segment {
78         #ifdef EDITOR
79         short   segnum;             // segment number, not sure what it means
80         #endif
81         side    sides[MAX_SIDES_PER_SEGMENT];       // 6 sides
82         short   children[MAX_SIDES_PER_SEGMENT];    // indices of 6 children segments, front, left, top, right, bottom, back
83         short   verts[MAX_VERTICES_PER_SEGMENT];    // vertex ids of 4 front and 4 back vertices
84         #ifdef  EDITOR
85         short   group;              // group number to which the segment belongs.
86         #endif
87         short   objects;            // pointer to objects in this segment
88         ubyte   special;            // what type of center this is
89         sbyte   matcen_num;         // which center segment is associated with.
90         short   value;
91         fix     static_light;       // average static light in segment
92         #ifndef EDITOR
93         short   pad;                // make structure longword aligned
94         #endif
95 } v16_segment;
96
97 #if 0
98 struct mfi_v19 {
99         ushort  fileinfo_signature;
100         ushort  fileinfo_version;
101         int     fileinfo_sizeof;
102         int     header_offset;      // Stuff common to game & editor
103         int     header_size;
104         int     editor_offset;      // Editor specific stuff
105         int     editor_size;
106         int     segment_offset;
107         int     segment_howmany;
108         int     segment_sizeof;
109         int     newseg_verts_offset;
110         int     newseg_verts_howmany;
111         int     newseg_verts_sizeof;
112         int     group_offset;
113         int     group_howmany;
114         int     group_sizeof;
115         int     vertex_offset;
116         int     vertex_howmany;
117         int     vertex_sizeof;
118         int     texture_offset;
119         int     texture_howmany;
120         int     texture_sizeof;
121         int     walls_offset;
122         int     walls_howmany;
123         int     walls_sizeof;
124         int     triggers_offset;
125         int     triggers_howmany;
126         int     triggers_sizeof;
127         int     links_offset;
128         int     links_howmany;
129         int     links_sizeof;
130         int     object_offset;      // Object info
131         int     object_howmany;
132         int     object_sizeof;
133         int     unused_offset;      // was: doors_offset
134         int     unused_howmamy;     // was: doors_howmany
135         int     unused_sizeof;      // was: doors_sizeof
136         short   level_shake_frequency;  // Shakes every level_shake_frequency seconds
137         short   level_shake_duration;   // for level_shake_duration seconds (on average, random).  In 16ths second.
138         int     secret_return_segment;
139         vms_matrix  secret_return_orient;
140
141         int     dl_indices_offset;
142         int     dl_indices_howmany;
143         int     dl_indices_sizeof;
144
145         int     delta_light_offset;
146         int     delta_light_howmany;
147         int     delta_light_sizeof;
148
149 };
150 #endif // 0
151
152 int CreateDefaultNewSegment();
153
154 int New_file_format_load = 1; // "new file format" is everything newer than d1 shareware
155
156 int d1_pig_present = 0; // can descent.pig from descent 1 be loaded?
157
158 /* returns nonzero if d1_tmap_num references a texture which isn't available in d2. */
159 int d1_tmap_num_unique(short d1_tmap_num) {
160         switch (d1_tmap_num) {
161         case   0: case   2: case   4: case   5: case   6: case   7: case   9:
162         case  10: case  11: case  12: case  17: case  18:
163         case  20: case  21: case  25: case  28:
164         case  38: case  39: case  41: case  44: case  49:
165         case  50: case  55: case  57: case  88:
166         case 132: case 141: case 147:
167         case 154: case 155: case 158: case 159:
168         case 160: case 161: case 167: case 168: case 169:
169         case 170: case 171: case 174: case 175: case 185:
170         case 193: case 194: case 195: case 198: case 199:
171         case 200: case 202: case 210: case 211:
172         case 220: case 226: case 227: case 228: case 229: case 230:
173         case 240: case 241: case 242: case 243: case 246:
174         case 250: case 251: case 252: case 253: case 257: case 258: case 259:
175         case 260: case 263: case 266: case 283: case 298:
176         case 315: case 317: case 319: case 320: case 321:
177         case 330: case 331: case 332: case 333: case 349:
178         case 351: case 352: case 353: case 354:
179         case 355: case 357: case 358: case 359:
180         case 362: case 370: return 1;
181         default: return 0;
182         }
183 }
184
185 #define TMAP_NUM_MASK 0x3FFF
186
187 /* Converts descent 1 texture numbers to descent 2 texture numbers.
188  * Textures from d1 which are unique to d1 have extra spaces around "return".
189  * If we can load the original d1 pig, we make sure this function is bijective.
190  * This function was updated using the file config/convtabl.ini from devil 2.2.
191  */
192 short convert_d1_tmap_num(short d1_tmap_num) {
193         switch (d1_tmap_num) {
194         case 0: case 2: case 4: case 5:
195                 // all refer to grey rock001 (exception to bijectivity rule)
196                 return  d1_pig_present ? 137 : 43; // (devil:95)
197         case   1: return 0;
198         case   3: return 1; // rock021
199         case   6:  return  270; // blue rock002
200         case   7:  return  271; // yellow rock265
201         case   8: return 2; // rock004
202         case   9:  return  d1_pig_present ? 138 : 62; // purple (devil:179)
203         case  10:  return  272; // red rock006
204         case  11:  return  d1_pig_present ? 139 : 117;
205         case  12:  return  d1_pig_present ? 140 : 12; //devil:43
206         case  13: return 3; // rock014
207         case  14: return 4; // rock019
208         case  15: return 5; // rock020
209         case  16: return 6;
210         case  17:  return  d1_pig_present ? 141 : 52;
211         case  18:  return  129;
212         case  19: return 7;
213         case  20:  return  d1_pig_present ? 142 : 22;
214         case  21:  return  d1_pig_present ? 143 : 9;
215         case  22: return 8;
216         case  23: return 9;
217         case  24: return 10;
218         case  25:  return  d1_pig_present ? 144 : 12; //devil:35
219         case  26: return 11;
220         case  27: return 12;
221         case  28:  return  d1_pig_present ? 145 : 11; //devil:43
222         //range handled by default case, returns 13..21 (- 16)
223         case  38:  return  163; //devil:27
224         case  39:  return  147; //31
225         case  40: return 22;
226         case  41:  return  266;
227         case  42: return 23;
228         case  43: return 24;
229         case  44:  return  136; //devil:135
230         case  45: return 25;
231         case  46: return 26;
232         case  47: return 27;
233         case  48: return 28;
234         case  49:  return  d1_pig_present ? 146 : 43; //devil:60
235         case  50:  return  131; //devil:138
236         case  51: return 29;
237         case  52: return 30;
238         case  53: return 31;
239         case  54: return 32;
240         case  55:  return  165; //devil:193
241         case  56: return 33;
242         case  57:  return  132; //devil:119
243         // range handled by default case, returns 34..63 (- 24)
244         case  88:  return  197; //devil:15
245         // range handled by default case, returns 64..106 (- 25)
246         case 132:  return  167;
247         // range handled by default case, returns 107..114 (- 26)
248         case 141:  return  d1_pig_present ? 148 : 110; //devil:106
249         case 142: return 115;
250         case 143: return 116;
251         case 144: return 117;
252         case 145: return 118;
253         case 146: return 119;
254         case 147:  return  d1_pig_present ? 149 : 93;
255         case 148: return 120;
256         case 149: return 121;
257         case 150: return 122;
258         case 151: return 123;
259         case 152: return 124;
260         case 153: return 125; // rock263
261         case 154:  return  d1_pig_present ? 150 : 27;
262         case 155:  return  126; // rock269
263         case 156: return 200; // metl002
264         case 157: return 201; // metl003
265         case 158:  return  186; //devil:227
266         case 159:  return  190; //devil:246
267         case 160:  return  d1_pig_present ? 151 : 206;
268         case 161:  return  d1_pig_present ? 152 : 114; //devil:206
269         case 162: return 202;
270         case 163: return 203;
271         case 164: return 204;
272         case 165: return 205;
273         case 166: return 206;
274         case 167:  return  d1_pig_present ? 153 : 206;
275         case 168:  return  d1_pig_present ? 154 : 206;
276         case 169:  return  d1_pig_present ? 155 : 206;
277         case 170:  return  d1_pig_present ? 156 : 227;//206;
278         case 171:  return  d1_pig_present ? 157 : 206;//227;
279         case 172: return 207;
280         case 173: return 208;
281         case 174:  return  d1_pig_present ? 158 : 202;
282         case 175:  return  d1_pig_present ? 159 : 206;
283         // range handled by default case, returns 209..217 (+ 33)
284         case 185:  return  d1_pig_present ? 160 : 217;
285         // range handled by default case, returns 218..224 (+ 32)
286         case 193:  return  d1_pig_present ? 161 : 206;
287         case 194:  return  d1_pig_present ? 162 : 203;//206;
288         case 195:  return  d1_pig_present ? 166 : 234;
289         case 196: return 225;
290         case 197: return 226;
291         case 198:  return  d1_pig_present ? 167 : 225;
292         case 199:  return  d1_pig_present ? 168 : 206; //devil:204
293         case 200:  return  d1_pig_present ? 169 : 206; //devil:204
294         case 201: return 227;
295         case 202:  return  d1_pig_present ? 170 : 206; //devil:227
296         // range handled by default case, returns 228..234 (+ 25)
297         case 210:  return  d1_pig_present ? 171 : 234; //devil:242
298         case 211:  return  d1_pig_present ? 172 : 206; //devil:240
299         // range handled by default case, returns 235..242 (+ 23)
300         case 220:  return  d1_pig_present ? 173 : 242; //devil:240
301         case 221: return 243;
302         case 222: return 244;
303         case 223:  return  d1_pig_present ? 174 : 313;
304         case 224: return 245;
305         case 225: return 246;
306         case 226:  return  164;//247; matching names but not matching textures
307         case 227:  return  179; //devil:181
308         case 228:  return  196;//248; matching names but not matching textures
309         case 229:  return  d1_pig_present ? 175 : 15; //devil:66
310         case 230:  return  d1_pig_present ? 176 : 15; //devil:66
311         // range handled by default case, returns 249..257 (+ 18)
312         case 240:  return  d1_pig_present ? 177 : 6; //devil:132
313         case 241:  return  130; //devil:131
314         case 242:  return  d1_pig_present ? 178 : 78; //devil:15
315         case 243:  return  d1_pig_present ? 180 : 33; //devil:38
316         case 244: return 258;
317         case 245: return 259;
318         case 246:  return  d1_pig_present ? 181 : 321; // grate metl127
319         case 247: return 260;
320         case 248: return 261;
321         case 249: return 262;
322         case 250:  return  340; //  white doorframe metl126
323         case 251:  return  412; //    red doorframe metl133
324         case 252:  return  410; //   blue doorframe metl134
325         case 253:  return  411; // yellow doorframe metl135
326         case 254: return 263; // metl136
327         case 255: return 264; // metl139
328         case 256: return 265; // metl140
329         case 257:  return  d1_pig_present ? 182 : 249;//246; brig001
330         case 258:  return  d1_pig_present ? 183 : 251;//246; brig002
331         case 259:  return  d1_pig_present ? 184 : 252;//246; brig003
332         case 260:  return  d1_pig_present ? 185 : 256;//246; brig004
333         case 261: return 273; // exit01
334         case 262: return 274; // exit02
335         case 263:  return  d1_pig_present ? 187 : 281; // ceil001
336         case 264: return 275; // ceil002
337         case 265: return 276; // ceil003
338         case 266:  return  d1_pig_present ? 188 : 279; //devil:291
339         // range handled by default case, returns 277..291 (+ 10)
340         case 282: return 293;
341         case 283:  return  d1_pig_present ? 189 : 295;
342         case 284: return 295;
343         case 285: return 296;
344         case 286: return 298;
345         // range handled by default case, returns 300..310 (+ 13)
346         case 298:  return  d1_pig_present ? 191 : 364; // devil:374 misc010
347         // range handled by default case, returns 311..326 (+ 12)
348         case 315:  return  d1_pig_present ? 192 : 361; // bad producer misc044
349         // range handled by default case,  returns  327..337 (+ 11)
350         case 327: return 352; // arw01
351         case 328: return 353; // misc17
352         case 329: return 354; // fan01
353         case 330:  return  380; // mntr04
354         case 331:  return  379;//373; matching names but not matching textures
355         case 332:  return  355;//344; matching names but not matching textures
356         case 333:  return  409; // lava misc11 //devil:404
357         case 334: return 356; // ctrl04
358         case 335: return 357; // ctrl01
359         case 336: return 358; // ctrl02
360         case 337: return 359; // ctrl03
361         case 338: return 360; // misc14
362         case 339: return 361; // producer misc16
363         case 340: return 362; // misc049
364         case 341: return 364; // misc060
365         case 342: return 363; // blown01
366         case 343: return 366; // misc061
367         case 344: return 365;
368         case 345: return 368;
369         case 346: return 376;
370         case 347: return 370;
371         case 348: return 367;
372         case 349:  return  372;
373         case 350: return 369;
374         case 351:  return  374;//429; matching names but not matching textures
375         case 352:  return  375;//387; matching names but not matching textures
376         case 353:  return  371;
377         case 354:  return  377;//425; matching names but not matching textures
378         case 355:  return  408;
379         case 356: return 378; // lava02
380         case 357:  return  383;//384; matching names but not matching textures
381         case 358:  return  384;//385; matching names but not matching textures
382         case 359:  return  385;//386; matching names but not matching textures
383         case 360: return 386;
384         case 361: return 387;
385         case 362:  return  d1_pig_present ? 194 : 388; // mntr04b (devil: -1)
386         case 363: return 388;
387         case 364: return 391;
388         case 365: return 392;
389         case 366: return 393;
390         case 367: return 394;
391         case 368: return 395;
392         case 369: return 396;
393         case 370:  return  d1_pig_present ? 195 : 392; // mntr04d (devil: -1)
394         // range 371..584 handled by default case (wall01 and door frames)
395         default:
396                 // ranges:
397                 if (d1_tmap_num >= 29 && d1_tmap_num <= 37)
398                         return d1_tmap_num - 16;
399                 if (d1_tmap_num >= 58 && d1_tmap_num <= 87)
400                         return d1_tmap_num - 24;
401                 if (d1_tmap_num >= 89 && d1_tmap_num <= 131)
402                         return d1_tmap_num - 25;
403                 if (d1_tmap_num >= 133 && d1_tmap_num <= 140)
404                         return d1_tmap_num - 26;
405                 if (d1_tmap_num >= 176 && d1_tmap_num <= 184)
406                         return d1_tmap_num + 33;
407                 if (d1_tmap_num >= 186 && d1_tmap_num <= 192)
408                         return d1_tmap_num + 32;
409                 if (d1_tmap_num >= 203 && d1_tmap_num <= 209)
410                         return d1_tmap_num + 25;
411                 if (d1_tmap_num >= 212 && d1_tmap_num <= 219)
412                         return d1_tmap_num + 23;
413                 if (d1_tmap_num >= 231 && d1_tmap_num <= 239)
414                         return d1_tmap_num + 18;
415                 if (d1_tmap_num >= 267 && d1_tmap_num <= 281)
416                         return d1_tmap_num + 10;
417                 if (d1_tmap_num >= 287 && d1_tmap_num <= 297)
418                         return d1_tmap_num + 13;
419                 if (d1_tmap_num >= 299 && d1_tmap_num <= 314)
420                         return d1_tmap_num + 12;
421                 if (d1_tmap_num >= 316 && d1_tmap_num <= 326)
422                          return  d1_tmap_num + 11; // matching names but not matching textures
423                 // wall01 and door frames:
424                 if (d1_tmap_num > 370 && d1_tmap_num < 584) {
425                         if (New_file_format_load) return d1_tmap_num + 64;
426                         // d1 shareware needs special treatment:
427                         if (d1_tmap_num < 410) return d1_tmap_num + 68;
428                         if (d1_tmap_num < 417) return d1_tmap_num + 73;
429                         if (d1_tmap_num < 446) return d1_tmap_num + 91;
430                         if (d1_tmap_num < 453) return d1_tmap_num + 104;
431                         if (d1_tmap_num < 462) return d1_tmap_num + 111;
432                         if (d1_tmap_num < 486) return d1_tmap_num + 117;
433                         if (d1_tmap_num < 494) return d1_tmap_num + 141;
434                         if (d1_tmap_num < 584) return d1_tmap_num + 147;
435                 }
436                 { // handle rare case where orientation != 0
437                         short tmap_num = d1_tmap_num &  TMAP_NUM_MASK;
438                         short orient = d1_tmap_num & ~TMAP_NUM_MASK;
439                         if (orient != 0) {
440                                 return orient | convert_d1_tmap_num(tmap_num);
441                         } else {
442                                 Warning("can't convert unknown descent 1 texture #%d.\n", tmap_num);
443                                 return d1_tmap_num;
444                         }
445                 }
446         }
447 }
448
449 #ifdef EDITOR
450
451 static char old_tmap_list[MAX_TEXTURES][FILENAME_LEN];
452 short tmap_xlate_table[MAX_TEXTURES];
453 static short tmap_times_used[MAX_TEXTURES];
454
455 // -----------------------------------------------------------------------------
456 //loads from an already-open file
457 // returns 0=everything ok, 1=old version, -1=error
458 int load_mine_data(CFILE *LoadFile)
459 {
460         int   i, j,oldsizeadjust;
461         short tmap_xlate;
462         int     translate;
463         char    *temptr;
464         int     mine_start = cftell(LoadFile);
465
466         oldsizeadjust=(sizeof(int)*2)+sizeof (vms_matrix);
467         fuelcen_reset();
468
469         for (i=0; i<MAX_TEXTURES; i++ )
470                 tmap_times_used[i] = 0;
471
472         #ifdef EDITOR
473         // Create a new mine to initialize things.
474         //texpage_goto_first();
475         create_new_mine();
476         #endif
477
478         //===================== READ FILE INFO ========================
479
480         // These are the default values... version and fileinfo_sizeof
481         // don't have defaults.
482         mine_fileinfo.header_offset     =   -1;
483         mine_fileinfo.header_size       =   sizeof(mine_header);
484         mine_fileinfo.editor_offset     =   -1;
485         mine_fileinfo.editor_size       =   sizeof(mine_editor);
486         mine_fileinfo.vertex_offset     =   -1;
487         mine_fileinfo.vertex_howmany    =   0;
488         mine_fileinfo.vertex_sizeof     =   sizeof(vms_vector);
489         mine_fileinfo.segment_offset    =   -1;
490         mine_fileinfo.segment_howmany   =   0;
491         mine_fileinfo.segment_sizeof    =   sizeof(segment);
492         mine_fileinfo.newseg_verts_offset     =   -1;
493         mine_fileinfo.newseg_verts_howmany    =   0;
494         mine_fileinfo.newseg_verts_sizeof     =   sizeof(vms_vector);
495         mine_fileinfo.group_offset                =     -1;
496         mine_fileinfo.group_howmany       =     0;
497         mine_fileinfo.group_sizeof                =     sizeof(group);
498         mine_fileinfo.texture_offset    =   -1;
499         mine_fileinfo.texture_howmany   =   0;
500         mine_fileinfo.texture_sizeof    =   FILENAME_LEN;  // num characters in a name
501         mine_fileinfo.walls_offset                =     -1;
502         mine_fileinfo.walls_howmany       =     0;
503         mine_fileinfo.walls_sizeof                =     sizeof(wall);  
504         mine_fileinfo.triggers_offset     =     -1;
505         mine_fileinfo.triggers_howmany  =       0;
506         mine_fileinfo.triggers_sizeof     =     sizeof(trigger);  
507         mine_fileinfo.object_offset             =       -1;
508         mine_fileinfo.object_howmany            =       1;
509         mine_fileinfo.object_sizeof             =       sizeof(object);  
510
511         mine_fileinfo.level_shake_frequency             =       0;
512         mine_fileinfo.level_shake_duration              =       0;
513
514         //      Delta light stuff for blowing out light sources.
515 //      if (mine_top_fileinfo.fileinfo_version >= 19) {
516                 mine_fileinfo.dl_indices_offset         =       -1;
517                 mine_fileinfo.dl_indices_howmany                =       0;
518                 mine_fileinfo.dl_indices_sizeof         =       sizeof(dl_index);  
519
520                 mine_fileinfo.delta_light_offset                =       -1;
521                 mine_fileinfo.delta_light_howmany               =       0;
522                 mine_fileinfo.delta_light_sizeof                =       sizeof(delta_light);  
523
524 //      }
525
526         mine_fileinfo.segment2_offset           = -1;
527         mine_fileinfo.segment2_howmany  = 0;
528         mine_fileinfo.segment2_sizeof    = sizeof(segment2);
529
530         // Read in mine_top_fileinfo to get size of saved fileinfo.
531         
532         memset( &mine_top_fileinfo, 0, sizeof(mine_top_fileinfo) );
533
534         if (cfseek( LoadFile, mine_start, SEEK_SET ))
535                 Error( "Error moving to top of file in gamemine.c" );
536
537         if (cfread( &mine_top_fileinfo, sizeof(mine_top_fileinfo), 1, LoadFile )!=1)
538                 Error( "Error reading mine_top_fileinfo in gamemine.c" );
539
540         if (mine_top_fileinfo.fileinfo_signature != 0x2884)
541                 return -1;
542
543         // Check version number
544         if (mine_top_fileinfo.fileinfo_version < COMPATIBLE_VERSION )
545                 return -1;
546
547         // Now, Read in the fileinfo
548         if (cfseek( LoadFile, mine_start, SEEK_SET ))
549                 Error( "Error seeking to top of file in gamemine.c" );
550
551         if (cfread( &mine_fileinfo, mine_top_fileinfo.fileinfo_sizeof, 1, LoadFile )!=1)
552                 Error( "Error reading mine_fileinfo in gamemine.c" );
553
554         if (mine_top_fileinfo.fileinfo_version < 18) {
555                 mprintf((1, "Old version, setting shake intensity to 0.\n"));
556                 Level_shake_frequency = 0;
557                 Level_shake_duration = 0;
558                 Secret_return_segment = 0;
559                 Secret_return_orient = vmd_identity_matrix;
560         } else {
561                 Level_shake_frequency = mine_fileinfo.level_shake_frequency << 12;
562                 Level_shake_duration = mine_fileinfo.level_shake_duration << 12;
563                 Secret_return_segment = mine_fileinfo.secret_return_segment;
564                 Secret_return_orient = mine_fileinfo.secret_return_orient;
565         }
566
567         //===================== READ HEADER INFO ========================
568
569         // Set default values.
570         mine_header.num_vertices        =   0;
571         mine_header.num_segments        =   0;
572
573         if (mine_fileinfo.header_offset > -1 )
574         {
575                 if (cfseek( LoadFile, mine_fileinfo.header_offset, SEEK_SET ))
576                         Error( "Error seeking to header_offset in gamemine.c" );
577         
578                 if (cfread( &mine_header, mine_fileinfo.header_size, 1, LoadFile )!=1)
579                         Error( "Error reading mine_header in gamemine.c" );
580         }
581
582         //===================== READ EDITOR INFO ==========================
583
584         // Set default values
585         mine_editor.current_seg         =   0;
586         mine_editor.newsegment_offset   =   -1; // To be written
587         mine_editor.newsegment_size     =   sizeof(segment);
588         mine_editor.Curside             =   0;
589         mine_editor.Markedsegp          =   -1;
590         mine_editor.Markedside          =   0;
591
592         if (mine_fileinfo.editor_offset > -1 )
593         {
594                 if (cfseek( LoadFile, mine_fileinfo.editor_offset, SEEK_SET ))
595                         Error( "Error seeking to editor_offset in gamemine.c" );
596         
597                 if (cfread( &mine_editor, mine_fileinfo.editor_size, 1, LoadFile )!=1)
598                         Error( "Error reading mine_editor in gamemine.c" );
599         }
600
601         //===================== READ TEXTURE INFO ==========================
602
603         if ( (mine_fileinfo.texture_offset > -1) && (mine_fileinfo.texture_howmany > 0))
604         {
605                 if (cfseek( LoadFile, mine_fileinfo.texture_offset, SEEK_SET ))
606                         Error( "Error seeking to texture_offset in gamemine.c" );
607
608                 for (i=0; i< mine_fileinfo.texture_howmany; i++ )
609                 {
610                         if (cfread( &old_tmap_list[i], mine_fileinfo.texture_sizeof, 1, LoadFile )!=1)
611                                 Error( "Error reading old_tmap_list[i] in gamemine.c" );
612                 }
613         }
614
615         //=============== GENERATE TEXTURE TRANSLATION TABLE ===============
616
617         translate = 0;
618         
619         Assert (NumTextures < MAX_TEXTURES);
620
621         {
622                 hashtable ht;
623         
624                 hashtable_init( &ht, NumTextures );
625         
626                 // Remove all the file extensions in the textures list
627         
628                 for (i=0;i<NumTextures;i++)     {
629                         temptr = strchr(TmapInfo[i].filename, '.');
630                         if (temptr) *temptr = '\0';
631                         hashtable_insert( &ht, TmapInfo[i].filename, i );
632                 }
633         
634                 // For every texture, search through the texture list
635                 // to find a matching name.
636                 for (j=0;j<mine_fileinfo.texture_howmany;j++)   {
637                         // Remove this texture name's extension
638                         temptr = strchr(old_tmap_list[j], '.');
639                         if (temptr) *temptr = '\0';
640         
641                         tmap_xlate_table[j] = hashtable_search( &ht,old_tmap_list[j]);
642                         if (tmap_xlate_table[j] < 0 )   {
643                                 //tmap_xlate_table[j] = 0;
644                                 // mprintf( (0, "Couldn't find texture '%s'\n", old_tmap_list[j] ));
645                                 ;
646                         }
647                         if (tmap_xlate_table[j] != j ) translate = 1;
648                         if (tmap_xlate_table[j] >= 0)
649                                 tmap_times_used[tmap_xlate_table[j]]++;
650                 }
651         
652                 {
653                         int count = 0;
654                         for (i=0; i<MAX_TEXTURES; i++ )
655                                 if (tmap_times_used[i])
656                                         count++;
657                         mprintf( (0, "This mine has %d unique textures in it (~%d KB)\n", count, (count*4096) /1024 ));
658                 }
659         
660                 // -- mprintf( (0, "Translate=%d\n", translate ));
661         
662                 hashtable_free( &ht );
663         }
664
665         //====================== READ VERTEX INFO ==========================
666
667         // New check added to make sure we don't read in too many vertices.
668         if ( mine_fileinfo.vertex_howmany > MAX_VERTICES )
669                 {
670                 mprintf((0, "Num vertices exceeds maximum.  Loading MAX %d vertices\n", MAX_VERTICES));
671                 mine_fileinfo.vertex_howmany = MAX_VERTICES;
672                 }
673
674         if ( (mine_fileinfo.vertex_offset > -1) && (mine_fileinfo.vertex_howmany > 0))
675         {
676                 if (cfseek( LoadFile, mine_fileinfo.vertex_offset, SEEK_SET ))
677                         Error( "Error seeking to vertex_offset in gamemine.c" );
678
679                 for (i=0; i< mine_fileinfo.vertex_howmany; i++ )
680                 {
681                         // Set the default values for this vertex
682                         Vertices[i].x = 1;
683                         Vertices[i].y = 1;
684                         Vertices[i].z = 1;
685
686                         if (cfread( &Vertices[i], mine_fileinfo.vertex_sizeof, 1, LoadFile )!=1)
687                                 Error( "Error reading Vertices[i] in gamemine.c" );
688                 }
689         }
690
691         //==================== READ SEGMENT INFO ===========================
692
693         // New check added to make sure we don't read in too many segments.
694         if ( mine_fileinfo.segment_howmany > MAX_SEGMENTS ) {
695                 mprintf((0, "Num segments exceeds maximum.  Loading MAX %d segments\n", MAX_SEGMENTS));
696                 mine_fileinfo.segment_howmany = MAX_SEGMENTS;
697                 mine_fileinfo.segment2_howmany = MAX_SEGMENTS;
698         }
699
700         // [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();
701
702         if ( (mine_fileinfo.segment_offset > -1) && (mine_fileinfo.segment_howmany > 0))        {
703
704                 if (cfseek( LoadFile, mine_fileinfo.segment_offset,SEEK_SET ))
705
706                         Error( "Error seeking to segment_offset in gamemine.c" );
707
708                 Highest_segment_index = mine_fileinfo.segment_howmany-1;
709
710                 for (i=0; i< mine_fileinfo.segment_howmany; i++ ) {
711
712                         // Set the default values for this segment (clear to zero )
713                         //memset( &Segments[i], 0, sizeof(segment) );
714
715                         if (mine_top_fileinfo.fileinfo_version < 20) {
716                                 v16_segment v16_seg;
717
718                                 Assert(mine_fileinfo.segment_sizeof == sizeof(v16_seg));
719
720                                 if (cfread( &v16_seg, mine_fileinfo.segment_sizeof, 1, LoadFile )!=1)
721                                         Error( "Error reading segments in gamemine.c" );
722
723                                 #ifdef EDITOR
724                                 Segments[i].segnum = v16_seg.segnum;
725                                 // -- Segments[i].pad = v16_seg.pad;
726                                 #endif
727
728                                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
729                                         Segments[i].sides[j] = v16_seg.sides[j];
730
731                                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
732                                         Segments[i].children[j] = v16_seg.children[j];
733
734                                 for (j=0; j<MAX_VERTICES_PER_SEGMENT; j++)
735                                         Segments[i].verts[j] = v16_seg.verts[j];
736
737                                 Segment2s[i].special = v16_seg.special;
738                                 Segment2s[i].value = v16_seg.value;
739                                 Segment2s[i].s2_flags = 0;
740                                 Segment2s[i].matcen_num = v16_seg.matcen_num;
741                                 Segment2s[i].static_light = v16_seg.static_light;
742                                 fuelcen_activate( &Segments[i], Segment2s[i].special );
743
744                         } else  {
745                                 if (cfread( &Segments[i], mine_fileinfo.segment_sizeof, 1, LoadFile )!=1)
746                                         Error("Unable to read segment %i\n", i);
747                         }
748
749                         Segments[i].objects = -1;
750                         #ifdef EDITOR
751                         Segments[i].group = -1;
752                         #endif
753
754                         if (mine_top_fileinfo.fileinfo_version < 15) {  //used old uvl ranges
755                                 int sn,uvln;
756
757                                 for (sn=0;sn<MAX_SIDES_PER_SEGMENT;sn++)
758                                         for (uvln=0;uvln<4;uvln++) {
759                                                 Segments[i].sides[sn].uvls[uvln].u /= 64;
760                                                 Segments[i].sides[sn].uvls[uvln].v /= 64;
761                                                 Segments[i].sides[sn].uvls[uvln].l /= 32;
762                                         }
763                         }
764
765                         if (translate == 1)
766                                 for (j=0;j<MAX_SIDES_PER_SEGMENT;j++) {
767                                         unsigned short orient;
768                                         tmap_xlate = Segments[i].sides[j].tmap_num;
769                                         Segments[i].sides[j].tmap_num = tmap_xlate_table[tmap_xlate];
770                                         if ((WALL_IS_DOORWAY(&Segments[i],j) & WID_RENDER_FLAG))
771                                                 if (Segments[i].sides[j].tmap_num < 0)  {
772                                                         mprintf( (0, "Couldn't find texture '%s' for Segment %d, side %d\n", old_tmap_list[tmap_xlate],i,j));
773                                                         Int3();
774                                                         Segments[i].sides[j].tmap_num = NumTextures-1;
775                                                 }
776                                         tmap_xlate = Segments[i].sides[j].tmap_num2 & TMAP_NUM_MASK;
777                                         orient = Segments[i].sides[j].tmap_num2 & (~TMAP_NUM_MASK);
778                                         if (tmap_xlate != 0) {
779                                                 int xlated_tmap = tmap_xlate_table[tmap_xlate];
780
781                                                 if ((WALL_IS_DOORWAY(&Segments[i],j) & WID_RENDER_FLAG))
782                                                         if (xlated_tmap <= 0)   {
783                                                                 mprintf( (0, "Couldn't find texture '%s' for Segment %d, side %d\n", old_tmap_list[tmap_xlate],i,j));
784                                                                 Int3();
785                                                                 Segments[i].sides[j].tmap_num2 = NumTextures-1;
786                                                         }
787                                                 Segments[i].sides[j].tmap_num2 = xlated_tmap | orient;
788                                         }
789                                 }
790                 }
791
792
793                 if (mine_top_fileinfo.fileinfo_version >= 20)
794                         for (i=0; i<=Highest_segment_index; i++) {
795                                 cfread(&Segment2s[i], sizeof(segment2), 1, LoadFile);
796                                 fuelcen_activate( &Segments[i], Segment2s[i].special );
797                         }
798         }
799
800         //===================== READ NEWSEGMENT INFO =====================
801
802         #ifdef EDITOR
803
804         {               // Default segment created.
805                 vms_vector      sizevec;
806                 med_create_new_segment(vm_vec_make(&sizevec,DEFAULT_X_SIZE,DEFAULT_Y_SIZE,DEFAULT_Z_SIZE));             // New_segment = Segments[0];
807                 //memset( &New_segment, 0, sizeof(segment) );
808         }
809
810         if (mine_editor.newsegment_offset > -1)
811         {
812                 if (cfseek( LoadFile, mine_editor.newsegment_offset,SEEK_SET ))
813                         Error( "Error seeking to newsegment_offset in gamemine.c" );
814                 if (cfread( &New_segment, mine_editor.newsegment_size,1,LoadFile )!=1)
815                         Error( "Error reading new_segment in gamemine.c" );
816         }
817
818         if ( (mine_fileinfo.newseg_verts_offset > -1) && (mine_fileinfo.newseg_verts_howmany > 0))
819         {
820                 if (cfseek( LoadFile, mine_fileinfo.newseg_verts_offset, SEEK_SET ))
821                         Error( "Error seeking to newseg_verts_offset in gamemine.c" );
822                 for (i=0; i< mine_fileinfo.newseg_verts_howmany; i++ )
823                 {
824                         // Set the default values for this vertex
825                         Vertices[NEW_SEGMENT_VERTICES+i].x = 1;
826                         Vertices[NEW_SEGMENT_VERTICES+i].y = 1;
827                         Vertices[NEW_SEGMENT_VERTICES+i].z = 1;
828                         
829                         if (cfread( &Vertices[NEW_SEGMENT_VERTICES+i], mine_fileinfo.newseg_verts_sizeof,1,LoadFile )!=1)
830                                 Error( "Error reading Vertices[NEW_SEGMENT_VERTICES+i] in gamemine.c" );
831
832                         New_segment.verts[i] = NEW_SEGMENT_VERTICES+i;
833                 }
834         }
835
836         #endif
837                                                                                                                         
838         //========================= UPDATE VARIABLES ======================
839
840         #ifdef EDITOR
841
842         // Setting to Markedsegp to NULL ignores Curside and Markedside, which
843         // we want to do when reading in an old file.
844         
845         Markedside = mine_editor.Markedside;
846         Curside = mine_editor.Curside;
847         for (i=0;i<10;i++)
848                 Groupside[i] = mine_editor.Groupside[i];
849
850         if ( mine_editor.current_seg != -1 )
851                 Cursegp = mine_editor.current_seg + Segments;
852         else
853                 Cursegp = NULL;
854
855         if (mine_editor.Markedsegp != -1 ) 
856                 Markedsegp = mine_editor.Markedsegp + Segments;
857         else
858                 Markedsegp = NULL;
859
860         num_groups = 0;
861         current_group = -1;
862
863         #endif
864
865         Num_vertices = mine_fileinfo.vertex_howmany;
866         Num_segments = mine_fileinfo.segment_howmany;
867         Highest_vertex_index = Num_vertices-1;
868         Highest_segment_index = Num_segments-1;
869
870         reset_objects(1);               //one object, the player
871
872         #ifdef EDITOR
873         Highest_vertex_index = MAX_SEGMENT_VERTICES-1;
874         Highest_segment_index = MAX_SEGMENTS-1;
875         set_vertex_counts();
876         Highest_vertex_index = Num_vertices-1;
877         Highest_segment_index = Num_segments-1;
878
879         warn_if_concave_segments();
880         #endif
881
882         #ifdef EDITOR
883                 validate_segment_all();
884         #endif
885
886         //create_local_segment_data();
887
888         //gamemine_find_textures();
889
890         if (mine_top_fileinfo.fileinfo_version < MINE_VERSION )
891                 return 1;               //old version
892         else
893                 return 0;
894
895 }
896 #endif
897
898 #define COMPILED_MINE_VERSION 0
899
900 void read_children(int segnum,ubyte bit_mask,CFILE *LoadFile)
901 {
902         int bit;
903
904         for (bit=0; bit<MAX_SIDES_PER_SEGMENT; bit++) {
905                 if (bit_mask & (1 << bit)) {
906                         Segments[segnum].children[bit] = cfile_read_short(LoadFile);
907                 } else
908                         Segments[segnum].children[bit] = -1;
909         }
910 }
911
912 void read_verts(int segnum,CFILE *LoadFile)
913 {
914         int i;
915         // Read short Segments[segnum].verts[MAX_VERTICES_PER_SEGMENT]
916         for (i = 0; i < MAX_VERTICES_PER_SEGMENT; i++)
917                 Segments[segnum].verts[i] = cfile_read_short(LoadFile);
918 }
919
920 void read_special(int segnum,ubyte bit_mask,CFILE *LoadFile)
921 {
922         if (bit_mask & (1 << MAX_SIDES_PER_SEGMENT)) {
923                 // Read ubyte   Segment2s[segnum].special
924                 Segment2s[segnum].special = cfile_read_byte(LoadFile);
925                 // Read byte    Segment2s[segnum].matcen_num
926                 Segment2s[segnum].matcen_num = cfile_read_byte(LoadFile);
927                 // Read short   Segment2s[segnum].value
928                 Segment2s[segnum].value = cfile_read_short(LoadFile);
929         } else {
930                 Segment2s[segnum].special = 0;
931                 Segment2s[segnum].matcen_num = -1;
932                 Segment2s[segnum].value = 0;
933         }
934 }
935
936 int load_mine_data_compiled(CFILE *LoadFile)
937 {
938         int     i, segnum, sidenum;
939         ubyte   compiled_version;
940         short   temp_short;
941         ushort  temp_ushort = 0;
942         ubyte   bit_mask;
943
944         d1_pig_present = cfexist(D1_PIGFILE);
945 #if 0 // the following will be deleted once reading old pigfiles works reliably
946         if (d1_pig_present) {
947                 CFILE * d1_Piggy_fp = cfopen( D1_PIGFILE, "rb" );
948                 switch (cfilelength(d1_Piggy_fp)) {
949                 case D1_SHARE_BIG_PIGSIZE:
950                 case D1_SHARE_10_PIGSIZE:
951                 case D1_SHARE_PIGSIZE:
952                 case D1_10_BIG_PIGSIZE:
953                 case D1_10_PIGSIZE:
954                         //d1_pig_present = 0;
955                 }
956                 cfclose (d1_Piggy_fp);
957         }
958 #endif
959
960         if (!strcmp(strchr(Gamesave_current_filename, '.'), ".sdl"))
961                 New_file_format_load = 0; // descent 1 shareware
962         else
963                 New_file_format_load = 1;
964
965         //      For compiled levels, textures map to themselves, prevent tmap_override always being gray,
966         //      bug which Matt and John refused to acknowledge, so here is Mike, fixing it.
967 #ifdef EDITOR
968         for (i=0; i<MAX_TEXTURES; i++)
969                 tmap_xlate_table[i] = i;
970 #endif
971
972 //      memset( Segments, 0, sizeof(segment)*MAX_SEGMENTS );
973         fuelcen_reset();
974
975         //=============================== Reading part ==============================
976         compiled_version = cfile_read_byte(LoadFile);
977         //Assert( compiled_version==COMPILED_MINE_VERSION );
978         if (compiled_version!=COMPILED_MINE_VERSION)
979                 mprintf((0,"compiled mine version=%i\n", compiled_version)); //many levels have "wrong" versions.  Theres no point in aborting because of it, I think.
980
981         if (New_file_format_load)
982                 Num_vertices = cfile_read_short(LoadFile);
983         else
984                 Num_vertices = cfile_read_int(LoadFile);
985         Assert( Num_vertices <= MAX_VERTICES );
986
987         if (New_file_format_load)
988                 Num_segments = cfile_read_short(LoadFile);
989         else
990                 Num_segments = cfile_read_int(LoadFile);
991         Assert( Num_segments <= MAX_SEGMENTS );
992
993         for (i = 0; i < Num_vertices; i++)
994                 cfile_read_vector( &(Vertices[i]), LoadFile);
995
996         for (segnum=0; segnum<Num_segments; segnum++ )  {
997
998                 #ifdef EDITOR
999                 Segments[segnum].segnum = segnum;
1000                 Segments[segnum].group = 0;
1001                 #endif
1002
1003                 if (New_file_format_load)
1004                         bit_mask = cfile_read_byte(LoadFile);
1005                 else
1006                         bit_mask = 0x7f; // read all six children and special stuff...
1007
1008                 if (Gamesave_current_version == 5) { // d2 SHAREWARE level
1009                         read_special(segnum,bit_mask,LoadFile);
1010                         read_verts(segnum,LoadFile);
1011                         read_children(segnum,bit_mask,LoadFile);
1012                 } else {
1013                         read_children(segnum,bit_mask,LoadFile);
1014                         read_verts(segnum,LoadFile);
1015                         if (Gamesave_current_version <= 1) { // descent 1 level
1016                                 read_special(segnum,bit_mask,LoadFile);
1017                         }
1018                 }
1019
1020                 Segments[segnum].objects = -1;
1021
1022                 if (Gamesave_current_version <= 5) { // descent 1 thru d2 SHAREWARE level
1023                         // Read fix     Segments[segnum].static_light (shift down 5 bits, write as short)
1024                         temp_ushort = cfile_read_short(LoadFile);
1025                         Segment2s[segnum].static_light  = ((fix)temp_ushort) << 4;
1026                         //cfread( &Segments[segnum].static_light, sizeof(fix), 1, LoadFile );
1027                 }
1028
1029                 // Read the walls as a 6 byte array
1030                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++ )      {
1031                         Segments[segnum].sides[sidenum].pad = 0;
1032                 }
1033
1034                 if (New_file_format_load)
1035                         bit_mask = cfile_read_byte(LoadFile);
1036                 else
1037                         bit_mask = 0x3f; // read all six sides
1038                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
1039                         ubyte byte_wallnum;
1040
1041                         if (bit_mask & (1 << sidenum)) {
1042                                 byte_wallnum = cfile_read_byte(LoadFile);
1043                                 if ( byte_wallnum == 255 )
1044                                         Segments[segnum].sides[sidenum].wall_num = -1;
1045                                 else
1046                                         Segments[segnum].sides[sidenum].wall_num = byte_wallnum;
1047                         } else
1048                                         Segments[segnum].sides[sidenum].wall_num = -1;
1049                 }
1050
1051                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++ )      {
1052
1053                         if ( (Segments[segnum].children[sidenum]==-1) || (Segments[segnum].sides[sidenum].wall_num!=-1) )       {
1054                                 // Read short Segments[segnum].sides[sidenum].tmap_num;
1055                                 if (New_file_format_load) {
1056                                         temp_ushort = cfile_read_short(LoadFile);
1057                                         Segments[segnum].sides[sidenum].tmap_num = temp_ushort & 0x7fff;
1058                                 } else
1059                                         Segments[segnum].sides[sidenum].tmap_num = cfile_read_short(LoadFile);
1060
1061                                 if (Gamesave_current_version <= 1)
1062                                         Segments[segnum].sides[sidenum].tmap_num = convert_d1_tmap_num(Segments[segnum].sides[sidenum].tmap_num);
1063
1064                                 if (New_file_format_load && !(temp_ushort & 0x8000))
1065                                         Segments[segnum].sides[sidenum].tmap_num2 = 0;
1066                                 else {
1067                                         // Read short Segments[segnum].sides[sidenum].tmap_num2;
1068                                         Segments[segnum].sides[sidenum].tmap_num2 = cfile_read_short(LoadFile);
1069                                         if (Gamesave_current_version <= 1 && Segments[segnum].sides[sidenum].tmap_num2 != 0)
1070                                                 Segments[segnum].sides[sidenum].tmap_num2 = convert_d1_tmap_num(Segments[segnum].sides[sidenum].tmap_num2);
1071                                 }
1072
1073                                 // Read uvl Segments[segnum].sides[sidenum].uvls[4] (u,v>>5, write as short, l>>1 write as short)
1074                                 for (i=0; i<4; i++ )    {
1075                                         temp_short = cfile_read_short(LoadFile);
1076                                         Segments[segnum].sides[sidenum].uvls[i].u = ((fix)temp_short) << 5;
1077                                         temp_short = cfile_read_short(LoadFile);
1078                                         Segments[segnum].sides[sidenum].uvls[i].v = ((fix)temp_short) << 5;
1079                                         temp_ushort = cfile_read_short(LoadFile);
1080                                         Segments[segnum].sides[sidenum].uvls[i].l = ((fix)temp_ushort) << 1;
1081                                         //cfread( &Segments[segnum].sides[sidenum].uvls[i].l, sizeof(fix), 1, LoadFile );
1082                                 }
1083                         } else {
1084                                 Segments[segnum].sides[sidenum].tmap_num = 0;
1085                                 Segments[segnum].sides[sidenum].tmap_num2 = 0;
1086                                 for (i=0; i<4; i++ )    {
1087                                         Segments[segnum].sides[sidenum].uvls[i].u = 0;
1088                                         Segments[segnum].sides[sidenum].uvls[i].v = 0;
1089                                         Segments[segnum].sides[sidenum].uvls[i].l = 0;
1090                                 }
1091                         }
1092                 }
1093         }
1094
1095 #if 0
1096         {
1097                 FILE *fp;
1098
1099                 fp = fopen("segments.out", "wt");
1100                 for (i = 0; i <= Highest_segment_index; i++) {
1101                         side    sides[MAX_SIDES_PER_SEGMENT];   // 6 sides
1102                         short   children[MAX_SIDES_PER_SEGMENT];        // indices of 6 children segments, front, left, top, right, bottom, back
1103                         short   verts[MAX_VERTICES_PER_SEGMENT];        // vertex ids of 4 front and 4 back vertices
1104                         int             objects;                                                                // pointer to objects in this segment
1105
1106                         for (j = 0; j < MAX_SIDES_PER_SEGMENT; j++) {
1107                                 sbyte   type;                           // replaces num_faces and tri_edge, 1 = quad, 2 = 0:2 triangulation, 3 = 1:3 triangulation
1108                                 ubyte   pad;                                                                    //keep us longword alligned
1109                                 short   wall_num;
1110                                 short   tmap_num;
1111                                 short   tmap_num2;
1112                                 uvl             uvls[4];
1113                                 vms_vector      normals[2];                                             // 2 normals, if quadrilateral, both the same.
1114                                 fprintf(fp, "%d\n", Segments[i].sides[j].type);
1115                                 fprintf(fp, "%d\n", Segments[i].sides[j].pad);
1116                                 fprintf(fp, "%d\n", Segments[i].sides[j].wall_num);
1117                                 fprintf(fp, "%d\n", Segments[i].tmap_num);
1118
1119                         }
1120                         fclose(fp);
1121                 }
1122         }
1123 #endif
1124
1125         Highest_vertex_index = Num_vertices-1;
1126         Highest_segment_index = Num_segments-1;
1127
1128         validate_segment_all();                 // Fill in side type and normals.
1129
1130         for (i=0; i<Num_segments; i++) {
1131                 if (Gamesave_current_version > 5)
1132                         segment2_read(&Segment2s[i], LoadFile);
1133                 fuelcen_activate( &Segments[i], Segment2s[i].special );
1134         }
1135
1136         reset_objects(1);               //one object, the player
1137
1138         return 0;
1139 }