Skip to content

Commit

Permalink
#67 Added toBoolean function globally. Added a bunch of component key…
Browse files Browse the repository at this point in the history
… types.
  • Loading branch information
Ismoh committed Mar 8, 2023
1 parent f4491c8 commit 225ec53
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 513 deletions.
34 changes: 28 additions & 6 deletions mods/noita-mp/files/scripts/extensions/globalExtensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ table.
Example:
]]

function __genOrderedIndex( t )
function __genOrderedIndex(t)
local orderedIndex = {}
for key in pairs(t) do
if type(key) == "number" then
table.insert( orderedIndex, key )
table.insert(orderedIndex, key)
end
end
table.sort( orderedIndex )
table.sort(orderedIndex)
return orderedIndex
end

Expand All @@ -32,13 +32,13 @@ function orderedNext(t, state)
--print("orderedNext: state = "..tostring(state) )
if state == nil then
-- the first time, generate the index
t.__orderedIndex = __genOrderedIndex( t )
key = t.__orderedIndex[1]
t.__orderedIndex = __genOrderedIndex(t)
key = t.__orderedIndex[1]
else
-- fetch the next value
for i = 1, table.getn(t.__orderedIndex) do
if t.__orderedIndex[i] == state then
key = t.__orderedIndex[i+1]
key = t.__orderedIndex[i + 1]
end
end
end
Expand All @@ -56,4 +56,26 @@ function orderedPairs(t)
-- Equivalent of the pairs() function on tables. Allows to iterate
-- in order
return orderedNext, t, nil
end

function toBoolean(value)
if type(value) == "nil" or type(value) == "userdata" or type(value) == "function" or
type(value) == "thread" or type(value) == "table" then
error(("Unable toBoolean when value %s is not 'string' or 'number' type: type = %s"):format(value, type(value)),
2)
end
if type(value) == "string" then
return string.contains(value, "true") or value == "1"
end
if type(value) == "number" then
if value == 1 then
return true
else
return false
end
end
if type(value) == "boolean" then
return value
end
error(("Unable to convert to boolean! value = %s"):format(value), 2)
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
--- @return number index also returns the index of the found key
table.contains = function(tbl, key)
for i = 1, #tbl do
-- better performance? Yes reduced load from 111 to 80. still only 10fps
-- better performance? Yes reduced load from 111 to 80.
local v = tbl[i]
if v == key then
return true, i
Expand Down
Loading

0 comments on commit 225ec53

Please sign in to comment.