]> icculus.org git repositories - btb/d2x.git/blob - main/gamesave.c
remove duplicate file
[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.1.1.1 2001-01-19 03:29:59 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                                         if (game_top_fileinfo.fileinfo_version < 30) {
1247                                                 v29_trigger trig29;
1248                                                 int t;
1249         
1250                                                 if (cfread(&trig29, game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1251                                                         Error( "Error reading Triggers[%d] in gamesave.c", i);
1252         
1253                                                 trig.flags              = trig29.flags;
1254                                                 trig.num_links  = trig29.num_links;
1255                                                 trig.num_links  = trig29.num_links;
1256                                                 trig.value              = trig29.value;
1257                                                 trig.time               = trig29.time;
1258         
1259                                                 for (t=0;t<trig.num_links;t++) {
1260                                                         trig.seg[t]  = trig29.seg[t];
1261                                                         trig.side[t] = trig29.side[t];
1262                                                 }
1263                                         }
1264                                         else
1265                                                 if (cfread(&trig, game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1266                                                         Error( "Error reading Triggers[%d] in gamesave.c", i);
1267
1268                                         //Assert(trig.flags & TRIGGER_ON);
1269                                         trig.flags &= ~TRIGGER_ON;
1270
1271                                         if (trig.flags & TRIGGER_CONTROL_DOORS)
1272                                                 type = TT_OPEN_DOOR;
1273                                         else if (trig.flags & TRIGGER_SHIELD_DAMAGE)
1274                                                 Int3();
1275                                         else if (trig.flags & TRIGGER_ENERGY_DRAIN)
1276                                                 Int3();
1277                                         else if (trig.flags & TRIGGER_EXIT)
1278                                                 type = TT_EXIT;
1279                                         else if (trig.flags & TRIGGER_ONE_SHOT)
1280                                                 Int3();
1281                                         else if (trig.flags & TRIGGER_MATCEN)
1282                                                 type = TT_MATCEN;
1283                                         else if (trig.flags & TRIGGER_ILLUSION_OFF)
1284                                                 type = TT_ILLUSION_OFF;
1285                                         else if (trig.flags & TRIGGER_SECRET_EXIT)
1286                                                 type = TT_SECRET_EXIT;
1287                                         else if (trig.flags & TRIGGER_ILLUSION_ON)
1288                                                 type = TT_ILLUSION_ON;
1289                                         else if (trig.flags & TRIGGER_UNLOCK_DOORS)
1290                                                 type = TT_UNLOCK_DOOR;
1291                                         else if (trig.flags & TRIGGER_OPEN_WALL)
1292                                                 type = TT_OPEN_WALL;
1293                                         else if (trig.flags & TRIGGER_CLOSE_WALL)
1294                                                 type = TT_CLOSE_WALL;
1295                                         else if (trig.flags & TRIGGER_ILLUSORY_WALL)
1296                                                 type = TT_ILLUSORY_WALL;
1297                                         else
1298                                                 Int3();
1299
1300                                         Triggers[i].type                        = type;
1301                                         Triggers[i].flags                       = 0;
1302                                         Triggers[i].num_links   = trig.num_links;
1303                                         Triggers[i].num_links   = trig.num_links;
1304                                         Triggers[i].value                       = trig.value;
1305                                         Triggers[i].time                        = trig.time;
1306
1307                                         for (t=0;t<trig.num_links;t++) {
1308                                                 Triggers[i].seg[t] = trig.seg[t];
1309                                                 Triggers[i].side[t] = trig.side[t];
1310                                         }
1311                                 }
1312                                 else {
1313 #ifndef MACINTOSH
1314                                         if (cfread(&Triggers[i], game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1315                                                 Error( "Error reading Triggers[%d] in gamesave.c", i);
1316 #else
1317                                         Triggers[i].type = read_byte(LoadFile);
1318                                         Triggers[i].flags = read_byte(LoadFile);
1319                                         Triggers[i].num_links = read_byte(LoadFile);
1320                                         Triggers[i].pad = read_byte(LoadFile);
1321                                         Triggers[i].value = read_fix(LoadFile);
1322                                         Triggers[i].time = read_fix(LoadFile);
1323                                         for (j=0; j<MAX_WALLS_PER_LINK; j++ )   
1324                                                 Triggers[i].seg[j] = read_short(LoadFile);
1325                                         for (j=0; j<MAX_WALLS_PER_LINK; j++ )
1326                                                 Triggers[i].side[j] = read_short(LoadFile);
1327 #endif
1328                                 }
1329                 }
1330         }
1331
1332         //================ READ CONTROL CENTER TRIGGER INFO ===============
1333
1334         if (game_fileinfo.control_offset > -1)
1335         {
1336                 if (!cfseek( LoadFile, game_fileinfo.control_offset,SEEK_SET )) {
1337                         for (i=0;i<game_fileinfo.control_howmany;i++)
1338 #ifndef MACINTOSH
1339                                 if (cfread(&ControlCenterTriggers, game_fileinfo.control_sizeof,1,LoadFile)!=1)
1340                                         Error( "Error reading ControlCenterTriggers in gamesave.c");
1341 #else
1342                                 ControlCenterTriggers.num_links = read_short(LoadFile);
1343                                 for (j=0; j<MAX_CONTROLCEN_LINKS; j++ )
1344                                         ControlCenterTriggers.seg[j] = read_short( LoadFile );
1345                                 for (j=0; j<MAX_CONTROLCEN_LINKS; j++ )
1346                                         ControlCenterTriggers.side[j] = read_short( LoadFile );
1347 #endif
1348                 }
1349         }
1350
1351
1352         //================ READ MATERIALOGRIFIZATIONATORS INFO ===============
1353
1354         if (game_fileinfo.matcen_offset > -1)
1355         {       int     j;
1356
1357                 if (!cfseek( LoadFile, game_fileinfo.matcen_offset,SEEK_SET ))  {
1358                         // mprintf((0, "Reading %i materialization centers.\n", game_fileinfo.matcen_howmany));
1359                         for (i=0;i<game_fileinfo.matcen_howmany;i++) {
1360                                 if (game_top_fileinfo.fileinfo_version < 27) {
1361                                         old_matcen_info m;
1362                                         Assert(game_fileinfo.matcen_sizeof == sizeof(m));
1363                                         if (cfread(&m, game_fileinfo.matcen_sizeof,1,LoadFile)!=1)
1364                                                 Error( "Error reading RobotCenters in gamesave.c");
1365                                         RobotCenters[i].robot_flags[0] = m.robot_flags;
1366                                         RobotCenters[i].robot_flags[1] = 0;
1367                                         RobotCenters[i].hit_points = m.hit_points;
1368                                         RobotCenters[i].interval = m.interval;
1369                                         RobotCenters[i].segnum = m.segnum;
1370                                         RobotCenters[i].fuelcen_num = m.fuelcen_num;
1371                                 }
1372                                 else {
1373                                         Assert(game_fileinfo.matcen_sizeof == sizeof(RobotCenters[i]));
1374 #ifndef MACINTOSH
1375                                         if (cfread(&RobotCenters[i], game_fileinfo.matcen_sizeof,1,LoadFile)!=1)
1376                                                 Error( "Error reading RobotCenters in gamesave.c");
1377 #else
1378                                         RobotCenters[i].robot_flags[0] = read_int(LoadFile);
1379                                         RobotCenters[i].robot_flags[1] = read_int(LoadFile);
1380                                         RobotCenters[i].hit_points = read_fix(LoadFile);
1381                                         RobotCenters[i].interval = read_fix(LoadFile);
1382                                         RobotCenters[i].segnum = read_short(LoadFile);
1383                                         RobotCenters[i].fuelcen_num = read_short(LoadFile);
1384 #endif
1385                                 }
1386
1387                                 //      Set links in RobotCenters to Station array
1388
1389                                 for (j=0; j<=Highest_segment_index; j++)
1390                                         if (Segment2s[j].special == SEGMENT_IS_ROBOTMAKER)
1391                                                 if (Segment2s[j].matcen_num == i)
1392                                                         RobotCenters[i].fuelcen_num = Segment2s[j].value;
1393
1394                                 // mprintf((0, "   %i: flags = %08x\n", i, RobotCenters[i].robot_flags));
1395                         }
1396                 }
1397         }
1398
1399
1400         //================ READ DL_INDICES INFO ===============
1401
1402         Num_static_lights = 0;
1403
1404         if (game_fileinfo.dl_indices_offset > -1) {
1405                 int     i;
1406
1407                 if (!cfseek( LoadFile, game_fileinfo.dl_indices_offset, SEEK_SET ))     {
1408                         Num_static_lights = game_fileinfo.dl_indices_howmany;
1409                         for (i=0; i<game_fileinfo.dl_indices_howmany; i++) {
1410                                 if (game_top_fileinfo.fileinfo_version < 29) {
1411                                         mprintf((0, "Warning: Old mine version.  Not reading Dl_indices info.\n"));
1412                                         Int3(); //shouldn't be here!!!
1413                                 } else {
1414 #ifndef MACINTOSH
1415                                         if (cfread(&Dl_indices[i], game_fileinfo.dl_indices_sizeof, 1, LoadFile) != 1)
1416                                                 Error( "Error reading Dl_indices in gamesave.c");
1417 #else
1418                                         Dl_indices[i].segnum = read_short(LoadFile);                                    
1419                                         Dl_indices[i].sidenum = read_byte(LoadFile);
1420                                         Dl_indices[i].count = read_byte(LoadFile);
1421                                         Dl_indices[i].index = read_short(LoadFile);
1422 #endif
1423                                 }
1424
1425                         }
1426                 }
1427         }
1428
1429         //      Indicate that no light has been subtracted from any vertices.
1430         clear_light_subtracted();
1431
1432         //================ READ DELTA LIGHT INFO ===============
1433
1434         if (game_fileinfo.delta_light_offset > -1) {
1435                 int     i;
1436
1437                 if (!cfseek( LoadFile, game_fileinfo.delta_light_offset, SEEK_SET ))    {
1438                         for (i=0; i<game_fileinfo.delta_light_howmany; i++) {
1439                                 if (game_top_fileinfo.fileinfo_version < 29) {
1440                                         mprintf((0, "Warning: Old mine version.  Not reading delta light info.\n"));
1441                                 } else {
1442 #ifndef MACINTOSH
1443                                         if (cfread(&Delta_lights[i], game_fileinfo.delta_light_sizeof, 1, LoadFile) != 1)
1444                                                 Error( "Error reading Delta Lights in gamesave.c");
1445 #else
1446                                         Delta_lights[i].segnum = read_short(LoadFile);
1447                                         Delta_lights[i].sidenum = read_byte(LoadFile);
1448                                         Delta_lights[i].dummy = read_byte(LoadFile);
1449                                         Delta_lights[i].vert_light[0] = read_byte(LoadFile);
1450                                         Delta_lights[i].vert_light[1] = read_byte(LoadFile);
1451                                         Delta_lights[i].vert_light[2] = read_byte(LoadFile);
1452                                         Delta_lights[i].vert_light[3] = read_byte(LoadFile);
1453 #endif
1454                                 }
1455
1456                         }
1457                 }
1458         }
1459
1460         //========================= UPDATE VARIABLES ======================
1461
1462         reset_objects(game_fileinfo.object_howmany);
1463
1464         for (i=0; i<MAX_OBJECTS; i++) {
1465                 Objects[i].next = Objects[i].prev = -1;
1466                 if (Objects[i].type != OBJ_NONE) {
1467                         int objsegnum = Objects[i].segnum;
1468
1469                         if (objsegnum > Highest_segment_index)          //bogus object
1470                                 Objects[i].type = OBJ_NONE;
1471                         else {
1472                                 Objects[i].segnum = -1;                 //avoid Assert()
1473                                 obj_link(i,objsegnum);
1474                         }
1475                 }
1476         }
1477
1478         clear_transient_objects(1);             //1 means clear proximity bombs
1479
1480         // Make sure non-transparent doors are set correctly.
1481         for (i=0; i< Num_segments; i++)
1482                 for (j=0;j<MAX_SIDES_PER_SEGMENT;j++) {
1483                         side    *sidep = &Segments[i].sides[j];
1484                         if ((sidep->wall_num != -1) && (Walls[sidep->wall_num].clip_num != -1)) {
1485                                 //mprintf((0, "Checking Wall %d\n", Segments[i].sides[j].wall_num));
1486                                 if (WallAnims[Walls[sidep->wall_num].clip_num].flags & WCF_TMAP1) {
1487                                         //mprintf((0, "Fixing non-transparent door.\n"));
1488                                         sidep->tmap_num = WallAnims[Walls[sidep->wall_num].clip_num].frames[0];
1489                                         sidep->tmap_num2 = 0;
1490                                 }
1491                         }
1492                 }
1493
1494
1495         Num_walls = game_fileinfo.walls_howmany;
1496         reset_walls();
1497
1498         Num_open_doors = game_fileinfo.doors_howmany;
1499         Num_triggers = game_fileinfo.triggers_howmany;
1500
1501         //go through all walls, killing references to invalid triggers
1502         for (i=0;i<Num_walls;i++)
1503                 if (Walls[i].trigger >= Num_triggers) {
1504                         mprintf((0,"Removing reference to invalid trigger %d from wall %d\n",Walls[i].trigger,i));
1505                         Walls[i].trigger = -1;  //kill trigger
1506                 }
1507
1508         //go through all triggers, killing unused ones
1509         for (i=0;i<Num_triggers;) {
1510                 int w;
1511
1512                 //      Find which wall this trigger is connected to.
1513                 for (w=0; w<Num_walls; w++)
1514                         if (Walls[w].trigger == i)
1515                                 break;
1516
1517         #ifdef EDITOR
1518                 if (w == Num_walls) {
1519                         mprintf((0,"Removing unreferenced trigger %d\n",i));
1520                         remove_trigger_num(i);
1521                 }
1522                 else
1523         #endif
1524                         i++;
1525         }
1526
1527         //      MK, 10/17/95: Make walls point back at the triggers that control them.
1528         //      Go through all triggers, stuffing controlling_trigger field in Walls.
1529         {       int t;
1530
1531         for (i=0; i<Num_walls; i++)
1532                 Walls[i].controlling_trigger = -1;
1533
1534         for (t=0; t<Num_triggers; t++) {
1535                 int     l;
1536                 for (l=0; l<Triggers[t].num_links; l++) {
1537                         int     seg_num, side_num, wall_num;
1538
1539                         seg_num = Triggers[t].seg[l];
1540                         side_num = Triggers[t].side[l];
1541                         wall_num = Segments[seg_num].sides[side_num].wall_num;
1542
1543                         // -- if (Walls[wall_num].controlling_trigger != -1)
1544                         // --   Int3();
1545
1546                         //check to see that if a trigger requires a wall that it has one,
1547                         //and if it requires a matcen that it has one
1548
1549                         if (Triggers[t].type == TT_MATCEN) {
1550                                 if (Segment2s[seg_num].special != SEGMENT_IS_ROBOTMAKER)
1551                                         Int3();         //matcen trigger doesn't point to matcen
1552                         }
1553                         else if (Triggers[t].type != TT_LIGHT_OFF && Triggers[t].type != TT_LIGHT_ON) { //light triggers don't require walls
1554                                 if (wall_num == -1)
1555                                         Int3(); //      This is illegal.  This trigger requires a wall
1556                                 else
1557                                         Walls[wall_num].controlling_trigger = t;
1558                         }
1559                 }
1560         }
1561         }
1562
1563         Num_robot_centers = game_fileinfo.matcen_howmany;
1564
1565         //fix old wall structs
1566         if (game_top_fileinfo.fileinfo_version < 17) {
1567                 int segnum,sidenum,wallnum;
1568
1569                 for (segnum=0; segnum<=Highest_segment_index; segnum++)
1570                         for (sidenum=0;sidenum<6;sidenum++)
1571                                 if ((wallnum=Segments[segnum].sides[sidenum].wall_num) != -1) {
1572                                         Walls[wallnum].segnum = segnum;
1573                                         Walls[wallnum].sidenum = sidenum;
1574                                 }
1575         }
1576
1577         #ifndef NDEBUG
1578         {
1579                 int     sidenum;
1580                 for (sidenum=0; sidenum<6; sidenum++) {
1581                         int     wallnum = Segments[Highest_segment_index].sides[sidenum].wall_num;
1582                         if (wallnum != -1)
1583                                 if ((Walls[wallnum].segnum != Highest_segment_index) || (Walls[wallnum].sidenum != sidenum))
1584                                         Int3(); //      Error.  Bogus walls in this segment.
1585                                                                 // Consult Yuan or Mike.
1586                 }
1587         }
1588         #endif
1589
1590         //create_local_segment_data();
1591
1592         fix_object_segs();
1593
1594         #ifndef NDEBUG
1595         dump_mine_info();
1596         #endif
1597
1598         if (game_top_fileinfo.fileinfo_version < GAME_VERSION && !(game_top_fileinfo.fileinfo_version==25 && GAME_VERSION==26))
1599                 return 1;               //means old version
1600         else
1601                 return 0;
1602 }
1603
1604
1605 int check_segment_connections(void);
1606
1607 extern void     set_ambient_sound_flags(void);
1608
1609 // -----------------------------------------------------------------------------
1610 //loads from an already-open file
1611 // returns 0=everything ok, 1=old version, -1=error
1612 int load_mine_data(CFILE *LoadFile);
1613 int load_mine_data_compiled(CFILE *LoadFile);
1614
1615 #define LEVEL_FILE_VERSION              8
1616 //1 -> 2  add palette name
1617 //2 -> 3  add control center explosion time
1618 //3 -> 4  add reactor strength
1619 //4 -> 5  killed hostage text stuff
1620 //5 -> 6  added Secret_return_segment and Secret_return_orient
1621 //6 -> 7         added flickering lights
1622 //7 -> 8  made version 8 to be not compatible with D2 1.0 & 1.1
1623
1624 #ifndef RELEASE
1625 char *Level_being_loaded=NULL;
1626 #endif
1627
1628 #ifdef COMPACT_SEGS
1629 extern void ncache_flush();
1630 #endif
1631
1632 extern int HoardEquipped();
1633
1634 extern  int     Slide_segs_computed;
1635
1636 int no_old_level_file_error=0;
1637
1638 //loads a level (.LVL) file from disk
1639 //returns 0 if success, else error code
1640 int load_level(char * filename_passed)
1641 {
1642         #ifdef EDITOR
1643         int use_compiled_level=1;
1644         #endif
1645         CFILE * LoadFile;
1646         char filename[128];
1647         int version,minedata_offset,gamedata_offset;
1648         int mine_err,game_err,i;
1649         char sig[4];
1650
1651         Slide_segs_computed = 0;
1652
1653 #ifdef NETWORK
1654    if (Game_mode & GM_NETWORK)
1655          {
1656           for (i=0;i<MAX_POWERUP_TYPES;i++)
1657                 {
1658                         MaxPowerupsAllowed[i]=0;
1659                         PowerupsInMine[i]=0;
1660                 }
1661          }
1662 #endif
1663
1664         #ifdef COMPACT_SEGS
1665         ncache_flush();
1666         #endif
1667
1668         #ifndef RELEASE
1669         Level_being_loaded = filename_passed;
1670         #endif
1671
1672         strcpy(filename,filename_passed);
1673         strupr(filename);
1674
1675         #ifdef EDITOR
1676                 //if we have the editor, try the LVL first, no matter what was passed.
1677                 //if we don't have an LVL, try RDL  
1678                 //if we don't have the editor, we just use what was passed
1679         
1680                 change_filename_extension(filename,filename_passed,".LVL");
1681                 use_compiled_level = 0;
1682         
1683                 if (!cfexist(filename)) {
1684                         change_filename_extension(filename,filename,".RL2");
1685                         use_compiled_level = 1;
1686                 }               
1687         #endif
1688
1689         LoadFile = cfopen( filename, "rb" );
1690
1691         if (!LoadFile)  {
1692                 #ifdef EDITOR
1693                         mprintf((0,"Can't open level file <%s>\n", filename));
1694                         return 1;
1695                 #else
1696                         Error("Can't open file <%s>\n",filename);
1697                 #endif
1698         }
1699
1700         strcpy( Gamesave_current_filename, filename );
1701
1702 //      #ifdef NEWDEMO
1703 //      if ( Newdemo_state == ND_STATE_RECORDING )
1704 //              newdemo_record_start_demo();
1705 //      #endif
1706
1707         cfread(sig, 1, 4, LoadFile);
1708         version                         = read_int(LoadFile);
1709         minedata_offset         = read_int(LoadFile);
1710         gamedata_offset         = read_int(LoadFile);
1711
1712         Assert((sig[0] == 'L') && (sig[1] == 'V') && (sig[2]=='L') && (sig[3] == 'P'));
1713
1714         if (version >= 8) {                     //read dummy data
1715 #ifdef NETWORK
1716                 if (HoardEquipped())
1717                 {
1718                         read_int(LoadFile);
1719                         read_short(LoadFile);
1720                         read_byte(LoadFile);
1721                 }
1722                 else
1723 #endif
1724                         // NOTE LINK TO ABOVE!
1725                         Error("This level requires the Vertigo Enhanced version of D2.");
1726
1727         }
1728
1729         if (version < 5)
1730                 read_int(LoadFile);             //was hostagetext_offset
1731
1732         if (version > 1) {
1733                 cfgets(Current_level_palette,sizeof(Current_level_palette),LoadFile);
1734                 if (Current_level_palette[strlen(Current_level_palette)-1] == '\n')
1735                         Current_level_palette[strlen(Current_level_palette)-1] = 0;
1736         }
1737
1738         if (version >= 3)
1739                 Base_control_center_explosion_time = read_int(LoadFile);
1740         else
1741                 Base_control_center_explosion_time = DEFAULT_CONTROL_CENTER_EXPLOSION_TIME;
1742                 
1743         if (version >= 4)
1744                 Reactor_strength = read_int(LoadFile);
1745         else
1746                 Reactor_strength = -1;  //use old defaults
1747
1748         if (version >= 7) {
1749                 Num_flickering_lights = read_int(LoadFile);
1750                 #ifdef MACINTOSH
1751                         Assert((Num_flickering_lights >= 0) && (Num_flickering_lights < MAX_FLICKERING_LIGHTS));
1752                         for (i = 0; i < Num_flickering_lights; i++)
1753                         {
1754                                 Flickering_lights[i].segnum     = read_short(LoadFile);
1755                                 Flickering_lights[i].sidenum    = read_short(LoadFile);
1756                                 Flickering_lights[i].mask               = read_int(LoadFile);
1757                                 Flickering_lights[i].timer              = read_fix(LoadFile);
1758                                 Flickering_lights[i].delay              = read_fix(LoadFile);
1759                         }
1760                 #else
1761                         cfread(Flickering_lights,sizeof(*Flickering_lights),Num_flickering_lights,LoadFile);
1762                 #endif
1763         }
1764         else
1765                 Num_flickering_lights = 0;
1766
1767         if (version <= 1 || Current_level_palette[0]==0)
1768                 strcpy(Current_level_palette,"groupa.256");
1769
1770         if (version < 6) {
1771                 Secret_return_segment = 0;
1772                 Secret_return_orient.rvec.x = F1_0;     Secret_return_orient.rvec.y = 0;                        Secret_return_orient.rvec.z = 0;
1773                 Secret_return_orient.fvec.x =    0;     Secret_return_orient.fvec.y = F1_0;             Secret_return_orient.fvec.z = 0;
1774                 Secret_return_orient.uvec.x =    0;     Secret_return_orient.uvec.y = 0;                        Secret_return_orient.uvec.z = F1_0;
1775         } else {
1776                 Secret_return_segment = read_int(LoadFile);
1777                 Secret_return_orient.rvec.x = read_int(LoadFile);
1778                 Secret_return_orient.rvec.y = read_int(LoadFile);
1779                 Secret_return_orient.rvec.z = read_int(LoadFile);
1780                 Secret_return_orient.fvec.x = read_int(LoadFile);
1781                 Secret_return_orient.fvec.y = read_int(LoadFile);
1782                 Secret_return_orient.fvec.z = read_int(LoadFile);
1783                 Secret_return_orient.uvec.x = read_int(LoadFile);
1784                 Secret_return_orient.uvec.y = read_int(LoadFile);
1785                 Secret_return_orient.uvec.z = read_int(LoadFile);
1786         }
1787
1788         cfseek(LoadFile,minedata_offset,SEEK_SET);
1789         #ifdef EDITOR
1790         if (!use_compiled_level) {
1791                 mine_err = load_mine_data(LoadFile);
1792                 //      Compress all uv coordinates in mine, improves texmap precision. --MK, 02/19/96
1793                 compress_uv_coordinates_all();
1794         } else
1795         #endif
1796                 //NOTE LINK TO ABOVE!!
1797                 mine_err = load_mine_data_compiled(LoadFile);
1798
1799         if (mine_err == -1) {   //error!!
1800                 cfclose(LoadFile);
1801                 return 2;
1802         }
1803
1804         cfseek(LoadFile,gamedata_offset,SEEK_SET);
1805         game_err = load_game_data(LoadFile);
1806
1807         if (game_err == -1) {   //error!!
1808                 cfclose(LoadFile);
1809                 return 3;
1810         }
1811
1812         //======================== CLOSE FILE =============================
1813
1814         cfclose( LoadFile );
1815
1816         set_ambient_sound_flags();
1817
1818         #ifdef EDITOR
1819         write_game_text_file(filename);
1820         if (Errors_in_mine) {
1821                 if (is_real_level(filename)) {
1822                         char  ErrorMessage[200];
1823
1824                         sprintf( ErrorMessage, "Warning: %i errors in %s!\n", Errors_in_mine, Level_being_loaded );
1825                         stop_time();
1826                         gr_palette_load(gr_palette);
1827                         nm_messagebox( NULL, 1, "Continue", ErrorMessage );
1828                         start_time();
1829                 } else
1830                         mprintf((1, "Error: %i errors in %s.\n", Errors_in_mine, Level_being_loaded));
1831         }
1832         #endif
1833
1834         #ifdef EDITOR
1835         //If an old version, ask the use if he wants to save as new version
1836         if (!no_old_level_file_error && (Function_mode == FMODE_EDITOR) && (((LEVEL_FILE_VERSION>3) && version<LEVEL_FILE_VERSION) || mine_err==1 || game_err==1)) {
1837                 char  ErrorMessage[200];
1838
1839                 sprintf( ErrorMessage, 
1840                                         "You just loaded a old version\n"
1841                                         "level.  Would you like to save\n"
1842                                         "it as a current version level?");
1843
1844                 stop_time();
1845                 gr_palette_load(gr_palette);
1846                 if (nm_messagebox( NULL, 2, "Don't Save", "Save", ErrorMessage )==1)
1847                         save_level(filename);
1848                 start_time();
1849         }
1850         #endif
1851
1852         #ifdef EDITOR
1853         if (Function_mode == FMODE_EDITOR)
1854                 editor_status("Loaded NEW mine %s, \"%s\"",filename,Current_level_name);
1855         #endif
1856
1857         #ifdef EDITOR
1858         if (check_segment_connections())
1859                 nm_messagebox( "ERROR", 1, "Ok", 
1860                                 "Connectivity errors detected in\n"
1861                                 "mine.  See monochrome screen for\n"
1862                                 "details, and contact Matt or Mike." );
1863         #endif
1864
1865         return 0;
1866 }
1867
1868 #ifdef EDITOR
1869 void get_level_name()
1870 {
1871 //NO_UI!!!      UI_WINDOW                               *NameWindow = NULL;
1872 //NO_UI!!!      UI_GADGET_INPUTBOX      *NameText;
1873 //NO_UI!!!      UI_GADGET_BUTTON                *QuitButton;
1874 //NO_UI!!!
1875 //NO_UI!!!      // Open a window with a quit button
1876 //NO_UI!!!      NameWindow = ui_open_window( 20, 20, 300, 110, WIN_DIALOG );
1877 //NO_UI!!!      QuitButton = ui_add_gadget_button( NameWindow, 150-24, 60, 48, 40, "Done", NULL );
1878 //NO_UI!!!
1879 //NO_UI!!!      ui_wprintf_at( NameWindow, 10, 12,"Please enter a name for this mine:" );
1880 //NO_UI!!!      NameText = ui_add_gadget_inputbox( NameWindow, 10, 30, LEVEL_NAME_LEN, LEVEL_NAME_LEN, Current_level_name );
1881 //NO_UI!!!
1882 //NO_UI!!!      NameWindow->keyboard_focus_gadget = (UI_GADGET *)NameText;
1883 //NO_UI!!!      QuitButton->hotkey = KEY_ENTER;
1884 //NO_UI!!!
1885 //NO_UI!!!      ui_gadget_calc_keys(NameWindow);
1886 //NO_UI!!!
1887 //NO_UI!!!      while (!QuitButton->pressed && last_keypress!=KEY_ENTER) {
1888 //NO_UI!!!              ui_mega_process();
1889 //NO_UI!!!              ui_window_do_gadgets(NameWindow);
1890 //NO_UI!!!      }
1891 //NO_UI!!!
1892 //NO_UI!!!      strcpy( Current_level_name, NameText->text );
1893 //NO_UI!!!
1894 //NO_UI!!!      if ( NameWindow!=NULL ) {
1895 //NO_UI!!!              ui_close_window( NameWindow );
1896 //NO_UI!!!              NameWindow = NULL;
1897 //NO_UI!!!      }
1898 //NO_UI!!!
1899
1900         newmenu_item m[2];
1901
1902         m[0].type = NM_TYPE_TEXT; m[0].text = "Please enter a name for this mine:";
1903         m[1].type = NM_TYPE_INPUT; m[1].text = Current_level_name; m[1].text_len = LEVEL_NAME_LEN;
1904
1905         newmenu_do( NULL, "Enter mine name", 2, m, NULL );
1906
1907 }
1908 #endif
1909
1910
1911 #ifdef EDITOR
1912
1913 int     Errors_in_mine;
1914
1915 // -----------------------------------------------------------------------------
1916 int compute_num_delta_light_records(void)
1917 {
1918         int     i;
1919         int     total = 0;
1920
1921         for (i=0; i<Num_static_lights; i++) {
1922                 total += Dl_indices[i].count;
1923         }
1924
1925         return total;
1926
1927 }
1928
1929 // -----------------------------------------------------------------------------
1930 // Save game
1931 int save_game_data(FILE * SaveFile)
1932 {
1933         int  player_offset, object_offset, walls_offset, doors_offset, triggers_offset, control_offset, matcen_offset; //, links_offset;
1934         int     dl_indices_offset, delta_light_offset;
1935         int start_offset,end_offset;
1936
1937         start_offset = ftell(SaveFile);
1938
1939         //===================== SAVE FILE INFO ========================
1940
1941         game_fileinfo.fileinfo_signature =      0x6705;
1942         game_fileinfo.fileinfo_version  =       GAME_VERSION;
1943         game_fileinfo.level                                     =  Current_level_num;
1944         game_fileinfo.fileinfo_sizeof           =       sizeof(game_fileinfo);
1945         game_fileinfo.player_offset             =       -1;
1946         game_fileinfo.player_sizeof             =       sizeof(player);
1947         game_fileinfo.object_offset             =       -1;
1948         game_fileinfo.object_howmany            =       Highest_object_index+1;
1949         game_fileinfo.object_sizeof             =       sizeof(object);
1950         game_fileinfo.walls_offset                      =       -1;
1951         game_fileinfo.walls_howmany             =       Num_walls;
1952         game_fileinfo.walls_sizeof                      =       sizeof(wall);
1953         game_fileinfo.doors_offset                      =       -1;
1954         game_fileinfo.doors_howmany             =       Num_open_doors;
1955         game_fileinfo.doors_sizeof                      =       sizeof(active_door);
1956         game_fileinfo.triggers_offset           =       -1;
1957         game_fileinfo.triggers_howmany  =       Num_triggers;
1958         game_fileinfo.triggers_sizeof           =       sizeof(trigger);
1959         game_fileinfo.control_offset            =       -1;
1960         game_fileinfo.control_howmany           =  1;
1961         game_fileinfo.control_sizeof            =  sizeof(control_center_triggers);
1962         game_fileinfo.matcen_offset             =       -1;
1963         game_fileinfo.matcen_howmany            =       Num_robot_centers;
1964         game_fileinfo.matcen_sizeof             =       sizeof(matcen_info);
1965
1966         game_fileinfo.dl_indices_offset         =       -1;
1967         game_fileinfo.dl_indices_howmany                =       Num_static_lights;
1968         game_fileinfo.dl_indices_sizeof         =       sizeof(dl_index);
1969
1970         game_fileinfo.delta_light_offset                =       -1;
1971         game_fileinfo.delta_light_howmany       =       compute_num_delta_light_records();
1972         game_fileinfo.delta_light_sizeof                =       sizeof(delta_light);
1973
1974         // Write the fileinfo
1975         fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
1976
1977         // Write the mine name
1978         fprintf(SaveFile,"%s\n",Current_level_name);
1979
1980         fwrite(&N_polygon_models,2,1,SaveFile);
1981         fwrite(Pof_names,N_polygon_models,sizeof(*Pof_names),SaveFile);
1982
1983         //==================== SAVE PLAYER INFO ===========================
1984
1985         player_offset = ftell(SaveFile);
1986         fwrite( &Players[Player_num], sizeof(player), 1, SaveFile );
1987
1988         //==================== SAVE OBJECT INFO ===========================
1989
1990         object_offset = ftell(SaveFile);
1991         //fwrite( &Objects, sizeof(object), game_fileinfo.object_howmany, SaveFile );
1992         {
1993                 int i;
1994                 for (i=0;i<game_fileinfo.object_howmany;i++)
1995                         write_object(&Objects[i],SaveFile);
1996         }
1997
1998         //==================== SAVE WALL INFO =============================
1999
2000         walls_offset = ftell(SaveFile);
2001         fwrite( Walls, sizeof(wall), game_fileinfo.walls_howmany, SaveFile );
2002
2003         //==================== SAVE DOOR INFO =============================
2004
2005         doors_offset = ftell(SaveFile);
2006         fwrite( ActiveDoors, sizeof(active_door), game_fileinfo.doors_howmany, SaveFile );
2007
2008         //==================== SAVE TRIGGER INFO =============================
2009
2010         triggers_offset = ftell(SaveFile);
2011         fwrite( Triggers, sizeof(trigger), game_fileinfo.triggers_howmany, SaveFile );
2012
2013         //================ SAVE CONTROL CENTER TRIGGER INFO ===============
2014
2015         control_offset = ftell(SaveFile);
2016         fwrite( &ControlCenterTriggers, sizeof(control_center_triggers), 1, SaveFile );
2017
2018
2019         //================ SAVE MATERIALIZATION CENTER TRIGGER INFO ===============
2020
2021         matcen_offset = ftell(SaveFile);
2022         // mprintf((0, "Writing %i materialization centers\n", game_fileinfo.matcen_howmany));
2023         // { int i;
2024         // for (i=0; i<game_fileinfo.matcen_howmany; i++)
2025         //      mprintf((0, "   %i: robot_flags = %08x\n", i, RobotCenters[i].robot_flags));
2026         // }
2027         fwrite( RobotCenters, sizeof(matcen_info), game_fileinfo.matcen_howmany, SaveFile );
2028
2029         //================ SAVE DELTA LIGHT INFO ===============
2030         dl_indices_offset = ftell(SaveFile);
2031         fwrite( Dl_indices, sizeof(dl_index), game_fileinfo.dl_indices_howmany, SaveFile );
2032
2033         delta_light_offset = ftell(SaveFile);
2034         fwrite( Delta_lights, sizeof(delta_light), game_fileinfo.delta_light_howmany, SaveFile );
2035
2036         //============= REWRITE FILE INFO, TO SAVE OFFSETS ===============
2037
2038         // Update the offset fields
2039         game_fileinfo.player_offset             =       player_offset;
2040         game_fileinfo.object_offset             =       object_offset;
2041         game_fileinfo.walls_offset                      =       walls_offset;
2042         game_fileinfo.doors_offset                      =       doors_offset;
2043         game_fileinfo.triggers_offset           =       triggers_offset;
2044         game_fileinfo.control_offset            =       control_offset;
2045         game_fileinfo.matcen_offset             =       matcen_offset;
2046         game_fileinfo.dl_indices_offset =       dl_indices_offset;
2047         game_fileinfo.delta_light_offset        =       delta_light_offset;
2048
2049
2050         end_offset = ftell(SaveFile);
2051
2052         // Write the fileinfo
2053         fseek(  SaveFile, start_offset, SEEK_SET );  // Move to TOF
2054         fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
2055
2056         // Go back to end of data
2057         fseek(SaveFile,end_offset,SEEK_SET);
2058
2059         return 0;
2060 }
2061
2062 int save_mine_data(FILE * SaveFile);
2063
2064 // -----------------------------------------------------------------------------
2065 // Save game
2066 int save_level_sub(char * filename, int compiled_version)
2067 {
2068         FILE * SaveFile;
2069         char temp_filename[128];
2070         int sig = 'PLVL',version=LEVEL_FILE_VERSION;
2071         int minedata_offset,gamedata_offset;
2072
2073         if ( !compiled_version )        {
2074                 write_game_text_file(filename);
2075
2076                 if (Errors_in_mine) {
2077                         if (is_real_level(filename)) {
2078                                 char  ErrorMessage[200];
2079         
2080                                 sprintf( ErrorMessage, "Warning: %i errors in this mine!\n", Errors_in_mine );
2081                                 stop_time();
2082                                 gr_palette_load(gr_palette);
2083          
2084                                 if (nm_messagebox( NULL, 2, "Cancel Save", "Save", ErrorMessage )!=1)   {
2085                                         start_time();
2086                                         return 1;
2087                                 }
2088                                 start_time();
2089                         } else
2090                                 mprintf((1, "Error: %i errors in this mine.  See the 'txm' file.\n", Errors_in_mine));
2091                 }
2092                 change_filename_extension(temp_filename,filename,".LVL");
2093         }
2094         else
2095         {
2096                 // macs are using the regular hog/rl2 files for shareware
2097                 #if defined(SHAREWARE) && !defined(MACINTOSH)
2098                         change_filename_extension(temp_filename,filename,".SL2");
2099                 #else           
2100                         change_filename_extension(temp_filename,filename,".RL2");
2101                 #endif
2102         }
2103
2104         SaveFile = fopen( temp_filename, "wb" );
2105         if (!SaveFile)
2106         {
2107                 char ErrorMessage[256];
2108
2109                 char fname[20];
2110                 _splitpath( temp_filename, NULL, NULL, fname, NULL );
2111
2112                 sprintf( ErrorMessage, \
2113                         "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" 
2114                         , temp_filename, fname );
2115                 stop_time();
2116                 gr_palette_load(gr_palette);
2117                 nm_messagebox( NULL, 1, "Ok", ErrorMessage );
2118                 start_time();
2119                 return 1;
2120         }
2121
2122         if (Current_level_name[0] == 0)
2123                 strcpy(Current_level_name,"Untitled");
2124
2125         clear_transient_objects(1);             //1 means clear proximity bombs
2126
2127         compress_objects();             //after this, Highest_object_index == num objects
2128
2129         //make sure player is in a segment
2130         if (update_object_seg(&Objects[Players[0].objnum]) == 0) {
2131                 if (ConsoleObject->segnum > Highest_segment_index)
2132                         ConsoleObject->segnum = 0;
2133                 compute_segment_center(&ConsoleObject->pos,&(Segments[ConsoleObject->segnum]));
2134         }
2135  
2136         fix_object_segs();
2137
2138         //Write the header
2139
2140         gs_write_int(sig,SaveFile);
2141         gs_write_int(version,SaveFile);
2142
2143         //save placeholders
2144         gs_write_int(minedata_offset,SaveFile);
2145         gs_write_int(gamedata_offset,SaveFile);
2146
2147         //Now write the damn data
2148
2149         //write the version 8 data (to make file unreadable by 1.0 & 1.1)
2150         gs_write_int(GameTime,SaveFile);
2151         gs_write_short(FrameCount,SaveFile);
2152         gs_write_byte(FrameTime,SaveFile);
2153
2154         // Write the palette file name
2155         fprintf(SaveFile,"%s\n",Current_level_palette);
2156
2157         gs_write_int(Base_control_center_explosion_time,SaveFile);
2158         gs_write_int(Reactor_strength,SaveFile);
2159
2160         gs_write_int(Num_flickering_lights,SaveFile);
2161         fwrite(Flickering_lights,sizeof(*Flickering_lights),Num_flickering_lights,SaveFile);
2162         
2163         gs_write_int(Secret_return_segment, SaveFile);
2164         gs_write_int(Secret_return_orient.rvec.x, SaveFile);
2165         gs_write_int(Secret_return_orient.rvec.y, SaveFile);
2166         gs_write_int(Secret_return_orient.rvec.z, SaveFile);
2167         gs_write_int(Secret_return_orient.fvec.x, SaveFile);
2168         gs_write_int(Secret_return_orient.fvec.y, SaveFile);
2169         gs_write_int(Secret_return_orient.fvec.z, SaveFile);
2170         gs_write_int(Secret_return_orient.uvec.x, SaveFile);
2171         gs_write_int(Secret_return_orient.uvec.y, SaveFile);
2172         gs_write_int(Secret_return_orient.uvec.z, SaveFile);
2173
2174         minedata_offset = ftell(SaveFile);
2175         if ( !compiled_version )        
2176                 save_mine_data(SaveFile);
2177         else
2178                 save_mine_data_compiled(SaveFile);
2179         gamedata_offset = ftell(SaveFile);
2180         save_game_data(SaveFile);
2181
2182         fseek(SaveFile,sizeof(sig)+sizeof(version),SEEK_SET);
2183         gs_write_int(minedata_offset,SaveFile);
2184         gs_write_int(gamedata_offset,SaveFile);
2185
2186         //==================== CLOSE THE FILE =============================
2187         fclose(SaveFile);
2188
2189         if ( !compiled_version )        {
2190                 if (Function_mode == FMODE_EDITOR)
2191                         editor_status("Saved mine %s, \"%s\"",filename,Current_level_name);
2192         }
2193
2194         return 0;
2195
2196 }
2197
2198 extern void compress_uv_coordinates_all(void);
2199
2200 int save_level(char * filename)
2201 {
2202         int r1;
2203
2204         // Save normal version...
2205         r1 = save_level_sub(filename, 0);
2206
2207         // Save compiled version...
2208         save_level_sub(filename, 1);
2209
2210         return r1;
2211 }
2212
2213 #endif  //EDITOR
2214
2215 #ifndef NDEBUG
2216 void dump_mine_info(void)
2217 {
2218         int     segnum, sidenum;
2219         fix     min_u, max_u, min_v, max_v, min_l, max_l, max_sl;
2220
2221         min_u = F1_0*1000;
2222         min_v = min_u;
2223         min_l = min_u;
2224
2225         max_u = -min_u;
2226         max_v = max_u;
2227         max_l = max_u;
2228
2229         max_sl = 0;
2230
2231         for (segnum=0; segnum<=Highest_segment_index; segnum++) {
2232                 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
2233                         int     vertnum;
2234                         side    *sidep = &Segments[segnum].sides[sidenum];
2235
2236                         if (Segment2s[segnum].static_light > max_sl)
2237                                 max_sl = Segment2s[segnum].static_light;
2238
2239                         for (vertnum=0; vertnum<4; vertnum++) {
2240                                 if (sidep->uvls[vertnum].u < min_u)
2241                                         min_u = sidep->uvls[vertnum].u;
2242                                 else if (sidep->uvls[vertnum].u > max_u)
2243                                         max_u = sidep->uvls[vertnum].u;
2244
2245                                 if (sidep->uvls[vertnum].v < min_v)
2246                                         min_v = sidep->uvls[vertnum].v;
2247                                 else if (sidep->uvls[vertnum].v > max_v)
2248                                         max_v = sidep->uvls[vertnum].v;
2249
2250                                 if (sidep->uvls[vertnum].l < min_l)
2251                                         min_l = sidep->uvls[vertnum].l;
2252                                 else if (sidep->uvls[vertnum].l > max_l)
2253                                         max_l = sidep->uvls[vertnum].l;
2254                         }
2255
2256                 }
2257         }
2258
2259 //      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)));
2260 //      mprintf((0, "Static light maximum = %7.3f\n", f2fl(max_sl)));
2261 //      mprintf((0, "Number of walls: %i\n", Num_walls));
2262
2263 }
2264
2265 #endif
2266
2267 #ifdef EDITOR
2268
2269 //read in every level in mission and save out compiled version 
2270 void save_all_compiled_levels(void)
2271 {
2272         do_load_save_levels(1);
2273 }
2274
2275 //read in every level in mission
2276 void load_all_levels(void)
2277 {
2278         do_load_save_levels(0);
2279 }
2280
2281
2282 void do_load_save_levels(int save)
2283 {
2284         int level_num;
2285
2286         if (! SafetyCheck())
2287                 return;
2288
2289         no_old_level_file_error=1;
2290
2291         for (level_num=1;level_num<=Last_level;level_num++) {
2292                 load_level(Level_names[level_num-1]);
2293                 load_palette(Current_level_palette,1,1);                //don't change screen
2294                 if (save)
2295                         save_level_sub(Level_names[level_num-1],1);
2296         }
2297
2298         for (level_num=-1;level_num>=Last_secret_level;level_num--) {
2299                 load_level(Secret_level_names[-level_num-1]);
2300                 load_palette(Current_level_palette,1,1);                //don't change screen
2301                 if (save)
2302                         save_level_sub(Secret_level_names[-level_num-1],1);
2303         }
2304
2305         no_old_level_file_error=0;
2306
2307 }
2308
2309 #endif
2310