//*******************************************************************
//
// File: Asura_Botanical_Game_Render_StateTable.cpp
//
// Created: 05 Dec 2006
//
// Description: 
//
//*******************************************************************

// system libraries
#include "Core/Asura.h"

#if defined( ASURA_USE_BOTANICALS)

// main header
#include "Botanicals/Asura_Botanical_Game_Render_Constants.h"
#include "Botanicals/Asura_Botanical_Game_Render_Instance_Header.h"
#include "Botanicals/Asura_Botanical_Game_Render_StateTable.h"
#include "Botanicals/Asura_Botanical_Game_Render_Type.h"

// other libraries

// internal dependencies

//*******************************************************************
// Constants
//*******************************************************************


//*******************************************************************
// Local Classes
//*******************************************************************


//*******************************************************************
// Function Definitions
//*******************************************************************

#if (defined( ASURA_BOTANICAL_SUPPORT_STATES) && 1 == ASURA_BOTANICAL_SUPPORT_STATES)

#if defined( ASURA_BOTANICAL_SUPPORT_STATES) && 1 == ASURA_BOTANICAL_SUPPORT_STATES
const Asura_BGR_StateTable_Version *Asura_BGR_Type::s_pxPreviousCollidableVersion = NULL;
#endif


void Asura_BGR_Type::SetSectionVersionFromStateTableSection( const Asura_BGR_StateTable_Section& xStateTableSection, const Asura_BGR_State_Value xPrevStateValue, const Asura_BGR_State_Value xStateValue, Asura_BGR_Instances_Header* pxInstanceHeader, Asura_BGR_DynamicInstance* pxDynamicInstance) const
{
	const Asura_BGR_Section_ID xSectionID = xStateTableSection.m_xSectionID;	
	
	const u_int uVersionCount = xStateTableSection.m_xArray_xVersions.Size();

	// Find the current version	
	const Asura_BGR_StateTable_Version* pxOldVersion = NULL;
	for( u_int uVersionIndex = 0; uVersionIndex < uVersionCount; uVersionIndex += 1)
	{
		const Asura_BGR_StateTable_Version& xStateTableVersion = xStateTableSection.m_xArray_xVersions[ uVersionIndex];
		if( xPrevStateValue < xStateTableVersion.m_xRangeStart)
		{
			continue;
		}
		if( xStateTableVersion.m_xRangeStop < xPrevStateValue)
		{
			continue;
		}
		
		pxOldVersion = &xStateTableVersion;		
	}

	// Find the new version	
	for( u_int uVersionIndex = 0; uVersionIndex < uVersionCount; uVersionIndex += 1)
	{
		const Asura_BGR_StateTable_Version& xStateTableVersion = xStateTableSection.m_xArray_xVersions[ uVersionIndex];
		if( xStateValue < xStateTableVersion.m_xRangeStart)
		{
			continue;
		}
		if( xStateTableVersion.m_xRangeStop < xStateValue)
		{
			continue;
		}

		const Asura_BGR_Version_ID xVersionID = xStateTableVersion.m_xVersionID;			
		pxInstanceHeader->SetDynamicSectionVersion( pxDynamicInstance, xSectionID, xVersionID);

		// If we're changing version, and it's a collidable foliage version, store a pointer to the
		// old version, so we've got a way to know if we need some extra PFX effects to help hide
		// the state transition
		if (&xStateTableVersion != pxOldVersion)
		{
			if (pxOldVersion->m_xCollisionMeshHash != ASURA_HASH_ID_UNSET)
			{				
				s_pxPreviousCollidableVersion = pxOldVersion;
			}
		}

		return;
	}
	
	// DLH - 05 December 2006 15:31:47 - There is not a range that covers the present values so turn the section off.
	pxInstanceHeader->SetDynamicSectionVersion( pxDynamicInstance, xSectionID, ASURA_BGR_VERSION_ID_OFF);
}
#endif

#if (defined( ASURA_BOTANICAL_SUPPORT_STATES) && 1 == ASURA_BOTANICAL_SUPPORT_STATES)
void Asura_BGR_Type::SetSectionVersionFromState( const Asura_BGR_State_ID xStateID, const Asura_BGR_State_Value xStateValue, Asura_BGR_Instances_Header* pxInstanceHeader, Asura_BGR_DynamicInstance* pxDynamicInstance) const
{
	Asura_BGR_State_Value xPrevStateValue = pxInstanceHeader->GetState(pxDynamicInstance, xStateID);	
	pxInstanceHeader->SetState( pxDynamicInstance, xStateID, xStateValue);
	
	const Asura_BGR_StateTable& xStateTable = this->m_xArray_xStateTables[ xStateID];
	const u_int uSectionCount = xStateTable.m_xArray_xSections.Size();
	for( u_int uSectionIndex = 0; uSectionIndex < uSectionCount; uSectionIndex += 1)
	{
		const Asura_BGR_StateTable_Section& xStateTableSection = xStateTable.m_xArray_xSections[ uSectionIndex];
		
		SetSectionVersionFromStateTableSection( xStateTableSection, xPrevStateValue, xStateValue, pxInstanceHeader, pxDynamicInstance);
	}
}
#endif

//*******************************************************************
//
// (c) 2006 Rebellion
//
// Warning: This computer program is protected by copyright law and 
// international treaties. Unauthorized reproduction or distribution
// of this program, or any portion of it, may result in severe civil and
// criminal penalties, and will be prosecuted to the maximum extent 
// possible under the law.
//
//*******************************************************************

#endif //defined( ASURA_USE_BOTANICALS)
