if(stage==3){

if(bgphase==1){

SetGraphicScale(1,1);
SetPerspectiveClip(0,2000);
SetAlpha(255);
SetFog(400,800,220,240,255);
SetViewTo(0,basev+viewv,viewh);
SetViewFrom(50,viewx,viewy);
WriteZBuffer(true);
UseZBuffer(true);

//END
	SetColor(220,240,255); SetAlpha(255); SetGraphicScale(1,1);
	SetTexture(BGend); SetGraphicRect(0,0,3000,2000);
	SetGraphicAngle(0,90,0); DrawGraphic3D(-1400,0,0);
	SetGraphicAngle(0,0,180); DrawGraphic3D(-700,0,-800);
	SetGraphicAngle(0,0,180); DrawGraphic3D(700,0,-800);
	SetColor(255,255,255);

let bgrepeat=300;
loop(7){
//FLOOR INSIDE
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGfloor1); SetGraphicRect(0,0+vscroll,150,300+vscroll);
	SetGraphicAngle(90,90,0); DrawGraphic3D(bgrepeat,0,0);
//FLOOR OUTSIDE
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGfloor2); SetGraphicRect(0,0+vscroll,100,300+vscroll);
	SetGraphicAngle(90,90,0); DrawGraphic3D(bgrepeat,0,-125);
//GRASS
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGgrass); SetGraphicRect(0,0+vscroll,300,300+vscroll);
	SetGraphicAngle(90,90,0); DrawGraphic3D(bgrepeat,-40,-325); DrawGraphic3D(bgrepeat,-40,-625);
//ROOM FLOOR
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGfloor3); SetGraphicRect(0,0+vscroll,300,300+vscroll);
	SetGraphicAngle(90,90,0); DrawGraphic3D(bgrepeat,0,225);
//CEILING 1
	SetAlpha(255); SetGraphicScale(0.6,1); SetTexture(BGceiling1); SetGraphicRect(0,0+vscroll,150,300+vscroll);
	SetGraphicAngle(128,180,90); DrawGraphic3D(bgrepeat,250,-35);
	SetGraphicAngle(52,180,90); DrawGraphic3D(bgrepeat,250,35);
//CEILING 2
	SetAlpha(255); SetGraphicScale(2,1); SetTexture(BGceiling2); SetGraphicRect(0,0+vscroll,75,300+vscroll);
	SetGraphicAngle(110,180,90); DrawGraphic3D(bgrepeat,230,-135);
//CEILING 3
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGceiling3); SetGraphicRect(0,0+vscroll,300,300+vscroll);
	SetGraphicAngle(90,90,0); DrawGraphic3D(bgrepeat,200,225);
//HORIZONTAL BEAM
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGbeam); SetGraphicRect(0,0,150,10);
	SetGraphicAngle(0,90,0); DrawGraphic3D(bgrepeat+scroll,220,0); DrawGraphic3D(bgrepeat+150+scroll,220,0);
//VERTICAL BEAM
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGbeam); SetGraphicRect(0,0,50,10);
	SetGraphicAngle(0,90,90); DrawGraphic3D(bgrepeat+scroll,250,0); DrawGraphic3D(bgrepeat+150+scroll,250,0);
bgrepeat-=300;
} //loop 6x

let bgrepeat=0;
loop(3){

//ROOM WALL
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGroomwall1); SetGraphicRect(0,0,298,200);
	SetGraphicAngle(0,0,180); DrawGraphic3D(bgrepeat-150+scroll,100,375);  DrawGraphic3D(bgrepeat+150+scroll,100,375);
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGroomwall2); SetGraphicRect(0,0,298,200);
	SetGraphicAngle(0,270,180); DrawGraphic3D(bgrepeat+scroll,100,225);  DrawGraphic3D(bgrepeat+300+scroll,100,225);
//INNER WALL
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGinnerwall1); SetGraphicRect(0,0,150,200);
	SetGraphicAngle(0,0,180); DrawGraphic3D(bgrepeat+scroll,100,75);
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGinnerwall2); SetGraphicRect(0,0,150,200);
	SetGraphicAngle(0,0,180); DrawGraphic3D(bgrepeat+150+scroll,100,75);
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGinnerwall1); SetGraphicRect(0,0,150,200);
	SetGraphicAngle(0,0,180); DrawGraphic3D(bgrepeat+300+scroll,100,75);
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGinnerwall3); SetGraphicRect(0,0,150,200);
	SetGraphicAngle(0,0,180); DrawGraphic3D(bgrepeat+450+scroll,100,75);

	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGtopwall); SetGraphicRect(0,0,600,50);
	SetGraphicAngle(0,0,180); DrawGraphic3D(bgrepeat+scroll,225,75); DrawGraphic3D(bgrepeat+scroll,225,-75);
//OUTER WALL
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGouterwall); SetGraphicRect(0,0,300,200);
	SetGraphicAngle(0,180,180); DrawGraphic3D(bgrepeat+scroll,100,-75);
	SetGraphicAngle(0,0,180); DrawGraphic3D(bgrepeat+300+scroll,100,-75);
//WALL BEAM
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGbeam); SetGraphicRect(0,0,200,10);
	SetGraphicAngle(0,0,90); DrawGraphic3D(bgrepeat+150+scroll,100,-74);
bgrepeat-=600;
} //loop 4x

//TREES
let i=0;
	while(i<length(tree)){
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGtree); SetGraphicRect(0,0,80,120);
	SetGraphicAngle(0,treerandomangle[i],180); if(tree[i]<=200){ DrawGraphic3D(tree[i],30,-200-treerandomx[i]); }
	SetGraphicAngle(0,treerandomangle[i]+90,180); if(tree[i]<=200){ DrawGraphic3D(tree[i],20,-200-treerandomx[i]); }
	i++;
	}

} //bgphase 1

//============================================================================================================

if(bgphase==2){

SetGraphicScale(1,1);
SetPerspectiveClip(0,2000);
SetAlpha(255);
SetFog(400,800,220,240,255);
SetViewTo(0,100,0);
SetViewFrom(50,-15+5*cos(time/3),10);
WriteZBuffer(true);
UseZBuffer(true);

//END
	SetColor(220,240,255); SetAlpha(255); SetGraphicScale(1,1);
	SetTexture(BGend); SetGraphicRect(0,0,3000,2000);
	SetGraphicAngle(0,90,0); DrawGraphic3D(-1400,0,0);
	SetColor(255,255,255);

let bgrepeat=0;
//GRASS
let ypos=0;
	loop(3){
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGgrass); SetGraphicRect(0,0,600,600);
	SetGraphicAngle(90,90,0); DrawGraphic3D(ypos,0,0); DrawGraphic3D(ypos,0,300); DrawGraphic3D(ypos,0,-300);
	ypos-=300;
	}
//LAKE
	SetAlpha(255); SetGraphicScale(1,2); SetTexture(BGlake); SetGraphicRect(0,0,400,400);
	SetGraphicAngle(90,90,0); DrawGraphic3D(-600,1,-150);
//PATH
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGpath); SetGraphicRect(0,0,100,300);
	SetGraphicAngle(90,120,0); DrawGraphic3D(-90,1,50);
	SetGraphicAngle(90,90,0); DrawGraphic3D(-385,1,125);
	SetGraphicAngle(90,50,0); DrawGraphic3D(-660,1,20);
	SetGraphicAngle(90,150,180); DrawGraphic3D(-160,1,-50);
//LANTERN
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGlantern); SetGraphicRect(0,0,30,80);
	SetGraphicAngle(0,100,180); DrawGraphic3D(-270,40,70);
//BRIDGE
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGbridge); SetGraphicRect(0,0,150,50);
	SetGraphicAngle(0,90,180); DrawGraphic3D(-730,20,-170);
//ROCKS
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGrocks);
	SetGraphicRect(0,0,30,30); SetGraphicAngle(0,90,180); DrawGraphic3D(-250,14,-30);
	SetGraphicRect(0,0,30,30); SetGraphicAngle(0,120,180); DrawGraphic3D(-430,14,30);
	SetGraphicRect(30,0,60,30); SetGraphicAngle(0,80,180); DrawGraphic3D(-270,14,-80);
	SetGraphicRect(60,0,90,30); SetGraphicAngle(0,60,180); DrawGraphic3D(-190,14,-150);
	SetGraphicRect(60,0,90,30); SetGraphicAngle(0,80,180); DrawGraphic3D(-540,14,0);
//TREE
let angle=0;
	loop(2){
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGtree); SetGraphicRect(0,0,80,120);
	SetGraphicAngle(0,70+angle,180); DrawGraphic3D(-200,60,150);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-400,60,180);
	SetGraphicAngle(0,30+angle,180); DrawGraphic3D(-500,60,70);
	SetGraphicAngle(0,30+angle,180); DrawGraphic3D(-200,60,-80);
	angle=90;
	}
//BUSH 1
let angle=0;
	loop(2){
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGbush1); SetGraphicRect(0,0,70,40);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-100,20,120);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-330,20,210);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-260,20,30);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-400,20,90);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-210,20,-90);
	angle=45;
	}
//BUSH 2
let angle=0;
	loop(2){
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGbush2); SetGraphicRect(0,0,70,40);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-60,20,100);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-150,20,30);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-240,20,160);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-180,20,210);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-320,20,80);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-350,20,320);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-220,20,-20);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-460,20,30);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-460,20,200);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-520,20,150);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-630,20,-30);
	angle=45;
	}
//BUSH 3
let angle=0;
	loop(2){
	SetAlpha(255); SetGraphicScale(1,1); SetTexture(BGbush3); SetGraphicRect(0,0,50,70);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-120,35,130);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-140,35,-40);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-280,35,280);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-320,35,180);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-350,35,320);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-550,35,130);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-520,35,190);
	SetGraphicAngle(0,-50+angle,180); DrawGraphic3D(-670,35,50);
	SetGraphicAngle(0,50+angle,180); DrawGraphic3D(-220,35,-40);
	angle=45;
	}

} //bgphase 2

}