BonkerBox_L.HidePlayer(BonkerBox_FWEnvi.GetSkinName())

addHook("PlayerQuit", function(player,reason)
	 BonkerBox_FWMain.ShutDown(player)
end)

addHook("MapLoad", function(mapnum)
	for player in players.iterate
		BonkerBox_FWMain.ShutDown(player)//whilst technically not neccessary as mobjs will respawn in PostThinkFrame, this also nils the fuzzywiggler table even if the player is still playing as fuzzywiggler which stops lingering effect timers etc. from stickign around
	end
end)

addHook("PostThinkFrame",function()
	for player in players.iterate
		if(BonkerBox_G.IsPlayingAs(player,BonkerBox_FWEnvi.GetSkinName())~=true)
			BonkerBox_FWMain.ShutDown(player)
			continue
		end
		
		local loadFromPersist = function()
			if(BonkerBox_G.IsNumber(player.bonkerbox_blittime)==true)//designed to only be true for the first frame of a map, doing this in the MapLoad hook doesn't work because that hook happens before PostThinkFrame
				player[BonkerBox_FWEnvi.GetSkinName()..BonkerBox_FWEnvi.GetPersistentTableSuffix()] = BonkerBox_G.DeepCopyTable(player[BonkerBox_FWEnvi.GetSkinName()..BonkerBox_FWEnvi.GetPersistentCacheTableSuffix()])
			end
		end
		
		BonkerBox_FWMain.StartUp(player, loadFromPersist)
		
		BonkerBox_FWMain.RecordBeforeTrackPosition(player)		
		BonkerBox_FWMain.ExtendedTrack(player)
		
		player[BonkerBox_FWEnvi.GetSkinName()].magdata = BonkerBox_FWFunc.CalculateMag(player,BonkerBox_E.DecimalToFixed(BonkerBox_FWEnvi.GetMaxDrivingSpeed()))
		player[BonkerBox_FWEnvi.GetSkinName()].gravitystatus = BonkerBox_G.GetGravityStatus(player.mo)
		player[BonkerBox_FWEnvi.GetSkinName()].hurtmode = BonkerBox_G.PlayerIsSuffering(player)
		
		BonkerBox_FWMain.BodyFollow(player)
		BonkerBox_FWMain.Animate(player)
		
		if(player.mo.state ~= S_KART_DEAD)
			player[BonkerBox_FWEnvi.GetSkinName()].deathstage = -1 //force the death animation to be stuck preparing forever recordless of deathtics if we're not dying
		end

		if(player.respawn.state ~= RESPAWNST_NONE)
			BonkerBox_FWMain.AnimateRespawn(player)
		elseif(player.mo.state == S_KART_DEAD)
			BonkerBox_FWMain.AnimateDead(player)
		else
			BonkerBox_FWMain.AnimateAlive(player)
		end
		
		local acc = BonkerBox_FWEnvi.GetAltColourCount()
		
		for i=1,acc
			BonkerBox_FWMain.ApplyModulatedColour(player,i,acc,player[BonkerBox_FWEnvi.GetSkinName()..BonkerBox_FWEnvi.GetPersistentTableSuffix()][BonkerBox_FWEnvi.GetAltColourName(i)],player[BonkerBox_FWEnvi.GetSkinName()..BonkerBox_FWEnvi.GetPersistentTableSuffix()].bodycolmapping)
		end
		
		BonkerBox_FWMain.UpdateModulatedColourOffset(player,FRACUNIT)

		BonkerBox_FWMain.ApplyHurtColour(player)
		BonkerBox_FWMain.ApplyGhosting(player)
		
		player[BonkerBox_FWEnvi.GetSkinName()].lastPlayerPos = BonkerBox_FWFunc.GetPlayerPosAsTable(player)
	end
end)