//Wechidna's moveset

addHook("PlayerSpawn", function(player)
	if player.mo and player.mo.valid
		player.wechidnacrashhold = false
		player.wechidnacrashfreeze = false
		player.wechidnacrashtime = 30*TICRATE
		player.wechidnacrashthegame = false
		player.wechidnacrashedme = false
		player.wechidnacursorx = 180
		player.wechidnacursory = 100
		player.wechidnacursorclick = false
		player.wechidnacursorclosehold = true
		player.wechidnacursordirection = 1
		player.wechidnacursordirectionhold = true
	end
end)

addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid
		if not (mapheaderinfo[gamemap].typeoflevel & TOL_NIGHTS)
		and not (mapheaderinfo[gamemap].typeoflevel & TOL_XMAS)
		and player.powers[pw_carry] != CR_NIGHTSMODE
		and player.powers[pw_carry] != CR_NIGHTSFALL
			if player.wechidna == true
			and player.iswechidna == true
			and player.playerstate == PST_LIVE
				if player.mo.eflags & MFE_UNDERWATER
					if not P_IsObjectOnGround(player.mo)
						player.pflags = $|PF_CANCARRY
						player.charability = CA_NONE
						if player.cmd.buttons & BT_JUMP
							if player.wechidnajumphold != true
								player.pflags = $ & ~PF_JUMPED
								player.mo.momz = 5*FRACUNIT*P_MobjFlip(player.mo)
								player.mo.state = S_PLAY_SPRING
								S_StartSound(player.mo, sfx_bubbl4, player)
								player.wechidnajumphold = true
							end
						else
							player.wechidnajumphold = false
						end
					else
						player.pflags = $ & ~PF_CANCARRY
					end
				else
					player.pflags = $ & ~PF_CANCARRY
					player.charability = CA_FLY
				end
				if player.powers[pw_tailsfly]
					player.pflags = $|PF_CANCARRY
				elseif player.mo.state == S_PLAY_FLY_TIRED
					player.pflags = $ & ~PF_CANCARRY
					player.mo.state = S_PLAY_SPRING
				end
			end
			if player.wechidnacrashhold == nil
			or (player.wechidnacrashhold == true and not (player.cmd.buttons & BT_CUSTOM1))
				player.wechidnacrashhold = false
			end
			if player.wechidnacrashfreeze == nil
				player.wechidnacrashfreeze = false
			end
			if player.wechidnacrashtime == nil
			or player.wechidnacrashtime > 30*TICRATE
				player.wechidnacrashtime = 30*TICRATE
			end
			if player.wechidnacrashtime > 0
				if player.wechidnacrashfreeze == true
					player.wechidnacrashtime = $-1
				elseif player.wechidnacrashthegame != true
					player.wechidnacrashtime = $+3
				end
			else
				player.wechidnacrashfreeze = false
				player.wechidnacrashthegame = true
			end
			if player.wechidnacrashthegame == nil
				player.wechidnacrashthegame = false
			elseif player.wechidnacrashthegame == true
				if player.wechidnareallycrashes == true
					COM_BufInsertText(player, "quit")
				end
				player.wechidnacrashfreeze = false
				player.wechidnacrashtime = $+1
				if (player.lasteststate != player.mo.state or player.lastestangle != player.drawangle or player.lastestframe != player.mo.frame or player.lastestmomx != player.mo.momx or player.lastestmomy != player.mo.momy or player.lastestmomz != player.mo.momz or player.lastestx != player.mo.x or player.lastesty != player.mo.y or player.lastestz != player.mo.z)
				and player.wechidnacrashedme != true
					player.lasteststate = player.mo.state
					player.lastestangle = player.drawangle
					player.lastestframe = player.mo.frame
					player.lastestmomx = player.mo.momx
					player.lastestmomy = player.mo.momy
					player.lastestmomz = player.mo.momz
					player.lastestx = player.mo.x
					player.lastesty = player.mo.y
					player.lastestz = player.mo.z
				end
				player.mo.state = player.lasteststate
				player.drawangle = player.lastestangle
				player.mo.frame = player.lastestframe
				player.mo.momx = 0
				player.mo.momy = 0
				player.mo.momz = 0
				P_TeleportMove(player.mo, player.lastestx, player.lastesty, player.lastestz)
				player.powers[pw_tailsfly] = $+1
				player.powers[pw_nocontrol] = 2
				player.wechidnacrashedme = true
				if player.wechidnacrashtime >= 10*TICRATE
					player.mo.state = player.lasteststate
					player.drawangle = player.lastestangle
					player.mo.frame = player.lastestframe
					player.mo.momx = player.lastestmomx
					player.mo.momy = player.lastestmomy
					player.mo.momz = player.lastestmomz
					player.wechidnacrashedme = false
					player.wechidnacrashtime = 30*TICRATE
					player.wechidnacrashthegame = false
				end
			end
			if player.wechidnaonpc == nil
				player.wechidnaonpc = true
			end
			if player.wechidnacursordirectionbutton == nil
				player.wechidnacursordirectionbutton = BT_CUSTOM2
			end
			if player.wechidnacursormovebutton == nil
				player.wechidnacursormovebutton = BT_FIRENORMAL
			end
			if player.wechidnacursorclickbutton == nil
				player.wechidnacursorclickbutton = BT_ATTACK
			end
			if player.wechidna == true
			and player.iswechidna == true
			and player.wechidnacrashhold != true
			and player.wechidnacrashthegame != true
			and player.cmd.buttons & BT_CUSTOM1
				if player.wechidnacrashfreeze != true
					S_StartSound(player.mo, sfx_cdfm39, player)
					player.wechidnacrashfreeze = true
				else
					S_StartSound(player.mo, sfx_cdfm46, player)
					player.wechidnacrashfreeze = false
				end
				player.wechidnacrashhold = true
			end
			if player.wechidna == true
				if player.wechidnaonpc == true
					if mouse.rdx > 0
						if player.wechidnacursorx < 361
							player.wechidnacursorx = $+min(mouse.rdx, 30)
						else
							player.wechidnacursorx = 360
						end
					end
					if mouse.rdx <= -1
						if player.wechidnacursorx > -1
							player.wechidnacursorx = $+max(mouse.rdx, -30)
						else
							player.wechidnacursorx = 0
						end
					end
					if mouse.rdy > 0
						if player.wechidnacursory < 201
							player.wechidnacursory = $+max(mouse.rdy, -30)
						else
							player.wechidnacursory = 200
						end
					end
					if mouse.rdy <= -1
						if player.wechidnacursory > -1
							player.wechidnacursory = $+min(mouse.rdy, 30)
						else
							player.wechidnacursory = 0
						end
					end
				else
					if player.cmd.buttons & player.wechidnacursordirectionbutton
						if player.wechidnacursordirectionhold != true
							if player.wechidnacursordirection == 1
								player.wechidnacursordirection = 2
							elseif player.wechidnacursordirection == 2
								player.wechidnacursordirection = 3
							elseif player.wechidnacursordirection == 3
								player.wechidnacursordirection = 4
							elseif player.wechidnacursordirection == 4
								player.wechidnacursordirection = 5
							elseif player.wechidnacursordirection == 5
								player.wechidnacursordirection = 6
							elseif player.wechidnacursordirection == 6
								player.wechidnacursordirection = 7
							elseif player.wechidnacursordirection == 7
								player.wechidnacursordirection = 8
							else
								player.wechidnacursordirection = 1
							end
							player.wechidnacursordirectionhold = true
						end
					else
						player.wechidnacursordirectionhold = false
					end
					if player.cmd.buttons & player.wechidnacursormovebutton
						if player.wechidnacursordirection == 1
							if player.wechidnacursory < 201
								player.wechidnacursory = $+5
							else
								player.wechidnacursory = 200
							end
						elseif player.wechidnacursordirection == 2
							if player.wechidnacursory < 201
								player.wechidnacursory = $+5
							else
								player.wechidnacursory = 200
							end
							if player.wechidnacursorx > -1
								player.wechidnacursorx = $-5
							else
								player.wechidnacursorx = 0
							end
						elseif player.wechidnacursordirection == 3
							if player.wechidnacursorx > -1
								player.wechidnacursorx = $-5
							else
								player.wechidnacursorx = 0
							end
						elseif player.wechidnacursordirection == 4
							if player.wechidnacursorx > -1
								player.wechidnacursorx = $-5
							else
								player.wechidnacursorx = 0
							end
							if player.wechidnacursory > -1
								player.wechidnacursory = $-5
							else
								player.wechidnacursory = 0
							end
						elseif player.wechidnacursordirection == 5
							if player.wechidnacursory > -1
								player.wechidnacursory = $-5
							else
								player.wechidnacursory = 0
							end
						elseif player.wechidnacursordirection == 6
							if player.wechidnacursory > -1
								player.wechidnacursory = $-5
							else
								player.wechidnacursory = 0
							end
							if player.wechidnacursorx < 361
								player.wechidnacursorx = $+5
							else
								player.wechidnacursorx = 360
							end
						elseif player.wechidnacursordirection == 7
							if player.wechidnacursorx < 361
								player.wechidnacursorx = $+5
							else
								player.wechidnacursorx = 360
							end
						elseif player.wechidnacursordirection == 8
							if player.wechidnacursorx < 361
								player.wechidnacursorx = $+5
							else
								player.wechidnacursorx = 360
							end
							if player.wechidnacursory < 201
								player.wechidnacursory = $+5
							else
								player.wechidnacursory = 200
							end
						end
					end
				end
				if player.cmd.buttons & player.wechidnacursorclickbutton
					if player.wechidnacursorclick != true
						S_StartSound(player.mo, sfx_s257, player)
					end
					player.wechidnacursorclick = true
				else
					player.wechidnacursorclick = false
					player.wechidnacursorclosehold = false
				end
			end
		end
	end
end)

addHook("MobjThinker", function(mobj)
	if not (mobj and mobj.valid) then return end
	if not (mobjinfo[mobj.type].flags & MF_ENEMY) then return end
	if mobj.player then return end

	for player in players.iterate
		if player.wechidna == true
		and player.iswechidna == true
		and player.wechidnacrashfreeze == true
			if (mobj.lasteststate != mobj.state or mobj.lastestangle != mobj.angle or mobj.lastestframe != mobj.frame or mobj.lastestmomx != mobj.momx or mobj.lastestmomy != mobj.momy or mobj.lastestmomz != mobj.momz or mobj.lastestx != mobj.x or mobj.lastesty != mobj.y or mobj.lastestz != mobj.z or mobj.lastestflags != mobj.flags)
			and mobj.wechidnacrashedme != true
				mobj.lasteststate = mobj.state
				mobj.lastestangle = mobj.angle
				mobj.lastestframe = mobj.frame
				mobj.lastestmomx = mobj.momx
				mobj.lastestmomy = mobj.momy
				mobj.lastestmomz = mobj.momz
				mobj.lastestx = mobj.x
				mobj.lastesty = mobj.y
				mobj.lastestz = mobj.z
				mobj.lastestflags = mobj.flags
			end
			mobj.state = mobj.lasteststate
			mobj.angle = mobj.lastestangle
			mobj.frame = mobj.lastestframe
			mobj.momx = 0
			mobj.momy = 0
			mobj.momz = 0
			P_TeleportMove(mobj, mobj.lastestx, mobj.lastesty, mobj.lastestz)
			mobj.flags = $|MF_SOLID & ~MF_ENEMY & ~MF_SPECIAL
			mobj.wechidnacrashedme = true
		elseif mobj.wechidnacrashedme == true
			mobj.state = mobj.lasteststate
			mobj.angle = mobj.lastestangle
			mobj.frame = mobj.lastestframe
			mobj.momx = mobj.lastestmomx
			mobj.momy = mobj.lastestmomy
			mobj.momz = mobj.lastestmomz
			mobj.flags = mobj.lastestflags
			mobj.wechidnacrashedme = false
		end
	end
end)

addHook("MobjMoveBlocked", function(mo, mobj, line)
	if mo and mo.valid
		mo.player.charability = CA_NONE
		if not P_IsObjectOnGround(mo)
		and mo.player.wechidna == true
		and mo.player.iswechidna == true
		and mo.player.cmd.buttons & BT_JUMP
			mo.player.pflags = $|PF_JUMPED
			S_StartSound(mo, sfx_spndsh, mo.player)
			mo.momz = 11*FRACUNIT*P_MobjFlip(mo)
			P_InstaThrust(mo, mo.player.drawangle+ANGLE_180, mo.player.speed+7*FRACUNIT)
			mo.state = S_PLAY_SPINDASH
		end
	end
end, MT_PLAYER)