Advertisement
TechManDylan

CharcoalMan v0.3

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