const RsPicPath = "pictures/Ronsen/";
const VN_RonJokyo = 16; 
const PN_rsBBlack = 125;
const PN_rsBack = PN_rsBBlack + 1;
const PN_rsMovie = 140;
const PN_rsTop = PN_rsMovie + 4;
const PN_rsCenter = 160;
const PN_rsKkBack = 170;
const PN_rsKkLine = PN_rsKkBack + 1;
const PN_rsKkTxt  = PN_rsKkLine + 1;
const PN_rsHyoka = PN_rsKkTxt  + 6;
const PN_rsRankImg = PN_rsHyoka + 1;
const PN_rsSelect = 460;    
const PN_rsSelTxt = PN_rsSelect + 5; 
const PN_SelTimer_Waku = PN_rsSelTxt + 5; 
const PN_rsTimer = PN_SelTimer_Waku + 1; 

// Fix: Store custom outline width when processing \ow[] and apply it at draw time
var _OriSel_processCharacter = Sprite_Picture.prototype._processCharacter;
Sprite_Picture.prototype._processCharacter = function(textState, bitmap) {
    _OriSel_processCharacter.call(this, textState, bitmap);
    // After processing, if outlineWidth was set, store it for draw time
    if (bitmap.outlineWidth && bitmap.outlineWidth != 4) {
        bitmap._customOutlineWidth = bitmap.outlineWidth;
    }
};

var _OriSel_drawTextOutline = Bitmap.prototype._drawTextOutline;
Bitmap.prototype._drawTextOutline = function(text, tx, ty, maxWidth) {
    if (this._customOutlineWidth > 0) {
        this.outlineWidth = this._customOutlineWidth;
    }
    _OriSel_drawTextOutline.call(this, text, tx, ty, maxWidth);
};

var Ronsen_Flg = false; 
var Ronsen_Btl = 0; 
var Ronsen_flame = 0; 
var _Bef_状況 = 0;
Game_Interpreter.prototype.Ronsen_Start = function () {
    Adv_Stop = Ronsen_Flg = true;
    Ronsen_flame = 0;
    isFastOff = true; 
    this.DelPictSpan(PN_tSelBtn , PN_mL2Base);
    $gameVariables.setValue(VN_RonJokyo ,0); 
    _Bef_状況 = 0; 
    this.SetPictFIn( PN_rsBBlack , "Black" , 0 , 0 , 0 , 0 , 15 , 100);
    this.SetPictFIn( PN_rsTop ,RsPicPath + "PN_rsTop" , 0 , 0 , 0 , 0 , 15);
    this.MoviePlayC(PN_rsMovie , "ronsen1junbi" , false , false , 720 , 405 , 255 , 200 , 200);
    _Rs_Startflame = 0;
}
var _Rs_Startflame = 0;
Game_Interpreter.prototype.Ronsen_Start_Update = function () {
    _Rs_Startflame++;
    return true;
}
Game_Interpreter.prototype.Ronsen_BarPos = function (_Zure , _time) {
    var _time = typeof _time !== 'undefined' ? _time : 60;
    EasingStr = "easeOutBounce";
    this.MovePict( PN_rsCenter , 720 + _Zure , true , 15);
    EasingStr = "";
}
var NAR_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    NAR_NUpdateSc.call(this);
    this.Ronsen_Update();
}
const _rs鼓動Time = 10;
var　_rs選択肢描画Flg = false;
Game_Interpreter.prototype.Ronsen_Update = function () {
    _rsSel_flame = _rsSetTime - $gameTimer._frames;
    if (_OriSelectFlg) {
        var _okFlg = false;
        var _CNo = overPointerCkArr(_rsPicArr);
        this.PicCngColorArr(_rsPicArr);
        if (_CNo != -1){
            _OriSelNo = _CNo - PN_rsSelect;
        }
        this.PicCngColor(PN_rsSelect + _OriSelNo , [100,100,100,0]);
        if(Input.isTriggered('down')) {
            _OriSelNo--;
        }
        if(Input.isTriggered('up')) {
            _OriSelNo++;
        }
        if(_OriSelNo < 0) _OriSelNo = _rsPicArr.length - 1;
        if(_OriSelNo >= _rsPicArr.length) _OriSelNo = 0;
        if(Input.isTriggered('ok')) _okFlg = true;
        if(TouchInput.isTriggered() && _CNo != -1) _okFlg = true;
        if(_okFlg ){
            this._branch[this._indent] = _OriSelNo;
            this._index++;
            _OriSelDrawFlg = false;
            _OriSelectFlg = false;
            this.DelPictSpan(PN_rsSelect , PN_SelTimer_Waku);
        }
    } else {
        if (_rsBarDrawFlg) {
            this.SetPict( PN_rsTimer , RsPicPath + "PN_SelTimer" , _rsSelPos._x , _rsSelPos._y + _rsTimerY , 0);
            this.MovePict( PN_rsTimer , true , true , 255, _rsFinTime);
            this.SetPict( PN_SelTimer_Waku , RsPicPath + "PN_SelTimer_Waku" ,
                _rsSelPos._x - 53 , _rsSelPos._y - 22 + _rsTimerY , 0);
            this.MovePict( PN_SelTimer_Waku , true , true , _rsFinTime);
            _rsBarDrawFlg = false;
            _rs選択肢描画Flg = true;
        } else {
            if (_rsSel_flame > 0) {
                if(_rsSel_flame < _rsSetTime) {
                    var _幅 = (_rsSel_flame / _rsSetTime) * 100;
                    this.MovePict( PN_rsTimer , true , true , 1 , 255 , _幅);
                    if (_rsSel_flame == _rsSetTime) {
                        this.DelPict(PN_rsTimer , PN_SelTimer_Waku);
                        this.DelPict(PN_SelTimer_Waku);
                        _rs選択肢描画Flg = false;
                    }
                }
            }
        }
        if(!$gameTimer._working && _rs選択肢描画Flg){
            this.DelPict(PN_rsTimer , PN_SelTimer_Waku);
            this.DelPict(PN_SelTimer_Waku);
            _rs選択肢描画Flg = false;
        }
    }
    if(!Ronsen_Flg) return;
    if(_Bef_状況 != $gameVariables.value(VN_RonJokyo)){
        EasingStr = "easeOutQuad";
        this.MoviePlayC(PN_rsMovie + 2 , "ronsen6move" , false , true , (720 * (_Bef_状況 / 50)) + 720 , 405 , 255 , 200 , 200);
        _Bef_状況 = $gameVariables.value(VN_RonJokyo);
        var _ronMvPosX = (720 * (_Bef_状況 / 50)) + 720;
        this.MovePict(PN_rsMovie + 1 , _ronMvPosX , true , 30 , 255 , true , true);
        this.MovePict(PN_rsMovie + 2 , _ronMvPosX , true , 30 , 255 , true , true);
        EasingStr = "";
    }
    Ronsen_flame++;
}
var _rsPicArr = []; 
var _rsSelArr = []; 
var _rsSelBusyFlg = false;
var _rsSetTime = 0; 
var _rsSel_flame = 0;
var _Rs選択時間設定 = function(_time){
    var _time = typeof _time !== 'undefined' ? _time : 180;
    var cw = SceneManager._scene._messageWindow._choiceWindow;
     cw._timeLimit = true;
     cw._limitedTime = _time;
}
var _rsSelPos = new Pos2D(538,473);
var _rsFinTime = 5;
var _rsTimerY = 0;
var _OriSelectFlg = false;
var _OriSelDrawFlg = false;
var _OriSelPos = new Pos2D(538,473);
var _OriSelNo = 0;
var OriSelectSetting = function(_PosX , _PosY){
    var _PosX = typeof _PosX !== 'undefined' ? _PosX : _rsSelPos._x;
    var _PosY = typeof _PosY !== 'undefined' ? _PosY : _rsSelPos._y;
    _OriSelectFlg = true;
    _OriSelPos._x = _PosX;
    _OriSelPos._y = _PosY;
}
var NAR_command101 = Game_Interpreter.prototype.command101;
Game_Interpreter.prototype.command101 = function() {
    if(_OriSelectFlg){
        return NAR_command101.call(this);
    } else {
        return NAR_command101.call(this);
    }
}
var NAR_command102 = Game_Interpreter.prototype.command102;
Game_Interpreter.prototype.command102 = function() {
    if(_OriSelectFlg){
        if (!$gameMessage.isBusy() && !_OriSelDrawFlg) {
            _OriSelNo = 0;
            _rsSelArr = this._params[0].clone();
            _rsPicArr = [];
            for (var i = 0; i <= _rsSelArr.length - 1; i++) {
                _rsPicArr.push(PN_rsSelect + i);
                this.SetPict( PN_rsSelect + i , RsPicPath + "PN_rsSelect" ,
                    _OriSelPos._x , _OriSelPos._y - (70 * ( _rsSelArr.length - 1 - i) ) , 0);
                this.MovePict( PN_rsSelect + i , true , true , _rsFinTime);
                this.SetPicStr( PN_rsSelTxt + i , "\\ow[5]\\oc[rgb(231,225,212)]" + _rsSelArr[i] , 23 ,
                    _OriSelPos._x + 25 , _OriSelPos._y + 15 - (70 * ( _rsSelArr.length - 1 - i) ) , 0);
                this.MovePict( PN_rsSelTxt + i , true , true , _rsFinTime);
            }
            _OriSelDrawFlg = true;
        }
    } else {
        return NAR_command102.call(this);
    }
};
var _rsTimerMax = 0;
var _rsBarDrawFlg = false;
var _NupuChoiceStart = Window_ChoiceList.prototype.start;
Window_ChoiceList.prototype.start = function() {
    _NupuChoiceStart.call(this);
    _rsTimerY = (this._list.length - 1) * -100 - 110;
    _rsSetTime = this._limitedTime;
    if(_rsSetTime > 0) {
        _rsBarDrawFlg = true;
    }
}
var NAR_isBusy = Game_Message.prototype.isBusy;
Game_Message.prototype.isBusy = function() {
    return NAR_isBusy.call(this);
};
Game_Interpreter.prototype.Ronsen_End = function () {
    $gameMessageEx.window(0).terminate();   
    $gameMessageEx.window(0).permitClose(); 
    this.DelPictSpan(PN_rsBBlack , PN_rsTop);
    this.DelPictSpan(PN_rsSelect , PN_SelTimer_Waku);
    _rsSel_flame = _rsSetTime = 0;
    Ronsen_Flg = false;
    isFastOff = false;
}
var Rn_RsltTxtArr = ["" , ""];
let Rn_LineDrawFlg = false;
Game_Interpreter.prototype.Ronsen_RzrtLine_Draw = function(){
    this.SetPictFIn( PN_rsKkBack , "Black" , 0 , 0 , 0 , 0 , 10 , 170);
    EasingStr = "easeOutBack";
    this.SetPictC( PN_rsKkLine , RsPicPath + "PN_rsKkLine" , 720 , 405 , 255 , 100 , 0);
    this.MovePict(PN_rsKkLine , true, true , 20 , 255 , 100 , 100);
    EasingStr = "";
    Rn_LineDrawFlg = true;
}
var SW_論戦左詰め = 15;
var VN_論戦TxtPosXZure = 17;
Game_Interpreter.prototype.Ronsen_RzrtTxt_Draw = function(){
    D_Text_Cng_font = "Makinas"; 
    if(!$gameSwitches.value(SW_論戦左詰め)){
        for (var i = 0; i <= Rn_RsltTxtArr.length - 1; i++) {
            var _fsize = 30;
            var _f_sa = _fsize + 7;
            var _yPos = 345 + (i * _f_sa);
            if(Rn_RsltTxtArr.length == 4){
                _fsize = 40; _f_sa -= 5; _yPos = 345 - 20 + (i * _f_sa);
            }
            if(Rn_RsltTxtArr.length == 5){
                _fsize = 35; _f_sa -= 10; _yPos = 345 - 25 + (i * _f_sa);
            }
            var _xPos = 720 + $gameVariables.value(VN_論戦TxtPosXZure);
            this.SetPicStrC( PN_rsKkTxt + i , Rn_RsltTxtArr[i] , _fsize , _xPos , _yPos , 255);
        }
    } else {
        for (var i = 0; i <= Rn_RsltTxtArr.length - 1; i++) {
            var _fsize = 20;
            var _f_sa = 20 + 7;
            var _yPos = 320 + (i * _f_sa);
            if(Rn_RsltTxtArr.length == 4){
                _fsize = 40; _f_sa -= 5; _yPos = 345 - 40 + (i * _f_sa);
            }
            if(Rn_RsltTxtArr.length == 5){
                _fsize = 35; _f_sa -= 10; _yPos = 345 - 55 + (i * _f_sa);
            }
            var _xPos = 410 + $gameVariables.value(VN_論戦TxtPosXZure);
            this.SetPicStr( PN_rsKkTxt + i , Rn_RsltTxtArr[i] , _fsize , _xPos , _yPos , 255);
        }
    }
    D_Text_Cng_font = ""; 
}
var Rn_RzltRankImg = "";
Game_Interpreter.prototype.Ronsen_RzrtRank_Draw = function () {
    EasingStr = "easeOutBack";
    this.SetPictC( PN_rsRankImg , RsPicPath + Rn_RzltRankImg , 715 , 500 , 0 , 150 , 150);
    this.MovePict(PN_rsRankImg , true, true , 15 , 255 , 100 , 100);
    EasingStr = "";
}
Game_Interpreter.prototype.Ronsen_Rzrt_Close = function(){
    for (var i = PN_rsKkBack; i <= PN_rsRankImg; i++) {
        this.MovePict(i , true, true , 15 , 0);
    }
    Rn_LineDrawFlg = false;
}
Game_Interpreter.prototype.Ronsen_Movie_Close = function(){
    this.MovieFOutEnd(PN_rsMovie + 0 , 30);
    this.MovieFOutEnd(PN_rsMovie + 1 , 30);
    this.MovieFOutEnd(PN_rsMovie + 2 , 30);
}
