MatthewJ217

Self library (v1)

Nov 5th, 2023 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. -- self library for turtles for enhanced movement and abstracted turtle self-management
  2.  
  3. local self = {
  4. craft = turtle.craft,
  5. --forward = turtle.forward,
  6. --back = turtle.back,
  7. --up = turtle.up,
  8. --down = turtle.down,
  9. --turnLeft = turtle.turnLeft,
  10. --turnRight = turtle.turnRight,
  11. dig = turtle.dig,
  12. digUp = turtle.digUp,
  13. digDown = turtle.digDown,
  14. --place = turtle.place,
  15. --placeUp = turtle.placeUp,
  16. --placeDown = turtle.placeDown,
  17. drop = turtle.drop,
  18. dropUp = turtle.dropUp,
  19. dropDown = turtle.dropDown,
  20. select = turtle.select,
  21. getItemCount = turtle.getItemCount,
  22. getItemSpace = turtle.getItemSpace,
  23. detect = turtle.detect,
  24. detectUp = turtle.detectUp,
  25. detectDown = turtle.detectDown,
  26. compare = turtle.compare,
  27. compareUp = turtle.compareUp,
  28. compareDown = turtle.compareDown,
  29. attack = turtle.attack,
  30. attackUp = turtle.attackUp,
  31. attackDown = turtle.attackDown,
  32. suck = turtle.suck,
  33. suckUp = turtle.suckUp,
  34. suckDown = turtle.suckDown,
  35. getFuelLevel = turtle.getFuelLevel,
  36. refuel = turtle.refuel,
  37. compareTo = turtle.compareTo,
  38. transferTo = turtle.transferTo,
  39. getSelectedSlot = turtle.getSelectedSlot,
  40. getFuelLimit = turtle.getFuelLimit,
  41. equipLeft = turtle.equipLeft,
  42. equipRight = turtle.equipRight,
  43. inspect = turtle.inspect,
  44. inspectUp = turtle.inspectUp,
  45. inspectDown = turtle.inspectDown,
  46. getItemDetail = turtle.getItemDetail
  47. }
  48.  
  49. local expect = require "cc.expect"
  50.  
  51. -- Enhanced movement
  52.  
  53. self.forced = false
  54.  
  55. self.forward = function()
  56. local b, res = turtle.forward()
  57. if self.forced then
  58. while not b do
  59. if not self.dig() then return false, "Unremovable obstruction" end
  60. b, res = turtle.forward()
  61. end
  62. end
  63. if self.track and b then
  64. if self.position.h == 0 then
  65. self.position.x = self.position.x + 1
  66. elseif self.position.h == 1 then
  67. self.position.z = self.position.z + 1
  68. elseif self.position.h == 2 then
  69. self.position.x = self.position.x - 1
  70. elseif self.position.h == 3 then
  71. self.position.z = self.position.z - 1
  72. end
  73. end
  74. -- print("Tracking: x " .. self.position.x .. ", y " .. self.position.y .. ", z " .. self.position.z .. ", h " .. self.position.h);
  75. return b, res;
  76. end
  77.  
  78. self.back = function()
  79. local b, res = turtle.back()
  80. if self.forced and not b then
  81. self.turnRight()
  82. self.turnRight()
  83. while not b do
  84. if not self.dig() then return false, "Unremovable obstruction" end
  85. b, res = turtle.forward()
  86. end
  87. self.turnRight()
  88. self.turnRight()
  89. end
  90. if self.track and b then
  91. if self.position.h == 0 then
  92. self.position.x = self.position.x - 1
  93. elseif self.position.h == 1 then
  94. self.position.z = self.position.z - 1
  95. elseif self.position.h == 2 then
  96. self.position.x = self.position.x + 1
  97. elseif self.position.h == 3 then
  98. self.position.z = self.position.z + 1
  99. end
  100. end
  101. -- print("Tracking: x " .. self.position.x .. ", y " .. self.position.y .. ", z " .. self.position.z .. ", h " .. self.position.h);
  102. return b, res;
  103. end
  104.  
  105. self.up = function()
  106. local b, res = turtle.up()
  107. if self.forced then
  108. while not b do
  109. if not self.digUp() then return false, "Unremovable obstruction" end
  110. b, res = turtle.up()
  111. end
  112. end
  113. if self.track and b then
  114. self.position.y = self.position.y + 1
  115. end
  116. -- print("Tracking: x " .. self.position.x .. ", y " .. self.position.y .. ", z " .. self.position.z .. ", h " .. self.position.h);
  117. return b, res;
  118. end
  119.  
  120. self.down = function()
  121. local b, res = turtle.down()
  122. if self.forced then
  123. while not b do
  124. if not self.digDown() then return false, "Unremovable obstruction" end
  125. b, res = turtle.down()
  126. end
  127. end
  128. if self.track and b then
  129. self.position.y = self.position.y - 1
  130. end
  131. -- print("Tracking: x " .. self.position.x .. ", y " .. self.position.y .. ", z " .. self.position.z .. ", h " .. self.position.h);
  132. return b, res;
  133. end
  134.  
  135. self.turnLeft = function()
  136. if self.track then
  137. self.position.h = (self.position.h+3)%4
  138. end
  139. -- print("Tracking: x " .. self.position.x .. ", y " .. self.position.y .. ", z " .. self.position.z .. ", h " .. self.position.h);
  140. return turtle.turnLeft();
  141. end
  142.  
  143. self.turnRight = function()
  144. if self.track then
  145. self.position.h = (self.position.h+1)%4
  146. end
  147. -- print("Tracking: x " .. self.position.x .. ", y " .. self.position.y .. ", z " .. self.position.z .. ", h " .. self.position.h);
  148. return turtle.turnRight();
  149. end
  150.  
  151. -- Auto-increment slots
  152. self.autoIncrementSlot = false;
  153.  
  154. self.place = function()
  155. if self.autoIncrementSlot then
  156. if turtle.getItemCount() == 0 then
  157. turtle.select(turtle.getSelectedSlot()%16+1)
  158. end
  159. end
  160. return turtle.place()
  161. end
  162.  
  163.  
  164. self.placeUp = function()
  165. if self.autoIncrementSlot then
  166. if turtle.getItemCount() == 0 then
  167. turtle.select(turtle.getSelectedSlot()%16+1)
  168. end
  169. end
  170. return turtle.placeUp()
  171. end
  172.  
  173.  
  174. self.placeDown = function()
  175. if self.autoIncrementSlot then
  176. if turtle.getItemCount() == 0 then
  177. turtle.select(turtle.getSelectedSlot()%16+1)
  178. end
  179. end
  180. return turtle.placeDown()
  181. end
  182.  
  183. -- location handling
  184.  
  185. self.track = true;
  186.  
  187. self.position = {
  188. x = 0,
  189. z = 0,
  190. y = 0,
  191. h = 0
  192. }
  193.  
  194. self.resetTracking = function()
  195. self.position.x = 0;
  196. self.position.z = 0;
  197. self.position.y = 0;
  198. self.position.h = 0;
  199. end
  200.  
  201. --self.markedPositions = {};
  202.  
  203. --self.markPosition = function()
  204. -- self.markedPositions[table.getn(markedPositions)] = {
  205. -- x = self.positions.x,
  206. -- z = self.positions.z,
  207. -- y = self.positions.y,
  208. -- h = self.positions.h,
  209. -- }
  210. --end
  211.  
  212. --self.clearMarkedPositions = function()
  213. -- self.markedPositions = {};
  214. --end
  215.  
  216. self.getPosition = function()
  217. return {
  218. x = self.position.x,
  219. z = self.position.z,
  220. y = self.position.y,
  221. h = self.position.h,
  222. }
  223. end
  224.  
  225. self.goToPosition = function(posOrX, z, y, h)
  226. local pos = {}
  227. expect(1, posOrX, "number", "table")
  228. if type(posOrX) == "number" then
  229. pos.x = posOrX
  230. expect(2, z, "number")
  231. pos.z = z
  232. expect(3, y, "number")
  233. pos.y = y
  234. expect(4, h, "number", "nil")
  235. pos.h = h
  236. if h == nil then h = self.position.h end
  237. else
  238. pos = posOrX;
  239. end
  240.  
  241. while self.position.y < pos.y do
  242. if not self.up() then return self.up() end
  243. end
  244. if self.position.z ~= pos.z then
  245. while self.position.h ~= 1 do
  246. if not self.turnLeft() then return self.turnLeft() end
  247. end
  248. end
  249. while self.position.h ~= 1 do
  250. if not self.turnRight() then return self.turnRight() end
  251. end
  252. while self.position.z > pos.z do
  253. if not self.back() then return self.back() end
  254. end
  255. while self.position.z < pos.z do
  256. if not self.forward() then return self.forward() end
  257. end
  258. if self.position.x ~= pos.x then
  259. while self.position.h ~= 0 do
  260. if not self.turnLeft() then return self.turnLeft() end
  261. end
  262. end
  263. while self.position.x > pos.x do
  264. if not self.back() then return self.back() end
  265. end
  266. while self.position.x < pos.x do
  267. if not self.forward() then return self.forward() end
  268. end
  269. while self.position.h ~= pos.h do
  270. if not self.turnLeft() then return self.turnLeft() end
  271. end
  272. while self.position.y > pos.y do
  273. if not self.down() then return self.down() end
  274. end
  275. return true, self.getPosition()
  276. end
  277.  
  278. return self
  279.  
Add Comment
Please, Sign In to add comment