#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Legacy of Bossrush Kingdom (with settings)"]
#Text["By Lushie/Lusushime"]

#System[".\default_system/Default_System.txt"]
#Player[".\player/LoLK Marisa (Lushie)/MarisaH.dnh"]
#include "./BackgroundTasks.dnh"
#include "./MusicPause.dnh"

let bgobj = ObjSound_Create();
let bgv = 100;

@Event{
    alternative(GetEventType())
    case(EV_PLAYER_SHOOTDOWN)
    {
        deathn += 1;
        cdeathn += 1;
        ObjText_SetText(ftext, vtos("00d", deathn));
        ObjText_SetText(ctext, vtos("00d", cdeathn));
    }
    case(EV_PAUSE_ENTER){
        bgv = ObjSound_GetVolumeRate(bgobj);
        pausemusic(bgobj, bgv);
    }
    case(EV_PAUSE_LEAVE){
        resumemusic(bgobj, 100);
    }
}

@Loading{
    LoadTexture(dir ~ "./img/boss1.png");
    LoadTexture(dir ~ "./img/boss2.png");
    LoadTexture(dir ~ "./img/boss3.png");
    LoadTexture(dir ~ "./img/boss4.png");
    LoadTexture(dir ~ "./img/boss5.png");
    LoadTexture(dir ~ "./img/boss6.png");
    LoadTexture(dir ~ "./img/boss7.png");

    LoadTexture(dir ~ "./img/bosspanels.png");
    LoadTexture(dir ~ "./img/bosspanels2.png");
    LoadTexture(dir ~ "./img/enemies.png");
    LoadTexture(dir ~ "./img/font1.png");
    LoadTexture(dir ~ "./img/front2.png");
    LoadTexture(dir ~ "./img/junkoaura.png");
    LoadTexture(dir ~ "./img/lifebar.png");
    LoadTexture(dir ~ "./img/maple.png");
    LoadTexture(dir ~ "./img/mcircle.png");
    LoadTexture(dir ~ "./img/pdbg.png");
    LoadTexture(dir ~ "./img/pdtext.png");
    LoadTexture(dir ~ "./img/SpellAttack.png");
    LoadTexture(dir ~ "./img/transparent.png");
    LoadTexture(dir ~ "./img/TransparentCircle.png");
    LoadTexture(dir ~ "./img/white.png");
    LoadTexture(dir ~ "./img/whitecircle.png");

    LoadTexture(dir ~ "./img/clownpiecebg1.png");
    LoadTexture(dir ~ "./img/clownpiecebg2.png");
    LoadTexture(dir ~ "./img/doremybg1.png");
    LoadTexture(dir ~ "./img/doremybg2.png");
    LoadTexture(dir ~ "./img/hecatiabg2.png");
    LoadTexture(dir ~ "./img/junkobg1.png");
    LoadTexture(dir ~ "./img/junkobg2.png");
    LoadTexture(dir ~ "./img/ringobg1.png");
    LoadTexture(dir ~ "./img/ringobg2.png");
    LoadTexture(dir ~ "./img/sagumebg1.png");
    LoadTexture(dir ~ "./img/sagumebg2.png");
    LoadTexture(dir ~ "./img/seiranbg1.png");
    LoadTexture(dir ~ "./img/seiranbg2.png");

    LoadTexture(dir ~ "./img/cutin1.png");
    LoadTexture(dir ~ "./img/cutin2.png");
    LoadTexture(dir ~ "./img/cutin3.png");
    LoadTexture(dir ~ "./img/cutin4.png");
    LoadTexture(dir ~ "./img/cutin5.png");
    LoadTexture(dir ~ "./img/cutin6.png");
    LoadTexture(dir ~ "./img/cutin7a.png");
    LoadTexture(dir ~ "./img/cutin7b.png");
    LoadTexture(dir ~ "./img/cutin7c.png");

    LoadTexture(dir ~ "./img/stage1bg1.png");
    LoadTexture(dir ~ "./img/stage1bg2.png");
    LoadTexture(dir ~ "./img/stage2bg1.png");
    LoadTexture(dir ~ "./img/stage2bg2.png");
    LoadTexture(dir ~ "./img/stage2bg3.png");
    LoadTexture(dir ~ "./img/stage3bg1.png");
    LoadTexture(dir ~ "./img/stage3bg2.png");
    LoadTexture(dir ~ "./img/stage3bg3.png");
    LoadTexture(dir ~ "./img/stage3bg4.png");
    LoadTexture(dir ~ "./img/stage3bg5.png");
    LoadTexture(dir ~ "./img/stage4bg1.png");
    LoadTexture(dir ~ "./img/stage4bg2.png");
    LoadTexture(dir ~ "./img/stage4bg3.png");
    LoadTexture(dir ~ "./img/stage4bg4.png");
    LoadTexture(dir ~ "./img/stage5bg1.png");
    LoadTexture(dir ~ "./img/stage5bg2.png");
    LoadTexture(dir ~ "./img/stage5bg3.png");
    LoadTexture(dir ~ "./img/stage6bg1.png");
    LoadTexture(dir ~ "./img/stage6bg2.png");
    LoadTexture(dir ~ "./img/stage6bg3.png");
    LoadTexture(dir ~ "./img/stage7bg3.png");
    LoadTexture(dir ~ "./img/stage7bg5.png");

    LoadScript(dir ~ "./SeiranStart.dnh");
    LoadScript(dir ~ "./11SeiranNon1.dnh");
    LoadScript(dir ~ "./11SeiranSpell1.dnh");
    LoadScript(dir ~ "./12SeiranNon2.dnh");
    LoadScript(dir ~ "./12SeiranSpell2.dnh");
    LoadScript(dir ~ "./RingoStart.dnh");
    LoadScript(dir ~ "./21RingoNon1.dnh");
    LoadScript(dir ~ "./21RingoSpell1.dnh");
    LoadScript(dir ~ "./22RingoNon2.dnh");
    LoadScript(dir ~ "./22RingoSpell2.dnh");
    LoadScript(dir ~ "./DoremyStart.dnh");
    LoadScript(dir ~ "./31DoremyNon1.dnh");
    LoadScript(dir ~ "./31DoremySpell1.dnh");
    LoadScript(dir ~ "./32DoremyNon2.dnh");
    LoadScript(dir ~ "./32DoremySpell2.dnh");
    LoadScript(dir ~ "./33DoremySpell3.dnh");
    LoadScript(dir ~ "./SagumeStart.dnh");
    LoadScript(dir ~ "./41SagumeNon1.dnh");
    LoadScript(dir ~ "./41SagumeSpell1.dnh");
    LoadScript(dir ~ "./42SagumeNon2.dnh");
    LoadScript(dir ~ "./42SagumeSpell2.dnh");
    LoadScript(dir ~ "./43SagumeSpell3.dnh");
    LoadScript(dir ~ "./ClownpieceStart.dnh");
    LoadScript(dir ~ "./51ClownpieceNon1.dnh");
    LoadScript(dir ~ "./51ClownpieceSpell1.dnh");
    LoadScript(dir ~ "./52ClownpieceNon2.dnh");
    LoadScript(dir ~ "./52ClownpieceSpell2.dnh");
    LoadScript(dir ~ "./53ClownpieceNon3.dnh");
    LoadScript(dir ~ "./53ClownpieceSpell3.dnh");
    LoadScript(dir ~ "./JunkoStart.dnh");
    LoadScript(dir ~ "./61JunkoNon1.dnh");
    LoadScript(dir ~ "./61JunkoSpell1.dnh");
    LoadScript(dir ~ "./62JunkoNon2.dnh");
    LoadScript(dir ~ "./62JunkoSpell2.dnh");
    LoadScript(dir ~ "./63JunkoNon3.dnh");
    LoadScript(dir ~ "./63JunkoSpell3.dnh");
    LoadScript(dir ~ "./HecatiaStart.dnh");
    LoadScript(dir ~ "./71HecatiaNon1.dnh");
    LoadScript(dir ~ "./71HecatiaSpell1.dnh");
    LoadScript(dir ~ "./72HecatiaNon2.dnh");
    LoadScript(dir ~ "./72HecatiaSpell2.dnh");
    LoadScript(dir ~ "./73HecatiaNon3.dnh");
    LoadScript(dir ~ "./73HecatiaSpell3.dnh");
    LoadScript(dir ~ "./74HecatiaSpell4.dnh");


}
@Initialize{
    TPlural;
}

@MainLoop{
    yield;
}

let dir = GetCurrentScriptDirectory();

let bSpell = false;
let objScene;

task TPlural{
    
    let obj;
    let vt = 90;
    let tt = 50;
    let tt2 = 0;
    let tt3 = tt+tt2;
    SetPlayerLife(99999);
    Stage1BG;
    bosspanels;
    deathcounter(0);

    obj = ObjEnemyBossScene_Create();
    ObjEnemyBossScene_Add(obj, 0, dir ~ "./.Difficulty.dnh");
    ObjEnemyBossScene_LoadInThread(obj);
    ObjEnemyBossScene_Regist(obj);
    objScene = GetEnemyBossSceneObjectID();
    while(!Obj_IsDeleted(obj)){
        yield;
    }

    LoadCommonDataAreaA2("mode", GetCurrentScriptDirectory() ~ "data/Difficulty.dat");
    let datapath = GetCurrentScriptDirectory() ~ "data/Difficulty.dat";
    LoadCommonDataAreaA2("mode", datapath);
    let playerspeed =  GetAreaCommonData("mode", "speed", 0);
    SetAreaCommonData("mode", "start", 1); 
    if(playerspeed == 0){SetPlayerSpeed(5, 2);}
    else{SetPlayerSpeed(4.5, 2);}

    //SetAreaCommonData("mode", "difficulty", 2); 
    TMode;

    
    deathcounter(1);
    bossfight("seiran");

    bgtransition("black");
    wait(tt3);
    Stage2BG;
    deathcounter(2);
    bossfight("ringo");

    bgtransition("black");
    wait(tt3);
    Stage3BG;
    deathcounter(3);
    bossfight("doremy");

    bgtransition("black");
    wait(tt3);
    Stage4BG;
    deathcounter(4);
    bossfight("sagume");

    bgtransition("black");
    wait(tt3);
    Stage5BG;
    deathcounter(5);
    bossfight("clownpiece");

    bgtransition("black");
    wait(tt3);
    Stage6BG;
    deathcounter(6);
    bossfight("junko");

    bgtransition("black");
    wait(tt3);
    Stage7BG;
    deathcounter(7);
    bossfight("hecatia");

    bSpell = false;
    //loop(99999){
        yield;
    //}
    wait(200);
    CloseStgScene();

    function bossfight(name){
        SetAreaCommonData("mode", "start", 0); 
        LoadCommonDataAreaA2("mode", GetCurrentScriptDirectory() ~ "data/Difficulty.dat");

        let v = 100;
        Obj_Delete(bgobj);
        bgobj = ObjSound_Create();
        obj = ObjEnemyBossScene_Create();
       
        if(name == "seiran"){
            TBossName(obj, 0);
            ObjSound_Load(bgobj, dir ~ "./se/boss1.mp3");
            ObjEnemyBossScene_Add(obj, 0, dir ~ "./SeiranStart.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./11SeiranNon1.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./11SeiranSpell1.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./12SeiranNon2.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./12SeiranSpell2.dnh");
            
        }
         else if(name =="ringo"){
            TBossName(obj, 1);
            ObjSound_Load(bgobj, dir ~ "./se/boss2.mp3");
            ObjEnemyBossScene_Add(obj, 0, dir ~ "./RingoStart.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./21RingoNon1.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./21RingoSpell1.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./22RingoNon2.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./22RingoSpell2.dnh");
        }
        else if(name =="doremy"){
            TBossName(obj, 2);
            ObjSound_Load(bgobj, dir ~ "./se/boss3.mp3");
            ObjEnemyBossScene_Add(obj, 0, dir ~ "./DoremyStart.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./31DoremyNon1.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./31DoremySpell1.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./32DoremyNon2.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./32DoremySpell2.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./33DoremySpell3.dnh");
        }
        else if(name =="sagume"){
            TBossName(obj, 3);
            ObjSound_Load(bgobj, dir ~ "./se/boss4.mp3");
            ObjEnemyBossScene_Add(obj, 0, dir ~ "./SagumeStart.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./41SagumeNon1.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./41SagumeSpell1.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./42SagumeNon2.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./42SagumeSpell2.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./43SagumeSpell3.dnh");
        }
        else if(name =="clownpiece"){
            TBossName(obj, 4);
            ObjSound_Load(bgobj, dir ~ "./se/boss5.mp3");
            ObjEnemyBossScene_Add(obj, 0, dir ~ "./ClownpieceStart.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./51ClownpieceNon1.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./51ClownpieceSpell1.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./52ClownpieceNon2.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./52ClownpieceSpell2.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./53ClownpieceNon3.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./53ClownpieceSpell3.dnh");
        }
        else if(name =="junko"){
            TBossName(obj, 5);
            ObjSound_Load(bgobj, dir ~ "./se/boss6.mp3");
            ObjEnemyBossScene_Add(obj, 0, dir ~ "./JunkoStart.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./61JunkoNon1.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./61JunkoSpell1.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./62JunkoNon2.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./62JunkoSpell2.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./63JunkoNon3.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./63JunkoSpell3.dnh");
        }
        else if(name =="hecatia"){
            TBossName(obj, 6);
            ObjSound_Load(bgobj, dir ~ "./se/boss7.mp3");
            ObjEnemyBossScene_Add(obj, 0, dir ~ "./HecatiaStart.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./71HecatiaNon1.dnh");
            ObjEnemyBossScene_Add(obj, 1, dir ~ "./71HecatiaSpell1.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./72HecatiaNon2.dnh");
            ObjEnemyBossScene_Add(obj, 2, dir ~ "./72HecatiaSpell2.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./73HecatiaNon3.dnh");
            ObjEnemyBossScene_Add(obj, 3, dir ~ "./73HecatiaSpell3.dnh");
            ObjEnemyBossScene_Add(obj, 4, dir ~ "./74HecatiaSpell4.dnh");
        }
        ObjSound_SetLoopEnable(bgobj, true);
        ObjSound_SetLoopTime(bgobj, 0, 999999999);
        ObjSound_SetVolumeRate(bgobj, v);
        ObjSound_Play(bgobj);
        

        ObjEnemyBossScene_LoadInThread(obj);
        ObjEnemyBossScene_Regist(obj);
        objScene = GetEnemyBossSceneObjectID();
        while(!Obj_IsDeleted(obj)){
            if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
            {
                bSpell = true;
            }
            else
            {
                bSpell = false;
            }
            yield;
        }
        bSpell = false;

        let vm = v/vt;
        if(name != "hecatia"){
            loop(v){
                v -= vm;
                ObjSound_SetVolumeRate(bgobj, v);
                yield;
            }
        }
    }

    task bgtransition(color){
        let c = 0;
        if(color == "white"){c = 255;}
         let img = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(img, 27);
        ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/white.png");  
        ObjSprite2D_SetSourceRect(img, 0, 0, 512, 512);
        ObjSprite2D_SetDestCenter(img);
        ObjRender_SetPosition(img, 192, 224, 1); 
        ObjRender_SetColor(img, c, c, c);
        let alpha = 0;
        let ap = 255/tt;
        loop(tt){
            alpha += ap;
            ObjRender_SetAlpha(img, alpha);
            yield;
        }
        wait(tt2);
        loop(tt){
            alpha -= ap;
            ObjRender_SetAlpha(img, alpha);
            yield;
        }
        Obj_Delete(img);

    }
}

task bosspanels{
    let img = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriority(img, 0.1);
    ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/bosspanels.png");  
    ObjSprite2D_SetSourceRect(img, 0, 0, 417, 417);
    ObjSprite2D_SetDestCenter(img);
    ObjRender_SetScaleXYZ(img, 0.5*rez, 0.5*rez, 1);
    ObjRender_SetPosition(img, 528.5*rez, 150*rez, 1); 
}

task bosspanels2(c){
    let img = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriority(img, 0.1);
    ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/bosspanels2.png");  
    
    ObjRender_SetScaleXYZ(img, 0.5*rez, 0.5*rez, 1);
    let x =  456 + c * 72.5;
    let y = 77;
    let x1 = c * 145; 
    let y1 = 0;
    if(c > 2 && c < 6){
        x -= 3*72.5;
        y += 73;
        x1 -= 3*145;
        y1 += 145;
    }
    else if(c > 5){
        x -= 6*72.5-71.5/2;
        y += 73*2;
        if(c == 6){x1 = 72.5;}
        else{x1 = 216;}
        y1 += 290;
    }
    let x2 = x1 + 127;
    let y2 = y1 + 127;
    ObjSprite2D_SetSourceRect(img, x1, y1, x2, y2);
    ObjSprite2D_SetDestCenter(img);
    ObjRender_SetPosition(img, x*rez, y*rez, 1); 

}

let ftext;
let ctext;

task deathcounter(c){
    bosspanels2(c);
    let text = ObjText_Create;
    InstallFont(GetCurrentScriptDirectory~"Meiryo.ttc");
    ObjText_SetText(text, vtos("00d", 0));
    ObjText_SetFontType(text, "Meiryo");
    ObjText_SetFontSize(text,20*rez);
    ObjText_SetFontBorderType(text,BORDER_FULL);
    ObjText_SetFontBorderColor(text,32,32,32);
    ObjText_SetFontBorderWidth(text,1*rez);
    ObjText_SetHorizontalAlignment(text, ALIGNMENT_RIGHT);
    ObjText_SetMaxWidth(text, 50*rez);
    Obj_SetRenderPriority(text, 0.11);
    if(c != 0){cdeathn = 0; ctext = text;}
    if(c == 0){ftext = text;}
    let x =  435 + c * 73;
    if(c > 2 && c < 6){x -= 3*73;}
    else if(c > 5){x -= 6*73-73/2;}
    let y = 91;
    if(c > 2 && c < 6){y += 73;}
    else if(c > 5){y += 146;}

    ObjRender_SetPosition(text, x*rez, y*rez, 1);
}
let deathn = 0;
let cdeathn = 0;

task TBossName(obj, v){
    let diff = GetAreaCommonData("mode", "difficulty", 0);
    let img = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriority(img, 0.75);
    ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/ename.png");  
    ObjSprite2D_SetSourceRect(img, 0, v*32, 255, v*32+21);
    ObjRender_SetScaleXYZ(img, 0.7*rez, 0.7*rez, 1);
    ObjSprite2D_SetDestCenter(img);
    ObjRender_SetPosition(img, 90*rez, 10*rez, 1); 
    while(!Obj_IsDeleted(obj)){yield;}
    Obj_Delete(img);
}

task TMode(){
    let diff = GetAreaCommonData("mode", "difficulty", 0);;
    let img = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriority(img, 0.1);
    ObjPrim_SetTexture(img,GetCurrentScriptDirectory() ~ "img/font1.png");  
    ObjSprite2D_SetSourceRect(img, 528, 642, 659, 673);
    if(diff == 2){
        ObjSprite2D_SetSourceRect(img, 528, 674, 659, 705);
    }
    if(diff == 3){
        ObjSprite2D_SetSourceRect(img, 528, 706, 659, 737);
    }
    ObjRender_SetScaleXYZ(img, 0.75*rez, 0.75*rez, 1);
    ObjSprite2D_SetDestCenter(img);
    ObjRender_SetPosition(img, 528.5*rez, 25*rez, 1); 
    //834-960
    //30-75
}

let rez = GetScreenHeight()/480;