kd2bwzgen

Randomized Turtles - code

Mar 22nd, 2016
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. local stream = dofile("stream.lua")
  2. stream.aiStream = function(max_n)
  3. ret = stream.Stream()
  4.  
  5. tStorage = {}
  6.  
  7. function next()
  8. return table.remove(tStorage, 1)
  9. end
  10.  
  11. function generate(target_amt)
  12. local i = #tStorage
  13. repeat
  14. i = i+1
  15. tStorage[i] = roll()
  16. until i == target_amt
  17. end
  18.  
  19. function hasNext()
  20. return #tStorage > 0
  21. end
  22.  
  23. ret.next = next
  24. ret.generate = generate
  25. ret.hasNext = hasNext
  26. return ret
  27. end
  28. function roll()
  29. local n = math.random(1,8)
  30. return n
  31. end
  32. function boundaries()
  33. local t,n = turtle.inspect()
  34. if t then
  35. if n['name'] == "minecraft:planks" or n['name'] == "minecraft:glass" then
  36. return true
  37. else
  38. return false
  39. end
  40. else
  41. return false
  42. end
  43. end
  44. local function fuel()
  45. if turtle.getFuelLevel() < 10 then
  46. turtle.select(16)
  47. turtle.refuel()
  48. turtle.select(1)
  49. end
  50. end
  51. local ai = stream.aiStream()
  52. ai.generate(20)
  53. while ai.hasNext() do
  54. local touching = boundaries()
  55. local act = ai.next()
  56. fuel()
  57. if act == 1 then
  58. turtle.up()
  59. elseif act == 2 then
  60. turtle.down()
  61. elseif act == 3 then
  62. turtle.turnLeft()
  63. elseif act == 4 then
  64. turtle.turnRight()
  65. elseif act == 5 then
  66. turtle.forward()
  67. elseif act == 6 then
  68. turtle.back()
  69. elseif act == 7 and touching == false then
  70. turtle.dig()
  71. else
  72. turtle.place()
  73. end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment