--------------------- Polite Notice ---------------------
-- YaLB was created by Daniel Astbury (jnz)            --
-- The source is avaliable to anyone, however          --
-- please do not take it can claim it yours.           --
-- Please follow my example and release your own.      --
-----------------------Thank you-------------------------


function GetWords(str)
	local ret = {}
	for v in string.gmatch(str, "[^%s]+") do 
		table.insert(ret, v)		
	end
	return ret
end

function GetLines(str)
	local ret = {}
	for v in string.gmatch(str, "[^\n]+") do 
		table.insert(ret, v)		
	end
	return ret
end


function Get_TeamIRCColour(Team)
	local teamColour = 00
	if Team == 1 then
		teamColour = 08
	elseif Team == 0 then
		teamColour = 04
	end
	return teamColour
end

function HPPercent(Object)
	return ((Get_Health(Object) + Get_Shield_Strength(Object)) / (Get_Max_Health(Object) + Get_Max_Shield_Strength(Object)))*100
end

function PPage(PlayerId, ...)
	local err,str = pcall(string.format, unpack(arg))
	if err ~= true then
		error(str, 2)
	end

	if Players[PlayerId] ~= nil and Players[PlayerId].Version >= 2.3 then
		Console_Input(string.format("cmsgp %d 0,255,255 %s", PlayerId, str))
		Console_Input(string.format("sndp %d yo.wav", PlayerId))
	else
		Console_Input(string.format("ppage %d %s", PlayerId, string.format(unpack(arg))))
	end
end

function Import_Database()
	db:Query("", "", "DELETE FROM IpCountry;")
	db:Query("", "", "BEGIN TRANSACTION;") 
	for line in io.lines("IpToCountry.csv") do 
		if string.match(line, ".") ~= "#" and line ~= "" then 
			db:Query("", "", string.format("INSERT INTO IpCountry(IPFrom, IPTO, Country) VALUES(\"%s\", \"%s\", \"%s\");", string.match(line, "\"([^\"]+)\","), string.match(line, ",\"([^\"]+)\","), string.match(line, "\"([^\"]+)\"$"))) 
		end
	end
	db:Query("", "", "COMMIT;") 
end

function LongToIP2(IP)
	local a,b,c,d = LongToIP(IP)
	return string.format("%d.%d.%d.%d", a, b, c, d)
end

function IPToLong2(IP)
	local a,b,c,d = string.match(IP, "(%d+).(%d+).(%d+).(%d+)")
	if a == nil or b == nil or c == nil or d == nil then
		return nil
	else
		return IPToLong(a, b, c, d)
	end
end

function Log(Type, Infomation)
	db:Query("", "", string.format("INSERT INTO Log VALUES('%s', '%s', %d);", string.gsub(Type, "'", "\\'"), string.gsub(Infomation, "'", "\\'"), os.time(os.date("*t"))))
end

function Console_Inputf(...)
	local err,str = pcall(string.format, unpack(arg))
	if err ~= true then
		error(str, 2)
	end
	
	Console_Input(str)
end

function Pretty_Player(Object)
	local veh = Get_Vehicle(Object)
	if veh == 0 then
		return string.format("%s (%s / %s)", Player_Name_With_Status(Get_Player_ID(Object)), Get_Translated_Preset_Name(Object), Get_Current_Translated_Weapon(Object))
	else
		return string.format("%s (%s)", Player_Name_With_Status(Get_Player_ID(Object)), Get_Translated_Preset_Name(veh))
	end
end

NoGameover_Tmp = nil
Stop_Game_Tmp = nil
Pause_Tmp = nil
function Pause_Game()
	Stop_Game_Tmp = MemoryRead(0x474CF0, 6)
	NoGameover_Tmp = MemoryRead(0x4616A0, 6)
	Pause_Tmp = MemoryRead(0x0472c95, 6)
	
	MemoryWrite(0x474CF0, {0xB8, 0x00, 0x00, 0x00, 0x00, 0xC3})
	MemoryWrite(0x4616A0, {0xB8, 0x00, 0x00, 0x00, 0x00, 0xC3})
	MemoryWrite(0x0472c95, {0x90, 0x90, 0x90, 0x90, 0x90, 0x90})
end

function Resume()
	if NoGameover_Tmp == nil or Stop_Game_Tmp == nil or Pause_Tmp == nil then
		return
	end
	MemoryWrite(0x474CF0, Stop_Game_Tmp)
	MemoryWrite(0x4616A0, NoGameover_Tmp)
	MemoryWrite(0x0472c95, Pause_Tmp)	
end

function Get_Building_Name(Building)
	local Type = Building_Type(Building)
	local Team = Get_Object_Type(Building)
	local tName = "Neutral"
	if Team == 0 then
		tName = "Nod"
	elseif Team == 1 then
		tName = "GDI"
	end
	
	if Type == 0 then
		return tName .. " Power Plant"
	elseif Type == 1 then
		if Team == 0 then
			return "Hand of Nod"
		elseif Team == 1 then
			return "Infantry Barracks"
		end
	elseif Type == 2 then
		if Team == 0 then
			return "Airstrip"
		elseif Team == 1 then
			return "War Factory"
		end
	elseif Type == 3 then
		return tName .. " Refinery"
	elseif Type == 4 then
		return tName .. " Communications Center"
	elseif Type == 5 then
		return tName .. " Repair Bay"
	elseif Type == 6 then
		return tName .. " Shrine"
	elseif Type == 7 then
		return tName .. " Helipad"
	elseif Type == 8 then
		return tName .. " Conyard"
	elseif Type == 9 then
		if Team == 0 then
			return "Obelisk"
		elseif Team == 1 then
			return "Advanced Guard Tower"
		end
	end
	
	return "None"
end

function testVar(condition, a, b)
	if condition then
		return a
	end
	return b
end