ksaw000

startup replacement

Feb 1st, 2021 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.01 KB | None | 0 0
  1. --[[Program for the turtle to execute on startup]]
  2. FIRST_FREE_SLOT = 4 --marks the first slot in the inventory
  3. -- here is the breakdown of slots : {1:lava,2:empty buckets,3:items chest,4 to 16 : everything else}
  4. TOTAL_SLOTS = 16
  5.  
  6. HOME_X,HOME_Y,HOME_Z = gps.locate()     --uses the pre-existing gps hosts to get position through trilateration
  7. HEADING = 0          
  8. STEPS = 0  --count for total steeps since last refuel
  9.  
  10. function forward()
  11.     turtle.forward()
  12.     STEPS=STEPS+1
  13. end
  14.  
  15. function turnLeft()
  16.     HEADING = (HEADING-1)%4
  17.     turtle.turnLeft()
  18.   end
  19.  
  20. function turnRight()
  21.     HEADING = (HEADING+1)%4
  22.     turtle.turnRight()
  23. end
  24.  
  25. function turnAround()
  26.     turnRight()
  27.     turnRight()
  28. end
  29.  
  30. function checkFuel()
  31.     if turtle.getFuelLevel()<=STEPS then
  32.         invDump(FIRST_FREE_SLOT) --make sure invetory is empty to accept potential items
  33.         turnAround()
  34.         while (turtle.detect()) do --makes space for chests even if it has to dig mulitiple blocks
  35.             turtle.dig()
  36.         end
  37.         turtle.select(1) -- select lava enderchest
  38.         turtle.place()
  39.         turtle.select(16) -- last slot specially for fuel
  40.         turtle.suck() --get lava bucket
  41.         turtle.refuel()
  42.         turtle.select(1) -- lavachest slot
  43.         turtle.dig()
  44.         turtle.select(2) -- bucket chest slot
  45.         turtle.place()
  46.         turtle.select(16) --empty bucket
  47.         turtle.drop() --puts bucket in enderchest
  48.         turtle.select(2)
  49.         turtle.dig()
  50.         turnAround()
  51.         print(turtle.getFuelLevel())
  52.     end
  53. end
  54.  
  55. function move(value)
  56.     checkFuel()
  57.     for i=value,0,-1 do
  58.         turtle.dig()
  59.         forward()
  60.         print(gps.locate())
  61.     end
  62. end
  63.  
  64. function moveVertical(value)
  65.     if value < 0 then
  66.         for i=math.abs(value),1,-1 do
  67.             turtle.digDown()
  68.             turtle.down()
  69.             STEPS=STEPS+1
  70.         end
  71.     else
  72.         for i=value,1,-1 do
  73.             turtle.digUp()
  74.             turtle.up()
  75.             STEPS=STEPS+1
  76.         end
  77.     end
  78. end
  79.  
  80. function makeHeading(target)
  81.     while target ~= HEADING do
  82.         if target>HEADING then
  83.             turnRight()
  84.         elseif target < HEADING then
  85.             turnLeft()
  86.         end
  87.     end
  88. end
  89.  
  90. function getOrientation()
  91.     loc1 = vector.new(gps.locate(2, false))
  92.     if not turtle.forward() then
  93.         for j=1,6 do
  94.             if not turtle.forward() then
  95.                 turtle.dig()
  96.             else
  97.                 break
  98.             end
  99.         end
  100.     end
  101.     loc2 = vector.new(gps.locate(2, false))
  102.     local heading = loc2 - loc1
  103.     return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))-1
  104.     end
  105.    
  106.     --[[orientation will be:
  107.     -x = 0  (WEST)
  108.     -z = 1  (NORTH)
  109.     +x = 2  (EAST)
  110.     +z = 3  (SOUTH)
  111.     --]]
  112.  
  113.  
  114.  
  115. function invDump(first_slot)
  116.     turnAround()
  117.     while (turtle.detect()) do --makes space for chests even if it has to dig mulitiple blocks
  118.         turtle.dig()
  119.     end
  120.     turtle.select(3)
  121.     turtle.place()
  122.     for i=TOTAL_SLOTS,first_slot,-1 do
  123.         turtle.select(i)
  124.         turtle.drop()
  125.     end
  126.     turtle.select(3)
  127.     turtle.dig()
  128.     turnAround()
  129. end
  130.  
  131. function moveTo(X,Y,Z)
  132.     local currX,currY,currZ=gps.locate()
  133.    
  134.     if(currX==X and currY==Y and currZ==Z) then
  135.         print("Already here!")
  136.         return
  137.  
  138.     else
  139.         while currX~=X or currY~=Y or currZ~=Z do
  140.             local currX,currY,currZ=gps.locate()
  141.             --translate in the X direction
  142.             if currX~=X then
  143.                 if X>currX and HEADING ~= 2 then
  144.                     makeHeading(2)
  145.                 elseif X<currX and HEADING ~=0 then
  146.                     makeHeading(0)
  147.                 end
  148.                 move(math.abs(X-currX))
  149.             end
  150.  
  151.             --translate in the Z direction
  152.             if currZ~=Z then
  153.                 if Z>currZ and HEADING ~= 3 then
  154.                     makeHeading(3)
  155.                 elseif Z<currZ and HEADING ~=1 then
  156.                     makeHeading(1)
  157.                 end
  158.                 move(math.abs(Z-currZ))
  159.             end
  160.             --translate in the Y direction
  161.             if currY~=Y then
  162.                 moveVertical(Y-currY)
  163.             end
  164.             print("done")
  165.         end
  166.     end
  167. end
  168.  
  169. --[[
  170.     The main loop of the turtle's functionality. It will start and then wait for a command via
  171.     rednet broadcast
  172. ]]
  173.  
  174. rednet.open("right") -- open the wireless modem for communication
  175.  while true do -- puts turtle into a waitloop for a message
  176.     HEADING = getOrientation()
  177.     local id,message = rednet.receive()
  178.     command = {}
  179.     for word in message:gmatch("%w+") do table.insert(command, word) end
  180.     if command[1]=="exit" then
  181.         break
  182.     elseif command[1] =="dump" then
  183.         invDump(FIRST_FREE_SLOT)
  184.     elseif command[1]=="checkFuel" then
  185.         checkFuel()
  186.     elseif command[1]=="moveTo" then
  187.         moveTo(tonumber(command[2]),tonumber(command[3]),tonumber(command[4]))
  188.     end
  189. end
Add Comment
Please, Sign In to add comment