1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/utils/utils.lua

101 lines
2.3 KiB
Lua
Raw Normal View History

2018-06-02 18:16:04 +02:00
local Module = {}
2017-06-13 13:16:07 +02:00
2018-06-02 18:16:04 +02:00
Module.distance = function(pos1, pos2)
2018-06-19 17:33:21 +02:00
local dx = pos2.x - pos1.x
local dy = pos2.y - pos1.y
return math.sqrt(dx * dx + dy * dy)
2017-06-13 13:16:07 +02:00
end
-- rounds number (num) to certain number of decimal places (idp)
2018-06-02 18:16:04 +02:00
math.round = function(num, idp)
2018-06-19 17:33:21 +02:00
local mult = 10 ^ (idp or 0)
return math.floor(num * mult + 0.5) / mult
end
function math.clamp(num, min, max)
if num < min then
return min
elseif num > max then
return max
else
return num
end
2017-06-13 13:16:07 +02:00
end
2018-06-02 18:16:04 +02:00
Module.print_except = function(msg, player)
2018-06-19 17:33:21 +02:00
for _, p in pairs(game.players) do
if p.connected and p ~= player then
p.print(msg)
end
end
end
2018-06-02 18:16:04 +02:00
Module.print_admins = function(msg)
2018-06-19 17:33:21 +02:00
for _, p in pairs(game.players) do
if p.connected and p.admin then
p.print(msg)
end
end
end
2018-06-02 18:16:04 +02:00
Module.get_actor = function()
2018-06-19 17:33:21 +02:00
if game.player then
return game.player.name
end
return '<server>'
end
2018-06-03 14:24:33 +02:00
Module.cast_bool = function(var)
2018-06-19 17:33:21 +02:00
if var then
return true
else
return false
end
2018-06-03 14:24:33 +02:00
end
2018-06-19 17:33:21 +02:00
Module.find_entities_by_last_user =
function(player, surface, filters)
if type(player) == 'string' or not player then
error(
"bad argument #1 to '" ..
debug.getinfo(1, 'n').name .. "' (number or LuaPlayer expected, got " .. type(player) .. ')',
1
)
return
end
if type(surface) ~= 'table' and type(surface) ~= 'number' then
error(
"bad argument #2 to '" ..
debug.getinfo(1, 'n').name .. "' (number or LuaSurface expected, got " .. type(surface) .. ')',
1
)
return
end
local entities = {}
local surface = surface
local player = player
local filters = filters or {}
if type(surface) == 'number' then
surface = game.surfaces[surface]
end
if type(player) == 'number' then
player = game.players[player]
end
filters.force = player.force.name
for _, e in pairs(surface.find_entities_filtered(filters)) do
if e.last_user == player then
table.insert(entities, e)
end
end
return entities
end
Module.ternary = function(c, t, f)
2018-06-19 17:33:21 +02:00
if c then
return t
else
return f
end
end
2018-06-03 14:24:33 +02:00
return Module