/*****************************************************************************
 * $Id: mod_pony_config.cc,v 1.1 2006/11/24 21:56:58 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 MOD_PONY_CONFIG_CC 1
#include "mod_pony_common.hh"
#include "mod_base_config.hh"
#include "mod_pony_config.hh"
#include "mod_base_craft.hh"
#include "mod_pony_gui.hh"
#include "gfx_config_file.hh"
#include "gfx_stream.hh"

// -- update extern string in header too --
string cfgkey_gui_zoom			= "gui_zoom";
string cfgkey_fog_density		= "fog_density";
string cfgkey_response_chase	= "response_chase";  // Percent
string cfgkey_prefix_player1	= "p1_";
string cfgkey_prefix_player2	= "p2_";

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

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

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

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

	config.Write( cfgkey_module,		 string(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 );  // Percent
	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
ModulePony::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 module.
	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 (Module::mPlayer) haven't been created yet.
	ConfigLoadDyna( config, cfgkey_prefix_player1, mPlayer1ConfigState );
	ConfigLoadDyna( config, cfgkey_prefix_player2, mPlayer2ConfigState );

	return true;
}
