function BossPear_process(v, k)
	local b = v.boss
	if b.route == 1 then
		if b.y < -150 then
			b.y = b.y + 2
		else
			b.route = 2
			b.r = b.y
			b.t = math.pi/2
			b.attack = 1
			for i=2, #b.enemies do
				b.enemies[i].immortal = false
				b.enemies[i].protected = true
			end
		end
	elseif b.route == 2 then
		if time - b.last_mood_swing > 2000 then
			b.last_mood_swing = time
			if math.random( 1, 10 ) > 7 then
				b.attack = (b.attack or 0) + 1
				if b.attack > 3 then
					b.attack = 1
					CreateOrangeStuff( -250, -400, { { -250, 400} }, 5 )
					CreateOrangeStuff(  250, -400, { {  250, 400} }, 5 )
					--b.r = dist( b.x, b.y, 0, 0 )
				end
			end
		end
		b.x = 0.5 * math.cos( b.t ) * b.r
		b.y = b.r + 50 * math.cos( b.t/3 )
		b.t = b.t + 0.01
	end
	if b.parts_destroyed and b.parts_destroyed == 5 then
		if not v.last_shot or time - v.last_shot > 1000 then
			v.last_shot = time
			PlaySnd( "shot1.wav", true, v.x, v.y )
			if math.random(1, 10) > 5 then
				for i=-5,5 do
					CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+i*math.pi/32 )
				end
			end
		end
	end
	UpdateBossPearPos()
end

function BossPear_processFront(v)
	local b = v.boss
	if b.attack then
		if (not v.last_shot) or time - v.last_shot > 1000 then
			v.last_shot = time
			if b.attack == 1 then
				PlaySnd( "shot3.wav", true, v.x, v.y )
				CreateSimpleRay( v.x, v.y, {0.8, 1, 0.8, 1} )
			elseif b.attack == 2 then
				PlaySnd( "shot3.wav", true, v.x, v.y )
				CreateSpriteBullet( v.x, v.y, "galactic_pear", 10, 4, {1, .8, .8, 1}, -math.pi/2 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/8 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/16 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
			elseif b.attack == 3 then
				PlaySnd( "shot3.wav", true, v.x, v.y )
				local pl = players[math.random(1,players_num)]
				local ang = -(math.atan2( pl.y-v.y, pl.x-v.x ))
				CreateSineBullet( v.x, v.y, 4, {1, 1, .8, 1}, ang )
				CreateSineBullet( v.x, v.y, 4, {1, 1, .8, 1}, ang+math.pi/16 )
				CreateSineBullet( v.x, v.y, 4, {1, 1, .8, 1}, ang-math.pi/16 )
			end
		end
	end
end

function BossPear_processSideGun(v)
	local b = v.boss
	if b.attack then
		if (not v.last_shot) or time - v.last_shot > 1300 then
			v.last_shot = time
			if b.attack == 1 then
				PlaySnd( "shot2.wav", true, v.x, v.y )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/8 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/16 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
			elseif b.attack == 2 then
				PlaySnd( "shot2.wav", true, v.x, v.y )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/8 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/16 )
				CreateSimpleBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
			end
		end
	end
end

function BossPear_processFarGun(v)
	local b = v.boss
	if b.attack then
		if (not v.last_shot) or time - v.last_shot > 1100 then
			v.last_shot = time
			if b.attack == 2 then
				PlaySnd( "shot1.wav", true, v.x, v.y )
				for i=1,12 do
					CreateSineBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+i*math.pi/6 )
				end
			elseif b.attack == 3 then
				PlaySnd( "shot1.wav", true, v.x, v.y )
				CreateSineBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2 )
				CreateSineBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/8 )
				CreateSineBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
				CreateSineBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2+math.pi/16 )
				CreateSineBullet( v.x, v.y, 4, {1, .8, .8, 1}, -math.pi/2-math.pi/8 )
			end
		end
	end
end

function UpdateBossPearPos()
	for key, value in pairs(BossPear.sprites) do
		SetObjPos( value[1], BossPear.x + value[2], BossPear.y + value[3] )
	end
	for key, value in pairs(BossPear.enemies) do
		if value.pear_sprite then
			value.x = BossPear.x + value.pear_sprite[2]
			value.y = BossPear.y + value.pear_sprite[3]
			if value.shift then
				value.x = value.x + value.shift
			end
		end
	end
end

function BossPear_hit( inst )
	local col = ( inst.max_health - inst.health ) / inst.max_health
	SetObjSpriteColor( inst.pear_sprite[1], { 1, 0, 0, col }, 0 )
end

function BossPear_destroy( x, y, v, k )
	if v.dead then
		return
	end
	v.dead = true
	local spr = v.pear_sprite[1]
	if v.type == "gun_left" then
		SetObjAnim( spr, 3, false )
	elseif v.type == "gun_right" then
		SetObjAnim( spr, 5, false )
	elseif v.type == "gun_far" then
		SetObjAnim( spr, 7, false )
	elseif v.type == "front" then
		SetObjAnim( spr, 9, false )
	end
	local b = v.boss
	b.parts_destroyed = (b.parts_destroyed or 0) + 1
	if b.parts_destroyed == 5 then
		b.enemies[1].immortal = false
	elseif b.parts_destroyed == 6 then
		for i=1,25 do
			CreateExplosion( BossPear.x + math.random(-100, 100), BossPear.y + math.random(-100, 100) )
		end
		for key, value in pairs( BossPear.sprites ) do
			SetObjDead( value[1] )
		end
		PearBoss = nil
		if BossDefeated then
			BossDefeated()
		end
	end
end

function CreateBossPear()
	PlaySnd("alarm.wav", true)
	BossPear = { x = 0, y = -500, route = 1, last_mood_swing = 0 }
	BossPear.sprites = {}
	local spr = CreateSprite( "galactic_pear", 0, 0 )
	SetObjPos( spr, 0, 0-500 )
	SetObjAnim( spr, 0, false )
	table.insert( BossPear.sprites, { spr, 0, 0 } )
	spr = CreateSprite( "galactic_pear", 0, 0 )
	SetObjPos( spr, 1, -69-500 )
	SetObjAnim( spr, 1, false )
	table.insert( BossPear.sprites, { spr, 1, -69 } )
	spr = CreateSprite( "galactic_pear", 0, 0 )
	SetObjPos( spr, -57, 28-500 )
	SetObjAnim( spr, 2, false )
	table.insert( BossPear.sprites, { spr, -55, 28 } )
	spr = CreateSprite( "galactic_pear", 0, 0 )
	SetObjPos( spr, 55, 28-500 )
	SetObjAnim( spr, 4, false )
	table.insert( BossPear.sprites, { spr, 55, 28 } )
	spr = CreateSprite( "galactic_pear", 0, 0 )
	SetObjPos( spr, 105, 31-500 )
	SetObjAnim( spr, 6, false )
	table.insert( BossPear.sprites, { spr, 105, 31 } )
	spr = CreateSprite( "galactic_pear", 0, 0 )
	SetObjPos( spr, -75, 31-500 )
	SetObjAnim( spr, 6, false )
	SetObjSpriteMirrored( spr, true )
	table.insert( BossPear.sprites, { spr, -75, 31 } )
	spr = CreateSprite( "galactic_pear", 0, 0 )
	SetObjPos( spr, 0, 170-500 )
	SetObjAnim( spr, 8, false )
	table.insert( BossPear.sprites, { spr, 0, 169 } )		
	BossPear.enemies = {}
	local enemy = {
		x = 0,
		y = -69-500,
		type = "pear",
		process = BossPear_process,
		boss = BossPear,
		size = 21,
		speed = 0,
		vel = {0, 0},
		health = math.floor( 15 * options.enemy_health + 0.5 ),
		max_health = math.floor( 10 * options.enemy_health + 0.5 ),
		hit = BossPear_hit,
		pear_sprite = BossPear.sprites[2],
		destroy = BossPear_destroy,
		immortal = true,
	}
	table.insert( enemies, enemy )
	table.insert( BossPear.enemies, enemy )
	enemy = {
		x = -57,
		y = 28-500,
		type = "gun_left",
		process = BossPear_processSideGun,
		health = math.floor( 10 * options.enemy_health + 0.5 ),
		max_health = math.floor( 10 * options.enemy_health + 0.5 ),
		boss = BossPear,
		size = 31,
		speed = 0,
		pear_sprite = BossPear.sprites[3],
		vel = {0, 0},
		hit = BossPear_hit,
		destroy = BossPear_destroy,
		immortal = true,
	}
	table.insert( enemies, enemy )
	table.insert( BossPear.enemies, enemy )
	enemy = {
		x = 55,
		y = 28-500,
		type = "gun_right",
		process = BossPear_processSideGun,
		health = math.floor( 10 * options.enemy_health + 0.5 ),
		max_health = math.floor( 10 * options.enemy_health + 0.5 ),
		boss = BossPear,
		size = 31,
		speed = 0,
		pear_sprite = BossPear.sprites[4],
		vel = {0, 0},
		hit = BossPear_hit,
		destroy = BossPear_destroy,
		immortal = true,
	}
	table.insert( enemies, enemy )
	table.insert( BossPear.enemies, enemy )
	table.insert( enemies, enemy )
	table.insert( BossPear.enemies, enemy )
	enemy = {
		x = 105,
		y = 31-500,
		type = "gun_far",
		health = math.floor( 10 * options.enemy_health + 0.5 ),
		max_health = math.floor( 10 * options.enemy_health + 0.5 ),
		boss = BossPear,
		size = 15,
		speed = 0,
		pear_sprite = BossPear.sprites[5],
		process = BossPear_processFarGun,
		vel = {0, 0},
		hit = BossPear_hit,
		destroy = BossPear_destroy,
		immortal = true,
	}
	table.insert( enemies, enemy )
	table.insert( BossPear.enemies, enemy )
	enemy = {
		x = -105,
		y = 31-500,
		type = "gun_far",
		health = math.floor( 10 * options.enemy_health + 0.5 ),
		max_health = math.floor( 10 * options.enemy_health + 0.5 ),
		boss = BossPear,
		size = 15,
		speed = 0,
		pear_sprite = BossPear.sprites[6],
		process = BossPear_processFarGun,
		shift = -30,
		vel = {0, 0},
		hit = BossPear_hit,
		destroy = BossPear_destroy,
		immortal = true,
	}
	table.insert( enemies, enemy )
	table.insert( BossPear.enemies, enemy )
	enemy = {
		x = 0,
		y = 170,
		type = "front",
		health = math.floor( 10 * options.enemy_health + 0.5 ),
		max_health = math.floor( 10 * options.enemy_health + 0.5 ),
		boss = BossPear,
		size = 15,
		speed = 0,
		pear_sprite = BossPear.sprites[7],
		process = function() end,
		vel = {0, 0},
		hit = BossPear_hit,
		destroy = BossPear_destroy,
		process = BossPear_processFront,
		immortal = true,
	}
	table.insert( enemies, enemy )
	table.insert( BossPear.enemies, enemy )
end
