-- EASING --
local path = GAMESTATE:GetCurrentSong():GetSongDir()..'lua/'
loadfile(path..'easing.lua')()

checked = false

local linear, inQuad, outQuad, inOutQuad, outInQuad, inCubic, outCubic, inOutCubic, outInCubic, inQuart,outQuart, inOutQuart, outInQuart, inQuint, outQuint, inOutQuint, outInQuint, inSine, outSine, inOutSine,outInSine, inExpo, outExpo, inOutExpo, outInExpo, inCirc, outCirc, inOutCirc, outInCirc, inElastic,outElastic, inOutElastic, outInElastic, inBack, outBack, inOutBack, outInBack, inBounce, outBounce,inOutBounce, outInBounce = ease.linear, ease.inQuad, ease.outQuad, ease.inOutQuad, ease.outInQuad,ease.inCubic, ease.outCubic, ease.inOutCubic, ease.outInCubic, ease.inQuart, ease.outQuart,ease.inOutQuart, ease.outInQuart, ease.inQuint, ease.outQuint, ease.inOutQuint, ease.outInQuint,ease.inSine, ease.outSine, ease.inOutSine, ease.outInSine, ease.inExpo, ease.outExpo, ease.inOutExpo,ease.outInExpo, ease.inCirc, ease.outCirc, ease.inOutCirc, ease.outInCirc, ease.inElastic,ease.outElastic, ease.inOutElastic, ease.outInElastic, ease.inBack, ease.outBack, ease.inOutBack,ease.outInBack, ease.inBounce, ease.outBounce, ease.inOutBounce, ease.outInBounce

	sw = SCREEN_WIDTH
	sh = SCREEN_HEIGHT --shortcuts
	
	scx = sw/2
	scy = sh/2
	
local po = {GAMESTATE:GetPlayerState(0):GetPlayerOptions'ModsLevel_Song', GAMESTATE:GetPlayerState(1):GetPlayerOptions'ModsLevel_Song'}
function mod_do(str,pn)
	--if pn then if GAMESTATE:IsPlayerEnabled(pn-1) then handle_mods(str,pn) end
	--else for pn=1,2 do if GAMESTATE:IsPlayerEnabled(pn-1) then handle_mods(str,pn) end end end
	
    if pn then
        po[pn]:FromString(str)
    else
        po[1]:FromString(str)
        po[2]:FromString(str)
    end
end

mods_numplayers = 0
local mods_diff = {0,0}
local mods_onlydiff = 0
if GAMESTATE:IsHumanPlayer(PLAYER_1) then
	local stepsr = (GAMESTATE:GetCurrentSteps(PLAYER_1):GetDifficulty())
	if stepsr == "Difficulty_Beginner" then mods_diff[1] = 0 end
	if stepsr == "Difficulty_Easy" then mods_diff[1] = 1 end
	if stepsr == "Difficulty_Medium" then mods_diff[1] = 2 end
	if stepsr == "Difficulty_Hard" then mods_diff[1] = 3 end
	if stepsr == "Difficulty_Challenge" then mods_diff[1] = 4 end
	mods_onlydiff = mods_diff[1]
	mods_numplayers = mods_numplayers+1
end
if GAMESTATE:IsHumanPlayer(PLAYER_2) then
	local stepsr = (GAMESTATE:GetCurrentSteps(PLAYER_2):GetDifficulty())
	if stepsr == "Difficulty_Beginner" then mods_diff[2] = 0 end
	if stepsr == "Difficulty_Easy" then mods_diff[2] = 1 end
	if stepsr == "Difficulty_Medium" then mods_diff[2] = 2 end
	if stepsr == "Difficulty_Hard" then mods_diff[2] = 3 end
	if stepsr == "Difficulty_Challenge" then mods_diff[2] = 4 end
	mods_onlydiff = mods_diff[2]
	mods_numplayers = mods_numplayers+1
end

print("tech num players: "..mods_numplayers)

mes, ma, ca, mfsb = {}, {}, 1, GAMESTATE:GetSongBeat();
local l, e, me, m2 = 'len', 'end', function (t)
	local beat,len,str1,str2,mod,ease,sus,opt1,opt2 = unpack(t)
	for pn=1,2 do
		if not t.min and not t.only or t.only and mods_diff[pn] == t.only or t.min and mods_diff[pn] >= t.min then
			if not t.plr or pn == t.plr then
				table.insert(mes,{beat,len,str1,str2,mod,'len',ease,pn,sus,opt1,opt2})
			end
		end
	end
end, function (beat,fun,per) table.insert(ma,{beat,fun,per}) end












-- STUFF

if GAMESTATE:GetCurrentStyle():GetName() ~= 'double' then
	
else
	--can do different things if we are playing on double loll
end

--m2 = RUN FUNCTION AT BEAT

if not GAMESTATE:InStepEditor() then
	m2(238,function()
		--if 1 player and the only selected diff is 3 (hard)
		if mods_numplayers == 1 and mods_onlydiff == 3 then
			SCREENMAN:GetTopScreen():PostScreenMessage("SM_NotesEnded",0)
		end
		
		--if 2 players and both players selected diff 3 (hard)
		if mods_numplayers == 2 and mods_diff[1] == 3 and mods_diff[2] == 3 then
			SCREENMAN:GetTopScreen():PostScreenMessage("SM_NotesEnded",0)
		end
	end)
end













function cmp(a,b)
	return a[1] < b[1]
end
if table.getn(ma) > 1 then
	table.sort(ma, cmp)
end

local ac = Def.ActorFrame{
	
	Def.Actor{
		OnCommand=function(s)
			s:queuecommand('Loop')
		end,
		LoopCommand=function(self)
			local beat = GAMESTATE:GetSongBeat()
			if beat > mfsb+0.1 then
				
				if not checked then
					mods_plr = {}
					mods_anyplayers = false
					mods_firstplayer = -1
					for pn=1,2 do
						if SCREENMAN:GetTopScreen():GetChild('PlayerP'..pn) then
							mods_plr[pn] = SCREENMAN:GetTopScreen():GetChild('PlayerP'..pn)
							mods_anyplayers = true
							if mods_firstplayer == -1 then
								mods_firstplayer = pn
							end
						end
					end
					
					checked = true
				end
				
				for i,v in pairs(mes) do
					if beat >=v[1] then
						if (v[6] == 'len' and beat <=v[1]+v[2]) or (v[6] == 'end' and beat <=v[2]) then
							local duration, curtime, diff, startstrength, curve, mod = v[2], beat - v[1], v[4] - v[3], v[3], v[7], v[5]
							if v[6] == 'end' then duration = v[2] - v[1] end
							local strength = curve(curtime, startstrength, diff, duration, v[10], v[11])
							local modstr = v[5] == 'xmod' and strength..'x' or (v[5] == 'cmod' and 'C'..strength or strength..' '..v[5])
							mod_do('*10000 '..modstr,v[8])
						elseif (v[9] and ((v[6] == 'len' and beat <=v[1]+v[2]+v[9]) or (v[6] == 'end' and beat <=v[9]))) then
							local strength, modstr = v[4], v[5] == 'xmod' and strength..'x' or (v[5] == 'cmod' and 'C'..strength or strength..' '..v[5])
							mod_do('*10000 '..modstr,v[8])
						end
					end
				end
				
				while ca<=table.getn(ma) and GAMESTATE:GetSongBeat()>=ma[ca][1] do
					if ma[ca][3] or GAMESTATE:GetSongBeat() < ma[ca][1]+2 then
						ma[ca][2]()
					end
					ca = ca+1;
				end
			end
			self:sleep(1/60) self:queuecommand('Loop')
		end
	},

	Def.Quad{ InitCommand=cmd(visible,false), OnCommand=cmd(sleep,9E9) },
	
}
return ac