Abahu

Remote

Dec 25th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.27 KB | None | 0 0
  1. m = {} --Coordinate arrays: [level][x][y] 0 is solid, 1 is clear, 2 is unknown, any higher is a turtle
  2. u = {} --Array for loading
  3. am = {} --Array for coordinates of options
  4. lid=0
  5. cid=0
  6. cx=32 --Cursor position
  7. cy=32
  8. lvl=1
  9. done=true
  10. menu=0
  11. option=1
  12. mo = {} --Number of options for each menu
  13. tm=0
  14. material = 1
  15. name = ""
  16. shut = 0
  17. change = 0
  18. db = 0
  19.  
  20. function ia() --Initialise our coordinate arrays
  21.     for i=1, 12 do
  22.         m[i] = {}
  23.         for j=1, 64 do
  24.             m[i][j] = {}
  25.             for k=1, 64 do
  26.                 m[i][j][k] = 0
  27.             end
  28.         end
  29.     end
  30.     for i=1,49152 do
  31.         u[i] = 0
  32.     end
  33.     for i=1,3 do
  34.         am[i] = {}
  35.         for j=3,47 do
  36.             am[i][j] = {}
  37.             for k=2,3 do
  38.                 am[i][j][k] = 0
  39.             end
  40.         end
  41.     end
  42.     for i=4,13 do
  43.         am[1][i][2] = 1
  44.     end
  45.     for i=16,25 do
  46.         am[1][i][2] = 2
  47.     end
  48.     for i=28,36 do
  49.         am[1][i][2] = 3
  50.     end
  51.     for i=39,43 do
  52.         am[1][i][2] = 4
  53.     end
  54.     for i=4,8 do
  55.         am[1][i][3] = 5
  56.     end
  57.     for i=3,6 do
  58.         am[2][i][2] = 1
  59.     end
  60.     for i=8,11 do
  61.         am[2][i][2] = 2
  62.     end
  63.     for i=13,16 do
  64.         am[2][i][2] = 3
  65.     end
  66.     for i=18,21 do
  67.         am[2][i][2] = 4
  68.     end
  69.     for i=18,21 do
  70.         am[2][i][2] = 5
  71.     end
  72.     for i=23,26 do
  73.         am[2][i][2] = 6
  74.     end
  75.     for i=28,31 do
  76.         am[2][i][2] = 7
  77.     end
  78.     for i=33,36 do
  79.         am[2][i][2] = 8
  80.     end
  81.     for i=38,42 do
  82.         am[2][i][2] = 9
  83.     end
  84.     for i=12,13 do
  85.         am[3][i][2] = 1
  86.     end
  87.     for i=37,39 do
  88.         am[3][i][2] = 2
  89.     end
  90.     if fs.exists("disk/builder/maps/") == false then
  91.         fs.makeDir("disk/builder/maps/")
  92.     end
  93.     mo[1] = 6 --main menu
  94.     mo[2] = 2 --quit
  95.     mo[5] = 2 --save before quit
  96.     mo[6] = 2 --cancel without saving
  97. end
  98. function cu(id, l, xv, yv) --Sends the value of a coordinate
  99.     local j = "7:"..tostring(l)..":"..tostring(xv)..":"..tostring(yv)..":"..tostring(m[l][xv][yv])..":;"
  100.     rednet.send(id, j)
  101. end
  102. function du()
  103.     term.clear()
  104.     term.setCursorPos(1,1)
  105.     if menu == 5 then
  106.         term.write("======================Save?=======================")
  107.     elseif menu == 6 then
  108.         term.write("=====================Cancel?======================")
  109.     else
  110.         term.write("==================================================")
  111.     end
  112.     term.setCursorPos(1,3)
  113.     term.write("==================================================")
  114.     for xv = 1, 50 do
  115.         for yv = 4, 18 do
  116.             term.setCursorPos(xv,yv)
  117.             if cx+xv-25 >= 1 and cy+yv-9 >= 1 and cx+xv-25 <= 64 and cy+yv-9 <= 64 then
  118.                 cm = m[lvl][cx+xv-25][cy+yv-9]
  119.                 if cm ~= 0 then
  120.                     term.write(tostring(cm))
  121.                 else
  122.                     term.write(" ")
  123.                 end
  124.             else
  125.                 term.write("!")
  126.             end
  127.         end
  128.     end
  129.     if cid == 0 then
  130.         term.setCursorPos(25,9)
  131.         term.write("x")
  132.     end
  133.     if menu == 0 then
  134.         term.setCursorPos(34,2)
  135.         term.write("x: "..tostring(cx))
  136.         term.setCursorPos(41,2)
  137.         term.write("z: "..tostring(cy))
  138.     end
  139.     term.setCursorPos(1,19)
  140.     if db == 1 then
  141.         term.write("File doesn't exist")
  142.     end
  143.         if menu == 0 then
  144.         term.setCursorPos(1,2)
  145.         term.write("| Layer: ")
  146.         term.setCursorPos(10,2)
  147.         term.write(tostring(lvl))
  148.         term.setCursorPos(15,2)
  149.         term.write("material: "..tostring(material))
  150.         term.setCursorPos(50,2)
  151.         term.write("|")
  152.     elseif menu == 1 then
  153.         term.setCursorPos(1,2)
  154.         term.write("|    Load   Save   Send   Help   Quit   Exit     |")
  155.         term.setCursorPos(-2+7*option,2)
  156.         term.write(">")
  157.     elseif menu == 2 or menu == 5 or menu == 6 then
  158.         term.setCursorPos(1,2)
  159.         term.write("|          No                       Yes          |")
  160.         if option == 1 then
  161.             term.setCursorPos(11,2)
  162.         else
  163.             term.setCursorPos(36,2)
  164.         end
  165.         term.write(">")
  166.     elseif menu == 3 then
  167.         term.setCursorPos(1,2)
  168.         term.write("| File Name:                                     |")
  169.         term.setCursorPos(14,2)
  170.         term.setCursorBlink(true)
  171.         load(read())
  172.         term.setCursorBlink(false)
  173.         menu=0
  174.         option=1
  175.         du()
  176.     elseif menu == 4 then
  177.         term.setCursorPos(1,2)
  178.         term.write("| Project Name:                                  |")
  179.         term.setCursorPos(17,2)
  180.         term.setCursorBlink(true)
  181.         term.setCursorBlink(false)
  182.         name = read()
  183.         if name ~= "" then
  184.             save()
  185.             if shut == 1 then
  186.                 tm=1
  187.             end
  188.             menu = 0
  189.         else
  190.             menu = 6
  191.         end
  192.         du()
  193.         option=1
  194.         menu=0
  195.     end
  196.    
  197. end
  198. function leer(m,l)
  199.     local i = 1
  200.     local p = ""
  201.     repeat
  202.         repeat
  203.             if m:sub(l,l) ~= ":" then
  204.                 p=p..m:sub(l,l)
  205.                 l=l+1
  206.             end
  207.         until m:sub(l,l) == ":"
  208.         u[i] = tonumber(p)
  209.         p=""
  210.         l=l+1
  211.         i=i+1
  212.     until m:sub(l,l) == ";"
  213.    
  214. end
  215. function save()
  216.     local file = fs.open("disk/builder/maps/"..name, "w")
  217.     for i=1,12 do
  218.         for xv=1,64 do
  219.             for yv=1,64 do
  220.                 if m[i][xv][yv] ~= 0 then
  221.                     file.write(tostring(i)..":"..tostring(xv)..":"..tostring(yv)..":"..tostring(m[i][xv][yv]..":"))
  222.                 end
  223.             end
  224.         end
  225.     end
  226.     file.write(";")
  227.     file.close()
  228. end
  229. function load(f)
  230.     if f ~= "" then
  231.         if fs.exists("disk/builder/maps/" .. f) then
  232.             ia()
  233.             local file = fs.open("disk/builder/maps/" .. f, "r")
  234.             local data = file.readAll()
  235.             file.close()
  236.             if data ~= "" and data ~= ";" then
  237.                 leer(data,1)
  238.                 local j=0
  239.                 repeat
  240.                     if u[1+4*j] ~= 0 then
  241.                         m[u[1+4*j]][u[2+4*j]][u[3+4*j]]=u[4+4*j]
  242.                         u[1+4*j], u[2+4*j], u[3+4*j], u[4+4*j]=0,0,0,0
  243.                     end
  244.                     j=j+1
  245.                     term.setCursorPos(1,19)
  246.                     term.write(tostring(j))
  247.                 until u[1+4*j] == 0
  248.             end
  249.         else
  250.             db = 1
  251.         end
  252.         name = f
  253.     end
  254. end
  255. function kr()
  256.     if mo[menu] >= option + 1 then
  257.         option = option + 1
  258.         du()
  259.     end
  260. end
  261. function kl()
  262.     if option ~= 1 then
  263.         option = option-1
  264.         du()
  265.     end
  266. end
  267. function enter()
  268.     if menu == 1 then
  269.         if option == 1 then
  270.             menu=3
  271.         elseif option == 2 then
  272.             option=1
  273.             if name == "" then
  274.                 menu=4
  275.             else
  276.                 save()
  277.             end
  278.         elseif option == 5 then
  279.             option=1
  280.             menu=2
  281.         elseif option == 6 then
  282.             option=1
  283.             menu=0
  284.         end
  285.     elseif menu == 2 then
  286.         if option == 1 then
  287.             option=1
  288.             menu=1
  289.         else
  290.             if change == 1 then
  291.                 option=1
  292.                 menu=5
  293.             else
  294.                 option = 1
  295.                 tm = 1
  296.                 menu = 0
  297.             end
  298.         end
  299.     elseif menu == 5 then
  300.         if option == 2 then
  301.             if name == "" then
  302.                 menu=4
  303.                 shut=1
  304.             else
  305.                 save()
  306.             end
  307.         else
  308.             option=1
  309.             tm=1
  310.             menu=0
  311.         end
  312.     elseif menu == 6 then
  313.         if option == 1 then
  314.             menu = 4
  315.         else
  316.             option = 1
  317.             menu = 0
  318.         end
  319.     end
  320. end
  321.  
  322. if fs.isDir("disk") == false then
  323.     print("Please insert a disk.")
  324.     local event, side = os.pullEvent("disk")
  325. end
  326. term.clear()
  327. term.setCursorBlink(false)
  328. ia()
  329. du()
  330. repeat
  331.     local event, a, b, c = os.pullEvent()
  332.     if event == "key" then
  333.         if a == 1 and menu ~= 0 then
  334.             option = 1
  335.             menu = 0
  336.             du()
  337.         end
  338.         if a == 200 and menu == 0 then
  339.             if cy ~= 1 then
  340.                 cy = cy-1
  341.                 du()
  342.             end
  343.         end
  344.         if a == 208 and menu == 0 then
  345.             if cy ~= 64 then
  346.                 cy =cy+1
  347.                 du()
  348.             end
  349.         end
  350.         if a == 205 then
  351.             if menu ~= 0 then
  352.                 kr()
  353.             elseif cx ~= 64 then
  354.                 cx=cx+1
  355.                 du()
  356.             end
  357.         end
  358.         if a == 203 then
  359.             if menu ~= 0 then
  360.                 kl()
  361.             elseif cx ~= 1 then
  362.                 cx=cx-1
  363.                 du()
  364.             end
  365.         end
  366.         if a == 14 and menu == 0 then
  367.             m[lvl][cx][cy] = 0
  368.             change = 1
  369.             du()
  370.         end
  371.         if a == 24 and menu == 0 then
  372.             cx = 1
  373.             cy = 1
  374.             du()
  375.         end
  376.         if a == 28 then
  377.             if menu == 0 then
  378.                 menu = 1
  379.             else
  380.                 enter()
  381.             end
  382.             du()
  383.         end
  384.         if a == 57 and menu == 0 then
  385.             m[lvl][cx][cy] = material
  386.             change = 1
  387.             du()
  388.         end
  389.         if a == 12 and lvl ~= 1 and menu == 0 then
  390.             lvl=lvl-1
  391.             du()
  392.         end
  393.         if a == 207 and menu == 0 then
  394.             tm = 1
  395.         end
  396.         if a == 13 and lvl ~= 12 and menu == 0 then
  397.             lvl=lvl+1
  398.             du()
  399.         end
  400.         if a >= 2 and a <= 10 and menu == 0 then
  401.             material=a-1
  402.             du()
  403.         end
  404.     end
  405.     if event == "mouse_drag" and menu == 0 then
  406.         if cx+b-25 >= 1 and cx+b-25 <= 64 and cy+c-9 >= 1 and cy+c-9 <= 64 then
  407.             if a == 1 then
  408.                 m[lvl][cx+b-25][cy+c-9] = material
  409.             else
  410.                 m[lvl][cx+b-25][cy+c-9] = 0
  411.             end
  412.             change = 1
  413.             du()
  414.         end
  415.     end
  416.     if event == "mouse_click" and menu == 0 then
  417.         if cx+b-25 >= 1 and cx+b-25 <= 64 and cy+c-9 >= 1 and cy+c-9 <= 64 then
  418.             if a == 1 then
  419.                 m[lvl][cx+b-25][cy+c-9] = material
  420.             else
  421.                 m[lvl][cx+b-25][cy+c-9] = 0
  422.             end
  423.             change = 1
  424.             du()
  425.         end
  426.     end
  427.     if event == "mouse_scroll" and menu == 0 then
  428.         if material + a ~= 0 and material + a ~= 10 then
  429.             material = material + a
  430.         end
  431.         du()
  432.     end
  433.     if event == "disk_eject" then
  434.         term.setCursorPos(1,19)
  435.         term.write("Please enter disk.")
  436.         local event, side = os.pullEvent("disk")
  437.         du()
  438.     end
  439. until tm == 1
  440. term.setCursorPos(1,1)
  441. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment