--[[

PROGRAM NAME:	KIMMY SCRIPT ENGINE
VERSION:		4.0
AUTHOR:			KARIS (2020/2024)

This file is part of KIMMY SCRIPT ENGINE.

	KIMMY SCRIPT ENGINE is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation.

	KIMMY SCRIPT ENGINE is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	Thanks to Matthew P. Ownby, Scott Duensing, RDG, Bladescater, DirtBagXon.

]]--

sAlph = nil; 
iCursor = 0
sName = "";
highscore = nil;

joySecs = 0
joyLimit = 0
joylastSeconds = 0
joythisSeconds = 0

function joyDelayOFF()

	joySecs = 0
	joyLimit = 0

end

function joyDelayON(thisLong)

	joySecs = 0
	joyLimit = thisLong
	joylastSeconds = os.clock()	

end

function joyDelayDue()
	
	joythisSeconds = os.clock()
	
	if bPause then
		
		joylastSeconds = joythisSeconds 
		
	else
		
		if (joythisSeconds ~= joylastSeconds) then		
			
			joySecs = joySecs + joythisSeconds - joylastSeconds
			joylastSeconds = joythisSeconds 		

		end
		
		if (joySecs >= joyLimit) then

			joyDelayOFF()
			return true
			
		else		
			
			return false
		
		end	
	
	end

end

function readScore()

	local s0 = ""
	local s1
	local hsName = ""
	local hsScore = ""
	local hsDiff = ""
	local hsNameLB = ""
	local hsScoreLB = ""	
	local hsNameS = ""
	local hsScoreS = ""	
	local k = 0

	if altCfg == true then

		s0 = MYDIR .."/Cfg/hscore2.cfg"

	else

		s0 = MYDIR .."/Cfg/hscore.cfg"

	end		

	local h1 = io.input(s0)

	highscore = nil; highscore = {}
	for k=1,10 do
		s1 = io.read("*line")
		
		hsName = string.sub(s1,1,string.find(s1,",")-1)
		hsScore = string.sub(s1,string.find(s1,",")+1, string.len(s1))
		if k==1 then iTopN = tonumber(hsScore) end
		
		highscore[k] = {}
		highscore[k] = {hsName, hsScore}
	
	end	

	s1 = io.read("*line")
	highscoreLB = nil; highscoreLB = {}
	for k=1,10 do
		s1 = io.read("*line")
		
		hsNameLB = string.sub(s1,1,string.find(s1,",")-1)
		hsScoreLB = string.sub(s1,string.find(s1,",")+1, string.len(s1))
		if k==1 then iTopLB = tonumber(hsScoreLB) end
		
		highscoreLB[k] = {}
		highscoreLB[k] = {hsNameLB, hsScoreLB}
	
	end	

	s1 = io.read("*line")
	highscoreS = nil; highscoreS = {}
	for k=1,10 do
		s1 = io.read("*line")
		
		hsNameS = string.sub(s1,1,string.find(s1,",")-1)
		hsScoreS = string.sub(s1,string.find(s1,",")+1, string.len(s1))
		if k==1 then iTopS = tonumber(hsScoreS) end
		
		highscoreS[k] = {}
		highscoreS[k] = {hsNameS, hsScoreS}
	
	end	

	s1 = io.read("*line")
	
	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t1 = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end	

	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t2 = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end

	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t3 = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end

	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t4 = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end

	s1 = io.read("*line")
	
	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t1l = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end	

	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t2l = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end

	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t3l = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end

	s1 = io.read("*line")
	
	if (s1 ~= nil) then
	
		t4l = tonumber(string.sub(s1,string.find(s1,"=")+1))
		
	end

	s1 = io.read("*line")
	hsDR = nil; hsDR = {}
	for k=1,4 do

		s1 = io.read("*line")
		
		if s1 ~= nil and s1 ~= "" and string.find(s1,"!") == nil then
		
			hsName = string.sub(s1,1,string.find(s1,",")-1)
			hsScore = string.sub(s1,string.find(s1,",")+1, string.len(s1))

		else

			hsName = "KAR"
			hsScore = "100"
		
		end
					
		hsDR[k] = {}
		hsDR[k] = {hsName, hsScore}
	
	end	

	s1 = io.read("*line")
	percent = nil; percent = {}
	for k=1,finalstage do
		s1 = io.read("*line")
		
		if s1 ~= nil and s1 ~= "" then
		
			hsName = string.sub(s1,1,string.find(s1,",")-1)
			hsScore = string.sub(s1,string.find(s1,",")+1, string.find(s1,"!")-1)
			hsDiff = string.sub(s1,string.find(s1,"!")+1, string.len(s1))

		else
			
			hsName = "KAR"
			hsScore = "80"
			hsDiff = "E"

		end
					
		percent[k] = {}
		percent[k] = {hsName, hsScore, hsDiff}
	
	end	

	io.close(h1)

end

function resetScore()

	local i
	local o

	for file in lfs.dir(MYDIR.."/Cfg/") do

		if file == "defscore.cfg" then

			i = io.open(MYDIR .."/Cfg/defscore.cfg", "r")
		  	
		else

			i = io.open(BASEDIR .. "/FrameworkKimmy/Cfg/defscore.cfg", "r")
		  	
		end 

		if altCfg == true then

			o = io.open(MYDIR .."/Cfg/hscore2.cfg", "w")

		else

			o = io.open(MYDIR .."/Cfg/hscore.cfg", "w")

		end	 

	    o:write(i:read("*a"))
	    o:close()
    	i:close()

	end 
		
end

function writeScore()

	local sNewLine = "\n"
	local s0 = ""
	local s1 = ""

	if altCfg == true then

		s0 = MYDIR .."/Cfg/hscore2.cfg"

	else

		s0 = MYDIR .."/Cfg/hscore.cfg"

	end		

	local h1 = io.output(s0)

	for k=1,10 do
		
		s1 = highscore[k][1] .. "," .. highscore[k][2]
		io.write(s1 .. sNewLine)
	
	end

	io.write(sNewLine)	
	
	for k=1,10 do
		
		s1 = highscoreLB[k][1] .. "," .. highscoreLB[k][2]
		io.write(s1 .. sNewLine)
	
	end

	io.write(sNewLine)	
	
	for k=1,10 do
		
		s1 = highscoreS[k][1] .. "," .. highscoreS[k][2]
		io.write(s1 .. sNewLine)
	
	end
	
	io.write(sNewLine)	

	io.write("t1 = " .. t1 .. sNewLine)
	io.write("t2 = " .. t2 .. sNewLine)
	io.write("t3 = " .. t3 .. sNewLine)	
	io.write("t4 = " .. t4 .. sNewLine)

	io.write(sNewLine)	

	io.write("t1l = " .. t1l .. sNewLine)
	io.write("t2l = " .. t2l .. sNewLine)
	io.write("t3l = " .. t3l .. sNewLine)	
	io.write("t4l = " .. t4l .. sNewLine)

	io.write(sNewLine)
	for k=1,4 do
		
		s1 = hsDR[k][1] .. "," .. hsDR[k][2]
		io.write(s1 .. sNewLine)
	
	end

	io.write(sNewLine)

	for k=1,finalstage do
		
		s1 = percent[k][1] .. "," .. percent[k][2] .. "!" .. percent[k][3]
		io.write(s1 .. sNewLine)
	
	end

	io.close(h1)

end

function doHighScore()

	if (lvlState == lvlSetup) then
		
		bShowLives = false
		bShowScore = false
		bShowCredits = false
		bShowAction = false
		bShowNext = false
		bShowCubes = false
		bShowLCD = false
		bIgnoreJoy = false
		iCursor = 1
		sName = ""
		initArray()
		iTilt = 0
		bShowWarnTilt = false
		bShowTilt = false
		
		setupClip(offsetNewHScore, offsetNewHScoreend)		
		lvlState = branch01
		
	elseif (lvlState == branch01) then
	
		if (currentFrame == iFrameEnd or p1BUTTON1) then
		
			p1BUTTON1 = false		
			
			if iFrameEnd+1 ~= offsetEnterHScore then 

				setupClip(offsetEnterHScore, offsetEnterHScoreend)		

			else
				
				iFrameEnd = offsetEnterHScoreend

			end	


			lvlState = lvlRunning
		
		end
		
	elseif (lvlState == branch02) then
		
		if currentFrame == iFrameEnd then 

			lvlState = lvlEnd 

		else

			if dip_GameType == 1 then

				drawHSLB()

			elseif dip_GameType == 3 then

				drawHSS()

			else

				drawHS()

			end

		end

	elseif (lvlState == branch03) then
		
		if currentFrame == iFrameEnd then 

			lvlState = lvlEnd 

		else

			drawPercent()

		end
		
	elseif (lvlState == lvlRunning) then
	
		if currentFrame == iFrameEnd then	
			
			if dip_GameType == 2 then

				updatePercent(sName, math.floor(100*(iRightMv/(iRightMv+iWrongMv))))
				
				setupClip(offsetRankings, offsetRankingsend)
				
				lvlState = branch03

			elseif dip_GameType == 4 then

				updatehsDR(sName, iTotDeath+iScDeath)
				
				setupClip(offsetRankings, offsetRankingsend)
				
				lvlState = branch03

			else

				if dip_GameType == 1 then

					updateHSLB(sName, iScore)

				elseif dip_GameType == 3 then

					updateHSS(sName, iScore)	

				else

					updateHS(sName, iScore)

				end

					setupClip(offsetRankings, offsetRankingsend)

					lvlState = branch02	

			end
			
		else
		
			drawLetters()
			
			if bIgnoreJoy then 
			
				if joyDelayDue() then bIgnoreJoy = false end
				
			else
				
				checkInput()
				
			end
		
		end	
	
	elseif (lvlState == lvlEnd) then	
				
		
		writeScore()

		if dip_GameType == 2 then

			lvlState = lvlSetup
			currentLevel = levelIntro

		else

			bGOAlt = true
			lvlState = lvlSetup
			currentLevel = levelGameOver
		
		end	

		
		
	end

end

function doTrophy() -- Manage trophies

	if LvlTrophy3 ~= 0 then

		if BeatGame() then --trophy 1 (finish the game)
			
			if dip_GameType == 1 then

				if t1l < dip_Difficulty + 1 then 
				
					t1l = dip_Difficulty + 1
					writeScore()	
				
				end
			
			else

				if t1 < dip_Difficulty + 1 then 

					t1 = dip_Difficulty + 1	
					writeScore()

				end

			end	

			if BeatGameWithOneCredit() then --trophy 2 (finish the game with 1 credit)

				if dip_GameType == 1 then

					if t2l < dip_Difficulty + 1 then	

						t2l = dip_Difficulty + 1 
						writeScore()

					end
		
				else

					if t2 < dip_Difficulty + 1 then	

						t2 = dip_Difficulty + 1 
						writeScore()

					end

				end

			end

		end	

		if BeatLevelWithOneLife(LvlTrophy3) then --trophy 3 (finish level 1 without dying)

			if dip_GameType == 1 then

				if t3l < dip_Difficulty + 1 then

					t3l = dip_Difficulty + 1
					numTrophy = dip_Difficulty + 1
					writeScore()
					
				end

			else

				if t3 < dip_Difficulty + 1 then

					t3 = dip_Difficulty + 1
					numTrophy = dip_Difficulty + 1
					writeScore()
					
				end

			end

		end

		if BeatLevelWithOneLife(LvlTrophy4) then --trophy 4 (finish level 2 without dying)

			if dip_GameType == 1 then

				if t4l < dip_Difficulty + 1 then

					t4l = dip_Difficulty + 1
					numTrophy = dip_Difficulty + 1
					writeScore()
					
				end

			else

				if t4 < dip_Difficulty + 1 then

					t4 = dip_Difficulty + 1
					numTrophy = dip_Difficulty + 1
					writeScore()
					
				end

			end

		end

	end	

end

function checkInput()

	if p1LEFT then 
		iCursor = iCursor - 1
		soundPlay(sndright)
		
	elseif p1RIGHT then 
		iCursor = iCursor + 1
		soundPlay(sndright)
		
	elseif p1DOWN then 
	
		soundPlay(sndright)
		if iCursor <= 35 then
			iCursor = iCursor + 9
		
		else
		
			if     iCursor == 36 then iCursor = 44
			elseif iCursor == 34 or iCursor == 35 then iCursor = 43
			elseif iCursor == 37 then iCursor = 1
			elseif iCursor == 38 then iCursor = 2		
			elseif iCursor == 39 then iCursor = 3		
			elseif iCursor == 40 then iCursor = 4
			elseif iCursor == 41 then iCursor = 5
			elseif iCursor == 42 then iCursor = 6
			elseif iCursor == 43 then iCursor = 7
			elseif iCursor == 44 then iCursor = 9
			end			
		
		end
	
	elseif p1UP then
	
		soundPlay(sndright)
		if iCursor >= 10 and iCursor <= 42 then
			iCursor = iCursor - 9
		
		else
		
			if     iCursor == 1 then iCursor = 37
			elseif iCursor == 2 then iCursor = 38
			elseif iCursor == 3 then iCursor = 39
			elseif iCursor == 4 then iCursor = 40
			elseif iCursor == 5 then iCursor = 41
			elseif iCursor == 6 then iCursor = 42
			elseif iCursor == 7 or iCursor == 8 then iCursor = 43
			elseif iCursor == 9 then iCursor = 44
			elseif iCursor == 43 then iCursor = 34
			elseif iCursor == 44 then iCursor = 36
			end	
		
		end
		
	elseif p1BUTTON1 then
	
		if iCursor < 43 then
		
			soundPlay(sndcredit)
			
			if string.len(sName) < 3 then
			
				sName = string.format("%s%s",sName,sAlph[iCursor])
			
			end

			if string.len(sName) == 3 then
			
				iCursor = 44
			
			end
	
		
		elseif iCursor == 43 then		
			
			soundPlay(snddeath)
			if string.len(sName) > 1 then
			
				sName = string.sub(sName,1, string.len(sName)-1)
				
			else
			
				sName = ""
			
			end
			
		
		elseif iCursor == 44 then
			
			if dip_GameType == 2 then

				updatePercent(sName, math.floor(100*(iRightMv/(iRightMv+iWrongMv))))
				setupClip(offsetRankings, offsetRankingsend)
				lvlState = branch03

			elseif dip_GameType == 4 then

				updatehsDR(sName, iTotDeath+iScDeath)
				setupClip(offsetRankings, offsetRankingsend)
				lvlState = branch03

			else

				if dip_GameType == 1 then

					updateHSLB(sName, iScore)

				elseif dip_GameType == 3 then

					updateHSS(sName, iScore)
					
				else

					updateHS(sName, iScore)

				end	

				setupClip(offsetRankings, offsetRankingsend)
				lvlState = branch02	

			end
					
		end
	
	end
	
	if iCursor > 44 then iCursor = 1
	elseif iCursor < 1 then iCursor = 44 end
	
	if p1UP or p1DOWN or p1LEFT or p1RIGHT or p1BUTTON1 then
		bIgnoreJoy = true
		joyDelayON(0.200)
	end
	
	p1UP = false
	p1DOWN = false
	p1LEFT = false
	p1RIGHT = false
	p1BUTTON1 = false

end

function initArray()

	sAlph = nil; sAlph = {}
	local s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ@123456789?!#   "
	
	local k = 1
	
	for k=1,42 do
	
		sAlph[k] = string.sub(s1,k,k)
	
	end
	
	sAlph[43] = "CLR"
	sAlph[44] = "END"

end

function printLetters()

	local s1 = ""
	local k = 0	
	
	for k=1,3 do
	
		if (k <= string.len(sName)) then
		
			s1 = s1 .. string.sub(sName,k,k) .. " "
		
		else
		
			s1 = s1 .. "_ "
		
		
		end	
	
	end
	
	fontSelect(fontLCD)
	setFontColor(mycolor2)
	fontPrint(ml17,OVLH/4,s1)
	
	

end

function drawLetters()

	local k = 0
	local q = 0
	local x1 = 0
	local y1 = OVLH/3
		
		

	if ShowTitle == true then

		setFontColor(mycolor2)
		fontSelect(fontTit)
		fontPrint(mt4, LINEH*1.5, "HIGH SCORE!!!")
		fontSelect(fontGame)
		fontPrint(mg3,OVLH-LINEH*4, "PLEASE ENTER YOUR NAME")
	
	end		
		
	printLetters()		

	fontSelect(fontLCD)

	for k = 1,44 do

		x1 = (OVLW - (33*LINEW))/2 + q*LINEW*4

		if k == iCursor then 
		
			blinkTimer(0.300)
			if heartbeat then setFontColor(mycolor2) else setFontColor(mycolor1) end
		
		else 
			setFontColor(mycolor1) 
		
		end
		
		if k < 43 then

			fontPrint(x1,y1,string.sub(sAlph[k],1,1))
		
		else
		
			if k == 43 then 		
				
				fontPrint(x1-LINEW, y1, "CLR")				
				
			end
			
			if k == 44 then			
				
				fontPrint(x1+LINEW*2, y1, "END")

			end
			
		end
		
		q = q + 1

		if q > 8 then 

			y1 = y1 + LINEH*5
			q = 0

		end
	
	end

end

function drawHSAll()

	local k = 0

	if AllowNormal and AllowLifeBar and AllowSurvivor then 
	
		if ShowTitle == true then

			fontSelect(fontTit)
			setFontColor(mycolor2)
			fontPrint(mt5,LINEH*1.5, "RANKINGS")
			
		end	

		fontSelect(fontGame)

		setFontColor(mycolor2)
		fontPrint(mg4,LINEH*15, "NORMAL")
		
		if OVLW/OVLH > 1.7 then

			fontPrint(OVLW/10.58,LINEH*15, "LIFE BAR")
			fontPrint(OVLW/1.28,LINEH*15, "SURVIVOR") 
		
		else

			fontPrint(OVLW/11.9,LINEH*15, "LIFE BAR")
			fontPrint(OVLW/1.34,LINEH*15, "SURVIVOR")

		end
		
		setFontColor(mycolor1)
			
		for k = 1, 10 do
				
			if k==1 then

				setFontColor(mycolor2)
				fontPrint(OVLW/2.56, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/21.8, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
				fontPrint(OVLW/1.34, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])
			
			elseif k==10 then

				setFontColor(mycolor1)
				fontPrint(OVLW/2.73, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscore[k][2])	
				fontPrint(OVLW/43.64, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
				fontPrint(OVLW/1.37, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])

			else

				setFontColor(mycolor1)
				fontPrint(OVLW/2.64, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/29.4, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
				fontPrint(OVLW/1.36, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])
			

			end

		end

	elseif AllowNormal and AllowLifeBar and not AllowSurvivor then 

		if ShowTitle == true then

			fontSelect(fontTit)
			setFontColor(mycolor2)
			fontPrint(mt5,LINEH*1.5, "RANKINGS")
			
		end	

		fontSelect(fontGame)

		setFontColor(mycolor2)
			
		if OVLW/OVLH > 1.7 then

			fontPrint(LINEW*9+OVLW/10.58,LINEH*15, "NORMAL")
			fontPrint(OVLW/1.28-LINEW*5,LINEH*15, "LIFE BAR") 
		
		else

			fontPrint(LINEW*9+OVLW/11.9,LINEH*15, "NORMAL")
			fontPrint(OVLW/1.34-LINEW*2,LINEH*15, "LIFE BAR")

		end
		
		setFontColor(mycolor1)
			
		for k = 1, 10 do
				
			if k==1 then

				setFontColor(mycolor2)
				fontPrint(LINEW*7+OVLW/21.8, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/1.34-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
			
			elseif k==10 then

				setFontColor(mycolor1)
				fontPrint(LINEW*7+OVLW/43.64, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/1.37-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])

			else

				setFontColor(mycolor1)
				fontPrint(LINEW*7+OVLW/29.4, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/1.36-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
			
			end

		end

	elseif AllowNormal and not AllowLifeBar and AllowSurvivor then 

		if ShowTitle == true then

			fontSelect(fontTit)
			setFontColor(mycolor2)
			fontPrint(mt5,LINEH*1.5, "RANKINGS")
			
		end	

		fontSelect(fontGame)
		setFontColor(mycolor2)
			
		if OVLW/OVLH > 1.7 then

			fontPrint(LINEW*9+OVLW/10.58,LINEH*15, "NORMAL")
			fontPrint(OVLW/1.28-LINEW*8,LINEH*15, "SURVIVOR") 
		
		else

			fontPrint(LINEW*9+OVLW/11.9,LINEH*15, "NORMAL")
			fontPrint(OVLW/1.34-LINEW*5,LINEH*15, "SURVIVOR")

		end
		
		setFontColor(mycolor1)
			
		for k = 1, 10 do
				
			if k==1 then

				setFontColor(mycolor2)
				fontPrint(LINEW*7+OVLW/21.8, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/1.34-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])
			
			elseif k==10 then

				setFontColor(mycolor1)
				fontPrint(LINEW*7+OVLW/43.64, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/1.37-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])

			else

				setFontColor(mycolor1)
				fontPrint(LINEW*7+OVLW/29.4, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/1.36-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])
			
			end

		end

	elseif not AllowNormal and AllowLifeBar and AllowSurvivor then 

		if ShowTitle == true then

			fontSelect(fontTit)
			setFontColor(mycolor2)
			fontPrint(mt5,LINEH*1.5, "RANKINGS")
			
		end	

		fontSelect(fontGame)
		setFontColor(mycolor2)
			
		if OVLW/OVLH > 1.7 then

			fontPrint(LINEW*7+OVLW/10.58,LINEH*15, "LIFE BAR")
			fontPrint(OVLW/1.28-LINEW*7,LINEH*15, "SURVIVOR") 
		
		else

			fontPrint(LINEW*9+OVLW/11.9,LINEH*15, "LIFE BAR")
			fontPrint(OVLW/1.34-LINEW*5,LINEH*15, "SURVIVOR")

		end
		
		setFontColor(mycolor1)
			
		for k = 1, 10 do
				
			if k==1 then

				setFontColor(mycolor2)
				fontPrint(LINEW*7+OVLW/21.8, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
				fontPrint(OVLW/1.34-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])
			
			elseif k==10 then

				setFontColor(mycolor1)
				fontPrint(LINEW*7+OVLW/43.64, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
				fontPrint(OVLW/1.37-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])

			else

				setFontColor(mycolor1)
				fontPrint(LINEW*7+OVLW/29.4, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(LINEW*7+OVLW/13.33, OVLH/4.59 + k *LINEH*5, highscore[k][1])
				fontPrint(LINEW*7+OVLW/5.69, OVLH/4.59 + k *LINEH*5, highscore[k][2])
				fontPrint(OVLW/1.36-LINEW*7, OVLH/4.59 + k *LINEH*5, k ..".")
				fontPrint(OVLW/1.28-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
				fontPrint(OVLW/1.13-LINEW*7, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])
			
			end

		end

	elseif AllowNormal and not AllowLifeBar and not AllowSurvivor then

		drawHS()

	elseif not AllowNormal and AllowLifeBar and not AllowSurvivor then

		drawHSLB()

	elseif not AllowNormal and not AllowLifeBar and AllowSurvivor then

		drawHSS()

	elseif not AllowNormal and not AllowLifeBar and not AllowSurvivor then

		if AllowPercent or AllowDie then 

			drawPercent()

		else

		end

	end

end

function drawHS()

	local k = 0
	
	if ShowTitle == true then

		fontSelect(fontTit)
		setFontColor(mycolor2)
		fontPrint(mt5,LINEH*1.5, "RANKINGS")
		
	end		

	fontSelect(fontLCD)
	setFontColor(mycolor2)
	fontPrint(ml13,LINEH*15, "NORMAL MODE")
	setFontColor(mycolor1)
		
	for k = 1, 10 do
			
		if k==1 then

			setFontColor(mycolor2)
			fontPrint(OVLW/2.56, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscore[k][1])
			fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscore[k][2])
					
		elseif k==10 then

			setFontColor(mycolor1)
			fontPrint(OVLW/2.73, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscore[k][1])
			fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscore[k][2])	
			
		else

			setFontColor(mycolor1)
			fontPrint(OVLW/2.64, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscore[k][1])
			fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscore[k][2])
			
		end

	end

end

function drawHSLB()

	local k = 0
	
	if ShowTitle == true then

		fontSelect(fontTit)
		setFontColor(mycolor2)
		fontPrint(mt5,LINEH*1.5, "RANKINGS")
		
	end		

	fontSelect(fontLCD)
	setFontColor(mycolor2)
	fontPrint(ml14,LINEH*15, "LIFE BAR MODE")
	setFontColor(mycolor1)
	
	for k = 1, 10 do
			
		if k==1 then

			setFontColor(mycolor2)
			fontPrint(OVLW/2.56, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
			fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
					
		elseif k==10 then

			setFontColor(mycolor1)
			fontPrint(OVLW/2.73, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
			fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])	
			
		else

			setFontColor(mycolor1)
			fontPrint(OVLW/2.64, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.37, OVLH/4.59 + k *LINEH*5, highscoreLB[k][1])
			fontPrint(OVLW/1.92, OVLH/4.59 + k *LINEH*5, highscoreLB[k][2])
			
		end


	end

end

function drawHSS()

	local k = 0
	
	if ShowTitle == true then

		fontSelect(fontTit)
		setFontColor(mycolor2)
		fontPrint(mt5,LINEH*1.5, "RANKINGS")
		
	end		

	fontSelect(fontLCD)
	setFontColor(mycolor2)
	fontPrint(ml15,LINEH*15, "SURVIVOR MODE")
	setFontColor(mycolor1)
		
	for k = 1, 10 do
			
		if k==1 then

			setFontColor(mycolor2)
			fontPrint(OVLW/2.36, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.17, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
			fontPrint(OVLW/1.72, OVLH/4.59 + k *LINEH*5, highscoreS[k][2]) 
					
		elseif k==10 then

			setFontColor(mycolor1)
			fontPrint(OVLW/2.53, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.17, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
			fontPrint(OVLW/1.72, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])	
			
		else

			setFontColor(mycolor1)
			fontPrint(OVLW/2.44, OVLH/4.59 + k *LINEH*5, k ..".")
			fontPrint(OVLW/2.17, OVLH/4.59 + k *LINEH*5, highscoreS[k][1])
			fontPrint(OVLW/1.72, OVLH/4.59 + k *LINEH*5, highscoreS[k][2])
			
		end

	end

end

function drawPercent()

	local k = 0
	local j = 0
	local l = 0

	if ShowTitle == true then

			fontSelect(fontTit)
			setFontColor(mycolor2)
			fontPrint(mt5,LINEH*1.5, "RANKINGS")
			
	end		

	if not AllowPercent and not AllowDie then

		if AllowNormal or AllowLifeBar or AllowSurvivor then 

			drawHSAll()

		else

		end

	elseif AllowPercent and AllowDie then 
			
		j = 4

		if finalstage > 11 then

			j = 2

		elseif finalstage > 23 then

			j = 1.5

		elseif finalstage > 31 then

			j = 1

		end

		fontSelect(fontLCD)
		
		setFontColor(mycolor2)
		fontPrint(ml16,LINEH*15, "BEST PERCENT")
		setFontColor(mycolor1)

		fontSelect(fontGame)

		for k = 1,finalstage,4 do

			l=l+1
			
			if k + 3 < finalstage then

				
				fontPrint(mg11, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k+1][3] .. ")    L" .. k+2 .. ": " .. percent[k+2][1] .. " " .. percent[k+2][2] .. " (" .. percent[k+2][3] .. ")    L" .. k+3 .. ": " .. percent[k+3][1] .. " " .. percent[k+3][2] .. " (" .. percent[k+3][3] .. ")")
		
			else

				if k+3 == finalstage then 

					fontPrint(mg11, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k+1][3] .. ")    L" .. k+2 .. ": " .. percent[k+2][1] .. " " .. percent[k+2][2] .. " (" .. percent[k+2][3] .. ")    L" .. k+3 .. ": " .. percent[k+3][1] .. " " .. percent[k+3][2] .. " (" .. percent[k+3][3] .. ")")
					break


				elseif k+3 == finalstage+1 then 

					fontPrint(mg5, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k+1][3] .. ")    L" .. k+2 .. ": " .. percent[k+2][1] .. " " .. percent[k+2][2] .. " (" .. percent[k+2][3] .. ")")
					break

				elseif k+3 == finalstage + 2 then

					fontPrint(mg6, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3].. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k][3] .. ")")
					break

				elseif k+3 == finalstage + 3 then

					fontPrint(mg7, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")")
					break

				end 
						
			end
					
		end

		l = l+1.5

		fontSelect(fontLCD)
		setFontColor(mycolor2)
		fontPrint(ml25,OVLH/5 + l *LINEH*5, "DIE AND RETRY DIFFICULTY CHAMPS")

		l = l +1.3

		fontSelect(fontGame)
		setFontColor(mycolor1)
		fontPrint(mg8, OVLH/5 + l *LINEH*5, "Easy: " .. hsDR[1][1] .. " " .. hsDR[1][2] .. "   Norm: " .. hsDR[2][1] .. " " .. hsDR[2][2] .. "   Diff: " .. hsDR[3][1] .. " " .. hsDR[3][2] .. "   Ext: " .. hsDR[4][1] .. " " .. hsDR[4][2])
	
	elseif AllowPercent and not AllowDie then

		j = 4

		if finalstage > 11 then

			j = 2

		elseif finalstage > 23 then

			j = 1.5

		elseif finalstage > 31 then

			j = 1

		end

		fontSelect(fontLCD)
		
		setFontColor(mycolor2)
		fontPrint(ml16,LINEH*15, "BEST PERCENT")
		setFontColor(mycolor1)

		fontSelect(fontGame)

		for k = 1,finalstage,4 do

			l=l+1
			
			if k + 3 < finalstage then

				
				fontPrint(mg11, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k+1][3] .. ")    L" .. k+2 .. ": " .. percent[k+2][1] .. " " .. percent[k+2][2] .. " (" .. percent[k+2][3] .. ")    L" .. k+3 .. ": " .. percent[k+3][1] .. " " .. percent[k+3][2] .. " (" .. percent[k+3][3] .. ")")
		
			else

				if k+3 == finalstage then 

					fontPrint(mg11, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k+1][3] .. ")    L" .. k+2 .. ": " .. percent[k+2][1] .. " " .. percent[k+2][2] .. " (" .. percent[k+2][3] .. ")    L" .. k+3 .. ": " .. percent[k+3][1] .. " " .. percent[k+3][2] .. " (" .. percent[k+3][3] .. ")")
					break


				elseif k+3 == finalstage+1 then 

					fontPrint(mg5, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k+1][3] .. ")    L" .. k+2 .. ": " .. percent[k+2][1] .. " " .. percent[k+2][2] .. " (" .. percent[k+2][3] .. ")")
					break

				elseif k+3 == finalstage + 2 then

					fontPrint(mg6, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3].. ")    L" .. k+1 .. ": " .. percent[k+1][1] .. " " .. percent[k+1][2] .. " (" .. percent[k][3] .. ")")
					break

				elseif k+3 == finalstage + 3 then

					fontPrint(mg7, OVLH/5 + l *LINEH*5, "L" .. k .. ": " .. percent[k][1] .. " " .. percent[k][2] .. " (" .. percent[k][3] .. ")")
					break

				end 
						
			end
					
		end

	elseif not AllowPercent and AllowDie then

		j = 4
		local q = 1

		fontSelect(fontLCD)
		setFontColor(mycolor2)
		fontPrint(ml25,OVLH/4 + j*LINEH*(q+1), "DIE AND RETRY DIFFICULTY CHAMPS")

		fontSelect(fontGame)
		setFontColor(mycolor1)
		fontPrint(mg8, OVLH/4 + (q+4)*LINEH*j, "Easy: " .. hsDR[1][1] .. " " .. hsDR[1][2] .. "   Norm: " .. hsDR[2][1] .. " " .. hsDR[2][2] .. "   Diff: " .. hsDR[3][1] .. " " .. hsDR[3][2] .. "   Ext: " .. hsDR[4][1] .. " " .. hsDR[4][2])
		
	end

end

function drawTrophy()

	local k = 0

	if LvlTrophy3 ~= 0 then
	
		if ShowTitle == true then

			fontSelect(fontTit)
			setFontColor(mycolor2)
			fontPrint(mt6,LINEH*1.5, "TROPHIES")
			
		end	

		fontSelect(fontLCD)

		setFontColor(mycolor2)
		fontPrint(ml13,OVLH-LINEH*5, "NORMAL MODE")

		setFontColor(mycolor1)		
		
		if t1 == 0 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TN])

		elseif t1 == 1 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TB])

		elseif t1 == 2 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TS])

		elseif t1 == 3 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TG])

		elseif t1 == 4 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TP])

		end


		if t2 == 0 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TN])

		elseif t2 == 1 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TB])

		elseif t2 == 2 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TS])

		elseif t2 == 3 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TG])

		elseif t2 == 4 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TP])

		end


		if t3 == 0 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TN])

		elseif t3 == 1 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TB])

		elseif t3 == 2 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TS])

		elseif t3 == 3 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TG])

		elseif t3 == 4 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TP])

		end


		if t4 == 0 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TN])

		elseif t4 == 1 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TB])

		elseif t4 == 2 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TS])

		elseif t4 == 3 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TG])

		elseif t4 == 4 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TP])

		end

	end

end

function drawTrophyLB()

	local k = 0
	
	if LvlTrophy3 ~= 0 then

		if ShowTitle == true then

			fontSelect(fontTit)
			setFontColor(mycolor2)
			fontPrint(mt6,LINEH*1.5, "TROPHIES")
			
		end	

		fontSelect(fontLCD)
		
		setFontColor(mycolor2)
		fontPrint(ml14,OVLH-LINEH*5, "LIFE BAR MODE")
				
		setFontColor(mycolor1)	
		
		if t1l == 0 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TN])

		elseif t1l == 1 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TB])

		elseif t1l == 2 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TS])

		elseif t1l == 3 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TG])

		elseif t1l == 4 then

			spriteDraw(3*OVLW/4,OVLH/4,sprite[TP])

		end


		if t2l == 0 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TN])

		elseif t2l == 1 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TB])

		elseif t2l == 2 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TS])

		elseif t2l == 3 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TG])

		elseif t2l == 4 then

			spriteDraw(3*OVLW/4,1.72*OVLH/4,sprite[TP])

		end


		if t3l == 0 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TN])

		elseif t3l == 1 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TB])

		elseif t3l == 2 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TS])

		elseif t3l == 3 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TG])

		elseif t3l == 4 then

			spriteDraw(3*OVLW/4,2.44*OVLH/4,sprite[TP])

		end


		if t4l == 0 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TN])

		elseif t4l == 1 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TB])

		elseif t4l == 2 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TS])

		elseif t4l == 3 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TG])

		elseif t4l == 4 then

			spriteDraw(3*OVLW/4,3.16*OVLH/4,sprite[TP])

		end

	end

end


function newScore(thisScore)

	local bResult = false
	local k = 0

	if dip_GameType == 4 then	

		if (tonumber(thisScore) <= tonumber(hsDR[dip_Difficulty + 1][2])) then
				
			bResult = true			
			
		end
	
	else

		for k=1,10 do
			
			if dip_GameType == 1 then	

				if (tonumber(thisScore) >= tonumber(highscoreLB[k][2])) then
					
					bResult = true			
					break
				
				end	

			elseif dip_GameType == 3 then	

				if (tonumber(thisScore) >= tonumber(highscoreS[k][2])) then
					
					bResult = true			
					break
				
				end

			else

				if (tonumber(thisScore) >= tonumber(highscore[k][2])) then
					
					bResult = true			
					break
				
				end	

			end
		
		end

	end
	
	return bResult

end

function newPercent(thisPercent)

	local bResult = false
	local bDiff = 0

	if percent[iLevel][3] == "E" then

		bDiff = 0

	elseif percent[iLevel][3] == "N" then

		bDiff = 1

	elseif percent[iLevel][3] == "H" then

		bDiff = 2

	elseif percent[iLevel][3] == "X" then

		bDiff = 3

	end

	if (tonumber(thisPercent) > tonumber(percent[iLevel][2])) or ((tonumber(thisPercent) == tonumber(percent[iLevel][2])) and dip_Difficulty >= bDiff) then
			
		bResult = true			
			
	end	

	return bResult

end


function getRandomName()

	local grabbag = {'CAL','AKI','DON','TYG','KIM','LAR','KOR','MOR','KAR','CYR'}
	local q = math.random(10)
	return grabbag[q]	

end

function updateHS(thisName, thisScore)

	local k = 0
	local tempName = ""
	local tempScore = ""
	local tempHS = nil
	local bFound = false
	local fromHere = 0
	
	if (thisName == "") then thisName = getRandomName() end
	
	tempHS = {}
	
	for k=1,10 do
	
		tempHS[k] = {}
		tempHS[k] = {highscore[k][1], highscore[k][2]}
		
	end
		
	for k=1,10 do
	
		tempName = highscore[k][1]
		tempScore = highscore[k][2]
	
		if (tonumber(thisScore) >= tonumber(highscore[k][2])) then
		
			bFound = true
			highscore[k][1] = thisName
			highscore[k][2] = thisScore
			fromHere = k
			break
		
		end
	
	
	end
	
	if bFound then
		
		for k=fromHere,9 do
		
			highscore[k+1][1] = tempHS[k][1]
			highscore[k+1][2] = tempHS[k][2]
		
		end	

	end
	
end

function updatehsDR(thisName, thisScore)
	
	hsDR[dip_Difficulty+1][1] = thisName
	hsDR[dip_Difficulty+1][2] = thisScore

end

function updateHSLB(thisName, thisScore)

	local k = 0
	local tempName = ""
	local tempScore = ""
	local tempHS = nil
	local bFound = false
	local fromHere = 0
	
	if (thisName == "") then thisName = getRandomName() end
	
	tempHS = {}
	
	for k=1,10 do
	
		tempHS[k] = {}
		tempHS[k] = {highscoreLB[k][1], highscoreLB[k][2]}
		
	end
		
	for k=1,10 do
	
		tempName = highscoreLB[k][1]
		tempScore = highscoreLB[k][2]
	
		if (tonumber(thisScore) >= tonumber(highscoreLB[k][2])) then
		
			bFound = true
			highscoreLB[k][1] = thisName
			highscoreLB[k][2] = thisScore
			fromHere = k
			break
		
		end
	
	
	end
	
	if bFound then
		
		for k=fromHere,9 do
		
			highscoreLB[k+1][1] = tempHS[k][1]
			highscoreLB[k+1][2] = tempHS[k][2]
		
		end	

	end
	
end

function updateHSS(thisName, thisScore)

	local k = 0
	local tempName = ""
	local tempScore = ""
	local tempHS = nil
	local bFound = false
	local fromHere = 0
	
	if (thisName == "") then thisName = getRandomName() end
	
	tempHS = {}
	
	for k=1,10 do
	
		tempHS[k] = {}
		tempHS[k] = {highscoreS[k][1], highscoreS[k][2]}
		
	end
		
	for k=1,10 do
	
		tempName = highscoreS[k][1]
		tempScore = highscoreS[k][2]
	
		if (tonumber(thisScore) >= tonumber(highscoreS[k][2])) then
		
			bFound = true
			highscoreS[k][1] = thisName
			highscoreS[k][2] = thisScore
			fromHere = k
			break
		
		end
	
	
	end
	
	if bFound then
		
		for k=fromHere,9 do
		
			highscoreS[k+1][1] = tempHS[k][1]
			highscoreS[k+1][2] = tempHS[k][2]
		
		end	

	end
	
end

function updatePercent(thisName, thisPercent)
	
	percent[iLevel][1] = thisName
	percent[iLevel][2] = thisPercent
	
	if dip_Difficulty == 0 then

		percent[iLevel][3] = "E"

	elseif dip_Difficulty == 1 then

		percent[iLevel][3] = "N"

	elseif dip_Difficulty == 3 then

		percent[iLevel][3] = "H"

	elseif dip_Difficulty == 4 then

		percent[iLevel][3] = "X"

	end

end
