Skip to content

Commit 3f13dc7

Browse files
committedJun 28, 2013
Add voxelarea.lua helper to builtin
1 parent 280946b commit 3f13dc7

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
 

Diff for: ‎builtin/builtin.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ dofile(minetest.get_modpath("__builtin").."/static_spawn.lua")
2525
dofile(minetest.get_modpath("__builtin").."/detached_inventory.lua")
2626
dofile(minetest.get_modpath("__builtin").."/falling.lua")
2727
dofile(minetest.get_modpath("__builtin").."/features.lua")
28-
28+
dofile(minetest.get_modpath("__builtin").."/voxelarea.lua")

Diff for: ‎builtin/voxelarea.lua

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
VoxelArea = {
2+
MinEdge = {x=1, y=1, z=1},
3+
MaxEdge = {x=0, y=0, z=0},
4+
ystride = 0,
5+
zstride = 0,
6+
}
7+
8+
function VoxelArea:new(o)
9+
o = o or {}
10+
setmetatable(o, self)
11+
self.__index = self
12+
13+
local e = o:getExtent()
14+
o.ystride = e.x
15+
o.zstride = e.x * e.y
16+
17+
return o
18+
end
19+
20+
function VoxelArea:getExtent()
21+
return {
22+
x = self.MaxEdge.x - self.MinEdge.x + 1,
23+
y = self.MaxEdge.y - self.MinEdge.y + 1,
24+
z = self.MaxEdge.z - self.MinEdge.z + 1,
25+
}
26+
end
27+
28+
function VoxelArea:getVolume()
29+
local e = self:getExtent()
30+
return e.x * e.y * e.z
31+
end
32+
33+
function VoxelArea:index(x, y, z)
34+
local i = (z - self.MinEdge.z) * self.zstride +
35+
(y - self.MinEdge.y) * self.ystride +
36+
(x - self.MinEdge.x) + 1
37+
return math.floor(i)
38+
end
39+
40+
function VoxelArea:indexp(p)
41+
local i = (p.z - self.MinEdge.z) * self.zstride +
42+
(p.y - self.MinEdge.y) * self.ystride +
43+
(p.x - self.MinEdge.x) + 1
44+
return math.floor(i)
45+
end
46+

Diff for: ‎doc/lua_api.txt

+10
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,16 @@ methods:
15721572
^ will be ignored
15731573
- update_liquids(): Update liquid flow
15741574

1575+
VoxelArea: A helper class for voxel areas
1576+
- Can be created via VoxelArea:new{MinEdge=pmin, MaxEdge=pmax}
1577+
- Coordinates are *inclusive*, like most other things in Minetest
1578+
methods:
1579+
- getExtent(): returns a 3d vector containing the size of the area formed by MinEdge and MaxEdge
1580+
- getVolume(): returns the volume of the area formed by MinEdge and MaxEdge
1581+
- index(x, y, z): returns the index of an absolute position in a flat array starting at 1
1582+
^ useful for things like VoxelManip, raw Schematic specifiers, PerlinNoiseMap:get2d/3dMap, and so on
1583+
- indexp(p): same as above, except takes a vector
1584+
15751585
Mapgen objects
15761586
---------------
15771587
A mapgen object is a construct used in map generation. Mapgen objects can be used by an on_generate

0 commit comments

Comments
 (0)
Please sign in to comment.