Skip to content

Commit

Permalink
Implement non-mapgen VoxelManip functionality (#82)
Browse files Browse the repository at this point in the history
* Implement non-mapgen VoxelManip functionality

* Improve VM unloaded areas

* Simplify code

* Add missing mineunit dependency

* Fix get_node_at

* Improve get_node_at

* Simplify VoxelManip unloaded area handling

* Add description in voxelmanip.lua

* Add missing dependency statement

* Simplify VoxelManip writing

* Do some simple optimization for VoxelManip

* Fix minetest.get_node param1

* Add VoxelManip tests

* Round VoxelManip node positions

* Simplify iteration in VoxelManip test

* Use vector.sort for VoxelManip box sorting
  • Loading branch information
TurkeyMcMac authored May 29, 2022
1 parent e70f06e commit db70bcd
Show file tree
Hide file tree
Showing 4 changed files with 389 additions and 21 deletions.
2 changes: 1 addition & 1 deletion core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ _G.minetest.after = noop
_G.minetest.find_nodes_with_meta = _G.world.find_nodes_with_meta
_G.minetest.find_nodes_in_area = _G.world.find_nodes_in_area
_G.minetest.get_node_or_nil = _G.world.get_node
_G.minetest.get_node = function(pos) return minetest.get_node_or_nil(pos) or {name="ignore",param2=0} end
_G.minetest.get_node = function(pos) return minetest.get_node_or_nil(pos) or {name="ignore",param1=0,param2=0} end
_G.minetest.dig_node = function(pos) return world.on_dig(pos) and true or false end
_G.minetest.remove_node = _G.world.remove_node

Expand Down
98 changes: 98 additions & 0 deletions spec/voxelmanip_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

-- For self tests package path must be set in a way that makes package loaders search current directory first
package.path = "./?.lua;../?/init.lua;../?.lua;" --.. package.path

describe("VoxelManip", function()

require("mineunit")
sourcefile("voxelmanip")
mineunit("core")
mineunit("common/vector")
mineunit("world")

mineunit:set_current_modname("test")
minetest.register_node("test:node", {})
minetest.register_node("test:node2", {})

local vm

before_each(function()
world.clear()
world.layout({
{vector.new(0, 0, 0), "test:node"},
{vector.new(20, 0, 0), "test:node"},
{vector.new(0, 20, 0), "test:node"},
})
vm = VoxelManip(vector.new(0, 0, 0), vector.new(20, 0, 0))
vm:read_from_map(vector.new(0, 0, 0), vector.new(0, 20, 0))
end)

it("nodes", function()
assert.same({name = "test:node", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(0, 0, 0)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(1, 1, 1)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(17, 17, 0)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(100, 100, 100)))
vm:set_node_at(vector.new(0, 0, 0), {name = "test:node2"})
vm:set_node_at(vector.new(1, 1, 1), {name = "test:node2"})
vm:set_node_at(vector.new(17, 17, 0), {name = "test:node2"})
vm:set_node_at(vector.new(100, 100, 100), {name = "test:node2"})
assert.same({name = "test:node2", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(0, 0, 0)))
assert.same({name = "test:node2", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(1, 1, 1)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(17, 17, 0)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(100, 100, 100)))
end)

it("get data", function()
local data = vm:get_data()
local param1_data = vm:get_light_data()
local param2_data = vm:get_param2_data()
local emin, emax = vm:get_emerged_area()
local i = 1
for z = emin.z, emax.z do
for y = emin.y, emax.y do
for x = emin.x, emax.x do
local node1 = vm:get_node_at(vector.new(x, y, z))
local node2 = {
name = minetest.get_name_from_content_id(data[i]),
param1 = param1_data[i], param2 = param2_data[i],
}
assert.same(node1, node2)
i = i + 1
end
end
end
end)

it("set data", function()
local data = {}
local param1_data = {}
local param2_data = {}
local content_id = minetest.get_content_id("test:node2")
local emin, emax = vm:get_emerged_area()
local volume = (emax.x - emin.x + 1) * (emax.y - emin.y + 1) * (emax.z - emin.z + 1)
for i = 1, volume do
data[i] = content_id
param1_data[i] = 2
param2_data[i] = 1
end
vm:set_data(data)
vm:set_light_data(param1_data)
vm:set_param2_data(param2_data)
assert.same({name = "test:node2", param1 = 2, param2 = 1}, vm:get_node_at(vector.new(0, 0, 0)))
assert.same({name = "test:node2", param1 = 2, param2 = 1}, vm:get_node_at(vector.new(1, 1, 1)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(17, 17, 0)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, vm:get_node_at(vector.new(100, 100, 100)))
end)

it("write", function()
vm:set_node_at(vector.new(0, 0, 0), {name = "test:node2"})
vm:set_node_at(vector.new(1, 1, 1), {name = "test:node2"})
vm:set_node_at(vector.new(17, 17, 0), {name = "test:node2"})
vm:set_node_at(vector.new(100, 100, 100), {name = "test:node2"})
vm:write_to_map()
assert.same({name = "test:node2", param1 = 0, param2 = 0}, minetest.get_node(vector.new(0, 0, 0)))
assert.same({name = "test:node2", param1 = 0, param2 = 0}, minetest.get_node(vector.new(1, 1, 1)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, minetest.get_node(vector.new(17, 17, 0)))
assert.same({name = "ignore", param1 = 0, param2 = 0}, minetest.get_node(vector.new(100, 100, 100)))
end)
end)
Loading

0 comments on commit db70bcd

Please sign in to comment.