KinoftheFlames

Untitled

Sep 18th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.14 KB | None | 0 0
  1. --hare
  2. --by KinoftheFlames
  3.  
  4. pre = "[hare]"
  5.  
  6. --move: move safely (no entity in way, no block in way, has fuel)
  7. --returns false if out of fuel or way is blocked, else returns true
  8. function forward()
  9.     if not hasFuel() then return false end
  10.     if turtle.forward() then return true end
  11.     if turtle.detect() then
  12.         print(pre.."Block in path (forward).")
  13.         return false end
  14.    
  15.     --if not moved, and no block in path, entity must be in path
  16.     print(pre.."Entity in path; attacking (forward).")
  17.     for i=1,20 do
  18.         turtle.attack()
  19.         os.sleep(1)
  20.         if turtle.forward() then break end
  21.         if turtle.detect() then --probably gravel/sand
  22.             print(pre.."Block in path (forward).")
  23.             return false end
  24.         if i == 20 then
  25.             print(pre.."Could not resolve obstacle.")
  26.             return false
  27.         end
  28.     end
  29.     print(pre.."Entity resolved, continuing.")
  30.     return true
  31. end
  32. function back()
  33.     if not hasFuel() then return false end
  34.     if turtle.back() then return true end
  35.     turnAround()
  36.     if turtle.detect() then
  37.         print(pre.."Block in path (back).")
  38.         turnAround()
  39.         return false end
  40.    
  41.     --if not moved, and no block in path, entity must be in path
  42.     print(pre.."Entity in path; attacking (back).")
  43.     for i=1,20 do
  44.         turtle.attack()
  45.         os.sleep(1)
  46.         if turtle.forward() then break end
  47.         if turtle.detect() then --probably gravel/sand
  48.             print(pre.."Block in path (back).")
  49.             return false end
  50.         if i == 20 then
  51.             print(pre.."Could not resolve obstacle.")
  52.             turnAround()
  53.             return false
  54.         end
  55.     end
  56.     print(pre.."Entity resolved, continuing.")
  57.     turnAround()
  58.     return true
  59. end
  60. function down()
  61.     if not hasFuel() then return false end
  62.     if turtle.down() then return true end
  63.     if turtle.detectDown() then
  64.         print(pre.."Block in path (down).")
  65.         return false end
  66.    
  67.     --if not moved, and no block in path, entity must be in path
  68.     print(pre.."Entity in path; attacking (down).")
  69.     for i=1,20 do
  70.         turtle.attackDown()
  71.         os.sleep(1)
  72.         if turtle.down() then break end
  73.         if turtle.detectDown() then --block placed?
  74.             print(pre.."Block in path (down).")
  75.             return false end
  76.         if i == 20 then
  77.             print(pre.."Could not resolve obstacle.")
  78.             return false
  79.         end
  80.     end
  81.     print(pre.."Entity resolved, continuing.")
  82.     return true
  83. end
  84. function up()
  85.     if not hasFuel() then return false end
  86.     if turtle.up() then return true end
  87.     if turtle.detectUp() then
  88.         print(pre.."Block in path (up).")
  89.         return false end
  90.    
  91.     --if not moved, and no block in path, entity must be in path
  92.     print(pre.."Entity in path; attacking (up).")
  93.     for i=1,20 do
  94.         turtle.attackUp()
  95.         os.sleep(1)
  96.         if turtle.up() then break end
  97.         if turtle.detectUp() then --probably gravel/sand
  98.             print(pre.."Block in path (up).")
  99.             return false end
  100.         if i == 20 then
  101.             print(pre.."Could not resolve obstacle.")
  102.             return false
  103.         end
  104.     end
  105.     print(pre.."Entity resolved, continuing.")
  106.     return true
  107. end
  108.  
  109. --left/right: turn, move, turn back
  110. function left()
  111.     turtle.turnLeft()
  112.     value = forward()
  113.     turtle.turnRight()
  114.     return value
  115. end
  116. function right()
  117.     turtle.turnRight()
  118.     value = forward()
  119.     turtle.turnLeft()
  120.     return value
  121. end
  122.  
  123. --redundancy
  124. function turnRight()
  125.     return turtle.turnRight()
  126. end
  127. function turnLeft()
  128.     return turtle.turnLeft()
  129. end
  130.  
  131. function hasFuel()
  132.     if turtle.getFuelLevel() == 0 then
  133.         print(pre.."Out of fuel.")
  134.         return false
  135.     end
  136.     return true
  137. end
  138.  
  139. --digMove: digs, then moves when available
  140. --returns success boolean of movement
  141. function digMove()
  142.     minedBlock = turtle.dig()
  143.     moved = forward()
  144.     if minedBlock and not moved then
  145.         os.sleep(1)
  146.         while minedBlock and not moved do --while gravel/sand in the way and unable to move
  147.             minedBlock = turtle.dig()
  148.             os.sleep(1)
  149.             moved = forward()
  150.         end
  151.         return true
  152.     end
  153.     return moved
  154. end
  155. function digMoveForward() digMove() end --referal
  156. function digMoveUp()
  157.     minedBlock = turtle.digUp()
  158.     moved = up()
  159.     if minedBlock and not moved then
  160.         os.sleep(1)
  161.         while minedBlock and not moved do --while gravel/sand in the way and unable to move
  162.             minedBlock = turtle.digUp()
  163.             os.sleep(1)
  164.             moved = up()
  165.         end
  166.         return true
  167.     end
  168.     return moved
  169. end
  170. function digMoveDown()
  171.     turtle.digDown()
  172.     return down()
  173. end
  174. function digMoveLeft()
  175.     turtle.turnLeft()
  176.     value = digMove()
  177.     turtle.turnRight()
  178.     return value
  179. end
  180. function digMoveRight()
  181.     turtle.turnRight()
  182.     value = digMove()
  183.     turtle.turnLeft()
  184.     return value
  185. end
  186.  
  187. --turn around/backwards/180
  188. function turnAround()
  189.     turtle.turnRight()
  190.     turtle.turnRight()
  191. end
  192.  
  193. --takes a string of chars and translates it to movement
  194. --i.e. scriptMove(udfblrLRA) is up down forward back turnLeft turnRight left right turnAround
  195. --lower case for basic movements, upper case for macros and actions
  196. function quickMove(script)
  197.     for i=1,#script do
  198.         char = script.sub(script, i, i)
  199.         if char == "f" then
  200.             hare.forward()
  201.         elseif char == "b" then
  202.             hare.back()
  203.         elseif char == "u" then
  204.             hare.up()
  205.         elseif char == "d" then
  206.             hare.down()
  207.         elseif char == "l" then
  208.             turtle.turnLeft()
  209.         elseif char == "r" then
  210.             turtle.turnRight()
  211.         elseif char == "L" then
  212.             hare.left()
  213.         elseif char == "R" then
  214.             hare.right()
  215.         elseif char == "a" then
  216.             turnAround()
  217.         elseif char == "A" then --DEPRECATED
  218.             turnAround()
  219.         elseif char == "D" then
  220.             turtle.drop()
  221.         elseif char == "S" then
  222.             turtle.suck()
  223.         elseif char == "G" then
  224.             turtle.dig()
  225.         elseif char == "P" then
  226.             turtle.place()
  227.         end
  228.     end
  229. end
  230. function qm(script) quickMove(script) end
  231. function quickDigMove(script)
  232.     for i=1,#script do
  233.         char = script.sub(script, i, i)
  234.         if char == "f" then
  235.             hare.digMove()
  236.         elseif char == "b" then
  237.             hare.turnAround()
  238.             hare.digMove()
  239.             hare.turnAround()
  240.         elseif char == "u" then
  241.             hare.digMoveUp()
  242.         elseif char == "d" then
  243.             hare.digMoveDown()
  244.         elseif char == "l" then
  245.             turtle.turnLeft()
  246.         elseif char == "r" then
  247.             turtle.turnRight()
  248.         elseif char == "L" then
  249.             hare.digMoveLeft()
  250.         elseif char == "R" then
  251.             hare.digMoveRight()
  252.         elseif char == "A" then
  253.             turnAround()
  254.         end
  255.     end
  256. end
  257. function qdm(script) quickDigMove(script) end
  258.  
  259.  
  260. --maybe should move to diff API
  261. function netprint(text)
  262.     rednet.open("right")
  263.     print(pre..text)
  264.     rednet.broadcast(pre..text)
  265. end
Advertisement
Add Comment
Please, Sign In to add comment