//Hi, I'm Takis! -Zyphyr
//from chrispy chars!!! by Lach and ChrispyPixels!!!!
local function SafeFreeslot(...)
	for _, item in ipairs({...})
		if rawget(_G, item) == nil
			freeslot(item)
		end
	end
end
local TR = TICRATE
//tb = textbox
//open
SafeFreeslot("sfx_tb_opn")
sfxinfo[sfx_tb_opn].caption = "/"
//close
SafeFreeslot("sfx_tb_cls")
sfxinfo[sfx_tb_cls].caption = "/"
//tween in
SafeFreeslot("sfx_tb_tin")
sfxinfo[sfx_tb_tin].caption = "/"
//tween out
SafeFreeslot("sfx_tb_tot")
sfxinfo[sfx_tb_tot].caption = "/"
SafeFreeslot("sfx_s_tak1")
sfxinfo[sfx_s_tak1].caption = "/"
SafeFreeslot("sfx_s_tak2")
sfxinfo[sfx_s_tak2].caption = "/"
SafeFreeslot("sfx_s_tak3")
sfxinfo[sfx_s_tak3].caption = "/"

//Takis cameo
SafeFreeslot("MT_TAKIS_FOX")
SafeFreeslot("S_TAKIS_FOX")
SafeFreeslot("SPR_TAKI")

states[S_TAKIS_FOX] = {
    sprite = SPR_TAKI,
    frame = A,
	tics = -1,
}

mobjinfo[MT_TAKIS_FOX] = {
	--$Name Takis NPC
	--$Sprite TAKIA1
	doomednum = 2023,
	spawnstate = S_TAKIS_FOX,
	radius = 16*FU,
	height = 48*FU,
	flags = MF_SOLID,
}

//Spleenik cameo
SafeFreeslot("MT_SPLEENIK_CRIMINAL")
SafeFreeslot("S_SPLEENIK_CRIMINAL")
SafeFreeslot("SPR_SPLN")

states[S_SPLEENIK_CRIMINAL] = {
    sprite = SPR_SPLN,
    frame = A,
	tics = -1,
}

mobjinfo[MT_SPLEENIK_CRIMINAL] = {
	--$Name Spleenik NPC
	--$Sprite SPLNA1
	doomednum = 2024,
	spawnstate = S_SPLEENIK_CRIMINAL,
	height = 36*FU,
	radius = 16*FU,
	flags = MF_SOLID,
}

//Talk bubble
SafeFreeslot("SPR_TLBB")

//Stupid rat
SafeFreeslot("MT_STUPID_INDIGO_RAT")
SafeFreeslot("S_STUPID_INDIGO_RAT")
SafeFreeslot("SPR_SRAT")

states[S_STUPID_INDIGO_RAT] = {
    sprite = SPR_SRAT,
    frame = A|FF_ANIMATE,
	var1 = 3,
	var2 = 3,
	tics = 3*3,
	nextstate = S_STUPID_INDIGO_RAT,
}

mobjinfo[MT_STUPID_INDIGO_RAT] = {
	--$Name Stupid Rat
	--$Sprite SRATA0
	doomednum = 2025,
	spawnstate = S_STUPID_INDIGO_RAT,
	radius = 32*FU,
	height = 74*FU,
	flags = MF_SOLID
}

//zb likes to cry when i add custom vars to mobjinfo
addHook("MobjSpawn",function(taykis)
	for p in players.iterate
		if p.realmo.skin == "takisthefox"
			P_RemoveMobj(taykis)
			return
		end
	end
	//im lazy
	taykis.color = SKINCOLOR_FOREST
	//erm,, this is stupid
	taykis.shadowscale = FU
	taykis.skinname = "takisthefox"
	taykis.dialogchoices = {
		"Hi, I'm Takis!",
		"IT'S HAPPY HOUR!",
		"Erm...",
		"Any good restaurants around here?",
		'Mario walks into a bar. "Oof!"',
		"Catchphrase",
		"Luigi please add details",
		"Blasted game!",
		"Why are there so many ads everywhere?",
		"I live here so I don't have to pay any taxes. No, not this room!",
		"My house is a bit off the coast. Pretty easy for me to get there, and it has an awesome view!",
	}
	taykis.dialogchoices[-6] = "What the blast!? You're me!"
	taykis.dialogchoices[-5] = "What the blast!? \x83Soap\x80!?"
	taykis.portname = "Takis"
	taykis.portgfx = "TAKISPORTRAIT"
	taykis.portsfx = {sfx_s_tak1,sfx_s_tak2,sfx_s_tak3}
	taykis.portchance = FU/3
end,MT_TAKIS_FOX)
addHook("MobjSpawn",function(spleen)
	for p in players.iterate
		if p.realmo.skin == "retrospleenik"
			P_RemoveMobj(spleen)
			return
		end
	end
	//erm,, this is stupid
	spleen.shadowscale = FU
	spleen.skinname = "retrospleenik"
	spleen.dialogchoices = {
		"wassup",
		"have you heard about qrb2?",
	}
	spleen.dialogchoices[-5] = "wow, it's like looking into a mirror!"
	spleen.portname = "Spleenik"
	spleen.portgfx = "SPLEENIKPORTRAIT"
end,MT_SPLEENIK_CRIMINAL)

addHook("PlayerThink",function(p)
	if not p
	or not p.valid
		return
	end
	
	//Init
	if p.takisnpc == nil
		p.takisnpc = {
			c2 = 0,
			dialog = '',
			missingdialog = '',
		}
	end
	
	if (p.cmd.buttons & BT_CUSTOM2)
		p.takisnpc.c2 = $+1
	else
		p.takisnpc.c2 = 0
	end	
end)

local missingtext = {
	"\x85This message should not appear.",
	"\x85This message should not appear, what should I do?",
	"\x85This message should not appear. I'll be scared if it does...",
	"\x85This message should not appear. If it does, it's a bug.",
	"\x85".."Error!",
	"\x85".."Error?",
}

local function npcthinker(tay)
	if not tay
	or not tay.valid
		return
	end
	
	local dist = 100
	
	for p in players.iterate
		local mo = p.mo
		
		if not mo
		or not mo.valid
			continue
		end
		
		local dx = tay.x-mo.x
		local dy = tay.y-mo.y
		local dz = tay.z-mo.z
		
		if FixedHypot(FixedHypot(dx,dy),dz) > dist*tay.scale
			continue
		end
		
		if p == displayplayer
			if not (p.textBoxInAction)
				local bbl = P_SpawnMobjFromMobj(tay,0,0,tay.height*3/2,MT_THOK)
				bbl.fuse = -1
				bbl.tics = 2
				bbl.frame = A
				bbl.sprite = SPR_TLBB
			end
		end
		
		if (tay.skinname == "takisthefox")
			if skins["soapthehedge"]
			and not tay.specialtext
				table.insert(taykis.dialogchoices,"I heard \x83Soap\x80 is around!")
				tay.specialtext = true
			end
		end
		
		if (p.takisnpc.c2 == 1)
		and not (p.textBoxInAction)
			p.takisnpc.missingdialog = P_RandomRange(1,#missingtext)
			p.takisnpc.dialog = tay.dialogchoices and P_RandomRange(1,#tay.dialogchoices) or 1
			if mo.skin == "soapthehedge"
				p.takisnpc.dialog = -5
			end
			if mo.skin == tay.skinname
				p.takisnpc.dialog = -6
			end
			CFTextBoxes:DisplayBox(p,
				{
					[1] = {
						name = tay.portname,
						graphic = tay.portgfx,
						text = 	
							tay.dialogchoices[p.takisnpc.dialog] or 
							missingtext[p.takisnpc.missingdialog],
						sound = tay.portsfx or {sfx_radio},
						soundchance = tay.portchance or FU/2,
						delay = 4*TICRATE,
						next = 0
					}
				}
			)
		end
	end
end

addHook("MobjThinker",npcthinker,MT_TAKIS_FOX)
addHook("MobjThinker",npcthinker,MT_SPLEENIK_CRIMINAL)
