xXm0dzXx

Mirrors

Oct 5th, 2013
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.88 KB | None | 0 0
  1. --Started programming 9/28/2013 9:38PM Pacific Time
  2. --Break: 9/28/2013 10:22PM Pacific Time (REASON: Sleep)
  3.  
  4. --Day 1 = 44 minutes
  5.  
  6. --Begin Day 2
  7. --Resumed programming 9/29/2013 9:46AM Pacific Time
  8. --Finsihed programming 9/29/2013 10:49AM Pacific Time
  9.  
  10. --Day 2 = 1 hour, 3 minutes
  11.  
  12. --Total: 1 hour, 47 minutes
  13.  
  14. --(This does not count for the time I spent building the levels)
  15.  
  16. --local currentVersion = 1.0 -- do not edit, OR THE WORLD WILL EXPLODDDDDDDDDDDDDDDDDEEEEEEEEEE!!!!!!!!!
  17.  
  18. if not term.isColor() then
  19.     print("Sorry! This requires a advanced computer")--. But don't worry, there are alot of great game engines out there for computercraft that doesnt need a advanced computer")
  20. end
  21.  
  22. local levelBuilder = false
  23. local level = 1
  24.  
  25. tArgs = { ... }
  26.  
  27. if #tArgs >= 1 then
  28.     if tonumber( tArgs[1] ) == nil then
  29.         if tArgs[1] == "levelbuilder" then
  30.             levelBuilder = true
  31.             level = -1
  32.         elseif tArgs[1] == "load" then
  33.             if tArgs[2] ~= nil then
  34.                 fs.makeDir( ".mirrors/user-levels" )
  35.             else
  36.                 print("Usage: " ..shell.getRunningProgram().. " load <levelID>")
  37.                 error()
  38.             end
  39.         else
  40.             print("Usage: " ..shell.getRunningProgram().. " [level#/levelbuilder/load] [levelID]")
  41.             error()
  42.         end
  43.     else
  44.         level = tonumber( tArgs[1] )
  45.     end
  46. end
  47.  
  48. ----------------------------------------
  49. --- xXm0dzXx Default Programming API ---
  50. -- All my programs will have this :DD --
  51. ----------------------------------------
  52.  
  53. local x,y = term.getSize()
  54.  
  55. local function cPrint( txt ) --Version 2.0 of cPrint
  56.     local function printC( text )
  57.         x2,y2 = term.getCursorPos()
  58.         term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  59.         write(text.. "\n")
  60.     end
  61.    
  62.     if type(txt) == "string" then
  63.         printC( txt )
  64.     elseif type(txt) == "table" then
  65.         for i=1,#txt do
  66.             printC( txt[i] )
  67.         end
  68.     end
  69. end
  70.  
  71. local drawingBoard = {
  72.     ["A"] = colors.white,
  73.     ["B"] = colors.orange,
  74.     ["C"] = colors.magenta,
  75.     ["D"] = colors.lightBlue,
  76.     ["E"] = colors.yellow,
  77.     ["F"] = colors.lime,
  78.     ["G"] = colors.pink,
  79.     ["H"] = colors.gray,
  80.     ["I"] = colors.lightGray,
  81.     ["J"] = colors.cyan,
  82.     ["K"] = colors.purple,
  83.     ["L"] = colors.blue,
  84.     ["M"] = colors.brown,
  85.     ["N"] = colors.green,
  86.     ["O"] = colors.red,
  87.     ["P"] = colors.black,
  88. }
  89.  
  90. local function advWrite( pos1, pos2, str )
  91.     for i=1,#str do
  92.         local symb = drawingBoard[ string.sub( str, i, i) ]
  93.         term.setCursorPos( pos1 + (i-1) , pos2 )
  94.         if symb then
  95.             term.setBackgroundColour( symb )
  96.             write(" ")
  97.         end
  98.     end
  99. end
  100.  
  101. local function setBG( col, colz )
  102.     term.setBackgroundColour( col )
  103.     term.clear()
  104.     term.setCursorPos(1,1)
  105.     term.setTextColour( colz )
  106. end
  107.  
  108. local function getLines( path )
  109.     local tLines = {}
  110.     local file = fs.open( path, "r" )
  111.     if file then
  112.         line = file.readLine()
  113.         while line do
  114.             tLines[ #tLines+1 ] = line
  115.             line = file.readLine()
  116.         end
  117.         file.close()
  118.     end
  119.    
  120.     return tLines
  121. end
  122.  
  123. -- End of API --
  124.  
  125. local mirrors = {}
  126.  
  127. local laX = 0
  128. local laY = 0
  129.  
  130. local endX = 0
  131. local endY = 0
  132.  
  133. local lazerColor = colors.red
  134.  
  135. local inventory = {}
  136. local selectedItem = 1
  137.  
  138. local lazerDirection = ">"
  139. local lazerDirections = {
  140.     "^",
  141.     "v",
  142.     "<",
  143.     ">",
  144. }
  145.  
  146. term.setTextColour( colors.red )
  147. term.setBackgroundColour( colors.white )
  148.  
  149. term.setCursorPos(1, y/2-2 )
  150. cPrint("Lazer")
  151. term.setTextColour( colors.blue )
  152. cPrint("Game")
  153. term.setTextColour( colors.red )
  154. sleep(3)
  155.  
  156. function rotate90( lDir )
  157.    
  158.     for i=1,#lDir do
  159.         local dahChar = string.sub( lDir, i, i )
  160.         if dahChar == "v" then
  161.             --lDir = string.gsub( lDir, dahChar, "<")
  162.            
  163.             lDir = "<"
  164.         elseif dahChar == "^" then
  165.             --lDir = string.gsub( lDir, dahChar, ">")
  166.            
  167.             lDir = ">"
  168.         elseif dahChar == ">" then
  169.             --lDir = string.gsub( lDir, dahChar, "%^" )
  170.            
  171.             lDir = "^"
  172.         elseif dahChar == "<" then
  173.             --lDir = string.gsub( lDir, dahChar, "v" )
  174.            
  175.             lDir = "v"
  176.         end
  177.     end
  178.    
  179.     return lDir
  180.    
  181.     --[[
  182.     if mirrors[laX] ~= nil then
  183.             if mirrors[laX][laY] ~= nil then
  184.                 local ddir = mirrors[laX][laY].dir
  185.                
  186.                 if(string.sub(ddir, 1, 1) == "+")then
  187.                     for i=1,#lazerDirection do
  188.                         local dir = string.sub(lazerDirection, i, i)
  189.                        
  190.                         for j=1,#lazerDirections do
  191.                             if lazerDirections[ j ] == dir then
  192.                                 local nu = j + tonumber( string.sub( ddir, 2, 2 ) )
  193.                                 if nu <= #lazerDirections then
  194.                                     ddir = lazerDirections[ nu ]
  195.                                 else
  196.                                     ddir = lazerDirections[ nu - #lazerDirections ]
  197.                                 end
  198.                             end
  199.                         end
  200.                     end
  201.                 end
  202.                
  203.                 lazerDirection = ddir
  204.                 sym = mirrors[laX][laY].symbol
  205.             end
  206.         end]]
  207. end
  208.  
  209. function rotate902( lDir )
  210.    
  211.     for i=1,#lDir do
  212.         local dahChar = string.sub( lDir, i, i )
  213.         if dahChar == "v" then
  214.             --lDir = string.gsub( lDir, dahChar, ">")
  215.            
  216.             lDir = ">"
  217.         elseif dahChar == "^" then
  218.             --lDir = string.gsub( lDir, dahChar, "<")
  219.            
  220.             lDir = "<"
  221.         elseif dahChar == ">" then
  222.             --lDir = string.gsub( lDir, dahChar, "v"
  223.            
  224.             lDir = "v"
  225.         elseif dahChar == "<" then
  226.             --lDir = string.gsub( lDir, dahChar, "%^" )
  227.            
  228.             lDir = "^"
  229.         end
  230.     end
  231.    
  232.     return lDir
  233. end
  234.  
  235. function addMirror( x, y, symbol, dir, txtcolor, bgcolor )
  236.     x = tonumber( x )
  237.     y = tonumber( y )
  238.     if mirrors[x] == nil then
  239.         mirrors[x] = {}
  240.     end
  241.    
  242.     if txtcolor == nil then
  243.         txtcolor = colors.cyan
  244.     end
  245.    
  246.     if bgcolor == nil then
  247.         bgcolor = colors.white
  248.     end
  249.    
  250.     mirrors[x][y] = {}
  251.     mirrors[x][y].symbol = symbol
  252.     mirrors[x][y].dir = dir
  253.     mirrors[x][y].txtcolor = txtcolor
  254.     mirrors[x][y].bgcolor = bgcolor
  255.    
  256.     term.setCursorPos( x, y )
  257.     term.setTextColour( txtcolor )
  258.     term.setBackgroundColour( bgcolor )
  259.     write( symbol )
  260. end
  261.  
  262. local function right( text )
  263.     local _, yy = term.getCursorPos()
  264.     term.setCursorPos(x-#(text)+1, yy)
  265. end
  266.    
  267. function drawHotbar()
  268.     term.setCursorPos(1,y-1)
  269.    
  270.     term.setTextColour(colors.lightGray)
  271.     term.setBackgroundColour( colors.gray )
  272.     term.clearLine()
  273.     write("Inventory")
  274.    
  275.     right("[Clear] [Undo]")
  276.    
  277.     term.setTextColour( colors.magenta )
  278.     write("[")
  279.     term.setTextColour( colors.red )
  280.     write("Clear")
  281.     term.setTextColour( colors.magenta )
  282.     write("] ")
  283.    
  284.     term.setTextColour( colors.magenta )
  285.     write("[")
  286.     term.setTextColour( colors.purple )
  287.     if levelBuilder then
  288.         write("Save")
  289.     else
  290.         write("Undo")
  291.     end
  292.     term.setTextColour( colors.magenta )
  293.     write("]")
  294.    
  295.     term.setCursorPos(1,y)
  296.     term.setBackgroundColour(colors.lightGray)
  297.     term.clearLine()
  298.    
  299.     for i=1,#inventory do
  300.         term.setCursorPos(i,y)
  301.        
  302.         if i == selectedItem then
  303.             term.setBackgroundColour(colors.gray)
  304.         else
  305.             term.setBackgroundColour(colors.lightGray)
  306.         end
  307.        
  308.         term.setTextColour( inventory[i].color )
  309.         write( inventory[i].symbol )
  310.     end
  311.    
  312.     for i=1,x do
  313.         for j=1,y-2 do
  314.             if i == x or i == 1 or j == 1 then
  315.                 if (i == laX and j == laY) or (i == endX and j == endY) then else
  316.                     term.setBackgroundColour( colors.gray )
  317.                     term.setCursorPos(i, j)
  318.                     write(" ")
  319.                 end
  320.             end
  321.         end
  322.     end
  323.    
  324.     term.setTextColour( colors.cyan )
  325.     term.setCursorPos( 1, 1 )
  326.     write("Lazer Game - Level " ..level)
  327.    
  328.     right("[Start Level]")
  329.     write("[Start Level]")
  330. end
  331.  
  332. function addItem( color, symbol, dir )
  333.     local iID = #inventory +1
  334.     inventory[ iID ] = {}
  335.     inventory[ iID ].color = color
  336.     inventory[ iID ].symbol = symbol
  337.     inventory[ iID ].direction = dir
  338. end
  339.  
  340. function addStart( x, y )
  341.     x = tonumber( x )
  342.     y = tonumber( y )
  343.     laX = x
  344.     laY = y
  345.     term.setCursorPos(x,y)
  346.     term.setTextColour( lazerColor )
  347.     term.setBackgroundColour( colors.cyan )
  348.     write("X")
  349. end
  350.  
  351. function addEnd( x, y )
  352.     x = tonumber( x )
  353.     y = tonumber( y )
  354.     endX = x
  355.     endY = y
  356.     term.setCursorPos(x,y)
  357.     term.setTextColour( lazerColor )
  358.     term.setBackgroundColour( colors.lightGray )
  359.     write("X")
  360. end
  361.  
  362. function extendLazer()
  363.     term.setBackgroundColour( colors.green ) --incase of error, then the text is visible
  364.    
  365.     if lazerDirection == nil then
  366.         term.setCursorPos( 1, 1 )
  367.         write("nil")
  368.     else
  369.         for i=1,#lazerDirection do
  370.             local dir = string.sub(lazerDirection, i, i)
  371.            
  372.             if dir == lazerDirections[1] then
  373.                 laY = laY -1
  374.             elseif dir == lazerDirections[2] then
  375.                 laY = laY +1
  376.             elseif dir == lazerDirections[3] then
  377.                 laX = laX -1
  378.             elseif dir == lazerDirections[4] then
  379.                 laX = laX +1
  380.             end
  381.         end
  382.        
  383.         local sym = " "
  384.        
  385.         local lColor = lazerColor
  386.        
  387.         if mirrors[laX] ~= nil then
  388.             if mirrors[laX][laY] ~= nil then
  389.                 mirror = mirrors[laX][laY]
  390.                
  391.                 local ddir = mirror.dir
  392.                
  393.                 if(type( ddir ) ~= "string")then
  394.                     ddir = ddir( lazerDirection )
  395.                 end
  396.                
  397.                 lazerDirection = ddir
  398.                 sym = mirror.symbol
  399.                 term.setTextColour( mirror.txtcolor )
  400.                
  401.                 if mirror.bgcolor ~= lColor and mirror.bgcolor ~= colors.white then
  402.                     lColor = mirror.bgcolor
  403.                 end
  404.             end
  405.         end
  406.        
  407.         term.setCursorPos( laX, laY )
  408.         term.setBackgroundColour( lColor )
  409.         write( sym )
  410.        
  411.         while true do
  412.             term.setBackgroundColour( colors.gray ) --incase of error, then the text is visible
  413.             term.setTextColour( colors.cyan )
  414.             term.setCursorPos( 1, 1 )
  415.             right(" [Stop Level]")
  416.             write(" [Stop Level]")
  417.            
  418.             dahtimer = os.startTimer( 0 )
  419.             event, key, _x, _y = os.pullEvent()
  420.             if event == "timer" and key == dahtimer then
  421.                 break
  422.             elseif event == "mouse_click" then
  423.                 if key == 1 and _y == 1 then
  424.                     loadLevel( level )
  425.                 end
  426.             end
  427.         end
  428.     end
  429. end
  430.  
  431. function startGame()
  432.     local failed = false
  433.    
  434.     while true do
  435.         extendLazer()
  436.        
  437.         if laX == endX and laY == endY then
  438.             break
  439.         elseif laX == x or laY == y-1 or laX == 1 or laY == 1 then
  440.             failed = true
  441.             break
  442.         end
  443.     end
  444.    
  445.     term.setCursorPos(1, y/2-1)
  446.     term.setTextColour( colors.blue )
  447.     term.setBackgroundColour( colors.red )
  448.     if failed then
  449.         cPrint("======FAILED!======")
  450.         cPrint("You failed the game")
  451.         cPrint("==Press==Any==Key==")
  452.     else
  453.         cPrint("==Congratulations==")
  454.         cPrint("You beat the level!")
  455.         cPrint("==Press==Any==Key==")
  456.     end
  457.    
  458.     while true do
  459.         event, key = os.pullEvent("key")
  460.         if key ~= 1 then
  461.             return failed
  462.         end
  463.     end
  464. end
  465.  
  466. local levelBuilderData = {}
  467.  
  468. function clear()
  469.     history = {}
  470.     mirrors = {}
  471.     levelBuilderData = {}
  472.    
  473.     laX = 0
  474.     laY = 0
  475.     endX = 0
  476.     endY = 0
  477.     lazerColor = colors.red
  478.     inventory = {}
  479.     selectedItem = 1
  480.     lazerDirection = ">"
  481.     lazerDirections = { --incase i decide to make a item that switches directions
  482.         "^",
  483.         "v",
  484.         "<",
  485.         ">",
  486.     }
  487.    
  488.     term.setTextColour( colors.red )
  489.     term.setBackgroundColour( colors.white )
  490.  
  491.     term.clear()
  492. end
  493.  
  494. local history = {}
  495. local playing = true
  496.  
  497. function undo()
  498.     tHistory = history[ #history ]
  499.     history[ #history ] = nil
  500.    
  501.     if tHistory ~= nil then
  502.         for i=1,x do
  503.             if mirrors[i] ~= nil then
  504.                 for j=1,y do
  505.                     if mirrors[i][j] ~= nil then
  506.                         term.setCursorPos( i, j )
  507.                         term.setBackgroundColour( colors.white )
  508.                         write(" ")
  509.                     end
  510.                 end
  511.             end
  512.         end
  513.        
  514.         mirrors = tHistory.mirrors
  515.         inventory = tHistory.inventory
  516.        
  517.         for i=1,x do
  518.             if mirrors[i] ~= nil then
  519.                 for j=1,y do
  520.                     local mirror = mirrors[i][j]
  521.                     if mirror ~= nil then
  522.                         term.setCursorPos( i, j )
  523.                         term.setTextColour( colors.cyan )
  524.                         write( mirror.symbol )
  525.                     end
  526.                 end
  527.             end
  528.         end
  529.     end
  530.    
  531.     --[[
  532.     mirrors[x][y] = {}
  533.     mirrors[x][y].symbol = symbol
  534.     mirrors[x][y].dir = dir]]
  535. end
  536.  
  537. local function info( text )
  538.     for i=1,#text do
  539.         term.setCursorPos(2, y-2-#text+i)
  540.        
  541.         term.setTextColour( colors.cyan )
  542.         term.setBackgroundColour( colors.white )
  543.         write( text[i] )
  544.     end
  545. end
  546.  
  547. function endgame()
  548.     laX = x
  549.     laY = y-2
  550.     return ""
  551. end
  552.  
  553. function split(pString, pPattern)
  554.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  555.    local fpat = "(.-)" .. pPattern
  556.    local last_end = 1
  557.    local s, e, cap = pString:find(fpat, 1)
  558.    while s do
  559.           if s ~= 1 or cap ~= "" then
  560.          table.insert(Table,cap)
  561.           end
  562.           last_end = e+1
  563.           s, e, cap = pString:find(fpat, last_end)
  564.    end
  565.    if last_end <= #pString then
  566.           cap = pString:sub(last_end)
  567.           table.insert(Table, cap)
  568.    end
  569.    return Table
  570. end
  571.  
  572. function loadMapData( mapData )
  573.     for i=1,#mapData do
  574.         objectData = split( mapData[i], "!" )
  575.         if objectData ~= nil and #objectData == 3 then
  576.             if objectData[1] == "/" then
  577.                 func = rotate90
  578.             elseif objectData[1] == "|" then
  579.                 func = "v"
  580.             elseif objectData[1] == "\\" then
  581.                 func = rotate902
  582.             end
  583.            
  584.             loadObject( objectData[1], func, objectData[2], objectData[3] )
  585.         end
  586.     end
  587. end
  588.  
  589. if http then
  590.     term.setTextColour( colors.white )
  591.     term.setBackgroundColour( colors.black )
  592.     term.clearLine()
  593.    
  594.     term.setCursorPos(1, y)
  595.     write("Downloading maps...")
  596.    
  597.     status = "failed"
  598.    
  599.     function get(paste)
  600.         local response = http.get(
  601.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
  602.         )
  603.        
  604.         local lines = {}
  605.            
  606.         if response then
  607.             local sResponse = response.readLine()
  608.            
  609.             term.setCursorPos(1, y-1)
  610.             term.clearLine()
  611.             write( sResponse )
  612.            
  613.             repeat
  614.                 if sResponse ~= nil then
  615.                     lines[ #lines +1 ] = sResponse
  616.                     sResponse = response.readLine()
  617.                 end
  618.             until sResponse == nil
  619.             response.close()
  620.            
  621.             status = "complete [" .. #lines .. "]"
  622.             return lines
  623.         else
  624.             status = "no response"
  625.         end
  626.     end
  627.    
  628.     levels = get("2eheura0")
  629.    
  630.     term.clearLine()
  631.    
  632.     term.setCursorPos(1, y)
  633.     write("Downloading maps... " ..status)
  634.     sleep(1)
  635. end
  636.  
  637. function loadObject( symbol, direction, _x, _y, deletingObject )
  638.     _x = tonumber( _x )
  639.     _y = tonumber( _y )
  640.        
  641.     if symbol == "S" then
  642.         addStart( _x, _y )
  643.     elseif symbol == "E" then
  644.         addEnd( _x, _y )
  645.     elseif symbol == "X" then
  646.         addMirror( _x, _y, " ", endgame, colors.black, colors.gray )
  647.     elseif symbol == "J" then
  648.         if mirrors[ _x ] ~= nil then
  649.             if mirrors[ _x ][ _y ] ~= nil then
  650.                 mirrors[ _x ][ _y ] = nil
  651.             end
  652.         end
  653.        
  654.         term.setCursorPos( _x, _y )
  655.         term.setBackgroundColour( colors.white )
  656.         write(" ")
  657.     else
  658.         addMirror(_x, _y, symbol, direction)
  659.     end
  660. end
  661.  
  662. function loadLevel( lev )
  663.     level = lev
  664.    
  665.     clear()
  666.    
  667.     if levelBuilder then
  668.         addItem( colors.cyan, "/", rotate90 )
  669.         addItem( colors.cyan, "|", "v" )
  670.         addItem( colors.cyan, "\\", rotate902 )
  671.         addItem( colors.red, "S", "" )
  672.         addItem( colors.red, "E", "" )
  673.         addItem( colors.red, "X", endgame )
  674.         addItem( colors.red, "J", endgame )
  675.     elseif lev == -1 then
  676.         addItem( colors.cyan, "/", rotate90 )
  677.         addItem( colors.cyan, "|", "v" )
  678.         addItem( colors.cyan, "\\", rotate902 )
  679.        
  680.         addStart( 1, 2 )
  681.         addEnd( x, y-2 )
  682.        
  683.         term.setTextColour( colors.cyan )
  684.         term.setBackgroundColour( colors.white )
  685.         term.setCursorPos( 2, y-3 )
  686.     elseif lev == 1 then
  687.         addItem( colors.cyan, "\\", rotate902 )
  688.         addStart( 3, 3 )
  689.         addEnd( x-2, y-3 )
  690.        
  691.         info( {
  692.             "Welcome to Lazer Game!",
  693.             "Click anywhere to place a mirror",
  694.             } )
  695.     elseif lev == 2 then
  696.         addItem( colors.cyan, "\\", rotate902 )
  697.         addItem( colors.cyan, "/", rotate90 )
  698.        
  699.         addStart( 3, 3 )
  700.         addEnd( 3, 6 )
  701.        
  702.         info( {
  703.             "You will need to use",
  704.             "mirrors to solve puzzles",
  705.             } )
  706.     elseif lev == 3 then
  707.         addItem( colors.cyan, "|", "v" )
  708.        
  709.         addMirror( 6, 6, "/", rotate90 )
  710.         addMirror( 6, 3, "/", rotate90 )
  711.        
  712.         addMirror( 12, 9, "/", rotate90 )
  713.        
  714.         addStart( 3, 6 )
  715.         addEnd( 3, 9 )
  716.        
  717.         info( {
  718.             "This mirror will send the lazer",
  719.             "down, regarding which side it got hit.",
  720.             } )
  721.     elseif lev == 4 then
  722.         addStart( 3, 6 )
  723.         addEnd( 3, 9 )
  724.        
  725.         addMirror( 12, 9, " ", endgame, colors.black, colors.gray )
  726.        
  727.         addItem( colors.cyan, "\\", rotate902 )
  728.         addItem( colors.cyan, "/", rotate90 )
  729.        
  730.         info( {
  731.             "Good Luck!",
  732.             } )
  733.     elseif lev >= 5 then
  734.         if http == nil then
  735.             info( { "Sorry! The HTTP API is needed to finish the game." } )
  736.         else
  737.             if levels[ lev - 4 ] ~= nil then
  738.                 levelBuilder = true
  739.                 loadMapData( get( levels[ lev - 4 ] ) )
  740.                 levelBuilder = false
  741.                
  742.                 if lev == 5 then
  743.                     addItem( colors.cyan, "\\", rotate902 )
  744.                 elseif lev == 6 then
  745.                     info( {
  746.                     "Protip: Did you know you",
  747.                     "can place mirrors over other mirrors?",
  748.                     } )
  749.                    
  750.                     addItem( colors.cyan, "\\", rotate902 )
  751.                 else
  752.                     info( {
  753.                     "Oh noez! Your client",
  754.                     "is out of date! Update then do:",
  755.                     "> " ..shell.getRunningProgram().. " " ..lev,
  756.                     "to resume your progress",
  757.                     } )
  758.                 end
  759.             else
  760.                 info( {
  761.                 "There are no more levels for the game. [" ..#levels.. "]",
  762.                 "In the meantime, how about opening the sandbox?",
  763.                 "Usage: " ..shell.getRunningProgram().. " levelbuilder",
  764.                 } )
  765.             end
  766.         end
  767.     end
  768.    
  769.     drawHotbar()
  770.    
  771.     while playing do
  772.         local event, key, _x, _y = os.pullEvent()
  773.        
  774.         if event == "mouse_click" or (event == "mouse_drag" and level == -1) then
  775.             if key == 1 then
  776.                 if _y == y and inventory[_x] ~= nil then
  777.                     selectedItem = _x
  778.                 elseif _y ~= 1 and _y ~= y-1 and _x ~= 1 and _x ~= x and not (_x == laX and _y == laY) and not (_x == endX and _y == endY) then
  779.                     stopplacement = false
  780.                     if mirrors[_x] ~= nil then
  781.                         if mirrors[_x][_y] ~= nil then
  782.                             if mirrors[_x][_y].symbol == " " and not levelBuilder then
  783.                                 stopplacement = true
  784.                             end
  785.                         end
  786.                     end
  787.                    
  788.                     if not stopplacement and inventory ~= nil and inventory[ selectedItem ] ~= nil then
  789.                         loadObject( inventory[ selectedItem ].symbol, inventory[ selectedItem ].direction, _x, _y )
  790.                        
  791.                         if levelBuilder then
  792.                             levelBuilderData[ #levelBuilderData +1 ] = inventory[ selectedItem ].symbol .. "!" .. _x .. "!" .. _y
  793.                         end
  794.                        
  795.                         if lev ~= -1 then
  796.                             local resetEnd = false
  797.                            
  798.                             for i=selectedItem, #inventory do
  799.                                 if inventory[i+1] ~= nil then
  800.                                     inventory[i] = inventory[i+1]
  801.                                     resetEnd = true
  802.                                 elseif resetEnd or #inventory == 1 then
  803.                                     inventory[i] = nil
  804.                                 end
  805.                             end
  806.                         end
  807.                        
  808.                         history[ #history +1 ] = {}
  809.                         history[ #history ].mirrors = mirrors
  810.                         history[ #history ].inventory = inventory
  811.                     end
  812.                 elseif _y == 1 then
  813.                     failed = startGame()
  814.                    
  815.                     if failed or lev == -1 then
  816.                         loadLevel( lev )
  817.                     else
  818.                         loadLevel( lev +1 )
  819.                     end
  820.                 elseif _y == y-1 then
  821.                     if _x >= x - 6 and #history ~= 0 then
  822.                         if levelBuilder then
  823.                             info({"Saving...          "})
  824.                             fs.delete( "custom-level" )
  825.                             file = fs.open( "custom-level", "w" )
  826.                             for i=1,#levelBuilderData do
  827.                                 if levelBuilderData[i] ~= "" then
  828.                                     if i == #levelBuilderData then
  829.                                         file.write( levelBuilderData[i] )
  830.                                     else
  831.                                         file.writeLine( levelBuilderData[i] )
  832.                                     end
  833.                                 end
  834.                             end
  835.                             file.close()
  836.                             info({"Saving... Complete!"})
  837.                             os.startTimer( 3 )
  838.                         else
  839.                             --undo()
  840.                            
  841.                             info( {
  842.                                 "Sorry! The undo button is broken.",
  843.                                 "Please use the clear button instead.",
  844.                                 } )
  845.                         end
  846.                     elseif _x >= x - 13 then
  847.                         loadLevel( lev )
  848.                     end
  849.                 end
  850.                
  851.                 drawHotbar()
  852.             end
  853.         elseif event == "timer" then
  854.             info({"                   "})
  855.         end
  856.     end
  857. end
  858.  
  859. loadLevel( level )
Advertisement
Add Comment
Please, Sign In to add comment