let SkillPic = "pictures/Adv/L1_Skill/";
const PN_SkillWin = PN_Layer1 + 5;
const PN_SkillTxt = PN_SkillWin + 1;
const PN_SkillLTxt = PN_SkillTxt + 4;
const PN_SkillLGold = PN_SkillLTxt + 4;
const PN_SkillBtn = PN_SkillLGold + 1;
const PN_SkillName = PN_SkillBtn + 15;
const PN_SkillLv = PN_SkillName + 15;
const PN_skCkWin = PN_SkillLv + 15;
const PN_skCkTxt = PN_skCkWin + 1;
const SkillSpNo = 6; 
let Skill保存 = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    Skill保存.call(this);
    this.SkillFZure = new Pos2D(0, 0); 
    this.SkillFuki = []; 
}
let SkillFuki_ReSet = function () { 
    $gameSystem.SkillFuki = [];
}
let SkillFuki_Zure = function (_SetX, _SetY) {
    _SetX = typeof _SetX !== 'undefined' ? _SetX : 0;
    _SetY = typeof _SetY !== 'undefined' ? _SetY : 0;
    $gameSystem.SkillFZure = new Pos2D(_SetX, _SetY);
}
let SkillFuki_Set = function (_Type, _Self1, _Self2, _吹形) { 
    _Self1 = typeof _Self1 !== 'undefined' ? _Self1 : "";
    _Self2 = typeof _Self2 !== 'undefined' ? _Self2 : "";
    _吹形 = typeof _吹形 !== 'undefined' ? _吹形 : 0;
    $gameSystem.SkillFuki.push(new SFuki(_Type, _Self1, _Self2, _吹形));
}
Game_Interpreter.prototype.AllSkillReset = function () {
    var actor = $gameActors.actor(1);
    actor.skills().forEach(skill => {
        actor.forgetSkill(skill.id);
    });
    $gameSystem.PL_SkillArr = [1 , 9 , 65 , 81 , 0 , 0, 0 , 0];
    for (let i = 0; i <= $gameSystem.PL_SkillArr.length - 1; i++) {
        $gameActors.actor(1).learnSkill($gameSystem.PL_SkillArr[i]); 
    }
}
let _ASkill_NoSelReset = false;
Game_Interpreter.prototype.Adv_L1SkillView_Reload = function () {
    _ASkill_NoSelReset = true;
    this.Adv_L1SkillView(true);
    _ASkill_NoSelReset = false;
}
let skill_flame = 0;
let _skill_Layer = 0; 
let _skill_SelX = 0;
let _skill_SelY = 0;
let _skill_TcPic = []; 
Game_Interpreter.prototype.Adv_L1SkillView = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false; 
    skill_flame = _skill_Layer = 0;
    if (!_ASkill_NoSelReset) {
        _skill_SelX = _skill_SelY = 0;
    }
    _shopFukiflame = 0;
    _shopFukiVNo = 0;
    _ShopFuki = $gameSystem.SkillFuki.slice();
    _ShopFZure = $gameSystem.SkillFZure;
    for (let i = 0; i <= $gameSystem.PL_SkillArr.length - 1; i++) {
        $gameActors.actor(1).learnSkill($gameSystem.PL_SkillArr[i]); 
    }
    _skill_TcPic = [];
    D_Text_Cng_font = "Makinas"; 
    let _BWPs = new Pos2D(588, 476);
    for (let i = 0; i <= 3 - 1; i++) {
        for (let j = 0; j <= 5 - 1; j++) {
            let _skillNo = 1 + (i * (8 * 5)) + (8 * j);
            let _LvNo = 0;
            for (let k = 0; k <= 5 - 1; k++) {
                if ($gameActors.actor(1).isLearnedSkill(_skillNo + k)) {
                    _LvNo = k + 1
                }
            }
            let _ckLv = _LvNo - 1;
            if (_ckLv < 0) _ckLv = 0;
            let _CkSkill = $dataSkills[_skillNo + _ckLv];
            let _ZureX = 274;
            let _ZureY = 65;
            let _opi = 255;
            if (!_SetFlg) {
                _opi = 0;
                this.Shop_TypeSet(Ftype.初期);
            }
            this.SetPict(PN_SkillBtn + (i * 5) + j, BtlPic + "PN_bEmBltWin",
                _BWPs._x + (_ZureX * i), _BWPs._y - (j * _ZureY), _opi);
            _skill_TcPic.push(PN_SkillBtn + (i * 5) + j);
            let PsvNo = MetaChecker(_CkSkill, "Psv", 0);
            let _CostStr = "PN_Cost_";
            if (PsvNo > 0) {
                _CostStr = "PN_Cost_brank";
            } else {
                if (_CkSkill.mpCost > 0) {
                    _CostStr += "m" + _CkSkill.mpCost; 
                } else if (_CkSkill.tpCost > 0) {
                    _CostStr += "p" + _CkSkill.tpCost; 
                }
            }
            this.SetPict(PN_Cmp, BtlPic + _CostStr, 0, 0, 0);
            this.CmpPict(PN_SkillBtn + (i * 5) + j, PN_Cmp, 177, 0);
            this.MovePict(PN_SkillBtn + (i * 5) + j, true, true, 15);
            this.SetPicStr(PN_SkillName + (i * 5) + j, "\\I[" + _CkSkill.iconIndex + "]" + _CkSkill.name,
                22, _BWPs._x + 9 + (_ZureX * i), _BWPs._y + 11 - (j * _ZureY), _opi);
            this.MovePict(PN_SkillName + (i * 5) + j, true, true, 15);
            if (MetaChecker(_CkSkill, "MaxLv", false) != false) {
                this.SetPict(PN_SkillLv + (i * 5) + j, SkillPic + "PN_ISkLv_5",
                    _BWPs._x + (_ZureX * i) + 161, _BWPs._y - (j * _ZureY) + 35, _opi);
            } else {
                this.SetPict(PN_SkillLv + (i * 5) + j, SkillPic + "PN_ISkLv_" + _LvNo,
                    _BWPs._x + (_ZureX * i) + 161, _BWPs._y - (j * _ZureY) + 35, _opi);
            }
            this.MovePict(PN_SkillLv + (i * 5) + j, true, true, 15);
            if (_LvNo <= 4) { 
                let _CkCost = MetaChecker(_CkSkill, "Cost", 0);
                _CkCost = Math.floor(_CkCost * ((100 - $gameVariables.value(VN_スキル割引)) / 100))
                if ($gameParty._gold < _CkCost) {
                    this.MovePict(PN_SkillBtn + (i * 5) + j, true, true, 15, 150);
                    this.MovePict(PN_SkillName + (i * 5) + j, true, true, 15, 150);
                    this.MovePict(PN_SkillLv + (i * 5) + j, true, true, 15, 150);
                }
            }
        }
    }
    D_Text_Cng_font = ""; 
    this.Adv_L1Skill_DrawTxt();
    this.MovePict(PN_AdvSell, _BWPs._x - 15 + (274 * _skill_SelX), _BWPs._y + 26 - (65 * _skill_SelY), 5);
    this.Adv_SpGold();
}
Game_Interpreter.prototype.Adv_L1Skill_DrawTxt = function () {
    this.DelPictSpan(PN_SkillTxt, PN_SkillLGold);
    D_Text_Cng_font = "Makinas"; 
    let _skillNo = 1 + (_skill_SelX * (8 * 5)) + (8 * _skill_SelY);
    let _LvNo = -1;
    for (let k = 0; k <= 5 - 1; k++) {
        if ($gameActors.actor(1).isLearnedSkill(_skillNo + k)) {
            _LvNo = k; 
        }
    }
    let _TPs = new Pos2D(591, 622);
    if (_LvNo == -1) {
        this.SetPicStr(PN_SkillTxt + 0, "- Not Learned -", 25, _TPs._x, _TPs._y, 200);
    } else {
        let _CkSkill = $dataSkills[_skillNo + _LvNo];
        let _SetTxt1 = "\\I[" + _CkSkill.iconIndex + "] " + _CkSkill.name;
        this.SetPicStr(PN_SkillTxt + 0, _SetTxt1, 22, _TPs._x, _TPs._y, 255);
        let _CkDescArr = _Skill_SetuGetArr(_skillNo + _LvNo);
        for (let i = 0; i <= _CkDescArr.length - 1; i++) {
            this.SetPicStr(PN_SkillTxt + 1 + i, _CkDescArr[i], 16, _TPs._x + 20, _TPs._y + 46 + (i * 40), 255);
        }
        if (MetaChecker(_CkSkill, "MaxLv", false) != false) {
            this.SetPict(PN_SkillTxt + 3, SkillPic + "PN_ISkLv_5", _TPs._x + 300, _TPs._y);
            D_Text_Cng_font = ""; 
            return;
        }
        this.SetPict(PN_SkillTxt + 3, SkillPic + "PN_ISkLv_" + (_LvNo + 1), _TPs._x + 300, _TPs._y);
    }
    if (_LvNo < 4) {
        _CkSkill = $dataSkills[_skillNo + _LvNo + 1];
        _TPs = new Pos2D(591 + 431, 622);
        let _SetTxt1 = "\\I[" + _CkSkill.iconIndex + "] " + _CkSkill.name;
        this.SetPicStr(PN_SkillLTxt + 0, _SetTxt1, 22, _TPs._x, _TPs._y, 255);
        let _CkDescArr = _Skill_SetuGetArr(_skillNo + _LvNo + 1);
        for (let i = 0; i <= _CkDescArr.length - 1; i++) {
            this.SetPicStr(PN_SkillLTxt + 1 + i, _CkDescArr[i], 16, _TPs._x + 20, _TPs._y + 46 + (i * 40), 255);
        }
        this.SetPict(PN_SkillLTxt + 3, SkillPic + "PN_ISkLv_" + (_LvNo + 2), _TPs._x + 300, _TPs._y);
        _LearnSkillCost = MetaChecker(_CkSkill, "Cost", 0);
        _LearnSkillCost = Math.floor(_LearnSkillCost * ((100 - $gameVariables.value(VN_スキル割引)) / 100))
        this.SetPicStrC(PN_SkillLGold, _LearnSkillCost + "B ", 25, 992, 777);
    }
    D_Text_Cng_font = ""; 
}
let _Skill_SetuGetArr = function (_SkillNo) {
    let _CkSkill = $dataSkills[_SkillNo];
    let _CkDescArr = _CkSkill.description.split(/\n/);
    try {
        let _calc = Math.floor(eval(_CkSkill.damage.formula));
        let _hit = _CkSkill.speed;
        for (let i = 0; i <= _CkDescArr.length - 1; i++) {
            _CkDescArr[i] = _CkDescArr[i].replace("calc", _calc);
            _CkDescArr[i] = _CkDescArr[i].replace("hit", _hit);
        }
    } catch (e) {
    }
    return _CkDescArr;
}
Game_Interpreter.prototype.Adv_L1SkillUpdate = function (_ReFlg) {
    _ReFlg = typeof _ReFlg !== 'undefined' ? _ReFlg : false; 
    if (_ShopStopFlg) {
        this.Shop_FukiDel();
        return;
    }
    if (_ShopEnd_flame > 0) {
        _ShopEnd_flame--;
        if (_ShopEnd_flame == 0) {
            this.Adv_L0_Back();
        }
        return;
    }
    let _mvFlg = false;
    let _okFlg = false;
    let _BWPs = new Pos2D(588, 476);
    switch (_skill_Layer) {
        case 0:
            if (_ReFlg) _mvFlg = true;
            if (Input.isTriggered('up')) {
                _skill_SelY++;
                _mvFlg = true;
            }
            if (Input.isTriggered('down')) {
                _skill_SelY--;
                _mvFlg = true;
            }
            if (Input.isTriggered('left')) {
                _skill_SelX--;
                _mvFlg = true;
            }
            if (Input.isTriggered('right')) {
                _skill_SelX++;
                _mvFlg = true;
            }
            if (_skill_SelX < 0) _skill_SelX = 2;
            if (_skill_SelX >= 3) _skill_SelX = 0;
            if (_skill_SelY < 0) _skill_SelY = 4;
            if (_skill_SelY >= 5) _skill_SelY = 0;
            let _CNo = overPointerCkArr(_skill_TcPic);
            if (_CNo != -1) {
                let _PicNo = _CNo - PN_SkillBtn;
                let _setSklX = Math.floor(_PicNo / 5);
                let _setSklY = _PicNo % 5;
                if (_skill_SelX != _setSklX || _skill_SelY != _setSklY) {
                    _skill_SelX = _setSklX;
                    _skill_SelY = _setSklY;
                    _mvFlg = true;
                }
                if (TouchInput.isTriggered()) _okFlg = true;
            }
            if (!_ReFlg) {
                if (Input.isTriggered('ok')) _okFlg = true;
            }
            if (_mvFlg) {
                SoundManager.playCursor();
                this.MovePict(PN_AdvSell, _BWPs._x - 15 + (274 * _skill_SelX), _BWPs._y + 26 - (65 * _skill_SelY), 5);
                this.Adv_L1Skill_DrawTxt();
            }
            if (skill_flame > 5) {
                if (_okFlg) {
                    SoundManager.playOk();
                    this.Shop_TypeSet(Ftype.確認);
                    let _skillNo = 1 + (_skill_SelX * (8 * 5)) + (8 * _skill_SelY);
                    let _LvNo = 0;
                    for (let k = 0; k <= 5 - 1; k++) {
                        if ($gameActors.actor(1).isLearnedSkill(_skillNo + k)) {
                            _LvNo = k + 1
                            let _CkSkill = $dataSkills[_skillNo + k];
                            if (MetaChecker(_CkSkill, "MaxLv", false) != false) {
                                _LvNo = 5; 
                            }
                        }
                    }
                    if (_LvNo >= 5) {
                        this.Shop_TypeSet(Ftype.最大);
                    } else if ($gameParty._gold < _LearnSkillCost) {
                        this.Shop_TypeSet(Ftype.不足);
                    } else {
                        _LearnSkillNo = _skillNo + _LvNo;
                        this.Adv_L1Skill_YNView();
                    }
                }
                if (Input.isTriggered('escape') || TouchInput.isCancelled()) { 
                    SoundManager.playCancel();
                    this.DelPictSpan(PN_IFuki, PN_IFukiTxt + 1);
                    _ShopEnd_flame = 5; 
                    _ShopStopFlg = true;
                    this.JN_EventLoad("スキル_退店", true);
                }
            }
            break;
        case 1:
            this.Adv_L1SkillYNUpdate();
            break;
    }
    this.Shop_FukiUpdate(); 
    skill_flame++;
}
const VN_習得スキルNo = 25;
let _sZureX = 34;
let _sZureY = -16;
let _LearnSkillNo = 0;
let _LearnSkillCost = 0;
Game_Interpreter.prototype.Adv_L1Skill_YNView = function () {
    _skill_Layer = 1;
    _sYNSel = 0;
    習得スキルNo = _LearnSkillNo;
    _CkSkill = $dataSkills[習得スキルNo];
    $gameVariables.setValue(VN_習得スキルNo, 習得スキルNo);
    let _SetTxt1 = "\\I[" + _CkSkill.iconIndex + "] " + _CkSkill.name;
    D_Text_Cng_font = "Makinas"; 
    this.SetPictFIn(PN_skCkWin, SkillPic + "PN_skCkWin", 0, 0, 0, 0, 10);
    this.SetPicStrC(PN_skCkTxt + 0, "Learn " + _SetTxt1 + "?", 25, 909 + 80, 348, 0);
    this.SetPicStrC(PN_skCkTxt + 1, "Price: " + _LearnSkillCost + "B ", 25, 909 + 80, 348 + 30, 0);
    this.MovePict(PN_skCkTxt + 0, true, true, 10);
    this.MovePict(PN_skCkTxt + 1, true, true, 10);
    D_Text_Cng_font = ""; 
    this.MovePict(PN_AdvSell, 596 + _sYNSel * 163, 401 + 28, 5);
}
let 習得スキルNo = -1;
let CN_スキル習得 = 22;
Game_Interpreter.prototype.Adv_L1SkillYNUpdate = function () {
    let _mvFlg = false;
    if (Input.isTriggered('ok') || TouchInput.isTriggered()) { 
        SoundManager.playOk();
        if (_sYNSel == 1) { 
            _skill_Layer = skill_flame = 0;
            this.DelPictSpan(PN_skCkWin, PN_skCkTxt + 1);
            this.Adv_L1SkillUpdate(true); 
            return;
        }
        _skill_Layer = skill_flame = 0;
        $gameParty.loseGold(_LearnSkillCost);
        $gameActors.actor(1).learnSkill(_LearnSkillNo);
        _CkSkill = $dataSkills[_LearnSkillNo];
        if (_CkSkill.name.indexOf("Lv1") == -1) { 
            for (let i = 0; i <= $gameSystem.PL_SkillArr.length - 1; i++) {
                if ($gameSystem.PL_SkillArr[i] == _LearnSkillNo -1)
                    $gameSystem.PL_SkillArr[i] = _LearnSkillNo; 
            }
        }
        let PsvNo = MetaChecker(_CkSkill, "Psv", 0);
        if (PsvNo > 0) {
            $gameParty.gainItem($dataArmors[PsvNo], 1, true)
            let _CkPowUp = MetaChecker($dataArmors[PsvNo], "PowUp", false)
            if (_CkPowUp != false) {
                $gameParty.gainItem($dataArmors[PsvNo - 1], -1000, true);
            }
        }
        this.SetCmnEventSc(CN_スキル習得);
        this.DelPictSpan(PN_skCkWin, PN_skCkTxt + 1);
        this.Adv_L1SkillUpdate(true); 
        this.Shop_TypeSet(Ftype.購入);
        _ShopStopFlg = true;
        this.JN_EventLoad("スキル_購入", true);
    }
    if (Input.isTriggered('escape') || TouchInput.isCancelled()) { 
        SoundManager.playCancel();
        _skill_Layer = skill_flame = 0;
        this.DelPictSpan(PN_skCkWin, PN_skCkTxt + 1);
        this.Adv_L1SkillUpdate(true); 
        if (!_ShopStopFlg) {
            this.Shop_TypeSet(Ftype.定期);
        }
    }
}
