hunter3216

robot.lua

Mar 11th, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. -- robot is an abstraction of turtle giving some more advanced functions involving gps
  2.  
  3. -- default fuel value - reset to desired value
  4. -- notice that if below minFuelLevel intended behavior is for the turtle to continue what it's doing
  5. -- it is assumed that if you get below the minFuelLevel that the program will bring the turtle to where it can get fuel
  6. local minFuelLevel = 80
  7. function setFuelLevel(fuelLevel)
  8. if fuelLevel >= 100000 then
  9. fuelLevel = 100000
  10. else
  11. minFuelLevel = fuelLevel
  12. end
  13.  
  14. end
  15.  
  16. function getMinFuelLevel()
  17. return minFuelLevel
  18. end
  19.  
  20. -- variable that determines which slots can be refueled from
  21. -- by default its all slots
  22. local refuelSlot = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
  23. function setRefuelSlots(var)
  24. refuelSlot = var
  25. end
  26.  
  27. -- return refuelSlot
  28. function getRefuelSlots()
  29. return refuelSlot
  30. end
  31.  
  32. -- function to return pos as a table of x, y, and z
  33. function getPos()
  34. local x, y, z = gps.locate()
  35. local pos = {
  36. x = x,
  37. y = y,
  38. z = z
  39. }
  40. return pos
  41. end
  42.  
  43.  
  44. -- return r1 - r2
  45. function getPosDiff(r1, r2)
  46. local diff = {
  47. x=nil,
  48. y=nil,
  49. z=nil
  50. }
  51.  
  52. diff.x = r1.x - r2.x
  53. diff.y = r1.y - r2.y
  54. diff.z = r1.z - r2.z
  55. end
  56.  
  57. local reservesPrinted = false -- if running on reserves print only once
  58. -- refuels until above minFuelLevel
  59. function refuel()
  60. local refueled = false
  61. local outPrinted = false
  62.  
  63. while (turtle.getFuelLevel() < minFuelLevel) do
  64. refueled = false
  65.  
  66. for k,v in pairs(refuelSlot) do
  67. if turtle.refuel(1) then
  68. refueled = true
  69. break
  70. end
  71. end
  72.  
  73. if not refueled then
  74. if turtle.getFuelLevel() > 0 then
  75. if not reservesPrinted then
  76. reservesPrinted = true
  77. print("Running on reserves!")
  78. end
  79. return false
  80. else
  81. if not outPrinted then
  82. outPrinted = true
  83. print("Outta fuel!")
  84. end
  85. end
  86. end
  87. end
  88.  
  89. reservesPrinted = false
  90. return true
  91. end
  92.  
  93. -- function that checks fuel level before moving
  94. -- will try to refuel from 1st slot first then others
  95. -- returns false if it can't move
  96. -- gets stuck if out of fuel
  97. function forward()
  98. refuel()
  99. return turtle.forward()
  100. end
  101.  
  102. function back()
  103. refuel()
  104. return turtle.back()
  105. end
  106.  
  107. function up()
  108. refuel()
  109. return turtle.up()
  110. end
  111.  
  112. function down()
  113. refuel()
  114. return turtle.down()
  115. end
  116.  
  117. -- making taking turtle functions and calling them using robot api
  118. function turnLeft()
  119. turtle.turnLeft()
  120. end
  121.  
  122. function turnRight()
  123. turtle.turnRight()
  124. end
  125.  
  126. -- gets the turtle to move 1 block horizontally
  127. -- returns false if it cannot move
  128. -- tries to move all 4 directions
  129. -- if it can't then it will move up
  130. -- if it can no longer move up it will try to move all the way down
  131. -- if it can no longer move down it return to its starting pos and dir and returns false
  132. local function moveHor()
  133. local moved = false
  134. local yDiff = 0
  135.  
  136. -- tries to move, if it can't move it goes up
  137. while not moved do
  138. for i = 1, 4 do
  139. moved = forward()
  140. if not moved then
  141. turnLeft()
  142. else
  143. break
  144. end
  145. end
  146. if not moved and up() then
  147. yDiff = yDiff + 1
  148. else
  149. break
  150. end
  151. end
  152.  
  153. -- tries to move, if it can't move it goes down
  154. if not moved then
  155. -- goes to 1 position lower than where it started
  156. for i = 1, yDiff+1 do
  157. if down() then
  158. yDiff = yDiff - 1
  159. end
  160. end
  161.  
  162. -- if it was able to go to 1 position lower than it started try to move forward again
  163. if yDiff == -1 then
  164. while not moved do
  165. for i = 1, 4 do
  166. moved = forward()
  167. if not moved then
  168. turnLeft()
  169. else
  170. break
  171. end
  172. end
  173. if not moved and down() then
  174. yDiff = yDiff - 1
  175. else
  176. break
  177. end
  178. end
  179. end
  180. end
  181. return moved
  182.  
  183. end
  184.  
  185. -- function to get the turtle's direction then returns to its startPos and direction
  186. -- return 0 for east, 1 for north, 2 for west, and 3 for south
  187. -- return false for a failed attempt
  188. function getDir()
  189. local startPos = getPos()
  190. -- try to move
  191. if not moveHor() then
  192. print("Door stuck! Please! I beg you!")
  193. return false
  194. end
  195.  
  196. local newPos = getPos()
  197. local diffPos = getPosDiff(startPos, newPos)
  198.  
  199. if diffPos.x == 1 then
  200. return 2
  201. elseif diffPos.x == -1 then
  202. return 0
  203. elseif diffPos.z == 1 then
  204. return 3
  205. elseif diffPos.z == -1 then
  206. return 1
  207. else
  208. print("Something went very wrong trying to get the direction!")
  209. return false
  210. end
  211. end
  212.  
  213. -- function to move the turtle to a given x, y, z coordinate
  214. function moveTo(x, y, z)
  215. local dir = getDir()
  216. end
  217.  
  218.  
Add Comment
Please, Sign In to add comment