Advertisement
blackrabt

Turtle 3x3 w torch

Mar 2nd, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.66 KB | None | 0 0
  1. distance = 25 --turtle will go twice this far
  2. torchCounter = 3
  3. turtleName = os.getComputerLabel()
  4. ownerTabletId = 2 --what is the computer # of the owner's PDA where messages will be sent.
  5.  
  6.  
  7.  
  8. --open wireless if available. modem is on the left arm.
  9.     if peripheral.isPresent("left") == true then
  10.       local wireless = peripheral.wrap("left")
  11.       --wireless.open(4)
  12.       rednet.open("left")
  13.       wireless.open(65535)
  14.         else
  15.                 print("no modem available")
  16.     end
  17.  
  18. --turtle movement functions
  19.  
  20. function moveForward()
  21.         while turtle.detect() == true do
  22.                 turtle.dig()
  23.                 os.sleep(0.5)
  24.                 --print("there is something in the way")
  25.                 --return to top of loop here.
  26.                 restartLoop = 1
  27.         --else
  28.                 --turtle.forward()
  29.                 --restartLoop = 0 --this is junk but might do the job until i make a better way
  30.         end
  31.         --print("the function is actually running")
  32.         turtle.forward()
  33.         while turtle.detectUp() do
  34.                 turtle.digUp()
  35.                 os.sleep(0.5)
  36.         end
  37.         turtle.digDown()
  38. end
  39.  
  40. --turtle inventory dump module
  41. --all slots will be dumped to the inventory in front of the turtle
  42. -- i = 1
  43. -- while i < 17 do
  44. --      slot = 1
  45. --      turtle.select(slot)
  46. --      turtle.drop()
  47. --      i = (i + 1)
  48. -- end
  49.  
  50. --pick up all items from normal size chest
  51. --turtle will not be able to hold it all in one trip
  52. --i = 1
  53. -- while i < 27 do
  54. --      turtle.suck()
  55. --      os.sleep(0.25)
  56. --      i = (i + 1)
  57. -- end
  58.  
  59.  
  60.  
  61. function moveUp()
  62.         while turtle.detectUp() == true do
  63.                 turtle.digUp()
  64.                 os.sleep(0.5)
  65.                 --return to top and check again
  66.         --else
  67.                 --turtle.up()
  68.         end
  69. end
  70.  
  71. function moveDown()
  72.         while turtle.detectDown() == true do
  73.                 turtle.digDown()
  74.                 os.sleep(0.5)
  75.                 --return to top
  76.         --else
  77.                 --turtle.down()
  78.         end
  79. end
  80.  
  81. function moveLeft() --not sure I like how this function works. the facing needs of a particular program may change
  82.         turtle.turnLeft()
  83.         moveForward()
  84. end
  85.  
  86.  
  87. --turn right then forward one. then dig up and down. then turn right again and dig up and down. then return to start. to turn twice then forward then left then forward then right.
  88.  
  89. function rightSide()
  90.         turtle.turnRight()
  91.         moveForward() --displaced one space right
  92.         turtle.digUp()
  93.         turtle.digDown()
  94.         turtle.turnRight()
  95.         moveForward() --now -1x, -1y
  96.         turtle.digUp()
  97.         turtle.digDown()
  98.         turtle.turnRight()
  99.         turtle.turnRight()
  100.         moveForward() --now -1x, 0y
  101.         turtle.turnLeft()
  102.         moveForward()
  103.         turtle.turnRight()--now facing the same was as when we started
  104. end
  105.  
  106. function leftSide()
  107.         turtle.turnLeft()
  108.         moveForward() --displaced one space left
  109.         turtle.digUp()
  110.         turtle.digDown()
  111.         turtle.turnLeft()
  112.         moveForward() --now +1x, +1y
  113.         turtle.digUp()
  114.         turtle.digDown()
  115.         turtle.turnLeft()
  116.         turtle.turnLeft()
  117.         moveForward() --now +1x, 0y
  118.         turtle.turnRight()
  119.         moveForward()
  120.         turtle.turnLeft()--now facing the same was as when we started
  121. end
  122.  
  123. --refuel module
  124. --checks the turtle inventory for compatible fuel and uses it
  125. function refuel()
  126.         limit = turtle.getFuelLimit()
  127.         level = turtle.getFuelLevel()
  128.         room = (limit) - (level)
  129.         i = 1
  130.         while i < 17 do
  131.                 slot = i
  132.                 turtle.select(slot)
  133.                 if room > 5000 then
  134.                                 --i feel like something belongs here
  135.                         turtle.refuel()
  136.                         --i = (i + 1)
  137.                         print(turtle.getFuelLevel)
  138.                         --if i = 16 then
  139.                 end
  140.                 if i == 16 then
  141.                         print(turtle.getFuelLevel())
  142.                 end
  143.         i = (i + 1)
  144.         end
  145. end
  146.  
  147. function checkFuel()
  148.         checkLevel = turtle.getFuelLevel()
  149.         if checkLevel < 500 then
  150.                 refuel()
  151.         end
  152.         if checkLevel < 250 then
  153.                 rednet.send(ownerTabletId, turtleName.." is running low on fuel.") --ad message protocol?
  154.                 --location data when available.
  155.         end
  156.         --if no fuel found return to start? or notify PDA
  157. end
  158.  
  159. function detectReturnBlockage()
  160.     if turtle.detect() then
  161.         waiting(timerId) = os.startTimer(20)
  162.         os.sleep(2)
  163.     end
  164.     while turtle.detect() do
  165.         os.sleep(1)
  166.     end
  167.     if os.pullEvent("waiting(timerId") then
  168.         rednet.send(2, turtleName.."here. Something is blocking my return!")  --add coordinates
  169.     end
  170. end
  171.  
  172.  
  173.  
  174.  
  175.  
  176. function returnToSender()
  177.         d=0
  178.         turtle.turnRight()
  179.         turtle.turnRight()--now facing 180
  180.         while d < distance do
  181.                 turtle.forward() --use the simple forward to save time and not break any work happening behind the turtle
  182.                 detectReturnBlockage()
  183.                 turtle.forward() --it moves 2 spaces ahead so it needs to move 2 spaces back per distance counter
  184.                 detectReturnBlockage()
  185.                 d = d + 1
  186.         end
  187. end
  188.  
  189. --torch placement function
  190. --designate slot for torches
  191. --divide i or d by 7 or 8 and place each time it divides evenly or reset the counter when it hits 7
  192. function torch()
  193.         if torchCounter == 6 then
  194.                 turtle.select(1)
  195.                 turtle.placeDown()
  196.                 torchCounter = 1
  197.         else
  198.                 torchCounter = torchCounter + 1
  199.         end
  200. end
  201.  
  202. function dig()
  203.         refuel()
  204.         i=1
  205.         while i < distance do
  206.                 moveForward()
  207.                 moveForward()
  208.                 rightSide()
  209.                 leftSide()
  210.                 i = i+1
  211.                 torch()
  212.                 print(torchCounter)
  213.                 checkFuel()
  214.         end
  215.  
  216. end
  217.  
  218. function workComplete()
  219.         rednet.send(ownerTabletId, turtleName.." has completed its task. Ready for work!")
  220. end
  221.  
  222. --lets do this
  223. dig()
  224. returnToSender()
  225. workComplete()
  226. --send my pda a message when work is done
  227. --send one if contact is lost?
  228.  
  229. -- i want something to say go X distance and check for full inventory, if full go back to the chest adn dump the inv,
  230. --then return to previous until total requested distance is completed and go back to start]]--
  231.  
  232. --this particular program should dig a 3x3 hole to whatever distance I desire and put the contents in a chest.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement