Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. --FUNC
  2. function point(x,y)
  3. return {["x"]=x,["y"]=y}
  4. end
  5.  
  6. function isEqual(a,b)
  7. return ((a.x == b.x) and (a.y == b.y))
  8. end
  9.  
  10. function format(a)
  11. return '('..a.x..', '..a.y..')'
  12. end
  13. --
  14.  
  15. function forward()
  16. while not turtle.forward() do
  17. if not turtle.dig() then
  18. turtle.attack()
  19. end
  20. sleep(0.1)
  21. end
  22. end
  23.  
  24. function up()
  25. while not turtle.up() do
  26. if not turtle.digUp() then
  27. turtle.attackUp()
  28. end
  29. sleep(0.1)
  30. end
  31. end
  32.  
  33. function down()
  34. while not turtle.down() do
  35. if not turtle.digDown() then
  36. turtle.attackDown()
  37. end
  38. sleep(0.1)
  39. end
  40. end
  41. --
  42.  
  43. function face(cur, dest)
  44. while not (cur == dest) do
  45. if math.abs(((cur+1)%4)-dest) <= math.abs(((cur-1))%4-dest) then
  46. turtle.turnRight()
  47. cur = cur+1
  48. else
  49. turtle.turnLeft()
  50. cur = cur-1
  51. end
  52. cur = cur%4
  53. end
  54. return cur
  55. end
  56.  
  57. function goTo(move, pos, f, dest)
  58. if not isEqual(pos, dest) then
  59. if (pos.y > 0 and dest.y > pos.y) or ( pos.y < 0 and dest.y < pos.y) then
  60. while not (pos.x == dest.x) do
  61. if pos.x < dest.x then
  62. pos, f = move.Xp(pos, f)
  63. elseif pos.x > dest.x then
  64. pos, f = move.Xm(pos, f)
  65. end
  66. end
  67.  
  68. while not (pos.y == dest.y) do
  69. if pos.y < dest.y then
  70. pos, f = move.Yp(pos, f)
  71. elseif pos.y > dest.y then
  72. pos, f = move.Ym(pos, f)
  73. end
  74. end
  75. else
  76. while not (pos.y == dest.y) do
  77. if pos.y < dest.y then
  78. pos, f = move.Yp(pos, f)
  79. elseif pos.y > dest.y then
  80. pos, f = move.Ym(pos, f)
  81. end
  82. end
  83.  
  84. while not (pos.x == dest.x) do
  85. if pos.x < dest.x then
  86. pos, f = move.Xp(pos, f)
  87. elseif pos.x > dest.x then
  88. pos, f = move.Xm(pos, f)
  89. end
  90. end
  91. end
  92. end
  93. return pos, f
  94. end
  95. --
  96.  
  97. local tArgs = { ... }
  98. if #tArgs < 2 then
  99. print("Use: digCylinder <radius> <height> <direction>")
  100. return
  101. end
  102. if tonumber(tArgs[1]) == nil then
  103. print("Error: argument <radius> is not a number")
  104. return
  105. elseif tonumber(tArgs[2]) == nil then
  106. print("Error: argument <radius> is not a number")
  107. return
  108. elseif tArgs[3] ~= "up" and tArgs[3] ~= "down" and tArgs[3] ~= "front" then
  109. print("<direction> must be up, down or front")
  110. return
  111. end
  112. tArgs[1] = tonumber(tArgs[1])
  113. tArgs[2] = tonumber(tArgs[2])
  114.  
  115. do
  116. local fuel = math.ceil((2*math.pi*(tArgs[1]^2)*tArgs[2])+tArgs[2]+2*tArgs[1])
  117. if turtle.getFuelLevel() < fuel then
  118. print("Not enough fuel. Need at least "..fuel.." units (rough estimate)")
  119. return
  120. end
  121. end
  122.  
  123. --
  124. local pts = {}
  125. print("Calculating pixels")
  126. do --Locals
  127. local E, X, Y
  128. E = -tArgs[1]
  129. X = tArgs[1]
  130. Y = 0
  131.  
  132. while Y <= X do
  133. table.insert(pts, point(X, Y))
  134. E = E+2*Y+1
  135. Y = Y+1
  136. if E >= 0 then
  137. E = E-2*X-1
  138. X = X-1
  139. end
  140. end
  141.  
  142. --First pixels
  143. for i = #pts, 1, -1 do
  144. table.insert(pts, point(pts[i].y, pts[i].x)) --On inverse les coordonnees
  145. end
  146.  
  147. --Finishing quarter
  148. for i = #pts-1, 1, -1 do --On saute le dernier
  149. table.insert(pts, point(-pts[i].x, pts[i].y))
  150. end
  151.  
  152. --Finishing other half
  153. for i = #pts-1, 2, -1 do --On saute le dernier et le premier
  154. table.insert(pts, point(pts[i].x, -pts[i].y))
  155. end
  156. end
  157. print("Done")
  158.  
  159. print("Defining lines...")
  160. table.sort(pts, function(a,b) return a.y > b.y end)
  161. local lines = {}
  162. do --Locals
  163. local t = {}
  164. for i = tArgs[1], -tArgs[1], -1 do
  165. table.insert(t, {})
  166. for k, v in ipairs(pts) do
  167. if i == v.y then
  168. table.insert(t[#t], v)
  169. end
  170. end
  171. table.sort(t[#t], function(a,b) return a.x > b.x end)
  172. end
  173.  
  174. for k, v in ipairs(t) do --Alternating lines starts and ends
  175. if k/2 == math.floor(k/2) then
  176. table.insert(lines, {v[1], v[#v], ["n"]=k})
  177. else
  178. table.insert(lines, {v[#v], v[1], ["n"]=k})
  179. end
  180. end
  181. end
  182. print("Done")
  183.  
  184. local pos = point(0,0)
  185. local facing = 0
  186. --[[ 0 -> y++
  187. 1 -> x++
  188. 2 -> y--
  189. 3 -> x-- ]]
  190.  
  191. local move = {}
  192. if tArgs[3] == "front" then
  193. move.Vp = function(p, f) --Vertical +
  194. f = face(f, 0)
  195. forward()
  196. return p, f
  197. end
  198. move.Vm = function(p, f) --Vertical -
  199. f = face(f, 2)
  200. forward()
  201. return p, f
  202. end
  203. move.Xp = function(p, f)
  204. f = face(f, 1)
  205. forward()
  206. p.x = p.x+1
  207. return p, f
  208. end
  209. move.Xm = function(p, f)
  210. f = face(f, 3)
  211. forward()
  212. p.x = p.x-1
  213. return p, f
  214. end
  215. move.Yp = function(p, f)
  216. up()
  217. p.y = p.y+1
  218. return p, f
  219. end
  220. move.Ym = function(p, f)
  221. down()
  222. p.y = p.y-1
  223. return p, f
  224. end
  225. elseif tArgs[3] == "up" then
  226. move.Vp = function(...)
  227. up()
  228. return ...
  229. end
  230. move.Vm = function(...)
  231. down()
  232. return ...
  233. end
  234. move.Xp = function(p, f)
  235. f = face(f, 1)
  236. forward()
  237. p.x = p.x+1
  238. return p, f
  239. end
  240. move.Xm = function(p, f)
  241. f = face(f, 3)
  242. forward()
  243. p.x = p.x-1
  244. return p, f
  245. end
  246. move.Yp = function(p, f)
  247. f = face(f, 0)
  248. forward()
  249. p.y = p.y+1
  250. return p, f
  251. end
  252. move.Ym = function(p, f)
  253. f = face(f, 2)
  254. forward()
  255. p.y = p.y-1
  256. return p, f
  257. end
  258. else
  259. move.Vp = function(...)
  260. down()
  261. return ...
  262. end
  263. move.Vm = function(...)
  264. up()
  265. return ...
  266. end
  267. move.Xp = function(p, f)
  268. f = face(f, 1)
  269. forward()
  270. p.x = p.x+1
  271. return p, f
  272. end
  273. move.Xm = function(p, f)
  274. f = face(f, 3)
  275. forward()
  276. p.x = p.x-1
  277. return p, f
  278. end
  279. move.Yp = function(p, f)
  280. f = face(f, 0)
  281. forward()
  282. p.y = p.y+1
  283. return p, f
  284. end
  285. move.Ym = function(p, f)
  286. f = face(f, 2)
  287. forward()
  288. p.y = p.y-1
  289. return p, f
  290. end
  291. end
  292.  
  293. pos, facing = goTo(move, pos, facing, lines[1][1]) --First move, don't want digging
  294.  
  295. print("Start")
  296. for i = 1, tArgs[2] do
  297. pos, facing = move.Vp(pos, facing) --Vertical +
  298.  
  299. for k, v in ipairs(lines) do
  300. --Keeping first two slots with only one item inside (we don't want cobble or dirt -> put blocks you don't want to keep in these slots)
  301. if turtle.getItemCount(1) > 0 then
  302. turtle.select(1)
  303. turtle.dropUp(turtle.getItemCount(1)-1)
  304. end
  305. if turtle.getItemCount(2) > 0 then
  306. turtle.select(2)
  307. turtle.dropUp(turtle.getItemCount(2)-1)
  308. end
  309. turtle.select(1)
  310.  
  311. print("Pos: "..format(pos).."; Dest: "..format(v[1]))
  312. pos, facing = goTo(move, pos, facing, v[1]) --Start of the line
  313.  
  314. print("Pos: "..format(pos).."; Dest: "..format(v[2]))
  315. pos,facing = goTo(move, pos, facing, v[2]) --End of the line
  316. end
  317. print("\tLayer "..i.." done")
  318.  
  319. --Reversing table
  320. if i/2 == math.floor(i/2) then
  321. table.sort(lines, function(a, b) return a.n < b.n end)
  322. else
  323. table.sort(lines, function(a, b) return a.n > b.n end)
  324. end
  325.  
  326. --We start on opposite side of line
  327. for k, v in ipairs(lines) do
  328. local buffer = v[1]
  329. lines[k][1] = v[2]
  330. lines[k][2] = buffer
  331. end
  332. end
  333.  
  334. pos, facing = goTo(move, pos, facing, point(0,0))
  335. for i = tArgs[2], 1, -1 do
  336. pos, facing = move.Vm(pos, facing)
  337. end
  338. facing = face(facing, 0)
  339. print("End")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement