Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- api used for 2d pathfinding
- -- defining variables of coordinates and direction
- -- Initialize parameters for the location and direction
- x_live_coord = 1
- y_live_coord = 1
- z_live_coord = 1
- live_facing_direction = 0
- item_name_g = nil
- -- Basic moveset functions turning and forward
- function up_rec()
- repeat
- turtle.digUp()
- until turtle.up()
- z_live_coord = z_live_coord + 1
- end
- function down_rec()
- repeat
- turtle.digDown()
- until turtle.down()
- z_live_coord = z_live_coord - 1
- end
- function goto_z(z_dest)
- if z_dest > z_live_coord then
- repeat
- up_rec()
- until z_live_coord == z_dest
- elseif z_dest < z_live_coord then
- repeat
- down_rec()
- until z_live_coord == z_dest
- end
- end
- function rec_turn_right()
- -- action
- turtle.turnRight()
- -- recording
- live_facing_direction = live_facing_direction + 1
- if (live_facing_direction == 4) then
- live_facing_direction = 0
- end
- end
- function rec_turn_left()
- -- action
- turtle.turnLeft()
- -- recording
- live_facing_direction = live_facing_direction - 1
- if (live_facing_direction == -1) then
- live_facing_direction = 3
- end
- end
- function rec_forward_firm()
- -- action
- repeat
- turtle.dig()
- until turtle.forward()
- --recording
- if (live_facing_direction == 0) then
- y_live_coord = y_live_coord + 1
- elseif (live_facing_direction == 1) then
- x_live_coord = x_live_coord + 1
- elseif (live_facing_direction == 2) then
- y_live_coord = y_live_coord - 1
- elseif (live_facing_direction == 3) then
- x_live_coord = x_live_coord - 1
- end
- print(x_live_coord, y_live_coord)
- while turtle.getFuelLevel() < 80 do
- print("going out of fuel")
- refuel_with("minecraft:coal")
- end
- end
- function up_firm()
- repeat
- turtle.digUp()
- until turtle.up()
- end
- function down_firm()
- repeat
- turtle.digDown()
- until turtle.down()
- end
- function turtle_face_turn(face)
- if (face - live_facing_direction == 1 or face - live_facing_direction == -3) then
- rec_turn_right()
- elseif(face - live_facing_direction == -1 or face - live_facing_direction == 3) then
- rec_turn_left()
- elseif(face == live_facing_direction) then
- print("not turning")
- else
- rec_turn_left()
- rec_turn_left()
- end
- end
- -- the GOTO function, give one coordinate and move to the function
- -- first align the facing direction to 0
- -- get to the coordinate and return facing direction to 0
- function turtle_goto(a,b)
- -- calculation
- delta_x = a - x_live_coord
- delta_y = b - y_live_coord
- -- action section below
- -- facing the correct direction for x movement
- if (delta_x > 0) then
- turtle_face_turn(1)
- elseif (delta_x < 0) then
- turtle_face_turn(3)
- delta_x = - delta_x
- end
- -- x movement
- for x_movement = 0,delta_x - 1,1
- do
- rec_forward_firm()
- end
- -- facing the correct direction for y movement
- if (delta_y > 0) then
- turtle_face_turn(0)
- elseif (delta_y < 0) then
- turtle_face_turn(2)
- delta_y = - delta_y
- end
- -- y movement
- for y_movement = 0,delta_y - 1,1
- do
- rec_forward_firm()
- end
- -- returning to the 0 facing direction
- end
- -- the pathfinding function, will try to visit all points in an array
- -- some utilities
- function pathfind_and_action()
- for loop_num=1, array_length, 1
- do
- print("NEXT!!")
- near_init_term_num = 1
- while visited[near_init_term_num] == 1 do
- near_init_term_num = near_init_term_num + 1
- end
- nearest_x = x_destinations[near_init_term_num]
- nearest_y = y_destinations[near_init_term_num]
- min_distance = math.abs(nearest_x - x_live_coord) + math.abs(nearest_y - y_live_coord)
- last_item_num = near_init_term_num
- for item_num = 1, array_length, 1
- do
- if visited[item_num] == 0 then
- if (math.abs(x_destinations[item_num] - x_live_coord) + math.abs(y_destinations[item_num] - y_live_coord)) < min_distance then
- nearest_x = x_destinations[item_num]
- nearest_y = y_destinations[item_num]
- min_distance = math.abs(nearest_x - x_live_coord) + math.abs(nearest_y - y_live_coord)
- last_item_num = item_num
- end
- end
- end
- visited[last_item_num] = 1
- print("going to ", nearest_x, nearest_y)
- turtle_goto(nearest_x,nearest_y)
- turtle_action()
- end
- end
- -- action loop section
- function place_item(item_name)
- item_tot = 0
- for cell_num = 1,16 do
- turtle.select(cell_num)
- if turtle.getItemDetail() then
- item_detail = turtle.getItemDetail()
- if (item_detail.name == item_name) then
- turtle.placeDown()
- item_tot = item_tot + item_detail.count
- end
- end
- end
- print("item_left: %d", item_tot)
- end
- function refuel_with(item_name)
- for cell_num = 1,16 do
- turtle.select(cell_num)
- if turtle.getItemDetail() then
- item_detail = turtle.getItemDetail()
- if (item_detail.name == item_name) then
- turtle.refuel(1)
- end
- end
- end
- end
- -- example code of coordinates given
- function turtle_action()
- print('nothing')
- place_item(item_name_g)
- end
- function plane_execution(x_destinations_input, y_destinations_input, visited_input, array_length_input, item_name)
- x_destinations = x_destinations_input
- y_destinations = y_destinations_input
- visited = visited_input
- array_length = array_length_input
- item_name_g = item_name
- pathfind_and_action()
- turtle_goto(1,1)
- turtle_face_turn(0)
- end
- function squarefill(a,b,s)
- array_length = a*b
- current_length=1
- for xfill=1,a,s
- do
- for yfill=1,b,s
- do
- x_destinations[current_length]=xfill
- y_destinations[current_length]=yfill
- visited[current_length]=0
- current_length=current_length + 1
- end
- end
- end
- function if_inlist(item_list)
- inlist = 0
- for num, item_name_l in pairs(item_list) do
- item_data = turtle.getItemDetail()
- if turtle.getItemDetail() then
- if string.match(item_data.name, item_name_l) then
- inlist = 1
- end
- end
- end
- return inlist
- end
- function clear_backpack(white_list)
- for i=1,16,1 do
- turtle.select(i)
- if if_inlist(white_list) == 0 then
- turtle.drop()
- end
- end
- end
- -- functions in charge of encoding and decoding
- function plane_encode(x_dests, y_dests, visited, array_l, item_name, protocol)
- task_name = 'plane'
- x_dests_str = '/'
- y_dests_str = '/'
- visited_str = '/'
- for i=1,array_l do
- x_dests_str = x_dests_str .. tostring(x_dests[i]) .. '/'
- y_dests_str = y_dests_str .. tostring(y_dests[i]) .. '/'
- visited_str = visited_str .. tostring(visited[i]) .. '/'
- end
- xyv_dests_str = (x_dests_str .. 'x' .. y_dests_str .. 'x' .. visited_str)
- out_message = (task_name .. 'x' .. xyv_dests_str .. 'x' .. item_name)
- -- broadcasting now
- rednet.broadcast(out_message, protocol)
- end
- function vertical_encode(z_dest, protocol)
- task_name = 'vert'
- out_message = (task_name .. 'x' .. z_dest)
- rednet.broadcast(out_message, protocol)
- end
- function worker_decode_and_action(msg)
- matched_func = string.gmatch(msg,'([^x]+)')
- task_name = matched_func()
- if task_name == 'vert' then
- print('vertnow')
- goto_z(tonumber(matched_func()))
- elseif task_name == 'plane' then
- x_dests_str = matched_func()
- y_dests_str = matched_func()
- visited_str = matched_func()
- item_name = matched_func()
- -- start filling in
- array_length = 0
- x_destinations = {}
- y_destinations = {}
- visited = {}
- for x_i in string.gmatch(x_dests_str,'([^/]+)')
- do
- array_length = array_length + 1
- x_destinations[array_length] = tonumber(x_i)
- end
- for y_i in string.gmatch(y_dests_str,'([^/]+)')
- do
- y_destinations[#y_destinations + 1] = tonumber(y_i)
- end
- for v_i in string.gmatch(visited_str,'([^/]+)')
- do
- visited[#visited + 1] = tonumber(v_i)
- end
- plane_execution(x_destinations, y_destinations, visited, array_length, item_name)
- end
- end
- rednet.open('left')
- while true do
- id, message = rednet.receive('command_to_worker')
- worker_decode_and_action(message)
- rednet.broadcast(1,'worker_to_command')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement