let MapMoveFlg = true; 
const MapPicPath = "pictures/UI_Map/";
const SN_5章リリースデー = 1377;
let Nupu_MapUpdate = Game_Interpreter.prototype.NUpdate;
Game_Interpreter.prototype.NUpdate = function () {
    this.NupuMapUpdate();
    Nupu_MapUpdate.call(this);
};
let MenuFCount = 0;
Game_Interpreter.prototype.NupuMapUpdate = function () {
    switch (MenuModeStr) {
        case _MMode.Map:
            this.MapUpdate();
            break;
        case _MMode.TalkMode:
            this.TalkUpdate();
            break;
        case _MMode.RdDungeon:
            //this.BLogUpdate(); //画面左上ログアップデート
            break;
        case _MMode.Battle:
            break;
    }
    MenuFCount += 1; 
};
Game_Interpreter.prototype.TalkStart = function () {
    this.TgtClear(); 
    L2dTalkTopAll(); 
    MenuModeStr = _MMode.TalkMode; 
    MenuFCount = 0;
    if (RD_Flg && BtlRomashaFlg) { 
        for (let i = 0; i <= $gameSystem.Btl_Chara.RericArr.length - 1; i++) {
            this.DelSprite(PN_Reric + i);
        }
        L2dAutoFadeOut(MdlRomashaBattle, 15);
        L2dAutoFadeOut("romasha_talk", 15);
    }
}
Game_Interpreter.prototype.TalkEnd = function () {
    this.MoveSprite(PNo_TBack, true, true, 30, 0); 
    L2dTalkTopAll(); 
    if (!RD_BtlFlg && !RD_Flg) { 
        MenuModeStr = _MMode.Map; 
    } else {
        MenuModeStr = _MMode.RdDungeon; 
    }
    if (RD_Flg && BtlRomashaFlg) { 
        if (RD_Mode != EnRDMode.RD_End) {
            L2dFadeMdl("romasha_talk", "rightout", 15, "");
            L2dFadeMdl(MdlRomashaBattle, "rightin", 15, "");
        }
    }
}
Game_Interpreter.prototype.TalkUpdate = function () {
    if (MenuFCount == 1 && !$gameSwitches.value(SW_会話UIOff)) {
        this.SetSprite(PNo_TBack, "Talk_Back", 0, 0);
        this.MoveSprite(PNo_TBack, true, true, 30);
    }
}
let _BefMapSwFlg = null; 
let _BefMsgStr = null; 
let _BefSijiritu = 0;
let _BefMailCount = 0; 
let _Bef_SW_StopEvMove = false; 
let _MMoveFlame = 0; 
let _GameMap_Setup = Game_Map.prototype.setup;
Game_Map.prototype.setup = function (mapId) {
    _GameMap_Setup.apply(this, arguments);
    let _MpName = MetaChecker($dataMap, "MapImg"); 
    if (_MpName != "") $gameVariables.setValue(VN_MapName, _MpName); 
}
let MapUpdate_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    MapUpdate_NUpdateSc.call(this);
    this.BLogUpdate(); 
}
Game_Interpreter.prototype.MapUpdate = function () {
    //this.BLogUpdate(); //画面左上ログアップデート
    if (_MMoveFlame == 1) MapMoveFlg = false; 
    if (MapMoveFlg) {
    }
    if (_BefMapSwFlg != $gameSwitches.value(SW_MapMenu)) {
        if ($gameSwitches.value(SW_MapMenu)) {
            _BefMapSwFlg = true;
            this.MapStart();
        } else {
            _BefMapSwFlg = false;
            this.MapEnd(); 
        }
    }
    if (MenuFCount == 1) { 
        this.MapView(true);
        $gameSwitches.setValue(SW_StopEvMove, _Bef_SW_StopEvMove);
    }
    this.MapUICheck(); 
    if ($gameSwitches.value(SW_MapMenu)) { 
        if (!battleflg) {
            this.MapGoldUpdate();
        }
        if (NewMailCounter() != _BefMailCount) {
            this.MapMCntUpdate(); 
        }
        if ($gameVariables.value(VN_HintStr) != _BefMsgStr ||
            $gameVariables.value(VN_Sijiritu) != _BefSijiritu) {
            _BefSijiritu = $gameVariables.value(VN_Sijiritu);
            this.MapHintUpdate(); 
        }
        if (!MenuFlgSwich) { 
            let _OvPicArr = [PNo_MailIcon, PNo_TelIcon, PN_HStBtn];
            if (!battleflg) {
                if ($gameSwitches.value(SW_MenuHStOff)) this.DelSprite(PN_HStBtn);
                if (!HMenuBtn_ViewFlg) this.DelSprite(PN_HStBtn);
            }
            let _CkOverNoL1 = overPointerSpCkArr(_OvPicArr);
            this.SpriteCngColorArr(_OvPicArr);
            if (_CkOverNoL1 != -1) {
                this.SpriteCngColor(_CkOverNoL1, [80, 80, 80, 0]);
            }
            if (TouchInput.isTriggered() || TouchInput.isRepeated()) {
                switch (_CkOverNoL1) {
                    case PNo_MailIcon: 
                        break;
                    case PNo_TelIcon: 
                        break;
                    case PN_HStBtn: 
                        break;
                }
            }
        }
    }
    this.MapYoubiUpdate(); 
    _MMoveFlame++;
}
let MapDrawFlg = function () {
    return ($gameSwitches.value(SW_MapMenu) && !battleflg && !RD_Flg && !fuzokuFlg && GetSpriteData(PNo_LTBase) == null);
}
Game_Interpreter.prototype.MapUICheck = function () {
    if (MapDrawFlg()) { 
        this.MapView(true);
    }
}
let _BefMapName = "";
const MailCIconNo = 176; 
Game_Interpreter.prototype.MapView = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    if (RdnMapId == $gameMap.mapId()) {
        this.MapDel();
        return; 
    }
    _BefMapSwFlg = true;
    if (MapDrawFlg()) { 
        this.MapUIDraw(_SetFlg);
    } else {
        if (_BefMapSwFlg == $gameSwitches.value(SW_MapMenu)) return;
        _BefMapSwFlg = false;
        this.MapEnd();
    }
}
let MapUITenki = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    MapUITenki.call(this);
    this._TenkiStr = "UI_Map_W_Hare"; 
};
Game_Interpreter.prototype.MapUIDraw = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    let _SetOpi = 255;
    if (_SetFlg) {
        _SetOpi = 0;
    }
    if (RD_Flg) return; 
    if (battleflg) return; 
    ViewSpriteArr_Reset();
    //this.SetPict(177, MapPicPath + "Kari_CharaSet",0,0);//仮キャラセット
    this.SetSprite(PNo_Map, MapPicPath + "UI_Map_CharaBack", 979, 0, _SetOpi);
    this.SetSprite(PNo_LTBase, MapPicPath + "UI_Map_MenuBack", 0, 0, _SetOpi);
    this.SetSprite(PNo_LTBtn + 0, MapPicPath + "UI_Map_MenuBtn", 245, 68, _SetOpi);
    //this.SetSprite(PNo_LTBtn + 1, MapPicPath + "UI_Map_CardBtn", 295, 68, _SetOpi);
    //this.SetSprite(PNo_LTBtn + 2, MapPicPath + "UI_Map_SexBtn", 345, 68, _SetOpi);
    this.MapYoubiSet(); 
    this.SetSpriteC(PNo_Youbi, MapPicPath + "UI_Map_YoubiMenu" + YoubiStr, 20, 20, _SetOpi);
    if ($gameVariables.value(VN_MapName) != 0) {
        this.SetSprite(PNo_MapName, MapPicPath + "MapName/" + $gameVariables.value(VN_MapName), 270, 23, _SetOpi);
    } else {
        let _ckMImg = MetaChecker($dataMap, "MapImg", "_Not_");
        if (_ckMImg == "_Not_") {
            if (_BefMapName != "") { 
                this.SetSprite(PNo_MapName, MapPicPath + "MapName/" + _BefMapName, 270, 23, _SetOpi);
            } else {
                this.DelSprite(PNo_MapName);
            }
        } else {
            _BefMapName = _ckMImg;
            if (_ckMImg != "") {
                this.SetSprite(PNo_MapName, MapPicPath + "MapName/" + _ckMImg, 270, 23, _SetOpi);
            } else {
                this.DelSprite(PNo_MapName);
            }
        }
    }
    if ($gameSystem._TenkiStr == undefined) $gameSystem._TenkiStr = "UI_Map_W_Hare";
    let imgStr = $gameSystem._TenkiStr;
    //if ($gameSwitches.value(SN_5章リリースデー)) imgStr += "_R";
    this.SetSprite(PNo_WezIcon, MapPicPath + imgStr, 100, 0, _SetOpi);
    this.MapMCntUpdate(_SetFlg);
    this.SetSprite(PNo_TelIcon, MapPicPath + "UI_Map_Tel_OFF", 230, 20, _SetOpi);
    if (!$gameSwitches.value(SW_MenuDeckOff) || $gameSwitches.value(SW_ゴブリンパート)) {
        this.DelSprite(PNo_TelIcon);
    }
    // if (!$gameSwitches.value(SW_MenuHStOff) && HMenuBtn_ViewFlg) {
    //     this.SetSpriteC(PN_HStBtn, MapPicPath + "PN_HStBtn", 1390, 770, _SetOpi);
    this.SpriteStr(PNo_CreIco, "c", 20, 185, 65, _SetOpi); 
    this.MapHintUpdate(_SetFlg);
    D_Text_Cng_font = ""; 
    _BefGold = -1; 
    for (let i = 0; i <= ViewSpriteArr.length - 1; i++) {
        this.MoveSprite(ViewSpriteArr[i], true, true, 10);
    }
}
let _YoubiEfFlame = -1; 
let YoubiStr = "";
Game_Interpreter.prototype.MapYoubiSet = function () {
    if ($gameSwitches.value(SW_Yobi + 0)) YoubiStr = "1";
    if ($gameSwitches.value(SW_Yobi + 1)) YoubiStr = "2";
    if ($gameSwitches.value(SW_Yobi + 2)) YoubiStr = "3";
    if ($gameSwitches.value(SW_Yobi + 3)) YoubiStr = "4";
}
Game_Interpreter.prototype.MapYoubiCng = function () {
    _YoubiEfFlame = 72;
    this.MapYoubiSet();
    this.CngSprite(PNo_Youbi, MapPicPath + "UI_Map_YoubiMenu_Cng" + YoubiStr); 
    this.MoveSprite(PNo_Youbi, true, true, 10, true, 80, 80);
}
Game_Interpreter.prototype.MapYoubiUpdate = function () {
    if (_YoubiEfFlame >= 0) {
        this.AngleSprite(PNo_Youbi, -_YoubiEfFlame); 
        if (_YoubiEfFlame == 10) {
            this.MapYoubiSet(); 
            this.CngSprite(PNo_Youbi, MapPicPath + "UI_Map_YoubiMenu" + YoubiStr); 
            this.MoveSprite(PNo_Youbi, true, true, 10, true, 100, 100)
        }
        if (_YoubiEfFlame > 40) _YoubiEfFlame--;
        if (_YoubiEfFlame > 20) _YoubiEfFlame--;
        _YoubiEfFlame--;
    }
}
let _BefGold = -1;
Game_Interpreter.prototype.MapGoldUpdate = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    let _SetOpi = 255;
    if (_SetFlg) {
        _SetOpi = 0;
    }
    let _GetGold = $gameParty._gold; 
    D_Text_Cng_font = "Makinas"; 
    let _goldX = 0;
    switch (String(_GetGold).length) {
        case 1:
            _goldX = 171;
            break;
        case 2:
            _goldX = 157;
            break;
        case 3:
            _goldX = 147;
            break;
        case 4:
            _goldX = 132;
            break;
        case 5:
            _goldX = 125;
            break;
        case 6:
            _goldX = 115;
            break;
        case 7:
            _goldX = 104;
            break;
    }
    _goldX = 180;
    if (_BefGold != _GetGold && !RD_Flg) {
        _BefGold = _GetGold;
        this.SpriteStrR(PNo_Credit, _GetGold, 19, _goldX, 66, _SetOpi);
    }
}
Game_Interpreter.prototype.MapHintReflesh = function () {
    if (RD_Flg) return; 
    if (battleflg) return; 
    if (!$gameSwitches.value(SW_MapMenu)) return; 
    if (!$gameSwitches.value(SW_HintMessageView)) return; 
    if (RD_EndAftFlame > 0) {
        this.DelSprite(PNo_HintB);
        this.DelSprite(PNo_HintStr);
        RD_EndAftFlame--;
        console.log("RD_EndAftFlame：描画中止中");
    }
    if (!$gameMap.isEventRunning() && !RD_Flg && RD_EndAftFlame <= 0) {
        this.SetSprite(PNo_HintB, MapPicPath + "UI_Map_Hint", 436, 8);
        this.MapHintUpdate();
    } else {
        this.MapHintDel();
    }
}
let MapHint_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    MapHint_NUpdateSc.call(this);
    this.MapHintReflesh();
}
Game_Interpreter.prototype.MapHintDel = function () {
    this.DelSprite(PNo_HintB);
    this.DelSprite(PNo_HintStr);
}
Game_Interpreter.prototype.MapHintUpdate = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    if (RD_EndAftFlame > 0) {
        this.DelSprite(PNo_HintB);
        this.DelSprite(PNo_HintStr);
        return;
    }
    let _SetOpi = 255;
    if (_SetFlg) _SetOpi = 0;
    let _ckStr = $gameVariables.value(VN_HintStr);
    _BefMsgStr = _ckStr;
    if (_ckStr == "街の人を助けて支持率100%まで上げよう！") {
        _ckStr += "（現在:" + $gameVariables.value(VN_Sijiritu) + "％）";
    }
    D_Text_Cng_font = "Makinas"; 
    if ($gameMap.mapId() != RdnMapId && $gameMap.mapId() != BtlStatic.BtlMapID) {
        this.SetSprite(PNo_HintB, MapPicPath + "UI_Map_Hint", 436, 8);
        this.SpriteStr(PNo_HintStr, _ckStr, 16, 495, 16, _SetOpi);
    }
    D_Text_Cng_font = ""; 
}
Game_Interpreter.prototype.MapMCntUpdate = function (_SetFlg) {
    _SetFlg = typeof _SetFlg !== 'undefined' ? _SetFlg : false;
    let _SetOpi = 255;
    if (_SetFlg) {
        _SetOpi = 0;
    }
    let _MailCount = NewMailCounter(); 
    _BefMailCount = _MailCount;
    if (_MailCount > 0) {
        this.SetSprite(PNo_MailIcon, MapPicPath + "UI_Map_Mail_ON", 180, 20, _SetOpi);
        if (_MailCount > 10) _MailCount = 10;
        let _MCountIcon = MailCIconNo + _MailCount - 1;
        this.SpriteStr(PNo_MailCount, "\\I[" + _MCountIcon + "]", 24, 192, 20, _SetOpi); 
    } else {
        this.SetSprite(PNo_MailIcon, MapPicPath + "UI_Map_Mail_OFF", 180, 20, _SetOpi);
        this.DelSprite(PNo_MailCount);
    }
}
let MapDelArr = [
    PNo_Map, PNo_LTBase, PNo_LTBtn + 0, PNo_LTBtn + 1, PNo_LTBtn + 2,
    PNo_MapName, PNo_WezIcon, PNo_MailIcon, PNo_MailCount, PNo_TelIcon,
    PNo_Credit, PNo_CreIco, PNo_HintB, PNo_HintStr
];
let MapDelArrC = [PNo_Youbi];
Game_Interpreter.prototype.MapEnd = function () {
    for (let i = 0; i <= MapDelArr.length - 1; i++) {
        this.MoveSprite(MapDelArr[i], true, true, 10, 0);
    }
    for (let i = 0; i <= MapDelArrC.length - 1; i++) {
        this.MoveSprite(MapDelArrC[i], true, true, 10, 0);
    }
}
Game_Interpreter.prototype.MapDel = function () {
    for (let i = 0; i <= MapDelArr.length - 1; i++)
        this.DelSprite(MapDelArr[i]);
    for (let i = 0; i <= MapDelArrC.length - 1; i++)
        this.DelSprite(MapDelArrC[i]);
}
Game_Interpreter.prototype.MapStart = function () {
    MenuModeStr = _MMode.Map; 
    MenuFCount = 0;
}
Game_Interpreter.prototype.TalkCk = function () {
}
let beflen = 0;  
let stdFlame = 40; 
let dddflame = 5; 
let stdFlame2 = 180; 
let dddflame2 = 0; 
let isstdflg = false;
let setvec;
let isstpflg = false; 
let maxstpflame = 30;
let dstpflame = 0;
let _Reset_AnalogMove = function () {
    beflen = 0;  
    stdFlame = 40; 
    dddflame = 5; 
    stdFlame2 = 180; 
    dddflame2 = 0; 
    isstdflg = false;
    setvec = null;
    isstpflg = false; 
    maxstpflame = 30;
    dstpflame = 0;
}
PlayerMover.prototype.updateVelocity = function () {
    if (Menu強制OpenFlg) return;
    if (this.isInputed()) {
        $gameTemp.clearDestination();
        this.clearTargetPosition();
        this.updateVelocityByInput();
        if (!this.isDashing()) {
            dddflame = stdFlame;
        }
        isstdflg = true;
        isstpflg = false;
        dstpflame = 0;
    } else {
        CharacterMover.prototype.updateVelocity.call(this);
    }
    if (beflen == 0 && this._velVec._len != 0 && this.isDashing()) {
        isstdflg = true;
    }
    if (isstdflg) {
        dddflame += 1;
        if (dddflame >= stdFlame) {
            dddflame = stdFlame;
            dddflame2 += 1; 
            if (dddflame2 >= stdFlame2) {
                dddflame2 = stdFlame2;
            }
        }
        if (this._velVec._len == 0) {
            $gamePlayer._patSpd = 1;
            isstdflg = false;
            isstpflg = true;
            dddflame = 5;
            dddflame2 = 0;
            this.clearTargetPosition();
            $gameTemp.clearDestination(); 
            if (!this.isDashing()) { 
                isstpflg = false;
                dstpflame = 0;
            }
        } else {
            setvec = this._velVec; 
        }
        this._velVec._len *= (stdFlame - (stdFlame - dddflame)) / stdFlame; 
        if (!this.isDashing()) { 
            $gamePlayer._patSpd = 1;
            //$gamePlayer.residual().setValid(false); //残像解除
            dddflame2 = 0;
        }
    }
    if (isstpflg) {
        if ($gameTemp._destinationX != null) {
            isstdflg = true;
            isstpflg = false;
            dstpflame = 0;
        }
        setvec._len *= (maxstpflame - dstpflame) / maxstpflame;
        this._velVec = setvec;
        dstpflame += 1;
        if (dstpflame == maxstpflame) {
            isstpflg = false;
            dstpflame = 0;
        }
    }
    beflen = this._velVec._len; 
};
let Nupu_reserveTransfer = Game_Player.prototype.reserveTransfer;
Game_Player.prototype.reserveTransfer = function (mapId, x, y, d, fadeType) {
    Nupu_reserveTransfer.call(this, mapId, x, y, d, fadeType);
    _Reset_AnalogMove();
    $gamePlayer._mover._tarPosVec = null;
    $gamePlayer._mover._velVec._len = 0;
    _MMoveFlame = 0; 
    MenuFCount = 0; 
    MapMoveFlg = true; 
};
Game_Player.prototype.updateDistination = function () {
    if (!this.canMove() || !this.isMoving()) {
        if ($gameTemp.isDestinationValid()) {
            //$gameTemp.clearDestination(); //Nupu
        }
        if (this.hasMover()) {
            this._mover.clearTargetPosition();
        }
    }
};
Game_CharacterBase.prototype.setDirVec = function (dirVec) {
    this.setDirection(CharacterMover.radToDir8(dirVec.dir()));
};
let NupuradToDir8 = CharacterMover.radToDir8;
CharacterMover.radToDir8 = function (radian) {
    let piDiv8 = Math.PI / 8.0;
    return (
        radian < piDiv8 * -7.0 ? 4 :
            radian < piDiv8 * -5.0 ? 14 :
                radian < piDiv8 * -3.0 ? 8 :
                    radian < piDiv8 * -1.0 ? 16 :
                        radian < piDiv8 * 1.0 ? 6 :
                            radian < piDiv8 * 3.0 ? 12 :
                                radian < piDiv8 * 5.0 ? 2 :
                                    radian < piDiv8 * 7.0 ? 10 :
                                        4
    );
    NupuradToDir8.call(this, radian);
};
let Nupudir8ToRad = CharacterMover.dir8ToRad;
CharacterMover.dir8ToRad = function (dir8) {
    let x = (dir8 % 3 === 0 ? 1.0 : (dir8 % 3 === 1 ? -1.0 : 0.0));
    let y = (dir8 / 3 <= 1 ? 1.0 : (dir8 / 3 > 2 ? -1.0 : 0.0));
    if (dir8 == 16) {
        x = 0.5;
        y = -0.5;
    }
    if (dir8 == 10) {
        x = -0.5;
        y = 0.5;
    }
    if (dir8 == 12) {
        x = 0.5;
        y = 0.5;
    }
    if (dir8 == 14) {
        x = -0.5;
        y = -0.5;
    }
    return Math.atan2(y, x);
    Nupudir8ToRad.call(this, dir8);
}
Game_Interpreter.prototype.TgtClear = function () {
    $gamePlayer._mover.clearTargetPosition(); 
    $gameTemp.clearDestination();
    dstpflame = maxstpflame;
}
let Menu強制OpenFlg_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    Menu強制OpenFlg_NUpdateSc.call(this);
    if (Menu強制OpenFlg) {
        _Reset_AnalogMove(); 
    }
}
let _暗がりImg = "";
let _暗がり演出Flg = false;
let _暗がり_反映Flame = 0;
Game_Interpreter.prototype.Map_暗がり開始 = function (_ImgName, _Flame) {
    _暗がり演出Flg = true;
}
Game_Interpreter.prototype.Map_暗がり解除 = function () {
    _暗がり演出Flg = false;
}
let Map_暗がりUpdate = Game_Interpreter.prototype.NUpdate;
Game_Interpreter.prototype.NUpdate = function () {
    Map_暗がりUpdate.call(this);
    if (_暗がり演出Flg) {
    } else {
    }
}
const MapPicElePath = MapPicPath + "MapElevator/";
const VN_Ele説明No = 985; 
Game_Interpreter.prototype.Map_エレベーター説明表示 = function () {
    var imgStr = MapPicElePath + "PN_EleMap" + $gameVariables.value(VN_Ele説明No);
    this.SetSpriteFIn( PN_EleMap , imgStr , 0 , 0 , 0 , 0 , 15);
}
Game_Interpreter.prototype.Map_エレベーター説明終了 = function () {
    this.DelSprite(PN_EleMap);
}
let Map_性感ゲージViewFlg = false;
Game_Interpreter.prototype.Map_性感ゲージ_Start = function () {
    Map_性感ゲージViewFlg = true;
}
let Map性感ゲージ_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    Map性感ゲージ_NUpdateSc.call(this);
    if (Map_性感ゲージViewFlg && !battleflg) {
        this.SeikanUpdate();
    }
}
Game_Interpreter.prototype.Map_性感ゲージ_End = function () {
    Map_性感ゲージViewFlg = false;
    this.DelSpriteSpan(PN_SGbase , PN_SGHart);
}
