le_Fish

CowFarmTurtle V2

Aug 13th, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.67 KB | None | 0 0
  1. -- Load different API's
  2. if fs.exists("extraFuncsApi") then
  3.     os.loadAPI("extraFuncsApi")
  4. else
  5.     shell.run("pastebin", "get", "5MwHEZxB", "extraFuncsApi")
  6.     os.loadAPI("extraFuncsApi")
  7. end
  8. if fs.exists("serverApi") then
  9.     os.loadAPI("serverApi")
  10. else
  11.     shell.run("pastebin", "get", "dazENzF5", "serverApi")
  12.     os.loadAPI("serverApi")
  13. end
  14. if fs.exists("facilityApi") then
  15.     os.loadAPI("facilityApi")
  16. else
  17.     shell.run("pastebin", "get", "Eyrmtw0m", "facilityApi")
  18.     os.loadAPI("facilityApi")
  19. end
  20.  
  21. local feederUpgrade = peripheral.wrap("left")
  22.  
  23. -- Set data table
  24. local farm_positions = {
  25.     home = vector.new(-3, 304, 8),
  26.     home_dir = "N",
  27.     farm_height = 7,
  28.     centerpoint = vector.new(-6, 295, 0),
  29.     total_farms = 8,
  30.     sectors = {
  31.             -- sector 1 = RV
  32.             -- sector 2 = RA
  33.             -- sector 3 = LV
  34.             -- sector 4 = LA
  35.             sector1 = {vector.new(1, 292, 0), vector.new(-3, 287, 0)},
  36.             sector2 = {vector.new(1, 297, 0), vector.new(-3, 302, 0)},
  37.             sector3 = {vector.new(-14, 292, 0), vector.new(-10, 287, 0)},
  38.             sector4 = {vector.new(-14, 297, 0), vector.new(-10, 302, 0)}
  39.     },
  40.     z_heights = {
  41.             [1] = 7
  42.     }
  43. }
  44. local farm_counter = 1
  45. local task = ""
  46.  
  47. function cowFarm()
  48.     this_turtle:get_gps()
  49.     goUpSector(farm_counter)
  50.     goToX(farm_positions.centerpoint)
  51.     goToY(farm_positions.centerpoint)
  52.     redNetFunction(true)
  53.     for i = 1, 4 do
  54.             goToSector(i)
  55.             feedCows(i)
  56.             goToX(farm_positions.centerpoint)
  57.             goToY(farm_positions.centerpoint)
  58.     end
  59.     this_turtle:get_gps()
  60.     goBackFromCenterpoint()
  61.     specialSleepFunction(10)
  62.     redNetFunction(false)
  63.     farm_counter = farm_counter + 1
  64.     if farm_counter > 8 then
  65.             farm_counter = 1
  66.             specialSleepFunction(100)
  67.     end
  68.     cowFarm()
  69. end
  70. function feedCows(sector)
  71.     this_turtle:get_gps()
  72.     local go_to_dir = "N"
  73.     if farm_positions.sectors["sector" .. sector][2].y ~= farm_positions.sectors["sector" .. sector][1].y then
  74.             if farm_positions.sectors["sector" .. sector][2].y > farm_positions.sectors["sector" .. sector][1].y then
  75.                     go_to_dir = "N"
  76.             else
  77.                     go_to_dir = "S"
  78.             end
  79.     elseif farm_positions.sectors["sector" .. sector][2].x ~= farm_positions.sectors["sector" .. sector][1].x then
  80.             if farm_positions.sectors["sector" .. sector][2].x < farm_positions.sectors["sector" .. sector][1].x then
  81.                     go_to_dir = "E"
  82.             else
  83.                     go_to_dir = "W"
  84.             end
  85.     end
  86.     local new_z_place = vector.new(0, 0, 0)
  87.  
  88.     new_z_place.z = farm_positions.z_heights[farm_counter] - 1
  89.  
  90.     goToZ(new_z_place)
  91.  
  92.     repeat
  93.             this_turtle:forward()
  94.             if go_to_dir == "E" or go_to_dir == "W" then
  95.                     repeat
  96.                             this_turtle:forward()
  97.                     until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].y or
  98.                             this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].y
  99.             elseif go_to_dir == "N" or go_to_dir == "S" then
  100.                     repeat
  101.                             feedWheat()
  102.                             this_turtle:forward()
  103.                             feedWheat()
  104.                     until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].x or
  105.                             this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].x
  106.             end
  107.  
  108.             if this_turtle.dir == "N" then
  109.                     if go_to_dir == "O" then
  110.                             this_turtle:right()
  111.                             this_turtle:forward()
  112.                             this_turtle:right()
  113.                     elseif go_to_dir == "W" then
  114.                             this_turtle:left()
  115.                             this_turtle:forward()
  116.                             this_turtle:left()
  117.                     end
  118.             elseif this_turtle.dir == "E" then
  119.                     if go_to_dir == "S" then
  120.                             this_turtle:right()
  121.                             this_turtle:forward()
  122.                             this_turtle:right()
  123.                     elseif go_to_dir == "N" then
  124.                             this_turtle:left()
  125.                             this_turtle:forward()
  126.                             this_turtle:left()
  127.                     end
  128.             elseif this_turtle.dir == "S" then
  129.                     if go_to_dir == "W" then
  130.                             this_turtle:right()
  131.                             this_turtle:forward()
  132.                             this_turtle:right()
  133.                     elseif go_to_dir == "O" then
  134.                             this_turtle:left()
  135.                             this_turtle:forward()
  136.                             this_turtle:left()
  137.                     end
  138.             elseif this_turtle.dir == "W" then
  139.                     if go_to_dir == "N" then
  140.                             this_turtle:right()
  141.                             this_turtle:forward()
  142.                             this_turtle:right()
  143.                     elseif go_to_dir == "S" then
  144.                             this_turtle:left()
  145.                             this_turtle:forward()
  146.                             this_turtle:left()
  147.                     end
  148.             end
  149.     until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].x and
  150.             this_turtle.pos.y == farm_positions.sectors["sector" .. sector][1].y
  151.     if go_to_dir == "E" or go_to_dir == "W" then
  152.             repeat
  153.                     this_turtle:forward()
  154.             until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].y or
  155.                     this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].y
  156.     elseif go_to_dir == "N" or go_to_dir == "S" then
  157.             repeat
  158.                     feedWheat()
  159.                     this_turtle:forward()
  160.                     feedWheat()
  161.             until this_turtle.pos.x == farm_positions.sectors["sector" .. sector][1].x or
  162.                     this_turtle.pos.x == farm_positions.sectors["sector" .. sector][2].x
  163.     end
  164.     new_z_place.z = farm_positions.z_heights[farm_counter]
  165.     goToZ(new_z_place)
  166. end
  167. function feedWheat()
  168.     local this_slot = 0
  169.     for i = 16, 3, -1 do
  170.             local p = turtle.getItemCount(i)
  171.             if p ~= 0 then
  172.                     this_slot = i
  173.                     turtle.select(i)
  174.                     break
  175.             end
  176.     end
  177.  
  178.     if this_slot == 0 then
  179.             turtle.select(1)
  180.             turtle.placeUp()
  181.             for q = 2, 16 do
  182.                     turtle.select(q)
  183.                     local toSuck = 64 - turtle.getItemCount(q)
  184.                     turtle.suckUp(toSuck)
  185.             end
  186.             turtle.select(2)
  187.             turtle.equipRight()
  188.             turtle.select(1)
  189.             if turtle.getItemCount(1) == 0 then
  190.                     turtle.digUp()
  191.                     turtle.select(2)
  192.                     turtle.equipRight()
  193.                     turtle.select(16)
  194.             else
  195.                     turtle.select(1)
  196.                     turtle.drop(64)
  197.                     turtle.digUp()
  198.                     turtle.select(2)
  199.                     turtle.equipRight()
  200.                     turtle.select(16)
  201.             end
  202.     end
  203.     for x = 1, 10 do
  204.             if feederUpgrade.feed() then
  205.                     print("feed something")
  206.             end
  207.     end
  208. end
  209.  
  210. function redNetFunction(bool)
  211.     if bool == true then
  212.             rednet.open("right")
  213.             rednet.broadcast((farm_positions.total_farms + 1) - farm_counter .. " enable")
  214.             rednet.close("right")
  215.     elseif bool == false then
  216.             rednet.open("right")
  217.             rednet.broadcast((farm_positions.total_farms + 1) - farm_counter .. " disable")
  218.             rednet.close("right")
  219.     end
  220. end
  221.  
  222. function goToY(place)
  223.     if this_turtle.pos.y ~= place.y then
  224.             if this_turtle.pos.y < place.y then
  225.                     while this_turtle.dir ~= "S" do
  226.                             this_turtle:left()
  227.                     end
  228.             elseif this_turtle.pos.y > place.y then
  229.                     while this_turtle.dir ~= "N" do
  230.                             this_turtle:right()
  231.                     end
  232.             end
  233.             while this_turtle.pos.y ~= place.y do
  234.                     -- print(this_turtle.pos)
  235.                     this_turtle:forward()
  236.             end
  237.     end
  238. end
  239. function goToX(place)
  240.     if this_turtle.pos.x ~= place.x then
  241.             if this_turtle.pos.x > place.x then
  242.                     while this_turtle.dir ~= "W" do
  243.                             this_turtle:left()
  244.                     end
  245.             elseif this_turtle.pos.x < place.x then
  246.                     while this_turtle.dir ~= "E" do
  247.                             this_turtle:right()
  248.                     end
  249.             end
  250.             while this_turtle.pos.x ~= place.x do
  251.                     this_turtle:forward()
  252.             end
  253.     end
  254. end
  255. function goToZ(place)
  256.     if this_turtle.pos.z ~= place.z then
  257.             if this_turtle.pos.z > place.z then
  258.                     while this_turtle.pos.z ~= place.z do
  259.                             this_turtle:down()
  260.                     end
  261.             elseif this_turtle.pos.z < place.z then
  262.                     while this_turtle.pos.z ~= place.z do
  263.                             this_turtle:up()
  264.                     end
  265.             end
  266.     end
  267. end
  268.  
  269. function goToSector(sector)
  270.     local goToPoint = vector.new(0, 0, 0)
  271.     if farm_positions.sectors["sector" .. sector][2].y ~= farm_positions.sectors["sector" .. sector][1].y then
  272.             goToPoint.y =
  273.                     math.floor(
  274.                     ((farm_positions.sectors["sector" .. sector][2].y + farm_positions.sectors["sector" .. sector][1].y) / 2)
  275.             )
  276.             goToPoint.x = farm_positions.sectors["sector" .. sector][2].x
  277.     elseif farm_positions.sectors["sector" .. sector][2].x ~= farm_positions.sectors["sector" .. sector][1].x then
  278.             goToPoint.x =
  279.                     math.floor(
  280.                     ((farm_positions.sectors["sector" .. sector][2].x + farm_positions.sectors["sector" .. sector][1].x) / 2)
  281.             )
  282.             goToPoint.y = farm_positions.sectors["sector" .. sector][2].y
  283.     end
  284.     goToY(goToPoint)
  285.     goToX(goToPoint)
  286. end
  287.  
  288. function goUpSector(height)
  289.     local z_height = tonumber(height)
  290.     while this_turtle.pos.z < farm_positions.z_heights[z_height] do
  291.             this_turtle:up()
  292.     end
  293.     while this_turtle.pos.z > farm_positions.z_heights[z_height] do
  294.             this_turtle:down()
  295.     end
  296.     while this_turtle.dir ~= "N" do
  297.             this_turtle:left()
  298.     end
  299.     local newX = farm_positions.home.x
  300.     local newY = farm_positions.home.y - 2
  301.     local newZ = farm_positions.home.z
  302.     local plus_y_vector = vector.new(newX, newY, newZ)
  303.     goToY(plus_y_vector)
  304. end
  305. function goBackFromCenterpoint()
  306.     local newX = farm_positions.home.x
  307.     local newY = farm_positions.home.y - 2
  308.     local newZ = farm_positions.home.z
  309.     local minus_y_vector = vector.new(newX, newY, newZ)
  310.     goToY(minus_y_vector)
  311.     goToX(farm_positions.home)
  312.     while this_turtle.dir ~= "N" do
  313.             this_turtle:left()
  314.     end
  315.     goToY(farm_positions.home)
  316. end
  317. function checkPosition()
  318.     this_turtle:get_gps()
  319.     if
  320.             this_turtle.pos.x == farm_positions.home.x and this_turtle.pos.y == farm_positions.home.y and
  321.                     this_turtle.pos.z == farm_positions.home.z
  322.      then
  323.             return "home"
  324.     elseif this_turtle.pos.x == farm_positions.home.x and this_turtle.pos.y == farm_positions.home.y then
  325.             return "shaft"
  326.     elseif
  327.             this_turtle.pos.x == farm_positions.centerpoint.x and
  328.                     this_turtle.pos.y == farm_positions.centerpoint.y
  329.      then
  330.             for k, v in pairs(farm_positions.z_heights) do
  331.                     if this_turtle.pos.z == v then
  332.                             return "centerpoint"
  333.                     end
  334.             end
  335.     else
  336.             for k, v in pairs(farm_positions.z_heights) do
  337.                     if this_turtle.pos.z == v then
  338.                             for i = 1, 4 do
  339.                                     local B =
  340.                                             vector.new(
  341.                                             farm_positions.sectors["sector" .. i][1].x,
  342.                                             farm_positions.sectors["sector" .. i][1].y,
  343.                                             farm_positions.sectors["sector" .. i][1].z
  344.                                     )
  345.                                     local A =
  346.                                             vector.new(
  347.                                             farm_positions.sectors["sector" .. i][2].x,
  348.                                             farm_positions.sectors["sector" .. i][2].y,
  349.                                             farm_positions.sectors["sector" .. i][2].z
  350.                                     )
  351.                                     local C = vector.new(this_turtle.pos.x, this_turtle.pos.y, this_turtle.pos.z)
  352.                                     if
  353.                                             (tonumber(C.x) >= tonumber(A.x) and tonumber(C.x) <= tonumber(B.x) and
  354.                                                     tonumber(C.y) >= tonumber(A.y) and
  355.                                                     tonumber(C.y) <= tonumber(B.y))
  356.                                      then
  357.                                             return "sector" .. i
  358.                                     end
  359.                             end
  360.                     end
  361.             end
  362.     end
  363. end
  364. -- Util functions
  365. function clearScreen()
  366.     term.clear()
  367.     term.setCursorPos(1, 1)
  368. end
  369. function waitInput()
  370.     local wait_input = false
  371.     while (wait_input == false) do
  372.             local event, param = os.pullEvent()
  373.             if event == "key" and param == 28 then -- key 28 is the 'enter' key
  374.                     wait_input = true
  375.                     return true
  376.             elseif event == "key" and param == 14 then
  377.                     return false
  378.             end
  379.             sleep(0.2)
  380.     end
  381. end
  382. function specialSleepFunction(time)
  383.     for i = 1, time, -1 do
  384.             clearScreen()
  385.             print("Timing out " .. i .. " seconds")
  386.             sleep(1)
  387.     end
  388. end
  389.  
  390. -- Init and start function
  391. function start()
  392.     if fs.exists("cowfarm_folder") then
  393.             local h = fs.open("cowfarm_folder/firstinput", "r")
  394.             data = h.readAll()
  395.             h.close()
  396.             farm_positions = textutils.unserialize(data)
  397.             cowFarm()
  398.     else
  399.             init()
  400.     end
  401. end
  402. function init()
  403.     this_facility = facilityApi.facility:new("CowFarm")
  404.     if this_facility:is_registered() == false then
  405.             error("Facility doesn't exists, make before running this script")
  406.     else
  407.             slave_turtle = serverApi.slave:new("cowFarmSlave", "CowFarm")
  408.             if this_computer:is_registered() == false then
  409.                     if this_computer:register() == true then
  410.                             print("Slave has been registered")
  411.                             this_turtle = extraFuncsApi.extraFuncs:new(vector.new(0, 0, 0), "N", true)
  412.                             farm_poistions.home = this_turtle:get_gps()
  413.                             farm_positions.home_dir = this_turtle:get_dir()
  414.                             print("Init function okay, now going to set positions")
  415.                             sleep(1)
  416.                             setPositions()
  417.                     else
  418.                             error("Couldn't register slave")
  419.                     end
  420.             else
  421.                     error("Turtle seems to already be registered?")
  422.             end
  423.     end
  424. end
  425.  
  426. -- Set positions function
  427. function savePositions()
  428.     for i = 1, farm_positions.total_farms do
  429.             farm_positions.z_heights[i] = farm_positions.home.z + (farm_positions.farm_height * i)
  430.     end
  431.     local h = fs.open("cowfarm_folder/firstinput", "w")
  432.     h.write(textutils.serialize(farm_positions))
  433.     h.close()
  434. end
  435. function editSector(i)
  436.     clearScreen()
  437.     local newSector = {
  438.             point1 = vector.new(0, 0, 0),
  439.             point2 = vector.new(0, 0, 0)
  440.     }
  441.     print("Old values - Sector " .. i)
  442.     print(farm_positions.sectors["sector" .. i][1])
  443.     print(farm_positions.sectors["sector" .. i][2])
  444.     print("")
  445.     print("First point =")
  446.     newSector.point1.x, newSector.point1.y, newSector.point1.z = read():match("([^,]+),([^,]+),([^,]+)")
  447.     print(" ")
  448.     print("Second point - Sector " .. i)
  449.     newSector.point2.x, newSector.point2.y, newSector.point2.z = read():match("([^,]+),([^,]+),([^,]+)")
  450.     clearScreen()
  451.     print("Old sector values - Sector " .. i)
  452.     print(farm_positions.sectors["sector" .. i][1])
  453.     print(farm_positions.sectors["sector" .. i][2])
  454.     print("")
  455.     print("New sector values - Sector " .. i)
  456.     print(newSector.point1)
  457.     print(newSector.point2)
  458.     print("")
  459.     print("Enter to save or backspace to restart")
  460.     if waitInput() == true then
  461.             clearScreen()
  462.     else
  463.             editSector(i)
  464.     end
  465. end
  466. function editFarmPosition(key, value)
  467.     if type(value) ~= "string" then
  468.             local newPoint = vector.new(0, 0, 0)
  469.             newPoint.x, newPoint.y, newPoint.z = value:match("([^,]+),([^,]+),([^,]+)")
  470.             clearScreen()
  471.             print("Old " .. key)
  472.             print(farm_positions[key])
  473.             print("New  " .. key)
  474.             print(newPoint)
  475.             print("")
  476.             print("Press enter to save or return to reset")
  477.             sleep(0.5)
  478.             if waitInput() == true then
  479.                     farm_positions[key] = newPoint
  480.             else
  481.                     editFarmPosition(key, read())
  482.             end
  483.     else
  484.             clearScreen()
  485.             print("Old  " .. key)
  486.             print(farm_positions[key])
  487.             print("New " .. key)
  488.             print(value)
  489.             print("")
  490.             print("Press enter to save or return to reset")
  491.             sleep(0.5)
  492.             if waitInput() == true then
  493.                     farm_positions[key] = value
  494.             else
  495.                     editFarmPosition(key, read())
  496.             end
  497.     end
  498. end
  499. function setPositions()
  500.     clearScreen()
  501.     for i = 1, 4 do
  502.             clearScreen()
  503.             print("Sector " .. i)
  504.             print(farm_positions.sectors["sector" .. i][1])
  505.             print(farm_positions.sectors["sector" .. i][2])
  506.             print("")
  507.             print("Press enter to keep this value or backspace to edit")
  508.             if waitInput() == false then
  509.                     editSector(i)
  510.             end
  511.     end
  512.     clearScreen()
  513.     print("Home position")
  514.     print(farm_positions.home)
  515.     print(" ")
  516.     print("Press enter to keep this value or backspace to edit")
  517.     if waitInput() == false then
  518.             print("Enter new value")
  519.             editFarmPosition("home", read())
  520.     end
  521.     clearScreen()
  522.     print("Center position")
  523.     print(farm_positions.centerpoint)
  524.     print(" ")
  525.     print("Press enter to keep this value or backspace to edit")
  526.     if waitInput() == false then
  527.             print("Enter new value")
  528.             editFarmPosition("centerpoint", read())
  529.     end
  530.     clearScreen()
  531.     print("Home direction")
  532.     print(farm_positions.home_dir)
  533.     print(" ")
  534.     print("Press enter to keep this value or backspace to edit")
  535.     if waitInput() == false then
  536.             print("Enter new value")
  537.             editFarmPosition("home_dir", read())
  538.     end
  539.     clearScreen()
  540.     print("Farms height")
  541.     print(farm_positions.farm_height)
  542.     print(" ")
  543.     print("Press enter to keep this value or backspace to edit")
  544.     if waitInput() == false then
  545.             print("Enter new value")
  546.             editFarmPosition("farm_height", read())
  547.     end
  548.     clearScreen()
  549.     print("Total farms")
  550.     print(farm_positions.total_farms)
  551.     print(" ")
  552.     print("Press enter to keep this value or backspace to edit")
  553.     if waitInput() == false then
  554.             print("Enter new value")
  555.             editFarmPosition("total_farms", read())
  556.     end
  557.     savePositions()
  558. end
  559.  
  560. this_turtle = extraFuncsApi.extraFuncs:new(vector.new(0, 0, 0), "N", true)
  561.  
  562. setPositions()
  563. sleep(1)
  564. clearScreen()
  565. this_turtle:get_dir()
  566. cowFarm()
Add Comment
Please, Sign In to add comment