ScriptName xLVMotionStorage extends Quest

xLVMotionStructure Property Motion000 Auto
xLVMotionStructure Property Motion001 Auto
xLVMotionStructure Property Motion002 Auto
xLVMotionStructure Property Motion003 Auto
xLVMotionStructure Property Motion004 Auto
xLVMotionStructure Property Motion005 Auto
xLVMotionStructure Property Motion006 Auto
xLVMotionStructure Property Motion007 Auto
xLVMotionStructure Property Motion008 Auto
xLVMotionStructure Property Motion009 Auto
xLVMotionStructure Property Motion010 Auto
xLVMotionStructure Property Motion011 Auto
xLVMotionStructure Property Motion012 Auto
xLVMotionStructure Property Motion013 Auto
xLVMotionStructure Property Motion014 Auto
xLVMotionStructure Property Motion015 Auto
xLVMotionStructure Property Motion016 Auto
xLVMotionStructure Property Motion017 Auto
xLVMotionStructure Property Motion018 Auto
xLVMotionStructure Property Motion019 Auto

xLVMotionStructureBase[] Motions
xLVMotionStructureBase[] EmptyArray
;;--------------------------------------------------;;
;; MF Motions
;;--------------------------------------------------;;
;; Validな全MF Motion配列 ;;
int SizeAll = 0
xLVMotionStructureBase[] MotionsAll
;;--------------------------------------------------;;
;; Single Motions
;;--------------------------------------------------;;
;; Validな全Single Motion配列 ;;
int SizeAllS = 0
xLVMotionStructureBase[] MotionsAllS

;;--------------------------------------------------------------------------------;;
;; Construct Functions
;;--------------------------------------------------------------------------------;;
Function ConstructMotion()

	Motions = new xLVMotionStructureBase[20]
	MotionsAll = new xLVMotionStructureBase[20]
	MotionsAllS = new xLVMotionStructureBase[20]
	
	Motions[0] = Motion000
	Motions[1] = Motion001
	Motions[2] = Motion002
	Motions[3] = Motion003
	Motions[4] = Motion004
	Motions[5] = Motion005
	Motions[6] = Motion006
	Motions[7] = Motion007
	Motions[8] = Motion008
	Motions[9] = Motion009
	Motions[10] = Motion010
	Motions[11] = Motion011
	Motions[12] = Motion012
	Motions[13] = Motion013
	Motions[14] = Motion014
	Motions[15] = Motion015
	Motions[16] = Motion016
	Motions[17] = Motion017
	Motions[18] = Motion018
	Motions[19] = Motion019
	
	xLVMotionStructureS WorkS = None
	xLVMotionStructure WorkMF = None
	
	int i = 0
	SizeAll = 0
	while ( i < Motions.Length )
		if ( Motions[i] != None )
			Motions[i].SetMotionID(i)
			
			;; Validなモーションのみを格納 ;;
			if ( Motions[i].IsValid() == True )
			
				WorkS = motions[i] as xLVMotionStructureS
				if ( WorkS != None )
					MotionsAllS[SizeAllS] = Motions[i]
					SizeAllS += 1
					SendModEvent( "XLVLog", "Motion ID " + Motions[i].GetMotionID() + " is registered to Single as Valid", 0 )
				endif
			
				WorkMF = motions[i] as xLVMotionStructure
				if ( WorkMF != None )
					MotionsAll[SizeAll] = Motions[i]
					SizeAll += 1
					SendModEvent( "XLVLog", "Motion ID " + Motions[i].GetMotionID() + " is registered to MF as Valid", 0 )
				endif
				
			endif
			
		endif
		i += 1
	endwhile
	
EndFunction

Event OnInit()
	ConstructMotion()
EndEvent

;;--------------------------------------------------------------------------------;;
;; Motion Select Helper Functions
;;--------------------------------------------------------------------------------;;
xLVMotionStructureBase[] Function SelectMotionArray( string array_name = "all" )

	xLVMotionStructureBase[] ret
	
	if ( array_name == "all" )
		ret = MotionsAll
	elseif ( array_name == "allS" )
		ret = MotionsAllS
	else
		ret = EmptyArray
	endif

	return ret
EndFunction

int Function GetMotionArraySize( string array_name = "all" )

	int ret = 0
	
	if ( array_name == "all" )
		ret = SizeAll
	elseif ( array_name == "allS" )
		ret = SizeAllS
	endif
	
	return ret

EndFunction

;; モーション配列から指定モーションのindexを検索する ;;
int Function FindIndexFromMotion( xLVMotionStructureBase pMotion, xLVMotionStructureBase[] pArray )

	if ( pMotion == None )
		return -1
	endif
	if ( pArray.Length == 0 )
		return -1
	endif

	int i = 0
	while ( i < pArray.Length )
		
		if ( pArray[i].GetMotionID() == pMotion.GetMotionID() )
			return i
		endif
		
		i += 1
	endwhile

	return -1
EndFunction

;; モーション配列内で、指定indexの次のモーションを返す ;;
xLVMotionStructureBase Function FindNextMotion( xLVMotionStructureBase[] pArray, int index )
	
	if ( pArray.Length == 0 )
		return None
	endif
	if ( index < 0 )
		return None
	endif
	if ( index >= pArray.Length )
		return None
	endif
	
	int i = index + 1
	if ( i >= pArray.Length )
		return pArray[pArray.Length - 1]
	endif
	
	return pArray[i]
EndFunction

;; モーション配列内で、指定indexの前のモーションを返す ;;
xLVMotionStructureBase Function FindPrevMotion( xLVMotionStructureBase[] pArray, int index )
	if ( pArray.Length == 0 )
		return None
	endif
	if ( index < 0 )
		return None
	endif
	if ( index >= pArray.Length )
		return None
	endif
	
	int i = index - 1
	if ( i < 0 )
		return pArray[0]
	endif
	
	return pArray[i]
EndFunction

;;--------------------------------------------------------------------------------;;
;; Motion Select Functions
;;--------------------------------------------------------------------------------;;
;; array_nameで指定されたmotion_array内のid番目のMotionStructureを返す ;;
;; 指定idが負値の場合、同motion_array内のランダムなMotionStructureを返す ;;
xLVMotionStructureBase Function GetMotion( int id, string array_name = "all" )

	xLVMotionStructureBase[] target_array = SelectMotionArray( array_name )
	int array_size = GetMotionArraySize( array_name )

	if ( id < 0 )
		;; idが負ならランダム応答 ;;
		return GetRandomMotion( array_name )
	endif
	if ( id >= array_size )
		return None
	endif
	if ( target_array.Length == 0 )
		return None
	endif
	
	return target_array[id]

EndFunction

xLVMotionStructureBase Function GetNextMotion( xLVMotionStructureBase pMotion, string array_name = "all" )

	int i = 0
	xLVMotionStructureBase ret = None
	xLVMotionStructureBase[] target_array = SelectMotionArray( array_name )
	
	if ( target_array.Length == 0 )
		return None
	endif
	
	i = FindIndexFromMotion( pMotion, target_array )
	if ( i == -1 )
		return None
	endif

	ret = FindNextMotion( target_array, i )
	if ( ret == None )
		SendModEvent( "XLVLog", "Next Motion not found for " + array_name, 1 )
	endif
	
	return ret
EndFunction

xLVMotionStructureBase Function GetPrevMotion( xLVMotionStructureBase pMotion, string array_name = "all" )

	int i = 0
	xLVMotionStructureBase ret = None
	xLVMotionStructureBase[] target_array = SelectMotionArray( array_name )
	
	if ( target_array.Length == 0 )
		return None
	endif
	
	i = FindIndexFromMotion( pMotion, target_array )
	if ( i == -1 )
		return None
	endif

	ret = FindPrevMotion( target_array, i )
	if ( ret == None )
		SendModEvent( "XLVLog", "Prev Motion not found for " + array_name, 1 )
	endif
	
	return ret
EndFunction

xLVMotionStructureBase Function GetRandomMotion( string array_name = "all" )

	xLVMotionStructureBase[] target_array = SelectMotionArray( array_name )
	int array_size = GetMotionArraySize( array_name )
	xLVMotionStructureBase ret = None
		
	if ( target_array.Length == 0 )
		return None
	endif
	
	int i = Utility.RandomInt( 0, array_size - 1 )
	
	ret = target_array[i]
	if ( ret == None )
		SendModEvent( "XLVLog", "Random Motion lookup failed for " + array_name, 1 )
	endif
	
	return ret
EndFunction

xLVMotionStructureBase Function GetMotionBySignature( string motion_signature )
	
	xLVMotionStructureBase[] target_array = MotionsAll
	int array_size = SizeAll
	
	int i = 0
	while ( i < array_size )		
		if ( target_array[i].GetSignature() == motion_signature )
			return target_array[i]
		endif
		i += 1
	endwhile
	
	return None
EndFunction

;;--------------------------------------------------------------------------------;;


