function Fight(_par, _notify, _data)
{
	var th = this
//--------------------------------//
	var cur = "user"
	var num_play = 0

	////////////////////////////////////
	this.getData = function(){
		var p = _data
		p.current_stage = cur

		return p
	}

	this.update = function(){
		calcHit("user")
		calcHit("bot")
	}

	this.updateView = function(){
		if(_data.num_attack >=5){
			// _par.setModal(null, this.getData().only_ready)
			// _par.resetOnlyReady()
			return
		}
		if(cur == "user")
			_par.setModal("text_your_attack")
		else if(cur == "bot"){
			_par.setModal("text_opponents_attack")
			_data.num_attack++
		}
	}
	this.shotToCell = function(player, i, j, num){
		var n = num ? num : 0
		if(player == "user")
			_data.all_shots_user++
		else if(player == 'bot')
			_data.all_shots_bot++
		var flag = false
		var arr = new Array()
		var curShips = player == "user" ? _data.ships_bot : _data.ships_user
		var curMines = player == "user" ? _data.mines_bot : _data.mines_user
		var curChests = player == "user" ? _data.chest_bot : _data.chest_user
		arr = arr.concat(curShips)
		arr = arr.concat(curMines)
		arr = arr.concat(curChests)
		for(var c=0; c< arr.length; c++){
			var m = arr[c].getTakes()
			for(var d=0; d< m.length; d++){
				if(i == m[d].i && j == m[d].j && m.length > arr[c].getHits().length){
							console.log(arr[c].getName())
							arr[c].setHit(d)
							addMove(player, i, j, arr[c].getName())
							if(arr[c].getName() == "mine"){
								_par.setLife(player, -1)
							}
							if(arr[c].getName() == "chest"){
								if(player == "user")
									_data.num_chests_user++
								else
									_data.num_chests_bot++
								t_delayOn(.5*n + .5, function(){
																	addBonus(player)
																})								
							}
							if(arr[c].getName() == "ship"){
								shotShip(player, arr[c].getTakes().length)
							}
							flag = true
						}
			}
		}
		if(player == "bot")
			buyBonuses()
		if(!flag)
			addMove(player, i, j, "empty")
		var end = true
		for(var a=0; a<curShips.length; a++)
			if(curShips[a].getTakes().length>curShips[a].getHits().length)
				end = false

		if(end){		
			t_delayOn(2, function(){	
				t_allInPause()
				var hit_rate_user =  _data.ship_shots_user == 0 ? 0 : Math.round(_data.ship_shots_user/_data.all_shots_user * 100)
				var hit_rate_bot = _data.ship_shots_bot == 0 ? 0 : Math.round(_data.ship_shots_bot/_data.all_shots_bot * 100)
				var ttl = player == "user" ? "text_red_victory" : "text_red_you_lose"
				v_choiseLocation("gameOver", {title: ttl, hit_rate_user: hit_rate_user, hit_rate_bot: hit_rate_bot, treasures_user: _data.num_chests_user, treasures_bot: _data.num_chests_bot, num_money:_data.num_money,
												reason: "sunk"});
				if(player == "user")
					MusicCTRL.play("sound6");
				else
					MusicCTRL.play("sound3");
				
			})
		}

		// console.log("/////////")
		// console.log("user ", _data.ship_shots_user, _data.all_shots_user)
		// console.log("bot ", _data.ship_shots_bot, _data.all_shots_bot)
	}

	this.changeCurrentPlayer = function(){
		num_play++
		if(num_play>=2)
			cur = null
		else
			cur = cur == "user" ? "bot" : "user"
		this.updateView()
		_notify()
	}

	this.setAlphaObjects = function(v){
		var change = new Array()
		change = change.concat(_data.ships_user)
		for(var a=0; a< change.length; a++)
			change[a].setAlpha(v)	
	}

	this.getAllTime = function(){
		return 0
	}

	this.after_time = function(){

	}
	function buyBonuses(){
		var randKind = Math.round(Math.random()*3)
		var randBool = Math.round(Math.random())
		// if(randBool){
			if(randKind == 2 && _data.num_bot_money>=__config.price_bonuses.bombs){
				_data.multibomb_num_bot ++
				_data.num_bot_money -=__config.price_bonuses.bombs
				// console.log("addBonus")
			// 
			}else if(randKind == 1 && _data.num_bot_money>=__config.price_bonuses.torpedo){
				_data.torpedo_num_bot ++
				_data.num_bot_money -=__config.price_bonuses.torpedo
				// console.log("addBonus")
			}else if(randKind == 0 && _data.num_bot_money>=__config.price_bonuses.air_strike){
				_data.dinamite_num_bot ++
				_data.num_bot_money -=__config.price_bonuses.air_strike
				// console.log("addBonus")
			}
			else if(randKind == 3 && _data.num_bot_money>=__config.price_bonuses.life){
				_par.setLife("bot", 1)
				_data.num_bot_money -=__config.price_bonuses.life
				// console.log("addBonus")
			}
			// }else if(randKind == 2 && _data.num_bot_money>=__config.price_bonuses.submarine){
			// 	_data.periskope_num_bot ++
			// 	_data.num_bot_money -=__config.price_bonuses.submarine
			// 	// console.log("addBonus")
		// }
	}
	// Вычисление столнкновений
	function calcHit(player){
		var arr = new Array()
		var curShips = player == "user" ? _data.ships_bot : _data.ships_user
		var curMines = player == "user" ? _data.mines_bot : _data.mines_user
		var curChests = _data.chests
		arr = arr.concat(curShips)
		arr = arr.concat(curMines)
		arr = arr.concat(curChests)
		var curTargets = player == "user" ? _data.user_targets : _data.bot_targets		
		for(var c=0; c< arr.length; c++){
			var m = arr[c].getTakes()
			for(var d=0; d< m.length; d++)
				for(var a=0; a< curTargets.length; a++)
					for(b=0; b<curTargets[a].length; b++){
						if(curTargets[a][b].i == m[d].i && curTargets[a][b].j == m[d].j && m.length > arr[c].getHits().length){
							arr[c].setHit(d)
							if(arr[c].getName() == "mine")
								_par.setLife(player, -1)
							if(arr[c].getName() == "chest")
								addBonus(player)
						}
					}
			}
	}

	function addBonus(player){
		MusicCTRL.play("sound4");
		var rand = Math.round(Math.random()*4)
		var l = _par.getData().life_user
		if(rand == 0){
			if(player == "user"){
				_data.multibomb_num_user ++
				t_delayOn(.5, function(){
					v_choiseLocation("bonus", {name: "air_strike", life: l})
				})
			}else
				_data.multibomb_num_bot ++ 
		}else if(rand == 1){
			if(player == "user"){
				_data.dinamite_num_user ++ 
				t_delayOn(.5, function(){
					v_choiseLocation("bonus", {name: "bombs", life: l})
				})
				
			}else
				_data.dinamite_num_bot ++ 
		}else if(rand == 2){
			if(player == "user"){
				_data.periskope_num_user ++ 
				t_delayOn(.5, function(){
					v_choiseLocation("bonus", {name: "submarine", life: l})
				})
				
			}else
				_data.periskope_num_bot ++ 
		}else if(rand == 3){
			_par.setLife(player, 1)
			l = _par.getData().life_user
			if(player == "user")
				t_delayOn(.5, function(){
					v_choiseLocation("bonus", {name: "life", life: l})
				})
				
		}else if(rand == 4){
			if(player == "user"){
				_data.torpedo_num_user ++ 
				t_delayOn(.5, function(){
					v_choiseLocation("bonus", {name: "torpedo", life: l})
				})
				
			}else
				_data.torpedo_num_bot ++ 
		}
	}

	function shotShip(player){
		if(player == "user"){
			_data.num_money += __config.price_ship
			_data.ship_shots_user++
		}else if(player == 'bot'){
			_data.num_bot_money += __config.price_ship_bot
			_data.ship_shots_bot++
		}
		// MusicCTRL.play("sound2");
		_par.setMoney(player, __config.price_ship)
	}

	function addMove(player, i, j, name){
		var curMas = player == "user" ? _data.used_move_user : _data.used_move_bot
		var debs = searchDebris(curMas)
		if(name == "empty"){
			if(!searchInMas(curMas, i, j))
				var o = new SeaObjects("empty", 1, "goriz")
		}else if(name == "mine"){
			if(!searchInMas(curMas, i, j)){
				var o = new SeaObjects("mine", 1, "goriz", null, true)
			}
		}else if(name == "chest"){
			if(!searchInMas(curMas, i, j))
				var o = new SeaObjects("chest", 1, "goriz", null, true)
		}else if(name == "ship" && player == "user"){	
			var r// = Math.round(Math.random() * 4)+1
			function se(){
				r = Math.round(Math.random() * 4)+1
				for(var a=0; a<debs.length; a++){
					if(debs[a].getTakes()[0].i == i-1 || debs[a].getTakes()[0].i == i+1)
						if(debs[a].getKind() == r){
							se()
						}
					if(debs[a].getTakes()[0].j == j-1 || debs[a].getTakes()[0].j == j+1)
						if(debs[a].getKind() == r){
							se()
						}
				}
			}
			se()
			if(!searchInMas(curMas, i, j))
				var o = new SeaObjects("debris", 1, "goriz", r, true)

			_data.flying_money = {i: i, j:j}
			_notify()
		}
		if(o){
			o.setTakes(i, j)
			curMas.push(o)
		}
		_notify()

		if(name == "mine"){
			_par.setModal("text_mine_hit")
		}else if(name == "ship"){
			_par.setModal("text_target_hit")
		}
		_notify()
		// console.log("asdw", player, name)
	}

	function searchInMas(m, i, j){
		for(var a=0; a<m.length; a++){
			var t = m[a].getTakes()
			for(var b=0; b<t.length; b++)
				if(t[b].i == i && t[b].j == j)
					return true
		}
	}

	function searchDebris(mas){
		var m = new Array()
		for(var a=0; a<mas.length; a++)
			if(mas[a].getName() == "debris")
				m.push(mas[a])
		return m

	}
}