let DoCkNo = 0;
let DoCkCharaArr;
let DoEnemyFlg = false;
let BtlEnemyCheck_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    BtlEnemyCheck_NUpdateSc.call(this);
    $gameSwitches.setValue(SW_ひん剥きOkCheck , _ひん剥き_UseCk());
}
Game_Interpreter.prototype.EnemyActionCheckUpdate = function () {
    let _CkChara = DoCkCharaArr[DoCkNo];
    if (_CkChara._hp <= 0) return;
    switch (_CkChara._enemyId) {
        case 25: //カジュア(通常)
            if (_CkChara._25_1 == undefined && _体力チェック(-1, 50)) { 
                _CkChara._25_1 = true;
                this._Re行動Set(); 
            }
            if (_CkChara._25_2 == undefined && _体力チェック(27, 50)) { 
                _CkChara._25_2 = true;
                this._Re行動Set(4); 
            }
            break;
        case 26: 
        case 186: 
            let b両方アクセスCheck = true;
            for (let i = 0; i <= DoCkCharaArr.length - 1; i++) {
                if (DoCkCharaArr[i]._enemyId != 26 && DoCkCharaArr[i]._enemyId != 28) b両方アクセスCheck = false;
            }
            if (b両方アクセスCheck) {
                if (_CkChara._26_1 == undefined && _体力チェック(-1, 75) && !$gameSwitches.value(SW_必殺技打った)) { 
                    _CkChara._26_1 = true;
                    this._Re行動Set(5); 
                    break;
                }
                if (_CkChara._26_2 == undefined && $gameSwitches.value(SW_必殺技準備)) { 
                    _CkChara._26_2 = true;
                    this._Re行動Set();
                    break;
                }
            }
            break;
        case 27: //バニラ(通常)
            if (_CkChara._OneFlg_1 == undefined && _体力チェック(-1, 50)) { 
                _CkChara._OneFlg_1 = true;
                this._Re行動Set(); 
            }
            break;
        case 28: 
        case 83: 
            let b両方アクセスCheck2 = true;
            for (let i = 0; i <= DoCkCharaArr.length - 1; i++) {
                if (DoCkCharaArr[i]._enemyId != 26 && DoCkCharaArr[i]._enemyId != 28) b両方アクセスCheck2 = false;
            }
            if (b両方アクセスCheck2) {
                if (_CkChara._26_1 == undefined && _体力チェック(-1, 75) && !$gameSwitches.value(SW_必殺技打った)) { 
                    _CkChara._26_1 = true;
                    this._Re行動Set(4); 
                    break;
                }
                if (_CkChara._28_2 == undefined && $gameSwitches.value(SW_必殺技準備)) { 
                    _CkChara._28_2 = true;
                    this._Re行動Set();
                }
            }
            if (_CkChara._28_3 == undefined && $gameSwitches.value(SW_必殺技打った)) { 
                _CkChara._28_3 = true;
                this._Re行動Set();
            }
            break;
        case 65: 
            if (_CkChara._65_1 == undefined && _体力チェック(-1, 40)) { 
                _CkChara._65_1 = true;
                this._Re行動Set(); 
            }
            let ck味方Cnt65 = _味方生存数();
            if (ck味方Cnt65 <= 1 && _CkChara._UsePtnNo == 2) {
                _CkChara._UsePtnNo = 3;
            }
            break;
        case 192: //バニラ(通常)：4章
            if (_CkChara._OneFlg_1 == undefined && _体力チェック(-1, 75)) { 
                _CkChara._OneFlg_1 = true;
                this._Re行動Set(); 
            }
            break;
        case 185: //カジュア(通常)：4章
            if (_CkChara._25_2 == undefined && _体力チェック(82, 50)) { 
                _CkChara._25_2 = true;
                this._Re行動Set(4); 
            }
            if (_CkChara._25_1 == undefined && _体力チェック(-1, 70)) { 
                _CkChara._25_1 = true;
                this._Re行動Set(); 
            }
            break;
        case 182: //バニラ(中央エレベーター)
            if (_CkChara._OneFlg_1 == undefined && _体力チェック(-1, 50)) { 
                _CkChara._OneFlg_1 = true;
                this._Re行動Set(); 
            }
            break;
        case 183: //バニラ::アクセス(中央エレベーター)
            if (_CkChara._OneFlg_2 == undefined && _体力チェック(-1, 50)) { 
                _CkChara._OneFlg_2 = true;
                this._Re行動Set(); 
            }
            break;
        case 87: 
            if (_CkChara._OneFlg_1 == undefined && _体力チェック(-1, 75)) { 
                _CkChara._OneFlg_1 = true;
                this._Re行動Set(); 
            }
            break;
    }
}
let TurnCheckDo_Em = Game_Interpreter.prototype.SetEnemyNextAction;
Game_Interpreter.prototype.SetEnemyNextAction = function (ckENo, resetFlg) {
    TurnCheckDo_Em.apply(this, arguments);
    DoCkNo = ckENo;
    switch (GetBtlChara_ENo(ckENo)._enemyId) {
        case 13: 
        case 14: 
            if (_他味方キャラが倒れた()) this._行動Set(3);
            break;
    }
}
let GetMikataExActionNo = function () {
    SetBtlMVVar_DoEnemyName_味方Flg = true;
    let enemyId = NPlayer[_TnEmNo]._enemyId; 
    let exActionNos = [];
    let SelectExAction = function () {
        let rtnNo = exActionNos[Math.floor( Math.random() * exActionNos.length)];
        return rtnNo;
    }
    switch (enemyId) {
        case 101: 
            exActionNos.push(0);
            exActionNos.push(1);
            exActionNos.push(2);
            return SelectExAction();
        case 102: 
            exActionNos.push(0);
            exActionNos.push(1);
            exActionNos.push(2);
            return SelectExAction();
        case 103: 
            if (_ニップルドーム_UseCk()) return 3; 
            if (_胸揉み_UseCk()) exActionNos.push(2); 
            if (_尻揉み_UseCk()) exActionNos.push(1); 
            exActionNos.push(0); 
            exActionNos.push(4); 
            exActionNos.push(5); 
            exActionNos.push(6); 
            exActionNos.push(7); 
            exActionNos.push(8); 
            return SelectExAction();
    }
}
Game_Interpreter.prototype.Atkモブ隊員 = function () {
    var wpnName = $gameVariables.value(1012);
    switch (wpnName) {
        case "Gun":
            this.SetSpineAnime(PN_CharacterPL + 1, "Atk_Gun", false);
            break;
        case "Sword":
            this.SetSpineAnime(PN_CharacterPL + 1, "Atk_Sword", false);
            break;
        case "Rifle":
            this.SetSpineAnime(PN_CharacterPL + 1, "Atk_Rifle", false);
            break;
        case "TacticalArm":
            this.SetSpineAnime(PN_CharacterPL + 1, "Atk_TacticalArm", false);
            break;
    }
}
const VN_Em行動制御 = 156; 
let GetEnemyExActionNo = function () {
    SetBtlMVVar_DoEnemyName_味方Flg = false;
    let emBefExActionNo = NEnemy[_TnEmNo].emBefExActionNo;
    if (emBefExActionNo == undefined) {
        NEnemy[_TnEmNo].emBefExActionNo = -1;
        emBefExActionNo = NEnemy[_TnEmNo].emBefExActionNo;
    }
    let exActionNos = [];
    let SelectExAction = function () {
        if (exActionNos.length > 1) {
            for (let i = 0; i < exActionNos.length; i++) {
                if (exActionNos[i] == emBefExActionNo) {
                    exActionNos.splice(i , 1);
                    i--;
                }
            }
        }
        let rtnNo = exActionNos[Math.floor( Math.random() * exActionNos.length)];
        NEnemy[_TnEmNo].emBefExActionNo = rtnNo;
        return rtnNo;
    }
    let enemyId = NEnemy[_TnEmNo]._enemyId; 
    switch (enemyId) {
        case 8: 
            if (_ニップルドーム_UseCk()) return 3; 
            if (_バインド_UseCk()) exActionNos.push(4);  
            if (_胸揉み_UseCk()) exActionNos.push(2); 
            if (_尻揉み_UseCk()) exActionNos.push(1); 
            return SelectExAction();
        case 9: 
            if (_ニップルドーム_UseCk()) return 3; 
            if (_胸揉み_UseCk()) exActionNos.push(2); 
            if (_尻揉み_UseCk()) exActionNos.push(1); 
            return SelectExAction();
        case 59: 
            if (_ひん剥き_UseCk() && BtlData.Turn <= 2) {
                exActionNos.push(0);
            } else {
                // if (!_尻揉み_UseCk() && !_胸揉み_UseCk() && !_股間責め_UseCk()) {
                //     exActionNos.push(0); //予定のエロ行動がすべて取れない場合はひん剥き
                if (_ひん剥き_UseCk()) exActionNos.push(0); 
                if (_尻揉み_UseCk()) exActionNos.push(1);
                if (_胸揉み_UseCk()) exActionNos.push(2);
                if (_股間責め_UseCk()) exActionNos.push(3);
                return SelectExAction();
            }
        case 60: 
        case 189:
            if (_ニップルドーム_UseCk()) return 4; 
            if (_ひん剥き_UseCk() && BtlData.Turn <= 2) {
                exActionNos.push(0);
            } else {
                // if (!_尻揉み_UseCk() && !_胸揉み_UseCk() && !_股間責め_UseCk()) {
                //     exActionNos.push(0); //予定のエロ行動がすべて取れない場合はひん剥き
                if (_ひん剥き_UseCk()) exActionNos.push(0); 
                if (_尻揉み_UseCk()) exActionNos.push(1);
                if (_胸揉み_UseCk()) exActionNos.push(2);
                if (_股間責め_UseCk()) exActionNos.push(3);
                return SelectExAction();
            }
            return 0;
        case 70: 
        case 71: 
            if (BtlData.Turn <= 5) return 0; 
            if (_尻揉み_UseCk()) exActionNos.push(0); 
            if (_胸揉み_UseCk()) exActionNos.push(1); 
            exActionNos.push(2); 
            return SelectExAction();
        case 72: 
            if (_尻揉み_UseCk()) exActionNos.push(1); 
            if (_胸揉み_UseCk()) exActionNos.push(2); 
            if (_ニップルドーム_UseCk()) exActionNos.push(3);
            return SelectExAction();
        case 73: 
            if (_下着剥ぎ取り_UseCk()) exActionNos.push(2);
            if (_ニップルドーム_UseCk()) exActionNos.push(1); 
            exActionNos.push(3); 
            return SelectExAction();
        case 75: 
        case 76: 
        case 95: 
        case 96: 
        case 97: 
        case 98: 
            if ($gameSwitches.value(SW_MinzokuON) && _ひん剥き_UseCk()) {
                return 0;
            }
            if (_ひん剥き_UseCk() && !$gameSwitches.value(SW_キャストオフ)
                && $gameVariables.value(VN_TL2DKigae) != -1) {
                return 0;
            } else {
                if (_尻揉み_UseCk()) exActionNos.push(1); 
                if (_胸揉み_UseCk()) exActionNos.push(2); 
                exActionNos.push(3);
                exActionNos.push(4);
                exActionNos.push(5);
                exActionNos.push(6);
                exActionNos.push(7);
                return SelectExAction();
            }
            break;
        case 77: 
            if (_ひん剥き_UseCk() && Math.random() > 0.5) {
                return 0;
            } else {
                exActionNos.push(1);
                exActionNos.push(2);
                exActionNos.push(3);
                return SelectExAction();
            }
            break;
        default: 
            return -1; 
    }
}
Game_Interpreter.prototype._この戦闘アクセス禁止 = function () {
    if (DeckCard.indexOf(1) != -1) {
        DeckCard.splice(DeckCard.indexOf(1), 1);
        BtlDeck.splice(BtlDeck.indexOf(1), 1);
    }
    this.DelSprite(PN_AWBack);
    this.DelSprite(PN_ACard);
}
Game_Interpreter.prototype._行動Set = function (_SetPtnNo) {
    DoCkCharaArr[DoCkNo]._UsePtnNo = _SetPtnNo - 1;
}
Game_Interpreter.prototype._Re行動Set = function (_SetPtnNo) {
    _SetPtnNo = typeof _SetPtnNo !== 'undefined' ? _SetPtnNo : -1;
    if (_SetPtnNo != -1) {
        MustSetActionNo = _SetPtnNo - 1;
    }
    this.SetEnemyNextAction(DoCkNo, true);        
}
let _他味方キャラが倒れた = function () {
    for (let i = 0; i <= DoCkCharaArr.length - 1; i++) {
        if (i == DoCkNo) continue;
        if (DoCkCharaArr[i]._hp <= 0) return true;
    }
    return false;
}
//味方の体力をチェックする。_CNoには敵キャラNoを入力(-1で自身) , _Varは(0~100)までを入力
let _体力チェック = function (_EmNo, _Var) {
    let _CkMaxHP = 1;
    let _CkNowHP = 1;
    if (_EmNo == -1) {
        _CkMaxHP = DoCkCharaArr[DoCkNo].mhp;
        _CkNowHP = DoCkCharaArr[DoCkNo]._hp;
    } else {
        if (!DoEnemyFlg) return false; 
        for (let i = 0; i <= DoCkCharaArr.length - 1; i++) {
            if (i == DoCkNo) continue; 
            if (DoCkCharaArr[i]._enemyId == _EmNo) {
                _CkMaxHP = DoCkCharaArr[i].mhp;
                _CkNowHP = DoCkCharaArr[i]._hp;
            }
        }
    }
    return ((_CkNowHP / _CkMaxHP) * 100 <= _Var);
}
let _味方生存数 = function () {
    let _emCnt = 0;
    for (let i = 0; i <= DoCkCharaArr.length - 1; i++) {
        if (DoCkCharaArr[i]._hp > 0) _emCnt++;
    }
    return _emCnt;
}
let _BtlEm_Update = Game_Interpreter.prototype.BtlUpdate;
Game_Interpreter.prototype.BtlUpdate = function () {
    _BtlEm_Update.call(this);
    for (let i = 0; i <= NEnemy.length - 1; i++) {
        DoCkNo = i;
        DoCkCharaArr = NEnemy;
        DoEnemyFlg = true;
        this.EnemyActionCheckUpdate();
    }
    for (let i = 1; i <= NPlayer.length - 1; i++) {
        DoCkNo = i;
        DoCkCharaArr = NPlayer;
        DoEnemyFlg = false;
        this.EnemyActionCheckUpdate();
    }
}
