(() => {
	const pluginName = "survivor_core";
	let textPictureText = "";
})();

//定数
const SURVIVOR_CONST = {
	DEATH_COUNT: 20,
	DAMAGE_DURATION: 20,
	DM_SE_DURATION: 6,
	POP_RATE: 2,
	LEFT_MARGIN: 0,
	RIGHT_MARGIN: 320,
	ABILITY_VARI: 173,
	// [100, 500, 700, 2000, 4000, 10000, 20000, 30000, 40000, 50000, Infinity]
	ABILITY_PRICE: [100, 500, 1000, 3000, 7000, Infinity],
	ABILITY_TEXT: {
		collect_range: "経験値玉の回収範囲が広がる",
		atk_power: "攻撃力が上昇",
		atk_rate: "攻撃の再発動が早くなる",
		atk_speed: "攻撃の移動速度が上昇",
		atk_num: "発射される攻撃が増える",
		atk_range: "攻撃の範囲が広がる",
		st_hp: "最大HPが増え回復する",
		dm_resist: "受けるダメージが減少",
		st_rec: "より多く毎秒HPが回復",
		st_exp: "得られる経験値が上昇",
		st_money: "得られるGOLDが上昇",
		st_life: "復活する回数が増える"
	},
	WEAPON_TEXT: {
		fire: "自身の周りを炎の玉が旋回する",
		thunder: "雷の玉を敵に向かって発射する",
		ice: "周囲に氷の柱が出る",
		maryoku_gun: "魔力の弾を前方に発射する",
		oboro: "上方向に刀を投げる", // 刀をグルグル回す
		houou_soutou: "回転した刀を投げる", //刀を投げ飛ばす
		sword: "前方の敵を切りつける" //slash
	},
	MONEY_VARI: 15,
	COLLECT_RANGE: 50,
	PLAYER_DAMAGE_RANGE: 24,
	PLAYER_DAMAGE_DURATION: 6,
	PLAYER_DAMAGE_SPRITES: 5,
	PLAYER_BASE_HP: 100
};

var SURVIVOR_DATA = {
	WeaponList: [], //new WEAPON
	AbilityList: [], // {id:id, level:level}
	MobList: [],

	refreshPlayerState: function () {
		// 初期値
		Object.assign($gameTemp.SURVIVOR_player_data, {
			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
		});

		// ステータスは加算で計算するヤツ
		var kasan = { atk_num: true, st_rec: true, st_life: true };

		//キャラのパラメータを追加
		var params = SURVIVOR_UNIT_STATE[$gameTemp.survivor_ac];
		for (var i = 0; i < params.length; i++) {
			var power = params[i];
			if (kasan[power]) {
				$gameTemp.SURVIVOR_player_data[power.id] += power.value;
			} else {
				$gameTemp.SURVIVOR_player_data[power.id] *= power.value;
			}
		}

		// 購入したパラメータの値を追加
		var powers = $gameVariables.value(173).power;
		for (var i = 0; i < powers.length; i++) {
			var power = powers[i];

			if (kasan[power.id]) {
				$gameTemp.SURVIVOR_player_data[power.id] += SURVIVOR_DEFO_ABILITY_DATA[power.id][power.level];
			} else {
				$gameTemp.SURVIVOR_player_data[power.id] *= SURVIVOR_DEFO_ABILITY_DATA[power.id][power.level];
			}
		}

		// 所持してるアビリティのパラメータを追加
		for (var i = 0; i < $gameTemp.SURVIVOR_player_data.ability_level.length; i++) {
			var data = $gameTemp.SURVIVOR_player_data.ability_level[i];
			if (kasan[data.id]) {
				$gameTemp.SURVIVOR_player_data[data.id] += SURVIVOR_ABILITY_DATA[data.id][data.level];
			} else {
				$gameTemp.SURVIVOR_player_data[data.id] *= SURVIVOR_ABILITY_DATA[data.id][data.level];
			}
		}

		$gameTemp.SURVIVOR_player_data.collect_range *= SURVIVOR_CONST.COLLECT_RANGE;
		$gameTemp.SURVIVOR_player_data.maxhp = SURVIVOR_CONST.PLAYER_BASE_HP;
		$gameTemp.SURVIVOR_player_data.maxhp *= $gameTemp.SURVIVOR_player_data.st_hp;
	}
};

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

Spriteset_survivorMap.prototype = Object.create(Spriteset_Map.prototype);
Spriteset_survivorMap.prototype.constructor = Spriteset_survivorMap;

(function () {
	//タイルマップを狭くして左に寄せる
	var Tilemap_initialize = Tilemap.prototype.initialize;
	Tilemap.prototype.initialize = function () {
		Tilemap_initialize.call(this);
		if (SceneManager._scene.constructor.name === "Scene_Survive_Battle_Map") {
			this._width -= SURVIVOR_CONST.RIGHT_MARGIN;
		}
	};

	var Game_Player_centerX = Game_Player.prototype.centerX;
	Game_Player.prototype.centerX = function () {
		if (SceneManager._scene.constructor.name === "Scene_Survive_Battle_Map") {
			return ($gameMap.screenTileX() - 1) / 2 - SURVIVOR_CONST.RIGHT_MARGIN / 48 / 2;
		} else {
			return Game_Player_centerX.call(this);
		}
	};

	Game_Message.prototype.isSurvivorBusy = function () {
		return this.hasText() || this.isChoice() || this.isNumberInput() || this.isItemChoice();
	};
})();

Spriteset_survivorMap.prototype.createLowerLayer = function () {
	Spriteset_Base.prototype.createLowerLayer.call(this);
	this.createParallax();
	this.createTilemap();
	this.createCharacters();
	this.createShadow();
	this.createDestination();
	this.createWeather();

	this._mobSp = [];
	var wave_count = 0;
	for (var i = 0; i < Wave[$gameTemp.survivor_map][wave_count][0].count * SURVIVOR_CONST.POP_RATE; i++) {
		this.createMob(Wave[$gameTemp.survivor_map][wave_count][0].id, i);
	}

	this._weaponSp = {};

	for (var i = 0; i < $gameTemp.SURVIVOR_player_data.weapon_level.length; i++) {
		this.addWeapon($gameTemp.SURVIVOR_player_data.weapon_level[i].id, 1);
	}

	this._keikenSp = [];
	this.createGauge();
	this.createCharaWindow();
};
//経験値ゲージの作成
Spriteset_survivorMap.prototype.createGauge = function () {
	var sp = new Sprite_Gauge_K();
	sp.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
	sp.y = 20;
	sp.zIndex = 1000;
	this.addChild(sp);
};

Spriteset_survivorMap.prototype.createCharaWindow = function (name, id) {};

Spriteset_survivorMap.prototype.createMob = function (name, id) {
	var m = new MOB(name, id);
	SURVIVOR_DATA.MobList.push(m);

	var sp = new Sprite_Mob(m, name, id);
	sp.zIndex = 300;
	this._mobSp.push(sp);
	this.addChild(sp);
};
Spriteset_survivorMap.prototype.createKeiken = function (obj) {
	var sp = new Sprite_Keiken(Math.floor(10000 * Math.random()));
	sp.x = obj.x;
	sp.y = obj.y;
	sp._CBR_exp = obj.exp || 1;
	sp.zIndex = 100;
	this._keikenSp.push(sp);
	this.addChild(sp);
};

// ######################## ココで武器情報を全て入れる ##################
Spriteset_survivorMap.prototype.weaponMaker = function (name, level) {
	var d = new WEAPON_MAKER(name, level);

	if (name == "fire") {
		this._weaponSp.fire = [];
		for (var i = 0; i < d.num; i++) {
			var w = new WEAPON("fire", i, d, level);
			var sp = new Sprite_Survive_Weapon(w);
			SURVIVOR_DATA.WeaponList.push(w);

			sp.zIndex = 500;
			this._weaponSp.fire.push(sp);
			this.addChild(sp);
		}
	} else if (name == "thunder") {
		this._weaponSp.thunder = [];
		for (var i = 0; i < d.num; i++) {
			var w = new WEAPON(name, i, d, level);
			var sp = new Sprite_Survive_Weapon(w);
			SURVIVOR_DATA.WeaponList.push(w);

			sp.zIndex = 500;
			this._weaponSp.thunder.push(sp);
			this.addChild(sp);
		}
	} else if (name == "ice") {
		this._weaponSp.ice = [];
		for (var i = 0; i < d.num; i++) {
			var w = new WEAPON(name, i, d, level);
			var sp = new Sprite_Survive_Weapon(w);
			SURVIVOR_DATA.WeaponList.push(w);

			sp.zIndex = 500;
			this._weaponSp.ice.push(sp);
			this.addChild(sp);
		}
		// 連射系は5倍入れる
	} else if (name == "maryoku_gun") {
		this._weaponSp.maryoku_gun = [];
		for (var i = 0; i < d.num * 5; i++) {
			var w = new WEAPON(name, i, d, level);
			var sp = new Sprite_Survive_Weapon(w);
			SURVIVOR_DATA.WeaponList.push(w);

			sp.zIndex = 500;
			this._weaponSp.maryoku_gun.push(sp);
			this.addChild(sp);
		}
		// 連射系は5倍入れる
	} else if (name == "oboro") {
		this._weaponSp.oboro = [];
		for (var i = 0; i < d.num * 5; i++) {
			var w = new WEAPON(name, i, d, level);
			var sp = new Sprite_Survive_Weapon(w);
			SURVIVOR_DATA.WeaponList.push(w);

			sp.zIndex = 500;
			this._weaponSp.oboro.push(sp);
			this.addChild(sp);
		}
		// 連射系は5倍入れる
	} else if (name == "houou_soutou") {
		this._weaponSp.houou_soutou = [];
		for (var i = 0; i < d.num * 5; i++) {
			var w = new WEAPON(name, i, d, level);
			var sp = new Sprite_Survive_Weapon(w);
			SURVIVOR_DATA.WeaponList.push(w);

			sp.zIndex = 500;
			this._weaponSp.houou_soutou.push(sp);
			this.addChild(sp);
		}
	} else if (name == "sword") {
		this._weaponSp.sword = [];
		for (var i = 0; i < 1; i++) {
			var w = new WEAPON(name, i, d, level);
			var sp = new Sprite_Survive_Weapon(w);
			SURVIVOR_DATA.WeaponList.push(w);

			sp.zIndex = 500;
			this._weaponSp.sword.push(sp);
			this.addChild(sp);
		}
	}
};

Spriteset_survivorMap.prototype.addWeapon = function (id, level) {
	this.weaponMaker(id, level);
};

Spriteset_survivorMap.prototype.refreshWeapon = function (id, level) {
	this.delWeapon(id);
	this.weaponMaker(id, level);
};

Spriteset_survivorMap.prototype.delWeapon = function (name) {
	if (this._weaponSp[name]) {
		for (var i = 0; i < this._weaponSp[name].length; i++) {
			this.removeChild(this._weaponSp[name][i]);
			this._weaponSp[name][i].destroy();
			this._weaponSp[name][i] = null;
		}
		SURVIVOR_DATA.WeaponList = SURVIVOR_DATA.WeaponList.filter((e) => e.name !== name);
	}
};

Spriteset_survivorMap.prototype.update = function () {
	if ($gameMessage.isBusy()) {
		return;
	}

	Spriteset_Base.prototype.update.call(this);
	this.updateTileset();
	this.updateParallax();
	this.updateTilemap();
	this.updateShadow();
	this.updateWeather();
	this.updateAnimations();
	this.updateBalloons();

	var len = survivorState.newKeiken.length;
	if (len) {
		for (var i = 0; i < len; i++) {
			this.createKeiken(survivorState.newKeiken[i]);
		}

		this.sortChildren(); //新しいの作ったらモンスターの下にくるようにしないとね
		survivorState.newKeiken.length = 0;
	}

	if (survivorState.se_cool) {
		survivorState.se_cool--;
	}

	//消すべき経験のSpriteを削除
	if (survivorState.keikenDel.length) {
		for (var i = 0, len = survivorState.keikenDel.length; i < len; i++) {
			var idx = this._keikenSp.findIndex((e) => e.spriteId == survivorState.keikenDel[i]);
			this.delKeiken(idx);
		}
		survivorState.keikenDel.length = 0;
	}
};

Spriteset_survivorMap.prototype.delKeiken = function (idx) {
	$gameTemp.SURVIVOR_player_data.now_exp += this._keikenSp[idx]._CBR_exp * $gameTemp.SURVIVOR_player_data.st_exp;

	var v = $gameVariables.value(173);
	v.gold += Math.ceil(this._keikenSp[idx]._CBR_exp * $gameTemp.SURVIVOR_player_data.st_money);
	$gameVariables.setValue(173, v);

	this._keikenSp[idx].destroy(); //mapのremoveChild-
	this._keikenSp.splice(idx, 1); //_keikenSpからも消す
};

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();

	if ($gameMessage.isBusy()) {
		return;
	}

	if (0 < Survivor_var.dm_se) {
		Survivor_var.dm_se--;
	}

	//中心から離れてる順にソートする
	SURVIVOR_DATA.MobList.sort(function (a, b) {
		var ax = a.x - (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
		var bx = b.x - (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
		var ay = a.y - Graphics.height / 2;
		var by = b.y - Graphics.height / 2;

		return Math.sqrt(bx * bx + by * by) - Math.sqrt(ax * ax + ay * ay);
	});

	//削除されてたら消す
	for (var i = SURVIVOR_DATA.MobList.length - 1; 0 <= i; i--) {
		if (!SURVIVOR_DATA.MobList[i]) {
			SURVIVOR_DATA.MobList.pop();
		} else {
			break;
		}
	}
	this.updatePlayerDir();
	this.updateMobCool();
	this.updateMobPosition();
	this.updateWeapon();
	this.updateDamage();
	this.updateKb();
	this.rePopMob();

	// レベルアップ処理、タイマーが0より大きい時だけ
	var next_exp = $gameTemp.SURVIVOR_next_exp($gameTemp.SURVIVOR_player_data.level);
	if (next_exp < $gameTemp.SURVIVOR_player_data.now_exp && 0 < $gameTimer._frames) {
		//$gameTemp.SURVIVOR_player_data.now_exp - next_exp;
		$gameMessage.setNumberInput(5, 3); // 5ならレベルアップウィンドウ、4ならキャンセルウィンドウ
	}

	if (this._spriteset._timerSprite) {
		this._spriteset._timerSprite.zIndex = 99999;
		this._spriteset.sortChildren();
	}

	// タイマーが0になって生き延びたら
	if ($gameTimer.isWorking() && $gameTimer._frames <= 0) {
		// var maplist = [
		// 	280, //ヒストリア
		// 	281, //鬼と刀
		// 	282, //令嬢
		// 	283, //ディストピア
		// 	284 //軋轢
		// ];

		var data = $gameVariables.value(173);
		var memoryData = data.memory; // [{name: string, phase: number}] 1普通にクリア、2特定のキャラクターでクリア

		// フォーネリア
		if ($gameParty.leader().characterName() == "survive_main_unit1" && $gameParty.leader().characterIndex() == 5) {
			var nowName = "for";
			// アリス
		} else if ($gameParty.leader().characterName() == "survive_main_unit1" && $gameParty.leader().characterIndex() == 2) {
			var nowName = "alice";
			// 藍華
		} else if ($gameParty.leader().characterName() == "survive_main_unit2" && $gameParty.leader().characterIndex() == 1) {
			var nowName = "aika";
			// ハヅキ
		} else if ($gameParty.leader().characterName() == "survive_main_unit1" && $gameParty.leader().characterIndex() == 4) {
			var nowName = "hazu";
			// クレイシア
		} else if ($gameParty.leader().characterName() == "survive_main_unit1" && $gameParty.leader().characterIndex() == 3) {
			var nowName = "cla";
		}

		var nowMapId = $gameMap.mapId();

		if (nowMapId === 280) {
			var stageName = "for";
			var ev = [, 1223, 1224];
		} else if (nowMapId === 282) {
			var stageName = "alice";
			var ev = [, 1227, 1228];
		} else if (nowMapId === 281) {
			var stageName = "aika";
			var ev = [, 1225, 1226];
		} else if (nowMapId === 283) {
			var stageName = "hazu";
			var ev = [, 1229, 1230];
		} else if (nowMapId === 284) {
			var stageName = "cla";
			var ev = [, 1231, 1232];
		}

		var clearIdx = memoryData.findIndex((e) => e.name === stageName);

		if (clearIdx === -1) {
			// このステージ初クリアだったら
			data.memory.push({ name: stageName, phase: 1 });
			$gameTemp.survivor_reserve_kaisou = ev[1];
		} else {
			if (data.memory[clearIdx].phase === 1 && stageName == nowName) {
				//2段階が初クリアだったら
				data.memory[clearIdx].phase = 2;
				$gameTemp.survivor_reserve_kaisou = ev[2];
			}
		}

		$gameVariables.setValue(173, data);

		$gameTimer.stop();
		$gameTemp.reserveCommonEvent(1221);
	}
};

Scene_Survive_Battle_Map.prototype.updatePlayerDir = function () {
	if (tttt % 30 == 20) {
		if (Survivor_var.now_x != $gameMap._parallaxX || Survivor_var.now_y != $gameMap._parallaxY) {
			Survivor_var.bf_x = Survivor_var.now_x;
			Survivor_var.bf_y = Survivor_var.now_y;
			Survivor_var.now_x = $gameMap._parallaxX;
			Survivor_var.now_y = $gameMap._parallaxY;
			Survivor_var.player_dir = Math.atan2(Survivor_var.now_y - Survivor_var.bf_y, Survivor_var.now_x - Survivor_var.bf_x);
		}
	}
};
//Sprite_Mobでdelが入ってから削除される
Scene_Survive_Battle_Map.prototype.updateDelMob = function () {
	SURVIVOR_DATA.WeaponList.forEach((e, index) => {
		if (e.del) {
			SURVIVOR_DATA.WeaponList.splice(index, 1);
		}
	});
};
//Sprite_Mobでdelが入ってから削除される
Scene_Survive_Battle_Map.prototype.updateDelMob = function () {
	SURVIVOR_DATA.MobList.forEach((e, index) => {
		if (e.del) {
			SURVIVOR_DATA.MobList.splice(index, 1);
		}
	});
};

var tttt = 0;
Scene_Survive_Battle_Map.prototype.rePopMob = function () {
	var time_span = $gameTemp.timerStart - $gameTimer.seconds();
	var wave_count = 0;
	wave_count = Math.floor(time_span / 30);
	wave_count = Math.min(wave_count, Wave[$gameTemp.survivor_map].length - 1);
	if (tttt % 30 == 10) {
		for (var i = 0, len = SURVIVOR_DATA.MobList.length; i < len; i++) {
			var m = SURVIVOR_DATA.MobList[i];
			if (m.del) {
				m.resetMob(Wave[$gameTemp.survivor_map][wave_count][0].id, m.id);
			}
		}
	}
	tttt++;
};

Scene_Survive_Battle_Map.prototype.updateKb = function () {
	for (var i = 0, len = SURVIVOR_DATA.MobList.length; i < len; i++) {
		var m = SURVIVOR_DATA.MobList[i];
		if (m.death) {
			continue;
		}

		if (m.kb) {
			if (m.kb.f) {
				var r = m.kb.power / m.kb.f;
				m.x += r * Math.cos(m.kb.dir);
				m.y += r * Math.sin(m.kb.dir);
				m.kb.f--;
			}

			if (m.kb.f <= 0) {
				m.kb = null;
			}
		}
	}
};

//移動、Sprite_Mobのupdateでやると順番がバラバラなのでココでやる
Scene_Survive_Battle_Map.prototype.updateMobPosition = function () {
	for (var i = 0, len = SURVIVOR_DATA.MobList.length; i < len; i++) {
		var m = SURVIVOR_DATA.MobList[i];

		//スクロールによる位置調整
		m.x -= ($gameMap._parallaxX - m.dispX) * 48;
		m.dispX = $gameMap._parallaxX;
		m.y -= ($gameMap._parallaxY - m.dispY) * 48;
		m.dispY = $gameMap._parallaxY;

		//死んでたら移動しない
		if (m.death) {
			continue;
		}
		//中心へ向かって移動
		var theta = Math.atan2(Graphics.height / 2 - m.y, (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2 - m.x);
		m.x += Math.cos(theta) * m.speed;
		m.y += Math.sin(theta) * m.speed;

		//もし範囲内にmobがいたら離れる
		for (var k = 0, len2 = SURVIVOR_DATA.MobList.length; k < len2; k++) {
			//自分より内側のヤツだけ攻撃範囲計算する
			//近付いてたら自分が離れる
			if (k <= i) {
				continue;
			}
			var m2 = SURVIVOR_DATA.MobList[k];
			if (m2.death) {
				continue;
			}
			var saX = m2.x - m.x;
			var saY = m2.y - m.y;
			var sa = Math.sqrt(saX * saX + saY * saY);
			var r = (m.r + m2.r) * 0.75; //衝突判定は攻撃範囲の半分の大きさでいいや
			if (sa < r && m.id != m2.id) {
				theta += (0.5 - Math.random()) * 3; //だいたい±π/2

				m.x -= Math.cos(theta) * (r - sa); //離したい分だけ離す
				m.y -= Math.sin(theta) * (r - sa);

				break;
			}
		}
	}
};

//攻撃判定
Scene_Survive_Battle_Map.prototype.updateWeapon = function () {
	for (var i = 0, len = SURVIVOR_DATA.WeaponList.length; i < len; i++) {
		var w = SURVIVOR_DATA.WeaponList[i];
		w.update_orbit();
	}
};
Scene_Survive_Battle_Map.prototype.updateMobCool = function () {
	for (var i = 0, len = SURVIVOR_DATA.MobList.length; i < len; i++) {
		var m = SURVIVOR_DATA.MobList[i];

		m.cool.forEach((e, index) => {
			if (e.t < 1) {
				m.cool.splice(index, 1);
			} else {
				e.t--;
			}
		});
	}
};

Scene_Survive_Battle_Map.prototype.updateDamage = function () {
	$gameTemp.SURVIVOR_player_data.recover_cool--;
	if ($gameTemp.SURVIVOR_player_data.recover_cool < 0) {
		$gameTemp.SURVIVOR_player_data.recover_cool = 60;

		$gameTemp.SURVIVOR_player_data.nowhp += $gameTemp.SURVIVOR_player_data.st_rec;
		if ($gameTemp.SURVIVOR_player_data.maxhp < $gameTemp.SURVIVOR_player_data.nowhp) {
			$gameTemp.SURVIVOR_player_data.nowhp = $gameTemp.SURVIVOR_player_data.maxhp;
		}
	}
	//プレイヤーのダメージ
	if (
		$gameTemp.SURVIVOR_player_data.damage_cool <= 0 &&
		SURVIVOR_DATA.MobList &&
		2 < SURVIVOR_DATA.MobList.length &&
		!$gameTemp.SURVIVOR_player_data.game_end
	) {
		// 一番中心に近いMOBを抽出
		var m = SURVIVOR_DATA.MobList[SURVIVOR_DATA.MobList.length - 1];
		var px = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
		var py = Graphics.height / 2;

		var kyori = (m.x - px) * (m.x - px);
		kyori += (m.y - py) * (m.y - py);
		kyori = Math.sqrt(kyori);

		// 距離の内側だったらダメージ
		if (kyori < SURVIVOR_CONST.PLAYER_DAMAGE_RANGE) {
			$gameTemp.SURVIVOR_player_data.damage_cool = SURVIVOR_CONST.PLAYER_DAMAGE_DURATION;
			$gameTemp.SURVIVOR_player_data.disp_damages[$gameTemp.SURVIVOR_player_data.damage_count] = {
				dm: m.damage,
				duration: SURVIVOR_CONST.DAMAGE_DURATION,
				visible: true
			};
			// hpをダメージ分減らす
			$gameTemp.SURVIVOR_player_data.nowhp -= m.damage;
			$gameTemp.SURVIVOR_player_data.damage_count = ($gameTemp.SURVIVOR_player_data.damage_count + 1) % SURVIVOR_CONST.PLAYER_DAMAGE_SPRITES;

			// もし死んだら
			if ($gameTemp.SURVIVOR_player_data.nowhp < 0) {
				$gameTemp.SURVIVOR_player_data.death_count++;

				// デスカウントが復活回数超えてたら
				if ($gameTemp.SURVIVOR_player_data.st_life < $gameTemp.SURVIVOR_player_data.death_count) {
					$gameTimer.stop();
					$gameTemp.reserveCommonEvent(1216);
					$gameTemp.SURVIVOR_player_data.game_end = true;
					// まだカウント残ってたら復活
				} else {
					$gameTemp.SURVIVOR_player_data.nowhp = $gameTemp.SURVIVOR_player_data.maxhp;
					$gameTemp.requestAnimation([$gamePlayer], 49);
				}
			}
		}
	} else {
		$gameTemp.SURVIVOR_player_data.damage_cool--;
	}

	//モブのダメージ
	for (var i = 0, len = SURVIVOR_DATA.MobList.length; i < len; i++) {
		var m = SURVIVOR_DATA.MobList[i];
		if (m.death) {
			continue;
		}

		for (var k = 0, len2 = SURVIVOR_DATA.WeaponList.length; k < len2; k++) {
			var w = SURVIVOR_DATA.WeaponList[k];

			//その武器が攻撃上限に達してたらスルー
			if (w.break) {
				continue;
			}

			var kyori = (m.x - w.x) * (m.x - w.x);
			kyori += (m.y - w.y) * (m.y - w.y);
			kyori = Math.sqrt(kyori);

			//もし攻撃範囲内の場合
			if (kyori < w.r * $gameTemp.SURVIVOR_player_data.atk_range) {
				//クールタイム中だったら次の武器へ
				var key = m.cool.findIndex(function (e) {
					return e.name == w.name + w.id;
				});
				if (key != -1) {
					continue;
				}

				//武器の反対方向ではなくプレイヤーの反対へ
				if (Survivor_var.dm_se < 1) {
					AudioManager.playSe({ name: "Blow2", volume: 10, pitch: 200, pan: 0 });
					Survivor_var.dm_se = SURVIVOR_CONST.DM_SE_DURATION;
				}

				//攻撃回数が設定されている場合
				if (w.count_max) {
					w.count++;
					if (w.count_max <= w.count) {
						w.break = true;
					}
				}

				//死んだ瞬間
				if (m.hp <= Math.ceil(w.damage * $gameTemp.SURVIVOR_player_data.atk_power)) {
					m.death = true;
					m.deathCount = SURVIVOR_CONST.DEATH_COUNT;
					m.deathFilter = false;
					survivorState.newKeiken.push({ x: m.x, y: m.y, exp: m.exp });

					break; //ココで終わればブキと対象mobの処理も終わる
				}

				//吹っ飛ばす
				m.cool.push({
					name: w.name + w.id,
					t: w.cool
				});
				m.setDamage(Math.ceil(w.damage * $gameTemp.SURVIVOR_player_data.atk_power));

				var theta = Math.atan2(m.y - Graphics.height / 2, m.x - (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2);
				m.kb = {
					dir: theta,
					f: w.kb.f,
					power: w.kb.power
				};
			}
		}
	}
};

const SURVIVOR_WEAPON_LIST = ["fire", "thunder", "ice", "maryoku_gun", "oboro", "houou_soutou", "sword"];

const SURVIVOR_WEAPON_DISP_DATA = {
	fire: { name: "ファイア", evo_name: "ファイアLv10", icon: { x: 0, y: 4 } },
	thunder: { name: "サンダー", evo_name: "サンダーLv10", icon: { x: 2, y: 4 } },
	ice: { name: "アイス", evo_name: "アイスLv10", icon: { x: 1, y: 4 } },
	maryoku_gun: { name: "魔力弾", evo_name: "エイシェント", icon: { x: 8, y: 6 } },
	oboro: { name: "朧刀", evo_name: "朧刀・淫", icon: { x: 8, y: 7 } }, // 刀をグルグル回す
	houou_soutou: { name: "鳳凰院双刀", evo_name: "天叢雲剣", icon: { x: 12, y: 7 } }, //刀を投げ飛ばす
	sword: { name: "標準の剣", evo_name: "エクスカリバー", icon: { x: 1, y: 6 } } //slash
};

const SURVIVOR_UNIT_STATE = {
	for: [
		{ id: "st_hp", value: 1.3 },
		{ id: "dm_resist", value: 0.8 }
	],
	aika: [
		{ id: "atk_power", value: 1.2 },
		{ id: "atk_rate", value: 0.95 }
	],
	hazu: [{ id: "atk_power", value: 1.3 }],
	alice: [{ id: "collect_range", value: 1.4 }],
	cla: [
		{ id: "atk_range", value: 1.3 },
		{ id: "atk_num", value: 1 }
	]
};

const SURVIVOR_ABILITY_LIST = [
	"collect_range",
	"atk_power",
	"atk_rate",
	"atk_speed",
	"atk_num",
	"atk_range",
	"st_hp",
	"dm_resist",
	"st_rec",
	"st_exp",
	"st_money",
	"st_life"
];

SURVIVOR_ABILITY_DISP_DATA = {
	collect_range: { name: "回収範囲", icon: { x: 3, y: 4 } },
	atk_power: { name: "攻撃力", icon: { x: 13, y: 4 } },
	atk_rate: { name: "クールダウン", icon: { x: 15, y: 4 } },
	atk_speed: { name: "攻撃速度", icon: { x: 12, y: 4 } },
	atk_num: { name: "追加弾数", icon: { x: 6, y: 7 } },
	atk_range: { name: "攻撃範囲", icon: { x: 8, y: 4 } },
	st_hp: { name: "最大HP", icon: { x: 4, y: 5 } },
	dm_resist: { name: "ダメージ軽減", icon: { x: 0, y: 8 } },
	st_rec: { name: "自動回復", icon: { x: 1, y: 15 } },
	st_exp: { name: "＋経験値", icon: { x: 13, y: 18 } },
	st_money: { name: "＋お金", icon: { x: 9, y: 19 } },
	st_life: { name: "復活回数", icon: { x: 13, y: 13 } }
};

const SURVIVOR_DEFO_ABILITY_DATA = {
	collect_range: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0],
	atk_power: [1, 1.02, 1.04, 1.06, 1.08, 1.1, 1.12, 1.14, 1.16, 1.18, 1.2],
	atk_rate: [1, 0.98, 0.96, 0.94, 0.92, 0.9, 0.88, 0.06, 0.84, 0.82, 0.8],
	atk_speed: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0],
	atk_num: [0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3], //これは加算
	atk_range: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0],
	st_hp: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0],
	dm_resist: [1, 0.97, 0.94, 0.91, 0.88, 0.85, 0.82, 0.79, 0.76, 0.73, 0.7],
	st_rec: [0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3], //これは加算
	st_exp: [1, 1.02, 1.04, 1.06, 1.08, 1.1, 1.12, 1.14, 1.16, 1.18, 1.2],
	st_money: [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6.0],
	st_life: [0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5] //これは加算
};

const SURVIVOR_ABILITY_DATA = {
	collect_range: [1, 1.4, 1.8, 2.2, 2.6, 3.0],
	atk_power: [1, 1.06, 1.12, 1.18, 1.24, 1.3],
	atk_rate: [1, 0.94, 0.88, 0.82, 0.76, 0.7],
	atk_speed: [1, 1.4, 1.8, 2.2, 2.6, 3.0],
	atk_num: [0, 1, 1, 2, 2, 3], //これは加算
	atk_range: [1, 1.2, 1.4, 1.6, 1.8, 2.0],
	st_hp: [1, 1.2, 1.4, 1.6, 1.8, 2.0],
	dm_resist: [1, 0.9, 0.8, 0.7, 0.6, 0.5],
	st_rec: [0, 1, 1, 1, 1, 2], //これは加算
	st_exp: [1, 1.06, 1.12, 1.18, 1.24, 1.3],
	st_money: [1, 1.2, 1.4, 1.6, 1.8, 2.0],
	st_life: [0, 1, 2, 3, 4, 5], // これは加算
	move_speed: [1, 1.2, 1.4, 1.6, 1.8, 2]
};

const Survivor_var = {
	dm_se: 0,
	bf_x: 0,
	bf_y: 0,
	now_x: 0,
	now_y: 0,
	player_dir: 0
};

var survivorState = {
	newKeiken: [],
	se_cool: 0,
	keikenDel: []
};

// 1面 1wave　種類って感じ
var Wave = {
	for: [
		[{ id: "ossan", count: 50 }],
		[{ id: "monster_slime_b", count: 50 }],
		[{ id: "bat", count: 50 }],
		[{ id: "jelly", count: 50 }],
		[{ id: "ossan_hage", count: 50 }],
		[{ id: "monster_ghost", count: 50 }],
		[{ id: "ossan_red", count: 50 }],
		[{ id: "monster_bone", count: 50 }],
		[{ id: "monster_succubus_r", count: 50 }],
		[{ id: "ossan_gold", count: 50 }]
	],
	aika: [
		[{ id: "ossan", count: 50 }],
		[{ id: "bee", count: 50 }],
		[{ id: "spider", count: 50 }],
		[{ id: "ossan_long", count: 50 }],
		[{ id: "monster_ghost", count: 50 }],
		[{ id: "ossan_majime", count: 50 }],
		[{ id: "monster_imp", count: 50 }],
		[{ id: "ossan_red", count: 50 }],
		[{ id: "monster_flower_b", count: 50 }],
		[{ id: "ossan_gold", count: 50 }]
	],
	alice: [
		[{ id: "ossan", count: 50 }],
		[{ id: "monster_flower_r", count: 50 }],
		[{ id: "bat", count: 50 }],
		[{ id: "monster_ghost", count: 50 }],
		[{ id: "ossan_mura", count: 50 }],
		[{ id: "monster_imp", count: 50 }],
		[{ id: "ossan_hakase", count: 50 }],
		[{ id: "monster_flower_b", count: 50 }],
		[{ id: "monster_orc", count: 50 }],
		[{ id: "ossan_gold", count: 50 }]
	],
	hazu: [
		[{ id: "ossan", count: 50 }],
		[{ id: "monster_slime_b", count: 50 }],
		[{ id: "spider", count: 50 }],
		[{ id: "ossan_hakase", count: 50 }],
		[{ id: "monster_ghost", count: 50 }],
		[{ id: "monster_bone", count: 50 }],
		[{ id: "ossan_red", count: 50 }],
		[{ id: "ossan_battle", count: 50 }],
		[{ id: "medama", count: 50 }],
		[{ id: "ossan_gold", count: 50 }]
	],
	cla: [
		[{ id: "ossan", count: 50 }],
		[{ id: "scorpion", count: 50 }],
		[{ id: "jelly", count: 50 }],
		[{ id: "ossan_hage", count: 50 }],
		[{ id: "monster_ghost", count: 50 }],
		[{ id: "monster_imp", count: 50 }],
		[{ id: "ossan_red", count: 50 }],
		[{ id: "monster_succubus_r", count: 50 }],
		[{ id: "fishman", count: 50 }],
		[{ id: "ossan_gold", count: 50 }]
	]
};

// これ入れないとボカしoffなので拡大した時黒い線が表示される
var sviv_Bitmap_updateScaleMode = Bitmap.prototype._updateScaleMode;
Bitmap.prototype._updateScaleMode = function () {
	sviv_Bitmap_updateScaleMode.call(this);
	if (this._baseTexture && SceneManager._scene.constructor.name === "Scene_Survive_Battle_Map") {
		this._baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
	}
};

// これ入れないとボカしoffなので拡大した時黒い線が表示される
// Bitmap.prototype._updateScaleMode = function () {
// 	if (this._baseTexture) {
// 		if (SceneManager._scene.constructor.name === "Scene_Survive_Battle_Map") {
// 			this._baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
// 		} else {
// 			if (this._smooth) {
// 				this._baseTexture.scaleMode = PIXI.SCALE_MODES.LINEAR;
// 			} else {
// 				this._baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
// 			}
// 		}
// 	}
// };

//右のウィンドウ
var Scene_Survive_Battle_Map_start = Scene_Survive_Battle_Map.prototype.start;
Scene_Survive_Battle_Map.prototype.start = function () {
	Scene_Survive_Battle_Map_start.call(this);

	const rect = this.infoWindowRect();
	this._infoWindow = new Window_Tachie_Info(rect);
	this.addWindow(this._infoWindow);
};

Scene_Survive_Battle_Map.prototype.infoWindowRect = function () {
	const ww = SURVIVOR_CONST.RIGHT_MARGIN;
	const wh = Graphics.height;
	const wx = Graphics.width - ww;
	const wy = 0;
	return new Rectangle(wx, wy, ww, wh);
};

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

Window_Tachie_Info.prototype = Object.create(Window_Selectable.prototype);
Window_Tachie_Info.prototype.constructor = Window_Tachie_Info;

Window_Tachie_Info.prototype.initialize = function (rect) {
	Window_Selectable.prototype.initialize.call(this, rect);
	this.refresh();
	this.now_cg_num = 0;
};

Window_Tachie_Info.prototype.colSpacing = function () {
	return 0;
};

Window_Tachie_Info.prototype.refresh = function () {
	this.contents.clear();
	this.drawCharacter(3, 10, 10);
	//this.drawIcon(3,10,10);
};

Window_Tachie_Info.prototype.update = function () {
	this.processCursorMove();
	this.processHandling();
	this.processTouch();
	Window_Scrollable.prototype.update.call(this);

	var new_cg_num = Math.min(4, Math.max(1, 4 - Math.floor(($gameTemp.SURVIVOR_player_data.nowhp / $gameTemp.SURVIVOR_player_data.maxhp) * 4)));

	var ary = {
		for: "survive_tachi_for_",
		alice: "survive_tachi_alice_",
		hazu: "survive_tachi_hazu_",
		aika: "survive_tachi_aika_",
		cla: "survive_tachi_cla_"
	};

	if (this.now_cg_num < new_cg_num) {
		this.drawCharacter(3, 10, 10, ary[$gameTemp.survivor_ac], new_cg_num);
	}
};

Window_Tachie_Info.prototype.drawCharacter = function (iconIndex, x, y, new_cg_name, new_cg_num) {
	if (!new_cg_name) {
		return;
	}

	var bitmap = ImageManager.loadPicture(`${new_cg_name}${new_cg_num}`);

	if (bitmap._loadingState == "loaded" && this.contents) {
		this.contents.clear();
		const pw = 675;
		const ph = 1080;
		const sx = 0;
		const sy = 0;
		this.contents.blt(bitmap, sx, sy, pw, ph, x - 110, y, (pw * 2) / 3, (ph * 2) / 3);
		this.now_cg_num = new_cg_num;
	}
};

Window_Tachie_Info.prototype.value = function () {};

Window_Tachie_Info.prototype.currencyUnit = function () {
	return TextManager.currencyUnit;
};

Window_Tachie_Info.prototype.open = function () {
	this.refresh();
	Window_Selectable.prototype.open.call(this);
};

Window_Tachie_Info.prototype.loadWindowskin = function () {
	this.windowskin = ImageManager.loadSystem("Window_sviv");
};

Window_Tachie_Info.prototype.updateBackOpacity = function () {
	this.backOpacity = 255;
};
Window_Tachie_Info.prototype._refreshBack = function () {
	const m = this._margin;
	const w = Math.max(0, this._width - m * 2);
	const h = Math.max(0, this._height - m * 2);
	const sprite = this._backSprite;
	const tilingSprite = sprite.children[0];
	// [Note] We use 95 instead of 96 here to avoid blurring edges.
	sprite.bitmap = this._windowskin;
	sprite.setFrame(0, 0, 95, 95);
	sprite.move(m, m);
	sprite.scale.x = w / 95;
	sprite.scale.y = h / 95;
	tilingSprite.bitmap = this._windowskin;
	tilingSprite.setFrame(0, 96, 96, 96);
	tilingSprite.move(0, 0, w, h);
	tilingSprite.scale.x = 1 / sprite.scale.x;
	tilingSprite.scale.y = 1 / sprite.scale.y;
	sprite.setColorTone([0, 0, 0, 255]);
};

// ################ 左下のウィンドウ ################
function Window_Survive_Gold_Info() {
	this.initialize(...arguments);
}

(function () {
	Window_Survive_Gold_Info.prototype = Object.create(Window_Selectable.prototype);
	Window_Survive_Gold_Info.prototype.constructor = Window_Survive_Gold_Info;

	Window_Survive_Gold_Info.prototype.initialize = function (rect) {
		Window_Selectable.prototype.initialize.call(this, rect);
		this.refresh();
	};

	Window_Survive_Gold_Info.prototype.refresh = function () {
		this.contents.clear();

		this.contents.fontSize = 18;
		this.drawText(`${$gameVariables.value(173).gold} GOLD`, 0, 10, 130, "right");
	};

	Window_Survive_Gold_Info.prototype.update = function () {
		this.processCursorMove();
		this.processHandling();
		this.processTouch();
		Window_Scrollable.prototype.update.call(this);
		this.refresh();
	};

	Window_Survive_Gold_Info.prototype.open = function () {
		//this.refresh();
		Window_Selectable.prototype.open.call(this);
	};
	Window_Survive_Gold_Info.prototype.lineHeight = function () {
		return 12;
	};

	Window_Survive_Gold_Info.prototype.loadWindowskin = function () {
		this.windowskin = ImageManager.loadSystem("Window_sviv");
	};

	const _Scene_Survive_Battle_Map_createAllWindows = Scene_Survive_Battle_Map.prototype.createAllWindows;
	Scene_Survive_Battle_Map.prototype.createAllWindows = function () {
		_Scene_Survive_Battle_Map_createAllWindows.call(this);

		const rect = this.goldWindowRect();
		this._goldWindow = new Window_Survive_Gold_Info(rect);
		this.addChild(this._goldWindow);
	};

	Scene_Survive_Battle_Map.prototype.goldWindowRect = function () {
		const ww = 130 + 12 * 2;
		const wh = 30 * 1 + 14 * 2;
		const wx = 806;
		const wy = 54;
		return new Rectangle(wx, wy, ww, wh);
	};
})();
