Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- turtle movement codes ------------------------------
- --
- -- 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
- destroy_status_g = 1
- -- 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
- 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()
- 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()
- val, data = turtle.inspectDown()
- if turtle.detectDown() then
- if (data.state.age == 7) and (item_name_g ~= 'notfound') then
- turtle.digDown()
- end
- end
- turtle.suckDown()
- 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)
- 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
- return out_message
- 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 turtle_chest_refuel()
- if turtle.getFuelLevel() < 1000 then
- turtle_goto(1,0)
- turtle.suckDown()
- refuel_with("minecraft:coal")
- turtle_goto(1,1)
- turtle_face_turn(0)
- 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
- ----- ui elements----------------------------------------
- -- defining screen
- Screen = {canvases={}}
- function Screen.render(self)
- for _p, canvas in pairs(self.canvases) do
- canvas:render()
- end
- end
- function Screen.pipe_inputs(self)
- event, data1, data2, data3 = os.pullEvent()
- for _p, canvas in pairs(self.canvases) do
- canvas:receive_event(event, data1, data2, data3)
- end
- end
- function Screen.update(self)
- for _p, canvas in pairs(self.canvases) do
- canvas:update()
- end
- end
- -- defining canvas
- Canvas_prototype = {owner_screen, x, y, x_size, y_size, v_x_size, v_y_size, x_offset=0, y_offset=0, elements={}, data0 = 'default'}
- function create_canvas(x, y, x_size, y_size, data0)
- local new_canvas = {}
- setmetatable(new_canvas, {__index = Canvas_prototype })
- new_canvas.elements={}
- new_canvas.x = x
- new_canvas.y = y
- new_canvas.x_size = x_size
- new_canvas.y_size = y_size
- new_canvas.data0 = data0
- return new_canvas
- end
- function Canvas_prototype.event_testing(self)
- event, data1, data2, data3 = os.pullEvent()
- self:receive_event(event,data1,data2,data3)
- end
- function Canvas_prototype.receive_event(self, event, data1, data2, data3)
- for _, element in pairs(self.elements) do
- element:receive_event(event, data1, data2, data3)
- end
- end
- function Canvas_prototype.render(self)
- for _, element in pairs(self.elements) do
- if element.render_status == 1 then
- element:render()
- end
- end
- end
- function Canvas_prototype.update(self)
- --get largest virtual sizes
- virtual_x_acc = 1
- virtual_y_acc = 1
- -- by going through all element virtual sizes
- for _, element in pairs(self.elements) do
- if element.virtual_x - self.x + 1 > virtual_x_acc then
- virtual_x_acc = element.virtual_x - self.x
- end
- if element.virtual_y - self.y + 1 > virtual_y_acc then
- virtual_y_acc = element.virtual_y - self.y
- end
- end
- self.v_x_size = virtual_x_acc
- self.v_y_size = virtual_y_acc
- --update render coordinates of each element based on offset
- for _, element in pairs(self.elements) do
- element.render_x = element.virtual_x-self.x_offset
- element.render_y = element.virtual_y-self.y_offset
- -- determing which to render
- --
- local is_within_bounds = element.render_x >= self.x and element.render_x < self.x + self.x_size
- and element.render_y >= self.y and element.render_y < self.y + self.y_size
- if is_within_bounds then
- element.render_status = 1
- else
- element.render_status = 0
- end
- -- update every element as well
- element:content_update()
- end
- end
- -- hbar and vbar is creating a new ui elements and stuff it in Canvas.elements
- function Canvas_prototype.vbar(self)
- local new_vbar = create_ui_element_no_ins(self, self.x+self.x_size-1, self.y, 'o', 'vertical scrollbar')
- new_vbar.render_x = new_vbar.virtual_x
- new_vbar.render_y = new_vbar.virtual_y
- new_vbar.y_size = self.y_size
- new_vbar.fg_y = new_vbar.canvas_obj.y
- new_vbar.fg_y_size = 1
- new_vbar.drag_init_y = 0
- new_vbar.drag_init_offset = 0
- new_vbar.pressed_down_mid = 0 -- status of whether it is pinned down by mouse, initiated by press method and only alleviated with a global release event
- local function render(self_bar)
- --draw bg
- drawbox_single(self_bar.render_x,self_bar.render_y, self_bar.x_size, self_bar.canvas_obj.y_size, '0', colors.black,colors.gray)
- --draw fg
- drawbox_single(self_bar.canvas_obj.x + self_bar.canvas_obj.x_size - 1,self_bar.canvas_obj.y+self_bar.fg_y,self_bar.x_size,self_bar.fg_y_size, '0', colors.black, colors.yellow)
- end
- new_vbar.render = render
- local function dragged(self_bar, x_dragged, y_dragged)
- --using canvas x_y size location and mouse drag update, determing in real time how y offset should be adjusted
- if self_bar.pressed_down_mid == 1 then
- self_bar.canvas_obj.y_offset = math.min(math.max(self_bar.drag_init_offset + (y_dragged - self_bar.drag_init_y)*(self_bar.canvas_obj.v_y_size/self_bar.canvas_obj.y_size), 0), self_bar.canvas_obj.v_y_size - self_bar.canvas_obj.y_size + 2)
- end
- end
- new_vbar.dragged = dragged
- local function pressed(self_bar, x_pressed, y_pressed)
- -- if clicked on fg bar change pressed_down status, if clicked on bg bar on either side, increment canvas y offset
- if y_pressed<self_bar.fg_y+self_bar.canvas_obj.y then
- self_bar.canvas_obj.y_offset = self_bar.canvas_obj.y_offset - 1
- elseif self_bar.fg_y+ self_bar.canvas_obj.y - 1< y_pressed and y_pressed<(self_bar.fg_y + self_bar.fg_y_size + self_bar.canvas_obj.y) then
- self_bar.pressed_down_mid = 1
- self_bar.drag_init_y = y_pressed
- self_bar.drag_init_offset = self_bar.canvas_obj.y_offset
- elseif self_bar.fg_y + self_bar.fg_y_size - 1 + self_bar.canvas_obj.y< y_pressed then
- self_bar.canvas_obj.y_offset = self_bar.canvas_obj.y_offset + 1
- end
- end
- new_vbar.pressed = pressed
- local function content_update(self_bar)
- -- calculate fg_y_size
- self_bar.fg_y_size = math.floor(self_bar.canvas_obj.y_size^2/self_bar.canvas_obj.v_y_size)
- self_bar.fg_y = math.floor(self_bar.canvas_obj.y_offset*(self_bar.canvas_obj.y_size/self_bar.canvas_obj.v_y_size))
- -- undo offset
- new_vbar.render_x = new_vbar.virtual_x
- new_vbar.render_y = new_vbar.virtual_y
- -- set render to 1
- self_bar.render_status = 1
- end
- new_vbar.content_update = content_update
- table.insert(self.elements, new_vbar)
- end
- function Canvas_prototype.hbar(self)
- local new_hbar = create_ui_element_no_ins(self, self.x, self.y+self.y_size-1, 'o', 'horizontal scrollbar')
- new_hbar.render_x = new_hbar.virtual_x
- new_hbar.render_y = new_hbar.virtual_y
- new_hbar.y_size = 1
- new_hbar.x_size = self.x_size
- new_hbar.fg_x = new_hbar.canvas_obj.x
- new_hbar.fg_x_size = 1
- new_hbar.drag_init_x = 0
- new_hbar.drag_init_offset = 0
- new_hbar.pressed_down_mid = 0 -- status of whether it is pinned down by mouse, initiated by press method and only alleviated with a global release event
- local function render(self_bar)
- --draw bg
- drawbox_single(self_bar.canvas_obj.x,self_bar.canvas_obj.y + self_bar.canvas_obj.y_size-1, self_bar.canvas_obj.x_size, self_bar.y_size, '0', colors.black,colors.gray)
- --draw fg
- drawbox_single(self_bar.canvas_obj.x + self_bar.fg_x,self_bar.canvas_obj.y+self_bar.canvas_obj.y_size - 1,self_bar.fg_x_size,self_bar.y_size, '0', colors.black, colors.yellow)
- end
- new_hbar.render = render
- local function dragged(self_bar, x_dragged, y_dragged)
- --using canvas x_y size location and mouse drag update, determing in real time how y offset should be adjusted
- if self_bar.pressed_down_mid == 1 then
- self_bar.canvas_obj.x_offset = math.min(math.max(self_bar.drag_init_offset + (x_dragged - self_bar.drag_init_x)*(self_bar.canvas_obj.v_x_size/self_bar.canvas_obj.x_size), 0), self_bar.canvas_obj.v_x_size - self_bar.canvas_obj.x_size + 2)
- end
- end
- new_hbar.dragged = dragged
- local function pressed(self_bar, x_pressed, y_pressed)
- -- if clicked on fg bar change pressed_down status, if clicked on bg bar on either side, increment canvas y offset
- if x_pressed<self_bar.fg_x+self_bar.canvas_obj.x then
- self_bar.canvas_obj.x_offset = self_bar.canvas_obj.x_offset - 1
- elseif self_bar.fg_x+ self_bar.canvas_obj.x - 1< x_pressed and x_pressed<(self_bar.fg_x + self_bar.fg_x_size + self_bar.canvas_obj.x) then
- self_bar.pressed_down_mid = 1
- self_bar.drag_init_x = x_pressed
- self_bar.drag_init_offset = self_bar.canvas_obj.x_offset
- elseif self_bar.fg_x + self_bar.fg_x_size + self_bar.canvas_obj.x - 1< x_pressed then
- self_bar.canvas_obj.x_offset = self_bar.canvas_obj.x_offset + 1
- end
- end
- new_hbar.pressed = pressed
- local function content_update(self_bar)
- -- calculate fg_y_size
- self_bar.fg_x_size = math.floor(self_bar.canvas_obj.x_size^2/self_bar.canvas_obj.v_x_size)
- self_bar.fg_x = math.floor(self_bar.canvas_obj.x_offset*(self_bar.canvas_obj.x_size/self_bar.canvas_obj.v_x_size))
- -- undo offset
- new_hbar.render_x = new_hbar.virtual_x
- new_hbar.render_y = new_hbar.virtual_y
- -- set render to 1
- self_bar.render_status = 1
- end
- new_hbar.content_update = content_update
- table.insert(self.elements, new_hbar)
- end
- -- defining ui_element
- --
- ui_element_prototype = {canvas_obj, render_status = 0, render_x, render_y, x_size=1, y_size=1, virtual_x, virtual_y, content = ' ', bg = colors.black, fg = colors.green, data0 = 'default data', pressed_down=0}
- function create_ui_element(canvas_obj, virtual_x,virtual_y,content,data)
- local new_ui_element = {}
- setmetatable(new_ui_element, {__index = ui_element_prototype })
- new_ui_element.virtual_x = virtual_x
- new_ui_element.virtual_y = virtual_y
- new_ui_element.content = content
- new_ui_element.data0 = data
- new_ui_element.canvas_obj = canvas_obj
- table.insert(canvas_obj.elements, new_ui_element)
- end
- function create_ui_element_no_ins(canvas_obj, virtual_x,virtual_y,content,data)
- local new_ui_element = {}
- setmetatable(new_ui_element, {__index = ui_element_prototype })
- new_ui_element.virtual_x = virtual_x
- new_ui_element.virtual_y = virtual_y
- new_ui_element.content = content
- new_ui_element.data0 = data
- new_ui_element.canvas_obj = canvas_obj
- return new_ui_element
- end
- function ui_element_prototype.render(self)
- if self.render_status == 1 then
- drawbox_single(self.render_x, self.render_y, self.x_size, self.y_size, self.content, self.bg, self.fg )
- end
- end
- function ui_element_prototype.check_click(self,x_cl,y_cl)
- if (self.canvas_obj.x <= x_cl and x_cl <= self.canvas_obj.x + self.canvas_obj.x_size) and
- (self.canvas_obj.y <= y_cl and y_cl <= self.canvas_obj.y + self.canvas_obj.y_size) then
- if self.render_status == 1 then
- if (self.render_x <= x_cl and x_cl < self.render_x + self.x_size) and
- (self.render_y <= y_cl and y_cl < self.render_y + self.y_size) then
- self:pressed(x_cl,y_cl)
- self.pressed_down = 1
- end
- end
- end
- end
- function ui_element_prototype.pressed(self)
- -- does nothing
- end
- function ui_element_prototype.dragged(self)
- --does nothing
- end
- function ui_element_prototype.released(self)
- self.pressed_down = 0
- if self.pressed_down_mid ~= nil then
- self.pressed_down_mid = 0
- end
- end
- function ui_element_prototype.content_update(self)
- -- does nothing
- if self.pressed_down == 1 then
- self.fg = colors.red
- else
- self.fg = colors.green
- end
- end
- function ui_element_prototype.receive_event(self, event, data1, data2, data3)
- if event == 'mouse_click' then
- self:check_click(data2,data3)
- elseif event == 'mouse_drag' then
- self:dragged(data2,data3)
- elseif event == 'mouse_up' then
- self:released()
- end
- end
- function drawbox_single(x, y, x_size, y_size, content, bg, fg)
- for i=x,x+x_size-1,1 do
- for j=y,y+y_size-1,1 do
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- term.setCursorPos(i,j)
- print(content)
- end
- end
- end
- function draw_txt_in_box(x, y, x_size, y_size, txt_string, bg, fg)
- --convert txt_string to an array of single char
- str_carray = {}
- for char_i in txt_string:gmatch(".") do
- table.insert(str_carray,char_i)
- end
- -- print with auto change line
- str_iterator = 1
- for i=x,x+x_size-1,1 do
- for j=y,y+y_size-1,1 do
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- term.setCursorPos(i,j)
- if str_iterator<=#str_carray then
- print(str_carray[str_iterator])
- end
- str_iterator = str_iterator + 1
- end
- end
- end
- function turtle_drop_with_quantity(item_name, 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 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)
- if item_total > 32 then
- turtle_drop_with_quantity(item_detail1.name,item_total-32)
- end
- else
- turtle.dropDown()
- end
- end
- end
- end
- function load_file_and_action()
- -- open file in read
- croplist_preserve = {}
- croplist_file = fs.open("auto_farmer_list", "r")
- end_of_file = false
- while end_of_file == false do
- crop_insert = croplist_file.readLine()
- if crop_insert == nil then
- end_of_file = true
- else
- table.insert(croplist_preserve,crop_insert)
- end
- end
- croplist_file.close()
- taskfile = fs.open("auto_farmer_task", "r")
- end_of_file = false
- while end_of_file == false do
- task_message = taskfile.readLine()
- if task_message == nil then
- end_of_file = true
- else
- turtle_chest_refuel()
- worker_decode_and_action(task_message)
- turtle_goto(2,0)
- turtle_selected_drop(croplist_preserve)
- turtle_organise()
- end
- end
- turtle_goto(1,1)
- turtle_face_turn(0)
- sleep(300)
- os.reboot()
- end
- --user data for interation
- function turtle_find_seed()
- for i=1,16 do
- turtle.select(i)
- if turtle.getItemDetail() then
- item_detail = turtle.getItemDetail()
- if findIndex(croplist_input,item_detail.name) == nil then
- table.insert(croplist_input,item_detail.name)
- end
- end
- end
- end
- function findIndex(table, search)
- for i, str in ipairs(table) do
- if str == search then
- return i
- end
- end
- return nil
- end
- function create_farmplot_button(canvas_owner, x, y,content,description)
- farmplot = create_ui_element_no_ins(canvas_owner, x,y,content,description)
- -- defining pressed action
- local function pressed(self,x,y)
- self.crop = selected_crop_name
- end
- farmplot.pressed = pressed
- -- defining content update action
- local function content_update(self)
- if findIndex(self.canvas_obj.croplist, self.crop) ~= nil then
- self.content = tostring(findIndex(self.canvas_obj.croplist, self.crop))
- else
- self.content=0
- end
- end
- farmplot.content_update = content_update
- return farmplot
- end
- function create_crop_text_element(canvas_owner,x,y,content)
- textbox = create_ui_element_no_ins(canvas_owner, x, y, content, 'textbox')
- textbox.cropname = 'nothing'
- local function render(self)
- draw_txt_in_box(self.virtual_x,self.virtual_y,self.x_size,self.y_size,content,self.bg,self.fg)
- end
- textbox.render = render
- local function pressed(self,x,y)
- selected_crop_name = self.cropname
- end
- textbox.pressed = pressed
- local function content_update(self)
- if selected_crop_name == self.cropname then
- self.fg = colors.yellow
- else
- self.fg = colors.white
- end
- end
- textbox.content_update = content_update
- textbox.x_size = canvas_owner.x_size
- return textbox
- end
- function user_gui_interation(plot_size_x, plot_size_y, croplist)
- local taskfile = fs.open("auto_farmer_task", "w")
- local croplist_file = fs.open("auto_farmer_list", "w")
- --generate plot canvas
- plot_canvas = create_canvas(1,1,20,10,'plot_canvas')
- plot_canvas.croplist = croplist
- for i=1,plot_size_x,1 do
- for j=1,plot_size_y,1 do
- plot_button = create_farmplot_button(plot_canvas,i,plot_size_y-j+1,'0','farmplot')
- plot_button.actual_x = i
- plot_button.actual_y = j
- plot_button.crop = 'none'
- table.insert(plot_canvas.elements, plot_button)
- end
- end
- if plot_size_x>plot_canvas.x_size then
- plot_canvas:hbar()
- end
- if plot_size_y>plot_canvas.y_size then
- plot_canvas:vbar()
- end
- --generate croplist canvas
- crop_canvas = create_canvas(21,1,19,10,'crops_display')
- for cIndex, cropname in pairs(croplist) do
- -- assemble string
- content_string = cIndex .. cropname
- -- draw textbox
- crop_box = create_crop_text_element(crop_canvas, crop_canvas.x, cIndex, content_string)
- crop_box.cropname = cropname
- crop_box.render_status=1
- table.insert(crop_box.canvas_obj.elements,crop_box)
- end
- --
- --generate functional canvas
- --
- functional_canvas = create_canvas(1,11,20,1,'funtional')
- exit_button = create_ui_element_no_ins(functional_canvas,1,11,'X','exit_button')
- exit_button.fg = colors.red
- local function pressed(self,x,y)
- screen_active=0
- end
- exit_button.pressed = pressed
- clear_button = create_ui_element_no_ins(functional_canvas,2,11, 'C', 'clear_button')
- local function pressed(self,x,y)
- selected_crop_name = 'nothing'
- end
- clear_button.pressed = pressed
- table.insert(functional_canvas.elements, exit_button)
- table.insert(functional_canvas.elements,clear_button)
- -- fill all canvases to screen
- -- create a screen first
- assign_plot_screen = Screen
- table.insert(assign_plot_screen.canvases,plot_canvas)
- table.insert(assign_plot_screen.canvases,crop_canvas)
- table.insert(assign_plot_screen.canvases,functional_canvas)
- -- create user interaction variables
- selected_crop_index = 0
- selected_crop_name = 'nothing'
- screen_active = 1
- --- screen loop here
- while screen_active == 1 do
- term.clear()
- assign_plot_screen:update()
- assign_plot_screen:render()
- assign_plot_screen:pipe_inputs()
- end
- --generate files needed
- -- go thruogh crop list
- for seed_it_num,seed_name in pairs(croplist) do
- -- for each crop in croplist, go thruogh every element in plot_canvas.elements()
- x_dests_buff = {}
- y_dests_buff = {}
- visited = {}
- current_length = 1
- for _, plot_element in pairs(plot_canvas.elements) do
- if plot_element.data0 == 'farmplot' then
- if plot_element.crop == seed_name then
- x_dests_buff[current_length] = plot_element.actual_x
- y_dests_buff[current_length] = plot_element.actual_y
- visited[current_length] = 0
- current_length = current_length + 1
- end
- end
- end
- task_string = plane_encode(x_dests_buff,y_dests_buff,visited,current_length-1,seed_name)
- taskfile.writeLine(task_string)
- croplist_file.writeLine(seed_name)
- end
- x_destinations = {}
- y_destinations = {}
- visited = {}
- array_length = 0
- squarefill(plot_size_x,plot_size_y,1)
- task_string = plane_encode(x_destinations,y_destinations,visited,array_length,'notfound')
- taskfile.writeLine(task_string)
- --reboot
- os.reboot()
- end
- print('starting AutoFarmer v 1.0 ...')
- sleep(2)
- print('checking for configuration file')
- sleep(1)
- x_plot_size = 0
- y_plot_size = 0
- croplist_input = {}
- -- greet user
- -- check for configuration files
- if not fs.exists("auto_farmer_task") then
- term.clear()
- term.setCursorPos(1,1)
- print("input size of farmplot in x direction")
- x_plot_size = tonumber(read())
- print("input size of farmplot in y direction")
- y_plot_size = tonumber(read())
- term.clear()
- print("put seed in the turtle inventory")
- print("press enter to continue")
- _ = read(" ")
- turtle_find_seed()
- -- starting gui
- user_gui_interation(x_plot_size,y_plot_size,croplist_input)
- end
- print("configuration file found")
- print("loading file")
- load_file_and_action()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement