blackrabt

TurtleImprovedv1

Mar 4th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.03 KB | None | 0 0
  1.    
  2.  
  3.     distance = 4 --turtle with go twice this far
  4.     torchCounter = 3 --jump start first torch placement
  5.     turtleName = os.getComputerLabel()
  6.     ownerTabletId = 2 --what is the computer # of the owner's PDA where messages will be sent.
  7.      
  8.      
  9.      
  10.     --open wireless if available. modem is on the left arm.
  11.         if peripheral.isPresent("right") == true then
  12.           local wireless = peripheral.wrap("right")
  13.           --wireless.open(4)
  14.           rednet.open("right")
  15.           wireless.open(65535)
  16.             else
  17.                     print("no modem available")
  18.         end
  19.      
  20.     --turtle movement functions
  21.      
  22.     function moveForward()
  23.             while turtle.detect() == true do
  24.                     turtle.dig()
  25.                     os.sleep(0.5)
  26.                     --print("there is something in the way")
  27.                     --return to top of loop here.
  28.                     restartLoop = 1
  29.             --else
  30.                     --turtle.forward()
  31.                     --restartLoop = 0 --this is junk but might do the job until i make a better way
  32.             end
  33.             --print("the function is actually running")
  34.             turtle.forward()
  35.             while turtle.detectUp() do
  36.                     turtle.digUp()
  37.                     os.sleep(0.5)
  38.             end
  39.             turtle.digDown()
  40.     end
  41.      
  42.     --turtle inventory dump module
  43.     --all slots will be dumped to the inventory in front of the turtle
  44.     -- i = 1
  45.     -- while i < 17 do
  46.     --      slot = 1
  47.     --      turtle.select(slot)
  48.     --      turtle.drop()
  49.     --      i = (i + 1)
  50.     -- end
  51.      
  52.     --pick up all items from normal size chest
  53.     --turtle will not be able to hold it all in one trip
  54.     --i = 1
  55.     -- while i < 27 do
  56.     --      turtle.suck()
  57.     --      os.sleep(0.25)
  58.     --      i = (i + 1)
  59.     -- end
  60.      
  61.      
  62.      
  63.     function moveUp()
  64.             while turtle.detectUp() == true do
  65.                     turtle.digUp()
  66.                     os.sleep(0.5)
  67.                     --return to top and check again
  68.             --else
  69.                     --turtle.up()
  70.             end
  71.     end
  72.      
  73.     function moveDown()
  74.             while turtle.detectDown() == true do
  75.                     turtle.digDown()
  76.                     os.sleep(0.5)
  77.                     --return to top
  78.             --else
  79.                     --turtle.down()
  80.             end
  81.     end
  82.      
  83.     function moveLeft() --not sure I like how this function works. the facing needs of a particular program may change
  84.             turtle.turnLeft()
  85.             moveForward()
  86.     end
  87.      
  88.      
  89.     --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.
  90.      
  91.     function rightSide()
  92.         turtle.turnRight()
  93.         moveForward() --displaced one space right
  94.         turtle.digUp()
  95.         turtle.digDown()
  96.         turtle.turnRight()
  97.         moveForward() --now -1x, -1y
  98.         turtle.digUp()
  99.         turtle.digDown()
  100.         turtle.turnRight()
  101.         turtle.turnRight()
  102.         moveForward() --now -1x, 0y
  103.         turtle.turnLeft()
  104.         moveForward()
  105.         turtle.turnRight()--now facing the same was as when we started
  106.     end
  107.      
  108.     function leftSide()
  109.         turtle.turnLeft()
  110.         moveForward() --displaced one space left
  111.         turtle.digUp()
  112.         turtle.digDown()
  113.         turtle.turnLeft()
  114.         moveForward() --now +1x, +1y
  115.         turtle.digUp()
  116.         turtle.digDown()
  117.         turtle.turnLeft()
  118.         turtle.turnLeft()
  119.         moveForward() --now +1x, 0y
  120.         turtle.turnRight()
  121.         moveForward()
  122.         turtle.turnLeft()--now facing the same was as when we started
  123.     end
  124.      
  125.     --refuel module
  126.     --checks the turtle inventory for compatible fuel and uses it
  127.     function refuel()
  128.         limit = turtle.getFuelLimit()
  129.         level = turtle.getFuelLevel()
  130.         room = (limit) - (level)
  131.         s = 1
  132.         while s < 17 do
  133.             slot = s
  134.             turtle.select(slot)
  135.             if room > 5000 then
  136.                 --i feel like something belongs here
  137.                 turtle.refuel()
  138.                 --i = (i + 1)
  139.                 print(turtle.getFuelLevel)
  140.                 --if i = 16 then
  141.             end
  142.             if s == 16 then
  143.                 print(turtle.getFuelLevel())
  144.             end
  145.         s = (s + 1)
  146.         end
  147.     end
  148.      
  149.     function checkFuel()
  150.         checkLevel = turtle.getFuelLevel()
  151.         if checkLevel < 500 then
  152.             refuel()
  153.         end
  154.         if checkLevel < 250 and managerNotified ~= true then
  155.             rednet.send(ownerTabletId, turtleName.." is running low on fuel.") --ad message protocol?
  156.             --location data when available. this sends every few seconds when normally running. find a way to make that happen LESS
  157.             managerNotified = true
  158.         end
  159.         --if no fuel found return to start? or notify PDA
  160.         checkLevel = turtle.getFuelLevel()
  161.         if checkLevel > 500 then
  162.             managerNotified = false
  163.         end
  164.     end
  165.      
  166.     -- function detectReturnBlockage()
  167.     --     location = {}
  168.     --     location["first"] = (shell.run("gp locate")) --keys 1,2,3 to v x y z?
  169.     --     print(location["first"])
  170.     --     rednet.send(2, turtleName.."here. Something is blocking my return! I am at "..shell.run("gps locate") --pull the result, not the whole damn thing.  --add coordinates use them to tell if it is moving or not
  171.     --     end
  172.     -- end
  173.      
  174.     -- function detectReturnBlockage()
  175.     --     local firstLocation = vector.new(gps.locate())
  176.     --     turtle.forward()
  177.     --     local secondLocation = vector.new(gps.locate())
  178.     --     local movement = secondLocation:sub(firstLocation)
  179.     --     if movement < 1 then
  180.     --     rednet.send(2, turtleName.."here. Something is blocking my return! I am at "..(gps.locate()) --pull the result, not the whole damn thing.  --add coordinates use them to tell if it is moving or not
  181.     --     end
  182.     -- end
  183.     function workComplete()
  184.             rednet.send(ownerTabletId, turtleName.." has completed its task. Ready for work!")
  185.     end
  186.      
  187.     function startPoint()
  188.        --startLocation = vector.new(gps.locate())
  189.        
  190.        startX, startY, startZ = (gps.locate(5))
  191.        print(startX, startY, startZ) --still only printing one number
  192.        
  193.     end
  194.      
  195.     function properReturn()
  196.           --firstLocation = vector.new(gps.locate())
  197.     --     turtle.forward()
  198.          local finishX, finishY, finishZ = (gps.locate(5))
  199.          if tonumber(startX) ~= tonumber(finishX) or tonumber(startY) ~= tonumber(finishY) or tonumber(startZ) ~= tonumber(finishZ) then
  200.             rednet.send(2, "I did not make it all the way back to start.")
  201.         else
  202.             workComplete()
  203.         end
  204.     end
  205.      
  206.      
  207.     function checkInventoryState()
  208.         invSlot = 1
  209.         counter = 0
  210.         invFull = false
  211.         while invSlot < 17 do
  212.             slot = invSlot
  213.             turtle.select(slot)
  214.             contents = 0
  215.             contents = turtle.getItemCount()
  216.             invSlot=invSlot+1
  217.             if contents > 0 then
  218.                 counter = counter + 1
  219.             end
  220.         end
  221.         if counter > 15 then
  222.             invFull = true
  223.         end
  224.     end
  225.      
  226.      
  227.      
  228.     function dumpToChest()
  229.         if invFull then
  230.             turtle.select(2)
  231.             turtle.placeDown()
  232.             i = 3
  233.             while i < 17 do
  234.                 slot = i
  235.                 turtle.select(slot)
  236.                 turtle.dropDown()
  237.                 i=i+1
  238.             end
  239.             os.sleep(150)
  240.         end
  241.         turtle.select(2)
  242.         turtle.digDown()
  243.         turtle.suckUp()
  244.     end
  245.         --if INV is full then select chest and place it then dump inv, then pick chest back up with proper slot selected
  246.      
  247.     function returnToSender()
  248.             d=1
  249.             turtle.turnRight()
  250.             turtle.turnRight()--now facing 180
  251.             while d < distance do
  252.                 turtle.forward() --use the simple forward to save time and not break any work happening behind the turtle
  253.                 --detectReturnBlockage()
  254.                 turtle.forward() --it moves 2 spaces ahead so it needs to move 2 spaces back per distance counter
  255.                 --detectReturnBlockage()
  256.                 d = d + 1
  257.             end
  258.     end
  259.      
  260.     --torch placement function
  261.     --designate slot for torches
  262.     --divide i or d by 7 or 8 and place each time it divides evenly or reset the counter when it hits 7
  263.     function torch()
  264.             if torchCounter == 4 then
  265.                     turtle.select(1)
  266.                     turtle.placeDown()
  267.                     torchCounter = 1
  268.             else
  269.                     torchCounter = torchCounter + 1
  270.             end
  271.     end
  272.      
  273.     function dig() --reused variables are messing up the counts for travel distance. and the vector isnt working.
  274.             refuel()
  275.             travel=1
  276.             while travel < distance do
  277.                     moveForward()
  278.                     moveForward()
  279.                     rightSide()
  280.                     leftSide()
  281.                     checkInventoryState()
  282.                     dumpToChest()
  283.                     travel = travel+1
  284.                     torch()
  285.                     print(torchCounter)
  286.                     checkFuel()
  287.             end
  288.      
  289.     end
  290.      
  291.      
  292.     --lets do this
  293.     startPoint()
  294.     dig()
  295.     returnToSender()
  296.     --properReturn()
  297.     workComplete()
  298.     --send my pda a message when work is done
  299.     --send one if contact is lost?
  300.      
  301.     -- i want something to say go X distance and check for full inventory, if full go back to the chest adn dump the inv,
  302.     --then return to previous until total requested distance is completed and go back to start]]--
  303.      
  304.     --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