/*****************************************************************************
 * $Id: game_pony_config.cc,v 1.22 2006/08/03 02:17:21 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Configuration, load/state state.
 * @remarks	Saving float values with double precision is necessary.
 *			Even a large unsigned 32-bit value cannot be exactly held
 *			in a normal float without round-off loss.
 *//*
 * LEGAL:	COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#define GAME_PONY_CONFIG_CC 1
#include "game_pony_common.hh"
#include "game_base_config.hh"
#include "game_pony_config.hh"
#include "game_base_craft.hh"
#include "game_pony_gui.hh"
#include "gfx_config_file.hh"
#include "gfx_stream.hh"

string cfgkey_gui_zoom			= "gui_zoom";
string cfgkey_fog_density		= "fog_density";
string cfgkey_response_chase	= "response_chase";
string cfgkey_prefix_player1	= "p1_";
string cfgkey_prefix_player2	= "p2_";

/*****************************************************************************
 * Get ConfigFile object.
 *****************************************************************************/
ConfigFile&
GamePony::GetConfigFile( void )
{
    return GetConfigFileBase( CONFIG_FILE_APPNAME_STR );
}

/*****************************************************************************
 * Save state to configuration file.
 *****************************************************************************/
bool
GamePony::SaveState( bool saveView )
{
	ConfigFile& config = GetConfigFile();

	// Chain to base game.
	if ( ! Parent::SaveStateBase( saveView, config ) ) return false;

	// - - - - save non-view state - - - - - - - - - - - - - - - - -

	config.Write( cfgkey_game_module,		string(GAME_MODULE_NAME_STR) );
	config.Write( cfgkey_gui_zoom,			(double)gGui.GetZoom() );
	config.Write( cfgkey_fog_density,		(double)gEng.GetFogDensity() );
	config.Write( cfgkey_response_chase,	(int)mControlResponseChase );
	if ( ! saveView ) return true;

	// - - - - only save view state follows - - - - - - - - - - - - -

	ConfigSaveDyna( config, cfgkey_prefix_player1, *mPlayer1Craft, (double)mPlayer1Craft->mChasePlane->GetDistance() );
	ConfigSaveDyna( config, cfgkey_prefix_player2, *mPlayer2Craft, (double)mPlayer2Craft->mChasePlane->GetDistance() );

	return true;
}

/*****************************************************************************
 * Load state from configuration file.
 *****************************************************************************/
bool
GamePony::LoadState( void )
{
	// Read() will normally fail if the configuration
	// is revised but an older configuration file is read.
	if ( mCmdarg.mNoReload ) return false;
	ConfigFile& config = GetConfigFile();
	if ( config.IfEmpty() ) return false;
	const bool loadView = mCmdarg.mReloadView;

	// Chain to base game.
	if ( ! Parent::LoadStateBase( loadView, config ) ) return false;

	int i; double d; string s;

	if ( ! mCmdarg.mGuiZoom )
	{
		if ( config.Read( cfgkey_gui_zoom, d ) )
			gGui.SetZoom( (float)d ); // safe even though Gui not up
	}
	if ( ! mCmdarg.mFogDensity )
	{
		if ( config.Read( cfgkey_fog_density, d ) )
		{
			gEng.SetFogDensity( d ); mDefaultFogDensity = d;
		}
	}

	if ( config.Read( cfgkey_response_chase, i ) )
	{
		mPlayer1ConfigState.SetControlResponse( i );
		mPlayer2ConfigState.SetControlResponse( i );
	}

	// - - - - - - all the rest reloads the saved view - - - - - - -

	if ( ! loadView ) return true;

	// Deferred loading: Load eye/player configuration into an interim struct
	// because the Player objects (Game::mPlayer) haven't been created yet.
	ConfigLoadDyna( config, cfgkey_prefix_player1, mPlayer1ConfigState );
	ConfigLoadDyna( config, cfgkey_prefix_player2, mPlayer2ConfigState );

	return true;
}
