-- Hell version of Desert Bus.
local STARTTIME = 6*TICRATE + (3*TICRATE/4)

freeslot("MT_DBUS_INNYUME", "sfx_dbuiny", "sfx_dbuiyt") -- woo
sfxinfo[_G["sfx_dbuiny"]].flags = SF_X8AWAYSOUND
sfxinfo[_G["sfx_dbuiyt"]].flags = SF_X8AWAYSOUND

local function FUCK_LightLevelChange()
	if leveltime <= 2 then
		for p in players.iterate do
			p.mazedone = false
		end
		server.hellbusbrightness = 255
		server.hellbusbrightnesssky = 255
		server.hellbusskyboxchange = false
	end
	server.hellbusbrightness = $ or 255
	server.hellbusbrightnesssky = $ or 255
	server.hellbusskyboxchange = $ or false
	if server.hellbusbrightness < 64 then server.hellbusbrightness = 64 end
	if server.hellbusbrightnesssky < 16 then 
		if not server.hellbusskyboxchange then
			-- change the skybox
			server.hellbusskyboxchange = true
			P_LinedefExecute(6002, nil)
		end
		server.hellbusbrightnesssky = 16 
	end
	if server.hellbustimer then
		server.hellbustimer = $ - 1
		server.hellbusbrightness = $ - 1
		server.hellbusbrightnesssky = $ - 2
	end
	if server.hellbustimer_ then
		server.hellbustimer_ = $ - 1
		server.hellbusbrightness = $ + 2
		server.hellbusbrightnesssky = $ + 2
	end
	for i = 0, #sectors do
		local s = sectors[i]
		if not s then continue end
		
		s.lightlevel = server.hellbusbrightness
		
		if s.tag == 6001 then
			s.lightlevel = server.hellbusbrightnesssky
		end
	end
end

local function FUCK_ThinkFrameShit()
	local p = nil
	for q in players.iterate do
		if q.spectator then continue end
		if not (q.mo and q.mo.valid) then continue end
		if leveltime > STARTTIME + 1*TICRATE and q.kartstuff[k_respawn] <= 0 and P_IsObjectOnGround(q.mo) then
			P_Thrust(q.mo, 0, FRACUNIT/24)
		end
		if q.kartstuff[k_position] ~= 1 then continue end -- only check for first placer.
		p = q
	end
	if not (p and p.valid) then return end -- nope. nothin
	local laps = p.laps + 1
	if p.kartstuff[k_lapanimation] and p.kartstuff[k_lapanimation] % 4 == 0 then
		if laps > 20 and laps < 45 then -- sunset
			server.hellbusbrightness = $ - 1
			server.hellbusbrightnesssky = $ - 2
		elseif laps >= 48 then -- sunrise
			server.hellbusbrightness = $ + 2
			server.hellbusbrightnesssky = $ + 2
		end
	end
end

COM_AddCommand("desertdebug", function(p, ...)
	local packedargs = {...}
	if not packedargs then CONS_Printf(p, "Supply a fucking command for god's sake!") return end
	local finalstring = table.concat(packedargs, " ")
	if not finalstring or finalstring == "" or finalstring == "" then CONS_Printf(p, "Supply a fucking command for god's sake!") return end
	
	if finalstring:lower():find("br low") then
		server.hellbustimer = 80
	elseif finalstring:lower():find("br high") then
		server.hellbustimer_ = 80
	elseif finalstring:lower():find("spawn") then
		if not (p.mo and p.mo.valid) then CONS_Printf(p, "Are you in spectator?") return end
		p.dbusinnyume = P_SpawnMobj(p.mo.x, p.mo.y, p.mo.z, MT_DBUS_INNYUME) -- spook
		p.dbusinnyume.innyumeintro = 207
		p.dbusinnyume.target = p.mo -- target YOU.
		p.dbusinnyume.lastplayer = p -- misremembrance
	else
		CONS_Printf(p, gamemap)
	end
end, COM_ADMIN)

addHook("ThinkFrame", function()
	if not mapheaderinfo[gamemap].desertbus_hell then return end
	FUCK_ThinkFrameShit()
	FUCK_LightLevelChange()
	
	for p in players.iterate do
		if not (p.mo and p.mo.valid) then continue end
		if p.spectator then continue end
		if not p.dbusinnyume then continue end
		if p.kartstuff[k_respawn] > 0 then
			P_SetOrigin(p.dbusinnyume, p.mo.x, p.mo.y, 0)
			p.dbusinnyume.angle = p.mo.angle
		end
	end
end)

addHook("MusicChange", function(oldname, newname)
	if mapheaderinfo[gamemap].desertbus_hell and (snowydeserthell.value == 1 or snowydeserthell.value == 3 or encoremode) then
		if leveltime < STARTTIME then -- hack
			return "ESTART"
		end
	end
end)

addHook("LinedefExecute", function(line, mobj, sector)
	if not (mobj and mobj.valid and mobj.player and mobj.player.valid) then return end
	if mobj.player.mazedone then -- nope
		P_DamageMobj(mobj, mobj, mobj, 10000)
		return
	end
	S_StartSound(mobj, sfx_dbuiyt, mobj.player)
	mobj.player.dbusinnyume = P_SpawnMobj(13824*FRACUNIT, -3584*FRACUNIT, 0, MT_DBUS_INNYUME) -- spook
	mobj.player.dbusinnyume.innyumeintro = 207
	mobj.player.dbusinnyume.target = mobj -- target YOU.
	mobj.player.dbusinnyume.lastplayer = mobj.player -- misremembrance
	print(mobj.player.name.." has entered the maze.")
	
	S_ChangeMusic("mapd5m", true, mobj.player)
end, "DBUSMAZE")

addHook("MobjThinker", function(mobj)
	mobj.innyumetime = $ or 0
	if mobj.innyumeintro <= 0 then
		mobj.innyumetime = $ + 1
	else
		if mobj.innyumeintro == 207 then
			S_StartSound(mobj, sfx_dbuiyt)
		end
		mobj.innyumeintro = $ - 1
	end
	if mobj.innyumetime % 414 == 1 then -- the funny sound
		S_StartSound(mobj, sfx_dbuiny)
	end
	if not (mobj.target and mobj.target.valid) then
		if mobj.lastplayer and mobj.lastplayer.valid and mobj.lastplayer.mo and mobj.lastplayer.mo.valid then
			mobj.target = mobj.lastplayer.mo
		else
			P_LookForPlayers(mobj, 0, true)
		end
	end
	if (mobj.target and mobj.target.valid) and (mobj.target.player.kartstuff[k_spinouttimer] <= 0) and mobj.innyumeintro <= 0 then
		mobj.angle = R_PointToAngle2(mobj.x, mobj.y, mobj.target.x, mobj.target.y)
		P_Thrust(mobj, mobj.angle, FRACUNIT/2+(FRACUNIT/4))
	end
	if (mobj.target and mobj.target.valid) then
		if mobj.target.player.exiting then
			K_SpawnMineExplosion(mobj, mobj.target.player.skincolor)
			S_StopSound(mobj)
			local sound = P_SpawnMobj(mobj.x, mobj.y, mobj.z, MT_THOK)
			sound.state = S_INVISIBLE
			sound.fuse = 5*TICRATE
			S_StartSound(sound, sfx_hmtchs)
			S_StartSound(nil, sfx_hmtchs, mobj.target.player)
			mobj.fuse = 1
		end
	end
end, MT_DBUS_INNYUME)

local function InnyumeCollide(mobj, mop)
	if not (mop.player and mop.player.valid) then return end
	if mobj.innyumeintro <= 0 then
		K_SpinPlayer(mop.player)
	end
end

addHook("MobjCollide", InnyumeCollide, MT_DBUS_INNYUME)
addHook("MobjMoveCollide", InnyumeCollide, MT_DBUS_INNYUME)

addHook("LinedefExecute", function(line, mobj, sector)
	if not (mobj and mobj.valid and mobj.player and mobj.player.valid) then return end
	print(mobj.player.name.." has exited the maze.")
	mobj.player.mazedone = true
	P_RemoveMobj(mobj.player.dbusinnyume)
	mobj.player.dbusinnyume = nil -- GONE
	S_ChangeMusic("kmapd9", true, mobj.player)
end, "DBUSMAZ2")

-- cacti stuff
addHook("MobjSpawn",function(mo)
	if not mapheaderinfo[gamemap].desertbus_hell then return end
	if (snowydeserthell.value == 1 or snowydeserthell.value == 3 or encoremode) then
		if mo.valid then
			mo.sprite = SPR_XMS3
			mo.frame = A
		end
	else
		if mo.valid then
			mo.sprite = SPR_MSCB
			mo.frame = A
		end
	end
	mo.scale = mapobjectscale/2
end, MT_CACTI2)

addHook("MobjSpawn",function(mo)
	if not mapheaderinfo[gamemap].desertbus_hell then return end
	if (snowydeserthell.value == 1 or snowydeserthell.value == 3 or encoremode) then
		if mo.valid then
			mo.state = S_SMK_SNOWBALL
		end
	else
		if mo.valid then
			mo.sprite = SPR_MSCA
			mo.frame = A
		end
	end
	mo.scale = mapobjectscale/2
end, MT_CACTI1)