Advertisement
exploder2013

ComputerCraft Turtle OS

Aug 11th, 2013
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.93 KB | None | 0 0
  1. -- This code is recommended to use together with ComputerCraft Miner OS!
  2. -- ComputerCraft Miner OS pastebin is YN6pE8gW!
  3.  
  4. -- Turtle Start-up
  5. if not os.loadAPI("data/ExAPI") then
  6.     term.setCursorPos(1,1)
  7.     print("ExAPI is missing!")
  8.     print("You need to have ExAPI in data folder")
  9.     print("ExAPI pastebin is PTK4hM5g")
  10.     print("If you have pastebin enabled type:")
  11.     print("pastebin get PTK4hM5g data/ExAPI")
  12.     error()
  13. end
  14. if not term.isColor() then
  15.   print("Program requires an advanced mining turtle.")
  16.   return
  17. end
  18. sleep(1)
  19. Eif not ExAPI.getModemSide() then
  20.   ExAPI.clearScreen()
  21.   ExAPI.writeCenter("Please add a modem to this computer",math.floor(h/2))
  22.   term.setCursorPos(1,1)
  23.   error()
  24. end
  25.  
  26. -- Tables
  27. startMenu = {
  28.   "               ",
  29.   " Start Mining  ",  
  30.   " Reset Configs ",
  31.   "    Reboot     ",
  32.   "   Shutdown    ",
  33.   "               "
  34. }
  35. contextMenu = {
  36.   "           ",
  37.   "   Reboot  ",
  38.   "    Exit   ",
  39.   "           "
  40. }
  41. -- Variables
  42. w,h = term.getSize()
  43. osName = tostring(ExAPI.readConfig(1))
  44. clockTimer = os.startTimer(1)
  45. src = 0
  46. computerID = 0
  47. -- Functions
  48. -- Turtle Functions
  49.  
  50. function readConfig(numb)
  51.   file = fs.open("data/config/cords","r")
  52.   if file then
  53.     curcfg = {}
  54.     line = file.readLine()
  55.     while line do
  56.       table.insert(curcfg,line)
  57.       line = file.readLine()
  58.     end
  59.       file.close()
  60.   else
  61.     defaultConfig()
  62.     readConfig(numb)
  63.   end
  64.   if numb == "all" then
  65.     return curcfg
  66.   else
  67.     return curcfg[numb]
  68.   end
  69. end
  70.  
  71. function checkFuel()
  72.     if turtle.getFuelLevel() < 20 then
  73.         for i=1,16 do
  74.             turtle.select(i)
  75.             turtle.refuel()
  76.         end
  77.     end
  78. end
  79.  
  80. function editConfig(numb,value)
  81.   curcfg = readConfig("all")
  82.   curcfg[numb] = tostring(value)
  83.  
  84.   file = fs.open("data/config/cords","w")
  85.   for k,v in pairs(curcfg) do
  86.     file.writeLine(curcfg[k])
  87.   end
  88.   file.close()
  89. end
  90.  
  91. function sendInformation()
  92.       info = {}
  93.       fuel = turtle.getFuelLevel()
  94.       table.insert(info,fuel)
  95.       table.insert(info,readConfig(2))
  96.       table.insert(info,readConfig(3))
  97.       info = textutils.serialize(info)
  98.       rednet.send(computerID,info)
  99. end
  100.  
  101. function defaultConfig(times)
  102.     file = fs.open("data/config/cords","w")
  103.     file.writeLine(tostring(1)) -- X
  104.     file.writeLine(tostring(0)) -- Y
  105.     file.writeLine(tostring(0)) -- Back
  106.     file.writeLine("forward") -- Facing
  107.     file.writeLine(tostring(0)) -- Is digging downwards?
  108.     file.writeLine(tostring(15)) -- How much times to loop?
  109.     file.close()
  110. end
  111.  
  112.  
  113.  
  114. function goUp(info)
  115.     checkFuel()
  116.     while tonumber(info[2]) > 0 do
  117.         info[2] = info[2] -1
  118.         editConfig(2,info[2])
  119.         turtle.digUp()
  120.         turtle.up()
  121.         editConfig(2,info[2])
  122.         sendInformation()
  123.     end
  124. end
  125.  
  126. function turnLeft(info)
  127.     editConfig(4,"left")
  128.     turtle.turnLeft()
  129.     editConfig(4,"left")
  130.     turtle.dig()
  131.     editConfig(4,"forward")
  132.     turtle.turnRight()
  133.     editConfig(4,"forward")
  134. end
  135.  
  136. function checkSide(info)
  137.     if info[4] == "left" then
  138.         turtle.dig()
  139.         editConfig(4,"forward")
  140.         turtle.turnRight()
  141.         editConfig(4,"forward")
  142.     elseif info[4] == "right" then
  143.         turtle.dig()
  144.         editConfig(4,"forward")
  145.         turtle.turnLeft()      
  146.         editConfig(4,"forward")
  147.     end
  148. end
  149.  
  150. function turnRight(info)
  151.     editConfig(4,"right")
  152.     turtle.turnRight()
  153.     editConfig(4,"right")
  154.     turtle.dig()
  155.     editConfig(4,"forward")
  156.     turtle.turnLeft()
  157.     editConfig(4,"forward")
  158. end
  159.  
  160. function goDown(info)
  161.     checkFuel()
  162.     turtle.digDown()
  163.     editConfig(5,1)
  164.     while turtle.down() do
  165.         info[2] = info[2] + 1
  166.         editConfig(2,info[2])
  167.         turtle.digDown()
  168.         sendInformation()
  169.         editConfig(2,info[2])
  170.         turnLeft(info)
  171.         turnRight(info)
  172.     end
  173.     editConfig(5,0)
  174. end
  175.  
  176. function goForward(info)
  177.     checkFuel()
  178.     info[1] = info[1] + 1
  179.     editConfig(1,info[1])
  180.     for i=1,tonumber(info[1]) do
  181.         editConfig(3,info[3])
  182.         sendInformation()
  183.         turtle.dig()
  184.         info[3] = info[3] + 1
  185.         editConfig(3,info[3])
  186.         turtle.forward()
  187.         editConfig(3,info[3])
  188.     end
  189. end
  190.  
  191. function goBack(info)
  192.     checkFuel()
  193.     repeat
  194.         info[3] = info[3] - 1
  195.         editConfig(3,info[3])
  196.         turtle.back()
  197.         editConfig(3,info[3])
  198.         sendInformation()
  199.     until info[3] <= 0
  200.     turtle.dropDown()
  201. end
  202.  
  203. -- Computer Functions
  204.  
  205. function startMine(numb)
  206.     for i=1,tonumber(numb) do
  207.         editConfig(6,15-i)
  208.         sendInformation()
  209.         contents = readConfig("all")
  210.         goForward(contents)
  211.         sendInformation()
  212.         contents = readConfig("all")
  213.         goDown(contents)
  214.         sendInformation()
  215.         contents = readConfig("all")
  216.         goUp(contents)
  217.         sendInformation()
  218.         contents = readConfig("all")
  219.         goBack(contents)
  220.         if i >= 15 then
  221.             defaultConfig()
  222.             rednet.send(computerID,"finish")
  223.             return
  224.         end
  225.     end
  226. end
  227.  
  228. -- Safe Check
  229. content = readConfig("all")
  230. if tonumber(content[1]) > 1 or tonumber(content[2]) > 0 then
  231.     checkSide(content)
  232.     if tonumber(readConfig(5)) == 1 then
  233.         goDown(content)
  234.     end
  235.     goUp(content)
  236.     goBack(content)
  237.     turnsLeft = tonumber(readConfig(6))
  238.     startMine(turnsLeft)
  239. end
  240.  
  241.   function drawDesktop()
  242.     background = paintutils.loadImage("data/backgrounds/.background1")
  243.     paintutils.drawImage(background,1,1)
  244.     ExAPI.clearScreen()
  245.     drawTitle()
  246.     drawBottom()
  247.   end
  248.  
  249. function drawExtMenu(contextX,contextY)
  250.   term.setBackgroundColor(128)
  251.   for k,v in pairs(contextMenu) do
  252.     term.setCursorPos(contextX,contextY - 1 + k)
  253.     term.write(tostring(v))
  254.   end
  255. end
  256.  
  257.   function drawTitle()
  258.     term.setCursorPos(1,1)
  259.     term.setBackgroundColor(colors.blue)
  260.     term.clearLine()
  261.     ExAPI.writeCenter(osName,1)
  262.   end
  263.  
  264. function drawBottom()
  265.   term.setBackgroundColor(colors.green)
  266.   for i=1,2 do
  267.     term.setCursorPos(1,h-2+i)
  268.     term.clearLine()
  269.   end
  270.   term.setBackgroundColor(colors.blue)
  271.   ExAPI.writeLeft(" [Start] ",0,h-1)
  272.   ExAPI.writeLeft("         ",0,h)
  273. end
  274.  
  275. function drawMenu()
  276.   term.setBackgroundColor(128)
  277.   for k,v in pairs(startMenu) do
  278.     ExAPI.writeLeft(tostring(v),0,h-#startMenu-2+k)
  279.   end
  280. end
  281.  
  282. function drawExtMenu(contextX,contextY)
  283.   term.setBackgroundColor(128)
  284.   for k,v in pairs(contextMenu) do
  285.     term.setCursorPos(contextX,contextY - 1 + k)
  286.     term.write(tostring(v))
  287.   end
  288. end
  289.  
  290. function displayTime()
  291.     term.setBackgroundColor(colors.green)
  292.     time = textutils.formatTime(os.time(),true)
  293.     ExAPI.writeRight(time,2,h-1)
  294. end
  295.  
  296. -- Main Loop
  297. drawDesktop()
  298. displayTime()
  299. while true do
  300.   event, button, X, Y = os.pullEvent()
  301.   if event == "rednet_message" and X ~= "ExMR" then
  302.       computerID = button
  303.       sendInformation()
  304.   if X == "startMine" then
  305.       sendInformation()
  306.       startMine(15)
  307.   end
  308.   elseif event == "mouse_click" then
  309.     if src == 0 then
  310.       if X >= 1 and X <= 15 and Y >= h-1 and button == 1 then
  311.         src = 1
  312.         drawMenu()
  313.       elseif X >= 1 and Y >= 2 and button == 2 then
  314.         src = 2
  315.         if X >= w - 11 then
  316.             contextX = w - 11
  317.         end
  318.         if X >= h - 7 then
  319.             contextY = h - 6
  320.         end
  321.         if X <= w - 11 then
  322.             contextX = X
  323.         end
  324.         if Y <= h - 4 then
  325.             contextY = Y
  326.         end
  327.         drawExtMenu(contextX,contextY)
  328.       end
  329.     elseif src == 1 then
  330.       if X >= 1 and X <= 15 and Y == h-3 and button == 1 then
  331.         src = 0
  332.         os.shutdown()
  333.       elseif X >= 1 and X <= 15 and Y == h-4 and button == 1 then
  334.         src = 0
  335.         os.reboot()
  336.       elseif X >= 1 and X <= 15 and Y == h-5 and button == 1 then
  337.         src = 0
  338.         defaultConfig()
  339.         drawDesktop()
  340.         displayTime()  
  341.       elseif X >= 1 and X <= 15 and Y == h-6 and button == 1 then
  342.         src = 0
  343.         drawDesktop()
  344.         displayTime()  
  345.         sendInformation()
  346.         startMine(15)      
  347.       else
  348.         src = 0
  349.         drawDesktop()
  350.         displayTime()
  351.       end
  352.     elseif src == 2 then
  353.       if X >= contextX and X <= contextX + 10 and Y == contextY + 1 and button == 1 then
  354.         src = 0
  355.         os.reboot()
  356.       elseif X >= contextX and X <= contextX + 10 and Y == contextY + 2 and button == 1 then
  357.         src = 0
  358.         term.setBackgroundColor(colors.black)
  359.         ExAPI.clearScreen()
  360.         error()
  361.       else
  362.         src = 0
  363.         drawDesktop()
  364.         displayTime()      
  365.       end
  366.     end
  367.     elseif event == "timer" then
  368.       if button == clockTimer then
  369.         rednet.broadcast("ExMR")
  370.         displayTime()
  371.         clockTimer = os.startTimer(1)
  372.       end
  373.   end
  374. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement