ksaw000

prototype3

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