freeslot(
"SKINCOLOR_EMPEROR1",
"SKINCOLOR_EMPEROR2",
"SKINCOLOR_EMPEROR3",
"SKINCOLOR_EMPEROR4",
"SKINCOLOR_EMPEROR5",
"SKINCOLOR_SMS1R",
"SKINCOLOR_SMS2R",
"SKINCOLOR_SMS3R",
"SKINCOLOR_SMS4R",
"SKINCOLOR_SMS5R",
"SKINCOLOR_SMS1",
"SKINCOLOR_SMS2",
"SKINCOLOR_SMS3",
"SKINCOLOR_SMS4",
"SKINCOLOR_FLAMEBREAKER1",
"SKINCOLOR_FLAMEBREAKER2",
"SKINCOLOR_FLAMEBREAKER3",
"SKINCOLOR_FLAMEBREAKER4",
"SKINCOLOR_FLAMEBREAKER5",
"SKINCOLOR_MYSTICAL1",
"SKINCOLOR_MYSTICAL2",
"SKINCOLOR_MYSTICAL3",
"SKINCOLOR_MYSTICAL4",
"SKINCOLOR_MYSTICAL5",
"SKINCOLOR_SONIC1",
"SKINCOLOR_SONIC2",
"SKINCOLOR_SONIC3",
"SKINCOLOR_SONIC4",
"SKINCOLOR_SONIC5",
"SKINCOLOR_TRANSFORMATION1",
"SKINCOLOR_TRANSFORMATION2",
"SKINCOLOR_TRANSFORMATION3",
"SKINCOLOR_TRANSFORMATION4",
"SKINCOLOR_TRANSFORMATION5",
"SKINCOLOR_ZEEJIN1",
"SKINCOLOR_ZEEJIN2",
"SKINCOLOR_ZEEJIN3",
"SKINCOLOR_ZEEJIN4",
"SKINCOLOR_ZEEJIN5",
"SKINCOLOR_ZENITH1",
"SKINCOLOR_ZENITH2",
"SKINCOLOR_ZENITH3",
"SKINCOLOR_ZENITH4",
"SKINCOLOR_ZENITH5",
"SKINCOLOR_MOONSLAMMER1",
"SKINCOLOR_MOONSLAMMER2",
"SKINCOLOR_MOONSLAMMER3",
"SKINCOLOR_MOONSLAMMER4",
"SKINCOLOR_MOONSLAMMER5",
"SKINCOLOR_BREEZY1",
"SKINCOLOR_BREEZY2",
"SKINCOLOR_BREEZY3",
"SKINCOLOR_BREEZY4",
"SKINCOLOR_BREEZY5",
"SKINCOLOR_SOLAR1",
"SKINCOLOR_SOLAR2",
"SKINCOLOR_SOLAR3",
"SKINCOLOR_SOLAR4",
"SKINCOLOR_SOLAR5",
"SKINCOLOR_GALACTIC1",
"SKINCOLOR_GALACTIC2",
"SKINCOLOR_GALACTIC3",
"SKINCOLOR_GALACTIC4",
"SKINCOLOR_GALACTIC5",
"SKINCOLOR_SMSROVERGLOW",
"SKINCOLOR_SMSROVERGLOWRED"
)

--Ridiculously long save-loading command. Practically requires copy&paste. ....that said, writing this manually rather than
--making a clever "for" loop has almost zero effect on game performance. So I'm not exactly in a rush to rewrite it.
COM_AddCommand("SMSautoconfig", function(p,a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,aswitch,a12,a13,a14,a15,a16,a17,a18,a19,a20)
if p.didsmsautoconfig == 2 
	return
end
a1 = tonumber(a1) //Turn all of you to numbers!
a2 = tonumber(a2)
a3 = tonumber(a3)
a4 = tonumber(a4)
a5 = tonumber(a5)
a6 = tonumber(a6)
a7 = tonumber(a7)
a8 = tonumber(a8)
a9 = tonumber(a9)
a10 = tonumber(a10)
a11 = tonumber(a11)
a12 = tonumber(a12)
a13 = tonumber(a13)
a14 = tonumber(a14)
a15 = tonumber(a15)
a16 = tonumber(a16)
a17 = tonumber(a17)
a18 = tonumber(a18)
a19 = tonumber(a19)
a20 = tonumber(a20)

		--=============================PERSONAL===========================================
					--First, load the player's personal settings.
		--================================================================================
		
			--What's our preferred color setting?
			if a1 == 0 or p.nosmscolorspls
				p.skincolor = SKINCOLOR_COBALT
				A_setSMSspecialcolors(p, 0)
			elseif a1 != nil and (a1 < 34)
				p.skincolor = SKINCOLOR_MYSTICAL4
				A_setSMSspecialcolors(p, a1)
			else 			
				p.shinyglosscoat = 1 //Our fallback default color. 
				p.skincolor = SKINCOLOR_MYSTICAL4
				A_setSMSspecialcolors(p, 1)
			end
			
			if a2 == 1 --Instant Air Boost
				p.sms_instantairboost = true
			else
				p.sms_instantairboost= false
			end
			
			if a3 == 1 --Old Homing means having to hold down spin to homing attack
				p.sms_oldhoming = true
			else
				p.sms_oldhoming = false
			end
			
			if a4 == 1 --Quakes turned off.
				p.nosmsquakespls = true
			else
				p.nosmsquakespls = false
			end
			
			if a5 == 1 --Simple Haymaker
				p.smseasyhaymaker = true
			else
				p.smseasyhaymaker = false
			end
			
			if a6 == 1 --Allow SMS to grab you or not?
				p.smsdontgrabme = 1
			elseif a6 == 2
				p.smsdontgrabme = 2
			else
				p.smsdontgrabme = 0
			end
			
			if a7 == 1 --On-screen move list aid
				p.smstutorial = true
			else
				p.smstutorial = false
			end
			
			if a8 == 1 --Legend Mode/Legacy Mode.
				p.smsblastboost = true
			else
				p.smsblastboost = false
			end
			
			if a9 == 1 --HMS music (and runshoes theme) turned off?
				p.nohmstheme = true
			else
				p.nohmstheme = false
			end
			
			if a10 == 1 --Newest addition, NOSMSCOLORS
				p.nosmscolorspls = 1
			else
				p.nosmscolorspls = 0
			end
			
			if a11 == 0 --Automatically upkeep momentum rather than having to hold custom1?
				p.autoc1mom = 0
			elseif a11 == 2
				p.autoc1mom = 2
			elseif a11 == 1
				p.autoc1mom = 1
			end
			
			p.didsmsautoconfig = 2
			
		--=============================ADMIN============================================
					--Next, stuff that is decided exclusively by the server.
		--==============================================================================
		
			--The rest is for the server admin only. Don't bother if you're not one.
			if SMS_Admin(p) == 0 return true end --		
			
			if a12 == 1
				p.smstransformonly = true
			else
				p.smstransformonly = false
			end
			
			if a13 == 1
				p.hmstransformdisable = 1
			elseif a13 == 2
				p.hmstransformdisable = 2
			else
				p.hmstransformdisable = 0
			end
			
			if a14 == 1
				p.powersharerestrict = 1
			elseif a14 == 2
				p.powersharerestrict = 2
			else
				p.powersharerestrict = 0
			end
			
			if a15 == 1
				p.smsgraphix = 1
			elseif a15 == 2
				p.smsgraphix = 2 
				p.smsautographix = false //It's safe to assume they only want crap graphics at this point.
			else
				p.smsgraphix = 0
			end
			
			if a16 == 1
				p.smsnoracegame = 1
			elseif a16 == 2
				p.smsnoracegame = 2
			else
				p.smsnoracegame = 0
			end
			
			if a17 == 1
				p.smsnomatchgame = 1
			else
				p.smsnomatchgame = 0
			end
			
			if a18 == 1
				p.smsnobmgame = 1
			elseif a18 == 2
				p.smsnobmgame = 2
			else
				p.smsnobmgame = 0
			end
			
			if a19 == 1
				p.smsnomsspawn = 1
			else
				p.smsnomsspawn = 0
			end
			
			if a20 and ((a20 > 1) and (a20 < 33))
				p.hmsmlimit = a20
			else
				p.hmsmlimit = 1
			end
			return true --
end)

--Load Settings
rawset(_G, "SMS_LoadGame", function(p)
	if (p == consoleplayer) and io
	local CODE,CODE2,FILE,FILE2
		FILE = io.openlocal("client/SMSreborn/SMSv3settings.dat") --Load savefile (LUA_MENU has the menu behaviour.)	
		if FILE
			CODE = FILE:read("*a")			
			if CODE ~= nil and not (string.find(CODE, ";"))
				if SMS_Admin(p) != 0
					FILE2 = io.openlocal("client/SMSreborn/SMSv3adminsettings.dat")
					if FILE2
						CODE2 = FILE2:read("*a")
						if CODE2 ~= nil
							COM_BufInsertText(p, "SMSautoconfig "+CODE+" "+CODE2)
							FILE:close()
							FILE2:close()
							return true --
						end
						FILE2:close()
					end
				end
				COM_BufInsertText(p, "SMSautoconfig "+CODE)
				FILE:close()
				return true --
			end
			file:close()
		end	
	end
end)

--Save Settings.
rawset(_G, "SMS_SaveGame", function(p)
	p.didsmsautoconfig = 2
	if io and (p == consoleplayer) --Do NOT sync this part across players.
local a1 =0 
local a2 =0 
local a3 =0 
local a4 =0 
local a5 =0 
local a6 =0 
local a7 =0 
local a8 =0 
local a9 =0 
local a10 =0
local a11 =0 
local a12 =0 
local a13 =0 
local a14 =0 
local a15 =0 
local a16= 0 
local a17 =0 
local a18 =0 
local a19 =0 
local a20 =0
			
	--Okay, let's generate this save file.
	--Since the code will be a pain to type out/remember, we'll auto-execute it when SMS loads.
	
		if p.shinyglosscoat != nil
			a1 = p.shinyglosscoat --Pref special color					
		else
			a1 = 1
		end
		
		if p.sms_instantairboost a2 = 1 end //1 = Instant Airboost
		if p.sms_oldhoming a3 = 1 end //1 = Simple Homing
		if p.nosmsquakespls a4 = 1 end //1 = No Quakes
		if p.smseasyhaymaker a5 = 1 end //1 = Simple Haymaker
		a6 = (p.smsdontgrabme) or 0 //1 is never grab, 2 is always grab, 0 is 3s
		if p.smstutorial a7 = 1 end //1 = Tutorial mode on
		if p.smsblastboost a8 = 1 end //1 == SMSblast on
		if p.nohmstheme a9 = 1 end //1 == No HMS theme playing.
		if p.nosmscolorspls a10 = 1 end //1 == Pulsing Colors are forbidden
		if p.autoc1mom a11 = p.autoc1mom else a11 = 0 end //2 = Key, 1 = Auto, 0 = C1
		
		local file = io.openlocal("client/SMSreborn/SMSv3settings.dat", "w+")
		file:write("SMSautoconfig "+a1+" "+a2+" "+a3+" "+a4+" "+a5+" "+a6+" "+a7+" "+a8+" "+a9+" "+a10+" "+a11)

			if (SMS_Admin(p) > 1)
				if p.smstransformonly a12 = 1 end //1 = SMS through transform-only
				if p.hmstransformdisable a13 = tostring(p.hmstransformdisable) else a13 = tostring(0) end //1= Chaos Emeralds only, 2= HMS off entirely, 0 = free HMS
				if p.powersharerestrict a14 = p.powersharerestrict else a14 = 0 end //1= Chaos Emeralds, 2=No powershare, 0=Free powershare.
				if p.smsgraphix a15 = p.smsgraphix else a15 = 0 end //0=Full, 1=Mid, 0=Garbage
				if p.smsnoracegame a16 = p.smsnoracegame else a16 = 0 end //0=SMSrace, 1=NoSMSrace , 2=SuperRace 
				if p.smsnomatchgame a17 = 1 end //0=SMSmatch, 1=NoSMSmatch (number)
				if p.smsnobmgame a18 = p.smsnobmgame else a18 = 0 end //2==SonicToSMSbm, 1=NoSMSbm, 0=SMSfreebm
				if p.smsnomsspawn a19 = p.smsnomsspawn else a19 = 0 end //1=NoMysticEmerald, 0=SpawnMS (number)
				if p.hmsmlimit a20 = p.hmsmlimit else a20 = 0 end //HMS limit. Number is the direct limit.
					
				local FILE2 = io.openlocal("client/SMSreborn/SMSv3adminsettings.dat", "w+")
				FILE2:write("SMSadmin "+a12+" "+a13+" "+a14+" "+a15+" "+a16+" "+a17+" "+a18+" "+a19+" "+a20)
				CONS_Printf(p, "\n")
				CONS_Printf(p, "Current game's\x82 SMSreborn\x80 and\x87 Admin\x80 settings saved.")	
				FILE2:close()
					
			else
				CONS_Printf(p, "\n")
				CONS_Printf(p, "Current game's\x82 SMSreborn\x80 settings saved.")			
			end
				
		file:close()
		P_FlashPal(p, PAL_MIXUP, 8) --succesfully saved! Make a flash and print a message.
	end
end)
