added all node placement prediction

This commit is contained in:
Kurtzmusch 2019-04-28 21:01:26 -03:00
parent 2ba62e3f30
commit 7b89b978e6
2 changed files with 623 additions and 6 deletions

View File

@ -197,6 +197,315 @@ smallblock.on_use = function( itemstack, user, pointed_thing )
end
end
halfslab.on_use = function( itemstack, user, pointed_thing )
-- find world coordinates
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, 8 )
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 bitmap4x4 = util.nodePoint_to_bitmap4x4( point_within_node )
local face_type = util.bitmap4x4_to_facetype( bitmap4x4 )
minetest.chat_send_all( face_type )
local bitmap = nil
if( face_type == "edge" ) then
util.bitmap4x4_expand_edge( bitmap4x4 )
bitmap = util.bitmap4x4_to_bitmap2x2( bitmap4x4 )
end
if( face_type == "corner" ) then
util.bitmap4x4_expand_normal( bitmap4x4, small_normal )
bitmap = util.bitmap4x4_to_bitmap2x2( bitmap4x4 )
end
if( bitmap ~= nil ) then bitmap = util.array3d_to_bitmap( bitmap )
minetest.log("warning", "placing at" .. util.bitmap_to_integer( bitmap ) )
if( minetest.get_node( target_node_position ).name == "air" ) then --TODO change this so its air or material
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:3_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 )
end
end
end
end
end
slab.on_use = function( itemstack, user, pointed_thing )
-- find world coordinates
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, 8 )
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 bitmap4x4 = util.nodePoint_to_bitmap4x4( point_within_node )
local face_type = util.bitmap4x4_to_facetype( bitmap4x4 )
minetest.chat_send_all( face_type )
local bitmap = nil
if( face_type == "edge" ) then
util.bitmap4x4_expand_edge( bitmap4x4 )
util.bitmap4x4_expand_normal( bitmap4x4, small_normal )
bitmap = util.bitmap4x4_to_bitmap2x2( bitmap4x4 )
end
if( face_type == "face" ) then
util.bitmap4x4_expand_face( bitmap4x4 )
bitmap = util.bitmap4x4_to_bitmap2x2( bitmap4x4 )
end
if( bitmap ~= nil ) then bitmap = util.array3d_to_bitmap( bitmap )
minetest.log("warning", "placing at" .. util.bitmap_to_integer( bitmap ) )
if( minetest.get_node( target_node_position ).name == "air" ) then --TODO change this so its air or material
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:15_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 )
end
end
end
end
end
stair.on_use = function( itemstack, user, pointed_thing )
-- find world coordinates
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, 8 )
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 bitmap4x4 = util.nodePoint_to_bitmap4x4( point_within_node )
local face_type = util.bitmap4x4_to_facetype( bitmap4x4 )
minetest.chat_send_all( face_type )
local bitmap = nil
if( face_type == "edge" ) then
util.bitmap4x4_expand_edge( bitmap4x4 )
util.bitmap4x4_expand_faces( bitmap4x4 )
bitmap = util.bitmap4x4_to_bitmap2x2( bitmap4x4 )
end
if( face_type == "corner" ) then
util.bitmap4x4_expand_normal( bitmap4x4, small_normal )
util.bitmap4x4_expand_faces( bitmap4x4 )
bitmap = util.bitmap4x4_to_bitmap2x2( bitmap4x4 )
end
if( bitmap ~= nil ) then bitmap = util.array3d_to_bitmap( bitmap )
minetest.log("warning", "placing at" .. util.bitmap_to_integer( bitmap ) )
if( minetest.get_node( target_node_position ).name == "air" ) then --TODO change this so its air or material
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:63_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 )
end
end
end
end
end
bitmap_name_definition_map = { -- maps a bitmap name to a definiton
[1] = {
name = "smallblock",
@ -208,21 +517,21 @@ bitmap_name_definition_map = { -- maps a bitmap name to a definiton
[3] = {
name = "halfslab",
description = "half-slab",
on_use = function() end,
on_use = halfslab.on_use,
on_rightclick = common.on_right_click
},
[15] = {
name = "slab",
description = "slab",
on_use = function() end,
on_use = slab.on_use,
on_rightclick = common.on_right_click
},
[63] = {
name = "stair",
description = "stair",
on_use = function() end,
on_use = stair.on_use,
on_rightclick = common.on_right_click
}

314
util.lua
View File

@ -94,9 +94,9 @@ util.nodePoint_to_bitmap = function( point )
-- 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 )
floored_point.x = math.floor( point.x*2 )
floored_point.y = math.floor( point.y*2 )
floored_point.z = math.floor( point.z*2 )
bitmap = {}
local index = 0
@ -115,6 +115,314 @@ util.nodePoint_to_bitmap = function( point )
return bitmap
end
util.nodePoint_to_bitmap4x4 = function( point )
local result = {}
for x=0, 3, 1 do
result[x] = {}
for y=0, 3, 1 do
result[x][y] = {}
for z=0, 3, 1 do
result[x][y][z] = 0
end
end
end
local size = 4
minetest.log( "warning", point.x.."|"..point.y.."|"..point.z )
local floored_point = {}
floored_point.x = math.floor( point.x*size )
floored_point.y = math.floor( point.y*size )
floored_point.z = math.floor( point.z*size )
minetest.log( "warning", point.x.."|"..point.y.."|"..point.z )
minetest.log( "error", floored_point.x.."|"..floored_point.y.."|"..floored_point.z )
result[floored_point.x][floored_point.y][floored_point.z] = 1
return result
end
-- this assumes only one of the values is 1
util.bitmap4x4_to_facetype = function( bitmap4x4 )
--find indexes
local ix
local iy
local iz
for x=0, 3, 1 do
for y=0, 3, 1 do
for z=0, 3, 1 do
if( bitmap4x4[x][y][z] == 1 ) then
ix = x
iy = y
iz = z
end
end
end
end
-- count indexes at edge of array
local edge_indexes = 0
if( ( ix == 0) or (ix == 3) ) then edge_indexes = edge_indexes + 1 end
if( ( iy == 0) or (iy == 3) ) then edge_indexes = edge_indexes + 1 end
if( ( iz == 0) or (iz == 3) ) then edge_indexes = edge_indexes + 1 end
minetest.chat_send_all( ix.." "..iy.." "..iz )
if( edge_indexes == 3 ) then return "corner" end
if( edge_indexes == 2 ) then return "edge" end
if( edge_indexes == 1 ) then return "face" end
return "unsuported"
end
util.bitmap4x4_to_bitmap2x2 = function( bitmap4x4 )
local result = {}
local point_list = {}
local i = 0
for ix=0, 3, 1 do
for iy=0, 3, 1 do
for iz=0, 3, 1 do
if( bitmap4x4[ix][iy][iz] == 1 ) then
point_list[i] = { x=ix/4.0,y=iy/4.0,z=iz/4.0 }
i = i + 1
end
end
end
end
local new_point_list = {}
for k, v in pairs(point_list) do
minetest.log( "warning", v.x..":"..math.floor(v.x*2).." ".. v.y..":"..math.floor(v.y*2).." ".. v.z..":"..math.floor(v.z*2) )
new_point_list[k] = { x=math.floor(v.x*2), y=math.floor(v.y*2), z=math.floor(v.z*2) }
end
for ix=0, 1, 1 do
result[ix] = {}
for iy=0, 1, 1 do
result[ix][iy] = {}
for iz=0, 1, 1 do
result[ix][iy][iz] = 0
end
end
end
for k, v in pairs(new_point_list) do
minetest.log( "none", v.x..v.y..v.z )
result[v.x][v.y][v.z] = 1
end
return result
end
-- assumes you give it a bitmap of type edge-expanded
util.bitmap4x4_expand_faces = function( bitmap4x4 )
-- remove corners
for x=0, 3, 1 do
for y=0, 3, 1 do
for z=0, 3, 1 do
if( bitmap4x4[x][y][z] == 1 ) then
if( -- if its a corner
( (x==0) or (x==3) )
and ( (y==0) or (y==3) )
and ( (z==0) or (z==3) )
) then
bitmap4x4[x][y][z] = 0
end
end
end
end
end
--find faces
local ix
local iy
local iz
local dimension = {}
for x=0, 3, 1 do
for y=0, 3, 1 do
for z=0, 3, 1 do
if( bitmap4x4[x][y][z] == 1 ) then
ix = x
iy = y
iz = z
end
end
end
end
local index = 0
if( (ix == 0) or (ix == 3) ) then
dimension[index] = 'x'
index = index + 1
end
if( (iy == 0) or (iy == 3) ) then
dimension[index] = 'y'
index = index + 1
end
if( (iz == 0) or (iz == 3) ) then
dimension[index] = 'z'
index = index + 1
end
for k, v in pairs(dimension) do
minetest.chat_send_all( v )
if( v == 'x' ) then
for z=0, 3, 1 do
for y=0, 3, 1 do
bitmap4x4[ix][y][z] = 1
end
end
end
if( v == 'y' ) then
for x=0, 3, 1 do
for z=0, 3, 1 do
bitmap4x4[x][iy][z] = 1
end
end
end
if( v == 'z' ) then
for x=0, 3, 1 do
for y=0, 3, 1 do
bitmap4x4[x][y][iz] = 1
end
end
end
end
end
-- assumes you give it a bitmap of type face
util.bitmap4x4_expand_face = function( bitmap4x4 )
--find face
local ix
local iy
local iz
local dimension
for x=0, 3, 1 do
for y=0, 3, 1 do
for z=0, 3, 1 do
if( bitmap4x4[x][y][z] == 1 ) then
ix = x
iy = y
iz = z
end
end
end
end
if( (ix == 0) or (ix == 3) ) then dimension = 'x' end
if( (iy == 0) or (iy == 3) ) then dimension = 'y' end
if( (iz == 0) or (iz == 3) ) then dimension = 'z' end
if( dimension == 'x' ) then
for z=0, 3, 1 do
for y=0, 3, 1 do
bitmap4x4[ix][y][z] = 1
end
end
end
if( dimension == 'y' ) then
for x=0, 3, 1 do
for z=0, 3, 1 do
bitmap4x4[x][iy][z] = 1
end
end
end
if( dimension == 'z' ) then
for x=0, 3, 1 do
for y=0, 3, 1 do
bitmap4x4[x][y][iz] = 1
end
end
end
end
util.bitmap4x4_expand_normal = function( bitmap4x4, normal )
--find dimension of normal that isn't 0
local dimension = ""
if( normal.x ~= 0 ) then dimension = 'x' end
if( normal.y ~= 0 ) then dimension = 'y' end
if( normal.z ~= 0 ) then dimension = 'z' end
-- find positions that are 1 and set all positions across the normal to 1
for x=0, 3, 1 do
for y=0, 3, 1 do
for z=0, 3, 1 do
if( bitmap4x4[x][y][z] == 1 ) then
if( dimension == 'x' ) then
for i=0, 3, 1 do
bitmap4x4[i][y][z] = 1
end
end
if( dimension == 'y' ) then
for i=0, 3, 1 do
bitmap4x4[x][i][z] = 1
end
end
if( dimension == 'z' ) then
for i=0, 3, 1 do
bitmap4x4[x][y][i] = 1
end
end
end
end
end
end
end
util.bitmap4x4_expand_edge = function( bitmap4x4 )
local print_string = ""
for i=0, 3, 1 do
for ii=0, 3, 1 do
for iii=0, 3, 1 do
print_string = print_string..bitmap4x4[i][ii][iii]
end
end
end
minetest.log( "warning", print_string )
--find index dimension not on the edge
local ix
local iy
local iz
local dimension
for x=0, 3, 1 do
for y=0, 3, 1 do
for z=0, 3, 1 do
if( bitmap4x4[x][y][z] == 1 ) then
ix = x
iy = y
iz = z
end
end
end
end
if( (ix ~= 0) and (ix ~= 3) ) then dimension = 'x' end
if( (iy ~= 0) and (iy ~= 3) ) then dimension = 'y' end
if( (iz ~= 0) and (iz ~= 3) ) then dimension = 'z' end
if( dimension == 'x' ) then
for i=0, 3, 1 do
bitmap4x4[i][iy][iz] = 1
end
end
if( dimension == 'y' ) then
for i=0, 3, 1 do
bitmap4x4[ix][i][iz] = 1
end
end
if( dimension == 'z' ) then
for i=0, 3, 1 do
bitmap4x4[ix][iy][i] = 1
end
end
print_string = ""
for i=0, 3, 1 do
for ii=0, 3, 1 do
for iii=0, 3, 1 do
print_string = print_string..bitmap4x4[i][ii][iii]
end
end
end
minetest.log( "warning", print_string )
end
util.integer_to_bitmap = function( integer )
local bitmap = { [0] = nil, [7] = nil }