optimize removal and placement range
This commit is contained in:
parent
934756d4cd
commit
9a72a7518f
@ -95,61 +95,37 @@ minetest.register_tool("building_lib:place", {
|
||||
end,
|
||||
on_use = function(itemstack, player)
|
||||
local playername = player:get_player_name()
|
||||
local pointed_mapblock_pos = building_lib.get_pointed_mapblock(player)
|
||||
local rotation = building_lib.get_build_rotation(player)
|
||||
|
||||
local placed_building_info, placed_building_origin = building_lib.get_placed_building_info(pointed_mapblock_pos)
|
||||
if placed_building_info then
|
||||
-- use origin and rotation of existing pointed-at building
|
||||
pointed_mapblock_pos = placed_building_origin
|
||||
rotation = placed_building_info.rotation
|
||||
end
|
||||
|
||||
local meta = itemstack:get_meta()
|
||||
local buildingname = meta:get_string("buildingname")
|
||||
building_lib.build(pointed_mapblock_pos, playername, buildingname, rotation)
|
||||
:catch(function(err)
|
||||
minetest.chat_send_player(playername, err)
|
||||
end)
|
||||
|
||||
local building_def, mb_pos1, _, rotation = building_lib.get_next_buildable_position(player, buildingname)
|
||||
|
||||
if building_def then
|
||||
building_lib.build(mb_pos1, playername, buildingname, rotation)
|
||||
:catch(function(err)
|
||||
minetest.chat_send_player(playername, err)
|
||||
end)
|
||||
end
|
||||
end,
|
||||
on_step = function(itemstack, player)
|
||||
local playername = player:get_player_name()
|
||||
local pointed_mapblock_pos = building_lib.get_pointed_mapblock(player)
|
||||
local rotation = building_lib.get_build_rotation(player)
|
||||
|
||||
local placed_building_info, placed_building_origin = building_lib.get_placed_building_info(pointed_mapblock_pos)
|
||||
if placed_building_info then
|
||||
-- use origin and rotation of existing pointed-at building
|
||||
pointed_mapblock_pos = placed_building_origin
|
||||
rotation = placed_building_info.rotation
|
||||
end
|
||||
|
||||
local meta = itemstack:get_meta()
|
||||
local buildingname = meta:get_string("buildingname")
|
||||
local building_def = building_lib.get_building(buildingname)
|
||||
if not building_def then
|
||||
|
||||
local building_def, mb_pos1, mb_pos2, rotation = building_lib.get_next_buildable_position(player, buildingname)
|
||||
if building_def then
|
||||
building_lib.show_preview(
|
||||
playername,
|
||||
"building_lib_place.png",
|
||||
"#00ff00",
|
||||
building_def,
|
||||
mb_pos1,
|
||||
mb_pos2,
|
||||
rotation
|
||||
)
|
||||
else
|
||||
building_lib.clear_preview(playername)
|
||||
return
|
||||
end
|
||||
|
||||
local size = building_lib.get_building_size(building_def, rotation)
|
||||
local mapblock_pos2 = vector.add(pointed_mapblock_pos, vector.subtract(size, 1))
|
||||
|
||||
local color = "#00ff00"
|
||||
local can_build = building_lib.can_build(pointed_mapblock_pos, playername, building_def.name, rotation)
|
||||
if not can_build then
|
||||
color = "#ffff00"
|
||||
end
|
||||
|
||||
building_lib.show_preview(
|
||||
playername,
|
||||
"building_lib_place.png",
|
||||
color,
|
||||
building_def,
|
||||
pointed_mapblock_pos,
|
||||
mapblock_pos2,
|
||||
rotation
|
||||
)
|
||||
end,
|
||||
on_deselect = function(_, player)
|
||||
local playername = player:get_player_name()
|
||||
|
53
common.lua
53
common.lua
@ -53,3 +53,56 @@ end
|
||||
function building_lib.get_pointed_mapblock(player)
|
||||
return mapblock_lib.get_pointed_position(player, 2)
|
||||
end
|
||||
|
||||
local min_range = 1
|
||||
local max_range = 10
|
||||
|
||||
function building_lib.get_next_buildable_position(player, buildingname)
|
||||
local playername = player:get_player_name()
|
||||
|
||||
for range=min_range,max_range do
|
||||
local pointed_mapblock_pos = mapblock_lib.get_pointed_position(player, range)
|
||||
local rotation = building_lib.get_build_rotation(player)
|
||||
|
||||
local placed_building_info, placed_building_origin = building_lib.get_placed_building_info(pointed_mapblock_pos)
|
||||
if placed_building_info then
|
||||
-- use origin and rotation of existing pointed-at building
|
||||
pointed_mapblock_pos = placed_building_origin
|
||||
rotation = placed_building_info.rotation
|
||||
end
|
||||
|
||||
local building_def = building_lib.get_building(buildingname)
|
||||
if building_def then
|
||||
local size = building_lib.get_building_size(building_def, rotation)
|
||||
local mapblock_pos2 = vector.add(pointed_mapblock_pos, vector.subtract(size, 1))
|
||||
|
||||
local can_build = building_lib.can_build(pointed_mapblock_pos, playername, building_def.name, rotation)
|
||||
if can_build then
|
||||
return building_def, pointed_mapblock_pos, mapblock_pos2, rotation
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function building_lib.get_next_removable_position(player)
|
||||
for range=min_range,max_range do
|
||||
local pointed_mapblock_pos = mapblock_lib.get_pointed_position(player, range)
|
||||
|
||||
local building_info, origin = building_lib.get_placed_building_info(pointed_mapblock_pos)
|
||||
if building_info then
|
||||
local building_def = building_lib.get_building(building_info.name)
|
||||
|
||||
local size = building_lib.get_building_size(building_def, building_info.rotation or 0)
|
||||
local mapblock_pos2 = vector.add(origin, vector.subtract(size, 1))
|
||||
|
||||
local can_remove = building_lib.can_remove(origin)
|
||||
if can_remove then
|
||||
return building_def, origin, mapblock_pos2, building_info.rotation
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
@ -5,42 +5,29 @@ minetest.register_tool("building_lib:remove", {
|
||||
stack_max = 1,
|
||||
range = 0,
|
||||
on_use = function(_, player)
|
||||
local mapblock_pos = building_lib.get_pointed_mapblock(player)
|
||||
local success, err = building_lib.remove(mapblock_pos)
|
||||
local _, mb_pos1 = building_lib.get_next_removable_position(player)
|
||||
|
||||
local success, err = building_lib.remove(mb_pos1)
|
||||
if not success then
|
||||
minetest.chat_send_player(player:get_player_name(), err)
|
||||
end
|
||||
end,
|
||||
on_step = function(_, player)
|
||||
local playername = player:get_player_name()
|
||||
local pointed_mapblock_pos = building_lib.get_pointed_mapblock(player)
|
||||
|
||||
local building_info, origin = building_lib.get_placed_building_info(pointed_mapblock_pos)
|
||||
if not building_info then
|
||||
local building_def, mb_pos1, mb_pos2, rotation = building_lib.get_next_removable_position(player)
|
||||
if building_def then
|
||||
building_lib.show_preview(
|
||||
playername,
|
||||
"building_lib_remove.png",
|
||||
"#ff0000",
|
||||
building_def,
|
||||
mb_pos1,
|
||||
mb_pos2,
|
||||
rotation
|
||||
)
|
||||
else
|
||||
building_lib.clear_preview(playername)
|
||||
return
|
||||
end
|
||||
|
||||
local building_def = building_lib.get_building(building_info.name)
|
||||
|
||||
local size = building_lib.get_building_size(building_def, building_info.rotation or 0)
|
||||
local mapblock_pos2 = vector.add(origin, vector.subtract(size, 1))
|
||||
|
||||
local color = "#ff0000"
|
||||
local can_remove = building_lib.can_remove(origin)
|
||||
if not can_remove then
|
||||
color = "#ffff00"
|
||||
end
|
||||
|
||||
building_lib.show_preview(
|
||||
playername,
|
||||
"building_lib_remove.png",
|
||||
color,
|
||||
building_def,
|
||||
origin,
|
||||
mapblock_pos2,
|
||||
building_info.rotation
|
||||
)
|
||||
end,
|
||||
on_deselect = function(_, player)
|
||||
local playername = player:get_player_name()
|
||||
|
Loading…
x
Reference in New Issue
Block a user