build_over fix and tests

This commit is contained in:
BuckarooBanzay 2022-11-10 08:09:26 +01:00
parent 03db7c71ce
commit 6e7ccca254
3 changed files with 31 additions and 9 deletions

View File

@ -35,15 +35,14 @@ function building_lib.can_build(mapblock_pos, _, building_name, rotation)
return false, message or "size check '" .. building_def.placement .. "' failed"
end
-- true if the existing building can be built over
local build_over_mode = false
-- check if we can build over other buildings
if building_def.build_over then
local other_building_info, origin = building_lib.get_placed_building_info(mapblock_pos)
if other_building_info then
-- other building exists, check if it matches
if not vector.equals(other_building_info.size or {x=1,y=1,z=1}, size) then
return false, "Existing building has different size"
end
if not vector.equals(origin, mapblock_pos) then
return false, "Placement-origin mismatch"
end
@ -74,8 +73,9 @@ function building_lib.can_build(mapblock_pos, _, building_name, rotation)
if not matches then
-- can't build over pointed building
-- TODO: proper pointed position
return false, "Existing building can't be built over"
else
build_over_mode = true
end
end
end
@ -87,9 +87,12 @@ function building_lib.can_build(mapblock_pos, _, building_name, rotation)
break
end
local is_free = check_free(offset_mapblock_pos)
if not is_free then
return false, "Space already occupied at " .. minetest.pos_to_string(offset_mapblock_pos)
if not build_over_mode then
-- check if the area is free
local is_free = check_free(offset_mapblock_pos)
if not is_free then
return false, "Space already occupied at " .. minetest.pos_to_string(offset_mapblock_pos)
end
end
local success

View File

@ -9,6 +9,13 @@ building_lib.register_building("building_lib:dummy", {
placement = "dummy"
})
building_lib.register_building("building_lib:dummy_v2", {
placement = "dummy",
build_over = {
names = {"building_lib:dummy"}
}
})
mtt.register("build", function(callback)
local mapblock_pos = {x=0, y=0, z=0}
local building_name = "building_lib:dummy"
@ -34,6 +41,11 @@ mtt.register("build", function(callback)
assert(err)
assert(not success)
-- try to build over with v2 building
success, err = building_lib.can_build(mapblock_pos, playername, building_name .. "_v2", rotation)
assert(not err, err)
assert(success)
-- check
local info = building_lib.get_placed_building_info(mapblock_pos)
assert(info.name == building_name)

View File

@ -91,6 +91,14 @@ minetest.register_tool("building_lib:place", {
on_step = function(itemstack, player)
local playername = player:get_player_name()
local mapblock_pos1 = 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(mapblock_pos1)
if placed_building_info then
-- use origin and rotation of existing pointed-at building
mapblock_pos1 = placed_building_origin
rotation = placed_building_info.rotation
end
local meta = itemstack:get_meta()
local buildingname = meta:get_string("buildingname")
@ -100,7 +108,6 @@ minetest.register_tool("building_lib:place", {
return
end
local rotation = building_lib.get_build_rotation(player)
local size = building_lib.get_building_size(building_def, rotation)
local mapblock_pos2 = vector.add(mapblock_pos1, vector.subtract(size, 1))