let btlEnd_flame = 0;
let btlEnd_flame2 = 0;  
let RzRank_Var = 0;     
const _決着SE = "Battle3";
Game_Interpreter.prototype.N_Btl_EndCng = function () {
    btlMode = _btlMode.End;
    btlEnd_flame = 0;
    btlEnd_flame2 = 0;
    RzRank_Var = 0;
    _Btl_StopCmnFlg = true;
    TH_ModeFlg = false;
    switch(WinLoseFlg){
        case _BtlBranch.Win:
            this.SetCmnEventSc(CN_倒した瞬間);
            set_branch = _BtlBranch.Win;
            break;
        case _BtlBranch.Lose:
            this.SetCmnEventSc(CN_自分敗北時);
            set_branch = _BtlBranch.Lose;
            break;
    }
}
Game_Interpreter.prototype.N_Btl_EndUpdate = function () {
    if(_Btl_StopCmnFlg) return;
    if(WinLoseFlg == _BtlBranch.Win) {
        this.N_Btl_Win_Update();
    }
    if (btlEnd_flame == 0) {
        this.PlaySe("Battle3", 90, 150); 
        this.SetSpriteC(PN_bRzChara, BtlPic + "PN_bRzEnst", (1440 / 2), (810 / 2), 255);
        this.MoveSprite(PN_bRzChara, true, true, 45, 255, 110, 110);
    }
    if (btlEnd_flame == 45) {
        let _BaseImg = "";
        switch (WinLoseFlg) {
            case _BtlBranch.Win: 
                _BaseImg = "PN_bRzBase_Win";
                break;
            case _BtlBranch.Lose: 
                _BaseImg = "PN_bRzBase_Lose";
                break;
        }
        this.SetSprite(PN_bRzBase, BtlPic + _BaseImg, 0, 0, 0);
        this.MoveSprite(PN_bRzBase, true, true, 15, 255);
        this.MoveSprite(PN_bRzChara, true, true, 15, 0, 120, 120);
    }
    if (btlEnd_flame == 60) { 
        switch (WinLoseFlg) {
            case _BtlBranch.Win: 
                this.SetSprite(PN_bRzChara, BtlTatie_Pl + "Win", 250, 0, 0);
                break;
            case _BtlBranch.Lose: 
                this.SetSprite(PN_bRzChara, BtlTatie_Pl + "Lose", 250, 0, 0);
                break;
        }
        this.MoveSprite(PN_bRzChara, 300, true, 60, 255);
    }
    if (btlEnd_flame > 120 && btlEnd_flame2 == 0) {
        if (Input.isTriggered('ok') || TouchInput.isPressed()) {
            btlEnd_flame2 = 1;
        }
    }
    if (btlEnd_flame2 > 0){
        if(btlEnd_flame2 == 2){
            if(WinLoseFlg == _BtlBranch.Win){
                _Btl_StopCmnFlg = true;
                this.SetCmnEvent(CN_リザルト表示後); 
            }
        }
        if (btlEnd_flame2 == 30){
            this.N_Btl_Close();
        }
        btlEnd_flame2++;
    }
    btlEnd_flame++;
}
Game_Interpreter.prototype.N_Btl_Win_Update = function () {
    D_Text_Cng_font = "Makinas"; 
    let _RzRankVar = 0;
    let _RzFSize = 45; let _RzTxtYPs = 275;
    SyokinDrawSize = _RzFSize;
    SyokinTxtYPs = _RzTxtYPs;
    _RzRankVar += (100 + 戦闘ターン * -10)
    if (btlEnd_flame == 70) { 
        this.SetSpriteFIn( PN_bRzTxt_1 , BtlPic + "PN_bRzTxt_1" , 0 , 0 , -20 , 0 , 30);
        this.SpriteStr(PN_bRzTxt_1 + 1 , 戦闘ターン + " " , _RzFSize , 387 - 20 , _RzTxtYPs + (75 * 0) , 0);
        this.MoveSprite(PN_bRzTxt_1 + 1 , 387 , true , 30);
    }
    _RzRankVar += 30 - Math.floor(30 * (Rz_合計被ダメージ / NPlayer.mhp));
    if (btlEnd_flame == 80) { 
        this.SetSpriteFIn( PN_bRzTxt_2 , BtlPic + "PN_bRzTxt_2" , 0 , 0 , -20 , 0 , 30);
        this.SpriteStr(PN_bRzTxt_2 + 1 , Rz_合計被ダメージ + " " , _RzFSize , 387 - 20 , _RzTxtYPs + (75 * 1) , 0);
        this.MoveSprite(PN_bRzTxt_2 + 1 , 387 , true , 30);
    }
    _RzRankVar += Math.floor(30 * (Rz_最大与ダメージ / NEnemy.mhp));
    if (btlEnd_flame == 90) { 
        this.SetSpriteFIn( PN_bRzTxt_3 , BtlPic + "PN_bRzTxt_3" , 0 , 0 , -20 , 0 , 30);
        this.SpriteStr(PN_bRzTxt_3 + 1 , Rz_最大与ダメージ + " " , _RzFSize , 387 - 20 , _RzTxtYPs + (75 * 2) , 0);
        this.MoveSprite(PN_bRzTxt_3 + 1 , 387 , true , 30);
    }
    _RzRankVar += Rz_最大チェイン数;
    if (btlEnd_flame == 100) { 
        this.SetSpriteFIn( PN_bRzTxt_4 , BtlPic + "PN_bRzTxt_4" , 0 , 0 , -20 , 0 , 30);
        this.SpriteStr(PN_bRzTxt_4 + 1 , Rz_最大チェイン数 + " " , _RzFSize , 387 - 20 , _RzTxtYPs + (75 * 3) , 0);
        this.MoveSprite(PN_bRzTxt_4 + 1 , 387 , true , 30);
        _Btl_TutorialNo = -1; 
    }
    NPlayer.PsvArr.forEach( _AmmId => {
        let _Armor = $dataArmors[_AmmId];
        for (let i = 0; i <= _Armor.traits.length - 1; i++) {
            if(_Armor.traits[i].code != 11) continue;
            let _Var = Math.floor(_Armor.traits[i].value * 100) - 100;
            switch(_Armor.traits[i].dataId){
                case _Zoku._戦闘評価変化:
                    _RzRankVar = Math.floor(_RzRankVar * _Armor.traits[i].value);
                    break;
                case _Zoku._戦闘後回復: 
                    let _UpHp = Math.floor(NPlayer.b_mhp * (_Var / 100)); 
                    NPlayer._hp += _UpHp;
                    if(NPlayer._hp > NPlayer.b_mhp) NPlayer._hp = NPlayer.b_mhp;
                    if(NPlayer._hp < 0) NPlayer._hp = 0;
                    break;
            }
        }
    });
    NPlayer.PsvArr = []; 
    let _RzRankImg = "Rank_S";
    if(_RzRankVar < 100) _RzRankImg = "Rank_A";
    if(_RzRankVar < 80) _RzRankImg = "Rank_B";
    if(_RzRankVar < 60) _RzRankImg = "Rank_C";
    if(_RzRankVar < 40) _RzRankImg = "Rank_D";
    if(_RzRankVar < 20) _RzRankImg = "Rank_E";
    if (btlEnd_flame == 110) { 
        EasingStr = "easeOutBack";
        this.SetSpriteC( PN_bRzRank , RsPicPath + _RzRankImg , 408 , 664 , 0 , 200 , 200);
        this.MoveSprite( PN_bRzRank , true, true , 10 , 255 , 150 , 150);
        EasingStr = "";
        RzRank_Var = _RzRankVar;
    }
    D_Text_Cng_font = ""; 
}
let SyokinDrawSize = 0;
let SyokinTxtYPs = 0;
Game_Interpreter.prototype._Btl_SyokinDraw = function () { 
    D_Text_Cng_font = "Makinas"; 
    //if ($gameSwitches.value(SW_野試合Flg)  && $gameVariables.value(VN_野試合累計報酬) > 0) {
    if ($gameVariables.value(VN_野試合累計報酬) > 0) {
        this.SetSpriteFIn( PN_bRzTxt_5 , BtlPic + "PN_bRzTxt_5" , 0 , 0 , 0 , 0 , 30);
        this.SpriteStr(PN_bRzTxt_5 + 1 , SCol.Yel + $gameVariables.value(VN_野試合累計報酬) + " B" ,
            SyokinDrawSize , 387 - 170 - 20 , SyokinTxtYPs + (75 * 3) + 70 , 0);
        this.MoveSprite(PN_bRzTxt_5 + 1 , 387 - 170 , true , 30);
    }
    $gameSwitches.setValue(SW_リロード , false); 
    //console.log($gameVariables.value(VN_野試合賞金));
    D_Text_Cng_font = ""; 
}
Game_Interpreter.prototype.N_Btl_Close = function () { 
    _BefBackLogX = BLogX; 
    _BefBackLogY = BLogY;
    BgmLoadSkipFlg = true; 
    battleflg = true;
    THMode_End(); 
    _BtlSaveMoveVarArr = [];
    for (let i = 0; i <= _BtlSaveVNArr.length - 1; i++) {
        _BtlSaveMoveVarArr.push($gameVariables.value(_BtlSaveVNArr[i]));
    }
    this.InstantLoad(); 
}
const RzExPic = "pictures/Btl/Rzlt/";
class _対戦相手 {
    constructor(_DStart , _DEnd , _Name){
        this._DStart = _DStart;
        this._DEnd = _DEnd;
        this._Name = _Name;
    }
}
let RzEx対戦相手Arr = [];
let NoBtlflame = 0;
let NoBtl_SelectNo = 0; 
let NoBtlTcPic = [];
const VN_野試合累計報酬 = 54;
Game_Interpreter.prototype.NoBattle_RzDraw = function () {
    NoBtlflame = 0;
    D_Text_Cng_font = "Makinas"; 
    this.SetSpriteFIn(PN_RzExBack , "Black" , 0 ,0 , 0 , 0 , 15 , 180);
    let RzExPs = new Pos2D(103 , 160);
    NoBtlTcPic = [PN_RzExYesBtn , PN_RzExNoBtn];
    this.SetSpriteFInC(PN_RzExYesBtn , RzExPic + "PN_RzExYesBtn" ,RzExPs._x + 227 , RzExPs._y + 175 , 0 , 0 , 15);
    this.SetSpriteFInC(PN_RzExNoBtn , RzExPic + "PN_RzExNoBtn" , RzExPs._x + 227 + 315 , RzExPs._y + 175 , 0 , 0 , 15);
    if($gameVariables.value(VN_野試合勝利数) != 0) {
        let _数字 = $gameVariables.value(VN_野試合勝利数);
        this.SpriteStr(PN_RzRensyoTxt , "　　　(" + _数字 + " Wins)" , 18 , 331 , 165 , 0);
    } else {
        this.SpriteStr(PN_RzRensyoTxt , "" , 18 , 331 , 165 , 0);
    }
    this.MoveSprite(PN_RzRensyoTxt , true , true , 15);
    this.SpriteStrR(PN_RzExGoldTxt , $gameVariables.value(VN_野試合累計報酬) , 25 ,
        RzExPs._x + 264 , RzExPs._y + 26 , 0);
    this.MoveSprite(PN_RzExGoldTxt , true , true , 15);
    let _bkImgStr = "PN_RzExWin";
    if(!battleflg) {
        NPlayer = new _PL_Data();
        NPlayer._hp = Math.floor(NPlayer.b_mhp * ($gameVariables.value(VN_PLKenko) / 100)) ;
        _bkImgStr = "PN_RzExWin_2";
    }
    this.SetSpriteFIn( PN_RzExWin , RzExPic + _bkImgStr , RzExPs._x , RzExPs._y , 0 , 0 , 15);
    let _Wari = (NPlayer._hp / NPlayer.mhp) * 100;
    this.SetSpriteFIn( PN_RzExHpBar , RzExPic + "PN_RzExHpBar" , RzExPs._x + 396 , RzExPs._y + 34, 0 , 0 , 15 , _Wari);
    this.SetSprite(PN_RzExHpBar , RzExPic + "PN_RzExHpBar" , RzExPs._x + 396 , RzExPs._y + 34 , 0 , _Wari);
    this.MoveSprite(PN_RzExHpBar , true , true , 15 , 255 , true , true);
    this.SpriteStrR(PN_RzExHpTxt , NPlayer._hp +  "/" + NPlayer.mhp , 25 , RzExPs._x + 618 , RzExPs._y + 17 , 0);
    this.MoveSprite(PN_RzExHpTxt , true , true , 15);
    let RzExPs_B = new Pos2D(284 , 442);
    this.SetSpriteFIn( PN_RzExBtlWin , RzExPic + "PN_RzExBtlWin" ,RzExPs_B._x , RzExPs_B._y , 0 , 0 , 15);
    for (let i = 0; i <= RzEx対戦相手Arr.length - 1; i++) {
        this.SetSpriteFInC( PN_RzExBtlDice + i , RzExPic + "PN_RzExBtlDice" ,
            RzExPs_B._x + 77 , RzExPs_B._y + 69 + (50 * i) , 0 , 0 , 15);
        this.SetSpriteFInC( PN_RzExBtlArw + i , RzExPic + "PN_RzExBtlArw" ,
            RzExPs_B._x + 77 + 100 , RzExPs_B._y + 69 + (50 * i) , 0 , 0 , 15);
        this.SetSpriteFIn( PN_RzExBtlLine + i , RzExPic + "PN_RzExBtlLine" ,
            RzExPs_B._x + 38 , RzExPs_B._y + 92 + (50 * i) , 0 , 0 , 15);
        this.SpriteStrC(PN_RzExBtlDiceTxt + i , RzEx対戦相手Arr[i]._DStart + "-" + RzEx対戦相手Arr[i]._DEnd , 25 ,
            RzExPs_B._x + 77 + 47 , RzExPs_B._y + 69 + (50 * i) , 0);
        this.MoveSprite(PN_RzExBtlDiceTxt + i , true , true , 15);
        this.SpriteStr(PN_RzExBtlChaTxt + i , RzEx対戦相手Arr[i]._Name , 25 ,
            RzExPs_B._x + 220 , RzExPs_B._y + 55 + (50 * i) , 0);
        this.MoveSprite(PN_RzExBtlChaTxt + i , true , true , 15);
    }
    oBtl_SelectNo = 0; 
    this.SetSpriteFInC( PN_RzExSelecter , RzExPic + "PN_RzExSelecter" ,
        (RzExPs._x + 227 - 103) + (NoBtl_SelectNo * 315), RzExPs._y + 175 , 0 , 0 , 15);
    D_Text_Cng_font = "";
}
let NoBtl_RzYesFlg = false;
Game_Interpreter.prototype.NoBattle_RzUpdate = function () {
    if(NoBtlflame > 15){
        let _okFlg = false;
        this.SpriteCngColorArr(NoBtlTcPic);
        let _CngSelNo = NoBtl_SelectNo;
        let _CNo = overPointerSpCkArr(NoBtlTcPic);
        if(_CNo != -1) {
            this.SpriteCngColor( _CNo ,[100,100,100,0]);
            _CngSelNo = _CNo - PN_RzExYesBtn;
            if(TouchInput.isTriggered()){
                _okFlg = true;
            }
        }
        if (Input.isTriggered('left')) {
            _CngSelNo = 0;
        }
        if (Input.isTriggered('right')) {
            _CngSelNo = 1;
        }
        let RzExPs = new Pos2D(103 , 160);
        if(NoBtl_SelectNo != _CngSelNo){
            NoBtl_SelectNo = _CngSelNo;
            this.MoveSprite(PN_RzExSelecter , (RzExPs._x + 227 - 103) + (NoBtl_SelectNo * 315) , true , 5);
        }
        if(Input.isTriggered('ok')) _okFlg = true;
        if(_okFlg){ 
            if (NoBtl_SelectNo == 0){ 
                NoBtl_RzYesFlg = true;
                battleflg = false;
            } else {
                NoBtl_RzYesFlg = false;
                NoBtlCnt = 0;
            }
            return false;
        }
    }
    NoBtlflame++;
    return true; 
}
Game_Interpreter.prototype.NoBattle_RzMove = function () {
    this.FOutSpriteSpan(PN_RzExBack , PN_RzExBtlChaTxt + 3 , 15);
}
Game_Interpreter.prototype.EnemySkillSet = function () {
    Em_Skills = []; 
    let _sumRate = 0;
    for (let i = 0; i <= NEnemy.actions.length - 1; i++) {
        if (NEnemy.meetsCondition(NEnemy.actions[i])) {
            Em_Skills.push(NEnemy.actions[i]);
            _sumRate += NEnemy.actions[i].rating;
        }
    }
    NEnemy.DO_Arr = [0, 0, 0, 0, 0, 0];
    let _Em_WillMp = NEnemy.Do_Mp;
    let loopCounter = 0;
    for (let i = 0; i <= NEnemy.DO_Arr.length - 1; i++) {
        if(_Brns_ブレイク数 > 0){
            NEnemy.DO_Arr[i] = -1;
            Em_DoViewArr[i] = true; 
            _Brns_ブレイク数--;
            continue;
        }
        let _ckVar = Math.floor(Math.random() * _sumRate);
        let _ckAVar = 0;
        for (let j = 0; j <= Em_Skills.length - 1; j++) {
            let _ckSkill = $dataSkills[Em_Skills[j].skillId];
            let _addMp = 0;
            if (_ckSkill.mpCost > 0) {
                if (_Em_WillMp - _ckSkill.mpCost < 0) continue;
                _addMp = -_ckSkill.mpCost;
            } else if (_ckSkill.tpCost > 0) {
                if (_Em_WillMp + _ckSkill.tpCost > 9) continue;
                _addMp = _ckSkill.tpCost;
            }
            if (_ckAVar < _ckVar &&
                (_ckAVar + Em_Skills[j].rating) > _ckVar) {
                _Em_WillMp += _addMp;
                NEnemy.DO_Arr[i] = Em_Skills[j].skillId;
            }
            _ckAVar += Em_Skills[j].rating;
        }
        if (loopCounter == 10000) {
            console.log("◆スキル選出エラー対策：待機選択")
            NEnemy.DO_Arr[i] = SNo_待機Skill;
            let _ckSkill = $dataSkills[SNo_待機Skill];
            _Em_WillMp += _ckSkill.tpCost;
        }
        if (NEnemy.DO_Arr[i] == 0) {
            i--; 
            loopCounter++;
            continue;
        }
        loopCounter = 0;
        Em_DoViewArr[i] = this._直感判定(); 
        if(_Btl_TutorialNo != -1) {
            if(_Tuto_EnemyDO_Arr[i] != 0) NEnemy.DO_Arr[i] = _Tuto_EnemyDO_Arr[i];
            if(_Tuto_Em_DoViewArr[i] != -1) Em_DoViewArr[i] = (_Tuto_Em_DoViewArr[i] == 1);
        }
    }
}
Game_Interpreter.prototype._再直感処理 = function () {
    for (let i = 0; i <= NEnemy.DO_Arr.length - 1; i++) {
        if(!Em_DoViewArr[i]){
            Em_DoViewArr[i] = this._直感判定(); 
        }
    }
    this._敵行動再描画();
}
Game_Interpreter.prototype._敵行動再描画 = function () {
    ViewSpriteArr_Reset();
    for (let i = 0; i <= NEnemy.DO_Arr.length - 1; i++) {
        let _opi = 0;
        if (Em_DoViewArr[i]) { 
            if(NEnemy.DO_Arr[i] > 0){
                let _PicNo = PN_bEmBltWin + (i * Cnt_PN_bEmBltWin);
                let _相性Col = Skill相性Col(NEnemy.DO_Arr[i], NPlayer.DO_Arr[i]);
                if (!Em_DoViewArr[i]) _相性Col = ""; 
                let _PosX = DoPos_Em._x;
                let _PosY = DoPos_Em._y + (_BltY差 * i);
                this.N_Btl_Em弾丸Draw(_PicNo , _相性Col , NEnemy.DO_Arr[i] , _PosX , _PosY , _opi , _Blt拡大);
            }
        }
    }
    for (let i = 0; i <= ViewSpriteArr.length - 1; i++) {
        this.MoveSprite(ViewSpriteArr[i], true, true, 15, 255, true, true);
    }
}
