Advertisement
samzzy

AutoFarmer_v0.1

Mar 28th, 2023 (edited)
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.00 KB | None | 0 0
  1. -- turtle movement codes ------------------------------
  2. --
  3. -- defining variables of coordinates and direction
  4. -- Initialize parameters for the location and direction
  5. x_live_coord = 1
  6. y_live_coord = 1
  7. z_live_coord = 1
  8. live_facing_direction = 0
  9. item_name_g = nil
  10. destroy_status_g = 1
  11.  
  12. -- Basic moveset functions turning and forward
  13.  
  14. function up_rec()
  15.     repeat
  16.         turtle.digUp()
  17.     until turtle.up()
  18.     z_live_coord = z_live_coord + 1
  19. end
  20.  
  21. function down_rec()
  22.     repeat
  23.         turtle.digDown()
  24.     until turtle.down()
  25.     z_live_coord = z_live_coord - 1
  26. end
  27.  
  28. function goto_z(z_dest)
  29.     if z_dest > z_live_coord then
  30.         repeat
  31.             up_rec()
  32.         until z_live_coord == z_dest
  33.     elseif z_dest < z_live_coord then
  34.         repeat
  35.             down_rec()
  36.         until z_live_coord == z_dest
  37.     end
  38. end
  39.  
  40. function rec_turn_right()
  41.     -- action
  42.     turtle.turnRight()
  43.  
  44.     -- recording
  45.     live_facing_direction = live_facing_direction + 1
  46.     if (live_facing_direction == 4) then
  47.         live_facing_direction = 0
  48.     end
  49. end
  50.  
  51. function rec_turn_left()
  52.     -- action
  53.     turtle.turnLeft()
  54.  
  55.     -- recording
  56.     live_facing_direction = live_facing_direction - 1
  57.     if (live_facing_direction == -1) then
  58.         live_facing_direction = 3
  59.     end
  60.  
  61. end
  62.  
  63. function rec_forward_firm()
  64.     -- action
  65.     repeat
  66.         turtle.dig()
  67.     until turtle.forward()
  68.  
  69.     --recording
  70.     if (live_facing_direction == 0) then
  71.         y_live_coord = y_live_coord + 1
  72.     elseif (live_facing_direction == 1) then
  73.         x_live_coord = x_live_coord + 1
  74.     elseif (live_facing_direction == 2) then
  75.         y_live_coord = y_live_coord - 1
  76.     elseif (live_facing_direction == 3) then
  77.         x_live_coord = x_live_coord - 1
  78.     end
  79.     print(x_live_coord, y_live_coord)
  80.     while turtle.getFuelLevel() < 80 do
  81.     print("going out of fuel")
  82.     refuel_with("minecraft:coal")
  83.     end
  84. end
  85.  
  86.  
  87. function up_firm()
  88.     repeat
  89.         turtle.digUp()
  90.     until turtle.up()
  91. end
  92.  
  93. function down_firm()
  94.     repeat
  95.         turtle.digDown()
  96.     until turtle.down()
  97. end
  98.  
  99. function turtle_face_turn(face)
  100.     if (face - live_facing_direction == 1 or face - live_facing_direction == -3) then
  101.         rec_turn_right()
  102.     elseif(face - live_facing_direction == -1 or face - live_facing_direction == 3) then
  103.         rec_turn_left()
  104.     elseif(face == live_facing_direction) then
  105.         print("not turning")    
  106.     else
  107.         rec_turn_left()
  108.         rec_turn_left()
  109.     end
  110. end
  111.  
  112.  
  113. -- the GOTO function, give one coordinate and move to the function
  114. -- first align the facing direction to 0
  115. -- get to the coordinate and return facing direction to 0
  116. function turtle_goto(a,b)
  117.     -- calculation
  118.     delta_x = a - x_live_coord
  119.     delta_y = b - y_live_coord
  120.  
  121.     -- action section below
  122.     -- facing the correct direction for x movement
  123.     if (delta_x > 0) then
  124.         turtle_face_turn(1)
  125.     elseif (delta_x < 0) then
  126.         turtle_face_turn(3)
  127.         delta_x = - delta_x
  128.     end
  129.  
  130.  
  131.     -- x movement
  132.     for x_movement = 0,delta_x - 1,1
  133.     do
  134.         rec_forward_firm()
  135.     end
  136.  
  137.  
  138.     -- facing the correct direction for y movement
  139.     if (delta_y > 0) then
  140.         turtle_face_turn(0)
  141.     elseif (delta_y < 0) then
  142.         turtle_face_turn(2)
  143.         delta_y = - delta_y
  144.     end
  145.     -- y movement
  146.     for y_movement = 0,delta_y - 1,1
  147.     do
  148.         rec_forward_firm()
  149.     end
  150.  
  151.  
  152.     -- returning to the 0 facing direction
  153.  
  154. end
  155.  
  156. -- the pathfinding function, will try to visit all points in an array
  157.  
  158. -- some utilities
  159.  
  160. function pathfind_and_action()
  161.     for loop_num=1, array_length, 1
  162.     do
  163.         print("NEXT!!")
  164.         near_init_term_num = 1
  165.         while visited[near_init_term_num] == 1 do
  166.             near_init_term_num = near_init_term_num + 1
  167.         end
  168.        
  169.         nearest_x = x_destinations[near_init_term_num]
  170.         nearest_y = y_destinations[near_init_term_num]
  171.         min_distance = math.abs(nearest_x - x_live_coord) +  math.abs(nearest_y - y_live_coord)
  172.        
  173.         last_item_num = near_init_term_num
  174.         for item_num = 1, array_length, 1
  175.         do
  176.             if visited[item_num] == 0 then
  177.                 if (math.abs(x_destinations[item_num] - x_live_coord) +  math.abs(y_destinations[item_num] - y_live_coord)) < min_distance then
  178.                     nearest_x = x_destinations[item_num]
  179.                     nearest_y = y_destinations[item_num]
  180.                     min_distance = math.abs(nearest_x - x_live_coord) +  math.abs(nearest_y - y_live_coord)
  181.                     last_item_num = item_num
  182.                 end            
  183.             end
  184.         end
  185.         visited[last_item_num] = 1
  186.         print("going to ", nearest_x, nearest_y)
  187.         turtle_goto(nearest_x,nearest_y)
  188.  
  189.         turtle_action()
  190.     end
  191.  
  192. end
  193.  
  194.  
  195.  
  196. -- action loop section
  197.  
  198. function place_item(item_name)
  199.     item_tot = 0
  200.     for cell_num = 1,16 do
  201.         turtle.select(cell_num)
  202.         if turtle.getItemDetail() then
  203.             item_detail = turtle.getItemDetail()
  204.             if (item_detail.name == item_name) then
  205.                 turtle.placeDown()
  206.                 item_tot = item_tot + item_detail.count
  207.             end
  208.         end
  209.     end
  210. end
  211.  
  212. function refuel_with(item_name)
  213.     item_tot = 0
  214.     for cell_num = 1,16 do
  215.         turtle.select(cell_num)
  216.         if turtle.getItemDetail() then
  217.             item_detail = turtle.getItemDetail()
  218.             if (item_detail.name == item_name) then
  219.                 turtle.refuel()
  220.                 item_tot = item_tot + item_detail.count
  221.             end
  222.         end
  223.     end
  224.     if item_tot < 1 then
  225.         distress_item_request(item_name,1,'worker_to_supply','supply_to_worker')
  226.     end
  227. end
  228.  
  229.  
  230.  
  231.  
  232. -- example code of coordinates given
  233.  
  234.  
  235.  
  236. function turtle_action()
  237.     val, data = turtle.inspectDown()
  238.     if turtle.detectDown() then
  239.         if (data.state.age == 7) and (item_name_g ~= 'notfound') then
  240.             turtle.digDown()
  241.         end
  242.     end
  243.     turtle.suckDown()
  244.     place_item(item_name_g)
  245. end
  246.  
  247.  
  248. function plane_execution(x_destinations_input, y_destinations_input, visited_input, array_length_input, item_name)
  249.     x_destinations = x_destinations_input
  250.     y_destinations = y_destinations_input
  251.     visited = visited_input
  252.     array_length = array_length_input
  253.     item_name_g = item_name
  254.     pathfind_and_action()
  255.     turtle_goto(1,1)
  256.     turtle_face_turn(0)
  257. end
  258.  
  259. function squarefill(a,b,s)
  260.     array_length = a*b
  261.     current_length=1
  262.     for xfill=1,a,s
  263.     do
  264.         for yfill=1,b,s
  265.         do
  266.             x_destinations[current_length]=xfill
  267.             y_destinations[current_length]=yfill
  268.             visited[current_length]=0
  269.             current_length=current_length + 1
  270.         end
  271.     end
  272. end
  273.  
  274. function if_inlist(item_list)
  275.     inlist = 0
  276.     for num, item_name_l in pairs(item_list) do
  277.         item_data = turtle.getItemDetail()
  278.         if turtle.getItemDetail() then
  279.             if string.match(item_data.name, item_name_l) then
  280.                 inlist = 1
  281.             end
  282.         end
  283.     end
  284.     return inlist
  285. end
  286.  
  287. function clear_backpack(white_list)
  288.     for i=1,16,1 do
  289.         turtle.select(i)
  290.         if if_inlist(white_list) == 0 then
  291.             turtle.drop()
  292.         end
  293.     end
  294. end
  295.  
  296. -- functions in charge of encoding and decoding
  297. function plane_encode(x_dests, y_dests, visited, array_l, item_name)
  298.     task_name = 'plane'
  299.     x_dests_str = '/'
  300.     y_dests_str = '/'
  301.     visited_str = '/'
  302.     for i=1,array_l do
  303.     x_dests_str = x_dests_str .. tostring(x_dests[i]) .. '/'
  304.     y_dests_str = y_dests_str .. tostring(y_dests[i]) .. '/'
  305.     visited_str = visited_str .. tostring(visited[i]) .. '/'
  306.     end
  307.     xyv_dests_str = (x_dests_str .. 'x' .. y_dests_str .. 'x' .. visited_str)
  308.     out_message = (task_name .. 'x' .. xyv_dests_str .. 'x' .. item_name)
  309.     -- broadcasting now
  310.     return out_message
  311. end
  312. function vertical_encode(z_dest, protocol)
  313.     task_name = 'vert'
  314.     out_message = (task_name .. 'x' .. z_dest)
  315.     rednet.broadcast(out_message, protocol)
  316. end
  317.  
  318.  
  319. function worker_decode_and_action(msg)
  320.     matched_func = string.gmatch(msg,'([^x]+)')
  321.     task_name = matched_func()
  322.     print(task_name)
  323.     if task_name == 'vert' then
  324.         goto_z(tonumber(matched_func()))
  325.     elseif task_name == 'plane' then
  326.     x_dests_str = matched_func()    
  327.     y_dests_str = matched_func()
  328.     visited_str = matched_func()
  329.     item_name = matched_func()
  330.     -- start filling in
  331.     array_length = 0
  332.     x_destinations = {}
  333.     y_destinations = {}
  334.     visited = {}
  335.  
  336.     for x_i in string.gmatch(x_dests_str,'([^/]+)')
  337.     do
  338.         array_length = array_length + 1
  339.         x_destinations[array_length] = tonumber(x_i)
  340.     end
  341.      
  342.     for y_i in string.gmatch(y_dests_str,'([^/]+)')
  343.     do
  344.         y_destinations[#y_destinations + 1] = tonumber(y_i)
  345.     end
  346.  
  347.     for v_i in string.gmatch(visited_str,'([^/]+)')
  348.     do
  349.         visited[#visited + 1] = tonumber(v_i)
  350.     end
  351.     plane_execution(x_destinations, y_destinations, visited, array_length, item_name)
  352.     end
  353. end
  354.  
  355. function distress_item_request(item_name,s_number,out_protocol,in_protocol)
  356.     -- message format looking like 1x2x3xminecraft:coal
  357.     out_message = (x_live_coord .. 'x' .. y_live_coord .. 'x' .. z_live_coord .. 'x' .. item_name .. 'x' .. s_number)
  358.     rednet.broadcast(out_message, out_protocol)
  359.     -- check if item is received
  360.     id, msg = rednet.receive(in_protocol)
  361. end
  362.  
  363. function count_item(item_name)
  364.     item_count = 0
  365.     for cell_num = 1,16 do
  366.     turtle.select(cell_num)
  367.     if turtle.getItemDetail() then
  368.             item_detail = turtle.getItemDetail()
  369.             if (item_detail.name == item_name) then
  370.         item_count = item_count + item_detail.count
  371.             end
  372.         end
  373.     end
  374.     return item_count
  375. end
  376.  
  377. function drop_item(item_name,num_of_stack)
  378.     dropped_count = 0
  379.     for i=1,num_of_stack,1 do
  380.     turtle.select(i)
  381.     turtle.dropDown()
  382.     end
  383. end
  384.  
  385. function distress_item_decode_action(msg,return_protocol)
  386.     matched_func = string.gmatch(msg,'([^x]+)')
  387.     x_dest = matched_func()
  388.     y_dest = matched_func()
  389.     z_dest = matched_func()
  390.     item_name = matched_func()
  391.     stack_num_r = matched_func()
  392.  
  393.  
  394.     -- check if there is enough fuel
  395.     -- go to fuel chest
  396.     if turtle.getFuelLevel()<4000 then
  397.         turtle_goto(0,findIndex(chest_item,'minecraft:coal'))
  398.     turtle.suckDown()
  399.     turtle.select(1)
  400.     turtle.refuel()
  401.     end
  402.  
  403.     -- go to item chest
  404.  
  405.     turtle_goto(0,findIndex(chest_item,item_name))
  406.     for i = 1,stack_num_r do
  407.     turtle.suckDown()
  408.     end
  409.  
  410.     -- go deliver item
  411.     goto_z(z_dest+1)
  412.     turtle_goto(x_dest,y_dest)
  413.     drop_item(item_name, stack_num_r)
  414.    
  415.     -- return and resume
  416.     turtle_goto(0,0)
  417.     goto_z(1)
  418.     turtle_face_turn(0)
  419.     -- return underlivered items
  420.     turtle_goto(0,findIndex(chest_item,item_name))
  421.     drop_item(item_name,16)
  422.     -- send message of completion
  423.     rednet.broadcast('yay',return_protocol)
  424. end
  425.  
  426.  
  427. function turtle_chest_refuel()
  428.     if turtle.getFuelLevel() < 1000 then
  429.     turtle_goto(1,0)
  430.         turtle.suckDown()
  431.         refuel_with("minecraft:coal")
  432.         turtle_goto(1,1)
  433.         turtle_face_turn(0)
  434.     end
  435. end
  436.  
  437. function turtle_organise()
  438.     for x=1,16,1 do
  439.     if turtle.getItemCount(x) < 64 then
  440.         turtle.select(x)
  441.         for y=x+1,16 do --Avoids testing the same slot
  442.         turtle.transferTo(y)
  443.         end
  444.     end
  445.     end
  446. end
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454. ----- ui elements----------------------------------------
  455. -- defining screen
  456. Screen = {canvases={}}
  457. function Screen.render(self)
  458.     for _p, canvas in pairs(self.canvases) do
  459.     canvas:render()
  460.     end
  461. end
  462. function Screen.pipe_inputs(self)
  463.     event, data1, data2, data3 = os.pullEvent()
  464.     for _p, canvas in pairs(self.canvases) do
  465.         canvas:receive_event(event, data1, data2, data3)
  466.     end
  467. end
  468. function Screen.update(self)
  469.  
  470.     for _p, canvas in pairs(self.canvases) do
  471.     canvas:update()
  472.     end
  473. end
  474.  
  475. -- defining canvas
  476. 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'}
  477. function create_canvas(x, y, x_size, y_size, data0)
  478.     local new_canvas = {}
  479.     setmetatable(new_canvas, {__index = Canvas_prototype })
  480.     new_canvas.elements={}
  481.     new_canvas.x = x
  482.     new_canvas.y = y
  483.     new_canvas.x_size = x_size
  484.     new_canvas.y_size = y_size
  485.     new_canvas.data0 = data0
  486.     return new_canvas
  487. end
  488.  
  489. function Canvas_prototype.event_testing(self)
  490.     event, data1, data2, data3 = os.pullEvent()
  491.     self:receive_event(event,data1,data2,data3)
  492. end
  493.  
  494. function Canvas_prototype.receive_event(self, event, data1, data2, data3)
  495.     for _, element in pairs(self.elements) do
  496.     element:receive_event(event, data1, data2, data3)
  497.     end
  498. end
  499.  
  500. function Canvas_prototype.render(self)
  501.     for _, element in pairs(self.elements) do
  502.     if element.render_status == 1 then
  503.         element:render()
  504.     end
  505.  
  506.     end
  507. end
  508.  
  509. function Canvas_prototype.update(self)
  510.     --get largest virtual sizes
  511.     virtual_x_acc = 1
  512.     virtual_y_acc = 1
  513.     -- by going through all element virtual sizes
  514.     for _, element in pairs(self.elements) do
  515.     if element.virtual_x - self.x + 1 > virtual_x_acc then
  516.         virtual_x_acc = element.virtual_x - self.x
  517.     end
  518.     if element.virtual_y - self.y + 1 > virtual_y_acc then
  519.         virtual_y_acc = element.virtual_y - self.y
  520.     end
  521.     end
  522.     self.v_x_size = virtual_x_acc
  523.     self.v_y_size = virtual_y_acc
  524.     --update render coordinates of each element based on offset
  525.     for _, element in pairs(self.elements) do
  526.     element.render_x = element.virtual_x-self.x_offset
  527.     element.render_y = element.virtual_y-self.y_offset
  528.     -- determing which to render
  529.     --
  530.     local is_within_bounds = element.render_x >= self.x and element.render_x < self.x + self.x_size
  531.                         and element.render_y >= self.y and element.render_y < self.y + self.y_size
  532.     if is_within_bounds then
  533.         element.render_status = 1
  534.     else
  535.         element.render_status = 0
  536.     end
  537.     -- update every element as well
  538.     element:content_update()
  539.     end
  540. end
  541.  
  542.  
  543.  
  544.  
  545.  
  546. -- hbar and vbar is creating a new ui elements and stuff it in Canvas.elements
  547. function Canvas_prototype.vbar(self)
  548.     local new_vbar = create_ui_element_no_ins(self, self.x+self.x_size-1, self.y, 'o', 'vertical scrollbar')
  549.     new_vbar.render_x = new_vbar.virtual_x
  550.     new_vbar.render_y = new_vbar.virtual_y
  551.     new_vbar.y_size = self.y_size
  552.     new_vbar.fg_y = new_vbar.canvas_obj.y
  553.     new_vbar.fg_y_size = 1
  554.     new_vbar.drag_init_y = 0
  555.     new_vbar.drag_init_offset = 0
  556.     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
  557.  
  558.  
  559.     local function render(self_bar)
  560.     --draw bg
  561.     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)
  562.     --draw fg
  563.     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)
  564.    
  565.     end
  566.  
  567.     new_vbar.render = render
  568.  
  569.     local function dragged(self_bar, x_dragged, y_dragged)
  570.     --using canvas x_y size location and mouse drag update, determing in real time how y offset should be adjusted
  571.     if self_bar.pressed_down_mid == 1 then
  572.         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)
  573.     end
  574.     end
  575.  
  576.     new_vbar.dragged = dragged
  577.  
  578.     local function pressed(self_bar, x_pressed, y_pressed)
  579.         -- if clicked on fg bar change pressed_down status, if clicked on bg bar on either side, increment canvas y offset
  580.     if y_pressed<self_bar.fg_y+self_bar.canvas_obj.y then
  581.         self_bar.canvas_obj.y_offset = self_bar.canvas_obj.y_offset - 1
  582.     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
  583.         self_bar.pressed_down_mid = 1
  584.         self_bar.drag_init_y = y_pressed
  585.         self_bar.drag_init_offset = self_bar.canvas_obj.y_offset
  586.     elseif self_bar.fg_y + self_bar.fg_y_size - 1 + self_bar.canvas_obj.y< y_pressed then
  587.         self_bar.canvas_obj.y_offset = self_bar.canvas_obj.y_offset + 1
  588.     end
  589.     end
  590.  
  591.     new_vbar.pressed = pressed
  592.  
  593.     local function content_update(self_bar)
  594.         -- calculate fg_y_size
  595.     self_bar.fg_y_size = math.floor(self_bar.canvas_obj.y_size^2/self_bar.canvas_obj.v_y_size)
  596.     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))
  597.     -- undo offset
  598.     new_vbar.render_x = new_vbar.virtual_x
  599.     new_vbar.render_y = new_vbar.virtual_y
  600.     -- set render to 1
  601.     self_bar.render_status = 1
  602.     end
  603.  
  604.     new_vbar.content_update = content_update
  605.  
  606.     table.insert(self.elements, new_vbar)
  607.  
  608. end
  609.  
  610.  
  611.  
  612. function Canvas_prototype.hbar(self)
  613.     local new_hbar = create_ui_element_no_ins(self, self.x, self.y+self.y_size-1, 'o', 'horizontal scrollbar')
  614.     new_hbar.render_x = new_hbar.virtual_x
  615.     new_hbar.render_y = new_hbar.virtual_y
  616.     new_hbar.y_size = 1
  617.     new_hbar.x_size = self.x_size
  618.     new_hbar.fg_x = new_hbar.canvas_obj.x
  619.     new_hbar.fg_x_size = 1
  620.     new_hbar.drag_init_x = 0
  621.     new_hbar.drag_init_offset = 0
  622.     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
  623.  
  624.  
  625.     local function render(self_bar)
  626.     --draw bg
  627.     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)
  628.     --draw fg
  629.     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)
  630.    
  631.     end
  632.  
  633.     new_hbar.render = render
  634.  
  635.     local function dragged(self_bar, x_dragged, y_dragged)
  636.     --using canvas x_y size location and mouse drag update, determing in real time how y offset should be adjusted
  637.     if self_bar.pressed_down_mid == 1 then
  638.         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)
  639.     end
  640.     end
  641.  
  642.     new_hbar.dragged = dragged
  643.  
  644.     local function pressed(self_bar, x_pressed, y_pressed)
  645.         -- if clicked on fg bar change pressed_down status, if clicked on bg bar on either side, increment canvas y offset
  646.     if x_pressed<self_bar.fg_x+self_bar.canvas_obj.x  then
  647.         self_bar.canvas_obj.x_offset = self_bar.canvas_obj.x_offset - 1
  648.     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
  649.         self_bar.pressed_down_mid = 1
  650.         self_bar.drag_init_x = x_pressed
  651.         self_bar.drag_init_offset = self_bar.canvas_obj.x_offset
  652.     elseif self_bar.fg_x + self_bar.fg_x_size + self_bar.canvas_obj.x - 1< x_pressed then
  653.         self_bar.canvas_obj.x_offset = self_bar.canvas_obj.x_offset + 1
  654.     end
  655.     end
  656.  
  657.     new_hbar.pressed = pressed
  658.  
  659.     local function content_update(self_bar)
  660.         -- calculate fg_y_size
  661.     self_bar.fg_x_size = math.floor(self_bar.canvas_obj.x_size^2/self_bar.canvas_obj.v_x_size)
  662.     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))
  663.     -- undo offset
  664.     new_hbar.render_x = new_hbar.virtual_x
  665.     new_hbar.render_y = new_hbar.virtual_y
  666.     -- set render to 1
  667.     self_bar.render_status = 1
  668.     end
  669.  
  670.     new_hbar.content_update = content_update
  671.  
  672.     table.insert(self.elements, new_hbar)
  673.  
  674. end
  675.  
  676.  
  677. -- defining ui_element
  678. --
  679. 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}
  680. function create_ui_element(canvas_obj, virtual_x,virtual_y,content,data)
  681.     local new_ui_element = {}
  682.     setmetatable(new_ui_element, {__index = ui_element_prototype })
  683.     new_ui_element.virtual_x = virtual_x
  684.     new_ui_element.virtual_y = virtual_y
  685.     new_ui_element.content = content
  686.     new_ui_element.data0 = data
  687.     new_ui_element.canvas_obj = canvas_obj
  688.     table.insert(canvas_obj.elements, new_ui_element)
  689. end
  690.  
  691. function create_ui_element_no_ins(canvas_obj, virtual_x,virtual_y,content,data)
  692.     local new_ui_element = {}
  693.     setmetatable(new_ui_element, {__index = ui_element_prototype })
  694.     new_ui_element.virtual_x = virtual_x
  695.     new_ui_element.virtual_y = virtual_y
  696.     new_ui_element.content = content
  697.     new_ui_element.data0 = data
  698.     new_ui_element.canvas_obj = canvas_obj
  699.     return new_ui_element
  700. end
  701.  
  702.  
  703.  
  704. function ui_element_prototype.render(self)
  705.     if self.render_status == 1 then
  706.     drawbox_single(self.render_x, self.render_y, self.x_size, self.y_size, self.content, self.bg, self.fg )
  707.     end
  708. end
  709.  
  710. function ui_element_prototype.check_click(self,x_cl,y_cl)
  711.     if (self.canvas_obj.x <= x_cl and x_cl <= self.canvas_obj.x + self.canvas_obj.x_size) and
  712.    (self.canvas_obj.y <= y_cl and y_cl <= self.canvas_obj.y + self.canvas_obj.y_size) then
  713.     if self.render_status == 1 then
  714.         if (self.render_x <= x_cl and x_cl < self.render_x + self.x_size) and
  715.    (self.render_y <= y_cl and y_cl < self.render_y + self.y_size) then
  716.         self:pressed(x_cl,y_cl)
  717.         self.pressed_down = 1
  718.         end
  719.     end
  720.     end
  721. end
  722.  
  723.  
  724. function ui_element_prototype.pressed(self)
  725.     -- does nothing
  726. end
  727.  
  728. function ui_element_prototype.dragged(self)
  729.     --does nothing
  730. end
  731.  
  732. function ui_element_prototype.released(self)
  733.     self.pressed_down = 0
  734.     if self.pressed_down_mid ~= nil then
  735.     self.pressed_down_mid = 0
  736.     end
  737. end
  738.  
  739. function ui_element_prototype.content_update(self)
  740.     -- does nothing
  741.     if self.pressed_down == 1 then
  742.     self.fg = colors.red
  743.     else
  744.     self.fg = colors.green
  745.     end
  746. end
  747.  
  748.  
  749.  
  750. function ui_element_prototype.receive_event(self, event, data1, data2, data3)
  751.     if event == 'mouse_click' then
  752.         self:check_click(data2,data3)
  753.     elseif event == 'mouse_drag' then
  754.         self:dragged(data2,data3)
  755.     elseif event == 'mouse_up' then
  756.     self:released()
  757.     end
  758. end
  759.  
  760. function drawbox_single(x, y, x_size, y_size, content, bg, fg)
  761.     for i=x,x+x_size-1,1 do
  762.     for j=y,y+y_size-1,1 do
  763.         term.setBackgroundColor(bg)
  764.         term.setTextColor(fg)
  765.         term.setCursorPos(i,j)
  766.         print(content)
  767.     end
  768.     end
  769. end
  770.  
  771. function draw_txt_in_box(x, y, x_size, y_size, txt_string, bg, fg)
  772.     --convert txt_string to an array of single char
  773.     str_carray = {}
  774.     for char_i in txt_string:gmatch(".") do
  775.         table.insert(str_carray,char_i)
  776.     end
  777.     -- print with auto change line
  778.     str_iterator = 1
  779.     for i=x,x+x_size-1,1 do
  780.         for j=y,y+y_size-1,1 do
  781.             term.setBackgroundColor(bg)
  782.         term.setTextColor(fg)
  783.         term.setCursorPos(i,j)
  784.         if str_iterator<=#str_carray then
  785.             print(str_carray[str_iterator])
  786.         end
  787.         str_iterator = str_iterator + 1
  788.         end
  789.     end
  790. end
  791.  
  792. function turtle_drop_with_quantity(item_name, drop_number)
  793.     drop_counter = 0
  794.     selected_cell = 1
  795.     while drop_counter<drop_number do
  796.     turtle.select(selected_cell)
  797.     if turtle.getItemDetail() then
  798.         item_detail = turtle.getItemDetail()
  799.         if item_detail.name == item_name then
  800.         turtle.dropDown(1)
  801.         drop_counter = drop_counter + 1
  802.         else
  803.         selected_cell = selected_cell + 1
  804.         end
  805.     else
  806.         selected_cell = selected_cell + 1
  807.     end
  808.     end
  809. end
  810.  
  811. function turtle_selected_drop(preserve_list)
  812.  
  813.     for drop_i=1,16,1 do
  814.         turtle.select(drop_i)
  815.     if turtle.getItemDetail() then
  816.         item_detail1 = turtle.getItemDetail()
  817.         if findIndex(preserve_list,item_detail1.name) ~= nil then
  818.         item_total = count_item(item_detail1.name)
  819.         if item_total > 32 then
  820.             turtle_drop_with_quantity(item_detail1.name,item_total-32)
  821.         end
  822.         else
  823.         turtle.dropDown()
  824.         end
  825.     end
  826.     end
  827. end
  828.  
  829. function load_file_and_action()
  830.     -- open file in read
  831.     croplist_preserve = {}
  832.     croplist_file = fs.open("auto_farmer_list", "r")
  833.     end_of_file = false
  834.     while end_of_file == false do
  835.     crop_insert = croplist_file.readLine()
  836.     if crop_insert == nil then
  837.         end_of_file = true
  838.     else
  839.         table.insert(croplist_preserve,crop_insert)
  840.     end
  841.     end
  842.     croplist_file.close()
  843.  
  844.     taskfile = fs.open("auto_farmer_task", "r")
  845.     end_of_file = false
  846.     while end_of_file == false do
  847.     task_message = taskfile.readLine()
  848.     if task_message == nil then
  849.         end_of_file = true
  850.     else
  851.         turtle_chest_refuel()
  852.         worker_decode_and_action(task_message)
  853.         turtle_goto(2,0)
  854.         turtle_selected_drop(croplist_preserve)
  855.         turtle_organise()
  856.     end
  857.     end
  858.     turtle_goto(1,1)
  859.     turtle_face_turn(0)
  860.     sleep(300)
  861.     os.reboot()
  862. end
  863.  
  864.  
  865.  
  866.  
  867. --user data for interation
  868. function turtle_find_seed()
  869.     for i=1,16 do
  870.         turtle.select(i)
  871.         if turtle.getItemDetail() then
  872.         item_detail = turtle.getItemDetail()
  873.         if findIndex(croplist_input,item_detail.name) == nil then
  874.         table.insert(croplist_input,item_detail.name)
  875.         end
  876.     end
  877.     end
  878. end
  879. function findIndex(table, search)
  880.     for i, str in ipairs(table) do
  881.     if str == search then
  882.         return i
  883.     end
  884.     end
  885.     return nil
  886. end
  887.  
  888.  
  889. function create_farmplot_button(canvas_owner, x, y,content,description)
  890.     farmplot = create_ui_element_no_ins(canvas_owner, x,y,content,description)
  891.     -- defining pressed action
  892.     local function pressed(self,x,y)
  893.     self.crop = selected_crop_name
  894.     end
  895.     farmplot.pressed = pressed
  896.     -- defining content update action
  897.     local function content_update(self)
  898.     if findIndex(self.canvas_obj.croplist, self.crop) ~= nil then
  899.         self.content = tostring(findIndex(self.canvas_obj.croplist, self.crop))
  900.     else
  901.         self.content=0
  902.     end
  903.     end
  904.     farmplot.content_update = content_update
  905.     return farmplot
  906. end
  907.  
  908. function create_crop_text_element(canvas_owner,x,y,content)
  909.     textbox = create_ui_element_no_ins(canvas_owner, x, y, content, 'textbox')
  910.     textbox.cropname = 'nothing'
  911.  
  912.     local function render(self)
  913.     draw_txt_in_box(self.virtual_x,self.virtual_y,self.x_size,self.y_size,content,self.bg,self.fg)
  914.     end
  915.     textbox.render = render
  916.  
  917.     local function pressed(self,x,y)
  918.     selected_crop_name = self.cropname
  919.     end
  920.     textbox.pressed = pressed
  921.  
  922.     local function content_update(self)
  923.     if selected_crop_name == self.cropname then
  924.         self.fg = colors.yellow
  925.     else
  926.         self.fg = colors.white
  927.     end
  928.     end
  929.     textbox.content_update = content_update
  930.  
  931.     textbox.x_size = canvas_owner.x_size
  932.     return textbox
  933.  
  934. end
  935.  
  936. function user_gui_interation(plot_size_x, plot_size_y, croplist)
  937.     local taskfile = fs.open("auto_farmer_task", "w")
  938.     local croplist_file = fs.open("auto_farmer_list", "w")
  939.     --generate plot canvas
  940.     plot_canvas = create_canvas(1,1,20,10,'plot_canvas')
  941.     plot_canvas.croplist = croplist
  942.     for i=1,plot_size_x,1 do
  943.         for j=1,plot_size_y,1 do
  944.             plot_button = create_farmplot_button(plot_canvas,i,plot_size_y-j+1,'0','farmplot')
  945.         plot_button.actual_x = i
  946.         plot_button.actual_y = j
  947.         plot_button.crop = 'none'
  948.         table.insert(plot_canvas.elements, plot_button)
  949.         end
  950.     end
  951.     if plot_size_x>plot_canvas.x_size then
  952.     plot_canvas:hbar()
  953.     end
  954.     if plot_size_y>plot_canvas.y_size then
  955.     plot_canvas:vbar()
  956.     end
  957.  
  958.     --generate croplist canvas
  959.     crop_canvas = create_canvas(21,1,19,10,'crops_display')
  960.     for cIndex, cropname in pairs(croplist) do
  961.     -- assemble string
  962.     content_string = cIndex .. cropname
  963.     -- draw textbox
  964.     crop_box = create_crop_text_element(crop_canvas, crop_canvas.x, cIndex, content_string)
  965.     crop_box.cropname = cropname
  966.     crop_box.render_status=1
  967.     table.insert(crop_box.canvas_obj.elements,crop_box)
  968.     end
  969.     --
  970.     --generate functional canvas
  971.     --
  972.     functional_canvas = create_canvas(1,11,20,1,'funtional')
  973.     exit_button = create_ui_element_no_ins(functional_canvas,1,11,'X','exit_button')
  974.     exit_button.fg = colors.red
  975.     local function pressed(self,x,y)
  976.         screen_active=0
  977.     end
  978.     exit_button.pressed = pressed
  979.  
  980.     clear_button = create_ui_element_no_ins(functional_canvas,2,11, 'C', 'clear_button')
  981.     local function pressed(self,x,y)
  982.     selected_crop_name = 'nothing'
  983.     end
  984.     clear_button.pressed = pressed
  985.  
  986.     table.insert(functional_canvas.elements, exit_button)
  987.     table.insert(functional_canvas.elements,clear_button)
  988.     -- fill all canvases to screen
  989.     -- create a screen first
  990.     assign_plot_screen = Screen
  991.     table.insert(assign_plot_screen.canvases,plot_canvas)
  992.     table.insert(assign_plot_screen.canvases,crop_canvas)
  993.     table.insert(assign_plot_screen.canvases,functional_canvas)
  994.     -- create user interaction variables
  995.     selected_crop_index = 0
  996.     selected_crop_name = 'nothing'
  997.     screen_active = 1
  998.  
  999.     --- screen loop here
  1000.     while screen_active == 1 do
  1001.     term.clear()
  1002.     assign_plot_screen:update()
  1003.     assign_plot_screen:render()
  1004.     assign_plot_screen:pipe_inputs()
  1005.     end
  1006.     --generate files needed
  1007.  
  1008.     -- go thruogh crop list
  1009.     for seed_it_num,seed_name in pairs(croplist) do
  1010.     -- for each crop in croplist, go thruogh every element in plot_canvas.elements()
  1011.     x_dests_buff = {}
  1012.     y_dests_buff = {}
  1013.     visited = {}
  1014.     current_length = 1
  1015.     for _, plot_element in pairs(plot_canvas.elements) do
  1016.         if plot_element.data0 == 'farmplot' then
  1017.         if plot_element.crop == seed_name then
  1018.             x_dests_buff[current_length] = plot_element.actual_x
  1019.             y_dests_buff[current_length] = plot_element.actual_y
  1020.             visited[current_length] = 0
  1021.             current_length = current_length + 1
  1022.         end
  1023.         end
  1024.     end
  1025.     task_string = plane_encode(x_dests_buff,y_dests_buff,visited,current_length-1,seed_name)
  1026.     taskfile.writeLine(task_string)
  1027.     croplist_file.writeLine(seed_name)
  1028.     end
  1029.     x_destinations = {}
  1030.     y_destinations = {}
  1031.     visited = {}
  1032.     array_length = 0
  1033.     squarefill(plot_size_x,plot_size_y,1)
  1034.     task_string = plane_encode(x_destinations,y_destinations,visited,array_length,'notfound')
  1035.     taskfile.writeLine(task_string)
  1036.     --reboot
  1037.     os.reboot()
  1038. end
  1039.  
  1040. print('starting AutoFarmer v 1.0 ...')
  1041. sleep(2)
  1042. print('checking for configuration file')
  1043. sleep(1)
  1044. x_plot_size = 0
  1045. y_plot_size = 0
  1046. croplist_input = {}
  1047.  
  1048. -- greet user
  1049. -- check for configuration files
  1050. if not fs.exists("auto_farmer_task") then
  1051.     term.clear()
  1052.     term.setCursorPos(1,1)
  1053.     print("input size of farmplot in x direction")
  1054.     x_plot_size = tonumber(read())
  1055.     print("input size of farmplot in y direction")
  1056.     y_plot_size = tonumber(read())
  1057.     term.clear()
  1058.     print("put seed in the turtle inventory")
  1059.     print("press enter to continue")
  1060.     _ = read(" ")
  1061.     turtle_find_seed()
  1062.     -- starting gui
  1063.     user_gui_interation(x_plot_size,y_plot_size,croplist_input)
  1064. end
  1065.  
  1066. print("configuration file found")
  1067. print("loading file")
  1068. load_file_and_action()
  1069.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement