2014-07-12 18:47:39 -07:00
|
|
|
local utils = {}
|
|
|
|
|
|
|
|
local memory = require "util.memory"
|
|
|
|
|
|
|
|
function utils.dist(x1, y1, x2, y2)
|
|
|
|
return math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.ingame()
|
|
|
|
return memory.raw(0x020E) > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.each(table, func)
|
|
|
|
for key,val in pairs(table) do
|
|
|
|
func(key.." = "..tostring(val)..",")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.eachi(table, func)
|
|
|
|
for idx,val in ipairs(table) do
|
2015-03-30 15:29:00 -07:00
|
|
|
if val then
|
2014-07-12 18:47:39 -07:00
|
|
|
func(idx.." "..val)
|
|
|
|
else
|
|
|
|
func(idx)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.match(needle, haystack)
|
|
|
|
for i,val in ipairs(haystack) do
|
2015-03-30 15:29:00 -07:00
|
|
|
if needle == val then
|
2014-07-12 18:47:39 -07:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2015-03-28 17:16:04 -07:00
|
|
|
return false
|
2014-07-12 18:47:39 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function utils.key(needle, haystack)
|
|
|
|
for key,val in pairs(haystack) do
|
2015-03-30 15:29:00 -07:00
|
|
|
if needle == val then
|
2014-07-12 18:47:39 -07:00
|
|
|
return key
|
|
|
|
end
|
|
|
|
end
|
2015-03-28 17:16:04 -07:00
|
|
|
return nil
|
2014-07-12 18:47:39 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function utils.igt()
|
|
|
|
local secs = memory.raw(0xDA44)
|
|
|
|
local mins = memory.raw(0xDA43)
|
|
|
|
local hours = memory.raw(0xDA41)
|
|
|
|
return secs + mins * 60 + hours * 3600
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.onPokemonSelect(battleMenu)
|
|
|
|
return battleMenu == 8 or battleMenu == 48 or battleMenu == 184 or battleMenu == 224
|
|
|
|
end
|
|
|
|
|
2015-03-30 14:46:37 -07:00
|
|
|
-- TIME
|
|
|
|
|
|
|
|
local function clockSegment(unit)
|
2015-03-30 15:29:00 -07:00
|
|
|
if unit < 10 then
|
2015-03-30 14:46:37 -07:00
|
|
|
unit = "0"..unit
|
|
|
|
end
|
|
|
|
return unit
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.timeSince(prevTime)
|
|
|
|
local currTime = utils.igt()
|
|
|
|
local diff = currTime - prevTime
|
|
|
|
local timeString
|
2015-03-30 15:29:00 -07:00
|
|
|
if diff > 0 then
|
2015-03-30 14:46:37 -07:00
|
|
|
local mins = math.floor(diff / 60)
|
|
|
|
local secs = mins % 60
|
|
|
|
timeString = clockSegment(mins)..":"..clockSegment(secs)
|
|
|
|
end
|
|
|
|
return currTime, timeString
|
|
|
|
end
|
|
|
|
|
|
|
|
function utils.elapsedTime()
|
|
|
|
local secs = memory.raw(0xDA44)
|
2015-03-30 15:29:00 -07:00
|
|
|
if secs < 10 then
|
2015-03-30 14:46:37 -07:00
|
|
|
secs = "0"..secs
|
|
|
|
end
|
|
|
|
local mins = memory.raw(0xDA43)
|
2015-03-30 15:29:00 -07:00
|
|
|
if mins < 10 then
|
2015-03-30 14:46:37 -07:00
|
|
|
mins = "0"..mins
|
|
|
|
end
|
|
|
|
return memory.raw(0xDA41)..":"..mins..":"..secs
|
|
|
|
end
|
|
|
|
|
2014-07-12 18:47:39 -07:00
|
|
|
return utils
|