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