]> icculus.org git repositories - btb/d2x.git/blob - main/gamesave.c
changed path for SDL headers
[btb/d2x.git] / main / gamesave.c
1 /* $Id $ */
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  * Save game information
18  *
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #ifdef RCS
27 char gamesave_rcsid[] = "$Id: gamesave.c,v 1.17 2002-09-14 00:20:44 btb Exp $";
28 #endif
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "pstypes.h"
34 #include "strutil.h"
35 #include "mono.h"
36 #include "key.h"
37 #include "gr.h"
38 #include "palette.h"
39 #include "newmenu.h"
40
41 #include "inferno.h"
42 #ifdef EDITOR
43 #include "editor/editor.h"
44 #endif
45 #include "error.h"
46 #include "object.h"
47 #include "game.h"
48 #include "screens.h"
49 #include "wall.h"
50 #include "gamemine.h"
51 #include "robot.h"
52
53
54 #include "cfile.h"
55 #include "bm.h"
56 #include "menu.h"
57 #include "switch.h"
58 #include "fuelcen.h"
59 #include "cntrlcen.h"
60 #include "powerup.h"
61 #include "weapon.h"
62 #include "newdemo.h"
63 #include "gameseq.h"
64 #include "automap.h"
65 #include "polyobj.h"
66 #include "text.h"
67 #include "gamefont.h"
68 #include "gamesave.h"
69 #include "gamepal.h"
70 #include "laser.h"
71 #include "byteswap.h"
72 #include "multi.h"
73 #include "makesig.h"
74 #include "hoard.h"
75
76 char Gamesave_current_filename[128];
77
78 int Gamesave_current_version;
79
80 #define GAME_VERSION            32
81 #define GAME_COMPATIBLE_VERSION 22
82
83 //version 28->29  add delta light support
84 //version 27->28  controlcen id now is reactor number, not model number
85 //version 28->29  ??
86 //version 29->30  changed trigger structure
87 //version 30->31  changed trigger structure some more
88 //version 31->32  change segment structure, make it 512 bytes w/o editor, add Segment2s array.
89
90 #define MENU_CURSOR_X_MIN       MENU_X
91 #define MENU_CURSOR_X_MAX       MENU_X+6
92
93 struct {
94         ushort  fileinfo_signature;
95         ushort  fileinfo_version;
96         int     fileinfo_sizeof;
97 } game_top_fileinfo;    // Should be same as first two fields below...
98
99 struct {
100         ushort  fileinfo_signature;
101         ushort  fileinfo_version;
102         int     fileinfo_sizeof;
103         char    mine_filename[15];
104         int     level;
105         int     player_offset;              // Player info
106         int     player_sizeof;
107         int     object_offset;              // Object info
108         int     object_howmany;
109         int     object_sizeof;
110         int     walls_offset;
111         int     walls_howmany;
112         int     walls_sizeof;
113         int     doors_offset;
114         int     doors_howmany;
115         int     doors_sizeof;
116         int     triggers_offset;
117         int     triggers_howmany;
118         int     triggers_sizeof;
119         int     links_offset;
120         int     links_howmany;
121         int     links_sizeof;
122         int     control_offset;
123         int     control_howmany;
124         int     control_sizeof;
125         int     matcen_offset;
126         int     matcen_howmany;
127         int     matcen_sizeof;
128         int     dl_indices_offset;
129         int     dl_indices_howmany;
130         int     dl_indices_sizeof;
131         int     delta_light_offset;
132         int     delta_light_howmany;
133         int     delta_light_sizeof;
134 } game_fileinfo;
135
136 //  LINT: adding function prototypes
137 void read_object(object *obj, CFILE *f, int version);
138 void write_object(object *obj, FILE *f);
139 void do_load_save_levels(int save);
140 void dump_mine_info(void);
141
142 extern char MaxPowerupsAllowed[MAX_POWERUP_TYPES];
143 extern char PowerupsInMine[MAX_POWERUP_TYPES];
144
145 #ifdef EDITOR
146 extern char mine_filename[];
147 extern int save_mine_data_compiled(FILE * SaveFile);
148 //--unused-- #else
149 //--unused-- char mine_filename[128];
150 #endif
151
152 int Gamesave_num_org_robots = 0;
153 //--unused-- grs_bitmap * Gamesave_saved_bitmap = NULL;
154
155 #ifdef EDITOR
156 // Return true if this level has a name of the form "level??"
157 // Note that a pathspec can appear at the beginning of the filename.
158 int is_real_level(char *filename)
159 {
160         int len = strlen(filename);
161
162         if (len < 6)
163                 return 0;
164
165         //mprintf((0, "String = [%s]\n", &filename[len-11]));
166         return !strnicmp(&filename[len-11], "level", 5);
167
168 }
169 #endif
170
171 void change_filename_extension( char *dest, char *src, char *new_ext )
172 {
173         int i;
174
175         strcpy (dest, src);
176
177         if (new_ext[0]=='.')
178                 new_ext++;
179
180         for (i=1; i<strlen(dest); i++ )
181                 if (dest[i]=='.'||dest[i]==' '||dest[i]==0)
182                         break;
183
184         if (i < 123) {
185                 dest[i]='.';
186                 dest[i+1]=new_ext[0];
187                 dest[i+2]=new_ext[1];
188                 dest[i+3]=new_ext[2];
189                 dest[i+4]=0;
190                 return;
191         }
192 }
193
194 //--unused-- vms_angvec zero_angles={0,0,0};
195
196 #define vm_angvec_zero(v) do {(v)->p=(v)->b=(v)->h=0;} while (0)
197
198 int Gamesave_num_players=0;
199
200 int N_save_pof_names;
201 char Save_pof_names[MAX_POLYGON_MODELS][FILENAME_LEN];
202
203 void check_and_fix_matrix(vms_matrix *m);
204
205 void verify_object( object * obj )      {
206
207         obj->lifeleft = IMMORTAL_TIME;          //all loaded object are immortal, for now
208
209         if ( obj->type == OBJ_ROBOT )   {
210                 Gamesave_num_org_robots++;
211
212                 // Make sure valid id...
213                 if ( obj->id >= N_robot_types )
214                         obj->id = obj->id % N_robot_types;
215
216                 // Make sure model number & size are correct...
217                 if ( obj->render_type == RT_POLYOBJ ) {
218                         Assert(Robot_info[obj->id].model_num != -1);
219                                 //if you fail this assert, it means that a robot in this level
220                                 //hasn't been loaded, possibly because he's marked as
221                                 //non-shareware.  To see what robot number, print obj->id.
222
223                         Assert(Robot_info[obj->id].always_0xabcd == 0xabcd);
224                                 //if you fail this assert, it means that the robot_ai for
225                                 //a robot in this level hasn't been loaded, possibly because
226                                 //it's marked as non-shareware.  To see what robot number,
227                                 //print obj->id.
228
229                         obj->rtype.pobj_info.model_num = Robot_info[obj->id].model_num;
230                         obj->size = Polygon_models[obj->rtype.pobj_info.model_num].rad;
231
232                         //@@Took out this ugly hack 1/12/96, because Mike has added code
233                         //@@that should fix it in a better way.
234                         //@@//this is a super-ugly hack.  Since the baby stripe robots have
235                         //@@//their firing point on their bounding sphere, the firing points
236                         //@@//can poke through a wall if the robots are very close to it. So
237                         //@@//we make their radii bigger so the guns can't get too close to 
238                         //@@//the walls
239                         //@@if (Robot_info[obj->id].flags & RIF_BIG_RADIUS)
240                         //@@    obj->size = (obj->size*3)/2;
241
242                         //@@if (obj->control_type==CT_AI && Robot_info[obj->id].attack_type)
243                         //@@    obj->size = obj->size*3/4;
244                 }
245
246                 if (obj->id == 65)                                              //special "reactor" robots
247                         obj->movement_type = MT_NONE;
248
249                 if (obj->movement_type == MT_PHYSICS) {
250                         obj->mtype.phys_info.mass = Robot_info[obj->id].mass;
251                         obj->mtype.phys_info.drag = Robot_info[obj->id].drag;
252                 }
253         }
254         else {          //Robots taken care of above
255
256                 if ( obj->render_type == RT_POLYOBJ ) {
257                         int i;
258                         char *name = Save_pof_names[obj->rtype.pobj_info.model_num];
259
260                         for (i=0;i<N_polygon_models;i++)
261                                 if (!stricmp(Pof_names[i],name)) {              //found it!     
262                                         // mprintf((0,"Mapping <%s> to %d (was %d)\n",name,i,obj->rtype.pobj_info.model_num));
263                                         obj->rtype.pobj_info.model_num = i;
264                                         break;
265                                 }
266                 }
267         }
268
269         if ( obj->type == OBJ_POWERUP ) {
270                 if ( obj->id >= N_powerup_types )       {
271                         obj->id = 0;
272                         Assert( obj->render_type != RT_POLYOBJ );
273                 }
274                 obj->control_type = CT_POWERUP;
275                 obj->size = Powerup_info[obj->id].size;
276                 obj->ctype.powerup_info.creation_time = 0;
277
278 #ifdef NETWORK
279                 if (Game_mode & GM_NETWORK)
280                         {
281                           if (multi_powerup_is_4pack(obj->id))
282                                 {
283                                  PowerupsInMine[obj->id-1]+=4;
284                                  MaxPowerupsAllowed[obj->id-1]+=4;
285                                 }
286                           PowerupsInMine[obj->id]++;
287                      MaxPowerupsAllowed[obj->id]++;
288                           mprintf ((0,"PowerupLimiter: ID=%d\n",obj->id));
289                           if (obj->id>MAX_POWERUP_TYPES)
290                                 mprintf ((1,"POWERUP: Overwriting array bounds!! Get JL!\n"));
291                         }
292 #endif
293
294         }
295
296         if ( obj->type == OBJ_WEAPON )  {
297                 if ( obj->id >= N_weapon_types )        {
298                         obj->id = 0;
299                         Assert( obj->render_type != RT_POLYOBJ );
300                 }
301
302                 if (obj->id == PMINE_ID) {              //make sure pmines have correct values
303
304                         obj->mtype.phys_info.mass = Weapon_info[obj->id].mass;
305                         obj->mtype.phys_info.drag = Weapon_info[obj->id].drag;
306                         obj->mtype.phys_info.flags |= PF_FREE_SPINNING;
307
308                         // Make sure model number & size are correct...         
309                         Assert( obj->render_type == RT_POLYOBJ );
310
311                         obj->rtype.pobj_info.model_num = Weapon_info[obj->id].model_num;
312                         obj->size = Polygon_models[obj->rtype.pobj_info.model_num].rad;
313                 }
314         }
315
316         if ( obj->type == OBJ_CNTRLCEN ) {
317
318                 obj->render_type = RT_POLYOBJ;
319                 obj->control_type = CT_CNTRLCEN;
320
321                 if (Gamesave_current_version <= 1) { // descent 1 reactor
322                         obj->id = 0;                         // used to be only one kind of reactor
323                         obj->rtype.pobj_info.model_num = Reactors[0].model_num;// descent 1 reactor
324                 }
325                 //@@// Make model number is correct...
326                 //@@for (i=0; i<Num_total_object_types; i++ )
327                 //@@    if ( ObjType[i] == OL_CONTROL_CENTER ) {
328                 //@@            obj->rtype.pobj_info.model_num = ObjId[i];
329                 //@@            obj->shields = ObjStrength[i];
330                 //@@            break;
331                 //@@    }
332
333                 #ifdef EDITOR
334                 {
335                 int i;
336                 // Check, and set, strength of reactor
337                 for (i=0; i<Num_total_object_types; i++ )       
338                         if ( ObjType[i]==OL_CONTROL_CENTER && ObjId[i] == obj->id ) {
339                                 obj->shields = ObjStrength[i];
340                                 break;          
341                         }
342                 Assert(i < Num_total_object_types);             //make sure we found it
343                 }
344                 #endif
345         }
346
347         if ( obj->type == OBJ_PLAYER )  {
348                 //int i;
349
350                 //Assert(obj == Player);
351
352                 if ( obj == ConsoleObject )             
353                         init_player_object();
354                 else
355                         if (obj->render_type == RT_POLYOBJ)     //recover from Matt's pof file matchup bug
356                                 obj->rtype.pobj_info.model_num = Player_ship->model_num;
357
358                 //Make sure orient matrix is orthogonal
359                 check_and_fix_matrix(&obj->orient);
360
361                 obj->id = Gamesave_num_players++;
362         }
363
364         if (obj->type == OBJ_HOSTAGE) {
365
366                 //@@if (obj->id > N_hostage_types)
367                 //@@    obj->id = 0;
368
369                 obj->render_type = RT_HOSTAGE;
370                 obj->control_type = CT_POWERUP;
371         }
372
373 }
374
375 //static gs_skip(int len,CFILE *file)
376 //{
377 //
378 //      cfseek(file,len,SEEK_CUR);
379 //}
380
381 #ifdef EDITOR
382 static void gs_write_int(int i,FILE *file)
383 {
384         if (fwrite( &i, sizeof(i), 1, file) != 1)
385                 Error( "Error reading int in gamesave.c" );
386
387 }
388
389 static void gs_write_fix(fix f,FILE *file)
390 {
391         if (fwrite( &f, sizeof(f), 1, file) != 1)
392                 Error( "Error reading fix in gamesave.c" );
393
394 }
395
396 static void gs_write_short(short s,FILE *file)
397 {
398         if (fwrite( &s, sizeof(s), 1, file) != 1)
399                 Error( "Error reading short in gamesave.c" );
400
401 }
402
403 static void gs_write_fixang(fixang f,FILE *file)
404 {
405         if (fwrite( &f, sizeof(f), 1, file) != 1)
406                 Error( "Error reading fixang in gamesave.c" );
407
408 }
409
410 static void gs_write_byte(byte b,FILE *file)
411 {
412         if (fwrite( &b, sizeof(b), 1, file) != 1)
413                 Error( "Error reading byte in gamesave.c" );
414
415 }
416
417 static void gr_write_vector(vms_vector *v,FILE *file)
418 {
419         gs_write_fix(v->x,file);
420         gs_write_fix(v->y,file);
421         gs_write_fix(v->z,file);
422 }
423
424 static void gs_write_matrix(vms_matrix *m,FILE *file)
425 {
426         gr_write_vector(&m->rvec,file);
427         gr_write_vector(&m->uvec,file);
428         gr_write_vector(&m->fvec,file);
429 }
430
431 static void gs_write_angvec(vms_angvec *v,FILE *file)
432 {
433         gs_write_fixang(v->p,file);
434         gs_write_fixang(v->b,file);
435         gs_write_fixang(v->h,file);
436 }
437
438 #endif
439
440
441 extern int multi_powerup_is_4pack(int);
442 //reads one object of the given version from the given file
443 void read_object(object *obj,CFILE *f,int version)
444 {
445
446         obj->type           = cfile_read_byte(f);
447         obj->id             = cfile_read_byte(f);
448
449         obj->control_type   = cfile_read_byte(f);
450         obj->movement_type  = cfile_read_byte(f);
451         obj->render_type    = cfile_read_byte(f);
452         obj->flags          = cfile_read_byte(f);
453
454         obj->segnum         = cfile_read_short(f);
455         obj->attached_obj   = -1;
456
457         cfile_read_vector(&obj->pos,f);
458         cfile_read_matrix(&obj->orient,f);
459
460         obj->size           = cfile_read_fix(f);
461         obj->shields        = cfile_read_fix(f);
462
463         cfile_read_vector(&obj->last_pos,f);
464
465         obj->contains_type  = cfile_read_byte(f);
466         obj->contains_id    = cfile_read_byte(f);
467         obj->contains_count = cfile_read_byte(f);
468
469         switch (obj->movement_type) {
470
471                 case MT_PHYSICS:
472
473                         cfile_read_vector(&obj->mtype.phys_info.velocity,f);
474                         cfile_read_vector(&obj->mtype.phys_info.thrust,f);
475
476                         obj->mtype.phys_info.mass               = cfile_read_fix(f);
477                         obj->mtype.phys_info.drag               = cfile_read_fix(f);
478                         obj->mtype.phys_info.brakes     = cfile_read_fix(f);
479
480                         cfile_read_vector(&obj->mtype.phys_info.rotvel,f);
481                         cfile_read_vector(&obj->mtype.phys_info.rotthrust,f);
482
483                         obj->mtype.phys_info.turnroll   = cfile_read_fixang(f);
484                         obj->mtype.phys_info.flags              = cfile_read_short(f);
485
486                         break;
487
488                 case MT_SPINNING:
489
490                         cfile_read_vector(&obj->mtype.spin_rate,f);
491                         break;
492
493                 case MT_NONE:
494                         break;
495
496                 default:
497                         Int3();
498         }
499
500         switch (obj->control_type) {
501
502                 case CT_AI: {
503                         int i;
504
505                         obj->ctype.ai_info.behavior                             = cfile_read_byte(f);
506
507                         for (i=0;i<MAX_AI_FLAGS;i++)
508                                 obj->ctype.ai_info.flags[i]                     = cfile_read_byte(f);
509
510                         obj->ctype.ai_info.hide_segment                 = cfile_read_short(f);
511                         obj->ctype.ai_info.hide_index                   = cfile_read_short(f);
512                         obj->ctype.ai_info.path_length                  = cfile_read_short(f);
513                         obj->ctype.ai_info.cur_path_index               = cfile_read_short(f);
514
515                         if (version <= 25) {
516                                 cfile_read_short(f);    //                              obj->ctype.ai_info.follow_path_start_seg        = 
517                                 cfile_read_short(f);    //                              obj->ctype.ai_info.follow_path_end_seg          = 
518                         }
519
520                         break;
521                 }
522
523                 case CT_EXPLOSION:
524
525                         obj->ctype.expl_info.spawn_time         = cfile_read_fix(f);
526                         obj->ctype.expl_info.delete_time                = cfile_read_fix(f);
527                         obj->ctype.expl_info.delete_objnum      = cfile_read_short(f);
528                         obj->ctype.expl_info.next_attach = obj->ctype.expl_info.prev_attach = obj->ctype.expl_info.attach_parent = -1;
529
530                         break;
531
532                 case CT_WEAPON:
533
534                         //do I really need to read these?  Are they even saved to disk?
535
536                         obj->ctype.laser_info.parent_type               = cfile_read_short(f);
537                         obj->ctype.laser_info.parent_num                = cfile_read_short(f);
538                         obj->ctype.laser_info.parent_signature  = cfile_read_int(f);
539
540                         break;
541
542                 case CT_LIGHT:
543
544                         obj->ctype.light_info.intensity = cfile_read_fix(f);
545                         break;
546
547                 case CT_POWERUP:
548
549                         if (version >= 25)
550                                 obj->ctype.powerup_info.count = cfile_read_int(f);
551                         else
552                                 obj->ctype.powerup_info.count = 1;
553
554                         if (obj->id == POW_VULCAN_WEAPON)
555                                         obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
556
557                         if (obj->id == POW_GAUSS_WEAPON)
558                                         obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
559
560                         if (obj->id == POW_OMEGA_WEAPON)
561                                         obj->ctype.powerup_info.count = MAX_OMEGA_CHARGE;
562
563                         break;
564
565
566                 case CT_NONE:
567                 case CT_FLYING:
568                 case CT_DEBRIS:
569                         break;
570
571                 case CT_SLEW:           //the player is generally saved as slew
572                         break;
573
574                 case CT_CNTRLCEN:
575                         break;
576
577                 case CT_MORPH:
578                 case CT_FLYTHROUGH:
579                 case CT_REPAIRCEN:
580                 default:
581                         Int3();
582         
583         }
584
585         switch (obj->render_type) {
586
587                 case RT_NONE:
588                         break;
589
590                 case RT_MORPH:
591                 case RT_POLYOBJ: {
592                         int i,tmo;
593
594                         obj->rtype.pobj_info.model_num          = cfile_read_int(f);
595
596                         for (i=0;i<MAX_SUBMODELS;i++)
597                                 cfile_read_angvec(&obj->rtype.pobj_info.anim_angles[i],f);
598
599                         obj->rtype.pobj_info.subobj_flags       = cfile_read_int(f);
600
601                         tmo = cfile_read_int(f);
602
603                         #ifndef EDITOR
604                         obj->rtype.pobj_info.tmap_override      = tmo;
605                         #else
606                         if (tmo==-1)
607                                 obj->rtype.pobj_info.tmap_override      = -1;
608                         else {
609                                 int xlated_tmo = tmap_xlate_table[tmo];
610                                 if (xlated_tmo < 0)     {
611                                         mprintf( (0, "Couldn't find texture for demo object, model_num = %d\n", obj->rtype.pobj_info.model_num));
612                                         Int3();
613                                         xlated_tmo = 0;
614                                 }
615                                 obj->rtype.pobj_info.tmap_override      = xlated_tmo;
616                         }
617                         #endif
618
619                         obj->rtype.pobj_info.alt_textures       = 0;
620
621                         break;
622                 }
623
624                 case RT_WEAPON_VCLIP:
625                 case RT_HOSTAGE:
626                 case RT_POWERUP:
627                 case RT_FIREBALL:
628
629                         obj->rtype.vclip_info.vclip_num = cfile_read_int(f);
630                         obj->rtype.vclip_info.frametime = cfile_read_fix(f);
631                         obj->rtype.vclip_info.framenum  = cfile_read_byte(f);
632
633                         break;
634
635                 case RT_LASER:
636                         break;
637
638                 default:
639                         Int3();
640
641         }
642
643 }
644
645 #ifdef EDITOR
646
647 //writes one object to the given file
648 void write_object(object *obj,FILE *f)
649 {
650         gs_write_byte(obj->type,f);
651         gs_write_byte(obj->id,f);
652
653         gs_write_byte(obj->control_type,f);
654         gs_write_byte(obj->movement_type,f);
655         gs_write_byte(obj->render_type,f);
656         gs_write_byte(obj->flags,f);
657
658         gs_write_short(obj->segnum,f);
659
660         gr_write_vector(&obj->pos,f);
661         gs_write_matrix(&obj->orient,f);
662
663         gs_write_fix(obj->size,f);
664         gs_write_fix(obj->shields,f);
665
666         gr_write_vector(&obj->last_pos,f);
667
668         gs_write_byte(obj->contains_type,f);
669         gs_write_byte(obj->contains_id,f);
670         gs_write_byte(obj->contains_count,f);
671
672         switch (obj->movement_type) {
673
674                 case MT_PHYSICS:
675
676                         gr_write_vector(&obj->mtype.phys_info.velocity,f);
677                         gr_write_vector(&obj->mtype.phys_info.thrust,f);
678
679                         gs_write_fix(obj->mtype.phys_info.mass,f);
680                         gs_write_fix(obj->mtype.phys_info.drag,f);
681                         gs_write_fix(obj->mtype.phys_info.brakes,f);
682
683                         gr_write_vector(&obj->mtype.phys_info.rotvel,f);
684                         gr_write_vector(&obj->mtype.phys_info.rotthrust,f);
685
686                         gs_write_fixang(obj->mtype.phys_info.turnroll,f);
687                         gs_write_short(obj->mtype.phys_info.flags,f);
688
689                         break;
690
691                 case MT_SPINNING:
692
693                         gr_write_vector(&obj->mtype.spin_rate,f);
694                         break;
695
696                 case MT_NONE:
697                         break;
698
699                 default:
700                         Int3();
701         }
702
703         switch (obj->control_type) {
704
705                 case CT_AI: {
706                         int i;
707
708                         gs_write_byte(obj->ctype.ai_info.behavior,f);
709
710                         for (i=0;i<MAX_AI_FLAGS;i++)
711                                 gs_write_byte(obj->ctype.ai_info.flags[i],f);
712
713                         gs_write_short(obj->ctype.ai_info.hide_segment,f);
714                         gs_write_short(obj->ctype.ai_info.hide_index,f);
715                         gs_write_short(obj->ctype.ai_info.path_length,f);
716                         gs_write_short(obj->ctype.ai_info.cur_path_index,f);
717
718                         // -- unused! mk, 08/13/95 -- gs_write_short(obj->ctype.ai_info.follow_path_start_seg,f);
719                         // -- unused! mk, 08/13/95 -- gs_write_short(obj->ctype.ai_info.follow_path_end_seg,f);
720
721                         break;
722                 }
723
724                 case CT_EXPLOSION:
725
726                         gs_write_fix(obj->ctype.expl_info.spawn_time,f);
727                         gs_write_fix(obj->ctype.expl_info.delete_time,f);
728                         gs_write_short(obj->ctype.expl_info.delete_objnum,f);
729
730                         break;
731
732                 case CT_WEAPON:
733
734                         //do I really need to write these objects?
735
736                         gs_write_short(obj->ctype.laser_info.parent_type,f);
737                         gs_write_short(obj->ctype.laser_info.parent_num,f);
738                         gs_write_int(obj->ctype.laser_info.parent_signature,f);
739
740                         break;
741
742                 case CT_LIGHT:
743
744                         gs_write_fix(obj->ctype.light_info.intensity,f);
745                         break;
746
747                 case CT_POWERUP:
748
749                         gs_write_int(obj->ctype.powerup_info.count,f);
750                         break;
751
752                 case CT_NONE:
753                 case CT_FLYING:
754                 case CT_DEBRIS:
755                         break;
756
757                 case CT_SLEW:           //the player is generally saved as slew
758                         break;
759
760                 case CT_CNTRLCEN:
761                         break;                  //control center object.
762
763                 case CT_MORPH:
764                 case CT_REPAIRCEN:
765                 case CT_FLYTHROUGH:
766                 default:
767                         Int3();
768         
769         }
770
771         switch (obj->render_type) {
772
773                 case RT_NONE:
774                         break;
775
776                 case RT_MORPH:
777                 case RT_POLYOBJ: {
778                         int i;
779
780                         gs_write_int(obj->rtype.pobj_info.model_num,f);
781
782                         for (i=0;i<MAX_SUBMODELS;i++)
783                                 gs_write_angvec(&obj->rtype.pobj_info.anim_angles[i],f);
784
785                         gs_write_int(obj->rtype.pobj_info.subobj_flags,f);
786
787                         gs_write_int(obj->rtype.pobj_info.tmap_override,f);
788
789                         break;
790                 }
791
792                 case RT_WEAPON_VCLIP:
793                 case RT_HOSTAGE:
794                 case RT_POWERUP:
795                 case RT_FIREBALL:
796
797                         gs_write_int(obj->rtype.vclip_info.vclip_num,f);
798                         gs_write_fix(obj->rtype.vclip_info.frametime,f);
799                         gs_write_byte(obj->rtype.vclip_info.framenum,f);
800
801                         break;
802
803                 case RT_LASER:
804                         break;
805
806                 default:
807                         Int3();
808
809         }
810
811 }
812 #endif
813
814 extern int remove_trigger_num(int trigger_num);
815
816 // -----------------------------------------------------------------------------
817 // Load game 
818 // Loads all the relevant data for a level.
819 // If level != -1, it loads the filename with extension changed to .min
820 // Otherwise it loads the appropriate level mine.
821 // returns 0=everything ok, 1=old version, -1=error
822 int load_game_data(CFILE *LoadFile)
823 {
824         int i,j;
825         int start_offset;
826
827         start_offset = cftell(LoadFile);
828
829         //===================== READ FILE INFO ========================
830
831         // Set default values
832         game_fileinfo.level                                     =       -1;
833         game_fileinfo.player_offset             =       -1;
834         game_fileinfo.player_sizeof             =       sizeof(player);
835         game_fileinfo.object_offset             =       -1;
836         game_fileinfo.object_howmany            =       0;
837         game_fileinfo.object_sizeof             =       sizeof(object);  
838         game_fileinfo.walls_offset                      =       -1;
839         game_fileinfo.walls_howmany             =       0;
840         game_fileinfo.walls_sizeof                      =       sizeof(wall);  
841         game_fileinfo.doors_offset                      =       -1;
842         game_fileinfo.doors_howmany             =       0;
843         game_fileinfo.doors_sizeof                      =       sizeof(active_door);  
844         game_fileinfo.triggers_offset           =       -1;
845         game_fileinfo.triggers_howmany  =       0;
846         game_fileinfo.triggers_sizeof           =       sizeof(trigger);  
847         game_fileinfo.control_offset            =       -1;
848         game_fileinfo.control_howmany           =       0;
849         game_fileinfo.control_sizeof            =       sizeof(control_center_triggers);
850         game_fileinfo.matcen_offset             =       -1;
851         game_fileinfo.matcen_howmany            =       0;
852         game_fileinfo.matcen_sizeof             =       sizeof(matcen_info);
853
854         game_fileinfo.dl_indices_offset         =       -1;
855         game_fileinfo.dl_indices_howmany                =       0;
856         game_fileinfo.dl_indices_sizeof         =       sizeof(dl_index);
857
858         game_fileinfo.delta_light_offset                =       -1;
859         game_fileinfo.delta_light_howmany               =       0;
860         game_fileinfo.delta_light_sizeof                =       sizeof(delta_light);
861
862         // Read in game_top_fileinfo to get size of saved fileinfo.
863
864         if (cfseek( LoadFile, start_offset, SEEK_SET )) 
865                 Error( "Error seeking in gamesave.c" ); 
866
867 //      if (cfread( &game_top_fileinfo, sizeof(game_top_fileinfo), 1, LoadFile) != 1)
868 //              Error( "Error reading game_top_fileinfo in gamesave.c" );
869
870         game_top_fileinfo.fileinfo_signature = cfile_read_short(LoadFile);
871         game_top_fileinfo.fileinfo_version = cfile_read_short(LoadFile);
872         game_top_fileinfo.fileinfo_sizeof = cfile_read_int(LoadFile);
873
874         // Check signature
875         if (game_top_fileinfo.fileinfo_signature != 0x6705)
876                 return -1;
877
878         // Check version number
879         if (game_top_fileinfo.fileinfo_version < GAME_COMPATIBLE_VERSION )
880                 return -1;
881
882         // Now, Read in the fileinfo
883         if (cfseek( LoadFile, start_offset, SEEK_SET )) 
884                 Error( "Error seeking to game_fileinfo in gamesave.c" );
885
886 //      if (cfread( &game_fileinfo, game_top_fileinfo.fileinfo_sizeof, 1, LoadFile )!=1)
887 //              Error( "Error reading game_fileinfo in gamesave.c" );
888
889         game_fileinfo.fileinfo_signature = cfile_read_short(LoadFile);
890         game_fileinfo.fileinfo_version = cfile_read_short(LoadFile);
891         game_fileinfo.fileinfo_sizeof = cfile_read_int(LoadFile);
892         for(i=0; i<15; i++)
893                 game_fileinfo.mine_filename[i] = cfile_read_byte(LoadFile);
894         game_fileinfo.level = cfile_read_int(LoadFile);
895         game_fileinfo.player_offset = cfile_read_int(LoadFile);                         // Player info
896         game_fileinfo.player_sizeof = cfile_read_int(LoadFile);
897         game_fileinfo.object_offset = cfile_read_int(LoadFile);                         // Object info
898         game_fileinfo.object_howmany = cfile_read_int(LoadFile);        
899         game_fileinfo.object_sizeof = cfile_read_int(LoadFile);  
900         game_fileinfo.walls_offset = cfile_read_int(LoadFile);
901         game_fileinfo.walls_howmany = cfile_read_int(LoadFile);
902         game_fileinfo.walls_sizeof = cfile_read_int(LoadFile);
903         game_fileinfo.doors_offset = cfile_read_int(LoadFile);
904         game_fileinfo.doors_howmany = cfile_read_int(LoadFile);
905         game_fileinfo.doors_sizeof = cfile_read_int(LoadFile);
906         game_fileinfo.triggers_offset = cfile_read_int(LoadFile);
907         game_fileinfo.triggers_howmany = cfile_read_int(LoadFile);
908         game_fileinfo.triggers_sizeof = cfile_read_int(LoadFile);
909         game_fileinfo.links_offset = cfile_read_int(LoadFile);
910         game_fileinfo.links_howmany = cfile_read_int(LoadFile);
911         game_fileinfo.links_sizeof = cfile_read_int(LoadFile);
912         game_fileinfo.control_offset = cfile_read_int(LoadFile);
913         game_fileinfo.control_howmany = cfile_read_int(LoadFile);
914         game_fileinfo.control_sizeof = cfile_read_int(LoadFile);
915         game_fileinfo.matcen_offset = cfile_read_int(LoadFile);
916         game_fileinfo.matcen_howmany = cfile_read_int(LoadFile);
917         game_fileinfo.matcen_sizeof = cfile_read_int(LoadFile);
918
919         if (game_top_fileinfo.fileinfo_version >= 29) {
920                 game_fileinfo.dl_indices_offset = cfile_read_int(LoadFile);
921                 game_fileinfo.dl_indices_howmany = cfile_read_int(LoadFile);
922                 game_fileinfo.dl_indices_sizeof = cfile_read_int(LoadFile);
923
924                 game_fileinfo.delta_light_offset = cfile_read_int(LoadFile);
925                 game_fileinfo.delta_light_howmany = cfile_read_int(LoadFile);
926                 game_fileinfo.delta_light_sizeof = cfile_read_int(LoadFile);
927         }
928
929         if (game_top_fileinfo.fileinfo_version >= 31) { //load mine filename
930                 // read newline-terminated string, not sure what version this changed.
931                 cfgets(Current_level_name,sizeof(Current_level_name),LoadFile);
932
933                 if (Current_level_name[strlen(Current_level_name)-1] == '\n')
934                         Current_level_name[strlen(Current_level_name)-1] = 0;
935         }
936         else if (game_top_fileinfo.fileinfo_version >= 14) { //load mine filename
937                 // read null-terminated string
938                 char *p=Current_level_name;
939                 //must do read one char at a time, since no cfgets()
940                 do *p = cfgetc(LoadFile); while (*p++!=0);
941         }
942         else
943                 Current_level_name[0]=0;
944
945         if (game_top_fileinfo.fileinfo_version >= 19) { //load pof names
946                 N_save_pof_names = cfile_read_short(LoadFile);
947                 if (N_save_pof_names != 0x614d && N_save_pof_names != 0x5547) { // "Ma"de w/DMB beta/"GU"ILE
948                         Assert(N_save_pof_names < MAX_POLYGON_MODELS);
949                         cfread(Save_pof_names,N_save_pof_names,FILENAME_LEN,LoadFile);
950                 }
951         }
952
953         //===================== READ PLAYER INFO ==========================
954         Object_next_signature = 0;
955
956         //===================== READ OBJECT INFO ==========================
957
958         Gamesave_num_org_robots = 0;
959         Gamesave_num_players = 0;
960
961         if (game_fileinfo.object_offset > -1) {
962                 if (cfseek( LoadFile, game_fileinfo.object_offset, SEEK_SET ))
963                         Error( "Error seeking to object_offset in gamesave.c" );
964
965                 for (i=0;i<game_fileinfo.object_howmany;i++) {
966
967                         read_object(&Objects[i], LoadFile, game_top_fileinfo.fileinfo_version);
968
969                         Objects[i].signature = Object_next_signature++;
970                         verify_object( &Objects[i] );
971                 }
972
973         }
974
975         //===================== READ WALL INFO ============================
976
977         if (game_fileinfo.walls_offset > -1)
978         {
979
980                 if (!cfseek( LoadFile, game_fileinfo.walls_offset,SEEK_SET ))   {
981                         for (i=0;i<game_fileinfo.walls_howmany;i++) {
982
983                                 if (game_top_fileinfo.fileinfo_version >= 20)
984                                         wall_read(&Walls[i], LoadFile); // v20 walls and up.
985                                 else if (game_top_fileinfo.fileinfo_version >= 17) {
986                                         v19_wall w;
987
988                                         v19_wall_read(&w, LoadFile);
989
990                                         Walls[i].segnum         = w.segnum;
991                                         Walls[i].sidenum                = w.sidenum;
992                                         Walls[i].linked_wall    = w.linked_wall;
993
994                                         Walls[i].type                   = w.type;
995                                         Walls[i].flags                  = w.flags;
996                                         Walls[i].hps                    = w.hps;
997                                         Walls[i].trigger                = w.trigger;
998                                         Walls[i].clip_num               = w.clip_num;
999                                         Walls[i].keys                   = w.keys;
1000
1001                                         Walls[i].state                  = WALL_DOOR_CLOSED;
1002                                 } else {
1003                                         v16_wall w;
1004
1005                                         v16_wall_read(&w, LoadFile);
1006
1007                                         Walls[i].segnum = Walls[i].sidenum = Walls[i].linked_wall = -1;
1008
1009                                         Walls[i].type           = w.type;
1010                                         Walls[i].flags          = w.flags;
1011                                         Walls[i].hps            = w.hps;
1012                                         Walls[i].trigger        = w.trigger;
1013                                         Walls[i].clip_num       = w.clip_num;
1014                                         Walls[i].keys           = w.keys;
1015                                 }
1016
1017                         }
1018                 }
1019         }
1020
1021         //===================== READ DOOR INFO ============================
1022
1023         if (game_fileinfo.doors_offset > -1)
1024         {
1025                 if (!cfseek( LoadFile, game_fileinfo.doors_offset,SEEK_SET ))   {
1026
1027                         for (i=0;i<game_fileinfo.doors_howmany;i++) {
1028
1029                                 if (game_top_fileinfo.fileinfo_version >= 20)
1030                                         active_door_read(&ActiveDoors[i], LoadFile); // version 20 and up
1031                                 else {
1032                                         v19_door d;
1033                                         int p;
1034
1035                                         v19_door_read(&d, LoadFile);
1036
1037                                         ActiveDoors[i].n_parts = d.n_parts;
1038
1039                                         for (p=0;p<d.n_parts;p++) {
1040                                                 int cseg,cside;
1041
1042                                                 cseg = Segments[d.seg[p]].children[d.side[p]];
1043                                                 cside = find_connect_side(&Segments[d.seg[p]],&Segments[cseg]);
1044
1045                                                 ActiveDoors[i].front_wallnum[p] = Segments[d.seg[p]].sides[d.side[p]].wall_num;
1046                                                 ActiveDoors[i].back_wallnum[p] = Segments[cseg].sides[cside].wall_num;
1047                                         }
1048                                 }
1049
1050                         }
1051                 }
1052         }
1053
1054         //==================== READ TRIGGER INFO ==========================
1055
1056
1057 // for MACINTOSH -- assume all triggers >= verion 31 triggers.
1058
1059         if (game_fileinfo.triggers_offset > -1)
1060         {
1061                 if (!cfseek( LoadFile, game_fileinfo.triggers_offset,SEEK_SET ))        {
1062                         for (i=0;i<game_fileinfo.triggers_howmany;i++)
1063                                 if (game_top_fileinfo.fileinfo_version < 31) {
1064                                         v30_trigger trig;
1065                                         int t,type;
1066
1067                                         type=0;
1068
1069                                         if (game_top_fileinfo.fileinfo_version < 30) {
1070                                                 v29_trigger trig29;
1071                                                 int t;
1072
1073                                                 v29_trigger_read(&trig29, LoadFile);
1074
1075                                                 trig.flags              = trig29.flags;
1076                                                 trig.num_links  = trig29.num_links;
1077                                                 trig.num_links  = trig29.num_links;
1078                                                 trig.value              = trig29.value;
1079                                                 trig.time               = trig29.time;
1080
1081                                                 for (t=0;t<trig.num_links;t++) {
1082                                                         trig.seg[t]  = trig29.seg[t];
1083                                                         trig.side[t] = trig29.side[t];
1084                                                 }
1085                                         }
1086                                         else
1087                                                 v30_trigger_read(&trig, LoadFile);
1088
1089                                         //Assert(trig.flags & TRIGGER_ON);
1090                                         trig.flags &= ~TRIGGER_ON;
1091
1092                                         if (trig.flags & TRIGGER_CONTROL_DOORS)
1093                                                 type = TT_OPEN_DOOR;
1094                                         else if (trig.flags & TRIGGER_SHIELD_DAMAGE)
1095                                                 Int3();
1096                                         else if (trig.flags & TRIGGER_ENERGY_DRAIN)
1097                                                 Int3();
1098                                         else if (trig.flags & TRIGGER_EXIT)
1099                                                 type = TT_EXIT;
1100                                         else if (trig.flags & TRIGGER_ONE_SHOT)
1101                                                 Int3();
1102                                         else if (trig.flags & TRIGGER_MATCEN)
1103                                                 type = TT_MATCEN;
1104                                         else if (trig.flags & TRIGGER_ILLUSION_OFF)
1105                                                 type = TT_ILLUSION_OFF;
1106                                         else if (trig.flags & TRIGGER_SECRET_EXIT)
1107                                                 type = TT_SECRET_EXIT;
1108                                         else if (trig.flags & TRIGGER_ILLUSION_ON)
1109                                                 type = TT_ILLUSION_ON;
1110                                         else if (trig.flags & TRIGGER_UNLOCK_DOORS)
1111                                                 type = TT_UNLOCK_DOOR;
1112                                         else if (trig.flags & TRIGGER_OPEN_WALL)
1113                                                 type = TT_OPEN_WALL;
1114                                         else if (trig.flags & TRIGGER_CLOSE_WALL)
1115                                                 type = TT_CLOSE_WALL;
1116                                         else if (trig.flags & TRIGGER_ILLUSORY_WALL)
1117                                                 type = TT_ILLUSORY_WALL;
1118                                         else
1119                                                 Int3();
1120
1121                                         Triggers[i].type        = type;
1122                                         Triggers[i].flags       = 0;
1123                                         Triggers[i].num_links   = trig.num_links;
1124                                         Triggers[i].num_links   = trig.num_links;
1125                                         Triggers[i].value       = trig.value;
1126                                         Triggers[i].time        = trig.time;
1127
1128                                         for (t=0;t<trig.num_links;t++) {
1129                                                 Triggers[i].seg[t] = trig.seg[t];
1130                                                 Triggers[i].side[t] = trig.side[t];
1131                                         }
1132                                 }
1133                                 else
1134                                         trigger_read(&Triggers[i], LoadFile);
1135                 }
1136         }
1137
1138         //================ READ CONTROL CENTER TRIGGER INFO ===============
1139
1140         if (game_fileinfo.control_offset > -1)
1141                 if (!cfseek( LoadFile, game_fileinfo.control_offset,SEEK_SET )) {
1142                         Assert(game_fileinfo.control_howmany == 1);
1143                         control_center_triggers_read(&ControlCenterTriggers, LoadFile);
1144                 }
1145
1146         //================ READ MATERIALOGRIFIZATIONATORS INFO ===============
1147
1148         if (game_fileinfo.matcen_offset > -1)
1149         {       int     j;
1150
1151                 if (!cfseek( LoadFile, game_fileinfo.matcen_offset,SEEK_SET ))  {
1152                         // mprintf((0, "Reading %i materialization centers.\n", game_fileinfo.matcen_howmany));
1153                         for (i=0;i<game_fileinfo.matcen_howmany;i++) {
1154                                 if (game_top_fileinfo.fileinfo_version < 27) {
1155                                         old_matcen_info m;
1156
1157                                         old_matcen_info_read(&m, LoadFile);
1158
1159                                         RobotCenters[i].robot_flags[0] = m.robot_flags;
1160                                         RobotCenters[i].robot_flags[1] = 0;
1161                                         RobotCenters[i].hit_points = m.hit_points;
1162                                         RobotCenters[i].interval = m.interval;
1163                                         RobotCenters[i].segnum = m.segnum;
1164                                         RobotCenters[i].fuelcen_num = m.fuelcen_num;
1165                                 }
1166                                 else
1167                                         matcen_info_read(&RobotCenters[i], LoadFile);
1168
1169                                 //      Set links in RobotCenters to Station array
1170
1171                                 for (j=0; j<=Highest_segment_index; j++)
1172                                         if (Segment2s[j].special == SEGMENT_IS_ROBOTMAKER)
1173                                                 if (Segment2s[j].matcen_num == i)
1174                                                         RobotCenters[i].fuelcen_num = Segment2s[j].value;
1175
1176                                 // mprintf((0, "   %i: flags = %08x\n", i, RobotCenters[i].robot_flags));
1177                         }
1178                 }
1179         }
1180
1181
1182         //================ READ DL_INDICES INFO ===============
1183
1184         Num_static_lights = 0;
1185
1186         if (game_fileinfo.dl_indices_offset > -1) {
1187                 int     i;
1188
1189                 if (!cfseek( LoadFile, game_fileinfo.dl_indices_offset, SEEK_SET ))     {
1190                         Num_static_lights = game_fileinfo.dl_indices_howmany;
1191                         for (i=0; i<game_fileinfo.dl_indices_howmany; i++) {
1192                                 if (game_top_fileinfo.fileinfo_version < 29) {
1193                                         mprintf((0, "Warning: Old mine version.  Not reading Dl_indices info.\n"));
1194                                         Int3(); //shouldn't be here!!!
1195                                 } else
1196                                         dl_index_read(&Dl_indices[i], LoadFile);
1197                         }
1198                 }
1199         }
1200
1201         //      Indicate that no light has been subtracted from any vertices.
1202         clear_light_subtracted();
1203
1204         //================ READ DELTA LIGHT INFO ===============
1205
1206         if (game_fileinfo.delta_light_offset > -1) {
1207                 int     i;
1208
1209                 if (!cfseek( LoadFile, game_fileinfo.delta_light_offset, SEEK_SET ))    {
1210                         for (i=0; i<game_fileinfo.delta_light_howmany; i++) {
1211                                 if (game_top_fileinfo.fileinfo_version < 29) {
1212                                         mprintf((0, "Warning: Old mine version.  Not reading delta light info.\n"));
1213                                 } else
1214                                         delta_light_read(&Delta_lights[i], LoadFile);
1215                         }
1216                 }
1217         }
1218
1219         //========================= UPDATE VARIABLES ======================
1220
1221         reset_objects(game_fileinfo.object_howmany);
1222
1223         for (i=0; i<MAX_OBJECTS; i++) {
1224                 Objects[i].next = Objects[i].prev = -1;
1225                 if (Objects[i].type != OBJ_NONE) {
1226                         int objsegnum = Objects[i].segnum;
1227
1228                         if (objsegnum > Highest_segment_index)          //bogus object
1229                                 Objects[i].type = OBJ_NONE;
1230                         else {
1231                                 Objects[i].segnum = -1;                 //avoid Assert()
1232                                 obj_link(i,objsegnum);
1233                         }
1234                 }
1235         }
1236
1237         clear_transient_objects(1);             //1 means clear proximity bombs
1238
1239         // Make sure non-transparent doors are set correctly.
1240         for (i=0; i< Num_segments; i++)
1241                 for (j=0;j<MAX_SIDES_PER_SEGMENT;j++) {
1242                         side    *sidep = &Segments[i].sides[j];
1243                         if ((sidep->wall_num != -1) && (Walls[sidep->wall_num].clip_num != -1)) {
1244                                 //mprintf((0, "Checking Wall %d\n", Segments[i].sides[j].wall_num));
1245                                 if (WallAnims[Walls[sidep->wall_num].clip_num].flags & WCF_TMAP1) {
1246                                         //mprintf((0, "Fixing non-transparent door.\n"));
1247                                         sidep->tmap_num = WallAnims[Walls[sidep->wall_num].clip_num].frames[0];
1248                                         sidep->tmap_num2 = 0;
1249                                 }
1250                         }
1251                 }
1252
1253
1254         Num_walls = game_fileinfo.walls_howmany;
1255         reset_walls();
1256
1257         Num_open_doors = game_fileinfo.doors_howmany;
1258         Num_triggers = game_fileinfo.triggers_howmany;
1259
1260         //go through all walls, killing references to invalid triggers
1261         for (i=0;i<Num_walls;i++)
1262                 if (Walls[i].trigger >= Num_triggers) {
1263                         mprintf((0,"Removing reference to invalid trigger %d from wall %d\n",Walls[i].trigger,i));
1264                         Walls[i].trigger = -1;  //kill trigger
1265                 }
1266
1267         //go through all triggers, killing unused ones
1268         for (i=0;i<Num_triggers;) {
1269                 int w;
1270
1271                 //      Find which wall this trigger is connected to.
1272                 for (w=0; w<Num_walls; w++)
1273                         if (Walls[w].trigger == i)
1274                                 break;
1275
1276         #ifdef EDITOR
1277                 if (w == Num_walls) {
1278                         mprintf((0,"Removing unreferenced trigger %d\n",i));
1279                         remove_trigger_num(i);
1280                 }
1281                 else
1282         #endif
1283                         i++;
1284         }
1285
1286         //      MK, 10/17/95: Make walls point back at the triggers that control them.
1287         //      Go through all triggers, stuffing controlling_trigger field in Walls.
1288         {       int t;
1289
1290         for (i=0; i<Num_walls; i++)
1291                 Walls[i].controlling_trigger = -1;
1292
1293         for (t=0; t<Num_triggers; t++) {
1294                 int     l;
1295                 for (l=0; l<Triggers[t].num_links; l++) {
1296                         int     seg_num, side_num, wall_num;
1297
1298                         seg_num = Triggers[t].seg[l];
1299                         side_num = Triggers[t].side[l];
1300                         wall_num = Segments[seg_num].sides[side_num].wall_num;
1301
1302                         // -- if (Walls[wall_num].controlling_trigger != -1)
1303                         // --   Int3();
1304
1305                         //check to see that if a trigger requires a wall that it has one,
1306                         //and if it requires a matcen that it has one
1307
1308                         if (Triggers[t].type == TT_MATCEN) {
1309                                 if (Segment2s[seg_num].special != SEGMENT_IS_ROBOTMAKER)
1310                                         Int3();         //matcen trigger doesn't point to matcen
1311                         }
1312                         else if (Triggers[t].type != TT_LIGHT_OFF && Triggers[t].type != TT_LIGHT_ON) { //light triggers don't require walls
1313                                 if (wall_num == -1)
1314                                         Int3(); //      This is illegal.  This trigger requires a wall
1315                                 else
1316                                         Walls[wall_num].controlling_trigger = t;
1317                         }
1318                 }
1319         }
1320         }
1321
1322         Num_robot_centers = game_fileinfo.matcen_howmany;
1323
1324         //fix old wall structs
1325         if (game_top_fileinfo.fileinfo_version < 17) {
1326                 int segnum,sidenum,wallnum;
1327
1328                 for (segnum=0; segnum<=Highest_segment_index; segnum++)
1329                         for (sidenum=0;sidenum<6;sidenum++)
1330                                 if ((wallnum=Segments[segnum].sides[sidenum].wall_num) != -1) {
1331                                         Walls[wallnum].segnum = segnum;
1332                                         Walls[wallnum].sidenum = sidenum;
1333                                 }
1334         }
1335
1336         #ifndef NDEBUG
1337         {
1338                 int     sidenum;
1339                 for (sidenum=0; sidenum<6; sidenum++) {
1340                         int     wallnum = Segments[Highest_segment_index].sides[sidenum].wall_num;
1341                         if (wallnum != -1)
1342                                 if ((Walls[wallnum].segnum != Highest_segment_index) || (Walls[wallnum].sidenum != sidenum))
1343                                         Int3(); //      Error.  Bogus walls in this segment.
1344                                                                 // Consult Yuan or Mike.
1345                 }
1346         }
1347         #endif
1348
1349         //create_local_segment_data();
1350
1351         fix_object_segs();
1352
1353         #ifndef NDEBUG
1354         dump_mine_info();
1355         #endif
1356
1357         if (game_top_fileinfo.fileinfo_version < GAME_VERSION && !(game_top_fileinfo.fileinfo_version==25 && GAME_VERSION==26))
1358                 return 1;               //means old version
1359         else
1360                 return 0;
1361 }
1362
1363
1364 int check_segment_connections(void);
1365
1366 extern void     set_ambient_sound_flags(void);
1367
1368 // ----------------------------------------------------------------------------
1369
1370 #define LEVEL_FILE_VERSION      8
1371 //1 -> 2  add palette name
1372 //2 -> 3  add control center explosion time
1373 //3 -> 4  add reactor strength
1374 //4 -> 5  killed hostage text stuff
1375 //5 -> 6  added Secret_return_segment and Secret_return_orient
1376 //6 -> 7  added flickering lights
1377 //7 -> 8  made version 8 to be not compatible with D2 1.0 & 1.1
1378
1379 #ifndef RELEASE
1380 char *Level_being_loaded=NULL;
1381 #endif
1382
1383 #ifdef COMPACT_SEGS
1384 extern void ncache_flush();
1385 #endif
1386
1387 extern int Slide_segs_computed;
1388
1389 int no_old_level_file_error=0;
1390
1391 //loads a level (.LVL) file from disk
1392 //returns 0 if success, else error code
1393 int load_level(char * filename_passed)
1394 {
1395 #ifdef EDITOR
1396         int use_compiled_level=1;
1397 #endif
1398         CFILE * LoadFile;
1399         char filename[128];
1400         int sig, minedata_offset, gamedata_offset;
1401         int mine_err, game_err;
1402 #ifdef NETWORK
1403         int i;
1404 #endif
1405
1406         Slide_segs_computed = 0;
1407
1408 #ifdef NETWORK
1409    if (Game_mode & GM_NETWORK)
1410          {
1411           for (i=0;i<MAX_POWERUP_TYPES;i++)
1412                 {
1413                         MaxPowerupsAllowed[i]=0;
1414                         PowerupsInMine[i]=0;
1415                 }
1416          }
1417 #endif
1418
1419         #ifdef COMPACT_SEGS
1420         ncache_flush();
1421         #endif
1422
1423         #ifndef RELEASE
1424         Level_being_loaded = filename_passed;
1425         #endif
1426
1427         strcpy(filename,filename_passed);
1428
1429         #ifdef EDITOR
1430                 //if we have the editor, try the LVL first, no matter what was passed.
1431                 //if we don't have an LVL, try RDL  
1432                 //if we don't have the editor, we just use what was passed
1433         
1434                 change_filename_extension(filename,filename_passed,".lvl");
1435                 use_compiled_level = 0;
1436         
1437                 if (!cfexist(filename)) {
1438                         change_filename_extension(filename,filename,".rl2");
1439                         use_compiled_level = 1;
1440                 }               
1441         #endif
1442
1443         LoadFile = cfopen( filename, "rb" );
1444
1445         if (!LoadFile)  {
1446                 #ifdef EDITOR
1447                         mprintf((0,"Can't open level file <%s>\n", filename));
1448                         return 1;
1449                 #else
1450                         Error("Can't open file <%s>\n",filename);
1451                 #endif
1452         }
1453
1454         strcpy( Gamesave_current_filename, filename );
1455
1456 //      #ifdef NEWDEMO
1457 //      if ( Newdemo_state == ND_STATE_RECORDING )
1458 //              newdemo_record_start_demo();
1459 //      #endif
1460
1461         sig                      = cfile_read_int(LoadFile);
1462         Gamesave_current_version = cfile_read_int(LoadFile);
1463         mprintf((0, "Gamesave_current_version = %d\n", Gamesave_current_version));
1464         minedata_offset          = cfile_read_int(LoadFile);
1465         gamedata_offset          = cfile_read_int(LoadFile);
1466
1467         Assert(sig == MAKE_SIG('P','L','V','L'));
1468
1469         if (Gamesave_current_version >= 8) {    //read dummy data
1470                 if (HoardEquipped())
1471                 {
1472                         cfile_read_int(LoadFile);
1473                         cfile_read_short(LoadFile);
1474                         cfile_read_byte(LoadFile);
1475                 }
1476                 else
1477                         Error("This level requires the Vertigo Enhanced version of D2.");
1478
1479         }
1480
1481         if (Gamesave_current_version < 5)
1482                 cfile_read_int(LoadFile);       //was hostagetext_offset
1483
1484         if (Gamesave_current_version > 1) {
1485                 cfgets(Current_level_palette,sizeof(Current_level_palette),LoadFile);
1486                 if (Current_level_palette[strlen(Current_level_palette)-1] == '\n')
1487                         Current_level_palette[strlen(Current_level_palette)-1] = 0;
1488         }
1489         if (Gamesave_current_version <= 1 || Current_level_palette[0]==0) // descent 1 level
1490                 strcpy(Current_level_palette, DEFAULT_LEVEL_PALETTE);
1491
1492         if (Gamesave_current_version >= 3)
1493                 Base_control_center_explosion_time = cfile_read_int(LoadFile);
1494         else
1495                 Base_control_center_explosion_time = DEFAULT_CONTROL_CENTER_EXPLOSION_TIME;
1496
1497         if (Gamesave_current_version >= 4)
1498                 Reactor_strength = cfile_read_int(LoadFile);
1499         else
1500                 Reactor_strength = -1;  //use old defaults
1501
1502         if (Gamesave_current_version >= 7) {
1503                 int i;
1504
1505                 Num_flickering_lights = cfile_read_int(LoadFile);
1506                 Assert((Num_flickering_lights >= 0) && (Num_flickering_lights < MAX_FLICKERING_LIGHTS));
1507                 for (i = 0; i < Num_flickering_lights; i++)
1508                         flickering_light_read(&Flickering_lights[i], LoadFile);
1509         }
1510         else
1511                 Num_flickering_lights = 0;
1512
1513         if (Gamesave_current_version < 6) {
1514                 Secret_return_segment = 0;
1515                 Secret_return_orient.rvec.x = F1_0;
1516                 Secret_return_orient.rvec.y = 0;
1517                 Secret_return_orient.rvec.z = 0;
1518                 Secret_return_orient.fvec.x = 0;
1519                 Secret_return_orient.fvec.y = F1_0;
1520                 Secret_return_orient.fvec.z = 0;
1521                 Secret_return_orient.uvec.x = 0;
1522                 Secret_return_orient.uvec.y = 0;
1523                 Secret_return_orient.uvec.z = F1_0;
1524         } else {
1525                 Secret_return_segment = cfile_read_int(LoadFile);
1526                 Secret_return_orient.rvec.x = cfile_read_int(LoadFile);
1527                 Secret_return_orient.rvec.y = cfile_read_int(LoadFile);
1528                 Secret_return_orient.rvec.z = cfile_read_int(LoadFile);
1529                 Secret_return_orient.fvec.x = cfile_read_int(LoadFile);
1530                 Secret_return_orient.fvec.y = cfile_read_int(LoadFile);
1531                 Secret_return_orient.fvec.z = cfile_read_int(LoadFile);
1532                 Secret_return_orient.uvec.x = cfile_read_int(LoadFile);
1533                 Secret_return_orient.uvec.y = cfile_read_int(LoadFile);
1534                 Secret_return_orient.uvec.z = cfile_read_int(LoadFile);
1535         }
1536
1537         cfseek(LoadFile,minedata_offset,SEEK_SET);
1538         #ifdef EDITOR
1539         if (!use_compiled_level) {
1540                 mine_err = load_mine_data(LoadFile);
1541 #if 0 //dunno - 3rd party stuff?
1542                 // Compress all uv coordinates in mine, improves texmap precision. --MK, 02/19/96
1543                 compress_uv_coordinates_all();
1544 #endif
1545         } else
1546         #endif
1547                 //NOTE LINK TO ABOVE!!
1548                 mine_err = load_mine_data_compiled(LoadFile);
1549
1550         if (mine_err == -1) {   //error!!
1551                 cfclose(LoadFile);
1552                 return 2;
1553         }
1554
1555         cfseek(LoadFile,gamedata_offset,SEEK_SET);
1556         game_err = load_game_data(LoadFile);
1557
1558         if (game_err == -1) {   //error!!
1559                 cfclose(LoadFile);
1560                 return 3;
1561         }
1562
1563         //======================== CLOSE FILE =============================
1564
1565         cfclose( LoadFile );
1566
1567         set_ambient_sound_flags();
1568
1569         #ifdef EDITOR
1570         write_game_text_file(filename);
1571         if (Errors_in_mine) {
1572                 if (is_real_level(filename)) {
1573                         char  ErrorMessage[200];
1574
1575                         sprintf( ErrorMessage, "Warning: %i errors in %s!\n", Errors_in_mine, Level_being_loaded );
1576                         stop_time();
1577                         gr_palette_load(gr_palette);
1578                         nm_messagebox( NULL, 1, "Continue", ErrorMessage );
1579                         start_time();
1580                 } else
1581                         mprintf((1, "Error: %i errors in %s.\n", Errors_in_mine, Level_being_loaded));
1582         }
1583         #endif
1584
1585         #ifdef EDITOR
1586         //If an old version, ask the use if he wants to save as new version
1587         if (!no_old_level_file_error && (Function_mode == FMODE_EDITOR) && (((LEVEL_FILE_VERSION > 3) && Gamesave_current_version < LEVEL_FILE_VERSION) || mine_err == 1 || game_err == 1)) {
1588                 char  ErrorMessage[200];
1589
1590                 sprintf( ErrorMessage,
1591                                         "You just loaded a old version\n"
1592                                         "level.  Would you like to save\n"
1593                                         "it as a current version level?");
1594
1595                 stop_time();
1596                 gr_palette_load(gr_palette);
1597                 if (nm_messagebox( NULL, 2, "Don't Save", "Save", ErrorMessage )==1)
1598                         save_level(filename);
1599                 start_time();
1600         }
1601         #endif
1602
1603         #ifdef EDITOR
1604         if (Function_mode == FMODE_EDITOR)
1605                 editor_status("Loaded NEW mine %s, \"%s\"",filename,Current_level_name);
1606         #endif
1607
1608         #ifdef EDITOR
1609         if (check_segment_connections())
1610                 nm_messagebox( "ERROR", 1, "Ok", 
1611                                 "Connectivity errors detected in\n"
1612                                 "mine.  See monochrome screen for\n"
1613                                 "details, and contact Matt or Mike." );
1614         #endif
1615
1616         return 0;
1617 }
1618
1619 #ifdef EDITOR
1620 void get_level_name()
1621 {
1622 //NO_UI!!!      UI_WINDOW                               *NameWindow = NULL;
1623 //NO_UI!!!      UI_GADGET_INPUTBOX      *NameText;
1624 //NO_UI!!!      UI_GADGET_BUTTON                *QuitButton;
1625 //NO_UI!!!
1626 //NO_UI!!!      // Open a window with a quit button
1627 //NO_UI!!!      NameWindow = ui_open_window( 20, 20, 300, 110, WIN_DIALOG );
1628 //NO_UI!!!      QuitButton = ui_add_gadget_button( NameWindow, 150-24, 60, 48, 40, "Done", NULL );
1629 //NO_UI!!!
1630 //NO_UI!!!      ui_wprintf_at( NameWindow, 10, 12,"Please enter a name for this mine:" );
1631 //NO_UI!!!      NameText = ui_add_gadget_inputbox( NameWindow, 10, 30, LEVEL_NAME_LEN, LEVEL_NAME_LEN, Current_level_name );
1632 //NO_UI!!!
1633 //NO_UI!!!      NameWindow->keyboard_focus_gadget = (UI_GADGET *)NameText;
1634 //NO_UI!!!      QuitButton->hotkey = KEY_ENTER;
1635 //NO_UI!!!
1636 //NO_UI!!!      ui_gadget_calc_keys(NameWindow);
1637 //NO_UI!!!
1638 //NO_UI!!!      while (!QuitButton->pressed && last_keypress!=KEY_ENTER) {
1639 //NO_UI!!!              ui_mega_process();
1640 //NO_UI!!!              ui_window_do_gadgets(NameWindow);
1641 //NO_UI!!!      }
1642 //NO_UI!!!
1643 //NO_UI!!!      strcpy( Current_level_name, NameText->text );
1644 //NO_UI!!!
1645 //NO_UI!!!      if ( NameWindow!=NULL ) {
1646 //NO_UI!!!              ui_close_window( NameWindow );
1647 //NO_UI!!!              NameWindow = NULL;
1648 //NO_UI!!!      }
1649 //NO_UI!!!
1650
1651         newmenu_item m[2];
1652
1653         m[0].type = NM_TYPE_TEXT; m[0].text = "Please enter a name for this mine:";
1654         m[1].type = NM_TYPE_INPUT; m[1].text = Current_level_name; m[1].text_len = LEVEL_NAME_LEN;
1655
1656         newmenu_do( NULL, "Enter mine name", 2, m, NULL );
1657
1658 }
1659 #endif
1660
1661
1662 #ifdef EDITOR
1663
1664 int     Errors_in_mine;
1665
1666 // -----------------------------------------------------------------------------
1667 int compute_num_delta_light_records(void)
1668 {
1669         int     i;
1670         int     total = 0;
1671
1672         for (i=0; i<Num_static_lights; i++) {
1673                 total += Dl_indices[i].count;
1674         }
1675
1676         return total;
1677
1678 }
1679
1680 // -----------------------------------------------------------------------------
1681 // Save game
1682 int save_game_data(FILE * SaveFile)
1683 {
1684         int  player_offset, object_offset, walls_offset, doors_offset, triggers_offset, control_offset, matcen_offset; //, links_offset;
1685         int     dl_indices_offset, delta_light_offset;
1686         int start_offset,end_offset;
1687
1688         start_offset = ftell(SaveFile);
1689
1690         //===================== SAVE FILE INFO ========================
1691
1692         game_fileinfo.fileinfo_signature =      0x6705;
1693         game_fileinfo.fileinfo_version  =       GAME_VERSION;
1694         game_fileinfo.level                                     =  Current_level_num;
1695         game_fileinfo.fileinfo_sizeof           =       sizeof(game_fileinfo);
1696         game_fileinfo.player_offset             =       -1;
1697         game_fileinfo.player_sizeof             =       sizeof(player);
1698         game_fileinfo.object_offset             =       -1;
1699         game_fileinfo.object_howmany            =       Highest_object_index+1;
1700         game_fileinfo.object_sizeof             =       sizeof(object);
1701         game_fileinfo.walls_offset                      =       -1;
1702         game_fileinfo.walls_howmany             =       Num_walls;
1703         game_fileinfo.walls_sizeof                      =       sizeof(wall);
1704         game_fileinfo.doors_offset                      =       -1;
1705         game_fileinfo.doors_howmany             =       Num_open_doors;
1706         game_fileinfo.doors_sizeof                      =       sizeof(active_door);
1707         game_fileinfo.triggers_offset           =       -1;
1708         game_fileinfo.triggers_howmany  =       Num_triggers;
1709         game_fileinfo.triggers_sizeof           =       sizeof(trigger);
1710         game_fileinfo.control_offset            =       -1;
1711         game_fileinfo.control_howmany           =  1;
1712         game_fileinfo.control_sizeof            =  sizeof(control_center_triggers);
1713         game_fileinfo.matcen_offset             =       -1;
1714         game_fileinfo.matcen_howmany            =       Num_robot_centers;
1715         game_fileinfo.matcen_sizeof             =       sizeof(matcen_info);
1716
1717         game_fileinfo.dl_indices_offset         =       -1;
1718         game_fileinfo.dl_indices_howmany                =       Num_static_lights;
1719         game_fileinfo.dl_indices_sizeof         =       sizeof(dl_index);
1720
1721         game_fileinfo.delta_light_offset                =       -1;
1722         game_fileinfo.delta_light_howmany       =       compute_num_delta_light_records();
1723         game_fileinfo.delta_light_sizeof                =       sizeof(delta_light);
1724
1725         // Write the fileinfo
1726         fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
1727
1728         // Write the mine name
1729         fprintf(SaveFile,"%s\n",Current_level_name);
1730
1731         fwrite(&N_polygon_models,2,1,SaveFile);
1732         fwrite(Pof_names,N_polygon_models,sizeof(*Pof_names),SaveFile);
1733
1734         //==================== SAVE PLAYER INFO ===========================
1735
1736         player_offset = ftell(SaveFile);
1737         fwrite( &Players[Player_num], sizeof(player), 1, SaveFile );
1738
1739         //==================== SAVE OBJECT INFO ===========================
1740
1741         object_offset = ftell(SaveFile);
1742         //fwrite( &Objects, sizeof(object), game_fileinfo.object_howmany, SaveFile );
1743         {
1744                 int i;
1745                 for (i=0;i<game_fileinfo.object_howmany;i++)
1746                         write_object(&Objects[i],SaveFile);
1747         }
1748
1749         //==================== SAVE WALL INFO =============================
1750
1751         walls_offset = ftell(SaveFile);
1752         fwrite( Walls, sizeof(wall), game_fileinfo.walls_howmany, SaveFile );
1753
1754         //==================== SAVE DOOR INFO =============================
1755
1756         doors_offset = ftell(SaveFile);
1757         fwrite( ActiveDoors, sizeof(active_door), game_fileinfo.doors_howmany, SaveFile );
1758
1759         //==================== SAVE TRIGGER INFO =============================
1760
1761         triggers_offset = ftell(SaveFile);
1762         fwrite( Triggers, sizeof(trigger), game_fileinfo.triggers_howmany, SaveFile );
1763
1764         //================ SAVE CONTROL CENTER TRIGGER INFO ===============
1765
1766         control_offset = ftell(SaveFile);
1767         fwrite( &ControlCenterTriggers, sizeof(control_center_triggers), 1, SaveFile );
1768
1769
1770         //================ SAVE MATERIALIZATION CENTER TRIGGER INFO ===============
1771
1772         matcen_offset = ftell(SaveFile);
1773         // mprintf((0, "Writing %i materialization centers\n", game_fileinfo.matcen_howmany));
1774         // { int i;
1775         // for (i=0; i<game_fileinfo.matcen_howmany; i++)
1776         //      mprintf((0, "   %i: robot_flags = %08x\n", i, RobotCenters[i].robot_flags));
1777         // }
1778         fwrite( RobotCenters, sizeof(matcen_info), game_fileinfo.matcen_howmany, SaveFile );
1779
1780         //================ SAVE DELTA LIGHT INFO ===============
1781         dl_indices_offset = ftell(SaveFile);
1782         fwrite( Dl_indices, sizeof(dl_index), game_fileinfo.dl_indices_howmany, SaveFile );
1783
1784         delta_light_offset = ftell(SaveFile);
1785         fwrite( Delta_lights, sizeof(delta_light), game_fileinfo.delta_light_howmany, SaveFile );
1786
1787         //============= REWRITE FILE INFO, TO SAVE OFFSETS ===============
1788
1789         // Update the offset fields
1790         game_fileinfo.player_offset             =       player_offset;
1791         game_fileinfo.object_offset             =       object_offset;
1792         game_fileinfo.walls_offset                      =       walls_offset;
1793         game_fileinfo.doors_offset                      =       doors_offset;
1794         game_fileinfo.triggers_offset           =       triggers_offset;
1795         game_fileinfo.control_offset            =       control_offset;
1796         game_fileinfo.matcen_offset             =       matcen_offset;
1797         game_fileinfo.dl_indices_offset =       dl_indices_offset;
1798         game_fileinfo.delta_light_offset        =       delta_light_offset;
1799
1800
1801         end_offset = ftell(SaveFile);
1802
1803         // Write the fileinfo
1804         fseek(  SaveFile, start_offset, SEEK_SET );  // Move to TOF
1805         fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
1806
1807         // Go back to end of data
1808         fseek(SaveFile,end_offset,SEEK_SET);
1809
1810         return 0;
1811 }
1812
1813 int save_mine_data(FILE * SaveFile);
1814
1815 // -----------------------------------------------------------------------------
1816 // Save game
1817 int save_level_sub(char * filename, int compiled_version)
1818 {
1819         FILE * SaveFile;
1820         char temp_filename[128];
1821         int sig = MAKE_SIG('P','L','V','L'),version=LEVEL_FILE_VERSION;
1822         int minedata_offset=0,gamedata_offset=0;
1823
1824         if ( !compiled_version )        {
1825                 write_game_text_file(filename);
1826
1827                 if (Errors_in_mine) {
1828                         if (is_real_level(filename)) {
1829                                 char  ErrorMessage[200];
1830         
1831                                 sprintf( ErrorMessage, "Warning: %i errors in this mine!\n", Errors_in_mine );
1832                                 stop_time();
1833                                 gr_palette_load(gr_palette);
1834          
1835                                 if (nm_messagebox( NULL, 2, "Cancel Save", "Save", ErrorMessage )!=1)   {
1836                                         start_time();
1837                                         return 1;
1838                                 }
1839                                 start_time();
1840                         } else
1841                                 mprintf((1, "Error: %i errors in this mine.  See the 'txm' file.\n", Errors_in_mine));
1842                 }
1843                 change_filename_extension(temp_filename,filename,".LVL");
1844         }
1845         else
1846         {
1847                 // macs are using the regular hog/rl2 files for shareware
1848                 #if defined(SHAREWARE) && !defined(MACINTOSH)
1849                         change_filename_extension(temp_filename,filename,".SL2");
1850                 #else
1851                         change_filename_extension(temp_filename,filename,".RL2");
1852                 #endif
1853         }
1854
1855         SaveFile = fopen( temp_filename, "wb" );
1856         if (!SaveFile)
1857         {
1858                 char ErrorMessage[256];
1859
1860                 char fname[20];
1861                 _splitpath( temp_filename, NULL, NULL, fname, NULL );
1862
1863                 sprintf( ErrorMessage, \
1864                         "ERROR: Cannot write to '%s'.\nYou probably need to check out a locked\nversion of the file. You should save\nthis under a different filename, and then\ncheck out a locked copy by typing\n\'co -l %s.lvl'\nat the DOS prompt.\n" 
1865                         , temp_filename, fname );
1866                 stop_time();
1867                 gr_palette_load(gr_palette);
1868                 nm_messagebox( NULL, 1, "Ok", ErrorMessage );
1869                 start_time();
1870                 return 1;
1871         }
1872
1873         if (Current_level_name[0] == 0)
1874                 strcpy(Current_level_name,"Untitled");
1875
1876         clear_transient_objects(1);             //1 means clear proximity bombs
1877
1878         compress_objects();             //after this, Highest_object_index == num objects
1879
1880         //make sure player is in a segment
1881         if (update_object_seg(&Objects[Players[0].objnum]) == 0) {
1882                 if (ConsoleObject->segnum > Highest_segment_index)
1883                         ConsoleObject->segnum = 0;
1884                 compute_segment_center(&ConsoleObject->pos,&(Segments[ConsoleObject->segnum]));
1885         }
1886  
1887         fix_object_segs();
1888
1889         //Write the header
1890
1891         gs_write_int(sig,SaveFile);
1892         gs_write_int(version,SaveFile);
1893
1894         //save placeholders
1895         gs_write_int(minedata_offset,SaveFile);
1896         gs_write_int(gamedata_offset,SaveFile);
1897
1898         //Now write the damn data
1899
1900         //write the version 8 data (to make file unreadable by 1.0 & 1.1)
1901         gs_write_int(GameTime,SaveFile);
1902         gs_write_short(FrameCount,SaveFile);
1903         gs_write_byte(FrameTime,SaveFile);
1904
1905         // Write the palette file name
1906         fprintf(SaveFile,"%s\n",Current_level_palette);
1907
1908         gs_write_int(Base_control_center_explosion_time,SaveFile);
1909         gs_write_int(Reactor_strength,SaveFile);
1910
1911         gs_write_int(Num_flickering_lights,SaveFile);
1912         fwrite(Flickering_lights,sizeof(*Flickering_lights),Num_flickering_lights,SaveFile);
1913         
1914         gs_write_int(Secret_return_segment, SaveFile);
1915         gs_write_int(Secret_return_orient.rvec.x, SaveFile);
1916         gs_write_int(Secret_return_orient.rvec.y, SaveFile);
1917         gs_write_int(Secret_return_orient.rvec.z, SaveFile);
1918         gs_write_int(Secret_return_orient.fvec.x, SaveFile);
1919         gs_write_int(Secret_return_orient.fvec.y, SaveFile);
1920         gs_write_int(Secret_return_orient.fvec.z, SaveFile);
1921         gs_write_int(Secret_return_orient.uvec.x, SaveFile);
1922         gs_write_int(Secret_return_orient.uvec.y, SaveFile);
1923         gs_write_int(Secret_return_orient.uvec.z, SaveFile);
1924
1925         minedata_offset = ftell(SaveFile);
1926         if ( !compiled_version )        
1927                 save_mine_data(SaveFile);
1928         else
1929                 save_mine_data_compiled(SaveFile);
1930         gamedata_offset = ftell(SaveFile);
1931         save_game_data(SaveFile);
1932
1933         fseek(SaveFile,sizeof(sig)+sizeof(version),SEEK_SET);
1934         gs_write_int(minedata_offset,SaveFile);
1935         gs_write_int(gamedata_offset,SaveFile);
1936
1937         //==================== CLOSE THE FILE =============================
1938         fclose(SaveFile);
1939
1940         if ( !compiled_version )        {
1941                 if (Function_mode == FMODE_EDITOR)
1942                         editor_status("Saved mine %s, \"%s\"",filename,Current_level_name);
1943         }
1944
1945         return 0;
1946
1947 }
1948
1949 #if 0 //dunno - 3rd party stuff?
1950 extern void compress_uv_coordinates_all(void);
1951 #endif
1952
1953 int save_level(char * filename)
1954 {
1955         int r1;
1956
1957         // Save normal version...
1958         r1 = save_level_sub(filename, 0);
1959
1960         // Save compiled version...
1961         save_level_sub(filename, 1);
1962
1963         return r1;
1964 }
1965
1966 #endif  //EDITOR
1967
1968 #ifndef NDEBUG
1969 void dump_mine_info(void)
1970 {
1971         int     segnum, sidenum;
1972         fix     min_u, max_u, min_v, max_v, min_l, max_l, max_sl;
1973
1974         min_u = F1_0*1000;
1975         min_v = min_u;
1976         min_l = min_u;
1977
1978         max_u = -min_u;
1979         max_v = max_u;
1980         max_l = max_u;
1981
1982         max_sl = 0;
1983
1984         for (segnum=0; segnum<=Highest_segment_index; segnum++) {
1985                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
1986                         int     vertnum;
1987                         side    *sidep = &Segments[segnum].sides[sidenum];
1988
1989                         if (Segment2s[segnum].static_light > max_sl)
1990                                 max_sl = Segment2s[segnum].static_light;
1991
1992                         for (vertnum=0; vertnum<4; vertnum++) {
1993                                 if (sidep->uvls[vertnum].u < min_u)
1994                                         min_u = sidep->uvls[vertnum].u;
1995                                 else if (sidep->uvls[vertnum].u > max_u)
1996                                         max_u = sidep->uvls[vertnum].u;
1997
1998                                 if (sidep->uvls[vertnum].v < min_v)
1999                                         min_v = sidep->uvls[vertnum].v;
2000                                 else if (sidep->uvls[vertnum].v > max_v)
2001                                         max_v = sidep->uvls[vertnum].v;
2002
2003                                 if (sidep->uvls[vertnum].l < min_l)
2004                                         min_l = sidep->uvls[vertnum].l;
2005                                 else if (sidep->uvls[vertnum].l > max_l)
2006                                         max_l = sidep->uvls[vertnum].l;
2007                         }
2008
2009                 }
2010         }
2011
2012 //      mprintf((0, "Smallest uvl = %7.3f %7.3f %7.3f.  Largest uvl = %7.3f %7.3f %7.3f\n", f2fl(min_u), f2fl(min_v), f2fl(min_l), f2fl(max_u), f2fl(max_v), f2fl(max_l)));
2013 //      mprintf((0, "Static light maximum = %7.3f\n", f2fl(max_sl)));
2014 //      mprintf((0, "Number of walls: %i\n", Num_walls));
2015
2016 }
2017
2018 #endif
2019
2020 #ifdef EDITOR
2021
2022 //read in every level in mission and save out compiled version 
2023 void save_all_compiled_levels(void)
2024 {
2025         do_load_save_levels(1);
2026 }
2027
2028 //read in every level in mission
2029 void load_all_levels(void)
2030 {
2031         do_load_save_levels(0);
2032 }
2033
2034
2035 void do_load_save_levels(int save)
2036 {
2037         int level_num;
2038
2039         if (! SafetyCheck())
2040                 return;
2041
2042         no_old_level_file_error=1;
2043
2044         for (level_num=1;level_num<=Last_level;level_num++) {
2045                 load_level(Level_names[level_num-1]);
2046                 load_palette(Current_level_palette,1,1);                //don't change screen
2047                 if (save)
2048                         save_level_sub(Level_names[level_num-1],1);
2049         }
2050
2051         for (level_num=-1;level_num>=Last_secret_level;level_num--) {
2052                 load_level(Secret_level_names[-level_num-1]);
2053                 load_palette(Current_level_palette,1,1);                //don't change screen
2054                 if (save)
2055                         save_level_sub(Secret_level_names[-level_num-1],1);
2056         }
2057
2058         no_old_level_file_error=0;
2059
2060 }
2061
2062 #endif