//this is for sonki, just keeping it here just incase

//define statusface variables
addHook("PlayerThink", function(p)
	if not p
	or not p.valid
		return
	end
	
	if ((p.mo) and (p.mo.valid))
		
		if not (p.statusface)
			
			p.statusface = {
				state = '',
				frame = 0,
				priority = 0,
			}
			
		end
		
	end
end)

local function calcstatusface(p,takis)
	local me = p.mo
	
	if not p.statusface
		return
	end
	
	//idle
	p.statusface.state = "IDLE"
	p.statusface.frame = (leveltime/3)%2
	p.statusface.priority = 0
	
	if p.statusface.priority < 10
		
		//dead
		if not (me)
		or (not me.health)
		or (p.playerstate ~= PST_LIVE)
		or (p.spectator)
			p.statusface.state = "DEAD"
			p.statusface.frame = 0
			p.statusface.priority = 9
		end
	end
	
	if p.statusface.priority < 9
		
		//pain
		if (takis.inPain)
		or (takis.ticsforpain)
		or (me.sprite2 == SPR2_PAIN)
		or (me.state == S_PLAY_PAIN)
		or takis.tauntid == 1
		or p.statusface.painfacetic
			p.statusface.state = "PAIN"
			p.statusface.frame = (leveltime%4)/2
			p.statusface.priority = 8
		end
		
	end
	
	
	if p.statusface.priority < 6
		
		//doom's godmode face
		if (p.pflags & PF_GODMODE)
			p.statusface.state = "GOD_"
			p.statusface.priority = 5
		end
		
	end
	
	if p.statusface.priority < 2
	
		//space drown
		if P_InSpaceSector(me)
		or ((p.powers[pw_underwater]) and (p.powers[pw_underwater] <= 11*TR))
			p.statusface.state = "SDWN"
			p.statusface.frame = (leveltime)%2
			p.statusface.priority = 1
		end
		
	end
	
	return p.statusface.state, p.statusface.frame
end

local function drawface(v,p)
	
	if not p.statusface
		return
	end

	local me = p.mo
	
	//all your head patches must start with this prefix here
	local prefix = "SONK_"
	
	local healthstate,healthframe = calcstatusface(p)	
	
	// String all the variables together to the the face graphic
	local headpatch = v.cachePatch(prefix..healthstate..tostring(healthframe))
	
	/*
		headpatch will return the head patch to draw
		
		the idle state will return
		SONK_IDLE0 or SONK_IDLE1
		the number at the end being the frame number
		
	*/
	
	//extra fancy stuff to change the head color
	//you dont need these if you dont want them
	local eflags = V_HUDTRANS
	local headcolor
	if p.spectator
		headcolor = SKINCOLOR_CLOUDY
		eflags = V_HUDTRANSHALF
	else
		if ((me) and (me.valid))
			headcolor = me.color
		else
			headcolor = SKINCOLOR_CLOUDY
			eflags = V_HUDTRANSHALF
		end
	end
	
	//draw the face erm,,,
	v.drawScaled(20*FU,27*FU,2*FU/5, 
		headpatch, 
		V_SNAPTOLEFT|V_SNAPTOTOP|V_PERPLAYER|eflags,
		v.getColormap(nil,headcolor)
	)

end

addHook("HUD", function(v,p)
	if not p
	or not p.valid
		return
	end
	
	if p.powers[pw_carry] == CR_NIGHTSMODE
		return
	end
	
	local takis = p.takistable
	local me = p.mo
	
	if skins[p.skin].name == "sonki"
		
		drawface(v,p)
		
	end
end)