let Nupu_RDEvents_Save = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    Nupu_RDEvents_Save.call(this);
    this.PlayRD1EventNo = []; 
    this.PlayRD28EventNo = []; 
    this.PlayRD27EventNo = []; 
    this.PlayRD29EventNo = []; 
};
let RD_EvStop = false;
const RD共通EvNo = 30;
const RD固有EvNo = 31;
const VN_RD中だけ味方No = 1019;
let RD_NowDungeonEcNo = []; 
let RDEventNoSet = function (setArr , _eventCount , notSelArr) {
    notSelArr = typeof notSelArr !== 'undefined' ? notSelArr : [];
    for (let i = 0; i < RD_NowDungeonEcNo.length; i++) {
        notSelArr.push(RD_NowDungeonEcNo[i]);
    }
    if (setArr == undefined) setArr = [];
    let rtnNo = Math.floor(Math.random() * _eventCount);
    if (setArr.length >= _eventCount) {
        while (notSelArr.indexOf(rtnNo) != -1) rtnNo = Math.floor(Math.random() * _eventCount);
        return rtnNo;
    }
    while (setArr.indexOf(rtnNo) != -1)
        rtnNo = Math.floor(Math.random() * _eventCount);
    if (notSelArr.indexOf(rtnNo) != -1) rtnNo = Math.floor(Math.random() * _eventCount);
    if (setArr.indexOf(rtnNo) == -1) setArr.push(rtnNo);
    RD_NowDungeonEcNo.push(rtnNo);
    return rtnNo;
}
Game_Interpreter.prototype.RD1EventNoSet = function () {
    return RDEventNoSet($gameSystem.PlayRD1EventNo , 10);
}
Game_Interpreter.prototype.RD27EventNoSet = function () {
    let notSelNos = []; 
    if ($gameVariables.value(VN_RD中だけ味方No) != 0) notSelNos = [8 , 9];
    return RDEventNoSet($gameSystem.PlayRD28EventNo , 10 , notSelNos);
}
Game_Interpreter.prototype.RD28EventNoSet = function () {
    return RDEventNoSet($gameSystem.PlayRD27EventNo , 11);
}
Game_Interpreter.prototype.RD29EventNoSet = function () {
    return RDEventNoSet($gameSystem.PlayRD29EventNo , 10);
}
Game_Interpreter.prototype.RD_EventEnd = function () {
    RD_Stop = RD_EvStop = false;
    RD_PLMove_On();
}
var RD_EvBtlFlg = false; 
Game_Interpreter.prototype.RD_EvBtlStart = function () {
    RD_BtlFlg = RD_EvBtlFlg = true;
    this.SetCmnEventSc(CN_RdBefBtl); 
}
Game_Interpreter.prototype.RD_EvCardPicDraw = function (_cardId , _count) {
    _count = typeof _count !== 'undefined' ? _count : 1;
    let drawCardArr = [];
    for (let i = 0; i < _count; i++) drawCardArr.push(_cardId);
    this.RD_EvCardPicDrawArr(drawCardArr)
}
Game_Interpreter.prototype.RD_EvCardPicDrawArr = function (_cardArr) {
    let _setX = 0;
    if (_cardArr.length == 2) _setX = -120;
    if (_cardArr.length == 3) _setX = -240;
    this.DelPictSpan(PN_RdEvCard , PN_RdEvCard + 2 * 3 + 1);
    for (let i = 0; i < _cardArr.length; i++) {
        this.CardPicDrawC(_cardArr[i], PN_RdEvCard + i * 2 , PN_RdEvCard + i * 2 + 1,
            610 + i * 240 + _setX , 325 , 0 , 100 , 100);
        this.MovePict(PN_RdEvCard + i * 2 , true , true , 15 , 255 , true , true);
    }
}
Game_Interpreter.prototype.RD_EvCardPicDell = function () {
    for (let i = 0; i < 3; i++) {
        this.MovePict(PN_RdEvCard + i * 2 , true , 340 , 15 , 0 , true , true );
        this.DelPict(PN_RdEvCard + i * 2 + 1);
    }
}
Game_Interpreter.prototype.RD_EvCardGet = function (_cardId , _count) {
    _count = typeof _count !== 'undefined' ? _count : 1;
    let setArr = [];
    for (let i = 0; i < _count; i++) setArr.push(_cardId);
    this.RD_EvCardGetArr(setArr);
}
Game_Interpreter.prototype.RD_EvCardGetArr = function (_cardIdArr) {
    for (let i = 0; i < _cardIdArr.length; i++) {
        $gameSystem.Btl_Chara.FDeck.push(_cardIdArr[i]);
    }
}
Game_Interpreter.prototype.RD_EvFotoniaGet = function (_getCount) {
    $gameVariables.setValue(VN_GetFotn, $gameVariables.value(VN_GetFotn) + _getCount);
    RDBtlSettingData.NextBtl_MpUp += _getCount; 
}
Game_Interpreter.prototype.RD_EvNextBtlMpCng = function (_setCount) {
    RDBtlSettingData.NextBtl_MpUp += _setCount;
}
Game_Interpreter.prototype.RD_EvHpCng = function (_hpCng , drawFlg) {
    drawFlg = typeof drawFlg !== 'undefined' ? drawFlg : true;
    $gameActors._data[1]._hp += Math.round(_hpCng);
    if ($gameActors._data[1]._hp <= 0) $gameActors._data[1]._hp = 1; 
    if ($gameActors._data[1]._hp > $gameActors._data[1].mhp) {
        $gameActors._data[1]._hp = $gameActors._data[1].mhp;
    }
    this.RD_MapHPDraw();
    if (!drawFlg) this.DelSpriteSpan(PN_rbStateWin + 0 , PN_rbStateWin + 1);
}
Game_Interpreter.prototype.RD_EvHp全回復 = function () {
    if ($gameActors._data[1]._hp < $gameActors._data[1].mhp){
        $gameActors._data[1]._hp = $gameActors._data[1].mhp;
        RD_Keep_NPlayer[0]._hp = $gameActors._data[1].mhp;
    }
}
Game_Interpreter.prototype.RD_EvStateAdd = function (_stateId , _setTurn) {
    if (RD_Keep_NPlayer == null) RD_Keep_NPlayer = NPlayer;
    if (NPlayer.length == 0) { 
        this.NPlayer_Setting(false); 
        RD_Keep_NPlayer = NPlayer;
    }
    AddNState(RD_Keep_NPlayer[0]._NState , _stateId , _setTurn);
}
let Bef_RD_SetAdSys = [];
Game_Interpreter.prototype.RD_BtlItemLost = function (_lostCount) {
    let rtnFlg = $gameSystem.SetAdSys.length > 0;
    for (let i = 0; i < _lostCount; i++) {
        if ($gameSystem.SetAdSys.length > 0) {
            Bef_RD_SetAdSys.push($gameSystem.SetAdSys[i]);
            $gameSystem.SetAdSys.shift(); 
            i--;
        }
    }
    return rtnFlg;
}
Game_Interpreter.prototype.RD_BtlItemGet = function (_GetItemId) {
    $gameParty.gainItem($dataItems[_GetItemId], 1)
    if ($gameSystem.SetAdSys.length < 3) {
        $gameSystem.SetAdSys.push(_GetItemId); 
    }
}
Game_Interpreter.prototype.RD_RndCardLost = function (_lostCount) {
    for (let i = 0; i <= _lostCount - 1; i++) {
        if ($gameSystem.Btl_Chara.FDeck.length > 0) {
            let lostNo = Math.floor( Math.random() * $gameSystem.Btl_Chara.FDeck.length);
            $gameSystem.Btl_Chara.FDeck.splice(lostNo , 1);
        }
    }
}
Game_Interpreter.prototype.RD_CngMaxHp = function (_cngMaxHp) {
    RD_CngMaxHpFunc(Math.floor(_cngMaxHp));
}
let RD_CngMaxHpFunc = function (_cngMaxHp) {
    if (RD_Keep_NPlayer != null && RD_Keep_NPlayer.length > 0) {
        RD_Keep_NPlayer[0]._hp += _cngMaxHp;
        RD_Keep_NPlayer[0]._paramPlus[0] += _cngMaxHp;
    }
    if ($gameActors._data.length > 1) {
        $gameActors._data[1].mhp += _cngMaxHp;
        if ($gameActors._data[1]._hp <= 0) $gameActors._data[1]._hp = 1;
        $gameActors._data[1]._paramPlus[0] += _cngMaxHp;
        RDBtlSettingData.Btl_MaxHpUp += _cngMaxHp;
        $gameActors._data[1]._hp = $gameActors._data[1].mhp;
    }
}
Game_Interpreter.prototype.RD_GoldGet = function (_getGold) {
    RD_GainGold += _getGold;
    if (RD_GainGold < 0) RD_GainGold = 0;
}
Game_Interpreter.prototype.RD_移動カード制限 = function (_setMaisu , _setTurn) {
    RDBtlSettingData.移動カード制限枚数 = _setMaisu;
    RDBtlSettingData.移動カード制限Turn = _setTurn;
    this.DelPictSpan(PN_CHikari_RD , PN_Card_RD + 10);
    this.RD_MoveCardReloadSetup();
}
