/*
this.PlaySe("nR_selecter");
this.PlaySe("nR_Kettei");
 */
SpriteLayerAutoNo = 150; 
Game_Interpreter.prototype.PreLoadArr = function (_SetArr) {
    for (let i = 0; i <= _SetArr.length - 1; i++) {
        ImageManager.loadBitmap("", _SetArr[i], null, false);
    }
}
let _TextLoadFlg = false;
let _LoadTxtArr = ["GameFont","Makinas","Makinas_S","Cornerstone","GENUINE","Russo_One"];
let _TextLoad = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    _TextLoad.call(this);
    if(!_TextLoadFlg){
        for (let i = 0; i <= _LoadTxtArr.length - 1; i++) {
            D_Text_Cng_font = _LoadTxtArr[i]; 
            this.SpriteStr(PVS_MaxPicNo - 1 - i , "_fonts_" , 20 , -100 , -100 , 255);
            D_Text_Cng_font = ""; 
        }
        _TextLoadFlg = true;
    }
    if($gameSystem.Voice重複禁止 == undefined) {
        $gameSystem.Voice重複禁止 = true;
    }
}
let Tmp_FirstCall = false; 
const VN_シナリオ変数 = 101; 
Game_Interpreter.prototype.JN_EventLoad = function (_種類 , _LoadFlg) {
    for (let i = 0; i <= $dataMap.events.length - 1; i++) {
        if($dataMap.events[i] != null){
            if($dataMap.events[i].name == _種類) return; 
        }
    }
    _LoadFlg = typeof _LoadFlg !== 'undefined' ? _LoadFlg : false; 
    if(_LoadFlg) { 
        let args = new Array(String(_種類), String(1), String(1));
        this.pluginCommand("ERS_MAKE_TEMPLATE", args);
        return;
    }
    if (!Tmp_FirstCall) {
        for (let i = 1; i <= $dataTemplateEvents.length - 1; i++) {
            if ($dataTemplateEvents[i] == null) continue;
            if ($dataTemplateEvents[i].name.indexOf("_") == -1) {
                if($dataTemplateEvents[i].pages[0].image.characterName == "0Event_Mark") continue;
                let _キャラNo = $dataTemplateEvents[i].pages[0].image.characterIndex;
                let _向きNo = $dataTemplateEvents[i].pages[0].image.direction;
                let _パターンNo = $dataTemplateEvents[i].pages[0].image.pattern; 
                let _行No = Math.floor(_キャラNo / 4) * 4;
                switch (_向きNo) {
                    case 2: _行No += 0; break;
                    case 4: _行No += 1; break;
                    case 6: _行No += 2; break;
                    case 8: _行No += 3; break;
                }
                let _列No = (_キャラNo % 4) * 3 + _パターンNo;
                let _変数 = _行No * 12 + _列No + 1;
                $dataTemplateEvents[i].name += "_" + _変数;
            }
        }
        Tmp_FirstCall = true; 
    }
    let _シナリオVar = $gameVariables.value(VN_シナリオ変数);
    let _CkStr = _種類 + "_";
    let _CkEvArr = [];
    for (let i = 0; i <= $dataTemplateEvents.length - 1; i++) {
        if ($dataTemplateEvents[i] == null) continue;
        if ($dataTemplateEvents[i].name.split('_')[0] == _種類){
            _CkEvArr.push($dataTemplateEvents[i]); 
        }
    }
    let _EvNo = -1; 
    for (let i = 0; i <= _CkEvArr.length - 1; i++) {
        let _No = Number(_CkEvArr[i].name.replace(_CkStr, ""));
        if (_No <= _シナリオVar) {
            if (_EvNo < _No) _EvNo = _No;
        }
    }
    if (_EvNo != -1) {
        let args = new Array(String(_CkStr + _EvNo), String(1), String(1));
        this.pluginCommand("ERS_MAKE_TEMPLATE", args);
        return String(_CkStr + _EvNo)
    }
    return "";
}
let EnPicCngStop = false; 
Game_Interpreter.prototype.EnFadeIn = function (_PicName, _time) {
    _time = typeof _time !== 'undefined' ? _time : 15;
    if(EnPicCngStop) return;
    this.SetPictFIn(PN_Ensyutu, _PicName, 0, 0, 0, 0, _time);
}
Game_Interpreter.prototype.EnFadeOut = function (_time) {
    _time = typeof _time !== 'undefined' ? _time : 15;
    if(EnPicCngStop) return;
    this.MovePict(PN_Ensyutu, true, true, _time, 0);
}
const PN_Top_Ensyutu = 495;
Game_Interpreter.prototype.EnFadeIn_2 = function (_PicName, _time) {
    _time = typeof _time !== 'undefined' ? _time : 15;
    if(EnPicCngStop) return;
        this.SetPictFIn(PN_Top_Ensyutu, _PicName, 0, 0, 0, 0, _time);
}
Game_Interpreter.prototype.EnFadeOut_2 = function (_time) {
    _time = typeof _time !== 'undefined' ? _time : 15;
    if(EnPicCngStop) return;
    this.MovePict(PN_Top_Ensyutu, true, true, _time, 0);
}
const SW_HSceneON = 16;
let NS_command102 = Game_Interpreter.prototype.command102;
Game_Interpreter.prototype.command102 = function () {
    for (let i = 0; i <= this._params[0].length - 1; i++) {
        this._params[0][i] = this._params[0][i].replace("\\ow[0]\\c[15]","");
        this._params[0][i] = "\\ow[0]\\c[15]" + this._params[0][i];
    }
    return NS_command102.call(this);
};
Game_Interpreter.prototype.command284 = function() {
    if(this._params[0] == "") {
        this.EventBackCng("_black");
        return true;
    }
    if(this._params[1] != 0){
        this.EventBackCng(this._params[0] , this._params[3]);
    } else {
        this.EventBackCng(this._params[0]);
    }
    return true;
};
let BLog_UpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    BLog_UpdateSc.call(this);
    this.BLogUpdate();
    if(Input.isTriggered('bugfix')) {
        WebPageOpen("https://forms.gle/sQXpn5N22Jd8MS4T8");
    }
}
const PN_LogBK = 380;
let BLog = []; 
let BLogStop = false; 
let NextPicNo = PN_LogBK;
let _BLogEndFlame = 0;
let BLogX = 7;
let BLogY = 7;
const BackLogY差 = 45; 
let _BLogDWait = 0;
Game_Interpreter.prototype.BLogUpdate = function () {
    if (BLog.length > 0) {
        _BLogEndFlame = 0;
        for (let i = 0; i <= BLog.length - 1; i++) {
            BLog[i][0]--; 
        }
        if (BLog[0][0] <= 0) {
            this.MovePict(BLog[0][2], 0 + BLogX, BLog[0][3] - BackLogY差, 15, 0);
            this.MovePict(BLog[0][2] + 1, 30 + BLogX, BLog[0][3] - BackLogY差, 15, 0);
            for (let i = 1; i <= BLog.length - 1; i++) {
                BLog[i][3] -= BackLogY差; 
                if (i <= 4) { 
                    this.MovePict(BLog[i][2], 0 + BLogX, BLog[i][3] + BLogY, 15, 255);
                    this.MovePict(BLog[i][2] + 1, 30 + BLogX, BLog[i][3] + BLogY, 15, 255);
                }
            }
            BLog.shift(); 
            if(BLog.length > 0){
                if(BLog[0][0] <= 0) BLog[0][0] = 10; 
            }
        }
    }
    else 
    {
        if(_BLogEndFlame == 60){
            this.DelPictSpan(PN_LogBK , PN_LogBK + 11);
        } else {
            _BLogEndFlame++;
        }
    }
    for (let i = 0; i <= BLog.length - 1; i++) {
        if (BLog[i][4] == false && _BLogDWait <= 0) { 
            this.SetPict(BLog[i][2], "PN_LogBK", 0 + BLogX, BLog[i][3] + BLogY , 0);
            this.SetPicStr(BLog[i][2] + 1, BLog[i][1], 22, 30 + BLogX, BLog[i][3] + BLogY , 0);
            this.MovePict(BLog[i][2], 0 + BLogX, BLog[i][3] + BLogY , 15);
            this.MovePict(BLog[i][2] + 1, 30 + BLogX, BLog[i][3] + BLogY , 15);
            BLog[i][4] = true;
            _BLogDWait = 15; break;
        }
        if (i == 4) {
            break; 
        }
    }
    _BLogDWait--;
}
Game_Interpreter.prototype.BLogStop = function () {
    BLog = []; BLogStop = true;
    this.BLogDell();
}
Game_Interpreter.prototype.BLogStart = function () {
    BLogStop = false;
}
Game_Interpreter.prototype.BLogAdd = function (SSFlame, SetStr, _ColNo) {
    _ColNo = typeof _ColNo !== 'undefined' ? _ColNo : -1;
    if (SetStr == "") return;
    let _ColStr = "";
    if (_ColNo != -1) {
        _ColStr = "\\C[" + _ColNo + "]";
    }
    BLog.push([SSFlame, _ColStr + SetStr, NextPicNo, BackLogY差 * BLog.length, false]);
    NextPicNo += 2;
    if (NextPicNo >= PN_LogBK + 11) {
        NextPicNo = PN_LogBK;
    }
};
Game_Interpreter.prototype.BLogDell = function () {
    for (let i = PN_LogBK; i <= PN_LogBK + 11; i++) {
        this.DelPict(i);
    }
}
class RdmBgvData {
    constructor(BgvName , Time) {
        this.BgvName = BgvName;
        this.Time = Time;
        this.EndTime = 0;
    }
}
var RdmBgvDataArr = [];
var NowPlayRdmBgvData = null;
var RdmBgvNowIndex = -1;
Game_Interpreter.prototype.SetランダムBGV = function (fileName , Time , Volume) {
    RdmBgvDataArr.push(new RdmBgvData(fileName , Time));
}
Game_Interpreter.prototype.PlayランダムBGV = function () {
    if (RdmBgvDataArr.length == 0) return;
    if (RdmBgvDataArr.length == 1) RdmBgvNowIndex = -1;
    var randomIndex = Math.floor(Math.random() * RdmBgvDataArr.length);
    while (RdmBgvNowIndex == randomIndex) {
        randomIndex = Math.floor(Math.random() * RdmBgvDataArr.length);
    }
    RdmBgvNowIndex = randomIndex;
    NowPlayRdmBgvData = RdmBgvDataArr[RdmBgvNowIndex];
    NowPlayRdmBgvData.EndTime = Date.now() + NowPlayRdmBgvData.Time * 1000;
    this.VBgsPlay(NowPlayRdmBgvData.BgvName);
}
Game_Interpreter.prototype.StopランダムBGV = function () {
    RdmBgvDataArr = [];
    NowPlayRdmBgvData = null;
    this.VBgsStop();
}
let RdmBgvData_Update = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    RdmBgvData_Update.call(this);
    if (NowPlayRdmBgvData != null) {
        if (Date.now() >= NowPlayRdmBgvData.EndTime) {
            this.PlayランダムBGV();
        }
    }
}
let NVoice_UpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    NVoice_UpdateSc.call(this);
}
let NS_DbgUpdate = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    NS_DbgUpdate.call(this);
    if (DbgFlg) {
        let _dbgNo = -1;
        if (Input.isTriggered('dbg_0')) _dbgNo = 0;
        if (Input.isTriggered('dbg_1')) _dbgNo = 1;
        if (Input.isTriggered('dbg_2')) _dbgNo = 2;
        if (Input.isTriggered('dbg_3')) _dbgNo = 3;
        if (Input.isTriggered('dbg_4')) _dbgNo = 4;
        if (Input.isTriggered('dbg_5')) _dbgNo = 5;
        if (Input.isTriggered('dbg_6')) _dbgNo = 6;
        if (Input.isTriggered('dbg_7')) _dbgNo = 7;
        if (Input.isTriggered('dbg_8')) _dbgNo = 8;
        if (Input.isTriggered('dbg_9')) _dbgNo = 9;
        if (_dbgNo != -1) this.Dbg_KeyDown(_dbgNo);
    }
}
let Dbg_HelpTxt = [
    "0キー：◆デバッグコマンド一覧　を閉じる",
    "1キー：TPゲージを100に",
    "2キー：所持金+20000",
    "3キー：味方のHPを0に",
    "4キー：敵のHPを1に",
    "5キー：味方のHPを+10",
    "6キー：敵のHPを+10",
    "7キー：",
    "8キー：",
    "9キー：ギャラリーモードリセット"
];
const _DbgPicNo = PVS_MaxPicNo - 15; 
let _DbgDrawFlg = false;
Game_Interpreter.prototype.Dbg_helpDraw = function () {
    this.SetPict(_DbgPicNo, "system/DbgHelp", NErrPos._x, NErrPos._y);
    D_Text_Cng_font = ""; 
    let fseSet = "\\ow[0]"
    for (let i = 0; i <= Dbg_HelpTxt.length - 1; i++) {
        this.SetPicStr(_DbgPicNo + 1 + i, fseSet + Dbg_HelpTxt[i], 16, NErrPos._x + 34, NErrPos._y + 39 + (20 * i));
        if (i == 10) {
            break;
        }
    }
    D_Text_Cng_font = ""; 
    _DbgDrawFlg = true;
}
Game_Interpreter.prototype.Dbg_KeyDown = function (_DbgNo) {
    if(_DbgDrawFlg){
        this.DelPictSpan(_DbgPicNo , PVS_MaxPicNo);
    }
    let _ckArr;
    switch (_DbgNo) {
        case 1: 
            NPlayer._tp = 100;
            btlSelect_PlTp = 100;
            this.N_Btl_PlStView(PLStPos._x, PLStPos._y, true);
            break;
        case 2: 
            $gameParty.gainGold(20000);
            break;
        case 3: 
            NPlayer._hp = 0;
            this.N_Btl_PlStView();
            break;
        case 4: 
            NEnemy._hp = 1;
            this.N_Btl_EmStView();
            break;
        case 5: 
            NPlayer._hp += 10;
            this.N_Btl_PlStView();
            break;
        case 6: 
            NEnemy._hp += 10;
            this.N_Btl_EmStView();
            break;
        case 7: 
            _ckArr = L2D_GetLive2D("Ta_Rury");
            if(_ckArr.length > 0){
                _ckArr[0].setExpression(5,"H28");
            }
            break;
        case 8: 
            _ckArr = L2D_GetLive2D("Ta_Rury");
            if(_ckArr.length > 0){
                _ckArr[0].delExpression(5);
            }
            break;
        case 9: 
            this.GallaryModeリセット();
            break;
        case 0: 
            if(!_DbgDrawFlg){
                this.Dbg_helpDraw();
            } else {
                _DbgDrawFlg = false;
            }
            break;
    }
    console.log(Dbg_HelpTxt[_DbgNo]);
    if(_DbgNo != 0) _DbgDrawFlg = false;
}
let DebugVN = function (No) {
    console.log("DebugVN:" + $gameVariables.value(No));
}
let ClickSpeedUpOff = false;
Scene_Map.prototype.updateMainMultiply = function() {
    this.updateMain();
    if (this.isFastForward() && !ClickSpeedUpOff) {
        this.updateMain();
    }
};
