shiryavsky

Minecraft testing movements

Nov 11th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.89 KB | None | 0 0
  1. currentPositionx = 0
  2. currentPositiony = 0
  3. currentPositionz = 0
  4. currentFacing = 0
  5. currentlySelectedSlot = 1
  6. fuelLevelToRefuelAt = 5
  7.  
  8. function main()
  9.   pillarToZ(1, 3)
  10.   lineToY(1, currentPositiony + 2)
  11.   turnZero()
  12.   safeForward()
  13.   pillarToZ(1, 3)
  14.   lineToY(1, currentPositiony + 2)
  15.   turnZero()
  16.   safeForward()
  17.   pillarToZ(1, 3)
  18. end  
  19.  
  20. function lineToY(item, y)  
  21.   turnZero()
  22.   while currentPositiony ~= y do
  23.      if not turtle.detectDown() then     
  24.        safePlaceDown(item)
  25.      end
  26.      if y > currentPositiony then              
  27.         turnFacing(0)
  28.         safeForward()  
  29.         safePlaceDown(item)    
  30.      elseif y < currentPositiony then              
  31.         turnFacing(2)    
  32.         safeForward()  
  33.         safePlaceDown(item)    
  34.      end
  35.   end  
  36. end
  37.  
  38. function pillarToZ(item, z)
  39.   goGround(128)
  40.   safeUp()
  41.   while currentPositionz < z do
  42.     safePlaceDown(item)
  43.     safeUp()
  44.   end
  45. end
  46.  
  47. function goGround(maximum)
  48.   local current = 0;
  49.   while not turtle.detectDown() do    
  50.     safeDown()
  51.     current = current + 1
  52.     if current >= maximum  then
  53.         return true
  54.     end
  55.   end
  56.   return true
  57. end
  58.  
  59. function getItemPlace(item)
  60.   while true do
  61.     turtle.select(item)
  62.     local skip = true
  63.     for i = 1, 15 do
  64.       if skip then
  65.         if turtle.getItemCount(i)==0 then
  66.           skip = false
  67.         end
  68.       else
  69.         if turtle.getItemCount(i)~=0 then        
  70.           if turtle.compareTo(i) then
  71.             print("Found material in "..tostring(i))
  72.             return i       
  73.           end
  74.         end
  75.       end
  76.     end
  77.     awaitFix("No building materials! Refill with same as selected slot!")
  78.   end  
  79. end
  80.  
  81. function ensureFuel()
  82.   -- Determine whether a refuel is required
  83.   local fuelLevel = turtle.getFuelLevel()
  84.   if (fuelLevel ~= "unlimited") then
  85.     if (fuelLevel < fuelLevelToRefuelAt) then
  86.       -- Need to refuel
  87.       turtle.select(16)
  88.       currentlySelectedSlot = 16
  89.       local fuelItems = turtle.getItemCount(16)
  90.       -- Do we need to impact the emergency fuel to continue? (always  
  91.       -- keep one fuel item in slot 16)
  92.       if (fuelItems == 0) then
  93.         print("I want more fuel!")
  94.       else
  95.         turtle.refuel(1)
  96.       end
  97.     end
  98.   end
  99. end
  100.  
  101. function awaitFix(message)
  102.     print("I have problem: " .. message)
  103.     print("Please fix it and press Enter to continue")
  104.     io.read()
  105. end
  106.  
  107. function safePlaceFront(itemTest)
  108.   item = getItemPlace(itemTest)
  109.   ensureFuel()
  110.   turtle.select(item)
  111.   success = false
  112.   while not success do
  113.     success = turtle.place()
  114.     if not success then
  115.       awaitFix("Can't place in front")
  116.     end
  117.   end
  118. end
  119.  
  120. function safePlaceDown(itemTest)
  121.   item = getItemPlace(itemTest)
  122.   ensureFuel()
  123.   turtle.select(item)
  124.   success = false
  125.   while not success do
  126.     success = turtle.placeDown()
  127.     if not success then
  128.       awaitFix("Can't place down")
  129.     end
  130.   end
  131. end
  132.  
  133.  
  134. function safePlaceUp(itemTest)
  135.   item = getItemPlace(itemTest)
  136.   ensureFuel()
  137.   turtle.select(item)
  138.   success = false
  139.   while not success do
  140.     success = turtle.placeUp()
  141.     if not success then
  142.       awaitFix("Can't place up")
  143.     end
  144.   end
  145. end
  146.  
  147.  
  148. function safeDigFront(item)
  149.   ensureFuel()
  150.   turtle.select(item)
  151.   success = false
  152.   while not success do
  153.     success = turtle.dig()
  154.     if not success then
  155.       awaitFix("Can't dig in front")
  156.     end
  157.   end
  158. end
  159.  
  160. function safeDigDown(item)
  161.   ensureFuel()
  162.   turtle.select(item)
  163.   success = false
  164.   while not success do
  165.     success = turtle.digDown()
  166.     if not success then
  167.       awaitFix("Can't dig down")
  168.     end
  169.   end
  170. end
  171.  
  172.  
  173. function safeDigUp(item)
  174.   ensureFuel()
  175.   turtle.select(item)
  176.   success = false
  177.   while not success do
  178.     success = turtle.digUp()
  179.     if not success then
  180.       awaitFix("Can't dig up")
  181.     end
  182.   end
  183. end
  184.  
  185. function turnZero()
  186.   while currentFacing ~= 0 do
  187.     turnRightTrack()
  188.   end
  189. end
  190.  
  191. function turnFacing(facing)
  192.   --todo: faster it  
  193.   while facing ~= currentFacing do
  194.     turnRightTrack()
  195.   end
  196. end
  197.  
  198. function turnRightTrack()
  199.   ensureFuel()
  200.   turtle.turnRight()
  201.   currentFacing = currentFacing + 1
  202.   if currentFacing >= 4 then
  203.     currentFacing = 0
  204.   end
  205. end
  206.  
  207. function turnLeftTrack()
  208.   ensureFuel()
  209.   turtle.turnLeft()
  210.   currentFacing = currentFacing - 1
  211.   if currentFacing < 0 then
  212.     currentFacing = 3
  213.   end
  214. end
  215.  
  216. function safeForward()
  217.   ensureFuel()
  218.   success = false
  219.   while not success do
  220.     success = turtle.forward()
  221.     if not success then
  222.       awaitFix("Blocked attempting to move forward")
  223.     end
  224.   end
  225.   if (currentFacing == 0) then
  226.     currentPositiony = currentPositiony + 1
  227.   elseif (currentFacing == 1) then
  228.     currentPositionx = currentPositionx + 1
  229.   elseif (currentFacing == 2) then
  230.     currentPositiony = currentPositiony - 1
  231.   elseif (currentFacing == 3) then
  232.     currentPositionx = currentPositionx - 1
  233.   else
  234.     awaitFix("Oy we gotta into 101010022..")
  235.   end
  236. end
  237.  
  238. function safeBack()
  239.   ensureFuel()
  240.   success = false
  241.   while not success do
  242.     success = turtle.back()
  243.     if not success then
  244.       awaitFix("Blocked attempting to move back")
  245.     end
  246.   end
  247.   if (currentFacing == 0) then
  248.     currentPositiony = currentPositiony - 1
  249.   elseif (currentFacing == 1) then
  250.     currentPositionx = currentPositionx - 1
  251.   elseif (currentFacing == 2) then
  252.     currentPositiony = currentPositiony + 1
  253.   elseif (currentFacing == 3) then
  254.     currentPositionx = currentPositionx + 1
  255.   else
  256.     awaitFix("Oy we gotta into 101010022..")
  257.   end  
  258. end
  259.  
  260. function safeUp()
  261.   ensureFuel()
  262.   success = false
  263.   while not success do
  264.     success = turtle.up()
  265.     if not success then
  266.       awaitFix("Blocked attempting to move up")
  267.     end
  268.   end
  269.   currentPositionz = currentPositionz + 1
  270. end
  271.  
  272. function safeDown()
  273.   ensureFuel()
  274.   success = false
  275.   while not success do
  276.     success = turtle.down()
  277.     if not success then
  278.       awaitFix("Blocked attempting to move down")
  279.     end
  280.   end
  281.   currentPositionz = currentPositionz - 1
  282. end
  283.  
  284. main()
Advertisement
Add Comment
Please, Sign In to add comment