UNOBTANIUM

Planetz Under Gravity

Jun 5th, 2013
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.99 KB | None | 0 0
  1. -- VARIABLES
  2.  
  3. local version = "Planetz Under Gravity 0.3.0"
  4. local computerID = os.getComputerID()
  5. local h, w = 26, 39
  6. local playingLevel, playingLevelName = 0, ""
  7. local warpedPlanets, amountPlanets, directionChanges = 0, 0, 0
  8. local space = {}
  9. local planet = {}
  10. local moon = {}
  11. local editorGUI = true
  12. local sideGUI = true
  13. local extendedGUI = false
  14. local selectedSpace = "N"
  15. local fancy = false
  16. local fancyColor = colors.white
  17.  
  18. -- ADD MONITOR
  19.  
  20. local monitor = peripheral.wrap("right")
  21. term.redirect(monitor)
  22.  
  23. -- PRELOAD TABLES
  24.  
  25. --[planetnumber][x|y|state]
  26. for i=1,10 do
  27.  planet[i] = {}
  28.  moon[i] = {}
  29.  for j=1,3 do
  30.   if j == 3 then
  31.    planet[i][j] = "noDir"
  32.    moon[i][j] = "noDir"
  33.   else
  34.    planet[i][j] = 0
  35.    moon[i][j] = 0
  36.   end
  37.  end
  38. end
  39.  
  40. for x=1,w do
  41.  space[x] = {}
  42.  for y=1,h do
  43.   space[x][y] = "N"
  44.  end
  45. end
  46.  
  47. -- DRAW AND CLEAR
  48.  
  49. function setFancyOr(individualColor)
  50.  if fancy then
  51.   term.setTextColor(fancyColor)
  52.  else
  53.   term.setTextColor(individualColor)
  54.  end
  55. end
  56.  
  57. function draw(editorMode)
  58.  term.setTextColor(colors.black)
  59.  for x=1,w do
  60.   for y=1,h do
  61.    if space[x][y] == "#" then -- ASTEROID
  62.     paintutils.drawPixel(x, y, colors.gray)
  63.    elseif space[x][y] == "+" then -- INCOMMING ASTEROID
  64.     paintutils.drawPixel(x, y, colors.lightGray)
  65.    elseif space[x][y] == "X" then -- WORM HOLE / GOAL
  66.    paintutils.drawPixel(x, y, colors.orange)
  67.    elseif editorMode and space[x][y] == "P" then -- PLANET (EDITOR)
  68.     paintutils.drawPixel(x, y, colors.lightBlue)
  69.    elseif editorMode and space[x][y] == "M" then -- MOON (EDITOR)
  70.     paintutils.drawPixel(x, y, colors.brown)
  71.    elseif space[x][y] == "O" then -- SUN / DEATH
  72.     paintutils.drawPixel(x, y, colors.red)
  73.    elseif space[x][y] == "A" or space[x][y] == "B" or space[x][y] == "C" then
  74.     paintutils.drawPixel(x,y, colors.yellow)
  75.     if editorMode then
  76.      term.setTextColor(colors.black)
  77.      term.setCursorPos(x,y)
  78.      term.write(space[x][y])
  79.     end
  80.    elseif space[x][y] == "1" or space[x][y] == "2" or space[x][y] == "3" then
  81.     if editorMode then
  82.      paintutils.drawPixel(x, y, colors.lightGray)
  83.      term.setTextColor(colors.white)
  84.      term.setCursorPos(x,y)
  85.      term.write(space[x][y])
  86.     else
  87.      paintutils.drawPixel(x, y, colors.black)
  88.     end
  89.    elseif space[x][y] == "6" or space[x][y] == "7" or space[x][y] == "8" then
  90.     paintutils.drawPixel(x, y, colors.gray)
  91.     if editorMode then
  92.      term.setTextColor(colors.white)
  93.      term.setCursorPos(x,y)
  94.      term.write(space[x][y])
  95.     end
  96.    else
  97.     paintutils.drawPixel(x, y, colors.black)
  98.    end
  99.    for p=1,10 do
  100.     if planet[p][1] == x and planet[p][2] == y then -- PLANET (INGAME)
  101.      paintutils.drawPixel(x, y, colors.lightBlue)
  102.     end
  103.     if moon[p][1] == x and moon[p][2] == y then -- MOON (INGAME)
  104.      paintutils.drawPixel(x, y, colors.brown)
  105.     end
  106.    end
  107.   end
  108.  end
  109.  term.setTextColor(colors.white)
  110. end
  111.  
  112. function clear()
  113.  term.setBackgroundColor(colors.black)
  114.  term.clear()
  115.  term.setCursorPos(1,1)
  116. end
  117.  
  118. -- PLANET INFO SET
  119.  
  120. function setPlanet(p,a,b,c)
  121.  planet[p][1] = a
  122.  planet[p][2] = b
  123.  planet[p][3] = c
  124. end
  125.  
  126. function setMoon(m,a,b,c)
  127.  moon[m][1] = a
  128.  moon[m][2] = b
  129.  moon[m][3] = c
  130. end
  131.  
  132. -- SET DIRECTION
  133.  
  134. function setDirection(newDir)
  135.  for p=1,10 do
  136.   if planet[p][1] > 0 and planet[p][3] == "noDir" then
  137.    planet[p][3] = newDir
  138.   end
  139.   if moon[p][1] > 0 and moon[p][3] == "noDir" then
  140.    moon[p][3] = newDir
  141.   end
  142.  end
  143. end
  144.  
  145. -- CHECK SPACE
  146.  
  147. function checkForOutOfSpace(p,isMoon)
  148.  if not isMoon then
  149.   if planet[p][1] == 0 or planet[p][1] == w+1 or planet[p][2] == 0 or planet[p][2] == h+1 then
  150.    return true
  151.   else
  152.    return false
  153.   end
  154.  else
  155.   if moon[p][1] == 0 or moon[p][1] == w+1 or moon[p][2] == 0 or moon[p][2] == h+1 then
  156.    return true
  157.   else
  158.    return false
  159.   end
  160.  end
  161. end
  162.  
  163. function checkForCollision(p,x,y)
  164.  if ( planet[p][1] == x and planet[p][2] == y ) or ( moon[p][1] == x and moon[p][2] == y ) then
  165.   return true
  166.  else
  167.   return false
  168.  end
  169. end
  170.  
  171. function checkSpace(x,y,collisionCheck,isMoon)
  172.  for p=1,10 do
  173.   if isMoon then                    --MOON
  174.    if not ( moon[p][3] == "noDir" ) then
  175.     if checkForOutOfSpace(p,true) then
  176.      return "death"
  177.     end
  178.    end
  179.   else                          --PLANET
  180.    if not ( planet[p][3] == "noDir" ) then
  181.     if checkForOutOfSpace(p,false) then
  182.      return "death"
  183.     end
  184.    end
  185.   end
  186.   if collisionCheck and checkForCollision(p,x,y) then   --BOTH
  187.    return "blocked"
  188.   end
  189.  end
  190.  if space[x][y] == "N" or space[x][y] == "+" then
  191.   return "free"
  192.  elseif space[x][y] == "O" then
  193.   return "death"
  194.  elseif space[x][y] == "X" then
  195.   return "wormhole"
  196.  elseif space[x][y] == "#" or space[x][y] == "6" or space[x][y] == "7" or space[x][y] == "8" then
  197.   return "blocked"
  198.  else           -- "1" "2" "3"
  199.   return "free"
  200.  end
  201. end
  202.  
  203. -- CHECK ACTION
  204.  
  205. function changeSpace(from, to)
  206.  for y=1,h do
  207.   for x=1,w do
  208.    if space[x][y] == from then
  209.     space[x][y] = to
  210.    elseif space[x][y] == to then
  211.     space[x][y] = from
  212.    end
  213.   end
  214.  end
  215. end
  216.  
  217. function checkForIncomming(p,isMoon)
  218.  if not isMoon then
  219.   if space[planet[p][1]][planet[p][2]] == "+" then
  220.    space[planet[p][1]][planet[p][2]] = "#"
  221.   end
  222.  else
  223.   if space[moon[p][1]][moon[p][2]] == "+" then
  224.    space[moon[p][1]][moon[p][2]] = "#"
  225.   end
  226.  end
  227. end
  228.  
  229. function checkForGate(p,isMoon)
  230.  if not isMoon then
  231.   if space[planet[p][1]][planet[p][2]] == "A" then
  232.    changeSpace("1","6")
  233.   elseif space[planet[p][1]][planet[p][2]] == "B" then
  234.    changeSpace("2","7")
  235.   elseif space[planet[p][1]][planet[p][2]] == "C" then
  236.    changeSpace("3","8")
  237.   end
  238.  else
  239.   if space[moon[p][1]][moon[p][2]] == "A" then
  240.    changeSpace("1","6")
  241.   elseif space[moon[p][1]][moon[p][2]] == "B" then
  242.    changeSpace("2","7")
  243.   elseif space[moon[p][1]][moon[p][2]] == "C" then
  244.    changeSpace("3","8")
  245.   end
  246.  end
  247. end
  248.  
  249.  
  250. -- MOVE PLANETS
  251.  
  252. function move()
  253.  for p=1,10 do
  254.   local relX, relY = 0, 0
  255.   if planet[p][1] > 0 and not (planet[p][3] == "noDir") then
  256.    if planet[p][3] == "up" then
  257.     relY = -1
  258.    elseif planet[p][3] == "right" then
  259.     relX = 1
  260.    elseif planet[p][3] == "down" then
  261.     relY = 1
  262.    elseif planet[p][3] == "left" then
  263.     relX = -1
  264.    end
  265.    local newSpace = checkSpace(planet[p][1]+relX,planet[p][2]+relY,true,false)
  266.    if newSpace == "free" then
  267.     checkForIncomming(p,false)
  268.     setPlanet(p,planet[p][1]+relX,planet[p][2]+relY,planet[p][3])
  269.     checkForGate(p,false)
  270.     if checkSpace(planet[p][1]+relX,planet[p][2]+relY,false,false) == "blocked" then
  271.      planet[p][3] = "noDir"
  272.     end
  273.    elseif newSpace == "blocked" then
  274.     planet[p][3] = "noDir"
  275.    elseif newSpace == "death" then
  276.     setPlanet(p,0,0,"noDir")
  277.    elseif newSpace == "wormhole" then
  278.     setPlanet(p,0,0,"noDir")
  279.     warpedPlanets = warpedPlanets + 1
  280.    end
  281.    relX, relY = 0, 0
  282.   end
  283.   if moon[p][1] > 0 and not (moon[p][3] == "noDir") then
  284.    if moon[p][3] == "up" then
  285.     relY = -1
  286.    elseif moon[p][3] == "right" then
  287.     relX = 1
  288.    elseif moon[p][3] == "down" then
  289.     relY = 1
  290.    elseif moon[p][3] == "left" then
  291.     relX = -1
  292.    end
  293.    local newSpace = checkSpace(moon[p][1]+relX,moon[p][2]+relY,true,true)
  294.    if newSpace == "free" then
  295.     checkForIncomming(p,true)
  296.     setMoon(p,moon[p][1]+relX,moon[p][2]+relY,moon[p][3])
  297.     checkForGate(p,true)
  298.     if checkSpace(moon[p][1]+relX,moon[p][2]+relY,false,true) == "blocked" then
  299.      moon[p][3] = "noDir"
  300.     end
  301.    elseif newSpace == "blocked" then
  302.     moon[p][3] = "noDir"
  303.    elseif newSpace == "death" then
  304.     setMoon(p,0,0,"noDir")
  305.    elseif newSpace == "wormhole" then
  306.     setMoon(p,0,0,"noDir")
  307.    end
  308.   end
  309.  end
  310. end
  311.  
  312.  
  313. -- ESC MENU
  314.  
  315. function escMenu()
  316.  for x=10,28 do
  317.   for y=4,20 do
  318.    if fancy then
  319.     paintutils.drawPixel(x, y, fancyColor )
  320.    else
  321.     paintutils.drawPixel(x, y, colors.white )
  322.    end
  323.   end
  324.  end
  325.  for x=11,27 do
  326.   for y=5,19 do
  327.    paintutils.drawPixel(x, y, colors.black)
  328.   end
  329.  end
  330.  setFancyOr(colors.white)
  331.  centered("CONTINUE",7)
  332.  centered("RESTART",12)
  333.  centered("EXIT",17)
  334.  sleep(0.1)
  335.  local event = { os.pullEvent("monitor_touch") }
  336.  if event[4] >= 1 and event[4] <= 10 then
  337.   return "continue"
  338.  elseif event[4] >= 11 and event[4] <= 14 then
  339.   return "restart"
  340.  elseif event[4] >= 15 and event[4] <= h then
  341.   return "exit"
  342.  end
  343. end
  344.  
  345. -- PLAY
  346.  
  347. function play()
  348.  clear()
  349.  local neededTime = 0 -- CHANGE IF LOAD AND SAFE AVAILABLE
  350.  local firstHit = true
  351.  directionChanges = 0
  352.  warpedPlanets = 0
  353.  draw(false)
  354.  local changeDir
  355.  local delay
  356.  local toDo
  357.  while amountPlanets > warpedPlanets do
  358.   changeDir = true
  359.   toDo = true
  360.   local direction = ""
  361.   local event = { os.pullEvent() }
  362.   if event[1] == "timer" then
  363.    move()
  364.    draw(false)
  365.    neededTime = neededTime + 0.15
  366.    delay = os.startTimer(0.15)
  367.   elseif event[1] == "monitor_touch" then
  368.    if event[4] >= 1 and event[4] <= 7 then
  369.     direction = "up"
  370.    elseif event[3] >= 30 and event[3] <= w+1 then
  371.     direction = "right"
  372.    elseif event[4] >= 18 and event[4] <= h+1 then
  373.     direction = "down"
  374.    elseif event[3] >= 1 and event[3] <= 10 then
  375.     direction = "left"
  376.    else
  377.     if not firstHit then
  378.      local event = { os.pullEvent("timer") }
  379.     end
  380.     local esc = escMenu()
  381.     if esc == "exit" then
  382.      return false
  383.     elseif esc == "continue" then
  384.      changeDir = false
  385.      toDo = false
  386.      if not firstHit then
  387.       delay = os.startTimer(0.15)
  388.      end
  389.      draw(false)
  390.     elseif esc == "restart" then
  391.      loadLevel(playingLevel)
  392.      changeDir, firstHit, toDo, neededTime, directionChanges, warpedPlanets = false, true, false, 0, 0, 0
  393.      draw(false)
  394.     end
  395.    end
  396.     if toDo and firstHit then
  397.      firstHit = not firstHit
  398.      delay = os.startTimer(0.15)
  399.      move()
  400.      draw(false)
  401.     end
  402.    if changeDir then
  403.     directionChanges = directionChanges + 1
  404.     setDirection(direction)
  405.    end
  406.   end
  407.  end
  408.  if amountPlanets == warpedPlanets then
  409.   clear()
  410.   setFancyOr(colors.white)
  411.   centered(playingLevelName,7)
  412.   centered("Gravity Modifications:",10)
  413.   centered(tostring(directionChanges), 12)
  414.   centered("Time:",15)
  415.   centered(tostring(neededTime), 17)
  416.   local event = { os.pullEvent("monitor_touch") }
  417.   playingLevel, playingLevelName = 0, ""
  418.  end
  419.  return true
  420. end
  421.  
  422.  
  423.  
  424.  
  425. -- EDITOR GUI
  426.  
  427. function drawTwo(x,y,c)
  428.  paintutils.drawPixel(x, y, c)
  429.  paintutils.drawPixel(x+1, y, c)
  430. end
  431.  
  432. function drawTwoText(x,y,str,ct,cb)
  433.  term.setTextColor(ct)
  434.  term.setBackgroundColor(cb)
  435.  term.setCursorPos(x,y)
  436.  term.write(str .. str)
  437. end
  438.  
  439. function drawEditorGUI()
  440.  if editorGUI then
  441.   for x=1,w do
  442.    paintutils.drawPixel(x, 1, colors.white)
  443.   end
  444.   setFancyOr(colors.black)
  445.   term.setCursorPos(1,1)
  446.   term.write("CLOSE")
  447.    drawTwo(7,1,colors.black)
  448.   drawTwo(10,1,colors.gray)
  449.   drawTwo(13,1,colors.lightBlue)
  450.   drawTwo(16,1,colors.lightGray)
  451.   drawTwo(19,1,colors.orange)
  452.   drawTwo(22,1,colors.red)
  453.   paintutils.drawPixel(24, 1, colors.white)
  454.   term.setCursorPos(w-3,1)
  455.   term.write("EXIT")
  456.   if extendedGUI then
  457.    term.setCursorPos(w-13,1)
  458.    term.write("LESS")
  459.    for x=1,w do
  460.     paintutils.drawPixel(x, 2, colors.white)
  461.    end
  462.    drawTwoText(4,2,"A",colors.black,colors.yellow)
  463.    drawTwoText(7,2,"B",colors.black,colors.yellow)
  464.    drawTwoText(10,2,"C",colors.black,colors.yellow)
  465.    drawTwoText(13,2,"1",colors.white,colors.lightGray)
  466.    drawTwoText(16,2,"2",colors.white,colors.lightGray)
  467.    drawTwoText(19,2,"3",colors.white,colors.lightGray)
  468.    drawTwoText(22,2,"6",colors.white,colors.gray)
  469.    drawTwoText(25,2,"7",colors.white,colors.gray)
  470.    drawTwoText(28,2,"8",colors.white,colors.gray)
  471.    drawTwo(31,2,colors.brown)
  472.    setFancyOr(colors.black)
  473.    term.setBackgroundColor(colors.white)
  474.   else
  475.    term.setCursorPos(w-13,1)
  476.    term.write("MORE")
  477.    setFancyOr(colors.black)
  478.    term.setBackgroundColor(colors.white)
  479.   end
  480.   term.setCursorPos(w-8,1)
  481.   term.write("SAVE")
  482.   setFancyOr(colors.white)
  483.  else
  484.   if sideGUI then
  485.    term.setCursorPos(1,1)
  486.   else
  487.    term.setCursorPos(5,1)
  488.   end
  489.   setFancyOr(colors.white)
  490.   term.write("GUI")
  491.  end
  492. end
  493.  
  494. -- CHECK GUI
  495.  
  496. function checkEditorGUI(xPos,yPos)
  497.  term.setCursorPos(1,1)
  498.  if editorGUI then
  499.   if yPos == 1 then
  500.    if xPos >= 1 and xPos <= 5 then
  501.     editorGUI = false
  502.    elseif xPos == 7 or xPos == 8 then
  503.     selectedSpace = "N"
  504.    elseif xPos == 10 or xPos == 11 then
  505.     selectedSpace = "#"
  506.    elseif xPos == 13 or xPos == 14 then
  507.     selectedSpace = "P"
  508.    elseif xPos == 16 or xPos == 17 then
  509.     selectedSpace = "+"
  510.    elseif xPos == 19 or xPos == 20 then
  511.     selectedSpace = "X"
  512.    elseif xPos == 22 or xPos == 23 then
  513.     selectedSpace = "O"
  514.    elseif xPos >= 26 and xPos <= 29 then
  515.     extendedGUI = not extendedGUI
  516.    elseif xPos >= 31 and xPos <= 34 then
  517.     return "save"
  518.    elseif xPos >= 36 then
  519.     return "exit"
  520.    end
  521.    return "true"
  522.   elseif extendedGUI and yPos == 2 then
  523.    if xPos == 4 or xPos == 5 then
  524.     selectedSpace = "A"
  525.    elseif xPos == 7 or xPos == 8 then
  526.     selectedSpace = "B"
  527.    elseif xPos == 10 or xPos == 11 then
  528.     selectedSpace = "C"
  529.    elseif xPos == 13 or xPos == 14 then
  530.     selectedSpace = "1"
  531.    elseif xPos == 16 or xPos == 17 then
  532.     selectedSpace = "2"
  533.    elseif xPos == 19 or xPos == 20 then
  534.     selectedSpace = "3"
  535.    elseif xPos == 22 or xPos == 23 then
  536.     selectedSpace = "6"
  537.    elseif xPos == 25 or xPos == 26 then
  538.     selectedSpace = "7"
  539.    elseif xPos == 28 or xPos == 29 then
  540.     selectedSpace = "8"
  541.    elseif xPos == 31 or xPos == 32 then
  542.     selectedSpace = "M"
  543.    end
  544.    return "true"
  545.   end
  546.  elseif not editorGUI and yPos == 1 then
  547.   if sideGUI and xPos <= 3 then
  548.    editorGUI = true
  549.    sideGUI = not sideGUI
  550.    sleep(0.1)
  551.    return "true"
  552.   elseif not sideGUI and xPos >= 5 and xPos <= 7 then
  553.    editorGUI = true
  554.    sideGUI = not sideGUI
  555.    sleep(0.1)
  556.    return "true"
  557.   end
  558.  end
  559.  return "false"
  560. end
  561.  
  562.  
  563. -- CREATE OR CHANGE LEVEL
  564.  
  565. function levelEditor()
  566.  selectedSpace = "#"
  567.  editorGUI = false
  568.  sideGUI = true
  569.  extendedGUI = false
  570.  for p=1,10 do
  571.   if planet[p][1] > 0 then
  572.    space[planet[p][1]][planet[p][2]] = "P"
  573.    setPlanet(p,0,0,"noDir")
  574.   end
  575.   if moon[p][1] > 0 then
  576.    space[moon[p][1]][moon[p][2]] = "M"
  577.    setMoon(p,0,0,"noDir")
  578.   end
  579.  end
  580.  while true do
  581.   draw(true)
  582.   drawEditorGUI()
  583.   local event = { os.pullEvent("monitor_touch") }
  584.   local GUIinteraction = checkEditorGUI(event[3],event[4])
  585.   if GUIinteraction == "false" then
  586.    space[event[3]][event[4]] = selectedSpace
  587.   elseif GUIinteraction == "save" then
  588.    if saveLevel() then return end
  589.   elseif GUIinteraction == "exit" then
  590.    return false
  591.   end
  592.  end
  593. end
  594.  
  595.  
  596. -- LEVEL SELECT                                         -- HERE
  597.  
  598. function pastebinImport()
  599.  local levelnumber = 0
  600.  for i=1,1000 do
  601.   if not fs.exists("PUG_"..i) then
  602.    levelnumber = i
  603.    break
  604.   end
  605.  end
  606.  clear()
  607.  setFancyOr(colors.white)
  608.  centered("Paste pastebin link:",5)
  609.  term.setCursorPos(10,8)
  610.  local link = read()
  611.  local accualLink = ""
  612.  if #link == 8 then
  613.   accualLink = link
  614.  elseif #link == 21 then
  615.   accualLink = link:sub(14,21)
  616.  elseif #link == 28 then
  617.   accualLink = link:sub(21,28)
  618.  else
  619.   centered("No Pastebin link", 11)
  620.   sleep(2)
  621.   return
  622.  end
  623.  centered("Getting Level...", 11)
  624.  term.setCursorPos(1,14)
  625.  shell.run("pastebin","get",accualLink,"PUG_" .. levelnumber)
  626.  centered("Finished", 17)
  627.  sleep(1.5)
  628. end
  629.  
  630.  
  631. function resetPlayfield()
  632.  for i=1,10 do
  633.   for j=1,3 do
  634.    if j == 3 then
  635.     planet[i][j] = "noDir"
  636.     moon[i][j] = "noDir"
  637.    else
  638.     planet[i][j] = 0
  639.     moon[i][j] = 0
  640.    end
  641.   end
  642.  end
  643.  
  644.  for x=1,w do
  645.   for y=1,h do
  646.    space[x][y] = "N"
  647.   end
  648.  end
  649. end
  650.  
  651. function saveLevel()
  652.  clear()
  653.  local goals = 0
  654.  local planets = 0
  655.  local moons = 0
  656.  for x=1,w do
  657.   for y=1,h do
  658.    if space[x][y] == "X" then
  659.     goals = goals + 1
  660.    elseif space[x][y] == "P" then
  661.     planets = planets + 1
  662.    elseif space[x][y] == "M" then
  663.     moons = moons + 1
  664.    end
  665.   end
  666.  end
  667.  
  668.  if goals == 0 or planets == 0 or planets > 10 or moons > 10 then
  669.   term.write("No goal or planet found or too many planets or moons! Maximal 10!")
  670.   sleep(2)
  671.   return false
  672.  end
  673.  
  674.  local levelName = ""
  675.  repeat
  676.   term.write("Name your level: ")
  677.   levelName = read()
  678.  until #levelName > 0
  679.  
  680.  local levelnumber = 0
  681.  for i=1,1000 do
  682.   if not fs.exists("PUG_"..i) then
  683.    levelnumber = i
  684.    break
  685.   end
  686.  end
  687.  
  688.  local file = fs.open("PUG_"..levelnumber , "w")
  689.  file.writeLine(levelName)
  690.  file.writeLine(planets)
  691.  for y=1, h do
  692.   for x=1,w do
  693.    if x == 39 then
  694.     file.writeLine(space[x][y])
  695.    else
  696.     file.write(space[x][y])
  697.    end
  698.   end
  699.  end
  700.  file.close()
  701.  return true
  702. end
  703.  
  704.  
  705. function loadLevel(levelName)
  706.  resetPlayfield()
  707.  
  708.  local planetnumber = 1
  709.  local moonnumber = 1
  710.  local file = fs.open("PUG_"..levelName,"r")
  711.  levelName = file.readLine()
  712.  amountPlanets = tonumber(file.readLine())
  713.  for y=1, h do
  714.   local row = file.readLine()
  715.   for x = 1, w do
  716.    local typeSpace = row:sub(x,x)
  717.    if typeSpace == "P" then
  718.     setPlanet(planetnumber,x,y,"noDir")
  719.     planetnumber = planetnumber + 1
  720.    elseif typeSpace == "M" then
  721.     setMoon(moonnumber,x,y,"noDir")
  722.     moonnumber = moonnumber + 1
  723.    else
  724.     space[x][y] = typeSpace
  725.    end
  726.   end
  727.  end
  728.  file.close()
  729. end
  730.  
  731. function deleteLevel()
  732.  fs.delete("PUG_" .. playingLevel)
  733.  resetPlayfield()
  734. end
  735.  
  736.  
  737. function selectLevel(editorMode, header)
  738.  local level = {}
  739.  for i=1,1000 do
  740.   if fs.exists("PUG_" .. i) then
  741.    local file = fs.open("PUG_"..i ,"r")
  742.    table.insert(level, 1, { [1]=file.readLine(), [2]=i })
  743.    file.close()
  744.   end
  745.  end
  746.  
  747.  local actions = 0
  748.  for k,v in pairs(level) do
  749.   actions = actions + 1
  750.  end
  751.  if actions == 0 then
  752.   if editorMode then
  753.    resetPlayfield()
  754.    return true
  755.   else
  756.    return false
  757.   end
  758.  end
  759.  
  760.  local more = 0
  761.  while true do
  762.   clear()
  763.   setFancyOr(colors.white)
  764.   centered(header .. ": Level " .. more+1 .. " to " .. more+15 , 1)
  765.   local maxLevelShown = 10
  766.   if actions-more < 10 then
  767.    maxLevelShown = actions-more
  768.   end
  769.   for l=1,maxLevelShown do
  770.    centered(level[l+more][1],l+3)
  771.   end
  772.   if actions-more > 15 then
  773.    centered("More level",23)
  774.   elseif more > 0 then
  775.    centered("Back to the first level",23)
  776.   end
  777.   if editorMode then
  778.    centered("Create a new level",24)
  779.   end
  780.   centered("Back to the menu",25)
  781.  
  782.   local event = { os.pullEvent("monitor_touch") }
  783.   if event[4] >= 4 and event[4] <= maxLevelShown+3 then
  784.    if type(level[event[4]-3+more][1]) == "string" then
  785.     loadLevel(level[event[4]-3+more][2])
  786.     playingLevel = level[event[4]-3+more][2]
  787.     playingLevelName = level[event[4]-3+more][1]
  788.     return true
  789.    end
  790.   elseif event[4] == 23 then
  791.    if actions-more > 15 then
  792.     more = more + 15
  793.    elseif more > 0 then
  794.     more = 0
  795.    end
  796.   elseif editorMode and event[4] == 24 then
  797.    resetPlayfield()
  798.    return true
  799.   elseif event[4] == 25 then
  800.    return false
  801.   end  
  802.  end
  803. end
  804.  
  805. -- MAIN MENU
  806.  
  807. function centered(str, line)
  808.  term.setCursorPos(w/2 - #str/2, line)
  809.  term.write(str)
  810. end
  811.  
  812.  
  813. function main()
  814.  clear()
  815.  term.setTextColor(fancyColor)
  816.  centered(version,1)
  817.  centered("PLAY",6)
  818.  centered("EDITOR",9)
  819.  centered("ADD", 12)
  820.  centered("DELETE",15)
  821.  centered("QUIT",18)
  822.  centered("by UNOBTANIUM",26)
  823.  term.setTextColor(colors.white)
  824.  local event = { os.pullEvent("monitor_touch") }
  825.  if event[4] >= 6 and event[4] <= 7 then
  826.   if selectLevel(false, "Play") then
  827.    again = play()
  828.   end
  829.  elseif event[4] >= 9 and event[4] <= 10 then
  830.   if selectLevel(true, "Editor") then
  831.    levelEditor()
  832.   end
  833.  elseif event[4] >= 12 and event[4] <= 13 then
  834.   pastebinImport()
  835.  elseif event[4] >= 15 and event[4] <= 16 then
  836.   if selectLevel(false, "Delete") then
  837.    deleteLevel()
  838.   end
  839.  elseif event[4] >= 18 and event[4] <= 19 then
  840.   clear()
  841.   return false
  842.  elseif event[4] == 26 then
  843.   if fancy then
  844.    fancyColor = colors.white
  845.   else
  846.    fancyColor = colors.orange
  847.   end
  848.   fancy = not fancy
  849.  end
  850.  return true
  851. end
  852.  
  853.  
  854. -- RUN
  855.  
  856. if not term.isColor() then print("Use an advanced computer and monitor!") sleep(2) term.clearScreen() term.setCursorPos(1,1) return end
  857. while main() do end
  858. shell.run("reboot")
Advertisement
Add Comment
Please, Sign In to add comment