#include_function "script\Ikareimu\Stage.dnh"

task Stage1 {
  Wait(60);
  let bossMarisa = GetCurrentScriptDirectory~"\Boss\eMarisa\Marisa.dnh";
  let bossCirno = GetCurrentScriptDirectory~"\Boss\mCirno\Cirno.dnh";
  let stageMusic = GetCurrentScriptDirectory~"\bgm\stage.mp3";
  let bossMusic = GetCurrentScriptDirectory~"\bgm\boss.mp3";
  let enemyRedFairy1 = GetCurrentScriptDirectory~"\Enemy\Stage1\RedFairy1.dnh";
  let enemyRedFairy2 = GetCurrentScriptDirectory~"\Enemy\Stage1\RedFairy2.dnh";
  let enemyRedFairy3 = GetCurrentScriptDirectory~"\Enemy\Stage1\RedFairy3.dnh";
  let enemyBlueFairy1 = GetCurrentScriptDirectory~"\Enemy\Stage1\BlueFairy1.dnh";
  let enemyBlueFairy2 = GetCurrentScriptDirectory~"\Enemy\Stage1\BlueFairy2.dnh";
  let enemyBlueFairy3 = GetCurrentScriptDirectory~"\Enemy\Stage1\BlueFairy3.dnh";
  let enemyCirno = GetCurrentScriptDirectory~"\Enemy\Stage1\Cirno.dnh";
  
  //Background
  bgspeed = 5;
  LoadGraphic("script\Ikareimu\img\stagebg\stage1floor.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\stagebg\stage1backdrop.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\stagebg\stage1treetrunk.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\stagebg\stage1treeleaves.png");
  yield ;
  
  //Enemy graphics
  LoadGraphic("script\Ikareimu\img\sprite\FairyRedSmall.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\sprite\FairyRedLarge.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\sprite\FairyBlueSmall.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\sprite\FairyBlueLarge.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\sprite\MagicCircleRed.png");
  yield ;
  LoadGraphic("script\Ikareimu\img\sprite\MagicCircleBlue.png");
  yield ;
  
  //Warning graphics
  LoadGraphic(warningText);
  yield ;
  LoadGraphic(warningBox);
  yield ;
  
  //Music
  LoadMusic(stageMusic);
  yield ;
  LoadMusic(bossMusic);
  yield ;
  
  //Enemy scripts
  CompileEnemyFromFile(enemyRedFairy1);
  yield ;
  CompileEnemyFromFile(enemyRedFairy2);
  yield ;
  CompileEnemyFromFile(enemyRedFairy3);
  yield ;
  CompileEnemyFromFile(enemyBlueFairy1);
  yield ;
  CompileEnemyFromFile(enemyBlueFairy2);
  yield ;
  CompileEnemyFromFile(enemyBlueFairy3);
  yield ;
  CompileEnemyFromFile(enemyCirno);
  
  ResetGameState;
  DeleteLoadingScreen;
  
  Wait(120);  
  PlayMusic(stageMusic);
  Wait(60);
  
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyRedFairy1, GetClipMaxX-50, GetClipMinY-20-i*10, 0, 0, 0);
    Wait(10);
    CreateEnemyFromFile(enemyRedFairy1, GetClipMaxX-100, GetClipMinY-20-i*20, 0, 0, 0);
    Wait(10);
  }

  Wait(90);
  
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyBlueFairy1, GetClipMinX+50, GetClipMinY-20-i*10, 0, 0, 0);
    Wait(10);
    CreateEnemyFromFile(enemyBlueFairy1, GetClipMinX+100, GetClipMinY-20-i*20, 0, 0, 0);
    Wait(10);
  }
  
  Wait(90);
  
  CreateEnemyFromFile(enemyRedFairy2, GetCenterX-50, GetClipMinY-100, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy2, GetCenterX+50, GetClipMinY-100, 0, 0, 0);		
  Wait(120);
  ascent(i in 0..2){
    CreateEnemyFromFile(enemyRedFairy1, GetCenterX-75+i*50, GetClipMinY-30, 0, 0, 0);
    CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+75-i*50, GetClipMinY-30, 0, 0, 0);
  }
  
  Wait(240);
  
  CreateEnemyFromFile(enemyRedFairy2, GetCenterX-50, GetClipMinY-100, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy2, GetCenterX+50, GetClipMinY-100, 0, 0, 0);		
  Wait(120);
  ascent(i in 0..2){
    CreateEnemyFromFile(enemyRedFairy1, GetCenterX+75+i*25, GetClipMinY-30, 0, 0, 0);
    CreateEnemyFromFile(enemyBlueFairy1, GetCenterX-75-i*25, GetClipMinY-30, 0, 0, 0);
  }
  
  Wait(300);
  
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyBlueFairy1, GetCenterX-30-i*30, GetClipMinY-30, 0, 0, 0);
    CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+30+i*30, GetClipMinY-30, 0, 0, 0);
    Wait(8);
  }
  CreateEnemyFromFile(enemyRedFairy1, GetCenterX+120, GetClipMinY-30, 0, 0, 0);
  CreateEnemyFromFile(enemyRedFairy1, GetCenterX-120, GetClipMinY-30, 0, 0, 0);
  Wait(90);
  CreateEnemyFromFile(enemyRedFairy3, GetCenterX, GetClipMinY-100, 0, 0, 0);
  
  Wait(300);
  
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyRedFairy1, GetCenterX-30-i*30, GetClipMinY-30, 0, 0, 0);
    CreateEnemyFromFile(enemyRedFairy1, GetCenterX+30+i*30, GetClipMinY-30, 0, 0, 0);
    Wait(8);
  }
  CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+120, GetClipMinY-30, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy1, GetCenterX-120, GetClipMinY-30, 0, 0, 0);
  Wait(90);
  CreateEnemyFromFile(enemyBlueFairy3, GetCenterX, GetClipMinY-100, 0, 0, 0);
  
  Wait(300);
  CreateEnemyFromFile(enemyRedFairy1, GetCenterX-40, GetClipMinY-30, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+40, GetClipMinY-30, 0, 0, 0);
  Wait(20);
  CreateEnemyFromFile(enemyRedFairy1, GetCenterX-20, GetClipMinY-30, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+20, GetClipMinY-30, 0, 0, 0);
  Wait(20);
  CreateEnemyFromFile(enemyRedFairy3, GetCenterX-30, GetClipMinY-100, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy3, GetCenterX+30, GetClipMinY-100, 0, 0, 0);
  
  Wait(300);

  CreateEnemyBossFromFile(bossCirno, GetClipMaxX+100, GetCenterY, 0, 0, 0);
  WaitForZeroEnemy;
  local {
    let BossPosition = GetCommonData("BossPosition");
    CreateEnemyFromFile(enemyCirno, BossPosition[0], BossPosition[1], 0, 0, 0);
    DeleteCommonData("BossPosition");
  }
  while(OnEvent){yield;}
  
  Wait(30);
  
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyRedFairy1, GetClipMinX+50+i*50, GetClipMinY-30, 0, 0, 0);
    Wait(30);
  }
  Wait(30);
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyRedFairy1, GetClipMaxX-50-i*50, GetClipMinY-30, 0, 0, 0);
    Wait(30);
  }
  
  Wait(60);
  
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyRedFairy1, GetCenterX-180+i*60, GetClipMinY, 0, 0, 0);
    Wait(15);
    CreateEnemyFromFile(enemyBlueFairy1, GetCenterX-150+i*60, GetClipMinY-30, 0, 0, 0);
    Wait(15);
  }
  
  Wait(60);
  
  ascent(i in 0..3){
    CreateEnemyFromFile(enemyRedFairy1, GetCenterX+180-i*60, GetClipMinY, 0, 0, 0);
    Wait(15);
    CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+150-i*60, GetClipMinY-30, 0, 0, 0);
    Wait(15);
  }
  
  Wait(180);
  
  CreateEnemyFromFile(enemyRedFairy2, GetCenterX, GetClipMinY-50, 0, 0, 0);
  CreateEnemyFromFile(enemyRedFairy2, GetCenterX-30, GetClipMinY-80, 0, 0, 0);
  CreateEnemyFromFile(enemyRedFairy2, GetCenterX+30, GetClipMinY-80, 0, 0, 0);
  Wait(30);
  CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+1, GetClipMinY, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy1, GetCenterX-50, GetClipMinY, 0, 0, 0);
  CreateEnemyFromFile(enemyBlueFairy1, GetCenterX+50, GetClipMinY, 0, 0, 0);
  
  Wait(240);
  
  CreateEnemyFromFile(enemyRedFairy3, GetCenterX, GetClipMinY-75, 0, 0, 0);
  CreateEnemyFromFile(enemyRedFairy2, GetCenterX+30, GetClipMinY-80, 0, 0, 0);
  CreateEnemyFromFile(enemyRedFairy2, GetCenterX-30, GetClipMinY-80, 0, 0, 0);
  
  Wait(420);
  
  DeleteMusic(stageMusic);
  Warning;
  Wait(90);
  bgshake = true;
  Wait(200);
  bgshake = false;
  Wait(30);
  
  CreateEnemyBossFromFile(bossMarisa, GetClipMaxX+100, GetCenterY, 0, 0, 0);
  Wait(60);
  PlayMusic(bossMusic);
  
  WaitForZeroEnemy;
  Wait(240);
  FadeOutMusic(bossMusic, 60);
  Wait(60);

  DeleteGraphic("script\Ikareimu\img\sprite\FairyRedSmall.png");
  DeleteGraphic("script\Ikareimu\img\sprite\FairyRedLarge.png");
  DeleteGraphic("script\Ikareimu\img\sprite\FairyBlueSmall.png");
  DeleteGraphic("script\Ikareimu\img\sprite\FairyBlueLarge.png");
  DeleteGraphic("script\Ikareimu\img\sprite\MagicCircleRed.png");
  DeleteGraphic("script\Ikareimu\img\sprite\MagicCircleBlue.png");
  DeleteGraphic("script\Ikareimu\img\stagebg\stage1floor.png");
  DeleteGraphic("script\Ikareimu\img\stagebg\stage1backdrop.png");
  DeleteGraphic("script\Ikareimu\img\stagebg\stage1treetrunk.png");
  DeleteGraphic("script\Ikareimu\img\stagebg\stage1treeleaves.png");
  DeleteGraphic(warningText);
  DeleteGraphic(warningBox);
  DeleteMusic(bossMusic);
  ClearByteCodeCash;
  
  // Don't bother uncommenting these, there's nothing in the other stages anyway.
  /*if(GetCommonData("gamemode")=="start"){
    SetCommonData("gameevent", "stage2");
    return ;
  }
  else if(GetCommonData("gamemode")=="practiceStart"){*/
    SetCommonData("gameevent", "clear");
    return ;
  /*}
  else {
    RaiseError("CommonData [gamemode] is not saving correctly.", "Ikareimu Bug");
  }*/
}

sub Stage1BG {
  if(GetCommonData("gamestate")=="stage1"){
    let floor = "script\Ikareimu\img\stagebg\stage1floor.png";
    let backdrop = "script\Ikareimu\img\stagebg\stage1backdrop.png";
    let treetrunk = "script\Ikareimu\img\stagebg\stage1treetrunk.png";
    let treeleaves = "script\Ikareimu\img\stagebg\stage1treeleaves.png";
    
    let x = 0;
    let z = 0;
    if(bgshake==true){
      x = rand(-10, 10);
      z = rand(-10, 10);
    }

    if(bgcounter<=-1024){
      bgcounter += 1024;
    }
    
    UseZBuffer(true);
    WriteZBuffer(true);
    SetFog(900, 1480, 100, 100, 100);
    
    SetTexture(floor);
    SetGraphicRect(0, bgcounter*2, 2048, 4000+bgcounter*2);
    SetGraphicScale(0.5, 0.5);
    SetGraphicAngle(90, 0, 0);
    DrawGraphic3D(x, 0, z+500);
    
    SetTexture(backdrop);
    SetGraphicRect(0, 0, 1024, 317);
    SetGraphicScale(1.3, 1);
    SetGraphicAngle(180, 0, 0);
    DrawGraphic3D(x, 150, z-550);
    
    ascent(i in -3..2){
      DrawTree(100,  512*i+0-bgcounter);
      DrawTree(-80,  512*i+64-bgcounter);
      DrawTree(280,  512*i+128-bgcounter);
      DrawTree(-160, 512*i+192-bgcounter);
      DrawTree(70,   512*i+256-bgcounter);
      DrawTree(-130, 512*i+320-bgcounter);
      DrawTree(140,  512*i+384-bgcounter);
      DrawTree(-270, 512*i+448-bgcounter);
    }
    
    SetViewTo(0, -150, 0);
    SetViewFrom(1000, 90, 20);
    
    function DrawTree(treex, treez){
      if(treez<900&&treez>-1000){
        SetTexture(treetrunk);
        SetGraphicScale(0.5, 0.5);
        SetGraphicAngle(180, -60, 0);
        SetGraphicRect(0, 0, 32, 1000);
        DrawGraphic3D(x+treex-11, 0, z+treez-5);
        SetGraphicRect(32, 0, 64, 1000);
        SetGraphicAngle(180, 0, 0);
        DrawGraphic3D(x+treex, 0, z+treez);
        SetGraphicRect(64, 0, 96, 1000);
        SetGraphicAngle(180, 60, 0);
        DrawGraphic3D(x+treex+11, 0, z+treez-5);
        
        SetTexture(treeleaves);
        if(treez>645){
          SetAlpha(900-treez);
        }
        SetGraphicRect(0, 0, 370, 338);
        SetGraphicScale(0.5, 0.5);
        SetGraphicAngle(180, -60, 0);
        DrawGraphic3D(x+treex+cos(-60)*15, 240, z+treez+cos(-60)*15);
        SetGraphicAngle(180, 0, 0);
        DrawGraphic3D(x+treex, 240, z+treez+15);
        SetGraphicAngle(180, 60, 0);
        DrawGraphic3D(x+treex+cos(60)*15, 240, z+treez+cos(60)*15);
        SetAlpha(255);
      }
    }
  }
}