let topIconMoveValue = 0; 
const PlayerMax = 4; 
const EnemyMax = 5; 
let CutIn_防御値表示OFF = false;
let bef_CutIn_防御値表示OFF = false;
let CutIn_防御値表示_OFF = function () {
    CutIn_防御値表示OFF = true;
}
let CutIn_防御値表示_ON = function () {
    CutIn_防御値表示OFF = false;
}
Game_Interpreter.prototype.LastPlayerHpUpdate = function () {
    let i = 0;
    let picNo = NPlayer[i]._PicNo;
    let uiPostion = new Pos2D(0, 0);
    uiPostion._x = NPlayer[i]._screenX + NPlayer[i]._bar_x - 39;
    uiPostion._y = NPlayer[i]._screenY + NPlayer[i]._bar_y + 93;
    this.SetSprite(PN_hpBaP + picNo, BtlPicPath + "PN_hpBsE", uiPostion._x, uiPostion._y , 0);
    this.SetSprite(PN_hpBrP + picNo, BtlPicPath + "PN_hpBrE", uiPostion._x + 21, uiPostion._y + 13
        , 0, (NPlayer[i]._hp / NPlayer[i].mhp) * 100 , 100 );
    this.SpriteStr(PN_T_hpP + picNo, String(NPlayer[i]._hp), 18, uiPostion._x + 42, uiPostion._y - 10 , 0);
}
Game_Interpreter.prototype.BtlUiDraw = function () {
    if (btlUiOffFlg) return;
    D_Text_Cng_font = "Makinas"; 
    this.SpriteStrC(PN_TurnCnt, "Turn_" + BtlData.Turn + " ", 16, 893, 112, 255);
    D_Text_Cng_font = "";
    if (CutIn_防御値表示OFF != bef_CutIn_防御値表示OFF) {
        bef_CutIn_防御値表示OFF = CutIn_防御値表示OFF;
        if (CutIn_防御値表示OFF) {
            for (let i = PN_DifNum_PL; i <= PN_DifNum + 4; i++) {
                Alpha0Sprite(i);
            }
            for (let i = PN_hpBsE; i <= PN_NextPicPL + 3 + 4; i++) {
                Alpha0Sprite(i);
            }
        } else {
            for (let i = PN_DifNum_PL; i <= PN_DifNum + 4; i++) {
                AlphaCngSprite(i , 255);
            }
            for (let i = PN_hpBsE; i <= PN_NextPicPL + 3 + 4; i++) {
                AlphaCngSprite(i , 255);
            }
        }
    }
    for (let i = 0; i <= PlayerMax - 1; i++) {
        Alpha0Sprite(PN_hpBaP + i);
        Alpha0Sprite(PN_hpBrP + i);
        //this.DelSprite(PN_T_hpP + i);
        Alpha0Sprite(PN_IjoP + i);
        Alpha0Sprite(PN_IjoPTn + i);
    }
    for (let i = 0; i <= EnemyMax - 1; i++) {
        Alpha0Sprite(PN_hpBsE + i * 4);
        Alpha0Sprite(PN_hpBrE + i * 4);
        Alpha0Sprite(PN_IjoE + i * 4);
        Alpha0Sprite(PN_IjoPETn + i);
    }
    if (!CutIn_防御値表示OFF) {
        let countIconStr = "";
        let ijoTurnStr = "";
        for (let i = 0; i <= NPlayer.length - 1; i++) {
            if (NPlayer[i]._hp <= 0) {
                Alpha0Sprite(PN_T_hpP + i);
                continue;
            }
            let picNo = NPlayer[i]._PicNo;
            let uiPostion = new Pos2D(0, 0);
            uiPostion._x = NPlayer[i]._screenX + NPlayer[i]._bar_x - 39;
            uiPostion._y = NPlayer[i]._screenY + NPlayer[i]._bar_y + 93;
            _NSprite_AncNo = 0;
            this.SetSprite(PN_hpBaP + picNo, BtlPicPath + "PN_hpBsE", uiPostion._x, uiPostion._y);
            this.SetSprite(PN_hpBrP + picNo, BtlPicPath + "PN_hpBrE", uiPostion._x + 21, uiPostion._y + 13
                , 255, (NPlayer[i]._hp / NPlayer[i].mhp) * 100);
            if (NPlayer[i]._hp != NPlayer[i]._befhp) {
                this.SpriteStr(PN_T_hpP + picNo, String(NPlayer[i]._hp), 18, uiPostion._x + 42, uiPostion._y - 10);
                NPlayer[i]._befhp = NPlayer[i]._hp;
            } else {
                if (GetSpriteData(PN_T_hpP + picNo) != null) {
                    GetSpriteData(PN_T_hpP + picNo)._x = uiPostion._x + 42;
                    GetSpriteData(PN_T_hpP + picNo)._y = uiPostion._y - 10;
                }
            }
            countIconStr = "";
            ijoTurnStr = "";
            ijoStr_2 = "";
            ijoTnStr_2 = "";
            let difenceViewFlg = false;
            let celDmgFlg = false;
            for (let j = 0; j <= NPlayer[i]._NState.length - 1; j++) {
                if (NPlayer[i]._NState[j]._Turn > 999) NPlayer[i]._NState[j]._Turn = 999; 
                if (NPlayer[i]._NState[j]._No == StOneType.CelDmg) {
                    let celDmgState = NPlayer[i]._NState.splice(j, 1)[0];
                    let defIndex = NPlayer[i]._NState.findIndex(state => state._No == StOneType.Defense);
                    if (defIndex != -1) {
                        NPlayer[i]._NState.splice(defIndex + 1, 0, celDmgState);
                    } else {
                        NPlayer[i]._NState.unshift(celDmgState);
                    }
                    break;
                }
            }
            for (let j = 0; j <= NPlayer[i]._NState.length - 1; j++) {
                let stateTurnCount = NPlayer[i]._NState[j]._Turn;
                if (stateTurnCount >= 11) {
                    stateTurnCount = 11;
                }
                let ijoTurnCount = stateTurnCount + Int1IcoNo - 1;
                if (NPlayer[i]._NState[j]._No == StOneType.Defense) { 
                    if (!CutIn_防御値表示OFF) {
                        this.SpriteStr(PN_DifNum_PL + picNo, NPlayer[i]._NState[j]._Turn, 15,
                            uiPostion._x - 13, uiPostion._y + 51);
                    }
                    ijoTurnCount = 0;
                    difenceViewFlg = true;
                    NPlayer[i]._befdef = NPlayer[i]._NState[j]._Turn;
                }
                if (NPlayer[i]._NState[j]._No == StOneType.CelDmg) {
                    N_Sprite_DLayer = 1;
                    let posY = uiPostion._y + 51;
                    if (j >= 5) posY += 40;
                    let posXNo = j % 5;
                    if (!CutIn_防御値表示OFF) {
                        this.SpriteStr(PN_DifNum_PL + picNo, NPlayer[i]._NState[j]._Turn, 15,
                            uiPostion._x - 13 + (posXNo * 36), posY);
                    }
                    ijoTurnCount = 0;
                    celDmgFlg = true;
                    N_Sprite_DLayer = 0;
                }
                if (i == 0 && j >= 5) { 
                    ijoStr_2 += "\\I[" + $dataStates[NPlayer[i]._NState[j]._No].iconIndex + "]";
                    ijoTnStr_2 += "\\I[" + ijoTurnCount + "]";
                } else {
                    countIconStr += "\\I[" + $dataStates[NPlayer[i]._NState[j]._No].iconIndex + "]";
                    ijoTurnStr += "\\I[" + ijoTurnCount + "]";
                }
            }
            if (!difenceViewFlg) {
                NPlayer[i]._befdef = -1;
                Alpha0Sprite(PN_DifNum_PL + picNo);
            }
            if (!celDmgFlg) { 
                N_Sprite_DLayer = 1;
                Alpha0Sprite(PN_DifNum_PL + picNo);
                N_Sprite_DLayer = 0;
            }
            this.SpriteStr(PN_IjoP + picNo, countIconStr, 25, uiPostion._x - 26, uiPostion._y + 26);
            this.SpriteStr(PN_IjoPTn + picNo, ijoTurnStr, 25, uiPostion._x - 26, uiPostion._y + 39);
            if (i == 0) {
                this.SpriteStr(PN_IjoP_2, ijoStr_2, 25, uiPostion._x - 26, uiPostion._y + 26 + 40);
                this.SpriteStr(PN_IjoPTn_2, ijoTnStr_2, 25, uiPostion._x - 26, uiPostion._y + 39 + 40);
            }
            const NextIconY = 5; 
            if (NPlayer[i]._enemyId != undefined && NPlayer[i]._hp >= 0) {
                NPlayer[i]._pWidth = _CkPicWidth(PN_CharacterPL + picNo);
                NPlayer[i]._pHeight = _CkPicHeight(PN_CharacterPL + picNo);
                let skillIconNo = 0;
                let nextSkillId = MetaChecker(GetEnemyData_ENo(-i - 1), "Sico" + (NPlayer[i]._UsePtnNo + 1), -1);
                if (nextSkillId != -1) {
                    skillIconNo = Number(nextSkillId); 
                } else {
                    let mikataNextSkillId = GetEnemyData_ENo(-i - 1).actions[NPlayer[i]._UsePtnNo].skillId; 
                    skillIconNo = $dataSkills[mikataNextSkillId].iconIndex;
                }
                if (CheckStateInType(NPlayer[0]._NState, StType.NotYosoku)) {
                    skillIconNo = 352;
                }
                this.SpriteStr(PN_NextIcoPL + picNo, "\\I[" + skillIconNo + "]", 25,
                    NPlayer[i]._screenX + NPlayer[i]._IcX - 16,
                    NPlayer[i]._screenY + NPlayer[i]._IcY - 50 - (NPlayer[i]._pHeight / 2) + (Math.sin(topIconMoveValue) * NextIconY));
                this.SpriteStr(PN_NextPicPL + picNo, "\\I[" + NexticoNo + "]", 25,
                    NPlayer[i]._screenX + NPlayer[i]._IcX - 16 - 8,
                    NPlayer[i]._screenY + NPlayer[i]._IcY - 67 - (NPlayer[i]._pHeight / 2) + (Math.sin(topIconMoveValue) * NextIconY));
            }
        }
        for (let i = 0; i <= NEnemy.length - 1; i++) {
            countIconStr = "";
            ijoTurnStr = "";
            if (NEnemy[i]._hp <= 0) {
                Alpha0Sprite(PN_T_hpE + i * 4);
                this.DelSprite(PN_DifNum + i); 
                continue;
            }
            if (NEnemy[i]._hp > 0) {
                let enemyPicNo = NEnemy[i]._PicNo;
                if (!NEnemy[i].SpineFlg) {
                    if (GetSpriteData(PN_Character + enemyPicNo) == null) {
                        NEnemy[i]._pWidth = 0;
                        NEnemy[i]._pHeight = 0;
                    } else {
                        NEnemy[i]._pWidth = GetSpriteData(PN_Character + enemyPicNo).width;
                        NEnemy[i]._pHeight = GetSpriteData(PN_Character + enemyPicNo).height;
                    }
                }
                this.SetSprite(PN_hpBsE + enemyPicNo * 4, BtlPicPath + "PN_hpBsE",
                    NEnemy[i]._screenX + NEnemy[i]._bar_x - 50,
                    NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y - 12
                );
                this.SetSprite(PN_hpBsE + enemyPicNo * 4 + 1, BtlPicPath + "PN_hpBrE",
                    NEnemy[i]._screenX + NEnemy[i]._bar_x - 30,
                    NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y + 1,
                    255, (NEnemy[i]._hp / NEnemy[i].mhp) * 100
                );
                if (NEnemy[i]._befhp != NEnemy[i]._hp) {
                    this.SpriteStr(PN_hpBsE + enemyPicNo * 4 + 2, String(NEnemy[i]._hp), 18,
                        NEnemy[i]._screenX + NEnemy[i]._bar_x - 6,
                        NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y - 19);
                    NEnemy[i]._befhp = NEnemy[i]._hp;
                } else {
                    GetSpriteData(PN_hpBsE + enemyPicNo * 4 + 2)._x = NEnemy[i]._screenX + NEnemy[i]._bar_x - 6;
                    GetSpriteData(PN_hpBsE + enemyPicNo * 4 + 2)._y = NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y - 19;
                }
                let difenceViewFlg = false;
                let celDmgFlg = false;
                for (let j = 0; j <= NEnemy[i]._NState.length - 1; j++) {
                    countIconStr += "\\I[" + $dataStates[NEnemy[i]._NState[j]._No].iconIndex + "]";
                    let stateTurnCount = NEnemy[i]._NState[j]._Turn;
                    if (stateTurnCount >= 11) {
                        stateTurnCount = 11;
                    }
                    switch (NEnemy[i]._NState[j]._No) {
                        case StOneType.Defense:
                            ijoTurnStr += "\\I[" + 0 + "]"; 
                            this.SpriteStr(PN_DifNum + enemyPicNo, NEnemy[i]._NState[j]._Turn, 15,
                                NEnemy[i]._screenX + NEnemy[i]._bar_x - 63,
                                NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y + 40
                            );
                            difenceViewFlg = true;
                            break;
                        case StOneType.CelDmg:
                            N_Sprite_DLayer = 1;
                            if (NEnemy[i]._NState[j]._Turn > 999) NEnemy[i]._NState[j]._Turn = 999; 
                            ijoTurnStr += "\\I[" + 0 + "]"; 
                            this.SpriteStr(PN_DifNum + enemyPicNo, NEnemy[i]._NState[j]._Turn, 15,
                                NEnemy[i]._screenX + NEnemy[i]._bar_x - 63,
                                NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y + 40
                            );
                            NEnemy[i]._befdef = NEnemy[i]._NState[j]._Turn;
                            celDmgFlg = true;
                            N_Sprite_DLayer = 0;
                            break;
                        default:
                            let SerIjoTc = stateTurnCount + Int1IcoNo - 1; 
                            ijoTurnStr += "\\I[" + SerIjoTc + "]";
                    }
                }
                if (!CutIn_防御値表示OFF) {
                    this.SpriteStr(PN_hpBsE + enemyPicNo * 4 + 3, countIconStr, 25,
                        NEnemy[i]._screenX + NEnemy[i]._bar_x - 76,
                        NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y + 14
                    );
                    this.SpriteStr(PN_IjoPETn + enemyPicNo, ijoTurnStr, 25,
                        NEnemy[i]._screenX + NEnemy[i]._bar_x - 75,
                        NEnemy[i]._screenY + (NEnemy[i]._pHeight / 2) + NEnemy[i]._bar_y + 26
                    );
                }
                if (!difenceViewFlg) {
                    NEnemy[i]._befdef = -1;
                    Alpha0Sprite(PN_DifNum + enemyPicNo);
                }
                if (!celDmgFlg) {
                    N_Sprite_DLayer = 1;
                    Alpha0Sprite(PN_DifNum + enemyPicNo);
                    N_Sprite_DLayer = 0;
                }
                if (NEnemy[i]._NState.length == 0) {
                    Alpha0Sprite(PN_hpBsE + enemyPicNo * 4 + 3);
                }
                const NextIconY = 5; 
                topIconMoveValue += 0.025;
                let skillIconNo = 0;
                let skillDmg = 0; 
                if (!CutIn_防御値表示OFF) {
                    let metaSIcoStr = MetaChecker(GetEnemyData_ENo(i), "Sico" + (NEnemy[i]._UsePtnNo + 1), -1);
                    try {
                        let nextSkillId = GetEnemyData_ENo(i).actions[NEnemy[i]._UsePtnNo].skillId; 
                        if (metaSIcoStr != -1) {
                            skillIconNo = Number(metaSIcoStr); 
                        } else {
                            skillIconNo = $dataSkills[nextSkillId].iconIndex;
                        }
                        if ($dataSkills[nextSkillId].damage.elementId == DType.Dmg) { 
                            skillDmg = GetSkillCalcValue(i, nextSkillId);
                            skillDmg += MetaChecker($dataSkills[nextSkillId], "Ico追加文字", "");
                        } else {
                            skillDmg = "";
                        }
                    } catch (e) {
                        console.log("敵予測アイコンの表示処理::Err");
                    }
                    if (CheckStateInType(NPlayer[0]._NState, StType.NotYosoku)) { 
                        skillIconNo = 352;
                    }
                    this.SpriteStr(PN_NextIco + enemyPicNo, "\\I[" + skillIconNo + "]", 25,
                        NEnemy[i]._screenX + NEnemy[i]._IcX - 16,
                        NEnemy[i]._screenY + NEnemy[i]._IcY - (NEnemy[i]._pHeight / 2) - 50 + (Math.sin(topIconMoveValue) * NextIconY));
                    this.SpriteStr(PN_NextPic + enemyPicNo, "\\I[" + NexticoNo + "]", 25,
                        NEnemy[i]._screenX + NEnemy[i]._IcX - 16 - 8,
                        NEnemy[i]._screenY + NEnemy[i]._IcY - 17 - (NEnemy[i]._pHeight / 2)
                        - 50 + (Math.sin(topIconMoveValue) * NextIconY));
                    if (!CheckStateInType(NPlayer[0]._NState, StType.NotYosoku)) {
                        let _AStr = "\\ow[3]\\oc[rgb(0,0,0)]"
                        this.SpriteStr(PN_NextDmg + enemyPicNo, _AStr + " " + skillDmg + " ", 18,
                            NEnemy[i]._screenX + NEnemy[i]._IcX - 5,
                            NEnemy[i]._screenY + NEnemy[i]._IcY - (NEnemy[i]._pHeight / 2) - 30 + (Math.sin(topIconMoveValue) * NextIconY));
                    } else {
                        this.DelSprite(PN_NextDmg + enemyPicNo); 
                    }
                }
            }
        }
    }
    if (reloadDeckPicDelFlame >= 0) {
        reloadDeckPicDelFlame--;
        if (reloadDeckPicDelFlame == 0) Alpha0Sprite(PN_DeckReload);
    }
    this.AccGageDraw();
}
Game_Interpreter.prototype.TurnEndBtnDraw = function () {
    this.SetSprite(PN_EdFrame, BtlPicPath + "PN_EdFrame", 777, 595);
    this.SetSprite(PN_EdBtn, BtlPicPath + "PN_EdBtn", 787, 705);
    if (NPlayer[0]._mp < 0) NPlayer[0]._mp = 0;
    if (NPlayer[0]._mp > 9) NPlayer[0]._mp = 9;
    this.SetSprite(PN_EdCount, GetBtlEdCountPic() , 817, 621);
}
Game_Interpreter.prototype.AccGageDraw = function () {
    let ckValue = Getアクセスシンクロ率();
    befAccValue = ckValue;
    let imgNo = Math.floor(ckValue * 21);
    if (imgNo > 0) imgNo--;
    if (AccessFlg)
        this.SetSpriteCell(PN_EdAccGage , BtlPicPath + "PN_EdAccGage" , 21 , imgNo, 803 , 603);
    else
        this.SetSpriteCell(PN_EdAccGage , BtlPicPath + "PN_EdAccGage" , 21 , imgNo, 803 , 603 , 100);
}
let btlUiOffFlg = false;
let savePicSkipArr = []; 
let savePicOpiDataArr = [];
class SavePicOpiData {
    constructor(_PicNo, _opacity) {
        this._PicNo = _PicNo
        this._opacity = _opacity;
    }
}
Game_Interpreter.prototype.Btl_UI_Off = function () {
    btlUiOffFlg = true;
    savePicOpiDataArr = [];
    $gameSystem.BtlAutoFlg = false;
    for (let i = PN_Card; i <= Talk_WinPicNo; i++) {
        let ckSpriteData = GetSpriteData(i);
        if (ckSpriteData != null) {
            this.MoveSprite(i, true, true, 10, 0, true, true);
            savePicOpiDataArr.push(new SavePicOpiData(i, ckSpriteData._opacity));
        }
    }
    for (let i = PN_SGbase; i <= PN_NextPicPL + 3; i++) {
        let ckSpriteData = GetSpriteData(i);
        if (ckSpriteData != null) {
            this.MoveSprite(i, true, true, 10, 0, true, true);
            savePicOpiDataArr.push(new SavePicOpiData(i, ckSpriteData._opacity));
        }
    }
    for (let i = PN_Card; i <= PN_CHikari + 1 - 1; i++) {
        this.MovePict(i, true, true, 10, 0, true, true);
    }
    savePicSkipArr = [];
    for (let i = PN_UseSWaku; i <= PN_UseSTxt + 2; i++) {
        savePicSkipArr.push(i);
    }
    if (LastEffectUISelectUpdate) {
        this.MovePict(PN_AutoBtn, true, true, 10, 0, true, true);
        savePicSkipArr.push(PN_AutoBtn);
    }
    this.DelSprite(PN_SelTurnC);
    for (let i = PN_DifNum_PL; i <= PN_DifNum + 4; i++) {
        for (let j = 0; j < 2; j++) {
            N_Sprite_DLayer = j;
            Alpha0Sprite(i);
            N_Sprite_DLayer = 0;
        }
    }
    for (let i = PN_hpBsE; i <= PN_NextPicPL + 3 + 4; i++) {
        for (let j = 0; j < 2; j++) {
            N_Sprite_DLayer = j;
            Alpha0Sprite(i);
            N_Sprite_DLayer = 0;
        }
    }
    for (let i = PN_SetuWin; i <= PN_TurnC; i++) {
        for (let j = 0; j < 2; j++) {
            N_Sprite_DLayer = j;
            Alpha0Sprite(i);
            N_Sprite_DLayer = 0;
        }
    }
}
Game_Interpreter.prototype.Btl_UI_On = function () {
    btlUiOffFlg = false;
    for (let i = 0; i <= savePicOpiDataArr.length - 1; i++) {
        if (savePicSkipArr.indexOf(savePicOpiDataArr[i]._PicNo) != -1) continue; 
        this.MoveSprite(savePicOpiDataArr[i]._PicNo, true, true, 10, savePicOpiDataArr[i]._opacity, true, true);
    }
    savePicOpiDataArr = [];
    for (let i = PN_Card; i <= PN_CHikari + 1 - 1; i++) {
        this.MovePict(i, true, true, 10, 255, true, true);
    }
    this.MoveSprite(PN_AutoBtn, true, true, 10, 255, true, true);
}
