add circuit preview

This commit is contained in:
Deet Mit 2020-07-19 02:09:39 +02:00
parent 149d73a664
commit 91566bedbb
57 changed files with 469 additions and 96 deletions

BIN
circuit_previeew.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

@ -236,7 +236,7 @@ minetest.register_craftitem("mesecons_autowire:wire", {
mesecons_autowire.pos1 = pointed_thing.above
end
print("debug.DB="..dump(mesecons_autowire))
end,
on_place = function(itemstack, user, pointed_thing)
if not pointed_thing then return end

@ -152,20 +152,26 @@ local short_title_size = 35
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "mesecons_autowire:circuit" then return end
if( fields.save and fields.title ) then
if( fields.save) then
local stack = player:get_wielded_item()
local inv = player:get_inventory()
local new_stack = nil
local meta = stack:get_meta()
local data = meta:to_table().fields
if data == nil then data = {} end
data.title = fields.title or ""
-- if no nodes then save nodes
if not data.nodes then
local nodes = minetest.deserialize(data.nodes)
--if not data.nodes then
if nodes == nil then
local rad = player:get_look_horizontal()
local tab = store_nodes_from_selection(rad)
print("tab="..dump(tab))
--print("tab="..dump(tab))
if( tab ) then
data.nodes = minetest.serialize(tab)
data.rad = rad
@ -176,17 +182,26 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
print("debug.af="..dump(data))
-- print("debug.af="..dump(data))
local count = stack:get_count()
if( count <= 0 ) then return end
if count == 1 then
inv:remove_item("main",stack)
if( stack:get_name() == "mesecons_autowire:circuit_full" ) then
elseif ( stack:get_name() == "mesecons_autowire:circuit_empty" ) then
local count = stack:get_count()
if( count <= 0 ) then return end
print("debug.count="..count)
if count == 1 then
--inv:remove_item("main",stack)
stack:set_name("mesecons_autowire:circuit_full")
else
stack:set_count(count - 1)
print("debug.count*="..stack:get_count() )
new_stack = ItemStack("mesecons_autowire:circuit_full")
end
else
stack:set_count(count - 1)
end
new_stack = ItemStack("mesecons_autowire:circuit_full")
if not fields.text then fields.text = "" end
if not fields.title then fields.title = "" end
@ -211,98 +226,23 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
else
minetest.add_item(player:get_pos(), new_stack)
end
else
print("debug.new_stack==nil")
stack:get_meta():from_table( {fields = data } )
end
--debug
player:set_wielded_item(stack)
end
end)
local function show_dialog(itemstack,user,pointed_thing)
local player_name = user:get_player_name()
local formspec
local esc = minetest.formspec_escape
local meta = itemstack:get_meta()
local data = meta:to_table().fields
local title, text = data.title or "", data.text or ""
local nodes_string = "empty"
local nodes = minetest.deserialize(data.nodes)
if( nodes ) then
-- show nodes
nodes_string = "size = (".. #nodes[1] .. ","..#nodes .. ")"
end
formspec = "size[18,8]" ..
"field[0.5,0.5;7.5,1;title;"..S("Name")..";"..S(title).."]"..
"textarea[0.5,1.5;7.5,7;text;" .. esc(S("Description:")) .. ";" ..
esc(S(text)) .. "]" ..
"button_exit[2.5,7.5;3,1;save;" .. esc(S("Save")) .. "]" ..
"label[10,0.5;"..nodes_string.."]"..
""
minetest.show_formspec(player_name, "mesecons_autowire:circuit", formspec)
return itemstack
end
local function make_selection(nodes,pos,rad)
if nodes == nil then return end
--if check_selection(mesecons_automove.pos1,mesecons_automove.pos2) == false then return end
local vsize = #nodes
local hsize = #nodes[1]
print("dump"..dump(nodes) .. "====" .. dump(pos) .. "===" .. vsize .. "=" .. hsize )
local direction = radians_to_direction_looking_forward(rad)
mesecons_automove.pos1 = pos
local dx,dz = 0,0
print("dumpsirection="..dump(direction))
hsize = hsize -1
vsize = vsize -1
if direction.z == 1 then
dx,dz = hsize,vsize
elseif direction.z == -1 then
dx,dz = -hsize,-vsize
elseif direction.x == 1 then
dx,dz = vsize,-hsize
else
--dirextion.x == -1
dx,dz = -vsize,hsize
end
mesecons_automove.pos2 = { x=pos.x + dx, y=pos.y, z = pos.z+dz }
mesecons_automove.rad = rad
--print("debug.all="..dump(mesecons_automove))
update_selection()
end
local function direction_to_number(direction)
if( direction.z == 1 ) then
return 1
@ -333,6 +273,124 @@ local function is_exception(node)
return false
end
local function node_to_image(node)
if node == nil then
return "unknown.png"
end
local name = node.name
local param2 = node.param2 or 0
if( name == "air" ) then
return "empty.png"
end
if (name == "mesecons_insulated:insulated_off") or
(name == "mesecons_insulated:insulated_on" ) then
if( param2 % 2 == 0 ) then
return "wireh.png"
else
return "wirev.png"
end
end
if (name == "mesecons_extrawires:crossover_off") or
(name == "mesecons_extrawires:crossover_on") or
(name == "mesecons_extrawires:crossover_01") or
(name == "mesecons_extrawires:crossover_10" ) then
return "corssover.png"
end
if (name == "mesecons_extrawires:corner_off" ) or
(name == "mesecons_extrawires:corner_on" ) then
return "corner"..param2 .. ".png"
end
if (name == "mesecons_extrawires:tjunction_off" ) or
(name == "mesecons_extrawires:tjunction_on" ) then
return "tjunction"..param2 .. ".png"
end
if ( name == "mesecons_morewires:xjunction_off" ) or
( name == "mesecons_morewires:xjunction_on" ) then
return "xjunction.png"
end
if( name == "mesecons_regs:flipflop_off" ) or ( name == "mesecons_regs:flipflop_on") then
return "ff"..param2..".png"
end
if( name == "mesecons_regs:latch_off" ) or ( name == "mesecons_regs:latch_on") then
return "latch"..param2..".png"
end
if( name == "mesecons_gates:and_off" ) or ( name =="mesecons_gates:and_on" )then
return "and".. param2 .. ".png"
end
if( name == "mesecons_gates:nand_off" ) or ( name =="mesecons_gates:nand_on" )then
return "nand".. param2 .. ".png"
end
if( name == "mesecons_gates:nor_off" ) or ( name =="mesecons_gates:nor_on" )then
return "nor".. param2 .. ".png"
end
if( name == "mesecons_gates:or_off" ) or ( name =="mesecons_gates:or_on" )then
return "or".. param2 .. ".png"
end
if( name == "mesecons_gates:xor_off" ) or ( name =="mesecons_gates:xor_on" )then
return "xor".. param2 .. ".png"
end
if( name == "mesecons_gates:diode_off" ) or ( name =="mesecons_gates:diode_on" )then
return "diode".. param2 .. ".png"
end
if( name == "mesecons_gates:not_off" ) or ( name =="mesecons_gates:not_on" )then
return "not".. param2 .. ".png"
end
return "unknown.png"
end
local function generate_spec(starting,nodes)
if nodes == nil then
return ""
end
local mv = #nodes
local mh = #nodes[1]
local str = ""
local x,z
for x = 1,mh do
for z = 1,mv do
local img = node_to_image(nodes[z][x])
--str = str .. "image[" .. 0.5*x .. "," .. 0.5*(mv-z+1) ..
str = str .. "image[" .. x +starting .. "," .. (mv-z+1) ..
";1,1;"..img.."]"
--";0.5,0.5;"..img.."]"
end
end
return str
end
local function special_case(node,rotate)
local new_node = node
@ -357,8 +415,6 @@ local function special_case(node,rotate)
return nil
end
local function rotate_node(node, saved_direction,direction)
if( is_exception(node) ) then return node end
@ -382,6 +438,232 @@ local function rotate_node(node, saved_direction,direction)
return new_node
end
local function rotate_nodes_for_window(nodes, oryginal)
if nodes == nil then return nil end
local mv = #nodes
local mh = #nodes[1]
local dir = {x=0,z=1}
local tab = {}
for k = 1,mv do
tab[k] = {}
for i = 1,mh do
tab[k][i] = rotate_node(nodes[k][i],oryginal,dir)
end
end
return tab
end
function is_gate(node)
local gates = {
"mesecons_gates:and",
"mesecons_gates:or",
"mesecons_gates:xor",
"mesecons_gates:nand",
"mesecons_gates:nor",
"mesecons_gates:not",
"mesecons_gates:diode",
"mesecons_gates3:and3",
"mesecons_gates3:or3",
"mesecons_gates3:nor3",
"mesecons_gates3:nand3",
"mesecons_regs:flipflop",
"mesecons_regs:latch"
}
local gates_with_states = {}
for _,v in ipairs(gates) do
table.insert(gates_with_states,v.."_on")
table.insert(gates_with_states,v.."_off")
end
for _,v in ipairs(gates_with_states) do
if node.name == v then
print("found.gate="..v)
return true
end
end
return false
end
local function count_stats(tab)
if tab == nil then return 0 end
local block, wire, gate = 0,0,0
local h,v = #tab[1], #tab
for i = 1,h do
for k = 1,v do
if tab[k][i].name ~= "air" then
block = block + 1
end
if is_wire_node(tab[k][i]) then
wire = wire + 1
end
if is_gate(tab[k][i]) then
gate = gate + 1
end
end
end
return block, wire, gate
end
local function show_dialog(itemstack,user,pointed_thing)
local player_name = user:get_player_name()
local formspec
local esc = minetest.formspec_escape
local meta = itemstack:get_meta()
local data = meta:to_table().fields
local title, text = data.title or "", data.text or ""
local nodes_string = "empty"
local nodes = minetest.deserialize(data.nodes)
local hight = 0
local width = 0
if( nodes ) then
-- show nodes
nodes_string = "size = (".. #nodes[1] .. ","..#nodes .. ")"
hight = #nodes
width = #nodes[1]
end
if( nodes == nil ) and
( check_selection(mesecons_automove.pos1,mesecons_automove.pos2) == false ) then
return
end
if nodes == nil then
local rad = user:get_look_horizontal()
local tab = store_nodes_from_selection(rad)
if tab == nil then return end
nodes = tab
data.rad = rad
hight = #nodes
width = #nodes[1]
end
local size_w = 45
size_w = width + 10
if( size_w < 20 ) then size_w = 20 end
if( size_w > 45 ) then size_w = 45 end
local size_scroll = size_w - 10
local starting_point = math.max(0, (size_scroll/2) - (width/2))
local block,wire,gate = count_stats(nodes)
local stats = "size : " .. width .. "x"..hight .. "(=".. width * hight .. ")\n" ..
"wires : " .. wire .. "\n" ..
"gates : " .. gate .. "\n" ..
"other : ".. block - wire - gate .. "\n" ..
"blocks: " .. block
local circuitspec =
generate_spec(starting_point,
rotate_nodes_for_window(
nodes,
radians_to_direction_looking_forward(
tonumber(data.rad))))
formspec =
"formspec_version[3]"..
--"size[45,25]" ..
"size[".. size_w .. ",25]"..
"field[0.5,1;8,1;title;"..S("Name")..";"..S(title).."]"..
"textarea[0.5,3;8,15;text;" .. esc(S("Description:")) .. ";" ..
esc(S(text)) .. "]" ..
"button_exit[2.5,18;3,1;save;" .. esc(S("Save")) .. "]" ..
"textarea[0.5,19;8,5;;;" ..
esc(S(stats)) .. "]" ..
--"label[10,0.5;"..nodes_string.."]"..
--"container[8,0.5]"..
"scrollbaroptions[min=0;max=".. hight -15 .. ";smallstep=2;largestep=10;arrows=show]"..
--"scrollbar[44,0.5;0.6,24;vertical;scr0;0]"..
"scrollbar[".. size_w - 1 .. ",0.5;0.6,24;vertical;scr0;0]"..
--"scroll_container[8,0.5;36,23;scr0;vertical;1]"..
"scroll_container[8,0.5;".. size_w -10 .. ",23;scr0;vertical;1]"..
circuitspec ..
"scroll_container_end[]"..
--"container_end[]"..
""
minetest.show_formspec(player_name, "mesecons_autowire:circuit", formspec)
return itemstack
end
local function make_selection(nodes,pos,rad)
if nodes == nil then return end
--if check_selection(mesecons_automove.pos1,mesecons_automove.pos2) == false then return end
local vsize = #nodes
local hsize = #nodes[1]
-- print("dump"..dump(nodes) .. "====" .. dump(pos) .. "===" .. vsize .. "=" .. hsize )
local direction = radians_to_direction_looking_forward(rad)
mesecons_automove.pos1 = pos
local dx,dz = 0,0
--print("dumpsirection="..dump(direction))
hsize = hsize -1
vsize = vsize -1
if direction.z == 1 then
dx,dz = hsize,vsize
elseif direction.z == -1 then
dx,dz = -hsize,-vsize
elseif direction.x == 1 then
dx,dz = vsize,-hsize
else
--dirextion.x == -1
dx,dz = -vsize,hsize
end
mesecons_automove.pos2 = { x=pos.x + dx, y=pos.y, z = pos.z+dz }
mesecons_automove.rad = rad
--print("debug.all="..dump(mesecons_automove))
update_selection()
end
local function paste_nodes(nodes,saved_direction,direction)
local pos1,pos2 = mesecons_automove.pos1,mesecons_automove.pos2

@ -0,0 +1,71 @@
local function on_use_page(itemstack, user, pointed_thing)
local player_name = user:get_player_name()
local esc = minetest.formspec_escape
local meta = itemstack:get_meta():to_table()
print("itemstack:meta()="..dump(meta))
minetest.create_detached_inventory("slots")
local inv = minetest.create_detached_inventory("inventory_name", {
allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
return count -- allow moving
end,
allow_put = function(inv, listname, index, stack, player)
return stack:get_count() -- allow putting
end,
allow_take = function(inv, listname, index, stack, player)
return -1 -- don't allow taking
end,
on_put = function(inv, listname, index, stack, player)
minetest.chat_send_all(player:get_player_name() ..
" gave " .. stack:to_string() ..
" to the donation chest from " .. minetest.pos_to_string(player:get_pos()))
end,
})
local formspec = "size[10,8]" ..
"list[detached:inventory_name;main;0.5,0;8,4]"..
"list[current_player;main;0,3;8,4]"..
""
local stack = ItemStack("default:dirt 7")
inv:set_size("main",8)
inv:add_item("main",stack)
minetest.show_formspec(player_name,"mesecons_autowire:page", formspec)
end
minetest.register_craftitem("mesecons_autowire:page_empty", {
description = "Circuit Page empty",
--inventory_image = "circuit_empty.png",
inventory_image = "page_empty.png",
--groups = {book = 1, flammable = 3},
on_use = on_use_page
})
minetest.register_craftitem("mesecons_autowire:page_full", {
description = "Circuit page saved",
--inventory_image = "circuit_full.png",
inventory_image = "page_full.png",
--groups = {book = 1, not_in_creative_inventory = 1, flammable = 3},
groups = { not_in_creative_inventory = 1},
stack_max = 1,
--on_use = on_use_circuit,
--on_place = on_place_circuit,
--on_secondary_use = on_place_circuit
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 177 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 201 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 B

@ -13,6 +13,26 @@ function dump(o)
end
function is_wire_node(node)
local list = {
"mesecons_insulated:insulated_off", "mesecons_insulated:insulated_on",
"mesecons_extrawires:corner_off", "mesecons_extrawires:corner_on",
"mesecons_extrawires:tjunction_off", "mesecons_extrawires:tjunction_on",
"mesecons_extrawires:crossover_off", "mesecons_extrawires:crossover_on",
"mesecons_extrawires:crossover_10", "mesecons_extrawires:crossover_01",
"mesecons_morewires:xjunction_off", "mesecons_morewires:xjunction_on",
}
local pos_name = node.name
for i,name in ipairs(list) do
if name == pos_name then
return true
end
end
return false
end
function is_wire(pos)
local list = {
"mesecons_insulated:insulated_off", "mesecons_insulated:insulated_on",
@ -124,7 +144,7 @@ end
function rotate_pins(p,r)
local cur = p
i = 0
local i = 0
while( i< r) do
cur = rotate_pin(cur)
i = i +1

@ -176,7 +176,7 @@ end
local function register_flipflop()
local name = "flipflop"
description = "Logic Memory Circuit: "..name
local description = "Logic Memory Circuit: "..name
local basename = "mesecons_regs:"..name
mesecon.register_node(basename,