Advertisement
UNOBTANIUM

Planetz Under Gravity Computer

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