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.
15 char gamesave_rcsid[] = "$Id: gamesave.c,v 1.2 2001-01-20 13:49:15 bradleyb Exp $";
32 #include "editor\editor.h"
63 char Gamesave_current_filename[128];
65 #define GAME_VERSION 32
66 #define GAME_COMPATIBLE_VERSION 22
68 //version 28->29 add delta light support
69 //version 27->28 controlcen id now is reactor number, not model number
71 //version 29->30 changed trigger structure
72 //version 30->31 changed trigger structure some more
73 //version 31->32 change segment structure, make it 512 bytes w/o editor, add Segment2s array.
75 #define MENU_CURSOR_X_MIN MENU_X
76 #define MENU_CURSOR_X_MAX MENU_X+6
78 //Start old wall structures
80 typedef struct v16_wall {
81 byte type; // What kind of special wall.
82 byte flags; // Flags for the wall.
83 fix hps; // "Hit points" of the wall.
84 byte trigger; // Which trigger is associated with the wall.
85 byte clip_num; // Which animation associated with the wall.
89 typedef struct v19_wall {
90 int segnum,sidenum; // Seg & side for this wall
91 byte type; // What kind of special wall.
92 byte flags; // Flags for the wall.
93 fix hps; // "Hit points" of the wall.
94 byte trigger; // Which trigger is associated with the wall.
95 byte clip_num; // Which animation associated with the wall.
97 int linked_wall; // number of linked wall
100 typedef struct v19_door {
101 int n_parts; // for linked walls
102 short seg[2]; // Segment pointer of door.
103 short side[2]; // Side number of door.
104 short type[2]; // What kind of door animation.
105 fix open; // How long it has been open.
108 //End old wall structures
110 //old trigger structs
112 typedef struct v29_trigger {
119 short seg[MAX_WALLS_PER_LINK];
120 short side[MAX_WALLS_PER_LINK];
123 typedef struct v30_trigger {
126 byte pad; //keep alignment
129 short seg[MAX_WALLS_PER_LINK];
130 short side[MAX_WALLS_PER_LINK];
133 //flags for V30 & below triggers
134 #define TRIGGER_CONTROL_DOORS 1 // Control Trigger
135 #define TRIGGER_SHIELD_DAMAGE 2 // Shield Damage Trigger
136 #define TRIGGER_ENERGY_DRAIN 4 // Energy Drain Trigger
137 #define TRIGGER_EXIT 8 // End of level Trigger
138 #define TRIGGER_ON 16 // Whether Trigger is active
139 #define TRIGGER_ONE_SHOT 32 // If Trigger can only be triggered once
140 #define TRIGGER_MATCEN 64 // Trigger for materialization centers
141 #define TRIGGER_ILLUSION_OFF 128 // Switch Illusion OFF trigger
142 #define TRIGGER_SECRET_EXIT 256 // Exit to secret level
143 #define TRIGGER_ILLUSION_ON 512 // Switch Illusion ON trigger
144 #define TRIGGER_UNLOCK_DOORS 1024 // Unlocks a door
145 #define TRIGGER_OPEN_WALL 2048 // Makes a wall open
146 #define TRIGGER_CLOSE_WALL 4096 // Makes a wall closed
147 #define TRIGGER_ILLUSORY_WALL 8192 // Makes a wall illusory
150 ushort fileinfo_signature;
151 ushort fileinfo_version;
153 } game_top_fileinfo; // Should be same as first two fields below...
156 ushort fileinfo_signature;
157 ushort fileinfo_version;
159 char mine_filename[15];
161 int player_offset; // Player info
163 int object_offset; // Object info
173 int triggers_howmany;
184 int dl_indices_offset;
185 int dl_indices_howmany;
186 int dl_indices_sizeof;
187 int delta_light_offset;
188 int delta_light_howmany;
189 int delta_light_sizeof;
192 // LINT: adding function prototypes
193 void read_object(object *obj, CFILE *f, int version);
194 void write_object(object *obj, FILE *f);
195 void dump_mine_info(void);
197 extern char MaxPowerupsAllowed[MAX_POWERUP_TYPES];
198 extern char PowerupsInMine[MAX_POWERUP_TYPES];
201 extern char mine_filename[];
202 extern int save_mine_data_compiled(FILE * SaveFile);
204 //--unused-- char mine_filename[128];
207 int Gamesave_num_org_robots = 0;
208 //--unused-- grs_bitmap * Gamesave_saved_bitmap = NULL;
211 // Return true if this level has a name of the form "level??"
212 // Note that a pathspec can appear at the beginning of the filename.
213 int is_real_level(char *filename)
215 int len = strlen(filename);
220 //mprintf((0, "String = [%s]\n", &filename[len-11]));
221 return !strnicmp(&filename[len-11], "level", 5);
226 void change_filename_extension( char *dest, char *src, char *new_ext )
235 for (i=1; i<strlen(dest); i++ )
236 if (dest[i]=='.'||dest[i]==' '||dest[i]==0)
241 dest[i+1]=new_ext[0];
242 dest[i+2]=new_ext[1];
243 dest[i+3]=new_ext[2];
249 //--unused-- vms_angvec zero_angles={0,0,0};
251 #define vm_angvec_zero(v) do {(v)->p=(v)->b=(v)->h=0;} while (0)
253 int Gamesave_num_players=0;
255 int N_save_pof_names;
256 char Save_pof_names[MAX_POLYGON_MODELS][FILENAME_LEN];
258 void check_and_fix_matrix(vms_matrix *m);
260 void verify_object( object * obj ) {
262 obj->lifeleft = IMMORTAL_TIME; //all loaded object are immortal, for now
264 if ( obj->type == OBJ_ROBOT ) {
265 Gamesave_num_org_robots++;
267 // Make sure valid id...
268 if ( obj->id >= N_robot_types )
269 obj->id = obj->id % N_robot_types;
271 // Make sure model number & size are correct...
272 if ( obj->render_type == RT_POLYOBJ ) {
273 Assert(Robot_info[obj->id].model_num != -1);
274 //if you fail this assert, it means that a robot in this level
275 //hasn't been loaded, possibly because he's marked as
276 //non-shareware. To see what robot number, print obj->id.
278 Assert(Robot_info[obj->id].always_0xabcd == 0xabcd);
279 //if you fail this assert, it means that the robot_ai for
280 //a robot in this level hasn't been loaded, possibly because
281 //it's marked as non-shareware. To see what robot number,
284 obj->rtype.pobj_info.model_num = Robot_info[obj->id].model_num;
285 obj->size = Polygon_models[obj->rtype.pobj_info.model_num].rad;
287 //@@Took out this ugly hack 1/12/96, because Mike has added code
288 //@@that should fix it in a better way.
289 //@@//this is a super-ugly hack. Since the baby stripe robots have
290 //@@//their firing point on their bounding sphere, the firing points
291 //@@//can poke through a wall if the robots are very close to it. So
292 //@@//we make their radii bigger so the guns can't get too close to
294 //@@if (Robot_info[obj->id].flags & RIF_BIG_RADIUS)
295 //@@ obj->size = (obj->size*3)/2;
297 //@@if (obj->control_type==CT_AI && Robot_info[obj->id].attack_type)
298 //@@ obj->size = obj->size*3/4;
301 if (obj->id == 65) //special "reactor" robots
302 obj->movement_type = MT_NONE;
304 if (obj->movement_type == MT_PHYSICS) {
305 obj->mtype.phys_info.mass = Robot_info[obj->id].mass;
306 obj->mtype.phys_info.drag = Robot_info[obj->id].drag;
309 else { //Robots taken care of above
311 if ( obj->render_type == RT_POLYOBJ ) {
313 char *name = Save_pof_names[obj->rtype.pobj_info.model_num];
315 for (i=0;i<N_polygon_models;i++)
316 if (!stricmp(Pof_names[i],name)) { //found it!
317 // mprintf((0,"Mapping <%s> to %d (was %d)\n",name,i,obj->rtype.pobj_info.model_num));
318 obj->rtype.pobj_info.model_num = i;
324 if ( obj->type == OBJ_POWERUP ) {
325 if ( obj->id >= N_powerup_types ) {
327 Assert( obj->render_type != RT_POLYOBJ );
329 obj->control_type = CT_POWERUP;
330 obj->size = Powerup_info[obj->id].size;
331 obj->ctype.powerup_info.creation_time = 0;
334 if (Game_mode & GM_NETWORK)
336 if (multi_powerup_is_4pack(obj->id))
338 PowerupsInMine[obj->id-1]+=4;
339 MaxPowerupsAllowed[obj->id-1]+=4;
341 PowerupsInMine[obj->id]++;
342 MaxPowerupsAllowed[obj->id]++;
343 mprintf ((0,"PowerupLimiter: ID=%d\n",obj->id));
344 if (obj->id>MAX_POWERUP_TYPES)
345 mprintf ((1,"POWERUP: Overwriting array bounds!! Get JL!\n"));
351 if ( obj->type == OBJ_WEAPON ) {
352 if ( obj->id >= N_weapon_types ) {
354 Assert( obj->render_type != RT_POLYOBJ );
357 if (obj->id == PMINE_ID) { //make sure pmines have correct values
359 obj->mtype.phys_info.mass = Weapon_info[obj->id].mass;
360 obj->mtype.phys_info.drag = Weapon_info[obj->id].drag;
361 obj->mtype.phys_info.flags |= PF_FREE_SPINNING;
363 // Make sure model number & size are correct...
364 Assert( obj->render_type == RT_POLYOBJ );
366 obj->rtype.pobj_info.model_num = Weapon_info[obj->id].model_num;
367 obj->size = Polygon_models[obj->rtype.pobj_info.model_num].rad;
371 if ( obj->type == OBJ_CNTRLCEN ) {
373 obj->render_type = RT_POLYOBJ;
374 obj->control_type = CT_CNTRLCEN;
376 //@@// Make model number is correct...
377 //@@for (i=0; i<Num_total_object_types; i++ )
378 //@@ if ( ObjType[i] == OL_CONTROL_CENTER ) {
379 //@@ obj->rtype.pobj_info.model_num = ObjId[i];
380 //@@ obj->shields = ObjStrength[i];
387 // Check, and set, strength of reactor
388 for (i=0; i<Num_total_object_types; i++ )
389 if ( ObjType[i]==OL_CONTROL_CENTER && ObjId[i] == obj->id ) {
390 obj->shields = ObjStrength[i];
393 Assert(i < Num_total_object_types); //make sure we found it
398 if ( obj->type == OBJ_PLAYER ) {
401 //Assert(obj == Player);
403 if ( obj == ConsoleObject )
404 init_player_object();
406 if (obj->render_type == RT_POLYOBJ) //recover from Matt's pof file matchup bug
407 obj->rtype.pobj_info.model_num = Player_ship->model_num;
409 //Make sure orient matrix is orthogonal
410 check_and_fix_matrix(&obj->orient);
412 obj->id = Gamesave_num_players++;
415 if (obj->type == OBJ_HOSTAGE) {
417 //@@if (obj->id > N_hostage_types)
420 obj->render_type = RT_HOSTAGE;
421 obj->control_type = CT_POWERUP;
426 static int read_int(CFILE *file)
430 if (cfread( &i, sizeof(i), 1, file) != 1)
431 Error( "Error reading int in gamesave.c" );
437 static fix read_fix(CFILE *file)
441 if (cfread( &f, sizeof(f), 1, file) != 1)
442 Error( "Error reading fix in gamesave.c" );
444 f = (fix)INTEL_INT((int)f);
448 static short read_short(CFILE *file)
452 if (cfread( &s, sizeof(s), 1, file) != 1)
453 Error( "Error reading short in gamesave.c" );
459 static short read_fixang(CFILE *file)
463 if (cfread( &f, sizeof(f), 1, file) != 1)
464 Error( "Error reading fixang in gamesave.c" );
466 f = (fixang)INTEL_SHORT((short)f);
470 static byte read_byte(CFILE *file)
474 if (cfread( &b, sizeof(b), 1, file) != 1)
475 Error( "Error reading byte in gamesave.c" );
480 static void read_vector(vms_vector *v,CFILE *file)
482 v->x = read_fix(file);
483 v->y = read_fix(file);
484 v->z = read_fix(file);
487 static void read_matrix(vms_matrix *m,CFILE *file)
489 read_vector(&m->rvec,file);
490 read_vector(&m->uvec,file);
491 read_vector(&m->fvec,file);
494 static void read_angvec(vms_angvec *v,CFILE *file)
496 v->p = read_fixang(file);
497 v->b = read_fixang(file);
498 v->h = read_fixang(file);
501 //static gs_skip(int len,CFILE *file)
504 // cfseek(file,len,SEEK_CUR);
508 static void gs_write_int(int i,FILE *file)
510 if (fwrite( &i, sizeof(i), 1, file) != 1)
511 Error( "Error reading int in gamesave.c" );
515 static void gs_write_fix(fix f,FILE *file)
517 if (fwrite( &f, sizeof(f), 1, file) != 1)
518 Error( "Error reading fix in gamesave.c" );
522 static void gs_write_short(short s,FILE *file)
524 if (fwrite( &s, sizeof(s), 1, file) != 1)
525 Error( "Error reading short in gamesave.c" );
529 static void gs_write_fixang(fixang f,FILE *file)
531 if (fwrite( &f, sizeof(f), 1, file) != 1)
532 Error( "Error reading fixang in gamesave.c" );
536 static void gs_write_byte(byte b,FILE *file)
538 if (fwrite( &b, sizeof(b), 1, file) != 1)
539 Error( "Error reading byte in gamesave.c" );
543 static void gr_write_vector(vms_vector *v,FILE *file)
545 gs_write_fix(v->x,file);
546 gs_write_fix(v->y,file);
547 gs_write_fix(v->z,file);
550 static void gs_write_matrix(vms_matrix *m,FILE *file)
552 gr_write_vector(&m->rvec,file);
553 gr_write_vector(&m->uvec,file);
554 gr_write_vector(&m->fvec,file);
557 static void gs_write_angvec(vms_angvec *v,FILE *file)
559 gs_write_fixang(v->p,file);
560 gs_write_fixang(v->b,file);
561 gs_write_fixang(v->h,file);
567 extern int multi_powerup_is_4pack(int);
568 //reads one object of the given version from the given file
569 void read_object(object *obj,CFILE *f,int version)
572 obj->type = read_byte(f);
573 obj->id = read_byte(f);
575 if (obj->type == OBJ_CNTRLCEN && version<28)
576 obj->id = 0; //used to be only one kind of reactor
578 obj->control_type = read_byte(f);
579 obj->movement_type = read_byte(f);
580 obj->render_type = read_byte(f);
581 obj->flags = read_byte(f);
583 obj->segnum = read_short(f);
584 obj->attached_obj = -1;
586 read_vector(&obj->pos,f);
587 read_matrix(&obj->orient,f);
589 obj->size = read_fix(f);
590 obj->shields = read_fix(f);
592 read_vector(&obj->last_pos,f);
594 obj->contains_type = read_byte(f);
595 obj->contains_id = read_byte(f);
596 obj->contains_count = read_byte(f);
598 switch (obj->movement_type) {
602 read_vector(&obj->mtype.phys_info.velocity,f);
603 read_vector(&obj->mtype.phys_info.thrust,f);
605 obj->mtype.phys_info.mass = read_fix(f);
606 obj->mtype.phys_info.drag = read_fix(f);
607 obj->mtype.phys_info.brakes = read_fix(f);
609 read_vector(&obj->mtype.phys_info.rotvel,f);
610 read_vector(&obj->mtype.phys_info.rotthrust,f);
612 obj->mtype.phys_info.turnroll = read_fixang(f);
613 obj->mtype.phys_info.flags = read_short(f);
619 read_vector(&obj->mtype.spin_rate,f);
629 switch (obj->control_type) {
634 obj->ctype.ai_info.behavior = read_byte(f);
636 for (i=0;i<MAX_AI_FLAGS;i++)
637 obj->ctype.ai_info.flags[i] = read_byte(f);
639 obj->ctype.ai_info.hide_segment = read_short(f);
640 obj->ctype.ai_info.hide_index = read_short(f);
641 obj->ctype.ai_info.path_length = read_short(f);
642 obj->ctype.ai_info.cur_path_index = read_short(f);
645 read_short(f); // obj->ctype.ai_info.follow_path_start_seg =
646 read_short(f); // obj->ctype.ai_info.follow_path_end_seg =
654 obj->ctype.expl_info.spawn_time = read_fix(f);
655 obj->ctype.expl_info.delete_time = read_fix(f);
656 obj->ctype.expl_info.delete_objnum = read_short(f);
657 obj->ctype.expl_info.next_attach = obj->ctype.expl_info.prev_attach = obj->ctype.expl_info.attach_parent = -1;
663 //do I really need to read these? Are they even saved to disk?
665 obj->ctype.laser_info.parent_type = read_short(f);
666 obj->ctype.laser_info.parent_num = read_short(f);
667 obj->ctype.laser_info.parent_signature = read_int(f);
673 obj->ctype.light_info.intensity = read_fix(f);
679 obj->ctype.powerup_info.count = read_int(f);
681 obj->ctype.powerup_info.count = 1;
683 if (obj->id == POW_VULCAN_WEAPON)
684 obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
686 if (obj->id == POW_GAUSS_WEAPON)
687 obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
689 if (obj->id == POW_OMEGA_WEAPON)
690 obj->ctype.powerup_info.count = MAX_OMEGA_CHARGE;
700 case CT_SLEW: //the player is generally saved as slew
714 switch (obj->render_type) {
723 obj->rtype.pobj_info.model_num = read_int(f);
725 for (i=0;i<MAX_SUBMODELS;i++)
726 read_angvec(&obj->rtype.pobj_info.anim_angles[i],f);
728 obj->rtype.pobj_info.subobj_flags = read_int(f);
733 obj->rtype.pobj_info.tmap_override = tmo;
736 obj->rtype.pobj_info.tmap_override = -1;
738 int xlated_tmo = tmap_xlate_table[tmo];
739 if (xlated_tmo < 0) {
740 mprintf( (0, "Couldn't find texture for demo object, model_num = %d\n", obj->rtype.pobj_info.model_num));
744 obj->rtype.pobj_info.tmap_override = xlated_tmo;
748 obj->rtype.pobj_info.alt_textures = 0;
753 case RT_WEAPON_VCLIP:
758 obj->rtype.vclip_info.vclip_num = read_int(f);
759 obj->rtype.vclip_info.frametime = read_fix(f);
760 obj->rtype.vclip_info.framenum = read_byte(f);
776 //writes one object to the given file
777 void write_object(object *obj,FILE *f)
779 gs_write_byte(obj->type,f);
780 gs_write_byte(obj->id,f);
782 gs_write_byte(obj->control_type,f);
783 gs_write_byte(obj->movement_type,f);
784 gs_write_byte(obj->render_type,f);
785 gs_write_byte(obj->flags,f);
787 gs_write_short(obj->segnum,f);
789 gr_write_vector(&obj->pos,f);
790 gs_write_matrix(&obj->orient,f);
792 gs_write_fix(obj->size,f);
793 gs_write_fix(obj->shields,f);
795 gr_write_vector(&obj->last_pos,f);
797 gs_write_byte(obj->contains_type,f);
798 gs_write_byte(obj->contains_id,f);
799 gs_write_byte(obj->contains_count,f);
801 switch (obj->movement_type) {
805 gr_write_vector(&obj->mtype.phys_info.velocity,f);
806 gr_write_vector(&obj->mtype.phys_info.thrust,f);
808 gs_write_fix(obj->mtype.phys_info.mass,f);
809 gs_write_fix(obj->mtype.phys_info.drag,f);
810 gs_write_fix(obj->mtype.phys_info.brakes,f);
812 gr_write_vector(&obj->mtype.phys_info.rotvel,f);
813 gr_write_vector(&obj->mtype.phys_info.rotthrust,f);
815 gs_write_fixang(obj->mtype.phys_info.turnroll,f);
816 gs_write_short(obj->mtype.phys_info.flags,f);
822 gr_write_vector(&obj->mtype.spin_rate,f);
832 switch (obj->control_type) {
837 gs_write_byte(obj->ctype.ai_info.behavior,f);
839 for (i=0;i<MAX_AI_FLAGS;i++)
840 gs_write_byte(obj->ctype.ai_info.flags[i],f);
842 gs_write_short(obj->ctype.ai_info.hide_segment,f);
843 gs_write_short(obj->ctype.ai_info.hide_index,f);
844 gs_write_short(obj->ctype.ai_info.path_length,f);
845 gs_write_short(obj->ctype.ai_info.cur_path_index,f);
847 // -- unused! mk, 08/13/95 -- gs_write_short(obj->ctype.ai_info.follow_path_start_seg,f);
848 // -- unused! mk, 08/13/95 -- gs_write_short(obj->ctype.ai_info.follow_path_end_seg,f);
855 gs_write_fix(obj->ctype.expl_info.spawn_time,f);
856 gs_write_fix(obj->ctype.expl_info.delete_time,f);
857 gs_write_short(obj->ctype.expl_info.delete_objnum,f);
863 //do I really need to write these objects?
865 gs_write_short(obj->ctype.laser_info.parent_type,f);
866 gs_write_short(obj->ctype.laser_info.parent_num,f);
867 gs_write_int(obj->ctype.laser_info.parent_signature,f);
873 gs_write_fix(obj->ctype.light_info.intensity,f);
878 gs_write_int(obj->ctype.powerup_info.count,f);
886 case CT_SLEW: //the player is generally saved as slew
890 break; //control center object.
900 switch (obj->render_type) {
909 gs_write_int(obj->rtype.pobj_info.model_num,f);
911 for (i=0;i<MAX_SUBMODELS;i++)
912 gs_write_angvec(&obj->rtype.pobj_info.anim_angles[i],f);
914 gs_write_int(obj->rtype.pobj_info.subobj_flags,f);
916 gs_write_int(obj->rtype.pobj_info.tmap_override,f);
921 case RT_WEAPON_VCLIP:
926 gs_write_int(obj->rtype.vclip_info.vclip_num,f);
927 gs_write_fix(obj->rtype.vclip_info.frametime,f);
928 gs_write_byte(obj->rtype.vclip_info.framenum,f);
944 int robot_flags; // Up to 32 different robots
945 fix hit_points; // How hard it is to destroy this particular matcen
946 fix interval; // Interval between materialogrifizations
947 short segnum; // Segment this is attached to.
948 short fuelcen_num; // Index in fuelcen array.
951 extern int remove_trigger_num(int trigger_num);
953 // -----------------------------------------------------------------------------
955 // Loads all the relevant data for a level.
956 // If level != -1, it loads the filename with extension changed to .min
957 // Otherwise it loads the appropriate level mine.
958 // returns 0=everything ok, 1=old version, -1=error
959 int load_game_data(CFILE *LoadFile)
964 start_offset = cftell(LoadFile);
966 //===================== READ FILE INFO ========================
968 // Set default values
969 game_fileinfo.level = -1;
970 game_fileinfo.player_offset = -1;
971 game_fileinfo.player_sizeof = sizeof(player);
972 game_fileinfo.object_offset = -1;
973 game_fileinfo.object_howmany = 0;
974 game_fileinfo.object_sizeof = sizeof(object);
975 game_fileinfo.walls_offset = -1;
976 game_fileinfo.walls_howmany = 0;
977 game_fileinfo.walls_sizeof = sizeof(wall);
978 game_fileinfo.doors_offset = -1;
979 game_fileinfo.doors_howmany = 0;
980 game_fileinfo.doors_sizeof = sizeof(active_door);
981 game_fileinfo.triggers_offset = -1;
982 game_fileinfo.triggers_howmany = 0;
983 game_fileinfo.triggers_sizeof = sizeof(trigger);
984 game_fileinfo.control_offset = -1;
985 game_fileinfo.control_howmany = 0;
986 game_fileinfo.control_sizeof = sizeof(control_center_triggers);
987 game_fileinfo.matcen_offset = -1;
988 game_fileinfo.matcen_howmany = 0;
989 game_fileinfo.matcen_sizeof = sizeof(matcen_info);
991 game_fileinfo.dl_indices_offset = -1;
992 game_fileinfo.dl_indices_howmany = 0;
993 game_fileinfo.dl_indices_sizeof = sizeof(dl_index);
995 game_fileinfo.delta_light_offset = -1;
996 game_fileinfo.delta_light_howmany = 0;
997 game_fileinfo.delta_light_sizeof = sizeof(delta_light);
999 // Read in game_top_fileinfo to get size of saved fileinfo.
1001 if (cfseek( LoadFile, start_offset, SEEK_SET ))
1002 Error( "Error seeking in gamesave.c" );
1004 // if (cfread( &game_top_fileinfo, sizeof(game_top_fileinfo), 1, LoadFile) != 1)
1005 // Error( "Error reading game_top_fileinfo in gamesave.c" );
1007 game_top_fileinfo.fileinfo_signature = read_short(LoadFile);
1008 game_top_fileinfo.fileinfo_version = read_short(LoadFile);
1009 game_top_fileinfo.fileinfo_sizeof = read_int(LoadFile);
1012 if (game_top_fileinfo.fileinfo_signature != 0x6705)
1015 // Check version number
1016 if (game_top_fileinfo.fileinfo_version < GAME_COMPATIBLE_VERSION )
1019 // Now, Read in the fileinfo
1020 if (cfseek( LoadFile, start_offset, SEEK_SET ))
1021 Error( "Error seeking to game_fileinfo in gamesave.c" );
1023 // if (cfread( &game_fileinfo, game_top_fileinfo.fileinfo_sizeof, 1, LoadFile )!=1)
1024 // Error( "Error reading game_fileinfo in gamesave.c" );
1026 game_fileinfo.fileinfo_signature = read_short(LoadFile);
1027 game_fileinfo.fileinfo_version = read_short(LoadFile);
1028 game_fileinfo.fileinfo_sizeof = read_int(LoadFile);
1030 game_fileinfo.mine_filename[i] = read_byte(LoadFile);
1031 game_fileinfo.level = read_int(LoadFile);
1032 game_fileinfo.player_offset = read_int(LoadFile); // Player info
1033 game_fileinfo.player_sizeof = read_int(LoadFile);
1034 game_fileinfo.object_offset = read_int(LoadFile); // Object info
1035 game_fileinfo.object_howmany = read_int(LoadFile);
1036 game_fileinfo.object_sizeof = read_int(LoadFile);
1037 game_fileinfo.walls_offset = read_int(LoadFile);
1038 game_fileinfo.walls_howmany = read_int(LoadFile);
1039 game_fileinfo.walls_sizeof = read_int(LoadFile);
1040 game_fileinfo.doors_offset = read_int(LoadFile);
1041 game_fileinfo.doors_howmany = read_int(LoadFile);
1042 game_fileinfo.doors_sizeof = read_int(LoadFile);
1043 game_fileinfo.triggers_offset = read_int(LoadFile);
1044 game_fileinfo.triggers_howmany = read_int(LoadFile);
1045 game_fileinfo.triggers_sizeof = read_int(LoadFile);
1046 game_fileinfo.links_offset = read_int(LoadFile);
1047 game_fileinfo.links_howmany = read_int(LoadFile);
1048 game_fileinfo.links_sizeof = read_int(LoadFile);
1049 game_fileinfo.control_offset = read_int(LoadFile);
1050 game_fileinfo.control_howmany = read_int(LoadFile);
1051 game_fileinfo.control_sizeof = read_int(LoadFile);
1052 game_fileinfo.matcen_offset = read_int(LoadFile);
1053 game_fileinfo.matcen_howmany = read_int(LoadFile);
1054 game_fileinfo.matcen_sizeof = read_int(LoadFile);
1056 if (game_top_fileinfo.fileinfo_version >= 29) {
1057 game_fileinfo.dl_indices_offset = read_int(LoadFile);
1058 game_fileinfo.dl_indices_howmany = read_int(LoadFile);
1059 game_fileinfo.dl_indices_sizeof = read_int(LoadFile);
1061 game_fileinfo.delta_light_offset = read_int(LoadFile);
1062 game_fileinfo.delta_light_howmany = read_int(LoadFile);
1063 game_fileinfo.delta_light_sizeof = read_int(LoadFile);
1066 if (game_top_fileinfo.fileinfo_version >= 14) { //load mine filename
1067 //@@char *p=Current_level_name;
1068 //@@//must do read one char at a time, since no cfgets()
1069 //@@do *p = cfgetc(LoadFile); while (*p++!=0);
1071 cfgets(Current_level_name,sizeof(Current_level_name),LoadFile);
1073 if (Current_level_name[strlen(Current_level_name)-1] == '\n')
1074 Current_level_name[strlen(Current_level_name)-1] = 0;
1077 Current_level_name[0]=0;
1079 if (game_top_fileinfo.fileinfo_version >= 19) { //load pof names
1080 // cfread(&N_save_pof_names,2,1,LoadFile);
1081 N_save_pof_names = read_short(LoadFile);
1082 cfread(Save_pof_names,N_save_pof_names,FILENAME_LEN,LoadFile);
1085 //===================== READ PLAYER INFO ==========================
1086 Object_next_signature = 0;
1088 //===================== READ OBJECT INFO ==========================
1090 Gamesave_num_org_robots = 0;
1091 Gamesave_num_players = 0;
1093 if (game_fileinfo.object_offset > -1) {
1094 if (cfseek( LoadFile, game_fileinfo.object_offset, SEEK_SET ))
1095 Error( "Error seeking to object_offset in gamesave.c" );
1097 for (i=0;i<game_fileinfo.object_howmany;i++) {
1099 read_object(&Objects[i],LoadFile,game_top_fileinfo.fileinfo_version);
1101 Objects[i].signature = Object_next_signature++;
1102 verify_object( &Objects[i] );
1107 //===================== READ WALL INFO ============================
1109 if (game_fileinfo.walls_offset > -1)
1112 if (!cfseek( LoadFile, game_fileinfo.walls_offset,SEEK_SET )) {
1113 for (i=0;i<game_fileinfo.walls_howmany;i++) {
1115 if (game_top_fileinfo.fileinfo_version >= 20) {
1117 Assert(sizeof(Walls[i]) == game_fileinfo.walls_sizeof);
1119 // code to correctly read wall structure on mac. I'm assuming only v20 walls
1122 if (cfread(&Walls[i], game_fileinfo.walls_sizeof, 1,LoadFile)!=1)
1123 Error( "Error reading Walls[%d] in gamesave.c", i);
1125 Walls[i].segnum = read_int(LoadFile);
1126 Walls[i].sidenum = read_int(LoadFile);
1127 Walls[i].hps = read_fix(LoadFile);
1128 Walls[i].linked_wall = read_int(LoadFile);
1129 Walls[i].type = read_byte(LoadFile);
1130 Walls[i].flags = read_byte(LoadFile);
1131 Walls[i].state = read_byte(LoadFile);
1132 Walls[i].trigger = read_byte(LoadFile);
1133 Walls[i].clip_num = read_byte(LoadFile);
1134 Walls[i].keys = read_byte(LoadFile);
1135 Walls[i].controlling_trigger = read_byte(LoadFile);
1136 Walls[i].cloak_value = read_byte(LoadFile);
1139 else if (game_top_fileinfo.fileinfo_version >= 17) {
1142 Assert(sizeof(w) == game_fileinfo.walls_sizeof);
1144 if (cfread(&w, game_fileinfo.walls_sizeof, 1,LoadFile)!=1)
1145 Error( "Error reading Walls[%d] in gamesave.c", i);
1147 Walls[i].segnum = w.segnum;
1148 Walls[i].sidenum = w.sidenum;
1149 Walls[i].linked_wall = w.linked_wall;
1151 Walls[i].type = w.type;
1152 Walls[i].flags = w.flags;
1153 Walls[i].hps = w.hps;
1154 Walls[i].trigger = w.trigger;
1155 Walls[i].clip_num = w.clip_num;
1156 Walls[i].keys = w.keys;
1158 Walls[i].state = WALL_DOOR_CLOSED;
1163 Assert(sizeof(w) == game_fileinfo.walls_sizeof);
1165 if (cfread(&w, game_fileinfo.walls_sizeof, 1,LoadFile)!=1)
1166 Error( "Error reading Walls[%d] in gamesave.c", i);
1168 Walls[i].segnum = Walls[i].sidenum = Walls[i].linked_wall = -1;
1170 Walls[i].type = w.type;
1171 Walls[i].flags = w.flags;
1172 Walls[i].hps = w.hps;
1173 Walls[i].trigger = w.trigger;
1174 Walls[i].clip_num = w.clip_num;
1175 Walls[i].keys = w.keys;
1182 //===================== READ DOOR INFO ============================
1184 if (game_fileinfo.doors_offset > -1)
1186 if (!cfseek( LoadFile, game_fileinfo.doors_offset,SEEK_SET )) {
1188 for (i=0;i<game_fileinfo.doors_howmany;i++) {
1190 if (game_top_fileinfo.fileinfo_version >= 20) {
1192 Assert(sizeof(ActiveDoors[i]) == game_fileinfo.doors_sizeof);
1194 // code to read doors for mac -- assume version 20 and greater for doors
1196 if (cfread(&ActiveDoors[i], game_fileinfo.doors_sizeof,1,LoadFile)!=1)
1197 Error( "Error reading ActiveDoors[%d] in gamesave.c", i);
1199 ActiveDoors[i].n_parts = read_int(LoadFile);
1200 ActiveDoors[i].front_wallnum[0] = read_short(LoadFile);
1201 ActiveDoors[i].front_wallnum[1] = read_short(LoadFile);
1202 ActiveDoors[i].back_wallnum[0] = read_short(LoadFile);
1203 ActiveDoors[i].back_wallnum[1] = read_short(LoadFile);
1204 ActiveDoors[i].time = read_fix(LoadFile);
1211 Assert(sizeof(d) == game_fileinfo.doors_sizeof);
1213 if (cfread(&d, game_fileinfo.doors_sizeof, 1,LoadFile)!=1)
1214 Error( "Error reading Doors[%d] in gamesave.c", i);
1216 ActiveDoors[i].n_parts = d.n_parts;
1218 for (p=0;p<d.n_parts;p++) {
1221 cseg = Segments[d.seg[p]].children[d.side[p]];
1222 cside = find_connect_side(&Segments[d.seg[p]],&Segments[cseg]);
1224 ActiveDoors[i].front_wallnum[p] = Segments[d.seg[p]].sides[d.side[p]].wall_num;
1225 ActiveDoors[i].back_wallnum[p] = Segments[cseg].sides[cside].wall_num;
1233 //==================== READ TRIGGER INFO ==========================
1236 // for MACINTOSH -- assume all triggers >= verion 31 triggers.
1238 if (game_fileinfo.triggers_offset > -1)
1240 if (!cfseek( LoadFile, game_fileinfo.triggers_offset,SEEK_SET )) {
1241 for (i=0;i<game_fileinfo.triggers_howmany;i++)
1242 if (game_top_fileinfo.fileinfo_version < 31) {
1248 if (game_top_fileinfo.fileinfo_version < 30) {
1252 if (cfread(&trig29, game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1253 Error( "Error reading Triggers[%d] in gamesave.c", i);
1255 trig.flags = trig29.flags;
1256 trig.num_links = trig29.num_links;
1257 trig.num_links = trig29.num_links;
1258 trig.value = trig29.value;
1259 trig.time = trig29.time;
1261 for (t=0;t<trig.num_links;t++) {
1262 trig.seg[t] = trig29.seg[t];
1263 trig.side[t] = trig29.side[t];
1267 if (cfread(&trig, game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1268 Error( "Error reading Triggers[%d] in gamesave.c", i);
1270 //Assert(trig.flags & TRIGGER_ON);
1271 trig.flags &= ~TRIGGER_ON;
1273 if (trig.flags & TRIGGER_CONTROL_DOORS)
1274 type = TT_OPEN_DOOR;
1275 else if (trig.flags & TRIGGER_SHIELD_DAMAGE)
1277 else if (trig.flags & TRIGGER_ENERGY_DRAIN)
1279 else if (trig.flags & TRIGGER_EXIT)
1281 else if (trig.flags & TRIGGER_ONE_SHOT)
1283 else if (trig.flags & TRIGGER_MATCEN)
1285 else if (trig.flags & TRIGGER_ILLUSION_OFF)
1286 type = TT_ILLUSION_OFF;
1287 else if (trig.flags & TRIGGER_SECRET_EXIT)
1288 type = TT_SECRET_EXIT;
1289 else if (trig.flags & TRIGGER_ILLUSION_ON)
1290 type = TT_ILLUSION_ON;
1291 else if (trig.flags & TRIGGER_UNLOCK_DOORS)
1292 type = TT_UNLOCK_DOOR;
1293 else if (trig.flags & TRIGGER_OPEN_WALL)
1294 type = TT_OPEN_WALL;
1295 else if (trig.flags & TRIGGER_CLOSE_WALL)
1296 type = TT_CLOSE_WALL;
1297 else if (trig.flags & TRIGGER_ILLUSORY_WALL)
1298 type = TT_ILLUSORY_WALL;
1302 Triggers[i].type = type;
1303 Triggers[i].flags = 0;
1304 Triggers[i].num_links = trig.num_links;
1305 Triggers[i].num_links = trig.num_links;
1306 Triggers[i].value = trig.value;
1307 Triggers[i].time = trig.time;
1309 for (t=0;t<trig.num_links;t++) {
1310 Triggers[i].seg[t] = trig.seg[t];
1311 Triggers[i].side[t] = trig.side[t];
1316 if (cfread(&Triggers[i], game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1317 Error( "Error reading Triggers[%d] in gamesave.c", i);
1319 Triggers[i].type = read_byte(LoadFile);
1320 Triggers[i].flags = read_byte(LoadFile);
1321 Triggers[i].num_links = read_byte(LoadFile);
1322 Triggers[i].pad = read_byte(LoadFile);
1323 Triggers[i].value = read_fix(LoadFile);
1324 Triggers[i].time = read_fix(LoadFile);
1325 for (j=0; j<MAX_WALLS_PER_LINK; j++ )
1326 Triggers[i].seg[j] = read_short(LoadFile);
1327 for (j=0; j<MAX_WALLS_PER_LINK; j++ )
1328 Triggers[i].side[j] = read_short(LoadFile);
1334 //================ READ CONTROL CENTER TRIGGER INFO ===============
1336 if (game_fileinfo.control_offset > -1)
1338 if (!cfseek( LoadFile, game_fileinfo.control_offset,SEEK_SET )) {
1339 for (i=0;i<game_fileinfo.control_howmany;i++)
1341 if (cfread(&ControlCenterTriggers, game_fileinfo.control_sizeof,1,LoadFile)!=1)
1342 Error( "Error reading ControlCenterTriggers in gamesave.c");
1344 ControlCenterTriggers.num_links = read_short(LoadFile);
1345 for (j=0; j<MAX_CONTROLCEN_LINKS; j++ )
1346 ControlCenterTriggers.seg[j] = read_short( LoadFile );
1347 for (j=0; j<MAX_CONTROLCEN_LINKS; j++ )
1348 ControlCenterTriggers.side[j] = read_short( LoadFile );
1354 //================ READ MATERIALOGRIFIZATIONATORS INFO ===============
1356 if (game_fileinfo.matcen_offset > -1)
1359 if (!cfseek( LoadFile, game_fileinfo.matcen_offset,SEEK_SET )) {
1360 // mprintf((0, "Reading %i materialization centers.\n", game_fileinfo.matcen_howmany));
1361 for (i=0;i<game_fileinfo.matcen_howmany;i++) {
1362 if (game_top_fileinfo.fileinfo_version < 27) {
1364 Assert(game_fileinfo.matcen_sizeof == sizeof(m));
1365 if (cfread(&m, game_fileinfo.matcen_sizeof,1,LoadFile)!=1)
1366 Error( "Error reading RobotCenters in gamesave.c");
1367 RobotCenters[i].robot_flags[0] = m.robot_flags;
1368 RobotCenters[i].robot_flags[1] = 0;
1369 RobotCenters[i].hit_points = m.hit_points;
1370 RobotCenters[i].interval = m.interval;
1371 RobotCenters[i].segnum = m.segnum;
1372 RobotCenters[i].fuelcen_num = m.fuelcen_num;
1375 Assert(game_fileinfo.matcen_sizeof == sizeof(RobotCenters[i]));
1377 if (cfread(&RobotCenters[i], game_fileinfo.matcen_sizeof,1,LoadFile)!=1)
1378 Error( "Error reading RobotCenters in gamesave.c");
1380 RobotCenters[i].robot_flags[0] = read_int(LoadFile);
1381 RobotCenters[i].robot_flags[1] = read_int(LoadFile);
1382 RobotCenters[i].hit_points = read_fix(LoadFile);
1383 RobotCenters[i].interval = read_fix(LoadFile);
1384 RobotCenters[i].segnum = read_short(LoadFile);
1385 RobotCenters[i].fuelcen_num = read_short(LoadFile);
1389 // Set links in RobotCenters to Station array
1391 for (j=0; j<=Highest_segment_index; j++)
1392 if (Segment2s[j].special == SEGMENT_IS_ROBOTMAKER)
1393 if (Segment2s[j].matcen_num == i)
1394 RobotCenters[i].fuelcen_num = Segment2s[j].value;
1396 // mprintf((0, " %i: flags = %08x\n", i, RobotCenters[i].robot_flags));
1402 //================ READ DL_INDICES INFO ===============
1404 Num_static_lights = 0;
1406 if (game_fileinfo.dl_indices_offset > -1) {
1409 if (!cfseek( LoadFile, game_fileinfo.dl_indices_offset, SEEK_SET )) {
1410 Num_static_lights = game_fileinfo.dl_indices_howmany;
1411 for (i=0; i<game_fileinfo.dl_indices_howmany; i++) {
1412 if (game_top_fileinfo.fileinfo_version < 29) {
1413 mprintf((0, "Warning: Old mine version. Not reading Dl_indices info.\n"));
1414 Int3(); //shouldn't be here!!!
1417 if (cfread(&Dl_indices[i], game_fileinfo.dl_indices_sizeof, 1, LoadFile) != 1)
1418 Error( "Error reading Dl_indices in gamesave.c");
1420 Dl_indices[i].segnum = read_short(LoadFile);
1421 Dl_indices[i].sidenum = read_byte(LoadFile);
1422 Dl_indices[i].count = read_byte(LoadFile);
1423 Dl_indices[i].index = read_short(LoadFile);
1431 // Indicate that no light has been subtracted from any vertices.
1432 clear_light_subtracted();
1434 //================ READ DELTA LIGHT INFO ===============
1436 if (game_fileinfo.delta_light_offset > -1) {
1439 if (!cfseek( LoadFile, game_fileinfo.delta_light_offset, SEEK_SET )) {
1440 for (i=0; i<game_fileinfo.delta_light_howmany; i++) {
1441 if (game_top_fileinfo.fileinfo_version < 29) {
1442 mprintf((0, "Warning: Old mine version. Not reading delta light info.\n"));
1445 if (cfread(&Delta_lights[i], game_fileinfo.delta_light_sizeof, 1, LoadFile) != 1)
1446 Error( "Error reading Delta Lights in gamesave.c");
1448 Delta_lights[i].segnum = read_short(LoadFile);
1449 Delta_lights[i].sidenum = read_byte(LoadFile);
1450 Delta_lights[i].dummy = read_byte(LoadFile);
1451 Delta_lights[i].vert_light[0] = read_byte(LoadFile);
1452 Delta_lights[i].vert_light[1] = read_byte(LoadFile);
1453 Delta_lights[i].vert_light[2] = read_byte(LoadFile);
1454 Delta_lights[i].vert_light[3] = read_byte(LoadFile);
1462 //========================= UPDATE VARIABLES ======================
1464 reset_objects(game_fileinfo.object_howmany);
1466 for (i=0; i<MAX_OBJECTS; i++) {
1467 Objects[i].next = Objects[i].prev = -1;
1468 if (Objects[i].type != OBJ_NONE) {
1469 int objsegnum = Objects[i].segnum;
1471 if (objsegnum > Highest_segment_index) //bogus object
1472 Objects[i].type = OBJ_NONE;
1474 Objects[i].segnum = -1; //avoid Assert()
1475 obj_link(i,objsegnum);
1480 clear_transient_objects(1); //1 means clear proximity bombs
1482 // Make sure non-transparent doors are set correctly.
1483 for (i=0; i< Num_segments; i++)
1484 for (j=0;j<MAX_SIDES_PER_SEGMENT;j++) {
1485 side *sidep = &Segments[i].sides[j];
1486 if ((sidep->wall_num != -1) && (Walls[sidep->wall_num].clip_num != -1)) {
1487 //mprintf((0, "Checking Wall %d\n", Segments[i].sides[j].wall_num));
1488 if (WallAnims[Walls[sidep->wall_num].clip_num].flags & WCF_TMAP1) {
1489 //mprintf((0, "Fixing non-transparent door.\n"));
1490 sidep->tmap_num = WallAnims[Walls[sidep->wall_num].clip_num].frames[0];
1491 sidep->tmap_num2 = 0;
1497 Num_walls = game_fileinfo.walls_howmany;
1500 Num_open_doors = game_fileinfo.doors_howmany;
1501 Num_triggers = game_fileinfo.triggers_howmany;
1503 //go through all walls, killing references to invalid triggers
1504 for (i=0;i<Num_walls;i++)
1505 if (Walls[i].trigger >= Num_triggers) {
1506 mprintf((0,"Removing reference to invalid trigger %d from wall %d\n",Walls[i].trigger,i));
1507 Walls[i].trigger = -1; //kill trigger
1510 //go through all triggers, killing unused ones
1511 for (i=0;i<Num_triggers;) {
1514 // Find which wall this trigger is connected to.
1515 for (w=0; w<Num_walls; w++)
1516 if (Walls[w].trigger == i)
1520 if (w == Num_walls) {
1521 mprintf((0,"Removing unreferenced trigger %d\n",i));
1522 remove_trigger_num(i);
1529 // MK, 10/17/95: Make walls point back at the triggers that control them.
1530 // Go through all triggers, stuffing controlling_trigger field in Walls.
1533 for (i=0; i<Num_walls; i++)
1534 Walls[i].controlling_trigger = -1;
1536 for (t=0; t<Num_triggers; t++) {
1538 for (l=0; l<Triggers[t].num_links; l++) {
1539 int seg_num, side_num, wall_num;
1541 seg_num = Triggers[t].seg[l];
1542 side_num = Triggers[t].side[l];
1543 wall_num = Segments[seg_num].sides[side_num].wall_num;
1545 // -- if (Walls[wall_num].controlling_trigger != -1)
1548 //check to see that if a trigger requires a wall that it has one,
1549 //and if it requires a matcen that it has one
1551 if (Triggers[t].type == TT_MATCEN) {
1552 if (Segment2s[seg_num].special != SEGMENT_IS_ROBOTMAKER)
1553 Int3(); //matcen trigger doesn't point to matcen
1555 else if (Triggers[t].type != TT_LIGHT_OFF && Triggers[t].type != TT_LIGHT_ON) { //light triggers don't require walls
1557 Int3(); // This is illegal. This trigger requires a wall
1559 Walls[wall_num].controlling_trigger = t;
1565 Num_robot_centers = game_fileinfo.matcen_howmany;
1567 //fix old wall structs
1568 if (game_top_fileinfo.fileinfo_version < 17) {
1569 int segnum,sidenum,wallnum;
1571 for (segnum=0; segnum<=Highest_segment_index; segnum++)
1572 for (sidenum=0;sidenum<6;sidenum++)
1573 if ((wallnum=Segments[segnum].sides[sidenum].wall_num) != -1) {
1574 Walls[wallnum].segnum = segnum;
1575 Walls[wallnum].sidenum = sidenum;
1582 for (sidenum=0; sidenum<6; sidenum++) {
1583 int wallnum = Segments[Highest_segment_index].sides[sidenum].wall_num;
1585 if ((Walls[wallnum].segnum != Highest_segment_index) || (Walls[wallnum].sidenum != sidenum))
1586 Int3(); // Error. Bogus walls in this segment.
1587 // Consult Yuan or Mike.
1592 //create_local_segment_data();
1600 if (game_top_fileinfo.fileinfo_version < GAME_VERSION && !(game_top_fileinfo.fileinfo_version==25 && GAME_VERSION==26))
1601 return 1; //means old version
1607 int check_segment_connections(void);
1609 extern void set_ambient_sound_flags(void);
1611 // -----------------------------------------------------------------------------
1612 //loads from an already-open file
1613 // returns 0=everything ok, 1=old version, -1=error
1614 int load_mine_data(CFILE *LoadFile);
1615 int load_mine_data_compiled(CFILE *LoadFile);
1617 #define LEVEL_FILE_VERSION 8
1618 //1 -> 2 add palette name
1619 //2 -> 3 add control center explosion time
1620 //3 -> 4 add reactor strength
1621 //4 -> 5 killed hostage text stuff
1622 //5 -> 6 added Secret_return_segment and Secret_return_orient
1623 //6 -> 7 added flickering lights
1624 //7 -> 8 made version 8 to be not compatible with D2 1.0 & 1.1
1627 char *Level_being_loaded=NULL;
1631 extern void ncache_flush();
1634 extern int HoardEquipped();
1636 extern int Slide_segs_computed;
1638 int no_old_level_file_error=0;
1640 //loads a level (.LVL) file from disk
1641 //returns 0 if success, else error code
1642 int load_level(char * filename_passed)
1645 int use_compiled_level=1;
1649 int version,minedata_offset,gamedata_offset;
1650 int mine_err,game_err,i;
1653 Slide_segs_computed = 0;
1656 if (Game_mode & GM_NETWORK)
1658 for (i=0;i<MAX_POWERUP_TYPES;i++)
1660 MaxPowerupsAllowed[i]=0;
1661 PowerupsInMine[i]=0;
1671 Level_being_loaded = filename_passed;
1674 strcpy(filename,filename_passed);
1678 //if we have the editor, try the LVL first, no matter what was passed.
1679 //if we don't have an LVL, try RDL
1680 //if we don't have the editor, we just use what was passed
1682 change_filename_extension(filename,filename_passed,".LVL");
1683 use_compiled_level = 0;
1685 if (!cfexist(filename)) {
1686 change_filename_extension(filename,filename,".RL2");
1687 use_compiled_level = 1;
1691 LoadFile = cfopen( filename, "rb" );
1695 mprintf((0,"Can't open level file <%s>\n", filename));
1698 Error("Can't open file <%s>\n",filename);
1702 strcpy( Gamesave_current_filename, filename );
1705 // if ( Newdemo_state == ND_STATE_RECORDING )
1706 // newdemo_record_start_demo();
1709 cfread(sig, 1, 4, LoadFile);
1710 version = read_int(LoadFile);
1711 minedata_offset = read_int(LoadFile);
1712 gamedata_offset = read_int(LoadFile);
1714 Assert((sig[0] == 'L') && (sig[1] == 'V') && (sig[2]=='L') && (sig[3] == 'P'));
1716 if (version >= 8) { //read dummy data
1718 if (HoardEquipped())
1721 read_short(LoadFile);
1722 read_byte(LoadFile);
1726 // NOTE LINK TO ABOVE!
1727 Error("This level requires the Vertigo Enhanced version of D2.");
1732 read_int(LoadFile); //was hostagetext_offset
1735 cfgets(Current_level_palette,sizeof(Current_level_palette),LoadFile);
1736 if (Current_level_palette[strlen(Current_level_palette)-1] == '\n')
1737 Current_level_palette[strlen(Current_level_palette)-1] = 0;
1741 Base_control_center_explosion_time = read_int(LoadFile);
1743 Base_control_center_explosion_time = DEFAULT_CONTROL_CENTER_EXPLOSION_TIME;
1746 Reactor_strength = read_int(LoadFile);
1748 Reactor_strength = -1; //use old defaults
1751 Num_flickering_lights = read_int(LoadFile);
1753 Assert((Num_flickering_lights >= 0) && (Num_flickering_lights < MAX_FLICKERING_LIGHTS));
1754 for (i = 0; i < Num_flickering_lights; i++)
1756 Flickering_lights[i].segnum = read_short(LoadFile);
1757 Flickering_lights[i].sidenum = read_short(LoadFile);
1758 Flickering_lights[i].mask = read_int(LoadFile);
1759 Flickering_lights[i].timer = read_fix(LoadFile);
1760 Flickering_lights[i].delay = read_fix(LoadFile);
1763 cfread(Flickering_lights,sizeof(*Flickering_lights),Num_flickering_lights,LoadFile);
1767 Num_flickering_lights = 0;
1769 if (version <= 1 || Current_level_palette[0]==0)
1770 strcpy(Current_level_palette,"groupa.256");
1773 Secret_return_segment = 0;
1774 Secret_return_orient.rvec.x = F1_0; Secret_return_orient.rvec.y = 0; Secret_return_orient.rvec.z = 0;
1775 Secret_return_orient.fvec.x = 0; Secret_return_orient.fvec.y = F1_0; Secret_return_orient.fvec.z = 0;
1776 Secret_return_orient.uvec.x = 0; Secret_return_orient.uvec.y = 0; Secret_return_orient.uvec.z = F1_0;
1778 Secret_return_segment = read_int(LoadFile);
1779 Secret_return_orient.rvec.x = read_int(LoadFile);
1780 Secret_return_orient.rvec.y = read_int(LoadFile);
1781 Secret_return_orient.rvec.z = read_int(LoadFile);
1782 Secret_return_orient.fvec.x = read_int(LoadFile);
1783 Secret_return_orient.fvec.y = read_int(LoadFile);
1784 Secret_return_orient.fvec.z = read_int(LoadFile);
1785 Secret_return_orient.uvec.x = read_int(LoadFile);
1786 Secret_return_orient.uvec.y = read_int(LoadFile);
1787 Secret_return_orient.uvec.z = read_int(LoadFile);
1790 cfseek(LoadFile,minedata_offset,SEEK_SET);
1792 if (!use_compiled_level) {
1793 mine_err = load_mine_data(LoadFile);
1794 // Compress all uv coordinates in mine, improves texmap precision. --MK, 02/19/96
1795 compress_uv_coordinates_all();
1798 //NOTE LINK TO ABOVE!!
1799 mine_err = load_mine_data_compiled(LoadFile);
1801 if (mine_err == -1) { //error!!
1806 cfseek(LoadFile,gamedata_offset,SEEK_SET);
1807 game_err = load_game_data(LoadFile);
1809 if (game_err == -1) { //error!!
1814 //======================== CLOSE FILE =============================
1816 cfclose( LoadFile );
1818 set_ambient_sound_flags();
1821 write_game_text_file(filename);
1822 if (Errors_in_mine) {
1823 if (is_real_level(filename)) {
1824 char ErrorMessage[200];
1826 sprintf( ErrorMessage, "Warning: %i errors in %s!\n", Errors_in_mine, Level_being_loaded );
1828 gr_palette_load(gr_palette);
1829 nm_messagebox( NULL, 1, "Continue", ErrorMessage );
1832 mprintf((1, "Error: %i errors in %s.\n", Errors_in_mine, Level_being_loaded));
1837 //If an old version, ask the use if he wants to save as new version
1838 if (!no_old_level_file_error && (Function_mode == FMODE_EDITOR) && (((LEVEL_FILE_VERSION>3) && version<LEVEL_FILE_VERSION) || mine_err==1 || game_err==1)) {
1839 char ErrorMessage[200];
1841 sprintf( ErrorMessage,
1842 "You just loaded a old version\n"
1843 "level. Would you like to save\n"
1844 "it as a current version level?");
1847 gr_palette_load(gr_palette);
1848 if (nm_messagebox( NULL, 2, "Don't Save", "Save", ErrorMessage )==1)
1849 save_level(filename);
1855 if (Function_mode == FMODE_EDITOR)
1856 editor_status("Loaded NEW mine %s, \"%s\"",filename,Current_level_name);
1860 if (check_segment_connections())
1861 nm_messagebox( "ERROR", 1, "Ok",
1862 "Connectivity errors detected in\n"
1863 "mine. See monochrome screen for\n"
1864 "details, and contact Matt or Mike." );
1871 void get_level_name()
1873 //NO_UI!!! UI_WINDOW *NameWindow = NULL;
1874 //NO_UI!!! UI_GADGET_INPUTBOX *NameText;
1875 //NO_UI!!! UI_GADGET_BUTTON *QuitButton;
1877 //NO_UI!!! // Open a window with a quit button
1878 //NO_UI!!! NameWindow = ui_open_window( 20, 20, 300, 110, WIN_DIALOG );
1879 //NO_UI!!! QuitButton = ui_add_gadget_button( NameWindow, 150-24, 60, 48, 40, "Done", NULL );
1881 //NO_UI!!! ui_wprintf_at( NameWindow, 10, 12,"Please enter a name for this mine:" );
1882 //NO_UI!!! NameText = ui_add_gadget_inputbox( NameWindow, 10, 30, LEVEL_NAME_LEN, LEVEL_NAME_LEN, Current_level_name );
1884 //NO_UI!!! NameWindow->keyboard_focus_gadget = (UI_GADGET *)NameText;
1885 //NO_UI!!! QuitButton->hotkey = KEY_ENTER;
1887 //NO_UI!!! ui_gadget_calc_keys(NameWindow);
1889 //NO_UI!!! while (!QuitButton->pressed && last_keypress!=KEY_ENTER) {
1890 //NO_UI!!! ui_mega_process();
1891 //NO_UI!!! ui_window_do_gadgets(NameWindow);
1894 //NO_UI!!! strcpy( Current_level_name, NameText->text );
1896 //NO_UI!!! if ( NameWindow!=NULL ) {
1897 //NO_UI!!! ui_close_window( NameWindow );
1898 //NO_UI!!! NameWindow = NULL;
1904 m[0].type = NM_TYPE_TEXT; m[0].text = "Please enter a name for this mine:";
1905 m[1].type = NM_TYPE_INPUT; m[1].text = Current_level_name; m[1].text_len = LEVEL_NAME_LEN;
1907 newmenu_do( NULL, "Enter mine name", 2, m, NULL );
1917 // -----------------------------------------------------------------------------
1918 int compute_num_delta_light_records(void)
1923 for (i=0; i<Num_static_lights; i++) {
1924 total += Dl_indices[i].count;
1931 // -----------------------------------------------------------------------------
1933 int save_game_data(FILE * SaveFile)
1935 int player_offset, object_offset, walls_offset, doors_offset, triggers_offset, control_offset, matcen_offset; //, links_offset;
1936 int dl_indices_offset, delta_light_offset;
1937 int start_offset,end_offset;
1939 start_offset = ftell(SaveFile);
1941 //===================== SAVE FILE INFO ========================
1943 game_fileinfo.fileinfo_signature = 0x6705;
1944 game_fileinfo.fileinfo_version = GAME_VERSION;
1945 game_fileinfo.level = Current_level_num;
1946 game_fileinfo.fileinfo_sizeof = sizeof(game_fileinfo);
1947 game_fileinfo.player_offset = -1;
1948 game_fileinfo.player_sizeof = sizeof(player);
1949 game_fileinfo.object_offset = -1;
1950 game_fileinfo.object_howmany = Highest_object_index+1;
1951 game_fileinfo.object_sizeof = sizeof(object);
1952 game_fileinfo.walls_offset = -1;
1953 game_fileinfo.walls_howmany = Num_walls;
1954 game_fileinfo.walls_sizeof = sizeof(wall);
1955 game_fileinfo.doors_offset = -1;
1956 game_fileinfo.doors_howmany = Num_open_doors;
1957 game_fileinfo.doors_sizeof = sizeof(active_door);
1958 game_fileinfo.triggers_offset = -1;
1959 game_fileinfo.triggers_howmany = Num_triggers;
1960 game_fileinfo.triggers_sizeof = sizeof(trigger);
1961 game_fileinfo.control_offset = -1;
1962 game_fileinfo.control_howmany = 1;
1963 game_fileinfo.control_sizeof = sizeof(control_center_triggers);
1964 game_fileinfo.matcen_offset = -1;
1965 game_fileinfo.matcen_howmany = Num_robot_centers;
1966 game_fileinfo.matcen_sizeof = sizeof(matcen_info);
1968 game_fileinfo.dl_indices_offset = -1;
1969 game_fileinfo.dl_indices_howmany = Num_static_lights;
1970 game_fileinfo.dl_indices_sizeof = sizeof(dl_index);
1972 game_fileinfo.delta_light_offset = -1;
1973 game_fileinfo.delta_light_howmany = compute_num_delta_light_records();
1974 game_fileinfo.delta_light_sizeof = sizeof(delta_light);
1976 // Write the fileinfo
1977 fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
1979 // Write the mine name
1980 fprintf(SaveFile,"%s\n",Current_level_name);
1982 fwrite(&N_polygon_models,2,1,SaveFile);
1983 fwrite(Pof_names,N_polygon_models,sizeof(*Pof_names),SaveFile);
1985 //==================== SAVE PLAYER INFO ===========================
1987 player_offset = ftell(SaveFile);
1988 fwrite( &Players[Player_num], sizeof(player), 1, SaveFile );
1990 //==================== SAVE OBJECT INFO ===========================
1992 object_offset = ftell(SaveFile);
1993 //fwrite( &Objects, sizeof(object), game_fileinfo.object_howmany, SaveFile );
1996 for (i=0;i<game_fileinfo.object_howmany;i++)
1997 write_object(&Objects[i],SaveFile);
2000 //==================== SAVE WALL INFO =============================
2002 walls_offset = ftell(SaveFile);
2003 fwrite( Walls, sizeof(wall), game_fileinfo.walls_howmany, SaveFile );
2005 //==================== SAVE DOOR INFO =============================
2007 doors_offset = ftell(SaveFile);
2008 fwrite( ActiveDoors, sizeof(active_door), game_fileinfo.doors_howmany, SaveFile );
2010 //==================== SAVE TRIGGER INFO =============================
2012 triggers_offset = ftell(SaveFile);
2013 fwrite( Triggers, sizeof(trigger), game_fileinfo.triggers_howmany, SaveFile );
2015 //================ SAVE CONTROL CENTER TRIGGER INFO ===============
2017 control_offset = ftell(SaveFile);
2018 fwrite( &ControlCenterTriggers, sizeof(control_center_triggers), 1, SaveFile );
2021 //================ SAVE MATERIALIZATION CENTER TRIGGER INFO ===============
2023 matcen_offset = ftell(SaveFile);
2024 // mprintf((0, "Writing %i materialization centers\n", game_fileinfo.matcen_howmany));
2026 // for (i=0; i<game_fileinfo.matcen_howmany; i++)
2027 // mprintf((0, " %i: robot_flags = %08x\n", i, RobotCenters[i].robot_flags));
2029 fwrite( RobotCenters, sizeof(matcen_info), game_fileinfo.matcen_howmany, SaveFile );
2031 //================ SAVE DELTA LIGHT INFO ===============
2032 dl_indices_offset = ftell(SaveFile);
2033 fwrite( Dl_indices, sizeof(dl_index), game_fileinfo.dl_indices_howmany, SaveFile );
2035 delta_light_offset = ftell(SaveFile);
2036 fwrite( Delta_lights, sizeof(delta_light), game_fileinfo.delta_light_howmany, SaveFile );
2038 //============= REWRITE FILE INFO, TO SAVE OFFSETS ===============
2040 // Update the offset fields
2041 game_fileinfo.player_offset = player_offset;
2042 game_fileinfo.object_offset = object_offset;
2043 game_fileinfo.walls_offset = walls_offset;
2044 game_fileinfo.doors_offset = doors_offset;
2045 game_fileinfo.triggers_offset = triggers_offset;
2046 game_fileinfo.control_offset = control_offset;
2047 game_fileinfo.matcen_offset = matcen_offset;
2048 game_fileinfo.dl_indices_offset = dl_indices_offset;
2049 game_fileinfo.delta_light_offset = delta_light_offset;
2052 end_offset = ftell(SaveFile);
2054 // Write the fileinfo
2055 fseek( SaveFile, start_offset, SEEK_SET ); // Move to TOF
2056 fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
2058 // Go back to end of data
2059 fseek(SaveFile,end_offset,SEEK_SET);
2064 int save_mine_data(FILE * SaveFile);
2066 // -----------------------------------------------------------------------------
2068 int save_level_sub(char * filename, int compiled_version)
2071 char temp_filename[128];
2072 int sig = 'PLVL',version=LEVEL_FILE_VERSION;
2073 int minedata_offset,gamedata_offset;
2075 if ( !compiled_version ) {
2076 write_game_text_file(filename);
2078 if (Errors_in_mine) {
2079 if (is_real_level(filename)) {
2080 char ErrorMessage[200];
2082 sprintf( ErrorMessage, "Warning: %i errors in this mine!\n", Errors_in_mine );
2084 gr_palette_load(gr_palette);
2086 if (nm_messagebox( NULL, 2, "Cancel Save", "Save", ErrorMessage )!=1) {
2092 mprintf((1, "Error: %i errors in this mine. See the 'txm' file.\n", Errors_in_mine));
2094 change_filename_extension(temp_filename,filename,".LVL");
2098 // macs are using the regular hog/rl2 files for shareware
2099 #if defined(SHAREWARE) && !defined(MACINTOSH)
2100 change_filename_extension(temp_filename,filename,".SL2");
2102 change_filename_extension(temp_filename,filename,".RL2");
2106 SaveFile = fopen( temp_filename, "wb" );
2109 char ErrorMessage[256];
2112 _splitpath( temp_filename, NULL, NULL, fname, NULL );
2114 sprintf( ErrorMessage, \
2115 "ERROR: Cannot write to '%s'.\nYou probably need to check out a locked\nversion of the file. You should save\nthis under a different filename, and then\ncheck out a locked copy by typing\n\'co -l %s.lvl'\nat the DOS prompt.\n"
2116 , temp_filename, fname );
2118 gr_palette_load(gr_palette);
2119 nm_messagebox( NULL, 1, "Ok", ErrorMessage );
2124 if (Current_level_name[0] == 0)
2125 strcpy(Current_level_name,"Untitled");
2127 clear_transient_objects(1); //1 means clear proximity bombs
2129 compress_objects(); //after this, Highest_object_index == num objects
2131 //make sure player is in a segment
2132 if (update_object_seg(&Objects[Players[0].objnum]) == 0) {
2133 if (ConsoleObject->segnum > Highest_segment_index)
2134 ConsoleObject->segnum = 0;
2135 compute_segment_center(&ConsoleObject->pos,&(Segments[ConsoleObject->segnum]));
2142 gs_write_int(sig,SaveFile);
2143 gs_write_int(version,SaveFile);
2146 gs_write_int(minedata_offset,SaveFile);
2147 gs_write_int(gamedata_offset,SaveFile);
2149 //Now write the damn data
2151 //write the version 8 data (to make file unreadable by 1.0 & 1.1)
2152 gs_write_int(GameTime,SaveFile);
2153 gs_write_short(FrameCount,SaveFile);
2154 gs_write_byte(FrameTime,SaveFile);
2156 // Write the palette file name
2157 fprintf(SaveFile,"%s\n",Current_level_palette);
2159 gs_write_int(Base_control_center_explosion_time,SaveFile);
2160 gs_write_int(Reactor_strength,SaveFile);
2162 gs_write_int(Num_flickering_lights,SaveFile);
2163 fwrite(Flickering_lights,sizeof(*Flickering_lights),Num_flickering_lights,SaveFile);
2165 gs_write_int(Secret_return_segment, SaveFile);
2166 gs_write_int(Secret_return_orient.rvec.x, SaveFile);
2167 gs_write_int(Secret_return_orient.rvec.y, SaveFile);
2168 gs_write_int(Secret_return_orient.rvec.z, SaveFile);
2169 gs_write_int(Secret_return_orient.fvec.x, SaveFile);
2170 gs_write_int(Secret_return_orient.fvec.y, SaveFile);
2171 gs_write_int(Secret_return_orient.fvec.z, SaveFile);
2172 gs_write_int(Secret_return_orient.uvec.x, SaveFile);
2173 gs_write_int(Secret_return_orient.uvec.y, SaveFile);
2174 gs_write_int(Secret_return_orient.uvec.z, SaveFile);
2176 minedata_offset = ftell(SaveFile);
2177 if ( !compiled_version )
2178 save_mine_data(SaveFile);
2180 save_mine_data_compiled(SaveFile);
2181 gamedata_offset = ftell(SaveFile);
2182 save_game_data(SaveFile);
2184 fseek(SaveFile,sizeof(sig)+sizeof(version),SEEK_SET);
2185 gs_write_int(minedata_offset,SaveFile);
2186 gs_write_int(gamedata_offset,SaveFile);
2188 //==================== CLOSE THE FILE =============================
2191 if ( !compiled_version ) {
2192 if (Function_mode == FMODE_EDITOR)
2193 editor_status("Saved mine %s, \"%s\"",filename,Current_level_name);
2200 extern void compress_uv_coordinates_all(void);
2202 int save_level(char * filename)
2206 // Save normal version...
2207 r1 = save_level_sub(filename, 0);
2209 // Save compiled version...
2210 save_level_sub(filename, 1);
2218 void dump_mine_info(void)
2220 int segnum, sidenum;
2221 fix min_u, max_u, min_v, max_v, min_l, max_l, max_sl;
2233 for (segnum=0; segnum<=Highest_segment_index; segnum++) {
2234 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
2236 side *sidep = &Segments[segnum].sides[sidenum];
2238 if (Segment2s[segnum].static_light > max_sl)
2239 max_sl = Segment2s[segnum].static_light;
2241 for (vertnum=0; vertnum<4; vertnum++) {
2242 if (sidep->uvls[vertnum].u < min_u)
2243 min_u = sidep->uvls[vertnum].u;
2244 else if (sidep->uvls[vertnum].u > max_u)
2245 max_u = sidep->uvls[vertnum].u;
2247 if (sidep->uvls[vertnum].v < min_v)
2248 min_v = sidep->uvls[vertnum].v;
2249 else if (sidep->uvls[vertnum].v > max_v)
2250 max_v = sidep->uvls[vertnum].v;
2252 if (sidep->uvls[vertnum].l < min_l)
2253 min_l = sidep->uvls[vertnum].l;
2254 else if (sidep->uvls[vertnum].l > max_l)
2255 max_l = sidep->uvls[vertnum].l;
2261 // mprintf((0, "Smallest uvl = %7.3f %7.3f %7.3f. Largest uvl = %7.3f %7.3f %7.3f\n", f2fl(min_u), f2fl(min_v), f2fl(min_l), f2fl(max_u), f2fl(max_v), f2fl(max_l)));
2262 // mprintf((0, "Static light maximum = %7.3f\n", f2fl(max_sl)));
2263 // mprintf((0, "Number of walls: %i\n", Num_walls));
2271 //read in every level in mission and save out compiled version
2272 void save_all_compiled_levels(void)
2274 do_load_save_levels(1);
2277 //read in every level in mission
2278 void load_all_levels(void)
2280 do_load_save_levels(0);
2284 void do_load_save_levels(int save)
2288 if (! SafetyCheck())
2291 no_old_level_file_error=1;
2293 for (level_num=1;level_num<=Last_level;level_num++) {
2294 load_level(Level_names[level_num-1]);
2295 load_palette(Current_level_palette,1,1); //don't change screen
2297 save_level_sub(Level_names[level_num-1],1);
2300 for (level_num=-1;level_num>=Last_secret_level;level_num--) {
2301 load_level(Secret_level_names[-level_num-1]);
2302 load_palette(Current_level_palette,1,1); //don't change screen
2304 save_level_sub(Secret_level_names[-level_num-1],1);
2307 no_old_level_file_error=0;