Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local b = require("ButtonH").terminal
- local sclick = {"tout",1,-1,-1}
- local dclick = {"tout",1,-1,-1}
- local w,h = term.getSize()
- local startCols = {}
- local tcur = term.current()
- local fullWin = window.create(tcur,1,1,w,h)
- local drawWin = window.create(fullWin,1,1,w-8,h)
- local guiWin = window.create(fullWin,w-8,1,w,h)
- for k,v in pairs(colors) do
- if type(v) == "number" then
- startCols[v] = {term.getPaletteColor(v)}
- end
- end
- local function deepCopyTbl(tbl)
- local token = "ev" .. tostring({})
- os.queueEvent(token, tbl)
- return select(2, os.pullEvent(token))
- end
- local createSelfIndexArray = function()
- return setmetatable({},
- {
- __index=function(t,k)
- local new = {}
- t[k]=new
- return new
- end
- }
- )
- end
- local keybinds = {
- delete = keys.x,
- cable = keys.p
- }
- local colors = deepCopyTbl(colors)
- colors.redOn = colors.red
- colors.redOff = colors.pink
- local cfilter = {
- [2]=true,
- [3]=true,
- [4]=true,
- [5]=true
- }
- local componentTex = {
- ["redstone"] = {s=" ",b=colors.red,t=colors.black,superPowering=false},
- ["sourceBlock"] = {s="#",b=colors.purple,t=colors.black},
- ["background"] = {t=colors.gray,s="\127",b=colors.black}
- }
- local componentUpdates = {
- ["redstone"] = {["true"]=colors.redOn,["false"]=colors.redOff},
- ["sourceBlock"] = {["true"]=colors.purple,["false"]=colors.blue},
- }
- local tableSub = function(input,suber)
- return {input[1]-suber[1],input[2]-suber[2]}
- end
- local sidecheck = {
- {-1,0},
- {0,1},
- {1,0},
- {0,-1},
- }
- local component = "redstone"
- local proccesNetwork
- proccesNetwork = function(net,x,y,blocked,original,selfDestruct)
- local sides = {}
- if selfDestruct then net[x][y].state = original.state end
- for k2,v2 in pairs(sidecheck) do
- local side = tableSub({x,y},v2)
- if not blocked[x][y] then table.insert(sides,side) end
- end
- for k,v in pairs(sides) do
- if net[v[1]] and net[v[1]][v[2]] then
- net[v[1]][v[2]].state = original.state
- blocked[x][y] = true
- proccesNetwork(net,v[1],v[2],blocked,original)
- end
- end
- end
- local getSides = function(pixNet,check,x,y)
- local sides = {}
- for i=1,#check do
- local sCur = tableSub({x,y},check[i])
- if pixNet[sCur[1]] and pixNet[sCur[1]][sCur[2]] then sides[i] = sCur end
- end
- return sides
- end
- local andgate = {
- tex = {
- [-1]={[0]={"a",colors.gray}},
- [0]={[0]={"n",colors.gray}},
- [1]={[0]={"d",colors.gray}}
- },
- con={
- inputs = {
- [-1]={[-1]={"in1",colors.red}},
- [1]={[-1]={"in2",colors.red}},
- },
- outputs = {
- [0]={[1]={"out1",colors.green}}
- }
- }
- }
- local andCode = function(inputs,outputCont)
- outputCont.out1.state = false
- end
- local proccesGate = function(sources,connections,gate,xy,yy,drawMap)
- local powerData = {}
- local outList = createSelfIndexArray()
- for x,v1 in pairs(gate.display.con.inputs) do
- for y,v2 in pairs(v1) do
- if next(getSides(connections,sidecheck,x+xy,y+yy)) then
- powerData[v2[1]] = true
- else
- powerData[v2[1]] = false
- end
- if not drawMap[x+xy][y+yy] then drawMap[x+xy][y+yy] = {} end
- drawMap[x+xy][y+yy].bc = v2[2]
- drawMap[x+xy][y+yy].tc = colors.black
- drawMap[x+xy][y+yy].sy = " "
- end
- end
- for x,v1 in pairs(gate.display.con.outputs) do
- _G.eb = v1
- for y,v2 in pairs(v1) do
- if not sources[x+xy] then sources[x+xy] = {} end
- sources[x+xy][y+yy] = {
- state=true,
- type="sourceBlock",
- comptype="source"
- }
- outList[v2[1]] = sources[x+xy][y+yy]
- if not drawMap[x+xy][y+yy] then drawMap[x+xy][y+yy] = {} end
- drawMap[x+xy][y+yy].bc = v2[2]
- drawMap[x+xy][y+yy].tc = colors.black
- drawMap[x+xy][y+yy].sy = (sources[x+xy][y+yy].state and "1") or "0"
- end
- end
- --for k,v in pairs(gate.display.tex)
- gate.code(powerData,outList)
- end
- local block = {0,0,0,0}
- local network = {}
- local sourceNetwork = {}
- local eventQueue = {}
- local drawMap = createSelfIndexArray()
- local gates = {
- [5]={
- [5]={
- display=andgate,
- code=andCode
- }
- }
- }
- local main = function()
- local menuClick = function()
- while true do
- sclick = b.timetouch(1,cfilter,false)
- end
- end
- local drawClick = function()
- local term = drawWin
- while true do
- dclick = b.timetouch(.5,cfilter,true)
- end
- end
- local keyDown = function()
- while true do
- local _,keyDownEv = os.pullEvent("key")
- if not eventQueue[keyDownEv] then
- eventQueue[keyDownEv] = true
- end
- end
- end
- local keyUp = function()
- while true do
- local _,keyUpEv = os.pullEvent("key_up")
- eventQueue[keyUpEv] = false
- end
- end
- local drawWin = function()
- local term = drawWin
- local w,h = drawWin.getSize()
- while true do
- drawWin.setVisible(false)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.gray)
- for i=1,h do drawWin.setCursorPos(1,i) drawWin.write(("\127"):rep(w)) end
- for x,v1 in pairs(gates) do
- for y,dat in pairs(v1) do
- proccesGate(sourceNetwork,network,dat,x,y,drawMap)
- end
- end
- for x=1,w do
- for y=1,h do
- if b.API(dclick,x,y-1,1,1) then
- if not b.API(dclick,block[1],block[2],block[3],block[4]) then
- if not network[x] then network[x] = {} end
- if not sourceNetwork[x] then sourceNetwork[x] = {} end
- if b.switch("db",1) or eventQueue[keybinds.delete] then
- network[x][y] = nil
- for i,side in pairs(getSides(network,sidecheck,x,y)) do
- proccesNetwork(network,side[1],side[2],createSelfIndexArray(),{state=false},true)
- end
- if (sourceNetwork[x][y] or {}).comptype == "source" then
- sourceNetwork[x][y] = nil
- end
- dclick = {"tout",1,-1,-1}
- else
- if not network[x][y] then
- if not eventQueue[keybinds.cable] then
- network[x][y] = {
- state=false,
- type=component,
- comptype="connection"
- }
- else
- sourceNetwork[x][y] = {
- state=true,
- type="sourceBlock",
- comptype="source"
- }
- end
- end
- dclick = {"tout",1,-1,-1}
- end
- end
- end
- end
- end
- _G.sources = sourceNetwork
- for k,v in pairs(network) do
- for k2,v2 in pairs(v) do
- local pixelData
- pixelData = componentTex[v2.type]
- term.setCursorPos(k,k2-1)
- term.setBackgroundColor(componentUpdates[v2.type][tostring(v2.state)] or pixelData.b)
- term.setTextColor(pixelData.t)
- term.write(pixelData.s)
- end
- end
- for k,v in pairs(sourceNetwork) do
- for k2,v2 in pairs(v) do
- local pixelData = componentTex[v2.type]
- term.setCursorPos(k,k2-1)
- term.setBackgroundColor(componentUpdates[v2.type][tostring(v2.state)] or pixelData.b)
- term.setTextColor(pixelData.t)
- term.write(pixelData.s)
- end
- end
- for k,v in pairs(drawMap) do
- for k2,v2 in pairs(v) do
- term.setCursorPos(k,k2-1)
- term.setBackgroundColor(v2.bc)
- term.setTextColor(v2.tc)
- term.write(v2.sy)
- end
- end
- drawWin.setVisible(true)
- os.queueEvent("fakeEvDraw")
- os.pullEvent("fakeEvDraw")
- end
- end
- local computerMain = function()
- while true do
- for x,v in pairs(sourceNetwork) do
- for y,v1 in pairs(v) do
- proccesNetwork(network,x,y,createSelfIndexArray(),deepCopyTbl({sourceNetwork[x][y]} or {state=false}))
- end
- end
- os.queueEvent("fakeEvComputer")
- os.pullEvent("fakeEvComputer")
- end
- end
- local graphicsInterface = function()
- end
- parallel.waitForAll(menuClick,drawClick,drawWin,computerMain,graphicsInterface,keyUp,keyDown)
- end
- local ok,err = pcall(main())
- for k,v in pairs(startCols) do
- local r,g,b = table.unpack(v)
- term.setPaletteColor(k,r,g,b)
- end
- if not ok then
- term.setBackgroundColor(colors.red)
- print(err)
- term.setBackgroundColor(colors.black)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement