From cd2de063e17a612955f1b39434e4e1ca5b2122ab Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Thu, 17 Nov 2022 20:57:06 +0100 Subject: [PATCH 1/3] use voxel manip for lv light illumination --- technic/machines/LV/lamp.lua | 44 +++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/technic/machines/LV/lamp.lua b/technic/machines/LV/lamp.lua index 86cc07b3..8adae8ed 100644 --- a/technic/machines/LV/lamp.lua +++ b/technic/machines/LV/lamp.lua @@ -29,16 +29,48 @@ minetest.register_node("technic:dummy_light_source", { groups = {not_in_creative_inventory = 1} }) +local content_id_light_source = minetest.get_content_id("technic:dummy_light_source") +local content_id_air = minetest.CONTENT_AIR local function illuminate(pos, active) - local pos1 = {x = pos.x - 3, y = pos.y - 1, z = pos.z - 3} - local pos2 = {x = pos.x + 3, y = pos.y - 3, z = pos.z + 3} + local pos1 = {x = pos.x - 3, y = pos.y - 3, z = pos.z - 3} + local pos2 = {x = pos.x + 3, y = pos.y - 1, z = pos.z + 3} + + -- prepare vmanip, voxel-area and node-data + local vm = minetest.get_voxel_manip() + local e1, e2 = vm:read_from_map(pos1, pos2) + local va = VoxelArea:new({MinEdge = e1, MaxEdge = e2}) + local node_data = vm:get_data() + + -- replacements + local src_node, dst_node + if active then + src_node = content_id_air + dst_node = content_id_light_source + else + src_node = content_id_light_source + dst_node = content_id_air + end - local find_node = active and "air" or "technic:dummy_light_source" - local set_node = {name = (active and "technic:dummy_light_source" or "air")} + -- dirty/changed flag + local dirty = false + + for x=pos1.x, pos2.x do + for y=pos1.y, pos2.y do + for z=pos1.z, pos2.z do + local index = va:index(x,y,z) + if node_data[index] == src_node then + node_data[index] = dst_node + dirty = true + end + end + end + end - for _,p in pairs(minetest.find_nodes_in_area(pos1, pos2, find_node)) do - minetest.set_node(p, set_node) + if dirty then + -- write data back to map if changed + vm:set_data(node_data) + vm:write_to_map() end end From 1829721758efd4d12b2d86767d4877cf5d98e27a Mon Sep 17 00:00:00 2001 From: OgelGames Date: Fri, 18 Nov 2022 15:09:57 +1100 Subject: [PATCH 2/3] shrink code --- technic/machines/LV/lamp.lua | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/technic/machines/LV/lamp.lua b/technic/machines/LV/lamp.lua index 8adae8ed..03113104 100644 --- a/technic/machines/LV/lamp.lua +++ b/technic/machines/LV/lamp.lua @@ -2,7 +2,6 @@ -- LV Lamp - a powerful light source. -- Illuminates a 7x7x3(H) volume below itself with light bright as the sun. - local S = technic.getter local desc = S("@1 Lamp", S("LV")) @@ -11,7 +10,6 @@ local unpowered_desc = S("@1 Unpowered", desc) local off_desc = S("@1 Off", desc) local demand = 50 - -- Invisible light source node used for illumination minetest.register_node("technic:dummy_light_source", { description = S("Dummy light source node"), @@ -29,46 +27,28 @@ minetest.register_node("technic:dummy_light_source", { groups = {not_in_creative_inventory = 1} }) -local content_id_light_source = minetest.get_content_id("technic:dummy_light_source") -local content_id_air = minetest.CONTENT_AIR +local cid_light = minetest.get_content_id("technic:dummy_light_source") +local cid_air = minetest.CONTENT_AIR local function illuminate(pos, active) local pos1 = {x = pos.x - 3, y = pos.y - 3, z = pos.z - 3} local pos2 = {x = pos.x + 3, y = pos.y - 1, z = pos.z + 3} - -- prepare vmanip, voxel-area and node-data local vm = minetest.get_voxel_manip() - local e1, e2 = vm:read_from_map(pos1, pos2) - local va = VoxelArea:new({MinEdge = e1, MaxEdge = e2}) + local va = VoxelArea(vm:read_from_map(pos1, pos2)) local node_data = vm:get_data() - -- replacements - local src_node, dst_node - if active then - src_node = content_id_air - dst_node = content_id_light_source - else - src_node = content_id_light_source - dst_node = content_id_air - end + local find_node = active and cid_air or cid_light + local set_node = active and cid_light or cid_air - -- dirty/changed flag local dirty = false - - for x=pos1.x, pos2.x do - for y=pos1.y, pos2.y do - for z=pos1.z, pos2.z do - local index = va:index(x,y,z) - if node_data[index] == src_node then - node_data[index] = dst_node + for i in va:iterp(pos1, pos2) do + if node_data[i] == find_node then + node_data[i] = set_node dirty = true end end - end - end - if dirty then - -- write data back to map if changed vm:set_data(node_data) vm:write_to_map() end From 19ef4ad490e7463733eb36857f5c408f5ddde9b2 Mon Sep 17 00:00:00 2001 From: OgelGames Date: Fri, 18 Nov 2022 15:18:04 +1100 Subject: [PATCH 3/3] oops don't use latest stuff --- technic/machines/LV/lamp.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/technic/machines/LV/lamp.lua b/technic/machines/LV/lamp.lua index 03113104..67f8218c 100644 --- a/technic/machines/LV/lamp.lua +++ b/technic/machines/LV/lamp.lua @@ -35,7 +35,8 @@ local function illuminate(pos, active) local pos2 = {x = pos.x + 3, y = pos.y - 1, z = pos.z + 3} local vm = minetest.get_voxel_manip() - local va = VoxelArea(vm:read_from_map(pos1, pos2)) + local emin, emax = vm:read_from_map(pos1, pos2) + local va = VoxelArea:new({MinEdge = emin, MaxEdge = emax}) local node_data = vm:get_data() local find_node = active and cid_air or cid_light