//////////////////////////////////////////////////////////////////////////////
//
// AVP_Chunk_Navigation.cpp
//
// Copyright (C) 2007 Rebellion. All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

// core
#include "Core/Asura.h"

// header
#include "ChunkSystem/AVP_Chunk_Navigation.h"

// asura
#include "ChunkSystem/Asura_Chunk_Stream.h"

// avp
#include "ChunkSystem/AVP_Chunk_Loaders.h"

// axon
#include "Core/AVP_Waypoint_Manager.h"

#ifdef ASURA_TOOLS
// tools
#include "Load&Save/LevelExport/Tools_Level_Export.h"
#include "Waypointing/ToolsAVP_Mode_Waypointing.h"
#endif

//////////////////////////////////////////////////////////////////////////////
//
// AVP_Chunk_Navigation
//
//////////////////////////////////////////////////////////////////////////////

#ifdef ASURA_TOOLS
void AVP_Chunk_Navigation::WriteToChunkStream( Asura_Chunk_Stream& xChunkStream )
{
    const bool bIncludeSettings = !Tools_Level_Export::IsExport();
    const u_int uFlags = bIncludeSettings ? CHUNK_CONTAINS_SETTINGS : 0;

    xChunkStream.WriteChunk_Start( AVP_CHUNKID_NAVIGATION, CURRENT_CHUNK_VERSION, uFlags );

    if ( bIncludeSettings )
    {
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fWaypointVolumeMinAngle;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fWaypointVolumeMaxAngle;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fLinkMaxAngle;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fTransitionLinkMinAngle;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fTransitionLinkMaxAngleToPerfect;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fSurfaceAngleTolerance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMinTransitionLinkDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMaxTransitionLinkDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMinFloorCeilingJumpDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMaxFloorCeilingJumpDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMinCeilingFloorFallDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMaxCeilingFloorFallDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMinWallWallJumpDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fMaxWallWallJumpDistance;
        xChunkStream << ToolsAVP_Mode_Waypointing::s_fJumpTargetSurfaceAngleTolerance;
    }

	AVP_Waypoint_Manager::WriteToChunkStream( xChunkStream );

    xChunkStream.WriteChunk_End();
}
#endif

void AVP_Chunk_Navigation::ReadFromChunkStream( Asura_Chunk_Stream& xChunkStream )
{
#if defined ASURA_TOOLS || defined ASURA_ASSERT
	const bool bIncludesSettings = ( ( Flags & CHUNK_CONTAINS_SETTINGS ) != 0 );
#endif

	if ( Version == CURRENT_CHUNK_VERSION )
	{
#ifdef ASURA_TOOLS
		if ( bIncludesSettings )
		{
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fWaypointVolumeMinAngle;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fWaypointVolumeMaxAngle;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fLinkMaxAngle;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fTransitionLinkMinAngle;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fTransitionLinkMaxAngleToPerfect;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fSurfaceAngleTolerance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinTransitionLinkDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxTransitionLinkDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinFloorCeilingJumpDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxFloorCeilingJumpDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinCeilingFloorFallDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxCeilingFloorFallDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinWallWallJumpDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxWallWallJumpDistance;
			xChunkStream >> ToolsAVP_Mode_Waypointing::s_fJumpTargetSurfaceAngleTolerance;
		}
#else
		Asura_Assert( !bIncludesSettings, "AVP_Chunk_Navigation::ReadFromChunkStream - shouldn't be saving waypointing settings to exported files" );
#endif

		AVP_Waypoint_Manager::ReadFromChunkStream( xChunkStream );
	}
	else if ( Version == 1 )
    {
#ifdef ASURA_TOOLS
        if ( bIncludesSettings )
        {
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fWaypointVolumeMinAngle;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fWaypointVolumeMaxAngle;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fLinkMaxAngle;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fTransitionLinkMinAngle;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fTransitionLinkMaxAngleToPerfect;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fSurfaceAngleTolerance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinTransitionLinkDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxTransitionLinkDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinFloorCeilingJumpDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxFloorCeilingJumpDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinCeilingFloorFallDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxCeilingFloorFallDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMinWallWallJumpDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fMaxWallWallJumpDistance;
            xChunkStream >> ToolsAVP_Mode_Waypointing::s_fJumpTargetSurfaceAngleTolerance;
        }
#else
        Asura_Assert( !bIncludesSettings, "AVP_Chunk_Navigation::ReadFromChunkStream - shouldn't be saving waypointing settings to exported files" );
#endif
    }
    else if ( Version == 0 )
    {
    }
    else
    {
        Asura_Assertf( false, ( "AVP_Chunk_Navigation::ReadFromChunkStream - unhandled chunk version (%d)", Version ) );
    }
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
