break out tool functions and track rotation
This commit is contained in:
parent
a9fdfc98dd
commit
6a7431215b
@ -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
|
29
create_tool.spec.lua
Normal file
29
create_tool.spec.lua
Normal file
@ -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)
|
2
init.lua
2
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
|
@ -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)
|
||||
|
||||
|
35
rotate_tool.lua
Normal file
35
rotate_tool.lua
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user