class PlayerElevationPrereqState extends PrereqState
{
	var m_minElevationValue : Float;
	var m_maxElevationValue : Float;
	var m_minElevationListener : CallbackHandle;
	var m_maxElevationListener : CallbackHandle;
	var m_owner : weak< GameObject >;

	protected event OnMinElevationUpdateFloat( value : Float )
	{
		var checkPassed : Bool;
		const var prereq : PlayerElevationPrereq;
		prereq = ( ( PlayerElevationPrereq )( GetPrereq() ) );
		if( m_minElevationValue != value )
		{
			checkPassed = prereq.Evaluate( m_owner, m_minElevationValue, m_maxElevationValue );
			OnChanged( checkPassed );
		}
		m_minElevationValue = value;
	}

	protected event OnMaxElevationUpdateFloat( value : Float )
	{
		var checkPassed : Bool;
		const var prereq : PlayerElevationPrereq;
		prereq = ( ( PlayerElevationPrereq )( GetPrereq() ) );
		if( m_maxElevationValue != value )
		{
			checkPassed = prereq.Evaluate( m_owner, m_minElevationValue, m_maxElevationValue );
			OnChanged( checkPassed );
		}
		m_maxElevationValue = value;
	}

}

class PlayerElevationPrereq extends IScriptablePrereq
{
	var m_elevationThreshold : Float;

	protected export override function Initialize( recordID : TweakDBID )
	{
		m_elevationThreshold = TDB.GetFloat( recordID + T".elevationThreshold" );
	}

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var bb : IBlackboard;
		var castedState : PlayerElevationPrereqState;
		bb = GameInstance.GetBlackboardSystem( game ).GetLocalInstanced( ( ( ScriptedPuppet )( context ) ).GetEntityID(), GetAllBlackboardDefs().PlayerStateMachine );
		castedState = ( ( PlayerElevationPrereqState )( state ) );
		castedState.m_owner = ( ( GameObject )( context ) );
		castedState.m_minElevationListener = bb.RegisterListenerFloat( GetAllBlackboardDefs().PlayerStateMachine.MinElevation, castedState, 'OnMinElevationUpdateFloat' );
		castedState.m_maxElevationListener = bb.RegisterListenerFloat( GetAllBlackboardDefs().PlayerStateMachine.MaxElevation, castedState, 'OnMaxElevationUpdateFloat' );
		return false;
	}

	protected const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var bb : IBlackboard;
		var castedState : PlayerElevationPrereqState;
		castedState = ( ( PlayerElevationPrereqState )( state ) );
		bb = GameInstance.GetBlackboardSystem( game ).GetLocalInstanced( ( ( ScriptedPuppet )( context ) ).GetEntityID(), GetAllBlackboardDefs().PlayerStateMachine );
		bb.UnregisterListenerFloat( GetAllBlackboardDefs().PlayerStateMachine.MinElevation, castedState.m_minElevationListener );
		bb.UnregisterListenerFloat( GetAllBlackboardDefs().PlayerStateMachine.MaxElevation, castedState.m_maxElevationListener );
	}

	public const function Evaluate( owner : GameObject, minElevationVal : Float, maxElevationVal : Float ) : Bool
	{
		return ( AbsF( minElevationVal ) > m_elevationThreshold ) || ( AbsF( maxElevationVal ) > m_elevationThreshold );
	}

}

