nodecore-cd2025/mods/nc_doors/craft_catapult.lua

166 lines
4.4 KiB
Lua
Raw Normal View History

2020-09-29 21:13:31 -04:00
-- LUALOCALS < ---------------------------------------------------------
local ItemStack, minetest, nodecore, pairs, vector
= ItemStack, minetest, nodecore, pairs, vector
2020-09-29 21:13:31 -04:00
-- LUALOCALS > ---------------------------------------------------------
local presstoolcaps = {}
minetest.after(0, function()
for name, def in pairs(minetest.registered_items) do
if def.tool_capabilities then
presstoolcaps[name] = "dig"
elseif def.tool_head_capabilities then
presstoolcaps[name] = def.tool_head_capabilities.groupcaps
end
end
end)
2020-09-29 22:27:05 -04:00
local function checktarget(data, stack)
2020-09-29 21:28:34 -04:00
local target = vector.subtract(vector.multiply(
data.pointed.under, 2), data.pointed.above)
local node = minetest.get_node(target)
local def = minetest.registered_items[node.name] or {walkable = true}
-- Inject item into available storebox
2020-09-29 21:28:34 -04:00
if def.groups and def.groups.visinv and (def.groups.is_stack_only
or def.storebox_access and def.storebox_access({
type = "node",
above = target,
under = vector.subtract(vector.multiply(
target, 2), data.pointed.under)
})) then
-- Never insert from bottom, so we can always dig
local dir = vector.subtract(data.pointed.under, data.pointed.above)
if dir.y <= 0 then
local one = ItemStack(stack:to_string())
one:set_count(1)
local tstack = nodecore.stack_get(target)
if tstack:item_fits(one) then
data.intostorebox = target
return true
end
end
end
-- Try to dig item
local caps = presstoolcaps[stack:get_name()]
if caps == "dig" and def and def.groups
and nodecore.tool_digs(stack, def.groups) then
2020-10-05 21:34:28 -04:00
data.pressdig = {
pos = target,
tool = stack
}
2020-09-29 21:28:34 -04:00
return true
end
-- Eject item as entity
if not def.walkable then return true end
if not caps then return end
local pumdata = {
action = "pummel",
pos = target,
pointed = {
type = "node",
above = data.pointed.under,
under = target
},
node = node,
nodedef = def,
duration = 3600,
toolgroupcaps = caps
}
local recipe = nodecore.craft_search(target, node, pumdata)
data.presscommit = recipe
return recipe
2020-09-29 21:28:34 -04:00
end
2020-09-29 21:13:31 -04:00
local function doitemeject(pos, data)
2020-09-29 23:27:41 -04:00
if data.pressdig then
nodecore.witness(pos, "door dig")
2020-10-05 21:34:28 -04:00
nodecore.machine_digging = data.pressdig
return minetest.dig_node(data.pressdig.pos)
2020-09-29 23:27:41 -04:00
end
if data.presscommit then
nodecore.witness(pos, "door pummel")
return data.presscommit()
end
2020-09-29 21:13:31 -04:00
local stack = nodecore.stack_get(pos)
if (not stack) or stack:is_empty() then return end
local one = ItemStack(stack:to_string())
one:set_count(1)
2020-09-29 21:28:34 -04:00
if data.intostorebox then
2020-09-29 23:27:41 -04:00
nodecore.witness(pos, "door store")
2020-09-29 21:28:34 -04:00
one = nodecore.stack_add(data.intostorebox, one)
if not one:is_empty() then return end
else
local ctr = {
x = data.axis.x ~= 0 and data.axis.x or pos.x,
y = data.axis.y ~= 0 and data.axis.y or pos.y,
z = data.axis.z ~= 0 and data.axis.z or pos.z
}
local vel = vector.add(
vector.subtract(pos, ctr),
vector.subtract(data.pointed.under, data.pointed.above)
)
local doorlv = minetest.get_item_group(minetest.get_node(
data.pointed.above).name, "door") or 0
nodecore.item_eject(
vector.add(pos, vector.multiply(vel, 0.25)),
one, 0, 1, vector.multiply(vel, 2 + doorlv)
)
end
2020-09-29 21:13:31 -04:00
nodecore.witness(pos, "door catapult")
stack:take_item(1)
if stack:is_empty() and nodecore.node_group("is_stack_only", pos) then
return minetest.remove_node(pos)
end
return nodecore.stack_set(pos, stack)
end
nodecore.register_craft({
action = "press",
label = "eject item",
priority = 2,
2020-09-29 21:13:31 -04:00
nodes = {
{match = {stacked = true, count = false}}
},
check = function(pos, data)
local stack = nodecore.stack_get(pos)
if (not stack) or stack:is_empty() then return end
2020-09-29 22:27:05 -04:00
return checktarget(data, stack)
2020-09-29 21:13:31 -04:00
end,
after = doitemeject
})
nodecore.register_craft({
action = "press",
label = "eject from storebox",
priority = -1,
nodes = {
{match = {groups = {storebox = true}}}
},
check = function(pos, data)
local stack = nodecore.stack_get(pos)
if (not stack) or stack:is_empty() then return end
2020-09-29 22:27:05 -04:00
if not checktarget(data, stack) then return end
2020-09-29 21:13:31 -04:00
2020-09-29 21:28:34 -04:00
local pt = data.pointed
2020-09-29 21:13:31 -04:00
local node = minetest.get_node(pt.under)
local def = minetest.registered_items[node.name] or {}
if not def.storebox_access then return end
local access = {
type = "node",
above = vector.add(vector.multiply(
2020-09-29 21:28:34 -04:00
vector.subtract(pt.above, pt.under),
-1), pt.under),
2020-09-29 21:13:31 -04:00
under = pt.under
}
return def.storebox_access(access)
end,
after = doitemeject
})