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
- live_facing_direction = 0
- -- Basic moveset functions turning and forward
- 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)
- refuel_with("minecraft:coal")
- 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')
- success, data = turtle.inspectDown()
- if success then
- if (data.state.age == 7) then
- turtle.digDown()
- place_item("minecraft:potato")
- end
- end
- end
- function plane_execution(x_destinations_input, y_destinations_input, turtle_action_input)
- x_destinations = x_destinations_input
- y_destinations = y_destinations_input
- turtle_action = turtle_action_input
- for x_i in x_destinations_input do
- visited[#visited + 1] = 0
- end
- array_length = #visited
- turtle_action = turtle_action_input
- pathfind_and_action()
- 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
- while true do
- x_destinations = {}
- y_destinations = {}
- visited = {}
- array_length = 0
- squarefill(5,9,1)
- pathfind_and_action()
- turtle_goto(1,1)
- turtle_face_turn(0)
- sleep(500)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement