//For Kirby!
addHook("MapLoad", do
	for mobj in mobjs.iterate() do
		if mobj.info.deathstate == S_TAZW then
			mobj.abilitytogive = -1000
			mobj.flags = $|MF_NOTHINK
		end
	end
end)

//Collisions
addHook("MobjMoveCollide", function(mobj, thing)
	if thing and thing.valid
	and thing.z - thing.scale <= mobj.z + mobj.height
	and thing.z + thing.height + thing.scale >= mobj.z then
//Thanks a lot Alfredo!
		if thing.flags & MF_SCENERY then
			if thing.info.deathstate == S_TAZW
			or thing.info.deathstate == S_TAZW2
			or thing.info.deathstate == S_TAZW3 then
				if P_PlayerCanDamage(mobj.player, thing)
				or mobj.player.panim == PA_ABILITY
				or mobj.player.panim == PA_ABILITY2
				or (mobj.player.charflags & SF_CANBUSTWALLS
				and (thing.info.deathstate == S_TAZW or thing.info.deathstate == S_TAZW3))
				or (mobj.player.speed >= 27*FRACUNIT
				and (thing.info.deathstate == S_TAZW2 or thing.info.deathstate == S_TAZW3)) then
//Eat
					if mobj.state == S_TAZBITEG
					or mobj.player.tazting == true then //Variable
						if mobj.skin == "taz" then
							S_StopSoundByID(mobj, sfx_tasty)
							S_StopSoundByID(mobj, sfx_taste)
							S_StopSoundByID(mobj, sfx_tast2)
							S_StartSound(mobj, sfx_taste)
						end
						if thing.info.deathstate == S_TAZW 
						or thing.info.deathstate == S_TAZW3 then
							if mobj.player.hungry == true then
								mobj.player.hungry = 4
								mobj.player.food = thing.info.painchance
								if thing.type == MT_BUSH
								or thing.type == MT_XMASBUSH
								or thing.type == MT_MARIOBUSH1
								or thing.type == MT_MARIOBUSH2
								or thing.type == MT_SPRINGTREE
								or thing.type == MT_BUSHTREE
								or thing.type == MT_BUSHREDTREE
								or thing.info.painchance == MT_TWDB then
									mobj.player.hungry = 5
								elseif thing.type == MT_FHZTREE
								or thing.type == MT_FHZPINKTREE then
									mobj.player.hungry = 6
								elseif thing.type == MT_CHECKERTREE
								or thing.type == MT_CHECKERSUNSETTREE
								or thing.type == MT_POLYGONTREE then
									mobj.player.hungry = 7
								elseif thing.type == MT_BERRYBUSH
								or thing.type == MT_XMASBERRYBUSH then
									mobj.player.hungry = 9
									mobj.player.food = MT_TWBERRY
								elseif thing.type == MT_BLUEBERRYBUSH
								or thing.type == MT_XMASBLUEBERRYBUSH then
									mobj.player.hungry = 10
									mobj.player.food = MT_TWBERRY
								elseif thing.info.painchance == MT_TIDB then
									mobj.player.hungry = 11
									mobj.player.food = MT_TIDB
								elseif thing.info.painchance == MT_CRYSTALCRUMBLE then
									mobj.player.hungry = 12
									mobj.player.food = MT_CRYSTALCRUMBLE
								elseif thing.info.painchance == MT_TWLEAF
								or thing.info.painchance == MT_TWLEAFB
								or thing.info.painchance == MT_TWLEAFS
								or thing.info.painchance == MT_TWLEAFD
								or thing.info.painchance == MT_TWLEAFL
								or thing.info.painchance == MT_TWLEAFW
								or thing.info.painchance == MT_TWLEAFP
								or thing.info.painchance == MT_TWLEAFR 
								or thing.info.painchance == MT_TMLEAF 
								or thing.info.painchance == MT_TMLEAFD 
								or thing.info.painchance == MT_EXPLODE then
									mobj.player.hungry = true
								end
							end
						end
					else
						local tzw4 = P_SpawnMobj(thing.x, thing.y, thing.z+10*FRACUNIT, thing.info.painchance)
						tzw4.momx = P_RandomRange(-3, 3)*FRACUNIT
						tzw4.momy = P_RandomRange(-3, 3)*FRACUNIT
						tzw4.momz = P_RandomRange(4, 7)*FRACUNIT
						local tzw5 = P_SpawnMobj(thing.x, thing.y, thing.z+10*FRACUNIT, thing.info.painchance)
						tzw5.momx = P_RandomRange(-3, 3)*FRACUNIT
						tzw5.momy = P_RandomRange(-3, 3)*FRACUNIT
						tzw5.momz = P_RandomRange(4, 7)*FRACUNIT
					end
					P_StartQuake(FRACUNIT*8, TICRATE/4)
					P_AddPlayerScore(mobj.player, thing.reactiontime)
					local tzw1 = P_SpawnMobj(thing.x, thing.y, thing.z+10*FRACUNIT, thing.info.painchance)
					tzw1.momx = P_RandomRange(-3, 3)*FRACUNIT
					tzw1.momy = P_RandomRange(-3, 3)*FRACUNIT
					tzw1.momz = P_RandomRange(4, 7)*FRACUNIT
					local tzw2 = P_SpawnMobj(thing.x, thing.y, thing.z+10*FRACUNIT, thing.info.painchance)
					tzw2.momx = P_RandomRange(-3, 3)*FRACUNIT
					tzw2.momy = P_RandomRange(-3, 3)*FRACUNIT
					tzw2.momz = P_RandomRange(4, 7)*FRACUNIT
					local tzw3 = P_SpawnMobj(thing.x, thing.y, thing.z+10*FRACUNIT, thing.info.painchance)
					tzw3.momx = P_RandomRange(-3, 3)*FRACUNIT
					tzw3.momy = P_RandomRange(-3, 3)*FRACUNIT
					tzw3.momz = P_RandomRange(4, 7)*FRACUNIT
					S_StartSound(mobj, thing.info.deathsound)
					thing.busted = true
					thing.killer = mobj.player
					P_RemoveMobj(thing)
					S_StopSoundByID(mobj, 22)
//Sounds
					if mobj.player.sam == true then
						if not S_IdPlaying(sfx_tasty)
						and not S_IdPlaying(sfx_ysmt)
						and not S_IdPlaying(sfx_ysmt2)
						and not S_IdPlaying(sfx_ysmt3)
						and not S_IdPlaying(sfx_ysmt4) then
						local ysmt = P_RandomRange(1, 4)
							if ysmt == 1 then
							local sam = P_RandomRange(1, 3)
								if sam == 1 then
									S_StartSound(mobj, sfx_ysmt, mobj.player)
								elseif sam == 2 then
									S_StartSound(mobj, sfx_ysmt3, mobj.player)
								elseif sam == 3 then
									S_StartSound(mobj, sfx_ysmt4, mobj.player)
								end
							end
						end
					end
				return false	
				end
			end
		end
	end
end,MT_PLAYER)

addHook("MobjMoveCollide", function(m, thing)
	if thing and thing.valid
	and m and m.valid then
		if thing.info.deathstate == S_TAZW2
		and m.flags & MF_MISSILE then
		return false
		end
	end
end)

//Projectiles too
addHook("ShouldDamage", function(t, i, s)
	if i and i.valid
	and s and s.valid
	and t and t.valid then
		if t.info.deathstate == S_TAZW
		or t.info.deathstate == S_TAZW2
		or (t.info.deathstate == S_TAZW3
		and s.player) then
			P_StartQuake(FRACUNIT*8, TICRATE/4)
			if s.player then
				P_AddPlayerScore(s.player, t.reactiontime)
				local combo = P_SpawnMobj(t.x, t.y, t.z, MT_COMBOCRUMBLE)
				combo.firer = s.player
			end
			local tzw1 = P_SpawnMobj(t.x, t.y, t.z, t.info.painchance)
			tzw1.momx = P_RandomRange(-3, 3)*FRACUNIT
			tzw1.momy = P_RandomRange(-3, 3)*FRACUNIT
			tzw1.momz = P_RandomRange(4, 7)*FRACUNIT
			local tzw2 = P_SpawnMobj(t.x, t.y, t.z, t.info.painchance)
			tzw2.momx = P_RandomRange(-3, 3)*FRACUNIT
			tzw2.momy = P_RandomRange(-3, 3)*FRACUNIT
			tzw2.momz = P_RandomRange(4, 7)*FRACUNIT
			local tzw3 = P_SpawnMobj(t.x, t.y, t.z, t.info.painchance)
			tzw3.momx = P_RandomRange(-3, 3)*FRACUNIT
			tzw3.momy = P_RandomRange(-3, 3)*FRACUNIT
			tzw3.momz = P_RandomRange(4, 7)*FRACUNIT
			local tzw4 = P_SpawnMobj(t.x, t.y, t.z, t.info.painchance)
			tzw4.momx = P_RandomRange(-3, 3)*FRACUNIT
			tzw4.momy = P_RandomRange(-3, 3)*FRACUNIT
			tzw4.momz = P_RandomRange(4, 7)*FRACUNIT
			local tzw5 = P_SpawnMobj(t.x, t.y, t.z, t.info.painchance)
			tzw5.momx = P_RandomRange(-3, 3)*FRACUNIT
			tzw5.momy = P_RandomRange(-3, 3)*FRACUNIT
			tzw5.momz = P_RandomRange(4, 7)*FRACUNIT
			S_StartSound(tzw1, t.info.deathsound)
			t.busted = true
			P_RemoveMobj(t)
			S_StopSoundByID(i, 22)
		return true
		end
	end
end)

//Multiple part scenery crumble
addHook("MobjMoveCollide", function(m, thing)
	if thing and thing.valid
	and m and m.valid then
		if thing.busted != true
		and thing.valid then
			if thing.info.deathstate == S_TAZW 
			or thing.info.deathstate == S_TAZW2 then
				P_AddPlayerScore(m.firer, thing.reactiontime)
				local tzw1 = P_SpawnMobj(m.x, m.y, m.z, t.info.painchance)
				tzw1.momx = P_RandomRange(-3, 3)*FRACUNIT
				tzw1.momy = P_RandomRange(-3, 3)*FRACUNIT
				tzw1.momz = P_RandomRange(4, 7)*FRACUNIT
				local tzw2 = P_SpawnMobj(m.x, m.y, m.z, t.info.painchance)
				tzw2.momx = P_RandomRange(-3, 3)*FRACUNIT
				tzw2.momy = P_RandomRange(-3, 3)*FRACUNIT
				tzw2.momz = P_RandomRange(4, 7)*FRACUNIT
				local tzw3 = P_SpawnMobj(m.x, m.y, m.z, t.info.painchance)
				tzw3.momx = P_RandomRange(-3, 3)*FRACUNIT
				tzw3.momy = P_RandomRange(-3, 3)*FRACUNIT
				tzw3.momz = P_RandomRange(4, 7)*FRACUNIT
				P_StartQuake(FRACUNIT*8, TICRATE/4)
				thing.busted = true
				S_StartSound(m, thing.info.deathsound)
				P_RemoveMobj(thing)
			end
			if thing.type == MT_FLAME then
				local tzw1 = P_SpawnMobjFromMobj(thing, P_RandomRange(-20, 20)*FRACUNIT, P_RandomRange(-20, 20)*FRACUNIT, P_RandomRange(-5, 10)*FRACUNIT, MT_FLAMEPARTICLE)
				tzw1.fuse = TICRATE
				tzw1.momz = FRACUNIT
				local tzw2 = P_SpawnMobjFromMobj(thing, P_RandomRange(-20, 20)*FRACUNIT, P_RandomRange(-20, 20)*FRACUNIT, P_RandomRange(-5, 10)*FRACUNIT, MT_FLAMEPARTICLE)
				tzw2.fuse = TICRATE
				tzw2.momz = FRACUNIT
				local tzw3 = P_SpawnMobjFromMobj(thing, P_RandomRange(-20, 20)*FRACUNIT, P_RandomRange(-20, 20)*FRACUNIT, P_RandomRange(-5, 10)*FRACUNIT, MT_FLAMEPARTICLE)
				tzw3.fuse = TICRATE
				tzw3.momz = FRACUNIT
				P_RemoveMobj(thing)
			end
		end
	end
end,MT_COMBOCRUMBLE)

addHook("MobjThinker",function(m)
	if m.scale == FRACUNIT
	and m.bpspawn != true then
		m.scalespeed = FRACUNIT*100
		m.destscale = FRACUNIT/5*7
		m.bpspawn = true
	end
	if m.scale == FRACUNIT/5*7 then
		m.destscale = FRACUNIT/64
	end
	if m.scale == FRACUNIT/64 then
		P_KillMobj(m)
	end
end,MT_COMBOCRUMBLE)