let RD_EndFlame = 0;
let RD_EndOneShot = false;
var RD初回クリアFlg = false;
Game_Interpreter.prototype.RD_RDEnd_Cng = function () {
    RD_EndFlame = 0;
    RD_Mode = EnRDMode.RD_End;
    RD_EndOneShot = false; 
    $gamePlayer.setTransparent(true); 
    this._味方HP_維持終了(); 
    this.DelPictSpan(PN_CHikari_RD , PN_Card_RD + 10);
    this.DelSprite(PN_BDeckBtn);
    this.DelSpriteSpan(PN_rbBossBase , PN_rbBossCnt);
    RD初回クリアFlg = false;
}
Game_Interpreter.prototype.RD_PlayerDown = function () {
    let _SetX = GetSpriteData(PN_rdPlayer)._x;
    let _SetY = GetSpriteData(PN_rdPlayer)._y;
    this.SetSpriteC(PN_rdPlayer , STY_MovePic_down , _SetX , _SetY , 255);
}
let Check未クリアHPブーストDungeonCnt3章 = function () {
    let ckCount = 0;
    if (!DgnClearCheck(5)) {
        $gameSystem.ClearDngn.push(5); 
        ckCount++; 
    }
    if (!DgnClearCheck(8)) {
        $gameSystem.ClearDngn.push(8); 
        ckCount++;
    } 
    if (!DgnClearCheck(22)) {
        $gameSystem.ClearDngn.push(22); 
        ckCount++; 
    }
    if (!DgnClearCheck(25)) {
        $gameSystem.ClearDngn.push(25); 
        ckCount++; 
    }
    return ckCount;
}
let ResetBtlMaxHpUp = function () {
    RD_CngMaxHpFunc(-RDBtlSettingData.Btl_MaxHpUp);
    RDBtlSettingData.Btl_MaxHpUp = 0;
}
let rdEndFlg = false;
Game_Interpreter.prototype.RD_RD_End_Update = function () {
    if (rdEndFlg) return;
    if (rd_rtnFlame > 0) {
        this.RD_End_ReturnUpdate();
        return;
    }
    //this.RD_MapLineDraw(true, 0); //マップ初期表示
    ResetBtlMaxHpUp();
    RD_戦闘勝利行動数増加量Reset(); 
    switch (RD_ClearMode) {
        case EnRDClearMode.Clear: 
            $gameSwitches.setValue(SW_rdClear, true);
            if(!DgnClearCheck(RD_Id)) {
                $gameSystem.ClearDngn.push(RD_Id);
                RD初回クリアFlg = true;
            }
            $gameSwitches.setValue(SW_rdTettaiFlg, false);
            this.RD_RD_End_WinUpdate(); 
            break;
        case EnRDClearMode.Faild: 
            this.RD_PlayerDown(); 
            this.DelSprite(PN_rdBTalkBack); 
            $gameSwitches.setValue(SW_rdClear, false);
            $gameSwitches.setValue(SW_rdTettaiFlg, false);
            this.RD_RD_End_LoseUpdate(); 
            break;
        case EnRDClearMode.Tettai: 
            $gameSwitches.setValue(SW_rdTettaiFlg, true);
            this.RD_RD_End_TettaiUpdate();
            break;
        default:
            $gameSwitches.setValue(SW_rdTettaiFlg, true);
            $gameSwitches.setValue(SW_rdClear, false);
            this.RD_RD_End_NumUpdate();
    }
    RD_EndFlame++;
}
Game_Interpreter.prototype.RD_RD_End_NumUpdate = function () {
    if (RD_EndFlame == 1) { 
        AudioManager.fadeOutBgm(1);
        this.EnFadeIN("Black",255,15);
    }
    if (RD_EndFlame == 20) {
        this.EnFadeIN("Black" , 255 , 29); 
        rd_rtnFlame = 1;
    }
}
const VN_Icon用 = 157; 
const VN_休憩用 = 158; 
Game_Interpreter.prototype.RD_報酬追加 = function (itemId , _個数) {
    try {
        let _AddFlg = true; 
        for (let i = 0; i <= RD_GainItem.length - 1; i++) {
            if (RD_GainItem[i]._No == itemId) {
                _AddFlg = false;
                RD_GainItem[i]._Cnt += _個数;
            }
        }
        if (_AddFlg){
            let _SetI = new DropItemData(itemId);
            _SetI._Cnt = _個数;
            RD_GainItem.push(_SetI);
        }
        let _Name = $dataItems[itemId].name;
        let _SStr = _Name + "を" + _個数 + "つ手に入れた";
        let _IcoNo = $dataItems[itemId].iconIndex;
        $gameVariables._data[VN_Icon用] = _IcoNo;
        $gameVariables._data[VN_休憩用] = _SStr;
        this.RD_GainItem描画();
    } catch (e) {
        console.log(e);
    }
}
Game_Interpreter.prototype.RD_報酬反映 = function () {
    BtlEndGetGold = RD_GainGold;
    DropItem = RD_GainItem.slice();
    GainWaitFlg = true;
}
const RDWinStartFlame = 10;
Game_Interpreter.prototype.RD_RD_End_WinUpdate = function () {
    if ($gameSystem.RD_BaseData.resultOffFlg) {
        RD_EndFlame = RDWinStartFlame + 120 + 1; 
    }
    if (RD_EndFlame == RDWinStartFlame) { 
        this.RD_EventPicDel();
        this.DelSprite(PN_rdPlayer);
        this.SetSprite(PN_rdClearBack + 0, RdnPicPath + "Clear_Back", 0, 0, 0);
        this.SetSprite(PN_rdClearBack + 1, RdnPicPath + "Clear_Win", 0, -30, 0);
        this.MoveSprite(PN_rdClearBack + 0, 0, 0, 15, 255);
        this.MoveSprite(PN_rdClearBack + 1, 0, 0, 15, 255);
        let WinBgm = "WinBgm"; 
        this.PlayBgm(WinBgm);
    }
    D_Text_Cng_font = "Makinas";
    if (RD_EndFlame == RDWinStartFlame) {
        let _GPosX = 680;
        let _討伐 = $gameVariables.value(VN_EmBrk) + " × " + "200 ";
        let _討伐C = $gameVariables.value(VN_EmBrk) * 200 ;
        this.SpriteStrC(PN_ClTxt + 0, SCol.Def0 + " " + _討伐, 23, 480, 279 + (45 * 0) - 30, 0);
        this.SpriteStrR(PN_ClTxt + 1, SCol.Def0 + _討伐C, 32, _GPosX, 262 + (45 * 0) - 30, 0);
        this.MoveSprite(PN_ClTxt + 0, true, 279 + (45 * 0) + 3, 15, 255);
        this.MoveSprite(PN_ClTxt + 1, true, 262 + (45 * 0), 15, 255);
        let _ﾌｫﾄ = $gameVariables.value(VN_GetFotn) + " × " + "200 ";
        let _ﾌｫﾄC = $gameVariables.value(VN_GetFotn) * 200;
        this.SpriteStrC(PN_ClTxt + 2, SCol.Def0 + " " + _ﾌｫﾄ, 23, 480, 283 + (45 * 1) - 30, 0);
        this.SpriteStrR(PN_ClTxt + 3, SCol.Def0 + _ﾌｫﾄC, 32, _GPosX, 262 + (45 * 1) - 30, 0);
        this.MoveSprite(PN_ClTxt + 2, true, 283 + (45 * 1) + 3, 15, 255);
        this.MoveSprite(PN_ClTxt + 3, true, 262 + (45 * 1), 15, 255);
        let _踏破 = $gameVariables.value(VN_ThRitu) + " × " + "20 ";
        let _踏破C = $gameVariables.value(VN_ThRitu) * 20;
        this.SpriteStrC(PN_ClTxt + 4, SCol.Def0 + " " + _踏破, 23, 480, 283 + (45 * 2) - 30, 0);
        this.SpriteStrR(PN_ClTxt + 5, SCol.Def0 + _踏破C, 32, _GPosX, 262 + (45 * 2) - 30, 0);
        this.MoveSprite(PN_ClTxt + 4, true, 283 + (45 * 2) + 3, 15, 255);
        this.MoveSprite(PN_ClTxt + 5, true, 262 + (45 * 2), 15, 255);
        let _お金C = RD_ClearGold;
        this.SpriteStrR(PN_ClTxt + 6, SCol.Def0 + _お金C, 32, _GPosX, 262 + (45 * 3) - 30, 0);
        this.MoveSprite(PN_ClTxt + 6, true, 262 + (45 * 3), 15, 255);
        let _合計 = _討伐C + _ﾌｫﾄC + _踏破C + _お金C;
        this.SpriteStrR(PN_ClTxt + 7, SCol.Yel0 + _合計, 32, _GPosX, 262 + (45 * 4) + 27 - 30, 0);
        this.MoveSprite(PN_ClTxt + 7, true, 262 + (45 * 4) + 27, 15, 255);
        RD_GainGold = _合計 + RD_戦闘取得Gold;
        this.RD_報酬反映(); 
    }
    if (RD_EndFlame == RDWinStartFlame + 10) {
        //this.SpriteStr(PN_CGetItmTxt, SCol.Def0 + "取得アイテム", 32, 233, 509, 0);
        this.MoveSprite(PN_CGetItmTxt, true, true, 15, 255);
        let _ItemIcoStr = "";
        let _CntIcoStr = "";
        for (let i = 0; i <= RD_GainItem.length - 1; i++) {
            let _IcoNo = $dataItems[RD_GainItem[i]._No].iconIndex;
            _ItemIcoStr += "\\I[" + _IcoNo + "]";
            let _ICnt = RD_GainItem[i]._Cnt;
            if (_ICnt >= 11) _ICnt = 11;
            let _CntStr = _ICnt + Int1IcoNo - 1;
            _CntIcoStr += "\\I[" + _CntStr + "]";
        }
        this.SpriteStr(PN_CGetItmTxt + 1, _ItemIcoStr, 32, 242, 556, 0);
        this.MoveSprite(PN_CGetItmTxt + 1, true, true, 1, 0, 150, 150);
        this.SpriteStr(PN_CGetItmTxt + 2, _CntIcoStr, 32, 242 + 8, 556 - 28, 0);
        this.MoveSprite(PN_CGetItmTxt + 2, true, true, 1, 0, 150, 150);
    }
    if (RD_EndFlame == RDWinStartFlame + 15) {
        this.MoveSprite(PN_CGetItmTxt + 1, true, true, 15, 255, 150, 150);
        this.MoveSprite(PN_CGetItmTxt + 2, true, true, 15, 255, 150, 150);
    }
    D_Text_Cng_font = "";
    if (RD_EndFlame > RDWinStartFlame + 120) {
        if (TouchInput.isTriggered()) {
            AudioManager.fadeOutBgm(1);
            this.EnFadeIN("Black" , 255 , 29); 
            rd_rtnFlame = 1;
        }
    }
}
let rd_rtnFlame = 0;
Game_Interpreter.prototype.RD_End_ReturnUpdate = function () {
    if (rd_rtnFlame == 30) {
        this.RD_End();
        this.ALLDelPict([PNo_Ensyutu]);
        this.ALLDelSprite([PNo_Ensyutu]);
        this.DelSprite(PN_rdClearBack + 0);
        this.DelSprite(PN_rdClearBack + 1);
        rd_rtnFlame = 0;
        rdEndFlg = true;
        return;
    }
    rd_rtnFlame++;
}
Game_Interpreter.prototype.RD_RD_End_LoseUpdate = function () {
    if (RD_EndFlame == 30) {
        this.SetSprite(PN_rdClearBack + 0, RdnPicPath + "Clear_Back", 0, 0, 0);
        this.SetSprite(PN_rdClearBack + 1, RdnPicPath + "Clear_Lose", 0, -30, 0);
        this.MoveSprite(PN_rdClearBack + 0, 0, 0, 15, 255);
        this.MoveSprite(PN_rdClearBack + 1, 0, 0, 15, 255);
        if (RD_KoudoZeroLoseFlg) {
            this.SetSprite(PN_ClTxt + 0, RdnPicPath + "Clear_Lose_Koudo", 0, -30, 0);
            this.MoveSprite(PN_ClTxt + 0, 0, 0, 15, 255);
            RD_KoudoZeroLoseFlg = false;
        }
        AudioManager.fadeOutBgm(1);
    }
    if (RD_EndFlame > 60) {
        if (TouchInput.isTriggered()) {
            this.EnFadeIN("Black" , 255 , 29); 
            rd_rtnFlame = 1;
        }
    }
}
Game_Interpreter.prototype.RD_RD_End_TettaiUpdate = function () {
    if (RD_EndFlame == 30) {
        AudioManager.fadeOutBgm(1);
        this.RD_報酬反映(); 
    }
    if (RD_EndFlame > 60) {
        this.EnFadeIN("Black" , 255 , 29); 
        rd_rtnFlame = 1;
    }
}
let RD_EndFDeck = [];
let RD_EndAftFlame = 0;
Game_Interpreter.prototype.RD_End = function () {
    TutorialDgn管理No = 0;    
    RD_EndFDeck = CopyArr($gameSystem.Btl_Chara.FDeck);
    if (RD_EndOneShot) return;
    RD_EndOneShot = true;
    for (let i = 0; i <= $gameSystem.Btl_Chara.LoadLive2DArr.length - 1; i++) {
        L2dAutoFadeOut($gameSystem.Btl_Chara.LoadLive2DArr[i], 30); 
    }
    L2dAutoFadeOut(MdlRomashaBattle, 30); 
    L2dTop("romasha_talk");
    $gameVariables.setValue(VN_RdKaiso , 0);  
    RD_Flg = false;     
    RD_Keep_NPlayer = null;     
    this.SetCmnEventSc(CN_RdAftEnd); 
    $gameVariables.setValue(VN_RD救助人数 , RD_救助人数); 
    RD_BtlFlg = false;
    MenuModeStr = _MMode.Map;
    $gameActors._data[1]._hp = $gameActors._data[1].mhp; 
    $gameSystem.enableMenu(); 
    this.DelSprite(PNo_HintStr);
    RD_EndAftFlame = 20;
    this.RD_ClearUIDell(); 
    RDBtlSettingData.RDEndReset(); 
    for (let i = 0; i < Bef_RD_SetAdSys.length; i++) {
        $gameSystem.SetAdSys.unshift(Bef_RD_SetAdSys[i]);
    }
}
let RD_EndFDeck_Check = function (_id) {
    return RD_EndFDeck.indexOf(_id) != -1;
}
Game_Interpreter.prototype.RD_強制終了 = function () {
    N_Sprite_DLayer = 0;
    this.RD_ClearUIDell(); 
    this.ALLDelSprite(); 
    $gameVariables.setValue(VN_RdKaiso , 0);  
    RD_Flg = false;     
    RD_BtlFlg = false; 
    RD_Keep_NPlayer = null;     
    WinSetFlg = false; LoseSetFlg = false;
    ResetBtlMaxHpUp(); 
    RD_SaveSprite = [[],[],[]];
    RDBtlSettingData.RDEndReset();
    RDBtlSettingData.BtlEndReset();
}
