freeslot("MT_SANS", "sfx_buzzn", "S_SANS", "sfx_uexpl", "sfx_uheal", "SPR_SSNS", "SPR_ADSW", "MT_UTSLASH", "S_UTSLASH","sfx_sscrea", "sfx_ssend", "sfx_sbell", "sfx_uslash", "sfx_uhit")

sfxinfo[sfx_ssend].caption = "Thrust"
sfxinfo[sfx_sbell].caption = "Eye is Very Scary"
sfxinfo[sfx_uslash].caption = "Slash"
sfxinfo[sfx_uhit].caption = "Hit"
sfxinfo[sfx_uheal].caption = "Heal"
sfxinfo[sfx_uexpl].caption = "Explosion"
sfxinfo[sfx_buzzn].caption = "No"
sfxinfo[sfx_sscrea].caption = "Screaming"

local hehe = {
	MT_BLUECRAWLA,
	MT_REDCRAWLA,
	MT_JETTBOMBER,
	MT_JETTGUNNER,
	MT_EGGGUARD,
	MT_ROBOHOOD,
	MT_POINTY,
	MT_FACESTABBER,
	MT_DRAGONBOMBER,
	MT_CRUSHSTACEAN,
	MT_WHIRLWIND_BOX,
	MT_FORCE_BOX,
	MT_ALARM,
	MT_YELLOWSHELL,
	MT_GSNAPPER
}

mobjinfo[MT_SANS] = {
	doomednum = 2727,
    spawnstate = S_SANS,
	painstate = S_SANS,
	deathstate = S_SPRK1,
	spawnhealth = 24,
    reactiontime = 1,
    radius = 64*FRACUNIT,
    height = 64*FRACUNIT,
	flags = MF_NOGRAVITY|MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE
}

states[S_SANS] = {
	sprite = SPR_SSNS,
	frame = A,
	tics = -1,
	nextframe = S_SANS,
	var1 = 0,
	var2 = 0
}

mobjinfo[MT_UTSLASH] = {
	doomednum = -1,
    spawnstate = S_UTSLASH,
	spawnhealth = 16,
    reactiontime = 1,
	deathstate = S_SPRK1,
    radius = 48*FRACUNIT,
    height = 64*FRACUNIT,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT
}

states[S_UTSLASH] = {
	sprite = SPR_ADSW,
	frame = A,
	tics = 105,
	nextframe = S_NULL,
	var1 = 0,
	var2 = 0
}

addHook("PlayerThink", function(player)
	if (player.mo and player.mo.valid)
		player.kred = $ or 0
		if (leveltime == 0)
			player.kred=0
		end
		if (gamemap != 355)
			return
		end
		if (player.kred == 0)
			return
		end
		player.c1hh = $ or 0
		camera.chase = false
		if (player.c1hh == 0) and (player.cmd.buttons & BT_CUSTOM1)
			player.c1hh = 1
			if (player.mo.heals > 0)
				S_StartSound(nil, sfx_uheal, player)
				player.mo.khp = $ + 50
				player.mo.krn = player.mo.khp
				player.mo.kr = false
				player.mo.heals = $ - 1
			else
				S_StartSound(nil, sfx_buzzn, player)
			end
		end
		if (player.c1hh == 1) and not (player.cmd.buttons & BT_CUSTOM1)
			player.c1hh = 0
		end
		player.attackcooldown = $ or 0
		player.attackcooldown = max(0,$-1)
		if (player.playerstate == PST_REBORN)
			player.kred = 0
		end
		if (player.attackcooldown <= 0) and (player.cmd.buttons & BT_ATTACK)
			player.attackcooldown = 16

			local th = P_SpawnMobj(player.mo.x + P_ReturnThrustX(player.mo, player.mo.angle, 60*FRACUNIT+player.speed), player.mo.y + P_ReturnThrustY(player.mo, player.mo.angle, 60*FRACUNIT+player.speed), player.mo.z+player.mo.height/2, MT_UTSLASH)
			th.angle = player.mo.angle
			th.target = player.mo
			th.momy = FixedMul(70*FRACUNIT, sin(player.mo.angle))	
			th.momx = FixedMul(70*FRACUNIT,cos(player.mo.angle))	
			th.momz = FixedMul(70*FRACUNIT, sin(player.aiming))	
			S_StartSound(player.mo, sfx_uslash)
		end
	end
end)

addHook("MobjSpawn", function(mo)
	mo.phase = 1
	mo.chattime = 0
	mo.chatted = false
end, MT_SANS)

addHook("MobjThinker", function(mo)
	if (mo.phase == 1)
		for player in players.iterate
			if (player.mo.valid and player.mo.y >= 1200*FRACUNIT)
				mo.phase = 2
				P_InstaThrust(player.mo, 270*ANG1, 108*FRACUNIT)
				player.mo.momz = 12*FRACUNIT
				player.pflags = $ & ~PF_SPINNING
				player.pflags = $ & ~PF_JUMPED
				player.mo.state = S_PLAY_PAIN
				player.sansflash = TICRATE/2
				S_StartSound(nil, sfx_uexpl)
				player.powers[pw_nocontrol] = TICRATE/2
				S_ChangeMusic("megalo", true, player)
				mo.destscale = 2*FRACUNIT
			end
		end
	end

	if (mo.phase >= 2)
		if (mo.phase == 2) and (mo.health <= 12)
			mo.phase = 3
			for sector in sectors.iterate
				sector.floorpic = "GFZROCK"
				sector.ceilingpic = "GFZROCK"
			end
			for side in sides.iterate
				side.toptexture = 1481
				side.bottomtexture = 1481
				side.midtexture = 1481
			end
			chatprint("\x85<Sans> Dont do that Boi.")
			mo.destscale = 3*FRACUNIT
			for player in players.iterate
				player.mo.state = S_PLAY_PAIN
				P_StartQuake(79*FRACUNIT, 3*TICRATE)
				P_FlashPal(player, PAL_NUKE, 3*TICRATE)
				P_Earthquake(player.mo, player.mo, 256*FRACUNIT)
				player.sansflash = TICRATE/2
				player.mo.state = S_PLAY_PAIN
				for a=1,64 do
					local explode = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_BOSSEXPLODE)
					explode.momx = P_RandomRange(-FRACUNIT, FRACUNIT) * 4
					explode.momy = P_RandomRange(-FRACUNIT, FRACUNIT) * 4
					explode.momz = P_RandomFixed() * 8
					explode.scale = 5*FRACUNIT
					S_StartSound(nil, sfx_alarm, player)
					S_StartSound(nil, sfx_cybdth, player)
					S_StartSound(nil, sfx_sscrea, player)
					S_StartSound(nil, sfx_crumbl, player)
					S_StartSound(nil, sfx_bgxpld, player)
					S_StartSound(nil, sfx_sscrea)
				end
			end
			for mobj in mobjs.iterate(mobj)
				P_SetObjectMomZ(mobj, 128*FRACUNIT, false)
			end
			mo.momz=0
		end
		local spd = 1
		local offset = cos(mo.chattime*2*ANG1)*96
		if (mo.phase==3)
			spd = 3
			for player in players.iterate
				player.viewrollangle = cos(mo.chattime*4*ANG1)/FRACUNIT*ANG1
			end
			offset = cos(mo.chattime*8*ANG1)*144
			if (mo.chattime % TICRATE*4 == 0)
				S_StartSound(mo,sfx_s3k51)
				local bruh = P_SpawnMobj(mo.x, mo.y, mo.z, hehe[P_RandomRange(10,15)])
				bruh.health = 1
				bruh.spritexscale=(P_RandomRange(1,4)*FRACUNIT)/2
				bruh.spriteyscale=(P_RandomRange(1,4)*FRACUNIT)/2
				bruh.momz = 3*FRACUNIT
				for side in sides.iterate
					side.toptexture = P_RandomRange(0,1481)
					side.bottomtexture = P_RandomRange(0,1481)
					side.midtexture = P_RandomRange(0,1481)
				end
			end
			for mobj in mobjs.iterate(mobj)
				mobj.spritexoffset = P_RandomRange(-2,2)*FRACUNIT
				mobj.spriteyoffset = P_RandomRange(-2,2)*FRACUNIT
				mobj.spritexscale = (P_RandomRange(1,4)*FRACUNIT)/2
				mobj.spriteyscale = (P_RandomRange(1,4)*FRACUNIT)/2
			end
		end
		mo.chattime = $ + 1
		mo.momx = 69
		mo.flags2 = $ & ~MF2_FRET
		P_MoveOrigin(mo, offset, 1280*FRACUNIT+sin((mo.chattime*spd)*ANG1)*(1440+(spd-1)*640), 200*FRACUNIT)
		if (mo.chattime > TICRATE) and (mo.chatted == false)
			mo.chatted = true
			chatprint("\x84<Sans> \x80heh. do you wanna skibidi out pal?")
			S_StartSound(nil, sfx_radio)
		end
		if (mo.chattime % TICRATE*3/(mo.phase-1) == 0)
			S_StartSound(mo,sfx_s3k51)
			local bruh = P_SpawnMobj(mo.x, mo.y, mo.z, hehe[P_RandomRange(1,9)])
			bruh.health = 1
			bruh.spritexscale=(P_RandomRange(1,4)*FRACUNIT)/2
			bruh.spriteyscale=(P_RandomRange(1,4)*FRACUNIT)/2
			bruh.momz = 3*FRACUNIT
		end
		for player in players.iterate
			if (player.playerstate == PST_REBORN)
				S_ChangeMusic("megalo", true, player)
			end
			player.kred = 1
		end
	end
end, MT_SANS)

addHook("MobjThinker", function(mobj)
	if (mobj.valid)
		mobj.timer = $ or 0
		mobj.initx = $ or mobj.momx
		mobj.inity = $ or mobj.momy
		mobj.initz = $ or mobj.momz
		mobj.timer = $ + 1
		if (mobj.initx ~= mobj.momx)
		or (mobj.inity ~= mobj.momy)
		or (mobj.initz ~= mobj.momz)
			mobj.momx, mobj.momy, mobj.momz = 0,0,0
			P_KillMobj(mobj)
			return
		end
		if (mobj.timer % TICRATE/4 == 0)
			P_SpawnGhostMobj(mobj)
		end
		if (mobj.timer > TICRATE*5)
			P_KillMobj(mobj)
		end
	end
end, MT_UTSLASH)

addHook("MobjDeath",function(mobj)
	for player in players.iterate
		player.mo.state = S_PLAY_PAIN
		S_StartSound(nil, sfx_sscrea)
		P_SetObjectMomZ(player.mo, 128*FRACUNIT, false)
		P_Earthquake(player.mo, player.mo, 256*FRACUNIT)
        P_StartQuake(79*FRACUNIT, 3*TICRATE)
        --P_FlashPal(player, PAL_NUKE, 3*TICRATE)

		for a=1,32 do
			local explode = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_BOSSEXPLODE)
			explode.momx = P_RandomRange(-FRACUNIT, FRACUNIT) * 4
			explode.momy = P_RandomRange(-32767, 32767) * 4
			explode.momz = P_RandomFixed() * 8
			explode.scale = 5*FRACUNIT
			explode.helpme = 1
			S_StartSound(nil, sfx_alarm, player)
			S_StartSound(nil, sfx_cybdth, player)
			S_StartSound(nil, sfx_crumbl, player)
			S_StartSound(nil, sfx_bgxpld, player)
		end
		G_ExitLevel()
	end
end, MT_SANS)

//some freinly
addHook("MobjMoveCollide", function(fist, victim)
    if fist.valid and victim.valid
	and (victim.z + victim.height + victim.momz >= fist.z + fist.target.momz)
	and (victim.z + victim.height + victim.momz <= fist.z + fist.target.momz + fist.height)
		P_DamageMobj(victim, fist, fist.target)
		S_StartSound(victim, sfx_uhit)
		P_KillMobj(fist)
    end
end, MT_UTSLASH)

addHook("MobjDamage", function(fist, victim)
    if fist.valid and victim.valid
	and (victim.type == MT_BOSSEXPLODE)
		victim.helpme = $ or 0
		if (victim.helpme==1)
			return true
		end
    end
end)

addHook("TouchSpecial", function(fist, victim)
    if fist.valid and victim.valid and (fist.type==MT_UTSLASH)
	and (victim.z + victim.height + victim.momz >= fist.z + fist.target.momz)
	and (victim.z + victim.height + victim.momz <= fist.z + fist.target.momz + fist.height)
		P_DamageMobj(victim, fist, fist.target)
		S_StartSound(victim, sfx_uhit)

		P_KillMobj(fist)
    end
end, MT_SANS)