//=============================================================================
// O_BattleCustom.js
//=============================================================================
/*:ja
 * @plugindesc 戦闘　カスタマイズ
 * @author 
 *
 * @help 
 */
 
(function() {
	var battle_list_x=0;
	var battle_list_y=0;
	var battle_list_height=0;
	var help_window_height=0;
	
	var enamyDamegeOffsetY = 0;

//***** エンカウント率の調整
	Game_Player.prototype.makeEncounterCount = function() {
		var n = $gameMap.encounterStep();
		var lange = 5;
		this._encounterCount = Math.randomInt(lange) + n;
	};
	Game_Map.prototype.encounterStep = function() {
		var res = 0;//$dataMap.encounterStep;
		var just_level = 0;
		var easy_level = 0;
		//マップ単位でレベル補正
		switch($gameMap.mapId()){
		case 7://カルタカの森南部
			just_level = 2;
			easy_level = 4;
			break;
		case 20://カルタカの森 中部北部
		case 21:
			just_level = 5;
			easy_level = 6;
			break;
		case 15://セイレン湖
			just_level = 8;
			easy_level = 11;
			break;
		case 16://ゲンガン洞穴
		case 4:
			just_level = 14;
			easy_level = 16;
			break;
		case 10://ヒャコルの塔
		case 12:
		case 5:
		case 22:
		case 13:
			just_level = 25;
			easy_level = 27;
			break;
		case 17://ウンジャク山
		case 23:
			just_level = 30;
			easy_level = 33;
			break;
		case 32://ガンショウ洞穴 地下祭壇
		case 33://ラスダン
		case 34:
		case 35:
		case 36:
			just_level = 38;
			easy_level = 41;
			break;
			
		}
		
		if(easy_level > 0 && $gameActors.actor(1).level >= easy_level){
			res = 65;
		}else if(just_level > 0 && $gameActors.actor(1).level >= just_level){
			res = 30;
		}else{
			res = 9;	
		}
		
		return res;
	};
	
//**** 戦闘突入時のフラッシュを無効化
	Scene_Map.prototype.startFlashForEncounter = function(duration) {
		var color = [0, 0, 0, 255];
    	$gameScreen.startFlash(color, duration);
	};
	
//**** 逃走率１００％
	BattleManager.processEscape = function() {
        $gameParty.performEscape();
        SoundManager.playEscape();
        var success = true;
		this.displayEscapeSuccessMessage();
		this._escaped = true;
		this.processAbort();
        return success;
    };

//バフ デバフの重ねがけを禁止
	Game_BattlerBase.prototype.isMaxBuffAffected = function(paramId) {
		return this._buffs[paramId] === 1;
	};
	Game_BattlerBase.prototype.isMaxDebuffAffected = function(paramId) {
		return this._buffs[paramId] === -1;
	};

//***** ステータスアイコンがキャラ画像の上に表示されるのを防ぐ
	Sprite_Enemy.prototype.createStateIconSprite = function() {};
	Sprite_Enemy.prototype.updateStateSprite = function() {};
	Sprite_Enemy.prototype.setBattler = function(battler) {
		Sprite_Battler.prototype.setBattler.call(this, battler);
		this._enemy = battler;
		this.setHome(battler.screenX(), battler.screenY());
		//this._stateIconSprite.setup(battler);
	};
//***** 敵ダメージポップアップ位置
	Sprite_Enemy.prototype.damageOffsetY = function() {
		return enamyDamegeOffsetY;
	};

//***** 敵画像表示位置 常に下いっぱいに表示
	var _Game_Enemy_prototype_setup=Game_Enemy.prototype.setup;
	Game_Enemy.prototype.setup = function(enemyId, x, y) {
		if($dataEnemies[enemyId].note.match("[boss]")){
			this._enemyId = enemyId;
			this._screenX = x;
			this._screenY = Graphics.boxHeight;
			this.recoverAll();
			
			//ダメージポップアップのメモ欄での指定を判定
			if($dataEnemies[enemyId].note.match("damage_offsetY:")){
				var tmp = $dataEnemies[enemyId].note.split("damage_offsetY:");
				tmp = tmp[1].split(']');
				enamyDamegeOffsetY = Number(tmp[0]);
			}
		}else{
			enamyDamegeOffsetY = 0;
			
			_Game_Enemy_prototype_setup.call(this,enemyId, x, y);	
		}
		
		if(this._screenY >= 512){
			enamyDamegeOffsetY = 512 - 576;
		}
		
	};

//*****
	var _Scene_Battle_prototype_createSpriteset = Scene_Battle.prototype.createSpriteset;
	Scene_Battle.prototype.createSpriteset = function() {
	   _Scene_Battle_prototype_createSpriteset.call(this);
		
		this._front_skin = new Sprite(ImageManager.loadSystem("battle_frame"));
		this.addChild(this._front_skin);
	};

//******バトルログの位置変更
	Window_BattleLog.prototype.initialize = function() {
		var width = this.windowWidth()/2;
		var height = this.windowHeight();
		//var y=Graphics.boxHeight-height;//Graphics.boxHeight-(this.lineHeight()*this.maxLines());
		var x = this.windowWidth() / 2 - 50;
		var y = 0;
		Window_Selectable.prototype.initialize.call(this, x, y, width, height);
		this.opacity = 0;
		this._lines = [];
		this._methods = [];
		this._waitCount = 0;
		this._waitMode = '';
		this._baseLineStack = [];
		this._spriteset = null;
		this.createBackBitmap();
		this.createBackSprite();
		this.refresh();
	};
	
	Window_BattleLog.prototype.drawLineText = function(index) {
		var rect = this.itemRectForText(index);
		rect.y+=this.y;
		this.contents.clearRect(rect.x, rect.y, rect.width, rect.height);
		this.drawTextEx(this._lines[index], rect.x, rect.y, rect.width);
	};
	//バトルログの背景は描画しない
	Window_BattleLog.prototype.drawBackground = function(){};
	
//******開幕のパーティーコマンドをすっ飛ばす
	Scene_Battle.prototype.changeInputWindow = function() {
        if (BattleManager.isInputting()) {
            BattleManager._actorIndex = 0;
            this.startActorCommandSelection();
        } else {
            this.endCommandSelection();
        }
    };
//******アクターコマンドを上書き 逃げるを追加 Escを除外
	Scene_Battle.prototype.createActorCommandWindow = function() {
		this._actorCommandWindow = new Window_ActorCommand();
		this._actorCommandWindow.setHandler('attack', this.commandAttack.bind(this));
		this._actorCommandWindow.setHandler('skill',  this.commandSkill.bind(this));
		this._actorCommandWindow.setHandler('item',   this.commandItem.bind(this));
		this._actorCommandWindow.setHandler('guard',  this.commandGuard.bind(this));
		this._actorCommandWindow.setHandler('escape', this.commandEscape.bind(this));
		//this._actorCommandWindow.setHandler('cancel', this.selectPreviousCommand.bind(this));
		this.addWindow(this._actorCommandWindow);
	};
//******敵選択をすっ飛ばす 敵は１体のみだから	
	Scene_Battle.prototype.selectEnemySelection = function() {
		BattleManager.startTurn();
	};
//******味方選択をすっ飛ばす
	Scene_Battle.prototype.selectActorSelection = function() {
		BattleManager.startTurn();
	};
//*****アクターコマンドに逃げるを追加
	var _Window_ActorCommand_prototype_makeCommandList=Window_ActorCommand.prototype.makeCommandList;
	Window_ActorCommand.prototype.makeCommandList = function() {
		_Window_ActorCommand_prototype_makeCommandList.call(this);
		if (this._actor) {
			this.addCommand(TextManager.escape, 'escape', BattleManager.canEscape());	
		}
	};
	Window_ActorCommand.prototype.numVisibleRows = function() {
		return 5;//コマンドの表示行数
	};
//*****アクターコマンドの位置調整
	var _Window_ActorCommand_prototype_initialize=Window_ActorCommand.prototype.initialize;
	Window_ActorCommand.prototype.initialize = function() {
	   _Window_ActorCommand_prototype_initialize.call(this);
	    this.x=(Graphics.boxWidth/2)-(this.width/2);//+this.width;//-(this.width/2);
		this.y=(Graphics.boxHeight/2)-(this.height/2);
		
		battle_list_x=this.x+this.width;
		battle_list_y=this.y;
		battle_list_height=this.height;
		
	};
//*****ステータスウィンドウ  入力完了後の位置更新をスキップ
	Scene_Battle.prototype.updateWindowPositions = function() {
	};

//*****ヘルプウィンドウの位置・サイズ調整
	Scene_Battle.prototype.createHelpWindow = function() {
		this._helpWindow = new Window_Battle_Help();
		this._helpWindow.visible = false;
		this.addWindow(this._helpWindow);
	};
	
	//Window_battle_help
	function Window_Battle_Help() {
		this.initialize.apply(this, arguments);
	}
	Window_Battle_Help.prototype = Object.create(Window_Help.prototype);
	Window_Battle_Help.prototype.constructor = Window_Battle_Help;
	
	Window_Battle_Help.prototype.initialize = function(numLines) {
		var width = Graphics.boxWidth-battle_list_x;
		var height = this.fittingHeight(numLines || 1);
		help_window_height=height;
		Window_Base.prototype.initialize.call(this, battle_list_x, battle_list_y+battle_list_height, width, height);
		this._text = '';
	};
	//ヘルプウィンドウのフォントサイズ
	Window_Battle_Help.prototype.standardFontSize= function() {
		return 22;
	};

//**** 戦闘開始時のメッセージを表示しない
	BattleManager.displayStartMessages = function() {
		//戦闘中のメッセージウィンドウは枠無しに
		$gameMessage.setBackground(1);
	};
//**** 戦闘勝利のメッセージを表示しない
	BattleManager.displayVictoryMessage = function() {};
//**** 戦闘勝利のメッセージを一括表示
	BattleManager.displayExp = function() {
		var text='';
		if (this._rewards.exp > 0) {
			text += this._rewards.exp+'Exp ';//TextManager.obtainExp.format(exp, TextManager.exp);	
		}
		if (this._rewards.gold > 0) {
			text += this._rewards.gold+'G ';//$gameMessage.add('\\.' + TextManager.obtainGold.format(gold));
		}
		if(text!=''){
			$gameMessage.add('Obtained '+text+'!');
		}
	};
	BattleManager.displayGold = function() {};

//LevelUP時にSEを鳴らす
	var _Game_Actor_prototype_displayLevelUp = Game_Actor.prototype.displayLevelUp;
	Game_Actor.prototype.displayLevelUp = function(newSkills) {
		AudioManager.playSe({'name':'Saint7','volume':90,'pitch':100,'pan':0});
		_Game_Actor_prototype_displayLevelUp.call(this,newSkills);
	};

//****アイテム/スキルウィンドウ
	Window_BattleItem.prototype.maxCols = function() {
		return 1;
	};
	
	Window_BattleItem.prototype.initialize = function(x, y, width, height) {
		
		this.makeItemList();
		
		var view_line = 5;
		var item_line= this._data.length; 
		//アイテム数に併せてリストウィンドウのY座標、高さを変動させる
		if(item_line > view_line){
			var hide_heihgt = (battle_list_height / view_line) * (item_line - view_line);
			
			if(hide_heihgt > battle_list_y){//ウインドウ上端を超える場合
				height=battle_list_height + battle_list_y;
				y = 0;
			}else{
				height=battle_list_height + hide_heihgt;
				y = battle_list_y - hide_heihgt;
			}
		}else{
			y = battle_list_y;
			height=battle_list_height;	
		}
		
		width=Graphics.boxWidth-battle_list_x;
		//height=battle_list_height;
		Window_ItemList.prototype.initialize.call(this, battle_list_x, y, width, height);
		this.hide();
	};
	
	Window_BattleSkill.prototype.maxCols = function() {
		return 1;
	};
	
	Window_BattleSkill.prototype.initialize = function(x, y, width, height) {
		var view_line = 5;
		var skill_line= $gameParty.members()[0]._skills.length; 
		//スキル数に併せてリストウィンドウのY座標、高さを変動させる
		if(skill_line > view_line){
			var hide_heihgt = (battle_list_height / view_line) * (skill_line - view_line);
			
			if(hide_heihgt > battle_list_y){//ウインドウ上端を超える場合
				height=battle_list_height + battle_list_y;
				y = 0;
			}else{
				height=battle_list_height + hide_heihgt;
				y = battle_list_y - hide_heihgt;
			}
		}else{
			y = battle_list_y;
			height=battle_list_height;	
		}
		
		width=Graphics.boxWidth-battle_list_x;
		Window_ItemList.prototype.initialize.call(this, battle_list_x, y, width, height);
		this.hide();
	};
	Window_BattleSkill.prototype.standardFontSize = function() {
		return 26;//defo 28
	};
	Window_BattleSkill.prototype.lineHeight = function() {
		return 32;//defo 36
	};
	
	Window_BattleSkill.prototype.drawIcon = function(iconIndex, x, y) {
		var bitmap = ImageManager.loadSystem('IconSet');
		var pw = Window_Base._iconWidth;
		var ph = Window_Base._iconHeight;
		var sx = iconIndex % 16 * pw;
		var sy = Math.floor(iconIndex / 16) * ph;
		this.contents.blt(bitmap, sx, sy, pw, ph, x, y-1, pw*0.8, ph*0.8);//アイコンサイズを小さく
	};

	/*
	Window_BattleSkill.prototype.maxRows = function() {
		console.log(this.maxItems()+' / '+this.maxCols());
		 return Math.max(Math.ceil(this.maxItems() / this.maxCols()), 1);
	};*/
	
	


//*****Game Over処理の無効化とコモンイベントの呼び出し
	BattleManager.updateBattleEnd = function() {
		if (this.isBattleTest()) {
			AudioManager.stopBgm();
			SceneManager.exit();
		} else if (!this._escaped && $gameParty.isAllDead()) {
			$gameScreen.startFadeOut(20);//フェードアウト状態
			$gameParty.reviveBattleMembers();
			SceneManager.goto(Scene_Gameover);
			
		} else {
			SceneManager.pop();
		}
		this._phase = null;
	};
	
	Scene_Gameover.prototype.playGameoverMusic = function() {
	};
	Scene_Gameover.prototype.createBackground = function() {
	};
	Scene_Gameover.prototype.terminate = function() {
		Scene_Base.prototype.terminate.call(this);
	};
	Scene_Gameover.prototype.start = function() {
		Scene_Base.prototype.start.call(this);
	};
	Scene_Gameover.prototype.stop = function() {
		Scene_Base.prototype.stop.call(this);
	};
	Scene_Gameover.prototype.update = function() {
		
		$gameTemp.reserveCommonEvent(6);//コモンイベント
		SceneManager.goto(Scene_Map);
		Scene_Base.prototype.update.call(this);
	};
	
})();