--╔════════════════════════════╗══════════════════════════════════════════════════════════════════════════════════════════════#
--║        Initial Lua         ║ X Sonic is a team effort between BlueBlur (Spriter) and Golden Shine (Gameplay & Lua)
--║           Setup            ║ All comments are written by Golden Shine. Except if it's a slur. Then Blur did it. :)
--╚════════════════════════════╝══════════════════════════════════════════════════════════════════════════════════════════════#

--Let's make this table to store functions for organization reasons. 
--That also makes it less likely other mods will overwrite our rawsets.

rawset(_G,"XSon",{}) --Global X Sonic table for holding functions.
rawset(_G, "XS_FAKEEMERALDS", 0) --Progress for Fake Emeralds.
rawset(_G, "XS_EGGSTHISMAP", 0) --Eggs obtained in this map.
rawset(_G, "XS_KILLEDFOES", 0) --Enemies killed in map so far.
rawset(_G, "XS_TENKAICHIMUSIC", 0) --Which song to play in Super Sonic Tenkaichi
rawset(_G, "XS_CAMSPEED", "0.3") --This doesn't sync on purpose. Keeps track of camera speed.
rawset(_G, "XS_NOPVP", 0) --Not allowed to do PvP? If set to 2, you can do Free-For-Alls.
rawset(_G, "XS_CRASHTABLE", {}) --Table of items X Sonic can destroy. I made it global so mods can add their objects to it.

rawset(_G, "XS_CRAPPYPRIORITIES", 0) --Rollangle-removing memory leak measure that seriously should NOT have been necessary.

--Here we set up one big table, both to prevent conflicts and for easy naming later on. 
--Some variables are created later in the middle of gameplay, not everything is listed here.
XSon.Setup = function(p)
	if p==consoleplayer
		--Revert a rare whoopsie that could occur if you sigsev during a Zipdash Leap or cinematic move.
		if CV_FindVar("cam_speed").string == "0.03997"
			if (XS_CAMSPEED!=nil)
				COM_BufInsertText(p, "cam_speed "+tostring(XS_CAMSPEED))
			else
				COM_BufInsertText(p, "cam_speed 0.3")
			end
		end	
		local CAM = CV_FindVar("cam_speed").string
		if CAM != "0.03997"
			XS_CAMSPEED = CAM
		end
	end

	p.xboomtable = {} --Seperate table keeping track of Sonic Booms effects so there can only be so many on-screen at once.
	if p.xsonictable==nil or (type(p.xsonictable) != "table")
		p.xsonictable = { --The big "XS" table! Everything's that's OK starting as nil is not listed here!
			voice = true,
			coopPvP = true,
			zipdashangle = p.drawangle,
			tutorials = "all",
			HUDpref = true, --HUD preference setting
			direct = p.cmd.forwardmove or 0,
			turnang = p.mo and p.mo.angle or p.drawangle or 0,
			zipdashslide = 0,
			zipcd = 0,
			zipdash = 0,
			zipdashleap = 0,
			zipdashleappower = 12,
			zipdashleapdist = 0,
			leapaftermath = 0,
			justleaped = 0,
			zipdashadd = 0,
			zipdashminus = 0,
			parkourstress = 0,
			snapdive = 0,
			snapdivetrick = 0,
			snapdiveroll = 0,
			snapdivekick = 0,
			skydive = 0,
			rollcd = 0, --Not rollangle related. It's the landing roll cd.
			fallbuffer = 0, --Jump buffer.
			fallbuffer2 = 0, --Spin buffer
			rolltimer = 0, --Snap Dive sideroll timer.
			rolldirection = 1, --Rollangle Direction
			rollangle = 0, --For Snap Dive/Pain anim.
			destroll = 0,
			rollspeed = ANG2,
			bonked = 0,
			bonkeddash = 0,
			ringburstcd = 0,
			ringburstcharge = 0,
			wallbounce = 0,
			runfriction = 59392, --Default SRB2 friction.
			ledgeprotectcd = 0,
			edgeanimcd = 0,
			framejump = 0,
			frictionspeedtreshold = 0,
			falltransition = 0,
			painrecovery = 0,
			airborne = 0,
			jumpboost = 0,
			uwdebuff = 0,
			poutysonic = 0,
			poutyanim = 0,

			lastx = 0, --Check over these again before release, not all will be necessary.
			lasty = 0,
			lastz = 0,
		--	lastmomx = 0,
		--	lastmomy = 0,
			lastmomz = 0,
			lastangle = 0,
		--	lastdrawangle = 0,
			
			darkphase = 0,
			jump = (p.cmd.buttons & BT_JUMP) and 1 or 0,
			spin = (p.cmd.buttons & BT_SPIN) and 1 or 0,
			custom1 = (p.cmd.buttons & BT_CUSTOM1) and 1 or 0,
			custom2 = 0,
			custom3 = 0,
			tossflag = 0,
			weaponnext = 0,
			weaponprev = 0
		}
	end

	CONS_Printf(p, "\n"+"\x8b"+"X Sonic\x80 [SRB2 2.2.13+]"+"\n"+"sprites by\x84 Blue Blur\x80"+"!")
	CONS_Printf(p, "Lua, SFX, GFX, and gameplay by\x82 Golden Shine"+"\n")	
	return true --
end

--Prevent a potential bug!
addHook("GameQuit", function()
	if consoleplayer
		if CV_FindVar("cam_speed").string == "0.03997"
			if (XS_CAMSPEED!=nil)
				COM_BufInsertText(consoleplayer, "cam_speed "+tostring(XS_CAMSPEED))
			else
				COM_BufInsertText(consoleplayer, "cam_speed 0.3")
			end
		end
	end
end)

/*--Potential X Sonic support for a jetski, I guess?
if not boatchars
	rawset(_G, "boatchars", {})
end
boatchars["xsonic"] = {0, 0, 0}*/

--Kirby gets wheel from X-Sonic
if not (kirbyabilitytable)
	rawset(_G, "kirbyabilitytable", {})
end
kirbyabilitytable["xsonic"] = 8
kirbyabilitytable["xsonicserious"] = 8

--Add footstep support!
if not PlayerAnimInfo
	rawset(_G, "PlayerAnimInfo", {})
end
PlayerAnimInfo["xsonic"] = {
["runFrames"] = {0,5},["run"] = true,
["walkFrames"] = {0,5},["walk"] = true}
PlayerAnimInfo["xsonicserious"] = {
["runFrames"] = {0,5},["run"] = true,
["walkFrames"] = {0,5},["walk"] = true}