Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Menu stuff
- ]]--
- function menu( title, ... )
- --edit the background color to make the GUI more unique
- term.setBackgroundColor( colors.black )
- local debug = false --#turn to true to see what it's thinking
- local tArgs = { ... }
- local pages = {[1]={}}
- for i = 1, #tArgs, 1 do
- if #pages[ #pages ] == 7 then
- pages[ #pages + 1 ] = {}
- end
- pages[ #pages ][ #pages[#pages] + 1 ] = tArgs[ i ]
- end
- local maxLen = 0
- for k, v in ipairs( tArgs ) do
- if #v > maxLen then maxLen = #v end
- end
- local maxx, maxy = term.getSize()
- if maxLen > maxx - 20 then
- error( 'largest string is too large', 2 )
- end
- local centerx = math.ceil( maxx/2 )
- local centery
- local yValue = {}
- local page = 1
- local selected = math.ceil( #pages[ page ] / 2 )
- local function render()
- local tbl = pages[ page ]
- centery = (maxy/2) - #tbl/2
- term.clear()
- term.setCursorPos( centerx - #title/2 + 1, 2 )
- term.write( title )
- for i = 1, #tbl do
- term.setCursorPos( centerx - (#tbl[i]/2), centery + i )
- yValue[ i ] = centery + i
- term.write( tbl[ i ] )
- end
- if pages[ page - 1 ] then
- term.setCursorPos( 3, centery + #tbl/2 + 1 )
- term.write( "previous" )
- end
- if pages[ page + 1 ] then
- term.setCursorPos( maxx - 5, centery + #tbl/2 + 1 )
- term.write( "next" )
- end
- local str = "(" .. page .. "/" .. #pages .. ")"
- term.setCursorPos( centerx - (#str/2), maxy )
- term.write( str )
- end
- while true do
- render()
- if debug then
- term.setCursorPos( 1, 1 )
- term.write( selected )
- term.setCursorPos( 1, 2 )
- term.write( page )
- end
- if term.isColor() then
- term.setTextColor( colors.yellow )
- end
- if selected == "previous" and not pages[ page - 1 ] then
- selected = math.ceil( #pages[ page ]/2 )
- elseif selected == "next" and not pages[ page + 1 ] then
- selected = math.ceil( #pages[ page ]/2 )
- end
- if type( selected ) == "number" then
- term.setCursorPos( centerx - (#pages[page][selected]/2) - 3, yValue[ selected ] )
- term.write( "[" )
- term.setCursorPos( centerx + (#pages[page][selected]/2) + 2, yValue[ selected ] )
- term.write( "]" )
- elseif selected == "previous" then
- term.setCursorPos( 1, centery + #pages[ page ]/2 + 1 )
- term.write( "[" )
- term.setCursorPos( 12, centery + #pages[ page ]/2 + 1 )
- term.write( "]" )
- elseif selected == "next" then
- term.setCursorPos( maxx - 7, centery + #pages[ page ]/2 + 1 )
- term.write( "[" )
- term.setCursorPos( maxx, centery + #pages[ page ]/2 + 1 )
- term.write( "]" )
- end
- if term.isColor() then
- term.setTextColor( colors.white )
- end
- local event, key = os.pullEvent( "key" )
- local old = selected
- if key == 200 and type( selected ) == "number" and pages[ page ][ selected - 1 ] then
- selected = selected - 1
- elseif key == 208 and type( selected ) == "number" and pages[ page ][ selected + 1 ] then
- selected = selected + 1
- elseif key == 28 then
- if type( selected ) == "number" then
- return pages[ page ][ selected ]
- elseif selected == "next" and pages[ page + 1 ] then
- page = page + 1
- elseif selected == "previous" and pages[ page - 1 ] then
- page = page - 1
- end
- elseif key == 203 then
- if selected == "next" then
- selected = math.ceil( #pages[ page ]/2 )
- elseif type( selected ) == "number" and pages[ page - 1 ] then
- selected = "previous"
- end
- elseif key == 205 then
- if selected == "previous" then
- selected = math.ceil( #pages[ page ]/2 )
- elseif type( selected ) == "number" and pages[ page + 1 ] then
- selected = "next"
- end
- end
- end
- end
- local function minimenu( ... )
- local tArgs = { ... }
- local tSelections = {}
- local _, y = term.getSize()
- local n = 1
- for i, str in ipairs( tArgs ) do
- tSelections[ i ] = { str = str }
- tSelections[ i ][ "x1" ] = n
- tSelections[ i ][ "x2" ] = n + #str + 1
- n = n + 2 + #str
- end
- for k, v in ipairs( tSelections ) do
- term.setCursorPos( v["x1"], y )
- term.write( ' ' .. v.str )
- end
- local slc = 1
- local last = 1
- while true do
- term.setCursorPos( tSelections[ last ]["x1"], y )
- term.write( ' ' )
- term.setCursorPos( tSelections[ last ]["x2"], y )
- term.write( ' ' )
- term.setCursorPos( tSelections[ slc ]["x1"], y )
- term.setTextColor( term.isColor() and colors.black or colors.white )
- term.write( "[" )
- term.setCursorPos( tSelections[ slc ]["x2"], y )
- term.write( "]" )
- while true do
- local event, key = os.pullEvent( "key" )
- if key == 203 and tSelections[ slc - 1 ] then
- last = slc
- slc = slc - 1
- break
- elseif key == 205 and tSelections[ slc + 1 ] then
- last = slc
- slc = slc + 1
- break
- elseif key == 28 then
- return tSelections[ slc ].str
- end
- end
- end
- end
- --[[
- drawing awesome stuff
- ]]--
- local characters = { --//TODO: FILL THIS TABLE
- [0] = "\128\128\128|000|000\n\128\128\128|000|000",
- -- single end
- [1] = "\151\143\148|0ff|f00\n\138\140\133|fff|000", -- point up
- [2] = "\151\140\139|0f0|f0f\n\138\140\135|fff|000", -- point right
- [3] = "\151\140\148|0ff|f00\n\138\131\133|f0f|0f0", -- point down
- [4] = "\135\140\148|0ff|f00\n\139\140\133|fff|000", -- point left
- --double curved
- [5] = "\151\143\139|0f0|f0f\n\138\140\135|fff|000", -- up and right
- [6] = "\151\140\139|0f0|f0f\n\138\131\135|f0f|0f0", -- right and down
- [7] = "\135\140\148|0ff|f00\n\139\131\133|f0f|0f0", -- down and left
- [8] = "\135\143\148|0ff|f00\n\139\140\133|fff|000", -- left and up
- --double straight
- [9] = "\128\128\128|f0f|0f0\n\128\128\128|f0f|0f0", -- vertical
- [10] = "\143\143\143|000|fff\n\131\131\131|fff|000", -- horizantal
- --triple
- [11] = "\135\143\139|0f0|f0f\n\139\140\135|fff|000", -- left, up and right
- [12] = "\151\143\139|0f0|f0f\n\138\131\135|f0f|0f0", -- up, right and down
- [13] = "\135\140\139|0f0|f0f\n\139\131\135|f0f|0f0", -- right, down and left
- [14] = "\135\143\148|0ff|f00\n\139\131\133|f0f|0f0", -- down, left and up
- --quadruple
- [15] = "\135\143\139|0f0|f0f\n\139\131\135|f0f|0f0" -- the only position it can be in
- }
- local rotation = {
- [0] = 0,
- -- single end
- [1] = 2,
- [2] = 3,
- [3] = 4,
- [4] = 1,
- -- double curved
- [5] = 6,
- [6] = 7,
- [7] = 8,
- [8] = 5,
- -- double straight
- [9] = 10,
- [10] = 9,
- -- triple
- [11] = 12,
- [12] = 13,
- [13] = 14,
- [14] = 11,
- -- quadruple
- [15] = 15,
- }
- local randomize_location
- do
- local single = {1, 2, 3, 4}
- local doublecurve = {5, 6, 7, 8}
- local doublestraight = {9, 10}
- local triple = {11,12,13,14}
- randomize_location = {
- [0] = {0},
- [1] = single,
- [2] = single,
- [3] = single,
- [4] = single,
- [5] = doublecurve,
- [6] = doublecurve,
- [7] = doublecurve,
- [8] = doublecurve,
- [9] = doublestraight,
- [10] = doublestraight,
- [11] = triple,
- [12] = triple,
- [13] = triple,
- [14] = triple,
- [15] = {15}
- }
- end
- local connectionInfo = {
- --blank tile
- [0]={},
- --single end
- [1] = {up=true},
- [2] = {right=true},
- [3] = {down=true},
- [4] = {left=true},
- --double curved
- [5] = {up=true,right=true},
- [6] = {right=true,down=true},
- [7] = {down=true,left=true},
- [8] = {left=true,up=true},
- --double straight
- [9] = {up=true,down=true},
- [10] = {right=true,left=true},
- --triple
- [11] = {left=true,up=true,right=true},
- [12] = {up=true,right=true,down=true},
- [13] = {right=true,down=true,left=true},
- [14] = {down=true,left=true,up=true},
- --quadruple
- [15] = {up=true,right=true,down=true,left=true},
- }
- local chartbl = {
- [0]= "0",
- ["0"] = 0,
- ["1"] = 1,
- ["2"] = 2,
- ["3"] = 3,
- ["4"] = 4,
- ["5"] = 5,
- ["6"] = 6,
- ["7"] = 7,
- ["8"] = 8,
- ["9"] = 9,
- ["a"] = 10,
- ["b"] = 11,
- ["c"] = 12,
- ["d"] = 13,
- ["e"] = 14,
- ["f"] = 15,
- }
- local inverse_chartbl = {}
- for k, v in pairs( chartbl ) do
- inverse_chartbl[ v ] = k
- end
- local maxx, maxy = term.getSize()
- local function serialize( tiles )
- local str = ""
- for y, t in ipairs( tiles ) do
- for x, tile in ipairs( t ) do
- str = str .. (inverse_chartbl[ tile ] or "0")
- end
- str = str .. "\n"
- end
- return str
- end
- local function unserialize( str )
- local tiles = {}
- local x, y = 1, 1
- for line in str:gmatch( "[^\r\n]+" ) do
- if line:sub( 1, 5 ) == "theme" then
- local f, b = line:match( "theme(.)(.)" )
- for k, v in pairs( characters ) do
- characters[ k ] = v:gsub("0", b ):gsub( "f", f )
- end
- else
- tiles[ y ] = {}
- for char in line:gmatch( "." ) do
- tiles[ y ][ x ] = chartbl[ char ]
- x = x + 1
- end
- x = 1
- y = y + 1
- end
- end
- return tiles
- end
- local function checkforwin( tiles )
- for y, t in ipairs( tiles ) do
- for x, tile in ipairs( t ) do
- for k, v in pairs( connectionInfo[ tile ] ) do
- if k == "right" then
- local target = connectionInfo[ tiles[ y ][ x + 1 ] or 0 ]
- if not target or not target.left then
- return false
- end
- elseif k == "left" then
- local target = connectionInfo[ tiles[ y ][ x - 1 ] or 0 ]
- if not target or not target.right then
- return false
- end
- elseif k == "down" then
- local target = connectionInfo[ tiles[ y + 1 ] and tiles[ y + 1 ][ x ] or 0 ]
- if not target or not target.up then
- return false
- end
- elseif k == "up" then
- local target = connectionInfo[ tiles[ y - 1 ] and tiles[ y - 1 ][ x ] or 0 ]
- if not target or not target.down then
- return false
- end
- end
- end
- end
- end
- return true
- end
- local function drawTile( tiles, x, y )
- if not tiles then
- error( "you idiot" )
- end
- if not tiles[ y ] then
- error( "no such tile y: " .. y )
- elseif not tiles[ y ][ x ] then
- error( "No such tile x: " .. x .. ", or y: " .. y )
- elseif not characters[ tiles[ y ][ x ] ] then
- error( "no such character: " .. tiles[ y ][ x ] or "nil" )
- end
- local upper, lower = characters[ tiles[ y ][ x ] ]:match( "(.+)\n(.+)" )
- term.setCursorPos( x * 3 - 2, y * 2 - 1 )
- term.blit( upper:match( "(.-)|(.-)|(.+)" ) )
- term.setCursorPos( x * 3 - 2, y * 2 )
- term.blit( lower:match( "(.-)|(.-)|(.+)" ) )
- end
- local function draw( sFile )
- local file = fs.open( sFile, "r" )
- local data = file.readAll()
- file.close()
- local tbl = unserialize( data )
- term.setBackgroundColor( 2^tonumber( characters[ 0 ]:sub( 6, 6 ), 16 ) )
- term.clear()
- for y, t in pairs( tbl ) do
- for x, tile in pairs( t ) do
- drawTile( tbl, x, y )
- end
- end
- return tbl
- end
- --[[
- awesome clicky stuff
- ]]--
- local function getClicks( tiles )
- while true do
- local event, button, x, y = os.pullEvent( "mouse_click" )
- local tilex = math.ceil( x / 3 )
- local tiley = math.ceil( y / 2 )
- if tiles[ tiley ] and tiles[ tiley ][ tilex ] then
- tiles[ tiley ][ tilex ] = rotation[ tiles[ tiley ][ tilex ] ]
- drawTile( tiles, tilex, tiley )
- if checkforwin( tiles ) then
- break
- end
- end
- end
- end
- local function levelEdit( sName )
- local themecolors = "f0"
- local tiles = {}
- if fs.exists( sName ) then
- local file = fs.open( sName, "r" )
- tiles = unserialize( file.readAll() )
- file.close()
- end
- term.setTextColor( colors.black )
- term.setBackgroundColor( 2^tonumber( characters[ 0 ]:sub( 6, 6 ), 16 ) )
- term.clear()
- for y = 2, maxy, 2 do
- tiles[ y / 2 ] = tiles[ y / 2 ] or {}
- for x = 3, maxx, 3 do
- tiles[ y / 2 ][ x / 3 ] = tiles[ y / 2 ][ x / 3 ] or 0
- drawTile( tiles, x/3, y/2 )
- end
- end
- while true do
- local event, button, x, y = os.pullEvent()
- if event == "mouse_click" and y < #tiles * 2 then
- local tilex, tiley = math.ceil( x / 3 ), math.ceil( y / 2 )
- if button == 1 then
- tiles[ tiley ][ tilex ] = math.min( 15, tiles[ tiley ][ tilex ] + 1 )
- elseif button == 2 then
- tiles[ tiley ][ tilex ] = math.max( 0, tiles[ tiley ][ tilex ] - 1 )
- end
- drawTile( tiles, tilex, tiley )
- elseif event == "char" and button == "q" then
- local file = fs.open( sName, "w" )
- file.write( serialize( tiles ) .. "theme" .. themecolors )
- file.close()
- term.setTextColor( colors.white )
- term.setBackgroundColor( colors.black )
- term.clear()
- term.setCursorPos( 1, 1 )
- print( "Saved as " .. sName )
- break
- elseif event == "char" and button == "r" then
- for y, t in pairs( tiles ) do
- for x, tile in pairs( t ) do
- local rt = randomize_location[ tile ]
- t[ x ] = rt[ math.random( 1, #rt ) ]
- drawTile( tiles, x, y )
- end
- end
- elseif event == "char" and button == "c" then
- term.setCursorPos( 1, maxy - 1 )
- term.write( "Select Two Colors (Foreground / Background" )
- term.setCursorPos( 1, maxy )
- term.clearLine()
- themecolors = minimenu( "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" ) .. (term.clearLine() or minimenu( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e" ) )
- local bc, fc = characters[ 1 ]:match( "|(.)(.)" )
- local newfc, newbc = themecolors:match( "(.)(.)" )
- for k, v in pairs( characters ) do
- characters[ k ] = v:gsub( bc, newbc ):gsub( fc, newfc )
- end
- term.setBackgroundColor( math.pow( 2, tonumber( newbc, 16 ) ) )
- term.clear()
- for y, t in pairs( tiles ) do
- for x, tile in pairs( t ) do
- drawTile( tiles, x, y )
- end
- end
- end
- term.setCursorPos( 1, maxy )
- term.write( checkforwin( tiles ) and "Solved " or "Unsolved" )
- end
- end
- local function getdir( path )
- local handle = http.get( 'https://api.github.com/repos/KingofGamesYami/InfinityLoopCC/contents/' .. path )
- if not handle then
- error( "Download failed", 0 )
- end
- local data = handle.readAll()
- local list = json.decode( data )
- handle.close()
- for k, v in pairs( list ) do
- if v.type == "dir" then
- print( "Found directory: " .. v.path )
- fs.makeDir( fs.combine( "inflooplevels", v.path ) )
- getdir( v.path )
- elseif v.type == "file" and v.name ~= "README.md" then
- print( "Found file: " .. v.path )
- local h = http.get( v.download_url )
- local file = fs.open( fs.combine( "inflooplevels", v.path ), "w" )
- file.write( h.readAll() )
- h.close()
- file.close()
- end
- end
- end
- local function getLevels()
- term.clear()
- term.setCursorPos( 1, 1 )
- fs.makeDir( "inflooplevels" )
- if not json and not os.loadAPI( "json" ) then
- local handle = http.get( "http://pastebin.com/raw/4nRg9CHU" )
- local file = fs.open( "json", "w" )
- file.write( handle.readAll() )
- handle.close()
- file.close()
- os.loadAPI( "json" )
- end
- print( "Getting main list of files" )
- getdir( "" )
- end
- local function randomLevel()
- local tiles = {}
- for y = 2, maxy, 2 do
- tiles[ y / 2 ] = {}
- for x = 3, maxx, 3 do
- tiles[ y / 3 ] = 15
- end
- end
- end
- local result = fs.exists( "solve.lua" ) and
- menu( "Infinity Loop CC", "Play", "Level Editor", "Generate Random Level", "Solve Level", "Download Levels" ) or
- menu( "Infinity Loop CC", "Play", "Level Editor", "Download Levels", "Download solve.lua" )
- if result == "Play" then
- local sFile = "inflooplevels"
- while fs.isDir( sFile ) do
- sFile = fs.combine( sFile, menu( "Select A Level", unpack( fs.list( sFile ) ) ) )
- end
- getClicks( draw( sFile ) )
- sleep( 1 )
- term.setTextColor( colors.white )
- term.setBackgroundColor( colors.black )
- term.clear()
- term.setCursorPos( 1, 1 )
- print( "You win!" )
- elseif result == "Level Editor" then
- term.clear()
- term.setCursorPos( 1, 1 )
- write( "File Name: " )
- local sFile = fs.combine( "inflooplevels", read() )
- levelEdit( sFile )
- elseif result == "Download Levels" then
- getLevels()
- elseif result == "Download solve.lua" then
- local handle = http.get( 'https://raw.githubusercontent.com/CoderPuppy/ilcc-solver/master/solve.lua' )
- local file = fs.open( "solve.lua", "w" )
- file.write( handle.readAll() )
- file.close()
- handle.close()
- term.clear()
- term.setCursorPos( 1, 1 )
- print( "Downloaded solve.lua" )
- elseif result == "Generate Random Level" then
- local w, h = math.floor( maxx / 3 ), math.floor( maxy / 2 )
- term.clear()
- term.setCursorPos( 1, 1 )
- write( "File Name: " )
- local name = fs.combine( "inflooplevels", read() )
- shell.run( "solve.lua g", name, w, h )
- elseif result == "Solve Level" then
- local sFile = "inflooplevels"
- while fs.isDir( sFile ) do
- sFile = fs.combine( sFile, menu( "Select A Level", unpack( fs.list( sFile ) ) ) )
- end
- shell.run( "solve.lua s", sFile )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement