Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local debug = true
- local oTemp = fs.open("console", "w")
- local function log(string)
- if debug then
- oTemp.writeLine(string)
- oTemp.flush()
- end
- end
- local currentCol
- local currentPos = {}
- local movementTab = {
- {0, -1, "-"},
- {1, 0, "|"},
- {0, 1, "-"},
- {-1, 0, "|"},
- ["0-2"] = 1,
- ["20"] = 2,
- ["02"] = 3,
- ["-20"] = 4
- }
- local flowTab, choiceTab = {}, {}
- local function resetFlowTab()
- for i = 1, 9 do
- flowTab[i] = {}
- for n = 1, 9 do
- flowTab[i][n] = 0
- end
- end
- end
- local flows = {} --x, y, dir(1/2/3/4)
- local flowBases = {} --x, y (base), --x, y (goal)
- local flowStates = {} --true/false (whether or not the flow is completed)
- local function resetFlowStuffs()
- for i = 1, 14 do
- flows[2^i] = {}
- flowBases[2^i] = {}
- flowStates[2^i] = false
- end
- end
- local function getNumWalls(xPos, yPos, col, usage)
- col = col or 0
- local num = 0
- local numTab = {}
- if yPos > 1 and (flowTab[xPos][yPos - 1] == 0 or flowTab[xPos][yPos - 1] == col) then
- numTab[#numTab + 1] = 1
- end
- if xPos < 9 and (flowTab[xPos + 1][yPos] == 0 or flowTab[xPos + 1][yPos] == col) then
- numTab[#numTab + 1] = 2
- end
- if yPos < 9 and (flowTab[xPos][yPos + 1] == 0 or flowTab[xPos][yPos + 1] == col) then
- numTab[#numTab + 1] = 3
- end
- if xPos > 1 and (flowTab[xPos - 1][yPos] == 0 or flowTab[xPos - 1][yPos] == col) then
- numTab[#numTab + 1] = 4
- end
- if usage then
- return numTab
- else
- return #numTab
- end
- end
- local function flowGen()
- resetFlowStuffs() --make a random generator
- resetFlowTab()
- for i = 1, 14 do
- log("generating base " .. tostring(i))
- --[[local choice
- repeat --== choose base 1
- local timeOut = 0
- repeat
- choice = {math.random(1, 9), math.random(1, 9)}
- timeOut = timeOut + 1
- if timeOut == 500 then error("") end
- until flowTab[choice[1] ][choice[2] ] == 0
- local numWalls = getNumWalls(choice[1], choice[2])
- until numWalls < 4 and numWalls > 0]]--
- local genInfo = {}
- local maxPos = 0
- log("entering loops")
- for n = 1, 9 do
- for j = 1, 9 do
- if flowTab[n][j] == 0 then
- genInfo[#genInfo + 1] = {getNumWalls(n, j), math.max(math.abs(5 - n), math.abs(5 - j)), n, j}
- if math.max(math.abs(5 - n), math.abs(5 - j)) > maxPos then maxPos = math.max(math.abs(5 - n), math.abs(5 - j)) end
- end
- end
- end
- log("loop 1 done")
- log(maxPos)
- for n = 1, #genInfo do
- if genInfo[n][2] < maxPos then
- genInfo[n] = nil
- end
- end
- local minSpaces = 4
- log("loop 2 done")
- for k, e in pairs(genInfo) do
- if e[1] < minSpaces and e[1] > 0 then minSpaces = e[1] end
- end
- log("loop 3 done")
- log(minSpaces)
- local count = 1
- local genOutput = {}
- for _, v in pairs(genInfo) do
- genOutput[count] = v
- count = count + 1
- end
- genInfo = genOutput
- for n = 1, #genInfo do
- if genInfo[n][1] > minSpaces or genInfo[n][1] == 0 then
- genInfo[n] = nil
- end
- end
- log("loop 4 done")
- for _, e in pairs(genInfo) do
- log(tostring(e[1]) .. " " .. tostring(e[2]) .. " " .. tostring(e[3]) .. " " .. tostring(e[4]))
- end
- count = 1
- genOutput = {}
- for _, v in pairs(genInfo) do
- genOutput[count] = v
- count = count + 1
- end
- genInfo = genOutput
- log("loop 5 done")
- local choice = math.random(1, #genInfo)
- choice = {genInfo[choice][3], genInfo[choice][4]}
- log(tostring(choice[1]) .. " " .. tostring(choice[2]))
- flowBases[2^i][1] = {choice[1]*2, choice[2]*2}
- flowTab[choice[1]][choice[2]] = 2^i
- local numTab = getNumWalls(choice[1], choice[2], _, true)
- local len = math.random(4, 8)
- while #numTab > 0 and len > 0 do
- local cTab, cDir, cMin = {}, {}, 5
- for _, e in pairs(numTab) do
- cTab[e] = getNumWalls(choice[1] + movementTab[e][1], choice[2] + movementTab[e][2], 2^i)
- end
- for _, e in pairs(cTab) do
- if e < cMin then cMin = e end
- end
- for n = 1, 4 do
- if cTab[n] == cMin then
- cDir[#cDir + 1] = n
- end
- end
- cDir = cDir[math.random(1, #cDir)]
- choice[1] = choice[1] + movementTab[cDir][1]
- choice[2] = choice[2] + movementTab[cDir][2]
- flowTab[choice[1]][choice[2]] = 2^i
- numTab = getNumWalls(choice[1], choice[2], _, true)
- --if math.random(1, 5) == 1 and not first then break end
- len = len - 1
- end
- log("loop 6 done")
- log("base generated")
- flowBases[2^i][2] = {choice[1]*2, choice[2]*2}
- end
- --resetFlowTab()
- end
- local function printFlows() --probably want to make the printing separate from the updating
- paintutils.drawBox(1, 1, 19, 19, colors.gray)
- for y = 2, 18 do
- for x = 2, 18 do
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(x, y)
- if x%2 == 1 and y%2 == 1 then
- write("+")
- elseif x%2 == 1 then
- write("|")
- elseif y%2 == 1 then
- write("-")
- else
- write(" ")
- end
- end
- end
- for i = 14, 1, -1 do
- if #flows[2^i] > 0 then
- for _, e in pairs(flows[2^i]) do
- term.setCursorPos(e[1], e[2])
- term.setBackgroundColor(2^i)
- term.setTextColor(colors.white)
- write(" ")
- if #e > 2 then
- term.setCursorPos(e[1] + movementTab[e[3]][1], e[2] + movementTab[e[3]][2])
- write(movementTab[e[3]][3])
- end
- end
- end
- if #flowBases[2^i] > 0 then
- for _, e in pairs(flowBases[2^i]) do
- term.setCursorPos(e[1], e[2])
- term.setBackgroundColor(2^i)
- if flowStates[2^i] then
- term.setTextColor(colors.white)
- else
- term.setTextColor(colors.black)
- end
- write("O")
- end
- end
- end
- end
- local function updateFlows()
- for i = 1, 14 do
- if #flowBases[2^i] > 0 then
- for _, e in pairs(flowBases[2^i]) do
- flowTab[e[1]/2][e[2]/2] = tostring(2^i)
- end
- end
- if #flows[2^i] > 0 then
- for _, e in pairs(flows[2^i]) do
- flowTab[e[1]/2][e[2]/2] = 2^i
- end
- if #flows[2^i] > 1 and (flows[2^i][#flows[2^i]][1] == flowBases[2^i][1][1] and flows[2^i][#flows[2^i]][2] == flowBases[2^i][1][2]) or (flows[2^i][#flows[2^i]][1] == flowBases[2^i][2][1] and flows[2^i][#flows[2^i]][2] == flowBases[2^i][2][2]) then
- flowStates[2^i] = true
- else
- flowStates[2^i] = false
- end
- end
- end
- end
- local function checkState()
- local flag = true
- for _, v in pairs(flowStates) do
- if not v then flag = false break end
- end
- return flag
- end
- local function eventHandler(events)
- if events[1] == "mouse_click" and events[2] == 1 then
- ---=== initializes which flow is active based on where the click is before the drag ===---
- if events[3]%2 == 0 and events[4]%2 == 0 and tonumber(flowTab[events[3]/2][events[4]/2]) > 0 then
- currentCol = tonumber(flowTab[events[3]/2][events[4]/2])
- currentPos = {events[3], events[4]}
- else
- currentCol = nil
- currentPos = {}
- end
- end
- if events[1] == "mouse_drag" and events[2] == 1 and events[3]%2 == 0 and events[4]%2 == 0 and currentCol then
- if (math.abs(events[3] - currentPos[1]) == 2 and math.abs(events[4] - currentPos[2]) == 0) or (math.abs(events[3] - currentPos[1]) == 0 and math.abs(events[4] - currentPos[2]) == 2) then
- local cut = false
- for i = 1, #flows[currentCol] do
- ---=== cuts off excess flow when you grab it from a point that isnt the very tip ===---
- if cut then
- flowTab[flows[currentCol][i][1]/2][flows[currentCol][i][2]/2] = 0
- flows[currentCol][i] = nil
- elseif currentPos[1] == flows[currentCol][i][1] and currentPos[2] == flows[currentCol][i][2] then
- cut = true
- end
- end
- if ((currentPos[1] == flowBases[currentCol][1][1] and currentPos[2] == flowBases[currentCol][1][2]) or (currentPos[1] == flowBases[currentCol][2][1] and currentPos[2] == flowBases[currentCol][2][2])) then
- ---=== if a base is clicked ===---
- flowStates[currentCol] = false
- flows[currentCol] = {}
- for i = 1, 9 do
- for n = 1, 9 do
- if flowTab[i][n] == currentCol then
- flowTab[i][n] = 0
- end
- end
- end
- if (flowTab[events[3]/2][events[4]/2] == 0 or (type(flowTab[events[3]/2][events[4]/2]) == "string" and tonumber(flowTab[events[3]/2][events[4]/2]) == currentCol)) then
- ---=== resets flowTab values whenever you reset a flow by clicking on one of the bases ===---
- flows[currentCol] = {{currentPos[1], currentPos[2], movementTab[tostring(events[3] - currentPos[1]) .. tostring(events[4] - currentPos[2])]}, {events[3], events[4]}}
- end
- currentPos = {events[3], events[4]}
- elseif #flows[currentCol] > 1 and events[3] == flows[currentCol][#flows[currentCol] - 1][1] and events[4] == flows[currentCol][#flows[currentCol] - 1][2] then
- ---=== allows you to backtrack along a flow ===---
- table.remove(flows[currentCol])
- flows[currentCol][#flows[currentCol]][3] = nil
- flowTab[currentPos[1]/2][currentPos[2]/2] = 0
- currentPos = {events[3], events[4]}
- if #flows[currentCol] == 1 then flows[currentCol] = {} end
- elseif (flowTab[events[3]/2][events[4]/2] == 0 or (type(flowTab[events[3]/2][events[4]/2]) == "string" and tonumber(flowTab[events[3]/2][events[4]/2]) == currentCol)) and #flows[currentCol] > 0 then
- ---=== allows you to drag a flow only to empty squares and its passive base ===---
- flows[currentCol][#flows[currentCol]][3] = movementTab[tostring(events[3] - currentPos[1]) .. tostring(events[4] - currentPos[2])]
- flows[currentCol][#flows[currentCol] + 1] = {events[3], events[4]}
- currentPos = {events[3], events[4]}
- end
- elseif not (events[3] == currentPos[1] and events[4] == currentPos[2]) then
- currentCol = nil
- currentPos = {} --possibly remove for increased versatility, but could cause instability if not used
- end
- end
- end
- --[[flowBases[2^1] = {{2, 2}, {2, 8}}
- flowBases[2^2] = {{18, 2}, {12, 8}}
- flowBases[2^3] = {{6, 4}, {12, 4}}
- flowBases[2^14] = {{6, 6}, {18, 4}}
- flowBases[2^5] = {{16, 6}, {18, 14}}
- flowBases[2^6] = {{14, 8}, {16, 12}}
- flowBases[2^7] = {{14, 10}, {12, 14}}
- flowBases[2^8] = {{14, 12}, {16, 14}}
- flowBases[2^9] = {{8, 10}, {2, 18}}
- flowBases[2^10] = {{10, 10}, {4, 12}}
- flowBases[2^11] = {{4, 18}, {8, 16}}
- flowBases[2^12] = {{6, 16}, {10, 18}}
- flowBases[2^13] = {{10, 14}, {18, 18}}
- flowBases[2^4] = {{12, 18}, {16, 18}}]]--
- repeat
- local ok = pcall(flowGen)
- for i = 1, 9 do
- for j = 1, 9 do
- if flowTab[i][j] == 0 then ok = false end
- --[[term.setCursorPos(i, j)
- if flowTab[i][j] == 0 then
- write("O")
- elseif flowTab[i][j] > 0 then
- write("X")
- else
- write(" ")
- end]]--
- end
- end
- until ok
- --os.pullEvent("key")
- local won
- while not won do
- printFlows()
- local events = {os.pullEvent()}
- eventHandler(events)
- updateFlows()
- won = checkState()
- end
- printFlows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement