Advertisement
samzzy

re_supplier

Mar 21st, 2023 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.14 KB | None | 0 0
  1. -- api used for 2d pathfinding
  2.  
  3.  
  4. -- defining variables of coordinates and direction
  5. -- Initialize parameters for the location and direction
  6. x_live_coord = 0
  7. y_live_coord = 0
  8. z_live_coord = 1
  9. live_facing_direction = 0
  10. item_name_g = nil
  11.  
  12.  
  13. -- Basic moveset functions turning and forward
  14.  
  15. function up_rec()
  16. repeat
  17. turtle.digUp()
  18. until turtle.up()
  19. z_live_coord = z_live_coord + 1
  20. end
  21.  
  22. function down_rec()
  23. repeat
  24. turtle.digDown()
  25. until turtle.down()
  26. z_live_coord = z_live_coord - 1
  27. end
  28.  
  29. function goto_z(z_dest)
  30. if z_dest > z_live_coord then
  31. repeat
  32. up_rec()
  33. until z_live_coord == z_dest
  34. elseif z_dest < z_live_coord then
  35. repeat
  36. down_rec()
  37. until z_live_coord == z_dest
  38. end
  39. end
  40.  
  41. function rec_turn_right()
  42. -- action
  43. turtle.turnRight()
  44.  
  45. -- recording
  46. live_facing_direction = live_facing_direction + 1
  47. if (live_facing_direction == 4) then
  48. live_facing_direction = 0
  49. end
  50. end
  51.  
  52. function rec_turn_left()
  53. -- action
  54. turtle.turnLeft()
  55.  
  56. -- recording
  57. live_facing_direction = live_facing_direction - 1
  58. if (live_facing_direction == -1) then
  59. live_facing_direction = 3
  60. end
  61.  
  62. end
  63.  
  64. function rec_forward_firm()
  65. -- action
  66. repeat
  67. turtle.dig()
  68. until turtle.forward()
  69.  
  70. --recording
  71. if (live_facing_direction == 0) then
  72. y_live_coord = y_live_coord + 1
  73. elseif (live_facing_direction == 1) then
  74. x_live_coord = x_live_coord + 1
  75. elseif (live_facing_direction == 2) then
  76. y_live_coord = y_live_coord - 1
  77. elseif (live_facing_direction == 3) then
  78. x_live_coord = x_live_coord - 1
  79. end
  80. print(x_live_coord, y_live_coord)
  81. while turtle.getFuelLevel() < 80 do
  82. print("going out of fuel")
  83. refuel_with("minecraft:coal")
  84. end
  85. end
  86.  
  87.  
  88. function up_firm()
  89. repeat
  90. turtle.digUp()
  91. until turtle.up()
  92. end
  93.  
  94. function down_firm()
  95. repeat
  96. turtle.digDown()
  97. until turtle.down()
  98. end
  99.  
  100. function turtle_face_turn(face)
  101. if (face - live_facing_direction == 1 or face - live_facing_direction == -3) then
  102. rec_turn_right()
  103. elseif(face - live_facing_direction == -1 or face - live_facing_direction == 3) then
  104. rec_turn_left()
  105. elseif(face == live_facing_direction) then
  106. print("not turning")
  107. else
  108. rec_turn_left()
  109. rec_turn_left()
  110. end
  111. end
  112.  
  113.  
  114. -- the GOTO function, give one coordinate and move to the function
  115. -- first align the facing direction to 0
  116. -- get to the coordinate and return facing direction to 0
  117. function turtle_goto(a,b)
  118. -- calculation
  119. delta_x = a - x_live_coord
  120. delta_y = b - y_live_coord
  121.  
  122. -- action section below
  123. -- facing the correct direction for x movement
  124. if (delta_x > 0) then
  125. turtle_face_turn(1)
  126. elseif (delta_x < 0) then
  127. turtle_face_turn(3)
  128. delta_x = - delta_x
  129. end
  130.  
  131.  
  132. -- x movement
  133. for x_movement = 0,delta_x - 1,1
  134. do
  135. rec_forward_firm()
  136. end
  137.  
  138.  
  139. -- facing the correct direction for y movement
  140. if (delta_y > 0) then
  141. turtle_face_turn(0)
  142. elseif (delta_y < 0) then
  143. turtle_face_turn(2)
  144. delta_y = - delta_y
  145. end
  146. -- y movement
  147. for y_movement = 0,delta_y - 1,1
  148. do
  149. rec_forward_firm()
  150. end
  151.  
  152.  
  153. -- returning to the 0 facing direction
  154.  
  155. end
  156.  
  157. -- the pathfinding function, will try to visit all points in an array
  158.  
  159. -- some utilities
  160.  
  161. function pathfind_and_action()
  162. for loop_num=1, array_length, 1
  163. do
  164. print("NEXT!!")
  165. near_init_term_num = 1
  166. while visited[near_init_term_num] == 1 do
  167. near_init_term_num = near_init_term_num + 1
  168. end
  169.  
  170. nearest_x = x_destinations[near_init_term_num]
  171. nearest_y = y_destinations[near_init_term_num]
  172. min_distance = math.abs(nearest_x - x_live_coord) + math.abs(nearest_y - y_live_coord)
  173.  
  174. last_item_num = near_init_term_num
  175. for item_num = 1, array_length, 1
  176. do
  177. if visited[item_num] == 0 then
  178. if (math.abs(x_destinations[item_num] - x_live_coord) + math.abs(y_destinations[item_num] - y_live_coord)) < min_distance then
  179. nearest_x = x_destinations[item_num]
  180. nearest_y = y_destinations[item_num]
  181. min_distance = math.abs(nearest_x - x_live_coord) + math.abs(nearest_y - y_live_coord)
  182. last_item_num = item_num
  183. end
  184. end
  185. end
  186. visited[last_item_num] = 1
  187. print("going to ", nearest_x, nearest_y)
  188. turtle_goto(nearest_x,nearest_y)
  189.  
  190. turtle_action()
  191. end
  192.  
  193. end
  194.  
  195.  
  196.  
  197. -- action loop section
  198.  
  199. function place_item(item_name)
  200. item_tot = 0
  201. for cell_num = 1,16 do
  202. turtle.select(cell_num)
  203. if turtle.getItemDetail() then
  204. item_detail = turtle.getItemDetail()
  205. if (item_detail.name == item_name) then
  206. turtle.placeDown()
  207. item_tot = item_tot + item_detail.count
  208. end
  209. end
  210. end
  211. if item_tot < 1 then
  212. distress_item_request(item_name,4)
  213. end
  214. print("item_left: %d", item_tot)
  215. end
  216.  
  217. function refuel_with(item_name)
  218. item_tot = 0
  219. for cell_num = 1,16 do
  220. turtle.select(cell_num)
  221. if turtle.getItemDetail() then
  222. item_detail = turtle.getItemDetail()
  223. if (item_detail.name == item_name) then
  224. turtle.refuel(1)
  225. item_tot = item_tot + item_detail.count
  226. end
  227. end
  228. end
  229. if item_tot < 1 then
  230. distress_item_request(item_name,1)
  231. end
  232. end
  233.  
  234.  
  235.  
  236. -- example code of coordinates given
  237.  
  238.  
  239.  
  240. function turtle_action()
  241. print('nothing')
  242. place_item(item_name_g)
  243. end
  244.  
  245.  
  246. function plane_execution(x_destinations_input, y_destinations_input, visited_input, array_length_input, item_name)
  247. x_destinations = x_destinations_input
  248. y_destinations = y_destinations_input
  249. visited = visited_input
  250. array_length = array_length_input
  251. item_name_g = item_name
  252. pathfind_and_action()
  253. turtle_goto(1,1)
  254. turtle_face_turn(0)
  255. end
  256.  
  257. function squarefill(a,b,s)
  258. array_length = a*b
  259. current_length=1
  260. for xfill=1,a,s
  261. do
  262. for yfill=1,b,s
  263. do
  264. x_destinations[current_length]=xfill
  265. y_destinations[current_length]=yfill
  266. visited[current_length]=0
  267. current_length=current_length + 1
  268. end
  269. end
  270. end
  271.  
  272. function if_inlist(item_list)
  273. inlist = 0
  274. for num, item_name_l in pairs(item_list) do
  275. item_data = turtle.getItemDetail()
  276. if turtle.getItemDetail() then
  277. if string.match(item_data.name, item_name_l) then
  278. inlist = 1
  279. end
  280. end
  281. end
  282. return inlist
  283. end
  284.  
  285. function clear_backpack(white_list)
  286. for i=1,16,1 do
  287. turtle.select(i)
  288. if if_inlist(white_list) == 0 then
  289. turtle.drop()
  290. end
  291. end
  292. end
  293.  
  294. -- functions in charge of encoding and decoding
  295. function plane_encode(x_dests, y_dests, visited, array_l, item_name, protocol)
  296. task_name = 'plane'
  297. x_dests_str = '/'
  298. y_dests_str = '/'
  299. visited_str = '/'
  300. for i=1,array_l do
  301. x_dests_str = x_dests_str .. tostring(x_dests[i]) .. '/'
  302. y_dests_str = y_dests_str .. tostring(y_dests[i]) .. '/'
  303. visited_str = visited_str .. tostring(visited[i]) .. '/'
  304. end
  305. xyv_dests_str = (x_dests_str .. 'x' .. y_dests_str .. 'x' .. visited_str)
  306. out_message = (task_name .. 'x' .. xyv_dests_str .. 'x' .. item_name)
  307. -- broadcasting now
  308. rednet.broadcast(out_message, protocol)
  309. end
  310.  
  311. function vertical_encode(z_dest, protocol)
  312. task_name = 'vert'
  313. out_message = (task_name .. 'x' .. z_dest)
  314. rednet.broadcast(out_message, protocol)
  315. end
  316.  
  317.  
  318. function worker_decode_and_action(msg)
  319. matched_func = string.gmatch(msg,'([^x]+)')
  320. task_name = matched_func()
  321. if task_name == 'vert' then
  322. goto_z(tonumber(matched_func()))
  323. elseif task_name == 'plane' then
  324. x_dests_str = matched_func()
  325. y_dests_str = matched_func()
  326. visited_str = matched_func()
  327. item_name = matched_func()
  328. -- start filling in
  329. array_length = 0
  330. x_destinations = {}
  331. y_destinations = {}
  332. visited = {}
  333.  
  334. for x_i in string.gmatch(x_dests_str,'([^/]+)')
  335. do
  336. array_length = array_length + 1
  337. x_destinations[array_length] = tonumber(x_i)
  338. end
  339.  
  340. for y_i in string.gmatch(y_dests_str,'([^/]+)')
  341. do
  342. y_destinations[#y_destinations + 1] = tonumber(y_i)
  343. end
  344.  
  345. for v_i in string.gmatch(visited_str,'([^/]+)')
  346. do
  347. visited[#visited + 1] = tonumber(v_i)
  348. end
  349. plane_execution(x_destinations, y_destinations, visited, array_length, item_name)
  350. end
  351. end
  352.  
  353. function distress_item_request(item_name,s_number,out_protocol,in_protocol)
  354. -- message format looking like 1x2x3xminecraft:coal
  355. out_message = (x_live_coord .. 'x' .. y_live_coord .. 'x' .. z_live_coord .. 'x' .. item_name .. 'x' .. s_number)
  356. rednet.broadcast(out_message, out_protocol)
  357. -- check if item is received
  358. id, msg = rednet.receive(in_protocol)
  359. end
  360.  
  361. function count_item(item_name)
  362. item_count = 0
  363. for cell_num = 1,16 do
  364. turtle.select(cell_num)
  365. if turtle.getItemDetail() then
  366. item_detail = turtle.getItemDetail()
  367. if (item_detail.name == item_name) then
  368. item_count = item_count + 1
  369. end
  370. end
  371. end
  372. return item_count
  373. end
  374.  
  375. function drop_item(item_name,num_of_stack)
  376. dropped_count = 0
  377. for i=1,num_of_stack,1 do
  378. turtle.select(i)
  379. turtle.dropDown()
  380. end
  381. end
  382.  
  383. function distress_item_decode_action(msg,return_protocol)
  384. matched_func = string.gmatch(msg,'([^x]+)')
  385. x_dest = matched_func()
  386. y_dest = matched_func()
  387. z_dest = matched_func()
  388. item_name = matched_func()
  389. stack_num_r = matched_func()
  390.  
  391. print(x_dest)
  392. -- check if there is enough fuel
  393. -- go to fuel chest
  394. if turtle.getFuelLevel()<4000 then
  395. turtle_goto(0,findIndex(chest_item,'minecraft:coal'))
  396. turtle.suckDown()
  397. turtle.select(1)
  398. turtle.refuel()
  399. end
  400.  
  401. -- go to item chest
  402. turtle.select(1)
  403. turtle_goto(0,findIndex(chest_item,item_name))
  404. for i = 1,stack_num_r do
  405. turtle.suckDown()
  406. end
  407.  
  408. -- go deliver item
  409. goto_z(z_dest+1)
  410. turtle_goto(x_dest,y_dest)
  411. drop_item(item_name, stack_num_r)
  412.  
  413. -- return and resume
  414. turtle_goto(0,0)
  415. goto_z(1)
  416. turtle_face_turn(0)
  417. -- return underlivered items
  418. turtle_goto(0,findIndex(chest_item,item_name))
  419. drop_item(item_name,16)
  420. -- send message of completion
  421. rednet.broadcast('yay',return_protocol)
  422. turtle_goto(0,0)
  423. goto_z(1)
  424. turtle_face_turn(0)
  425. turtle.select(1)
  426. rednet.broadcast('yay',return_protocol)
  427. end
  428.  
  429. function findIndex(array, value)
  430. for i, v in ipairs(array) do
  431. if v == value then
  432. return i
  433. end
  434. end
  435. return nil
  436. end
  437.  
  438.  
  439.  
  440. chest_item = {}
  441. chest_item[1] = 'minecraft:coal'
  442. chest_item[2] = 'chisel:planks/oak/braced_planks'
  443. chest_item[3] = 'minecraft:glass'
  444.  
  445. rednet.open('left')
  446. while true do
  447. id, message = rednet.receive('worker_to_supply')
  448. print(message)
  449. distress_item_decode_action(message,'supply_to_worker')
  450. end
  451.  
  452.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement