Advertisement
KroNixZ

Turtle Wandbau

Nov 27th, 2023 (edited)
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.90 KB | None | 0 0
  1. --Erstellt 27.11.2023
  2. --Version 1.0
  3. --Zuletzt geändert 04.12.2023
  4. --Wandbau Programm für eine CCraft Turtle
  5. --0.1 - Abbau der Wand (Player in the Way) Turtle fährt nicht bis zum schluss
  6. --0.2 - Test Fix des "PitW" Fehlers; Anfang des Wandbau-Modus
  7. --0.3 - Abbau Modus fertig, Bau Modus tests
  8. --0.4 - Bau Modus Fix
  9.  
  10. --
  11. --Info
  12. --
  13.  
  14. term.clear()
  15. term.setCursorPos(1,1)
  16. print("The turtle must face the wall to be built with its front and right side.")
  17. print("The room size is given in WxDxH from the turtle's perspective, not the wall size.")
  18. print("A crate behind the turtle.") --2 Modi: Blöcke im weg=vorher Dig ; im Freien, direkter Bau
  19.  
  20. --
  21. --Variables
  22. --
  23.  
  24. term.write("Width: ")
  25. local breit = tonumber(read())
  26. term.write("Depth: ")
  27. local tief = tonumber(read())
  28. term.write("Height : ")
  29. local hoch = tonumber(read())
  30. local hochv = hoch --veränderbar
  31. local hochgereist = 0 --wenn einmal rum, nach unten zu fahren und das Inv zu leeren in eine Kiste
  32. local slot = 1
  33. local q = 0 -- Anzahl der Blöcke
  34. term.write("is the path blocked?? y/n: ")
  35. local mode = string.lower(read()) --Modus für im Freien bauen oder wenn Blöcke im Weg sind
  36. local bt = breit --wechsel zwischen breit und tief
  37. local four = 0 --4x für 4 Wände
  38.  
  39. --
  40. --functions
  41. --
  42.  
  43. function checkInv()
  44.     q = 0
  45.     for i = 1, 16 do
  46.         q = q + turtle.getItemCount(i)
  47.     end
  48.     if q < 1 then
  49.         print("Inventory empty! Give me some Blocks!")
  50.         print("Press ENTER to continue.")
  51.         repeat
  52.             sleep(0.2)
  53.             local event, key = os.pullEvent( "key" )
  54.         until key == keys.enter
  55.     end
  56. end
  57.  
  58. function check()
  59.     repeat
  60.         slotSelect()
  61.         if turtle.getItemCount(slot) < 1 then
  62.             checkInv()
  63.         else
  64.             break
  65.         end
  66.     until q >= 1
  67. end
  68.  
  69. function slotSelect() --für den Bau-Modus
  70.     for i = 1, 16 do
  71.         if turtle.getItemCount(i) > 0 then
  72.             turtle.select(i)
  73.             slot = i
  74.             break
  75.         end
  76.     end
  77. end
  78.  
  79. function dig()
  80.     if hochv >= 3 then
  81.         dig3()
  82.         elseif hochv == 2 then
  83.         dig2()
  84.         elseif hochv == 1 then
  85.         dig1()
  86.     end
  87. end
  88.  
  89. function dig1()
  90.     turtle.dig()
  91. end
  92.  
  93. function dig2()
  94.     turtle.dig()
  95.     turtle.digDown()
  96. end
  97.  
  98. function dig3()
  99.     turtle.dig()
  100.     turtle.digDown()
  101.     turtle.digUp()
  102. end
  103.  
  104. function place()
  105.     if hochv >= 3 then
  106.         place3()
  107.         elseif hochv == 2 then
  108.         place2()
  109.         elseif hochv == 1 then
  110.         place1()
  111.     end
  112. end
  113.  
  114. function place1()
  115.     check()
  116.     turtle.place()
  117. end
  118.  
  119. function place2()
  120.     check()
  121.     turtle.placeDown()
  122.     check()
  123.     turtle.place()
  124. end
  125.  
  126. function place3()
  127.     check()
  128.     turtle.placeDown()
  129.     check()
  130.     turtle.placeUp()
  131.     check()
  132.     turtle.place()
  133. end
  134.  
  135. function placefirst()
  136.     if hochv >= 3 then
  137.         placefirst3()
  138.         elseif hochv == 2 then
  139.         placefirst2()
  140.         elseif hochv == 1 then
  141.         placefirst1()
  142.     end
  143. end
  144.  
  145. function placefirst1()
  146.     if hochgereist >=3 then
  147. --        turtle.place()
  148.     else
  149.         sleep(0.1)
  150.     end
  151. end
  152.  
  153. function placefirst2()
  154.     if hochgereist >=3 then
  155.         check()
  156.         turtle.placeDown()
  157. --        turtle.place()
  158.     else
  159.         sleep(0.1)
  160.     end
  161. end
  162.  
  163. function placefirst3()
  164.     if hochgereist >=3 then
  165.         check()
  166.         turtle.placeDown()
  167.         check()
  168.         turtle.placeUp()
  169. --        turtle.place()
  170.     else
  171.         check()
  172.         turtle.placeUp()
  173.     end
  174. end
  175.  
  176. function placelast()
  177.     check()
  178.     turtle.place()
  179.     sleep(0.1) -- Temporär? (für immer)
  180. end
  181.  
  182. function placefirstone()
  183.     sleep(0.1)
  184. end
  185.  
  186. function starta() -- Start Abbau-Modus -- MUSS AUCH REDUNDAND WERDEN SIEHE "PitW" Fehler
  187.     turtle.dig()
  188.     turtle.forward()
  189.     turtle.turnRight()
  190.     turtle.dig()
  191.     turtle.forward()
  192.     around()
  193.     if hoch > 1 then
  194.         turtle.digUp()
  195.         up()
  196.     end
  197. end
  198.  
  199. function startb() -- Start Bau-Modus -- MUSS AUCH REDUNDAND WERDEN SIEHE "PitW" Fehler
  200.     turtle.forward()
  201.     turtle.turnRight()
  202.     turtle.forward()
  203.     if hoch > 1 then
  204.         up()
  205.     end
  206. end
  207.  
  208. function around()
  209.     turtle.turnRight()
  210.     turtle.turnRight()
  211. end
  212.  
  213. function up() -- MUSS AUCH REDUNDAND WERDEN SIEHE "PitW" Fehler
  214.     turtle.digUp()
  215.     turtle.up()
  216.     hochgereist = hochgereist + 1
  217. end
  218.  
  219. function down() -- MUSS AUCH REDUNDAND WERDEN SIEHE "PitW" Fehler
  220.     turtle.down()
  221.     hochgereist = hochgereist - 1
  222. end
  223.  
  224. function home() --return to Home Position -- MUSS AUCH REDUNDAND WERDEN SIEHE "PitW" Fehler
  225.     if hochgereist >= 1 then
  226.         repeat
  227.             down()
  228.         until hochgereist == 0
  229.     end
  230.     turtle.forward()
  231.     turtle.turnLeft()
  232.     turtle.forward()
  233.     around()
  234. end
  235.  
  236. function home2()
  237.     turtle.back()
  238.     check()
  239.     turtle.place()
  240.     turtle.turnLeft()
  241.     turtle.back()
  242.     check()
  243.     turtle.place()
  244. end
  245.  
  246. function ecke()
  247.     if hochgereist >= 1 then
  248.         if hochgereist + 1 < hoch then
  249.             check()
  250.             turtle.placeUp()
  251.         end
  252.         repeat
  253.             down()
  254.             check()
  255.             turtle.placeUp()
  256.         until hochgereist == 0
  257.     end
  258. end
  259.  
  260. --
  261. --Main Code
  262. --
  263.  
  264. -- ABBAU MODUS
  265. if mode == "y" then -- IF01START--abbau Modus
  266.     starta() -- start Abbau-Modus
  267.     while hochv >= 1 do -- WHILE01START -- Muss vielleicht angeasst werden
  268.         repeat -- REPEAT01START --4x für 4 Wände
  269.             local a = 0
  270.             local a = -1 --hier muss geschaut werden ob es 3,4 oder 5 "Wände" insgesamt sind
  271.             repeat --REPEAT02START --Wand abbauen
  272.                 dig()
  273.                 local count = 0
  274.                 while not turtle.forward() do -- WHILE02START
  275.                     dig()
  276.                     sleep(0.2)
  277.                     turtle.attack()
  278.                     sleep(0.2)
  279.                 end -- WHILE02END
  280.                 a = a + 1
  281.             until a >= bt -- REPEAT02END --Wand abbauen
  282.             if bt == breit then -- IF03START --wechsel zwischen breit und tief
  283.                 bt = tief
  284.             else
  285.                 bt = breit
  286.             end -- IF03END
  287.             turtle.turnLeft()
  288.             sleep(0.1)
  289.             four = four + 1
  290.         until four >= 4 -- REPEAT01END --4x für 4 Wände
  291.         four = 0
  292.         if hochv <= 0 then -- IF04START
  293.             print("hochv <0")
  294.             break
  295.         elseif hochv == 1 then
  296.             hochv = hochv - 1
  297.         elseif hochv == 2 then
  298.             hochv = hochv - 2
  299.         elseif hochv >= 3 then
  300.             hochv = hochv - 3
  301.         end -- IF04END
  302.         if hochv >= 2 then -- IF05START
  303.             up()
  304.             up()
  305.             up()
  306.         elseif hochv == 1 then
  307.             up()
  308.             up()
  309.         elseif hochv <= 0 then
  310.             break
  311.         end -- IF05END
  312.     end -- WHILE01END
  313.     home()
  314.     print("")
  315.     bt = breit -- neu-indexierung --wechsel zwischen breit und tief
  316.     hochv = hoch --veränderbar
  317.     four = 0 --4x für 4 Wände
  318.     hochgereist = 0
  319.     term.write("In den Bau Modus wechseln? y: ")
  320.     local xy = string.lower(read())
  321.     if xy == "y" then
  322.         mode = "n"
  323.         print("Jetzt brauche ich die Bau-Blöcke")
  324.         print("")
  325.     else
  326.         print("")
  327.         term.write("Do you want to Exit? y: ")
  328.         xy = string.lower(read())
  329.         if xy == "y" then
  330.             mode = "false"
  331.         else
  332.             print("")
  333.             term.write("In den Bau Modus wechseln? y: ")
  334.             local xy = string.lower(read())
  335.             if xy == "y" then
  336.                 mode = "n"
  337.                 print("Jetzt brauche ich die Bau-Blöcke")
  338.                 print("")
  339.             else
  340.                 mode = "false"
  341.             end
  342.         end
  343.     end
  344. end -- IF01END
  345.  
  346. -- BAU MODUS
  347. if mode == "n" then --bau Modus
  348.     startb()
  349.     while hochv >= 1 do -- WHILE01START -- Muss vielleicht angeasst werden
  350.         local first = 0 --damit der Block freigelassen word für home()
  351.         repeat -- REPEAT01START --4x für 4 Wände
  352.             local a = 0
  353.             local a = -1 --hier muss geschaut werden ob es 3,4 oder 5 "Wände" insgesamt sind
  354.             repeat --REPEAT02START --Wand abbauen
  355.                 local count = 0
  356.                 while not turtle.back() do -- WHILE02START
  357.                     sleep(0.2)
  358.                     count = count + 1
  359.                     if count >= 10 then -- IF02START
  360.                         around()
  361.                         turtle.attack()
  362.                         around()
  363.                         count = 0
  364.                     end -- IF02END
  365.                 end -- WHILE02END
  366.                 if (first == 1) and (hochv == 1) and (hoch == 1) then -- IF11START
  367.                     placefirstone()
  368.                     first = 2
  369.                 elseif first == 0 then
  370.                     placefirst()
  371.                     first = 1
  372.                 elseif (four == 3) and (a >= bt - 1) then
  373.                     placelast()
  374.                 else
  375.                     place()
  376.                 end -- IF11END
  377.                 a = a + 1
  378.             until a >= bt -- REPEAT02END --Wand abbauen
  379.             if bt == breit then -- IF03START --wechsel zwischen breit und tief
  380.                 bt = tief
  381.             else
  382.                 bt = breit
  383.             end -- IF03END
  384.             turtle.turnLeft()
  385.             four = four + 1
  386.         until four >= 4 -- REPEAT01END --4x für 4 Wände
  387.         four = 0
  388.         if hochv <= 0 then -- IF04START
  389.             print("hochv <0")
  390.             break
  391.         elseif hochv == 1 then
  392.             hochv = hochv - 1
  393.         elseif hochv == 2 then
  394.             hochv = hochv - 2
  395.         elseif hochv >= 3 then
  396.             hochv = hochv - 3
  397.         end -- IF04END
  398.         if hochv >= 2 then -- IF05START
  399.             up()
  400.             up()
  401.             up()
  402.         elseif hochv == 1 then
  403.             up()
  404.             up()
  405.         elseif hochv <= 0 then
  406.             break
  407.         end -- IF05END
  408.     end -- WHILE01END
  409. ecke()
  410. home2()
  411. else
  412.     print("End")
  413. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement