Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. -- TODO: Implement mineSpiral() and mineExcavate()
  2. -- TIP: Place Repeater to the left of the turtle, when spiral mining for it to be safe
  3.  
  4. -- General config
  5.  
  6. local mode = "spiral" -- Has to be either "spiral" or "excavate"
  7. local modemPos = "right" -- Has to be configured
  8.  
  9. -- Excavate config (starts at the bottom left corner of designated area)
  10.  
  11. local depth = 100
  12. local height = 3;
  13. local width = 20;
  14.  
  15. -- Spiral mining config
  16.  
  17. local radius = 10 -- Max spiral side length
  18. local safeRadius = 5 -- Spiral side length on start
  19. local height = 5 -- Multiplied by two -> number of spirals in total
  20.  
  21. -- Wireless transmission config
  22.  
  23. local posChannel = 12
  24. local oreStatChannel = 14
  25. local replyChannel = 0
  26.  
  27. -- ===================================
  28. -- ===================================
  29.  
  30. -- Mapping turtle position along a new x, y, z coordinate system (turtle starts at 0, 0, 0 facing positive x)
  31.  
  32. local posX = 0
  33. local posY = 0
  34. local posZ = 0
  35.  
  36. local facing = 0; -- 0: positive x / 1: positive z / 2: negative x / 3: negative z
  37.  
  38. local minedOres = 0
  39.  
  40. local warnedPosChannel = false
  41. local warnedOreStatChannel = false
  42.  
  43. -- Wireless networking methods
  44.  
  45. local modem = peripheral.wrap(modemPos)
  46. modem.open(replyChannel)
  47.  
  48. function transmitPos()
  49. if modem.isOpen(posChannel) then
  50. modem.transmit(posChannel, replyChannel, vector.new(posX, posY, posZ))
  51. elseif warnedPosChannel == false then
  52. print("[Warning] posChannel couldn't be opened, check config")
  53. warnedPosChannel = true
  54. end
  55. end
  56.  
  57. function transmitOreStat()
  58. if modem.isOpen(oreStatChannel) then
  59. modem.transmit(oreStatChannel, replyChannel, minedOres)
  60. elseif warnedOreStatChannel == false then
  61. print("[Warning] oreStatChannel couldn't be opened, check config")
  62. warnedOreStatChannel = true
  63. end
  64. end
  65.  
  66. -- Statistics methods
  67.  
  68. function increaseOreStat()
  69. minedOres = minedOres + 1
  70. transmitOreStat()
  71. end
  72.  
  73. -- Ore inspection methods
  74.  
  75. function isOreUp()
  76. local success, block = turtle.inspectUp()
  77.  
  78. if success then
  79. if string.match(block.name, "ore") or string.match(block.name, "Ore") then
  80. return true
  81. end
  82. end
  83. end
  84.  
  85. function isOreDown()
  86. local success, block = turtle.inspectDown()
  87.  
  88. if success then
  89. if string.match(block.name, "ore") or string.match(block.name, "Ore") then
  90. return true
  91. end
  92. end
  93. end
  94.  
  95. function isOre()
  96. local success, block = turtle.inspect()
  97.  
  98. if success then
  99. if string.match(block.name, "ore") or string.match(block.name, "Ore") then
  100. return true
  101. end
  102. end
  103. end
  104.  
  105. -- Movement/mining methods
  106.  
  107. function forceUp()
  108. turtle.digUp()
  109. moveUp()
  110. end
  111.  
  112. function forceForward()
  113. turtle.dig()
  114. moveForward()
  115. end
  116.  
  117. function forceDown()
  118. turtle.digDown()
  119. moveDown()
  120. end
  121.  
  122. function moveUp()
  123. turtle.up()
  124. posY = posY + 1
  125.  
  126. transmitPos()
  127. end
  128.  
  129. function moveDown()
  130. turtle.down()
  131. posY = posY - 1
  132.  
  133. transmitPos()
  134. end
  135.  
  136. function moveForward()
  137. turtle.forward()
  138.  
  139. if facing == 0 then
  140. posX = posX + 1
  141. elseif facing == 1 then
  142. posZ = posZ + 1
  143. elseif facing == 2 then
  144. posX = posX - 1
  145. elseif facing == 3 then
  146. posZ = posZ - 1
  147. end
  148.  
  149. transmitPos()
  150. end
  151.  
  152. function moveBack()
  153. turtle.back()
  154.  
  155. if facing == 0 then
  156. posX = posX - 1
  157. elseif facing == 1 then
  158. posZ = posZ - 1
  159. elseif facing == 2 then
  160. posX = posX + 1
  161. elseif facing == 3 then
  162. posZ = posZ + 1
  163. end
  164.  
  165. transmitPos()
  166. end
  167.  
  168. function turnRight()
  169. turtle.turnRight()
  170.  
  171. if facing < 3 then
  172. facing = facing + 1
  173. else
  174. facing = 0
  175. end
  176. end
  177.  
  178. function turnLeft()
  179. turtle.turnLeft()
  180.  
  181. if facing > 0 then
  182. facing = facing - 1
  183. else
  184. facing = 3
  185. end
  186. end
  187.  
  188. function turnAround()
  189. turnLeft()
  190. turnLeft()
  191. end
  192.  
  193. -- Mining methods
  194.  
  195. function mineVein()
  196. if isOreUp() then
  197. increaseOreStat()
  198.  
  199. forceUp()
  200. mineVein()
  201. moveDown()
  202. end
  203.  
  204. if isOreDown() then
  205. increaseOreStat()
  206.  
  207. forceDown()
  208. mineVein()
  209. moveUp()
  210. end
  211.  
  212. for i=0, 3 do
  213. if isOre() then
  214. increaseOreStat()
  215.  
  216. forceForward()
  217. mineVein()
  218. moveBack()
  219. end
  220.  
  221. turnRight()
  222. end
  223. end
  224.  
  225. local s = safeRadius + 2;
  226.  
  227. function mineSpiralForward()
  228. while s < radius do
  229. for i=0, s do
  230. mineVein()
  231. forceForward()
  232. end
  233.  
  234. turnLeft()
  235.  
  236. for i=0, s do
  237. mineVein()
  238. forceForward()
  239. end
  240.  
  241. turnLeft()
  242.  
  243. s = s + 3;
  244. end
  245. end
  246.  
  247. function mineSpiralBackward()
  248. turnAround()
  249.  
  250. while s > (safeRadius + 2) do
  251. for i=0, s do
  252. mineVein()
  253. forceForward()
  254. end
  255.  
  256. turnLeft()
  257.  
  258. for i=0, s do
  259. mineVein()
  260. forceForward()
  261. end
  262.  
  263. turnLeft()
  264.  
  265. s = s - 3;
  266. end
  267.  
  268. turnAround()
  269. end
  270.  
  271. function mineSpiral()
  272. local h = 0;
  273.  
  274. while h < (height * 2) do
  275. mineSpiralForward()
  276. forceUp()
  277. h = h + 1;
  278.  
  279. mineSpiralBackward()
  280. forceUp()
  281. h = h + 1;
  282. end
  283.  
  284. print("[Note] Done mining! Now coming home...")
  285.  
  286. while h > 0 do
  287. forceDown()
  288. h = h - 1;
  289. end
  290. end
  291.  
  292. function mineExcavate()
  293. -- TODO: To be implemented
  294. end
  295.  
  296. -- Position retrieval methods
  297.  
  298. function getPosVector()
  299. return tostring(vector.new(posX, posY, posZ))
  300. end
  301.  
  302. function getDistanceToStart()
  303. return tostring(math.sqrt(math.pow(posX, 2) + math.pow(posY, 2) + math.pow(posZ, 2)))
  304. end
  305.  
  306. -- Main method
  307.  
  308. function main()
  309. print("==== Fredriks Miner v1.0 ====")
  310. print()
  311.  
  312. if mode == "spiral" then
  313. mineSpiral()
  314. elseif mode == "excavate" then
  315. mineExcavate()
  316. else
  317. print("[Error] Please set mode to either 'spiral' or 'excavate'")
  318. end
  319. end
  320.  
  321. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement