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.
19 char gamesave_rcsid[] = "$Id: gamesave.c,v 1.4 2001-01-31 15:17:53 bradleyb Exp $";
35 #include "editor\editor.h"
66 char Gamesave_current_filename[128];
68 #define GAME_VERSION 32
69 #define GAME_COMPATIBLE_VERSION 22
71 //version 28->29 add delta light support
72 //version 27->28 controlcen id now is reactor number, not model number
74 //version 29->30 changed trigger structure
75 //version 30->31 changed trigger structure some more
76 //version 31->32 change segment structure, make it 512 bytes w/o editor, add Segment2s array.
78 #define MENU_CURSOR_X_MIN MENU_X
79 #define MENU_CURSOR_X_MAX MENU_X+6
81 //Start old wall structures
83 typedef struct v16_wall {
84 byte type; // What kind of special wall.
85 byte flags; // Flags for the wall.
86 fix hps; // "Hit points" of the wall.
87 byte trigger; // Which trigger is associated with the wall.
88 byte clip_num; // Which animation associated with the wall.
92 typedef struct v19_wall {
93 int segnum,sidenum; // Seg & side for this wall
94 byte type; // What kind of special wall.
95 byte flags; // Flags for the wall.
96 fix hps; // "Hit points" of the wall.
97 byte trigger; // Which trigger is associated with the wall.
98 byte clip_num; // Which animation associated with the wall.
100 int linked_wall; // number of linked wall
103 typedef struct v19_door {
104 int n_parts; // for linked walls
105 short seg[2]; // Segment pointer of door.
106 short side[2]; // Side number of door.
107 short type[2]; // What kind of door animation.
108 fix open; // How long it has been open.
111 //End old wall structures
113 //old trigger structs
115 typedef struct v29_trigger {
122 short seg[MAX_WALLS_PER_LINK];
123 short side[MAX_WALLS_PER_LINK];
126 typedef struct v30_trigger {
129 byte pad; //keep alignment
132 short seg[MAX_WALLS_PER_LINK];
133 short side[MAX_WALLS_PER_LINK];
136 //flags for V30 & below triggers
137 #define TRIGGER_CONTROL_DOORS 1 // Control Trigger
138 #define TRIGGER_SHIELD_DAMAGE 2 // Shield Damage Trigger
139 #define TRIGGER_ENERGY_DRAIN 4 // Energy Drain Trigger
140 #define TRIGGER_EXIT 8 // End of level Trigger
141 #define TRIGGER_ON 16 // Whether Trigger is active
142 #define TRIGGER_ONE_SHOT 32 // If Trigger can only be triggered once
143 #define TRIGGER_MATCEN 64 // Trigger for materialization centers
144 #define TRIGGER_ILLUSION_OFF 128 // Switch Illusion OFF trigger
145 #define TRIGGER_SECRET_EXIT 256 // Exit to secret level
146 #define TRIGGER_ILLUSION_ON 512 // Switch Illusion ON trigger
147 #define TRIGGER_UNLOCK_DOORS 1024 // Unlocks a door
148 #define TRIGGER_OPEN_WALL 2048 // Makes a wall open
149 #define TRIGGER_CLOSE_WALL 4096 // Makes a wall closed
150 #define TRIGGER_ILLUSORY_WALL 8192 // Makes a wall illusory
153 ushort fileinfo_signature;
154 ushort fileinfo_version;
156 } game_top_fileinfo; // Should be same as first two fields below...
159 ushort fileinfo_signature;
160 ushort fileinfo_version;
162 char mine_filename[15];
164 int player_offset; // Player info
166 int object_offset; // Object info
176 int triggers_howmany;
187 int dl_indices_offset;
188 int dl_indices_howmany;
189 int dl_indices_sizeof;
190 int delta_light_offset;
191 int delta_light_howmany;
192 int delta_light_sizeof;
195 // LINT: adding function prototypes
196 void read_object(object *obj, CFILE *f, int version);
197 void write_object(object *obj, FILE *f);
198 void dump_mine_info(void);
200 extern char MaxPowerupsAllowed[MAX_POWERUP_TYPES];
201 extern char PowerupsInMine[MAX_POWERUP_TYPES];
204 extern char mine_filename[];
205 extern int save_mine_data_compiled(FILE * SaveFile);
207 //--unused-- char mine_filename[128];
210 int Gamesave_num_org_robots = 0;
211 //--unused-- grs_bitmap * Gamesave_saved_bitmap = NULL;
214 // Return true if this level has a name of the form "level??"
215 // Note that a pathspec can appear at the beginning of the filename.
216 int is_real_level(char *filename)
218 int len = strlen(filename);
223 //mprintf((0, "String = [%s]\n", &filename[len-11]));
224 return !strnicmp(&filename[len-11], "level", 5);
229 void change_filename_extension( char *dest, char *src, char *new_ext )
238 for (i=1; i<strlen(dest); i++ )
239 if (dest[i]=='.'||dest[i]==' '||dest[i]==0)
244 dest[i+1]=new_ext[0];
245 dest[i+2]=new_ext[1];
246 dest[i+3]=new_ext[2];
252 //--unused-- vms_angvec zero_angles={0,0,0};
254 #define vm_angvec_zero(v) do {(v)->p=(v)->b=(v)->h=0;} while (0)
256 int Gamesave_num_players=0;
258 int N_save_pof_names;
259 char Save_pof_names[MAX_POLYGON_MODELS][FILENAME_LEN];
261 void check_and_fix_matrix(vms_matrix *m);
263 void verify_object( object * obj ) {
265 obj->lifeleft = IMMORTAL_TIME; //all loaded object are immortal, for now
267 if ( obj->type == OBJ_ROBOT ) {
268 Gamesave_num_org_robots++;
270 // Make sure valid id...
271 if ( obj->id >= N_robot_types )
272 obj->id = obj->id % N_robot_types;
274 // Make sure model number & size are correct...
275 if ( obj->render_type == RT_POLYOBJ ) {
276 Assert(Robot_info[obj->id].model_num != -1);
277 //if you fail this assert, it means that a robot in this level
278 //hasn't been loaded, possibly because he's marked as
279 //non-shareware. To see what robot number, print obj->id.
281 Assert(Robot_info[obj->id].always_0xabcd == 0xabcd);
282 //if you fail this assert, it means that the robot_ai for
283 //a robot in this level hasn't been loaded, possibly because
284 //it's marked as non-shareware. To see what robot number,
287 obj->rtype.pobj_info.model_num = Robot_info[obj->id].model_num;
288 obj->size = Polygon_models[obj->rtype.pobj_info.model_num].rad;
290 //@@Took out this ugly hack 1/12/96, because Mike has added code
291 //@@that should fix it in a better way.
292 //@@//this is a super-ugly hack. Since the baby stripe robots have
293 //@@//their firing point on their bounding sphere, the firing points
294 //@@//can poke through a wall if the robots are very close to it. So
295 //@@//we make their radii bigger so the guns can't get too close to
297 //@@if (Robot_info[obj->id].flags & RIF_BIG_RADIUS)
298 //@@ obj->size = (obj->size*3)/2;
300 //@@if (obj->control_type==CT_AI && Robot_info[obj->id].attack_type)
301 //@@ obj->size = obj->size*3/4;
304 if (obj->id == 65) //special "reactor" robots
305 obj->movement_type = MT_NONE;
307 if (obj->movement_type == MT_PHYSICS) {
308 obj->mtype.phys_info.mass = Robot_info[obj->id].mass;
309 obj->mtype.phys_info.drag = Robot_info[obj->id].drag;
312 else { //Robots taken care of above
314 if ( obj->render_type == RT_POLYOBJ ) {
316 char *name = Save_pof_names[obj->rtype.pobj_info.model_num];
318 for (i=0;i<N_polygon_models;i++)
319 if (!stricmp(Pof_names[i],name)) { //found it!
320 // mprintf((0,"Mapping <%s> to %d (was %d)\n",name,i,obj->rtype.pobj_info.model_num));
321 obj->rtype.pobj_info.model_num = i;
327 if ( obj->type == OBJ_POWERUP ) {
328 if ( obj->id >= N_powerup_types ) {
330 Assert( obj->render_type != RT_POLYOBJ );
332 obj->control_type = CT_POWERUP;
333 obj->size = Powerup_info[obj->id].size;
334 obj->ctype.powerup_info.creation_time = 0;
337 if (Game_mode & GM_NETWORK)
339 if (multi_powerup_is_4pack(obj->id))
341 PowerupsInMine[obj->id-1]+=4;
342 MaxPowerupsAllowed[obj->id-1]+=4;
344 PowerupsInMine[obj->id]++;
345 MaxPowerupsAllowed[obj->id]++;
346 mprintf ((0,"PowerupLimiter: ID=%d\n",obj->id));
347 if (obj->id>MAX_POWERUP_TYPES)
348 mprintf ((1,"POWERUP: Overwriting array bounds!! Get JL!\n"));
354 if ( obj->type == OBJ_WEAPON ) {
355 if ( obj->id >= N_weapon_types ) {
357 Assert( obj->render_type != RT_POLYOBJ );
360 if (obj->id == PMINE_ID) { //make sure pmines have correct values
362 obj->mtype.phys_info.mass = Weapon_info[obj->id].mass;
363 obj->mtype.phys_info.drag = Weapon_info[obj->id].drag;
364 obj->mtype.phys_info.flags |= PF_FREE_SPINNING;
366 // Make sure model number & size are correct...
367 Assert( obj->render_type == RT_POLYOBJ );
369 obj->rtype.pobj_info.model_num = Weapon_info[obj->id].model_num;
370 obj->size = Polygon_models[obj->rtype.pobj_info.model_num].rad;
374 if ( obj->type == OBJ_CNTRLCEN ) {
376 obj->render_type = RT_POLYOBJ;
377 obj->control_type = CT_CNTRLCEN;
379 //@@// Make model number is correct...
380 //@@for (i=0; i<Num_total_object_types; i++ )
381 //@@ if ( ObjType[i] == OL_CONTROL_CENTER ) {
382 //@@ obj->rtype.pobj_info.model_num = ObjId[i];
383 //@@ obj->shields = ObjStrength[i];
390 // Check, and set, strength of reactor
391 for (i=0; i<Num_total_object_types; i++ )
392 if ( ObjType[i]==OL_CONTROL_CENTER && ObjId[i] == obj->id ) {
393 obj->shields = ObjStrength[i];
396 Assert(i < Num_total_object_types); //make sure we found it
401 if ( obj->type == OBJ_PLAYER ) {
404 //Assert(obj == Player);
406 if ( obj == ConsoleObject )
407 init_player_object();
409 if (obj->render_type == RT_POLYOBJ) //recover from Matt's pof file matchup bug
410 obj->rtype.pobj_info.model_num = Player_ship->model_num;
412 //Make sure orient matrix is orthogonal
413 check_and_fix_matrix(&obj->orient);
415 obj->id = Gamesave_num_players++;
418 if (obj->type == OBJ_HOSTAGE) {
420 //@@if (obj->id > N_hostage_types)
423 obj->render_type = RT_HOSTAGE;
424 obj->control_type = CT_POWERUP;
429 static int read_int(CFILE *file)
433 if (cfread( &i, sizeof(i), 1, file) != 1)
434 Error( "Error reading int in gamesave.c" );
440 static fix read_fix(CFILE *file)
444 if (cfread( &f, sizeof(f), 1, file) != 1)
445 Error( "Error reading fix in gamesave.c" );
447 f = (fix)INTEL_INT((int)f);
451 static short read_short(CFILE *file)
455 if (cfread( &s, sizeof(s), 1, file) != 1)
456 Error( "Error reading short in gamesave.c" );
462 static short read_fixang(CFILE *file)
466 if (cfread( &f, sizeof(f), 1, file) != 1)
467 Error( "Error reading fixang in gamesave.c" );
469 f = (fixang)INTEL_SHORT((short)f);
473 static byte read_byte(CFILE *file)
477 if (cfread( &b, sizeof(b), 1, file) != 1)
478 Error( "Error reading byte in gamesave.c" );
483 static void read_vector(vms_vector *v,CFILE *file)
485 v->x = read_fix(file);
486 v->y = read_fix(file);
487 v->z = read_fix(file);
490 static void read_matrix(vms_matrix *m,CFILE *file)
492 read_vector(&m->rvec,file);
493 read_vector(&m->uvec,file);
494 read_vector(&m->fvec,file);
497 static void read_angvec(vms_angvec *v,CFILE *file)
499 v->p = read_fixang(file);
500 v->b = read_fixang(file);
501 v->h = read_fixang(file);
504 //static gs_skip(int len,CFILE *file)
507 // cfseek(file,len,SEEK_CUR);
511 static void gs_write_int(int i,FILE *file)
513 if (fwrite( &i, sizeof(i), 1, file) != 1)
514 Error( "Error reading int in gamesave.c" );
518 static void gs_write_fix(fix f,FILE *file)
520 if (fwrite( &f, sizeof(f), 1, file) != 1)
521 Error( "Error reading fix in gamesave.c" );
525 static void gs_write_short(short s,FILE *file)
527 if (fwrite( &s, sizeof(s), 1, file) != 1)
528 Error( "Error reading short in gamesave.c" );
532 static void gs_write_fixang(fixang f,FILE *file)
534 if (fwrite( &f, sizeof(f), 1, file) != 1)
535 Error( "Error reading fixang in gamesave.c" );
539 static void gs_write_byte(byte b,FILE *file)
541 if (fwrite( &b, sizeof(b), 1, file) != 1)
542 Error( "Error reading byte in gamesave.c" );
546 static void gr_write_vector(vms_vector *v,FILE *file)
548 gs_write_fix(v->x,file);
549 gs_write_fix(v->y,file);
550 gs_write_fix(v->z,file);
553 static void gs_write_matrix(vms_matrix *m,FILE *file)
555 gr_write_vector(&m->rvec,file);
556 gr_write_vector(&m->uvec,file);
557 gr_write_vector(&m->fvec,file);
560 static void gs_write_angvec(vms_angvec *v,FILE *file)
562 gs_write_fixang(v->p,file);
563 gs_write_fixang(v->b,file);
564 gs_write_fixang(v->h,file);
570 extern int multi_powerup_is_4pack(int);
571 //reads one object of the given version from the given file
572 void read_object(object *obj,CFILE *f,int version)
575 obj->type = read_byte(f);
576 obj->id = read_byte(f);
578 if (obj->type == OBJ_CNTRLCEN && version<28)
579 obj->id = 0; //used to be only one kind of reactor
581 obj->control_type = read_byte(f);
582 obj->movement_type = read_byte(f);
583 obj->render_type = read_byte(f);
584 obj->flags = read_byte(f);
586 obj->segnum = read_short(f);
587 obj->attached_obj = -1;
589 read_vector(&obj->pos,f);
590 read_matrix(&obj->orient,f);
592 obj->size = read_fix(f);
593 obj->shields = read_fix(f);
595 read_vector(&obj->last_pos,f);
597 obj->contains_type = read_byte(f);
598 obj->contains_id = read_byte(f);
599 obj->contains_count = read_byte(f);
601 switch (obj->movement_type) {
605 read_vector(&obj->mtype.phys_info.velocity,f);
606 read_vector(&obj->mtype.phys_info.thrust,f);
608 obj->mtype.phys_info.mass = read_fix(f);
609 obj->mtype.phys_info.drag = read_fix(f);
610 obj->mtype.phys_info.brakes = read_fix(f);
612 read_vector(&obj->mtype.phys_info.rotvel,f);
613 read_vector(&obj->mtype.phys_info.rotthrust,f);
615 obj->mtype.phys_info.turnroll = read_fixang(f);
616 obj->mtype.phys_info.flags = read_short(f);
622 read_vector(&obj->mtype.spin_rate,f);
632 switch (obj->control_type) {
637 obj->ctype.ai_info.behavior = read_byte(f);
639 for (i=0;i<MAX_AI_FLAGS;i++)
640 obj->ctype.ai_info.flags[i] = read_byte(f);
642 obj->ctype.ai_info.hide_segment = read_short(f);
643 obj->ctype.ai_info.hide_index = read_short(f);
644 obj->ctype.ai_info.path_length = read_short(f);
645 obj->ctype.ai_info.cur_path_index = read_short(f);
648 read_short(f); // obj->ctype.ai_info.follow_path_start_seg =
649 read_short(f); // obj->ctype.ai_info.follow_path_end_seg =
657 obj->ctype.expl_info.spawn_time = read_fix(f);
658 obj->ctype.expl_info.delete_time = read_fix(f);
659 obj->ctype.expl_info.delete_objnum = read_short(f);
660 obj->ctype.expl_info.next_attach = obj->ctype.expl_info.prev_attach = obj->ctype.expl_info.attach_parent = -1;
666 //do I really need to read these? Are they even saved to disk?
668 obj->ctype.laser_info.parent_type = read_short(f);
669 obj->ctype.laser_info.parent_num = read_short(f);
670 obj->ctype.laser_info.parent_signature = read_int(f);
676 obj->ctype.light_info.intensity = read_fix(f);
682 obj->ctype.powerup_info.count = read_int(f);
684 obj->ctype.powerup_info.count = 1;
686 if (obj->id == POW_VULCAN_WEAPON)
687 obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
689 if (obj->id == POW_GAUSS_WEAPON)
690 obj->ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
692 if (obj->id == POW_OMEGA_WEAPON)
693 obj->ctype.powerup_info.count = MAX_OMEGA_CHARGE;
703 case CT_SLEW: //the player is generally saved as slew
717 switch (obj->render_type) {
726 obj->rtype.pobj_info.model_num = read_int(f);
728 for (i=0;i<MAX_SUBMODELS;i++)
729 read_angvec(&obj->rtype.pobj_info.anim_angles[i],f);
731 obj->rtype.pobj_info.subobj_flags = read_int(f);
736 obj->rtype.pobj_info.tmap_override = tmo;
739 obj->rtype.pobj_info.tmap_override = -1;
741 int xlated_tmo = tmap_xlate_table[tmo];
742 if (xlated_tmo < 0) {
743 mprintf( (0, "Couldn't find texture for demo object, model_num = %d\n", obj->rtype.pobj_info.model_num));
747 obj->rtype.pobj_info.tmap_override = xlated_tmo;
751 obj->rtype.pobj_info.alt_textures = 0;
756 case RT_WEAPON_VCLIP:
761 obj->rtype.vclip_info.vclip_num = read_int(f);
762 obj->rtype.vclip_info.frametime = read_fix(f);
763 obj->rtype.vclip_info.framenum = read_byte(f);
779 //writes one object to the given file
780 void write_object(object *obj,FILE *f)
782 gs_write_byte(obj->type,f);
783 gs_write_byte(obj->id,f);
785 gs_write_byte(obj->control_type,f);
786 gs_write_byte(obj->movement_type,f);
787 gs_write_byte(obj->render_type,f);
788 gs_write_byte(obj->flags,f);
790 gs_write_short(obj->segnum,f);
792 gr_write_vector(&obj->pos,f);
793 gs_write_matrix(&obj->orient,f);
795 gs_write_fix(obj->size,f);
796 gs_write_fix(obj->shields,f);
798 gr_write_vector(&obj->last_pos,f);
800 gs_write_byte(obj->contains_type,f);
801 gs_write_byte(obj->contains_id,f);
802 gs_write_byte(obj->contains_count,f);
804 switch (obj->movement_type) {
808 gr_write_vector(&obj->mtype.phys_info.velocity,f);
809 gr_write_vector(&obj->mtype.phys_info.thrust,f);
811 gs_write_fix(obj->mtype.phys_info.mass,f);
812 gs_write_fix(obj->mtype.phys_info.drag,f);
813 gs_write_fix(obj->mtype.phys_info.brakes,f);
815 gr_write_vector(&obj->mtype.phys_info.rotvel,f);
816 gr_write_vector(&obj->mtype.phys_info.rotthrust,f);
818 gs_write_fixang(obj->mtype.phys_info.turnroll,f);
819 gs_write_short(obj->mtype.phys_info.flags,f);
825 gr_write_vector(&obj->mtype.spin_rate,f);
835 switch (obj->control_type) {
840 gs_write_byte(obj->ctype.ai_info.behavior,f);
842 for (i=0;i<MAX_AI_FLAGS;i++)
843 gs_write_byte(obj->ctype.ai_info.flags[i],f);
845 gs_write_short(obj->ctype.ai_info.hide_segment,f);
846 gs_write_short(obj->ctype.ai_info.hide_index,f);
847 gs_write_short(obj->ctype.ai_info.path_length,f);
848 gs_write_short(obj->ctype.ai_info.cur_path_index,f);
850 // -- unused! mk, 08/13/95 -- gs_write_short(obj->ctype.ai_info.follow_path_start_seg,f);
851 // -- unused! mk, 08/13/95 -- gs_write_short(obj->ctype.ai_info.follow_path_end_seg,f);
858 gs_write_fix(obj->ctype.expl_info.spawn_time,f);
859 gs_write_fix(obj->ctype.expl_info.delete_time,f);
860 gs_write_short(obj->ctype.expl_info.delete_objnum,f);
866 //do I really need to write these objects?
868 gs_write_short(obj->ctype.laser_info.parent_type,f);
869 gs_write_short(obj->ctype.laser_info.parent_num,f);
870 gs_write_int(obj->ctype.laser_info.parent_signature,f);
876 gs_write_fix(obj->ctype.light_info.intensity,f);
881 gs_write_int(obj->ctype.powerup_info.count,f);
889 case CT_SLEW: //the player is generally saved as slew
893 break; //control center object.
903 switch (obj->render_type) {
912 gs_write_int(obj->rtype.pobj_info.model_num,f);
914 for (i=0;i<MAX_SUBMODELS;i++)
915 gs_write_angvec(&obj->rtype.pobj_info.anim_angles[i],f);
917 gs_write_int(obj->rtype.pobj_info.subobj_flags,f);
919 gs_write_int(obj->rtype.pobj_info.tmap_override,f);
924 case RT_WEAPON_VCLIP:
929 gs_write_int(obj->rtype.vclip_info.vclip_num,f);
930 gs_write_fix(obj->rtype.vclip_info.frametime,f);
931 gs_write_byte(obj->rtype.vclip_info.framenum,f);
947 int robot_flags; // Up to 32 different robots
948 fix hit_points; // How hard it is to destroy this particular matcen
949 fix interval; // Interval between materialogrifizations
950 short segnum; // Segment this is attached to.
951 short fuelcen_num; // Index in fuelcen array.
954 extern int remove_trigger_num(int trigger_num);
956 // -----------------------------------------------------------------------------
958 // Loads all the relevant data for a level.
959 // If level != -1, it loads the filename with extension changed to .min
960 // Otherwise it loads the appropriate level mine.
961 // returns 0=everything ok, 1=old version, -1=error
962 int load_game_data(CFILE *LoadFile)
967 start_offset = cftell(LoadFile);
969 //===================== READ FILE INFO ========================
971 // Set default values
972 game_fileinfo.level = -1;
973 game_fileinfo.player_offset = -1;
974 game_fileinfo.player_sizeof = sizeof(player);
975 game_fileinfo.object_offset = -1;
976 game_fileinfo.object_howmany = 0;
977 game_fileinfo.object_sizeof = sizeof(object);
978 game_fileinfo.walls_offset = -1;
979 game_fileinfo.walls_howmany = 0;
980 game_fileinfo.walls_sizeof = sizeof(wall);
981 game_fileinfo.doors_offset = -1;
982 game_fileinfo.doors_howmany = 0;
983 game_fileinfo.doors_sizeof = sizeof(active_door);
984 game_fileinfo.triggers_offset = -1;
985 game_fileinfo.triggers_howmany = 0;
986 game_fileinfo.triggers_sizeof = sizeof(trigger);
987 game_fileinfo.control_offset = -1;
988 game_fileinfo.control_howmany = 0;
989 game_fileinfo.control_sizeof = sizeof(control_center_triggers);
990 game_fileinfo.matcen_offset = -1;
991 game_fileinfo.matcen_howmany = 0;
992 game_fileinfo.matcen_sizeof = sizeof(matcen_info);
994 game_fileinfo.dl_indices_offset = -1;
995 game_fileinfo.dl_indices_howmany = 0;
996 game_fileinfo.dl_indices_sizeof = sizeof(dl_index);
998 game_fileinfo.delta_light_offset = -1;
999 game_fileinfo.delta_light_howmany = 0;
1000 game_fileinfo.delta_light_sizeof = sizeof(delta_light);
1002 // Read in game_top_fileinfo to get size of saved fileinfo.
1004 if (cfseek( LoadFile, start_offset, SEEK_SET ))
1005 Error( "Error seeking in gamesave.c" );
1007 // if (cfread( &game_top_fileinfo, sizeof(game_top_fileinfo), 1, LoadFile) != 1)
1008 // Error( "Error reading game_top_fileinfo in gamesave.c" );
1010 game_top_fileinfo.fileinfo_signature = read_short(LoadFile);
1011 game_top_fileinfo.fileinfo_version = read_short(LoadFile);
1012 game_top_fileinfo.fileinfo_sizeof = read_int(LoadFile);
1015 if (game_top_fileinfo.fileinfo_signature != 0x6705)
1018 // Check version number
1019 if (game_top_fileinfo.fileinfo_version < GAME_COMPATIBLE_VERSION )
1022 // Now, Read in the fileinfo
1023 if (cfseek( LoadFile, start_offset, SEEK_SET ))
1024 Error( "Error seeking to game_fileinfo in gamesave.c" );
1026 // if (cfread( &game_fileinfo, game_top_fileinfo.fileinfo_sizeof, 1, LoadFile )!=1)
1027 // Error( "Error reading game_fileinfo in gamesave.c" );
1029 game_fileinfo.fileinfo_signature = read_short(LoadFile);
1030 game_fileinfo.fileinfo_version = read_short(LoadFile);
1031 game_fileinfo.fileinfo_sizeof = read_int(LoadFile);
1033 game_fileinfo.mine_filename[i] = read_byte(LoadFile);
1034 game_fileinfo.level = read_int(LoadFile);
1035 game_fileinfo.player_offset = read_int(LoadFile); // Player info
1036 game_fileinfo.player_sizeof = read_int(LoadFile);
1037 game_fileinfo.object_offset = read_int(LoadFile); // Object info
1038 game_fileinfo.object_howmany = read_int(LoadFile);
1039 game_fileinfo.object_sizeof = read_int(LoadFile);
1040 game_fileinfo.walls_offset = read_int(LoadFile);
1041 game_fileinfo.walls_howmany = read_int(LoadFile);
1042 game_fileinfo.walls_sizeof = read_int(LoadFile);
1043 game_fileinfo.doors_offset = read_int(LoadFile);
1044 game_fileinfo.doors_howmany = read_int(LoadFile);
1045 game_fileinfo.doors_sizeof = read_int(LoadFile);
1046 game_fileinfo.triggers_offset = read_int(LoadFile);
1047 game_fileinfo.triggers_howmany = read_int(LoadFile);
1048 game_fileinfo.triggers_sizeof = read_int(LoadFile);
1049 game_fileinfo.links_offset = read_int(LoadFile);
1050 game_fileinfo.links_howmany = read_int(LoadFile);
1051 game_fileinfo.links_sizeof = read_int(LoadFile);
1052 game_fileinfo.control_offset = read_int(LoadFile);
1053 game_fileinfo.control_howmany = read_int(LoadFile);
1054 game_fileinfo.control_sizeof = read_int(LoadFile);
1055 game_fileinfo.matcen_offset = read_int(LoadFile);
1056 game_fileinfo.matcen_howmany = read_int(LoadFile);
1057 game_fileinfo.matcen_sizeof = read_int(LoadFile);
1059 if (game_top_fileinfo.fileinfo_version >= 29) {
1060 game_fileinfo.dl_indices_offset = read_int(LoadFile);
1061 game_fileinfo.dl_indices_howmany = read_int(LoadFile);
1062 game_fileinfo.dl_indices_sizeof = read_int(LoadFile);
1064 game_fileinfo.delta_light_offset = read_int(LoadFile);
1065 game_fileinfo.delta_light_howmany = read_int(LoadFile);
1066 game_fileinfo.delta_light_sizeof = read_int(LoadFile);
1069 if (game_top_fileinfo.fileinfo_version >= 14) { //load mine filename
1070 //@@char *p=Current_level_name;
1071 //@@//must do read one char at a time, since no cfgets()
1072 //@@do *p = cfgetc(LoadFile); while (*p++!=0);
1074 cfgets(Current_level_name,sizeof(Current_level_name),LoadFile);
1076 if (Current_level_name[strlen(Current_level_name)-1] == '\n')
1077 Current_level_name[strlen(Current_level_name)-1] = 0;
1080 Current_level_name[0]=0;
1082 if (game_top_fileinfo.fileinfo_version >= 19) { //load pof names
1083 // cfread(&N_save_pof_names,2,1,LoadFile);
1084 N_save_pof_names = read_short(LoadFile);
1085 cfread(Save_pof_names,N_save_pof_names,FILENAME_LEN,LoadFile);
1088 //===================== READ PLAYER INFO ==========================
1089 Object_next_signature = 0;
1091 //===================== READ OBJECT INFO ==========================
1093 Gamesave_num_org_robots = 0;
1094 Gamesave_num_players = 0;
1096 if (game_fileinfo.object_offset > -1) {
1097 if (cfseek( LoadFile, game_fileinfo.object_offset, SEEK_SET ))
1098 Error( "Error seeking to object_offset in gamesave.c" );
1100 for (i=0;i<game_fileinfo.object_howmany;i++) {
1102 read_object(&Objects[i],LoadFile,game_top_fileinfo.fileinfo_version);
1104 Objects[i].signature = Object_next_signature++;
1105 verify_object( &Objects[i] );
1110 //===================== READ WALL INFO ============================
1112 if (game_fileinfo.walls_offset > -1)
1115 if (!cfseek( LoadFile, game_fileinfo.walls_offset,SEEK_SET )) {
1116 for (i=0;i<game_fileinfo.walls_howmany;i++) {
1118 if (game_top_fileinfo.fileinfo_version >= 20) {
1120 Assert(sizeof(Walls[i]) == game_fileinfo.walls_sizeof);
1122 // code to correctly read wall structure on mac. I'm assuming only v20 walls
1125 if (cfread(&Walls[i], game_fileinfo.walls_sizeof, 1,LoadFile)!=1)
1126 Error( "Error reading Walls[%d] in gamesave.c", i);
1128 Walls[i].segnum = read_int(LoadFile);
1129 Walls[i].sidenum = read_int(LoadFile);
1130 Walls[i].hps = read_fix(LoadFile);
1131 Walls[i].linked_wall = read_int(LoadFile);
1132 Walls[i].type = read_byte(LoadFile);
1133 Walls[i].flags = read_byte(LoadFile);
1134 Walls[i].state = read_byte(LoadFile);
1135 Walls[i].trigger = read_byte(LoadFile);
1136 Walls[i].clip_num = read_byte(LoadFile);
1137 Walls[i].keys = read_byte(LoadFile);
1138 Walls[i].controlling_trigger = read_byte(LoadFile);
1139 Walls[i].cloak_value = read_byte(LoadFile);
1142 else if (game_top_fileinfo.fileinfo_version >= 17) {
1145 Assert(sizeof(w) == game_fileinfo.walls_sizeof);
1147 if (cfread(&w, game_fileinfo.walls_sizeof, 1,LoadFile)!=1)
1148 Error( "Error reading Walls[%d] in gamesave.c", i);
1150 Walls[i].segnum = w.segnum;
1151 Walls[i].sidenum = w.sidenum;
1152 Walls[i].linked_wall = w.linked_wall;
1154 Walls[i].type = w.type;
1155 Walls[i].flags = w.flags;
1156 Walls[i].hps = w.hps;
1157 Walls[i].trigger = w.trigger;
1158 Walls[i].clip_num = w.clip_num;
1159 Walls[i].keys = w.keys;
1161 Walls[i].state = WALL_DOOR_CLOSED;
1166 Assert(sizeof(w) == game_fileinfo.walls_sizeof);
1168 if (cfread(&w, game_fileinfo.walls_sizeof, 1,LoadFile)!=1)
1169 Error( "Error reading Walls[%d] in gamesave.c", i);
1171 Walls[i].segnum = Walls[i].sidenum = Walls[i].linked_wall = -1;
1173 Walls[i].type = w.type;
1174 Walls[i].flags = w.flags;
1175 Walls[i].hps = w.hps;
1176 Walls[i].trigger = w.trigger;
1177 Walls[i].clip_num = w.clip_num;
1178 Walls[i].keys = w.keys;
1185 //===================== READ DOOR INFO ============================
1187 if (game_fileinfo.doors_offset > -1)
1189 if (!cfseek( LoadFile, game_fileinfo.doors_offset,SEEK_SET )) {
1191 for (i=0;i<game_fileinfo.doors_howmany;i++) {
1193 if (game_top_fileinfo.fileinfo_version >= 20) {
1195 Assert(sizeof(ActiveDoors[i]) == game_fileinfo.doors_sizeof);
1197 // code to read doors for mac -- assume version 20 and greater for doors
1199 if (cfread(&ActiveDoors[i], game_fileinfo.doors_sizeof,1,LoadFile)!=1)
1200 Error( "Error reading ActiveDoors[%d] in gamesave.c", i);
1202 ActiveDoors[i].n_parts = read_int(LoadFile);
1203 ActiveDoors[i].front_wallnum[0] = read_short(LoadFile);
1204 ActiveDoors[i].front_wallnum[1] = read_short(LoadFile);
1205 ActiveDoors[i].back_wallnum[0] = read_short(LoadFile);
1206 ActiveDoors[i].back_wallnum[1] = read_short(LoadFile);
1207 ActiveDoors[i].time = read_fix(LoadFile);
1214 Assert(sizeof(d) == game_fileinfo.doors_sizeof);
1216 if (cfread(&d, game_fileinfo.doors_sizeof, 1,LoadFile)!=1)
1217 Error( "Error reading Doors[%d] in gamesave.c", i);
1219 ActiveDoors[i].n_parts = d.n_parts;
1221 for (p=0;p<d.n_parts;p++) {
1224 cseg = Segments[d.seg[p]].children[d.side[p]];
1225 cside = find_connect_side(&Segments[d.seg[p]],&Segments[cseg]);
1227 ActiveDoors[i].front_wallnum[p] = Segments[d.seg[p]].sides[d.side[p]].wall_num;
1228 ActiveDoors[i].back_wallnum[p] = Segments[cseg].sides[cside].wall_num;
1236 //==================== READ TRIGGER INFO ==========================
1239 // for MACINTOSH -- assume all triggers >= verion 31 triggers.
1241 if (game_fileinfo.triggers_offset > -1)
1243 if (!cfseek( LoadFile, game_fileinfo.triggers_offset,SEEK_SET )) {
1244 for (i=0;i<game_fileinfo.triggers_howmany;i++)
1245 if (game_top_fileinfo.fileinfo_version < 31) {
1251 if (game_top_fileinfo.fileinfo_version < 30) {
1255 if (cfread(&trig29, game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1256 Error( "Error reading Triggers[%d] in gamesave.c", i);
1258 trig.flags = trig29.flags;
1259 trig.num_links = trig29.num_links;
1260 trig.num_links = trig29.num_links;
1261 trig.value = trig29.value;
1262 trig.time = trig29.time;
1264 for (t=0;t<trig.num_links;t++) {
1265 trig.seg[t] = trig29.seg[t];
1266 trig.side[t] = trig29.side[t];
1270 if (cfread(&trig, game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1271 Error( "Error reading Triggers[%d] in gamesave.c", i);
1273 //Assert(trig.flags & TRIGGER_ON);
1274 trig.flags &= ~TRIGGER_ON;
1276 if (trig.flags & TRIGGER_CONTROL_DOORS)
1277 type = TT_OPEN_DOOR;
1278 else if (trig.flags & TRIGGER_SHIELD_DAMAGE)
1280 else if (trig.flags & TRIGGER_ENERGY_DRAIN)
1282 else if (trig.flags & TRIGGER_EXIT)
1284 else if (trig.flags & TRIGGER_ONE_SHOT)
1286 else if (trig.flags & TRIGGER_MATCEN)
1288 else if (trig.flags & TRIGGER_ILLUSION_OFF)
1289 type = TT_ILLUSION_OFF;
1290 else if (trig.flags & TRIGGER_SECRET_EXIT)
1291 type = TT_SECRET_EXIT;
1292 else if (trig.flags & TRIGGER_ILLUSION_ON)
1293 type = TT_ILLUSION_ON;
1294 else if (trig.flags & TRIGGER_UNLOCK_DOORS)
1295 type = TT_UNLOCK_DOOR;
1296 else if (trig.flags & TRIGGER_OPEN_WALL)
1297 type = TT_OPEN_WALL;
1298 else if (trig.flags & TRIGGER_CLOSE_WALL)
1299 type = TT_CLOSE_WALL;
1300 else if (trig.flags & TRIGGER_ILLUSORY_WALL)
1301 type = TT_ILLUSORY_WALL;
1305 Triggers[i].type = type;
1306 Triggers[i].flags = 0;
1307 Triggers[i].num_links = trig.num_links;
1308 Triggers[i].num_links = trig.num_links;
1309 Triggers[i].value = trig.value;
1310 Triggers[i].time = trig.time;
1312 for (t=0;t<trig.num_links;t++) {
1313 Triggers[i].seg[t] = trig.seg[t];
1314 Triggers[i].side[t] = trig.side[t];
1319 if (cfread(&Triggers[i], game_fileinfo.triggers_sizeof,1,LoadFile)!=1)
1320 Error( "Error reading Triggers[%d] in gamesave.c", i);
1322 Triggers[i].type = read_byte(LoadFile);
1323 Triggers[i].flags = read_byte(LoadFile);
1324 Triggers[i].num_links = read_byte(LoadFile);
1325 Triggers[i].pad = read_byte(LoadFile);
1326 Triggers[i].value = read_fix(LoadFile);
1327 Triggers[i].time = read_fix(LoadFile);
1328 for (j=0; j<MAX_WALLS_PER_LINK; j++ )
1329 Triggers[i].seg[j] = read_short(LoadFile);
1330 for (j=0; j<MAX_WALLS_PER_LINK; j++ )
1331 Triggers[i].side[j] = read_short(LoadFile);
1337 //================ READ CONTROL CENTER TRIGGER INFO ===============
1339 if (game_fileinfo.control_offset > -1)
1341 if (!cfseek( LoadFile, game_fileinfo.control_offset,SEEK_SET )) {
1342 for (i=0;i<game_fileinfo.control_howmany;i++)
1344 if (cfread(&ControlCenterTriggers, game_fileinfo.control_sizeof,1,LoadFile)!=1)
1345 Error( "Error reading ControlCenterTriggers in gamesave.c");
1347 ControlCenterTriggers.num_links = read_short(LoadFile);
1348 for (j=0; j<MAX_CONTROLCEN_LINKS; j++ )
1349 ControlCenterTriggers.seg[j] = read_short( LoadFile );
1350 for (j=0; j<MAX_CONTROLCEN_LINKS; j++ )
1351 ControlCenterTriggers.side[j] = read_short( LoadFile );
1357 //================ READ MATERIALOGRIFIZATIONATORS INFO ===============
1359 if (game_fileinfo.matcen_offset > -1)
1362 if (!cfseek( LoadFile, game_fileinfo.matcen_offset,SEEK_SET )) {
1363 // mprintf((0, "Reading %i materialization centers.\n", game_fileinfo.matcen_howmany));
1364 for (i=0;i<game_fileinfo.matcen_howmany;i++) {
1365 if (game_top_fileinfo.fileinfo_version < 27) {
1367 Assert(game_fileinfo.matcen_sizeof == sizeof(m));
1368 if (cfread(&m, game_fileinfo.matcen_sizeof,1,LoadFile)!=1)
1369 Error( "Error reading RobotCenters in gamesave.c");
1370 RobotCenters[i].robot_flags[0] = m.robot_flags;
1371 RobotCenters[i].robot_flags[1] = 0;
1372 RobotCenters[i].hit_points = m.hit_points;
1373 RobotCenters[i].interval = m.interval;
1374 RobotCenters[i].segnum = m.segnum;
1375 RobotCenters[i].fuelcen_num = m.fuelcen_num;
1378 Assert(game_fileinfo.matcen_sizeof == sizeof(RobotCenters[i]));
1380 if (cfread(&RobotCenters[i], game_fileinfo.matcen_sizeof,1,LoadFile)!=1)
1381 Error( "Error reading RobotCenters in gamesave.c");
1383 RobotCenters[i].robot_flags[0] = read_int(LoadFile);
1384 RobotCenters[i].robot_flags[1] = read_int(LoadFile);
1385 RobotCenters[i].hit_points = read_fix(LoadFile);
1386 RobotCenters[i].interval = read_fix(LoadFile);
1387 RobotCenters[i].segnum = read_short(LoadFile);
1388 RobotCenters[i].fuelcen_num = read_short(LoadFile);
1392 // Set links in RobotCenters to Station array
1394 for (j=0; j<=Highest_segment_index; j++)
1395 if (Segment2s[j].special == SEGMENT_IS_ROBOTMAKER)
1396 if (Segment2s[j].matcen_num == i)
1397 RobotCenters[i].fuelcen_num = Segment2s[j].value;
1399 // mprintf((0, " %i: flags = %08x\n", i, RobotCenters[i].robot_flags));
1405 //================ READ DL_INDICES INFO ===============
1407 Num_static_lights = 0;
1409 if (game_fileinfo.dl_indices_offset > -1) {
1412 if (!cfseek( LoadFile, game_fileinfo.dl_indices_offset, SEEK_SET )) {
1413 Num_static_lights = game_fileinfo.dl_indices_howmany;
1414 for (i=0; i<game_fileinfo.dl_indices_howmany; i++) {
1415 if (game_top_fileinfo.fileinfo_version < 29) {
1416 mprintf((0, "Warning: Old mine version. Not reading Dl_indices info.\n"));
1417 Int3(); //shouldn't be here!!!
1420 if (cfread(&Dl_indices[i], game_fileinfo.dl_indices_sizeof, 1, LoadFile) != 1)
1421 Error( "Error reading Dl_indices in gamesave.c");
1423 Dl_indices[i].segnum = read_short(LoadFile);
1424 Dl_indices[i].sidenum = read_byte(LoadFile);
1425 Dl_indices[i].count = read_byte(LoadFile);
1426 Dl_indices[i].index = read_short(LoadFile);
1434 // Indicate that no light has been subtracted from any vertices.
1435 clear_light_subtracted();
1437 //================ READ DELTA LIGHT INFO ===============
1439 if (game_fileinfo.delta_light_offset > -1) {
1442 if (!cfseek( LoadFile, game_fileinfo.delta_light_offset, SEEK_SET )) {
1443 for (i=0; i<game_fileinfo.delta_light_howmany; i++) {
1444 if (game_top_fileinfo.fileinfo_version < 29) {
1445 mprintf((0, "Warning: Old mine version. Not reading delta light info.\n"));
1448 if (cfread(&Delta_lights[i], game_fileinfo.delta_light_sizeof, 1, LoadFile) != 1)
1449 Error( "Error reading Delta Lights in gamesave.c");
1451 Delta_lights[i].segnum = read_short(LoadFile);
1452 Delta_lights[i].sidenum = read_byte(LoadFile);
1453 Delta_lights[i].dummy = read_byte(LoadFile);
1454 Delta_lights[i].vert_light[0] = read_byte(LoadFile);
1455 Delta_lights[i].vert_light[1] = read_byte(LoadFile);
1456 Delta_lights[i].vert_light[2] = read_byte(LoadFile);
1457 Delta_lights[i].vert_light[3] = read_byte(LoadFile);
1465 //========================= UPDATE VARIABLES ======================
1467 reset_objects(game_fileinfo.object_howmany);
1469 for (i=0; i<MAX_OBJECTS; i++) {
1470 Objects[i].next = Objects[i].prev = -1;
1471 if (Objects[i].type != OBJ_NONE) {
1472 int objsegnum = Objects[i].segnum;
1474 if (objsegnum > Highest_segment_index) //bogus object
1475 Objects[i].type = OBJ_NONE;
1477 Objects[i].segnum = -1; //avoid Assert()
1478 obj_link(i,objsegnum);
1483 clear_transient_objects(1); //1 means clear proximity bombs
1485 // Make sure non-transparent doors are set correctly.
1486 for (i=0; i< Num_segments; i++)
1487 for (j=0;j<MAX_SIDES_PER_SEGMENT;j++) {
1488 side *sidep = &Segments[i].sides[j];
1489 if ((sidep->wall_num != -1) && (Walls[sidep->wall_num].clip_num != -1)) {
1490 //mprintf((0, "Checking Wall %d\n", Segments[i].sides[j].wall_num));
1491 if (WallAnims[Walls[sidep->wall_num].clip_num].flags & WCF_TMAP1) {
1492 //mprintf((0, "Fixing non-transparent door.\n"));
1493 sidep->tmap_num = WallAnims[Walls[sidep->wall_num].clip_num].frames[0];
1494 sidep->tmap_num2 = 0;
1500 Num_walls = game_fileinfo.walls_howmany;
1503 Num_open_doors = game_fileinfo.doors_howmany;
1504 Num_triggers = game_fileinfo.triggers_howmany;
1506 //go through all walls, killing references to invalid triggers
1507 for (i=0;i<Num_walls;i++)
1508 if (Walls[i].trigger >= Num_triggers) {
1509 mprintf((0,"Removing reference to invalid trigger %d from wall %d\n",Walls[i].trigger,i));
1510 Walls[i].trigger = -1; //kill trigger
1513 //go through all triggers, killing unused ones
1514 for (i=0;i<Num_triggers;) {
1517 // Find which wall this trigger is connected to.
1518 for (w=0; w<Num_walls; w++)
1519 if (Walls[w].trigger == i)
1523 if (w == Num_walls) {
1524 mprintf((0,"Removing unreferenced trigger %d\n",i));
1525 remove_trigger_num(i);
1532 // MK, 10/17/95: Make walls point back at the triggers that control them.
1533 // Go through all triggers, stuffing controlling_trigger field in Walls.
1536 for (i=0; i<Num_walls; i++)
1537 Walls[i].controlling_trigger = -1;
1539 for (t=0; t<Num_triggers; t++) {
1541 for (l=0; l<Triggers[t].num_links; l++) {
1542 int seg_num, side_num, wall_num;
1544 seg_num = Triggers[t].seg[l];
1545 side_num = Triggers[t].side[l];
1546 wall_num = Segments[seg_num].sides[side_num].wall_num;
1548 // -- if (Walls[wall_num].controlling_trigger != -1)
1551 //check to see that if a trigger requires a wall that it has one,
1552 //and if it requires a matcen that it has one
1554 if (Triggers[t].type == TT_MATCEN) {
1555 if (Segment2s[seg_num].special != SEGMENT_IS_ROBOTMAKER)
1556 Int3(); //matcen trigger doesn't point to matcen
1558 else if (Triggers[t].type != TT_LIGHT_OFF && Triggers[t].type != TT_LIGHT_ON) { //light triggers don't require walls
1560 Int3(); // This is illegal. This trigger requires a wall
1562 Walls[wall_num].controlling_trigger = t;
1568 Num_robot_centers = game_fileinfo.matcen_howmany;
1570 //fix old wall structs
1571 if (game_top_fileinfo.fileinfo_version < 17) {
1572 int segnum,sidenum,wallnum;
1574 for (segnum=0; segnum<=Highest_segment_index; segnum++)
1575 for (sidenum=0;sidenum<6;sidenum++)
1576 if ((wallnum=Segments[segnum].sides[sidenum].wall_num) != -1) {
1577 Walls[wallnum].segnum = segnum;
1578 Walls[wallnum].sidenum = sidenum;
1585 for (sidenum=0; sidenum<6; sidenum++) {
1586 int wallnum = Segments[Highest_segment_index].sides[sidenum].wall_num;
1588 if ((Walls[wallnum].segnum != Highest_segment_index) || (Walls[wallnum].sidenum != sidenum))
1589 Int3(); // Error. Bogus walls in this segment.
1590 // Consult Yuan or Mike.
1595 //create_local_segment_data();
1603 if (game_top_fileinfo.fileinfo_version < GAME_VERSION && !(game_top_fileinfo.fileinfo_version==25 && GAME_VERSION==26))
1604 return 1; //means old version
1610 int check_segment_connections(void);
1612 extern void set_ambient_sound_flags(void);
1614 // -----------------------------------------------------------------------------
1615 //loads from an already-open file
1616 // returns 0=everything ok, 1=old version, -1=error
1617 int load_mine_data(CFILE *LoadFile);
1618 int load_mine_data_compiled(CFILE *LoadFile);
1620 #define LEVEL_FILE_VERSION 8
1621 //1 -> 2 add palette name
1622 //2 -> 3 add control center explosion time
1623 //3 -> 4 add reactor strength
1624 //4 -> 5 killed hostage text stuff
1625 //5 -> 6 added Secret_return_segment and Secret_return_orient
1626 //6 -> 7 added flickering lights
1627 //7 -> 8 made version 8 to be not compatible with D2 1.0 & 1.1
1630 char *Level_being_loaded=NULL;
1634 extern void ncache_flush();
1637 extern int HoardEquipped();
1639 extern int Slide_segs_computed;
1641 int no_old_level_file_error=0;
1643 //loads a level (.LVL) file from disk
1644 //returns 0 if success, else error code
1645 int load_level(char * filename_passed)
1648 int use_compiled_level=1;
1652 int version,minedata_offset,gamedata_offset;
1653 int mine_err,game_err;
1659 Slide_segs_computed = 0;
1662 if (Game_mode & GM_NETWORK)
1664 for (i=0;i<MAX_POWERUP_TYPES;i++)
1666 MaxPowerupsAllowed[i]=0;
1667 PowerupsInMine[i]=0;
1677 Level_being_loaded = filename_passed;
1680 strcpy(filename,filename_passed);
1684 //if we have the editor, try the LVL first, no matter what was passed.
1685 //if we don't have an LVL, try RDL
1686 //if we don't have the editor, we just use what was passed
1688 change_filename_extension(filename,filename_passed,".LVL");
1689 use_compiled_level = 0;
1691 if (!cfexist(filename)) {
1692 change_filename_extension(filename,filename,".RL2");
1693 use_compiled_level = 1;
1697 LoadFile = cfopen( filename, "rb" );
1701 mprintf((0,"Can't open level file <%s>\n", filename));
1704 Error("Can't open file <%s>\n",filename);
1708 strcpy( Gamesave_current_filename, filename );
1711 // if ( Newdemo_state == ND_STATE_RECORDING )
1712 // newdemo_record_start_demo();
1715 cfread(sig, 1, 4, LoadFile);
1716 version = read_int(LoadFile);
1717 minedata_offset = read_int(LoadFile);
1718 gamedata_offset = read_int(LoadFile);
1720 Assert((sig[0] == 'L') && (sig[1] == 'V') && (sig[2]=='L') && (sig[3] == 'P'));
1722 if (version >= 8) { //read dummy data
1724 if (HoardEquipped())
1727 read_short(LoadFile);
1728 read_byte(LoadFile);
1732 // NOTE LINK TO ABOVE!
1733 Error("This level requires the Vertigo Enhanced version of D2.");
1738 read_int(LoadFile); //was hostagetext_offset
1741 cfgets(Current_level_palette,sizeof(Current_level_palette),LoadFile);
1742 if (Current_level_palette[strlen(Current_level_palette)-1] == '\n')
1743 Current_level_palette[strlen(Current_level_palette)-1] = 0;
1747 Base_control_center_explosion_time = read_int(LoadFile);
1749 Base_control_center_explosion_time = DEFAULT_CONTROL_CENTER_EXPLOSION_TIME;
1752 Reactor_strength = read_int(LoadFile);
1754 Reactor_strength = -1; //use old defaults
1757 Num_flickering_lights = read_int(LoadFile);
1759 Assert((Num_flickering_lights >= 0) && (Num_flickering_lights < MAX_FLICKERING_LIGHTS));
1760 for (i = 0; i < Num_flickering_lights; i++)
1762 Flickering_lights[i].segnum = read_short(LoadFile);
1763 Flickering_lights[i].sidenum = read_short(LoadFile);
1764 Flickering_lights[i].mask = read_int(LoadFile);
1765 Flickering_lights[i].timer = read_fix(LoadFile);
1766 Flickering_lights[i].delay = read_fix(LoadFile);
1769 cfread(Flickering_lights,sizeof(*Flickering_lights),Num_flickering_lights,LoadFile);
1773 Num_flickering_lights = 0;
1775 if (version <= 1 || Current_level_palette[0]==0)
1776 strcpy(Current_level_palette,"groupa.256");
1779 Secret_return_segment = 0;
1780 Secret_return_orient.rvec.x = F1_0; Secret_return_orient.rvec.y = 0; Secret_return_orient.rvec.z = 0;
1781 Secret_return_orient.fvec.x = 0; Secret_return_orient.fvec.y = F1_0; Secret_return_orient.fvec.z = 0;
1782 Secret_return_orient.uvec.x = 0; Secret_return_orient.uvec.y = 0; Secret_return_orient.uvec.z = F1_0;
1784 Secret_return_segment = read_int(LoadFile);
1785 Secret_return_orient.rvec.x = read_int(LoadFile);
1786 Secret_return_orient.rvec.y = read_int(LoadFile);
1787 Secret_return_orient.rvec.z = read_int(LoadFile);
1788 Secret_return_orient.fvec.x = read_int(LoadFile);
1789 Secret_return_orient.fvec.y = read_int(LoadFile);
1790 Secret_return_orient.fvec.z = read_int(LoadFile);
1791 Secret_return_orient.uvec.x = read_int(LoadFile);
1792 Secret_return_orient.uvec.y = read_int(LoadFile);
1793 Secret_return_orient.uvec.z = read_int(LoadFile);
1796 cfseek(LoadFile,minedata_offset,SEEK_SET);
1798 if (!use_compiled_level) {
1799 mine_err = load_mine_data(LoadFile);
1800 // Compress all uv coordinates in mine, improves texmap precision. --MK, 02/19/96
1801 compress_uv_coordinates_all();
1804 //NOTE LINK TO ABOVE!!
1805 mine_err = load_mine_data_compiled(LoadFile);
1807 if (mine_err == -1) { //error!!
1812 cfseek(LoadFile,gamedata_offset,SEEK_SET);
1813 game_err = load_game_data(LoadFile);
1815 if (game_err == -1) { //error!!
1820 //======================== CLOSE FILE =============================
1822 cfclose( LoadFile );
1824 set_ambient_sound_flags();
1827 write_game_text_file(filename);
1828 if (Errors_in_mine) {
1829 if (is_real_level(filename)) {
1830 char ErrorMessage[200];
1832 sprintf( ErrorMessage, "Warning: %i errors in %s!\n", Errors_in_mine, Level_being_loaded );
1834 gr_palette_load(gr_palette);
1835 nm_messagebox( NULL, 1, "Continue", ErrorMessage );
1838 mprintf((1, "Error: %i errors in %s.\n", Errors_in_mine, Level_being_loaded));
1843 //If an old version, ask the use if he wants to save as new version
1844 if (!no_old_level_file_error && (Function_mode == FMODE_EDITOR) && (((LEVEL_FILE_VERSION>3) && version<LEVEL_FILE_VERSION) || mine_err==1 || game_err==1)) {
1845 char ErrorMessage[200];
1847 sprintf( ErrorMessage,
1848 "You just loaded a old version\n"
1849 "level. Would you like to save\n"
1850 "it as a current version level?");
1853 gr_palette_load(gr_palette);
1854 if (nm_messagebox( NULL, 2, "Don't Save", "Save", ErrorMessage )==1)
1855 save_level(filename);
1861 if (Function_mode == FMODE_EDITOR)
1862 editor_status("Loaded NEW mine %s, \"%s\"",filename,Current_level_name);
1866 if (check_segment_connections())
1867 nm_messagebox( "ERROR", 1, "Ok",
1868 "Connectivity errors detected in\n"
1869 "mine. See monochrome screen for\n"
1870 "details, and contact Matt or Mike." );
1877 void get_level_name()
1879 //NO_UI!!! UI_WINDOW *NameWindow = NULL;
1880 //NO_UI!!! UI_GADGET_INPUTBOX *NameText;
1881 //NO_UI!!! UI_GADGET_BUTTON *QuitButton;
1883 //NO_UI!!! // Open a window with a quit button
1884 //NO_UI!!! NameWindow = ui_open_window( 20, 20, 300, 110, WIN_DIALOG );
1885 //NO_UI!!! QuitButton = ui_add_gadget_button( NameWindow, 150-24, 60, 48, 40, "Done", NULL );
1887 //NO_UI!!! ui_wprintf_at( NameWindow, 10, 12,"Please enter a name for this mine:" );
1888 //NO_UI!!! NameText = ui_add_gadget_inputbox( NameWindow, 10, 30, LEVEL_NAME_LEN, LEVEL_NAME_LEN, Current_level_name );
1890 //NO_UI!!! NameWindow->keyboard_focus_gadget = (UI_GADGET *)NameText;
1891 //NO_UI!!! QuitButton->hotkey = KEY_ENTER;
1893 //NO_UI!!! ui_gadget_calc_keys(NameWindow);
1895 //NO_UI!!! while (!QuitButton->pressed && last_keypress!=KEY_ENTER) {
1896 //NO_UI!!! ui_mega_process();
1897 //NO_UI!!! ui_window_do_gadgets(NameWindow);
1900 //NO_UI!!! strcpy( Current_level_name, NameText->text );
1902 //NO_UI!!! if ( NameWindow!=NULL ) {
1903 //NO_UI!!! ui_close_window( NameWindow );
1904 //NO_UI!!! NameWindow = NULL;
1910 m[0].type = NM_TYPE_TEXT; m[0].text = "Please enter a name for this mine:";
1911 m[1].type = NM_TYPE_INPUT; m[1].text = Current_level_name; m[1].text_len = LEVEL_NAME_LEN;
1913 newmenu_do( NULL, "Enter mine name", 2, m, NULL );
1923 // -----------------------------------------------------------------------------
1924 int compute_num_delta_light_records(void)
1929 for (i=0; i<Num_static_lights; i++) {
1930 total += Dl_indices[i].count;
1937 // -----------------------------------------------------------------------------
1939 int save_game_data(FILE * SaveFile)
1941 int player_offset, object_offset, walls_offset, doors_offset, triggers_offset, control_offset, matcen_offset; //, links_offset;
1942 int dl_indices_offset, delta_light_offset;
1943 int start_offset,end_offset;
1945 start_offset = ftell(SaveFile);
1947 //===================== SAVE FILE INFO ========================
1949 game_fileinfo.fileinfo_signature = 0x6705;
1950 game_fileinfo.fileinfo_version = GAME_VERSION;
1951 game_fileinfo.level = Current_level_num;
1952 game_fileinfo.fileinfo_sizeof = sizeof(game_fileinfo);
1953 game_fileinfo.player_offset = -1;
1954 game_fileinfo.player_sizeof = sizeof(player);
1955 game_fileinfo.object_offset = -1;
1956 game_fileinfo.object_howmany = Highest_object_index+1;
1957 game_fileinfo.object_sizeof = sizeof(object);
1958 game_fileinfo.walls_offset = -1;
1959 game_fileinfo.walls_howmany = Num_walls;
1960 game_fileinfo.walls_sizeof = sizeof(wall);
1961 game_fileinfo.doors_offset = -1;
1962 game_fileinfo.doors_howmany = Num_open_doors;
1963 game_fileinfo.doors_sizeof = sizeof(active_door);
1964 game_fileinfo.triggers_offset = -1;
1965 game_fileinfo.triggers_howmany = Num_triggers;
1966 game_fileinfo.triggers_sizeof = sizeof(trigger);
1967 game_fileinfo.control_offset = -1;
1968 game_fileinfo.control_howmany = 1;
1969 game_fileinfo.control_sizeof = sizeof(control_center_triggers);
1970 game_fileinfo.matcen_offset = -1;
1971 game_fileinfo.matcen_howmany = Num_robot_centers;
1972 game_fileinfo.matcen_sizeof = sizeof(matcen_info);
1974 game_fileinfo.dl_indices_offset = -1;
1975 game_fileinfo.dl_indices_howmany = Num_static_lights;
1976 game_fileinfo.dl_indices_sizeof = sizeof(dl_index);
1978 game_fileinfo.delta_light_offset = -1;
1979 game_fileinfo.delta_light_howmany = compute_num_delta_light_records();
1980 game_fileinfo.delta_light_sizeof = sizeof(delta_light);
1982 // Write the fileinfo
1983 fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
1985 // Write the mine name
1986 fprintf(SaveFile,"%s\n",Current_level_name);
1988 fwrite(&N_polygon_models,2,1,SaveFile);
1989 fwrite(Pof_names,N_polygon_models,sizeof(*Pof_names),SaveFile);
1991 //==================== SAVE PLAYER INFO ===========================
1993 player_offset = ftell(SaveFile);
1994 fwrite( &Players[Player_num], sizeof(player), 1, SaveFile );
1996 //==================== SAVE OBJECT INFO ===========================
1998 object_offset = ftell(SaveFile);
1999 //fwrite( &Objects, sizeof(object), game_fileinfo.object_howmany, SaveFile );
2002 for (i=0;i<game_fileinfo.object_howmany;i++)
2003 write_object(&Objects[i],SaveFile);
2006 //==================== SAVE WALL INFO =============================
2008 walls_offset = ftell(SaveFile);
2009 fwrite( Walls, sizeof(wall), game_fileinfo.walls_howmany, SaveFile );
2011 //==================== SAVE DOOR INFO =============================
2013 doors_offset = ftell(SaveFile);
2014 fwrite( ActiveDoors, sizeof(active_door), game_fileinfo.doors_howmany, SaveFile );
2016 //==================== SAVE TRIGGER INFO =============================
2018 triggers_offset = ftell(SaveFile);
2019 fwrite( Triggers, sizeof(trigger), game_fileinfo.triggers_howmany, SaveFile );
2021 //================ SAVE CONTROL CENTER TRIGGER INFO ===============
2023 control_offset = ftell(SaveFile);
2024 fwrite( &ControlCenterTriggers, sizeof(control_center_triggers), 1, SaveFile );
2027 //================ SAVE MATERIALIZATION CENTER TRIGGER INFO ===============
2029 matcen_offset = ftell(SaveFile);
2030 // mprintf((0, "Writing %i materialization centers\n", game_fileinfo.matcen_howmany));
2032 // for (i=0; i<game_fileinfo.matcen_howmany; i++)
2033 // mprintf((0, " %i: robot_flags = %08x\n", i, RobotCenters[i].robot_flags));
2035 fwrite( RobotCenters, sizeof(matcen_info), game_fileinfo.matcen_howmany, SaveFile );
2037 //================ SAVE DELTA LIGHT INFO ===============
2038 dl_indices_offset = ftell(SaveFile);
2039 fwrite( Dl_indices, sizeof(dl_index), game_fileinfo.dl_indices_howmany, SaveFile );
2041 delta_light_offset = ftell(SaveFile);
2042 fwrite( Delta_lights, sizeof(delta_light), game_fileinfo.delta_light_howmany, SaveFile );
2044 //============= REWRITE FILE INFO, TO SAVE OFFSETS ===============
2046 // Update the offset fields
2047 game_fileinfo.player_offset = player_offset;
2048 game_fileinfo.object_offset = object_offset;
2049 game_fileinfo.walls_offset = walls_offset;
2050 game_fileinfo.doors_offset = doors_offset;
2051 game_fileinfo.triggers_offset = triggers_offset;
2052 game_fileinfo.control_offset = control_offset;
2053 game_fileinfo.matcen_offset = matcen_offset;
2054 game_fileinfo.dl_indices_offset = dl_indices_offset;
2055 game_fileinfo.delta_light_offset = delta_light_offset;
2058 end_offset = ftell(SaveFile);
2060 // Write the fileinfo
2061 fseek( SaveFile, start_offset, SEEK_SET ); // Move to TOF
2062 fwrite( &game_fileinfo, sizeof(game_fileinfo), 1, SaveFile );
2064 // Go back to end of data
2065 fseek(SaveFile,end_offset,SEEK_SET);
2070 int save_mine_data(FILE * SaveFile);
2072 // -----------------------------------------------------------------------------
2074 int save_level_sub(char * filename, int compiled_version)
2077 char temp_filename[128];
2078 int sig = 'PLVL',version=LEVEL_FILE_VERSION;
2079 int minedata_offset,gamedata_offset;
2081 if ( !compiled_version ) {
2082 write_game_text_file(filename);
2084 if (Errors_in_mine) {
2085 if (is_real_level(filename)) {
2086 char ErrorMessage[200];
2088 sprintf( ErrorMessage, "Warning: %i errors in this mine!\n", Errors_in_mine );
2090 gr_palette_load(gr_palette);
2092 if (nm_messagebox( NULL, 2, "Cancel Save", "Save", ErrorMessage )!=1) {
2098 mprintf((1, "Error: %i errors in this mine. See the 'txm' file.\n", Errors_in_mine));
2100 change_filename_extension(temp_filename,filename,".LVL");
2104 // macs are using the regular hog/rl2 files for shareware
2105 #if defined(SHAREWARE) && !defined(MACINTOSH)
2106 change_filename_extension(temp_filename,filename,".SL2");
2108 change_filename_extension(temp_filename,filename,".RL2");
2112 SaveFile = fopen( temp_filename, "wb" );
2115 char ErrorMessage[256];
2118 _splitpath( temp_filename, NULL, NULL, fname, NULL );
2120 sprintf( ErrorMessage, \
2121 "ERROR: Cannot write to '%s'.\nYou probably need to check out a locked\nversion of the file. You should save\nthis under a different filename, and then\ncheck out a locked copy by typing\n\'co -l %s.lvl'\nat the DOS prompt.\n"
2122 , temp_filename, fname );
2124 gr_palette_load(gr_palette);
2125 nm_messagebox( NULL, 1, "Ok", ErrorMessage );
2130 if (Current_level_name[0] == 0)
2131 strcpy(Current_level_name,"Untitled");
2133 clear_transient_objects(1); //1 means clear proximity bombs
2135 compress_objects(); //after this, Highest_object_index == num objects
2137 //make sure player is in a segment
2138 if (update_object_seg(&Objects[Players[0].objnum]) == 0) {
2139 if (ConsoleObject->segnum > Highest_segment_index)
2140 ConsoleObject->segnum = 0;
2141 compute_segment_center(&ConsoleObject->pos,&(Segments[ConsoleObject->segnum]));
2148 gs_write_int(sig,SaveFile);
2149 gs_write_int(version,SaveFile);
2152 gs_write_int(minedata_offset,SaveFile);
2153 gs_write_int(gamedata_offset,SaveFile);
2155 //Now write the damn data
2157 //write the version 8 data (to make file unreadable by 1.0 & 1.1)
2158 gs_write_int(GameTime,SaveFile);
2159 gs_write_short(FrameCount,SaveFile);
2160 gs_write_byte(FrameTime,SaveFile);
2162 // Write the palette file name
2163 fprintf(SaveFile,"%s\n",Current_level_palette);
2165 gs_write_int(Base_control_center_explosion_time,SaveFile);
2166 gs_write_int(Reactor_strength,SaveFile);
2168 gs_write_int(Num_flickering_lights,SaveFile);
2169 fwrite(Flickering_lights,sizeof(*Flickering_lights),Num_flickering_lights,SaveFile);
2171 gs_write_int(Secret_return_segment, SaveFile);
2172 gs_write_int(Secret_return_orient.rvec.x, SaveFile);
2173 gs_write_int(Secret_return_orient.rvec.y, SaveFile);
2174 gs_write_int(Secret_return_orient.rvec.z, SaveFile);
2175 gs_write_int(Secret_return_orient.fvec.x, SaveFile);
2176 gs_write_int(Secret_return_orient.fvec.y, SaveFile);
2177 gs_write_int(Secret_return_orient.fvec.z, SaveFile);
2178 gs_write_int(Secret_return_orient.uvec.x, SaveFile);
2179 gs_write_int(Secret_return_orient.uvec.y, SaveFile);
2180 gs_write_int(Secret_return_orient.uvec.z, SaveFile);
2182 minedata_offset = ftell(SaveFile);
2183 if ( !compiled_version )
2184 save_mine_data(SaveFile);
2186 save_mine_data_compiled(SaveFile);
2187 gamedata_offset = ftell(SaveFile);
2188 save_game_data(SaveFile);
2190 fseek(SaveFile,sizeof(sig)+sizeof(version),SEEK_SET);
2191 gs_write_int(minedata_offset,SaveFile);
2192 gs_write_int(gamedata_offset,SaveFile);
2194 //==================== CLOSE THE FILE =============================
2197 if ( !compiled_version ) {
2198 if (Function_mode == FMODE_EDITOR)
2199 editor_status("Saved mine %s, \"%s\"",filename,Current_level_name);
2206 extern void compress_uv_coordinates_all(void);
2208 int save_level(char * filename)
2212 // Save normal version...
2213 r1 = save_level_sub(filename, 0);
2215 // Save compiled version...
2216 save_level_sub(filename, 1);
2224 void dump_mine_info(void)
2226 int segnum, sidenum;
2227 fix min_u, max_u, min_v, max_v, min_l, max_l, max_sl;
2239 for (segnum=0; segnum<=Highest_segment_index; segnum++) {
2240 for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
2242 side *sidep = &Segments[segnum].sides[sidenum];
2244 if (Segment2s[segnum].static_light > max_sl)
2245 max_sl = Segment2s[segnum].static_light;
2247 for (vertnum=0; vertnum<4; vertnum++) {
2248 if (sidep->uvls[vertnum].u < min_u)
2249 min_u = sidep->uvls[vertnum].u;
2250 else if (sidep->uvls[vertnum].u > max_u)
2251 max_u = sidep->uvls[vertnum].u;
2253 if (sidep->uvls[vertnum].v < min_v)
2254 min_v = sidep->uvls[vertnum].v;
2255 else if (sidep->uvls[vertnum].v > max_v)
2256 max_v = sidep->uvls[vertnum].v;
2258 if (sidep->uvls[vertnum].l < min_l)
2259 min_l = sidep->uvls[vertnum].l;
2260 else if (sidep->uvls[vertnum].l > max_l)
2261 max_l = sidep->uvls[vertnum].l;
2267 // mprintf((0, "Smallest uvl = %7.3f %7.3f %7.3f. Largest uvl = %7.3f %7.3f %7.3f\n", f2fl(min_u), f2fl(min_v), f2fl(min_l), f2fl(max_u), f2fl(max_v), f2fl(max_l)));
2268 // mprintf((0, "Static light maximum = %7.3f\n", f2fl(max_sl)));
2269 // mprintf((0, "Number of walls: %i\n", Num_walls));
2277 //read in every level in mission and save out compiled version
2278 void save_all_compiled_levels(void)
2280 do_load_save_levels(1);
2283 //read in every level in mission
2284 void load_all_levels(void)
2286 do_load_save_levels(0);
2290 void do_load_save_levels(int save)
2294 if (! SafetyCheck())
2297 no_old_level_file_error=1;
2299 for (level_num=1;level_num<=Last_level;level_num++) {
2300 load_level(Level_names[level_num-1]);
2301 load_palette(Current_level_palette,1,1); //don't change screen
2303 save_level_sub(Level_names[level_num-1],1);
2306 for (level_num=-1;level_num>=Last_secret_level;level_num--) {
2307 load_level(Secret_level_names[-level_num-1]);
2308 load_palette(Current_level_palette,1,1); //don't change screen
2310 save_level_sub(Secret_level_names[-level_num-1],1);
2313 no_old_level_file_error=0;