Added a lot of circuits code
This commit is contained in:
parent
3ea8517ff9
commit
9d3ab2f0e0
@ -113,6 +113,7 @@ end
|
||||
nbconn: Which correspond each nodebox correspond to
|
||||
ex: {1,1,2,1} -> nodeboxes 1,2,3 correspond to connection ES, and nodebox 2 correspond to connection W
|
||||
--]]
|
||||
logikraft.cablesblocks = {}
|
||||
logikraft.cablesnb = {}
|
||||
logikraft.cablesconn = {}
|
||||
logikraft.cablesnbconn = {}
|
||||
@ -150,6 +151,7 @@ local function createCables(name,nodeboxes,connections,nbconn)
|
||||
if name == "EW8_N1_S1"
|
||||
then print(dump(connections2)) end
|
||||
|
||||
logikraft.cablesblocks["logikraft:cable_"..name] = name
|
||||
logikraft.cablesnb[name] = nodeboxes
|
||||
logikraft.cablesconn[name] = connections2
|
||||
logikraft.cablesnbconn[name] = nbconn2
|
||||
|
||||
@ -33,7 +33,8 @@ minetest.register_node("logikraft:circuitBlock", {
|
||||
description = "Circuit Block",
|
||||
tiles = {"circuit_block.png"},
|
||||
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||
logikraft.discoverBlocks(pos)
|
||||
local poz = logikraft.discoverBlocks(pos)
|
||||
local circuit = logikraft.compileCircuit(poz)
|
||||
end,
|
||||
groups = {circuitry = 1,dig_immediate = 3}
|
||||
})
|
||||
@ -12,16 +12,16 @@ end
|
||||
-- | XXX h
|
||||
-- +−−−−−−−−−−−> x+
|
||||
-- {name,width,height,{cables_left,cables_right,cables_top,cables_bottom}}
|
||||
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}}
|
||||
|
||||
logikraft.components = {
|
||||
["and"] = {2,3,{{"in_2",nil,"in_1"},{nil,"out",nil},nil,nil}},
|
||||
["switch"] = {2,1,{{"in"},{"out"},{"activate",nil},nil}},
|
||||
["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 = {}
|
||||
logikraft.componentsblocks = {}
|
||||
|
||||
for name,cmp in pairs(components)
|
||||
for name,cmp in pairs(logikraft.components)
|
||||
do
|
||||
local w = cmp[1]
|
||||
local h = cmp[2]
|
||||
@ -109,7 +109,7 @@ do
|
||||
end
|
||||
end
|
||||
})
|
||||
componentsblocks["nodename"] = {name,x,y}
|
||||
logikraft.componentsblocks[nodename] = {name = name, x = x, y = y}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
129
circuits.lua
129
circuits.lua
@ -31,9 +31,6 @@ function logikraft.discoverBlocks(pos)
|
||||
do
|
||||
local p = poz[j]
|
||||
j = j+1
|
||||
print(dump(poz))
|
||||
print(i)
|
||||
print(j)
|
||||
local neighbourhood = {
|
||||
{x = p.x+1, y = p.y, z = p.z},
|
||||
{x = p.x-1, y = p.y, z = p.z},
|
||||
@ -52,6 +49,128 @@ function logikraft.discoverBlocks(pos)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
minetest.chat_send_all("Discovered ".. (i-1) .. " blocks")
|
||||
end
|
||||
return poz
|
||||
end
|
||||
|
||||
local function collapseConnectionsStep(connz)
|
||||
for k,conna in ipairs(connz)
|
||||
do
|
||||
for l,connb in ipairs(connz)
|
||||
do
|
||||
if k<l
|
||||
then
|
||||
local match = table.findMatch1(conna,connb)
|
||||
if match
|
||||
then
|
||||
-- we add connb\m[2] to conna\m[1], and then we remove connbç
|
||||
table.remove(conna,match[1])
|
||||
for i,x in pairs(connb) do
|
||||
if i ~= match[2] then table.insert(conna,x) end
|
||||
end
|
||||
table.remove(connz,l)
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
function logikraft.collapseConnections(connz)
|
||||
while collapseConnectionsStep(connz)
|
||||
do end
|
||||
end
|
||||
|
||||
-- {x,y,z,d=0 if x+, 1 if z+}
|
||||
function logikraft.compileCircuit(poz)
|
||||
|
||||
local components = {}
|
||||
local connections = {}
|
||||
for i,pos in ipairs(poz)
|
||||
do
|
||||
local node = minetest.get_node(pos)
|
||||
print("Analyzing "..node.name)
|
||||
if logikraft.cablesblocks[node.name]
|
||||
then
|
||||
-- It is a cable
|
||||
local conns = logikraft.cablesconn[logikraft.cablesblocks[node.name]]
|
||||
for j,c in ipairs(conns)
|
||||
do
|
||||
if not (c == 1 or c == 2 or c == 4 or c == 8 or c == 17 or c == 18 or c == 20 or c == 24)
|
||||
then
|
||||
local newconn = {}
|
||||
if math.fmod(math.floor(c/1),2)==1
|
||||
then table.insert(newconn,{x=pos.x-1,y=pos.y,z=pos.z,d=0}) end
|
||||
if math.fmod(math.floor(c/2),2)==1
|
||||
then table.insert(newconn,{x=pos.x,y=pos.y,z=pos.z-1,d=1}) end
|
||||
if math.fmod(math.floor(c/4),2)==1
|
||||
then table.insert(newconn,{x=pos.x,y=pos.y,z=pos.z,d=0}) end
|
||||
if math.fmod(math.floor(c/8),2)==1
|
||||
then table.insert(newconn,{x=pos.x,y=pos.y,z=pos.z,d=1}) end
|
||||
table.insert(connections,newconn)
|
||||
end
|
||||
end
|
||||
elseif logikraft.componentsblocks[node.name]
|
||||
then
|
||||
-- It is a component
|
||||
local cpblock = logikraft.componentsblocks[node.name]
|
||||
if cpblock.x == 1 and cpblock.y == 1
|
||||
then
|
||||
local component = logikraft.components[cpblock.name]
|
||||
local cp = {type = cpblock.name, ports = {}}
|
||||
print(dump(component))
|
||||
-- Left
|
||||
if component[3][1]
|
||||
then
|
||||
for j,n in pairs(component[3][1])
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x-1,y=pos.y,z=pos.z+j-1,d=0}
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Right
|
||||
if component[3][2]
|
||||
then
|
||||
for j,n in pairs(component[3][2])
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x+component[1]-1,y=pos.y,z=pos.z+j-1,d=0}
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Top
|
||||
if component[3][3]
|
||||
then
|
||||
for j,n in pairs(component[3][3])
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x+j-1,y=pos.y,z=pos.z+component[2]-1,d=1}
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Bottom
|
||||
if component[3][4]
|
||||
then
|
||||
for j,n in pairs(component[3][4])
|
||||
do
|
||||
if n
|
||||
then
|
||||
cp.ports[n] = {x=pos.x+j-1,y=pos.y,z=pos.z-1,d=1}
|
||||
end
|
||||
end
|
||||
end
|
||||
table.insert(components,cp)
|
||||
-- We do everything in the 0 0 block, we ignore the others
|
||||
end
|
||||
-- else we just ignore
|
||||
end
|
||||
end
|
||||
logikraft.collapseConnections(connections)
|
||||
print("Discovered:")
|
||||
print(dump(components))
|
||||
print(dump(connections))
|
||||
end
|
||||
|
||||
22
items.lua
22
items.lua
@ -3,11 +3,11 @@ minetest.register_craftitem("logikraft:rotator", {
|
||||
inventory_image = "rotator.png",
|
||||
on_place = function(itemstack, user, pointed_thing)
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
if not string.startsWith(node.name,"logikraft:cable_")
|
||||
if not logikraft.cablesblocks[node.name]
|
||||
then return itemstack
|
||||
end
|
||||
|
||||
local new = logikraft.rotateCable(string.without(node.name,"logikraft:cable_"),1)
|
||||
local new = logikraft.rotateCable(logikraft.cablesblocks[node.name],1)
|
||||
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||
return itemstack
|
||||
end
|
||||
@ -25,14 +25,14 @@ minetest.register_craftitem("logikraft:linker", {
|
||||
local selectedy = meta:get_int("logikraft:selected_y")
|
||||
local selectedz = meta:get_int("logikraft:selected_z")
|
||||
local selectedpos = {x = selectedx, y = selectedy, z = selectedz}
|
||||
if string.startsWith(node.name,"logikraft:cable_")
|
||||
if logikraft.cablesblocks[node.name]
|
||||
then
|
||||
if selectedx == pointed_thing.under.x and selectedy == pointed_thing.under.y and selectedz == pointed_thing.under.z
|
||||
then
|
||||
local thisnb = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||
if(selectednb ~= thisnb)
|
||||
then
|
||||
local new = logikraft.connectInCable(string.without(node.name,"logikraft:cable_"),selectednb,thisnb)
|
||||
local new = logikraft.connectInCable(logikraft.cablesblocks[node.name],selectednb,thisnb)
|
||||
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||
end
|
||||
meta:set_int("logikraft:selected_nb",0)
|
||||
@ -40,8 +40,8 @@ minetest.register_craftitem("logikraft:linker", {
|
||||
else
|
||||
local thisnb = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||
local new = logikraft.connectContiguousCable(
|
||||
string.without(minetest.get_node(selectedpos).name,"logikraft:cable_"),selectedpos,selectednb,
|
||||
string.without(node.name,"logikraft:cable_"),pointed_thing.under,thisnb)
|
||||
logikraft.cablesblocks[minetest.get_node(selectedpos).name],selectedpos,selectednb,
|
||||
logikraft.cablesblocks[node.name],pointed_thing.under,thisnb)
|
||||
if new
|
||||
then
|
||||
minetest.swap_node(selectedpos, {name = "logikraft:cable_" .. new[1]})
|
||||
@ -63,7 +63,7 @@ minetest.register_craftitem("logikraft:linker", {
|
||||
meta:set_string("inventory_image","linker.png")
|
||||
end
|
||||
else
|
||||
if string.startsWith(node.name,"logikraft:cable_")
|
||||
if logikraft.cablesblocks[node.name]
|
||||
then
|
||||
-- If we are looking at a cable we activate and store data
|
||||
meta:set_int("logikraft:selected_nb",logikraft.lookingAtNbIndex(user,pointed_thing.under))
|
||||
@ -97,13 +97,13 @@ minetest.register_craftitem("logikraft:unlinker", {
|
||||
inventory_image = "unlinker.png",
|
||||
on_place = function(itemstack, user, pointed_thing)
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
if not string.startsWith(node.name,"logikraft:cable_")
|
||||
if not logikraft.cablesblocks[node.name]
|
||||
then return itemstack
|
||||
end
|
||||
|
||||
local index = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||
|
||||
local new = logikraft.disconnectInCable(string.without(node.name,"logikraft:cable_"),index)
|
||||
local new = logikraft.disconnectInCable(logikraft.cablesblocks[node.name],index)
|
||||
if new
|
||||
then minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||
else minetest.remove_node(pointed_thing.under)
|
||||
@ -117,13 +117,13 @@ minetest.register_craftitem("logikraft:resizer", {
|
||||
inventory_image = "resizer.png",
|
||||
on_place = function(itemstack, user, pointed_thing)
|
||||
local node = minetest.get_node(pointed_thing.under)
|
||||
if not string.startsWith(node.name,"logikraft:cable_")
|
||||
if not logikraft.cablesblocks[node.name]
|
||||
then return itemstack
|
||||
end
|
||||
|
||||
local index = logikraft.lookingAtNbIndex(user,pointed_thing.under)
|
||||
|
||||
local new = logikraft.resizeInCable(string.without(node.name,"logikraft:cable_"),index)
|
||||
local new = logikraft.resizeInCable(logikraft.cablesblocks[node.name],index)
|
||||
minetest.swap_node(pointed_thing.under, {name = "logikraft:cable_" .. new})
|
||||
|
||||
return itemstack
|
||||
|
||||
33
utils.lua
33
utils.lua
@ -37,7 +37,7 @@ function string.without(str,ex)
|
||||
return string.sub(str,string.len(ex)+1,-1)
|
||||
end
|
||||
function table.equals1(arr1,arr2)
|
||||
for i,v in ipairs(arr1)
|
||||
for i,v in pairs(arr1)
|
||||
do
|
||||
if arr2[i] ~= v
|
||||
then return false
|
||||
@ -119,3 +119,34 @@ end
|
||||
function logikraft.addToPos4dir(dir,pos,w,h)
|
||||
return logikraft.addToPos8dir(dir*2+1,pos,w,h)
|
||||
end
|
||||
|
||||
function table.findMatch(ta,tb)
|
||||
do
|
||||
for i,x in ipairs(ta)
|
||||
do
|
||||
for j,y in ipairs(tb)
|
||||
do
|
||||
if x == y
|
||||
then return {i,j}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function table.findMatch1(ta,tb)
|
||||
do
|
||||
for i,x in ipairs(ta)
|
||||
do
|
||||
for j,y in ipairs(tb)
|
||||
do
|
||||
if table.equals1(x,y)
|
||||
then return {i,j}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user