2017-02-11 04:26:00 +01:00
|
|
|
mcl_util = {}
|
|
|
|
|
2024-05-22 21:51:47 +02:00
|
|
|
local modname = minetest.get_current_modname()
|
|
|
|
local modpath = minetest.get_modpath(modname)
|
|
|
|
dofile(modpath.."/roman_numerals.lua")
|
|
|
|
dofile(modpath.."/nodes.lua")
|
2023-10-27 03:19:25 +02:00
|
|
|
|
2021-11-09 19:50:49 +01:00
|
|
|
-- Updates all values in t using values from to*.
|
|
|
|
function table.update(t, ...)
|
2023-01-06 23:27:15 +01:00
|
|
|
for _, to in ipairs {...} do
|
|
|
|
for k, v in pairs(to) do
|
2021-11-09 19:50:49 +01:00
|
|
|
t[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Updates nil values in t using values from to*.
|
|
|
|
function table.update_nil(t, ...)
|
2023-01-06 23:27:15 +01:00
|
|
|
for _, to in ipairs {...} do
|
|
|
|
for k, v in pairs(to) do
|
2021-11-09 19:50:49 +01:00
|
|
|
if t[k] == nil then
|
|
|
|
t[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2023-11-19 01:23:57 +01:00
|
|
|
---Works the same as `pairs`, but order returned by keys
|
|
|
|
---
|
|
|
|
---Taken from https://www.lua.org/pil/19.3.html
|
|
|
|
---@generic T: table, K, V, C
|
|
|
|
---@param t T
|
|
|
|
---@param f? fun(a: C, b: C):boolean
|
|
|
|
---@return fun():K, V
|
|
|
|
function table.pairs_by_keys(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 function iter() -- iterator function
|
|
|
|
i = i + 1
|
|
|
|
if a[i] == nil then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return a[i], t[a[i]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return iter
|
|
|
|
end
|
|
|
|
|
2024-09-14 03:24:34 +02:00
|
|
|
-- Removes one element randomly selected from the array section of the table and
|
|
|
|
-- returns it, or nil if there are no elements in the array section of the table
|
|
|
|
function table.remove_random_element(table)
|
2024-09-14 00:10:05 +02:00
|
|
|
local count = #table
|
2024-09-14 03:24:34 +02:00
|
|
|
if count == 0 then return nil end
|
|
|
|
|
|
|
|
local idx = math.random(count)
|
|
|
|
local res = table[idx]
|
|
|
|
table[idx] = table[count]
|
|
|
|
table[count] = nil
|
|
|
|
count = count - 1
|
|
|
|
return res
|
2024-09-14 00:10:05 +02:00
|
|
|
end
|
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
local LOGGING_ON = minetest.settings:get_bool("mcl_logging_default", false)
|
2022-11-08 23:27:10 +01:00
|
|
|
local LOG_MODULE = "[MCL2]"
|
2023-01-06 23:27:15 +01:00
|
|
|
function mcl_util.mcl_log(message, module, bypass_default_logger)
|
2022-11-08 23:27:10 +01:00
|
|
|
local selected_module = LOG_MODULE
|
|
|
|
if module then
|
|
|
|
selected_module = module
|
|
|
|
end
|
2022-11-08 23:54:16 +01:00
|
|
|
if (bypass_default_logger or LOGGING_ON) and message then
|
2022-11-08 23:27:10 +01:00
|
|
|
minetest.log(selected_module .. " " .. message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-08 22:51:13 +02:00
|
|
|
local player_timers = {}
|
|
|
|
|
2023-03-28 02:43:25 +02:00
|
|
|
-- This is a dtime timer than can be used in on_step functions so it works every x seconds
|
2023-05-08 22:51:13 +02:00
|
|
|
-- self - Object you want to store timer data on. E.g. mob or a minecart, or player_name
|
2023-03-28 02:43:25 +02:00
|
|
|
-- dtime - The time since last run of on_step, should be passed in to function
|
|
|
|
-- timer_name - This is the name of the timer and also the key to store the data. No spaces + lowercase.
|
|
|
|
-- threshold - The time before it returns successful. 0.2 if you want to run it 5 times a second.
|
|
|
|
function mcl_util.check_dtime_timer(self, dtime, timer_name, threshold)
|
|
|
|
if not self or not threshold or not dtime then return end
|
|
|
|
if not timer_name or timer_name == "" then return end
|
|
|
|
|
2023-05-08 22:51:13 +02:00
|
|
|
if type(self) == "string" then
|
|
|
|
local player_name = self
|
|
|
|
if not player_timers[player_name] then
|
|
|
|
player_timers[player_name] = {}
|
|
|
|
end
|
|
|
|
self = player_timers[player_name]
|
|
|
|
end
|
|
|
|
|
2023-03-28 02:43:25 +02:00
|
|
|
if not self._timers then
|
|
|
|
self._timers = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self._timers[timer_name] then
|
|
|
|
self._timers[timer_name] = 0
|
|
|
|
else
|
|
|
|
self._timers[timer_name] = self._timers[timer_name] + dtime
|
|
|
|
--minetest.log("dtime: " .. tostring(self._timers[timer_name]))
|
|
|
|
end
|
|
|
|
|
|
|
|
if self._timers[timer_name] > threshold then
|
|
|
|
--minetest.log("Over threshold")
|
|
|
|
self._timers[timer_name] = 0
|
|
|
|
return true
|
|
|
|
--else
|
|
|
|
--minetest.log("Not over threshold")
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-10-15 23:03:48 +02:00
|
|
|
-- While we should always favour the new minetest vector functions such as vector.new or vector.offset which validate on
|
|
|
|
-- creation. There may be cases where state gets corrupted and we may have to check the vector is valid if created the
|
|
|
|
-- old way. This allows us to do this as a tactical solution until old style vectors are completely removed.
|
|
|
|
function mcl_util.validate_vector (vect)
|
|
|
|
if vect then
|
|
|
|
if tonumber(vect.x) and tonumber(vect.y) and tonumber(vect.z) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-10-06 07:44:59 +02:00
|
|
|
function mcl_util.file_exists(name)
|
2022-10-28 13:34:58 +02:00
|
|
|
if type(name) ~= "string" then return end
|
2022-10-06 07:44:59 +02:00
|
|
|
local f = io.open(name)
|
|
|
|
if not f then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-10-27 00:15:57 +02:00
|
|
|
--- Selects item stack to transfer from
|
|
|
|
---@param src_inventory InvRef Source innentory to pull from
|
|
|
|
---@param src_list string Name of source inventory list to pull from
|
|
|
|
---@param dst_inventory InvRef Destination inventory to push to
|
|
|
|
---@param dst_list string Name of destination inventory list to push to
|
|
|
|
---@param condition? fun(stack: ItemStack) Condition which items are allowed to be transfered.
|
2024-02-12 13:03:55 +01:00
|
|
|
---@param count? integer Number of items to try to transfer at once
|
2023-10-27 00:15:57 +02:00
|
|
|
---@return integer Item stack number to be transfered
|
2024-02-12 13:03:55 +01:00
|
|
|
function mcl_util.select_stack(src_inventory, src_list, dst_inventory, dst_list, condition, count)
|
2023-10-27 00:15:57 +02:00
|
|
|
local src_size = src_inventory:get_size(src_list)
|
2017-08-04 03:00:33 +02:00
|
|
|
local stack
|
2023-10-27 00:15:57 +02:00
|
|
|
for i = 1, src_size do
|
2017-08-04 03:00:33 +02:00
|
|
|
stack = src_inventory:get_stack(src_list, i)
|
2024-02-12 13:03:55 +01:00
|
|
|
|
|
|
|
-- Allow for partial stack movement
|
2024-02-17 00:44:55 +01:00
|
|
|
if count and stack:get_count() >= count then
|
2024-02-12 13:03:55 +01:00
|
|
|
local new_stack = ItemStack(stack)
|
|
|
|
new_stack:set_count(count)
|
|
|
|
stack = new_stack
|
|
|
|
end
|
|
|
|
|
2023-10-27 00:15:57 +02:00
|
|
|
if not stack:is_empty() and dst_inventory:room_for_item(dst_list, stack) and ((condition == nil or condition(stack))) then
|
2017-08-04 03:00:33 +02:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2017-08-03 23:23:33 +02:00
|
|
|
-- Moves a single item from one inventory to another.
|
2017-02-14 00:44:23 +01:00
|
|
|
--- source_inventory: Inventory to take the item from
|
|
|
|
--- source_list: List name of the source inventory from which to take the item
|
2017-02-14 01:25:02 +01:00
|
|
|
--- source_stack_id: The inventory position ID of the source inventory to take the item from (-1 for first occupied slot)
|
2017-02-14 00:44:23 +01:00
|
|
|
--- destination_inventory: Put item into this inventory
|
|
|
|
--- destination_list: List name of the destination inventory to which to put the item into
|
|
|
|
|
2017-02-14 00:10:37 +01:00
|
|
|
-- Returns true on success and false on failure
|
2017-02-14 00:44:23 +01:00
|
|
|
-- Possible failures: No item in source slot, destination inventory full
|
2017-02-14 00:10:37 +01:00
|
|
|
function mcl_util.move_item(source_inventory, source_list, source_stack_id, destination_inventory, destination_list)
|
2024-02-12 13:03:55 +01:00
|
|
|
-- Can't move items we don't have
|
|
|
|
if source_inventory:is_empty(source_list) then return false end
|
|
|
|
|
|
|
|
-- Can't move from an empty stack
|
|
|
|
local stack = source_inventory:get_stack(source_list, source_stack_id)
|
|
|
|
if stack:is_empty() then return false end
|
|
|
|
|
|
|
|
local new_stack = ItemStack(stack)
|
|
|
|
new_stack:set_count(1)
|
|
|
|
if not destination_inventory:room_for_item(destination_list, new_stack) then
|
|
|
|
return false
|
2017-02-14 00:10:37 +01:00
|
|
|
end
|
2024-02-12 13:03:55 +01:00
|
|
|
stack:take_item()
|
|
|
|
source_inventory:set_stack(source_list, source_stack_id, stack)
|
|
|
|
destination_inventory:add_item(destination_list, new_stack)
|
|
|
|
return true
|
2017-02-14 00:10:37 +01:00
|
|
|
end
|
2017-02-14 00:44:23 +01:00
|
|
|
|
2023-10-27 00:15:57 +02:00
|
|
|
--- Try pushing item from hopper inventory to destination inventory
|
|
|
|
---@param pos Vector
|
|
|
|
---@param dst_pos Vector
|
|
|
|
function mcl_util.hopper_push(pos, dst_pos)
|
|
|
|
local hop_inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
local hop_list = 'main'
|
|
|
|
|
|
|
|
-- Get node pos' for item transfer
|
|
|
|
local dst = minetest.get_node(dst_pos)
|
|
|
|
if not minetest.registered_nodes[dst.name] then return end
|
|
|
|
local dst_type = minetest.get_item_group(dst.name, "container")
|
|
|
|
if dst_type ~= 2 then return end
|
|
|
|
local dst_def = minetest.registered_nodes[dst.name]
|
|
|
|
|
|
|
|
local dst_list = 'main'
|
|
|
|
local dst_inv, stack_id
|
2023-12-24 05:48:41 +01:00
|
|
|
|
2024-02-12 13:03:55 +01:00
|
|
|
-- Find a inventory stack in the destination
|
2023-10-27 00:15:57 +02:00
|
|
|
if dst_def._mcl_hoppers_on_try_push then
|
|
|
|
dst_inv, dst_list, stack_id = dst_def._mcl_hoppers_on_try_push(dst_pos, pos, hop_inv, hop_list)
|
|
|
|
else
|
|
|
|
local dst_meta = minetest.get_meta(dst_pos)
|
|
|
|
dst_inv = dst_meta:get_inventory()
|
2024-02-12 13:03:55 +01:00
|
|
|
stack_id = mcl_util.select_stack(hop_inv, hop_list, dst_inv, dst_list, nil, 1)
|
2018-05-12 22:48:49 +02:00
|
|
|
end
|
2024-02-12 13:03:55 +01:00
|
|
|
if not stack_id then return false end
|
2018-05-12 22:48:49 +02:00
|
|
|
|
2024-02-12 13:03:55 +01:00
|
|
|
-- Move the item
|
|
|
|
local ok = mcl_util.move_item(hop_inv, hop_list, stack_id, dst_inv, dst_list)
|
|
|
|
if dst_def._mcl_hoppers_on_after_push then
|
|
|
|
dst_def._mcl_hoppers_on_after_push(dst_pos)
|
2017-08-04 01:58:31 +02:00
|
|
|
end
|
|
|
|
|
2024-02-12 13:03:55 +01:00
|
|
|
return ok
|
2023-10-27 00:15:57 +02:00
|
|
|
end
|
2017-02-14 01:25:02 +01:00
|
|
|
|
2023-10-27 00:15:57 +02:00
|
|
|
-- Try pulling from source inventory to hopper inventory
|
|
|
|
---@param pos Vector
|
|
|
|
---@param src_pos Vector
|
|
|
|
function mcl_util.hopper_pull(pos, src_pos)
|
|
|
|
local hop_inv = minetest.get_meta(pos):get_inventory()
|
|
|
|
local hop_list = 'main'
|
|
|
|
|
|
|
|
-- Get node pos' for item transfer
|
|
|
|
local src = minetest.get_node(src_pos)
|
|
|
|
if not minetest.registered_nodes[src.name] then return end
|
|
|
|
local src_type = minetest.get_item_group(src.name, "container")
|
|
|
|
if src_type ~= 2 then return end
|
|
|
|
local src_def = minetest.registered_nodes[src.name]
|
|
|
|
|
|
|
|
local src_list = 'main'
|
|
|
|
local src_inv, stack_id
|
2023-12-24 05:48:41 +01:00
|
|
|
|
2023-10-27 00:15:57 +02:00
|
|
|
if src_def._mcl_hoppers_on_try_pull then
|
|
|
|
src_inv, src_list, stack_id = src_def._mcl_hoppers_on_try_pull(src_pos, pos, hop_inv, hop_list)
|
|
|
|
else
|
|
|
|
local src_meta = minetest.get_meta(src_pos)
|
|
|
|
src_inv = src_meta:get_inventory()
|
2024-02-12 13:03:55 +01:00
|
|
|
stack_id = mcl_util.select_stack(src_inv, src_list, hop_inv, hop_list, nil, 1)
|
2018-05-12 21:50:56 +02:00
|
|
|
end
|
2017-08-03 23:23:33 +02:00
|
|
|
|
2023-10-27 00:15:57 +02:00
|
|
|
if stack_id ~= nil then
|
|
|
|
local ok = mcl_util.move_item(src_inv, src_list, stack_id, hop_inv, hop_list)
|
|
|
|
if src_def._mcl_hoppers_on_after_pull then
|
|
|
|
src_def._mcl_hoppers_on_after_pull(src_pos)
|
2017-02-14 00:44:23 +01:00
|
|
|
end
|
|
|
|
end
|
2017-02-14 01:25:02 +01:00
|
|
|
end
|
2017-02-21 03:00:48 +01:00
|
|
|
|
2022-02-22 18:03:27 +01:00
|
|
|
local function drop_item_stack(pos, stack)
|
|
|
|
if not stack or stack:is_empty() then return end
|
|
|
|
local drop_offset = vector.new(math.random() - 0.5, 0, math.random() - 0.5)
|
|
|
|
minetest.add_item(vector.add(pos, drop_offset), stack)
|
|
|
|
end
|
|
|
|
|
2022-02-07 08:47:41 +01:00
|
|
|
function mcl_util.drop_items_from_meta_container(listname)
|
2022-01-20 09:22:17 +01:00
|
|
|
return function(pos, oldnode, oldmetadata)
|
2022-02-22 18:03:27 +01:00
|
|
|
if oldmetadata and oldmetadata.inventory then
|
|
|
|
-- process in after_dig_node callback
|
|
|
|
local main = oldmetadata.inventory.main
|
|
|
|
if not main then return end
|
|
|
|
for _, stack in pairs(main) do
|
|
|
|
drop_item_stack(pos, stack)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local inv = meta:get_inventory()
|
|
|
|
for i = 1, inv:get_size("main") do
|
|
|
|
drop_item_stack(pos, inv:get_stack("main", i))
|
2022-01-20 09:22:17 +01:00
|
|
|
end
|
2022-02-22 18:03:27 +01:00
|
|
|
meta:from_table()
|
2022-01-20 09:22:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-21 03:00:48 +01:00
|
|
|
-- Returns true if item (itemstring or ItemStack) can be used as a furnace fuel.
|
|
|
|
-- Returns false otherwise
|
|
|
|
function mcl_util.is_fuel(item)
|
2023-01-06 23:27:15 +01:00
|
|
|
return minetest.get_craft_result({method = "fuel", width = 1, items = {item}}).time ~= 0
|
2017-02-21 03:00:48 +01:00
|
|
|
end
|
2017-02-21 16:38:28 +01:00
|
|
|
|
2021-01-26 14:13:21 +01:00
|
|
|
-- adjust the y level of an object to the center of its collisionbox
|
|
|
|
-- used to get the origin position of entity explosions
|
|
|
|
function mcl_util.get_object_center(obj)
|
|
|
|
local collisionbox = obj:get_properties().collisionbox
|
|
|
|
local pos = obj:get_pos()
|
|
|
|
local ymin = collisionbox[2]
|
|
|
|
local ymax = collisionbox[5]
|
|
|
|
pos.y = pos.y + (ymax - ymin) / 2.0
|
|
|
|
return pos
|
|
|
|
end
|
2021-03-05 09:37:27 +01:00
|
|
|
|
|
|
|
function mcl_util.get_color(colorstr)
|
|
|
|
local mc_color = mcl_colors[colorstr:upper()]
|
|
|
|
if mc_color then
|
2021-03-05 10:20:19 +01:00
|
|
|
colorstr = mc_color
|
2021-04-06 14:50:34 +02:00
|
|
|
elseif #colorstr ~= 7 or colorstr:sub(1, 1) ~= "#" then
|
2021-03-05 09:37:27 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local hex = tonumber(colorstr:sub(2, 7), 16)
|
|
|
|
if hex then
|
|
|
|
return colorstr, hex
|
|
|
|
end
|
|
|
|
end
|
2021-04-14 15:46:52 +02:00
|
|
|
|
|
|
|
function mcl_util.call_on_rightclick(itemstack, player, pointed_thing)
|
|
|
|
-- Call on_rightclick if the pointed node defines it
|
|
|
|
if pointed_thing and pointed_thing.type == "node" then
|
2021-04-18 16:03:23 +02:00
|
|
|
local pos = pointed_thing.under
|
|
|
|
local node = minetest.get_node(pos)
|
2021-04-14 15:46:52 +02:00
|
|
|
if player and not player:get_player_control().sneak then
|
2021-04-18 16:03:23 +02:00
|
|
|
local nodedef = minetest.registered_nodes[node.name]
|
|
|
|
local on_rightclick = nodedef and nodedef.on_rightclick
|
|
|
|
if on_rightclick then
|
|
|
|
return on_rightclick(pos, node, player, itemstack, pointed_thing) or itemstack
|
2021-04-14 15:46:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_util.calculate_durability(itemstack)
|
|
|
|
local unbreaking_level = mcl_enchanting.get_enchantment(itemstack, "unbreaking")
|
|
|
|
local armor_uses = minetest.get_item_group(itemstack:get_name(), "mcl_armor_uses")
|
|
|
|
|
|
|
|
local uses
|
|
|
|
|
|
|
|
if armor_uses > 0 then
|
|
|
|
uses = armor_uses
|
|
|
|
if unbreaking_level > 0 then
|
|
|
|
uses = uses / (0.6 + 0.4 / (unbreaking_level + 1))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local def = itemstack:get_definition()
|
|
|
|
if def then
|
|
|
|
local fixed_uses = def._mcl_uses
|
|
|
|
if fixed_uses then
|
|
|
|
uses = fixed_uses
|
|
|
|
if unbreaking_level > 0 then
|
|
|
|
uses = uses * (unbreaking_level + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-02 17:28:19 +01:00
|
|
|
|
|
|
|
local _, groupcap = next(itemstack:get_tool_capabilities().groupcaps)
|
|
|
|
uses = uses or (groupcap or {}).uses
|
2021-04-14 15:46:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return uses or 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_util.use_item_durability(itemstack, n)
|
|
|
|
local uses = mcl_util.calculate_durability(itemstack)
|
|
|
|
itemstack:add_wear(65535 / uses * n)
|
2024-05-22 20:55:07 +02:00
|
|
|
tt.reload_itemstack_description(itemstack) -- update tooltip
|
2021-04-14 15:46:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_util.deal_damage(target, damage, mcl_reason)
|
|
|
|
local luaentity = target:get_luaentity()
|
|
|
|
|
|
|
|
if luaentity then
|
|
|
|
if luaentity.deal_damage then
|
2021-04-25 13:50:07 +02:00
|
|
|
luaentity:deal_damage(damage, mcl_reason or {type = "generic"})
|
2021-04-14 15:46:52 +02:00
|
|
|
return
|
2022-05-25 14:02:10 +02:00
|
|
|
elseif luaentity.is_mob then
|
2021-04-25 13:50:07 +02:00
|
|
|
-- local puncher = mcl_reason and mcl_reason.direct or target
|
|
|
|
-- target:punch(puncher, 1.0, {full_punch_interval = 1.0, damage_groups = {fleshy = damage}}, vector.direction(puncher:get_pos(), target:get_pos()), damage)
|
2021-05-05 08:54:03 +02:00
|
|
|
if luaentity.health > 0 then
|
|
|
|
luaentity.health = luaentity.health - damage
|
|
|
|
end
|
2021-04-14 15:46:52 +02:00
|
|
|
return
|
|
|
|
end
|
2023-08-11 16:54:58 +02:00
|
|
|
elseif not target:is_player() then return end
|
2021-04-14 15:46:52 +02:00
|
|
|
|
2023-02-01 06:29:29 +01:00
|
|
|
local is_immortal = target:get_armor_groups().immortal or 0
|
|
|
|
if is_immortal>0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-05-05 08:54:03 +02:00
|
|
|
local hp = target:get_hp()
|
|
|
|
|
|
|
|
if hp > 0 then
|
|
|
|
target:set_hp(hp - damage, {_mcl_reason = mcl_reason})
|
|
|
|
end
|
2021-04-14 15:46:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-18 20:21:11 +02:00
|
|
|
function mcl_util.get_hp(obj)
|
|
|
|
local luaentity = obj:get_luaentity()
|
|
|
|
|
2022-05-25 14:02:10 +02:00
|
|
|
if luaentity and luaentity.is_mob then
|
2021-04-18 20:21:11 +02:00
|
|
|
return luaentity.health
|
|
|
|
else
|
|
|
|
return obj:get_hp()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-14 15:46:52 +02:00
|
|
|
function mcl_util.get_inventory(object, create)
|
|
|
|
if object:is_player() then
|
|
|
|
return object:get_inventory()
|
|
|
|
else
|
|
|
|
local luaentity = object:get_luaentity()
|
|
|
|
local inventory = luaentity.inventory
|
|
|
|
|
|
|
|
if create and not inventory and luaentity.create_inventory then
|
|
|
|
inventory = luaentity:create_inventory()
|
|
|
|
end
|
|
|
|
|
|
|
|
return inventory
|
|
|
|
end
|
|
|
|
end
|
2021-04-25 16:42:38 +02:00
|
|
|
|
|
|
|
function mcl_util.get_wielded_item(object)
|
|
|
|
if object:is_player() then
|
|
|
|
return object:get_wielded_item()
|
|
|
|
else
|
|
|
|
-- ToDo: implement getting wielditems from mobs as soon as mobs have wielditems
|
|
|
|
return ItemStack()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function mcl_util.get_object_name(object)
|
|
|
|
if object:is_player() then
|
|
|
|
return object:get_player_name()
|
|
|
|
else
|
|
|
|
local luaentity = object:get_luaentity()
|
|
|
|
|
2021-04-29 11:17:52 +02:00
|
|
|
if not luaentity then
|
2021-05-05 13:24:23 +02:00
|
|
|
return tostring(object)
|
2021-04-29 11:17:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-25 16:42:38 +02:00
|
|
|
return luaentity.nametag and luaentity.nametag ~= "" and luaentity.nametag or luaentity.description or luaentity.name
|
|
|
|
end
|
|
|
|
end
|
2021-09-09 11:54:58 +02:00
|
|
|
|
|
|
|
function mcl_util.replace_mob(obj, mob)
|
2022-11-07 13:06:34 +01:00
|
|
|
if not obj then return end
|
2021-09-09 11:54:58 +02:00
|
|
|
local rot = obj:get_yaw()
|
|
|
|
local pos = obj:get_pos()
|
|
|
|
obj:remove()
|
|
|
|
obj = minetest.add_entity(pos, mob)
|
2022-11-07 13:06:34 +01:00
|
|
|
if not obj then return end
|
2021-09-09 11:54:58 +02:00
|
|
|
obj:set_yaw(rot)
|
|
|
|
return obj
|
|
|
|
end
|
2022-02-17 15:35:51 +01:00
|
|
|
|
2022-02-17 16:30:41 +01:00
|
|
|
function mcl_util.get_pointed_thing(player, liquid)
|
2022-02-17 15:35:51 +01:00
|
|
|
local pos = vector.offset(player:get_pos(), 0, player:get_properties().eye_height, 0)
|
|
|
|
local look_dir = vector.multiply(player:get_look_dir(), 5)
|
|
|
|
local pos2 = vector.add(pos, look_dir)
|
2022-02-17 16:30:41 +01:00
|
|
|
local ray = minetest.raycast(pos, pos2, false, liquid)
|
2022-05-26 07:29:28 +02:00
|
|
|
|
2022-02-17 15:35:51 +01:00
|
|
|
if ray then
|
|
|
|
for pointed_thing in ray do
|
|
|
|
return pointed_thing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-10-06 04:01:56 +02:00
|
|
|
|
|
|
|
-- This following part is 2 wrapper functions + helpers for
|
|
|
|
-- object:set_bones
|
|
|
|
-- and player:set_properties preventing them from being resent on
|
|
|
|
-- every globalstep when they have not changed.
|
|
|
|
|
|
|
|
local function roundN(n, d)
|
|
|
|
if type(n) ~= "number" then return n end
|
2023-01-06 23:27:15 +01:00
|
|
|
local m = 10 ^ d
|
|
|
|
return math.floor(n * m + 0.5) / m
|
2022-10-06 04:01:56 +02:00
|
|
|
end
|
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
local function close_enough(a, b)
|
|
|
|
local rt = true
|
2022-10-06 04:01:56 +02:00
|
|
|
if type(a) == "table" and type(b) == "table" then
|
2023-01-06 23:27:15 +01:00
|
|
|
for k, v in pairs(a) do
|
|
|
|
if roundN(v, 2) ~= roundN(b[k], 2) then
|
|
|
|
rt = false
|
2022-10-06 04:01:56 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2023-01-06 23:27:15 +01:00
|
|
|
rt = roundN(a, 2) == roundN(b, 2)
|
2022-10-06 04:01:56 +02:00
|
|
|
end
|
|
|
|
return rt
|
|
|
|
end
|
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
local function props_changed(props, oldprops)
|
|
|
|
local changed = false
|
|
|
|
local p = {}
|
|
|
|
for k, v in pairs(props) do
|
|
|
|
if not close_enough(v, oldprops[k]) then
|
|
|
|
p[k] = v
|
|
|
|
changed = true
|
2022-10-06 04:01:56 +02:00
|
|
|
end
|
|
|
|
end
|
2023-01-06 23:27:15 +01:00
|
|
|
return changed, p
|
2022-10-06 04:01:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--tests for roundN
|
2023-01-06 23:27:15 +01:00
|
|
|
local test_round1 = 15
|
|
|
|
local test_round2 = 15.00199999999
|
|
|
|
local test_round3 = 15.00111111
|
|
|
|
local test_round4 = 15.00999999
|
2022-10-06 04:01:56 +02:00
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
assert(roundN(test_round1, 2) == roundN(test_round1, 2))
|
|
|
|
assert(roundN(test_round1, 2) == roundN(test_round2, 2))
|
|
|
|
assert(roundN(test_round1, 2) == roundN(test_round3, 2))
|
|
|
|
assert(roundN(test_round1, 2) ~= roundN(test_round4, 2))
|
2022-10-06 04:01:56 +02:00
|
|
|
|
|
|
|
-- tests for close_enough
|
2023-01-06 23:27:15 +01:00
|
|
|
local test_cb = {-0.35, 0, -0.35, 0.35, 0.8, 0.35} --collisionboxes
|
|
|
|
local test_cb_close = {-0.351213, 0, -0.35, 0.35, 0.8, 0.351212}
|
|
|
|
local test_cb_diff = {-0.35, 0, -1.35, 0.35, 0.8, 0.35}
|
2022-10-06 04:01:56 +02:00
|
|
|
|
|
|
|
local test_eh = 1.65 --eye height
|
|
|
|
local test_eh_close = 1.65123123
|
|
|
|
local test_eh_diff = 1.35
|
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
local test_nt = {r = 225, b = 225, a = 225, g = 225} --nametag
|
|
|
|
local test_nt_diff = {r = 225, b = 225, a = 0, g = 225}
|
2022-10-06 04:01:56 +02:00
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
assert(close_enough(test_cb, test_cb_close))
|
|
|
|
assert(not close_enough(test_cb, test_cb_diff))
|
|
|
|
assert(close_enough(test_eh, test_eh_close))
|
|
|
|
assert(not close_enough(test_eh, test_eh_diff))
|
|
|
|
assert(not close_enough(test_nt, test_nt_diff)) --no floats involved here
|
2022-10-06 04:01:56 +02:00
|
|
|
|
|
|
|
--tests for properties_changed
|
2023-01-06 23:27:15 +01:00
|
|
|
local test_properties_set1 = {collisionbox = {-0.35, 0, -0.35, 0.35, 0.8, 0.35}, eye_height = 0.65,
|
|
|
|
nametag_color = {r = 225, b = 225, a = 225, g = 225}}
|
|
|
|
local test_properties_set2 = {collisionbox = {-0.35, 0, -0.35, 0.35, 0.8, 0.35}, eye_height = 1.35,
|
|
|
|
nametag_color = {r = 225, b = 225, a = 225, g = 225}}
|
2022-10-06 04:01:56 +02:00
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
local test_p1, _ = props_changed(test_properties_set1, test_properties_set1)
|
|
|
|
local test_p2, _ = props_changed(test_properties_set1, test_properties_set2)
|
2022-10-06 04:01:56 +02:00
|
|
|
|
|
|
|
assert(not test_p1)
|
|
|
|
assert(test_p2)
|
|
|
|
|
2023-01-06 23:27:15 +01:00
|
|
|
function mcl_util.set_properties(obj, props)
|
|
|
|
local changed, p = props_changed(props, obj:get_properties())
|
2022-10-06 04:01:56 +02:00
|
|
|
if changed then
|
|
|
|
obj:set_properties(p)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-06 17:48:22 +01:00
|
|
|
function mcl_util.set_bone_position(obj, bone, pos, rot)
|
|
|
|
local current_pos, current_rot = obj:get_bone_position(bone)
|
|
|
|
local pos_equal = not pos or vector.equals(vector.round(current_pos), vector.round(pos))
|
|
|
|
local rot_equal = not rot or vector.equals(vector.round(current_rot), vector.round(rot))
|
|
|
|
if not pos_equal or not rot_equal then
|
|
|
|
obj:set_bone_position(bone, pos or current_pos, rot or current_rot)
|
2022-10-06 04:01:56 +02:00
|
|
|
end
|
|
|
|
end
|
2023-01-06 21:07:07 +01:00
|
|
|
|
2023-01-12 20:22:36 +01:00
|
|
|
--[[Check for a protection violation in a given area.
|
2023-01-06 21:07:07 +01:00
|
|
|
--
|
2023-01-12 20:22:36 +01:00
|
|
|
-- Applies is_protected() to a 3D lattice of points in the defined volume. The points are spaced
|
|
|
|
-- evenly throughout the volume and have a spacing similar to, but no larger than, "interval".
|
|
|
|
--
|
|
|
|
-- @param pos1 A position table of the area volume's first edge.
|
|
|
|
-- @param pos2 A position table of the area volume's second edge.
|
2023-01-06 21:58:56 +01:00
|
|
|
-- @param player The player performing the action.
|
2023-01-12 20:22:36 +01:00
|
|
|
-- @param interval Optional. Max spacing between checked points at the volume.
|
|
|
|
-- Default: Same as minetest.is_area_protected.
|
2023-01-06 21:07:07 +01:00
|
|
|
--
|
|
|
|
-- @return true on protection violation detection. false otherwise.
|
2023-01-12 20:22:36 +01:00
|
|
|
--
|
|
|
|
-- @notes *All corners and edges of the defined volume are checked.
|
2023-01-06 21:07:07 +01:00
|
|
|
]]
|
2023-01-12 20:22:36 +01:00
|
|
|
function mcl_util.check_area_protection(pos1, pos2, player, interval)
|
2023-01-06 21:07:07 +01:00
|
|
|
local name = player and player:get_player_name() or ""
|
|
|
|
|
2023-01-12 20:22:36 +01:00
|
|
|
local protected_pos = minetest.is_area_protected(pos1, pos2, name, interval)
|
|
|
|
if protected_pos then
|
|
|
|
minetest.record_protection_violation(protected_pos, name)
|
|
|
|
return true
|
2023-01-06 21:07:07 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-01-06 21:58:56 +01:00
|
|
|
--[[Check for a protection violation on a single position.
|
|
|
|
--
|
|
|
|
-- @param position A position table to check for protection violation.
|
2023-01-12 20:22:36 +01:00
|
|
|
-- @param player The player performing the action.
|
2023-01-06 21:58:56 +01:00
|
|
|
--
|
2023-01-12 20:22:36 +01:00
|
|
|
-- @return true on protection violation detection. false otherwise.
|
2023-01-06 21:58:56 +01:00
|
|
|
]]
|
2023-01-12 20:22:36 +01:00
|
|
|
function mcl_util.check_position_protection(position, player)
|
2023-01-06 21:58:56 +01:00
|
|
|
local name = player and player:get_player_name() or ""
|
|
|
|
|
|
|
|
if minetest.is_protected(position, name) then
|
|
|
|
minetest.record_protection_violation(position, name)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
2023-02-12 08:57:04 +01:00
|
|
|
|
2023-11-19 01:24:54 +01:00
|
|
|
---Move items from one inventory list to another, drop items that do not fit in provided pos and direction.
|
|
|
|
---@param src_inv mt.InvRef
|
|
|
|
---@param src_listname string
|
|
|
|
---@param out_inv mt.InvRef
|
|
|
|
---@param out_listname string
|
|
|
|
---@param pos mt.Vector Position to throw items at
|
|
|
|
---@param dir? mt.Vector Direction to throw items in
|
|
|
|
---@param insta_collect? boolean Enable instant collection, let players collect dropped items instantly. Default `false`
|
|
|
|
function mcl_util.move_list(src_inv, src_listname, out_inv, out_listname, pos, dir, insta_collect)
|
|
|
|
local src_list = src_inv:get_list(src_listname)
|
|
|
|
|
|
|
|
if not src_list then return end
|
|
|
|
for i, stack in ipairs(src_list) do
|
|
|
|
if out_inv:room_for_item(out_listname, stack) then
|
|
|
|
out_inv:add_item(out_listname, stack)
|
|
|
|
else
|
|
|
|
local p = vector.copy(pos)
|
|
|
|
p.x = p.x + (math.random(1, 3) * 0.2)
|
|
|
|
p.z = p.z + (math.random(1, 3) * 0.2)
|
|
|
|
|
|
|
|
local obj = minetest.add_item(p, stack)
|
|
|
|
if obj then
|
|
|
|
if dir then
|
|
|
|
local v = vector.copy(dir)
|
|
|
|
v.x = v.x * 4
|
|
|
|
v.y = v.y * 4 + 2
|
|
|
|
v.z = v.z * 4
|
|
|
|
obj:set_velocity(v)
|
2023-12-30 04:53:17 +01:00
|
|
|
mcl_util.mcl_log("item velocity calculated "..vector.to_string(v), "[mcl_util]")
|
2023-11-19 01:24:54 +01:00
|
|
|
end
|
|
|
|
if not insta_collect then
|
|
|
|
obj:get_luaentity()._insta_collect = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
stack:clear()
|
|
|
|
src_inv:set_stack(src_listname, i, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---Move items from a player's inventory list to its main inventory list, drop items that do not fit in front of him.
|
|
|
|
---@param player mt.PlayerObjectRef
|
|
|
|
---@param src_listname string
|
|
|
|
function mcl_util.move_player_list(player, src_listname)
|
|
|
|
mcl_util.move_list(player:get_inventory(), src_listname, player:get_inventory(), "main",
|
|
|
|
vector.offset(player:get_pos(), 0, 1.2, 0),
|
|
|
|
player:get_look_dir(), false)
|
|
|
|
end
|
2023-12-24 05:48:41 +01:00
|
|
|
|
|
|
|
function mcl_util.is_it_christmas()
|
|
|
|
local date = os.date("*t")
|
|
|
|
if date.month == 12 and date.day >= 24 or date.month == 1 and date.day <= 7 then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2024-06-22 02:47:33 +02:00
|
|
|
|
|
|
|
function mcl_util.to_bool(val)
|
|
|
|
if not val then return false end
|
|
|
|
return true
|
|
|
|
end
|
2024-09-09 20:05:59 +02:00
|
|
|
|
|
|
|
if not vector.in_area then
|
|
|
|
-- backport from minetest 5.8, can be removed when the minimum version is 5.8
|
|
|
|
vector.in_area = function(pos, min, max)
|
|
|
|
return (pos.x >= min.x) and (pos.x <= max.x) and
|
|
|
|
(pos.y >= min.y) and (pos.y <= max.y) and
|
|
|
|
(pos.z >= min.z) and (pos.z <= max.z)
|
|
|
|
end
|
|
|
|
end
|