#e[Stage]
#Title[K[_̗@ubN[x]
#Text[K[_̗@AW
[U[`L[ŔgK𔭎]
#BGM[]
#Player[.\K[_̗p@\atype.txt ]
#ScriptVersion[2]

script_stage_main{
      SetCommonData("kinkai",0);
      SetCommonData("bgx",0);
      SetCommonData("bgy",0);
      SetCommonData("haya",1);
      SetCommonData("ransu",0);
      SetCommonData("prerank",0);
      SetCommonData("rank",0);
      SetCommonData("itemloop",0);
      SetCommonData("itemloop2",0);
      SetCommonData("itemtable",1);
      SetCommonData("kunsyo",1);
      SetCommonData("kunsyonum",0);
      SetCommonData("power",0);
      SetCommonData("option",0);
      SetCommonData("bombchip",0);
      SetCommonData("maura",0);

      SetCommonData("kakusei",0);
      SetCommonData("ruikei",0);
      SetCommonData("gekitui",0);
      SetCommonData("kakuseikinsi",true);
      SetCommonData("autobomb",true);

      SetCommonData("ex",0);

      let extend = 1;
      let stage = 1;
      let phase = 0;
      let speed = 1;
      let fspeedup = false;
      let haya = false;
      let time = 0;
      let clear = false;
      let misscount = 0;
      let misssyori = false;
      let clearhyouzi = 0;
      let select = 0;
      let stageselect = false;
      let rankupcount = 0;
      let zakotank1 = GetCurrentScriptDirectory~"zakotank1.txt";
      let zakotank2 = GetCurrentScriptDirectory~"zakotank2.txt";
      let zakohune1 = GetCurrentScriptDirectory~"zakohune1.txt";
      let zakocannon1 = GetCurrentScriptDirectory~"zakocannon1.txt";
      let zakoheli1 = GetCurrentScriptDirectory~"zakoheli1.txt";
      let zakoheli2 = GetCurrentScriptDirectory~"zakoheli2.txt";
      let zakoheli3 = GetCurrentScriptDirectory~"zakoheli3.txt";
      let zakosky1 = GetCurrentScriptDirectory~"zakosky1.txt";
      let tank1 = GetCurrentScriptDirectory~"tank1.txt";
      let tank2 = GetCurrentScriptDirectory~"tank2.txt";
      let tank3 = GetCurrentScriptDirectory~"tank3.txt";
      let tank4 = GetCurrentScriptDirectory~"tank4.txt";
      let lift = GetCurrentScriptDirectory~"lift.txt";
      let sky1 = GetCurrentScriptDirectory~"sky1.txt";
      let sky2 = GetCurrentScriptDirectory~"sky2.txt";
      let sky3 = GetCurrentScriptDirectory~"sky3.txt";
      let sky4 = GetCurrentScriptDirectory~"sky4.txt";
      let skybig1 = GetCurrentScriptDirectory~"skybig1.txt";
      let hunebig1 = GetCurrentScriptDirectory~"hunebig1.txt";
      let item = GetCurrentScriptDirectory~"item.txt";
      let obj1 = GetCurrentScriptDirectory~"obj1.txt";
      let obj2 = GetCurrentScriptDirectory~"obj2.txt";

      let st1boss = GetCurrentScriptDirectory~"st1boss.txt";
      let st2boss = GetCurrentScriptDirectory~"st2boss.txt";
      let st3semiboss = GetCurrentScriptDirectory~"st3semiboss.txt";
      let st3boss = GetCurrentScriptDirectory~"st3boss.txt";
      let st3exboss = GetCurrentScriptDirectory~"st3exboss.txt";
      let trueboss = GetCurrentScriptDirectory~"trueboss.txt";

      let bgmst1 = GetCurrentScriptDirectory~"bgm\Stage1.mp3";
      let bgmst2 = GetCurrentScriptDirectory~"bgm\Stage2.mp3";
      let bgmst3 = GetCurrentScriptDirectory~"bgm\Stage3.mp3";
      let bgmboss = GetCurrentScriptDirectory~"bgm\Boss.mp3";
      let bgmlastboss = GetCurrentScriptDirectory~"bgm\LastBoss.mp3";
      let bgmtruelastboss = GetCurrentScriptDirectory~"bgm\TrueLastBoss.mp3";
      let bgmclear = GetCurrentScriptDirectory~"bgm\Clear.mp3";

      let se4 = GetCurrentScriptDirectory~"wav\se4.wav";

      let system = GetCurrentScriptDirectory~"img\system.png";
      let stringarray=["0","1","2","3","4","5","6","7","8","9"];
   @Initialize(){
      /*if(GetKeyState(VK_USER)==KEY_HOLD){
         SetCommonData("ex",1);
      }*/
      SetRateScoreSystemEnable(false);
      LoadGraphic( system );
      LoadGraphic( GetCurrentScriptDirectory~"img\title.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st1bg1.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st1bg2.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st2bg1.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st3bg1.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st3bg2.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st3bg3.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st3bg4.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st3bg5.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st3bg6.png" );
      LoadGraphic( GetCurrentScriptDirectory~"img\st3boss.png" );
      AddPoint(100);
      SetCommonData("bairitu",1);
      ExpertEx( true, 0, 3 );
      ForbidBomb(true);
   }
   @MainLoop(){
      if(stage==0){
         ForbidShot(true);
         SetPlayerX(224);
         SetPlayerY(400);
         if(GetKeyState(VK_DOWN)==KEY_PUSH){
            select+=1;
            if(select>3){select=0}
         }
         if(GetKeyState(VK_UP)==KEY_PUSH){
            select-=1;
            if(select<0){select=3}
         }
         if(GetKeyState(VK_USER)==KEY_PUSH){
            if(GetCommonData("ex")==0){SetCommonData("ex",1);}
            else if(GetCommonData("ex")==1){SetCommonData("ex",0);}
         }
         if(GetKeyState(VK_BOMB)==KEY_PUSH){
            if(GetCommonData("autobomb")==false){SetCommonData("autobomb",true);}
            else if(GetCommonData("autobomb")==true){SetCommonData("autobomb",false);}
         }
         if(GetKeyState(VK_SHOT)==KEY_PUSH){
            SetCommonData("kakuseikinsi",false);
            ForbidShot(false);
            if(select==0){stage=1}
            else{stageselect=true;stage=select;ExtendPlayer(6);}
         }
      }
      if(stage==1){Stage1;}
      if(stage==2){Stage2;}
      if(stage==3){Stage3;}


      time++;
      if(stage>0){rankupcount++;}

      if(OnPlayerMissed==true){
         DeleteEnemyShotImmediatelyInCircle(ALL,224,248,500);
         if(misssyori==false){
            SetCommonData("prerank",0);
            loop(4){
               CreateEnemyFromFile(GetCurrentScriptDirectory~"airitem.txt",GetPlayerX,GetPlayerY-30, -5, rand(-3,3), 2);
            }
            SetCommonData("power",0);
            loop(GetCommonData("option")){
               CreateEnemyFromFile(GetCurrentScriptDirectory~"airitem.txt",GetPlayerX,GetPlayerY-30, -5, rand(-3,3), 3);
            }
            SetCommonData("option",0);
            misssyori=true;
         }
      }
      else{misssyori=false;}


      if(GetScore>=(2000000*extend)&&clear==false&&stageselect==false){
         if(GetPlayerLife<6){ExtendPlayer(1);}
         PlaySE("se1UP.wav");extend++;
      }
      if(GetMissCount>misscount){
         SetCommonData("bombchip",GetCommonData("bombchip")+20);
         if(GetCommonData("bombchip")>100){SetCommonData("bombchip",100);}
         misscount++;
      }

      if(rankupcount>=60){SetCommonData("prerank",GetCommonData("prerank")+0.05);rankupcount=0;}
      if(GetCommonData("prerank")>4){SetCommonData("prerank",4);}
      SetCommonData("rank",GetCommonData("prerank"));
      if(GetCommonData("prerank")>=4){SetCommonData("rank",5);}


      if(GetCommonData("gekitui")==1&&GetTimeOfPlayerInvincibility==0){
         ShootDownPlayer;
      }
      SetCommonData("gekitui",0);
      yield;
   }
   @Finalize(){}
   @BackGround{
      if(stage==1){
         if(phase==1){
            if(time>3000){
               SetTexture( GetCurrentScriptDirectory~"img\st1bg2.png" );
               SetGraphicRect(0,-GetCommonData("bgy")/2,448,-GetCommonData("bgy")/2+480);
               DrawGraphic(224,240);
            }
            if(time<3600){
               SetTexture( GetCurrentScriptDirectory~"img\st1bg1.png" );
               SetGraphicRect(GetCommonData("bgx"),2760-GetCommonData("bgy"),GetCommonData("bgx")+384,2760-GetCommonData("bgy")+448);
               DrawGraphic(224,240);
            }
         }
      }
      if(stage==2){
            SetTexture( GetCurrentScriptDirectory~"img\st2bg1.png" );
            SetGraphicRect(GetCommonData("bgx"),2760-GetCommonData("bgy"),GetCommonData("bgx")+384,2760-GetCommonData("bgy")+448);
            DrawGraphic(224,240);
      }
      if(stage==3){
         if(phase<=1){
            SetTexture( GetCurrentScriptDirectory~"img\st3bg1.png" );
            SetGraphicRect(GetCommonData("bgx"),2760-GetCommonData("bgy"),GetCommonData("bgx")+384,2760-GetCommonData("bgy")+448);
            DrawGraphic(224,240);
         }
         if(phase==2){
            SetTexture( GetCurrentScriptDirectory~"img\st3bg2.png" );
            SetGraphicRect(GetCommonData("bgx"),2760-GetCommonData("bgy"),GetCommonData("bgx")+384,2760-GetCommonData("bgy")+448);
            DrawGraphic(224,240);
         }
         if(phase==3){
            SetTexture( GetCurrentScriptDirectory~"img\st3bg3.png" );
            SetGraphicRect(GetCommonData("bgx"),2880-GetCommonData("bgy"),GetCommonData("bgx")+384,2880-GetCommonData("bgy")+450);
            DrawGraphic(224,240);

            if(time>2500&&time<=3400){
            SetTexture( GetCurrentScriptDirectory~"img\st3boss.png" );
            SetGraphicRect(0,0,256,320);
            DrawGraphic(224,GetCommonData("bgy")-2640);

            SetGraphicRect( 512, 64, 800, 352 );
            SetGraphicAngle(0,0,90-50);
	    DrawGraphic( 224+65, GetCommonData("bgy")-2640-70 );
            SetGraphicAngle(180,0,90-50);
	    DrawGraphic( 224-65, GetCommonData("bgy")-2640-70 );
            SetGraphicAngle(0,0,0);

            }
         }
         if(phase==4){
            if(time>780){
               SetGraphicScale(0.5,0.5);
               SetTexture( GetCurrentScriptDirectory~"img\st3bg6.png" );
               SetGraphicRect(0,-GetCommonData("bgy")/4,896,-GetCommonData("bgy")/4+960);
               DrawGraphic(224,240);
               SetGraphicScale(1,1);
               SetTexture( GetCurrentScriptDirectory~"img\st3bg5.png" );
               SetGraphicRect(0,0-GetCommonData("bgy")/2,384,480-GetCommonData("bgy")/2);
               DrawGraphic(224,240);
            }
            if(time<1650){
               SetTexture( GetCurrentScriptDirectory~"img\st3bg4.png" );
               SetGraphicRect(GetCommonData("bgx"),2880-GetCommonData("bgy"),GetCommonData("bgx")+384,2880-GetCommonData("bgy")+450);
               DrawGraphic(224,240);
            }
         }
      }
   }
   @DrawTopObject{
        /*let a=GetCommonData("rank");
        DrawText(""~ToString(a)~"",150,20,16,255);
        let a=GetCommonData("power");
        DrawText(""~ToString(a)~"",150,40,16,255);
        let a=GetCommonData("option");
        DrawText(""~ToString(a)~"",150,60,16,255);
        let a=GetCommonData("bombchip");
        DrawText(""~ToString(a)~"",150,80,16,255);*/

        /*let a=time;
        DrawText(""~ToString(a)~"",60,60,16,255);
        let a=GetCommonData("kinkaiten");
        DrawText(""~ToString(a)~"",60,80,16,255);*/

        let a=GetCommonData("prerank");
	SetTexture( system );
        if(a>=4){SetColor(205+sin(time*70)*50,205+sin(time*70)*50,205+sin(time*70)*50);}
        else{SetColor(230,230,230);}
        SetGraphicRect( 16+80-a*20, 32, 16+160-a*20, 49 );
	DrawGraphic( 224, 49 );
        SetFontColor(255, 255, 255, 255, 255, 255);
        SetColor(255,255,255);
        SetGraphicRect( 0, 48, 112, 80 );
	DrawGraphic( 224, 40 );

        if(GetPlayerX>224-120||GetPlayerY<410){
           SetGraphicRect( 0, 0, 32, 32 );
           SetGraphicScale(0.7,0.7);
	   DrawGraphic( 224-170, 440 );
           let a=GetCommonData("bombchip");
           DrawText(""~RealToIntString(a)~"",224-153,433,16,255);
        }
        SetGraphicScale(1.0,1.0);

        if(clearhyouzi>0){
           DrawText("STAGE CLEAR",clearhyouzi-240,120,40,255);
           let a=GetCommonData("ruikei")*1000;
           DrawText("BONUS  "~RealToIntString(a)~" pts",-clearhyouzi+448+50,250,20,255);
        }
        if(stage==0){
           SetTexture( GetCurrentScriptDirectory~"img\title.png" );
           SetGraphicRect(0,0,384,480);
           DrawGraphic(224,240);
           SetGraphicRect(0,481,384,511);
           DrawGraphic(224,223+32*select);
           if(GetCommonData("autobomb")==true){SetGraphicRect(0,513,384,543);}
           else{SetGraphicRect(0,513+32,384,543+32);}
           DrawGraphic(224,380);
           if(GetCommonData("ex")==1){
              SetGraphicRect(0,481+96,384,511+96);
              DrawGraphic(224,150);
           }
        }
   }




   #include_function".\sfanc.txt"
   #include_function".\Stage1.txt"
   #include_function".\Stage2.txt"
   #include_function".\Stage3.txt"
}
