//====================================================================
// Stage functions
//====================================================================
//--------------------------------------------------------------------
// Stage variables
//--------------------------------------------------------------------

let dirStage = GetCurrentScriptDirectory();
/** Main Mokou Boss */
/** Image directories */
let dirStage2 = dirStage ~ "Stage\";
let mapBamboo = dirStage2 ~ "Image\Bamboo.png";
let mapGround = dirStage2 ~ "Image\Ground.png";
/** Tables */
let RandTable = [];
let MaxRandLength = 255;

/** Count */
let STGCount = 0;
/** Loading */
LoadGraphic( mapBamboo );
LoadGraphic( mapGround );

/** Areas! */
CreateCommonDataArea( "TEMP" );
CreateCommonDataArea( "STAGE_PARAMETER" );
CreateCommonDataArea( "GAP_X" );
CreateCommonDataArea( "GAP_Y" );
CreateCommonDataArea( "GAP_Z" );


/** From... */
let FromDistance = 0;
let FromYaw = 1;
let FromPitch = 2;
let FromX = 3;
let FromY = 4;
let FromZ = 5;

/** Phase */
let Phase = 0;
let PhaseCount = 0;

//--------------------------------------------------------------------
// Stage functions
//--------------------------------------------------------------------

/**
*	MainLoop special
*/
function BGMainLoop{
	return;
	let SpeedTable = [ 0.3, 2, 1, 3 ];
	let BGParam = GetCommonDataDefaultEx( "TEMP", "STAGE_PARAMETER", [ 0, 90, 0, 0, 200, 0 ] );
	BGParam[ FromZ ] = BGParam[ FromZ ] - Smooth( SpeedTable[ Max( Phase - 1, 0 ) ], SpeedTable[ Phase ], PhaseCount, 300 );
	if( Phase == 0 && PhaseCount <= 1000 ){
		BGParam[ FromDistance ] = STGCount % sin( Phase % 720 ) * 2;
		BGParam[ FromPitch ] = PhaseCount;
	}
	SetCommonDataEx( "TEMP", "STAGE_PARAMETER", BGParam );
	PhaseCount ++; Phase ++; STGCount ++;
}

/** 
*	mORE STUFF
*/
function CommonInit{
	if( !GetCommonDataDefault( "CDNS", false ) ){
		SetCommonData( "CDNS", true );
		CreateCommonDataArea( "CDNS" );
	}
	ascent( let i in 0 .. MaxRandLength ){ RandTable = RandTable ~ [ rand_int( 0, 255 ) ]; }
}

/**
*	Draw the BG graphics
*	@param StageCount	( You guess )
*/
function DrawBackground( let StageCount ){
	if( !GetCommonDataDefault( "STAGE_PARAMETER", 0 ) ){ SetFog( 200, 1000, 0, 0, 0 ); }
	let BGParam = GetCommonDataEx( "TEMP", "STAGE_PARAMETER" );
	SetFog( 
		Smooth( 50, 200, StageCount, 600 ),
		Smooth( 100, 1000, StageCount, 500 ),
		0,
		0,
		0
	);
	SetViewFrom( BGParam[ FromDistance ] * 1 / 4, BGParam[ FromYaw ] * sin( STGCount / 3 ), BGParam[ FromPitch ] * sin( STGCount / 4 ) );
	let ViewToX = BGParam[ FromX ] + GetCommonDataDefaultEx( "TEMP", "GAP_X", 0 );
	let ViewToY = BGParam[ FromY ] + GetCommonDataDefaultEx( "TEMP", "GAP_Y", 0 );
	let ViewToZ = BGParam[ FromZ ] + GetCommonDataDefaultEx( "TEMP", "GAP_Z", 0 );
	SetViewTo( ViewToX, ViewToY, ViewToZ );
	SetTexture( mapGround );
	SetGraphicAngle( 90, 0, 0 );
	SetGraphicRect( 0, 0, 1536, 512 );
	let Z = truncEx( BGParam[ FromZ ], 1024 ) - 1792;
	ascent( let i in - 1 .. 6 ){ DrawGraphic3D( 0, - 100, Z + i * 512 ); }

	SetTexture( mapBamboo );
	SetGraphicRect( 0, 0, 214, 512 );
	SetGraphicScale( 1, 1 );
	let Space = 256;
	let Z = truncEx( BGParam[ FromZ ], Space ) - 1000;
	loop( trunc( 1600 / Space ) ){
		let UniqueNum = trunc( absolute( Z ) / Space );
		ascent( let x in 0.. 2 ){
			let Dir = [ 1, -1 ][ x ];
			let DstX = ( 112 + RandTable[ ( UniqueNum + 17 ) % MaxRandLength ] / 2 ) * Dir;
			let DstZ = Z + RandTable[ UniqueNum % MaxRandLength ] / 5 + x * Space / 3;
			let Branch = [ 4, 2 ][ Z < - 2400 ];
			ascent( i in 0 .. Branch ){
				SetGraphicAngle( 0, Smooth( 0, 180, i, Branch ) + RandTable[ UniqueNum % MaxRandLength ], 180 );
				DrawGraphic3D( DstX, 100 + RandTable[ ( UniqueNum + i ) % MaxRandLength ] / 8, DstZ );
				DrawGraphic3D( DstX - 190, 100 + RandTable[ ( UniqueNum + i ) % MaxRandLength ] / 8, DstZ );
				DrawGraphic3D( DstX + 190, 100 + RandTable[ ( UniqueNum + i ) % MaxRandLength ] / 8, DstZ );
				DrawGraphic3D( DstX - 390, 100 + RandTable[ ( UniqueNum + i ) % MaxRandLength ] / 8, DstZ );
				DrawGraphic3D( DstX + 290, 100 + RandTable[ ( UniqueNum + i ) % MaxRandLength ] / 8, DstZ );
			}
		}
		Z += Space;
	}
	SetColor( 255, 255, 255 );
	SetAlpha( 255 );
	SetGraphicAngle( 0, 0, 0 );
	STGCount ++;
	if( STGCount == 1400 ){ STGCount = STGCount % 20000 }
}

/**
*	Smooth out colors and stuff
*
*	@param Start	( Starting effect )
*	@param End	( Ending Effect )
*	@param Now	( Effect right now )
*	@param Limit	( Max Limit )
*/
function Smooth( let Start, let End, let Now, let Limit ){
	if( Now >= Limit || Start == End || Limit <= 0 ){ return End; }
	if( Now <= 0 ){ return Start; }
	return Start + ( End - Start ) * Now / Limit;
}

/**
*	Math shit
*	@param Expr	...
*	@param Unit	...
*/
function truncEx( let Expr, let Unit ){ return trunc( Expr / Unit ) * Unit; }
function Max( Expr, Expr2 ){ return [ Expr2, Expr ][ Expr > Expr2 ]; }


