Advertisement
exploder2013

ExAPI

Aug 6th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.08 KB | None | 0 0
  1. -- Variables
  2. turtle = {}
  3. dist = {}
  4. timer1 = os.startTimer(1)
  5. i= 0
  6. turtles = 0
  7.  
  8. function getModemSide()
  9.   local sides = {"left","right","top","front","back","bottom"}
  10.   for i=1,6 do
  11.     if peripheral.isPresent(sides[i]) then
  12.       if peripheral.getType(sides[i]) == "modem" then
  13.         rednet.open(sides[i])
  14.         return true
  15.       end
  16.     end
  17.   end
  18.   return false
  19. end
  20.  
  21. function clearScreen()
  22.   term.clear()
  23.   term.setCursorPos(1,1)
  24. end
  25.  
  26. function drawInputBox(name)
  27.   term.setBackgroundColor(colors.blue)
  28.   for i=1,7 do
  29.     writeCenter("                                ",math.floor(h/2-4+i))
  30.   end
  31.   term.setTextColor(colors.red)
  32.   writeCenter(name,h/2-4+1)
  33.   term.setBackgroundColor(128)
  34.   writeCenter("                            ",h/2-4+4)
  35.   term.setCursorPos(w-40,h/2-4+4)
  36.   term.setTextColor(1)
  37.   input = read()
  38.   return input
  39. end
  40.  
  41. function writeCenter(text,y)
  42.   local w,h = term.getSize()
  43.   if y == nil or y == 0 then
  44.     y = h + 1
  45.   end
  46.   term.setCursorPos(math.floor(w-#text)/2,y)
  47.   term.write(text)
  48. end
  49.  
  50. function writeLeft(text,spaces,y)
  51.   term.setCursorPos(1+spaces,y)
  52.   term.write(text)
  53. end
  54.  
  55. function writeRight(text,spaces,y)
  56.   local w,h = term.getSize()
  57.   term.setCursorPos(w-#text-spaces,y)
  58.   term.write(text)
  59. end
  60.  
  61. function makeConfig()
  62.   fs.makeDir("data/config")
  63.   file = fs.open("data/config/config","w")
  64.   file.writeLine("Exploder's OS")
  65.   file.writeLine("1")
  66.   file.writeLine("1")
  67.   file.writeLine("0")
  68.   file.close()
  69. end
  70.  
  71. function makeTurtleConfig()
  72.   fs.makeDir("data/config")
  73.   file = fs.open("data/config/turtleID","w")
  74.  
  75.   file.close()
  76. end
  77.  
  78. function editConfig(numb,value)
  79.   curcfg = readConfig("all")
  80.   curcfg[numb] = tostring(value)
  81.  
  82.   file = fs.open("data/config/config","w")
  83.   for k,v in pairs(curcfg) do
  84.     file.writeLine(curcfg[k])
  85.   end
  86.   file.close()
  87. end
  88.  
  89. function editTurtleConfig(numb,value)
  90.   curcfg = readTurtleConfig("all")
  91.   file = fs.open("data/config/turtleID","w")
  92.   for k,v in pairs(turtle) do
  93.     file.writeLine(tostring(turtle[k]))
  94.   end
  95.   file.close()
  96. end
  97.  
  98. function readTurtleConfig(numb)
  99.   file = fs.open("data/config/turtleID","r")
  100.   if file then
  101.     curcfg = {}
  102.     line = file.readLine()
  103.     while line do
  104.       table.insert(curcfg,line)
  105.       line = file.readLine()
  106.     end
  107.       file.close()
  108.   else
  109.     makeTurtleConfig()
  110.     readTurtleConfig(numb)
  111.   end
  112.   if numb == "all" then
  113.     return curcfg
  114.   else
  115.     return curcfg[numb]
  116.   end
  117. end
  118.  
  119. function readConfig(numb)
  120.   file = fs.open("data/config/config","r")
  121.   if file then
  122.     curcfg = {}
  123.     line = file.readLine()
  124.     while line do
  125.       table.insert(curcfg,line)
  126.       line = file.readLine()
  127.     end
  128.       file.close()
  129.   else
  130.     makeConfig()
  131.     readConfig(numb)
  132.   end
  133.   if numb == "all" then
  134.     return curcfg
  135.   else
  136.     return curcfg[numb]
  137.   end
  138. end
  139.  
  140. function randomBackground(numb)
  141.   bin = numb
  142.   if tonumber(bin) > 2 then
  143.     bin = math.random(1,tonumber(bin)-1)
  144.   else
  145.     bin = 1
  146.   end
  147.   return bin
  148. end
  149.  
  150. function showInfo(numb)
  151.   local w,h = term.getSize()
  152.   local turtle = readTurtleConfig(numb)
  153.   term.setBackgroundColor(colors.cyan)
  154.   clearScreen()
  155.   term.setBackgroundColor(colors.orange)
  156.   for i=1, h-4 do
  157.     term.setCursorPos(2,2+i)
  158.     write("                                                 ")
  159.   end
  160.   for i=1,49 do
  161.     term.setCursorPos(1+i,3)
  162.     term.write("-")
  163.     term.setCursorPos(1+i,h-2)
  164.     term.write("-")
  165.     term.setCursorPos(1+i,5)
  166.     term.write("-")
  167.     term.setCursorPos(1+i,7)
  168.     term.write("-")
  169.     term.setCursorPos(1+i,9)
  170.     term.write("-")
  171.     term.setCursorPos(1+i,h-6)
  172.     term.write("-")
  173.     term.setCursorPos(1+i,h-4)
  174.     term.write("-")
  175.   end
  176.   for i=1,h-4 do
  177.     term.setCursorPos(2,2+i)
  178.     term.write(":")
  179.     term.setCursorPos(50,2+i)
  180.     term.write(":")
  181.   end
  182.   writeCenter("[Back]",h-3)
  183.   writeCenter("Turtle ID: "..turtle.." control panel",4)
  184.   term.setTextColor(colors.blue)
  185.   writeLeft("[Start Turtle]",6,h-5)
  186.   term.setTextColor(colors.red)
  187.   writeRight("[Stop Turtle]",6,h-5)
  188.   term.setTextColor(colors.white)
  189.   -- Getting information from the turtle.
  190.     while true do
  191.       rednet.send(tonumber(turtle),"info")
  192.       if tonumber(readConfig(4)) == 1 then
  193.         break
  194.       end
  195.       event, p1,p2,p3,p4 = os.pullEvent()
  196.       if event == "mouse_click" then
  197.         if p2 >= 22 and p2 <= 27 and p3 == 16 and p1 == 1 then
  198.           turtleControl()
  199.         end
  200.       elseif event == "rednet_message" and p1 == tonumber(turtle) then
  201.         info = textutils.unserialize(p2)
  202.         writeLeft("Fuel Level: ",4,6)
  203.         writeLeft("Distance Forward: ",4,8)
  204.         writeRight("Depth: ",4,6)
  205.         writeRight("Distance: ",4,8)
  206.         writeCenter("Messages",h-9)
  207.        
  208.         if type(info) == "table" then
  209.             if tonumber(info[1]) >= 1000 then term.setTextColor(colors.green)
  210.             elseif tonumber(info[1]) < 1000 then term.setTextColor(colors.red)
  211.             end
  212.        
  213.             writeLeft(tostring(info[1]),16,6)
  214.             writeLeft(tostring(info[3]),22,8)
  215.            
  216.             if tonumber(info[1]) < 1185 then
  217.                 term.setTextColor(colors.red)
  218.                 writeCenter("Warning, fuel is too low for one full run",h-8)
  219.             end        
  220.  
  221.             if tonumber(info[2]) >= 50 then term.setTextColor(colors.red)
  222.             elseif tonumber(info[2]) < 50 then term.setTextColor(colors.green)
  223.             end
  224.        
  225.             writeRight(tostring(info[2]),3,6)
  226.             writeRight(tostring(math.floor(p3)),3,8)
  227.             term.setTextColor(colors.white)
  228.  
  229.             if tonumber(info[3]) >= 1 then
  230.                 editConfig(4,1)
  231.             end
  232.             break
  233.         end
  234.       end
  235.     end
  236.    
  237.   -- Main Loop
  238.   while true do
  239.     rednet.send(tonumber(turtle),"info")
  240.     event,button, X, Y = os.pullEvent()
  241.     if event == "mouse_click" then
  242.       if X >= 22 and X <= 27 and Y == 16 and button == 1 then
  243.         turtleControl()
  244.       elseif X >= 7 and X <= 20 and Y == 14 and button == 1 then
  245.         rednet.send(tonumber(turtle),"startMine")
  246.         editConfig(4,1)
  247.       elseif X >= 32 and X <= 44 and Y == 14 and button == 1 then
  248.         rednet.send(tonumber(turtle),"stopMine")
  249.         editConfig(4,0)
  250.       end
  251.     elseif event == "rednet_message" and tonumber(readConfig(4)) == 1 then
  252.       if button == tonumber(turtle) then
  253.         info = textutils.unserialize(X)
  254.  
  255.         writeLeft("Fuel Level: ",4,6)
  256.         writeLeft("Distance Forward: ",4,8)
  257.         writeRight("Depth: ",4,6)
  258.         writeRight("Distance: ",4,8)
  259.         writeCenter("Messages",h-9)
  260.        
  261.         if type(info) == "table" then
  262.        
  263.             if tonumber(info[1]) >= 1000 then term.setTextColor(colors.green)
  264.             elseif tonumber(info[1]) < 1000 then term.setTextColor(colors.red)
  265.             end
  266.        
  267.             writeLeft(tostring(info[1]),16,6)
  268.             writeLeft(tostring(info[3]),22,8)
  269.            
  270.             if tonumber(info[1]) < 1185 then
  271.                 term.setTextColor(colors.red)
  272.                 writeCenter("Warning, fuel is too low for one full run",h-8)
  273.             end
  274.                
  275.             if tonumber(info[2]) >= 50 then term.setTextColor(colors.red)
  276.             elseif tonumber(info[2]) < 50 then term.setTextColor(colors.green)
  277.             end
  278.        
  279.             writeRight(tostring(info[2]),3,6)
  280.             writeRight(tostring(math.floor(Y)),3,8)
  281.             term.setTextColor(colors.white)
  282.  
  283.         end
  284.     elseif button == tonumber(turtle) and X == "finish" then
  285.         editConfig(4,0)
  286.       end
  287.     end
  288.   end
  289. end
  290.  
  291. function turtleScan()
  292.   local w,h = term.getSize()
  293.   term.setBackgroundColor(colors.cyan)
  294.   clearScreen()
  295.   term.setBackgroundColor(colors.orange)
  296.   for i=1, h-4 do
  297.     term.setCursorPos(2,2+i)
  298.     write("                                                 ")
  299.   end
  300.   for i=1,49 do
  301.     term.setCursorPos(1+i,3)
  302.     term.write("-")
  303.     term.setCursorPos(1+i,h-2)
  304.     term.write("-")
  305.     term.setCursorPos(1+i,5)
  306.     term.write("-")
  307.     term.setCursorPos(1+i,h-4)
  308.     term.write("-")
  309.     term.setCursorPos(1+i,7)
  310.     term.write("-")
  311.   end
  312.   for i=1,h-4 do
  313.     term.setCursorPos(2,2+i)
  314.     term.write(":")
  315.     term.setCursorPos(50,2+i)
  316.     term.write(":")
  317.   end
  318.   writeCenter("Turtle scanner",4)
  319.   writeLeft("[Save & Back]",5,h-3)
  320.   writeRight("[Back]",5,h-3)
  321.   writeCenter("Turtles Found: "..turtles,6)
  322.   if turtles >= 1 then
  323.     for k,v in pairs(turtle) do
  324.       writeCenter("TurtleID: "..turtle[k].." Distance: "..dist[k],7+k)
  325.     end
  326.   end
  327.   while true do
  328.     local event, button, X, Y = os.pullEvent()
  329.     if event == "mouse_click" then
  330.       if X >= 6 and X <= 18 and Y == 16 then
  331.         for k,v in pairs(turtle) do
  332.           editTurtleConfig(k,v)
  333.         end
  334.         os.reboot()
  335.       elseif X >= w-11 and X <= w-6 and Y == 16 then
  336.         os.reboot()
  337.       end
  338.     elseif event == "rednet_message" then
  339.       if X == "ExMR" then
  340.         if turtles >= 1 then
  341.           for k,v in pairs(turtle) do
  342.             if turtle[k] == button then
  343.               turtleScan()
  344.             end
  345.           end
  346.         end
  347.        
  348.         turtles = turtles + 1
  349.         table.insert(turtle,button)
  350.         table.insert(dist,math.floor(Y))
  351.         turtleScan()
  352.       end
  353.     end
  354.   end
  355. end
  356.  
  357. function turtleControl()
  358.   local w,h = term.getSize()
  359.   term.setBackgroundColor(colors.cyan)
  360.   clearScreen()
  361.   term.setBackgroundColor(colors.orange)
  362.   for i=1, h-4 do
  363.     term.setCursorPos(2,2+i)
  364.     write("                                                 ")
  365.   end
  366.   for i=1,49 do
  367.     term.setCursorPos(1+i,3)
  368.     term.write("-")
  369.     term.setCursorPos(1+i,h-2)
  370.     term.write("-")
  371.     term.setCursorPos(1+i,5)
  372.     term.write("-")
  373.   end
  374.   for i=1,h-4 do
  375.     term.setCursorPos(2,2+i)
  376.     term.write(":")
  377.     term.setCursorPos(50,2+i)
  378.     term.write(":")
  379.   end
  380.   curcfg = readTurtleConfig("all")
  381.  
  382.   writeCenter("Turtle Control Panel",4)
  383.   writeCenter("[Back]",h-3)
  384.   for i=1,#curcfg do
  385.     writeCenter("[Turtle number: "..i.." ID: "..curcfg[i].."]",5+i)
  386.   end
  387.   if #curcfg < 1 then
  388.     writeCenter("[No turtles are stored.]",10)
  389.   end
  390.  
  391.   while true do
  392.     event, button, X, Y = os.pullEvent()
  393.     if event == "mouse_click" then
  394.       if X >= 22 and X <= 27 and Y == 16 and button == 1 then
  395.         os.reboot()
  396.       elseif X >= 13 and X <= 36 and Y == 6 and button == 1 and #curcfg >= 1 then
  397.         showInfo(1)
  398.       elseif X >= 13 and X <= 36 and Y == 7 and button == 1 and #curcfg >= 2 then
  399.         showInfo(2)
  400.       elseif X >= 13 and X <= 36 and Y == 8 and button == 1 and #curcfg >= 3 then
  401.         showInfo(3)
  402.       elseif X >= 13 and X <= 36 and Y == 9 and button == 1 and #curcfg >= 4 then
  403.         showInfo(4)
  404.       elseif X >= 13 and X <= 36 and Y == 10 and button == 1 and #curcfg >= 5 then
  405.         showInfo(5)
  406.       elseif X >= 13 and X <= 36 and Y == 11 and button == 1 and #curcfg >= 6 then
  407.         showInfo(6)
  408.       end
  409.     end
  410.   end
  411. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement