2015-04-06 01:18:46 -07:00
|
|
|
local Utils = {}
|
2014-07-12 18:47:39 -07:00
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
local Memory = require "util.memory"
|
2014-07-12 18:47:39 -07:00
|
|
|
|
2015-04-08 01:51:38 -07:00
|
|
|
local EMP = 1
|
2015-04-07 19:06:29 -07:00
|
|
|
|
2015-04-06 17:17:00 -07:00
|
|
|
local yellow = YELLOW
|
|
|
|
|
2015-04-10 22:02:25 -07:00
|
|
|
-- GLOBAL
|
|
|
|
|
|
|
|
function p(...)
|
|
|
|
local string
|
|
|
|
if #arg == 0 then
|
|
|
|
string = arg[0]
|
|
|
|
else
|
|
|
|
string = ""
|
|
|
|
for i,str in ipairs(arg) do
|
|
|
|
if str == true then
|
|
|
|
string = string.."\n"
|
|
|
|
else
|
|
|
|
string = string..str.." "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print(string)
|
|
|
|
end
|
|
|
|
|
2015-04-04 18:37:48 -07:00
|
|
|
-- GENERAL
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.dist(x1, y1, x2, y2)
|
2014-07-12 18:47:39 -07:00
|
|
|
return math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
|
|
|
|
end
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.each(table, func)
|
2014-07-12 18:47:39 -07:00
|
|
|
for key,val in pairs(table) do
|
|
|
|
func(key.." = "..tostring(val)..",")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.eachi(table, func)
|
2014-07-12 18:47:39 -07:00
|
|
|
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
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.match(needle, haystack)
|
2014-07-12 18:47:39 -07:00
|
|
|
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
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.key(needle, haystack)
|
2014-07-12 18:47:39 -07:00
|
|
|
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
|
|
|
|
|
2015-04-07 04:52:03 -07:00
|
|
|
function Utils.capitalize(string)
|
2015-04-07 19:06:29 -07:00
|
|
|
return string:sub(1, 1):upper()..string:sub(2)
|
2015-04-07 04:52:03 -07:00
|
|
|
end
|
|
|
|
|
2015-04-12 11:26:52 -07:00
|
|
|
function Utils.nextCircularIndex(index, direction, totalCount)
|
|
|
|
local nextIndex = index + direction
|
|
|
|
if nextIndex < 1 then
|
|
|
|
nextIndex = totalCount
|
|
|
|
elseif nextIndex > totalCount then
|
|
|
|
nextIndex = 1
|
|
|
|
end
|
|
|
|
return nextIndex
|
|
|
|
end
|
|
|
|
|
2015-04-04 18:37:48 -07:00
|
|
|
-- GAME
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.canPotionWith(potion, forDamage, curr_hp, max_hp)
|
2015-04-04 18:37:48 -07:00
|
|
|
local potion_hp
|
|
|
|
if potion == "full_restore" then
|
|
|
|
potion_hp = 9001
|
|
|
|
elseif potion == "super_potion" then
|
|
|
|
potion_hp = 50
|
|
|
|
else
|
|
|
|
potion_hp = 20
|
|
|
|
end
|
|
|
|
return math.min(curr_hp + potion_hp, max_hp) >= forDamage - 1
|
|
|
|
end
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.ingame()
|
|
|
|
return Memory.raw(0x020E) > 0
|
2014-07-12 18:47:39 -07:00
|
|
|
end
|
|
|
|
|
2015-04-07 19:06:29 -07:00
|
|
|
function Utils.drawText(x, y, message)
|
|
|
|
gui.text(x * EMP, y * EMP, message)
|
|
|
|
end
|
|
|
|
|
2015-03-30 14:46:37 -07:00
|
|
|
-- TIME
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.igt()
|
2015-04-10 01:14:27 -07:00
|
|
|
local hours = Memory.value("time", "hours")
|
|
|
|
local mins = Memory.value("time", "minutes")
|
|
|
|
local secs = Memory.value("time", "seconds")
|
|
|
|
return (hours * 60 + mins) * 60 + secs
|
2015-04-04 18:37:48 -07:00
|
|
|
end
|
|
|
|
|
2015-03-30 14:46:37 -07:00
|
|
|
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
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.timeSince(prevTime)
|
|
|
|
local currTime = Utils.igt()
|
2015-03-30 14:46:37 -07:00
|
|
|
local diff = currTime - prevTime
|
|
|
|
local timeString
|
2015-03-30 15:29:00 -07:00
|
|
|
if diff > 0 then
|
2015-04-05 18:29:45 -07:00
|
|
|
local secs = diff % 60
|
2015-03-30 14:46:37 -07:00
|
|
|
local mins = math.floor(diff / 60)
|
|
|
|
timeString = clockSegment(mins)..":"..clockSegment(secs)
|
|
|
|
end
|
|
|
|
return currTime, timeString
|
|
|
|
end
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.elapsedTime()
|
2015-04-10 01:14:27 -07:00
|
|
|
local secs = Memory.value("time", "seconds")
|
|
|
|
local mins = Memory.value("time", "minutes")
|
|
|
|
local hours = Memory.value("time", "hours")
|
|
|
|
return hours..":"..clockSegment(mins)..":"..clockSegment(secs)
|
2015-03-30 14:46:37 -07:00
|
|
|
end
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
function Utils.frames()
|
2015-04-10 01:14:27 -07:00
|
|
|
local totalFrames = Memory.value("time", "hours") * 60
|
|
|
|
totalFrames = (totalFrames + Memory.value("time", "minutes")) * 60
|
|
|
|
totalFrames = (totalFrames + Memory.value("time", "seconds")) * 60
|
|
|
|
totalFrames = totalFrames + Memory.value("time", "frames")
|
|
|
|
return totalFrames
|
2015-04-01 10:45:29 -07:00
|
|
|
end
|
|
|
|
|
2015-04-06 01:18:46 -07:00
|
|
|
return Utils
|