function Scene_Survive_Battle_Map() {
	this.initialize(...arguments);
}

Scene_Survive_Battle_Map.prototype = Object.create(Scene_Message.prototype);
Scene_Survive_Battle_Map.prototype.constructor = Scene_Survive_Battle_Map;

Scene_Survive_Battle_Map.prototype.initialize = function () {
	Scene_Message.prototype.initialize.call(this);
	this._waitCount = 0;
	this._encounterEffectDuration = 0;
	this._mapLoaded = false;
	this._touchCount = 0;
	this._menuEnabled = false;

	SURVIVOR_DATA.WeaponList = [];
	SURVIVOR_DATA.MobList = [];

	$gameTemp.SURVIVOR_player_data = {
		level: 1,
		death_count: 0,
		maxhp: SURVIVOR_CONST.PLAYER_BASE_HP,
		nowhp: SURVIVOR_CONST.PLAYER_BASE_HP,
		damage_cool: 0,
		disp_damages: [], // {value:num, duration: num}
		damage_count: 0, //次何番のsprite使ってダメージ表示するか
		now_exp: 0,
		collect_range: 1,
		atk_power: 1,
		atk_rate: 1,
		atk_speed: 1,
		atk_num: 0,
		atk_range: 1,
		st_hp: 1,
		dm_resist: 1,
		st_rec: 1,
		st_exp: 1,
		st_money: 1,
		st_life: 1,
		recover_cool: 60,
		weapon_level: [], //{id: string, level: number}
		ability_level: [],
		game_end: false
	};

	SURVIVOR_DATA.refreshPlayerState();

	// maxhpを反映
	$gameTemp.SURVIVOR_player_data.nowhp = $gameTemp.SURVIVOR_player_data.maxhp;

	var ary = {
		for: { id: "fire", level: 1 },
		alice: { id: "maryoku_gun", level: 1 },
		hazu: { id: "oboro", level: 1 },
		aika: { id: "houou_soutou", level: 1 },
		cla: { id: "sword", level: 1 }
	};

	$gameTemp.SURVIVOR_player_data.weapon_level.push(ary[$gameTemp.survivor_ac]);

	$gameTemp.SURVIVOR_next_exp = function (level) {
		if (level < 21) {
			return level * 10 - 5;
		} else if (level < 41) {
			return (level - 20) * 13 + 195;
		} else {
			return (level - 40) * 16 + 455;
		}
	};
};

Scene_Survive_Battle_Map.prototype.createPowerUpWindow = function () {
	const wx = 0;
	const wy = 0;
	const ww = 640;
	const wh = 640;
	const rect = new Rectangle(wx, wy, ww, wh);

	this._PowerUpWindow = new Window_power_up_list(rect);
	this.addWindow(this._PowerUpWindow);
};

Scene_Survive_Battle_Map.prototype.create = function () {
	Scene_Message.prototype.create.call(this);
	this._transfer = $gamePlayer.isTransferring();
	this._lastMapWasNull = !$dataMap;
	if (this._transfer) {
		DataManager.loadMapData($gamePlayer.newMapId());
		this.onTransfer();
	} else {
		DataManager.loadMapData($gameMap.mapId());
	}
};

Scene_Survive_Battle_Map.prototype.isReady = function () {
	if (!this._mapLoaded && DataManager.isMapLoaded()) {
		this.onMapLoaded();
		this._mapLoaded = true;
	}
	return this._mapLoaded && Scene_Message.prototype.isReady.call(this);
};

Scene_Survive_Battle_Map.prototype.onMapLoaded = function () {
	if (this._transfer) {
		$gamePlayer.performTransfer();
	}
	this.createDisplayObjects();
};

Scene_Survive_Battle_Map.prototype.onTransfer = function () {
	ImageManager.clear();
	EffectManager.clear();
};

Scene_Survive_Battle_Map.prototype.start = function () {
	Scene_Message.prototype.start.call(this);
	SceneManager.clearStack();
	if (this._transfer) {
		this.fadeInForTransfer();
		this.onTransferEnd();
	} else if (this.needsFadeIn()) {
		this.startFadeIn(this.fadeSpeed(), false);
	}
	this.menuCalling = false;

	if (this._spriteset._timerSprite) {
		this._spriteset._timerSprite.updatePosition = function () {
			this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN - this.bitmap.width) / 2;
			this.y = 40;
		};
	}
	// 10秒のアタックモード
	$gameTemp.timerStart = 5 * 60;
	$gameTimer.start(5 * 60 * 60);
};

Scene_Survive_Battle_Map.prototype.onTransferEnd = function () {
	this._mapNameWindow.open();
	$gameMap.autoplay();
	if (this.shouldAutosave()) {
		this.requestAutosave();
	}
};

Scene_Survive_Battle_Map.prototype.shouldAutosave = function () {
	return !this._lastMapWasNull;
};

Scene_Survive_Battle_Map.prototype.update = function () {
	Scene_Message.prototype.update.call(this);
	this.updateDestination();
	this.updateMenuButton();
	this.updateMapNameWindow();
	this.updateMainMultiply();
	if (this.isSceneChangeOk()) {
		this.updateScene();
	} else if (SceneManager.isNextScene(Scene_Battle)) {
		this.updateEncounterEffect();
	}
	this.updateWaitCount();
};

Scene_Survive_Battle_Map.prototype.updateMainMultiply = function () {
	if (this.isFastForward()) {
		this.cancelMessageWait();
		this.updateMain();
	}
	this.updateMain();
};

Scene_Survive_Battle_Map.prototype.updateMain = function () {
	$gameMap.update(this.isActive());
	$gamePlayer.update(this.isPlayerActive());
	$gameTimer.update(this.isActive());
	$gameScreen.update();
};

Scene_Survive_Battle_Map.prototype.isPlayerActive = function () {
	return this.isActive() && !this.isFading();
};

Scene_Survive_Battle_Map.prototype.isFastForward = function () {
	return $gameMap.isEventRunning() && !SceneManager.isSceneChanging() && (Input.isLongPressed("ok") || TouchInput.isLongPressed());
};

Scene_Survive_Battle_Map.prototype.stop = function () {
	Scene_Message.prototype.stop.call(this);
	$gamePlayer.straighten();
	this._mapNameWindow.close();
	if (this.needsSlowFadeOut()) {
		this.startFadeOut(this.slowFadeSpeed(), false);
	} else if (SceneManager.isNextScene(Scene_Survive_Battle_Map)) {
		this.fadeOutForTransfer();
	} else if (SceneManager.isNextScene(Scene_Battle)) {
		this.launchBattle();
	}
};

Scene_Survive_Battle_Map.prototype.isBusy = function () {
	return this.isMessageWindowClosing() || this._waitCount > 0 || this._encounterEffectDuration > 0 || Scene_Message.prototype.isBusy.call(this);
};

Scene_Survive_Battle_Map.prototype.terminate = function () {
	Scene_Message.prototype.terminate.call(this);
	if (!SceneManager.isNextScene(Scene_Battle)) {
		this._spriteset.update();
		this._mapNameWindow.hide();
		this.hideMenuButton();
		SceneManager.snapForBackground();
	}
	$gameScreen.clearZoom();
};

Scene_Survive_Battle_Map.prototype.needsFadeIn = function () {
	return SceneManager.isPreviousScene(Scene_Battle) || SceneManager.isPreviousScene(Scene_Load);
};

Scene_Survive_Battle_Map.prototype.needsSlowFadeOut = function () {
	return SceneManager.isNextScene(Scene_Title) || SceneManager.isNextScene(Scene_Gameover);
};

Scene_Survive_Battle_Map.prototype.updateWaitCount = function () {
	if (this._waitCount > 0) {
		this._waitCount--;
		return true;
	}
	return false;
};

Scene_Survive_Battle_Map.prototype.updateDestination = function () {
	if (this.isMapTouchOk()) {
		this.processMapTouch();
	} else {
		$gameTemp.clearDestination();
		this._touchCount = 0;
	}
};

Scene_Survive_Battle_Map.prototype.updateMenuButton = function () {
	if (this._menuButton) {
		const menuEnabled = this.isMenuEnabled();
		if (menuEnabled === this._menuEnabled) {
			this._menuButton.visible = this._menuEnabled;
		} else {
			this._menuEnabled = menuEnabled;
		}
	}
};

Scene_Survive_Battle_Map.prototype.hideMenuButton = function () {
	if (this._menuButton) {
		this._menuButton.visible = false;
		this._menuEnabled = false;
	}
};

Scene_Survive_Battle_Map.prototype.updateMapNameWindow = function () {
	if ($gameMessage.isBusy()) {
		this._mapNameWindow.close();
	}
};

Scene_Survive_Battle_Map.prototype.isMenuEnabled = function () {
	return $gameSystem.isMenuEnabled() && !$gameMap.isEventRunning();
};

Scene_Survive_Battle_Map.prototype.isMapTouchOk = function () {
	return this.isActive() && $gamePlayer.canMove();
};

Scene_Survive_Battle_Map.prototype.processMapTouch = function () {
	if (TouchInput.isTriggered() || this._touchCount > 0) {
		if (TouchInput.isPressed() && !this.isAnyButtonPressed()) {
			if (this._touchCount === 0 || this._touchCount >= 15) {
				this.onMapTouch();
			}
			this._touchCount++;
		} else {
			this._touchCount = 0;
		}
	}
};

Scene_Survive_Battle_Map.prototype.isAnyButtonPressed = function () {
	return this._menuButton && this._menuButton.isPressed();
};

Scene_Survive_Battle_Map.prototype.onMapTouch = function () {
	const x = $gameMap.canvasToMapX(TouchInput.x);
	const y = $gameMap.canvasToMapY(TouchInput.y);
	$gameTemp.setDestination(x, y);
};

Scene_Survive_Battle_Map.prototype.isSceneChangeOk = function () {
	return this.isActive() && !$gameMessage.isBusy();
};

Scene_Survive_Battle_Map.prototype.updateScene = function () {
	this.checkGameover();
	if (!SceneManager.isSceneChanging()) {
		this.updateTransferPlayer();
	}
	if (!SceneManager.isSceneChanging()) {
		this.updateEncounter();
	}
	if (!SceneManager.isSceneChanging()) {
		this.updateCallMenu();
	}
	if (!SceneManager.isSceneChanging()) {
		this.updateCallDebug();
	}
};

Scene_Survive_Battle_Map.prototype.createDisplayObjects = function () {
	this.createSpriteset();
	this.createWindowLayer();
	this.createAllWindows();
	this.createButtons();
};

Scene_Survive_Battle_Map.prototype.createSpriteset = function () {
	this._spriteset = new Spriteset_survivorMap();
	this.addChild(this._spriteset);
	this._spriteset.update();
};

Scene_Survive_Battle_Map.prototype.createAllWindows = function () {
	this.createMapNameWindow();
	Scene_Message.prototype.createAllWindows.call(this);

	this._messageWindow.startInput = function () {
		if ($gameMessage.isChoice()) {
			this._choiceListWindow.start();
			return true;
		} else if ($gameMessage.isNumberInput()) {
			// 5ならレベルアップウィンドウ、4ならキャンセルウィンドウ
			if ($gameMessage._numInputVariableId === 5) {
				this._numberInputWindow.start();
			} else {
				this._cancelWindow.start();
			}
			return true;
		} else if ($gameMessage.isItemChoice()) {
			this._eventItemWindow.start();
			return true;
		} else {
			return false;
		}
	};

	this._messageWindow.isAnySubWindowActive = function () {
		return this._choiceListWindow.active || this._numberInputWindow.active || this._eventItemWindow.active || this._cancelWindow.active;
	};
};

Scene_Survive_Battle_Map.prototype.associateWindows = function () {
	this._messageWindow.setCancelWindow = function (cancelWindow) {
		this._cancelWindow = cancelWindow;
	};

	const messageWindow = this._messageWindow;
	messageWindow.setGoldWindow(this._goldWindow);
	messageWindow.setNameBoxWindow(this._nameBoxWindow);
	messageWindow.setChoiceListWindow(this._choiceListWindow);
	messageWindow.setNumberInputWindow(this._numberInputWindow);
	messageWindow.setCancelWindow(this._cancelWindow);
	messageWindow.setEventItemWindow(this._eventItemWindow);
	this._nameBoxWindow.setMessageWindow(messageWindow);
	this._choiceListWindow.setMessageWindow(messageWindow);
	this._numberInputWindow.setMessageWindow(messageWindow);
	this._cancelWindow.setMessageWindow(messageWindow);
	this._eventItemWindow.setMessageWindow(messageWindow);
};

Scene_Survive_Battle_Map.prototype.createMapNameWindow = function () {
	const rect = this.mapNameWindowRect();
	this._mapNameWindow = new Window_MapName(rect);
	this.addWindow(this._mapNameWindow);
};

Scene_Survive_Battle_Map.prototype.mapNameWindowRect = function () {
	const wx = 0;
	const wy = 0;
	const ww = 360;
	const wh = this.calcWindowHeight(1, false);
	return new Rectangle(wx, wy, ww, wh);
};

Scene_Survive_Battle_Map.prototype.createButtons = function () {
	if (ConfigManager.touchUI) {
		this.createMenuButton();
	}
};

Scene_Survive_Battle_Map.prototype.createMenuButton = function () {
	this._menuButton = new Sprite_Button("menu");
	this._menuButton.x = Graphics.boxWidth - this._menuButton.width - 4;
	this._menuButton.y = this.buttonY();
	this._menuButton.visible = false;
	this.addWindow(this._menuButton);
};

Scene_Survive_Battle_Map.prototype.updateTransferPlayer = function () {
	if ($gamePlayer.isTransferring()) {
		SceneManager.goto(Scene_Survive_Battle_Map);
	}
};

Scene_Survive_Battle_Map.prototype.updateEncounter = function () {
	if ($gamePlayer.executeEncounter()) {
		SceneManager.push(Scene_Battle);
	}
};

Scene_Survive_Battle_Map.prototype.updateCallMenu = function () {
	if (this.isMenuEnabled()) {
		if (this.isMenuCalled()) {
			this.menuCalling = true;
		}
		if (this.menuCalling && !$gamePlayer.isMoving() && 0 < $gameTimer._frames && !$gameTemp.SURVIVOR_player_data.game_end) {
			this.callMenu();
		}
	} else {
		this.menuCalling = false;
	}
};

Scene_Survive_Battle_Map.prototype.isMenuCalled = function () {
	return Input.isTriggered("menu") || TouchInput.isCancelled();
};

Scene_Survive_Battle_Map.prototype.callMenu = function () {
	SoundManager.playOk();
	this.menuCalling = false;
	$gameMessage.setNumberInput(4, 3); // 5ならレベルアップウィンドウ、4ならキャンセルウィンドウ
	// SceneManager.push(Scene_Menu);
	// Window_MenuCommand.initCommandPosition();
	// $gameTemp.clearDestination();
	// this._mapNameWindow.hide();
	// this._waitCount = 2;
};

Scene_Survive_Battle_Map.prototype.updateCallDebug = function () {
	if (this.isDebugCalled()) {
		SceneManager.push(Scene_Debug);
	}
};

Scene_Survive_Battle_Map.prototype.isDebugCalled = function () {
	return Input.isTriggered("debug") && $gameTemp.isPlaytest();
};

Scene_Survive_Battle_Map.prototype.fadeInForTransfer = function () {
	const fadeType = $gamePlayer.fadeType();
	switch (fadeType) {
		case 0:
		case 1:
			this.startFadeIn(this.fadeSpeed(), fadeType === 1);
			break;
	}
};

Scene_Survive_Battle_Map.prototype.fadeOutForTransfer = function () {
	const fadeType = $gamePlayer.fadeType();
	switch (fadeType) {
		case 0:
		case 1:
			this.startFadeOut(this.fadeSpeed(), fadeType === 1);
			break;
	}
};

Scene_Survive_Battle_Map.prototype.launchBattle = function () {
	BattleManager.saveBgmAndBgs();
	this.stopAudioOnBattleStart();
	SoundManager.playBattleStart();
	this.startEncounterEffect();
	this._mapNameWindow.hide();
};

Scene_Survive_Battle_Map.prototype.stopAudioOnBattleStart = function () {
	if (!AudioManager.isCurrentBgm($gameSystem.battleBgm())) {
		AudioManager.stopBgm();
	}
	AudioManager.stopBgs();
	AudioManager.stopMe();
	AudioManager.stopSe();
};

Scene_Survive_Battle_Map.prototype.startEncounterEffect = function () {
	this._spriteset.hideCharacters();
	this._encounterEffectDuration = this.encounterEffectSpeed();
};

Scene_Survive_Battle_Map.prototype.updateEncounterEffect = function () {
	if (this._encounterEffectDuration > 0) {
		this._encounterEffectDuration--;
		const speed = this.encounterEffectSpeed();
		const n = speed - this._encounterEffectDuration;
		const p = n / speed;
		const q = ((p - 1) * 20 * p + 5) * p + 1;
		const zoomX = $gamePlayer.screenX();
		const zoomY = $gamePlayer.screenY() - 24;
		if (n === 2) {
			$gameScreen.setZoom(zoomX, zoomY, 1);
			this.snapForBattleBackground();
			this.startFlashForEncounter(speed / 2);
		}
		$gameScreen.setZoom(zoomX, zoomY, q);
		if (n === Math.floor(speed / 6)) {
			this.startFlashForEncounter(speed / 2);
		}
		if (n === Math.floor(speed / 2)) {
			BattleManager.playBattleBgm();
			this.startFadeOut(this.fadeSpeed());
		}
	}
};

Scene_Survive_Battle_Map.prototype.snapForBattleBackground = function () {
	this._windowLayer.visible = false;
	SceneManager.snapForBackground();
	this._windowLayer.visible = true;
};

Scene_Survive_Battle_Map.prototype.startFlashForEncounter = function (duration) {
	const color = [255, 255, 255, 255];
	$gameScreen.startFlash(color, duration);
};

Scene_Survive_Battle_Map.prototype.encounterEffectSpeed = function () {
	return 60;
};
