let _PLSelect_SUFlg = false; 
let _Bef_SelNo = -1;
let _YPlsMFlg = false; 
Game_Interpreter.prototype.RD_PLSelect_Cng = function () { 
    RD_Mode = EnRDMode.PL_Select;
    _Bef_SelNo = -1;
    _PLSelect_SUFlg = false;
    _PLSelect_SU_Flame = 0;
    _PLSelect_Flame = 0;
    PL_Select_Mode = RDSelMode._通常;
    this.RD_MapHPDraw(); 
}
Game_Interpreter.prototype.RD移動後共通処理 = function () {
    this.TmpEvPCall("RD_All" , 1);
}
let _PLSelect_SU_Flame = 0; 
Game_Interpreter.prototype.RD_PLSelect_SetUp = function () { 
    if (_PLSelect_SU_Flame == 0) { 
        this.RD_ClrUIDraw(); 
        RD_EvMode = Em_RD_EvMode.CSelect; 
        console.log("ベースコモンイベント");
        this.TmpEvPCall("RD" + RD_Id + "_0" , $gameVariables.value(VN_RdKaiso) + 1);
        if (RDBtlSettingData.移動カード制限Turn > 0) RDBtlSettingData.移動カード制限Turn--;
    }
    let rdDrawCount = $gameSystem.RD_HandVar;
    if (RDBtlSettingData.移動カード制限Turn > 0) { 
        rdDrawCount = RDBtlSettingData.移動カード制限枚数;
    }
    rdDrawCount += RDBtlSettingData.移動カードCng枚数;
    if (rdDrawCount <= 0) rdDrawCount = 1;
    if (_PLSelect_SU_Flame == 1) { 
        if ($gameSystem.RD_HandCard.length >= rdDrawCount) {
            _PLSelect_SUFlg = true; 
            if (RD_ReloadCnt == 0 && RDBtlSettingData.移動カード制限Turn <= 0) {
                this.RD_ReloadBtnOn();
            }
        } else {
            this.RD_CardSetting();
            this.RD_CardDrawMove();
        }
    }
    if (_PLSelect_SU_Flame == 15) {
        _PLSelect_SU_Flame = 0; 
    }
    _PLSelect_SU_Flame++;
}
let _PLSelect_Flame = 0;
let _UseCardPNo = -1;
let PL_Select_Mode = 1; 
let RDSelMode = {
    _通常:1,
    _状態:2,
    _撤退:3,
    _デッキ確認:4
};
let RD_KoudoZeroLoseFlg = false;
let rdSelectWait_flame = 0; 
Game_Interpreter.prototype.RD_PL_Select_Update = function () { 
    if(rdSelectWait_flame > 0){
        rdSelectWait_flame--;
        return;
    }
    if(RD_Koudo <= 0) { 
        RD_LoseEndFlg = RD_KoudoZeroLoseFlg = 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(); 
        return;
    }
    this.RD_行動数描画Update();
    if (!_PLSelect_SUFlg){
        L2dTop($gameSystem.Btl_Chara.Live2D.Name); 
        this.RD_PLSelect_SetUp(); 
    } else {
        switch (PL_Select_Mode){
            case RDSelMode._通常: 
                this.OpeUpdate(); 
                this.RD_PLSelect_BaseUpdate();
                break;
            case RDSelMode._撤退: 
                this.RD_PLSelect_撤退Update();
                break;
            case RDSelMode._デッキ確認: 
                this.RD_PLSelect_デッキ確認Update();
                break;
        }
        _PLSelect_Flame++;
    }
}
Game_Interpreter.prototype.RD_PLSelect_BaseUpdate = function () {
    if (TutorialDgn管理No == 1) {
        this.SetCmnEvent(CN_RDチュートリアル用);
        return;
    }
    let overPicArr = [];
    for (let i = 0; i <= $gameSystem.RD_HandCard.length - 1; i++) {
        overPicArr.push($gameSystem.RD_HandCard[i].setPNo);
    }
    let ckPicNo = overPointerCkArr(overPicArr);
    if (ckPicNo != -1) {
        for (let i = 0; i <= $gameSystem.RD_HandCard.length - 1; i++) {
            if ($gameSystem.RD_HandCard[i].setPNo == ckPicNo) {
                let selectCard = $gameSystem.RD_HandCard[i];
                this.PicCngColor(selectCard.setPNo, [-20, -20, -20, 0]);
                if (_Bef_SelNo != ckPicNo) {
                    this.PicCngColor(selectCard.setPNo);
                    this.RD_CardDrawMove(selectCard.setPNo);
                    SelRndRoot = selectCard.CardRoot;
                    this.RD_MoveMapDraw();
                    _Bef_SelNo = ckPicNo;
                }
                if (TouchInput.isPressed()) {
                    if (TutorialDgn管理No == 6 && $gameVariables.value(VN_rdSyori) == 6) {
                        return;
                    }
                    this.RD_DMapArr_Get();
                    this.PlaySe("card");
                    this.MoveSprite(ckPicNo, selectCard.XPos - 20, selectCard.YPos - 129, 20, 0, 80, 80);
                    if (RD_Koudo < 99) RD_Koudo--;
                    _UseCardPNo = ckPicNo;
                    $gameSystem.RD_HandCard.splice(i, 1);
                    this.RD_PLMove_Cng(); 
                    if (RD_ReloadCnt > 0 && RDBtlSettingData.移動カード制限Turn <= 0){
                        RD_ReloadCnt--;
                        //this.SpriteStr(PN_Reload + 1, RD_ReloadCnt, 18, 10, 570, 150);
                    }
                    if ($gameSystem.RD_LineX == 0) { 
                        this.RD_ReloadBtnOff();
                    }
                    break; 
                }
            }
        }
    }
    this.RD_レリックUpdate();
    if (RD無限マリガンCount > 0) {
        this.CngSprite(PN_Reload , RdnPicPath + "PN_Reload_Non");
    }
    else {
        this.CngSprite(PN_Reload , RdnPicPath + "PN_Reload");
    }
    this.SpriteCngColor(PN_Reload);
    let ckPointer = overPointerSpCk(PN_Reload) && RD_ReloadCnt == 0 && RDBtlSettingData.移動カード制限Turn <= 0;
    if (RD無限マリガンCount > 0) {
        ckPointer = overPointerSpCk(PN_Reload) && RD無限マリガンCount > 0;
    }
    if (ckPointer) {
        this.SpriteCngColor(PN_Reload, [100, 100, 100, 0]);
        if (TouchInput.isPressed()) {
            if (TutorialDgn管理No >= 1 && TutorialDgn管理No <= 4) return;
            if (RD_Koudo > 1 || RD無限マリガンCount > 0) {
                if (RD_Koudo < 99) {
                    if (RD無限マリガンCount <= 0) {
                        RD_Koudo--;
                    }
                }
                this.RD_MoveCardReloadSetup(); 
            }
            else {
                console.log("行動数が0になってしまいます");
            }
            if (TutorialDgn管理No == 6) RD_ReloadCnt = 9;
        }
    }
    this.RD_撤退BtnUpdate();
}
Game_Interpreter.prototype.RD_MoveCardReloadSetup = function () {
    $gameSystem.RD_HandCard = [];
    this.RD_ReloadBtnOff();
    this.RD_PLSelect_Cng(); 
    this.DelPict(PN_HandMax + 2);
    this.RD_行動数描画Update();
}
Game_Interpreter.prototype.RD_ReloadBtnOn = function () {
    RD_ReloadCnt = 0;
    if (TutorialDgn管理No >= 1 && TutorialDgn管理No <= 4) return;
    if (TutorialDgn管理No == 5) TutorialDgn管理No = 6;
    this.MoveSprite(PN_Reload, 0, true, 7);
    //this.MoveSprite(PN_Reload + 1, true, true, 7, 0);
}
let RD無限マリガンCount = 0;
Game_Interpreter.prototype.RD_ReloadBtnOff = function () {
    RD_ReloadCnt = 0;
    if ($gameSystem.RD_LineX == 0 && $gameActors._data[1].equips()[3].id == 8 || RD無限マリガンCount > 0) {
        RD_ReloadCnt = 0;
    }
    if (RD無限マリガンCount > 0) {
        RD無限マリガンCount--;
    }
    this.MoveSprite(PN_Reload, -192, true, 7);
}
Game_Interpreter.prototype.RD_撤退BtnUpdate = function () {
    let ckPicArr = [PN_rdRtBtn + 0, PN_rdRtBtn + 1];
    this.SpriteCngColorArr(ckPicArr); 
    let ckPicNo = overPointerSpCkArr(ckPicArr);
    if (ckPicNo != -1){
        this.SpriteCngColor(ckPicNo, [100, 100, 100, 0]);
        if (TouchInput.isPressed()){ 
            switch (ckPicNo - PN_rdRtBtn){
                case 1: 
                    this.RD_PLSelect_撤退View();
                    PL_Select_Mode = RDSelMode._撤退;
                    break;
            }
        }
    }
    this.SpriteCngColor( PN_BDeckBtn);
    if(overPointerSpCk(PN_BDeckBtn)){
        this.SpriteCngColor( PN_BDeckBtn , [150,150,150,0]);
        if (TouchInput.isTriggered()) {
            this.RD_PLelect_デッキ確認Cng();
        }
    }
}
const Rd_DeckPath = RdnPicPath + "Deck/";
Game_Interpreter.prototype.RD_PLelect_デッキ確認Cng = function () {
    PL_Select_Mode = RDSelMode._デッキ確認;
    this.Deck一覧View(PN_BDeckBack , $gameSystem.Btl_Chara.FDeck);
}
Game_Interpreter.prototype.RD_PLSelect_デッキ確認Update = function () {
    if(this.Deck一覧Update()) {
        rdSelectWait_flame = 10;
        PL_Select_Mode = RDSelMode._通常; 
    }
};
Game_Interpreter.prototype.RD_PLSelect_撤退View = function () {
    this.SetSprite(PN_rdMenu, RdnPicPath + "TettaiBase", 0, 0, 0);
    this.MoveSprite(PN_rdMenu, true, true, 10);
    RD_TettaiSelectNo = 1;
    this.SetSpriteC(PN_rdMenu + 1, RdnPicPath + "TettaiSelect", 524 + (351 * RD_TettaiSelectNo), 461, 0);
    this.MoveSprite(PN_rdMenu + 1, true, true, 10);
}
let _RdTettaiCM = [ 
    [422, 422, 202, 78],
    [750, 422, 249, 78]
];
let RD_TettaiSelectNo = 1; 
Game_Interpreter.prototype.RD_PLSelect_撤退Update = function () {
    let ckNo = overSpriteClickable(PN_rdMenu, _RdTettaiCM); 
    switch (ckNo){
        case 0: 
            if (ckNo != RD_TettaiSelectNo){
                RD_TettaiSelectNo = ckNo;
                this.MoveSprite(PN_rdMenu + 1, 524 + (351 * RD_TettaiSelectNo), true, 5);
            }
            if (TouchInput.isPressed()) {
                this.RD_RDEnd_Cng(); 
            }
            break;
        case 1: 
            if (ckNo != RD_TettaiSelectNo) {
                RD_TettaiSelectNo = ckNo;
                this.MoveSprite(PN_rdMenu + 1, 524 + (351 * RD_TettaiSelectNo), true, 5);
            }
            if (TouchInput.isPressed()) {
                PL_Select_Mode = 1; 
                this.RD_RTBtn_MenuDel();
            }
            break;
    }
    if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
        PL_Select_Mode = 1; 
        this.RD_RTBtn_MenuDel();
    }
}
Game_Interpreter.prototype.RD_RTBtn_MenuDel = function () {
    for (let i = 0; i <= 30; i++) {
        this.DelPict(PN_rdMenu + i);
        this.DelSprite(PN_rdMenu + i);
    }
}
Game_Interpreter.prototype.RD_行動数描画Update = function () {
    this.SetSprite(PN_rdKoudoCnt + 0, RdnPicPath + "PN_rdKoudoCnt", 0, 0);
    let _cntStr = RD_Koudo;
    if (_cntStr < 0) _cntStr = 0;
    if (_cntStr > 9) _cntStr = 10;
    this.SetSpriteC(PN_rdKoudoCnt + 1, BtlPicPath + "PN_EdCount" + _cntStr, 948, 654);
    if (overPointerSpCkTm(PN_rdKoudoCnt + 0)) {
        D_Text_Cng_font = "Makinas";
        this.SetSpriteC(PN_dgnKodoWin, RdnPicPath + "PN_dgnKodoWin", 946, 569);
        if (RD_Koudo >= 99) {
            this.SpriteStrC( PN_dgnKodoWin + 1 , "無制限" , 24 , 946, 562);
        } else {
            this.SpriteStrC( PN_dgnKodoWin + 1 , "残り：" + RD_Koudo + "行動" , 24 , 946, 562);
        }
        D_Text_Cng_font = "";
    } else {
        this.DelSpriteSpan(PN_dgnKodoWin , PN_dgnKodoWin + 1);
    }
}
let RD_Koudo_WinPlsCnt = 3;
Game_Interpreter.prototype.RD_戦闘勝利行動数増加量設定 = function (_setCnt) {
    RD_Koudo_WinPlsCnt = _setCnt;
}
let RD_戦闘勝利行動数増加量Reset = function () {
    RD_Koudo_WinPlsCnt = 3;
}
Game_Interpreter.prototype.RD_行動数変更 = function (_cngCnt) {
    RD_Koudo += _cngCnt;
    if (RD_Koudo < 0) RD_Koudo = 0;
}
