2006-03-20 17:12:09 +00:00
/*
2014-06-01 17:55:23 +00:00
* Portions of this file are copyright Rebirth contributors and licensed as
* described in COPYING . txt .
* Portions of this file are copyright Parallax Software and licensed
* according to the Parallax license below .
* See COPYING . txt for license details .
2006-03-20 17:12:09 +00:00
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ( " PARALLAX " ) . PARALLAX , IN DISTRIBUTING THE CODE TO
END - USERS , AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN , GRANTS A
ROYALTY - FREE , PERPETUAL LICENSE TO SUCH END - USERS FOR USE BY SUCH END - USERS
IN USING , DISPLAYING , AND CREATING DERIVATIVE WORKS THEREOF , SO LONG AS
SUCH USE , DISPLAY OR CREATION IS FOR NON - COMMERCIAL , ROYALTY OR REVENUE
FREE PURPOSES . IN NO EVENT SHALL THE END - USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE - BEARING PURPOSES . THE END - USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE .
COPYRIGHT 1993 - 1999 PARALLAX SOFTWARE CORPORATION . ALL RIGHTS RESERVED .
*/
/*
*
* Functions to save / restore game state .
*
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <string.h>
# include "pstypes.h"
# include "inferno.h"
# include "segment.h"
# include "textures.h"
# include "wall.h"
# include "object.h"
# include "gamemine.h"
2012-07-07 18:35:06 +00:00
# include "dxxerror.h"
2019-01-20 05:36:55 +00:00
# include "console.h"
2020-04-19 20:47:07 +00:00
# include "config.h"
2006-09-04 22:24:09 +00:00
# include "gamefont.h"
2006-03-20 17:12:09 +00:00
# include "gameseg.h"
# include "switch.h"
# include "game.h"
# include "newmenu.h"
# include "fuelcen.h"
# include "hash.h"
# include "key.h"
# include "piggy.h"
# include "player.h"
2020-04-19 20:47:07 +00:00
# include "playsave.h"
2006-03-20 17:12:09 +00:00
# include "cntrlcen.h"
# include "morph.h"
# include "weapon.h"
# include "render.h"
# include "gameseq.h"
2015-10-09 02:46:10 +00:00
# include "event.h"
2015-04-02 02:36:57 +00:00
# include "robot.h"
2006-03-20 17:12:09 +00:00
# include "gauges.h"
# include "newdemo.h"
# include "automap.h"
# include "piggy.h"
# include "paging.h"
# include "titles.h"
# include "text.h"
# include "mission.h"
# include "pcx.h"
2012-11-11 00:14:30 +00:00
# include "collide.h"
2006-03-20 17:12:09 +00:00
# include "u_mem.h"
# include "args.h"
# include "ai.h"
# include "fireball.h"
# include "controls.h"
# include "laser.h"
2015-04-19 04:18:51 +00:00
# include "hudmsg.h"
2006-03-20 17:12:09 +00:00
# include "state.h"
2009-03-20 12:10:38 +00:00
# include "multi.h"
2006-03-20 17:12:09 +00:00
# include "gr.h"
2016-09-24 18:06:11 +00:00
# if DXX_USE_OGL
2009-09-11 05:15:53 +00:00
# include "ogl_init.h"
2006-03-20 17:12:09 +00:00
# endif
2016-11-15 08:18:56 +00:00
# if DXX_USE_EDITOR
# include "editor/editor.h"
# endif
2014-01-18 18:02:02 +00:00
# include "compiler-range_for.h"
2018-12-13 02:31:38 +00:00
# include "d_enumerate.h"
2020-08-10 03:45:14 +00:00
# include "d_levelstate.h"
2019-05-04 18:27:36 +00:00
# include "d_range.h"
2014-01-18 18:02:02 +00:00
# include "partial_range.h"
2020-07-05 23:34:33 +00:00
# include "d_zip.h"
2020-05-02 21:18:42 +00:00
# include <utility>
2014-01-18 18:02:02 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
# define STATE_VERSION 7
2015-08-27 16:55:14 +00:00
# define STATE_MATCEN_VERSION 25 // specific version of metcen info written into D1 savegames. Currenlty equal to GAME_VERSION (see gamesave.cpp). If changed, then only along with STATE_VERSION.
2013-03-03 01:03:33 +00:00
# define STATE_COMPATIBLE_VERSION 6
# elif defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
# define STATE_VERSION 22
# define STATE_COMPATIBLE_VERSION 20
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
// 0 - Put DGSS (Descent Game State Save) id at tof.
// 1 - Added Difficulty level save
2011-02-14 21:27:07 +00:00
// 2 - Added cheats.enabled flag
2006-03-20 17:12:09 +00:00
// 3 - Added between levels save.
// 4 - Added mission support
// 5 - Mike changed ai and object structure.
// 6 - Added buggin' cheat save
// 7 - Added other cheat saves and game_id.
// 8 - Added AI stuff for escort and thief.
// 9 - Save palette with screen shot
// 12- Saved last_was_super array
// 13- Saved palette flash stuff
// 14- Save cloaking wall stuff
// 15- Save additional ai info
// 16- Save Light_subtracted
// 17- New marker save
// 18- Took out saving of old cheat status
2011-02-14 21:27:07 +00:00
// 19- Saved cheats.enabled flag
2006-03-20 17:12:09 +00:00
// 20- First_secret_visit
// 22- Omega_charge
# define THUMBNAIL_W 100
# define THUMBNAIL_H 50
2016-07-16 16:52:04 +00:00
constexpr char dgss_id [ 4 ] = { ' D ' , ' G ' , ' S ' , ' S ' } ;
2006-03-20 17:12:09 +00:00
2013-10-26 03:40:25 +00:00
unsigned state_game_id ;
2006-03-20 17:12:09 +00:00
2020-04-26 17:26:23 +00:00
namespace dcx {
2019-07-16 04:00:50 +00:00
namespace {
2020-04-26 17:26:23 +00:00
constexpr unsigned NUM_SAVES = d_game_unique_state : : MAXIMUM_SAVE_SLOTS . value ;
2019-07-16 04:00:50 +00:00
struct relocated_player_data
{
fix shields ;
2019-07-16 04:00:50 +00:00
int16_t num_robots_level ;
2019-07-16 04:00:50 +00:00
int16_t num_robots_total ;
2019-07-16 04:00:50 +00:00
uint16_t hostages_total ;
2019-07-16 04:00:50 +00:00
uint8_t hostages_level ;
2019-07-16 04:00:50 +00:00
} ;
2020-01-18 21:57:39 +00:00
struct savegame_mission_path {
2020-05-02 21:18:42 +00:00
std : : array < char , 9 > original ;
std : : array < char , DXX_MAX_MISSION_PATH_LENGTH > full ;
2020-01-18 21:57:39 +00:00
} ;
enum class savegame_mission_name_abi : uint8_t
{
original ,
pathname ,
} ;
static_assert ( sizeof ( savegame_mission_path ) = = sizeof ( savegame_mission_path : : original ) + sizeof ( savegame_mission_path : : full ) , " padding error " ) ;
2020-04-26 17:26:23 +00:00
}
2019-07-16 04:00:50 +00:00
}
2010-12-22 00:17:59 +00:00
// Following functions convert object to object_rw and back to be written to/read from Savegames. Mostly object differs to object_rw in terms of timer values (fix/fix64). as we reset GameTime64 for writing so it can fit into fix it's not necessary to increment savegame version. But if we once store something else into object which might be useful after restoring, it might be handy to increment Savegame version and actually store these new infos.
// turn object to object_rw to be saved to Savegame.
2016-08-25 04:05:32 +00:00
namespace dsx {
2020-04-19 20:47:07 +00:00
2019-12-22 05:34:08 +00:00
static void state_object_to_object_rw ( const object & obj , object_rw * const obj_rw )
2010-12-22 00:17:59 +00:00
{
2019-12-22 05:34:08 +00:00
const auto otype = obj . type ;
2016-10-08 23:24:21 +00:00
obj_rw - > type = otype ;
2020-08-10 03:45:13 +00:00
obj_rw - > signature = static_cast < uint16_t > ( obj . signature ) ;
2019-12-22 05:34:08 +00:00
obj_rw - > id = obj . id ;
obj_rw - > next = obj . next ;
obj_rw - > prev = obj . prev ;
2020-08-10 03:45:13 +00:00
obj_rw - > control_source = static_cast < uint8_t > ( obj . control_source ) ;
2020-08-10 03:45:13 +00:00
obj_rw - > movement_source = static_cast < uint8_t > ( obj . movement_source ) ;
2019-12-22 05:34:08 +00:00
obj_rw - > render_type = obj . render_type ;
obj_rw - > flags = obj . flags ;
obj_rw - > segnum = obj . segnum ;
obj_rw - > attached_obj = obj . attached_obj ;
obj_rw - > pos . x = obj . pos . x ;
obj_rw - > pos . y = obj . pos . y ;
obj_rw - > pos . z = obj . pos . z ;
obj_rw - > orient . rvec . x = obj . orient . rvec . x ;
obj_rw - > orient . rvec . y = obj . orient . rvec . y ;
obj_rw - > orient . rvec . z = obj . orient . rvec . z ;
obj_rw - > orient . fvec . x = obj . orient . fvec . x ;
obj_rw - > orient . fvec . y = obj . orient . fvec . y ;
obj_rw - > orient . fvec . z = obj . orient . fvec . z ;
obj_rw - > orient . uvec . x = obj . orient . uvec . x ;
obj_rw - > orient . uvec . y = obj . orient . uvec . y ;
obj_rw - > orient . uvec . z = obj . orient . uvec . z ;
obj_rw - > size = obj . size ;
obj_rw - > shields = obj . shields ;
2019-12-22 05:34:08 +00:00
obj_rw - > last_pos = obj . pos ;
2019-12-22 05:34:08 +00:00
obj_rw - > contains_type = obj . contains_type ;
obj_rw - > contains_id = obj . contains_id ;
obj_rw - > contains_count = obj . contains_count ;
obj_rw - > matcen_creator = obj . matcen_creator ;
obj_rw - > lifeleft = obj . lifeleft ;
2016-10-08 23:24:21 +00:00
2020-08-10 03:45:13 +00:00
switch ( typename object : : movement_type { obj_rw - > movement_source } )
2010-12-22 00:17:59 +00:00
{
2020-08-10 03:45:13 +00:00
case object : : movement_type : : None :
obj_rw - > mtype = { } ;
break ;
case object : : movement_type : : physics :
2019-12-22 05:34:08 +00:00
obj_rw - > mtype . phys_info . velocity . x = obj . mtype . phys_info . velocity . x ;
obj_rw - > mtype . phys_info . velocity . y = obj . mtype . phys_info . velocity . y ;
obj_rw - > mtype . phys_info . velocity . z = obj . mtype . phys_info . velocity . z ;
obj_rw - > mtype . phys_info . thrust . x = obj . mtype . phys_info . thrust . x ;
obj_rw - > mtype . phys_info . thrust . y = obj . mtype . phys_info . thrust . y ;
obj_rw - > mtype . phys_info . thrust . z = obj . mtype . phys_info . thrust . z ;
obj_rw - > mtype . phys_info . mass = obj . mtype . phys_info . mass ;
obj_rw - > mtype . phys_info . drag = obj . mtype . phys_info . drag ;
2017-01-01 23:19:21 +00:00
obj_rw - > mtype . phys_info . obsolete_brakes = 0 ;
2019-12-22 05:34:08 +00:00
obj_rw - > mtype . phys_info . rotvel . x = obj . mtype . phys_info . rotvel . x ;
obj_rw - > mtype . phys_info . rotvel . y = obj . mtype . phys_info . rotvel . y ;
obj_rw - > mtype . phys_info . rotvel . z = obj . mtype . phys_info . rotvel . z ;
obj_rw - > mtype . phys_info . rotthrust . x = obj . mtype . phys_info . rotthrust . x ;
obj_rw - > mtype . phys_info . rotthrust . y = obj . mtype . phys_info . rotthrust . y ;
obj_rw - > mtype . phys_info . rotthrust . z = obj . mtype . phys_info . rotthrust . z ;
obj_rw - > mtype . phys_info . turnroll = obj . mtype . phys_info . turnroll ;
obj_rw - > mtype . phys_info . flags = obj . mtype . phys_info . flags ;
2010-12-22 00:17:59 +00:00
break ;
2020-08-10 03:45:13 +00:00
case object : : movement_type : : spinning :
2019-12-22 05:34:08 +00:00
obj_rw - > mtype . spin_rate . x = obj . mtype . spin_rate . x ;
obj_rw - > mtype . spin_rate . y = obj . mtype . spin_rate . y ;
obj_rw - > mtype . spin_rate . z = obj . mtype . spin_rate . z ;
2010-12-22 00:17:59 +00:00
break ;
}
2020-08-10 03:45:13 +00:00
switch ( typename object : : control_type { obj_rw - > control_source } )
2010-12-22 00:17:59 +00:00
{
2020-08-06 03:47:56 +00:00
case object : : control_type : : weapon :
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . laser_info . parent_type = obj . ctype . laser_info . parent_type ;
obj_rw - > ctype . laser_info . parent_num = obj . ctype . laser_info . parent_num ;
2020-08-10 03:45:13 +00:00
obj_rw - > ctype . laser_info . parent_signature = static_cast < uint16_t > ( obj . ctype . laser_info . parent_signature ) ;
2019-12-22 05:34:08 +00:00
if ( obj . ctype . laser_info . creation_time - GameTime64 < F1_0 * ( - 18000 ) )
2010-12-22 00:17:59 +00:00
obj_rw - > ctype . laser_info . creation_time = F1_0 * ( - 18000 ) ;
else
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . laser_info . creation_time = obj . ctype . laser_info . creation_time - GameTime64 ;
obj_rw - > ctype . laser_info . last_hitobj = obj . ctype . laser_info . get_last_hitobj ( ) ;
obj_rw - > ctype . laser_info . track_goal = obj . ctype . laser_info . track_goal ;
obj_rw - > ctype . laser_info . multiplier = obj . ctype . laser_info . multiplier ;
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : explosion :
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . expl_info . spawn_time = obj . ctype . expl_info . spawn_time ;
obj_rw - > ctype . expl_info . delete_time = obj . ctype . expl_info . delete_time ;
obj_rw - > ctype . expl_info . delete_objnum = obj . ctype . expl_info . delete_objnum ;
obj_rw - > ctype . expl_info . attach_parent = obj . ctype . expl_info . attach_parent ;
obj_rw - > ctype . expl_info . prev_attach = obj . ctype . expl_info . prev_attach ;
obj_rw - > ctype . expl_info . next_attach = obj . ctype . expl_info . next_attach ;
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : ai :
2010-12-22 00:17:59 +00:00
{
int i ;
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . ai_info . behavior = static_cast < uint8_t > ( obj . ctype . ai_info . behavior ) ;
2010-12-22 00:17:59 +00:00
for ( i = 0 ; i < MAX_AI_FLAGS ; i + + )
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . ai_info . flags [ i ] = obj . ctype . ai_info . flags [ i ] ;
obj_rw - > ctype . ai_info . hide_segment = obj . ctype . ai_info . hide_segment ;
obj_rw - > ctype . ai_info . hide_index = obj . ctype . ai_info . hide_index ;
obj_rw - > ctype . ai_info . path_length = obj . ctype . ai_info . path_length ;
obj_rw - > ctype . ai_info . cur_path_index = obj . ctype . ai_info . cur_path_index ;
obj_rw - > ctype . ai_info . danger_laser_num = obj . ctype . ai_info . danger_laser_num ;
if ( obj . ctype . ai_info . danger_laser_num ! = object_none )
2020-08-10 03:45:13 +00:00
obj_rw - > ctype . ai_info . danger_laser_signature = static_cast < uint16_t > ( obj . ctype . ai_info . danger_laser_signature ) ;
2017-01-21 19:05:43 +00:00
else
obj_rw - > ctype . ai_info . danger_laser_signature = 0 ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2015-05-09 17:39:01 +00:00
obj_rw - > ctype . ai_info . follow_path_start_seg = segment_none ;
2015-05-09 17:39:00 +00:00
obj_rw - > ctype . ai_info . follow_path_end_seg = segment_none ;
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . ai_info . dying_sound_playing = obj . ctype . ai_info . dying_sound_playing ;
if ( obj . ctype . ai_info . dying_start_time = = 0 ) // if bot not dead, anything but 0 will kill it
2011-01-05 23:28:43 +00:00
obj_rw - > ctype . ai_info . dying_start_time = 0 ;
2010-12-22 00:17:59 +00:00
else
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . ai_info . dying_start_time = obj . ctype . ai_info . dying_start_time - GameTime64 ;
2013-03-03 01:03:33 +00:00
# endif
2010-12-22 00:17:59 +00:00
break ;
}
2020-08-06 03:47:56 +00:00
case object : : control_type : : light :
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . light_info . intensity = obj . ctype . light_info . intensity ;
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : powerup :
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . powerup_info . count = obj . ctype . powerup_info . count ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2019-12-22 05:34:08 +00:00
if ( obj . ctype . powerup_info . creation_time - GameTime64 < F1_0 * ( - 18000 ) )
2010-12-22 00:17:59 +00:00
obj_rw - > ctype . powerup_info . creation_time = F1_0 * ( - 18000 ) ;
else
2019-12-22 05:34:08 +00:00
obj_rw - > ctype . powerup_info . creation_time = obj . ctype . powerup_info . creation_time - GameTime64 ;
obj_rw - > ctype . powerup_info . flags = obj . ctype . powerup_info . flags ;
2013-03-03 01:03:33 +00:00
# endif
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : None :
case object : : control_type : : flying :
case object : : control_type : : slew :
case object : : control_type : : flythrough :
case object : : control_type : : repaircen :
case object : : control_type : : morph :
case object : : control_type : : debris :
case object : : control_type : : remote :
default :
break ;
2010-12-22 00:17:59 +00:00
}
2020-08-06 03:47:56 +00:00
2010-12-22 00:17:59 +00:00
switch ( obj_rw - > render_type )
{
case RT_MORPH :
case RT_POLYOBJ :
case RT_NONE : // HACK below
{
int i ;
2019-12-22 05:34:08 +00:00
if ( obj . render_type = = RT_NONE & & obj . type ! = OBJ_GHOST ) // HACK: when a player is dead or not connected yet, clients still expect to get polyobj data - even if render_type == RT_NONE at this time. Here it's not important, but it might be for Multiplayer Savegames.
2010-12-22 00:17:59 +00:00
break ;
2019-12-22 05:34:08 +00:00
obj_rw - > rtype . pobj_info . model_num = obj . rtype . pobj_info . model_num ;
2010-12-22 00:17:59 +00:00
for ( i = 0 ; i < MAX_SUBMODELS ; i + + )
{
2019-12-22 05:34:08 +00:00
obj_rw - > rtype . pobj_info . anim_angles [ i ] . p = obj . rtype . pobj_info . anim_angles [ i ] . p ;
obj_rw - > rtype . pobj_info . anim_angles [ i ] . b = obj . rtype . pobj_info . anim_angles [ i ] . b ;
obj_rw - > rtype . pobj_info . anim_angles [ i ] . h = obj . rtype . pobj_info . anim_angles [ i ] . h ;
2010-12-22 00:17:59 +00:00
}
2019-12-22 05:34:08 +00:00
obj_rw - > rtype . pobj_info . subobj_flags = obj . rtype . pobj_info . subobj_flags ;
obj_rw - > rtype . pobj_info . tmap_override = obj . rtype . pobj_info . tmap_override ;
obj_rw - > rtype . pobj_info . alt_textures = obj . rtype . pobj_info . alt_textures ;
2010-12-22 00:17:59 +00:00
break ;
}
case RT_WEAPON_VCLIP :
case RT_HOSTAGE :
case RT_POWERUP :
case RT_FIREBALL :
2019-12-22 05:34:08 +00:00
obj_rw - > rtype . vclip_info . vclip_num = obj . rtype . vclip_info . vclip_num ;
obj_rw - > rtype . vclip_info . frametime = obj . rtype . vclip_info . frametime ;
obj_rw - > rtype . vclip_info . framenum = obj . rtype . vclip_info . framenum ;
2010-12-22 00:17:59 +00:00
break ;
case RT_LASER :
break ;
}
}
// turn object_rw to object after reading from Savegame
2019-12-22 05:34:08 +00:00
static void state_object_rw_to_object ( const object_rw * const obj_rw , object & obj )
2010-12-22 00:17:59 +00:00
{
2019-12-22 05:34:08 +00:00
obj = { } ;
DXX_POISON_VAR ( obj , 0xfd ) ;
set_object_type ( obj , obj_rw - > type ) ;
if ( obj . type = = OBJ_NONE )
2019-07-07 22:00:02 +00:00
{
2019-12-22 05:34:08 +00:00
obj . signature = object_signature_t { 0 } ;
2017-01-07 23:34:18 +00:00
return ;
2019-07-07 22:00:02 +00:00
}
2017-01-07 23:34:18 +00:00
2019-12-22 05:34:08 +00:00
obj . signature = object_signature_t { static_cast < uint16_t > ( obj_rw - > signature ) } ;
obj . id = obj_rw - > id ;
obj . next = obj_rw - > next ;
obj . prev = obj_rw - > prev ;
2020-08-10 03:45:13 +00:00
obj . control_source = typename object : : control_type { obj_rw - > control_source } ;
2020-08-10 03:45:13 +00:00
obj . movement_source = typename object : : movement_type { obj_rw - > movement_source } ;
2020-05-02 21:18:43 +00:00
const auto render_type = obj_rw - > render_type ;
if ( valid_render_type ( render_type ) )
obj . render_type = render_type_t { render_type } ;
else
{
con_printf ( CON_URGENT , " save file used bogus render type %#x for object %p; using none instead " , render_type , & obj ) ;
obj . render_type = RT_NONE ;
}
2019-12-22 05:34:08 +00:00
obj . flags = obj_rw - > flags ;
obj . segnum = obj_rw - > segnum ;
obj . attached_obj = obj_rw - > attached_obj ;
obj . pos . x = obj_rw - > pos . x ;
obj . pos . y = obj_rw - > pos . y ;
obj . pos . z = obj_rw - > pos . z ;
obj . orient . rvec . x = obj_rw - > orient . rvec . x ;
obj . orient . rvec . y = obj_rw - > orient . rvec . y ;
obj . orient . rvec . z = obj_rw - > orient . rvec . z ;
obj . orient . fvec . x = obj_rw - > orient . fvec . x ;
obj . orient . fvec . y = obj_rw - > orient . fvec . y ;
obj . orient . fvec . z = obj_rw - > orient . fvec . z ;
obj . orient . uvec . x = obj_rw - > orient . uvec . x ;
obj . orient . uvec . y = obj_rw - > orient . uvec . y ;
obj . orient . uvec . z = obj_rw - > orient . uvec . z ;
obj . size = obj_rw - > size ;
obj . shields = obj_rw - > shields ;
obj . contains_type = obj_rw - > contains_type ;
obj . contains_id = obj_rw - > contains_id ;
obj . contains_count = obj_rw - > contains_count ;
obj . matcen_creator = obj_rw - > matcen_creator ;
obj . lifeleft = obj_rw - > lifeleft ;
2010-12-22 00:17:59 +00:00
2020-08-10 03:45:13 +00:00
switch ( obj . movement_source )
2010-12-22 00:17:59 +00:00
{
2020-08-10 03:45:13 +00:00
case object : : movement_type : : None :
2016-11-20 23:12:00 +00:00
break ;
2020-08-10 03:45:13 +00:00
case object : : movement_type : : physics :
2019-12-22 05:34:08 +00:00
obj . mtype . phys_info . velocity . x = obj_rw - > mtype . phys_info . velocity . x ;
obj . mtype . phys_info . velocity . y = obj_rw - > mtype . phys_info . velocity . y ;
obj . mtype . phys_info . velocity . z = obj_rw - > mtype . phys_info . velocity . z ;
obj . mtype . phys_info . thrust . x = obj_rw - > mtype . phys_info . thrust . x ;
obj . mtype . phys_info . thrust . y = obj_rw - > mtype . phys_info . thrust . y ;
obj . mtype . phys_info . thrust . z = obj_rw - > mtype . phys_info . thrust . z ;
obj . mtype . phys_info . mass = obj_rw - > mtype . phys_info . mass ;
obj . mtype . phys_info . drag = obj_rw - > mtype . phys_info . drag ;
obj . mtype . phys_info . rotvel . x = obj_rw - > mtype . phys_info . rotvel . x ;
obj . mtype . phys_info . rotvel . y = obj_rw - > mtype . phys_info . rotvel . y ;
obj . mtype . phys_info . rotvel . z = obj_rw - > mtype . phys_info . rotvel . z ;
obj . mtype . phys_info . rotthrust . x = obj_rw - > mtype . phys_info . rotthrust . x ;
obj . mtype . phys_info . rotthrust . y = obj_rw - > mtype . phys_info . rotthrust . y ;
obj . mtype . phys_info . rotthrust . z = obj_rw - > mtype . phys_info . rotthrust . z ;
obj . mtype . phys_info . turnroll = obj_rw - > mtype . phys_info . turnroll ;
obj . mtype . phys_info . flags = obj_rw - > mtype . phys_info . flags ;
2010-12-22 00:17:59 +00:00
break ;
2020-08-10 03:45:13 +00:00
case object : : movement_type : : spinning :
2019-12-22 05:34:08 +00:00
obj . mtype . spin_rate . x = obj_rw - > mtype . spin_rate . x ;
obj . mtype . spin_rate . y = obj_rw - > mtype . spin_rate . y ;
obj . mtype . spin_rate . z = obj_rw - > mtype . spin_rate . z ;
2010-12-22 00:17:59 +00:00
break ;
}
2020-08-10 03:45:13 +00:00
switch ( obj . control_source )
2010-12-22 00:17:59 +00:00
{
2020-08-06 03:47:56 +00:00
case object : : control_type : : weapon :
2019-12-22 05:34:08 +00:00
obj . ctype . laser_info . parent_type = obj_rw - > ctype . laser_info . parent_type ;
obj . ctype . laser_info . parent_num = obj_rw - > ctype . laser_info . parent_num ;
obj . ctype . laser_info . parent_signature = object_signature_t { static_cast < uint16_t > ( obj_rw - > ctype . laser_info . parent_signature ) } ;
obj . ctype . laser_info . creation_time = obj_rw - > ctype . laser_info . creation_time ;
obj . ctype . laser_info . reset_hitobj ( obj_rw - > ctype . laser_info . last_hitobj ) ;
obj . ctype . laser_info . track_goal = obj_rw - > ctype . laser_info . track_goal ;
obj . ctype . laser_info . multiplier = obj_rw - > ctype . laser_info . multiplier ;
2013-08-24 22:09:58 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2019-12-22 05:34:08 +00:00
obj . ctype . laser_info . last_afterburner_time = 0 ;
2013-08-24 22:09:58 +00:00
# endif
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : explosion :
2019-12-22 05:34:08 +00:00
obj . ctype . expl_info . spawn_time = obj_rw - > ctype . expl_info . spawn_time ;
obj . ctype . expl_info . delete_time = obj_rw - > ctype . expl_info . delete_time ;
obj . ctype . expl_info . delete_objnum = obj_rw - > ctype . expl_info . delete_objnum ;
obj . ctype . expl_info . attach_parent = obj_rw - > ctype . expl_info . attach_parent ;
obj . ctype . expl_info . prev_attach = obj_rw - > ctype . expl_info . prev_attach ;
obj . ctype . expl_info . next_attach = obj_rw - > ctype . expl_info . next_attach ;
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : ai :
2010-12-22 00:17:59 +00:00
{
int i ;
2019-12-22 05:34:08 +00:00
obj . ctype . ai_info . behavior = static_cast < ai_behavior > ( obj_rw - > ctype . ai_info . behavior ) ;
2010-12-22 00:17:59 +00:00
for ( i = 0 ; i < MAX_AI_FLAGS ; i + + )
2019-12-22 05:34:08 +00:00
obj . ctype . ai_info . flags [ i ] = obj_rw - > ctype . ai_info . flags [ i ] ;
obj . ctype . ai_info . hide_segment = obj_rw - > ctype . ai_info . hide_segment ;
obj . ctype . ai_info . hide_index = obj_rw - > ctype . ai_info . hide_index ;
obj . ctype . ai_info . path_length = obj_rw - > ctype . ai_info . path_length ;
obj . ctype . ai_info . cur_path_index = obj_rw - > ctype . ai_info . cur_path_index ;
obj . ctype . ai_info . danger_laser_num = obj_rw - > ctype . ai_info . danger_laser_num ;
if ( obj . ctype . ai_info . danger_laser_num ! = object_none )
obj . ctype . ai_info . danger_laser_signature = object_signature_t { static_cast < uint16_t > ( obj_rw - > ctype . ai_info . danger_laser_signature ) } ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
# elif defined(DXX_BUILD_DESCENT_II)
2019-12-22 05:34:08 +00:00
obj . ctype . ai_info . dying_sound_playing = obj_rw - > ctype . ai_info . dying_sound_playing ;
obj . ctype . ai_info . dying_start_time = obj_rw - > ctype . ai_info . dying_start_time ;
2013-03-03 01:03:33 +00:00
# endif
2010-12-22 00:17:59 +00:00
break ;
}
2020-08-06 03:47:56 +00:00
case object : : control_type : : light :
2019-12-22 05:34:08 +00:00
obj . ctype . light_info . intensity = obj_rw - > ctype . light_info . intensity ;
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : powerup :
2019-12-22 05:34:08 +00:00
obj . ctype . powerup_info . count = obj_rw - > ctype . powerup_info . count ;
2015-07-02 02:37:55 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2019-12-22 05:34:08 +00:00
obj . ctype . powerup_info . creation_time = 0 ;
obj . ctype . powerup_info . flags = 0 ;
2015-07-02 02:37:55 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2019-12-22 05:34:08 +00:00
obj . ctype . powerup_info . creation_time = obj_rw - > ctype . powerup_info . creation_time ;
obj . ctype . powerup_info . flags = obj_rw - > ctype . powerup_info . flags ;
2013-03-03 01:03:33 +00:00
# endif
2010-12-22 00:17:59 +00:00
break ;
2020-08-06 03:47:56 +00:00
case object : : control_type : : cntrlcen :
2013-08-03 10:53:56 +00:00
{
2019-12-22 05:34:08 +00:00
if ( obj . type = = OBJ_GHOST )
2017-02-04 23:10:29 +00:00
{
/* Boss missions convert the reactor into OBJ_GHOST
* instead of freeing it . Old releases ( before
* ed46a05296f9d480f934d8c951c4755ebac1d5e7 ( " Update
* control_type when ghosting reactor " )) did not update
* ` control_type ` , so games saved by those releases have an
2020-08-10 03:45:13 +00:00
* object with obj - > type = = OBJ_GHOST and obj - > control_source = =
2020-08-06 03:47:56 +00:00
* object : : control_type : : cntrlcen . That inconsistency triggers an assertion down
2017-02-04 23:10:29 +00:00
* in ` calc_controlcen_gun_point ` because obj - > type ! =
* OBJ_CNTRLCEN .
*
* Add a special case here to correct this
* inconsistency .
*/
2020-08-10 03:45:13 +00:00
obj . control_source = object : : control_type : : None ;
2017-02-04 23:10:29 +00:00
break ;
}
2013-08-03 10:53:56 +00:00
// gun points of reactor now part of the object but of course not saved in object_rw and overwritten due to reset_objects(). Let's just recompute them.
2015-08-26 03:15:10 +00:00
calc_controlcen_gun_point ( obj ) ;
2013-08-03 10:53:56 +00:00
break ;
}
2020-08-06 03:47:56 +00:00
case object : : control_type : : None :
case object : : control_type : : flying :
case object : : control_type : : slew :
case object : : control_type : : flythrough :
case object : : control_type : : repaircen :
case object : : control_type : : morph :
case object : : control_type : : debris :
case object : : control_type : : remote :
default :
break ;
2010-12-22 00:17:59 +00:00
}
2019-12-22 05:34:08 +00:00
switch ( obj . render_type )
2010-12-22 00:17:59 +00:00
{
case RT_MORPH :
case RT_POLYOBJ :
case RT_NONE : // HACK below
{
int i ;
2019-12-22 05:34:08 +00:00
if ( obj . render_type = = RT_NONE & & obj . type ! = OBJ_GHOST ) // HACK: when a player is dead or not connected yet, clients still expect to get polyobj data - even if render_type == RT_NONE at this time. Here it's not important, but it might be for Multiplayer Savegames.
2010-12-22 00:17:59 +00:00
break ;
2019-12-22 05:34:08 +00:00
obj . rtype . pobj_info . model_num = obj_rw - > rtype . pobj_info . model_num ;
2010-12-22 00:17:59 +00:00
for ( i = 0 ; i < MAX_SUBMODELS ; i + + )
{
2019-12-22 05:34:08 +00:00
obj . rtype . pobj_info . anim_angles [ i ] . p = obj_rw - > rtype . pobj_info . anim_angles [ i ] . p ;
obj . rtype . pobj_info . anim_angles [ i ] . b = obj_rw - > rtype . pobj_info . anim_angles [ i ] . b ;
obj . rtype . pobj_info . anim_angles [ i ] . h = obj_rw - > rtype . pobj_info . anim_angles [ i ] . h ;
2010-12-22 00:17:59 +00:00
}
2019-12-22 05:34:08 +00:00
obj . rtype . pobj_info . subobj_flags = obj_rw - > rtype . pobj_info . subobj_flags ;
obj . rtype . pobj_info . tmap_override = obj_rw - > rtype . pobj_info . tmap_override ;
obj . rtype . pobj_info . alt_textures = obj_rw - > rtype . pobj_info . alt_textures ;
2010-12-22 00:17:59 +00:00
break ;
}
case RT_WEAPON_VCLIP :
case RT_HOSTAGE :
case RT_POWERUP :
case RT_FIREBALL :
2019-12-22 05:34:08 +00:00
obj . rtype . vclip_info . vclip_num = obj_rw - > rtype . vclip_info . vclip_num ;
obj . rtype . vclip_info . frametime = obj_rw - > rtype . vclip_info . frametime ;
obj . rtype . vclip_info . framenum = obj_rw - > rtype . vclip_info . framenum ;
2010-12-22 00:17:59 +00:00
break ;
case RT_LASER :
break ;
}
}
2020-04-19 20:47:07 +00:00
deny_save_result deny_save_game ( fvcobjptr & vcobjptr , const d_level_unique_control_center_state & LevelUniqueControlCenterState , const d_game_unique_state & GameUniqueState )
{
# if defined(DXX_BUILD_DESCENT_I)
( void ) GameUniqueState ;
# elif defined(DXX_BUILD_DESCENT_II)
if ( Current_level_num < 0 )
return deny_save_result : : denied ;
if ( GameUniqueState . Final_boss_countdown_time ) //don't allow save while final boss is dying
return deny_save_result : : denied ;
# endif
2020-05-17 23:35:25 +00:00
return deny_save_game ( vcobjptr , LevelUniqueControlCenterState ) ;
2020-04-19 20:47:07 +00:00
}
namespace {
2010-12-22 00:17:59 +00:00
// Following functions convert player to player_rw and back to be written to/read from Savegames. player only differ to player_rw in terms of timer values (fix/fix64). as we reset GameTime64 for writing so it can fit into fix it's not necessary to increment savegame version. But if we once store something else into object which might be useful after restoring, it might be handy to increment Savegame version and actually store these new infos.
// turn player to player_rw to be saved to Savegame.
2019-07-16 04:00:50 +00:00
static void state_player_to_player_rw ( const relocated_player_data & rpd , const player * pl , player_rw * pl_rw , const player_info & pl_info )
2010-12-22 00:17:59 +00:00
{
int i = 0 ;
2014-07-05 16:48:12 +00:00
pl_rw - > callsign = pl - > callsign ;
2013-09-20 23:25:10 +00:00
memset ( pl_rw - > net_address , 0 , 6 ) ;
2010-12-22 00:17:59 +00:00
pl_rw - > connected = pl - > connected ;
pl_rw - > objnum = pl - > objnum ;
2013-09-20 23:25:10 +00:00
pl_rw - > n_packets_got = 0 ;
pl_rw - > n_packets_sent = 0 ;
2015-11-07 21:55:59 +00:00
pl_rw - > flags = pl_info . powerup_flags . get_player_flags ( ) ;
2015-11-07 21:55:58 +00:00
pl_rw - > energy = pl_info . energy ;
2019-07-16 04:00:50 +00:00
pl_rw - > shields = rpd . shields ;
2018-12-01 01:58:37 +00:00
/*
* The savegame only allocates a uint8_t for this value . If the
* player has exceeded the maximum representable value , cap at that
* value . This is better than truncating the value , since the
* player will get to keep more lives this way than with truncation .
*/
pl_rw - > lives = std : : min < unsigned > ( pl - > lives , std : : numeric_limits < uint8_t > : : max ( ) ) ;
2010-12-22 00:17:59 +00:00
pl_rw - > level = pl - > level ;
2020-08-24 01:31:28 +00:00
pl_rw - > laser_level = static_cast < uint8_t > ( pl_info . laser_level ) ;
2010-12-22 00:17:59 +00:00
pl_rw - > starting_level = pl - > starting_level ;
2015-11-07 21:55:59 +00:00
pl_rw - > killer_objnum = pl_info . killer_objnum ;
2015-11-07 21:55:59 +00:00
pl_rw - > primary_weapon_flags = pl_info . primary_weapon_flags ;
2017-02-26 09:29:55 +00:00
# if defined(DXX_BUILD_DESCENT_I)
// make sure no side effects for Mac demo
pl_rw - > secondary_weapon_flags = 0x0f | ( pl_info . secondary_ammo [ MEGA_INDEX ] > 0 ) < < MEGA_INDEX ;
# elif defined(DXX_BUILD_DESCENT_II)
// make sure no side effects for PC demo
pl_rw - > secondary_weapon_flags = 0xef | ( pl_info . secondary_ammo [ MEGA_INDEX ] > 0 ) < < MEGA_INDEX
| ( pl_info . secondary_ammo [ SMISSILE4_INDEX ] > 0 ) < < SMISSILE4_INDEX // mercury missile
| ( pl_info . secondary_ammo [ SMISSILE5_INDEX ] > 0 ) < < SMISSILE5_INDEX ; // earthshaker missile
# endif
2017-01-01 23:19:21 +00:00
pl_rw - > obsolete_primary_ammo = { } ;
2017-08-11 23:43:53 +00:00
pl_rw - > vulcan_ammo = pl_info . vulcan_ammo ;
2010-12-22 00:17:59 +00:00
for ( i = 0 ; i < MAX_SECONDARY_WEAPONS ; i + + )
2015-11-07 21:55:58 +00:00
pl_rw - > secondary_ammo [ i ] = pl_info . secondary_ammo [ i ] ;
2017-01-01 23:19:21 +00:00
# if defined(DXX_BUILD_DESCENT_II)
pl_rw - > pad = 0 ;
# endif
2016-10-15 00:53:19 +00:00
pl_rw - > last_score = pl_info . mission . last_score ;
2016-10-15 00:53:19 +00:00
pl_rw - > score = pl_info . mission . score ;
2010-12-22 00:17:59 +00:00
pl_rw - > time_level = pl - > time_level ;
pl_rw - > time_total = pl - > time_total ;
2017-01-01 00:45:46 +00:00
if ( ! ( pl_info . powerup_flags & PLAYER_FLAGS_CLOAKED ) | | pl_info . cloak_time - GameTime64 < F1_0 * ( - 18000 ) )
2010-12-22 00:17:59 +00:00
pl_rw - > cloak_time = F1_0 * ( - 18000 ) ;
else
2015-11-14 18:17:21 +00:00
pl_rw - > cloak_time = pl_info . cloak_time - GameTime64 ;
2017-01-01 00:45:46 +00:00
if ( ! ( pl_info . powerup_flags & PLAYER_FLAGS_INVULNERABLE ) | | pl_info . invulnerable_time - GameTime64 < F1_0 * ( - 18000 ) )
2010-12-22 00:17:59 +00:00
pl_rw - > invulnerable_time = F1_0 * ( - 18000 ) ;
else
2015-11-14 18:17:21 +00:00
pl_rw - > invulnerable_time = pl_info . invulnerable_time - GameTime64 ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2016-11-12 18:10:07 +00:00
pl_rw - > KillGoalCount = pl_info . KillGoalCount ;
2013-03-03 01:03:33 +00:00
# endif
2016-11-12 18:10:07 +00:00
pl_rw - > net_killed_total = pl_info . net_killed_total ;
2016-11-12 18:10:07 +00:00
pl_rw - > net_kills_total = pl_info . net_kills_total ;
2010-12-22 00:17:59 +00:00
pl_rw - > num_kills_level = pl - > num_kills_level ;
pl_rw - > num_kills_total = pl - > num_kills_total ;
2019-07-16 04:00:50 +00:00
pl_rw - > num_robots_level = LevelUniqueObjectState . accumulated_robots ;
2019-07-16 04:00:50 +00:00
pl_rw - > num_robots_total = GameUniqueState . accumulated_robots ;
2016-10-15 00:53:19 +00:00
pl_rw - > hostages_rescued_total = pl_info . mission . hostages_rescued_total ;
2019-07-16 04:00:50 +00:00
pl_rw - > hostages_total = GameUniqueState . total_hostages ;
2016-10-15 00:53:19 +00:00
pl_rw - > hostages_on_board = pl_info . mission . hostages_on_board ;
2019-07-16 04:00:50 +00:00
pl_rw - > hostages_level = LevelUniqueObjectState . total_hostages ;
2015-11-14 18:17:22 +00:00
pl_rw - > homing_object_dist = pl_info . homing_object_dist ;
2010-12-22 00:17:59 +00:00
pl_rw - > hours_level = pl - > hours_level ;
pl_rw - > hours_total = pl - > hours_total ;
}
// turn player_rw to player after reading from Savegame
2019-07-16 04:00:50 +00:00
static void state_player_rw_to_player ( const player_rw * pl_rw , player * pl , player_info & pl_info , relocated_player_data & rpd )
2010-12-22 00:17:59 +00:00
{
int i = 0 ;
2014-07-05 16:48:12 +00:00
pl - > callsign = pl_rw - > callsign ;
2010-12-22 00:17:59 +00:00
pl - > connected = pl_rw - > connected ;
pl - > objnum = pl_rw - > objnum ;
2015-11-07 21:55:59 +00:00
pl_info . powerup_flags = player_flags ( pl_rw - > flags ) ;
2015-11-07 21:55:58 +00:00
pl_info . energy = pl_rw - > energy ;
2019-07-16 04:00:50 +00:00
rpd . shields = pl_rw - > shields ;
2010-12-22 00:17:59 +00:00
pl - > lives = pl_rw - > lives ;
pl - > level = pl_rw - > level ;
2020-08-24 01:31:28 +00:00
pl_info . laser_level = laser_level { pl_rw - > laser_level } ;
2010-12-22 00:17:59 +00:00
pl - > starting_level = pl_rw - > starting_level ;
2015-11-07 21:55:59 +00:00
pl_info . killer_objnum = pl_rw - > killer_objnum ;
2015-11-07 21:55:59 +00:00
pl_info . primary_weapon_flags = pl_rw - > primary_weapon_flags ;
2015-11-07 21:55:59 +00:00
pl_info . vulcan_ammo = pl_rw - > vulcan_ammo ;
2010-12-22 00:17:59 +00:00
for ( i = 0 ; i < MAX_SECONDARY_WEAPONS ; i + + )
2015-11-07 21:55:58 +00:00
pl_info . secondary_ammo [ i ] = pl_rw - > secondary_ammo [ i ] ;
2016-10-15 00:53:19 +00:00
pl_info . mission . last_score = pl_rw - > last_score ;
2016-10-15 00:53:19 +00:00
pl_info . mission . score = pl_rw - > score ;
2010-12-22 00:17:59 +00:00
pl - > time_level = pl_rw - > time_level ;
pl - > time_total = pl_rw - > time_total ;
2015-11-14 18:17:21 +00:00
pl_info . cloak_time = pl_rw - > cloak_time ;
2015-11-14 18:17:21 +00:00
pl_info . invulnerable_time = pl_rw - > invulnerable_time ;
2016-12-17 18:39:18 +00:00
# if defined(DXX_BUILD_DESCENT_I)
pl_info . KillGoalCount = 0 ;
# elif defined(DXX_BUILD_DESCENT_II)
2016-11-12 18:10:07 +00:00
pl_info . KillGoalCount = pl_rw - > KillGoalCount ;
2013-03-03 01:03:33 +00:00
# endif
2016-11-12 18:10:07 +00:00
pl_info . net_killed_total = pl_rw - > net_killed_total ;
2016-11-12 18:10:07 +00:00
pl_info . net_kills_total = pl_rw - > net_kills_total ;
2010-12-22 00:17:59 +00:00
pl - > num_kills_level = pl_rw - > num_kills_level ;
pl - > num_kills_total = pl_rw - > num_kills_total ;
2019-07-16 04:00:50 +00:00
rpd . num_robots_level = pl_rw - > num_robots_level ;
2019-07-16 04:00:50 +00:00
rpd . num_robots_total = pl_rw - > num_robots_total ;
2016-10-15 00:53:19 +00:00
pl_info . mission . hostages_rescued_total = pl_rw - > hostages_rescued_total ;
2019-07-16 04:00:50 +00:00
rpd . hostages_total = pl_rw - > hostages_total ;
2016-10-15 00:53:19 +00:00
pl_info . mission . hostages_on_board = pl_rw - > hostages_on_board ;
2019-07-16 04:00:50 +00:00
rpd . hostages_level = pl_rw - > hostages_level ;
2015-11-14 18:17:22 +00:00
pl_info . homing_object_dist = pl_rw - > homing_object_dist ;
2010-12-22 00:17:59 +00:00
pl - > hours_level = pl_rw - > hours_level ;
pl - > hours_total = pl_rw - > hours_total ;
}
2019-07-16 04:00:50 +00:00
static void state_write_player ( PHYSFS_File * fp , const player & pl , const relocated_player_data & rpd , const player_info & pl_info )
2013-11-02 16:24:51 +00:00
{
player_rw pl_rw ;
2019-07-16 04:00:50 +00:00
state_player_to_player_rw ( rpd , & pl , & pl_rw , pl_info ) ;
2013-11-02 16:24:51 +00:00
PHYSFS_write ( fp , & pl_rw , sizeof ( pl_rw ) , 1 ) ;
}
2010-12-22 00:17:59 +00:00
2019-07-16 04:00:50 +00:00
static void state_read_player ( PHYSFS_File * fp , player & pl , int swap , player_info & pl_info , relocated_player_data & rpd )
2013-11-02 16:29:43 +00:00
{
player_rw pl_rw ;
PHYSFS_read ( fp , & pl_rw , sizeof ( pl_rw ) , 1 ) ;
player_rw_swap ( & pl_rw , swap ) ;
2019-07-16 04:00:50 +00:00
state_player_rw_to_player ( & pl_rw , & pl , pl_info , rpd ) ;
}
2020-04-19 20:47:07 +00:00
void state_format_savegame_filename ( d_game_unique_state : : savegame_file_path & filename , const unsigned i )
{
snprintf ( filename . data ( ) , filename . size ( ) , PLAYER_DIRECTORY_STRING ( " %.8s.%cg%x " ) , static_cast < const char * > ( InterfaceUniqueState . PilotName ) , ( Game_mode & GM_MULTI_COOP ) ? ' m ' : ' s ' , i ) ;
}
void state_autosave_game ( const int multiplayer )
{
d_game_unique_state : : savegame_description desc ;
const char * p ;
time_t t = time ( nullptr ) ;
if ( struct tm * ptm = ( t = = - 1 ) ? nullptr : localtime ( & t ) )
{
p = desc . data ( ) ;
strftime ( desc . data ( ) , desc . size ( ) , " auto %m-%d %H:%M:%S " , ptm ) ;
}
else
p = " <autosave> " ;
if ( multiplayer )
{
const auto & & player_range = partial_const_range ( Players , N_players ) ;
2020-04-26 17:26:23 +00:00
multi_execute_save_game ( d_game_unique_state : : save_slot : : _autosave , desc , player_range ) ;
2020-04-19 20:47:07 +00:00
}
else
{
d_game_unique_state : : savegame_file_path filename ;
state_format_savegame_filename ( filename , NUM_SAVES - 1 ) ;
if ( state_save_all_sub ( filename . data ( ) , p ) )
con_printf ( CON_NORMAL , " Autosave written to \" %s \" " , filename . data ( ) ) ;
}
}
}
void state_set_immediate_autosave ( d_game_unique_state & GameUniqueState )
{
GameUniqueState . Next_autosave = { } ;
}
void state_set_next_autosave ( d_game_unique_state & GameUniqueState , const std : : chrono : : steady_clock : : time_point now , const autosave_interval_type interval )
{
GameUniqueState . Next_autosave = now + interval ;
}
void state_set_next_autosave ( d_game_unique_state & GameUniqueState , const autosave_interval_type interval )
{
const auto now = std : : chrono : : steady_clock : : now ( ) ;
state_set_next_autosave ( GameUniqueState , now , interval ) ;
}
void state_poll_autosave_game ( d_game_unique_state & GameUniqueState , const d_level_unique_object_state & LevelUniqueObjectState )
{
const auto multiplayer = Game_mode & GM_MULTI ;
if ( multiplayer & & ! multi_i_am_master ( ) )
return ;
const auto interval = ( multiplayer ? static_cast < const d_gameplay_options & > ( Netgame . MPGameplayOptions ) : PlayerCfg . SPGameplayOptions ) . AutosaveInterval ;
if ( interval . count ( ) < = 0 )
/* Autosave is disabled */
return ;
auto & LevelUniqueControlCenterState = LevelUniqueObjectState . ControlCenterState ;
auto & Objects = LevelUniqueObjectState . Objects ;
if ( deny_save_game ( Objects . vcptr , LevelUniqueControlCenterState , GameUniqueState ) ! = deny_save_result : : allowed )
return ;
const auto now = std : : chrono : : steady_clock : : now ( ) ;
if ( now < GameUniqueState . Next_autosave )
return ;
state_set_next_autosave ( GameUniqueState , now , interval ) ;
state_autosave_game ( multiplayer ) ;
2019-07-16 04:00:50 +00:00
}
2020-04-19 20:47:07 +00:00
2013-11-02 16:29:43 +00:00
}
2015-08-12 03:11:46 +00:00
namespace {
2006-03-20 17:12:09 +00:00
//-------------------------------------------------------------------
2015-01-17 18:31:42 +00:00
struct state_userdata
2006-03-20 17:12:09 +00:00
{
2020-07-22 03:11:18 +00:00
static constexpr std : : integral_constant < unsigned , 1 > decorative_item_count = { } ;
2020-04-26 17:26:23 +00:00
unsigned citem ;
2020-05-02 21:18:42 +00:00
std : : array < grs_bitmap_ptr , NUM_SAVES > sc_bmp ;
2015-01-17 18:31:42 +00:00
} ;
2015-08-12 03:11:46 +00:00
}
2015-01-17 18:31:42 +00:00
static int state_callback ( newmenu * menu , const d_event & event , state_userdata * const userdata )
{
2020-05-02 21:18:42 +00:00
std : : array < grs_bitmap_ptr , NUM_SAVES > & sc_bmp = userdata - > sc_bmp ;
2010-01-07 14:49:07 +00:00
newmenu_item * items = newmenu_get_items ( menu ) ;
2020-04-26 17:26:23 +00:00
unsigned citem ;
2015-01-17 18:31:42 +00:00
if ( event . type = = EVENT_NEWMENU_SELECTED )
2015-02-28 22:34:07 +00:00
userdata - > citem = static_cast < const d_select_event & > ( event ) . citem ;
2020-04-26 17:26:23 +00:00
else if ( event . type = = EVENT_NEWMENU_DRAW & & ( citem = newmenu_get_citem ( menu ) ) > 0 )
2008-02-24 14:41:27 +00:00
{
2020-07-22 03:11:18 +00:00
if ( sc_bmp [ citem - userdata - > decorative_item_count ] )
{
2015-06-13 22:42:20 +00:00
const auto & & fspacx = FSPACX ( ) ;
2015-06-13 22:42:20 +00:00
const auto & & fspacy = FSPACY ( ) ;
2016-09-24 18:06:11 +00:00
# if !DXX_USE_OGL
2015-06-13 22:42:20 +00:00
auto temp_canv = gr_create_canvas ( fspacx ( THUMBNAIL_W ) , fspacy ( THUMBNAIL_H ) ) ;
2012-05-18 08:54:41 +00:00
# else
2014-09-17 02:45:12 +00:00
auto temp_canv = gr_create_canvas ( THUMBNAIL_W * 2 , ( THUMBNAIL_H * 24 / 10 ) ) ;
2012-05-18 08:54:41 +00:00
# endif
2020-05-02 21:18:42 +00:00
const std : : array < grs_point , 3 > vertbuf { {
2014-11-21 03:34:50 +00:00
{ 0 , 0 } ,
{ 0 , 0 } ,
{ i2f ( THUMBNAIL_W * 2 ) , i2f ( THUMBNAIL_H * 24 / 10 ) }
} } ;
2017-04-30 16:25:17 +00:00
scale_bitmap ( * sc_bmp [ citem - 1 ] . get ( ) , vertbuf , 0 , temp_canv - > cv_bitmap ) ;
2016-09-24 18:06:11 +00:00
# if !DXX_USE_OGL
2016-12-29 03:27:13 +00:00
gr_bitmap ( * grd_curcanv , ( grd_curcanv - > cv_bitmap . bm_w / 2 ) - fspacx ( THUMBNAIL_W / 2 ) , items [ 0 ] . y - 3 , temp_canv - > cv_bitmap ) ;
2006-08-18 09:47:07 +00:00
# else
2016-12-29 03:27:12 +00:00
ogl_ubitmapm_cs ( * grd_curcanv , ( grd_curcanv - > cv_bitmap . bm_w / 2 ) - fspacx ( THUMBNAIL_W / 2 ) , items [ 0 ] . y - fspacy ( 3 ) , fspacx ( THUMBNAIL_W ) , fspacy ( THUMBNAIL_H ) , temp_canv - > cv_bitmap , ogl_colors : : white , F1_0 ) ;
2006-08-18 09:47:07 +00:00
# endif
2006-03-20 17:12:09 +00:00
}
2010-01-07 14:49:07 +00:00
return 1 ;
2008-02-24 14:41:27 +00:00
}
2010-01-07 14:49:07 +00:00
return 0 ;
2006-03-20 17:12:09 +00:00
}
2009-08-22 07:21:19 +00:00
#if 0
2006-03-20 17:12:09 +00:00
void rpad_string ( char * string , int max_chars )
{
int i , end_found ;
end_found = 0 ;
for ( i = 0 ; i < max_chars ; i + + ) {
if ( * string = = 0 )
end_found = 1 ;
if ( end_found )
* string = ' ' ;
string + + ;
}
* string = 0 ; // NULL terminate
}
2009-08-22 07:21:19 +00:00
# endif
2006-03-20 17:12:09 +00:00
2016-01-26 03:45:06 +00:00
namespace dsx {
2007-06-16 08:14:00 +00:00
/* Present a menu for selection of a savegame filename.
* For saving , dsc should be a pre - allocated buffer into which the new
* savegame description will be stored .
* For restoring , dsc should be NULL , in which case empty slots will not be
* selectable and savagames descriptions will not be editable .
*/
2020-04-26 17:26:23 +00:00
static d_game_unique_state : : save_slot state_get_savegame_filename ( d_game_unique_state : : savegame_file_path & fname , d_game_unique_state : : savegame_description * const dsc , const char * const caption , const blind_save entry_blind )
2006-03-20 17:12:09 +00:00
{
2020-04-26 17:26:23 +00:00
int version , nsaves ;
2020-05-02 21:18:42 +00:00
std : : array < d_game_unique_state : : savegame_file_path , NUM_SAVES > filename ;
std : : array < d_game_unique_state : : savegame_description , NUM_SAVES > desc ;
2015-01-17 18:31:42 +00:00
state_userdata userdata ;
2020-07-22 03:11:18 +00:00
constexpr auto decorative_item_count = userdata . decorative_item_count ;
std : : array < newmenu_item , NUM_SAVES + decorative_item_count > m ;
2015-01-17 18:31:42 +00:00
auto & sc_bmp = userdata . sc_bmp ;
2019-07-28 21:09:07 +00:00
char id [ 4 ] ;
2006-03-20 17:12:09 +00:00
int valid ;
nsaves = 0 ;
2020-04-19 20:47:07 +00:00
nm_set_item_text ( m [ 0 ] , " \n \n \n " ) ;
/* Always subtract 1 for the fixed text leader. Conditionally
* subtract another 1 if the call is for saving , since interactive
* saves should not access the last slot . The last slot is reserved
* for autosaves .
*/
2020-07-22 03:11:18 +00:00
const unsigned max_slots_shown = ( dsc ? m . size ( ) - 1 : m . size ( ) ) - decorative_item_count ;
2020-04-19 20:47:07 +00:00
range_for ( const unsigned i , xrange ( max_slots_shown ) )
{
state_format_savegame_filename ( filename [ i ] , i ) ;
2006-03-20 17:12:09 +00:00
valid = 0 ;
2020-07-22 03:11:18 +00:00
auto & mi = m [ i + decorative_item_count ] ;
2019-07-28 21:09:07 +00:00
if ( const auto fp = PHYSFSX_openReadBuffered ( filename [ i ] . data ( ) ) )
2015-01-17 18:31:42 +00:00
{
2006-03-20 17:12:09 +00:00
//Read id
PHYSFS_read ( fp , id , sizeof ( char ) * 4 , 1 ) ;
if ( ! memcmp ( id , dgss_id , 4 ) ) {
//Read version
PHYSFS_read ( fp , & version , sizeof ( int ) , 1 ) ;
2011-02-09 11:58:32 +00:00
// In case it's Coop, read state_game_id & callsign as well
if ( Game_mode & GM_MULTI_COOP )
{
2019-07-28 21:09:07 +00:00
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + sizeof ( PHYSFS_sint32 ) + sizeof ( char ) * CALLSIGN_LEN + 1 ) ; // skip state_game_id, callsign
2011-02-09 11:58:32 +00:00
}
2009-10-05 02:51:37 +00:00
if ( ( version > = STATE_COMPATIBLE_VERSION ) | | ( SWAPINT ( version ) > = STATE_COMPATIBLE_VERSION ) ) {
2006-03-20 17:12:09 +00:00
// Read description
2019-07-28 21:09:07 +00:00
PHYSFS_read ( fp , desc [ i ] . data ( ) , desc [ i ] . size ( ) , 1 ) ;
desc [ i ] . back ( ) = 0 ;
if ( ! dsc )
2020-07-22 03:11:18 +00:00
mi . type = NM_TYPE_MENU ;
2006-03-20 17:12:09 +00:00
// Read thumbnail
sc_bmp [ i ] = gr_create_bitmap ( THUMBNAIL_W , THUMBNAIL_H ) ;
2014-12-02 03:24:38 +00:00
PHYSFS_read ( fp , sc_bmp [ i ] - > get_bitmap_data ( ) , THUMBNAIL_W * THUMBNAIL_H , 1 ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
if ( version > = 9 ) {
2013-01-06 21:03:57 +00:00
palette_array_t pal ;
2013-01-06 21:11:53 +00:00
PHYSFS_read ( fp , & pal [ 0 ] , sizeof ( pal [ 0 ] ) , pal . size ( ) ) ;
2015-01-25 05:32:45 +00:00
gr_remap_bitmap_good ( * sc_bmp [ i ] . get ( ) , pal , - 1 , - 1 ) ;
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
nsaves + + ;
valid = 1 ;
}
}
2007-06-16 08:14:00 +00:00
}
2020-07-22 03:11:18 +00:00
mi . text = desc [ i ] . data ( ) ;
2006-03-20 17:12:09 +00:00
if ( ! valid ) {
2019-07-28 21:09:07 +00:00
strcpy ( desc [ i ] . data ( ) , TXT_EMPTY ) ;
if ( ! dsc )
mi . type = NM_TYPE_TEXT ;
2007-06-16 08:14:00 +00:00
}
2019-07-28 21:09:07 +00:00
if ( dsc )
{
mi . type = NM_TYPE_INPUT_MENU ;
mi . imenu ( ) . text_len = desc [ i ] . size ( ) - 1 ;
2006-03-20 17:12:09 +00:00
}
}
2019-07-28 21:09:07 +00:00
if ( ! dsc & & nsaves < 1 )
{
2006-03-20 17:12:09 +00:00
nm_messagebox ( NULL , 1 , " Ok " , " No saved games were found! " ) ;
2020-04-26 17:26:23 +00:00
return d_game_unique_state : : save_slot : : None ;
2006-03-20 17:12:09 +00:00
}
2020-04-26 17:26:23 +00:00
const auto quicksave_selection = GameUniqueState . quicksave_selection ;
const auto valid_selection = [ dsc ] ( d_game_unique_state : : save_slot selection ) {
return dsc
? GameUniqueState . valid_save_slot ( selection )
: GameUniqueState . valid_load_slot ( selection ) ;
} ;
const auto blind = ( entry_blind = = blind_save : : no | | ! valid_selection ( quicksave_selection ) )
/* If not a blind save, or if is a blind save and no slot picked, force to ::no */
? blind_save : : no
/* otherwise, user's choice */
: entry_blind ;
const auto choice = ( blind ! = blind_save : : no )
? quicksave_selection
: (
userdata . citem = 0 ,
2020-07-22 03:11:18 +00:00
newmenu_do2 ( nullptr , caption , max_slots_shown + decorative_item_count , m . data ( ) , state_callback , & userdata , ( GameUniqueState . valid_save_slot ( quicksave_selection ) ? static_cast < unsigned > ( quicksave_selection ) : 0 ) + decorative_item_count , nullptr ) ,
2020-04-26 17:26:23 +00:00
userdata . citem = = 0
? d_game_unique_state : : save_slot : : None
2020-07-22 03:11:18 +00:00
: static_cast < d_game_unique_state : : save_slot > ( userdata . citem - decorative_item_count )
2020-04-26 17:26:23 +00:00
) ;
if ( valid_selection ( choice ) )
2015-01-17 18:31:42 +00:00
{
2020-04-26 17:26:23 +00:00
GameUniqueState . quicksave_selection = choice ;
const auto uchoice = static_cast < std : : size_t > ( choice ) ;
fname = filename [ uchoice ] ;
2019-07-28 21:09:07 +00:00
if ( dsc )
2020-06-30 19:46:06 +00:00
{
2020-07-22 03:11:18 +00:00
auto & d = desc [ uchoice ] ;
if ( d . front ( ) )
* dsc = d ;
else
{
time_t t = time ( nullptr ) ;
if ( struct tm * ptm = ( t = = - 1 ) ? nullptr : localtime ( & t ) )
strftime ( dsc - > data ( ) , dsc - > size ( ) , " %m-%d %H:%M:%S " , ptm ) ;
else
strcpy ( dsc - > data ( ) , " -no title- " ) ;
2020-06-30 19:46:06 +00:00
}
}
2006-03-20 17:12:09 +00:00
}
2020-04-26 17:26:23 +00:00
return choice ;
2006-03-20 17:12:09 +00:00
}
2020-04-26 17:26:23 +00:00
d_game_unique_state : : save_slot state_get_save_file ( d_game_unique_state : : savegame_file_path & fname , d_game_unique_state : : savegame_description * const dsc , const blind_save blind_save )
2007-06-16 08:14:00 +00:00
{
2010-02-25 03:08:10 +00:00
return state_get_savegame_filename ( fname , dsc , " Save Game " , blind_save ) ;
2007-06-16 08:14:00 +00:00
}
2020-04-26 17:26:23 +00:00
d_game_unique_state : : save_slot state_get_restore_file ( d_game_unique_state : : savegame_file_path & fname , blind_save blind_save )
2007-06-16 08:14:00 +00:00
{
2013-12-10 17:13:32 +00:00
return state_get_savegame_filename ( fname , NULL , " Select Game to Restore " , blind_save ) ;
2007-06-16 08:14:00 +00:00
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
# elif defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
// -----------------------------------------------------------------------------------
// Imagine if C had a function to copy a file...
2012-11-11 22:12:51 +00:00
static int copy_file ( const char * old_file , const char * new_file )
2006-03-20 17:12:09 +00:00
{
int buf_size ;
2015-01-17 18:31:42 +00:00
RAIIPHYSFS_File in_file { PHYSFS_openRead ( old_file ) } ;
2015-01-17 18:31:42 +00:00
if ( ! in_file )
2006-03-20 17:12:09 +00:00
return - 2 ;
2015-01-17 18:31:42 +00:00
RAIIPHYSFS_File out_file { PHYSFS_openWrite ( new_file ) } ;
if ( ! out_file )
return - 1 ;
2006-03-20 17:12:09 +00:00
buf_size = PHYSFS_fileLength ( in_file ) ;
2015-01-28 03:42:53 +00:00
RAIIdmem < sbyte [ ] > buf ;
2013-01-20 22:26:33 +00:00
for ( ; ; ) {
if ( buf_size = = 0 )
return - 5 ; // likely to be an empty file
2015-01-28 03:42:53 +00:00
if ( MALLOC ( buf , sbyte [ ] , buf_size ) )
2013-01-20 22:26:33 +00:00
break ;
2006-03-20 17:12:09 +00:00
buf_size / = 2 ;
2013-01-20 22:26:33 +00:00
}
2006-03-20 17:12:09 +00:00
while ( ! PHYSFS_eof ( in_file ) )
{
int bytes_read ;
bytes_read = PHYSFS_read ( in_file , buf , 1 , buf_size ) ;
if ( bytes_read < 0 )
Error ( " Cannot read from file <%s>: %s " , old_file , PHYSFS_getLastError ( ) ) ;
Assert ( bytes_read = = buf_size | | PHYSFS_eof ( in_file ) ) ;
if ( PHYSFS_write ( out_file , buf , 1 , bytes_read ) < bytes_read )
Error ( " Cannot write to file <%s>: %s " , new_file , PHYSFS_getLastError ( ) ) ;
}
2015-01-17 18:31:42 +00:00
if ( ! out_file . close ( ) )
2006-03-20 17:12:09 +00:00
return - 4 ;
return 0 ;
}
2020-04-26 17:26:23 +00:00
2020-05-02 21:18:42 +00:00
static void format_secret_sgc_filename ( std : : array < char , PATH_MAX > & fname , const d_game_unique_state : : save_slot filenum )
2020-04-26 17:26:23 +00:00
{
snprintf ( fname . data ( ) , fname . size ( ) , PLAYER_DIRECTORY_STRING ( " %xsecret.sgc " ) , static_cast < unsigned > ( filenum ) ) ;
}
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
// -----------------------------------------------------------------------------------
2014-12-30 02:09:23 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2015-04-19 04:18:49 +00:00
int state_save_all ( const blind_save blind_save )
2014-12-30 02:09:23 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2015-04-19 04:18:49 +00:00
int state_save_all ( const secret_save secret , const blind_save blind_save )
2014-12-30 02:09:23 +00:00
# endif
2006-03-20 17:12:09 +00:00
{
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2017-06-25 20:46:03 +00:00
static constexpr std : : integral_constant < secret_save , secret_save : : none > secret { } ;
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2019-08-15 01:34:22 +00:00
auto & LevelUniqueControlCenterState = LevelUniqueObjectState . ControlCenterState ;
2015-04-19 04:18:49 +00:00
if ( Current_level_num < 0 & & secret = = secret_save : : none )
2015-04-02 02:36:53 +00:00
{
2013-06-23 16:27:34 +00:00
HUD_init_message_literal ( HM_DEFAULT , " Can't save in secret level! " ) ;
2006-03-20 17:12:09 +00:00
return 0 ;
}
2019-08-06 02:59:40 +00:00
if ( GameUniqueState . Final_boss_countdown_time ) //don't allow save while final boss is dying
2006-03-20 17:12:09 +00:00
return 0 ;
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
if ( Game_mode & GM_MULTI )
{
if ( Game_mode & GM_MULTI_COOP )
multi_initiate_save_game ( ) ;
return 0 ;
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
// If this is a secret save and the control center has been destroyed, don't allow
// return to the base level.
2019-08-15 01:34:22 +00:00
if ( secret ! = secret_save : : none & & LevelUniqueControlCenterState . Control_center_destroyed )
{
2006-03-20 17:12:09 +00:00
PHYSFS_delete ( SECRETB_FILENAME ) ;
return 0 ;
}
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
2019-07-28 21:09:07 +00:00
d_game_unique_state : : savegame_file_path filename_storage ;
const char * filename ;
2019-07-28 21:09:07 +00:00
d_game_unique_state : : savegame_description desc { } ;
2020-04-26 17:26:23 +00:00
d_game_unique_state : : save_slot filenum = d_game_unique_state : : save_slot : : None ;
2015-10-09 02:46:09 +00:00
{
pause_game_world_time p ;
2006-03-20 17:12:09 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2015-04-19 04:18:49 +00:00
if ( secret = = secret_save : : b ) {
2019-07-28 21:09:07 +00:00
filename = SECRETB_FILENAME ;
2015-04-19 04:18:49 +00:00
} else if ( secret = = secret_save : : c ) {
2019-07-28 21:09:07 +00:00
filename = SECRETC_FILENAME ;
2013-03-03 01:03:33 +00:00
} else
# endif
{
2020-04-26 17:26:23 +00:00
filenum = state_get_save_file ( filename_storage , & desc , blind_save ) ;
if ( ! GameUniqueState . valid_save_slot ( filenum ) )
2006-03-20 17:12:09 +00:00
return 0 ;
2019-07-28 21:09:07 +00:00
filename = filename_storage . data ( ) ;
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
// MK, 1/1/96
2007-10-31 00:49:45 +00:00
// Do special secret level stuff.
2006-03-20 17:12:09 +00:00
// If secret.sgc exists, then copy it to Nsecret.sgc (where N = filenum).
// If it doesn't exist, then delete Nsecret.sgc
2015-04-19 04:18:49 +00:00
if ( secret = = secret_save : : none & & ! ( Game_mode & GM_MULTI_COOP ) ) {
2020-04-26 17:26:23 +00:00
if ( filenum ! = d_game_unique_state : : save_slot : : None )
{
2020-05-02 21:18:42 +00:00
std : : array < char , PATH_MAX > fname ;
2020-04-26 17:26:23 +00:00
const auto temp_fname = fname . data ( ) ;
format_secret_sgc_filename ( fname , filenum ) ;
2011-06-01 07:59:55 +00:00
if ( PHYSFSX_exists ( temp_fname , 0 ) )
2006-03-20 17:12:09 +00:00
{
if ( ! PHYSFS_delete ( temp_fname ) )
Error ( " Cannot delete file <%s>: %s " , temp_fname , PHYSFS_getLastError ( ) ) ;
}
2011-06-01 07:59:55 +00:00
if ( PHYSFSX_exists ( SECRETC_FILENAME , 0 ) )
2006-03-20 17:12:09 +00:00
{
2016-05-21 17:24:50 +00:00
const int rval = copy_file ( SECRETC_FILENAME , temp_fname ) ;
2006-03-20 17:12:09 +00:00
Assert ( rval = = 0 ) ; // Oops, error copying secret.sgc to temp_fname!
2011-06-28 07:16:24 +00:00
( void ) rval ;
2006-03-20 17:12:09 +00:00
}
}
}
2013-03-03 01:03:33 +00:00
# endif
2015-10-09 02:46:09 +00:00
}
2006-03-20 17:12:09 +00:00
2019-07-28 21:09:07 +00:00
const int rval = state_save_all_sub ( filename , desc . data ( ) ) ;
2010-02-25 03:08:10 +00:00
2015-04-19 04:18:49 +00:00
if ( rval & & secret = = secret_save : : none )
2020-07-22 03:11:18 +00:00
HUD_init_message ( HM_DEFAULT , " Game saved to \" %s \" : \" %s \" " , filename , desc . data ( ) ) ;
2006-03-20 17:12:09 +00:00
return rval ;
}
2013-06-08 22:24:17 +00:00
int state_save_all_sub ( const char * filename , const char * desc )
2006-03-20 17:12:09 +00:00
{
2019-08-15 01:34:22 +00:00
auto & LevelUniqueControlCenterState = LevelUniqueObjectState . ControlCenterState ;
2019-03-03 00:31:08 +00:00
auto & Objects = LevelUniqueObjectState . Objects ;
auto & vcobjptr = Objects . vcptr ;
auto & vmobjptr = Objects . vmptr ;
2020-02-26 05:07:34 +00:00
auto & LevelUniqueMorphObjectState = LevelUniqueObjectState . MorphObjectState ;
2019-02-02 18:36:39 +00:00
auto & RobotCenters = LevelSharedRobotcenterState . RobotCenters ;
2019-02-02 18:36:39 +00:00
auto & Station = LevelUniqueFuelcenterState . Station ;
2010-12-22 00:17:59 +00:00
fix tmptime32 = 0 ;
2006-03-20 17:12:09 +00:00
2007-05-21 11:24:27 +00:00
# ifndef NDEBUG
2016-07-21 01:43:22 +00:00
if ( CGameArg . SysUsePlayersDir & & strncmp ( filename , PLAYER_DIRECTORY_TEXT , sizeof ( PLAYER_DIRECTORY_TEXT ) - 1 ) )
2006-03-20 17:12:09 +00:00
Int3 ( ) ;
# endif
2015-01-17 18:31:42 +00:00
auto fp = PHYSFSX_openWriteBuffered ( filename ) ;
2006-03-20 17:12:09 +00:00
if ( ! fp ) {
2019-01-20 05:36:55 +00:00
con_printf ( CON_URGENT , " Failed to open %s: %s " , filename , PHYSFS_getLastError ( ) ) ;
2007-10-31 00:49:45 +00:00
nm_messagebox ( NULL , 1 , TXT_OK , " Error writing savegame. \n Possibly out of disk \n space. " ) ;
2006-03-20 17:12:09 +00:00
return 0 ;
}
2015-10-09 02:46:09 +00:00
pause_game_world_time p ;
2015-08-30 12:04:59 +00:00
2006-03-20 17:12:09 +00:00
//Save id
PHYSFS_write ( fp , dgss_id , sizeof ( char ) * 4 , 1 ) ;
//Save version
2016-05-21 17:24:50 +00:00
{
const int i = STATE_VERSION ;
2006-03-20 17:12:09 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ;
2016-05-21 17:24:50 +00:00
}
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
// Save Coop state_game_id and this Player's callsign. Oh the redundancy... we have this one later on but Coop games want to read this before loading a state so for easy access save this here, too
if ( Game_mode & GM_MULTI_COOP )
{
2016-07-14 01:59:02 +00:00
PHYSFS_write ( fp , & state_game_id , sizeof ( unsigned ) , 1 ) ;
2015-07-25 23:10:46 +00:00
PHYSFS_write ( fp , & get_local_player ( ) . callsign , sizeof ( char ) * CALLSIGN_LEN + 1 , 1 ) ;
2011-02-09 11:58:32 +00:00
}
2006-03-20 17:12:09 +00:00
//Save description
2019-07-28 21:09:07 +00:00
PHYSFS_write ( fp , desc , 20 , 1 ) ;
2011-02-09 11:58:32 +00:00
2006-03-20 17:12:09 +00:00
// Save the current screen shot...
2014-09-17 02:45:12 +00:00
auto cnv = gr_create_canvas ( THUMBNAIL_W , THUMBNAIL_H ) ;
2006-03-20 17:12:09 +00:00
{
2017-04-30 16:25:17 +00:00
render_frame ( * cnv , 0 ) ;
2006-03-20 17:12:09 +00:00
2016-05-21 17:24:50 +00:00
{
2016-09-24 18:06:11 +00:00
# if DXX_USE_OGL
2015-01-28 03:42:53 +00:00
RAIIdmem < uint8_t [ ] > buf ;
MALLOC ( buf , uint8_t [ ] , THUMBNAIL_W * THUMBNAIL_H * 4 ) ;
2016-09-24 18:06:11 +00:00
# if !DXX_USE_OGLES
2015-04-04 20:58:02 +00:00
GLint gl_draw_buffer ;
2011-01-06 11:43:58 +00:00
glGetIntegerv ( GL_DRAW_BUFFER , & gl_draw_buffer ) ;
glReadBuffer ( gl_draw_buffer ) ;
# endif
2015-02-17 03:52:59 +00:00
glReadPixels ( 0 , SHEIGHT - THUMBNAIL_H , THUMBNAIL_W , THUMBNAIL_H , GL_RGBA , GL_UNSIGNED_BYTE , buf . get ( ) ) ;
2015-01-28 03:42:53 +00:00
int k ;
2006-03-20 17:12:09 +00:00
k = THUMBNAIL_H ;
2016-05-21 17:24:50 +00:00
for ( unsigned i = 0 ; i < THUMBNAIL_W * THUMBNAIL_H ; i + + )
{
2014-11-11 04:21:57 +00:00
int j ;
2006-03-20 17:12:09 +00:00
if ( ! ( j = i % THUMBNAIL_W ) )
k - - ;
2014-12-02 03:24:38 +00:00
cnv - > cv_bitmap . get_bitmap_data ( ) [ THUMBNAIL_W * k + j ] =
2011-01-06 11:43:58 +00:00
gr_find_closest_color ( buf [ 4 * i ] / 4 , buf [ 4 * i + 1 ] / 4 , buf [ 4 * i + 2 ] / 4 ) ;
2006-03-20 17:12:09 +00:00
}
# endif
2016-05-21 17:24:50 +00:00
}
2006-03-20 17:12:09 +00:00
PHYSFS_write ( fp , cnv - > cv_bitmap . bm_data , THUMBNAIL_W * THUMBNAIL_H , 1 ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2013-01-06 21:11:53 +00:00
PHYSFS_write ( fp , & gr_palette [ 0 ] , sizeof ( gr_palette [ 0 ] ) , gr_palette . size ( ) ) ;
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
}
// Save the Between levels flag...
2016-05-21 17:24:50 +00:00
{
const int i = 0 ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ;
2016-05-21 17:24:50 +00:00
}
2006-03-20 17:12:09 +00:00
// Save the mission info...
2020-01-18 21:57:39 +00:00
savegame_mission_path mission_pathname { } ;
# if defined(DXX_BUILD_DESCENT_II)
mission_pathname . original [ 1 ] = static_cast < uint8_t > ( Current_mission - > descent_version ) ;
# endif
mission_pathname . original . back ( ) = static_cast < uint8_t > ( savegame_mission_name_abi : : pathname ) ;
auto Current_mission_pathname = Current_mission - > path . c_str ( ) ;
// Current_mission_filename is not necessarily 9 bytes long so for saving we use a proper string - preventing corruptions
snprintf ( mission_pathname . full . data ( ) , mission_pathname . full . size ( ) , " %s " , Current_mission_pathname ) ;
PHYSFS_write ( fp , & mission_pathname , sizeof ( mission_pathname ) , 1 ) ;
2006-03-20 17:12:09 +00:00
//Save level info
PHYSFS_write ( fp , & Current_level_num , sizeof ( int ) , 1 ) ;
PHYSFS_write ( fp , & Next_level_num , sizeof ( int ) , 1 ) ;
//Save GameTime
2010-12-22 00:17:59 +00:00
// NOTE: GameTime now is GameTime64 with fix64 since GameTime could only last 9 hrs. To even help old Savegames, we do not increment Savegame version but rather RESET GameTime64 to 0 on every save! ALL variables based on GameTime64 now will get the current GameTime64 value substracted and saved to fix size as well.
tmptime32 = 0 ;
PHYSFS_write ( fp , & tmptime32 , sizeof ( fix ) , 1 ) ;
2006-03-20 17:12:09 +00:00
//Save player info
2010-12-22 00:17:59 +00:00
//PHYSFS_write(fp, &Players[Player_num], sizeof(player), 1);
2016-10-02 00:34:48 +00:00
const auto & plrobj = get_local_plrobj ( ) ;
auto & player_info = plrobj . ctype . player_info ;
2019-07-16 04:00:50 +00:00
state_write_player ( fp , get_local_player ( ) , relocated_player_data {
plrobj . shields ,
static_cast < int16_t > ( LevelUniqueObjectState . accumulated_robots ) ,
static_cast < int16_t > ( GameUniqueState . accumulated_robots ) ,
2019-07-16 04:00:50 +00:00
static_cast < uint16_t > ( GameUniqueState . total_hostages ) ,
2019-07-16 04:00:50 +00:00
static_cast < uint8_t > ( LevelUniqueObjectState . total_hostages )
} , player_info ) ;
2006-03-20 17:12:09 +00:00
// Save the current weapon info
2016-08-06 19:55:23 +00:00
{
2016-08-28 22:41:49 +00:00
int8_t v = static_cast < int8_t > ( static_cast < primary_weapon_index_t > ( player_info . Primary_weapon ) ) ;
2016-08-06 19:55:23 +00:00
PHYSFS_write ( fp , & v , sizeof ( int8_t ) , 1 ) ;
}
2016-10-02 00:34:43 +00:00
{
int8_t v = static_cast < int8_t > ( static_cast < secondary_weapon_index_t > ( player_info . Secondary_weapon ) ) ;
PHYSFS_write ( fp , & v , sizeof ( int8_t ) , 1 ) ;
}
2006-03-20 17:12:09 +00:00
// Save the difficulty level
2019-06-27 03:26:20 +00:00
{
const int Difficulty_level = GameUniqueState . Difficulty_level ;
2006-03-20 17:12:09 +00:00
PHYSFS_write ( fp , & Difficulty_level , sizeof ( int ) , 1 ) ;
2019-06-27 03:26:20 +00:00
}
2009-08-22 07:21:19 +00:00
2006-03-20 17:12:09 +00:00
// Save cheats enabled
2011-02-14 21:27:07 +00:00
PHYSFS_write ( fp , & cheats . enabled , sizeof ( int ) , 1 ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
PHYSFS_write ( fp , & cheats . turbo , sizeof ( int ) , 1 ) ;
# endif
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//Finish all morph objects
2017-06-10 03:31:02 +00:00
range_for ( const auto & & objp , vmobjptr )
2014-10-12 23:05:46 +00:00
{
2015-06-13 22:42:19 +00:00
if ( objp - > type ! = OBJ_NONE & & objp - > render_type = = RT_MORPH )
{
2020-02-26 05:07:34 +00:00
if ( const auto umd = find_morph_data ( LevelUniqueMorphObjectState , objp ) )
2020-02-01 22:33:31 +00:00
{
const auto md = umd - > get ( ) ;
2020-08-10 03:45:13 +00:00
md - > obj - > control_source = md - > morph_save_control_type ;
2020-08-10 03:45:13 +00:00
md - > obj - > movement_source = md - > morph_save_movement_type ;
2011-02-09 11:58:32 +00:00
md - > obj - > render_type = RT_POLYOBJ ;
md - > obj - > mtype . phys_info = md - > morph_save_phys_info ;
2020-02-01 22:33:31 +00:00
umd - > reset ( ) ;
2011-02-09 11:58:32 +00:00
} else { //maybe loaded half-morphed from disk
2015-06-13 22:42:19 +00:00
objp - > flags | = OF_SHOULD_BE_DEAD ;
objp - > render_type = RT_POLYOBJ ;
2020-08-10 03:45:13 +00:00
objp - > control_source = object : : control_type : : None ;
2020-08-10 03:45:13 +00:00
objp - > movement_source = object : : movement_type : : None ;
2006-03-20 17:12:09 +00:00
}
}
2011-02-09 11:58:32 +00:00
}
//Save object info
2016-05-21 17:24:50 +00:00
{
const int i = Highest_object_index + 1 ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ;
2016-05-21 17:24:50 +00:00
}
2011-02-09 11:58:32 +00:00
{
2016-10-13 03:14:11 +00:00
object_rw None { } ;
None . type = OBJ_NONE ;
range_for ( const auto & & objp , vcobjptr )
{
object_rw obj_rw ;
auto & obj = * objp ;
PHYSFS_write ( fp , obj . type = = OBJ_NONE ? & None : ( state_object_to_object_rw ( objp , & obj_rw ) , & obj_rw ) , sizeof ( obj_rw ) , 1 ) ;
}
2011-02-09 11:58:32 +00:00
}
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//Save wall info
2018-12-30 00:43:58 +00:00
{
auto & Walls = LevelUniqueWallSubsystemState . Walls ;
auto & vcwallptr = Walls . vcptr ;
2016-05-21 17:24:50 +00:00
{
2018-12-13 02:31:39 +00:00
const int i = Walls . get_count ( ) ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ;
2016-05-21 17:24:50 +00:00
}
2016-02-12 04:02:28 +00:00
range_for ( const auto & & w , vcwallptr )
wall_write ( fp , * w , 0x7fff ) ;
2011-02-09 11:58:32 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2011-02-09 11:58:32 +00:00
//Save exploding wall info
2018-08-12 21:08:07 +00:00
expl_wall_write ( Walls . vmptr , fp ) ;
2013-03-03 01:03:33 +00:00
# endif
2018-12-30 00:43:58 +00:00
}
2011-02-09 11:58:32 +00:00
//Save door info
2018-12-30 00:43:58 +00:00
{
auto & ActiveDoors = LevelUniqueWallSubsystemState . ActiveDoors ;
2016-05-21 17:24:50 +00:00
{
2016-12-10 17:51:08 +00:00
const int i = ActiveDoors . get_count ( ) ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ;
2016-05-21 17:24:50 +00:00
}
2018-12-30 00:43:58 +00:00
range_for ( auto & & ad , ActiveDoors . vcptr )
2014-04-27 02:39:44 +00:00
active_door_write ( fp , ad ) ;
2018-12-30 00:43:58 +00:00
}
2011-02-09 11:58:32 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2011-02-09 11:58:32 +00:00
//Save cloaking wall info
2018-12-30 00:43:58 +00:00
{
auto & CloakingWalls = LevelUniqueWallSubsystemState . CloakingWalls ;
2016-05-21 17:24:50 +00:00
{
2016-11-19 17:24:54 +00:00
const int i = CloakingWalls . get_count ( ) ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ;
2016-05-21 17:24:50 +00:00
}
2018-12-30 00:43:58 +00:00
range_for ( auto & & w , CloakingWalls . vcptr )
2014-01-12 23:00:43 +00:00
cloaking_wall_write ( w , fp ) ;
2018-12-30 00:43:58 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2011-02-09 11:58:32 +00:00
//Save trigger info
2018-12-30 00:43:58 +00:00
{
auto & Triggers = LevelUniqueWallSubsystemState . Triggers ;
2016-02-06 22:12:53 +00:00
{
unsigned num_triggers = Triggers . get_count ( ) ;
PHYSFS_write ( fp , & num_triggers , sizeof ( int ) , 1 ) ;
}
2019-12-22 05:34:08 +00:00
range_for ( const trigger & vt , Triggers . vcptr )
trigger_write ( fp , vt ) ;
2018-12-30 00:43:58 +00:00
}
2011-02-09 11:58:32 +00:00
//Save tmap info
2016-02-12 04:02:28 +00:00
range_for ( const auto & & segp , vcsegptr )
2011-02-09 11:58:32 +00:00
{
2020-07-05 23:34:33 +00:00
for ( auto & & [ ss , us ] : zip ( segp - > shared_segment : : sides , segp - > unique_segment : : sides ) ) // d_zip
2018-12-13 02:31:38 +00:00
{
2020-07-05 23:34:33 +00:00
segment_side_wall_tmap_write ( fp , ss , us ) ;
2018-12-13 02:31:38 +00:00
}
2006-03-20 17:12:09 +00:00
}
2011-02-09 11:58:32 +00:00
// Save the fuelcen info
2019-08-15 01:34:22 +00:00
{
const int Control_center_destroyed = LevelUniqueControlCenterState . Control_center_destroyed ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & Control_center_destroyed , sizeof ( int ) , 1 ) ;
2019-08-15 01:34:22 +00:00
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2019-08-15 01:34:22 +00:00
PHYSFS_write ( fp , & LevelUniqueControlCenterState . Countdown_seconds_left , sizeof ( int ) , 1 ) ;
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2019-08-15 01:34:22 +00:00
PHYSFS_write ( fp , & LevelUniqueControlCenterState . Countdown_timer , sizeof ( int ) , 1 ) ;
2013-03-03 01:03:33 +00:00
# endif
2019-03-12 04:01:07 +00:00
const unsigned Num_robot_centers = LevelSharedRobotcenterState . Num_robot_centers ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & Num_robot_centers , sizeof ( int ) , 1 ) ;
2016-02-12 04:02:28 +00:00
range_for ( auto & r , partial_const_range ( RobotCenters , Num_robot_centers ) )
2015-08-26 03:15:10 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2015-08-27 16:55:14 +00:00
matcen_info_write ( fp , r , STATE_MATCEN_VERSION ) ;
2015-08-26 03:15:10 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2014-01-12 23:00:43 +00:00
matcen_info_write ( fp , r , 0x7f ) ;
2015-08-26 03:15:10 +00:00
# endif
2013-12-23 23:57:05 +00:00
control_center_triggers_write ( & ControlCenterTriggers , fp ) ;
2019-02-02 18:36:39 +00:00
const auto Num_fuelcenters = LevelUniqueFuelcenterState . Num_fuelcenters ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & Num_fuelcenters , sizeof ( int ) , 1 ) ;
2014-10-12 23:07:12 +00:00
range_for ( auto & s , partial_range ( Station , Num_fuelcenters ) )
2013-03-03 01:03:33 +00:00
{
2014-10-12 23:07:12 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2013-03-03 01:03:33 +00:00
// NOTE: Usually Descent1 handles countdown by Timer value of the Reactor Station. Since we now use Descent2 code to handle countdown (which we do in case there IS NO Reactor Station which causes potential trouble in Multiplayer), let's find the Reactor here and store the timer in it.
2014-10-12 23:07:12 +00:00
if ( s . Type = = SEGMENT_IS_CONTROLCEN )
2019-08-15 01:34:22 +00:00
s . Timer = LevelUniqueControlCenterState . Countdown_timer ;
2013-03-03 01:03:33 +00:00
# endif
2014-01-12 23:00:43 +00:00
fuelcen_write ( fp , s ) ;
2014-10-12 23:07:12 +00:00
}
2011-02-09 11:58:32 +00:00
// Save the control cen info
2019-08-15 01:34:22 +00:00
{
const int Control_center_been_hit = LevelUniqueControlCenterState . Control_center_been_hit ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & Control_center_been_hit , sizeof ( int ) , 1 ) ;
2019-08-15 01:34:22 +00:00
}
2019-06-20 04:02:27 +00:00
{
2019-08-15 01:34:22 +00:00
const auto cc = static_cast < int > ( LevelUniqueControlCenterState . Control_center_player_been_seen ) ;
2019-06-20 04:02:27 +00:00
PHYSFS_write ( fp , & cc , sizeof ( int ) , 1 ) ;
}
2019-08-15 01:34:22 +00:00
PHYSFS_write ( fp , & LevelUniqueControlCenterState . Frametime_until_next_fire , sizeof ( int ) , 1 ) ;
2019-08-15 01:34:22 +00:00
{
const int Control_center_present = LevelUniqueControlCenterState . Control_center_present ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & Control_center_present , sizeof ( int ) , 1 ) ;
2019-08-15 01:34:22 +00:00
}
2019-08-15 01:34:22 +00:00
{
const auto Dead_controlcen_object_num = LevelUniqueControlCenterState . Dead_controlcen_object_num ;
2017-01-21 19:05:43 +00:00
int dead_controlcen_object_num = Dead_controlcen_object_num = = object_none ? - 1 : Dead_controlcen_object_num ;
2013-12-29 04:28:07 +00:00
PHYSFS_write ( fp , & dead_controlcen_object_num , sizeof ( int ) , 1 ) ;
2019-08-15 01:34:22 +00:00
}
2011-02-09 11:58:32 +00:00
// Save the AI state
ai_save_state ( fp ) ;
// Save the automap visited info
2019-08-06 02:59:40 +00:00
PHYSFS_write ( fp , LevelUniqueAutomapState . Automap_visited . data ( ) , sizeof ( uint8_t ) , std : : max < std : : size_t > ( Highest_segment_index + 1 , MAX_SEGMENTS_ORIGINAL ) ) ;
2011-02-09 11:58:32 +00:00
2016-07-14 01:59:02 +00:00
PHYSFS_write ( fp , & state_game_id , sizeof ( unsigned ) , 1 ) ;
2016-05-21 17:24:50 +00:00
{
const int i = 0 ;
2011-02-14 21:27:07 +00:00
PHYSFS_write ( fp , & cheats . rapidfire , sizeof ( int ) , 1 ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ; // was Ugly_robot_cheat
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ; // was Ugly_robot_texture
PHYSFS_write ( fp , & cheats . ghostphysics , sizeof ( int ) , 1 ) ;
# endif
2011-02-14 21:27:07 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ; // was Lunacy
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2011-02-14 21:27:07 +00:00
PHYSFS_write ( fp , & i , sizeof ( int ) , 1 ) ; // was Lunacy, too... and one was Ugly robot stuff a long time ago...
2006-03-20 17:12:09 +00:00
// Save automap marker info
2018-03-10 18:32:02 +00:00
range_for ( int m , MarkerState . imobjidx )
2017-01-21 19:05:43 +00:00
{
if ( m = = object_none )
m = - 1 ;
2014-08-05 03:10:04 +00:00
PHYSFS_write ( fp , & m , sizeof ( m ) , 1 ) ;
2017-01-21 19:05:43 +00:00
}
2014-01-01 23:29:08 +00:00
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + ( NUM_MARKERS ) * ( CALLSIGN_LEN + 1 ) ) ; // PHYSFS_write(fp, MarkerOwner, sizeof(MarkerOwner), 1); MarkerOwner is obsolete
2018-03-10 18:32:02 +00:00
range_for ( const auto & m , MarkerState . message )
2016-05-21 17:24:50 +00:00
PHYSFS_write ( fp , m . data ( ) , m . size ( ) , 1 ) ;
2006-03-20 17:12:09 +00:00
PHYSFS_write ( fp , & Afterburner_charge , sizeof ( fix ) , 1 ) ;
//save last was super information
2016-08-28 22:41:47 +00:00
{
auto & Primary_last_was_super = player_info . Primary_last_was_super ;
2020-05-02 21:18:42 +00:00
std : : array < uint8_t , MAX_PRIMARY_WEAPONS > last_was_super { } ;
2016-08-28 22:41:47 +00:00
/* Descent 2 shipped with Primary_last_was_super and
* Secondary_last_was_super each sized to contain MAX_ * _WEAPONS ,
* but only the first half of those are ever used .
* Unfortunately , the save file format is defined as saving
* MAX_ * _WEAPONS for each . Copy into a temporary , then write
* the temporary to the file .
*/
2017-02-19 19:33:44 +00:00
for ( uint_fast32_t j = primary_weapon_index_t : : VULCAN_INDEX ; j ! = primary_weapon_index_t : : SUPER_LASER_INDEX ; + + j )
{
if ( Primary_last_was_super & ( 1 < < j ) )
last_was_super [ j ] = 1 ;
}
2016-08-28 22:41:47 +00:00
PHYSFS_write ( fp , & last_was_super , MAX_PRIMARY_WEAPONS , 1 ) ;
2016-08-28 22:41:48 +00:00
auto & Secondary_last_was_super = player_info . Secondary_last_was_super ;
2017-02-19 19:33:45 +00:00
for ( uint_fast32_t j = secondary_weapon_index_t : : CONCUSSION_INDEX ; j ! = secondary_weapon_index_t : : SMISSILE1_INDEX ; + + j )
{
if ( Secondary_last_was_super & ( 1 < < j ) )
last_was_super [ j ] = 1 ;
}
2016-08-28 22:41:48 +00:00
PHYSFS_write ( fp , & last_was_super , MAX_SECONDARY_WEAPONS , 1 ) ;
2016-08-28 22:41:47 +00:00
}
2006-03-20 17:12:09 +00:00
// Save flash effect stuff
PHYSFS_write ( fp , & Flash_effect , sizeof ( int ) , 1 ) ;
2010-12-22 00:17:59 +00:00
if ( Time_flash_last_played - GameTime64 < F1_0 * ( - 18000 ) )
tmptime32 = F1_0 * ( - 18000 ) ;
else
tmptime32 = Time_flash_last_played - GameTime64 ;
PHYSFS_write ( fp , & tmptime32 , sizeof ( fix ) , 1 ) ;
2006-03-20 17:12:09 +00:00
PHYSFS_write ( fp , & PaletteRedAdd , sizeof ( int ) , 1 ) ;
PHYSFS_write ( fp , & PaletteGreenAdd , sizeof ( int ) , 1 ) ;
PHYSFS_write ( fp , & PaletteBlueAdd , sizeof ( int ) , 1 ) ;
2013-12-31 04:05:48 +00:00
{
2017-01-01 00:45:46 +00:00
union {
2020-05-02 21:18:42 +00:00
std : : array < uint8_t , MAX_SEGMENTS > light_subtracted ;
std : : array < uint8_t , MAX_SEGMENTS_ORIGINAL > light_subtracted_original ;
2017-01-01 00:45:46 +00:00
} ;
const auto & & r = make_range ( vcsegptr ) ;
2020-08-10 03:45:13 +00:00
/* For compatibility with old game versions, always write at
* least MAX_SEGMENTS_ORIGINAL entries . If the level is larger
* than that , then write as many entries as needed for the
* level .
*/
2017-01-01 00:45:46 +00:00
const unsigned count = ( Highest_segment_index + 1 > MAX_SEGMENTS_ORIGINAL )
2020-08-10 03:45:13 +00:00
/* Every written element will be filled by the loop, so
* there is no need to initialize the storage area .
*/
2017-01-01 00:45:46 +00:00
? vcsegptr . count ( )
2020-08-10 03:45:13 +00:00
/* The loop may fill fewer than MAX_SEGMENTS_ORIGINAL
* entries , but MAX_SEGMENTS_ORIGINAL entries will be
* written , so zero - initialize the elements first .
*/
2017-01-01 00:45:46 +00:00
: ( light_subtracted_original = { } , MAX_SEGMENTS_ORIGINAL ) ;
auto j = light_subtracted . begin ( ) ;
2020-08-10 03:45:13 +00:00
for ( const unique_segment & useg : r )
* j + + = useg . light_subtracted ;
2017-01-01 00:45:46 +00:00
PHYSFS_write ( fp , light_subtracted . data ( ) , sizeof ( uint8_t ) , count ) ;
2013-12-31 04:05:48 +00:00
}
2006-03-20 17:12:09 +00:00
PHYSFS_write ( fp , & First_secret_visit , sizeof ( First_secret_visit ) , 1 ) ;
2016-10-02 00:34:48 +00:00
auto & Omega_charge = player_info . Omega_charge ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & Omega_charge , sizeof ( Omega_charge ) , 1 ) ;
2013-03-03 01:03:33 +00:00
# endif
2016-05-21 17:24:50 +00:00
}
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
// Save Coop Info
if ( Game_mode & GM_MULTI_COOP )
2006-03-20 17:12:09 +00:00
{
2015-11-07 21:55:58 +00:00
/* Write local player's shields for everyone. Remote players'
* shields are ignored , and using local everywhere is cheaper
* than using it only for the one slot where it may matter .
*/
2016-10-02 00:34:48 +00:00
const auto shields = plrobj . shields ;
2019-07-16 04:00:50 +00:00
const relocated_player_data rpd { shields , 0 , 0 , 0 , 0 } ;
2016-03-19 19:08:09 +00:00
// I know, I know we only allow 4 players in coop. I screwed that up. But if we ever allow 8 players in coop, who's gonna laugh then?
range_for ( auto & i , partial_const_range ( Players , MAX_PLAYERS ) )
2006-03-20 17:12:09 +00:00
{
2019-07-16 04:00:50 +00:00
state_write_player ( fp , i , rpd , player_info ) ;
2006-03-20 17:12:09 +00:00
}
2014-12-22 04:35:48 +00:00
PHYSFS_write ( fp , Netgame . mission_title . data ( ) , Netgame . mission_title . size ( ) , 1 ) ;
2015-01-03 23:44:32 +00:00
PHYSFS_write ( fp , Netgame . mission_name . data ( ) , Netgame . mission_name . size ( ) , 1 ) ;
2011-02-09 11:58:32 +00:00
PHYSFS_write ( fp , & Netgame . levelnum , sizeof ( int ) , 1 ) ;
PHYSFS_write ( fp , & Netgame . difficulty , sizeof ( ubyte ) , 1 ) ;
PHYSFS_write ( fp , & Netgame . game_status , sizeof ( ubyte ) , 1 ) ;
PHYSFS_write ( fp , & Netgame . numplayers , sizeof ( ubyte ) , 1 ) ;
PHYSFS_write ( fp , & Netgame . max_numplayers , sizeof ( ubyte ) , 1 ) ;
PHYSFS_write ( fp , & Netgame . numconnected , sizeof ( ubyte ) , 1 ) ;
2011-04-19 23:47:15 +00:00
PHYSFS_write ( fp , & Netgame . level_time , sizeof ( int ) , 1 ) ;
2006-03-20 17:12:09 +00:00
}
return 1 ;
}
// -----------------------------------------------------------------------------------
// Set the player's position from the globals Secret_return_segment and Secret_return_orient.
2012-11-11 00:14:30 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
void set_pos_from_return_segment ( void )
{
2020-05-17 23:35:25 +00:00
auto & LevelSharedVertexState = LevelSharedSegmentState . get_vertex_state ( ) ;
2019-03-03 00:31:08 +00:00
auto & Objects = LevelUniqueObjectState . Objects ;
2020-05-17 23:35:25 +00:00
auto & Vertices = LevelSharedVertexState . get_vertices ( ) ;
2019-03-03 00:31:08 +00:00
auto & vmobjptr = Objects . vmptr ;
auto & vmobjptridx = Objects . vmptridx ;
2017-06-10 03:31:02 +00:00
const auto & & plobjnum = vmobjptridx ( get_local_player ( ) . objnum ) ;
2019-08-06 02:59:41 +00:00
const auto & & segp = vmsegptridx ( LevelSharedSegmentState . Secret_return_segment ) ;
2018-12-30 00:43:57 +00:00
auto & vcvertptr = Vertices . vcptr ;
2018-03-12 03:43:47 +00:00
compute_segment_center ( vcvertptr , plobjnum - > pos , segp ) ;
2018-03-12 03:43:46 +00:00
obj_relink ( vmobjptr , vmsegptr , plobjnum , segp ) ;
2006-03-20 17:12:09 +00:00
reset_player_object ( ) ;
2019-08-06 02:59:41 +00:00
plobjnum - > orient = LevelSharedSegmentState . Secret_return_orient ;
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
// -----------------------------------------------------------------------------------
2014-12-30 02:09:23 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2015-04-19 04:18:49 +00:00
int state_restore_all ( const int in_game , std : : nullptr_t , const blind_save blind )
2014-12-30 02:09:23 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2015-04-19 04:18:49 +00:00
int state_restore_all ( const int in_game , const secret_restore secret , const char * const filename_override , const blind_save blind )
2014-12-30 02:09:23 +00:00
# endif
2006-03-20 17:12:09 +00:00
{
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2017-06-25 20:46:03 +00:00
static constexpr std : : integral_constant < secret_restore , secret_restore : : none > secret { } ;
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2015-04-19 04:18:49 +00:00
if ( in_game & & Current_level_num < 0 & & secret = = secret_restore : : none )
2015-04-02 02:36:53 +00:00
{
2013-06-23 16:27:34 +00:00
HUD_init_message_literal ( HM_DEFAULT , " Can't restore in secret level! " ) ;
2006-03-20 17:12:09 +00:00
return 0 ;
}
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
if ( Newdemo_state = = ND_STATE_RECORDING )
newdemo_stop_recording ( ) ;
if ( Newdemo_state ! = ND_STATE_NORMAL )
return 0 ;
2011-02-09 11:58:32 +00:00
if ( Game_mode & GM_MULTI )
{
if ( Game_mode & GM_MULTI_COOP )
multi_initiate_restore_game ( ) ;
return 0 ;
}
2019-07-28 21:09:07 +00:00
d_game_unique_state : : savegame_file_path filename_storage ;
const char * filename ;
2020-04-26 17:26:23 +00:00
d_game_unique_state : : save_slot filenum = d_game_unique_state : : save_slot : : None ;
2015-10-09 02:46:09 +00:00
{
pause_game_world_time p ;
2006-03-20 17:12:09 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
if ( filename_override ) {
2019-07-28 21:09:07 +00:00
filename = filename_override ;
2020-04-26 17:26:23 +00:00
filenum = d_game_unique_state : : save_slot : : secret_save_filename_override ; // place outside of save slots
2013-03-03 01:03:33 +00:00
} else
# endif
2015-04-02 02:36:53 +00:00
{
2020-04-26 17:26:23 +00:00
filenum = state_get_restore_file ( filename_storage , blind ) ;
if ( ! GameUniqueState . valid_load_slot ( filenum ) )
2019-07-28 21:09:07 +00:00
{
return 0 ;
}
filename = filename_storage . data ( ) ;
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
// MK, 1/1/96
2007-10-31 00:49:45 +00:00
// Do special secret level stuff.
2006-03-20 17:12:09 +00:00
// If Nsecret.sgc (where N = filenum) exists, then copy it to secret.sgc.
// If it doesn't exist, then delete secret.sgc
2015-04-19 04:18:49 +00:00
if ( secret = = secret_restore : : none )
2015-04-02 02:36:53 +00:00
{
2006-03-20 17:12:09 +00:00
int rval ;
2020-04-26 17:26:23 +00:00
if ( filenum ! = d_game_unique_state : : save_slot : : None )
{
2020-05-02 21:18:42 +00:00
std : : array < char , PATH_MAX > fname ;
2020-04-26 17:26:23 +00:00
const auto temp_fname = fname . data ( ) ;
format_secret_sgc_filename ( fname , filenum ) ;
2011-06-01 07:59:55 +00:00
if ( PHYSFSX_exists ( temp_fname , 0 ) )
2006-03-20 17:12:09 +00:00
{
rval = copy_file ( temp_fname , SECRETC_FILENAME ) ;
Assert ( rval = = 0 ) ; // Oops, error copying temp_fname to secret.sgc!
2011-06-28 07:16:24 +00:00
( void ) rval ;
2006-03-20 17:12:09 +00:00
} else
PHYSFS_delete ( SECRETC_FILENAME ) ;
}
}
2013-03-03 01:03:33 +00:00
# endif
2015-04-19 04:18:49 +00:00
if ( secret = = secret_restore : : none & & in_game & & blind = = blind_save : : no )
2015-04-02 02:36:53 +00:00
{
2006-03-20 17:12:09 +00:00
int choice ;
choice = nm_messagebox ( NULL , 2 , " Yes " , " No " , " Restore Game? " ) ;
if ( choice ! = 0 ) {
return 0 ;
}
}
2015-10-09 02:46:09 +00:00
}
2018-12-30 00:43:57 +00:00
return state_restore_all_sub (
# if defined(DXX_BUILD_DESCENT_II)
2018-12-30 00:43:58 +00:00
LevelSharedSegmentState . DestructibleLights , secret ,
2018-12-30 00:43:57 +00:00
# endif
filename ) ;
2006-03-20 17:12:09 +00:00
}
2015-04-02 02:36:53 +00:00
# if defined(DXX_BUILD_DESCENT_I)
int state_restore_all_sub ( const char * filename )
# elif defined(DXX_BUILD_DESCENT_II)
2018-12-30 00:43:58 +00:00
int state_restore_all_sub ( const d_level_shared_destructible_light_state & LevelSharedDestructibleLightState , const secret_restore secret , const char * const filename )
2015-04-02 02:36:53 +00:00
# endif
2006-03-20 17:12:09 +00:00
{
2019-08-15 01:34:22 +00:00
auto & LevelUniqueControlCenterState = LevelUniqueObjectState . ControlCenterState ;
2019-03-03 00:31:08 +00:00
auto & Objects = LevelUniqueObjectState . Objects ;
auto & vmobjptr = Objects . vmptr ;
auto & vmobjptridx = Objects . vmptridx ;
2019-02-02 18:36:39 +00:00
auto & RobotCenters = LevelSharedRobotcenterState . RobotCenters ;
2019-02-02 18:36:39 +00:00
auto & Station = LevelUniqueFuelcenterState . Station ;
2017-02-19 19:33:44 +00:00
int version , coop_player_got [ MAX_PLAYERS ] , coop_org_objnum = get_local_player ( ) . objnum ;
2009-10-05 02:51:37 +00:00
int swap = 0 ; // if file is not endian native, have to swap all shorts and ints
2011-06-28 07:16:24 +00:00
int current_level ;
2006-03-20 17:12:09 +00:00
char id [ 5 ] ;
2010-12-22 00:17:59 +00:00
fix tmptime32 = 0 ;
2020-08-24 01:31:28 +00:00
std : : array < std : : array < short , MAX_SIDES_PER_SEGMENT > , MAX_SEGMENTS > TempTmapNum ;
std : : array < std : : array < texture2_value , MAX_SIDES_PER_SEGMENT > , MAX_SEGMENTS > TempTmapNum2 ;
2006-03-20 17:12:09 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2017-06-25 20:46:03 +00:00
static constexpr std : : integral_constant < secret_restore , secret_restore : : none > secret { } ;
2013-06-04 03:30:50 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2018-12-30 00:43:59 +00:00
auto & Robot_info = LevelSharedRobotInfoState . Robot_info ;
2013-06-04 03:30:50 +00:00
fix64 old_gametime = GameTime64 ;
2013-03-03 01:03:33 +00:00
# endif
2007-05-21 11:24:27 +00:00
# ifndef NDEBUG
2016-07-21 01:43:22 +00:00
if ( CGameArg . SysUsePlayersDir & & strncmp ( filename , PLAYER_DIRECTORY_TEXT , sizeof ( PLAYER_DIRECTORY_TEXT ) - 1 ) )
2006-03-20 17:12:09 +00:00
Int3 ( ) ;
# endif
2015-01-17 18:31:42 +00:00
auto fp = PHYSFSX_openReadBuffered ( filename ) ;
2006-03-20 17:12:09 +00:00
if ( ! fp ) return 0 ;
//Read id
PHYSFS_read ( fp , id , sizeof ( char ) * 4 , 1 ) ;
if ( memcmp ( id , dgss_id , 4 ) ) {
return 0 ;
}
//Read version
2009-10-05 02:51:37 +00:00
//Check for swapped file here, as dgss_id is written as a string (i.e. endian independent)
2006-03-20 17:12:09 +00:00
PHYSFS_read ( fp , & version , sizeof ( int ) , 1 ) ;
2009-10-05 02:51:37 +00:00
if ( version & 0xffff0000 )
{
swap = 1 ;
version = SWAPINT ( version ) ;
}
2006-03-20 17:12:09 +00:00
if ( version < STATE_COMPATIBLE_VERSION ) {
return 0 ;
}
2011-02-09 11:58:32 +00:00
// Read Coop state_game_id. Oh the redundancy... we have this one later on but Coop games want to read this before loading a state so for easy access we have this here
if ( Game_mode & GM_MULTI_COOP )
{
2014-07-05 16:48:12 +00:00
callsign_t saved_callsign ;
2011-02-09 11:58:32 +00:00
state_game_id = PHYSFSX_readSXE32 ( fp , swap ) ;
2011-04-19 23:47:15 +00:00
PHYSFS_read ( fp , & saved_callsign , sizeof ( char ) * CALLSIGN_LEN + 1 , 1 ) ;
2015-07-25 23:10:46 +00:00
if ( ! ( saved_callsign = = get_local_player ( ) . callsign ) ) // check the callsign of the palyer who saved this state. It MUST match. If we transferred this savegame from pilot A to pilot B, others won't be able to restore us. So bail out here if this is the case.
2011-02-09 11:58:32 +00:00
{
return 0 ;
}
}
2006-03-20 17:12:09 +00:00
// Read description
2019-07-28 21:09:07 +00:00
d_game_unique_state : : savegame_description desc ;
PHYSFS_read ( fp , desc . data ( ) , 20 , 1 ) ;
desc . back ( ) = 0 ;
2006-03-20 17:12:09 +00:00
// Skip the current screen shot...
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + THUMBNAIL_W * THUMBNAIL_H ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
// And now...skip the goddamn palette stuff that somebody forgot to add
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + 768 ) ;
2013-03-03 01:03:33 +00:00
# endif
2006-03-20 17:12:09 +00:00
// Read the Between levels flag...
2016-05-21 17:24:50 +00:00
PHYSFSX_readSXE32 ( fp , swap ) ;
2006-03-20 17:12:09 +00:00
// Read the mission info...
2020-01-18 21:57:39 +00:00
savegame_mission_path mission_pathname { } ;
PHYSFS_read ( fp , mission_pathname . original . data ( ) , mission_pathname . original . size ( ) , 1 ) ;
mission_name_type name_match_mode ;
mission_entry_predicate mission_predicate ;
switch ( static_cast < savegame_mission_name_abi > ( mission_pathname . original . back ( ) ) )
{
case savegame_mission_name_abi : : original : /* Save game without the ability to do extended mission names */
name_match_mode = mission_name_type : : basename ;
mission_predicate . filesystem_name = mission_pathname . original . data ( ) ;
# if defined(DXX_BUILD_DESCENT_II)
mission_predicate . check_version = false ;
# endif
break ;
case savegame_mission_name_abi : : pathname : /* Save game with extended mission name */
{
PHYSFS_read ( fp , mission_pathname . full . data ( ) , mission_pathname . full . size ( ) , 1 ) ;
if ( mission_pathname . full . back ( ) )
{
nm_messagebox ( " ERROR " , 1 , TXT_OK , " Unable to load game \n Unrecognized mission name format " ) ;
return 0 ;
}
}
name_match_mode = mission_name_type : : pathname ;
mission_predicate . filesystem_name = mission_pathname . full . data ( ) ;
# if defined(DXX_BUILD_DESCENT_II)
mission_predicate . check_version = true ;
mission_predicate . descent_version = static_cast < Mission : : descent_version_type > ( mission_pathname . original [ 1 ] ) ;
# endif
break ;
default : /* Save game written by a future version of Rebirth. ABI unknown. */
nm_messagebox ( " ERROR " , 1 , TXT_OK , " Unable to load game \n Unrecognized save game format " ) ;
return 0 ;
}
2006-03-20 17:12:09 +00:00
2020-01-18 21:57:39 +00:00
if ( const auto errstr = load_mission_by_name ( mission_predicate , name_match_mode ) )
2016-11-13 09:21:45 +00:00
{
2020-01-18 21:57:39 +00:00
nm_messagebox ( " ERROR " , 1 , TXT_OK , " Unable to load mission \n '%s' \n \n %s " , mission_pathname . full . data ( ) , errstr ) ;
2006-03-20 17:12:09 +00:00
return 0 ;
}
//Read level info
2009-10-05 02:51:37 +00:00
current_level = PHYSFSX_readSXE32 ( fp , swap ) ;
2011-06-28 07:16:24 +00:00
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + sizeof ( PHYSFS_sint32 ) ) ; // skip Next_level_num
2006-03-20 17:12:09 +00:00
//Restore GameTime
2010-12-22 00:17:59 +00:00
tmptime32 = PHYSFSX_readSXE32 ( fp , swap ) ;
2016-07-07 03:08:13 +00:00
GameTime64 = static_cast < fix64 > ( tmptime32 ) ;
2006-03-20 17:12:09 +00:00
// Start new game....
2014-07-05 16:48:12 +00:00
callsign_t org_callsign ;
2019-07-16 04:00:50 +00:00
LevelUniqueObjectState . accumulated_robots = 0 ;
2019-07-16 04:00:50 +00:00
LevelUniqueObjectState . total_hostages = 0 ;
2019-07-16 04:00:50 +00:00
GameUniqueState . accumulated_robots = 0 ;
2019-07-16 04:00:50 +00:00
GameUniqueState . total_hostages = 0 ;
2011-02-09 11:58:32 +00:00
if ( ! ( Game_mode & GM_MULTI_COOP ) )
{
Game_mode = GM_NORMAL ;
change_playernum_to ( 0 ) ;
N_players = 1 ;
2019-08-29 02:40:32 +00:00
auto & callsign = vmplayerptr ( 0u ) - > callsign ;
callsign = InterfaceUniqueState . PilotName ;
org_callsign = callsign ;
2015-04-19 04:18:49 +00:00
if ( secret = = secret_restore : : none )
2015-04-02 02:36:53 +00:00
{
2011-02-09 11:58:32 +00:00
InitPlayerObject ( ) ; //make sure player's object set up
2012-04-15 13:32:48 +00:00
init_player_stats_game ( 0 ) ; //clear all stats
2011-02-09 11:58:32 +00:00
}
}
else // in coop we want to stay the player we are already.
{
2015-07-25 23:10:46 +00:00
org_callsign = get_local_player ( ) . callsign ;
2015-04-19 04:18:49 +00:00
if ( secret = = secret_restore : : none )
2012-04-15 13:32:48 +00:00
init_player_stats_game ( Player_num ) ;
2006-03-20 17:12:09 +00:00
}
2010-07-30 17:59:21 +00:00
if ( Game_wind )
2020-08-28 00:18:45 +00:00
window_set_visible ( * Game_wind , 0 ) ;
2010-07-30 17:59:21 +00:00
2006-03-20 17:12:09 +00:00
//Read player info
2016-12-21 03:54:18 +00:00
{
2015-11-07 21:55:58 +00:00
player_info pl_info ;
2019-07-16 04:00:50 +00:00
relocated_player_data rpd ;
2017-01-24 10:18:01 +00:00
# if defined(DXX_BUILD_DESCENT_II)
player_info ret_pl_info ;
Remember player's `hostages_on_board` across secret level return
Returning from a secret level clears the player's `hostages_on_board` to
0 as part of initializing a "new" ship, but this is not desirable
behavior. Copy the counter out before and back afterward.
```
Thread 1 "d2x-rebirth-edi" hit Hardware watchpoint 1: -location d2x::LevelUniqueObjectState.Objects._M_elems[0].ctype.player_info.mission.hostages_on_board
Old value = 5 '\005'
New value = 0 '\000'
#0 d2x::init_player_stats_ship (GameTime64=<optimized out>, plrobj=...) at similar/main/gameseq.cpp:613
#1 d2x::init_player_stats_level (secret_flag=d2x::secret_restore::survived, plrobj=..., plr=...) at similar/main/gameseq.cpp:615
#2 d2x::StartNewLevelSub (level_num=level_num@entry=3, page_in_textures=page_in_textures@entry=1, secret_flag=secret_flag@entry=d2x::secret_restore::survived) at similar/main/gameseq.cpp:1901
#3 d2x::state_restore_all_sub (LevelSharedDestructibleLightState=..., secret=d2x::secret_restore::survived, filename=<optimized out>) at similar/main/state.cpp:1670
#4 d2x::state_restore_all (in_game=in_game@entry=1, secret=secret@entry=d2x::secret_restore::survived, filename_override=<optimized out>, blind=blind@entry=dcx::blind_save::no) at similar/main/state.cpp:1490
#5 d2x::ExitSecretLevel ()
```
Reported-by: teratorn <https://github.com/dxx-rebirth/dxx-rebirth/issues/495>
2020-02-01 22:33:31 +00:00
ret_pl_info . mission . hostages_on_board = get_local_plrobj ( ) . ctype . player_info . mission . hostages_on_board ;
2017-01-24 10:18:01 +00:00
# endif
2006-03-20 17:12:09 +00:00
{
2016-11-15 08:18:56 +00:00
# if DXX_USE_EDITOR
// Don't bother with the other game sequence stuff if loading saved game in editor
if ( EditorWindow )
LoadLevel ( current_level , 1 ) ;
else
# endif
StartNewLevelSub ( current_level , 1 , secret ) ;
2006-03-20 17:12:09 +00:00
2017-08-13 20:38:31 +00:00
auto & plr = get_local_player ( ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2017-02-06 02:00:49 +00:00
auto & plrobj = get_local_plrobj ( ) ;
2015-04-19 04:18:49 +00:00
if ( secret ! = secret_restore : : none ) {
2006-03-20 17:12:09 +00:00
player dummy_player ;
2019-07-16 04:00:50 +00:00
state_read_player ( fp , dummy_player , swap , pl_info , rpd ) ;
2015-04-19 04:18:49 +00:00
if ( secret = = secret_restore : : survived ) { // This means he didn't die, so he keeps what he got in the secret level.
Remember player's `hostages_on_board` across secret level return
Returning from a secret level clears the player's `hostages_on_board` to
0 as part of initializing a "new" ship, but this is not desirable
behavior. Copy the counter out before and back afterward.
```
Thread 1 "d2x-rebirth-edi" hit Hardware watchpoint 1: -location d2x::LevelUniqueObjectState.Objects._M_elems[0].ctype.player_info.mission.hostages_on_board
Old value = 5 '\005'
New value = 0 '\000'
#0 d2x::init_player_stats_ship (GameTime64=<optimized out>, plrobj=...) at similar/main/gameseq.cpp:613
#1 d2x::init_player_stats_level (secret_flag=d2x::secret_restore::survived, plrobj=..., plr=...) at similar/main/gameseq.cpp:615
#2 d2x::StartNewLevelSub (level_num=level_num@entry=3, page_in_textures=page_in_textures@entry=1, secret_flag=secret_flag@entry=d2x::secret_restore::survived) at similar/main/gameseq.cpp:1901
#3 d2x::state_restore_all_sub (LevelSharedDestructibleLightState=..., secret=d2x::secret_restore::survived, filename=<optimized out>) at similar/main/state.cpp:1670
#4 d2x::state_restore_all (in_game=in_game@entry=1, secret=secret@entry=d2x::secret_restore::survived, filename_override=<optimized out>, blind=blind@entry=dcx::blind_save::no) at similar/main/state.cpp:1490
#5 d2x::ExitSecretLevel ()
```
Reported-by: teratorn <https://github.com/dxx-rebirth/dxx-rebirth/issues/495>
2020-02-01 22:33:31 +00:00
const auto hostages_on_board = ret_pl_info . mission . hostages_on_board ;
2017-01-24 10:18:01 +00:00
ret_pl_info = plrobj . ctype . player_info ;
Remember player's `hostages_on_board` across secret level return
Returning from a secret level clears the player's `hostages_on_board` to
0 as part of initializing a "new" ship, but this is not desirable
behavior. Copy the counter out before and back afterward.
```
Thread 1 "d2x-rebirth-edi" hit Hardware watchpoint 1: -location d2x::LevelUniqueObjectState.Objects._M_elems[0].ctype.player_info.mission.hostages_on_board
Old value = 5 '\005'
New value = 0 '\000'
#0 d2x::init_player_stats_ship (GameTime64=<optimized out>, plrobj=...) at similar/main/gameseq.cpp:613
#1 d2x::init_player_stats_level (secret_flag=d2x::secret_restore::survived, plrobj=..., plr=...) at similar/main/gameseq.cpp:615
#2 d2x::StartNewLevelSub (level_num=level_num@entry=3, page_in_textures=page_in_textures@entry=1, secret_flag=secret_flag@entry=d2x::secret_restore::survived) at similar/main/gameseq.cpp:1901
#3 d2x::state_restore_all_sub (LevelSharedDestructibleLightState=..., secret=d2x::secret_restore::survived, filename=<optimized out>) at similar/main/state.cpp:1670
#4 d2x::state_restore_all (in_game=in_game@entry=1, secret=secret@entry=d2x::secret_restore::survived, filename_override=<optimized out>, blind=blind@entry=dcx::blind_save::no) at similar/main/state.cpp:1490
#5 d2x::ExitSecretLevel ()
```
Reported-by: teratorn <https://github.com/dxx-rebirth/dxx-rebirth/issues/495>
2020-02-01 22:33:31 +00:00
ret_pl_info . mission . hostages_on_board = hostages_on_board ;
2019-07-16 04:00:50 +00:00
rpd . shields = plrobj . shields ;
2017-08-13 20:38:31 +00:00
plr . level = dummy_player . level ;
plr . time_level = dummy_player . time_level ;
2015-07-25 23:10:46 +00:00
2017-01-24 10:18:01 +00:00
ret_pl_info . homing_object_dist = - 1 ;
2017-08-13 20:38:31 +00:00
plr . hours_level = dummy_player . hours_level ;
plr . hours_total = dummy_player . hours_total ;
2017-01-24 10:18:01 +00:00
do_cloak_invul_secret_stuff ( old_gametime , ret_pl_info ) ;
2006-03-20 17:12:09 +00:00
} else {
2017-08-13 20:38:31 +00:00
plr = dummy_player ;
2017-01-24 10:19:50 +00:00
// Keep keys even if they died on secret level (otherwise game becomes impossible)
// Example: Cameron 'Stryker' Fultz's Area 51
pl_info . powerup_flags | = ( plrobj . ctype . player_info . powerup_flags &
( PLAYER_FLAGS_BLUE_KEY |
PLAYER_FLAGS_RED_KEY |
PLAYER_FLAGS_GOLD_KEY ) ) ;
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
} else
# endif
{
2019-07-16 04:00:50 +00:00
state_read_player ( fp , plr , swap , pl_info , rpd ) ;
2006-03-20 17:12:09 +00:00
}
2019-07-16 04:00:50 +00:00
LevelUniqueObjectState . accumulated_robots = rpd . num_robots_level ;
2019-07-16 04:00:50 +00:00
LevelUniqueObjectState . total_hostages = rpd . hostages_level ;
2019-07-16 04:00:50 +00:00
GameUniqueState . accumulated_robots = rpd . num_robots_total ;
2019-07-16 04:00:50 +00:00
GameUniqueState . total_hostages = rpd . hostages_total ;
2006-03-20 17:12:09 +00:00
}
2017-08-13 20:38:31 +00:00
{
auto & plr = get_local_player ( ) ;
plr . callsign = org_callsign ;
2012-05-09 23:40:39 +00:00
if ( Game_mode & GM_MULTI_COOP )
2017-08-13 20:38:31 +00:00
plr . objnum = coop_org_objnum ;
}
2006-03-20 17:12:09 +00:00
2016-10-02 00:34:48 +00:00
auto & Primary_weapon = pl_info . Primary_weapon ;
2006-03-20 17:12:09 +00:00
// Restore the weapon states
2016-08-06 19:55:23 +00:00
{
int8_t v ;
PHYSFS_read ( fp , & v , sizeof ( int8_t ) , 1 ) ;
Primary_weapon = static_cast < primary_weapon_index_t > ( v ) ;
}
2016-10-02 00:34:48 +00:00
auto & Secondary_weapon = pl_info . Secondary_weapon ;
2016-10-02 00:34:43 +00:00
{
int8_t v ;
PHYSFS_read ( fp , & v , sizeof ( int8_t ) , 1 ) ;
Secondary_weapon = static_cast < secondary_weapon_index_t > ( v ) ;
}
2006-03-20 17:12:09 +00:00
2016-10-02 00:34:45 +00:00
select_primary_weapon ( pl_info , nullptr , Primary_weapon , 0 ) ;
2016-10-02 00:34:46 +00:00
select_secondary_weapon ( pl_info , nullptr , Secondary_weapon , 0 ) ;
2006-03-20 17:12:09 +00:00
// Restore the difficulty level
2018-05-12 18:24:19 +00:00
{
const unsigned u = PHYSFSX_readSXE32 ( fp , swap ) ;
2019-06-27 03:26:20 +00:00
GameUniqueState . Difficulty_level = cast_clamp_difficulty ( u ) ;
2018-05-12 18:24:19 +00:00
}
2006-03-20 17:12:09 +00:00
// Restore the cheats enabled flag
2012-03-28 08:07:16 +00:00
game_disable_cheats ( ) ; // disable cheats first
2011-02-14 21:27:07 +00:00
cheats . enabled = PHYSFSX_readSXE32 ( fp , swap ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
cheats . turbo = PHYSFSX_readSXE32 ( fp , swap ) ;
# endif
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
Do_appearance_effect = 0 ; // Don't do this for middle o' game stuff.
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//Clear out all the objects from the lvl file
2020-08-10 03:45:13 +00:00
for ( unique_segment & useg : vmsegptr )
useg . objects = object_none ;
2018-12-30 00:43:57 +00:00
reset_objects ( LevelUniqueObjectState , 1 ) ;
2011-02-09 11:58:32 +00:00
//Read objects, and pop 'em into their respective segments.
2016-05-21 17:24:50 +00:00
{
const int i = PHYSFSX_readSXE32 ( fp , swap ) ;
2016-01-09 16:38:12 +00:00
Objects . set_count ( i ) ;
2016-05-21 17:24:50 +00:00
}
2017-06-10 03:31:02 +00:00
range_for ( const auto & & objp , vmobjptr )
2011-02-09 11:58:32 +00:00
{
2013-12-08 17:13:17 +00:00
object_rw obj_rw ;
PHYSFS_read ( fp , & obj_rw , sizeof ( obj_rw ) , 1 ) ;
object_rw_swap ( & obj_rw , swap ) ;
2015-06-13 22:42:19 +00:00
state_object_rw_to_object ( & obj_rw , objp ) ;
2011-02-09 11:58:32 +00:00
}
2017-06-10 03:31:02 +00:00
range_for ( const auto & & obj , vmobjptridx )
2014-10-12 23:05:46 +00:00
{
2011-02-09 11:58:32 +00:00
obj - > rtype . pobj_info . alt_textures = - 1 ;
if ( obj - > type ! = OBJ_NONE ) {
2016-11-06 17:12:03 +00:00
const auto segnum = obj - > segnum ;
2018-03-12 03:43:46 +00:00
obj_link_unchecked ( Objects . vmptr , obj , Segments . vmptridx ( segnum ) ) ;
2010-12-22 00:17:59 +00:00
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2011-02-09 11:58:32 +00:00
//look for, and fix, boss with bogus shields
2013-10-07 23:52:33 +00:00
if ( obj - > type = = OBJ_ROBOT & & Robot_info [ get_robot_id ( obj ) ] . boss_flag ) {
2011-02-09 11:58:32 +00:00
fix save_shields = obj - > shields ;
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
copy_defaults_to_robot ( obj ) ; //calculate starting shields
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//if in valid range, use loaded shield value
if ( save_shields > 0 & & save_shields < = obj - > shields )
obj - > shields = save_shields ;
else
obj - > shields / = 2 ; //give player a break
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2011-02-09 11:58:32 +00:00
}
2018-12-30 00:43:57 +00:00
special_reset_objects ( LevelUniqueObjectState ) ;
2017-02-06 02:00:49 +00:00
/* Reload plrobj reference. The player's object number may have
* been changed by the state_object_rw_to_object call .
*/
auto & plrobj = get_local_plrobj ( ) ;
2019-07-16 04:00:50 +00:00
plrobj . shields = rpd . shields ;
2017-01-24 10:18:01 +00:00
# if defined(DXX_BUILD_DESCENT_II)
if ( secret = = secret_restore : : survived )
{ // This means he didn't die, so he keeps what he got in the secret level.
ret_pl_info . mission . last_score = pl_info . mission . last_score ;
plrobj . ctype . player_info = ret_pl_info ;
}
else
# endif
plrobj . ctype . player_info = pl_info ;
2016-12-21 03:54:18 +00:00
}
2017-02-06 02:00:49 +00:00
/* Reload plrobj reference. This is unnecessary for correctness,
* but is required by scoping rules , since the previous correct copy
* goes out of scope when pl_info goes out of scope , and that needs
* to be removed from scope to avoid a shadow warning when
* cooperative players are loaded below .
*/
auto & plrobj = get_local_plrobj ( ) ;
2011-02-09 11:58:32 +00:00
// 1 = Didn't die on secret level.
// 2 = Died on secret level.
2015-04-19 04:18:49 +00:00
if ( secret ! = secret_restore : : none & & ( Current_level_num > = 0 ) ) {
2011-02-09 11:58:32 +00:00
set_pos_from_return_segment ( ) ;
2015-04-02 02:36:53 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2015-04-19 04:18:49 +00:00
if ( secret = = secret_restore : : died )
2012-04-15 13:32:48 +00:00
init_player_stats_new_ship ( Player_num ) ;
2015-04-02 02:36:53 +00:00
# endif
2011-02-09 11:58:32 +00:00
}
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//Restore wall info
2018-08-12 21:08:07 +00:00
init_exploding_walls ( ) ;
2018-12-30 00:43:58 +00:00
{
auto & Walls = LevelUniqueWallSubsystemState . Walls ;
2016-02-06 22:12:54 +00:00
Walls . set_count ( PHYSFSX_readSXE32 ( fp , swap ) ) ;
2018-12-30 00:43:58 +00:00
range_for ( const auto & & w , Walls . vmptr )
2016-02-12 04:02:28 +00:00
wall_read ( fp , * w ) ;
2006-03-20 17:12:09 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2011-02-09 11:58:32 +00:00
//now that we have the walls, check if any sounds are linked to
//walls that are now open
2018-12-30 00:43:58 +00:00
range_for ( const auto & & wp , Walls . vcptr )
2014-04-27 23:12:34 +00:00
{
2016-02-12 04:02:28 +00:00
auto & w = * wp ;
2014-04-27 23:12:34 +00:00
if ( w . type = = WALL_OPEN )
digi_kill_sound_linked_to_segment ( w . segnum , w . sidenum , - 1 ) ; //-1 means kill any sound
2011-02-09 11:58:32 +00:00
}
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//Restore exploding wall info
if ( version > = 10 ) {
2015-02-28 19:36:01 +00:00
unsigned i = PHYSFSX_readSXE32 ( fp , swap ) ;
2018-08-12 21:08:07 +00:00
expl_wall_read_n_swap ( Walls . vmptr , fp , swap , i ) ;
2011-02-09 11:58:32 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2018-12-30 00:43:58 +00:00
}
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//Restore door info
2018-12-30 00:43:58 +00:00
{
auto & ActiveDoors = LevelUniqueWallSubsystemState . ActiveDoors ;
2016-12-10 17:51:08 +00:00
ActiveDoors . set_count ( PHYSFSX_readSXE32 ( fp , swap ) ) ;
2018-12-30 00:43:58 +00:00
range_for ( auto & & ad , ActiveDoors . vmptr )
2014-04-27 02:39:44 +00:00
active_door_read ( fp , ad ) ;
2018-12-30 00:43:58 +00:00
}
2006-03-20 17:12:09 +00:00
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2011-02-09 11:58:32 +00:00
if ( version > = 14 ) { //Restore cloaking wall info
2016-11-19 17:24:54 +00:00
unsigned num_cloaking_walls = PHYSFSX_readSXE32 ( fp , swap ) ;
2018-12-30 00:43:58 +00:00
auto & CloakingWalls = LevelUniqueWallSubsystemState . CloakingWalls ;
2016-11-19 17:24:54 +00:00
CloakingWalls . set_count ( num_cloaking_walls ) ;
2018-12-30 00:43:58 +00:00
range_for ( auto & & w , CloakingWalls . vmptr )
2014-04-27 18:30:07 +00:00
cloaking_wall_read ( w , fp ) ;
2011-02-09 11:58:32 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2011-02-09 11:58:32 +00:00
//Restore trigger info
2018-12-30 00:43:58 +00:00
{
auto & Triggers = LevelUniqueWallSubsystemState . Triggers ;
2016-02-06 22:12:53 +00:00
Triggers . set_count ( PHYSFSX_readSXE32 ( fp , swap ) ) ;
2019-12-22 05:34:08 +00:00
range_for ( trigger & t , Triggers . vmptr )
trigger_read ( fp , t ) ;
2018-12-30 00:43:58 +00:00
}
2006-03-20 17:12:09 +00:00
2011-02-09 11:58:32 +00:00
//Restore tmap info (to temp values so we can use compiled-in tmap info to compute static_light
2017-06-10 03:31:02 +00:00
range_for ( const auto & & segp , vmsegptridx )
2014-10-12 23:10:05 +00:00
{
2019-05-04 18:27:36 +00:00
range_for ( const unsigned j , xrange ( 6u ) )
2017-02-19 19:33:44 +00:00
{
2018-12-13 02:31:38 +00:00
segp - > shared_segment : : sides [ j ] . wall_num = PHYSFSX_readSXE16 ( fp , swap ) ;
2015-12-22 04:18:51 +00:00
TempTmapNum [ segp ] [ j ] = PHYSFSX_readSXE16 ( fp , swap ) ;
2020-08-24 01:31:28 +00:00
TempTmapNum2 [ segp ] [ j ] = static_cast < texture2_value > ( PHYSFSX_readSXE16 ( fp , swap ) ) ;
2011-02-09 11:58:32 +00:00
}
2006-03-20 17:12:09 +00:00
}
2011-02-09 11:58:32 +00:00
//Restore the fuelcen info
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Control_center_destroyed = PHYSFSX_readSXE32 ( fp , swap ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Countdown_seconds_left = PHYSFSX_readSXE32 ( fp , swap ) ;
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Countdown_timer = 0 ;
2013-03-03 01:03:33 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Countdown_timer = PHYSFSX_readSXE32 ( fp , swap ) ;
2013-03-03 01:03:33 +00:00
# endif
2019-03-12 04:01:07 +00:00
const unsigned Num_robot_centers = PHYSFSX_readSXE32 ( fp , swap ) ;
LevelSharedRobotcenterState . Num_robot_centers = Num_robot_centers ;
2014-01-18 18:02:02 +00:00
range_for ( auto & r , partial_range ( RobotCenters , Num_robot_centers ) )
2014-01-12 23:00:43 +00:00
# if defined(DXX_BUILD_DESCENT_I)
2015-08-27 16:55:14 +00:00
matcen_info_read ( fp , r , STATE_MATCEN_VERSION ) ;
2014-01-12 23:00:43 +00:00
# elif defined(DXX_BUILD_DESCENT_II)
matcen_info_read ( fp , r ) ;
# endif
2015-09-13 21:02:19 +00:00
control_center_triggers_read ( & ControlCenterTriggers , fp ) ;
2019-02-02 18:36:39 +00:00
const unsigned Num_fuelcenters = PHYSFSX_readSXE32 ( fp , swap ) ;
LevelUniqueFuelcenterState . Num_fuelcenters = Num_fuelcenters ;
2014-01-18 18:02:02 +00:00
range_for ( auto & s , partial_range ( Station , Num_fuelcenters ) )
2017-07-08 18:17:49 +00:00
{
2014-01-12 23:00:43 +00:00
fuelcen_read ( fp , s ) ;
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
// NOTE: Usually Descent1 handles countdown by Timer value of the Reactor Station. Since we now use Descent2 code to handle countdown (which we do in case there IS NO Reactor Station which causes potential trouble in Multiplayer), let's find the Reactor here and read the timer from it.
2017-07-08 18:17:49 +00:00
if ( s . Type = = SEGMENT_IS_CONTROLCEN )
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Countdown_timer = s . Timer ;
2013-03-03 01:03:33 +00:00
# endif
2017-07-08 18:17:49 +00:00
}
2011-02-09 11:58:32 +00:00
// Restore the control cen info
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Control_center_been_hit = PHYSFSX_readSXE32 ( fp , swap ) ;
2019-06-20 04:02:27 +00:00
{
const int cc = PHYSFSX_readSXE32 ( fp , swap ) ;
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Control_center_player_been_seen = static_cast < player_visibility_state > ( cc ) ;
2019-06-20 04:02:27 +00:00
}
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Frametime_until_next_fire = PHYSFSX_readSXE32 ( fp , swap ) ;
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Control_center_present = PHYSFSX_readSXE32 ( fp , swap ) ;
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Dead_controlcen_object_num = PHYSFSX_readSXE32 ( fp , swap ) ;
2019-08-15 01:34:22 +00:00
if ( LevelUniqueControlCenterState . Control_center_destroyed )
2019-08-15 01:34:22 +00:00
LevelUniqueControlCenterState . Total_countdown_time = LevelUniqueControlCenterState . Countdown_timer / F0_5 ; // we do not need to know this, but it should not be 0 either...
2011-02-09 11:58:32 +00:00
// Restore the AI state
ai_restore_state ( fp , version , swap ) ;
2011-05-04 13:07:57 +00:00
{
2019-08-06 02:59:40 +00:00
auto & Automap_visited = LevelUniqueAutomapState . Automap_visited ;
// Restore the automap visited info
2015-09-29 02:41:22 +00:00
Automap_visited = { } ;
2019-08-06 02:59:40 +00:00
DXX_MAKE_MEM_UNDEFINED ( Automap_visited . begin ( ) , Automap_visited . end ( ) ) ;
PHYSFS_read ( fp , Automap_visited . data ( ) , sizeof ( uint8_t ) , std : : max < std : : size_t > ( Highest_segment_index + 1 , MAX_SEGMENTS_ORIGINAL ) ) ;
2011-05-04 13:07:57 +00:00
}
2011-02-09 11:58:32 +00:00
2016-12-21 03:54:18 +00:00
{
2011-02-09 11:58:32 +00:00
// Restore hacked up weapon system stuff.
2016-10-02 00:34:48 +00:00
auto & player_info = plrobj . ctype . player_info ;
2016-12-21 03:54:18 +00:00
/* These values were never saved, so coerce them to a sane default.
*/
player_info . Fusion_charge = 0 ;
player_info . Player_eggs_dropped = false ;
player_info . FakingInvul = false ;
player_info . lavafall_hiss_playing = false ;
player_info . missile_gun = 0 ;
2019-05-04 18:27:36 +00:00
player_info . Spreadfire_toggle = 0 ;
# if defined(DXX_BUILD_DESCENT_II)
player_info . Helix_orientation = 0 ;
# endif
2016-12-21 03:54:18 +00:00
player_info . Last_bumped_local_player = 0 ;
2016-10-02 00:34:48 +00:00
auto & Next_laser_fire_time = player_info . Next_laser_fire_time ;
auto & Next_missile_fire_time = player_info . Next_missile_fire_time ;
2016-12-10 17:51:09 +00:00
player_info . Auto_fire_fusion_cannon_time = 0 ;
2016-12-17 18:39:18 +00:00
player_info . Next_flare_fire_time = GameTime64 ;
2011-02-09 11:58:32 +00:00
Next_laser_fire_time = GameTime64 ;
Next_missile_fire_time = GameTime64 ;
2006-03-20 17:12:09 +00:00
state_game_id = 0 ;
if ( version > = 7 ) {
2009-10-05 02:51:37 +00:00
state_game_id = PHYSFSX_readSXE32 ( fp , swap ) ;
2011-02-14 21:27:07 +00:00
cheats . rapidfire = PHYSFSX_readSXE32 ( fp , swap ) ;
2011-06-28 07:16:24 +00:00
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + sizeof ( PHYSFS_sint32 ) ) ; // PHYSFSX_readSXE32(fp, swap); // was Lunacy
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + sizeof ( PHYSFS_sint32 ) ) ; // PHYSFSX_readSXE32(fp, swap); // was Lunacy, too... and one was Ugly robot stuff a long time ago...
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_I)
cheats . ghostphysics = PHYSFSX_readSXE32 ( fp , swap ) ;
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + sizeof ( PHYSFS_sint32 ) ) ; // PHYSFSX_readSXE32(fp, swap);
# endif
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# if defined(DXX_BUILD_DESCENT_II)
2006-03-20 17:12:09 +00:00
if ( version > = 17 ) {
2018-03-10 18:32:02 +00:00
range_for ( auto & i , MarkerState . imobjidx )
2016-03-19 19:08:09 +00:00
i = PHYSFSX_readSXE32 ( fp , swap ) ;
2012-06-10 09:26:29 +00:00
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + ( NUM_MARKERS ) * ( CALLSIGN_LEN + 1 ) ) ; // PHYSFS_read(fp, MarkerOwner, sizeof(MarkerOwner), 1); // skip obsolete MarkerOwner
2018-03-10 18:32:02 +00:00
range_for ( auto & i , MarkerState . message )
2014-12-22 04:35:48 +00:00
{
2020-05-02 21:18:42 +00:00
std : : array < char , MARKER_MESSAGE_LEN > a ;
2014-12-22 04:35:48 +00:00
PHYSFS_read ( fp , a . data ( ) , a . size ( ) , 1 ) ;
i . copy_if ( a ) ;
}
2006-03-20 17:12:09 +00:00
}
else {
2011-06-28 07:16:24 +00:00
int num ;
2006-03-20 17:12:09 +00:00
// skip dummy info
2009-10-05 02:51:37 +00:00
num = PHYSFSX_readSXE32 ( fp , swap ) ; // was NumOfMarkers
2011-06-28 07:16:24 +00:00
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + sizeof ( PHYSFS_sint32 ) ) ; // PHYSFSX_readSXE32(fp, swap); // was CurMarker
2006-03-20 17:12:09 +00:00
PHYSFS_seek ( fp , PHYSFS_tell ( fp ) + num * ( sizeof ( vms_vector ) + 40 ) ) ;
2018-03-10 18:32:02 +00:00
range_for ( auto & i , MarkerState . imobjidx )
2016-03-19 19:08:09 +00:00
i = object_none ;
2006-03-20 17:12:09 +00:00
}
if ( version > = 11 ) {
2015-04-19 04:18:49 +00:00
if ( secret ! = secret_restore : : survived )
2009-10-05 02:51:37 +00:00
Afterburner_charge = PHYSFSX_readSXE32 ( fp , swap ) ;
2006-03-20 17:12:09 +00:00
else {
2009-10-05 02:51:37 +00:00
PHYSFSX_readSXE32 ( fp , swap ) ;
2006-03-20 17:12:09 +00:00
}
}
if ( version > = 12 ) {
//read last was super information
2016-08-28 22:41:47 +00:00
auto & Primary_last_was_super = player_info . Primary_last_was_super ;
2020-05-02 21:18:42 +00:00
std : : array < uint8_t , MAX_PRIMARY_WEAPONS > last_was_super ;
2016-08-28 22:41:47 +00:00
/* Descent 2 shipped with Primary_last_was_super and
* Secondary_last_was_super each sized to contain MAX_ * _WEAPONS ,
* but only the first half of those are ever used .
* Unfortunately , the save file format is defined as saving
* MAX_ * _WEAPONS for each . Read into a temporary , then copy the
* meaningful elements to the live data .
*/
PHYSFS_read ( fp , & last_was_super , MAX_PRIMARY_WEAPONS , 1 ) ;
2017-02-19 19:33:44 +00:00
Primary_last_was_super = 0 ;
for ( uint_fast32_t j = primary_weapon_index_t : : VULCAN_INDEX ; j ! = primary_weapon_index_t : : SUPER_LASER_INDEX ; + + j )
{
if ( last_was_super [ j ] )
Primary_last_was_super | = 1 < < j ;
}
2016-08-28 22:41:48 +00:00
PHYSFS_read ( fp , & last_was_super , MAX_SECONDARY_WEAPONS , 1 ) ;
auto & Secondary_last_was_super = player_info . Secondary_last_was_super ;
2017-02-19 19:33:45 +00:00
Secondary_last_was_super = 0 ;
for ( uint_fast32_t j = secondary_weapon_index_t : : CONCUSSION_INDEX ; j ! = secondary_weapon_index_t : : SMISSILE1_INDEX ; + + j )
{
if ( last_was_super [ j ] )
Secondary_last_was_super | = 1 < < j ;
}
2006-03-20 17:12:09 +00:00
}
if ( version > = 12 ) {
2009-10-05 02:51:37 +00:00
Flash_effect = PHYSFSX_readSXE32 ( fp , swap ) ;
2010-12-22 00:17:59 +00:00
tmptime32 = PHYSFSX_readSXE32 ( fp , swap ) ;
2016-07-07 03:08:13 +00:00
Time_flash_last_played = static_cast < fix64 > ( tmptime32 ) ;
2009-10-05 02:51:37 +00:00
PaletteRedAdd = PHYSFSX_readSXE32 ( fp , swap ) ;
PaletteGreenAdd = PHYSFSX_readSXE32 ( fp , swap ) ;
PaletteBlueAdd = PHYSFSX_readSXE32 ( fp , swap ) ;
2006-03-20 17:12:09 +00:00
} else {
Flash_effect = 0 ;
Time_flash_last_played = 0 ;
PaletteRedAdd = 0 ;
PaletteGreenAdd = 0 ;
PaletteBlueAdd = 0 ;
}
// Load Light_subtracted
if ( version > = 16 ) {
2011-05-04 13:07:57 +00:00
if ( Highest_segment_index + 1 > MAX_SEGMENTS_ORIGINAL )
{
2020-08-10 03:45:13 +00:00
for ( unique_segment & useg : vmsegptr )
2015-06-13 22:42:19 +00:00
{
2020-08-10 03:45:13 +00:00
PHYSFS_read ( fp , & useg . light_subtracted , sizeof ( useg . light_subtracted ) , 1 ) ;
2015-06-13 22:42:19 +00:00
}
2011-05-04 13:07:57 +00:00
}
else
2013-12-31 04:05:48 +00:00
{
2019-12-22 05:34:08 +00:00
range_for ( unique_segment & i , partial_range ( Segments , MAX_SEGMENTS_ORIGINAL ) )
2015-06-13 22:42:19 +00:00
{
2016-03-19 19:08:09 +00:00
PHYSFS_read ( fp , & i . light_subtracted , sizeof ( i . light_subtracted ) , 1 ) ;
2015-06-13 22:42:19 +00:00
}
2013-12-31 04:05:48 +00:00
}
2018-12-30 00:43:58 +00:00
apply_all_changed_light ( LevelSharedDestructibleLightState , Segments . vmptridx ) ;
2006-03-20 17:12:09 +00:00
} else {
2020-08-10 03:45:13 +00:00
for ( unique_segment & useg : vmsegptr )
useg . light_subtracted = 0 ;
2006-03-20 17:12:09 +00:00
}
2015-04-19 04:18:49 +00:00
if ( secret = = secret_restore : : none )
2015-04-02 02:36:53 +00:00
{
2006-03-20 17:12:09 +00:00
if ( version > = 20 ) {
2009-10-05 02:51:37 +00:00
First_secret_visit = PHYSFSX_readSXE32 ( fp , swap ) ;
2006-03-20 17:12:09 +00:00
} else
First_secret_visit = 1 ;
} else
First_secret_visit = 0 ;
2017-01-24 10:22:04 +00:00
if ( secret ! = secret_restore : : survived )
{
player_info . Omega_charge = 0 ;
/* The savegame does not record this, so pick a value. Be
* nice to the player : let the cannon recharge immediately .
*/
player_info . Omega_recharge_delay = 0 ;
}
2006-03-20 17:12:09 +00:00
if ( version > = 22 )
{
2016-11-10 04:22:18 +00:00
auto i = PHYSFSX_readSXE32 ( fp , swap ) ;
2015-04-19 04:18:49 +00:00
if ( secret ! = secret_restore : : survived )
2016-11-12 18:10:08 +00:00
{
2016-11-10 04:22:18 +00:00
player_info . Omega_charge = i ;
2016-11-12 18:10:08 +00:00
}
2006-03-20 17:12:09 +00:00
}
2013-03-03 01:03:33 +00:00
# endif
2016-12-21 03:54:18 +00:00
}
2006-03-20 17:12:09 +00:00
2013-06-04 03:30:50 +00:00
// static_light should now be computed - now actually set tmap info
2017-06-10 03:31:02 +00:00
range_for ( const auto & & segp , vmsegptridx )
2014-10-12 23:10:05 +00:00
{
2019-05-04 18:27:36 +00:00
range_for ( const unsigned j , xrange ( 6u ) )
2017-02-19 19:33:44 +00:00
{
2018-12-13 02:31:38 +00:00
auto & uside = segp - > unique_segment : : sides [ j ] ;
uside . tmap_num = TempTmapNum [ segp ] [ j ] ;
uside . tmap_num2 = TempTmapNum2 [ segp ] [ j ] ;
2013-06-04 03:30:50 +00:00
}
}
2011-02-09 11:58:32 +00:00
// Read Coop Info
if ( Game_mode & GM_MULTI_COOP )
{
player restore_players [ MAX_PLAYERS ] ;
2012-05-09 23:40:39 +00:00
object restore_objects [ MAX_PLAYERS ] ;
2011-06-28 07:16:24 +00:00
int coop_got_nplayers = 0 ;
2011-02-09 11:58:32 +00:00
2014-09-21 22:10:12 +00:00
for ( playernum_t i = 0 ; i < MAX_PLAYERS ; i + + )
2011-02-09 11:58:32 +00:00
{
// prepare arrays for mapping our players below
coop_player_got [ i ] = 0 ;
// read the stored players
2016-12-21 03:54:18 +00:00
player_info pl_info ;
2019-07-16 04:00:50 +00:00
relocated_player_data rpd ;
2019-07-16 04:00:50 +00:00
/* No need to reload num_robots_level again. It was already
* restored above when the local player was restored .
*/
2019-07-16 04:00:50 +00:00
state_read_player ( fp , restore_players [ i ] , swap , pl_info , rpd ) ;
2012-05-09 23:40:39 +00:00
// make all (previous) player objects to ghosts but store them first for later remapping
2017-06-10 03:31:02 +00:00
const auto & & obj = vmobjptr ( restore_players [ i ] . objnum ) ;
2011-09-23 17:48:48 +00:00
if ( restore_players [ i ] . connected = = CONNECT_PLAYING & & obj - > type = = OBJ_PLAYER )
{
2015-11-07 21:55:58 +00:00
obj - > ctype . player_info = pl_info ;
2019-07-16 04:00:50 +00:00
obj - > shields = rpd . shields ;
2013-12-12 21:53:23 +00:00
restore_objects [ i ] = * obj ;
2011-09-23 17:48:48 +00:00
obj - > type = OBJ_GHOST ;
multi_reset_player_object ( obj ) ;
}
2011-02-09 11:58:32 +00:00
}
2014-09-21 22:10:12 +00:00
for ( playernum_t i = 0 ; i < MAX_PLAYERS ; i + + ) // copy restored players to the current slots
2011-02-09 11:58:32 +00:00
{
2017-02-19 19:33:44 +00:00
for ( unsigned j = 0 ; j < MAX_PLAYERS ; j + + )
2011-02-09 11:58:32 +00:00
{
// map stored players to current players depending on their unique (which we made sure) callsign
2017-08-13 20:38:32 +00:00
if ( vcplayerptr ( i ) - > connected = = CONNECT_PLAYING & & restore_players [ j ] . connected = = CONNECT_PLAYING & & vcplayerptr ( i ) - > callsign = = restore_players [ j ] . callsign )
2011-02-09 11:58:32 +00:00
{
2017-08-13 20:38:32 +00:00
auto & p = * vmplayerptr ( i ) ;
const auto sav_objnum = p . objnum ;
p = restore_players [ j ] ;
p . objnum = sav_objnum ;
2011-02-09 11:58:32 +00:00
coop_player_got [ i ] = 1 ;
coop_got_nplayers + + ;
2012-05-09 23:40:39 +00:00
2017-08-13 20:38:32 +00:00
const auto & & obj = vmobjptridx ( vcplayerptr ( i ) - > objnum ) ;
2012-05-09 23:40:39 +00:00
// since a player always uses the same object, we just have to copy the saved object properties to the existing one. i hate you...
2020-08-10 03:45:13 +00:00
obj - > control_source = restore_objects [ j ] . control_source ;
2020-08-10 03:45:13 +00:00
obj - > movement_source = restore_objects [ j ] . movement_source ;
2012-05-09 23:40:39 +00:00
obj - > render_type = restore_objects [ j ] . render_type ;
obj - > flags = restore_objects [ j ] . flags ;
2012-05-21 08:33:53 +00:00
obj - > pos = restore_objects [ j ] . pos ;
obj - > orient = restore_objects [ j ] . orient ;
obj - > size = restore_objects [ j ] . size ;
obj - > shields = restore_objects [ j ] . shields ;
obj - > lifeleft = restore_objects [ j ] . lifeleft ;
2012-05-09 23:40:39 +00:00
obj - > mtype . phys_info = restore_objects [ j ] . mtype . phys_info ;
obj - > rtype . pobj_info = restore_objects [ j ] . rtype . pobj_info ;
2011-02-09 11:58:32 +00:00
// make this restored player object an actual player again
obj - > type = OBJ_PLAYER ;
2018-01-29 01:56:40 +00:00
set_player_id ( obj , i ) ; // assign player object id to player number
2011-02-09 11:58:32 +00:00
multi_reset_player_object ( obj ) ;
2018-10-08 03:58:48 +00:00
update_object_seg ( vmobjptr , LevelSharedSegmentState , LevelUniqueSegmentState , obj ) ;
2011-02-09 11:58:32 +00:00
}
}
}
2014-12-22 04:35:48 +00:00
{
2020-05-02 21:18:42 +00:00
std : : array < char , MISSION_NAME_LEN + 1 > a ;
2014-12-22 04:35:48 +00:00
PHYSFS_read ( fp , a . data ( ) , a . size ( ) , 1 ) ;
Netgame . mission_title . copy_if ( a ) ;
}
2015-01-03 23:44:32 +00:00
{
2020-05-02 21:18:42 +00:00
std : : array < char , 9 > a ;
2015-01-03 23:44:32 +00:00
PHYSFS_read ( fp , a . data ( ) , a . size ( ) , 1 ) ;
Netgame . mission_name . copy_if ( a ) ;
}
2011-02-09 11:58:32 +00:00
Netgame . levelnum = PHYSFSX_readSXE32 ( fp , swap ) ;
PHYSFS_read ( fp , & Netgame . difficulty , sizeof ( ubyte ) , 1 ) ;
PHYSFS_read ( fp , & Netgame . game_status , sizeof ( ubyte ) , 1 ) ;
PHYSFS_read ( fp , & Netgame . numplayers , sizeof ( ubyte ) , 1 ) ;
PHYSFS_read ( fp , & Netgame . max_numplayers , sizeof ( ubyte ) , 1 ) ;
PHYSFS_read ( fp , & Netgame . numconnected , sizeof ( ubyte ) , 1 ) ;
Netgame . level_time = PHYSFSX_readSXE32 ( fp , swap ) ;
2014-09-21 22:10:12 +00:00
for ( playernum_t i = 0 ; i < MAX_PLAYERS ; i + + )
2011-04-19 23:47:15 +00:00
{
2017-08-13 20:38:32 +00:00
const auto & & objp = vmobjptr ( vcplayerptr ( i ) - > objnum ) ;
2016-10-15 00:53:19 +00:00
auto & pi = objp - > ctype . player_info ;
2016-11-12 18:10:07 +00:00
Netgame . killed [ i ] = pi . net_killed_total ;
2016-10-15 00:53:19 +00:00
Netgame . player_score [ i ] = pi . mission . score ;
Netgame . net_player_flags [ i ] = pi . powerup_flags ;
2011-04-19 23:47:15 +00:00
}
2014-09-21 22:10:12 +00:00
for ( playernum_t i = 0 ; i < MAX_PLAYERS ; i + + ) // Disconnect connected players not available in this Savegame
2017-08-13 20:38:32 +00:00
if ( ! coop_player_got [ i ] & & vcplayerptr ( i ) - > connected = = CONNECT_PLAYING )
2011-02-09 11:58:32 +00:00
multi_disconnect_player ( i ) ;
2015-07-25 23:10:45 +00:00
Viewer = ConsoleObject = & get_local_plrobj ( ) ; // make sure Viewer and ConsoleObject are set up (which we skipped by not using InitPlayerObject but we need since objects changed while loading)
2018-12-30 00:43:57 +00:00
special_reset_objects ( LevelUniqueObjectState ) ; // since we juggled around with objects to remap coop players rebuild the index of free objects
2020-06-10 02:25:33 +00:00
state_set_next_autosave ( GameUniqueState , Netgame . MPGameplayOptions . AutosaveInterval ) ;
2011-02-09 11:58:32 +00:00
}
2020-06-10 02:25:33 +00:00
else
state_set_next_autosave ( GameUniqueState , PlayerCfg . SPGameplayOptions . AutosaveInterval ) ;
2010-07-30 17:59:21 +00:00
if ( Game_wind )
2020-08-28 00:18:45 +00:00
if ( ! window_is_visible ( * Game_wind ) )
window_set_visible ( * Game_wind , 1 ) ;
2010-07-30 17:59:21 +00:00
reset_time ( ) ;
2006-03-20 17:12:09 +00:00
return 1 ;
}
2011-02-09 11:58:32 +00:00
2020-04-19 20:47:07 +00:00
deny_save_result deny_save_game ( fvcobjptr & vcobjptr , const d_level_unique_control_center_state & LevelUniqueControlCenterState )
{
if ( LevelUniqueControlCenterState . Control_center_destroyed )
return deny_save_result : : denied ;
if ( Game_mode & GM_MULTI )
{
if ( ! ( Game_mode & GM_MULTI_COOP ) )
/* Deathmatch games can never be saved */
return deny_save_result : : denied ;
if ( multi_common_deny_save_game ( vcobjptr , partial_const_range ( Players , N_players ) ) )
return deny_save_result : : denied ;
}
return deny_save_result : : allowed ;
}
2020-05-17 23:35:25 +00:00
}
namespace dcx {
2019-07-28 21:09:07 +00:00
int state_get_game_id ( const d_game_unique_state : : savegame_file_path & filename )
2011-02-09 11:58:32 +00:00
{
int version ;
int swap = 0 ; // if file is not endian native, have to swap all shorts and ints
2014-10-10 11:20:48 +00:00
char id [ 5 ] ;
callsign_t saved_callsign ;
2011-02-09 11:58:32 +00:00
# ifndef NDEBUG
2019-07-28 21:09:07 +00:00
if ( CGameArg . SysUsePlayersDir & & strncmp ( filename . data ( ) , PLAYER_DIRECTORY_TEXT , sizeof ( PLAYER_DIRECTORY_TEXT ) - 1 ) )
2011-02-09 11:58:32 +00:00
Int3 ( ) ;
# endif
if ( ! ( Game_mode & GM_MULTI_COOP ) )
return 0 ;
2019-07-28 21:09:07 +00:00
auto fp = PHYSFSX_openReadBuffered ( filename . data ( ) ) ;
2011-02-09 11:58:32 +00:00
if ( ! fp ) return 0 ;
//Read id
PHYSFS_read ( fp , id , sizeof ( char ) * 4 , 1 ) ;
if ( memcmp ( id , dgss_id , 4 ) ) {
return 0 ;
}
//Read version
//Check for swapped file here, as dgss_id is written as a string (i.e. endian independent)
PHYSFS_read ( fp , & version , sizeof ( int ) , 1 ) ;
if ( version & 0xffff0000 )
{
swap = 1 ;
version = SWAPINT ( version ) ;
}
if ( version < STATE_COMPATIBLE_VERSION ) {
return 0 ;
}
// Read Coop state_game_id to validate the savegame we are about to load matches the others
state_game_id = PHYSFSX_readSXE32 ( fp , swap ) ;
2011-04-19 23:47:15 +00:00
PHYSFS_read ( fp , & saved_callsign , sizeof ( char ) * CALLSIGN_LEN + 1 , 1 ) ;
2015-07-25 23:10:46 +00:00
if ( ! ( saved_callsign = = get_local_player ( ) . callsign ) ) // check the callsign of the palyer who saved this state. It MUST match. If we transferred this savegame from pilot A to pilot B, others won't be able to restore us. So bail out here if this is the case.
2011-02-09 11:58:32 +00:00
return 0 ;
return state_game_id ;
}
2019-08-06 02:59:40 +00:00
}