OreSeur-

Dig Code

Jan 17th, 2021 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.25 KB | None | 0 0
  1. --the codes for the ports
  2. local SERVER_PORT = 568
  3. local CLIENT_PORT = 342
  4.  
  5. local INVENTORY_SLOT = 1
  6. local FUEL_SLOT = 2
  7.  
  8. --The number of slots in the turtle
  9. local SLOT_COUNT = 16
  10.  
  11. --To help with turning while mining
  12. local RIGHT = 0
  13. local LEFT = 1
  14. --we always turn right first while digging
  15. local turnDirection = 0
  16.  
  17. --split a string message into a table of strings
  18. --inputstr is the sting to be separated
  19. --sep is the string that will be found between sections
  20. function split (inputstr, sep)
  21.     if sep == nil then
  22.             sep = "%s"
  23.     end
  24.     local t={}
  25.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  26.             table.insert(t, str)
  27.     end
  28.     return t
  29. end
  30.  
  31. --the data is the
  32. --put the string data into the coords array
  33. function parseParams(data)
  34.     coords = {}
  35.     params = split(data, " ")
  36.  
  37.     coords[1] = vector.new(params[1], params[2], params[3])
  38.     coords[2] = vector.new(params[4], params[5], params[6])
  39.  
  40.     return (coords)
  41. end
  42.  
  43. function checkFuel()
  44.     while(turtle.getFuelLevel() < 100) do
  45.         --selectchest
  46.         turtle.select(FUEL_SLOT)
  47.         --make space for chest
  48.         turtle.digUp()
  49.         --place chest
  50.         turtle.placeUp()
  51.         --get lava bucket
  52.         turtle.suckUp()
  53.         --refuel
  54.         turtle.refuel()
  55.         --put bucket back
  56.         turtle.dropUp()
  57.         --get chest back
  58.         turtle.digUp()
  59.     end
  60. end
  61.  
  62. function checkInventory()
  63.     --we deposit our inventory if we have extra chests or if the inventory is almost full
  64.    
  65.     if ((turtle.getItemCount(SLOT_COUNT - 1) > 0) or (turtle.getItemCount(FUEL_SLOT) > 1) or (turtle.getItemCount(INVENTORY_SLOT) > 1)) then
  66.         --select the chest
  67.         turtle.select(INVENTORY_SLOT)
  68.         --make space for the chest
  69.         turtle.digUp()
  70.         --place chest
  71.         turtle.placeUp()
  72.         --place everything except chests in the chest
  73.         for i = 1, SLOT_COUNT, 1 do
  74.             --we want to deposit everything except the inventory and fuel chest
  75.             --We know that one inventory chest is above the turtle so we can deposit any extras
  76.             --We need to make sure we keep at least on fuel chest
  77.             if i == FUEL_SLOT then
  78.                 --drop evertyihing in the slot except one
  79.                 turtle.dropUp(turtle.getItemCount(FUEL_SLOT) - 1)
  80.             else
  81.                 --for every slot except FUEL_SLOT we want to deposit everything
  82.                 turtle.dropUp()
  83.             end
  84.         end
  85.         --get the chest back
  86.         turtle.digUp()
  87.     end
  88. end
  89.  
  90. -------------------- Movement Functions -------------------------
  91.  
  92. --turn the turtle around
  93. function turnAround(heading)
  94.     turtle.turnRight()
  95.     turtle.turnRight()
  96.     return ((heading + 1) % 4) + 1
  97. end
  98.  
  99. --Dig the forward block and move forward
  100. function forward()
  101.     checkFuel()
  102.     while(turtle.detect()) do
  103.         turtle.dig()
  104.     end
  105.     turtle.forward()
  106.     checkInventory()
  107. end
  108.  
  109. --Dig the forward block, move forward,
  110. --dig up if digSize is 2 or more
  111. --dig down if digSize is 3 or more
  112. function dig(digSize)
  113.     forward()
  114.    
  115.     if digSize >= 2 then
  116.         while(turtle.detectUp()) do
  117.             turtle.digUp()
  118.         end
  119.     end
  120.    
  121.     if digSize >= 3 then
  122.         while(turtle.detectDown()) do
  123.             turtle.digDown()
  124.         end
  125.     end
  126.     checkInventory()
  127. end
  128.  
  129. -- do a digging u turn to the left
  130. function turnLeft(digSize, heading)
  131.     turtle.turnLeft()
  132.     dig(digSize)
  133.     turtle.turnLeft()
  134.     return ((heading + 1) % 4) + 1
  135. end
  136.  
  137. --do a digging u turn to the right
  138. function turnRight(digSize, heading)
  139.     turtle.turnRight()
  140.     dig(digSize)
  141.     turtle.turnRight()
  142.     return ((heading + 1) % 4) + 1
  143. end
  144.  
  145. --turn the correct direction while digging
  146. function digTurn(digSize, heading)
  147.     if turnDirection == RIGHT then
  148.         heading = turnRight(digSize, heading)
  149.     else
  150.         heading = turnLeft(digSize, heading)
  151.     end
  152.     turnDirection = (turnDirection + 1) % 2
  153.     return heading
  154. end
  155.  
  156. --turn to a heading to go a specific direction
  157. function turnToFaceHeading(heading, destinationHeading)
  158.     if heading > destinationHeading then
  159.         for t = 1, math.abs(destinationHeading - heading), 1 do
  160.             turtle.turnLeft()
  161.         end
  162.     elseif heading < destinationHeading then
  163.         for t = 1, math.abs(destinationHeading - heading), 1 do
  164.             turtle.turnRight()
  165.         end
  166.     end
  167. end
  168.  
  169. --find the orientation of the turtle
  170. --then move into position to start dig
  171. --returns the Heading
  172. function getOrientation()
  173.     loc1 = vector.new(gps.locate(2, false))
  174.     forward()
  175.     loc2 = vector.new(gps.locate(2, false))
  176.     heading = loc2 - loc1
  177.     turtle.down()
  178.     turtle.down()
  179.     return (heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3)
  180. end
  181.  
  182. --turn to the heading needed to move in z direction
  183. function setHeadingZ(zDiff, heading)
  184.     local destinationHeading = heading
  185.     if zDiff < 0 then
  186.         destinationHeading = 2
  187.     elseif zDiff > 0 then
  188.         destinationHeading = 4
  189.     end
  190.     turnToFaceHeading(heading, destinationHeading)
  191.  
  192.     return destinationHeading
  193. end
  194.  
  195. --turn to the heading needed to move in x direction
  196. function setHeadingX(xDiff, heading)
  197.     local destinationHeading = heading
  198.     if xDiff < 0 then
  199.         destinationHeading = 1
  200.     elseif xDiff > 0 then
  201.         destinationHeading = 3
  202.     end
  203.  
  204.     turnToFaceHeading(heading, destinationHeading)
  205.     return destinationHeading
  206. end
  207.  
  208. --move dig and move the specified amount
  209. function digAndMove(n, digSize)
  210.     for x = 1, n, 1 do
  211.         dig(digSize)
  212.     end
  213. end
  214.  
  215. function move(n)
  216.     for x = 1, n, 1 do
  217.         forward()
  218.     end
  219. end
  220.  
  221. --move turle up
  222. function MoveDown(n)
  223.     for y = 1, n, 1 do
  224.         while(turtle.detectDown()) do
  225.             turtle.digDown()
  226.         end
  227.         turtle.down()
  228.         checkFuel()
  229.     end
  230. end
  231.  
  232. --move turtledown
  233. function MoveUp(n)
  234.     for y = 1, n, 1 do
  235.         while(turtle.detectUp()) do
  236.             turtle.digUp()
  237.         end
  238.         turtle.up()
  239.         checkFuel()
  240.     end
  241. end
  242.  
  243. --move to a specific coordinate
  244. function moveTo(coords, heading)
  245.     local currX, currY, currZ = gps.locate()
  246.     local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  247.     print(string.format("Distances from start: %d %d %d", xDiff, yDiff, zDiff))
  248.  
  249.     --    -x = 1
  250.     --    -z = 2
  251.     --    +x = 3
  252.     --    +z = 4
  253.  
  254.     -- Move to X start
  255.     heading = setHeadingX(xDiff, heading)
  256.     move(math.abs(xDiff))
  257.  
  258.     -- Move to Z start
  259.     heading = setHeadingZ(zDiff, heading)
  260.     move(math.abs(zDiff))
  261.  
  262.     -- Move to Y start
  263.     if yDiff < 0  then    
  264.         MoveDown(math.abs(yDiff))
  265.     elseif yDiff > 0 then
  266.         MoveUp(math.abs(yDiff))
  267.     end
  268.  
  269.     return heading
  270. end
  271.  
  272. --------------------------------START BEGIN TRIP CODE-------------------------
  273.  
  274. --connect to the modem on the turtle
  275. local modem = peripheral.wrap("left")
  276. modem.open(CLIENT_PORT)
  277.  
  278. --tell the server we are placed
  279. modem.transmit(SERVER_PORT, CLIENT_PORT, "CLIENT_DEPLOYED")
  280.  
  281. --we should receive the fuel and deposit chest right now
  282.  
  283. --get mining coords from server
  284. event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  285. data = parseParams(msg)
  286.  
  287. --get Fuel
  288. checkFuel()
  289.  
  290. --Go to starting Postion
  291. local startCoords = data[1]
  292. local currentHeading = moveTo(startCoords, getOrientation())
  293.  
  294.  --Turn to face North
  295. local NORTH_HEADING = 2
  296. turnToFaceHeading(currentHeading, NORTH_HEADING)
  297. currentHeading = NORTH_HEADING
  298. --Now in Starting Position--
  299. --------------------------------START MINING CODE------------------------------
  300.  
  301. --function for mining a quarry
  302. function startQuary(width, height, length, heading)
  303.     --define the dig size
  304.     local digSize = 3
  305.    
  306.     --repeat for each tier in the dig
  307.     for tier = 1, height, 3 do
  308.         --define the dig size
  309.         digSize = 3
  310.         if((height - (tier - 1)) < digSize) then
  311.             digSize = (height - (tier - 1))
  312.         end
  313.        
  314.         --we need to move differently for the first tier
  315.         if tier == 1 then
  316.             --clear blocks in starting path
  317.             if digSize == 3 then
  318.                 moveDown(2)
  319.                 moveUp(1)
  320.             elseif digSize == 2 then
  321.                 moveDown(1)
  322.             end
  323.         else
  324.             --move to next tier
  325.             if digSize == 3 then
  326.                 moveDown(4)
  327.                 moveUp(1)
  328.             elseif digSize == 2 then
  329.                 moveDown(3)
  330.             else
  331.                 moveDown(2)
  332.             end
  333.         end
  334.        
  335.         --for each row j
  336.         for j = 1, width, 1 do
  337.             --dig the length of the dig not including the block we are already in
  338.             digAndMove( (length-1) , digSize)
  339.            
  340.             --we don't want to turn if it is the last row
  341.             if(j ~= width) then
  342.                 heading = digTurn(digSize, heading)
  343.             end
  344.         end
  345.        
  346.         --turn around
  347.         heading = turnAround(heading)
  348.        
  349.     end
  350.    
  351.     return heading
  352. end
  353.  
  354. --get the size of the quarry from the server message
  355. local quary = data[2]
  356.  
  357. --start dig
  358. currentHeading = startQuary(quary.x, quary.y, quary.z, currentHeading)
  359.  
  360.  
  361. --------------------------------START RETURN TRIP CODE-------------------------
  362.  
  363. function returnTo(coords, heading)
  364.     local currX, currY, currZ = gps.locate()
  365.     local xDiff, yDiff, zDiff = coords.x - currX, coords.y - currY, coords.z - currZ
  366.     print(string.format("Distances from end: %d %d %d", xDiff, yDiff, zDiff))
  367.    
  368.     -- Move to Y start
  369.     if yDiff < 0 then    
  370.         MoveDown(math.abs(yDiff))
  371.     elseif yDiff > 0 then
  372.         MoveUp(math.abs(yDiff))
  373.     end
  374.  
  375.     -- Move to X start
  376.     heading = setHeadingX(xDiff, heading)
  377.     move(math.abs(xDiff))
  378.  
  379.     -- Move to Z start
  380.     heading = setHeadingZ(zDiff, heading)
  381.     move(math.abs(zDiff))
  382.    
  383.     return heading
  384. end
  385.  
  386. endCoords = data[3]
  387. returnTo(endCoords, currentHeading)
  388.  
  389. local timoutWait = 60
  390. for i = 1, timoutWait, 1 do
  391.     os.sleep(1)
  392.     print(string.format( "Waiting for brothers %d/%d", i, timoutWait))
  393. end
  394.  
  395. modem.transmit(SERVER_PORT, CLIENT_PORT, "end")
  396.  
Add Comment
Please, Sign In to add comment