From 43d1b6df2f847685c5c67e5eb408f32a9a516d21 Mon Sep 17 00:00:00 2001 From: Aaron Suen Date: Sat, 3 Jul 2021 10:42:45 -0400 Subject: [PATCH] Genericize sand-raking API, add gravel support --- mods/nc_writing/raking.lua | 168 ++++++++++++++++++++----------------- 1 file changed, 90 insertions(+), 78 deletions(-) diff --git a/mods/nc_writing/raking.lua b/mods/nc_writing/raking.lua index f178f7fc..7905a6bc 100644 --- a/mods/nc_writing/raking.lua +++ b/mods/nc_writing/raking.lua @@ -1,84 +1,96 @@ -- LUALOCALS < --------------------------------------------------------- -local math, minetest, nodecore, vector - = math, minetest, nodecore, vector -local math_pi - = math.pi +local math, minetest, nodecore, string, vector + = math, minetest, nodecore, string, vector +local math_pi, string_gsub, string_lower + = math.pi, string.gsub, string.lower -- LUALOCALS > --------------------------------------------------------- local modname = minetest.get_current_modname() -local sandname = "nc_terrain:sand" -local sanddef = minetest.registered_items[sandname] or {} -local basedef = { - description = "Raked Sand", - paramtype2 = "facedir", - falling_replacement = sandname, - silktouch_as = sandname, - on_door_conveyed = function(pos) - return minetest.set_node(pos, {name = sandname}) - end -} -minetest.register_node(modname .. ":sand_raked", - nodecore.underride({ - tiles = { - sanddef.tiles[1] .. "^" .. modname - .. "_raking_linear.png", - sanddef.tiles[1], - sanddef.tiles[1] .. "^" .. modname - .. "_raking_side.png" - }, - on_place = function(itemstack, placer, pointed_thing) - return minetest.rotate_and_place( - itemstack, placer, pointed_thing, - false, {force_floor = true}) - end - }, basedef, sanddef)) -minetest.register_node(modname .. ":sand_raked_nexus", - nodecore.underride({ - tiles = { - sanddef.tiles[1] .. "^" .. modname - .. "_raking_nexus.png", - sanddef.tiles[1], - sanddef.tiles[1] .. "^" .. modname - .. "_raking_side.png" - } - }, basedef, sanddef)) - -nodecore.register_craft({ - label = "rake sand", - action = "pummel", - wield = {groups = {rakey = true}}, - duration = 0.5, - normal = {y = 1}, - indexkeys = {"group:sand"}, - nodes = {{match = {groups = {sand = true, falling_repose = false}}}}, - after = function(pos, data) - if not (data.crafter and data.crafter.getpos - and data.crafter.get_look_horizontal) then return end - - local newnode - local ppos = data.crafter:get_pos() - ppos.y = pos.y - if vector.distance(pos, ppos) < 0.4 then - newnode = { - name = modname .. ":sand_raked_nexus", - param2 = 0 - } - else - local dir = data.crafter:get_look_horizontal() - while dir >= math_pi * 3/4 do dir = dir - math_pi end - dir = minetest.yaw_to_dir(dir + math_pi / 4) - newnode = { - name = modname .. ":sand_raked", - param2 = minetest.dir_to_facedir(dir) - } - end - - local node = data.node or minetest.get_node(pos) - if node.name == newnode.name and node.param2 == newnode.param2 then - newnode = {name = sandname} - end - nodecore.wear_wield(data.crafter, {snappy = 1}, 1) - return nodecore.set_loud(pos, newnode) +function nodecore.register_raked(basename, desc, recipematch, recipeidx) + local name = string_gsub(string_lower(desc), "%W", "_") + local basedef = minetest.registered_items[basename] or {} + local commondef = { + description = "Raked " .. desc, + paramtype2 = "facedir", + falling_replacement = basename, + silktouch_as = basename, + on_door_conveyed = function(pos) + return minetest.set_node(pos, {name = basename}) end - }) + } + local linearname = modname .. ":" .. name .. "_raked" + minetest.register_node(linearname, + nodecore.underride({ + tiles = { + basedef.tiles[1] .. "^" .. modname + .. "_raking_linear.png", + basedef.tiles[1], + basedef.tiles[1] .. "^" .. modname + .. "_raking_side.png" + }, + on_place = function(itemstack, placer, pointed_thing) + return minetest.rotate_and_place( + itemstack, placer, pointed_thing, + false, {force_floor = true}) + end + }, commondef, basedef)) + + local nexusname = modname .. ":" .. name .. "_raked_nexus" + minetest.register_node(nexusname, + nodecore.underride({ + tiles = { + basedef.tiles[1] .. "^" .. modname + .. "_raking_nexus.png", + basedef.tiles[1], + basedef.tiles[1] .. "^" .. modname + .. "_raking_side.png" + } + }, commondef, basedef)) + + nodecore.register_craft({ + label = "rake " .. name, + action = "pummel", + wield = {groups = {rakey = true}}, + duration = 0.5, + normal = {y = 1}, + indexkeys = recipeidx, + nodes = {{match = recipematch}}, + after = function(pos, data) + if not (data.crafter and data.crafter.getpos + and data.crafter.get_look_horizontal) then return end + + local newnode + local ppos = data.crafter:get_pos() + ppos.y = pos.y + if vector.distance(pos, ppos) < 0.4 then + newnode = { + name = nexusname, + param2 = 0 + } + else + local dir = data.crafter:get_look_horizontal() + while dir >= math_pi * 3/4 do dir = dir - math_pi end + dir = minetest.yaw_to_dir(dir + math_pi / 4) + newnode = { + name = linearname, + param2 = minetest.dir_to_facedir(dir) + } + end + + local node = data.node or minetest.get_node(pos) + if node.name == newnode.name and node.param2 == newnode.param2 then + newnode = {name = basename} + end + nodecore.wear_wield(data.crafter, {snappy = 1}, 1) + return nodecore.set_loud(pos, newnode) + end + }) +end + +nodecore.register_raked("nc_terrain:sand", "Sand", + {groups = {sand = true, falling_repose = false}}, + {"group:sand"}) +nodecore.register_raked("nc_terrain:gravel", "Gravel", + {groups = {gravel = true, falling_repose = false}}, + {"group:gravel"})