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