Abahu

Turtle Builder

Mar 28th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.83 KB | None | 0 0
  1. m = {} --Coordinate arrays: [level][x][y] 1-9 (material types)
  2. am = {} --Array for coordinates of options
  3. mo = {6,2,0,0,2,2,0}
  4. chest = {}
  5. manifest = {} --Names for material types
  6. usedMats = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  7. matChar = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
  8. start = {0,0,0}
  9. dir = 0
  10.  
  11. cx=1 --Cursor position
  12. cy=1
  13. px=1 --Mouse cursor click position
  14. py=11
  15. cursorChar = {"x","^",">","v","<"};
  16. lvl=1
  17. material = 1
  18.  
  19. --Control button mechanism
  20. ctrl = 0;
  21. cPos = {{0,0},{0,0},{0,0}}; --Positions for the control; y1,y2; x1,x2; z1,z2;
  22.  
  23. menu=0
  24. option=1
  25. shut = 0
  26. tm=0
  27.  
  28. change = 0
  29. name = ""
  30. db = 0
  31.  
  32. id = 0
  33. side = ""
  34.  
  35. bound = 96
  36. function ia() --Initialise our arrays
  37.     if not fs.exists("remote/") then fs.makeDir("remote/") end
  38.     for i=1,bound do
  39.         m[i] = {}
  40.         for j=1, bound do
  41.             m[i][j] = {}
  42.             for k=1, bound do
  43.                 m[i][j][k] = 0;
  44.             end
  45.         end
  46.     end
  47.     for i=1,4 do
  48.         am[i] = {}
  49.         for j=1,50 do
  50.             am[i][j] = {}
  51.             for k=1,19 do
  52.                 am[i][j][k] = 0
  53.             end
  54.         end
  55.     end
  56.     for i=1,16 do
  57.         manifest[i] = "minecraft:stone"
  58.     end
  59.     for i=1,3 do
  60.         chest[i] = {}
  61.         for j=1,9 do
  62.             chest[i][j] = 0
  63.         end
  64.     end
  65. end
  66.  
  67. function du()
  68.     term.clear()
  69.     term.setCursorPos(1,1)
  70.     term.write("==================================================")
  71.     term.setCursorPos(1,3)
  72.     if menu == 8 then
  73.             term.setCursorPos(1,18)
  74.     end
  75.     term.write("==================================================")
  76.     for xv = 1, 50 do
  77.         for yv = 4, 18 do
  78.             term.setCursorPos(xv,yv)
  79.             if cx+xv-25 >= 1 and cy+yv-9 >= 1 and cx+xv-25 <= bound and cy+yv-9 <= bound then
  80.                 local cm = m[lvl][cx+xv-25][cy+yv-9]
  81.                 if cm ~= 0 then
  82.                     term.write(matChar[math.floor(cm)])
  83.                 else
  84.                     term.write(" ")
  85.                 end
  86.             else
  87.                 term.write("!")
  88.             end
  89.         end
  90.     end
  91.     if ctrl == 1 then
  92.         term.setCursorPos(50,19);
  93.         term.write("#");
  94.     end
  95.     term.setCursorPos(px-cx+25,py-cy+9);
  96.     term.write(cursorChar[dir+1]);
  97.     term.setCursorPos(1,19)
  98.     if db == 1 then
  99.         term.write("File doesn't exist")
  100.     end
  101.     if menu == 0 then
  102.         term.setCursorPos(1,2)
  103.         term.write("| Layer: "..tostring(lvl))
  104.         term.setCursorPos(14,2)
  105.         term.write("material: "..matChar[material])
  106.         term.setCursorPos(27,2)
  107.         term.write("x: "..tostring(px))
  108.         term.setCursorPos(34,2)
  109.         term.write("z: "..tostring(py))
  110.         term.setCursorPos(41,2)
  111.         term.write("dir: "..tostring(dir))
  112.         term.setCursorPos(50,2)
  113.         term.write("|")
  114.     elseif menu == 1 then
  115.         term.setCursorPos(1,2)
  116.         term.write("|    Load   Save   Send   Help   Quit   Exit     |")
  117.         term.setCursorPos(-2+7*option,2)
  118.         term.write(">")
  119.     elseif menu == 2 or menu == 5 or menu == 6 then
  120.         if menu == 2 then
  121.             term.setCursorPos(23,1)
  122.             term.write("Quit?")
  123.         elseif menu == 5 then
  124.             term.setCursorPos(23,1)
  125.             term.write("Save")
  126.         elseif menu == 6 then
  127.             term.setCursorPos(22,1)
  128.             term.write("Cancel?")
  129.         end
  130.         term.setCursorPos(1,2)
  131.         term.write("|          No                       Yes          |")
  132.         if option == 1 then
  133.             term.setCursorPos(11,2)
  134.         else
  135.             term.setCursorPos(36,2)
  136.         end
  137.         term.write(">")
  138.     elseif menu == 3 then
  139.         term.setCursorPos(1,2)
  140.         term.write("| File Name:                                     |")
  141.         term.setCursorPos(14,2)
  142.         term.setCursorBlink(true)
  143.         load(read())
  144.         term.setCursorBlink(false)
  145.         menu=0
  146.         option=1
  147.         du()
  148.     elseif menu == 4 then
  149.         term.setCursorPos(1,2)
  150.         term.write("| Project Name:                                  |")
  151.         term.setCursorPos(17,2)
  152.         term.setCursorBlink(true)
  153.         name = read()
  154.         if name ~= "" then
  155.             save()
  156.             if shut == 1 then
  157.                 tm=1
  158.             end
  159.             menu = 0
  160.         else
  161.             menu = 6
  162.         end
  163.         term.setCursorBlink(false)
  164.         du()
  165.         option=1
  166.         menu=0
  167.     elseif menu == 7 then
  168.         term.setCursorPos(1,2)
  169.         term.write("| Turtle ID:                                     |")
  170.         term.setCursorPos(14,2)
  171.         term.setCursorBlink(true)
  172.         local tid = read()
  173.         if tid == "$r" then
  174.             send(true)
  175.             menu=0
  176.             option=1
  177.             du()
  178.         else
  179.             if tonumber(tid) then
  180.                 id = tonumber(tid)
  181.                 local derp = ""
  182.                 for i=1,16 do --Get Chest locations
  183.                     if usedMats[i] ~= 0 then
  184.                         for j=1,3 do
  185.                             if j==1 then
  186.                                 derp = "X"
  187.                             elseif j==2 then
  188.                                 derp = "Y"
  189.                             else
  190.                                 derp = "Z"
  191.                             end
  192.                             term.setCursorPos(1,2)
  193.                             term.write("| Chest "..tostring(i).." "..derp..":                                     |")
  194.                             term.setCursorPos(14,2)
  195.                             derp = read() or "0"
  196.                             if derp then
  197.                                 chest[j][i] = tonumber(derp)
  198.                             else
  199.                                 chest[j][i] = 0
  200.                             end
  201.                         end
  202.                     end
  203.                 end
  204.                 for i=1,3 do
  205.                     if i==1 then
  206.                         derp = "X"
  207.                     elseif i==2 then
  208.                         derp = "Y"
  209.                     else
  210.                         derp = "Z"
  211.                     end
  212.                     while not tonumber(derp) do
  213.                         term.setCursorPos(1,2)
  214.                         term.write("| Start Coordinate "..derp..":                                     |")
  215.                         term.setCursorPos(23,2)
  216.                         derp = read()
  217.                     end
  218.                     start[i] = tonumber(derp)
  219.                 end
  220.                 term.setCursorPos(1,19)
  221.                 send()
  222.             end
  223.             menu = 0
  224.             option = 1
  225.             du()
  226.         end
  227.     end
  228. end
  229. function leer(m,l,mode)
  230.     l=l or 1
  231.     mode=mode or 0
  232.     local i = 1
  233.     local p = ""
  234.     local r = {}
  235.     local u = {}
  236.     repeat
  237.         repeat
  238.             if m:sub(l,l) ~= "~" then
  239.                 p=p..m:sub(l,l)
  240.                 l=l+1
  241.             end
  242.         until m:sub(l,l) == "~"
  243.         if mode == 0 then
  244.             u[i] = tonumber(p)
  245.         else
  246.             r[i] = p
  247.         end
  248.         p=""
  249.         l=l+1
  250.         i=i+1
  251.     until m:sub(l,l) == ";"
  252.     if mode == 0 then
  253.         return u
  254.     else
  255.         return r
  256.     end
  257. end
  258.  
  259. function save()
  260. local file = fs.open("remote/"..name..".bdf", "w")
  261. for i=1,bound do
  262.     for xv=1,bound do
  263.         for yv=1,bound do
  264.             if m[i][xv][yv] ~= 0 then
  265.                 local cm = m[i][xv][yv]
  266.                 file.write(tostring(i).."~"..tostring(xv).."~"..tostring(yv).."~"..tostring(cm).."~")
  267.                 usedMats[math.floor(cm)] = usedMats[math.floor(cm)] + 1
  268.             end
  269.         end
  270.     end
  271. end
  272. file.write(";")
  273. file.close()
  274. file = fs.open("remote/"..name..".bmf", "w")
  275. for i=1,16 do
  276.     file.write(tostring(i).."~"..manifest[i].."~"..tostring(usedMats[i]).."~")
  277. end
  278. file.write(";")
  279. file.close()
  280. change = 0
  281. end
  282.  
  283. function send(resend)
  284.     term.setCursorPos(1,19)
  285.     if resend then
  286.         rednet.send(id,"A")
  287.         os.sleep(0.2)
  288.         local file = fs.open("remote/038temp.bdf", "r")
  289.         local message = file.readAll()
  290.         file.close()
  291.         local file = fs.open("remote/038debugger.bmf","r")
  292.         id = tonumber(file.readLine())
  293.         rednet.send(id,message)
  294.         os.sleep(0.2)
  295.         for i=1,3 do
  296.             rednet.send(id,file.readLine())
  297.             os.sleep(0.2)
  298.         end
  299.         file.close()
  300.         term.write("done")
  301.     else
  302.         rednet.send(id,"A")
  303.         term.write("2:")
  304.         os.sleep(0.2)
  305.         local tname = name --Send the map
  306.         local tchange = change
  307.         name = "038temp"
  308.         save()
  309.         local file = fs.open("remote/038temp.bdf","r")
  310.         rednet.send(id,file.readAll())
  311.         file.close()
  312.         name = tname
  313.         change = tchange
  314.         term.write("3:")
  315.         os.sleep(0.2)
  316.        
  317.         local file = fs.open("remote/038debugger.bmf", "w")
  318.         file.writeLine(tostring(id))
  319.         local message = "" --Send the manifest
  320.         for i=1,16 do
  321.             message = message..manifest[i].."~"
  322.         end
  323.         message = message..";"
  324.         file.writeLine(message)
  325.         rednet.send(id, message)
  326.         term.write("4:")
  327.         os.sleep(0.2)
  328.        
  329.         message = "" --Send the chest coordinates
  330.         for i=1,16 do
  331.             if chest[2][i] ~= 0 then
  332.                 message = message..tostring(chest[1][i]).."~"..tostring(chest[2][i]).."~"..tostring(chest[3][i]).."~"
  333.             end
  334.         end
  335.         message = message..";"
  336.         rednet.send(id, message)
  337.         file.writeLine(message)
  338.         term.write("5:")
  339.         os.sleep(0.2)
  340.        
  341.         message = "" --Send the start coordinates
  342.         message = start[1].."~"..start[2].."~"..start[3].."~"..";"
  343.         rednet.send(id,message)
  344.         file.writeLine(message)
  345.         file.close()
  346.         --term.write(message)
  347.         os.sleep(0.2)
  348.     end
  349. end
  350.  
  351. function load(f)
  352.     if f ~= "" then
  353.         if fs.exists("remote/" .. f .. ".bdf") then
  354.             ia()
  355.             local file = fs.open("remote/" .. f .. ".bdf", "r")
  356.             local data = file.readAll()
  357.             file.close()
  358.             if data ~= "" and data ~= ";" then
  359.                 local u = leer(data)
  360.                 local j=0
  361.                 repeat
  362.                     if u[1+4*j] then
  363.                         m[u[1+4*j]][u[2+4*j]][u[3+4*j]]=u[4+4*j]
  364.                     end
  365.                     j=j+1
  366.                 until not u[1+4*j]
  367.             end
  368.             j=0
  369.             file = fs.open("remote/"..f..".bmf", "r")
  370.             data = file.readAll()
  371.             if data ~= "" and data ~= ";" then
  372.                 local r = leer(data,1,1)
  373.                 while r[1+3*j] do
  374.                     manifest[tonumber(r[1+3*j])] = r[2+3*j]
  375.                     usedMats[tonumber(r[1+3*j])] = tonumber(r[3+3*j])
  376.                     j=j+1
  377.                 end
  378.             end
  379.             file.close();
  380.         else
  381.             db = 1
  382.         end
  383.         name = f
  384.     end
  385. end
  386. function kr()
  387.     if mo[menu] >= option + 1 then
  388.         option = option + 1
  389.         du()
  390.     end
  391. end
  392. function kl()
  393.     if option ~= 1 then
  394.         option = option-1
  395.         du()
  396.     end
  397. end
  398. function enter()
  399.     if menu == 1 then
  400.         if option == 1 then
  401.             menu=3
  402.         elseif option == 2 then
  403.             option=1
  404.             if name == "" then
  405.                 menu=4
  406.             else
  407.                 save()
  408.             end
  409.         elseif option == 3 then
  410.             option=1
  411.             menu=7
  412.         elseif option == 4 then
  413.             option=1
  414.             menu=8
  415.         elseif option == 5 then
  416.             option=1
  417.             menu=2
  418.         elseif option == 6 then
  419.             option=1
  420.             menu=0
  421.         end
  422.     elseif menu == 2 then
  423.         if option == 1 then
  424.             option=1
  425.             menu=1
  426.         else
  427.             if change == 1 then
  428.                 option=1
  429.                 menu=5
  430.             else
  431.                 option = 1
  432.                 tm = 1
  433.                 menu = 0
  434.             end
  435.         end
  436.     elseif menu == 5 then
  437.         if option == 2 then
  438.             if name == "" then
  439.                 menu=4
  440.                 shut=1
  441.             else
  442.                 save()
  443.                 menu=0
  444.                 option=1
  445.                 tm=1
  446.             end
  447.         else
  448.             option=1
  449.             tm=1
  450.             menu=0
  451.         end
  452.     elseif menu == 6 then
  453.         if option == 1 then
  454.             menu = 4
  455.         else
  456.             option = 1
  457.             menu = 0
  458.         end
  459.     end
  460. end
  461.  
  462. function mapPress(x,y,z,del) --del = 0 for not delete
  463.     del = del or 0
  464.     del = 1 - del;
  465.     if x >= 1 and x <= bound and z >= 1 and z <= bound then
  466.         if ctrl == 0 then
  467.             m[y][x][z] = (material + (dir/10))*del;
  468.             change = 1;
  469.         else
  470.             if cPos[1][1] == 0 then
  471.                 cPos[1][1], cPos[2][1], cPos[3][1] = y, x, z;
  472.             else
  473.                 cPos[1][2], cPos[2][2], cPos[3][2] = y, x, z;
  474.                 for i = math.min(cPos[1][1],cPos[1][2]),math.max(cPos[1][1],cPos[1][2]) do --y
  475.                     for j = math.min(cPos[2][1],cPos[2][2]),math.max(cPos[2][1],cPos[2][2]) do --x
  476.                         for k = math.min(cPos[3][1],cPos[3][2]),math.max(cPos[3][1],cPos[3][2]) do --z
  477.                             m[i][j][k] = (material + (dir/10))*del;
  478.                         end
  479.                     end
  480.                 end
  481.                 cPos = {{0,0},{0,0},{0,0}};
  482.                 change = 1;
  483.             end
  484.         end
  485.     end
  486. end
  487.  
  488. if peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
  489.     rednet.open("right")
  490.     side = "right"
  491. elseif peripheral.isPresent("back") and peripheral.getType("back") == "modem" then
  492.     rednet.open("back")
  493.     side = "back"
  494. elseif peripheral.isPresent("top") and peripheral.getType("top") == "modem" then
  495.     rednet.open("top")
  496.     side = "top"
  497. elseif peripheral.isPresent("left") and peripheral.getType("left") == "modem" then
  498.     rednet.open("left")
  499.     side = "left"
  500. else
  501.   print("Please attach a wireless modem")
  502.   os.sleep(5)
  503.   term.clear()
  504.   term.setCursorPos(1,1)
  505.   tm = 1
  506. end
  507.  
  508. term.clear()
  509. term.setCursorBlink(false)
  510. ia()
  511. du()
  512. while tm ~= 1 do
  513.     local event, a, b, c = os.pullEvent()
  514.     if event == "key" then
  515.         if a == 1 and menu ~= 0 then
  516.             option = 1
  517.             menu = 0
  518.         end
  519.         if a == 200 and menu == 0 then
  520.             if cy ~= 1 then
  521.                 cy = cy-1
  522.                 py=cy
  523.             end
  524.         end
  525.         if a == 208 and menu == 0 then
  526.             if cy ~= bound then
  527.                 cy =cy+1
  528.                 py=cy
  529.             end
  530.         end
  531.         if a == 205 then
  532.             if menu ~= 0 then
  533.                 kr()
  534.             elseif cx ~= bound then
  535.                 cx=cx+1
  536.                 px=cx
  537.             end
  538.         end
  539.         if a == 203 then
  540.             if menu ~= 0 then
  541.                 kl()
  542.             elseif cx ~= 1 then
  543.                 cx=cx-1
  544.                 px=cx
  545.             end
  546.         end
  547.         if a == 14 and menu == 0 then
  548.             mapPress(cx,lvl,cy,1);
  549.         end
  550.         if a == 24 and menu == 0 then
  551.             cx, cy = 1, 1;
  552.             px, py = 1, 1;
  553.         end
  554.         if a == 28 then
  555.             if menu == 0 then
  556.                 menu = 1
  557.             else
  558.                 enter()
  559.             end
  560.         end
  561.         if a == 57 and menu == 0 then
  562.             mapPress(cx,lvl,cy);
  563.         end
  564.         if a == 12 and lvl ~= 1 and menu == 0 then
  565.             lvl=lvl-1
  566.         end
  567.         if a == 207 and menu == 0 then
  568.             tm = 1
  569.         end
  570.         if a == 13 and lvl ~= bound and menu == 0 then
  571.             lvl=lvl+1
  572.         end
  573.         if a == 2  and menu == 0  then
  574.             material=material-1;
  575.             if material <= 0 then material = 16-material end
  576.         end
  577.         if a == 3 and menu == 0  then
  578.             material=(material) % 16 + 1;
  579.         end
  580.         if a == 74 then
  581.             dir = dir - 1
  582.             if dir < 0 then dir = 5 + dir end
  583.         end
  584.         if a == 78 then
  585.             dir = (dir + 1) % 5;
  586.         end
  587.         if a == 29 then
  588.             ctrl = 1 - ctrl;
  589.             cPos = {{0,0},{0,0},{0,0}};
  590.         end
  591.         du()
  592.     end
  593.     if event == "mouse_click" then
  594.         if menu ~= 0 then
  595.             --if am[menu][a][b] ~= 0 then
  596.             --  option = am[menu][a][b]
  597.             --  enter()
  598.             --end
  599.         else
  600.             local xv = cx-25+b;
  601.             local yv = cy-9+c;
  602.             px=xv;
  603.             py=yv;
  604.             mapPress(xv,lvl,yv,a-1);
  605.             du();
  606.         end
  607.     end
  608.     if event == "mouse_drag" then
  609.         if menu == 0 then
  610.             local xv = cx-25+b;
  611.             local yv = cy-9+c;
  612.             px=xv;
  613.             py=yv;
  614.             mapPress(xv,lvl,yv,a-1);
  615.             du();
  616.         end
  617.     end
  618.     if menu == 99 then
  619.         tm = 1
  620.     end
  621. end
  622. rednet.close(side)
  623. term.setCursorPos(1,1)
  624. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment