Advertisement
USMCDefender

Untitled

Jan 6th, 2021
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. rednet.open("left")
  2. os.loadAPI('json')
  3. function get()
  4. local object = json.decodeFromFile("bot/cords.json")
  5. cords = {object["x"], object["y"], object["z"], object["direction"]}
  6. return cords
  7. end
  8.  
  9. function add_to_cords(x, y, z, newdir)
  10. old_cords = get()
  11. old_x = old_cords[1]
  12. old_y = old_cords[2]
  13. old_z = old_cords[3]
  14. if newdir ~= -1 then
  15. dir = newdir
  16. else
  17. dir = old_cords[4]
  18. end
  19. x = tostring(tonumber(old_x) + x)
  20. y = tostring(tonumber(old_y) + y)
  21. z = tostring(tonumber(old_z) + z)
  22. obj = "{\n \"x\": \"" .. tostring(x) .. "\",\n \"y\": \"" .. tostring(y) .. "\",\n \"z\": \"".. tostring(z) .. "\",\n \"direction\": \"" .. tostring(dir) .. "\"\n}"
  23. f = fs.open("bot/cords.json", 'w')
  24. f.write(obj)
  25. f.close()
  26. print(obj)
  27. sleep(2)
  28. end
  29.  
  30. function turnLeft()
  31. cords = get()
  32. dir = cords[4]
  33. new_dir = ""
  34. if turtle.turnLeft() then
  35. if dir == "1" then
  36. new_dir = "4"
  37.  
  38. elseif dir == "2" then
  39. new_dir = "1"
  40.  
  41. elseif dir == "3" then
  42. new_dir = "2"
  43.  
  44. elseif dir == "4" then
  45. new_dir = "3"
  46. end
  47. add_to_cords(0, 0, 0, new_dir)
  48. end
  49. end
  50.  
  51. function turnRight()
  52. if turtle.turnRight() then
  53. cords = get()
  54. dir = cords[4]
  55. new_dir = ""
  56. if dir == "1" then
  57. new_dir = "2"
  58.  
  59. elseif dir == "2" then
  60. new_dir = "3"
  61.  
  62. elseif dir == "3" then
  63. new_dir = "4"
  64.  
  65. elseif dir == "4" then
  66. new_dir = "1"
  67. end
  68. add_to_cords(0, 0, 0, new_dir)
  69. end
  70. end
  71.  
  72. function forward()
  73. cords = get()
  74. old_x = cords[1]
  75. old_2 = cords[2]
  76. old_3 = cords[3]
  77. dir = cords[4]
  78. if turtle.forward() then
  79. if dir == "1" then
  80. -- neg Z
  81. add_to_cords(0, 0, -1, -1)
  82. elseif dir == "2" then
  83. -- pos X
  84. add_to_cords(1, 0, 0, -1)
  85. elseif dir == "3" then
  86. -- pos Z
  87. add_to_cords(0, 0, 1, -1)
  88. elseif dir == "4" then
  89. -- neg X
  90. add_to_cords(-1, 0, 0, -1)
  91. end
  92. end
  93. end
  94.  
  95. function back()
  96. cords = get()
  97. old_x = cords[1]
  98. old_2 = cords[2]
  99. old_3 = cords[3]
  100. dir = cords[4]
  101. if turtle.back() then
  102. if dir == "1" then
  103. -- pos Z
  104. add_to_cords(0, 0, 1, -1)
  105. elseif dir == "2" then
  106. -- neg X
  107. add_to_cords(-1, 0, 0, -1)
  108. elseif dir == "3" then
  109. -- neg Z
  110. add_to_cords(0, 0, -1, -1)
  111. elseif dir == "4" then
  112. -- pos X
  113. add_to_cords(1, 0, 0, -1)
  114. end
  115. end
  116. end
  117.  
  118. while true do
  119. local sender, message, method = rednet.receive()
  120. if message == "w" then
  121. forward()
  122. elseif message == "s" then
  123. back()
  124. elseif message == "a" then
  125. turnLeft()
  126. elseif message == "d" then
  127. turnRight()
  128. elseif message == "r" then
  129. turtle.refuel()
  130. end
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement