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