Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. LWOG = {};
  2.  
  3. function LWOG.new() --[[LWOG for Location WithOut GPS]]--
  4. local self = {};
  5.  
  6. --[[
  7. All the positions and the heading are relative to the initial position of the turtle when the LWOG is initialized and not necessary match with the minecraft coordinate / GPS system of computercraft
  8. ]]--
  9.  
  10. local position_x = 0;
  11. local position_y = 0;
  12. local position_z = 0;
  13.  
  14. local heading = 0; --[[Heading relative from the initial heading. 0: front; 1: right; 2: behind; 3: left]]--
  15.  
  16. function self.updatePosition(order) --[[Keep in mind that the heading determine if we are "really" going forward or not in the relative environment]]--
  17. if (order == "forward") then
  18. if (heading == 0) then
  19. position_x = position_x + 1;
  20. elseif (heading == 1) then
  21. position_y = position_y + 1;
  22. elseif (heading == 2) then
  23. position_x = position_x - 1;
  24. elseif (heading == 3) then
  25. position_y = position_y - 1;
  26. else
  27. write("Wrong self.heading")
  28. end
  29. elseif (order == "back") then
  30. if (heading == 0) then
  31. position_x = position_x - 1;
  32. elseif (heading == 1) then
  33. position_y = position_y - 1;
  34. elseif (heading == 2) then
  35. position_x = position_x + 1;
  36. elseif (heading == 3) then
  37. position_y = position_y + 1;
  38. else
  39. write("Wrong self.heading")
  40. end
  41. elseif (order == "up") then
  42. position_z = position_z + 1;
  43. elseif (order == "down") then
  44. position_z = position_z - 1;
  45. elseif (order == "turnLeft") then
  46. if (heading == 0) then
  47. heading = 3;
  48. else
  49. heading = heading - 1;
  50. end
  51. elseif (order == "turnRight") then
  52. if (heading == 3) then
  53. heading = 0;
  54. else
  55. heading = heading + 1;
  56. end
  57. end
  58. end
  59.  
  60. --[[All moving functions give a parameter called "how_much" to allow you to call just one time forward to travel 100 blocks for example. just call LWOG.forward(100)]]--
  61. function self.forward(how_much)
  62. if (how_much <= 0) then
  63. print("Don't move.");
  64. end
  65. for i = 1, how_much do
  66. turtle.forward();
  67. self.updatePosition("forward");
  68. end
  69. end
  70.  
  71. function self.back(how_much)
  72. if (how_much <= 0) then
  73. print("Don't move.");
  74. end
  75. for i = 1, how_much do
  76. turtle.back();
  77. self.updatePosition("back");
  78. end
  79. end
  80.  
  81. function self.up(how_much)
  82. if (how_much <= 0) then
  83. print("Up: Don't move.");
  84. else
  85. print("Up for");
  86. print(how_much);
  87. end
  88. for i = 1, how_much do
  89. turtle.up();
  90. self.updatePosition("up");
  91. end
  92. end
  93.  
  94. function self.down(how_much)
  95. local last_state = false;
  96. if (how_much <= 0) then
  97. print("Don't move.");
  98. end
  99. for i = 1, how_much do
  100. last_state = turtle.down();
  101. self.updatePosition("down");
  102. end
  103. return last_state;
  104. end
  105.  
  106. function self.turnLeft(how_much)
  107. if (how_much <= 0) then
  108. print("Don't move.");
  109. end
  110. for i = 1, how_much do
  111. turtle.turnLeft();
  112. self.updatePosition("turnLeft");
  113. end
  114. end
  115.  
  116. function self.turnRight(how_much)
  117. if (how_much <= 0) then
  118. print("Don't move.");
  119. end
  120. for i = 1, how_much do
  121. turtle.turnRight();
  122. self.updatePosition("turnRight");
  123. end
  124. end
  125.  
  126. function self.printPosition()
  127. print("x:", position_x, " y:", position_y, " z:", position_z);
  128. print("orientation: ", heading);
  129. end
  130.  
  131. function self.getPosition()
  132. return position_x, position_y, position_z;
  133. end
  134.  
  135. function self.goToX(dest)
  136. local distance = dest - position_x;
  137. if (distance == 0) then
  138. print("[ ERROR ] goToX: nil distance");
  139. elseif (distance > 0) then
  140. while (heading ~= 0) do
  141. self.turnRight(1);
  142. end
  143. elseif (distance < 0) then
  144. distance = distance * -1;
  145. while (heading ~= 2) do
  146. self.turnRight(1);
  147. end
  148. end
  149. print("goToX: ");
  150. print(distance);
  151. self.forward(distance);
  152. end
  153.  
  154. function self.goToY(dest)
  155. local distance = dest - position_y;
  156. if (distance == 0) then
  157. print("[ ERROR ] goToY: nil distance");
  158. elseif (distance > 0) then
  159. while (heading ~= 1) do
  160. self.fuelCheck();
  161. self.turnRight(1);
  162. end
  163. elseif (distance < 0) then
  164. distance = distance * -1;
  165. while (heading ~= 1) do
  166. self.fuelCheck();
  167. self.turnRight(1);
  168. end
  169. end
  170. print("goToY: ");
  171. print(distance);
  172. self.forward(distance);
  173. end
  174.  
  175. function self.goToZ(dest)
  176. local distance = dest - position_z;
  177. if (distance == 0) then
  178. print("[ ERROR ] goToZ: nil distance");
  179. elseif (distance > 0) then
  180. print("goToZ: ");
  181. print(distance);
  182. turtle.up(distance);
  183. elseif (distance < 0) then
  184. distance = distance * -1;
  185. print("goToZ: ");
  186. print(distance);
  187. turtle.up(distance);
  188. end
  189. end
  190.  
  191. function self.goTo(destX, destY)
  192. self.goToX(destX);
  193. self.goToY(destY);
  194. end
  195.  
  196. function self.fuelCheck()
  197. local fuelLevel = turtle.getFuelLevel()
  198. if fuelLevel < 16 then
  199. turtle.select(1)
  200. turtle.refuel(1)
  201. print("Refueled!")
  202. end
  203. end
  204.  
  205. return self;
  206. end
  207.  
  208. function inspect_column(t)
  209. turtle.digDown();
  210. while (t.down(1)) do
  211. t.fuelCheck();
  212. turtle.digDown();
  213. print("Digging down");
  214. for i = 0, 3 do -- No need to rotate 4 time, just 3
  215. local s, d = turtle.inspect();
  216. if (d.name ~= "minecraft:stone" and d.name ~= "minecraft:cobblestone" and d.name ~= "minecraft:dirt" and d.name ~= "minecraft:gravel") then
  217. turtle.dig();
  218. end
  219. t.turnRight(1);
  220. end
  221. end
  222. t.goToZ(0);
  223. end
  224.  
  225.  
  226. --[[---------------- Here start the code for optimized_mining ----------------]]--
  227. local t = LWOG.new();
  228.  
  229. t.fuelCheck();
  230. local x, y, z = t.getPosition();
  231. for i = 0, 3 do
  232. x, y, z = t.getPosition();
  233. t.fuelCheck();
  234. inspect_column(t);
  235. t.goTo(x + 2, y + 1);
  236. end
  237. t.goTo(x - 1, y + 2);
  238. inspect_column(t);
  239. --[[
  240. for i = 0, 5 do
  241. x, y, z = t.getPosition();
  242. t.fuelCheck();
  243. inspect_column();
  244. t.goTo(x - 2, y - 1);
  245. end]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement