local quiztext = {
	{ //text 1
		"EGGQUIZ3", //image
		"Eggman", //name
		"Mwhahahahaha!", //line1
		"You have fallen", //line2
		"into my trap!", //line3
		15, //waittime
		2, //nexttext
		false, //flip
	},
	{ //text 2
		"EGGQUIZ2", //image
		"Eggman", //name
		"There is no one in", //line1
		"the world who can match", //line2
		"my intellect!", //line3
		15, //waittime
		3, //nexttext
		false, //flip
	},
	{ //text 3
		"EGGQUIZ2", //image
		"Eggman", //name
		"And to prove it,", //line1
		"you Sonic-loving", //line2
		"scatterbrains", //line3
		15, //waittime
		4, //nexttext
		false, //flip
	},
	{ //text 4
		"EGGQUIZ2", //image
		"Eggman", //name
		"are being put to the", //line1
		"test in the hardest", //line2
		"quiz in the world!", //line3
		15, //waittime
		5, //nexttext
		false, //flip
	},
	{ //text 5
		"EGGQUIZ2", //image
		"Eggman", //name
		"And make sure you're", //line1
		"in full agreement", //line2
		"while answering!", //line3
		15, //waittime
		6, //nexttext
		false, //flip
	},
	{ //text 6
		"EGGQUIZ1", //image
		"Eggman", //name
		"Now, question 1:", //line1
		"what is the color", //line2
		"of my cape?", //line3
		15, //waittime
		7, //nexttext
		false, //flip
	},
	{ //text 7
		"EGGQUIZ1", //image
		"Eggman", //name
		"Red: Green, Blue: Red,", //line1
		"Yellow: Blue, or Green: Yellow?", //line2
		nil, //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 8
		"EGGQUIZ3", //image
		"Eggman", //name
		"WRONG!", //line1
		nil, //line2
		nil, //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 9
		"EGGQUIZ2", //image
		"Eggman", //name
		"Well that was just a warmup", //line1
		"question! Now for the real", //line2
		"question 1!", //line3
		15, //waittime
		10, //nexttext
		false, //flip
	},
	{ //text 10
		"EGGQUIZ1", //image
		"Eggman", //name
		"Which of these is not", //line1
		"a selectable S_SKIN", //line2
		"character ability?", //line3
		15, //waittime
		11, //nexttext
		false, //flip
	},
	{ //text 11
		"EGGQUIZ1", //image
		"Eggman", //name
		"Red: Telekinesis, Blue: Bounce,", //line1
		"Green: Homing Attack,", //line2
		"or Yellow: Jump Boost?", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 12
		"EGGQUIZ4", //image
		"Eggman", //name
		"Drat! I thought you'd", //line1
		"mix it up with Homing", //line2
		"Thok for sure!", //line3
		15, //waittime
		13, //nexttext
		false, //flip
	},
	{ //text 13
		"EGGQUIZ2", //image
		"Eggman", //name
		"Now, question 2:", //line1
		"How many tokens are", //line2
		"in Greenflower 1?", //line3
		15, //waittime
		14, //nexttext
		false, //flip
	},
	{ //text 14
		"EGGQUIZ1", //image
		"Eggman", //name
		"Red: 1, Blue: 2,", //line1
		"Green: 3, or Yellow: 4?", //line2
		nil, //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 15
		"EGGQUIZ1", //image
		"Eggman", //name
		"Wait, that's the right", //line1
		"answer? Well, nevermind", //line2
		"that. Question 3!", //line3
		15, //waittime
		16, //nexttext
		false, //flip
	},
	{ //text 16
		"EGGQUIZ2", //image
		"Eggman", //name
		"How many seconds are", //line1
		"in a TICRATE?", //line2
		nil, //line3
		15, //waittime
		17, //nexttext
		false, //flip
	},
	{ //text 17
		"EGGQUIZ1", //image
		"Eggman", //name
		"Red: 1/35, Blue: 1,", //line1
		"Green: 30, or Yellow: 35", //line2
		nil, //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 18
		"EGGQUIZ3", //image
		"Eggman", //name
		"TICRATE is equal to 35,", //line1
		"which is one second in", //line2
		"35 FPS! But, question 4!", //line3
		15, //waittime
		19, //nexttext
		false, //flip
	},
	{ //text 19
		"EGGQUIZ1", //image
		"Eggman", //name
		"What are the names", //line1
		"of my wonderful", //line2
		"bee badniks?", //line3
		15, //waittime
		20, //nexttext
		false, //flip
	},
	{ //text 20
		"EGGQUIZ1", //image
		"Eggman", //name
		"Red: Bitz, Blue: Bumblebores,", //line1
		"Green: Buzzpores,", //line2
		"or Yellow: Embers?", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 21
		"EGGQUIZ1", //image
		"Eggman", //name
		"Nobody could ever forget", //line1
		"my favorite creations!", //line2
		"But, Question 5!", //line3
		35, //waittime
		22, //nexttext
		false, //flip
	},
	{ //text 22
		"EGGQUIZ2", //image
		"Eggman", //name
		"How many times do you", //line1
		"need to beat Ultimate", //line2
		"Mode to unlock Shadow?", //line3
		15, //waittime
		23, //nexttext
		false, //flip
	},
	{ //text 23
		"EGGQUIZ1", //image
		"Eggman", //name
		"Red: 200, Blue: 400,", //line1
		"Green: 600, or Yellow: 999?", //line2
		nil, //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 24
		"EGGQUIZ3", //image
		"Eggman", //name
		"Speaking of Ultimate Mode,", //line1
		"can you get through this", //line2
		"ERZ1 room without rings?", //line3
		35, //waittime
		25, //nexttext
		false, //flip
	},
	{ //text 25
		"EGGQUIZ3", //image
		"Eggman", //name
		"Red: No, Blue: No,", //line1
		"Green: No, or Yellow: Yes?", //line2
		nil, //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 26
		"EGGQUIZ4", //image
		"Eggman", //name
		"You...", //line1
		"I guess I shouldn't have", //line2
		"made that the correct answer!", //line3
		15, //waittime
		27, //nexttext
		false, //flip
	},
	{ //text 27
		"EGGQUIZ1", //image
		"Eggman", //name
		"Question 7:", //line1
		"How many emblems do you", //line2
		"have when unlocking Amy?", //line3
		15, //waittime
		28, //nexttext
		false, //flip
	},
	{ //text 28
		"EGGQUIZ3", //image
		"Eggman", //name
		"Red: 5, Blue: 10,", //line1
		"Green: 15,", //line2
		"or Yellow: Don't Care?", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 29
		"EGGQUIZ3", //image
		"Eggman", //name
		"That's right, you only", //line1
		"ever need to beat", //line2
		"Frozen Hillside Zone!", //line3
		15, //waittime
		30, //nexttext
		false, //flip
	},
	{ //text 30
		"EGGQUIZ1", //image
		"Eggman", //name
		"Question 8:", //line1
		"What was Sonic's first", //line2
		"ever home appearance?", //line3
		15, //waittime
		31, //nexttext
		false, //flip
	},
	{ //text 31
		"EGGQUIZ3", //image
		"Eggman", //name
		"Red: Rad Mobile, Blue: Quick & Silva,", //line1
		"Green: Sonic the Hedgehog,", //line2
		"or Yellow: Golden Axe?", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 32
		"EGGQUIZ3", //image
		"Eggman", //name
		"I love bootlegs! In fact,", //line1
		"here's a question for", //line2
		"another bootleg!", //line3
		15, //waittime
		33, //nexttext
		false, //flip
	},
	{ //text 33
		"EGGQUIZ1", //image
		"Eggman", //name
		"Question 9:", //line1
		"How is Spring Yard misspelled", //line2
		"in Somari's level select?", //line3
		15, //waittime
		34, //nexttext
		false, //flip
	},
	{ //text 34
		"EGGQUIZ3", //image
		"Eggman", //name
		"Red: Spring Yand, Blue: Sping Yard,", //line1
		"Green: Spning Yard,", //line2
		"or Yellow: Sprang Yird?", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 35
		"EGGQUIZ3", //image
		"Eggman", //name
		"I bet you disastrous", //line1
		"dimwits would've made", //line2
		"the same typo!", //line3
		15, //waittime
		36, //nexttext
		false, //flip
	},
	{ //text 36
		"EGGQUIZ2", //image
		"Eggman", //name
		"Question 10:", //line1
		"Which of these SRB2", //line2
		"objects do not have", //line3
		15, //waittime
		37, //nexttext
		false, //flip
	},
	{ //text 37
		"EGGQUIZ2", //image
		"Eggman", //name
		"The MF_ENEMY object", //line1
		"flag when spawning?", //line2
		"Use your eyes this time!", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 38
		"EGGQUIZ3", //image
		"Eggman", //name
		"Amy will never not", //line1
		"be an enemy to me!", //line2
		"Now, Question 11:", //line3
		15, //waittime
		39, //nexttext
		false, //flip
	},
	{ //text 39
		"EGGQUIZ1", //image
		"Eggman", //name
		"What game is the", //line1
		"Pure Fat jingle from?", //line2
		nil, //line3
		15, //waittime
		40, //nexttext
		false, //flip
	},
	{ //text 40
		"EGGQUIZ1", //image
		"Eggman", //name
		"Red: Earthworm 3D, Blue: SRB2,", //line1
		"Green: ClayFighter 63 1/3,", //line2
		"or Yellow: Sonic Shuffle?", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 41
		"EGGQUIZ4", //image
		"Eggman", //name
		"I really wish they", //line1
		"never removed that", //line2
		"animation!", //line3
		15, //waittime
		42, //nexttext
		false, //flip
	},
	{ //text 42
		"EGGQUIZ2", //image
		"Eggman", //name
		"Question 12:", //line1
		"How much scrap does", //line2
		"Skip need to make a", //line3
		15, //waittime
		43, //nexttext
		false, //flip
	},
	{ //text 43
		"EGGQUIZ1", //image
		"Eggman", //name
		"Golden Force Monitor?", //line1
		"Red: 82, Blue: 104,", //line2
		"Green: 126, or Yellow: 148?", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 44
		"EGGQUIZ3", //image
		"Eggman", //name
		"You should play as", //line1
		"Skip! Message paid", //line2
		"for by SMS Alfredo!", //line3
		15, //waittime
		45, //nexttext
		false, //flip
	},
	{ //text 45
		"EGGQUIZ1", //image
		"Eggman", //name
		"Question 13:", //line1
		"We ran out of ideas,", //line2
		"so just guess!", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
	{ //text 46
		"EGGQUIZ4", //image
		"Eggman", //name
		"...", //line1
		"Fine, you get to live!", //line2
		"But you won't be so", //line3
		15, //waittime
		47, //nexttext
		false, //flip
	},
	{ //text 47
		"EGGQUIZ4", //image
		"Eggman", //name
		"lucky next time!", //line1
		"Because next time, I'll", //line2
		"prove my creativity!", //line3
		35, //waittime
		0, //nexttext
		false, //flip
	},
}

//Get the value of a certain data from a certain id in quiztext
local quiztextvalue = function(id,arg)
	if arg == "image" return quiztext[id][1]
	elseif arg == "name" return quiztext[id][2]
	elseif arg == "line1" return quiztext[id][3]
	elseif arg == "line2" return quiztext[id][4]
	elseif arg == "line3" return quiztext[id][5]
	elseif arg == "waittime" return quiztext[id][6]
	elseif arg == "nexttext" return quiztext[id][7]
	elseif arg == "flip" return quiztext[id][8] end
end

//Values used for the text display process
local textid = 0
local textletter = 0
local textwait = 0

//Start the text
//Or stop the text if we give 0 or nil as a value
//Also handles skin alterate text
rawset(_G, "startquiztext", function(id)
	if not id
		textid = 0
		textletter = 0
		textwait = 0
		return
	end
	if type(quiztext[id]) != "table"
		print(id.." is not a valid text id!")
		textid = 0
		textletter = 0
		textwait = 0
		return
	end
	if consoleplayer.realmo and consoleplayer.realmo.valid
	and quiztext[id][consoleplayer.realmo.skin] != nil
		textid = quiztext[id][consoleplayer.realmo.skin]
	else
		textid = id
	end
	textletter = 0
	textwait = 0
end)

local textseen = {}

addHook("LinedefExecute", function(line, mobj)
	local id = line.frontside.textureoffset/FRACUNIT
	if mobj.player and mobj.player == consoleplayer
	and (not textseen[id] or line.flags&ML_NOCLIMB)
		startquiztext(id)
	end
end, "QUIZTEXT")

addHook("LinedefExecute", function(line, mobj)
	local id = line.frontside.textureoffset/FRACUNIT
	if mobj.player and mobj.player == consoleplayer
	and (not textseen[id] or line.flags&ML_NOCLIMB)
		startquiztext(id)
	end
end, "QUIZTEXT")

addHook("LinedefExecute", function(line, mobj)
	local dumbsector
	local dumbmobj
	for sector in sectors.iterate
		if sector and sector.valid and sector.tag == line.tag
			dumbsector = sector
			break
		end
	end
	if not (dumbsector and dumbsector.valid) return end
	for mobj in dumbsector.thinglist()
		if mobj and mobj.valid and mobj.type == MT_TELEPORTMAN
			dumbmobj = mobj
			break
		end
	end
	if not (dumbmobj and dumbmobj.valid) return end
	for player in players.iterate
		if player.mo and player.mo.valid
			P_FlashPal(player, PAL_MIXUP, 10)
			P_TeleportMove(player.mo, dumbmobj.x, dumbmobj.y, dumbmobj.z)
			player.mo.angle = dumbmobj.angle
			P_ResetPlayer(player)
			player.mo.state = S_PLAY_STND
			P_InstaThrust(player.mo,0,0)
			player.mo.momz = 0
		end
		S_StartSound(nil, sfx_ideya)
		S_StartSound(nil, sfx_nxbump)
		S_StartSound(nil, sfx_mixup)
	end
end, "GOODQUIZTELEPORT")

addHook("LinedefExecute", function(line, mobj)
	for player in players.iterate
		if player.mo and player.mo.valid
			P_FlashPal(player, PAL_MIXUP, 10)
			P_TeleportMove(player.mo, -1472*FRACUNIT, 704*FRACUNIT, 1792*FRACUNIT)
			P_ResetPlayer(player)
			player.mo.state = S_PLAY_FALL
			P_InstaThrust(player.mo,0,0)
			player.mo.momz = 0
			startquiztext(8)
		end
		S_StartSound(nil, sfx_lose)
		S_StartSound(nil, sfx_mixup)
	end
end, "DEATHQUIZTELEPORT")

addHook("MobjDamage", function(target)
	if mapheaderinfo[gamemap].iqquiz
		for player in players.iterate
			if player.mo and player.mo.valid
				P_FlashPal(player, PAL_MIXUP, 10)
				P_TeleportMove(player.mo, -1472*FRACUNIT, 704*FRACUNIT, 1792*FRACUNIT)
				P_ResetPlayer(player)
				player.mo.state = S_PLAY_FALL
				P_InstaThrust(player.mo,0,0)
				player.mo.momz = 0
				startquiztext(8)
			end
			S_StartSound(nil, sfx_lose)
			S_StartSound(nil, sfx_mixup)
		end
		return true
	end
end, MT_PLAYER)

hud.add(function(v, player, cam)
	if not textid or menuactive or paused return end
	
	local length1 = string.len(quiztextvalue(textid,"line1"))
	local length2 = 0
	local length3 = 0
	local text1 = quiztextvalue(textid,"line1")
	local text2 = ""
	local text3 = ""
	if quiztextvalue(textid,"line2") != nil
		length2 = string.len(quiztextvalue(textid,"line2"))
		text2 = quiztextvalue(textid,"line2")
		if quiztextvalue(textid,"line3") != nil
			length3 = string.len(quiztextvalue(textid,"line3"))
			text3 = quiztextvalue(textid,"line3")
		end
	end
	local totallength = length1+length2+length3
	local flip = quiztextvalue(textid,"flip")
	
	v.drawScaled(0,150*FRACUNIT,FRACUNIT*3/2,
	v.cachePatch("SPECTILE"),V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_50TRANS)
	
	if flip
		v.drawScaled(284*FRACUNIT,150*FRACUNIT,FRACUNIT/5,
		v.cachePatch("TALKBOX"),V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_FLIP)
	else
		v.drawScaled(36*FRACUNIT,150*FRACUNIT,FRACUNIT/5,
		v.cachePatch("TALKBOX"),V_SNAPTOBOTTOM|V_SNAPTOLEFT)
	end
	
	if quiztextvalue(textid,"image") == "skin" and consoleplayer.realmo
	and consoleplayer.realmo.valid
		if flip
			v.drawScaled(292*FRACUNIT,149*FRACUNIT,FRACUNIT/2,
			v.getSprite2Patch(consoleplayer.realmo.skin, SPR2_XTRA, false, 1),
			V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_FLIP)
		else
			v.drawScaled(28*FRACUNIT,149*FRACUNIT,FRACUNIT/2,
			v.getSprite2Patch(consoleplayer.realmo.skin, SPR2_XTRA, false, 1),
			V_SNAPTOBOTTOM|V_SNAPTOLEFT)
		end
	elseif flip
		v.drawScaled(284*FRACUNIT,150*FRACUNIT,FRACUNIT/5,
		v.cachePatch(quiztextvalue(textid,"image")),V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_FLIP)
	else
		v.drawScaled(36*FRACUNIT,150*FRACUNIT,FRACUNIT/5,
		v.cachePatch(quiztextvalue(textid,"image")),V_SNAPTOBOTTOM|V_SNAPTOLEFT)
	end
	
	local name = quiztextvalue(textid,"name")
	
	if name == "skin" and consoleplayer.realmo
	and consoleplayer.realmo.valid
		name = skins[consoleplayer.realmo.skin].realname
	end
	
	local x = 91
	local snap = V_SNAPTOLEFT
	if flip
		x = 93
		snap = V_SNAPTORIGHT
		v.drawString(229, 150, name, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_YELLOWMAP, "right")
	else
		v.drawString(91, 150, name, V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_YELLOWMAP)
	end
	
	if textletter > 0
		v.drawString(x, 162,
		string.sub(quiztextvalue(textid,"line1"),1,min(textletter, length1)),
		V_SNAPTOBOTTOM|snap|V_ALLOWLOWERCASE)
	end
	
	if textletter > length1 and length2 != 0
		v.drawString(x, 174,
		string.sub(quiztextvalue(textid,"line2"),1,min(textletter-length1, length2)),
		V_SNAPTOBOTTOM|snap|V_ALLOWLOWERCASE)
	end
	
	if textletter > length1+length2 and length3 != 0
		v.drawString(x, 186,
		string.sub(quiztextvalue(textid,"line3"),1,min(textletter-length1-length2, length3)),
		V_SNAPTOBOTTOM|snap|V_ALLOWLOWERCASE)
	end
	
	if textwait
		if player.cmd.buttons&BT_JUMP
			textwait = max($-2,0)
		else
			textwait = max($-1,0)
		end
	else
		textletter = $+1
		if textletter > totallength
			startquiztext(quiztextvalue(textid,"nexttext"))
		else
			local dumb = string.sub(text1..text2..text3, textletter, textletter)
			if dumb == "," or dumb == "." or dumb == "!" or dumb == "?"
				textwait = 15
			elseif player.cmd.buttons&BT_JUMP
				textwait = 0
			else
				textwait = 1
			end
			if textletter == totallength and quiztextvalue(textid,"waittime") != nil
				textwait = $+quiztextvalue(textid,"waittime")
			end
		end
	end
end)