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