Advertisement
9551

Untitled

Nov 19th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.89 KB | None | 0 0
  1. local b = require("ButtonH").terminal
  2. local sclick = {"tout",1,-1,-1}
  3. local dclick = {"tout",1,-1,-1}
  4. local w,h = term.getSize()
  5. local startCols = {}
  6. local tcur = term.current()
  7. local fullWin = window.create(tcur,1,1,w,h)
  8. local drawWin = window.create(fullWin,1,1,w-8,h)
  9. local guiWin = window.create(fullWin,w-8,1,w,h)
  10. for k,v in pairs(colors) do
  11.     if type(v) == "number" then
  12.         startCols[v] = {term.getPaletteColor(v)}
  13.     end
  14. end
  15. local function deepCopyTbl(tbl)
  16.     local token = "ev" .. tostring({})
  17.     os.queueEvent(token, tbl)
  18.     return select(2, os.pullEvent(token))
  19. end
  20. local createSelfIndexArray = function()
  21.     return setmetatable({},
  22.         {
  23.             __index=function(t,k)
  24.                 local new = {}
  25.                 t[k]=new
  26.                 return new
  27.             end
  28.         }
  29.     )
  30. end
  31. local keybinds = {
  32.     delete = keys.x,
  33.     cable = keys.p
  34. }
  35. local colors = deepCopyTbl(colors)
  36. colors.redOn = colors.red
  37. colors.redOff = colors.pink
  38. local cfilter = {
  39.     [2]=true,
  40.     [3]=true,
  41.     [4]=true,
  42.     [5]=true
  43. }
  44. local componentTex = {
  45.     ["redstone"] = {s=" ",b=colors.red,t=colors.black,superPowering=false},
  46.     ["sourceBlock"] = {s="#",b=colors.purple,t=colors.black},
  47.     ["background"] = {t=colors.gray,s="\127",b=colors.black}
  48. }
  49. local componentUpdates = {
  50.     ["redstone"] = {["true"]=colors.redOn,["false"]=colors.redOff},
  51.     ["sourceBlock"] = {["true"]=colors.purple,["false"]=colors.blue},
  52. }
  53. local tableSub = function(input,suber)
  54.     return {input[1]-suber[1],input[2]-suber[2]}
  55. end
  56. local sidecheck = {
  57.     {-1,0},
  58.     {0,1},
  59.     {1,0},
  60.     {0,-1},
  61. }
  62. local component = "redstone"
  63. local proccesNetwork
  64. proccesNetwork = function(net,x,y,blocked,original,selfDestruct)
  65.     local sides = {}
  66.     if selfDestruct then net[x][y].state = original.state end
  67.     for k2,v2 in pairs(sidecheck) do
  68.         local side = tableSub({x,y},v2)
  69.         if not blocked[x][y] then table.insert(sides,side) end
  70.     end
  71.     for k,v in pairs(sides) do
  72.         if net[v[1]] and net[v[1]][v[2]] then
  73.             net[v[1]][v[2]].state = original.state
  74.             blocked[x][y] = true
  75.             proccesNetwork(net,v[1],v[2],blocked,original)
  76.         end
  77.     end
  78. end
  79. local getSides = function(pixNet,check,x,y)
  80.     local sides = {}
  81.     for i=1,#check do
  82.         local sCur = tableSub({x,y},check[i])
  83.         if pixNet[sCur[1]] and pixNet[sCur[1]][sCur[2]] then sides[i] = sCur end
  84.     end
  85.     return sides
  86. end
  87. local andgate = {
  88.     tex = {
  89.         [-1]={[0]={"a",colors.gray}},
  90.         [0]={[0]={"n",colors.gray}},
  91.         [1]={[0]={"d",colors.gray}}
  92.     },
  93.     con={
  94.         inputs = {
  95.             [-1]={[-1]={"in1",colors.red}},
  96.             [1]={[-1]={"in2",colors.red}},
  97.         },
  98.         outputs = {
  99.             [0]={[1]={"out1",colors.green}}
  100.         }
  101.     }
  102. }
  103. local andCode = function(inputs,outputCont)
  104.     outputCont.out1.state = false
  105. end
  106. local proccesGate = function(sources,connections,gate,xy,yy,drawMap)
  107.     local powerData = {}
  108.     local outList = createSelfIndexArray()
  109.     for x,v1 in pairs(gate.display.con.inputs) do
  110.         for y,v2 in pairs(v1) do
  111.             if next(getSides(connections,sidecheck,x+xy,y+yy)) then
  112.                 powerData[v2[1]] = true
  113.             else
  114.                 powerData[v2[1]] = false
  115.             end
  116.             if not drawMap[x+xy][y+yy] then drawMap[x+xy][y+yy] = {} end
  117.             drawMap[x+xy][y+yy].bc = v2[2]
  118.             drawMap[x+xy][y+yy].tc = colors.black
  119.             drawMap[x+xy][y+yy].sy = " "
  120.         end
  121.     end
  122.     for x,v1 in pairs(gate.display.con.outputs) do
  123.         _G.eb = v1
  124.         for y,v2 in pairs(v1) do
  125.             if not sources[x+xy] then sources[x+xy] = {} end
  126.             sources[x+xy][y+yy] = {
  127.                 state=true,
  128.                 type="sourceBlock",
  129.                 comptype="source"
  130.             }
  131.             outList[v2[1]] = sources[x+xy][y+yy]
  132.             if not drawMap[x+xy][y+yy] then drawMap[x+xy][y+yy] = {} end
  133.             drawMap[x+xy][y+yy].bc = v2[2]
  134.             drawMap[x+xy][y+yy].tc = colors.black
  135.             drawMap[x+xy][y+yy].sy = (sources[x+xy][y+yy].state and "1") or "0"
  136.         end
  137.     end
  138.     --for k,v in pairs(gate.display.tex)
  139.     gate.code(powerData,outList)
  140. end
  141. local block = {0,0,0,0}
  142. local network = {}
  143. local sourceNetwork = {}
  144. local eventQueue = {}
  145. local drawMap = createSelfIndexArray()
  146. local gates = {
  147.     [5]={
  148.         [5]={
  149.             display=andgate,
  150.             code=andCode
  151.         }
  152.     }
  153. }
  154. local main = function()
  155.     local menuClick = function()
  156.         while true do
  157.             sclick = b.timetouch(1,cfilter,false)
  158.         end
  159.     end
  160.     local drawClick = function()
  161.         local term = drawWin
  162.         while true do
  163.             dclick = b.timetouch(.5,cfilter,true)
  164.         end
  165.     end
  166.     local keyDown = function()
  167.         while true do
  168.             local _,keyDownEv = os.pullEvent("key")
  169.             if not eventQueue[keyDownEv] then
  170.                 eventQueue[keyDownEv] = true
  171.             end
  172.         end
  173.     end
  174.     local keyUp = function()
  175.         while true do
  176.             local _,keyUpEv = os.pullEvent("key_up")
  177.             eventQueue[keyUpEv] = false
  178.         end
  179.     end
  180.     local drawWin = function()
  181.         local term = drawWin
  182.         local w,h = drawWin.getSize()
  183.         while true do
  184.             drawWin.setVisible(false)
  185.             term.setBackgroundColor(colors.black)
  186.             term.setTextColor(colors.gray)
  187.             for i=1,h do drawWin.setCursorPos(1,i) drawWin.write(("\127"):rep(w)) end
  188.             for x,v1 in pairs(gates) do
  189.                 for y,dat in pairs(v1) do
  190.                     proccesGate(sourceNetwork,network,dat,x,y,drawMap)
  191.                 end
  192.             end
  193.             for x=1,w do
  194.                 for y=1,h do
  195.                     if b.API(dclick,x,y-1,1,1) then
  196.                         if not b.API(dclick,block[1],block[2],block[3],block[4]) then
  197.                             if not network[x] then network[x] = {} end
  198.                             if not sourceNetwork[x] then sourceNetwork[x] = {} end
  199.                             if b.switch("db",1) or eventQueue[keybinds.delete] then
  200.                                 network[x][y] = nil
  201.                                 for i,side in pairs(getSides(network,sidecheck,x,y)) do
  202.                                     proccesNetwork(network,side[1],side[2],createSelfIndexArray(),{state=false},true)
  203.                                 end
  204.                                 if (sourceNetwork[x][y] or {}).comptype == "source" then
  205.                                     sourceNetwork[x][y] = nil
  206.                                 end
  207.                                 dclick = {"tout",1,-1,-1}
  208.                             else
  209.                                 if not network[x][y] then
  210.                                     if not eventQueue[keybinds.cable] then
  211.                                         network[x][y] = {
  212.                                             state=false,
  213.                                             type=component,
  214.                                             comptype="connection"
  215.                                         }
  216.                                     else
  217.                                         sourceNetwork[x][y] = {
  218.                                             state=true,
  219.                                             type="sourceBlock",
  220.                                             comptype="source"
  221.                                         }
  222.                                     end
  223.                                 end
  224.                                 dclick = {"tout",1,-1,-1}
  225.                             end
  226.                         end
  227.                     end
  228.                 end
  229.             end
  230.             _G.sources = sourceNetwork
  231.             for k,v in pairs(network) do
  232.                 for k2,v2 in pairs(v) do
  233.                     local pixelData
  234.                     pixelData = componentTex[v2.type]
  235.                     term.setCursorPos(k,k2-1)
  236.                     term.setBackgroundColor(componentUpdates[v2.type][tostring(v2.state)] or pixelData.b)
  237.                     term.setTextColor(pixelData.t)
  238.                     term.write(pixelData.s)
  239.                 end
  240.             end
  241.             for k,v in pairs(sourceNetwork) do
  242.                 for k2,v2 in pairs(v) do
  243.                     local pixelData = componentTex[v2.type]
  244.                     term.setCursorPos(k,k2-1)
  245.                     term.setBackgroundColor(componentUpdates[v2.type][tostring(v2.state)] or pixelData.b)
  246.                     term.setTextColor(pixelData.t)
  247.                     term.write(pixelData.s)
  248.                 end
  249.             end
  250.             for k,v in pairs(drawMap) do
  251.                 for k2,v2 in pairs(v) do
  252.                     term.setCursorPos(k,k2-1)
  253.                     term.setBackgroundColor(v2.bc)
  254.                     term.setTextColor(v2.tc)
  255.                     term.write(v2.sy)
  256.                 end
  257.             end
  258.             drawWin.setVisible(true)
  259.             os.queueEvent("fakeEvDraw")
  260.             os.pullEvent("fakeEvDraw")
  261.         end
  262.     end
  263.     local computerMain = function()
  264.         while true do
  265.             for x,v in pairs(sourceNetwork) do
  266.                 for y,v1 in pairs(v) do
  267.                     proccesNetwork(network,x,y,createSelfIndexArray(),deepCopyTbl({sourceNetwork[x][y]} or {state=false}))
  268.                 end
  269.             end
  270.             os.queueEvent("fakeEvComputer")
  271.             os.pullEvent("fakeEvComputer")
  272.         end
  273.     end
  274.     local graphicsInterface = function()
  275.     end
  276.     parallel.waitForAll(menuClick,drawClick,drawWin,computerMain,graphicsInterface,keyUp,keyDown)
  277. end
  278.  
  279. local ok,err = pcall(main())
  280. for k,v in pairs(startCols) do
  281.     local r,g,b = table.unpack(v)
  282.     term.setPaletteColor(k,r,g,b)
  283. end
  284. if not ok then
  285.     term.setBackgroundColor(colors.red)
  286.     print(err)
  287.     term.setBackgroundColor(colors.black)
  288. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement