Advertisement
raniel

Builder 2.0

Mar 26th, 2023 (edited)
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | Gaming | 0 0
  1. --[[
  2.     Buil everything from a string char.
  3.  
  4.     Commands characters:
  5.         - moving:
  6.             - i: move turtle forward
  7.             - j: turn turtle to left
  8.             - k: move turtle backward
  9.             - l: turn turtle to right
  10.             - u: move turtle up
  11.             - n: move turtle down
  12.        
  13.         - building:
  14.             - 0: select and place down the item in slot 1
  15.             - 1: select and place down the item in slot 2
  16.             - 2: select and place down the item in slot 3
  17.             - 3: select and place down the item in slot 4
  18.             - 4: select and place down the item in slot 5
  19.             - 5: select and place down the item in slot 6
  20.             - 6: select and place down the item in slot 7
  21.             - 7: select and place down the item in slot 8
  22.             - 8: select and place down the item in slot 9
  23.             - 9: select and place down the item in slot 10
  24.             - a: select and place down the item in slot 11
  25.             - b: select and place down the item in slot 12
  26.             - c: select and place down the item in slot 13
  27.             - d: select and place down the item in slot 14
  28.             - e: select and place down the item in slot 15
  29.             - f: select and place down the item in slot 16
  30.             - z: place down the item in last selected slot
  31.             - x: search for the first slot not empty and place down it (useful for big constructions with the same block type and you have the turtle filled with the same block)
  32. ]]--
  33.  
  34. -- get args
  35. local args = {...}
  36.  
  37. local strCommand = args[1]
  38.  
  39. local function getConstructionSlot()
  40.     for i = 1,16 do
  41.         if turtle.getItemCount(i) > 0 then
  42.             return i
  43.         end
  44.     end
  45.  
  46.     return 0
  47. end
  48.  
  49. local function selectConstructionSlot()
  50.     local slot = getConstructionSlot()
  51.  
  52.     if slot > 0 then
  53.         turtle.select(slot)
  54.     else
  55.         printError("No construction material found")
  56.     end
  57. end
  58.  
  59. local function turtleMove(command)
  60.     if command == 'i' then
  61.         return turtle.forward()
  62.     elseif command == 'k' then
  63.         return turtle.back()
  64.     elseif command == 'j' then
  65.         return turtle.turnLeft()
  66.     elseif command == 'l' then
  67.         return turtle.turnRight()
  68.     elseif command == 'u' then
  69.         return turtle.up()
  70.     elseif command == 'n' then
  71.         return turtle.down()
  72.     end
  73.  
  74.     return false
  75. end
  76.  
  77. local function selectInventory(command)
  78.     if command == '0' then
  79.         return turtle.select(1)
  80.     elseif command == '1' then
  81.         return turtle.select(2)
  82.     elseif command == '2' then
  83.         return turtle.select(3)
  84.     elseif command == '3' then
  85.         return turtle.select(4)
  86.     elseif command == '4' then
  87.         return turtle.select(5)
  88.     elseif command == '5' then
  89.         return turtle.select(6)
  90.     elseif command == '6' then
  91.         return turtle.select(7)
  92.     elseif command == '7' then
  93.         return turtle.select(8)
  94.     elseif command == '8' then
  95.         return turtle.select(9)
  96.     elseif command == '9' then
  97.         return turtle.select(10)
  98.     elseif command == 'a' then
  99.         return turtle.select(11)
  100.     elseif command == 'b' then
  101.         return turtle.select(12)
  102.     elseif command == 'c' then
  103.         return turtle.select(13)
  104.     elseif command == 'd' then
  105.         return turtle.select(14)
  106.     elseif command == 'e' then
  107.         return turtle.select(15)
  108.     elseif command == 'f' then
  109.         return turtle.select(16)
  110.     end
  111.  
  112.     return false
  113. end
  114.  
  115. for i = 1, #strCommand do
  116.     local c = strCommand:sub(i, i)
  117.  
  118.     if c == 'z' or selectInventory(c) then
  119.         -- place
  120.         turtle.placeDown()
  121.     elseif c == 'x' then
  122.         -- place
  123.         selectConstructionSlot()
  124.         turtle.placeDown()
  125.     elseif turtleMove(c) then
  126.         -- move
  127.     else
  128.         -- invalid char
  129.         print("Invalid char command "..c)
  130.     end
  131. end
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement