const _Debug_Ex = false; 
const PN_dBackT = 10;
const PN_dDayTxt = PN_dBackT + 1;
const PN_dDataBase = PN_dDayTxt + 1;
const PN_dHPBar = PN_dDataBase + 1;
const PN_dKaiBar = PN_dHPBar + 2;
const PN_dKpkTxt = PN_dKaiBar + 2
const PN_dKhtTxt = PN_dKpkTxt + 2
const PN_dDinSu = PN_dKhtTxt + 2
const PN_dGijutu = PN_dDinSu + 1
const PN_dStTxt = PN_dGijutu + 2
const PN_dKupkRock = PN_dStTxt + 4;
const PN_dBtn = PN_dKupkRock + 2;
const PN_dRock_Btn = PN_dBtn + 8;
const PN_dNzBack = PN_dRock_Btn + 8;
const PN_dBtnNz = PN_dNzBack + 2;
const PN_dZTurn = PN_dBtnNz + 2;
const PN_dKoyuRock = PN_dZTurn + 2;
const PN_dPtRock = PN_dKoyuRock + 2;
const PN_dPtRockBar = PN_dPtRock + 1;
const PN_dPtRockTxt = PN_dPtRockBar + 1;
const PN_dFukiImg = PN_dPtRockTxt + 1;
const PN_dFukiTxt = PN_dFukiImg + 1;
const PN_dPantiBtn = PN_dFukiTxt + 2;
const PN_dGold = PN_dPantiBtn + 2;
const PN_dSell = 140; 
const SW_奴隷 = 81; 
const SW_屈服ロック解除 = 82; 
const SW_固有ロック解除 = 83; 
const VN_奴隷_種類No = 81;
const VN_奴隷_動員数 = 82;
const VN_奴隷_体力 = 83;
const VN_奴隷_快楽値 = 84;
const VN_奴隷_屈服度 = 85;
const VN_奴隷_開発度 = 86;
const VN_奴隷_技術値 = 87;
const VN_奴隷_嗜虐心 = 88;
const VN_奴隷_ターン数 = 97;
const DPicPath = "pictures/Adv_Dorei/";
let _奴隷_保存変数 = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    _奴隷_保存変数.call(this);
    this.DoreiBtnArr = ["PN_dBtn_Rinkan", "PN_dBtn_Housi", "PN_dBtn_Kyukei",
        "PN_dBtn_Koyu", "PN_dBtn_Party"];
}
const SW_奴隷Btn_Off = 101; 
let _DBtnPicArr = [];
let _dL0_flame = 0; 
let _PtyPosX = 0;
let _PtyPosY = 0;
let _LastBtnY = 0;
const _BtnStartY = 243;
let _Dorei_CngFlg = false;
Game_Interpreter.prototype.Adv_BefDoreiCng = function () {
    NPicLockArr = [PN_Ensyutu]; 
    EnPicCngStop = true;
}
Game_Interpreter.prototype.Adv_DoreiFstStart = function () {
    NPicLockArr = []; 
    EnPicCngStop = false;
    this.EnFadeOut();
}
Game_Interpreter.prototype.Adv_Dorei_Cng = function () {
    this.DelPict(PN_AdvSell);
    this.DelPictSpan(PN_tGBase, PN_Layer1);
    $gameSwitches.setValue(SW_奴隷, true);
    _dorei_Selecter = 0;
    _dL0_flame = 0;
    this.SetPictFIn(PN_dBackT, DPicPath + "PN_dBackT", 0, 0, 0, 0, 5);
    this.Adv_Dorei_TurnDraw();
    let i = 0;
    let _PsX = 40;
    if (i >= 3) _PsX += 300;
    let _PsY = (_BtnStartY + 38) + (i % 3) * 90;
    if (i >= 3) _PsY += 35;
    this.SetPictC(PN_dSell, DPicPath + "PN_mAdvSell", _PsX, _PsY, 255);
    this.Adv_L0Dorei_SelecterMove();
    D_Text_Cng_font = "Cornerstone"; 
    this.SpriteStr(PN_dDayTxt, " " + $gameVariables.value(VN_日付) + " ", 60, 1380, 43, 0);
    this.MovePict(PN_dDayTxt, true, true, 5, 255);
    D_Text_Cng_font = ""; 
    this.Adv_Dorei_UIUpdate(true);
}
Game_Interpreter.prototype.Adv_Dorei_BtnDraw = function () {
    $gameSwitches.setValue(SW_奴隷, true);
    _DBtnPicArr = [];
    for (let i = 0; i <= $gameSystem.DoreiBtnArr.length - 1; i++) {
        let _DBtnPicArr_AddSkip = false;
        let _BtnStr = $gameSystem.DoreiBtnArr[i];
        let _PlsX = 0;
        if (i >= 3) _PlsX = 300;
        let _BtnPsY = _BtnStartY + (90 * (i % 3));
        if (i >= 3) _BtnPsY += 35;
        if (_BtnStr == "PN_dBtn_Rinkan") {
            let _PsX = 229 + 14 + _PlsX;
            let _PsY = _BtnPsY - 14;
            this.SetPictFIn(PN_dNzBack + 0, DPicPath + "PN_dNzBack_1", _PsX, _PsY, 0, 0, 5);
            this.SetPicStrR(PN_dBtnNz + 0, " " + $gameVariables.value(VN_奴隷_動員数) + "  ", 22,
                _PsX + 110, _PsY + 8, 0);
            this.MovePict(PN_dBtnNz + 0, true, true, 5, 255);
        }
        if (_BtnStr == "PN_dBtn_Party") {
            let _PsX = 229 + 14 + _PlsX;
            let _PsY = _BtnPsY - 14;
            _PtyPosX = _PsX - 229;
            _PtyPosY = _PsY;
            this.SetPictFIn(PN_dNzBack + 1, DPicPath + "PN_dNzBack_2", _PsX, _PsY, 0, 0, 5);
            this.SetPicStrR(PN_dBtnNz + 1, " " + $gameVariables.value(VN_奴隷_動員数) * 5 + "  ", 22,
                _PsX + 110, _PsY + 8, 0);
            this.MovePict(PN_dBtnNz + 1, true, true, 5, 255);
            if ($gameVariables.value(VN_奴隷_嗜虐心) < 100 && !_Debug_Ex) {
                this.Adv_Dorei_PartyLock();
                _DBtnPicArr_AddSkip = true;
            }
        }
        if (_BtnStr == "PN_dBtn_Koyu") {
            _BtnStr += $gameVariables.value(VN_奴隷_種類No);
            $gameSwitches.setValue(SW_固有ロック解除, false);
            if ($gameVariables.value(VN_奴隷_屈服度) >= 100) {
                $gameSwitches.setValue(SW_固有ロック解除, true);
            }
            if ($gameVariables.value(VN_奴隷_屈服度) < 100 && !_Debug_Ex) {
                this.SetPict(PN_dKoyuRock + 0, DPicPath + "PN_dKoyuRock", 14 + _PlsX, _BtnPsY, 0);
                this.MovePict(PN_dKoyuRock + 0, true, true, 5, 255);
                let _解除条件 = "Need 100% Submission";
                this.SetPicStrC(PN_dKoyuRock + 1, _解除条件, 25, 145 + 14 + _PlsX, _BtnPsY + 39, 0);
                this.MovePict(PN_dKoyuRock + 1, true, true, 5, 255);
                _DBtnPicArr_AddSkip = true;
            } else {
                this.DelPict(PN_dKoyuRock + 0);
                this.DelPict(PN_dKoyuRock + 1);
            }
        }
        if ($gameSwitches.value(SW_奴隷Btn_Off + i)) {
            _DBtnPicArr_AddSkip = true;
            if (_dorei_Selecter == i) _dorei_Selecter++;
        }
        this.SetPictFIn(PN_dBtn + i, DPicPath + _BtnStr, 14 + _PlsX, _BtnPsY, 0, 0, 5);
        if (!_DBtnPicArr_AddSkip) {
            _DBtnPicArr.push(PN_dBtn + i);
        }
    }
    this.DelPictSpan(PN_dRock_Btn, PN_dRock_Btn + 7);
    for (let i = 0; i <= 6 - 1; i++) {
        if ($gameSwitches.value(SW_奴隷Btn_Off + i)) {
            let _PicData = this.PicObjGet(PN_dBtn + i);
            this.SetPict(PN_dRock_Btn + i, DPicPath + "PN_dBtn_Rock", _PicData._x, _PicData._y, 255);
        }
    }
    this.Adv_L0Dorei_SelecterMove();
}
Game_Interpreter.prototype.Adv_Dorei_TurnDraw = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false; 
    let _Opi = 255;
    if (_SetFlg) {
        _Opi = 0;
    }
    let _BPs = new Pos2D(1150, 13);
    _LastBtnY = _BtnStartY - (90 * ($gameSystem.DoreiBtnArr.length - 1));
    D_Text_Cng_font = "Makinas"; 
    this.SetPict(PN_dZTurn + 0, DPicPath + "PN_dZTurn", _BPs._x, _BPs._y, _Opi);
    this.MovePict(PN_dZTurn + 0, true, true, 5, 255);
    this.SetPicStrC(PN_dZTurn + 1, " " + $gameVariables.value(VN_奴隷_ターン数) + " ", 35,
        _BPs._x + 100, _BPs._y + 44, _Opi);
    this.MovePict(PN_dZTurn + 1, true, true, 5, 255);
    D_Text_Cng_font = ""; 
    this.Adv_Dorei_BtnDraw();
}
Game_Interpreter.prototype.Adv_Dorei_UIDell = function () {
    _Dorei_UiDrawStop = 999999;
    for (let i = PN_dDataBase; i <= PN_dKupkRock; i++) {
        this.MovePict(i, true, true, 5, 0, true, true);
    }
}
Game_Interpreter.prototype.Adv_Dorei_PartyLock = function () {
    this.SetPict(PN_dPtRock, DPicPath + "PN_dPtRock", _PtyPosX, _PtyPosY, 0);
    this.MovePict(PN_dPtRock, true, true, 5, 255);
    this.SetPictR(PN_dPtRockBar, DPicPath + "PN_dPtRockBar", 257 + _PtyPosX, _PtyPosY + 56, 0,
        $gameVariables.value(VN_奴隷_嗜虐心));
    this.MovePict(PN_dPtRockBar, true, true, 5, 255, true);
    let _嗜虐Str = $gameVariables.value(VN_奴隷_嗜虐心) + "%";
    this.SetPicStrR(PN_dPtRockTxt, _嗜虐Str, 22, 251 + _PtyPosX, _PtyPosY + 28, 0);
    this.MovePict(PN_dPtRockTxt, true, true, 5, 255);
}
let _Dorei_UiDrawStop = 0;
Game_Interpreter.prototype.Adv_Dorei_UIUpdate = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false; 
    if (_Dorei_UiDrawStop > 0) {
        _Dorei_UiDrawStop--;
        return;
    }
    let _Fin_Flame = 5; 
    let _opi = 255;
    if (_SetFlg) { 
        this.SetPictFIn(PN_dDataBase, DPicPath + "PN_dDataBase", 0, 0, 0, 0, 5);
        this.Adv_Dorei_BtnDraw(); 
        _opi = 0;
        if ($gameVariables.value(VN_奴隷_嗜虐心) < 100 && !_Debug_Ex) {
            this.Adv_Dorei_PartyLock();
            _DBtnPicArr_AddSkip = true;
        } else {
            this.DelPict(PN_dPtRock);
            this.DelPict(PN_dPtRockBar);
            this.DelPict(PN_dPtRockTxt);
        }
        this.Adv_Dorei_TurnDraw();
    }
    if ($gameVariables.value(VN_奴隷_体力) < 0) {
        $gameVariables.setValue(VN_奴隷_体力, 0);
    }
    this.SetPict(PN_dHPBar + 0, DPicPath + "PN_dHPBar", 1251, 196, _opi, $gameVariables.value(VN_奴隷_体力));
    this.MovePict(PN_dHPBar + 0, true, true, _Fin_Flame, 255, true);
    this.SetPict(PN_dKaiBar + 0, DPicPath + "PN_dKaiBar", 1251, 249, _opi, $gameVariables.value(VN_奴隷_快楽値));
    this.MovePict(PN_dKaiBar + 0, true, true, _Fin_Flame, 255, true);
    D_Text_Cng_font = "Makinas"; 
    let _文字数字 = $gameVariables.value(VN_奴隷_体力) + " / 100";
    this.SetPicStrR(PN_dHPBar + 1, _文字数字, 22, 1436, 166, _opi);
    this.MovePict(PN_dHPBar + 1, true, true, _Fin_Flame, 255);
    _文字数字 = $gameVariables.value(VN_奴隷_快楽値) + "%";
    this.SetPicStrR(PN_dKaiBar + 1, _文字数字, 22, 1436, 220, _opi);
    this.MovePict(PN_dKaiBar + 1, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dKpkTxt + 0, " " + $gameVariables.value(VN_奴隷_屈服度) + "% ", 44, 1374, 343, _opi);
    this.MovePict(PN_dKpkTxt + 0, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dKhtTxt + 0, " " + $gameVariables.value(VN_奴隷_開発度) + " ", 44, 1374, 343 + 116, _opi);
    this.MovePict(PN_dKhtTxt + 0, true, true, _Fin_Flame, 255);
    this.SetPicStrR(PN_dDinSu, " " + $gameVariables.value(VN_奴隷_動員数) + " ", 30, 1415, 542, _opi);
    this.MovePict(PN_dDinSu, true, true, _Fin_Flame, 255);
    this.SetPicStrR(PN_dGijutu + 1, " " + $gameVariables.value(VN_奴隷_技術値) + " ", 30, 1432, 592, _opi);
    this.MovePict(PN_dGijutu + 1, true, true, _Fin_Flame, 255);
    let _口回数 = $gameVariables.value(122) + $gameVariables.value(125)
    this.SetPicStrC(PN_dStTxt + 0, " " + _口回数 + " ", 24, 1356, 675, _opi);
    this.MovePict(PN_dStTxt + 0, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dStTxt + 1, " " + $gameVariables.value(VN_胸経験) + " ", 24, 1356 + 63, 675, _opi);
    this.MovePict(PN_dStTxt + 1, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dStTxt + 2, " " + $gameVariables.value(VN_膣経験) + " ", 24, 1356, 675 + 45, _opi);
    this.MovePict(PN_dStTxt + 2, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dStTxt + 3, " " + $gameVariables.value(VN_尻経験) + " ", 24, 1356 + 63, 675 + 45, _opi);
    this.MovePict(PN_dStTxt + 3, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dKpkTxt + 1, this._屈服度StrGet(), 18, 1374, 343 + 31, _opi);
    this.MovePict(PN_dKpkTxt + 1, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dKhtTxt + 1, this._開発度StrGet(), 18, 1374, 343 + 106 + 31, _opi);
    this.MovePict(PN_dKhtTxt + 1, true, true, _Fin_Flame, 255);
    this.SetPicStrC(PN_dDayTxt, " " + $gameVariables.value(VN_日付) + " ", 80, 1380, 43, _opi);
    this.MovePict(PN_dDayTxt, true, true, _Fin_Flame, 255);
    D_Text_Cng_font = ""; 
}
let _dorei_Layer = 0;
let _dorei_flame = 0;
Game_Interpreter.prototype.Adv_Dorei_Update = function () {
    if (_dL0_flame == 10) {
        this.JN_EventLoad("奴隷"); 
    }
    if (_dL0_flame < 15) {
        _dL0_flame++;
        return;
    }
    if (Rn_LineDrawFlg) return;
    switch (_dorei_Layer) {
        case 0:
            this.Adv_L0Dorei_Update();
            _dL0_flame++;
            break;
    }
    _dorei_flame++;
}
Game_Interpreter.prototype.Adv_ReDoreiEvent = function () {
    console.log("Adv_ReDoreiEvent");
    _dL0_flame = 9;
    this.Adv_Dorei_TurnDraw();
    D_ReUpFlg = true;
}
let _dorei_Selecter = 0;
let D_ReUpFlg = false;
Game_Interpreter.prototype.Adv_L0Dorei_Update = function () {
    let _upFlg = false;
    let _okFlg = false;
    if (Input.isTriggered('up')) {
        _dorei_Selecter_Cng(-1);
        _upFlg = true;
    }
    if (Input.isTriggered('down')) {
        _dorei_Selecter_Cng(+1);
        _upFlg = true;
    }
    if (Input.isTriggered('left')) {
        _dorei_Selecter_Cng(-3);
        _upFlg = true;
    }
    if (Input.isTriggered('right')) {
        _dorei_Selecter_Cng(+3);
        _upFlg = true;
    }
    if (Input.isTriggered('ok')) {
        _okFlg = true;
    }
    if ((Input.isTriggered('escape') || TouchInput.isCancelled()) && _dL0_flame > 30) { 
        this.Dorei_Menu_Open();
    }
    let _CNo = overPointerCkArr(_DBtnPicArr);
    if (_CNo != -1) {
        _CNo -= PN_dBtn;
        if (_CNo != _dorei_Selecter || D_ReUpFlg) {
            D_ReUpFlg = false;
            _dorei_Selecter = _CNo;
            _upFlg = true;
        }
        if (TouchInput.isTriggered()) _okFlg = true;
    }
    if (_upFlg) {
        this.Adv_L0Dorei_SelecterMove();
    }
    if (_okFlg) {
        SoundManager.playOk();
        let _SelStr = $gameSystem.DoreiBtnArr[_dorei_Selecter];
        switch (_SelStr) {
            case "PN_dBtn_Rinkan":
                this.JN_EventLoad("輪姦", true);
                break;
            case "PN_dBtn_Housi":
                this.JN_EventLoad("奉仕", true);
                break;
            case "PN_dBtn_Kyukei":
                this.JN_EventLoad("休憩", true);
                break;
            case "PN_dBtn_Koyu":
                this.JN_EventLoad("固有", true);
                break;
            case "PN_dBtn_Party":
                this.JN_EventLoad("パーティ", true);
                break;
            //     this.JN_EventLoad("ショップ" , true);
        }
    }
}
Game_Interpreter.prototype.Adv_L0Dorei_SelecterMove = function () {
    SoundManager.playCursor();
    let _PsX = 40 + 14;
    if (_dorei_Selecter >= 3) _PsX += 300;
    let _PsY = (_BtnStartY + 38) + (_dorei_Selecter % 3) * 90;
    if (_dorei_Selecter >= 3) _PsY += 35;
    this.MovePict(PN_dSell, _PsX, _PsY, 3);
}
let _dorei_Selecter_Cng = function (_CngVar) {
    _dorei_Selecter += _CngVar;
    if (_dorei_Selecter < 0) _dorei_Selecter = $gameSystem.DoreiBtnArr.length - 1;
    if (_dorei_Selecter >= $gameSystem.DoreiBtnArr.length) _dorei_Selecter = 0;
    while (true) {
        if ($gameSwitches.value(SW_奴隷Btn_Off + _dorei_Selecter)) {
            if (_CngVar > 0) _dorei_Selecter++;
            if (_CngVar < 0) _dorei_Selecter--;
        } else {
            if (_Debug_Ex) return;
            let _CkBtnStr = $gameSystem.DoreiBtnArr[_dorei_Selecter];
            switch (_CkBtnStr) {
                case "PN_dBtn_Koyu":
                    if ($gameSwitches.value(SW_固有ロック解除)) return;
                    if ($gameVariables.value(VN_奴隷_屈服度) >= 100) return;
                    if (_CngVar > 0) _dorei_Selecter++;
                    if (_CngVar < 0) _dorei_Selecter--;
                    break;
                case "PN_dBtn_Party":
                    if ($gameVariables.value(VN_奴隷_嗜虐心) >= 100) return;
                    if (_CngVar > 0) _dorei_Selecter++;
                    if (_CngVar < 0) _dorei_Selecter--;
                    break;
                default:
                    return;
            }
        }
        if (_dorei_Selecter < 0) _dorei_Selecter = $gameSystem.DoreiBtnArr.length - 1;
        if (_dorei_Selecter >= $gameSystem.DoreiBtnArr.length) _dorei_Selecter = 0;
    }
}
