Code: Select all
function PrintTable ( t, indent, done )
done = done or {}
indent = indent or 0
for key, value in pairs (t) do
print( string.rep ("\t", indent) )
if type (value) == "table" and not done [value] then
done [value] = true
print(tostring (key) .. ":");
PrintTable (value, indent + 2, done)
else
print(tostring (key) .. "\t=\t" .. tostring(value))
end
end
end
function PrintTableSimple ( t, indent, done )
done = done or {}
indent = indent or 0
for key, value in pairs (t) do
print( string.rep ("\t", indent) )
print(tostring (key) .. "\t=\t" .. tostring(value))
end
end
function Distance(x1,y1,x2,y2)
return math.sqrt((x2 - x1)^2+(y2 - y1)^2)
end
function math.Clamp( _in, low, high )
if (_in < low ) then return low end
if (_in > high ) then return high end
return _in
end
function AngleBetweenPoints(x1,y1,x2,y2)
--local angle = math.atan2(y1-y2,x1-x2)
local angle = math.atan2(y2-y1,x2-x1)
return angle
end
function math.Deg2Rad( deg )
return deg * ( math.pi / 180 )
end
function table.Count(tbl)
if tbl ~= nil then
local num = 0
for k,v in pairs(tbl) do
num = num + 1
end
return num
end
end
--Desc: Taken straight from http://lua-users.org/wiki/PitLibTablestuff
function table.Merge(dest, source)
for k,v in pairs(source) do
if ( type(v) == 'table' and type(dest[k]) == 'table' ) then
-- don't overwrite one table with another;
-- instead merge them recurisvely
table.Merge(dest[k], v)
else
dest[k] = v
end
end
return dest
end
function table.Copy(t, lookup_table)
if (t == nil) then return nil end
local copy = {}
setmetatable(copy, getmetatable(t))
for i,v in pairs(t) do
if type(v) ~= "table" then
copy[i] = v
else
lookup_table = lookup_table or {}
lookup_table[t] = copy
if lookup_table[v] then
copy[i] = lookup_table[v] -- we already copied this table. reuse the copy.
else
copy[i] = table.Copy(v,lookup_table) -- not yet copied. copy it.
end
end
end
return copy
end
function table.HasValue(tbl, value)
for k,v in pairs(tbl) do
if v == value then
return true
end
end
return false
end
function table.HasKey(tbl, key)
for k,v in pairs(tbl) do
if k == key then
return true
end
end
return false
end
function string.ToTable ( str )
local tab = {}
for i=1, string.len( str ) do
table.insert( tab, string.sub( str, i, i ) )
end
return tab
end
function string.Explode ( seperator, str )
if ( seperator == "" ) then
return string.ToTable( str )
end
local tble={}
local ll=0
while (true) do
l = string.find( str, seperator, ll, true )
if (l ~= nil) then
table.insert(tble, string.sub(str,ll,l-1))
ll=l+1
else
table.insert(tble, string.sub(str,ll))
break
end
end
return tble
end
function string.Implode(seperator,Table) return
table.concat(Table,seperator)
end
function string.GetExtensionFromFilename(path)
local ExplTable = string.ToTable( path )
for i = table.getn(ExplTable), 1, -1 do
if ExplTable[i] == "." then return string.sub(path, i+1)end
if ExplTable[i] == "/" or ExplTable[i] == "\\" then return "" end
end
return ""
end
function string.GetPathFromFilename(path)
local ExplTable = string.ToTable( path )
for i = table.getn(ExplTable), 1, -1 do
if ExplTable[i] == "/" or ExplTable[i] == "\\" then return string.sub(path, 1, i) end
end
return ""
end
function string.GetFileFromFilename(path)
local ExplTable = string.ToTable( path )
for i = table.getn(ExplTable), 1, -1 do
if ExplTable[i] == "/" or ExplTable[i] == "\\" then return string.sub(path, i) end
end
return ""
end
function string.FormattedTime( TimeInSeconds, Format )
if not TimeInSeconds then TimeInSeconds = 0 end
local i = math.floor( TimeInSeconds )
local h,m,s,ms = ( i/3600 ),
( i/60 )-( math.floor( i/3600 )*3600 ),
TimeInSeconds-( math.floor( i/60 )*60 ),
( TimeInSeconds-i )*100
if Format then
return string.format( Format, m, s, ms )
else
return { h=h, m=m, s=s, ms=ms }
end
end
function string.ToMinutesSecondsMilliseconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i:%02i") end
function string.ToMinutesSeconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i") end
function string.Left(str, num)
return string.sub(str, 1, num)
end
function string.Right(str, num)
return string.sub(str, -num)
end
function string.Replace(str, tofind, toreplace)
local start = 1
while (true) do
local pos = string.find(str, tofind, start, true)
if (pos == nil) then
break
end
local left = string.sub(str, 1, pos-1)
local right = string.sub(str, pos + #tofind)
str = left .. toreplace .. right
start = pos + #toreplace
end
return str
end
function string.Trim( s, char )
if (char==nil) then char = "%s" end
return string.gsub(s, "^".. char.."*(.-)"..char.."*$", "%1")
end
function string.TrimRight( s, char )
if (char==nil) then char = " " end
if ( string.sub( s, -1 ) == char ) then
s = string.sub( s, 0, -2 )
s = string.TrimRight( s, char )
end
return s
end
function string.TrimLeft( s, char )
if (char==nil) then char = " " end
if ( string.sub( s, 1 ) == char ) then
s = string.sub( s, 1 )
s = string.TrimLeft( s, char )
end
return s
end
function table.ForceInsert( t, v )
if t == nil then
t = {}
end
table.insert( t, v )
end
function string.wrap(str, limit, indent, indent1)
indent = indent or ""
indent1 = indent1 or indent
limit = limit or 72
local here = 1-#indent1
return indent1..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
if fi-here > limit then
here = st - #indent
return "\n"..indent..word
end
end)
end
function string.reflow(str, limit, indent, indent1)
return (str:gsub("%s*\n%s+", "\n")
:gsub("%s%s+", " ")
:gsub("[^\n]+",
function(line)
return string.wrap(line, limit, indent, indent1)
end))
end
function math.Round( i )
i = i or 0
return math.floor( i + 0.5 )
end
--like ipairs but will do nonwhole number keys [notfinished]
function nonintpairs (tbl)
local least = math.huge
local numdone = 0
local done = {}
while numdone < table.Count(tbl) do
for k,v in pairs(nonintpairs) do
if k < least and not table.HasValue(done, k) then
least = k
end
end
end
end
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
---------------------------------------------------------
--Name: table.SortByKey( table )
--Desc: Returns a table sorted numerically by Key value
---------------------------------------------------------*/
function table.SortByKey( Table, Desc )
local temp = {}
for key, _ in pairs(Table) do table.insert(temp, key) end
if ( Desc ) then
table.sort(temp, function(a, b) return Table[a] < Table[b] end)
else
table.sort(temp, function(a, b) return Table[a] > Table[b] end)
end
return temp
end
---------------------------------------------------------
-- Name: table.SortByMember( table )
-- Desc: Sorts table by named member
---------------------------------------------------------*/
function table.SortByMember( Table, MemberName, bAsc )
local TableMemberSort = function( a, b, MemberName, bReverse )
if ( type(a) ~= "table" ) then return bReverse end
if ( type(b) ~= "table" ) then return bReverse end
if not ( a[MemberName] ) then return bReverse end
if not ( b[MemberName] ) then return bReverse end
if ( bReverse ) then
return a[MemberName] < b[MemberName]
else
return a[MemberName] > b[MemberName]
end
end
table.sort( Table, function(a, b) return TableMemberSort( a, b, MemberName, bAsc or false ) end )
end