--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($) //Turn all of you to numbers! (Except aswitch and a0)
	a2 = tonumber($)
	a3 = tonumber($)
	a4 = tonumber($)
	a5 = tonumber($)
	a6 = tonumber($)
	a7 = tonumber($)
	a8 = tonumber($)
	a9 = tonumber($)
	a10 = tonumber($)
	a11 = tonumber($)
	a12 = tonumber($)
	a13 = tonumber($)
	a14 = tonumber($)
	a15 = tonumber($)
	a16 = tonumber($)
	a17 = tonumber($)
	a18 = tonumber($)
	a19 = tonumber($)
	a20 = tonumber($)

	--=============================PERSONAL===========================================
				--First, load the player's personal settings.
	--================================================================================
	
	if p.sms_originalcolor == nil and not (p.mo and p.mo.skin == "sms")
		p.sms_originalcolor = p.skincolor
	end
	
	--What's our preferred color setting?
	if a1 == 0 or p.nosmscolorspls
		if p.mo and p.mo.skin == "sms" and not p.sms_transformstate
			p.skincolor = SKINCOLOR_COBALT
		end
		A_setSMSspecialcolors(p, 0)
	else
		if p.mo and p.mo.skin == "sms" and not p.sms_transformstate
			p.skincolor = SKINCOLOR_MYSTICAL4
		end	
		A_setSMSspecialcolors(p, (a1!=nil and (a1<34)) and a1 or 1)
	end
	
	--Instant Air Boost
	p.sms_instantairboost = (a2==1)
	
	--Old Homing means having to hold down spin to homing attack
	p.sms_oldhoming = (a3==1)
	
	--Quakes turned off.
	p.nosmsquakespls = (a4==1)
	
	--Simple Haymaker
	p.smseasyhaymaker = (a5==1)
	
	--Allow SMS to grab you or not?
	p.smsdontgrabme = (a6==1) and 1 or (a6==2) and 2 or 0
	
	--On-screen move list aid
	p.smstutorial = (a7==1)
	
	--Legend Mode/Legacy Mode.
	p.smsblastboost = (a8==1)
	
	--HMS music (and runshoes theme) turned off?
	p.nohmstheme = (a9 == 1)
	
	--Newest addition, NOSMSCOLORS
	p.nosmscolorspls = (a10==1) and 1 or 0
	
	--Automatically upkeep momentum rather than having to hold custom1?
	p.autoc1mom = (a11==2) and 2 or (a11==1) and 1 or 0
	
	p.didsmsautoconfig = 2
	
	--=============================ADMIN============================================
				--Next, stuff that is decided exclusively by the server.
	--==============================================================================
	
	if SMS_Admin(p) != 0
	
		p.smstransformonly = (a12==1)
		
		p.hmstransformdisable = (a13==1) and 1 or (a13==2) and 2 or 0
		
		p.powersharerestrict = (a14==1) and 1 or (a14==2) and 2 or 0
		
		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
		
		p.smsnoracegame = (a16==1) and 1 or (a16==2) and 2 or 0
		
		p.smsnomatchgame = (a17==1) and 1 or 0
		
		p.smsnobmgame = (a18==1) and 1 or (a18==2) and 2 or 0
		
		p.smsnomsspawn = (a19==1) and 1 or 0
		
		p.hmsmlimit = (a20 and ((a20>1) and (a20<33))) and a20 or 1
	
		return true --
	end
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 (p==consoleplayer) and io --Do NOT sync this part across players.
	
		local a = {} for i = 1,20 a[i] = 0 end
		local a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20 = unpack(a)
				
		--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.	
		
		a1 = (p.shinyglosscoat == nil) and 1 or p.shinyglosscoat --Prefer special colors
		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) 