//
--SA-Sonic is a team effort between Metalwario64 (Spriter) and Golden Shine (Gameplay & Lua)
--Any comments in these scripts are written from Golden Shine's perspective.
--
--Special shoutouts to SMS Alfredo for his SA2 Speed Up mix, and Icezer for providing higher quality SA1 voicelines.
--Thank you to everyone who came to beta test and give feedback across this long journey!
//

--Custom Objects
freeslot("MT_YUWINDLINE", "MT_YUJUMPTRAIL", "MT_YUSPINTRAIL", "MT_YUJUMPBALL", "MT_YUINSTASHIELD", "MT_YULEDGEGRAB", "MT_YULDORB", "MT_YUDUMMYTRACKER",
"MT_YUTHOK", "MT_YUTHOKDISP", "MT_YUECHECK", "MT_YUVAULT", "MT_POWERSHARE")
--Aura stuff
freeslot("MT_YUAURAGOLD", "MT_YUAURARUN", "MT_YUAURABLUEBOLT", "MT_YUAURAFLY", "MT_YUAURAFLYD", "MT_YUAURASA2")
--Sounds
freeslot("sfx_yuvoc1", "sfx_yuvoc2", "sfx_yuvoc3", "sfx_yuvoc4", "sfx_yuvoc5", "sfx_yuvoc7", "sfx_yuvoc9", "sfx_yuvocb", "sfx_yuvocc", "sfx_yuvocd", "sfx_yuvoce", "sfx_yuvocf",
"sfx_yusfx1", "sfx_yusfx2", "sfx_yusfx3", "sfx_yusfx4", "sfx_yusfx5", "sfx_yusfx6", "sfx_yusfx7", "sfx_yusfx8", "sfx_yusfx9", "sfx_yusfxa", "sfx_yusfxb", "sfx_yusfxc", "sfx_yusfxd", "sfx_yusfxe", "sfx_yusfxf", "sfx_yusfxg", 
"sfx_yuvocg", "sfx_yuvoch", "sfx_yuvoci", "sfx_yuvocj", "sfx_yuvock", "sfx_yuvocl", "sfx_yuvocm", "sfx_yuvocn", "sfx_yuvoco", "sfx_yuvocp", "sfx_yuvocq", "sfx_yuvocr", "sfx_yuvocs", "sfx_yuvoct", "sfx_yuvocu", "sfx_yuvocv", "sfx_yuvocw")
 

 
--PS: We refer to Adventure Sonic as "YU-Sonic" in this lua, our project's codename. Yu = Yuji Uekawa.

--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.

rawset(_G, "SetupYuSonic", function(p)

	--Revert a rare whoopsie that could occur if you sigsev during an LSA/Lightdash.
	if consoleplayer and CV_FindVar("cam_speed").string == "0.03999"
		COM_BufInsertText(p, "cam_speed 0.3")
	end
	
	
	p.yuemeraldcolors2={SKINCOLOR_SUPERSILVER1,SKINCOLOR_SUPERSKY1,SKINCOLOR_SUPERRED1, SKINCOLOR_SUPERPURPLE1, SKINCOLOR_SUPERGOLD1, SKINCOLOR_SUPERPERIDOT1, SKINCOLOR_SUPERSKY1}
	p.yuemeraldcolors4={SKINCOLOR_YUEMERALD1,SKINCOLOR_YUEMERALD1,SKINCOLOR_YUEMERALD2,SKINCOLOR_YUEMERALD2,SKINCOLOR_YUEMERALD3,SKINCOLOR_YUEMERALD3,
	SKINCOLOR_YUEMERALD4,SKINCOLOR_YUEMERALD4, SKINCOLOR_YUEMERALD5,SKINCOLOR_YUEMERALD5, SKINCOLOR_YUEMERALD6,SKINCOLOR_YUEMERALD6, SKINCOLOR_YUEMERALD7,SKINCOLOR_YUEMERALD7}
	
	--Create the big table! Everything's that's fine starting off as nil is NOT listed here!
	
p.yusonictable = {
startdash = 0,
spincharge = 12*FRACUNIT,
setupspinsfx = 23, --Start of spin sound timer, transitions into yusfx2
setupspinsfx2 = 27,
yspinstretch = 100,
xspinstretch = 100,
spinanglecooldown = 0,
forceyuangle = 0,
forceyuangletimer = 0,
direct = 0,
turnang = 0,
frictionspeedtreshold = 0,
frictionlock = 0,
brakeanim = 0,
airborne = 0,
nightscharge = 0,
flyswitchdisabled = false,
ret2transform = 0,
presupertransformangle = 0,
supersonic = false,
hypersonic = false,
hypercolor = 1,
hypercolor2 = 2,
hyperpower = false,
superoffset = 0,
superoffsetd = 1,
superflow = 0,
superaura = 0,
superaurabuild = 0,
superauraframe = A,
superidleframe = A,
superidletics = 7,
superflyrun = 0,
supertrick = 0,
superflash = 0,
superflashd = 1,
advancesuper = SKINCOLOR_ADVANCESUPERSONIC,
edgenope = 0,
nosarun4 = 0,
ledgeprotectanim = A,
ledgeprotectcd = 0,
ledgegrabcooldown = 0,
advance2trail = 0,
spindashfriction = false,
groundfriction = 59392,
runfriction = 59392,
frictionreset = 180,
alteredscale = false,
autojumproll = 0,
lsacheck = 0,
bonkeddash = 0,
lsadash = 0, --The LSA forwards burst dash.
lsahoming = 0, --Note this applies to the Lightdash AND the LSA.
lsaattack = 0,
lsaattackchain = 0,
hadnotarget = 0,
lsatrickanim = 0,
lsatrickrollang = 0,
lsadeadjump = 0,
speedup = 0,
lsadashlastangle = 0,
lightdashaftereffects = 0,
lightdashtimer = 0,
vertstylecharge = 0,
tauntstate = 0,
bosskiller = 0,
lastx = 0,
lasty = 0,
lastz = 0,
prevringx = 0,
prevringy = 0,
prevringz = 0,
airleverage = 0,
savedspeed = 0,
savedspeedtimer = 0,
lightdashing = false,
lsalimit = 0, --For CBW_Chaos gamemode
lsacharged = false, --Whether you're currently carrying around a Lightdash charge to be unleashed
lsacharge = 0,
lsaangle = 90, 
lsazmom = 0,
//lsaorbangle = ANGLE_180, --Direction the orb will travel.
lsaglowd = 1, --The "direction" of the glow. 1 is up, -1 is down. Also determines deathroll direction.
lsaglow = 250, --The added pulse size of the LSA glow.
setuplsasfx = 38, --Start of LightSpeed Attack sound timer, transitions into yusfx5
setuplsasfx2 = 27,
didmanuallsa = 1,
otherchar = false,
bouncestate = 0,
bouncefloor = 0,
firstbounce = false,
supereyes = 0,
superbounceprep = 0,
superbouncetime = 20,
bouncexstretch = 100,
bounceystretch = 100,
maxcrush = 0,
huggroan = 0,
skipgroundcheck = 0,
painrecovery = 0, 
falltransition = 0,
framejump = 0,
prehomecancel = 3,
prehomeangle = 0,
prehomez = 0,
farthok = 0,
forcebuilder = 0,
noairdrag = 0,
noairdrag2 = 0,
nospindrag = 0,
spindashhover = 42,
lastslopeunstuck = 0,
airstart = 0,
noubercontrol = 0,
hitspring = 0,
homingrecurl = 0,
lasttrick = 0,
homingtrick = 0,
htrickroll = 0,
chosentrick = 0,
homingresist = 0,
homingsetup = false,
hominganim = 4,
hominganim2 = 4,
hominganim3 = 4,
hominganimstart = 0,
spinanimspeed = 0,
dramaticdeath = false,
ballspinonly = true,
sa2jumpdone = false,
yuflagstats = false, 
yuvoice = true,
yumusic = true,
watertransition = false,
spinguideline = true, 
spindashcontrol = 2,
shoes = 1, --2 if you have speedshoes, 1 otherwise.
jump = 0,
spin = 0,
tossflag = 0,
custom2 = 0,
custom3 = 0 
}

CONS_Printf(p, "\n"+"\x84"+"Adventure Sonic\x80 [SRB2 2.2.9+]"+"\n"+"All sprites by\x89 Metalwario64\x80"+"!"+" Support his onlyfans at\x8d bit.ly/OnlyWarioFans64")
CONS_Printf(p, "Lua and gameplay designed by\x82 Golden Shine"+"\n")
	
	return true
end)

addHook("GameQuit", function()
	if consoleplayer
		if CV_FindVar("cam_speed").string == "0.03999"
			if consoleplayer.yusonictable and consoleplayer.yusonictable.lsacamspeed
			COM_BufInsertText(p, "cam_speed "+yu.lsacamspeed) --Restore OG settings.
			else
			COM_BufInsertText(consoleplayer, "cam_speed 0.3") --We never got any? Pick 0.3 to be sure, then.
			end
		end
		if consoleplayer.yusonictable and consoleplayer.spawnmemories --You've just unlocked...?!
			SAsaveorload(p, true, false)
		end
	end
end)

--The Golden Shield, granted for free by Super Sonic if you've no other shield! 
--Protects against ALL elements, halves superform ring drain, and negates death pits once.
--Doesn't prevent drowning or grant abilities.

local SH_YUSUPERSHIELD = 37|SH_PROTECTFIRE|SH_PROTECTSPIKE|SH_PROTECTWATER
rawset(_G, "SH_YUSUPERSHIELD", SH_YUSUPERSHIELD)

sfxinfo[sfx_yuvoc7] = {flags = SF_X2AWAYSOUND,}
sfxinfo[sfx_yuvocc] = {flags = SF_X4AWAYSOUND,}
sfxinfo[sfx_yuvocd] = {flags = SF_X2AWAYSOUND,}
sfxinfo[sfx_yusfx6] = {flags = SF_X2AWAYSOUND,}
sfxinfo[sfx_yusfx7] = {flags = SF_X4AWAYSOUND,}
sfxinfo[sfx_yusfxa] = {flags = SF_X2AWAYSOUND,}

--VOC are the vocal sounds.
sfxinfo[sfx_yuvoc1].caption = "HUT!"
sfxinfo[sfx_yuvoc2].caption = "Grabbing a Ledge"
sfxinfo[sfx_yuvoc3].caption = "Oops...!"
sfxinfo[sfx_yuvoc4].caption = "Readyyy!!"
sfxinfo[sfx_yuvoc5].caption = "GO!!"
sfxinfo[sfx_yuvoc7].caption = "Hyaahh!!"
sfxinfo[sfx_yuvoc9].caption = "Too cool to fall off ledges"
sfxinfo[sfx_yuvocb].caption = "Request to play denied."
sfxinfo[sfx_yuvocc].caption = "WHHHAOOOOOOOOoooooOOOOHHH!!!"
sfxinfo[sfx_yuvocd].caption = "Hang on!"
sfxinfo[sfx_yuvoce].caption = "HUUUAAAAOOHHHHH!!!"
sfxinfo[sfx_yuvocf].caption = "G-guh..."

sfxinfo[sfx_yuvocs].caption = "YAHH-HO!!"
sfxinfo[sfx_yuvoct].caption = "Unfgh!"

--Otherwise, the custom sounds!
sfxinfo[sfx_yusfx1].caption = "Starting Spin"
sfxinfo[sfx_yusfx2].caption = "Holding Charge..."
sfxinfo[sfx_yusfx3].caption = "Tactical Ricochet Manoeuvre"
sfxinfo[sfx_yusfx4].caption = "SA1 Jump"
sfxinfo[sfx_yusfx5].caption = "Lightspeed Attack!"
sfxinfo[sfx_yusfx6].caption = "Lightspeed Attack 2! The Sequel"
sfxinfo[sfx_yusfx7].caption = "Super Sonic"
sfxinfo[sfx_yusfx8].caption = "Wish granted"
sfxinfo[sfx_yusfx9].caption = "Chaos Emerges!"
sfxinfo[sfx_yusfxa].caption = "Energy Ripple"
sfxinfo[sfx_yusfxb].caption = "Super Flight"
sfxinfo[sfx_yusfxc].caption = "Cutting Wind Resistance"
sfxinfo[sfx_yusfxd].caption = "Adventure Menu"
sfxinfo[sfx_yusfxe].caption = "Adventure Menu"
sfxinfo[sfx_yusfxf].caption = "Adventure Menu"
sfxinfo[sfx_yusfxg].caption = "SA1 Spin"

--Taunt sounds! Also falls under VOC
sfxinfo[sfx_yuvocg].caption = "Let's move it!!"
sfxinfo[sfx_yuvoch].caption = "Ya made it!"
sfxinfo[sfx_yuvoci].caption = "Yeeeah right!"
sfxinfo[sfx_yuvocj].caption = "What's up?"
sfxinfo[sfx_yuvock].caption = "Don't worry, buddy."
sfxinfo[sfx_yuvocl].caption = "What's up, faker!"
sfxinfo[sfx_yuvocm].caption = "I found you!"
sfxinfo[sfx_yuvocn].caption = "Yyyyeeesss!!"
sfxinfo[sfx_yuvoco].caption = "Ready to surrender?"
sfxinfo[sfx_yuvocp].caption = "Do you know who I am?!"
sfxinfo[sfx_yuvocq].caption = "It's no use."
sfxinfo[sfx_yuvocr].caption = "Alright!"
sfxinfo[sfx_yuvocu].caption = "Lemme see your true power!"
sfxinfo[sfx_yuvocv].caption = "Uuuuggghhhhh..."
sfxinfo[sfx_yuvocw].caption = "Why not have a lil' fun?"

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

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

--Add objects that aren't neccessarily SA-Sonic's but need to exist for checks to work. Don't add if they already exist.
local function SingleFreeSlot(...)
	for _,slot in ipairs({...})
		if not rawget(_G, slot) freeslot(slot) end
	end
end
SingleFreeSlot("MT_ICEPLATFORMSEGMENT","MT_ICEPLATFORMTWO", "MT_EMERALD8", "MT_MASTEREMERALD")

--Make these objects ledgegrab-able upon spawn.
local function LedgeGrabAble(s)
	s.saledgegrab = true
end
addHook("MobjSpawn", LedgeGrabAble, MT_BIGGARGOYLE)
addHook("MobjSpawn", LedgeGrabAble, MT_EGGSTATUE)
addHook("MobjSpawn", LedgeGrabAble, MT_ICEPLATFORMSEGMENT)
addHook("MobjSpawn", LedgeGrabAble, MT_ICEPLATFORMTWO)
//addHook("MobjSpawn", LedgeGrabAble, MT_RYDER_ROCKBLOCK)

addHook("MobjSpawn", LedgeGrabAble, MT_PINETREE) --All the trees!
for d = MT_GFZTREE,MT_SPRINGTREE
	addHook("MobjSpawn", LedgeGrabAble, d)
end