let SelRndRoot = null;
Game_Interpreter.prototype.RD_MoveMapDraw = function () {
    let _SetX = 285 + 501 + 42;
    let _SetY = 288 - 33;
    if (RD_LeftStartFlg) _SetX -= (6 * RD_MassSizeX);
    for (let i = PN_RootPic; i <= PN_RootPic + 6; i++) {
        this.DelSprite(i);
    }
    if (SelRndRoot != null){
        let _MvX = 0; 
        let _MvY = 2; 
        let _CSetX = _SetX - (_MvX * RD_MassSizeX);
        if (RD_LeftStartFlg) _CSetX = _SetX + (_MvX * RD_MassSizeX);
        let _CSetY = _SetY + (_MvY * RD_MassSizeY);
        let _befStr = "";
        for (let i = 0; i <= SelRndRoot.length - 1; i++) {
            let _setImg = "line_";
            switch (i) {
                case 0:
                    _setImg += "st_";
                    break;
                default:
                    _setImg += "in_" + _befStr;
                    break;
            }
            _befStr = SelRndRoot[i];
            switch (SelRndRoot[i]) {
                case "l":
                    if (RD_LeftStartFlg) {
                        _setImg += "r";
                        _befStr = "r";
                    }
                    else {
                        _setImg += "l";
                    }
                    _MvX += 1;
                    break;
                case "t":
                    _setImg += "t";
                    _MvY -= 1;
                    break;
                case "d":
                    _setImg += "d";
                    _MvY += 1;
                    break;
            }
            this.SetSpriteC(PN_RootPic + i, RdnPicPath + _setImg, _CSetX, _CSetY);
            _CSetX = _SetX - (_MvX * 88);
            if (RD_LeftStartFlg) _CSetX = _SetX + (_MvX * 88);
            _CSetY = _SetY + (_MvY * 68);
        }
        this.SetSpriteC(PN_RootPic + SelRndRoot.length, RdnPicPath + "line_ed_" + _befStr,
            _CSetX, _CSetY);
    }
}
let _RdPlX = 0; let _RdPlY = 2;
let _PLMove_EndFlg = false;
let _YMoveFlg = false; 
let _RD_PLMovingFlg = false; 
Game_Interpreter.prototype.RD_PLMove_Cng = function () {
    RD_Mode = EnRDMode.PL_Move;
    _RdPlX = 0;
    _RdPlY = 2;
    _PLMove_EndFlg = false;
    _PLMove_Flame = 0;
    _MRd_Cnt = 0;
    _YMoveFlg = false;
    _RD_PLMovingFlg = true;
}
let _PLMove_Flame = 0;
let _PLMoveWait = 0; 
let _MRd_Cnt = 0;
let _Bef_SetMvPic = "";
let _BefBtlEffX = 0; let _BefBtlEffY = 0;
let _Rd_Event; 
Game_Interpreter.prototype.RD_PLMove_Update = function () {
    this.RD_行動数描画Update();
    this.DelSpriteSpan(PN_dgnKodoWin , PN_dgnKodoWin + 1);
    if (_PLMoveWait > 0){
        _PLMoveWait--;
        return;
    }
    if (_PLMove_EndFlg) {
        let _ux = $gameSystem.RD_LineX;
        let _uy = $gameSystem.RD_LineY;
        $gameSystem.RD_Map[_ux + _RdPlX][_uy + _RdPlY] = null; 
        this.RD_PLMove_End()
    } else {
        if (_PLMove_Flame == 1) {
            for (let i = 0; i <= RD_DMapArr.length - 1; i++) {
                if (RD_DMapArr[i].length > 5) {
                    if (_YPlsMFlg) {
                        RD_DMapArr[i].pop();
                    } else {
                        RD_DMapArr[i].shift();
                    }
                }
            }
            let _EventFlg = false;
            if (RD_DMapArr[_RdPlX][_RdPlY] != null) {
                if (RD_DMapArr[_RdPlX][_RdPlY].type != "Ok") {
                    _EventFlg = true;
                }
            }
            if (_EventFlg) { 
                let _ux = $gameSystem.RD_LineX;
                let _uy = $gameSystem.RD_LineY;
                _Rd_Event = RD_DMapArr[_RdPlX][_RdPlY];
                if (RD_DMapArr[_RdPlX][_RdPlY].putData != null) {
                    if (RD_DMapArr[_RdPlX][_RdPlY].putData.type == "ライン") {
                        for (let i = 0; i <= RD_DMapArr[_RdPlX].length - 1; i++) {
                            RD_DMapArr[_RdPlX][i] = null;
                        }
                        for (let i = 0; i <= $gameSystem.RD_Map[_ux + _RdPlX].length - 1; i++) {
                            $gameSystem.RD_Map[_ux + _RdPlX][i] = null;
                        }
                    }
                }
                let setMassData = new RDMassData();
                setMassData.type = "Ok";
                setMassData.played = true;
                RD_DMapArr[_RdPlX][_RdPlY] = setMassData; 
                $gameSystem.RD_Map[_ux + _RdPlX][_uy + _RdPlY] = setMassData; 
                this.RD_EvStart_Cng(); 
                return;
            } else {
                let _ux = $gameSystem.RD_LineX;
                let _uy = $gameSystem.RD_LineY;
                let setMassData = new RDMassData();
                setMassData.type = "Ok";
                RD_DMapArr[_RdPlX][_RdPlY] = setMassData; 
                $gameSystem.RD_Map[_ux + _RdPlX][_uy + _RdPlY] = setMassData; 
            }
            if (_MRd_Cnt == SelRndRoot.length) { 
                _PLMove_Flame = 0;
                _PLMove_EndFlg = true; 
                _RD_PLMovingFlg = false;
            } else {
                let _Ev_X = RDMapPosX + 578;
                let _Ev_Y = RDMapPosY - 5;
                let _SetX = _Ev_X - (_RdPlX * RD_MassSizeX);
                let _SetY = _Ev_Y + (_RdPlY * RD_MassSizeY);
                _BefBtlEffX = _SetX;
                _BefBtlEffY = _SetY;
                if (RD_LeftStartFlg) {
                    _Ev_X -= (6 * RD_MassSizeX);
                    _SetX = _Ev_X + (_RdPlX * RD_MassSizeX);
                }
                let _SPicNo = PN_rdMap_MvM + _MRd_Cnt;
                this.SetSpriteC(_SPicNo, RdnPicPath + "Ms/Ok", _SetX, _SetY + 40, 255);
                this.PlaySe("panel_move");
                this.PlayPtcSc("RD_Panel",_SetX,_SetY + 50);
                let _SetMvPic = ";"
                switch (SelRndRoot[_MRd_Cnt]) {
                    case "l":
                        _SetMvPic = STY_MovePic_l;
                        _Bef_SetMvPic = "l";
                        if (RD_LeftStartFlg) _SetMvPic = STY_MovePic_r;
                        _RdPlX += 1;
                        break;
                    case "t":
                        _SetMvPic = STY_MovePic_t;
                        _Bef_SetMvPic = "t";
                        _RdPlY -= 1;
                        break;
                    case "d":
                        _SetMvPic = STY_MovePic_d;
                        _Bef_SetMvPic = "d";
                        _RdPlY += 1;
                        break;
                }
                this.PlaySpriteAnimeC(PN_rdPlayer, _SetMvPic, 6, 10, _SetX, _SetY);
                let _SetPLX = _Ev_X - (_RdPlX * RD_MassSizeX);
                if (RD_LeftStartFlg) _SetPLX = _Ev_X + (_RdPlX * RD_MassSizeX);
                let _SetPLY = _Ev_Y + (_RdPlY * RD_MassSizeY);
                this.MoveSprite(PN_rdPlayer, _SetPLX, _SetPLY, 15);
                $gameVariables.setValue(VN_ThRitu ,$gameVariables.value(VN_ThRitu) + 1);
                this._OpeUIDraw(); 
                _MRd_Cnt++;
            }
        }
        if (_PLMove_Flame == 15) {
            this.DelPict(_UseCardPNo);
            this.RD_CardDrawMove(); 
        }
        if (_PLMove_Flame == 16) {
            _PLMove_Flame = 0;
        }
        _PLMove_Flame++;
    }
}
Game_Interpreter.prototype.RD_PLMove_End = function () {
    if (_PLMove_Flame == 15) {
        if(_RdPlX == 0 && _RdPlY == 2){
            this.RD_PLSelect_Cng(); 
            return;
        }
        this.RD_MassPicDel(); 
        this.RD_EventPicDel();
        if (_RdPlX > 0) {
            this.RD_MapLineDraw(true, 1);
            _RdPlX--;
            _PLMove_Flame = 0;
            return; 
        }
        if (_RdPlY != 2) { 
            let _MvInt = _RdPlY - 2;
            _YMoveFlg = true;
            if (_MvInt > 0) {
                this.RD_MapLineDraw(false, -1);
                _RdPlY--;
            } else {
                this.RD_MapLineDraw(false, 1);
                _RdPlY++;
            }
            return; 
        }
    }
    _PLMove_Flame++;
}
let RD_BossFlg = false;
let RD_EvEndFlg = false;
let RD_BtlFlg = false; 
Game_Interpreter.prototype.RD_EvStart_Cng = function () {
    WinSetFlg = false; LoseSetFlg = false;
    RD_BtlFlg = false;
    RD_EvEndFlg = false;
    RD_Mode = EnRDMode.Ev_Start;
}
Game_Interpreter.prototype.RD_EvStart_Update = function () {
    RD_BossFlg = false;
    if (!battleflg) {
        if (_Rd_Event.type == "") {
            switch (_Rd_Event.massImage) {
                case "Boss":
                case "Enemy":
                    _Rd_Event.type = "Enemy";
                    break;
                case "Event":
                    _Rd_Event.type = "Event";
                    break;
                case "Fotonia":
                    _Rd_Event.type = "Fotonia";
                    break;
                case "Ok":
                    _Rd_Event.type = "Ok";
                    break;
            }
        }
        switch (_Rd_Event.type) {
            case "Enemy":
                if (_Bef_SetMvPic == "l") { 
                    this.PlayPtcPic("Attack",_BefBtlEffX - RD_MassSizeX,_BefBtlEffY);
                }
                if (_Rd_Event.bossFlg) {
                    RD_BossFlg = true;
                    RD_EvMode = Em_RD_EvMode.BossBef; 
                    this.TmpEvPCall("RD" + RD_Id + "_0" , $gameVariables.value(VN_RdKaiso) + 1);
                } else {
                    RD_BtlFlg = true;
                    this.SetCmnEventSc(CN_RdBefBtl); 
                }
                break;
            case "Event":
                RD_Stop = true; 
                RD_Mode = EnRDMode.PL_Move; 
                RD_EvMode = _Rd_Event.id;
                this.TmpEvPCall("RD" + RD_Id + "_0" , $gameVariables.value(VN_RdKaiso) + 1);
                break;
            case "Fotonia":
                RD_Stop = false; 
                RD_Mode = EnRDMode.PL_Move; 
                $gameVariables.setValue(VN_GetFotn, $gameVariables.value(VN_GetFotn) + 1);
                this.SetCmnEvent(CN_GetFot);
                RD_Koudo++;
                break;
            case "Ok": 
                RD_Mode = EnRDMode.PL_Move; 
                break;
        }
        _Rd_Event.type = ""; 
    }
}
Game_Interpreter.prototype.RD_BossBtlStart = function () {
    RD_EvMode = Em_RD_EvMode.CSelect; 
    RD_BtlFlg = true;
    this.SetCmnEvent(CN_BefBtl); 
}
let _Bef_必殺CNo = -1;
let RD_SaveSprite = [[],[],[]];
let RD_EndLive2DViewFlg = false;
let RD_BtlEndCmnFlg = false;
let RD_KeepDeck = [];
let RD_SettingDeckArr = [];
const VN_次戦闘敵No = 1007;
Game_Interpreter.prototype.RD_BtlCmnStart = function () {
    if (RD_BtlFlg) {
        if (!(WinSetFlg || LoseSetFlg)) {
            RD_LoseEndFlg = false;
            WinSetFlg = false;
            LoseSetFlg = false;
            RD_BtlWinFlg = false; 
            BtlData._逃走可能Flg = !RD_BossFlg;
            this.WinBgmSkip(true); 
            this.DelSprite(PNo_TBack); 
            this.InstantSave(); 
            $gameSystem.AdSysCT = BtlKeepRDAdSysCT;
            BtlData.EnemyGrpNo = _Rd_Event.id; 
            if ($gameVariables.value(VN_次戦闘敵No) != 0) {
                BtlData.EnemyGrpNo = $gameVariables.value(VN_次戦闘敵No);
                $gameVariables.setValue(VN_次戦闘敵No ,0); 
            }
            this.ALLDelPict([PNo_Ensyutu]);
            this.KeepSprite(RD_SaveSprite);
            $gamePlayer.reserveTransfer(BtlStatic.BtlMapID, 0, 0, 0, 2); 
            battleflg = true;
            this.RD_ClearUIDell();
            _Bef_必殺CNo = -1;
            for (let i = 0; i <= $gameSystem.Btl_Chara.FDeck.length - 1; i++) {
                let _ckWpn = $dataWeapons[$gameSystem.Btl_Chara.FDeck[i]];
                if (MetaChecker(_ckWpn , "NCNo" , false) != false) {
                    _Bef_必殺CNo = $gameSystem.Btl_Chara.FDeck[i];
                }
            }
            RD_SettingDeckArr = CopyArr($gameSystem.Btl_Chara.FDeck);
        } else {
            for (let i = 0; i <= BtlDeck.length - 1; i++) {
                let _ckWpn = $dataWeapons[BtlDeck[i]];
                if (MetaChecker(_ckWpn , "アクセス" , false) != false) {
                    BtlDeck.splice(i , 1); i--;
                }
                if (MetaChecker(_ckWpn , "NCNo" , false) != false) {
                    BtlDeck.splice(i , 1); i--;
                }
            }
            this.BtlKeepLoad(); 
            if (_Bef_必殺CNo != -1) BtlDeck.push(_Bef_必殺CNo);
            AccessFlg = false; 
            this.Playerアクセスステート解除(); 
            RD_Keep_NPlayer = NPlayer;
            N_Sprite_L.children = [];
            N_Sprite_M.children = [];
            N_Sprite_T.children = [];
            this.ReturnSprite(RD_SaveSprite);
            RD_SaveSprite = [[],[],[]];
            //this.RD_Live2D_Draw(); //ランダムダンジョン用Live2D描画
            RD_EndLive2DViewFlg = true;
            L2dAutoFadeOut($gameSystem.Btl_Chara.Live2D.Name , 1);
            if ($gameSystem.RD_BaseData._Bgm != "") {
                this.PlayBgm($gameSystem.RD_BaseData._Bgm); 
            }
            this.MovePict(PNo_Ensyutu, 0, 0, 25, 0);
            battleflg = false; 
            RD_Mode = EnRDMode.PL_Move;
            for (let i = 0; i <= RD_Keep_NPlayer.length - 1; i++) {
                RD_Keep_NPlayer[i]._befdef = -1;
                RD_Keep_NPlayer[i]._befhp = -1;
            }
            RD_Keep_NPlayer[0]._mp = $gameSystem.Btl_Chara.ベースMP;
            $gameActors._data[1]._hp = RD_Keep_NPlayer[0]._hp; 
            $gameSystem.Btl_Chara.FDeck = CopyArr(BtlDeck);
            this.Btl_STY戦闘終了後_データ反映();
            L2dTop($gameSystem.Btl_Chara.Live2D.Name);
            this.RD_MapHPDraw();
            RD_BtlWinFlg = WinSetFlg;
            if (_Rd_Event.bossFlg) {
                if (WinSetFlg) { 
                    let _bsCnt = $gameVariables.value(VN_BossBrk) + 1;
                    $gameVariables.setValue(VN_BossBrk, _bsCnt);
                }
                RD_EvMode = Em_RD_EvMode.BossAft; 
            } else {
                if (WinSetFlg) { 
                    let _emCnt = $gameVariables.value(VN_EmBrk) + 1;
                    $gameVariables.setValue(VN_EmBrk, _emCnt);
                }
                RD_EvMode = Em_RD_EvMode.EnemyBrk; 
                if (RD_EvBtlFlg) { 
                    RD_EvMode = Em_RD_EvMode.EvEnemyBrk;
                }
            }
            RD_EvBtlFlg = false;
            if (!RD_BtlWinFlg) {
                RD_LoseEndFlg = true;
                let _SetX = GetSpriteData(PN_rdPlayer)._x;
                let _SetY = GetSpriteData(PN_rdPlayer)._y;
                this.SetSpriteC(PN_rdPlayer , STY_MovePic_down , _SetX , _SetY , 255);
                RD_ClearMode = EnRDClearMode.Faild; 
                this.RD_RDEnd_Cng();
            }
            this.RD_ClrUIDraw(); 
            if (WinSetFlg && !BtlEscapeFlg) {
                this.RD_Gain処理(); 
                RD_Koudo += RD_Koudo_WinPlsCnt + 1;
            }
            WinSetFlg = false;
            LoseSetFlg = false; 
            this.wait(30); 
            this.TmpEvPCall("RD" + RD_Id + "_0" , $gameVariables.value(VN_RdKaiso) + 1);
        }
    }
}
Game_Interpreter.prototype.RD_Gain処理 = function () {
    if (BtlEndGetGold != undefined) RD_GainGold += BtlEndGetGold;
    BtlEndGetGold = undefined;
    for (let i = 0; i <= DropItem.length - 1; i++) {
        let _AddFlg = true;
        for (let j = 0; j <= RD_GainItem.length - 1; j++) {
            if (DropItem[i]._No == RD_GainItem[j]._No) {
                _AddFlg = false;
                RD_GainItem[j]._Cnt += DropItem[i]._Cnt;
            }
        }
        if (_AddFlg) RD_GainItem.push(DropItem[i]);
    }
    DropItem = [];
    this.RD_GainItem描画();
}
Game_Interpreter.prototype.RD_GainItem描画 = function () {
    if (RD_GainItem.length > 0) {
        this.SetSprite(PN_rdGainWin, RdnPicPath + "PN_rdGainWin", 0, 0);
        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_rdGainIco + 0, _ItemIcoStr, 25, 470, 104);
        this.SpriteStr(PN_rdGainIco + 1, _CntIcoStr, 25, 476, 90);
    }
}
Game_Interpreter.prototype.RD_PlayerPlayAnime = function (animeNo) {
    let _SetX = GetSpriteData(PN_rdPlayer)._x;
    let _SetY = GetSpriteData(PN_rdPlayer)._y;
    ScreenAnim.play(_SetX, _SetY, animeNo, { mirror:false, scale:1.0, z:200 });
}
let RD_救助人数 = 0;
Game_Interpreter.prototype.RD_救助人数増加 = function () {
    RD_救助人数++;
}
