/*## TODO:
	find way to fix poses for competition game modes	--## skipping for now
	fix electric shield interaction with stomp	--## works for air shield, test w/electric
	make hyper mode give pity shield in battlemod	--## done?
	whatever else needs to be done from the bugs/changes list
	counter implementation	--# consider using MF_NOTHINK to prevent things from moving in undesirable ways	--## cut
	
	consider performing sector search to prevent improper crushing damage.
*/

freeslot(
	"SPR2_CRCH",
	"S_PLAY_HONEYCROUCH",
	"SPR2_BKCK",
	"S_PLAY_HONEYBACKFLIP",
	"S_PLAY_FAKEROLL",
	"S_PLAY_HONEYDIVE",
	"S_PLAY_HONEYDIVEEND",
	"SPR2_DIVE",
	"SPR2_DVEN",
	"SPR2_HMEL",
	"SPR2_HJMP",
	"SPR2_VICT",
	"SPR2_VCTE",
	"S_PLAY_HONEYVICT1",
	"S_PLAY_HONEYVICT2",
	"S_PLAY_HONEYMELEE1",
	"S_PLAY_HONEYMELEE2",
	"SFX_HRFL",
	"SFX_HBOP",
	"SFX_HDIV",
	"SFX_HFLP",
	"SFX_HKIK",
	"SFX_HLND",
	"MT_KICKKILLER",
	"S_KICKKILLER",
	"MT_KICKKILLER2",
	"S_KICKKILLER2",
	"SPR_KCKL",
	"SPR_TCHE",
	"S_HTECH",
	"MT_HTECH",
	"SPR2_KCK1",
	"S_PLAY_HONEYKICKROLL",
	"S_PLAY_HONEYMIDJUMP",
	"S_HONEYKICK1",
	"S_PLAY_HONEYKICK1",
	"S_HONEYKICK2",
	"S_PLAY_HONEYKICK2",
	"SPR_HWI0",
	"SPR_HWI1",
	"SPR_HWI2",
	"SPR_HW20",
	"SPR_HW21",
	"S_HWING0",
	"S_HWING1",
	"S_HWING2",
	"S_HWING3",
	"S_HWING21",
	"S_HWING20",
	"MT_HWING",
	"MT_HWING2",
	
	"S_PLAY_FOOTSTOOL",		--# added footstool compatability
	"S_PLAY_FOOTSTOOL_WAIT",	--#
	"S_PLAY_HONEYLOSS",
	"MT_HONEY_SUPERUNDERLAY"
)

states[S_PLAY_HONEYCROUCH] = {
	sprite = SPR_PLAY,
	frame = SPR2_CRCH|FF_ANIMATE,
	tics = 80,
	var1 = 5,
	var2 = 5,
	nextstate = S_PLAY_HONEYCROUCH
}

states[S_PLAY_HONEYLOSS] = {
	sprite = SPR_PLAY,
	frame = SPR2_CNT1|FF_ANIMATE,
	tics = -1,
	var1 = 4,
	var2 = 4,
	nextstate = S_PLAY_HONEYLOSS
}

sfxinfo[sfx_hrfl].caption = "Resources Refilled"
sfxinfo[sfx_hdiv].caption = "Whoosh"
sfxinfo[sfx_hbop].caption = "Whoosh"
sfxinfo[sfx_hflp].caption = "Whoosh"
sfxinfo[sfx_hkik].caption = "Smash"

mobjinfo[MT_KICKKILLER2] = {
doomednum = -1,
spawnstate = S_KICKKILLER2,
spawnhealth = 1,
seestate = S_NULL,
reactiontime = 32,
attacksound = sfx_None,
painstate = S_NULL,
painchance = 200,
painsound = sfx_None,
meleestate = S_NULL,
missilestate = S_NULL,
deathsound = sfx_s3k52,
speed = 90*FRACUNIT,
radius = 100*FRACUNIT,
height = 10*FRACUNIT,
dispoffset = 0,
mass = DMG_SPIKE,
damage = 2,
flags = MF_MISSILE|MF_NOGRAVITY|MF_FLOAT,
raisestate = S_NULL}



mobjinfo[MT_KICKKILLER] = {
doomednum = -1,
spawnstate = S_KICKKILLER,
spawnhealth = 1,
seestate = S_NULL,
reactiontime = 32,
attacksound = sfx_None,
painstate = S_NULL,
painchance = 200,
painsound = sfx_None,
meleestate = S_NULL,
missilestate = S_NULL,
deathsound = sfx_s3k52,
speed = 80*FRACUNIT,
radius = 35*FRACUNIT,
height = 35*FRACUNIT,
dispoffset = 0,
mass = DMG_SPIKE,
damage = 2,
flags = MF_MISSILE|MF_NOGRAVITY|MF_FLOAT,
raisestate = S_NULL}

mobjinfo[MT_HTECH] = {
	doomednum = -1,
	spawnstate = S_HTECH,
	flags = MF_NOCLIP|MF_SCENERY|MF_NOGRAVITY|MF_NOCLIPHEIGHT,
	dispoffset = 3
}

states[S_HTECH] = {
	sprite = SPR_TCHE,
	frame = A|FF_ANIMATE,
	var1 = 10,
	var2 = 2
}

states[S_HWING0] = {
	sprite = SPR_HWI0,
	frame = TR_TRANS10|A
}

states[S_HWING1] = {
	sprite = SPR_HWI1,
	frame = TR_TRANS10|FF_ANIMATE|A,
	var1 = 1,
	var2 = 5
}

states[S_HWING2] = {
	sprite = SPR_HWI2,
	frame = TR_TRANS10|FF_ANIMATE|A,
	var1 = 1,
	var2 = 5
}

states[S_HWING3] = {
	sprite = SPR_HWI3,
	frame = TR_TRANS90|A
}

states[S_HWING21] = {
	sprite = SPR_HW21,
	frame = TR_TRANS50|A
}

states[S_HWING20] = {
	sprite = SPR_HW20,
	frame = TR_TRANS50|A
}

mobjinfo[MT_HWING] = {
	doomednum = -1,
	spawnstate = S_HWING2,
	flags = MF_NOCLIP|MF_SCENERY|MF_NOGRAVITY|MF_NOCLIPHEIGHT,
	dispoffset = 3
}

mobjinfo[MT_HWING2] = {
	doomednum = -1,
	spawnstate = S_HWING3,
	flags = MF_NOCLIP|MF_SCENERY|MF_NOGRAVITY|MF_NOCLIPHEIGHT,
	dispoffset = 3
}

local Battlemod
local Arena

local function BoolToInt(b)	//## remove if unnecessary
	local i
	if(b == true)
		i = 1
	else
		i = 0
	end
	return i
end

local function HoneyReset(p)
		p.honey.jumpdown = false
		p.honey.spindown = false
		p.honey.cjumpdown = false
		p.honey.cspindown = false
		p.honey.customdown = false
		p.honey.custom2down = false
		--backflipping = false
		p.honey.rolltimer = 0
		p.honey.gravityfuckery = false
		p.honey.rollground = false	-- for telling Honey to keep rolling when she hits the ground
		p.honey.cancurl = false
		p.honey.divecount = 0
		p.honey.divetickdown = 0
		p.honey.rollhelper = false
		p.honey.ihateairshit = 3
		p.honey.ihateairshitagain = 3
		p.honey.meleeangle = 0
		p.honey.meleetimer = 0
		p.honey.laststate = S_PLAY_WALK
		p.honey.colorizing = 0
		p.honey.stomptime = 0
		p.honey.kickangle = 0
		p.honey.resources = 2
		p.honey.resourcechargetimer = 0
		p.honey.haswingmeter = false
		p.honey.chargetimerlock = false
		p.honey.wdistimer = 0
		p.honey.meleedrawangle = 0
		p.honey.meleecontrollock = 0
		p.honey.sliding = false
		p.honey.hypermode = 0
		p.honey.hyperflash = 0
		p.honey.hyperstarangle = 0
		p.honey.hyperstars = {}
		p.honey.hyperghost = {}
		p.honey.pounceroll = false
		p.honey.nextstate = nil
		p.honey.posing = 0
		p.honey.isCarried = false
end
local ishoney = function(p) return p.mo and p.mo.valid and p.mo.skin == "honey" end

local carryWaypoints = {
	MT_PTERABYTEWAYPOINT,
	MT_TUBEWAYPOINT,
	MT_PLAYER
}
local IsCarried = function(p)
	if not p then return false end
	if p.powers and p.powers[pw_carry] then return true end
	if not (p.mo and p.mo.tracer) return false end
	for i,v in ipairs(carryWaypoints)
		if p.mo.tracer.type == v then return true end
	end
	return false
end

local Airdash = function(p)
	if p.honey.resources > 0 and not p.exiting and not p.honey.meleecontrollock and (p.guard == nil or p.guard == 0) and (p.airdodge == nil or p.airdodge == 0)	//## may include redundancies
				if p.mo.state != S_PLAY_HONEYDIVE and p.mo.state != S_PLAY_HONEYDIVEEND and p.mo.state != S_PLAY_HONEYMELEE1 and p.mo.state != S_PLAY_HONEYMELEE2 and p.powers[pw_carry] == CR_NONE and p.mo.state != S_PLAY_PAIN and not (p.powers[pw_carry])	--## check for redundancy
					p.mo.state = S_PLAY_HONEYDIVE
					p.drawangle = p.mo.angle
					if not (p.pflags & PF_JUMPED)
						p.pflags = $ | PF_JUMPED
					end
					--look i can't be bothered to explain all this bullshit ok, just trust me when i say its all needed
					p.honey.rollhelper = false
					p.ability2 = CA2_SPINDASH
					p.honey.rolltimer = 18
					p.honey.cancurl = true
					S_StartSound(p.mo, sfx_hdiv)
					--setting the dive counter & cooldown
						p.honey.resources = $ - 1
						p.honey.resourcechargetimer = 0
						p.honey.chargetimerlock = true
					if p.honey.divecount == 1
						p.honey.divetickdown = 35
					end
					if p.honey.divecount == 2
						p.honey.divetickdown = 75
					end
					--P_InstaThrust(p.mo, p.cmd.angleturn<<16 + R_PointToAngle2(0, 0, p.cmd.forwardmove*FRACUNIT, -p.cmd.sidemove*FRACUNIT), p.normalspeed)
					p.honey.ihateairshitagain = 1
						p.honey.chargetimerlock = true
					--if not P_IsObjectOnGround(p.mo)
					local superscaler = 1 + BoolToInt(p.powers[pw_super]>0)*2/3
						if not (p.mo.eflags & MFE_UNDERWATER)
							if p.mo.momz*P_MobjFlip(p.mo) <= 5*p.mo.scale*superscaler
								P_SetObjectMomZ(p.mo, 5*FRACUNIT*superscaler, false)
							end
						else
							if p.mo.momz*P_MobjFlip(p.mo) <= 3*p.mo.scale*superscaler
								P_SetObjectMomZ(p.mo, 3*FRACUNIT*superscaler, false)
							end
						end
					--end
				end
	end
end

local Stomp = function(p)
		if not p.honey.spindown and not ((p.cmd.buttons & BT_CUSTOM1) or (p.pflags & PF_THOKKED) or (P_IsObjectInGoop(p.mo)))
			if IsCarried(p) then
				return false
			end
			p.pflags = $ | PF_THOKKED
			p.honey.stomptime = 17
			p.mo.state = S_PLAY_FAKEROLL
			P_InstaThrust(p.mo, p.mo.angle, p.speed/3)
			S_StartSound(p.mo, sfx_appear)
			p.pflags = $ & ~ PF_JUMPED
		end
end

/**************************************
/* Victory/defeat pose upon level finish
/*
/* p must be a Honey player
/**************************************/
local Pose = function(p)
	if(p.exiting > 0)	// if Honey is exiting, but not yet posing, then make her pose
		
		if (	//## obsolete?	// check to see if Honey is already posing or not on the ground. if so, end function early
			p.mo.state != S_PLAY_HONEYVICT1
			and p.mo.state != S_PLAY_HONEYVICT2
			and p.mo.state != S_PLAY_HONEYLOSS
			and P_IsObjectOnGround(p.mo)
			)
		else return end
		
		if(p.honey.posing == 1)
			p.mo.state = S_PLAY_HONEYVICT1 return
		elseif(p.honey.posing == 2)
			p.mo.state = S_PLAY_HONEYVICT2 return
		end
		
		local winscore = CV_FindVar("pointlimit").value	// number of points required to win in static score based matches
		
		if ((not multiplayer) or (gametype == GT_COOP))	// single player and coop
			// simply sets Honey to perform her victory pose
		
			p.mo.state = S_PLAY_HONEYVICT1
			p.honey.posing = 1
			
		elseif(gametype == GT_RACE or gametype == GT_COMPETITION)	// race and competition
			//## Not yet implemented
			// Should set Honey to perform her victory pose when
			// she is the first one to enter the goal.
			
			// Otherwise, she is set to perform her loss animation
			// when exiting.
			
			// Possible future upgrade: Pre-calculate final scores in competition
			// and base victory/defeat animation based off of the result.
		
			
		elseif(p.ctfteam and (redscore or bluescore))	// team capture the flag
			//## Not yet implemented
			// Checks to see if the team that Honey is on has more points than the other team.
			// If so, Honey strikes her victory pose. If not, Honey performs her loss animation.
			
			
		elseif	// tag and Battlemod arena gametypes //## Not yet implemented
		(
			winscore ~= nil
			or G_TagGametype()
			or
			(
				Battlemod
				and
				(
					Battlemod.CPGametype()
					or Battlemod.DiamondGametype()
					or Battlemod.TagGametype()
				)
			)
		)
			// Checks to see if the p's score is the highest score. If so, victory animation! Otherwise, loss animation.
			
			local highscore = 0
			
			if (Arena == nil)	// For traditional tag	//## doesn't really do anything since tag ends before Honey can pose.
				
			else
				
			end
			
		elseif(Battlemod and Battlemod.BattleGametype())	// Any other Battlemod game mode.
			// checks to see if p has the most lives. If so... you get the picture.
			
		end
		
	elseif (not p.exiting) and (p.mo.state == S_PLAY_HONEYVICT1 or p.mo.state == S_PLAY_HONEYVICT2 or p.mo.state == S_PLAY_HONEYLOSS)	--# resets state for new level
		p.mo.state = S_PLAY_STND
		p.honey.posing = 0	//## I'm pretty sure this is redundant. Doesn't HoneyReset() get called when a new stage begins?
	end
end

local SpawnAfterimage = function(p)
					if(p.superobj != nil and p.superobj.valid)
						local gmobj = P_SpawnGhostMobj(p.superobj)
						gmobj.type = MT_HONEY_SUPERUNDERLAY
						gmobj.rollangle = p.mo.rollangle
					end
						
					local hairEx = P_SpawnGhostMobj(p.mo)
					hairEx.frame = $1 & ~FF_TRANSMASK
					hairEx.frame = $1|TR_TRANS90<<FF_TRANSSHIFT
					hairEx.color = p.mo.color
					hairEx.rollangle = p.mo.rollangle
				
					local AfterImageSpawn = P_SpawnGhostMobj(p.mo)
					AfterImageSpawn.frame = $ & ~FF_SPR2SUPER
					AfterImageSpawn.color = p.mo.color
					AfterImageSpawn.colorized = true
end

addHook("PlayerThink",  function(p)
	if not p.honey
		if p.mo and p.mo.valid and p.mo.skin == "honey"
			p.honey = {
				jumpdown = false,
				spindown = false,
				cjumpdown = false,
				cspindown = false,
				customdown = false,
				custom2down = false,
				--backflipping = false,
				rolltimer = 0,
				gravityfuckery = false,
				rollground = false,	-- for telling Honey to keep rolling when she hits the ground
				cancurl = false,
				divecount = 0,
				divetickdown = 0,
				rollhelper = false,
				ihateairshit = 3,
				ihateairshitagain = 3,
				meleeangle = 0,
				meleetimer = 0,
				laststate = S_PLAY_WALK,
				colorizing = 0,
				stomptime = 0,
				kickangle = 0,
				resources = 2,
				resourcechargetimer = 0,
				haswingmeter = false,
				chargetimerlock = false,
				wdistimer = 0,
				meleedrawangle = 0,
				meleecontrollock = 0,
				sliding = false,
				hypermode = 0,
				hyperflash = 0,
				hyperstarangle = 0,
				hyperstars = {},
				hyperghost = {},
				pounceroll = false,
				isCarried = false
			}
		else
			p.honey = nil
		end
	end
	if ishoney(p)
		if (p.guard ~= nil and p.guard ~= 0) return end
		if not p.mo.health
			HoneyReset(p)
			return
		end
		
		local isOnGround = P_IsObjectOnGround(p.mo)
		
		if p.powers[pw_carry] != CR_NONE	--/ thanks amperbee!
			p.pflags = $ | PF_THOKKED
			p.honey.isCarried = true
		elseif (p.honey.isCarried == true) and not p.honey.spindown
			p.honey.isCarried = false
			p.pflags = $ & ~PF_THOKKED
		end
			
		local thrustangle
		if (p.mo.flags2 & MF2_TWOD)	// Honey is in 2D mode
			thrustangle = p.cmd.angleturn<<16
		else	//Honey is not in 2D mode
			thrustangle = p.cmd.angleturn<<16 + R_PointToAngle2(0, 0, p.cmd.forwardmove*FRACUNIT, -p.cmd.sidemove*FRACUNIT)
		end
		if p.pflags&PF_ANALOGMODE then thrustangle = p.mo.angle end
		
		if p.mo.state == S_PLAY_STND
			p.mo.tics = -1
		end
		if(p.mo.state == S_PLAY_HONEYCROUCH)
			p.powers[pw_nocontrol] = 1
		end
		if(p.honey.nextstate != nil)	--# the only time this condition is true should be when the player initiated Honey's melee on the previous tic
			if(p.mo.state != S_PLAY_PAIN and p.powers[pw_carry] == CR_NONE)
				p.mo.state = p.honey.nextstate
			end
			p.honey.nextstate = nil
		end
		if(p.mo.state == S_PLAY_ROLL and (p.cmd.buttons & BT_CUSTOM1))
			p.pflags = $ | PF_JUMPSTASIS
		end
		if p.powers[pw_carry] == CR_NIGHTSMODE or p.iseggrobo or p.isjettysyn return end
		//Dive
		--Do a short dive when you press either c1 + spin or jump while airborne! 
		--if (p.pflags & PF_JUMPED) and not P_IsObjectOnGround(p.mo) --ground check probably not needed but eh
		if not (CBW_Battle and CBW_Battle.PreRoundWait(p))
			
			if p.cmd.buttons & BT_SPIN and not (p.cmd.buttons & BT_JUMP) and p.honey.cspindown == false
				if p.cmd.buttons & BT_CUSTOM1
					Airdash(p)
				end
			elseif(	--## could be optimized a little
				p.cmd.buttons & BT_JUMP
				and not (p.cmd.buttons & BT_SPIN)
				and p.honey.cjumpdown == false
				and p.honey.jumpdown == false
				and not (p.cmd.buttons & BT_CUSTOM1)
				and not P_IsObjectOnGround(p.mo)
				and not ((p.pflags & PF_STARTJUMP) and (p.mo.momz * P_MobjFlip(p.mo) > 0))
				and (p.pflags & PF_JUMPED)
			)	--# other conditions have been moved into the Airdash function itself
				Airdash(p)
			end
			
			----print(p.jumpfactor)
			--oh boy more air bullshit
			if p.honey.ihateairshitagain == 2
				p.honey.chargetimerlock = true
				if p.mo.state != S_PLAY_HONEYDIVE and p.mo.state != S_PLAY_HONEYDIVEEND
					p.mo.state = S_PLAY_HONEYDIVE
				end
				
				local speedshoescaler
				speedshoescaler = p.normalspeed*BoolToInt(p.powers[pw_sneakers]>0)*33/50
				local superscaler
				superscaler = p.normalspeed*BoolToInt(p.powers[pw_super]>0)*2/3
				
				if not (p.mo.eflags & MFE_UNDERWATER)
					P_InstaThrust(p.mo, thrustangle, FixedMul(p.normalspeed+speedshoescaler+superscaler, p.mo.scale))
				else
					P_InstaThrust(p.mo, thrustangle, FixedMul((p.normalspeed+speedshoescaler+superscaler)*3/4, p.mo.scale))
				end
			end
			if p.honey.ihateairshitagain <= 3
				p.honey.ihateairshitagain = $ + 1
			end
			--dive cooldown
			if p.honey.divetickdown > 0
				p.honey.divetickdown = $ - 1
			end
			if p.honey.divetickdown == 1
				p.honey.divecount = 0
				S_StartSound(p.mo, sfx_hrfl)
			end
			--if you press spin while diving, start rolling!
			if p.honey.cancurl == true and (not p.powers[pw_carry]) and not p.exiting -- and not (p.cmd.buttons & BT_CUSTOM1)
				if
				(
					p.honey.rollhelper == true
					and
					(
						(
							p.honey.jumpdown == false
							and p.cmd.buttons & BT_JUMP
						)
						or
						(
							p.honey.spindown == false
							and p.cmd.buttons & BT_SPIN
							and p.cmd.buttons & BT_CUSTOM1
						)
					)
					and not P_IsObjectOnGround(p.mo)
					and not (p.mo.eflags & MFE_JUSTHITFLOOR)
				)
					p.mo.state = S_PLAY_ROLL
					S_StartSound(p.mo, sfx_spin)
					--p.honey.rollground = true	--## Is this line redundant?
					p.honey.cancurl = false
					p.honey.pounceroll = true
				end
			end
			if P_IsObjectOnGround(p.mo) and p.honey.cancurl == true and p.mo.state != S_PLAY_HONEYDIVE or p.powers[pw_carry] or P_PlayerInPain(p)
				p.honey.cancurl = false
			end
			--end
			if p.mo.state == S_PLAY_ROLL and (not P_IsObjectOnGround(p.mo))/* and ((p.cmd.buttons & BT_SPIN and p.cmd.buttons & BT_CUSTOM1) or p.cmd.buttons & BT_JUMP)*/	--## doesn't interfere with normal activity. test with cez chains and zoomtubes
				p.honey.rollground = true
			else
				p.honey.rollground = false
			end
				--## Get rid of unecessary operations.
			/*
			if p.mo.state == S_PLAY_ROLL	--## doesn't interfere with normal activity. test with cez chains and zoomtubes
				p.honey.rollground = true
			else
				p.honey.rollground = false
			end
			*/
			if(P_IsObjectOnGround(p.mo))
				p.honey.pounceroll = false
			end
			if p.mo.state != S_PLAY_ROLL and p.mo.state != S_PLAY_HONEYMELEE1 and p.mo.state != S_PLAY_HONEYMELEE2 and p.pflags & PF_SPINNING
				p.pflags = $ & ~ PF_SPINNING
			end
			if p.mo.state == S_PLAY_HONEYDIVE or p.mo.state == S_PLAY_HONEYDIVEEND
				if p.honey.rolltimer > 0
					p.honey.rolltimer = $ - 1
				end
				if p.honey.rolltimer == 1 
						if P_IsObjectOnGround(p.mo) and p.mo.state != S_PLAY_ROLL	--## I don't think it's possible that she could be rolling here.
							if p.speed > p.runspeed
								p.mo.state = S_PLAY_RUN
							else
								p.mo.state = S_PLAY_WALK
							end
						else
							p.mo.state = S_PLAY_FALL
						end
					if p.mo.state != S_PLAY_ROLL	--## I want to take another look at this
						p.pflags = $ & ~ PF_SPINNING
					end
				end
				--p.pflags = $ + PF_SPINNING
			end
			if p.mo.state == S_PLAY_HONEYDIVE or p.mo.state == S_PLAY_HONEYDIVEEND or (p.mo.state == S_PLAY_ROLL and not p.powers[pw_carry])
				--p.ability2 = CA2_SPINDASH
				if leveltime % 2 == 0 
					SpawnAfterimage(p)
				end
			else
				--p.ability2 = CA2_NONE
			end
		end
		
		//Crouching & Backflipping
		--some battle pre-round checks
		if not (CBW_Battle and CBW_Battle.PreRoundWait(p))
			--If you're on the ground & pressing c1, you're crouching.
			if p.honey.sliding == false and p.exiting == 0 and p.powers[pw_carry] == CR_NONE and not p.honey.meleecontrollock
			if P_IsObjectOnGround(p.mo) and (p.cmd.buttons & BT_CUSTOM1) and p.mo.state != S_PLAY_HONEYDIVE and p.mo.state != S_PLAY_HONEYDIVEEND and p.mo.state != S_PLAY_HONEYBACKFLIP and p.mo.state != S_PLAY_ROLL and not p.honey.rollground --and p.honey.meleecontrollock == 0
			and p.mo.state != S_PLAY_SUPER_TRANS1 and p.mo.state != S_PLAY_SUPER_TRANS2 and p.mo.state != S_PLAY_SUPER_TRANS3 and p.mo.state != S_PLAY_SUPER_TRANS4 and p.mo.state != S_PLAY_SUPER_TRANS5 and p.mo.state != S_PLAY_SUPER_TRANS6 
				p.powers[pw_nocontrol] = 1
				p.powers[pw_noautobrake] = 1
				p.drawangle = p.mo.angle
				if p.mo.state != S_PLAY_ROLL and p.mo.state != S_PLAY_HONEYCROUCH and not p.honey.rollground
					p.mo.state = S_PLAY_HONEYCROUCH
				end
			else
				if p.mo.state == S_PLAY_HONEYCROUCH
						p.mo.state = S_PLAY_STND
				end
				--p.normalspeed = skins[p.mo.skin].normalspeed
			end
			--Set your height
			if P_MobjFlip(p.mo) == 1	--## looks like this may be unfinished
				if p.mo.state == S_PLAY_HONEYCROUCH or p.mo.state == S_PLAY_ROLL
					--p.height = 1*FRACUNIT
					p.height = skins[p.mo.skin].spinheight
					p.mo.height = P_GetPlayerSpinHeight(p)
				else
					--p.height = skins[p.mo.skin].height
					--#if p.mo.height != skins[p.mo.skin].height
					--# it's faster to just set it to the correct value regardless.
						p.height = skins[p.mo.skin].height
					--#end
				end
			end
			end
			--If you're crouching and press jump, do a backflip! --# also works while rolling
			--Just make sure you're not holding down jump first.
			if(p.mo.state == S_PLAY_HONEYCROUCH or (p.mo.state == S_PLAY_ROLL and P_IsObjectOnGround(p.mo) and p.powers[pw_carry] == CR_NONE)) and (p.cmd.buttons & BT_JUMP) and p.honey.jumpdown == false and not (p.cmd.buttons & BT_SPIN) and not p.honey.meleecontrollock
				if not (p.mo.eflags & MFE_UNDERWATER)
					P_SetObjectMomZ(p.mo, 21*FRACUNIT, false)
				else
					P_SetObjectMomZ(p.mo, 14*FRACUNIT, false)
				end
				p.mo.state = S_PLAY_HONEYBACKFLIP
				S_StartSound(p.mo, sfx_hflp)
				p.honey.ihateairshit = 1
				p.honey.gravityfuckery = true
				p.honey.resources = $ - 1
				p.honey.resourcechargetimer = 0
				p.honey.chargetimerlock = true
			end
			if p.mo.state == S_PLAY_HONEYBACKFLIP
				p.drawangle = p.mo.angle
				--if P_IsObjectOnGround(p.mo) or p.honey.ihateairshit < 3
				local kickkiller = P_SpawnMobjFromMobj(p.mo, P_RandomRange(-10, 10)*FRACUNIT, P_RandomRange(-10, 10)*FRACUNIT, P_RandomRange(-3, 0)*FRACUNIT, MT_KICKKILLER)  --P_SPMAngle(p.mo, MT_KICKKILLER, p.mo.angle)
				--if kickkiller
					if p.mo.eflags & MFE_UNDERWATER and CBW_Battle or (p.mo.eflags & MFE_TOUCHWATER and CBW_Battle)
						kickkiller.fuse = 5
					else
						kickkiller.fuse = 10
					end
					kickkiller.momx = p.mo.momx
					kickkiller.momy = p.mo.momy
					kickkiller.target = p.mo
					kickkiller.source = p.mo
					kickkiller.angle = p.mo.angle
					if gametyperules & GTR_TEAMS
						kickkiller.color = p.mo.color
					else
						kickkiller.color = SKINCOLOR_GREEN
					end
					if P_IsObjectOnGround(p.mo) or p.honey.ihateairshit <= 3
						P_InstaThrust(kickkiller, kickkiller.angle, p.speed/2)
					else
						P_Thrust(kickkiller, kickkiller.angle, p.speed+p.speed)
					end
				--end
				--end
			end
			--mess with gravity so that you rise faster
			if P_IsObjectOnGround(p.mo) and p.mo.state != S_PLAY_HONEYBACKFLIP or p.mo.momz*P_MobjFlip(p.mo) <= 1*p.mo.scale
				p.honey.gravityfuckery = false
			end
			----print(p.honey.gravityfuckery)
			if p.honey.gravityfuckery == true and not P_IsObjectOnGround(p.mo)
				p.mo.momz = $+(3*P_GetMobjGravity(p.mo)/4)
			end
			--srb2's friction is dumb so i gotta add this
			if p.honey.ihateairshit == 2
				P_InstaThrust(p.mo, p.mo.angle, p.speed/2-10*p.mo.scale)
			end
			if p.honey.ihateairshit <= 3
				p.honey.ihateairshit = $ + 1
			end
		end
		//////////////////////
		//Homing Attack thingy
        --if (p.cmd.buttons & BT_SPIN) and not (p.cmd.buttons & BT_CUSTOM1) and P_IsObjectOnGround(p.mo) and p.honey.spindown == false and p.honey.cspindown == false
		if not (CBW_Battle and CBW_Battle.PreRoundWait(p)) and (p.realtime > 0)
				/*##if p.mo
					p.meleetarget = P_LookForEnemies(p, true, false)
				end
		   --end
			
			if (p.meleetarget and p.meleetarget.valid)
				p.honey.meleeangle = R_PointToAngle2(p.mo.x, p.mo.y, p.meleetarget.x, p.meleetarget.y)
			end*/
			
			if(	--## I need to revisit this and see if there's a way to cut down on the number of checks getting made
				(p.cmd.buttons & BT_SPIN)
				and not (p.cmd.buttons & BT_CUSTOM1)
				and p.honey.spindown == false
				and p.honey.cspindown == false
				and P_IsObjectOnGround(p.mo)
				and 
				(
					p.panim == PA_WALK
					or p.panim == PA_IDLE
					or p.panim == PA_EDGE
					or p.mo.state == S_PLAY_RUN
					or p.mo.state == S_PLAY_HONEYCROUCH
				)
				and not p.exiting
			-- and p.honey.resources > 0 
			)
				if /*##(p.honey.resources > 0 and p.honey.meleecontrollock > 0) or*/ p.honey.meleecontrollock == 0
					S_StartSound(p.mo, sfx_hbop)/*##
					if p.honey.meleecontrollock > 0
						p.honey.resources = $ - 1
						p.honey.resourcechargetimer = 0
					end*/
					p.honey.meleeangle =  p.mo.angle
					
						if not (p.mo.eflags & MFE_UNDERWATER)
							P_InstaThrust(p.mo, p.mo.angle, 26*p.mo.scale)
							P_SetObjectMomZ(p.mo, 5*FRACUNIT, false)
						else
							P_InstaThrust(p.mo, p.mo.angle, 17*p.mo.scale)
							P_SetObjectMomZ(p.mo, 3*FRACUNIT, false)
						end
						if p.mo.state != S_PLAY_HONEYMELEE1 and p.mo.state != S_PLAY_HONEYMELEE2
							p.mo.state = S_PLAY_HONEYMELEE1
							p.honey.nextstate = S_PLAY_HONEYMELEE1
							p.honey.meleetimer = 23
						end
						//p.honey.meleeangle = p.mo.angle	--## I want to take another look at this.
						p.pflags = $ | PF_SPINNING
					
					p.honey.meleecontrollock = 15
				end
			end
			if P_IsObjectOnGround(p.mo) and p.honey.meleecontrollock > 0 
				p.honey.meleecontrollock = $ - 1
				p.powers[pw_noautobrake] = 1
				if p.honey.meleecontrollock <= 20 and p.mo.state != S_PLAY_HONEYMELEE1 and p.mo.state != S_PLAY_HONEYMELEE2 and p.mo.state != S_PLAY_HONEYBACKFLIP
					p.mo.state = S_PLAY_HONEYCROUCH
					p.drawangle = p.honey.meleeangle - ANGLE_180
					if p.honey.meleecontrollock <= 1
						p.drawangle = p.honey.meleeangle
					end
				end
			elseif(P_IsObjectOnGround(p.mo) and (p.mo.state == S_PLAY_HONEYMELEE1) and p.mo.state ~=S_PLAY_ROLL)
				p.mo.state = S_PLAY_HONEYCROUCH
				p.drawangle = p.honey.meleeangle - ANGLE_180
				if p.honey.meleecontrollock <= 1
					p.drawangle = p.honey.meleeangle
				end
			end
			/*
			if p.honey.meleecontrollock > 0
				p.honey.chargetimerlock = true
				p.honey.resourcechargetimer = 0
			end*/
			if (p.mo.state == S_PLAY_HONEYMELEE1 or p.mo.state == S_PLAY_HONEYMELEE2) and p.mo.state ~=S_PLAY_ROLL
				if p.honey.meleetimer <= 0 and P_IsObjectOnGround(p.mo)
					p.mo.state = S_PLAY_HONEYCROUCH
				end
				if p.mo.state == S_PLAY_HONEYMELEE2
					p.pflags = $ & ~ PF_SPINNING
				end
				p.drawangle = p.honey.meleeangle - ANGLE_180
			end
			if p.mo.state != S_PLAY_HONEYMELEE1 and p.mo.state != S_PLAY_HONEYMELEE2 and p.mo.state != S_PLAY_HONEYCROUCH
				p.honey.meleecontrollock = 0
			end
		end
		--power springing	--# stomp power springing is in the ThinkFrame hook.
		if p.honeystatelast
			if (p.mo.eflags & MFE_SPRUNG) and (p.honeystatelast == S_PLAY_HONEYMELEE1 or p.honeystatelast == S_PLAY_HONEYMELEE2 )
				p.mo.momz = $*5/4
				p.mo.momx = $*5/4
				p.mo.momy = $*5/4
				p.honey.stomptime = 0
				p.honey.juststomped = true
				S_StartSound(p.mo, sfx_sprong)
			end
		end
		// prevent power springing after a stomp that does not land on a spring
		if(p.honey.stomptime < 0)	--# this check is required or else setting stomptime to 0 will cancel stomps.	--## is this still used?
			 p.honey.stomptime = 0
		end
		p.honeystatelast = p.mo.state
		if p.honey.meleetimer > 0
			p.honey.meleetimer = $ - 1
		end
		--Ukemi/Tech. Allows you to jump out of your pain state.
		if P_PlayerInPain(p) and not p.exiting
			if not p.honey.customdown and p.cmd.buttons & BT_CUSTOM1
				if p.honey.resources == 2
					P_InstaThrust(p.mo, p.cmd.angleturn<<16 + R_PointToAngle2(0, 0, p.cmd.forwardmove*FRACUNIT, -p.cmd.sidemove*FRACUNIT), min(p.speed*3, 20*FRACUNIT))
					P_SetObjectMomZ(p.mo, 10*FRACUNIT, false)
					S_StartSound(p.mo, sfx_cdfm23)
					p.pflags = $ | PF_STARTJUMP
					p.mo.state = S_PLAY_FAKEROLL
					local ukemi = P_SpawnMobjFromMobj(p.mo, 0, 0, p.mo.scale * 20, MT_HTECH)
					ukemi.angle = p.mo.angle
					ukemi.fuse = 20
					ukemi.scale = p.mo.scale*4/3
					if gametyperules & GTR_TEAMS
						ukemi.color = p.mo.color
					else
						ukemi.color = SKINCOLOR_GREEN
					end
					--ukemi.color = p.mo.color
					ukemi.source = p.mfo
					p.powers[pw_flashing] = 0
					p.honey.colorizing = 20
					p.honey.resources = 0
					p.honey.resourcechargetimer = 0
					p.honey.chargetimerlock = true
				end
			end
		end
		if p.honey.colorizing > 1
			p.mo.colorized = true
			if p.honey.colorizing % 2 == 0
				if gametyperules & GTR_TEAMS
					p.mo.color = p.skincolor
				else
					p.mo.color = SKINCOLOR_EMERALD
				end
			else
				p.mo.color = SKINCOLOR_WHITE
			end
			p.powers[pw_nocontrol] = 1
		end
		if p.honey.colorizing == 1
			p.mo.colorized = false
			p.mo.color = p.skincolor
		end
		if p.honey.colorizing > 0
			p.honey.colorizing = $ - 1
		end
		--mid jump thingie
		if (p.mo.state == S_PLAY_JUMP or p.mo.state == S_PLAY_SPRING and not p.isxmomentum) and p.mo.momz*P_MobjFlip(p.mo) <= 1*p.mo.scale
			p.mo.state = S_PLAY_HONEYMIDJUMP
		end
		
		if
		(
			(p.mo.ceilingz - p.mo.floorz < P_GetPlayerHeight(p))
			and p.mo.state != S_PLAY_HONEYMELEE1 and p.mo.state != S_PLAY_HONEYMELEE2 and p.honey.meleecontrollock == 0
		)
			p.mo.state = S_PLAY_ROLL
			P_Thrust(p.mo, p.mo.angle, 5*p.mo.scale)
			p.mo.player.pflags = $|PF_SPINNING&~PF_JUMPED&~PF_THOKKED
		end
		
		--kick shit
		if p.mo.state == S_PLAY_HONEYKICK1 or p.mo.state == S_PLAY_HONEYKICK2 or p.mo.state == S_PLAY_HONEYKICKROLL
			p.drawangle = p.honey.kickangle
			if leveltime % 3 == 0
				SpawnAfterimage(p)
			end
		end
		--pose when you end a level
		Pose(p)
		--honey's attack resources. certain moves take resource to use.
		if p.honey.resources < 2 and p.honey.chargetimerlock == false
			p.honey.resourcechargetimer = $ + 1
			if p.honey.resourcechargetimer >= 35
				p.honey.resources = $ + 1
				p.honey.resourcechargetimer = 0
				if p.honey.resources == 2
					S_StartSound(p.mo, sfx_hrfl)
				end
			end
		end
		if p.honey.resources == 2
			if p.honey.wdistimer < 75
				p.honey.wdistimer = $ + 1
			end
		else
			p.honey.wdistimer = 0 
		end
		if not p.haswingmeter
			local wingmeter = P_SpawnMobjFromMobj(p.mo, 0, 0, 0, MT_HWING)
			wingmeter.source = p.mo
			wingmeter.target = p.mo
			local wingmeter2 = P_SpawnMobjFromMobj(p.mo, 0, 0, 0, MT_HWING2)
			wingmeter2.source = p.mo
			wingmeter2.target = p.mo
			p.haswingmeter = true
		end
		if P_IsObjectOnGround(p.mo) or (p.mo.eflags & MFE_JUSTHITFLOOR) or (p.powers[pw_carry] and p.powers[pw_carry] != CR_PTERABYTE)
			p.honey.chargetimerlock = false
		end
		if p.mo.state == S_PLAY_ROLL or P_PlayerInPain(p)
			p.honey.chargetimerlock = true
			p.honey.resourcechargetimer = 0 
		end
		//////////////////////////
		--Hyper-Mode
		if not (CBW_Battle and CBW_Battle.PreRoundWait(p))
			if (p.cmd.buttons & BT_CUSTOM1 and p.cmd.buttons & BT_JUMP and p.cmd.buttons & BT_SPIN) and p.honey.custom2down == false and p.honey.hypermode == 0 and P_IsObjectOnGround(p.mo) and not p.exiting
				if p.rings >= 20 and not (p.powers[pw_super]) and not (p.gotflag or p.gotcrystal)
					p.honey.hypermode = 12*TICRATE + 15
					if gametyperules & GTR_FRIENDLY or gametyperules & GTR_RACE
						p.rings = $ - 20
					else
						p.rings = 0 
					end
					p.mo.state = S_PLAY_SUPER_TRANS1//##
					p.mo.momx = 0
				end
			end
		end
		if p.gotflag or p.gotcrystal
			if p.honey.hypermode > 1
				p.honey.hypermode = 1
			end
			p.honey.resources = 0 
			p.honey.resourcechargetimer = 0
		end
		if p.honey.hypermode > 0
			p.honey.resources = 2
			if (p.powers[pw_shield] & SH_NOSTACK) == SH_ARMAGEDDON
				P_BlackOw(p)
			end
			p.powers[pw_shield] = 0 
			p.honey.resourcechargetimer = 0
			if p.honey.hypermode == 1
				P_RestoreMusic(p)
				p.normalspeed = skins[p.mo.skin].normalspeed
				p.mo.colorized = false
				p.mo.color = p.skincolor
				p.honey.hyperflash = 0
			else
				S_ChangeMusic("hprmd", false, p)
				p.normalspeed = 44*FRACUNIT
				p.honey.meleecontrollock = 0
			end
			--print(p.honey.hyperflash)
			----print(leveltime)
		end
		if P_PlayerInPain(p) and p.honey.hypermode > 0
			p.honey.hypermode = 0
			p.mo.colorized = false
			p.mo.color = p.skincolor
			P_RestoreMusic(p)
		end
        if p.mo.state == S_PLAY_SUPER_TRANS1 or p.mo.state == S_PLAY_SUPER_TRANS2 or p.mo.state == S_PLAY_SUPER_TRANS3 or p.mo.state == S_PLAY_SUPER_TRANS4 or p.mo.state == S_PLAY_SUPER_TRANS5 or p.mo.state == S_PLAY_SUPER_TRANS6
			p.mo.momx = 0
			p.mo.momy = 0
			p.mo.momz = 0
			p.mo.colorized = false
			p.mo.color = p.skincolor
		end
		if p.honey.hypermode > 0 
			p.honey.hypermode = $ - 1
		end
		if p.powers[pw_super]
			p.honey.meleecontrollock = 0
			p.honey.resources = 2
			p.honey.resourcechargetimer = 0
		end
		if not multiplayer
			if p.candy == 1 and not p.powers[pw_super]
				if p.skincolor == SKINCOLOR_RED
					p.skincolor = SKINCOLOR_SKY
					p.mo.color = SKINCOLOR_SKY
				end
			else
				if p.skincolor == SKINCOLOR_SKY
					p.skincolor = SKINCOLOR_RED
					p.mo.color = SKINCOLOR_RED
				end
			end
		end
		//# spawns afterimages for Honey's stomp
		if p.honey.stomptime > 33
			SpawnAfterimage(p)
		end
		--This detects if you was holding down jump or spin the previous frame. 
		--Would just use PF_JUMPDOWN/PF_SPINDOWN, but that doesn't work in playerthink hooks.
		if p.cmd.buttons & BT_JUMP
			p.honey.jumpdown = true
		else
			p.honey.jumpdown = false
		end
		if p.cmd.buttons & BT_SPIN
			p.honey.spindown = true
		else
			p.honey.spindown = false
		end
		if p.cmd.buttons & BT_JUMP and p.cmd.buttons & BT_CUSTOM1
			p.honey.cjumpdown = true
		else
			p.honey.cjumpdown = false
		end
		if p.cmd.buttons & BT_SPIN and p.cmd.buttons & BT_CUSTOM1
			p.honey.cspindown = true
		else
			p.honey.cspindown = false
		end
		if p.cmd.buttons & BT_CUSTOM1
			p.honey.customdown = true
		else
			p.honey.customdown = false
		end
		if p.cmd.buttons & BT_CUSTOM2
			p.honey.custom2down = true
		else
			p.honey.custom2down = false
		end
		if not (p.cmd.buttons & BT_SPIN)
			p.honey.rollhelper = true
		end
		p.honey.laststate = p.mo.state
		----print(p.honey.rollhelper)
	end
	----print(p.haswingmeter)
	if(p.mo and p.mo.valid and p.mo.state == S_PLAY_HONEYCROUCH)
		p.powers[pw_nocontrol] = 1
	end
end)
addHook("PlayerSpawn", function(p)
	if p.mo and p.mo.valid and p.mo.skin == "honey"	// initializes Honey's special graphics, resources, and timers.
		p.mo.state = S_PLAY_STND
		if p.honey
			p.haswingmeter = false
			p.honey.resources = 2
			p.honey.hypermode = 0 
			p.honey.stomptime = 0 
			p.honey.hyperstars = {}
			p.honey.hyperghost = {}
		end
	end
end)
addHook("PlayerCanDamage", function(p, badnik)
	if p.mo and p.mo.health and p.mo.valid and p.mo.skin == "honey"
		if p.mo.state == S_PLAY_ROLL or p.mo.state == S_PLAY_HONEYBACKFLIP or p.mo.state == S_PLAY_HONEYMELEE1 or p.mo.state == S_PLAY_HONEYMELEE2 or p.honey.stomptime > 0 
			return true
		end
		if p.mo.state == S_PLAY_HONEYDIVE or p.mo.state == S_PLAY_HONEYDIVEEND or p.mo.state == S_PLAY_ROLL
			p.honey.resources = min(2, $+1)
			return true
		end
	end
end)
addHook("TouchSpecial", function(ring, p)
	if p and p.player and p.health and p.skin == "honey" and p.player.honey.colorizing > 0
		if ring and ring.valid and ring.health
			return true
		end
	end
end, MT_FLINGRING)
addHook("TouchSpecial", function(ring, p)
	if p and p.player and p.health and p.skin == "honey" and p.player.honey.hypermode > 0
		if not (gametyperules & GTR_FRIENDLY) and not (gametyperules & GTR_RACE)
			if ring and ring.valid and ring.health
				return true
			end
		end
	end
end, MT_RING)
addHook("MobjMoveCollide", function(tmthing, thing)
	if tmthing.source == thing
		return false
	end
end, MT_KICKKILLER)
local spikekiller = function(thing, tmthing)
	if not (thing and thing.valid and tmthing and tmthing.valid) return end
	if tmthing.player and tmthing.skin == "honey" and ((tmthing.state == S_PLAY_HONEYMELEE1 or tmthing.state == S_PLAY_HONEYMELEE2))
	and (abs(thing.z - tmthing.z) <= 60*tmthing.scale)
		if tmthing.z >= thing.z-tmthing.height and tmthing.z <= thing.z+thing.height+tmthing.height
			P_KillMobj(thing, tmthing)
			return true
		end
	end
end
addHook("AbilitySpecial", function(p)
	if ishoney(p)
		return true
	end
end)
addHook("JumpSpinSpecial", function(p)
	if ishoney(p)
		if(p.powers[pw_carry] == CR_NONE)
			Stomp(p)
		else
			p.honey.isCarried = true
			p.pflags = $ | PF_THOKKED
		end
		return true
	end
end)
addHook("MobjDamage", function(trg, inf, src, dmg, dType)
	if trg and trg.valid and (trg.flags & (MF_ENEMY|MF_BOSS|MF_MONITOR) or trg.player)
	and src and src.valid and src.player and src.health and src.skin == "honey"
	and src.player.pflags & PF_JUMPED
	and not
		(src.state == S_PLAY_ROLL
		or src.state == S_PLAY_FOOTSTOOL
		or src.state == S_PLAY_FOOTSTOOL_WAIT
		or src.state == S_PLAY_HONEYDIVE
		or src.state == S_PLAY_HONEYDIVEEND)
	and inf == src and dType == 0
		----print("test")
		src.player.honey.kickangle = R_PointToAngle2(src.x, src.y, trg.x, trg.y)
		src.state = S_PLAY_HONEYKICKROLL
		S_StartSound(src, sfx_hkik)
		src.player.honey.resources = min(2, $+1)
	end
	if trg and trg.valid and trg.player and trg.health and trg.skin == "honey"
	and trg.player.honey.colorizing > 0
		return true
	end
end)
addHook("MobjDeath", function(trg, inf, src)
	if trg and trg.valid and trg.flags & (MF_MONITOR)
	and src and src.valid and src.player and src.health and src.skin == "honey" and src.player.pflags & PF_JUMPED and not (src.state == S_PLAY_ROLL or src.state == S_PLAY_FOOTSTOOL or src.state == S_PLAY_FOOTSTOOL_WAIT)	--# added footstool compatability
	and inf == src
		----print("test")
		src.player.honey.kickangle = R_PointToAngle2(src.x, src.y, trg.x, trg.y)
		src.state = S_PLAY_HONEYKICKROLL
		S_StartSound(src, sfx_hkik)
		src.player.honey.resources = min(2, $+1)
	end
end)
addHook("MobjThinker", function(mo)
	if not mo.source or not mo.source.valid or not mo.source.player
		P_RemoveMobj(mo)
		return
	end
    if not mo.valid return end
	if not mo.health return end
	if not mo.source return end
	if not mo.source.valid return end
	if not mo.source.player return end
		mo.lifetime = $ or 0
		--mo.x = mo.source.x + mo.lifetime * mo.momx
		--mo.y = mo.source.y + mo.lifetime * mo.momy
		--mo.z = mo.source.z + mo.lifetime * mo.momz
		--if mo.source.state != S_PLAY_JUMP and mo.source.state != S_PLAY_ROLL
		if mo.valid and mo.source.valid and mo.source.health and mo.health and mo.source.player
			if mo.source.player.honey.resources == 2 and mo.state != S_HWING2
				mo.state = S_HWING2
			end
			if mo.source.player.honey.resources == 1 and mo.state != S_HWING1
				mo.state = S_HWING1
			end
			if mo.source.player.honey.resources == 0 and mo.state != S_HWING0
				mo.state = S_HWING0
			end
			if mo.source.player.honey.wdistimer == 75
				mo.state = S_HWING3
			end
		end
		if mo.source.valid and mo.source.player
			mo.scale = mo.source.scale + mo.source.scale/3
			--P_TeleportMove(mo, mo.source.x, mo.source.y, mo.source.z)
		end
		--end
		mo.lifetime = $ + 1
		if mo.source.skin != "honey" or not mo.source.health
			mo.source.player.haswingmeter = false
			P_KillMobj(mo)
		end
		if mo.valid and mo.source.valid
			A_CapeChase(mo, 0, 0)
		end
	--end
end, MT_HWING)

addHook("MobjThinker", function(mo)
	if not mo.source or not mo.source.valid or not mo.source.player
		P_RemoveMobj(mo)
		return
	end
	if not mo.valid return end
	if not mo.health return end
    if not mo.source return end
	if not mo.source.valid return end
	if not mo.source.player return end
		mo.lifetime = $ or 0
		--mo.x = mo.source.x + mo.lifetime * mo.momx
		--mo.y = mo.source.y + mo.lifetime * mo.momy
		--mo.z = mo.source.z + mo.lifetime * mo.momz
		--if mo.source.state != S_PLAY_JUMP and mo.source.state != S_PLAY_ROLL
		if mo.valid and mo.source.valid and mo.source.health and mo.health and mo.source.player
			if mo.source.player.honey.resources == 2 and mo.state != S_HWING3
				mo.state = S_HWING3
			end
			if mo.source.player.honey.resources == 1 and mo.state != S_HWING21
				mo.state = S_HWING21
			end
			if mo.source.player.honey.resources == 0 and mo.state != S_HWING20
				mo.state = S_HWING20
			end
			if mo.source.player.honey.wdistimer == 75
				mo.state = S_HWING3
			end
		end
		if mo.source.valid and mo.source.player
			mo.scale = mo.source.scale + mo.source.scale/3
			--P_TeleportMove(mo, mo.source.x, mo.source.y, mo.source.z)
		end
		--end
		mo.lifetime = $ + 1
		if mo.source.skin != "honey" or not mo.source.health
			mo.source.player.haswingmeter = false
			P_KillMobj(mo)
		end
		if mo.valid and mo.source.valid
			A_CapeChase(mo, 0, 0)
		end
	--end
end, MT_HWING2)
addHook("MobjCollide", function(thing, tmthing) spikekiller(thing, tmthing) end, MT_SPIKE)
addHook("MobjCollide", function(thing, tmthing) spikekiller(thing, tmthing) end, MT_WALLSPIKE)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player
		if inflictor == nil and source == nil and target.skin == "honey"
		and damagetype == DMG_CRUSHED and P_IsObjectOnGround(target)
		and not (target.ceilingz - target.floorz < P_GetPlayerSpinHeight(target.player))
		and target.state != S_PLAY_ROLL and target.state != S_PLAY_HONEYMELEE1 and target.state != S_PLAY_HONEYMELEE2 and target.player.honey.meleecontrollock == 0
			target.state = S_PLAY_ROLL
			P_Thrust(target, target.angle, 5*target.scale)
			target.player.pflags = $|PF_SPINNING&~PF_JUMPED&~PF_THOKKED
			return false
		end
	end
end, MT_PLAYER)
addHook("ShieldSpecial", function(p)
	if p.mo and p.mo.health and p.mo.valid and p.mo.skin == "honey"
		if (p.pflags & PF_JUMPED)
			if(p.powers[pw_carry] == CR_NONE)
				Stomp(p)
			else
				p.honey.isCarried = true
				p.pflags = $ | PF_THOKKED
			end
		else
			Airdash(p)
		end
		return true
	end
end)
addHook("JumpSpecial", function(p)
	if p.mo and p.mo.health and p.mo.valid and p.mo.skin == "honey"
		if p.powers[pw_carry] ~= CR_NONE
			p.honey.jumpdown = true
		end
	end
end)
addHook("ThinkFrame", do
	if (CBW_BATTLE and not Battlemod)
		Battlemod = CBW_BATTLE
		Arena = Battlemod.arena
	end
	for p in players.iterate
		if p.honey and not p.playerstate
			if p.honey.rollground == true
				if P_IsObjectOnGround(p.mo) 
					p.mo.state = S_PLAY_ROLL
					p.pflags = $ | PF_SPINNING
					p.honey.rollground = false
				end
			end
			if not (p.pflags & PF_SLIDING)
				p.honey.sliding = false
			else
				p.honey.sliding = true
			end
			
			--stomp 'n stuff
			if p.powers[pw_carry]
				return
			end
			p.honey.juststomped = false
			if p.honey.stomptime > 0
				if not(p.powers[pw_justsprung])
					if (P_IsObjectOnGround(p.mo) or (p.mo.state != S_PLAY_FALL and p.mo.state != S_PLAY_FAKEROLL))	
						if P_IsObjectOnGround(p.mo) and p.honey.stomptime > 33
							if (p == displayplayer)
								P_StartQuake(20*FRACUNIT, 8)
								S_StartSound(p.mo, sfx_hlnd)
							end
						end
						p.honey.stomptime = 0
						p.honey.juststomped = true
					else
						p.honey.stomptime = $ + 1
					end
					if p.honey.stomptime == 26 and p.mo.state == S_PLAY_FAKEROLL
						p.mo.state = S_PLAY_FALL
						S_StartSound(p.mo, sfx_zoom)
					end
					if(p.mo.state != S_PLAY_PAIN)
						if p.honey.stomptime <= 26
							p.mo.momz = 0 
						else
							P_SetObjectMomZ(p.mo, -p.honey.stomptime*FRACUNIT, false)
						end
					end
					if P_IsObjectInGoop(p.mo)
						p.honey.stomptime = 0 
					end
				else
					p.mo.momz = $*5/4
					p.mo.momx = $*5/4
					p.mo.momy = $*5/4
					p.honey.stomptime = 0
					p.honey.juststomped = true
					S_StartSound(p.mo, sfx_sprong)
				end
			end
		end
	end
end)


// Honey's attack becomes a roll when hitting horizontal springs.
local function Springroll(mo, thing)
	if(mo.skin == "honey")
		if(mo.player and mo.player.honey and (mo.state == S_PLAY_HONEYMELEE1 or mo.state == S_PLAY_HONEYMELEE2))
		
			local tp = thing.type
			if not(tp == MT_YELLOWHORIZ or tp == MT_REDHORIZ or tp == MT_BLUEHORIZ)
				return
			end
			
			//# that Honey is considered to be colliding with springs 
			// when she is in the air above them regardless of whether she is
			// actually touching them or not.
			local flags = thing.flags
			local mz = mo.z
			local tz = thing.z
			local mh = mo.height//##FixedMul(mo.height, mo.scale)
			local th = thing.height//##FixedMul(thing.height, mo.scale)
			local mFlip = P_MobjFlip(mo)
			local tFlip = P_MobjFlip(thing)
			local mt = mh * mFlip
			local tt = th * tFlip
			
		
			if
			(		//# Spring collision is weird. This function gets called
					//	whenever she passes above them as well, so we have to
					//	check for collision again ourselves.
					(
						mz >= tz
						and mz <= tz + tt
					)
					or
					(
						mz <= tz
						and mz >= tz + tt
					)
					or
					(
						mz + mt >= tz + tt
						and mz + mt <= tz
					)
					or
					(
						mz + mt <= (tz + tt)
						and mz + mt >= tz
					)
			)
		
				mo.state = S_PLAY_ROLL
				mo.player.pflags = $ | PF_SPINNING
				mo.player.honey.rollground = true
				mo.player.honey.meleecontrollock = 0
				mo.player.honey.meleetimer = 0
				mo.player.honey.nextstate = nil
				mo.player.honeystatelast = nil
			end
		end
	end
end
addHook("MobjMoveCollide",Springroll,MT_PLAYER)

local honeybattle = function(target, inflictor, source, damage, damagetype)
	--Easier to parry moon kick TY lumyni
	if not (CBW_Battle
	and inflictor
	and inflictor.valid
	and inflictor.type == MT_KICKKILLER
	and inflictor.target)
		return
	end

	--parrying the mace triggers parry for its player
	CBW_Battle.GuardFunc.Parry(target, inflictor.target, source, damage, damagetype)
end
addHook("ShouldDamage",honeybattle,MT_PLAYER)