From 6a7431215bda005f44cce4f4b10a109914dc397d Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Thu, 7 Mar 2024 07:47:10 +0100 Subject: [PATCH] break out tool functions and track rotation --- create_tool.lua | 26 +++++++++++++++++++++----- create_tool.spec.lua | 29 +++++++++++++++++++++++++++++ init.lua | 2 ++ place_tool.lua | 24 +++++------------------- rotate_tool.lua | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 create_tool.spec.lua create mode 100644 rotate_tool.lua diff --git a/create_tool.lua b/create_tool.lua index 317ffb7..ca3cc1b 100644 --- a/create_tool.lua +++ b/create_tool.lua @@ -9,11 +9,27 @@ function pick_and_place.create_tool(pos1, pos2, name) local schematic = pick_and_place.serialize(pos1, pos2) tool_meta:set_string("schematic", schematic) - local desc = string.format( - "Placement tool '%s' (%d bytes, size: %s)", - name or "", #schematic, minetest.pos_to_string(size) - ) - tool_meta:set_string("description", desc) + -- set name + tool_meta:set_string("name", name) + + -- add rotation info (with respect to original in-world build) + tool_meta:set_int("rotation", 0) + + -- update description + pick_and_place.update_placement_tool_description(tool_meta) return tool +end + +function pick_and_place.update_placement_tool_description(tool_meta) + local name = tool_meta:get_string("name") + local size_str = tool_meta:get_string("size") + local schematic = tool_meta:get_string("schematic") + local rotation = tool_meta:get_int("rotation") + + local desc = string.format( + "Placement tool '%s' (%d bytes, rotation: %d°, size: %s)", + name or "", #schematic, rotation, size_str + ) + tool_meta:set_string("description", desc) end \ No newline at end of file diff --git a/create_tool.spec.lua b/create_tool.spec.lua new file mode 100644 index 0000000..d8c9e77 --- /dev/null +++ b/create_tool.spec.lua @@ -0,0 +1,29 @@ + +local pos1 = {x=0, y=0, z=0} +local pos2 = {x=10, y=10, z=20} + +mtt.emerge_area(pos1, pos2) + +mtt.register("create_tool and rotate", function(callback) + minetest.set_node(pos1, { name = "default:mese" }) + + local tool = pick_and_place.create_tool(pos1, pos2, "my build") + assert(tool) + local meta = tool:get_meta() + assert(meta:get_string("description")) + assert(meta:get_int("rotation") == 0) + + local success, err = pick_and_place.rotate_tool(tool, 90) + assert(success) + assert(not err) + assert(meta:get_string("description")) + assert(meta:get_int("rotation") == 90) + + success, err = pick_and_place.rotate_tool(tool, 270) + assert(success) + assert(not err) + assert(meta:get_string("description")) + assert(meta:get_int("rotation") == 0) + + callback() +end) \ No newline at end of file diff --git a/init.lua b/init.lua index 61024ad..e70cf47 100644 --- a/init.lua +++ b/init.lua @@ -4,6 +4,7 @@ pick_and_place = {} local MP = minetest.get_modpath("pick_and_place") dofile(MP .. "/common.lua") dofile(MP .. "/rotate.lua") +dofile(MP .. "/rotate_tool.lua") dofile(MP .. "/schematic_rotate.lua") dofile(MP .. "/schematic_flip.lua") dofile(MP .. "/schematic_orient.lua") @@ -24,6 +25,7 @@ dofile(MP .. "/preview.lua") dofile(MP .. "/craft.lua") if minetest.get_modpath("mtt") and mtt.enabled then + dofile(MP .. "/create_tool.spec.lua") dofile(MP .. "/encode.spec.lua") dofile(MP .. "/schematic_rotate.spec.lua") end \ No newline at end of file diff --git a/place_tool.lua b/place_tool.lua index 82a457f..2c55428 100644 --- a/place_tool.lua +++ b/place_tool.lua @@ -95,11 +95,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return false end - local itemstack = player:get_wielded_item() - if itemstack:get_name() ~= "pick_and_place:place" then - return true - end - local rotation = 0 if fields.deg90 then rotation = 90 @@ -110,26 +105,17 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end if rotation == 0 then + -- nothing to do return true end - local meta = itemstack:get_meta() - local schematic_data = meta:get_string("schematic") - local schematic, err = pick_and_place.decode_schematic(schematic_data) - if err then - minetest.chat_send_player(player:get_player_name(), "Schematic decode error: " .. err) + local itemstack = player:get_wielded_item() + local success, err = pick_and_place.rotate_tool(itemstack, rotation) + if not success then + minetest.chat_send_player(player:get_player_name(), "Rotation error: " .. err) return true end - -- rotate schematic - pick_and_place.schematic_rotate(schematic, rotation) - meta:set_string("schematic", pick_and_place.encode_schematic(schematic)) - - -- rotate size - local size = minetest.string_to_pos(meta:get_string("size")) - size = pick_and_place.rotate_size(size, rotation) - meta:set_string("size", minetest.pos_to_string(size)) - -- set tool player:set_wielded_item(itemstack) diff --git a/rotate_tool.lua b/rotate_tool.lua new file mode 100644 index 0000000..2b551cc --- /dev/null +++ b/rotate_tool.lua @@ -0,0 +1,35 @@ + +function pick_and_place.rotate_tool(itemstack, rotation) + if itemstack:get_name() ~= "pick_and_place:place" then + return false, "unexpected item" + end + + local meta = itemstack:get_meta() + local schematic_data = meta:get_string("schematic") + local schematic, err = pick_and_place.decode_schematic(schematic_data) + if err then + return false, "Schematic decode error: " .. err + end + + -- rotate schematic + pick_and_place.schematic_rotate(schematic, rotation) + meta:set_string("schematic", pick_and_place.encode_schematic(schematic)) + + -- set new rotation info + local old_rotation = meta:get_int("rotation") + local new_rotation = old_rotation + rotation + if new_rotation >= 360 then + new_rotation = new_rotation - 360 + end + meta:set_int("rotation", new_rotation) + + -- rotate size + local size = minetest.string_to_pos(meta:get_string("size")) + size = pick_and_place.rotate_size(size, rotation) + meta:set_string("size", minetest.pos_to_string(size)) + + -- update description + pick_and_place.update_placement_tool_description(meta) + + return true +end \ No newline at end of file