add new feature to black too: automatic bending for multiple wires

This commit is contained in:
Deet Mit 2022-01-12 22:39:31 +01:00
parent 194f444d2c
commit ff73354c1f
9 changed files with 384 additions and 46 deletions

@ -126,7 +126,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local info = {} local info = {}
local file = data.file local file = data.file
info = read_table_from_file(file) info = read_table_from_file(file)
info.title = fields.title info.title = fields.title
info.text = fields.text info.text = fields.text

@ -9,6 +9,14 @@ function generate_file_name(user)
return file return file
end end
function generate_file_name_library(user)
local days = minetest.get_day_count()
local sec = minetest.get_gametime()
local rand = math.abs(mesecons_autotools.rand:next())
local file = "library-"..user.."-"..sec.."-" .. rand
return file
end
local path = minetest.get_worldpath() .. "/circuits/" local path = minetest.get_worldpath() .. "/circuits/"
function save_table_to_file(filename,tab) function save_table_to_file(filename,tab)

@ -3,7 +3,12 @@
function render_circuit(circ,start_x,start_y) function render_circuit(circ,start_x,start_y)
circ.title = circ.title or "" circ.title = circ.title or ""
local spec = "button["..start_x..","..start_y..";10,1;".. "title_" .. circ.id .. ";"..circ.title.."]" -- local spec = "button["..start_x..","..start_y..";10,1;".. "title_" .. circ.id .. ";"..circ.title.."]"
local spec = "image_button["..start_x+1 ..","..start_y..";1,1;circuit_full.png;;;;]"
spec = spec .. "label[" .. start_x+2 .. "," .. start_y+0.5 .. ";" .. circ.title .. "]"
spec = spec .. "image_button[".. start_x ..","..start_y..";1,1;drop_btn.png;;;;]"
return spec,1 return spec,1
end end
@ -111,24 +116,22 @@ end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "mesecons_autotools:library_view" then return end if formname ~= "mesecons_autotools:library_view" then return end
--print ("RECEIVE:=player="..dump(player) .. ", formname="..formname .. ".fiels="..dump(fields))
local user = player:get_player_name() local user = player:get_player_name()
local stack = player:get_wielded_item() -- local stack = player:get_wielded_item()
local data = stack:get_meta():to_table().fields -- local data = stack:get_meta():to_table().fields
local lib = data.lib -- local lib = data.lib
print("lib="..dump(lib)) -- print("lib="..dump(lib))
if fields.quit then return end -- print("fields="..dump(fields))
--if lib == nil then return end
-- print("DB2: " .. dump(data)) -- if fields.quit then return end
-- print("FIELDS:"..dump(fields))
lib = lib or {} --[[
-- print ("lib="..dump(lib)) lib = lib or { id = "<empty>", list = {}, title = "empty" , type = "library" }
traverse_list(lib.list,function(elem) traverse_list(lib.list,function(elem)
print("#") print("#")
if fields["fold_".. elem.id] == true then if fields["fold_".. elem.id] == true then
@ -136,17 +139,13 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
end) end)
-- print("libafger="..dump(lib))
--[[
if (fields.save) or fields.key_enter_field == "title" then
]]--
data.lib = lib data.lib = lib
stack:get_meta():from_table({ fields = data}) stack:get_meta():from_table({ fields = data})
player:set_wielded_item(stack) player:set_wielded_item(stack)
local formspec = render_formspec(lib) local formspec = render_formspec(lib)
minetest.show_formspec(user, "mesecons_autotools:library_view", formspec) minetest.show_formspec(user, "mesecons_autotools:library_view", formspec)
]]--
end) end)
function render_formspec(lib) function render_formspec(lib)
@ -159,9 +158,7 @@ end
lib = { lib = {
id = "l1", id = "l1", title = "library bla bla ", type = "library",
title = "library bla bla ",
type = "library",
list = { list = {
{id = "c1", type="circuit", title="somethign something"}, {id = "c1", type="circuit", title="somethign something"},
{id = "c2", type="circuit", title="somethign something2"}, {id = "c2", type="circuit", title="somethign something2"},
@ -173,29 +170,67 @@ end
}, },
{id = "c111", type="circuit", title="circuit sak/32/d0"}, {id = "c111", type="circuit", title="circuit sak/32/d0"},
{id = "c1x", type="circuit", title="somethign something"},
{id = "c2x", type="circuit", title="somethign something2"},
{id = "l2x", type="library", title="wooihoihoh/", folded = false,
list = {
{id = "c11x", type="circuit", title="somethign something"},
{id = "c21x", type="circuit", title="somethign something2"},
}
},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="circuit sak/32/d0"},
{id = "c111x", type="circuit", title="ostatni ak/32/d0"},
} }
} }
function generate_empty_library(file)
local lib = {
id = file, list = {} , title = "" , type = "library" , folded = false
}
return lib
end
function on_place_library(itemstack, player, pointed_thing) function on_place_library(itemstack, player, pointed_thing)
local user = player:get_player_name() local user = player:get_player_name()
local stack = player:get_wielded_item() local stack = player:get_wielded_item()
local data = stack:get_meta():to_table().fields local fields = stack:get_meta():to_table().fields
print("DU:"..dump(itemstack:get_meta():to_table())) -- print("DU:"..dump(itemstack:get_meta():to_table()))
print("item stackc:" .. dump(itemstack)) -- print("item stackc:" .. dump(itemstack))
if data.count == nil then data.count = 0 end -- if data.count == nil then data.count = 0 end
data.count = data.count + 1 -- data.count = data.count + 1
--[[ local lib = {}
if fields.file == nil then
stack:get_meta():from_table({ fields = data}) fields.file = generate_file_name_library(user)
player:set_wielded_item(stack) lib = generate_empty_library(fields.file)
save_table_to_file(fields.file,lib)
end
]]--
stack:get_meta():from_table({ fields = fields})
-- player:set_wielded_item(stack)
--lib = lib
local formspec = render_formspec(lib) local formspec = render_formspec(lib)
-- print("formtspec="..formspec) -- print("formtspec="..formspec)
minetest.show_formspec(user, "mesecons_autotools:library_view", formspec) minetest.show_formspec(user, "mesecons_autotools:library_view", formspec)
@ -210,7 +245,8 @@ minetest.register_tool("mesecons_autotools:library", {
stack_max = 1, stack_max = 1,
on_place = on_place_library, -- on_place = on_place_library,
on_use = on_place_library,
--[[ --[[
on_use = on_use_new_circuit, on_use = on_use_new_circuit,

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

@ -49,7 +49,7 @@ local tool_list = {
{"red", "Delete Tool"}, {"red", "Delete Tool"},
{"yellow", "Move Horizontal Tool"}, {"yellow", "Move Horizontal Tool"},
{"yellow_updown", "Move Vertical Tool"}, {"yellow_updown", "Move Vertical Tool"},
{"refresh", "Refresh Tool"} {"refresh", "Refresh Tool"},
} }
for _,t in pairs(tool_list) do for _,t in pairs(tool_list) do
local tool = t[1] local tool = t[1]

@ -26,6 +26,7 @@ end)
mesecons_autotools.register_action("black","right","block", function(user,pos,rad) mesecons_autotools.register_action("black","right","block", function(user,pos,rad)
local pos1 = mesecons_autotools.get_pos(user,1) local pos1 = mesecons_autotools.get_pos(user,1)
local pos2 = mesecons_autotools.get_pos(user,2) local pos2 = mesecons_autotools.get_pos(user,2)
local user_direction = radians_to_direction_looking_forward(rad)
local sel = {} local sel = {}
sel.pos1 = pos1 sel.pos1 = pos1
sel.pos2 = pos2 sel.pos2 = pos2
@ -48,7 +49,10 @@ mesecons_autotools.register_action("black","right","block", function(user,pos,ra
mesecons_autotools.render(user) mesecons_autotools.render(user)
mesecons_autotools.zero_stack_counter(user) mesecons_autotools.zero_stack_counter(user)
mesecons_autotools.zero_stack_direction(user) mesecons_autotools.zero_stack_direction(user)
end elseif create_bundle_bended_wire(user,sel,pos,user_direction) then
-- nothing to do here so far
-- update of new selection is inside the function
end

@ -66,14 +66,14 @@ function is_in_tube(sel,p)
return false return false
end end
--[[
function is_in_wings(sel,p) function is_in_wings(sel,p)
if is_in_selection(sel,p) then return false end if is_in_selection(sel,p) then return false end
if not is_in_layer(sel,p) then return false end if not is_in_layer(sel,p) then return false end
if is_in_tube(sel,p) then return false end if is_in_tube(sel,p) then return false end
return true return true
end end
]]--
function front_wall_tube(sel,p) function front_wall_tube(sel,p)
@ -560,12 +560,9 @@ function create_bundle_straight_wire(sel,pos)
local pos1 = sel.pos1 local pos1 = sel.pos1
local pos2 = sel.pos2 local pos2 = sel.pos2
if not is_in_tube(sel,pos) then return false end if not is_in_tube(sel,pos) then return false end
local wall,direction = front_wall_tube(sel,pos) local wall,direction = front_wall_tube(sel,pos)
local distance = distance_to_selection(sel,pos,direction) local distance = distance_to_selection(sel,pos,direction)
@ -578,8 +575,6 @@ function create_bundle_straight_wire(sel,pos)
end) end)
else else
create_wires_inside_selection(sel,wall,direction) create_wires_inside_selection(sel,wall,direction)
@ -589,11 +584,307 @@ function create_bundle_straight_wire(sel,pos)
end) end)
end end
return true return true
end end
function is_flat_selection(sel)
local p1 = sel.pos1
local p2 = sel.pos2
if( p1.x == p2.x and p1.z == p2.z ) then return false end
if (p1.x == p2.x ) then return true end
if( p1.z == p2.z ) then return true end
return false
end
-- direction of selection (according to the shape (=flat edge)
function sel_directions_frontwall(sel)
local p1 = sel.pos1
local p2 = sel.pos2
if( p1.x == p2.x ) then
return {x=1,y=0,z=0},{x=-1,y=0,z=0}
end
if( p1.z == p2.z ) then
return {x=0,y=0,z=1},{x=0,y=0,z=-1}
end
print "Error: sel_direction(sel) not a flat selection (use is_flat_selection(sel) to check!!!"
return {x=0,y=0,z=0}
end
function normalize(p)
local x,y,z
if( p.x > 0 ) then x = 1 end
if( p.x == 0 ) then x = 0 end
if( p.x < 0 ) then x = -1 end
if( p.y > 0 ) then y = 1 end
if( p.y == 0 ) then y = 0 end
if( p.y < 0 ) then y = -1 end
if( p.z > 0 ) then z = 1 end
if( p.z == 0 ) then z = 0 end
if( p.z < 0 ) then z = -1 end
return {x=x,y=y,z=z}
end
-- vectors directing from p1 to p2
function toward_direction(p1,p2)
local d1 = {x=p2.x-p1.x,y=0,z=0 }
local d2 = {x=0,y=0,z=p2.z-p1.z}
return normalize(d1),normalize(d2)
end
-- comparing two points/vectors
function cmp(p1,p2)
if (p1.x==p2.x) and (p1.y==p2.y) and (p1.z==p2.z) then return true else return false end
end
-- {p1,p2} cup {p3,p4} // only one can be common, so I dont check other possibilietes
function cup(p1,p2,p3,p4)
if cmp(p1,p3) then return p1 end
if cmp(p1,p4) then return p1 end
if cmp(p2,p3) then return p2 end
if cmp(p2,p4) then return p2 end
--print("DEGUG.CRITICAL ERROR: cup, p1,p2,p3,p4 =" ..dump(p1)..dump(p2)..dump(p3)..dump(p4))
return nil
end
-- checks if vectors have the same parallerism
function same_line(d1,d2)
if math.abs(d1.x) == math.abs(d2.x) and math.abs(d1.z) == math.abs(d2.z) then return true end
return false
end
-- returns direction from selection toward point, and direction from poin towards selection
function compute_directions(sel,pos)
local s1,s2
local sp1,sp2
s1,s2 = sel_directions_frontwall(sel)
--print("sel,pos="..dump(sel).."," .. dump(pos))
sp1,sp2 = toward_direction(sel.pos1,pos) -- no matter if sel.pos1 or sel.pos2
--print("toward="..dump(sp1)..","..dump(sp2))
local sel_direction = cup(s1,s2,sp1,sp2)
local p1,p2
p1,p2 = toward_direction(pos,sel.pos1) -- no matter if sel.pos1 or sel.pos2
local pos_direction
if same_line(sel_direction,p1) then
pos_direction = p2
else
pos_direction = p1
end
return sel_direction, pos_direction
end
-- computing point A
function compute_A(sel,sel_dir, pos, pos_dir)
local s1 = sel.pos1
local s2 = sel.pos2
local dist1x = math.abs(pos.x-s1.x)
local dist2x = math.abs(pos.x-s2.x)
local dist1z = math.abs(pos.z-s1.z)
local dist2z = math.abs(pos.z-s2.z)
local mx = math.min(dist1x,dist2x)
local mz = math.min(dist1z,dist2z)
local sihfta
if pos_dir.x ~= 0 then
shifta = vector.multiply(pos_dir,mx)
else
shifta = vector.multiply(pos_dir,mz)
end
local a = vector.add(shifta,pos)
return a
end
--[[
function compute_X(sel,sel_dir,pos,pos_dir,A)
local d = vector.multiply(sel_dir,-1)
return vector.add(A,d)
end
]]--
--[[
function selection_len(sel)
local p1=sel.pos1
local p2=sel.pos2
if( p1.x == p2.x ) then return math.abs(p2.z-p1.z)+1 end
if( p1.z == p2.z ) then return math.abs(p2.x-p1.x)+1 end
-- print("CRITICAL ERRROR ERROR ERROR: selection_len(sel): "..dump(sel))
end
]]--
--[[
function compute_D(sel,sel_dir,pos,pos_dir,A)
local l = selection_len(sel)
return vector.add(A, vector.multiply(pos_dir,l-1))
end
]]--
--[[
function crossing_point(p1,p1_dir,p2)
local len
local cross
if(p1_dir.x ~= 0 ) then
len = math.abs(p2.x-p1.x)
else
len = math.abs(p2.z-p1.z)
end
cross = vector.add(p1,vector.multiply(p1_dir,len))
return cross
end
]]--
function create_wire_bended(p1,dir1, p2, dir2)
local len
if(dir1.x ~= 0 ) then
len = math.abs(p2.x-p1.x)
else
len = math.abs(p2.z-p1.z)
end
create_straight_wire_n(p1,dir1,len+1)
local cross = vector.add(p1,vector.multiply(dir1,len))
if(dir2.x ~= 0 ) then
len = math.abs(p2.x-p1.x)
else
len = math.abs(p2.z-p1.z)
end
create_straight_wire_n(cross,dir2,len+1)
end
--[[
function create_bundle_twist_wire(sel,in_dir,_X,_Y,xm, out_dir, _A,am)
local max_size = 0
--print("debug|sel.in_dir,X,Y,xm,out_dir,A,am=" .. dump(sel) .. dump(in_dir)..dump(X)..dump(Y)..dump(xm)..dump(out_dir)..dump(A).. dump(am))
local usize = hight_of_selection(sel)
local X = _X
local A = _A
local Y = _Y
for k=1,usize do
--move all point up to next layer
local shifts_in = get_shifts(X,xm,in_dir,sel)
local ssize = #shifts_in
max_size = math.max(max_size,ssize)
print("shifts="..dump(shifts_in))
for i=1,ssize do
local start_pos = vector.add(X,vector.multiply(xm,shifts_in[i]))
local end_pos = vector.add(A,vector.multiply(am,i-1))
create_wire_bended(start_pos,in_dir,end_pos,vector.multiply(out_dir,1))
end
local up = {x=0,y=1,z=0}
X = vector.add(X,up)
Y = vector.add(Y,up)
A = vector.add(A,up)
end
return max_size
end
]]--
function compute_S1(sel,sel_dir,pos,pos_dir)
local s1 = sel.pos1
local s2 = sel.pos2
local p = pos
local sx1 = math.abs(p.x-s1.x)
local sx2 = math.abs(p.x-s2.x)
local sz1 = math.abs(p.z-s1.z)
local sz2 = math.abs(p.z-s2.z)
local mx = math.min(sx1,sx2)
local mz = math.min(sz1,sz2)
local A = compute_A(sel,sel_dir, pos,pos_dir)
local len
if( sel_dir.x ~= 0 ) then
len = mx
else
len = mz
end
local S1 = vector.add(A, vector.multiply( vector.multiply(sel_dir,len) , -1))
return S1
end
function get_virtual_shifts(pos,move_direction,look_direction,sel)
local curr = pos
local shift = 0
local shifts = {}
while( is_in_selection(sel,curr) ) do
table.insert(shifts,shift)
shift = shift + 1
curr = vector.add(curr,move_direction)
end
return shifts
end
function create_bundle_bended_wire(user,sel,pos,user_dir)
local sel_direction, pos_direction
local pos1 = sel.pos1
local pos2 = sel.pos2
if not is_in_wings(sel,pos) then return false end
if not is_flat_selection(sel) then return false end
sel_direction,pos_direction = compute_directions(sel,pos)
local usize = hight_of_selection(sel)
local S1_original = compute_S1(sel,sel_direction,pos,pos_direction)
local max_wires = 0
if is_empty_selection(pos1,pos2) then
local S1 = S1_original
for k=1,usize do
local shifts = get_virtual_shifts(S1,pos_direction,sel_direction,sel)
local ssize = #shifts
max_wires = math.max(max_wires,ssize)
for i=1,ssize do
local start_pos = vector.add(S1,vector.multiply(pos_direction,shifts[i]))
local end_pos = vector.add(pos, vector.multiply(sel_direction,i-1))
create_wire_bended(start_pos,sel_direction,end_pos, vector.multiply(pos_direction,-1))
end
S1 = vector.add(S1,{x=0,y=1,z=0})
end
else
local S1 = S1_original
for k=1,usize do
local shifts = get_shifts(S1,pos_direction,sel_direction,sel)
local ssize = #shifts
max_wires=math.max(max_wires,ssize)
for i=1,ssize do
local start_pos = vector.add(S1,vector.multiply(pos_direction,shifts[i]))
local end_pos = vector.add(pos, vector.multiply(sel_direction,i-1))
create_wire_bended(start_pos,sel_direction,end_pos, vector.multiply(pos_direction,-1))
end
S1 = vector.add(S1,{x=0,y=1,z=0})
end
end
-- computing selection
local new_sel = {}
new_sel.pos1 = pos
new_sel.pos2 = vector.add(vector.add(pos, vector.multiply(sel_direction,max_wires-1)),{x=0,y=usize-1,z=0})
mesecons_autotools.set_pos(user,1,new_sel.pos1)
mesecons_autotools.set_pos(user,2,new_sel.pos2)
mesecons_autotools.render(user)
mesecons_autotools.zero_stack_counter(user)
mesecons_autotools.zero_stack_direction(user)
return true
end

@ -179,7 +179,6 @@ local function move_selection(user,rad,click)
copy_to_buffor(pos[1],pos[2],buffor) copy_to_buffor(pos[1],pos[2],buffor)
tail = generate_tail_wires(back_wall,direction) tail = generate_tail_wires(back_wall,direction)
print("yellow.buffor= " .. dump(buffor) )
delete(pos[1],pos[2]) delete(pos[1],pos[2])
paste_from_buffor(direction,buffor) paste_from_buffor(direction,buffor)