Advertisement
TechManDylan

CharcoalMan v0.2

Jan 29th, 2023 (edited)
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.19 KB | None | 0 0
  1. -- Start Declaring varibles
  2.  
  3. startX, startY, startZ = gps.locate()
  4.  
  5. function currentLocation()
  6.  
  7.   currentX, currentY, currentZ = gps.locate()
  8.  
  9. end
  10.  
  11. -- End Declaring varibles
  12.  
  13.  
  14. -- Start name setting section
  15. function checkLabel()
  16.   label = os.getComputerLabel()
  17.     if label then
  18.       print("Already has the name : "..label)
  19.     else
  20.       print("Does not have name")
  21.       print("Setting name to CoalMan")
  22.       os.setComputerLabel("CoalMan")
  23.   end
  24. end
  25. -- End name setting section
  26.  
  27.  
  28. -- Start Find Item function
  29.  
  30. function findItem(name)
  31.   for i=1,16 do
  32.     if turtle.getItemCount(i) > 0 then
  33.       local data = turtle.getItemDetail(i)
  34.       if data and data.name == name then
  35.         return i
  36.       end
  37.     end
  38.   end
  39.   return nil
  40. end
  41.  
  42. -- End Find Item Function
  43.  
  44. -- Start Place block function
  45.  
  46. function placeBlock(name)
  47.   local slot = findItem(name)
  48.   if slot then
  49.     turtle.select(slot)
  50.     turtle.placeDown()
  51.     return true
  52.   else
  53.     print("Error: block not found in inventory.")
  54.     return false
  55.   end
  56. end
  57.  
  58.  
  59. -- End Place block function
  60.  
  61.  
  62. -- Start find facingDirection()
  63.  
  64. -- If z plus is south 1
  65. -- if z minus is north 2
  66. -- if x minus is west 3
  67. -- if x plus is east 4
  68.  
  69. function facingDirection()
  70.  
  71.   turtle.forward()
  72.  
  73.   currentLocation()
  74.  
  75.  
  76.   if currentX > startX then
  77.     currentDirection = "east"
  78.   end
  79.  
  80.   if currentX < startX then
  81.     currentDirection = "west"
  82.   end
  83.  
  84.   if currentZ > startZ then
  85.     currentDirection = "north"
  86.   end
  87.  
  88.   if currentZ < startZ then
  89.     currentDirection = "south"
  90.   end
  91.  
  92.   print("Facing: "..currentDirection)
  93. end
  94.  
  95.  
  96. -- End find facingDirection()
  97.  
  98. -- Start turnDirection()
  99.  
  100. function turnDirection(directionToFace)
  101.  
  102.   -- Case east
  103.   if directionToFace == "east" then  
  104.     if currentDirection == "north" then
  105.       turtle.turnRight()
  106.     elseif currentDirection == "east" then
  107.       --Do nothing
  108.     elseif currentDirection == "south" then
  109.       turtle.turnRight()
  110.       turtle.turnRight()
  111.     elseif currentDirection == "west" then
  112.       turtle.turnLeft()
  113.     end
  114.   end
  115.  
  116. --Case east
  117.  
  118. --Case west
  119.   if directionToFace == "west" then
  120.     if currentDirection == "north" then
  121.       turtle.turnLeft()
  122.     elseif currentDirection == "east" then
  123.       turtle.turnRight()
  124.       turtle.turnRight()
  125.     elseif currentDirection == "south" then
  126.       turtle.turnLeft()
  127.     elseif currentDirection == "west" then
  128.       --Do nothing
  129.     end
  130.   end
  131. --Case west
  132.  
  133. --Case north
  134.   if directionToFace == "north" then
  135.     if currentDirection == "north" then
  136.       --Do nothing
  137.     elseif currentDirection == "east" then
  138.       turtle.turnRight()
  139.     elseif currentDirection == "south" then
  140.       turtle.turnRight()
  141.       turtle.turnRight()
  142.     elseif currentDirection == "west" then
  143.       turtle.turnLeft()
  144.     end
  145.   end
  146. --Case north
  147.  
  148. --Case south
  149.   if directionToFace == "south" then
  150.     if currentDirection == "north" then
  151.       turtle.turnRight()
  152.       turtle.turnRight()
  153.     elseif currentDirection == "east" then
  154.       turtle.turnLeft()
  155.     elseif currentDirection == "south" then
  156.       --Do nothing
  157.     elseif currentDirection == "west" then
  158.       turtle.turnRight()
  159.     end
  160.   end
  161. --Case south
  162. end
  163.  
  164.  
  165. -- End turnDirection()
  166.  
  167. -- Start moveTo() function
  168.  
  169. -- Get the initial location
  170. startX, startY, startZ = gps.locate()
  171.  
  172.  
  173.  
  174.  
  175. -- Function to move to the target coordinates
  176. function moveTo(goX, goZ)
  177.   -- Calculate the distance from the initial location to the target
  178.  
  179.   targetX = startX + goX
  180.   targetZ = startZ + goZ
  181.  
  182.  currentLocation()
  183.  
  184. -- If z plus is south 1
  185. -- if z minus is north 2
  186. -- if x minus is west 3
  187. -- if x plus is east 4
  188.    
  189.   if currentX > targetX then
  190.     turnDirection("west")
  191.     while currentX ~= targetX do
  192.         print("Moving west")
  193.         print(tostring(currentX))
  194.         turtle.forward()
  195.         currentLocation()
  196.       end
  197.       print("Reached X")
  198.     end
  199.  
  200.     if currentX < targetX then
  201.     turnDirection("east")
  202.     while currentX ~= targetX do
  203.         print("Moving east")
  204.         print(tostring(currentX))
  205.         turtle.forward()
  206.         currentLocation()
  207.       end
  208.       print("Reached X")
  209.     end
  210.  
  211.     if currentZ > targetZ then
  212.     turnDirection("south")
  213.     while currentX ~= targetX do
  214.         print("Moving south")
  215.         print(tostring(currentZ))
  216.         turtle.forward()
  217.         currentLocation()
  218.       end
  219.       print("Reached X")
  220.     end
  221.  
  222.     if currentZ < targetZ then
  223.     turnDirection("north")
  224.       while currentX ~= targetX do
  225.         print("Moving north")
  226.         print(tostring(currentZ))
  227.         turtle.forward()
  228.         currentLocation()
  229.       end
  230.       print("Reached X")
  231.     end
  232. end
  233.  
  234. -- End moveTo() function
  235.  
  236.  
  237.  
  238. -- Start requests materials for build.  
  239.  
  240. checkLabel()
  241. facingDirection()
  242.  
  243. -- Start Build code
  244.  
  245. turtle.up()
  246. moveTo(9, 9)
  247. placeBlock("minecraft:cobblestone")
  248. moveTo(8, 9)
  249. placeBlock("minecraft:cobblestone")
  250. moveTo(7, 9)
  251. placeBlock("minecraft:cobblestone")
  252. moveTo(6, 9)
  253. placeBlock("minecraft:cobblestone")
  254. moveTo(5, 9)
  255. placeBlock("minecraft:cobblestone")
  256. moveTo(5, 8)
  257. placeBlock("minecraft:cobblestone")
  258. moveTo(6, 8)
  259. placeBlock("minecraft:cobblestone")
  260. moveTo(7, 8)
  261. placeBlock("minecraft:cobblestone")
  262. moveTo(8, 8)
  263. placeBlock("minecraft:cobblestone")
  264. moveTo(9, 8)
  265. placeBlock("minecraft:cobblestone")
  266. moveTo(9, 7)
  267. redstone.setOutput("bottom", true)
  268. placeBlock("minecraft:hopper")
  269. moveTo(8, 7)
  270. redstone.setOutput("bottom", false)
  271. placeBlock("minecraft:cobblestone")
  272. moveTo(7, 7)
  273. placeBlock("minecraft:cobblestone")
  274. moveTo(6, 7)
  275. placeBlock("minecraft:cobblestone")
  276. moveTo(5, 7)
  277. placeBlock("minecraft:cobblestone")
  278. moveTo(5, 6)
  279. placeBlock("minecraft:cobblestone")
  280. moveTo(6, 6)
  281. placeBlock("minecraft:cobblestone")
  282. moveTo(7, 6)
  283. placeBlock("minecraft:cobblestone")
  284. moveTo(8, 6)
  285. placeBlock("minecraft:cobblestone")
  286. moveTo(9, 6)
  287. placeBlock("minecraft:cobblestone")
  288. moveTo(9, 5)
  289. placeBlock("minecraft:cobblestone")
  290. moveTo(8, 5)
  291. placeBlock("minecraft:cobblestone")
  292. moveTo(7, 5)
  293. placeBlock("minecraft:cobblestone")
  294. moveTo(6, 5)
  295. placeBlock("minecraft:cobblestone")
  296. moveTo(5, 5)
  297. placeBlock("minecraft:cobblestone")
  298. turtle.up()
  299. moveTo(4, 5)
  300. placeBlock("minecraft:cobblestone")
  301. moveTo(4, 6)
  302. placeBlock("minecraft:cobblestone")
  303. moveTo(4, 7)
  304. placeBlock("minecraft:cobblestone")
  305. moveTo(4, 8)
  306. placeBlock("minecraft:cobblestone")
  307. moveTo(4, 9)
  308. placeBlock("minecraft:cobblestone")
  309. moveTo(5, 10)
  310. placeBlock("minecraft:cobblestone")
  311. moveTo(6, 10)
  312. placeBlock("minecraft:cobblestone")
  313. moveTo(7, 10)
  314. placeBlock("minecraft:cobblestone")
  315. moveTo(8, 10)
  316. placeBlock("minecraft:cobblestone")
  317. moveTo(9, 10)
  318. placeBlock("minecraft:cobblestone")
  319. moveTo(10, 9)
  320. placeBlock("minecraft:cobblestone")
  321. moveTo(10, 8)
  322. placeBlock("minecraft:cobblestone")
  323. moveTo(10, 7)
  324. placeBlock("minecraft:cobblestone")
  325. moveTo(10, 6)
  326. placeBlock("minecraft:cobblestone")
  327. moveTo(10, 5)
  328. placeBlock("minecraft:cobblestone")
  329. moveTo(9, 4)
  330. placeBlock("minecraft:cobblestone")
  331. moveTo(8, 4)
  332. placeBlock("minecraft:cobblestone")
  333. moveTo(7, 4)
  334. placeBlock("minecraft:cobblestone")
  335. moveTo(6, 4)
  336. placeBlock("minecraft:cobblestone")
  337. moveTo(5, 4)
  338. placeBlock("minecraft:cobblestone")
  339. turtle.up()
  340. moveTo(7, 7)
  341. placeBlock("minecraft:dirt")
  342. turtle.up()
  343. placeBlock("minecraft:spruce_sapling")
  344.  
  345. -- End build code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement