diff --git a/blocks/component.lua b/blocks/component.lua index 14a523c..26ba398 100644 --- a/blocks/component.lua +++ b/blocks/component.lua @@ -1,51 +1,3 @@ - ---[[ -Component 2x3: - - z+ - /|\ -\ 22 | 11 / - \ 22 | 11 / - \ 22 | 11 / -333 \ | / 000 -333 \|/ 000 -−−−−−−−+−−−−−−−> x+ -444 /|\ 777 -444 / | \ 777 - / 55 | 66 \ - / 55 | 66 \ -/ 55 | 66 \ ---]] -local function vecTo8dir(vec) - local x = vec[1] - local z = vec[3] - return - x>=0 and z>=0 and z<=x and 0 or - x>=0 and z>=0 and z>x and 1 or - x<0 and z>=0 and -z<=x and 2 or - x<0 and z>=0 and -z>x and 3 or - x<0 and z<0 and z>=x and 4 or - x<0 and z<0 and z=0 and z<0 and -z>=x and 6 or - x>=0 and z<0 and -z x+ -- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}} -components = { - "and" = {2,3,{{"in_1","in_2"},{"out"},{},{}}}, - "switch" = {2,1,{{"in"},{"out"},{"activate"},{}}}, - "demux" = {2,8,{{"in_1","in_2","in_3"},{"out_1","out_2","out_3","out_4","out_5","out_6","out_7","out_8"},{},{}}}, -} +components = {} +components["and"] = {2,3,{{"in_2",nil,"in_1"},{nil,"out",nil},nil,nil}} +components["switch"] = {2,1,{{"in"},{"out"},{"activate",nil},nil}} +components["demux"] = {2,8,{{nil,nil,nil,nil,nil,"in_3","in_2","in_1"},{"out_8","out_7","out_6","out_5","out_4","out_3","out_2","out_1"},nil,nil}} + -- componentsblocks["block"] = {name,x,y} componentsblocks = {} +for name,cmp in pairs(components) +do + local w = cmp[1] + local h = cmp[2] + local cables = cmp[3] + for x=1,w do + for y=1,h do -minetest.register_node("logikraft:component_head", { - drawtype = "block", - tiles = {"component.png"}, - groups = {circuitry = 1,dig_immediate = 3}, - on_place = function(itemstack, placer, pointed_thing) - local w = 3 - local h = 5 + local nodename = "logikraft:component_"..name.."_"..tostring(x).."_"..tostring(y) + minetest.register_node(nodename, { + drawtype = "block", + tiles = { + "component_"..name..".png", + "component.png", + ((x==w) and (cables[2] and cables[2][y] and "component_port.png" or "component.png") or "component.png"), -- x+ + ((x==1) and (cables[1] and cables[1][y] and "component_port.png" or "component.png") or "component.png"), -- x- + ((y==h) and (cables[3] and cables[3][x] and "component_port.png" or "component.png") or "component.png"), -- z+ + ((y==1) and (cables[4] and cables[4][x] and "component_port.png" or "component.png") or "component.png"), -- z- + }, + groups = {circuitry = 1,dig_immediate = 3}, + paramtype2 = "4dir", + inventory_image = (x == 1 and y == 1 and "component_"..name..".png") or nil, + drop = "logikraft:component_"..name.."_1_1", + on_place = (x == 1 and y == 1) and function(itemstack, placer, pointed_thing) + + local under = pointed_thing.under + local node = minetest.get_node(under) + local udef = minetest.registered_nodes[node.name] + + -- Rightclick is prioritary + if udef and udef.on_rightclick and + not (placer and placer:is_player() and + placer:get_player_control().sneak) then + return udef.on_rightclick(under, node, placer, itemstack, + pointed_thing) or itemstack + end + + -- Pos will be the head location + local pos + if udef and udef.buildable_to then + pos = under + else + pos = pointed_thing.above + end + + local dir8 = logikraft.vecTo8dir(placer:get_look_dir()) - local under = pointed_thing.under - local node = minetest.get_node(under) - local udef = minetest.registered_nodes[node.name] + for i = 1,w do + for j = 1,h do + local loc = logikraft.addToPos8dir(dir8,pos,i-1,j-1) + local node_def = minetest.registered_nodes[minetest.get_node(loc).name] + if not node_def or not node_def.buildable_to then + -- Then we cannot build here + return itemstack + end + end + end - -- Rightclick is prioritary - if udef and udef.on_rightclick and - not (placer and placer:is_player() and - placer:get_player_control().sneak) then - return udef.on_rightclick(under, node, placer, itemstack, - pointed_thing) or itemstack - end + local dir4 = logikraft.dir8to4(dir8) + local invw = dir8 % 2 == 0 - -- Pos will be the head location - local pos - if udef and udef.buildable_to then - pos = under - else - pos = pointed_thing.above - end + -- If we reached this point, we can build the component + for i = 1,w do + for j = 1,h do + local loc = logikraft.addToPos8dir(dir8,pos,invw and (w-i) or (i-1),j-1) + minetest.set_node(loc, {name = "logikraft:component_"..name.."_"..tostring(i).."_"..tostring(j), param2 = dir4}) + end + end - - local player_name = placer and placer:get_player_name() or "" - - local dir = placer and placer:get_look_dir() and - vecTo8dir(placer:get_look_dir()) or 0 - - for i = 0,w*h-1 do - local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w) - local node_def = minetest.registered_nodes[minetest.get_node(loc).name] - if not node_def or not node_def.buildable_to then - -- Then we cannot build here + if not minetest.is_creative_enabled(placer) then + itemstack:take_item() + end return itemstack + end or nil, + on_destruct = function(pos) + + local node = minetest.get_node(pos) + dir4 = node.param2 + + for i = 1,w do + for j = 1,h do + local loc = logikraft.addToPos4dir(dir4,pos,i-x,j-y) + remove_no_destruct(loc) + end + end end - end - -- If we reached this point, we can build the component - minetest.set_node(pos, {name = "logikraft:component_head", param2 = dir}) - for i = 1,w*h-1 do - local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w) - minetest.set_node(loc, {name = "logikraft:component_tail", param2 = dir}) - end - - if not minetest.is_creative_enabled(player_name) then - itemstack:take_item() - end - return itemstack - end, - on_destruct = function(pos) - local w = 3 - local h = 5 - - local node = minetest.get_node(pos) - - dir = node.param2 - - remove_no_destruct(pos) -- Should be logikraft:component_head - for i = 1,w*h-1 do - local loc = addToPos8dir(dir,pos,i%w,(i-i%w)/w) - local onode = minetest.get_node(loc) - if onode.name == "logikraft:component_tail" and onode.param2 == dir - then - remove_no_destruct(loc) - end - end - + }) + componentsblocks["nodename"] = {name,x,y} + end end -}) -minetest.register_node("logikraft:component_tail", { - drawtype = "block", - paramtype2 = "4dir", - groups = {circuitry = 1}, - -- Which direction is the one of the head block - tiles = {"component.png^[colorizehsl:120"} -}) \ No newline at end of file + minetest.register_alias("logikraft:component_"..name, "logikraft:component_"..name .. "_1_1") +end \ No newline at end of file diff --git a/textures/component_and.png b/textures/component_and.png new file mode 100644 index 0000000..1532e76 Binary files /dev/null and b/textures/component_and.png differ diff --git a/textures/component_demux.png b/textures/component_demux.png new file mode 100644 index 0000000..8f87f57 Binary files /dev/null and b/textures/component_demux.png differ diff --git a/textures/component_port.png b/textures/component_port.png new file mode 100644 index 0000000..478d35b Binary files /dev/null and b/textures/component_port.png differ diff --git a/textures/component_switch.png b/textures/component_switch.png new file mode 100644 index 0000000..81e986e Binary files /dev/null and b/textures/component_switch.png differ diff --git a/utils.lua b/utils.lua index 4e6d74d..6653261 100644 --- a/utils.lua +++ b/utils.lua @@ -79,4 +79,43 @@ function logikraft.lookingAtNbIndex(user,under) end end return 0 -end \ No newline at end of file +end + +function logikraft.vecTo8dir(vec) + local x = vec[1] + local z = vec[3] + return + x<0 and z>=0 and -z<=x and 0 or + x>=0 and z>=0 and z>x and 1 or + x>=0 and z>=0 and z<=x and 2 or + x>=0 and z<0 and -z=0 and z<0 and -z>=x and 4 or + x<0 and z<0 and z=x and 6 or + x<0 and z>=0 and -z>x and 7 or + 0 -- Should not happen +end + +function logikraft.dir8to4(dir) + return math.floor(dir / 2) +end + +function logikraft.addToPos8dir(dir,pos,w,h) + local x = pos[1] + local y = pos[2] + local z = pos[3] + return + dir == 0 and {x = x-w, y = y, z = z+h} or + dir == 1 and {x = x+w, y = y, z = z+h} or + dir == 2 and {x = x+h, y = y, z = z+w} or + dir == 3 and {x = x+h, y = y, z = z-w} or + dir == 4 and {x = x+w, y = y, z = z-h} or + dir == 5 and {x = x-w, y = y, z = z-h} or + dir == 6 and {x = x-h, y = y, z = z-w} or + dir == 7 and {x = x-h, y = y, z = z+w} or + pos -- Should not happen +end + +function logikraft.addToPos4dir(dir,pos,w,h) + return logikraft.addToPos8dir(dir*2+1,pos,w,h) +end