firs commit - proof of concept

This commit is contained in:
Kurtzmusch 2019-04-19 13:21:34 -03:00
commit 13631a917d
6 changed files with 773 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.swp

97
generate_map.lua Normal file
View File

@ -0,0 +1,97 @@
for key_int, val_bitmap_as_int in pairs( smallblocks.origin_bitmaps ) do
local bitmap_as_int = val_bitmap_as_int
local bitmap = util.integer_to_bitmap( bitmap_as_int )
local rotated_bitmap = bitmap
local facedir = 0
-- rotations around +y
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
for i = 1, 3, 1 do
rotated_bitmap = util.rotate_bitmap_around_plus_y( rotated_bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
end
rotated_bitmap = util.orient_bitmap_towards_plus_z( bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
for i = 1, 3, 1 do
rotated_bitmap = util.rotate_bitmap_around_plus_z( rotated_bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
end
rotated_bitmap = util.orient_bitmap_towards_minus_z( bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
for i = 1, 3, 1 do
rotated_bitmap = util.rotate_bitmap_around_minus_z( rotated_bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
end
rotated_bitmap = util.orient_bitmap_towards_plus_x( bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
for i = 1, 3, 1 do
rotated_bitmap = util.rotate_bitmap_around_plus_x( rotated_bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
end
rotated_bitmap = util.orient_bitmap_towards_minus_x( bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
for i = 1, 3, 1 do
rotated_bitmap = util.rotate_bitmap_around_minus_x( rotated_bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
end
rotated_bitmap = util.orient_bitmap_towards_minus_y( bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
for i = 1, 3, 1 do
rotated_bitmap = util.rotate_bitmap_around_minus_y( rotated_bitmap )
smallblocks.int_facedir_map[util.bitmap_to_integer( rotated_bitmap )] = facedir
smallblocks.int_name_map[util.bitmap_to_integer( rotated_bitmap )] = ""..bitmap_as_int
facedir = facedir + 1
end
end
for i = 1, 255, 1 do
minetest.log( "none", i.." maps to " .. smallblocks.int_name_map[i] .. " facedir:" .. smallblocks.int_facedir_map[i] )
end
local test_bitmap_plus_y = util.integer_to_bitmap( 1 )
local test_bitmap = test_bitmap_plus_y
test_bitmap = util.orient_bitmap_towards_minus_x( test_bitmap_plus_y )
minetest.log( "error", "tb-x" .. util.bitmap_to_integer( test_bitmap ) )
test_bitmap = util.orient_bitmap_towards_plus_x( test_bitmap_plus_y )
minetest.log( "error", "tb+x" .. util.bitmap_to_integer( test_bitmap ) )
test_bitmap = util.orient_bitmap_towards_minus_z( test_bitmap_plus_y )
minetest.log( "error", "tb-z" .. util.bitmap_to_integer( test_bitmap ) )
test_bitmap = util.orient_bitmap_towards_plus_z( test_bitmap_plus_y )
minetest.log( "error", "tb+z" .. util.bitmap_to_integer( test_bitmap ) )

40
generate_nodeboxes.lua Normal file
View File

@ -0,0 +1,40 @@
nodeboxes = {}
smallblocks.nodeboxes = nodeboxes
local function box_from_point( point )
local nodebox = { point.x, point.y, point.z, point.x+0.5, point.y+0.5, point.z+0.5 }
return nodebox
end
for key_int, val_bitmap_as_int in pairs(smallblocks.origin_bitmaps) do
local bitmap = util.integer_to_bitmap( val_bitmap_as_int )
local bitmap_as_array = util.bitmap_to_array3d( bitmap )
local index = 1
local max_index = 1
nodeboxes[key_int] = {}
minetest.log("none", "generating nodebox for ".. key_int.. ">>".. val_bitmap_as_int )
local gen_string = ""
local bitmap_string = ""
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
bitmap_string = bitmap_string .. bitmap_as_array[ix][iy][iz]
if( bitmap_as_array[ix][iy][iz] == 1) then
nodeboxes[key_int][index] = box_from_point( { x=ix/2-0.5, y=iy/2-0.5, z=iz/2-0.5} )
index = index + 1
gen_string = gen_string .."{"
for k, v in pairs(nodeboxes[key_int][index-1]) do
gen_string = gen_string..v..", "
end
gen_string = gen_string .."} "
end
end
end
end
minetest.log("none", bitmap_string)
minetest.log("none", gen_string )
end

72
generate_shapes.lua Normal file
View File

@ -0,0 +1,72 @@
origin_bitmaps = { [255] = nil }
smallblocks.origin_bitmaps = origin_bitmaps
function check_shapes( bitmap )
for i = 0, 255, 1 do
if( origin_bitmaps[i] ~= nil ) then
if( util.bitmap_to_integer( bitmap ) == origin_bitmaps[i] ) then
return true
end
end
end
return false
end
function check_all_rotations( bitmap_as_int )
local bitmap = util.integer_to_bitmap( bitmap_as_int )
local rotated_bitmap = bitmap
-- rotations around +y
for i = 1, 4, 1 do
rotated_bitmap = util.rotate_bitmap_around_plus_y( rotated_bitmap )
if( check_shapes( rotated_bitmap ) ) then return true end
end
rotated_bitmap = util.orient_bitmap_towards_plus_z( bitmap )
for i = 1, 4, 1 do
rotated_bitmap = util.rotate_bitmap_around_plus_z( rotated_bitmap )
if( check_shapes( rotated_bitmap ) ) then return true end
end
rotated_bitmap = util.orient_bitmap_towards_minus_z( bitmap )
for i = 1, 4, 1 do
rotated_bitmap = util.rotate_bitmap_around_minus_z( rotated_bitmap )
if( check_shapes( rotated_bitmap ) ) then return true end
end
rotated_bitmap = util.orient_bitmap_towards_plus_x( bitmap )
for i = 1, 4, 1 do
rotated_bitmap = util.rotate_bitmap_around_plus_x( rotated_bitmap )
if( check_shapes( rotated_bitmap ) ) then return true end
end
rotated_bitmap = util.orient_bitmap_towards_minus_x( bitmap )
for i = 1, 4, 1 do
rotated_bitmap = util.rotate_bitmap_around_minus_x( rotated_bitmap )
if( check_shapes( rotated_bitmap ) ) then return true end
end
rotated_bitmap = util.orient_bitmap_towards_minus_y( bitmap )
for i = 1, 4, 1 do
rotated_bitmap = util.rotate_bitmap_around_minus_y( rotated_bitmap )
if( check_shapes( rotated_bitmap ) ) then return true end
end
return false
end
local index = 1
for bitmap_as_int = 1, 255, 1 do
shape_exists = check_all_rotations( bitmap_as_int )
if( shape_exists == false ) then
origin_bitmaps[index] = bitmap_as_int
index = index + 1
minetest.log( "none", " found shape "..bitmap_as_int )
end
end

286
init.lua Normal file
View File

@ -0,0 +1,286 @@
--[[
k-smallblocks is a Minetest mod that adds smaller blocks to minetest aswell as its own node placement prediction/system
Copyright (C) 2019 Kurtzmusch
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
--]]
-- bitmaps are a binary representation of which of the 8 divisions of each node are filled by the material
-- origin bitmaps are the ones that can be rotated to make any other of the 255 possibilities
-- many bitmaps can be mapped to a single origin_bitmap, and each origin is a node registration
-- the origin bitmaps are generated by generate_origin_bitmaps.lua
-- 22 bitmaps will be generated and 22 nodes for each material will be registered
-- the mod creates bitmaps when placing nodes over other nodes of the same material,
-- then finds the correct node name and facedir using the maps int_facedir_map and int_name_map
-- all 'unnamed nodes' will be called "<number>_<material>"
-- 'named nodes' are the ones craftable and have a distinc name like "stair_<material>"
-- example of named node "stair_stonebrick"
-- exmaple of unnamed node "127_stonebrick"
-- named nodes apear in the creative inventory and can be crafted
-- unnamed nodes will not drop themselves when dug, but will drop the equivalent amount in smallblock_<material>
-- example: when a cobble inner stair is dug, it will drop 7 smallblock_cobble
-- inner and outer stairs are not craftable, but easily buildable
-- the mod doesnt work with screwdriver, the idea is that you never need to rotate anything because you can manually
-- dig a smallblock out of a node and replace it in other place in that node. textures always lign up aswell
-- definitions of named nodes will contain a recipe, logic for correct orientation when placed and dug
modpath = minetest.get_modpath("k_smallblocks")
smallblocks = {}
smallblocks.int_facedir_map = { [255]=0 } -- maps an int that represents a bitmap to a facedir
smallblocks.int_name_map = { [255]="" } -- maps an int that represents a bitmap to a name. many ints can map to a single name. a name is a string of the interger that represents the bitmap
smallblocks.name_int_map = {} -- inverse of the above map, might be usefull in the future
-- definitions of named (and craftable) nodes
smallblock = {}
halfslab = {}
slab = {}
stair = {}
common = {} -- common for all nodes
common.on_right_click = function( position, node, clicker, itemstack, pointed_thing )
-- find world coordinates for smallblock removal
local camera_position = clicker:get_pos()
camera_position.y = camera_position.y + clicker:get_properties().eye_height
local raycaster = Raycast(
camera_position,
vector.add( camera_position, vector.multiply(clicker:get_look_dir(), 5) ),
false, false
)
local intersected_thing = raycaster:next()
if( intersected_thing == nil ) then return end
--pushes the intersection inside the face to resolve ambiguity and get a rogh position in the world
local small_normal = vector.divide( intersected_thing.intersection_normal, 4 )
minetest.chat_send_all( small_normal.x .." ".. small_normal.y .." ".. small_normal.z )
small_normal = vector.multiply( small_normal, -1 ) -- reverse vector so it points inside the face
minetest.chat_send_all( small_normal.x .." ".. small_normal.y .." ".. small_normal.z )
local world_rough_position = vector.add( small_normal, intersected_thing.intersection_point )
local target_node_position = {
x = math.floor(world_rough_position.x+0.5),
y = math.floor(world_rough_position.y+0.5),
z = math.floor(world_rough_position.z+0.5),
}
local point_within_node = util.worldPoint_to_nodePoint( world_rough_position )
local bitmap = util.nodePoint_to_bitmap( point_within_node ) -- bitmap representing the smallblock to be removed
local underscore_index
local targetNode_fullName = minetest.get_node( target_node_position ).name
underscore_index = string.find( targetNode_fullName, "_", 3 )
local targetNode_materialName = string.sub( targetNode_fullName, underscore_index+1 )
minetest.log( "warning", targetNode_fullName )
minetest.log( "warning", targetNode_materialName )
local targetNode_bitmapName = string.sub( targetNode_fullName, 0, underscore_index-1 )
local colon_index = string.find( targetNode_fullName, ":" )
targetNode_bitmapName = string.sub( targetNode_bitmapName, colon_index+1 )
if( targetNode_bitmapName == "smallblock" ) then targetNode_bitmapName = "1" end
minetest.chat_send_all( targetNode_bitmapName )
-- find bitmap from name and facedir
local bitmap_list = {} -- list of bitmaps that are a rotation of targetNode bitmapName
local index = 1
for key_bitmap_as_int, val_bitmap_name in pairs(smallblocks.int_name_map) do
if( val_bitmap_name == targetNode_bitmapName ) then
bitmap_list[index] = key_bitmap_as_int -- grabing keys that map to the target node name
index = index + 1
minetest.chat_send_all( key_bitmap_as_int )
end
end
local targetNode_bitmap
-- find the bitmap that corresponds to the targetNode facedir
for i=1, index-1 do
if( smallblocks.int_facedir_map[bitmap_list[i]] == minetest.get_node( target_node_position ).param2 ) then
targetNode_bitmap = bitmap_list[i]
minetest.chat_send_all( "TN bitmap: " .. targetNode_bitmap )
end
end
if( targetNode_bitmap ~= nil ) then --this should always happen
--targetNode_bitmap = util.integer_to_bitmap( targetNode_bitmap )
local tnb = util.integer_to_bitmap( targetNode_bitmap )
minetest.log( "none", "oring "..targetNode_bitmap .. " with ".. util.bitmap_to_integer( bitmap ) )
local finalBitmap = util.xor_bitmap( tnb, bitmap )
if( util.bitmap_to_integer( finalBitmap ) == 0 ) then
minetest.remove_node( target_node_position )
return
end
minetest.log("none", "result " .. util.bitmap_to_integer( finalBitmap ) )
local finalNode_name = smallblocks.int_name_map[util.bitmap_to_integer( finalBitmap )]
if( finalNode_name == "1" ) then
finalNode_name = "smallblock"
end
local finalNode_facedir = smallblocks.int_facedir_map[util.bitmap_to_integer( finalBitmap )]
local finalNode = { name="k_smallblocks:"..finalNode_name.."_"..targetNode_materialName, param2=finalNode_facedir }
minetest.swap_node( target_node_position, finalNode )
minetest.chat_send_all( "materials match" )
end
end
smallblock.on_use = function( itemstack, user, pointed_thing )
-- find world coordinates for smallblock placement
local camera_position = user:get_pos()
camera_position.y = camera_position.y + user:get_properties().eye_height
local raycaster = Raycast(
camera_position,
vector.add( camera_position, vector.multiply(user:get_look_dir(), 5) ),
false, false
)
local intersected_thing = raycaster:next()
if( intersected_thing == nil ) then return end
--pushes the intersection away from the face to resolve ambiguity and get a rogh position in the world
local small_normal = vector.divide( intersected_thing.intersection_normal, 4 )
local world_rough_position = vector.add( small_normal, intersected_thing.intersection_point )
local target_node_position = {
x = math.floor(world_rough_position.x+0.5),
y = math.floor(world_rough_position.y+0.5),
z = math.floor(world_rough_position.z+0.5),
}
local point_within_node = util.worldPoint_to_nodePoint( world_rough_position )
minetest.chat_send_all( "x: "..point_within_node.x )
minetest.chat_send_all( "y: "..point_within_node.y )
minetest.chat_send_all( "z: "..point_within_node.z )
local bitmap = util.nodePoint_to_bitmap( point_within_node )
minetest.log("warning", "placing at" .. util.bitmap_to_integer( bitmap ) )
if( minetest.get_node( target_node_position ).name == "air" ) then
minetest.log( smallblocks.int_name_map[util.bitmap_to_integer(bitmap)] )
local bitmap_string = ""
for i = 0, 7, 1 do
bitmap_string = bitmap_string .. bitmap[i]
end
minetest.log( bitmap_string )
minetest.set_node( target_node_position, {
name="k_smallblocks:smallblock_stonebrick",
param2=smallblocks.int_facedir_map[util.bitmap_to_integer(bitmap)]
} )
else
local underscore_index
local wieldedNode_fullName = itemstack:get_name()
underscore_index = string.find( wieldedNode_fullName, "_", 3 )
local wieldedNode_materialName = string.sub( wieldedNode_fullName, underscore_index+1 )
local targetNode_fullName = minetest.get_node( target_node_position ).name
underscore_index = string.find( targetNode_fullName, "_", 3 )
local targetNode_materialName = string.sub( targetNode_fullName, underscore_index+1 )
if( targetNode_materialName == wieldedNode_materialName ) then
local targetNode_bitmapName = string.sub( targetNode_fullName, 0, underscore_index-1 )
local colon_index = string.find( targetNode_fullName, ":" )
targetNode_bitmapName = string.sub( targetNode_bitmapName, colon_index+1 )
if( targetNode_bitmapName == "smallblock" ) then targetNode_bitmapName = "1" end
minetest.chat_send_all( targetNode_bitmapName )
-- find bitmap from name and facedir
local bitmap_list = {} -- list of bitmaps that are a rotation of targetNode bitmapName
local index = 1
for key_bitmap_as_int, val_bitmap_name in pairs(smallblocks.int_name_map) do
if( val_bitmap_name == targetNode_bitmapName ) then
bitmap_list[index] = key_bitmap_as_int -- grabing keys that map to the target node name
index = index + 1
minetest.chat_send_all( key_bitmap_as_int )
end
end
local targetNode_bitmap
-- find the bitmap that corresponds to the targetNode facedir
for i=1, index-1 do
if( smallblocks.int_facedir_map[bitmap_list[i]] == minetest.get_node( target_node_position ).param2 ) then
targetNode_bitmap = bitmap_list[i]
minetest.chat_send_all( "TN bitmap: " .. targetNode_bitmap )
end
end
if( targetNode_bitmap ~= nil ) then --this should always happen
--targetNode_bitmap = util.integer_to_bitmap( targetNode_bitmap )
local tnb = util.integer_to_bitmap( targetNode_bitmap )
minetest.log( "none", "oring "..targetNode_bitmap .. " with ".. util.bitmap_to_integer( bitmap ) )
local finalBitmap = util.or_bitmap( tnb, bitmap )
minetest.log("none", "result " .. util.bitmap_to_integer( finalBitmap ) )
local finalNode_name = smallblocks.int_name_map[util.bitmap_to_integer( finalBitmap )]
local finalNode_facedir = smallblocks.int_facedir_map[util.bitmap_to_integer( finalBitmap )]
local finalNode = { name="k_smallblocks:"..finalNode_name.."_"..targetNode_materialName, param2=finalNode_facedir }
minetest.swap_node( target_node_position, finalNode )
minetest.chat_send_all( "materials match" )
end
end
end
end
dofile(modpath .. "/util.lua")
test_bitmap = util.integer_to_bitmap( 182 )
test_int = util.bitmap_to_integer( test_bitmap )
minetest.log("warning", test_int )
for i = 0, 7, 1 do
minetest.log("warning", test_bitmap[i] )
end
dofile(modpath .. "/generate_shapes.lua")
dofile(modpath .. "/generate_nodeboxes.lua")
dofile(modpath .. "/generate_map.lua") -- maps int to shape, facedir
smallblocks.name_int_map = util.table_invert( smallblocks.int_name_map )
for k,v in pairs(origin_bitmaps) do
minetest.log("error", " ".. v)
end
for k, v in pairs(smallblocks.origin_bitmaps) do
local shape_int = v
local shape_nb = smallblocks.nodeboxes[k]
if( v == 1 ) then
minetest.register_node("k_smallblocks:smallblock_stonebrick",
{
description = "Stone Brick Smallblock",
paramtype = "light",
paramtype2 = "facedir",
drawtype = "nodebox",
tiles = { {name="default_stone_brick.png", align_style="world"} },
groups = { cracky=2 },
sunlight_propagates = true,
node_box = {
type = "fixed",
fixed = shape_nb
},
on_use = smallblock.on_use,
on_rightclick = common.on_right_click,
on_place = function( itemstack, placer, pointed_thing )
if( string.sub(minetest.get_node( pointed_thing.under ).name, 1, 13 ) == "k_smallblocks" ) then
minetest.chat_send_all( "on_place" .. pointed_thing.under.x .. " "..
pointed_thing.under.y.." ".. pointed_thing.under.z )
minetest.item_place( itemstack, placer, pointed_thing, 0)
-- doesnt actually place any item, but calls target node on_rightclick if its defined
end
end
}
)
else
minetest.register_node("k_smallblocks:"..shape_int.."_".."stonebrick",
{
description = "Stone Brick",
paramtype = "light",
paramtype2 = "facedir",
drawtype = "nodebox",
tiles = { {name="default_stone_brick.png", align_style="world"} },
is_ground_content = false,
groups = { cracky=2},
sunlight_propagates = true,
node_box = {
type = "fixed",
fixed = shape_nb
},
on_place = function() end,
on_rightclick = common.on_right_click
}
)
end
end

277
util.lua Normal file
View File

@ -0,0 +1,277 @@
--[[
k-smallblocks is a Minetest mod that adds smaller blocks to minetest aswell as its own node placement prediction/system
Copyright (C) 2019 Kurtzmusch
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
--]]
-- bitmap is an array of 8 integers that can be either 0 or 1
-- it is used to indicate wich part of the node is filled with a smallblock
util = {}
util.table_invert = function(t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
util.table_shallow_copy = function(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
-- does the equivalent of a bitwise OR with 2 bitmaps
util.or_bitmap = function( array1, array2 )
local result = { [7] = nil }
local index = 0
while (index < 8 ) do
if( (array1[index] == 1) or (array2[index] == 1) ) then
result[index] = 1
else
result[index] = 0
end
index = index + 1
end
return result
end
util.xor_bitmap = function( bitmap1, bitmap2 )
local result = {}
local index = 0
while(index<8) do
if(
( (bitmap1[index] == 1) or (bitmap2[index] == 1 ) )
and
( (bitmap1[index] == 0) or (bitmap2[index] == 0 ) )
) then
result[index] = 1
else
result[index] = 0
end
index = index + 1
end
return result
end
-- convert world coordinates into node coordinates, ranging from 0 to 1
util.worldPoint_to_nodePoint = function( point )
point.x = point.x + 65536.5
point.y = point.y + 65536.5
point.z = point.z + 65536.5
return { x=point.x%1, y=point.y%1, z=point.z%1 }
end
-- converts a point in a node into a bitmap
-- input coords must be between 0 and 1
-- the bitmap is empty, except for the smallblock in the given coordinate
util.nodePoint_to_bitmap = function( point )
local size = 2 -- lenght of a node in smallblocks
-- floored components are indexes of a 3d array
local floored_point = {}
floored_point.x = math.floor( point.x+0.5 )
floored_point.y = math.floor( point.y+0.5 )
floored_point.z = math.floor( point.z+0.5 )
bitmap = {}
local index = 0
while (index < 8) do
bitmap[index] = 0
index = index + 1
end
-- this is like converting a 3d array to a 1d array
index =
(floored_point.x) +
(floored_point.y*size) +
(floored_point.z*(size*size))
bitmap[index] = 1
return bitmap
end
util.integer_to_bitmap = function( integer )
local bitmap = { [0] = nil, [7] = nil }
local value = 128
for i = 7, 0, -1 do
if(integer >= value) then
bitmap[i] = 1
integer = integer - value
else
bitmap[i] = 0
end
value = value/2
end
return bitmap
end
util.bitmap_to_integer = function( bitmap )
local integer = 0
local value = 1
for index = 0, 7, 1 do
integer = integer + (bitmap[index] * value)
value = value * 2
end
return integer
end
--TODO delete this if not used
util.mirror_bitmap_across_y = function( bitmap )
local new_array = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
local my = max_index
for iy = 0, max_index, 1 do
for ix = 0, max_index, 1 do
for iz = 0, max_index, 1 do
new_array[ix][my][iz] = bitmap[ ix + (iy*2) + (iz*4) ]
end
end
my = my - 1
end
return util.array3d_to_bitmap( new_array )
end
--bitmap rotation: rotation of the 3d array that represents the bitmap
--rotates the array clockwise by 90 degress around the given axis
util.rotate_bitmap_around_plus_y = function( bitmap )
local new_bitmap = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
new_bitmap[iz][iy][max_index-ix] = bitmap[ ix + (iy*2) + (iz*4) ]
end
end
end
return util.array3d_to_bitmap( new_bitmap )
end
util.rotate_bitmap_around_minus_y = function( bitmap )
local new_bitmap = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
new_bitmap[max_index-iz][iy][ix] = bitmap[ ix + (iy*2) + (iz*4) ]
end
end
end
return util.array3d_to_bitmap( new_bitmap )
end
util.rotate_bitmap_around_plus_z = function( bitmap )
local new_bitmap = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
new_bitmap[max_index-iy][ix][iz] = bitmap[ ix + (iy*2) + (iz*4) ]
end
end
end
return util.array3d_to_bitmap( new_bitmap )
end
util.rotate_bitmap_around_minus_z = function( bitmap )
local new_bitmap = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
new_bitmap[iy][max_index-ix][iz] = bitmap[ ix + (iy*2) + (iz*4) ]
end
end
end
return util.array3d_to_bitmap( new_bitmap )
end
util.rotate_bitmap_around_plus_x = function( bitmap )
local new_bitmap = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
new_bitmap[ix][max_index-iz][iy] = bitmap[ ix + (iy*2) + (iz*4) ]
end
end
end
return util.array3d_to_bitmap( new_bitmap )
end
util.rotate_bitmap_around_minus_x = function( bitmap )
local new_bitmap = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
new_bitmap[ix][iz][max_index-iy] = bitmap[ ix + (iy*2) + (iz*4) ]
end
end
end
return util.array3d_to_bitmap( new_bitmap )
end
-- bitmap orientation assumes the current bitmap orientation is plus y
util.orient_bitmap_towards_plus_z = function( bitmap )
return util.rotate_bitmap_around_plus_x( bitmap )
end
util.orient_bitmap_towards_minus_z = function( bitmap )
return util.rotate_bitmap_around_minus_x( bitmap )
end
util.orient_bitmap_towards_plus_x = function( bitmap )
return util.rotate_bitmap_around_minus_z( bitmap )
end
util.orient_bitmap_towards_minus_x = function( bitmap )
return util.rotate_bitmap_around_plus_z( bitmap )
end
util.orient_bitmap_towards_minus_y = function( bitmap )
return util.rotate_bitmap_around_plus_z( util.rotate_bitmap_around_plus_z( bitmap ) )
end
util.bitmap_to_array3d = function( bitmap )
local array = { [0] = { [0]={}, [1]={}}, [1]={[0]={}, [1]={}} }
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
array[ix][iy][iz] = bitmap[ ix + (iy*2) + (iz*4)]
end
end
end
return array
end
util.array3d_to_bitmap = function( array )
local bitmap = {}
local max_index = 1
for ix = 0, max_index, 1 do
for iy = 0, max_index, 1 do
for iz = 0, max_index, 1 do
bitmap[ ix + (iy*2) + (iz*4) ] = array[ix][iy][iz]
end
end
end
return bitmap
end