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
- if item_tot < 1 then
- distress_item_request(item_name,4,'worker_to_supply','supply_to_worker')
- 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
- end
- print("item_left: %d", item_tot)
- end
- function refuel_with(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.refuel(1)
- item_tot = item_tot + item_detail.count
- end
- end
- end
- if item_tot < 1 then
- distress_item_request(item_name,1,'worker_to_supply','supply_to_worker')
- 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()
- print(task_name)
- if task_name == 'vert' then
- 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
- function distress_item_request(item_name,s_number,out_protocol,in_protocol)
- -- message format looking like 1x2x3xminecraft:coal
- out_message = (x_live_coord .. 'x' .. y_live_coord .. 'x' .. z_live_coord .. 'x' .. item_name .. 'x' .. s_number)
- rednet.broadcast(out_message, out_protocol)
- -- check if item is received
- id, msg = rednet.receive(in_protocol)
- end
- function count_item(item_name)
- item_count = 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
- item_count = item_count + item_detail.count
- end
- end
- end
- return item_count
- end
- function drop_item(item_name,num_of_stack)
- dropped_count = 0
- for i=1,num_of_stack,1 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- function distress_item_decode_action(msg,return_protocol)
- matched_func = string.gmatch(msg,'([^x]+)')
- x_dest = matched_func()
- y_dest = matched_func()
- z_dest = matched_func()
- item_name = matched_func()
- stack_num_r = matched_func()
- -- check if there is enough fuel
- -- go to fuel chest
- if turtle.getFuelLevel()<4000 then
- turtle_goto(0,findIndex(chest_item,'minecraft:coal'))
- turtle.suckDown()
- turtle.select(1)
- turtle.refuel()
- end
- -- go to item chest
- turtle_goto(0,findIndex(chest_item,item_name))
- for i = 1,stack_num_r do
- turtle.suckDown()
- end
- -- go deliver item
- goto_z(z_dest+1)
- turtle_goto(x_dest,y_dest)
- drop_item(item_name, stack_num_r)
- -- return and resume
- turtle_goto(0,0)
- goto_z(1)
- turtle_face_turn(0)
- -- return underlivered items
- turtle_goto(0,findIndex(chest_item,item_name))
- drop_item(item_name,16)
- -- send message of completion
- rednet.broadcast('yay',return_protocol)
- end
- function findIndex(array, value)
- for i, v in ipairs(array) do
- if v == value then
- return i
- end
- end
- return nil
- end
- function turtle_drop_with_quantity(item_name_drop, drop_number)
- drop_counter = 0
- selected_cell = 1
- while drop_counter<drop_number do
- turtle.select(selected_cell)
- if turtle.getItemDetail() then
- item_detail = turtle.getItemDetail()
- if item_detail.name == item_name_drop then
- turtle.dropDown(1)
- drop_counter = drop_counter + 1
- else
- selected_cell = selected_cell + 1
- end
- else
- selected_cell = selected_cell + 1
- end
- end
- end
- function turtle_selected_drop(preserve_list)
- for drop_i=1,16,1 do
- turtle.select(drop_i)
- if turtle.getItemDetail() then
- item_detail1 = turtle.getItemDetail()
- if findIndex(preserve_list,item_detail1.name) ~= nil then
- item_total = count_item(item_detail1.name)
- print(item_total)
- if item_total > 32 then
- print(item_detail.name, item_total)
- turtle_drop_with_quantity(item_detail1.name,item_total-32)
- end
- else
- turtle.dropDown()
- end
- end
- end
- end
- function turtle_organise()
- for x=1,16,1 do
- if turtle.getItemCount(x) < 64 then
- turtle.select(x)
- for y=x+1,16 do --Avoids testing the same slot
- turtle.transferTo(y)
- end
- end
- end
- end
- turtle_selected_drop({'minecraft:potato','minecraft:wheat_seeds','simplefarming:onion_seeds'})
Add Comment
Please, Sign In to add comment