let BtlValueReset = function () {
    NPlayer = [];
    BtlDeck = [];
    DeckCard = [];
    CardData = [];
    TurnCard = [];
    UseSkill = [];
    WinSetFlg = false;
    LoseSetFlg = false;
    BtlEscapeFlg = false;
    BtlEndGetGold = undefined;
    DropItem = [];
    BtlBit.picCounter = 0; 
    CardSelecter_Endflame = 0; 
    notUseCardFlg = false; 
    bラスボス撃破 = false;
    btlSpineFstLoadFlg = false; 
    btlStopFlg = false; 
    btlUiOffFlg = false; 
    Btl毎ターンCmnPlay中 = false; 
    _射精欲EvStopFlg = false; 
    BtlBef羞恥SwValue = false;
    SetBtlMVVar_DoEnemyName_味方Flg = false;
    B戦闘復活Count = 0;
}
let BtlSaveSprite = [[], [], []]; 
Game_Interpreter.prototype.command301 = function () {
    $gameSwitches.setValue(SW_BtlEndAccessFlg, false);
    EnforceCardUseNo = -1; 
    this.BtlEnemy行動SW_Reset(); 
    L2dAllDelSprite(); 
    this.InstantSave(); 
    BtlData.EnemyGrpNo = this._params[1]; 
    BtlData._逃走可能Flg = this._params[2]; 
    if (TBtl_EnemyNo != 0) BtlData.EnemyGrpNo = TBtl_EnemyNo; 
    if (BtlReTryCheck()) battleflg = false;
    if (!battleflg) {
        this.KeepSprite(BtlSaveSprite);
        this.ALLDelPict([PNo_Ensyutu]);
        $gamePlayer.reserveTransfer(BtlStatic.BtlMapID, 0, 0, 0, 2); 
        this._index = this._list.length; 
        this._list = null; 
        $gameMap.eraseEvent(this._eventId);
    } else {
        this.command301_BattleEnd(); 
    }
    battleflg = true;
    return true; 
};
Game_Interpreter.prototype.command283 = function () {
    $gameMap.changeBattleback(this._params[0], this._params[1]);
    BtlStatic.Battleback1 = this._params[0];
    BtlStatic.Battleback2 = this._params[1];
    return true;
};
Game_Interpreter.prototype.BattleOpen = function () {
    this.BtlCharaSetting("Romasha");
    BtlValueReset(); 
    AccessFlg = false;
    if (!RD_Flg) {
        this.NPlayer_Setting(); 
        NPlayer[0]._NState = []; 
    } else {
        _StartAccessFlg = false;
        if (RD_Keep_NPlayer != null) {
            NPlayer.push(RD_Keep_NPlayer[0]);
            this.NPlayer_Setting(true); 
            if (BtlRomashaFlg) {
                for (let i = 0; i <= NPlayer[0]._NState.length - 1; i++) {
                    if (NPlayer[0]._NState[i]._No == En_StNo.アクセス) {
                        _StartAccessFlg = AccessFlg = true;
                    }
                }
            }
        } else {
            this.NPlayer_Setting(); 
        }
    }
    this.BtlLive2DStart(); 
    this.BattleSetUp(); 
    this.setupChild($dataCommonEvents[BtlStatic.CN_戦闘開始時].list, this.character(0)._eventId);
    this._戦闘開始時レリック変数リセット(); 
}
const StateNo_酩酊 = 40; 
Game_Interpreter.prototype.BattleSetUp = function () {
    if (RD_BtlFlg && RD_Id == 60) BtlData._逃走可能Flg = false;
    if ($gameVariables.value(VN_Seikan) > 90) $gameVariables.setValue(VN_Seikan ,80);
    OtherStopFlg = false; 
    this.DelSpriteSpan(PNo_Map , PNo_HintStr);
    $gameSwitches.setValue(551, true); 
    Btl最初のドローFlg = false;
    DefBtnCoolTime = 0; 
    BtlRomashaFlg = (PlayerBtlChara.Name == "Romasha"); 
    N_Sprite_MNo = 41; 
    BtlData.Turn = 0; 
    BtlData.EndGetGold倍率 = 1;  
    CardReset(); 
    this.BtlItemSetUp(); 
    $gameTroop._turnCount = 0; 
    this.MapEnd(); 
    BtlData.Mode = EnBtlMode.CardSelect;
    NextTurnCardMoveSkip = false;
    BtlEnemyCellDmgFlg = false;
    BtlStatic.VN_味方No = 28;
    if (RD_Flg && $gameVariables.value(VN_RD中だけ味方No) != 0) {
        BtlStatic.VN_味方No = VN_RD中だけ味方No;
        if (!RD_MikataSetup) {
            this._味方HP_維持開始(); 
        }
    }
    if (TBtl_MikataNo != 0) $gameVariables.setValue(BtlStatic.VN_味方No, TBtl_MikataNo); 
    if ($gameVariables.value(BtlStatic.VN_味方No) != 0) { 
        if (_Mikata_Hp_維持Flg && _Mikata_Hp[0] == 0) {
        } else {
            this.NPlayer_Add($gameVariables.value(BtlStatic.VN_味方No));
            if (_Mikata_Hp_維持Flg) NPlayer[1]._hp = _Mikata_Hp[0];
        }
    }
    if (BtlStatic.Battleback1 != "") {
        this.SetSprite(PN_B_back, "battlebacks1/" + BtlStatic.Battleback1, 0, 0);
    }
    if (BtlStatic.Battleback2 != "") {
        this.SetSprite(PN_B_back + 1, "battlebacks2/" + BtlStatic.Battleback2, 0, 0);
    }
    this.NEnemys_Setting(BtlData.EnemyGrpNo); 
    this.SetSprite(PN_UFuti, BtlPicPath + "PN_UFuti", 0, 0);
    this.SetSprite(PN_DFuti, BtlPicPath + "PN_DFuti", 0, 507);
    this.SetSprite(PN_PFuti, BtlPicPath + "PN_PFuti", 0, 0);
    this.SetSprite(PN_RTbase, BtlPicPath + "PN_RTbase", 784, 0);
    this.BtlItemBtnDraw();
    this.TurnEndBtnDraw();
    if (!$gameSystem.BtlAutoFlg) {
        this.SetSpriteC(PN_AutoBtn, BtlPicPath + "AutoBtn", 109, 39);
    } else {
        this.SetSpriteC(PN_AutoBtn, BtlPicPath + "Auto_StopBtn", 109, 39);
    }
    BtlDeckSetting(); 
    if (BtlRomashaFlg) {
        this.DeckNomalCng();    //アクセスカードを通常カードに変換(ロマーシャ時Only)
        this.BtlStartAccess();  
    }
    this.BtlLive2DParamSet_State(); 
    this.SeikanUpdate(); 
    NPlayer[0]._TurnGage = 1; 
    this.NextTCSetting(true); 
    this.DeckReSetting(true); 
    this.CardSelect(-1);    
    this.BtlRericDraw();
    this.DefBtn_Draw();
    this.ChangePhase_TurnChange(); 
    this.DeckMaisuView(); 
    //プレイヤーが酩酊ステート(40)の場合表示を行う
    if (_PLステート確認(StateNo_酩酊)) {
        this.BLogAdd(220, SCol.Yel + "マリガンによって一時的に酩酊状態から意識が鮮明になった");
    }
    plNotHirumi = false;
    _所持3ターン敗北Turn = 0;
};
Game_Interpreter.prototype.IsBtlSetupLoading = function () {
    if (!btlSpineFstLoadFlg) {
        console.log("IsBtlSetupLoading：戦闘前ローディング");
        if (IsSpineLoading()) return true;
        for (let i = 0; i < NEnemy.length; i++) {
            if (NEnemy[i]._hp != 0) {
                if (NEnemy[i].SpineFlg) {
                    if (NEnemy[i]._pHeight == 0) return true;
                }
                else {
                    let ckEnemyPicData = GetSpriteData(PN_Character + NEnemy[i]._PicNo);
                    if (ckEnemyPicData == null) return true;
                }
            }
        }
        this.BattleLoadComp(); 
        btlWaitFlame = 20;
        btl味方VoiceData = null;
        if ($gameSwitches.value(SW_ゴブリンパート)) {
            this.PVoice("romasha","h1_448_haha");
        } else {
            let charaName = "romasha";
            let voiceFile = ["btl_371_sennto","btl_372_yoro_2","btl_373_kotir","btl_374_zetta","btl_374_zetta_2","btl_375_saa","btl_375_saasu_3r"];
            if (NPlayer.length > 1) {
                let ckEnemy = $dataEnemies[NPlayer[1]._enemyId];
                btl味方VoiceData = new Btl味方VoiceData(NPlayer[1]._enemyId);
                let plVoiceFlg = Math.random() < 0.3;
                if (btl味方VoiceData.isVoice != false && !plVoiceFlg) {
                    if (btl味方VoiceData.StartVoice.length > 0) {
                        charaName = btl味方VoiceData.charaName;
                        voiceFile = btl味方VoiceData.StartVoice;
                    } else {
                        plVoiceFlg = true; 
                    }
                }
                if (plVoiceFlg) {
                    if (MetaChecker(ckEnemy , "スピナ" , false) != false) {
                        voiceFile = ["btl_376_ikuwa"];
                    }
                }
            }
            this.PRandVoice(charaName,voiceFile);
        }
    }
    return false;
}
class Btl味方VoiceData {
    constructor(enemyId) {
        let ckEnemy = $dataEnemies[enemyId];
        this.isVoice = false;
        this.charaName = "";
        this.StartVoice = [];
        this.AtkVoice = [];
        this.HealVoice = [];
        this.CngVoice = [];
        this.DmgVoice = [];
        this.DownVoice = [];
        this.WinVoice = [];
        this.ExVoice = [];
        let ckVoice = MetaChecker(ckEnemy , "Voice" , false);
        if (ckVoice != false) {
            this.isVoice = true;
            this.charaName = ckVoice;
            let AddVoiceArr = function (addArr , checkMemo , addFileName) {
                let VStartCnt = MetaChecker(ckEnemy , checkMemo , false);
                if (VStartCnt == false) return;
                for (let i = 0; i < Number(VStartCnt); i++) {
                    addArr.push(addFileName + (i + 1));
                }
            }
            AddVoiceArr(this.StartVoice , "Vstart" , "bstart");
            AddVoiceArr(this.AtkVoice , "Vatk" , "batk");
            AddVoiceArr(this.HealVoice , "Vheal" , "bheal");
            AddVoiceArr(this.CngVoice , "Vcng" , "bcng");
            AddVoiceArr(this.DmgVoice , "Vdmg" , "bdmg");
            AddVoiceArr(this.DownVoice , "Vdown" , "bdown");
            AddVoiceArr(this.WinVoice , "Vwin" , "bwin");
            AddVoiceArr(this.ExVoice , "Vex" , "bex");
        }
    }
}
/** @type {Btl味方VoiceData} */
let btl味方VoiceData = null;
Game_Interpreter.prototype.BattleLoadComp = function () {
    this.MovePict(PNo_Ensyutu, true, true, 60, 0);
    this.PlayBtlBgmKeep();
    btlSpineFstLoadFlg = true;
}
let CardReset = function () {
    BtlCardUse.flame = 0;      
    UseCard_SkillCounter = 0;     
    cardUseMoveTimer = 0;  
    cardUseEffTimer = 0;   
    BtlCardUse.moveAfterWaitFlame = 0;   
    moveTypeCng = -1;   
    damageWait = 0;        
}
let StarCardSettingFlg = false;
let EventDeckArr = [];
let RD_BadStCardAdds = [];
let ckAcCardIds = [1,681,682,683,684,685,686,687,688,689,690,691]; 
let BtlDeckSetting = function () {
    if (RD_Flg) {
        $gameSystem.MasterDeck = CopyArr(RD_SettingDeckArr);
    } else {
        RentalDeckSetting();
        $gameSystem.MasterDeck = $gameSystem.DeckArr[$gameSystem.SelDeck]; 
    }
    if (!$gameSystem._OffAcCard) {
        let _in必殺Card = false;
        for (let i = 0; i <= $gameSystem.MasterDeck.length - 1; i++) {
            if (Check必殺Card($dataWeapons[$gameSystem.MasterDeck[i]])) {
                _in必殺Card = true;
            }
        }
        if (!_in必殺Card) {
            AbmNoArr_Get();
            let addAcNo = -1;
            for (let i = 0; i <= dataWeapons_AbmNo.length - 1; i++) {
                if ($gameParty.numItems(dataWeapons_AbmNo[i]) <= 0) continue;
                let ckID = MetaChecker(dataWeapons_AbmNo[i] , "NCNo" , false);
                if (ckID != false) addAcNo = ckID;
            }
        }
    }
    BtlDeck = CopyArr($gameSystem.MasterDeck);
    if ($gameSystem.Btl_Chara.Name == "Romasha") {
        for (let i = 0; i <= $gameSystem.HandiSaveArr.length - 1; i++) {
            BtlDeck.push($gameSystem.HandiSaveArr[i]);
        }
        ArrRemoveArr(BtlDeck, stateToDeckNoArr); 
        if (RD_Flg) {
            BtlDeck = CopyArr($gameSystem.MasterDeck); 
            for (let i = 0; i < RD_BadStCardAdds.length; i++) {
                for (let j = 0; j < BtlDeck.length; j++) {
                    if (BtlDeck[j] == RD_BadStCardAdds[i]) {
                        BtlDeck.splice(j , 1); j--;
                    }
                }
            }
            RD_BadStCardAdds = [];
        }
        stateToDeckArr.forEach(sData => {
            if ($gameActors.actor(1).isStateAffected(sData.StId)) {
                for (let i = 0; i <= sData.Maisu - 1; i++) {
                    BtlDeck.push(sData.CardNo);
                    RD_BadStCardAdds.push(sData.CardNo);
                }
            }
        });
    }
    if ($gameVariables.value(VN_Scenario2Wa) >= 25 && $gameVariables.value(VN_Scenario4Wa) < 1) {
        for (let i = 0; i <= BtlDeck.length - 1; i++) {
            for (let j = 0; j <= ckAcCardIds.length - 1; j++) {
                if (BtlDeck[i] == ckAcCardIds[j]) {
                    BtlDeck.splice(i , 1);
                    i--;
                    break;
                }
            }
        }
    }
    if (BtlDeck.length > 0) {
       for (let i = 4; i <= 13; i++) {
           for (let j = 0; j <= BtlDeck.length - 1; j++) {
               if (BtlDeck[j] == i) {
                   BtlDeck.splice(j , 1);
                   j--;
               }
           }
       }
    }
    if (EventDeckArr.length > 0) {
        BtlDeck = CopyArr(EventDeckArr);
    }
    EventDeckArr = [];
}
Game_Interpreter.prototype.AccessCardGetBtn_Draw = function () {
    this.SetSpriteC(PN_AWBack, BtlPicPath + "PN_AWBack", 60, 602);
    this.PlaySpriteAnime(PN_ACard, BtlPicPath + "PN_ACard", 7, 2, 14, 560, 255, 100, 100
        , [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7]);
}
let DefBtnCoolTime = 0;
const StNo_防御禁止 = 199;
let DefBtnZureX = 114 - 22;
let DefBtnZureY = 650 - 42;
Game_Interpreter.prototype.DefBtn_Draw = function () {
    if (DefBtnCoolTime > 0 || _PLステート確認(StNo_防御禁止)) {
        this.DelPict(PN_BCard);
        this.PlaySpriteAnime(PN_BCard, BtlPicPath + "PN_BCard", 8, 2, DefBtnZureX, DefBtnZureY, 180, 100, 100
            , [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
        this.SetSprite(PN_BCard + 1 , BtlPicPath + "PN_BCard500", DefBtnZureX, DefBtnZureY , 180);
        this.SetSprite(PN_BCard + 2 , BtlPicPath + "PN_BCardLock", DefBtnZureX, DefBtnZureY , 180);
        if (!_PLステート確認(StNo_防御禁止))
            this.SpriteStrC(PN_BCard + 3 ,SCol.Red + SWCol.Wht + DefBtnCoolTime , 38 , DefBtnZureX + 37, DefBtnZureY + 44);
    } else {
        this.DelPict(PN_BCard);
        this.PlaySpriteAnime(PN_BCard, BtlPicPath + "PN_BCard", 8, 2, DefBtnZureX, DefBtnZureY, 255, 100, 100
            , [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 1, 1, 1, 1, 1, 1, 1]);
        this.SetSprite(PN_BCard + 1 , BtlPicPath + "PN_BCard500", DefBtnZureX, DefBtnZureY);
        this.DelSprite(PN_BCard + 2);
        this.DelSprite(PN_BCard + 3);
    }
}
Game_Interpreter.prototype.DeckReSetting = function (BtlStartFlg) {
    BtlStartFlg = typeof BtlStartFlg !== 'undefined' ? BtlStartFlg : false;
    let setDeck = BtlDeck.concat();
    for (let i = 0; i <= CardData.length - 1; i++) {
        for (let j = 0; j <= setDeck.length - 1; j++) {
            if (CardData[i].CardData.id == setDeck[j]) {
                setDeck.splice(j, 1); 
                break; 
            }
        }
    }
    ShuffleArr(setDeck); 
    let isアクセスFlg = false;
    for (let i = 0; i < setDeck.length; i++) {
        let cardData = $dataWeapons[setDeck[i]];
        if (MetaChecker(cardData  , "アクセス" , false) != false) {
            if (!isアクセスFlg) {
                isアクセスFlg = true;
            } else {
                setDeck.splice(i , 1);
            }
        }
    }
    if (BtlStartFlg) {
        let keepArr = [];
        let _starFlg = true;
        let addAcCardId = -1;
        for (let i = 0; i < setDeck.length; i++) {
            let cardData = $dataWeapons[setDeck[i]];
            if (MetaChecker(cardData , "アクセス" , false) != false) {
                addAcCardId = setDeck[i];
                setDeck.splice(i , 1);
                i--;
                continue;
            }
            if (CardKoukaCk(cardData , KSkill.デッキトップ)) {
                keepArr.push(setDeck[i]);
                setDeck.splice(i , 1);
                i--;
                continue;
            }
            if (_starFlg && cardData.id == $gameSystem.StarCardNo[$gameSystem.SelDeck]) {
                _starFlg = false; StarCardSettingFlg = true;
                keepArr.push(setDeck[i]);
                setDeck.splice(i , 1);
                i--;
                continue;
            }
        }
        keepArr.forEach(setNo => {
            setDeck.unshift(setNo);
        });
        if (addAcCardId != -1 && $gameActors.actor(1).hasArmor($dataArmors[3])) {
            let randMax = 8;
            if (randMax > setDeck.length) {
                randMax = setDeck.length;
            }
            let index = Math.floor(Math.random() * randMax);
            setDeck.splice(index, 0, addAcCardId);
        } else {
            let index = Math.floor(Math.random() * setDeck.length);
            setDeck.splice(index, 0, addAcCardId);
        }
    }
    // for (let i = 1; i <= setDeck.length - 1; i++) {
    //     if (stateToDeckNoArr.indexOf(setDeck[i]) != -1) {
    //         _addArr.push(setDeck[i]);
    //         setDeck.splice(i, 1);
    //     if ($gameSystem.HandiSaveArr.indexOf(setDeck[i]) != -1) {
    //         _addArr.push(setDeck[i]);
    //         setDeck.splice(i, 1);
    // for (let i = 0; i <= _addArr.length - 1; i++) {
    //     setDeck.unshift(_addArr[i]);
    if ($gameSwitches.value(SW_BtlLastBoss) && NPlayer.length > 1) { 
        NPlayer[1]._hp = NPlayer[1].mhp; 
    }
    DeckCard = setDeck.concat();
}
class BReric_Class {
    constructor() {
        this.Pos = new Pos2D(1413, 626);
        this.PicArr = [];
        this.SpaceY = 50; 
        this.Scale = 125; 
    }
}
let BReric = new BReric_Class();
Game_Interpreter.prototype.BtlRericDraw = function () {
    if ($gameSystem.Btl_Chara == null) return;
    BtlPLTurn.PicArr = [];
    for (let i = 0; i <= $gameSystem.Btl_Chara.RericArr.length - 1; i++) {
        let itemNo = $gameSystem.Btl_Chara.RericArr[i];
        let iconNo = $dataItems[itemNo].iconIndex;
        let iconText = " " + "\\I[" + iconNo + "] ";
        this.SpriteStrC(PN_BReric + i, iconText, 25,
            BReric.Pos._x, BReric.Pos._y - (BReric.SpaceY * i), 255, BReric.Scale, BReric.Scale);
        BtlPLTurn.PicArr.push(PN_BReric + i);
    }
}
let _StartAccessFlg = false; 
Game_Interpreter.prototype.BtlStartAccess = function () {
    if ($gameSwitches.value(Sw_BtlStartAcs) || _StartAccessFlg) {
        AccessFlg = true;
        BtlData.PlTurnCardPic = PlayerBtlChara.Name + "_A"; 
        this.SpineCngAccess();
        this.SpineSkinCng(PN_CharacterPL);
        this.DeckAccessCng(); 
        sateAddPlayerFlg = true;
        AddNState(NPlayer[0]._NState, 10, 200);
        $gameActors.actor(1).addState(10); 
        this.TurnEndBtnDraw();
    } else {
        AccessFlg = false;
        BtlData.PlTurnCardPic = PlayerBtlChara.Name; 
        this.RomaSpineSkinSet(); 
    }
    Live2DParamSetCmnEventStart(MdlRomashaBattle); 
    this.SpineView(PN_CharacterPL, "Romasha", "Idl", true, NPlayer[0]._PosX, NPlayer[0]._PosY, 255, plSpineScaleX, plSpineScaleY);
}
let BtlBgmKeepFlg = false; 
let BtlBgmKeepData = null;
Game_Interpreter.prototype.command241 = function() {
    if (!BtlBgmKeepFlg) AudioManager.playBgm(this._params[0]);
    else BtlBgmKeepData = this._params[0];
    return true;
};
Game_Interpreter.prototype.PlayBtlBgmKeep = function() {
    if (BtlBgmKeepData != null) AudioManager.playBgm(BtlBgmKeepData);
    BtlBgmKeepData = null;
    BtlBgmKeepFlg = false;
}
