Advertisement
samzzy

2dapi_3

Mar 19th, 2023 (edited)
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.67 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 = 1
  7. y_live_coord = 1
  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.     print("item_left: %d", item_tot)
  212. end
  213.  
  214. function refuel_with(item_name)
  215.     for cell_num = 1,16 do
  216.         turtle.select(cell_num)
  217.         if turtle.getItemDetail() then
  218.             item_detail = turtle.getItemDetail()
  219.             if (item_detail.name == item_name) then
  220.                 turtle.refuel(1)
  221.             end
  222.         end
  223.     end
  224. end
  225.  
  226.  
  227.  
  228. -- example code of coordinates given
  229.  
  230.  
  231.  
  232. function turtle_action()
  233.     print('nothing')
  234.     place_item(item_name_g)
  235. end
  236.  
  237.  
  238. function plane_execution(x_destinations_input, y_destinations_input, visited_input, array_length_input, item_name)
  239.     x_destinations = x_destinations_input
  240.     y_destinations = y_destinations_input
  241.     visited = visited_input
  242.     array_length = array_length_input
  243.     item_name_g = item_name
  244.     pathfind_and_action()
  245.     turtle_goto(1,1)
  246.     turtle_face_turn(0)
  247. end
  248.  
  249. function squarefill(a,b,s)
  250.     array_length = a*b
  251.     current_length=1
  252.     for xfill=1,a,s
  253.     do
  254.         for yfill=1,b,s
  255.         do
  256.             x_destinations[current_length]=xfill
  257.             y_destinations[current_length]=yfill
  258.             visited[current_length]=0
  259.             current_length=current_length + 1
  260.         end
  261.     end
  262. end
  263.  
  264. function if_inlist(item_list)
  265.     inlist = 0
  266.     for num, item_name_l in pairs(item_list) do
  267.         item_data = turtle.getItemDetail()
  268.         if turtle.getItemDetail() then
  269.             if string.match(item_data.name, item_name_l) then
  270.                 inlist = 1
  271.             end
  272.         end
  273.     end
  274.     return inlist
  275. end
  276.  
  277. function clear_backpack(white_list)
  278.     for i=1,16,1 do
  279.         turtle.select(i)
  280.         if if_inlist(white_list) == 0 then
  281.             turtle.drop()
  282.         end
  283.     end
  284. end
  285.  
  286. -- functions in charge of encoding and decoding
  287. function plane_encode(x_dests, y_dests, visited, array_l, item_name, protocol)
  288.     task_name = 'plane'
  289.     x_dests_str = '/'
  290.     y_dests_str = '/'
  291.     visited_str = '/'
  292.     for i=1,array_l do
  293.     x_dests_str = x_dests_str .. tostring(x_dests[i]) .. '/'
  294.     y_dests_str = y_dests_str .. tostring(y_dests[i]) .. '/'
  295.     visited_str = visited_str .. tostring(visited[i]) .. '/'
  296.     end
  297.     xyv_dests_str = (x_dests_str .. 'x' .. y_dests_str .. 'x' .. visited_str)
  298.     out_message = (task_name .. 'x' .. xyv_dests_str .. 'x' .. item_name)
  299.     -- broadcasting now
  300.     rednet.broadcast(out_message, protocol)
  301. end
  302.  
  303. function vertical_encode(z_dest, protocol)
  304.     task_name = 'vert'
  305.     out_message = (task_name .. 'x' .. z_dest)
  306.     rednet.broadcast(out_message, protocol)
  307. end
  308.  
  309.  
  310. function worker_decode_and_action(msg)
  311.     matched_func = string.gmatch(msg,'([^x]+)')
  312.     task_name = matched_func()
  313.     if task_name == 'vert' then
  314.         print('vertnow')
  315.         goto_z(tonumber(matched_func()))
  316.     elseif task_name == 'plane' then
  317.     x_dests_str = matched_func()   
  318.     y_dests_str = matched_func()
  319.     visited_str = matched_func()
  320.     item_name = matched_func()
  321.     -- start filling in
  322.     array_length = 0
  323.     x_destinations = {}
  324.     y_destinations = {}
  325.     visited = {}
  326.  
  327.     for x_i in string.gmatch(x_dests_str,'([^/]+)')
  328.     do
  329.         array_length = array_length + 1
  330.         x_destinations[array_length] = tonumber(x_i)
  331.     end
  332.      
  333.     for y_i in string.gmatch(y_dests_str,'([^/]+)')
  334.     do
  335.         y_destinations[#y_destinations + 1] = tonumber(y_i)
  336.     end
  337.  
  338.     for v_i in string.gmatch(visited_str,'([^/]+)')
  339.     do
  340.         visited[#visited + 1] = tonumber(v_i)
  341.     end
  342.     plane_execution(x_destinations, y_destinations, visited, array_length, item_name)
  343.     end
  344. end
  345.  
  346. rednet.open('left')
  347. while true do
  348.     id, message = rednet.receive('command_to_worker')
  349.     worker_decode_and_action(message)
  350.     rednet.broadcast(1,'worker_to_command')
  351. end
  352.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement