]> icculus.org git repositories - btb/d2x.git/blob - main/gamemine.c
move old per-file change logs into new file ChangeLog-old
[btb/d2x.git] / main / gamemine.c
1 /* $Id: gamemine.c,v 1.28 2004-08-28 23:17:45 schaffner 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.28 2004-08-28 23:17:45 schaffner 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 #if 0
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 // 0
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: // all refer to rock001 (exception to bijectivity rule)
195                 return  d1_pig_present ? 137 : 43;  // grey (devil:95)
196         case   1: return 0;
197         case   3: return 1; // rock021
198         case   6:  return  270; // blue
199         case   7:  return  271; // yellow
200         case   8: return 2;
201         case   9:  return  d1_pig_present ? 138 : 62; // purple (devil:179)
202         case  10:  return  272; // red
203         case  11:  return  d1_pig_present ? 139 : 117;
204         case  12:  return  d1_pig_present ? 140 : 12; //devil:43
205         case  13: return 3;
206         case  14: return 4;
207         case  15: return 5;
208         case  16: return 6;
209         case  17:  return  d1_pig_present ? 141 : 52;
210         case  18:  return  129;
211         case  19: return 7;
212         case  20:  return  d1_pig_present ? 142 : 22;
213         case  21:  return  d1_pig_present ? 143 : 9;
214         case  22: return 8;
215         case  23: return 9;
216         case  24: return 10;
217         case  25:  return  d1_pig_present ? 144 : 12; //devil:35
218         case  26: return 11;
219         case  27: return 12;
220         case  28:  return  d1_pig_present ? 145 : 11; //devil:43
221         //range handled by default case, returns 13..21
222         case  38:  return  163; //devil:27
223         case  39:  return  147; //31
224         case  40: return 22;
225         case  41:  return  266;
226         case  42: return 23;
227         case  43: return 24;
228         case  44:  return  136; //devil:135
229         case  45: return 25;
230         case  46: return 26;
231         case  47: return 27;
232         case  48: return 28;
233         case  49:  return  d1_pig_present ? 146 : 43; //devil:60
234         case  50:  return  131; //devil:138
235         case  51: return 29;
236         case  52: return 30;
237         case  53: return 31;
238         case  54: return 32;
239         case  55:  return  165; //devil:193
240         case  56: return 33;
241         case  57:  return  132; //devil:119
242         // range handled by default case, returns 34..63
243         case  88:  return  197; //devil:15
244         // range handled by default case, returns 64..106
245         case 132:  return  167;
246         // range handled by default case, returns 107..114
247         case 141:  return  d1_pig_present ? 148 : 110; //devil:106
248         case 142: return 115;
249         case 143: return 116;
250         case 144: return 117;
251         case 145: return 118;
252         case 146: return 119;
253         case 147:  return  d1_pig_present ? 149 : 93;
254         case 148: return 120;
255         case 149: return 121;
256         case 150: return 122;
257         case 151: return 123;
258         case 152: return 124;
259         case 153: return 125;
260         case 154:  return  d1_pig_present ? 150 : 27;
261         case 155:  return  126;
262         case 156: return 200;
263         case 157: return 201;
264         case 158:  return  186; //devil:227
265         case 159:  return  190; //devil:246
266         case 160:  return  d1_pig_present ? 151 : 206;
267         case 161:  return  d1_pig_present ? 152 : 114; //devil:206
268         case 162: return 202;
269         case 163: return 203;
270         case 164: return 204;
271         case 165: return 205;
272         case 166: return 206;
273         case 167:  return  d1_pig_present ? 153 : 206;
274         case 168:  return  d1_pig_present ? 154 : 206;
275         case 169:  return  d1_pig_present ? 155 : 206;
276         case 170:  return  d1_pig_present ? 156 : 227;//206;
277         case 171:  return  d1_pig_present ? 157 : 206;//227;
278         case 172: return 207;
279         case 173: return 208;
280         case 174:  return  d1_pig_present ? 158 : 202;
281         case 175:  return  d1_pig_present ? 159 : 206;
282         // range handled by default case, returns 209..217
283         case 185:  return  d1_pig_present ? 160 : 217;
284         // range handled by default case, returns 218..224
285         case 193:  return  d1_pig_present ? 161 : 206;
286         case 194:  return  d1_pig_present ? 162 : 203;//206;
287         case 195:  return  d1_pig_present ? 166 : 234;
288         case 196: return 225;
289         case 197: return 226;
290         case 198:  return  d1_pig_present ? 167 : 225;
291         case 199:  return  d1_pig_present ? 168 : 206; //devil:204
292         case 200:  return  d1_pig_present ? 169 : 206; //devil:204
293         case 201: return 227;
294         case 202:  return  d1_pig_present ? 170 : 206; //devil:227
295         // range handled by default case, returns 228..234
296         case 210:  return  d1_pig_present ? 171 : 234; //devil:242
297         case 211:  return  d1_pig_present ? 172 : 206; //devil:240
298         // range handled by default case, returns 235..242
299         case 220:  return  d1_pig_present ? 173 : 242; //devil:240
300         case 221: return 243;
301         case 222: return 244;
302         case 223:  return  d1_pig_present ? 174 : 313;
303         case 224: return 245;
304         case 225: return 246;
305         case 226:  return  164;//247; matching names but not matching textures
306         case 227:  return  179; //devil:181
307         case 228:  return  196;//248; matching names but not matching textures
308         case 229:  return  d1_pig_present ? 175 : 15; //devil:66
309         case 230:  return  d1_pig_present ? 176 : 15; //devil:66
310         // range handled by default case, returns 249..257
311         case 240:  return  d1_pig_present ? 177 : 6; //devil:132
312         case 241:  return  130; //devil:131
313         case 242:  return  d1_pig_present ? 178 : 78; //devil:15
314         case 243:  return  d1_pig_present ? 180 : 33; //devil:38
315         case 244: return 258;
316         case 245: return 259;
317         case 246:  return  d1_pig_present ? 181 : 321;
318         case 247: return 260;
319         case 248: return 261;
320         case 249: return 262;
321         case 250:  return  340; // white entrance
322         case 251:  return  412; // red entrance
323         case 252:  return  410; // blue entrance
324         case 253:  return  411; // yellow entrance
325         case 254: return 263;
326         case 255: return 264;
327         case 256: return 265;
328         case 257:  return  d1_pig_present ? 182 : 249;//246; brig001
329         case 258:  return  d1_pig_present ? 183 : 251;//246; brig002
330         case 259:  return  d1_pig_present ? 184 : 252;//246; brig003
331         case 260:  return  d1_pig_present ? 185 : 256;//246; brig004
332         case 261: return 273;
333         case 262: return 274;
334         case 263:  return  d1_pig_present ? 187 : 281;
335         case 264: return 275;
336         case 265: return 276;
337         case 266:  return  d1_pig_present ? 188 : 279; //devil:291
338         // range handled by default case, returns 277..291
339         case 282: return 293;
340         case 283:  return  d1_pig_present ? 189 : 295;
341         case 284: return 295;
342         case 285: return 296;
343         case 286: return 298;
344         // range handled by default case, returns 300..310
345         case 298:  return  d1_pig_present ? 191 : 364; //devil:374
346         // range handled by default case, returns 311..326
347         case 315:  return  d1_pig_present ? 192 : 361; // broken producer
348         // range handled by default case,  returns  327..337
349         case 327: return 352;
350         case 328: return 353;
351         case 329: return 354;
352         case 330:  return  380;
353         case 331:  return  379;//373; matching names but not matching textures
354         case 332:  return  355;//344; matching names but not matching textures
355         case 333:  return  409; // lava  //devil:404
356         case 334: return 356;
357         case 335: return 357;
358         case 336: return 358;
359         case 337: return 359;
360         case 338: return 360;
361         case 339: return 361; // producer
362         case 340: return 362;
363         case 341: return 364;
364         case 342: return 363;
365         case 343: return 366;
366         case 344: return 365;
367         case 345: return 368;
368         case 346: return 376;
369         case 347: return 370;
370         case 348: return 367;
371         case 349:  return  372;
372         case 350: return 369;
373         case 351:  return  374;//429; matching names but not matching textures
374         case 352:  return  375;//387; matching names but not matching textures
375         case 353:  return  371;
376         case 354:  return  377;//425; matching names but not matching textures
377         case 355:  return  408;
378         case 356: return 378; // lava02
379         case 357:  return  383;//384; matching names but not matching textures
380         case 358:  return  384;//385; matching names but not matching textures
381         case 359:  return  385;//386; matching names but not matching textures
382         case 360: return 386;
383         case 361: return 387;
384         case 362:  return  d1_pig_present ? 194 : 388; // mntr04a (devil: -1)
385         case 363: return 388;
386         case 364: return 391;
387         case 365: return 392;
388         case 366: return 393;
389         case 367: return 394;
390         case 368: return 395;
391         case 369: return 396;
392         case 370:  return  d1_pig_present ? 195 : 392; // mntr04b (devil: -1)
393         // range 371..584 handled by default case (wall01 and door frames)
394         default:
395                 // ranges:
396                 if (d1_tmap_num >= 29 && d1_tmap_num <= 37)
397                         return d1_tmap_num - 16;
398                 if (d1_tmap_num >= 58 && d1_tmap_num <= 87)
399                         return d1_tmap_num - 24;
400                 if (d1_tmap_num >= 89 && d1_tmap_num <= 131)
401                         return d1_tmap_num - 25;
402                 if (d1_tmap_num >= 133 && d1_tmap_num <= 140)
403                         return d1_tmap_num - 26;
404                 if (d1_tmap_num >= 176 && d1_tmap_num <= 184)
405                         return d1_tmap_num + 33;
406                 if (d1_tmap_num >= 186 && d1_tmap_num <= 192)
407                         return d1_tmap_num + 32;
408                 if (d1_tmap_num >= 203 && d1_tmap_num <= 209)
409                         return d1_tmap_num + 25;
410                 if (d1_tmap_num >= 212 && d1_tmap_num <= 219)
411                         return d1_tmap_num + 23;
412                 if (d1_tmap_num >= 231 && d1_tmap_num <= 239)
413                         return d1_tmap_num + 18;
414                 if (d1_tmap_num >= 267 && d1_tmap_num <= 281)
415                         return d1_tmap_num + 10;
416                 if (d1_tmap_num >= 287 && d1_tmap_num <= 297)
417                         return d1_tmap_num + 13;
418                 if (d1_tmap_num >= 299 && d1_tmap_num <= 314)
419                         return d1_tmap_num + 12;
420                 if (d1_tmap_num >= 316 && d1_tmap_num <= 326)
421                          return  d1_tmap_num + 11; // matching names but not matching textures
422                 // wall01 and door frames:
423                 if (d1_tmap_num > 370 && d1_tmap_num < 584) {
424                         if (New_file_format_load) return d1_tmap_num + 64;
425                         // d1 shareware needs special treatment:
426                         if (d1_tmap_num < 410) return d1_tmap_num + 68;
427                         if (d1_tmap_num < 417) return d1_tmap_num + 73;
428                         if (d1_tmap_num < 446) return d1_tmap_num + 91;
429                         if (d1_tmap_num < 453) return d1_tmap_num + 104;
430                         if (d1_tmap_num < 462) return d1_tmap_num + 111;
431                         if (d1_tmap_num < 486) return d1_tmap_num + 117;
432                         if (d1_tmap_num < 494) return d1_tmap_num + 141;
433                         if (d1_tmap_num < 584) return d1_tmap_num + 147;
434                 }
435                 { // handle rare case where orientation != 0
436                         short tmap_num = d1_tmap_num &  TMAP_NUM_MASK;
437                         short orient = d1_tmap_num & ~TMAP_NUM_MASK;
438                         if (orient != 0) {
439                                 return orient | convert_d1_tmap_num(tmap_num);
440                         } else {
441                                 Warning("can't convert unknown descent 1 texture #%d.\n", tmap_num);
442                                 return d1_tmap_num;
443                         }
444                 }
445         }
446 }
447
448 #ifdef EDITOR
449
450 static char old_tmap_list[MAX_TEXTURES][FILENAME_LEN];
451 short tmap_xlate_table[MAX_TEXTURES];
452 static short tmap_times_used[MAX_TEXTURES];
453
454 // -----------------------------------------------------------------------------
455 //loads from an already-open file
456 // returns 0=everything ok, 1=old version, -1=error
457 int load_mine_data(CFILE *LoadFile)
458 {
459         int   i, j,oldsizeadjust;
460         short tmap_xlate;
461         int     translate;
462         char    *temptr;
463         int     mine_start = cftell(LoadFile);
464
465         d1_pig_present = cfexist(D1_PIGFILE);
466
467         oldsizeadjust=(sizeof(int)*2)+sizeof (vms_matrix);
468         fuelcen_reset();
469
470         for (i=0; i<MAX_TEXTURES; i++ )
471                 tmap_times_used[i] = 0;
472
473         #ifdef EDITOR
474         // Create a new mine to initialize things.
475         //texpage_goto_first();
476         create_new_mine();
477         #endif
478
479         //===================== READ FILE INFO ========================
480
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);  
511
512         mine_fileinfo.level_shake_frequency             =       0;
513         mine_fileinfo.level_shake_duration              =       0;
514
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);  
520
521                 mine_fileinfo.delta_light_offset                =       -1;
522                 mine_fileinfo.delta_light_howmany               =       0;
523                 mine_fileinfo.delta_light_sizeof                =       sizeof(delta_light);  
524
525 //      }
526
527         mine_fileinfo.segment2_offset           = -1;
528         mine_fileinfo.segment2_howmany  = 0;
529         mine_fileinfo.segment2_sizeof    = sizeof(segment2);
530
531         // Read in mine_top_fileinfo to get size of saved fileinfo.
532         
533         memset( &mine_top_fileinfo, 0, sizeof(mine_top_fileinfo) );
534
535         if (cfseek( LoadFile, mine_start, SEEK_SET ))
536                 Error( "Error moving to top of file in gamemine.c" );
537
538         if (cfread( &mine_top_fileinfo, sizeof(mine_top_fileinfo), 1, LoadFile )!=1)
539                 Error( "Error reading mine_top_fileinfo in gamemine.c" );
540
541         if (mine_top_fileinfo.fileinfo_signature != 0x2884)
542                 return -1;
543
544         // Check version number
545         if (mine_top_fileinfo.fileinfo_version < COMPATIBLE_VERSION )
546                 return -1;
547
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" );
551
552         if (cfread( &mine_fileinfo, mine_top_fileinfo.fileinfo_sizeof, 1, LoadFile )!=1)
553                 Error( "Error reading mine_fileinfo in gamemine.c" );
554
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;
561         } else {
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;
566         }
567
568         //===================== READ HEADER INFO ========================
569
570         // Set default values.
571         mine_header.num_vertices        =   0;
572         mine_header.num_segments        =   0;
573
574         if (mine_fileinfo.header_offset > -1 )
575         {
576                 if (cfseek( LoadFile, mine_fileinfo.header_offset, SEEK_SET ))
577                         Error( "Error seeking to header_offset in gamemine.c" );
578         
579                 if (cfread( &mine_header, mine_fileinfo.header_size, 1, LoadFile )!=1)
580                         Error( "Error reading mine_header in gamemine.c" );
581         }
582
583         //===================== READ EDITOR INFO ==========================
584
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;
592
593         if (mine_fileinfo.editor_offset > -1 )
594         {
595                 if (cfseek( LoadFile, mine_fileinfo.editor_offset, SEEK_SET ))
596                         Error( "Error seeking to editor_offset in gamemine.c" );
597         
598                 if (cfread( &mine_editor, mine_fileinfo.editor_size, 1, LoadFile )!=1)
599                         Error( "Error reading mine_editor in gamemine.c" );
600         }
601
602         //===================== READ TEXTURE INFO ==========================
603
604         if ( (mine_fileinfo.texture_offset > -1) && (mine_fileinfo.texture_howmany > 0))
605         {
606                 if (cfseek( LoadFile, mine_fileinfo.texture_offset, SEEK_SET ))
607                         Error( "Error seeking to texture_offset in gamemine.c" );
608
609                 for (i=0; i< mine_fileinfo.texture_howmany; i++ )
610                 {
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" );
613                 }
614         }
615
616         //=============== GENERATE TEXTURE TRANSLATION TABLE ===============
617
618         translate = 0;
619         
620         Assert (NumTextures < MAX_TEXTURES);
621
622         {
623                 hashtable ht;
624         
625                 hashtable_init( &ht, NumTextures );
626         
627                 // Remove all the file extensions in the textures list
628         
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 );
633                 }
634         
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';
641         
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] ));
646                                 ;
647                         }
648                         if (tmap_xlate_table[j] != j ) translate = 1;
649                         if (tmap_xlate_table[j] >= 0)
650                                 tmap_times_used[tmap_xlate_table[j]]++;
651                 }
652         
653                 {
654                         int count = 0;
655                         for (i=0; i<MAX_TEXTURES; i++ )
656                                 if (tmap_times_used[i])
657                                         count++;
658                         mprintf( (0, "This mine has %d unique textures in it (~%d KB)\n", count, (count*4096) /1024 ));
659                 }
660         
661                 // -- mprintf( (0, "Translate=%d\n", translate ));
662         
663                 hashtable_free( &ht );
664         }
665
666         //====================== READ VERTEX INFO ==========================
667
668         // New check added to make sure we don't read in too many vertices.
669         if ( mine_fileinfo.vertex_howmany > MAX_VERTICES )
670                 {
671                 mprintf((0, "Num vertices exceeds maximum.  Loading MAX %d vertices\n", MAX_VERTICES));
672                 mine_fileinfo.vertex_howmany = MAX_VERTICES;
673                 }
674
675         if ( (mine_fileinfo.vertex_offset > -1) && (mine_fileinfo.vertex_howmany > 0))
676         {
677                 if (cfseek( LoadFile, mine_fileinfo.vertex_offset, SEEK_SET ))
678                         Error( "Error seeking to vertex_offset in gamemine.c" );
679
680                 for (i=0; i< mine_fileinfo.vertex_howmany; i++ )
681                 {
682                         // Set the default values for this vertex
683                         Vertices[i].x = 1;
684                         Vertices[i].y = 1;
685                         Vertices[i].z = 1;
686
687                         if (cfread( &Vertices[i], mine_fileinfo.vertex_sizeof, 1, LoadFile )!=1)
688                                 Error( "Error reading Vertices[i] in gamemine.c" );
689                 }
690         }
691
692         //==================== READ SEGMENT INFO ===========================
693
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;
699         }
700
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();
702
703         if ( (mine_fileinfo.segment_offset > -1) && (mine_fileinfo.segment_howmany > 0))        {
704
705                 if (cfseek( LoadFile, mine_fileinfo.segment_offset,SEEK_SET ))
706
707                         Error( "Error seeking to segment_offset in gamemine.c" );
708
709                 Highest_segment_index = mine_fileinfo.segment_howmany-1;
710
711                 for (i=0; i< mine_fileinfo.segment_howmany; i++ ) {
712
713                         // Set the default values for this segment (clear to zero )
714                         //memset( &Segments[i], 0, sizeof(segment) );
715
716                         if (mine_top_fileinfo.fileinfo_version < 20) {
717                                 v16_segment v16_seg;
718
719                                 Assert(mine_fileinfo.segment_sizeof == sizeof(v16_seg));
720
721                                 if (cfread( &v16_seg, mine_fileinfo.segment_sizeof, 1, LoadFile )!=1)
722                                         Error( "Error reading segments in gamemine.c" );
723
724                                 #ifdef EDITOR
725                                 Segments[i].segnum = v16_seg.segnum;
726                                 // -- Segments[i].pad = v16_seg.pad;
727                                 #endif
728
729                                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
730                                         Segments[i].sides[j] = v16_seg.sides[j];
731
732                                 for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
733                                         Segments[i].children[j] = v16_seg.children[j];
734
735                                 for (j=0; j<MAX_VERTICES_PER_SEGMENT; j++)
736                                         Segments[i].verts[j] = v16_seg.verts[j];
737
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 );
744
745                         } else  {
746                                 if (cfread( &Segments[i], mine_fileinfo.segment_sizeof, 1, LoadFile )!=1)
747                                         Error("Unable to read segment %i\n", i);
748                         }
749
750                         Segments[i].objects = -1;
751                         #ifdef EDITOR
752                         Segments[i].group = -1;
753                         #endif
754
755                         if (mine_top_fileinfo.fileinfo_version < 15) {  //used old uvl ranges
756                                 int sn,uvln;
757
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;
763                                         }
764                         }
765
766                         if (translate == 1)
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));
774                                                         Int3();
775                                                         Segments[i].sides[j].tmap_num = NumTextures-1;
776                                                 }
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];
781
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));
785                                                                 Int3();
786                                                                 Segments[i].sides[j].tmap_num2 = NumTextures-1;
787                                                         }
788                                                 Segments[i].sides[j].tmap_num2 = xlated_tmap | orient;
789                                         }
790                                 }
791                 }
792
793
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 );
798                         }
799         }
800
801         //===================== READ NEWSEGMENT INFO =====================
802
803         #ifdef EDITOR
804
805         {               // Default segment created.
806                 vms_vector      sizevec;
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) );
809         }
810
811         if (mine_editor.newsegment_offset > -1)
812         {
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" );
817         }
818
819         if ( (mine_fileinfo.newseg_verts_offset > -1) && (mine_fileinfo.newseg_verts_howmany > 0))
820         {
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++ )
824                 {
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;
829                         
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" );
832
833                         New_segment.verts[i] = NEW_SEGMENT_VERTICES+i;
834                 }
835         }
836
837         #endif
838                                                                                                                         
839         //========================= UPDATE VARIABLES ======================
840
841         #ifdef EDITOR
842
843         // Setting to Markedsegp to NULL ignores Curside and Markedside, which
844         // we want to do when reading in an old file.
845         
846         Markedside = mine_editor.Markedside;
847         Curside = mine_editor.Curside;
848         for (i=0;i<10;i++)
849                 Groupside[i] = mine_editor.Groupside[i];
850
851         if ( mine_editor.current_seg != -1 )
852                 Cursegp = mine_editor.current_seg + Segments;
853         else
854                 Cursegp = NULL;
855
856         if (mine_editor.Markedsegp != -1 ) 
857                 Markedsegp = mine_editor.Markedsegp + Segments;
858         else
859                 Markedsegp = NULL;
860
861         num_groups = 0;
862         current_group = -1;
863
864         #endif
865
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;
870
871         reset_objects(1);               //one object, the player
872
873         #ifdef EDITOR
874         Highest_vertex_index = MAX_SEGMENT_VERTICES-1;
875         Highest_segment_index = MAX_SEGMENTS-1;
876         set_vertex_counts();
877         Highest_vertex_index = Num_vertices-1;
878         Highest_segment_index = Num_segments-1;
879
880         warn_if_concave_segments();
881         #endif
882
883         #ifdef EDITOR
884                 validate_segment_all();
885         #endif
886
887         //create_local_segment_data();
888
889         //gamemine_find_textures();
890
891         if (mine_top_fileinfo.fileinfo_version < MINE_VERSION )
892                 return 1;               //old version
893         else
894                 return 0;
895
896 }
897 #endif
898
899 #define COMPILED_MINE_VERSION 0
900
901 void read_children(int segnum,ubyte bit_mask,CFILE *LoadFile)
902 {
903         int bit;
904
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);
908                 } else
909                         Segments[segnum].children[bit] = -1;
910         }
911 }
912
913 void read_verts(int segnum,CFILE *LoadFile)
914 {
915         int i;
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);
919 }
920
921 void read_special(int segnum,ubyte bit_mask,CFILE *LoadFile)
922 {
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);
930         } else {
931                 Segment2s[segnum].special = 0;
932                 Segment2s[segnum].matcen_num = -1;
933                 Segment2s[segnum].value = 0;
934         }
935 }
936
937 int load_mine_data_compiled(CFILE *LoadFile)
938 {
939         int     i, segnum, sidenum;
940         ubyte   compiled_version;
941         short   temp_short;
942         ushort  temp_ushort = 0;
943         ubyte   bit_mask;
944
945         d1_pig_present = cfexist(D1_PIGFILE);
946
947         if (!strcmp(strchr(Gamesave_current_filename, '.'), ".sdl"))
948                 New_file_format_load = 0; // descent 1 shareware
949         else
950                 New_file_format_load = 1;
951
952         //      For compiled levels, textures map to themselves, prevent tmap_override always being gray,
953         //      bug which Matt and John refused to acknowledge, so here is Mike, fixing it.
954 #ifdef EDITOR
955         for (i=0; i<MAX_TEXTURES; i++)
956                 tmap_xlate_table[i] = i;
957 #endif
958
959 //      memset( Segments, 0, sizeof(segment)*MAX_SEGMENTS );
960         fuelcen_reset();
961
962         //=============================== Reading part ==============================
963         compiled_version = cfile_read_byte(LoadFile);
964         //Assert( compiled_version==COMPILED_MINE_VERSION );
965         if (compiled_version!=COMPILED_MINE_VERSION)
966                 mprintf((0,"compiled mine version=%i\n", compiled_version)); //many levels have "wrong" versions.  Theres no point in aborting because of it, I think.
967
968         if (New_file_format_load)
969                 Num_vertices = cfile_read_short(LoadFile);
970         else
971                 Num_vertices = cfile_read_int(LoadFile);
972         Assert( Num_vertices <= MAX_VERTICES );
973
974         if (New_file_format_load)
975                 Num_segments = cfile_read_short(LoadFile);
976         else
977                 Num_segments = cfile_read_int(LoadFile);
978         Assert( Num_segments <= MAX_SEGMENTS );
979
980         for (i = 0; i < Num_vertices; i++)
981                 cfile_read_vector( &(Vertices[i]), LoadFile);
982
983         for (segnum=0; segnum<Num_segments; segnum++ )  {
984
985                 #ifdef EDITOR
986                 Segments[segnum].segnum = segnum;
987                 Segments[segnum].group = 0;
988                 #endif
989
990                 if (New_file_format_load)
991                         bit_mask = cfile_read_byte(LoadFile);
992                 else
993                         bit_mask = 0x7f; // read all six children and special stuff...
994
995                 if (Gamesave_current_version == 5) { // d2 SHAREWARE level
996                         read_special(segnum,bit_mask,LoadFile);
997                         read_verts(segnum,LoadFile);
998                         read_children(segnum,bit_mask,LoadFile);
999                 } else {
1000                         read_children(segnum,bit_mask,LoadFile);
1001                         read_verts(segnum,LoadFile);
1002                         if (Gamesave_current_version <= 1) { // descent 1 level
1003                                 read_special(segnum,bit_mask,LoadFile);
1004                         }
1005                 }
1006
1007                 Segments[segnum].objects = -1;
1008
1009                 if (Gamesave_current_version <= 5) { // descent 1 thru d2 SHAREWARE level
1010                         // Read fix     Segments[segnum].static_light (shift down 5 bits, write as short)
1011                         temp_ushort = cfile_read_short(LoadFile);
1012                         Segment2s[segnum].static_light  = ((fix)temp_ushort) << 4;
1013                         //cfread( &Segments[segnum].static_light, sizeof(fix), 1, LoadFile );
1014                 }
1015
1016                 // Read the walls as a 6 byte array
1017                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++ )      {
1018                         Segments[segnum].sides[sidenum].pad = 0;
1019                 }
1020
1021                 if (New_file_format_load)
1022                         bit_mask = cfile_read_byte(LoadFile);
1023                 else
1024                         bit_mask = 0x3f; // read all six sides
1025                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
1026                         ubyte byte_wallnum;
1027
1028                         if (bit_mask & (1 << sidenum)) {
1029                                 byte_wallnum = cfile_read_byte(LoadFile);
1030                                 if ( byte_wallnum == 255 )
1031                                         Segments[segnum].sides[sidenum].wall_num = -1;
1032                                 else
1033                                         Segments[segnum].sides[sidenum].wall_num = byte_wallnum;
1034                         } else
1035                                         Segments[segnum].sides[sidenum].wall_num = -1;
1036                 }
1037
1038                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++ )      {
1039
1040                         if ( (Segments[segnum].children[sidenum]==-1) || (Segments[segnum].sides[sidenum].wall_num!=-1) )       {
1041                                 // Read short Segments[segnum].sides[sidenum].tmap_num;
1042                                 if (New_file_format_load) {
1043                                         temp_ushort = cfile_read_short(LoadFile);
1044                                         Segments[segnum].sides[sidenum].tmap_num = temp_ushort & 0x7fff;
1045                                 } else
1046                                         Segments[segnum].sides[sidenum].tmap_num = cfile_read_short(LoadFile);
1047
1048                                 if (Gamesave_current_version <= 1)
1049                                         Segments[segnum].sides[sidenum].tmap_num = convert_d1_tmap_num(Segments[segnum].sides[sidenum].tmap_num);
1050
1051                                 if (New_file_format_load && !(temp_ushort & 0x8000))
1052                                         Segments[segnum].sides[sidenum].tmap_num2 = 0;
1053                                 else {
1054                                         // Read short Segments[segnum].sides[sidenum].tmap_num2;
1055                                         Segments[segnum].sides[sidenum].tmap_num2 = cfile_read_short(LoadFile);
1056                                         if (Gamesave_current_version <= 1 && Segments[segnum].sides[sidenum].tmap_num2 != 0)
1057                                                 Segments[segnum].sides[sidenum].tmap_num2 = convert_d1_tmap_num(Segments[segnum].sides[sidenum].tmap_num2);
1058                                 }
1059
1060                                 // Read uvl Segments[segnum].sides[sidenum].uvls[4] (u,v>>5, write as short, l>>1 write as short)
1061                                 for (i=0; i<4; i++ )    {
1062                                         temp_short = cfile_read_short(LoadFile);
1063                                         Segments[segnum].sides[sidenum].uvls[i].u = ((fix)temp_short) << 5;
1064                                         temp_short = cfile_read_short(LoadFile);
1065                                         Segments[segnum].sides[sidenum].uvls[i].v = ((fix)temp_short) << 5;
1066                                         temp_ushort = cfile_read_short(LoadFile);
1067                                         Segments[segnum].sides[sidenum].uvls[i].l = ((fix)temp_ushort) << 1;
1068                                         //cfread( &Segments[segnum].sides[sidenum].uvls[i].l, sizeof(fix), 1, LoadFile );
1069                                 }
1070                         } else {
1071                                 Segments[segnum].sides[sidenum].tmap_num = 0;
1072                                 Segments[segnum].sides[sidenum].tmap_num2 = 0;
1073                                 for (i=0; i<4; i++ )    {
1074                                         Segments[segnum].sides[sidenum].uvls[i].u = 0;
1075                                         Segments[segnum].sides[sidenum].uvls[i].v = 0;
1076                                         Segments[segnum].sides[sidenum].uvls[i].l = 0;
1077                                 }
1078                         }
1079                 }
1080         }
1081
1082 #if 0
1083         {
1084                 FILE *fp;
1085
1086                 fp = fopen("segments.out", "wt");
1087                 for (i = 0; i <= Highest_segment_index; i++) {
1088                         side    sides[MAX_SIDES_PER_SEGMENT];   // 6 sides
1089                         short   children[MAX_SIDES_PER_SEGMENT];        // indices of 6 children segments, front, left, top, right, bottom, back
1090                         short   verts[MAX_VERTICES_PER_SEGMENT];        // vertex ids of 4 front and 4 back vertices
1091                         int             objects;                                                                // pointer to objects in this segment
1092
1093                         for (j = 0; j < MAX_SIDES_PER_SEGMENT; j++) {
1094                                 sbyte   type;                           // replaces num_faces and tri_edge, 1 = quad, 2 = 0:2 triangulation, 3 = 1:3 triangulation
1095                                 ubyte   pad;                                                                    //keep us longword alligned
1096                                 short   wall_num;
1097                                 short   tmap_num;
1098                                 short   tmap_num2;
1099                                 uvl             uvls[4];
1100                                 vms_vector      normals[2];                                             // 2 normals, if quadrilateral, both the same.
1101                                 fprintf(fp, "%d\n", Segments[i].sides[j].type);
1102                                 fprintf(fp, "%d\n", Segments[i].sides[j].pad);
1103                                 fprintf(fp, "%d\n", Segments[i].sides[j].wall_num);
1104                                 fprintf(fp, "%d\n", Segments[i].tmap_num);
1105
1106                         }
1107                         fclose(fp);
1108                 }
1109         }
1110 #endif
1111
1112         Highest_vertex_index = Num_vertices-1;
1113         Highest_segment_index = Num_segments-1;
1114
1115         validate_segment_all();                 // Fill in side type and normals.
1116
1117         for (i=0; i<Num_segments; i++) {
1118                 if (Gamesave_current_version > 5)
1119                         segment2_read(&Segment2s[i], LoadFile);
1120                 fuelcen_activate( &Segments[i], Segment2s[i].special );
1121         }
1122
1123         reset_objects(1);               //one object, the player
1124
1125         return 0;
1126 }