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