codingbutter

placeToTarget

Aug 3rd, 2021 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.72 KB | None | 0 0
  1. --[[
  2.     for chunky turtles with pickaxes
  3.  
  4.     Description: This turtle will lay blocks from its current location to a starting location
  5.    
  6.     Usage:[all parameters are required]
  7.         placeToTarget currentX currentY currentZ targetX targetY targetZ depth
  8.    
  9.     Directions: The turtle uses reletive position to track its current position,
  10.     It must be placed facing 0,0,0 on the z meaning if you are south of 0,0,0 it must face north
  11.     but if you are north of 0,0,0 you must face south.
  12.    
  13.     you must load a fuel source in slot 1 and have enough fuel to make the trip to the target
  14.     you must load the block(including wires or redstone ex..) into slots 5-8
  15.    
  16.     Notes: if your server crashes while the turtle is working you can find the
  17.     position within the world/computercraft/computers/computerId/scriptDirectory/turtle
  18.    
  19.     @TODO
  20.         * Auto resume if after server reboot
  21.         * Communicate Wirelessly (will require a pair of turtles)
  22.         * Start the turtle wirelessly
  23.         * create a gui for managing the turtles wirelessy
  24. --]]
  25.  
  26. -- set global variables
  27. startX = 0
  28. startY = 0
  29. startZ = 0
  30. targetX = 0
  31. targetY = 0
  32. targetZ = 0
  33. depth = 0
  34. currentX = 0
  35. currentY = 0
  36. currentZ = 0
  37. placeItemSlot = 5
  38. moves = 0
  39. updateXZ = {
  40.     [1] = function() -- facing north add to z
  41.             currentZ = currentZ - 1
  42.         end,
  43.     [2] = function() -- facing east add to x
  44.             currentX = currentX + 1
  45.         end,
  46.     [3] = function() -- facing south subtract from z
  47.             currentZ = currentZ + 1
  48.         end,
  49.     [4] = function() -- facing west subtract from x
  50.             currentX = currentX - 1
  51.         end
  52. }
  53. facing = 1
  54.  
  55. --utility functions
  56.  
  57. function storeData() -- This will store the turtles current state
  58.     local data = {
  59.         ["currentX"] = currentX,
  60.         ["currentY"] = currentY,
  61.         ["currentZ"] = currentZ,
  62.         ["targetX"] = targetX,
  63.         ["targetY"] = targetY,
  64.         ["targetZ"] = targetZ,
  65.         ["facing"] = facing
  66.    }
  67.    local file = fs.open("turtle","w")
  68.    file.write(textutils.serialize(data))
  69.    file.close()
  70. end
  71.  
  72. function say(message)
  73.     print(message)
  74. end
  75.  
  76. function getDistanceBetween(x,y)
  77.     say("distance between " .. x .. ", " .. y)
  78.     return x - y
  79. end
  80.  
  81. function getAbsoluteDistance(x,y)
  82.     say("absolute distance between " .. x .. ", " .. y)
  83.     return math.abs(x - y)
  84. end
  85.  
  86.  
  87. function getTotalMoves()
  88.     local xDistance = getAbsoluteDistance(startX , targetX)
  89.     local zDistance = getAbsoluteDistance(startZ , targetZ)
  90.     local yDistance = getAbsoluteDistance(startY , depth)
  91.     local targetYDistance = getAbsoluteDistance(targetY , depth)
  92.     moves = yDistance + xDistance + zDistance + targetYDistance
  93.     say("we need " .. moves .. " fuel To get there")
  94.     return moves
  95. end
  96.  
  97. function refuel()
  98.     say("Refueling System")
  99.     while turtle.getFuelLevel() < moves do
  100.         turtle.select(1)
  101.         turtle.refuel();
  102.     end
  103.     turtle.select(placeItemSlot)
  104.     say("Refueled")
  105.     return true
  106. end
  107.  
  108. --Rotation Functions
  109.  
  110. function turnRight(turns)
  111.     turns = turns or 1
  112.     while turns > 0 do
  113.         turns = turns - 1
  114.         turtle.turnRight()
  115.         if facing == 4 then
  116.             facing = 1
  117.         else
  118.             facing = facing + 1
  119.         end
  120.         storeData()
  121.     end
  122. end
  123.  
  124. function turnLeft(turns)
  125.     turns = turns or 1
  126.     while turns > 0 do
  127.         turns = turns - 1
  128.         turtle.turnLeft()
  129.         if facing == 1 then
  130.             facing = 4
  131.         else
  132.             facing = facing - 1
  133.         end
  134.         storeData()
  135.     end
  136. end
  137.  
  138. function faceNorth()
  139.         while facing ~= 1 do
  140.             turnRight()
  141.         end
  142. end
  143.  
  144. function faceTargetZ()
  145.     if targetZ > currentZ then
  146.         while facing ~= 3 do
  147.             turnRight()
  148.         end
  149.     else
  150.          while facing ~= 1 do
  151.             turnRight()
  152.         end
  153.     end
  154. end
  155.  
  156. function faceTargetX()
  157.     if targetX < currentX then
  158.         while facing ~= 4 do
  159.             turnRight()
  160.         end
  161.     else
  162.          while facing ~= 2 do
  163.             turnRight()
  164.         end
  165.     end
  166. end
  167.  
  168. --Movement Functions
  169. function moveForward()
  170.     if turtle.detect() == false then
  171.         turtle.forward()
  172.         updateXZ[facing]()
  173.         storeData()
  174.     else
  175.         say("I cant move forward")
  176.     end
  177.    
  178. end
  179.  
  180. function moveDown()
  181.     if turtle.detectDown() == false then
  182.         turtle.down()
  183.         currentY = currentY - 1
  184.         storeData()
  185.     else
  186.         say("I cant move down")
  187.     end
  188. end
  189.  
  190.  
  191. function moveUp()
  192.     if turtle.detectUp() == false then
  193.         turtle.up()
  194.         currentY = currentY + 1
  195.         storeData()
  196.     else
  197.         say("I cant move Up")
  198.     end
  199. end
  200.  
  201. --actions
  202. function refilSlotFromRight()
  203.     say("refilling main item slot")
  204.     local fromSlot = placeItemSlot + 1
  205.     local targetSlot = fromSlot - 1
  206.     while targetSlot < placeItemSlot + 4 do
  207.         turtle.select(fromSlot)
  208.         say("transfering to Slot: " .. targetSlot .. " from slot: " .. turtle.getSelectedSlot())
  209.         turtle.transferTo(targetSlot,1)
  210.         fromSlot = fromSlot + 1
  211.         targetSlot = fromSlot - 1
  212.     end
  213.     turtle.select(placeItemSlot)
  214. end
  215.  
  216. function placeItem()
  217.     turnRight(2)
  218.     turtle.place(placeItemSlot)
  219.     refilSlotFromRight()
  220.     turnRight(2)
  221. end
  222.  
  223. --Main Functions
  224.  
  225.  
  226.  
  227. function moveAndPlace()
  228.     turtle.dig()
  229.     moveForward()
  230.     placeItem()
  231. end
  232.  
  233. --Core Functions
  234.  
  235. function goToTargetZ()
  236.     while getAbsoluteDistance(targetZ,currentZ) > 0 do
  237.         say("current Z distance is " .. getAbsoluteDistance(targetZ,currentZ))
  238.         moveAndPlace()
  239.     end
  240. end
  241. function goToTargetX()
  242.     while getAbsoluteDistance(targetX,currentX) > 0 do
  243.         say("current X distance is " .. getAbsoluteDistance(targetX,currentX))
  244.         moveAndPlace()
  245.     end
  246. end
  247.  
  248.  
  249. function goToDepth()
  250.     say("moving to target depth")
  251.     while getAbsoluteDistance(depth,currentY)>0 do
  252.         turtle.digDown()
  253.         turtle.placeUp(placeItemSlot)
  254.         moveDown()
  255.         say("placing item above me from slot " .. placeItemSlot)
  256.     end
  257. end
  258.  
  259. function goToTargetY()
  260.     say("moving to target Y")
  261.     while getAbsoluteDistance(targetY,currentY ) > 0 do
  262.         turtle.digUp()
  263.         moveUp()
  264.          say("placing item below me from slot " .. placeItemSlot)
  265.         turtle.placeDown(placeItemSlot)
  266.     end
  267. end
  268.  
  269.  
  270. function systemsReady()
  271.     say("doing systems check")
  272.     if #arg >=7 then
  273.          
  274.         startX  = tonumber(arg[1])
  275.         startY  = tonumber(arg[2])
  276.         startZ  = tonumber(arg[3])
  277.         targetX  = tonumber(arg[4])
  278.         targetY  = tonumber(arg[5])
  279.         targetZ  = tonumber(arg[6])
  280.         depth   =  tonumber(arg[7])
  281.         currentX = startX
  282.         currentY = startY
  283.         currentZ = startZ
  284.        
  285.         say("Checking ItemInventory")
  286.         if turtle.getItemCount(placeItemSlot) > moves then
  287.             if refuel() then
  288.                 turtle.select(placeItemSlot)
  289.                 say("Lets place these bitches")
  290.                 return true
  291.             else
  292.                 say("Feed me semour")
  293.                
  294.             end
  295.         else
  296.             say("We Need An Item in Slot " .. placeItemSlot)
  297.         end
  298.            
  299.     else
  300.         say("Tell me where to go (e.g placeTotarget x y z [optional] depth")
  301.     end
  302.     return false
  303. end
  304.  
  305. function initialize()
  306.     say("initializing")
  307.     local systemIsReady = systemsReady()
  308.     if systemIsReady == true then
  309.         getTotalMoves()
  310.         goToDepth()
  311.         storeData()
  312.         faceTargetZ()
  313.         goToTargetZ()
  314.         faceTargetX()
  315.         goToTargetX()
  316.         goToTargetY()
  317.         faceNorth()
  318.     end
  319. end
  320.  
  321. initialize()
Add Comment
Please, Sign In to add comment