From 6a036e3ec2172f13497ac40de56d443e624d90d6 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 14 Mar 2023 13:27:52 +0100 Subject: [PATCH] build-over tests --- build_over.spec.lua | 55 +++++++++++++++++++++++++++++++++++++++++++++ conditions.lua | 12 ++++++---- init.lua | 1 + mtt.lua | 5 ++++- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 build_over.spec.lua diff --git a/build_over.spec.lua b/build_over.spec.lua new file mode 100644 index 0000000..384d6b3 --- /dev/null +++ b/build_over.spec.lua @@ -0,0 +1,55 @@ + +building_lib.register_building("building_lib:dummy_base", { + placement = "dummy", + size = { x=3, y=1, z=1 } +}) + +-- building can only be placed over "dummy" +building_lib.register_building("building_lib:dummy_extension", { + placement = "dummy", + size = { x=3, y=1, z=1 }, + conditions = { + { + ["*"] = { name = "building_lib:dummy_base" } + } + } +}) + +mtt.register("build-over", function(callback) + -- clear store + building_lib.store:clear() + + local mapblock_pos = {x=0, y=0, z=0} + local rotation = 0 + local playername = "singleplayer" + + -- build + local callback_called = false + local success, err = building_lib.build(mapblock_pos, playername, "building_lib:dummy_base", rotation, + function() callback_called = true end + ) + assert(not err) + assert(success) + assert(callback_called) + + -- build over (wrong angle) + success, err = building_lib.build(mapblock_pos, playername, "building_lib:dummy_extension", 90) + assert(err) + assert(not success) + + -- build over (wrong angle 2) + success, err = building_lib.build(mapblock_pos, playername, "building_lib:dummy_extension", 270) + assert(err) + assert(not success) + + -- build over (180° rotated) + callback_called = false + success, err = building_lib.build(mapblock_pos, playername, "building_lib:dummy_extension", 180, + function() callback_called = true end + ) + assert(not err) + assert(success) + assert(callback_called) + + callback() +end) diff --git a/conditions.lua b/conditions.lua index 59f0149..208034d 100644 --- a/conditions.lua +++ b/conditions.lua @@ -15,7 +15,7 @@ function building_lib.check_condition_table(map, mapblock_pos) local success, msg = check_condition(key, value, mapblock_pos) if not success then -- failure and in AND mode, return immediately - return false, msg or "condition failed: '" .. key .. "'" + return false, msg or "condition failed: '" .. key .. "' pos: " .. minetest.pos_to_string(mapblock_pos) end end return true @@ -50,17 +50,19 @@ end -- go through all condition groups and return true if any of them matches function building_lib.check_condition_groups(mapblock_pos1, mapblock_pos2, condition_groups) + local success, err for _, condition_group in ipairs(condition_groups or default_condition_groups) do - local success = building_lib.check_condition_group(mapblock_pos1, mapblock_pos2, condition_group) + success, err = building_lib.check_condition_group(mapblock_pos1, mapblock_pos2, condition_group) if success then return true end end - return false, "no matching condition found" + return false, "no matching condition found" .. (err and ", last error: " .. err or "") end function building_lib.check_condition_group(mapblock_pos1, mapblock_pos2, condition_group) local group_match = true + local success, err for selector, conditions in pairs(condition_group) do local it @@ -95,7 +97,7 @@ function building_lib.check_condition_group(mapblock_pos1, mapblock_pos2, condit break end - local success = building_lib.check_condition_table(conditions, mapblock_pos) + success, err = building_lib.check_condition_table(conditions, mapblock_pos) if not success then group_match = false break @@ -110,6 +112,8 @@ function building_lib.check_condition_group(mapblock_pos1, mapblock_pos2, condit if group_match then return true end + + return false, err end -- checks if a building with specified group is placed there already diff --git a/init.lua b/init.lua index 83def3d..8b2eb41 100644 --- a/init.lua +++ b/init.lua @@ -32,4 +32,5 @@ if minetest.get_modpath("mtt") and mtt.enabled then dofile(MP .. "/events.spec.lua") dofile(MP .. "/conditions.spec.lua") dofile(MP .. "/build.spec.lua") + dofile(MP .. "/build_over.spec.lua") end diff --git a/mtt.lua b/mtt.lua index 023d26f..5020cc0 100644 --- a/mtt.lua +++ b/mtt.lua @@ -1,5 +1,8 @@ building_lib.register_placement("dummy", { check = function() return true end, - get_size = function() return {x=1,y=1,z=1} end, + get_size = function(_, _, building_def, rotation) + local size = building_def.size or {x=1,y=1,z=1} + return mapblock_lib.rotate_size(size, rotation) + end, place = function(_, _, _, _, callback) callback() end }) \ No newline at end of file