Bmorr

random_parkour.lua

Jul 19th, 2021 (edited)
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.88 KB | None | 0 0
  1. --[[
  2. pastebin get U7QGw0sv parkour.lua
  3.  
  4.  
  5. pastebin run U7QGw0sv
  6. --]]
  7.  
  8. function shallowcopy(orig)
  9.     local orig_type = type(orig)
  10.     local copy
  11.     if orig_type == 'table' then
  12.         copy = {}
  13.         for orig_key, orig_value in pairs(orig) do
  14.             copy[orig_key] = orig_value
  15.         end
  16.     else -- number, string, boolean, etc
  17.         copy = orig
  18.     end
  19.     return copy
  20. end
  21.  
  22. -- Inventory Functions
  23. function checkIfSlotIsItem(slot, name)
  24.     local item = turtle.getItemDetail(slot)
  25.     if item ~= nil then
  26.         return item["name"] == name
  27.     end
  28.     return false
  29. end
  30.  
  31. function findItem(name)
  32.     for slot = 1, 16 do
  33.         if checkIfSlotIsItem(slot, name) then
  34.             return slot
  35.         end
  36.     end
  37.     return -1
  38. end
  39.  
  40. function checkIfHaveItem(name)
  41.     return findItem(name) ~= -1
  42. end
  43.  
  44. -- Movement Functions
  45. local moves = {}
  46.  
  47. function goForward()
  48.     local success = turtle.forward()
  49.     if not success then
  50.         turtle.dig()
  51.         turtle.forward()
  52.     end
  53.     moves[#moves + 1] = "f"
  54. end
  55.  
  56. function goBack()
  57.     local success = turtle.back()
  58.     if not success then
  59.         turtle.turnLeft()
  60.         turtle.dig()
  61.         turtle.forward()
  62.         turtle.turnLeft()
  63.     end
  64.     moves[#moves + 1] = "b"
  65. end
  66.  
  67. function goUp()
  68.     local success = turtle.up()
  69.     if not success then
  70.         turtle.digUp()
  71.         turtle.up()
  72.     end
  73.     moves[#moves + 1] = "u"
  74. end
  75.  
  76. function goDown()
  77.     local success = turtle.down()
  78.     if not success then
  79.         turtle.digDown()
  80.         turtle.down()
  81.     end
  82.     moves[#moves + 1] = "d"
  83. end
  84.  
  85. function turnLeft()
  86.     turtle.turnLeft()
  87.     moves[#moves + 1] = "l"
  88. end
  89.  
  90. function turnRight()
  91.     turtle.turnRight()
  92.     moves[#moves + 1] = "r"
  93. end
  94.  
  95. function doMove(str)
  96.     if str == "f" then
  97.         goForward()
  98.     elseif str == "b" then
  99.         goBack()
  100.     elseif str == "u" then
  101.         goUp()
  102.     elseif str == "d" then
  103.         goDown()
  104.     elseif str == "l" then
  105.         turnLeft()
  106.     elseif str == "r" then
  107.         turnRight()
  108.     end
  109. end
  110. -- Main
  111. print("Building Parkour!")
  112. local first = true
  113. while findItem("minecraft:cobblestone") ~= -1 do
  114.     turtle.select(findItem("minecraft:cobblestone"))
  115.     local toTurn = math.random(3)
  116.     if toTurn == 1 and not first then
  117.         if math.random(2) == 1 then
  118.             turnRight()
  119.         else
  120.             turnLeft()
  121.         end
  122.     end
  123.  
  124.     local random = math.random(3, 4)  -- second num == 5 if 4 blocks included
  125.     for i = 1, random do
  126.         goForward()
  127.         if i == 1 and first then
  128.             turtle.turnLeft()
  129.             turtle.turnLeft()
  130.             turtle.place()
  131.             turtle.turnLeft()
  132.             turtle.turnLeft()
  133.         end
  134.     end
  135.     if random ~= 5 and math.random(3) == 3 and not first then
  136.         goUp()
  137.     elseif math.random(3) == 1 then
  138.         if math.random(2) == 1 then
  139.             turnRight()
  140.             for i = 1, math.random(2) do
  141.                 goForward()
  142.             end
  143.             turnLeft()
  144.         else
  145.             turnLeft()
  146.             for i = 1, math.random(2) do
  147.                 goForward()
  148.             end
  149.             turnRight()
  150.         end
  151.     end
  152.     if first then
  153.         first = false
  154.     end
  155.     turtle.placeUp()
  156. end
  157. local initial_moves = shallowcopy(moves)
  158. -- Return to start
  159. print("Returning to start!")
  160. local opposites = {l="r", r="l", u="d", d="u", f="b", b="f"}
  161. for i = #initial_moves, 2, -1 do
  162.     local move = initial_moves[i]
  163.     doMove(opposites[move])
  164. end
  165.  
  166. sleep(10)
  167.  
  168. print("Destroying Parkour!")
  169. for i = 2, #initial_moves, 1 do
  170.     local move = initial_moves[i]
  171.     doMove(move)
  172.     turtle.digUp()
  173. end
  174.  
  175. print("Returning to start!")
  176. for i = #initial_moves, 2, -1 do
  177.     local move = initial_moves[i]
  178.     doMove(opposites[move])
  179. end
  180. turnLeft()
  181. turnLeft()
  182. turtle.dig()
  183. goForward()
  184. turnLeft()
  185. turnLeft()
  186.  
Advertisement
Add Comment
Please, Sign In to add comment