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