Advertisement
2222Jonathan

JMove

Dec 6th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. os.loadAPI("JAPI/JUtils.lua")
  2.  
  3. FORWARD = 0
  4. LEFT = 1
  5. BACK = 2
  6. RIGHT = 3
  7.  
  8. POS_Y = 0
  9. NEG_X = 1
  10. NEG_Y = 2
  11. POS_X = 3
  12.  
  13. local DIR_TYPES_STR = { "FORWARD", "LEFT", "BACK", "RIGHT", }
  14. local DIR_TYPES_STR_V2 = { "POS_Y", "NEG_X", "NEG_Y", "POS_X", }
  15. -- +y (FORWARD)
  16. -- /\
  17. -- |
  18. -- |
  19. -- |
  20. -- ---------> +x (RIGHT)
  21.  
  22. local cx = 0 -- X position
  23. local cy = 0 -- Y position
  24. local cz = 0 -- Z position
  25. local cdir = 0 -- 0: FORWAD, 1: LEFT, 2: BACK, 3: RIGHT
  26. local dirDisplacement = 0
  27.  
  28. local refuelAmount = 1
  29. local minFuelLevel = 5
  30. local fuelType = "minecraft:coal"
  31.  
  32. -- Set the position (x, y, z), it will not move to it.
  33. function setPos(x, y, z)
  34. cx = x
  35. cy = y
  36. cz = z
  37. end
  38.  
  39. function displaceDir(dir)
  40. if not dir then JUtils.err("Error: Dir was nil in displaceDir!") end
  41. if type(dir) ~= "number" then JUtils.err("Error: Dir was a number in displaceDir! dir type:", type(dir)) end
  42. --dir = dir-dirDisplacement
  43. --if dir < 0 then dir = 4+dir end
  44. local d = (dirDisplacement+dir) % 4
  45. return d
  46. end
  47.  
  48. -- Check if dir is a valid direction, this can be a string or a number. This will return a bool and a table which holds the type and the dir.
  49. function isDir(dir)
  50. if type(dir) == "string" then
  51. local dirStr = string.upper(dir)
  52. for i = 1, #DIR_TYPES_STR do
  53. if dirStr == DIR_TYPES_STR[i] then return true, {type="string", dir=(i-1)} end
  54. end
  55. for i = 1, #DIR_TYPES_STR_V2 do
  56. if dirStr == DIR_TYPES_STR_V2[i] then return true, {type="string", dir=(i-1)} end
  57. end
  58. dir = tonumber(dir)
  59. end
  60.  
  61. if type(dir) == "number" then
  62. local last = #DIR_TYPES_STR-1
  63. for i = 0, last do
  64. if dir == i then return true, {type="number", dir=(i)} end
  65. end
  66. end
  67. return false, nil
  68. end
  69.  
  70. local function getDirNum(dir)
  71. local d = -1
  72. local success, data = isDir(dir)
  73. if success then
  74. if data.type == "number" then d = dir
  75. elseif data.type == "string" then d = data.dir end
  76. else
  77. JUtils.err("Error: that is not a valid direction! dir:".. dir)
  78. end
  79. if type(d) ~= "number" then JUtils.err("Error: Dir is not a number!") end
  80. return d
  81. end
  82.  
  83. -- Set the start direction, it will not turn to it.
  84. function setPOSY(dir)
  85. cdir = getDirNum(dir)
  86. print("POS_Y was set to", cdir)
  87. if not dir then JUtils.err("Error: Dir was nil in displaceDir!") end
  88. --dirDisplacement = 4-cdir
  89. --cdir = 0
  90. end
  91.  
  92. function getX()
  93. return cx
  94. end
  95.  
  96. function getX()
  97. return cy
  98. end
  99.  
  100. function getX()
  101. return cz
  102. end
  103.  
  104. -- Make a string of the position vector.
  105. function posToStr()
  106. if not cx then JUtils.err("Error: cx is nil!")
  107. elseif not cy then JUtils.err("Error: cy is nil!")
  108. elseif not cz then JUtils.err("Error: cz is nil!") end
  109. return "(" .. cx .. ", " .. cy .. ", " .. cz .. ")"
  110. end
  111.  
  112. -- Set the refuel information to use when refueling.
  113. function setRefuelInfo(fuelTypeI, minFuelLevelI, refuelAmountI)
  114. fuelType = fuelTypeI
  115. minFuelLevel = minFuelLevelI
  116. refuelAmount = refuelAmountI
  117. end
  118.  
  119. -- Refuel with the set information
  120. function refuel()
  121. JUtils.refuel(fuelType, minFuelLevel, refuelAmount)
  122. end
  123.  
  124. -- Turn the turtle to the specified direction.
  125. function turnTo(dir)
  126. dir = getDirNum(dir)
  127. --dir = displaceDir(d)
  128. if cdir == dir then
  129. return
  130. end
  131. while (cdir ~= dir) do
  132. turtle.turnLeft()
  133. cdir = (cdir + 1) % 4
  134. end
  135. end
  136.  
  137. -- Move to position (x, y, z) from position (cx, cy, cz). The turtle will move in straight lines.
  138. function moveTo(x, y, z)
  139. local dx = x - cx
  140. local dy = y - cy
  141. local dz = z - cz
  142. -- Down
  143. while dz < 0 do
  144. refuel()
  145. turtle.down()
  146. cz = cz - 1
  147. dz = z - cz
  148. end
  149. -- Up
  150. while dz > 0 do
  151. refuel()
  152. turtle.up()
  153. cz = cz + 1
  154. dz = z - cz
  155. end
  156.  
  157. -- Right
  158. while dx > 0 do
  159. refuel()
  160. turnTo(RIGHT)
  161. turtle.forward()
  162. cx = cx + 1
  163. dx = x - cx
  164. end
  165.  
  166. -- Left
  167. while dx < 0 do
  168. refuel()
  169. turnTo(LEFT)
  170. turtle.forward()
  171. cx = cx - 1
  172. dx = x - cx
  173. end
  174.  
  175. -- Forward
  176. while dy > 0 do
  177. refuel()
  178. turnTo(FORWARD)
  179. turtle.forward()
  180. cy = cy + 1
  181. dy = y - cy
  182. end
  183.  
  184. -- Back
  185. while dy < 0 do
  186. refuel()
  187. turnTo(BACK)
  188. turtle.forward()
  189. cy = cy - 1
  190. dy = y - cy
  191. end
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement