Advertisement
Shurhaian

SkunkWorks Logistics Seed Breeder - Main

May 19th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.06 KB | None | 0 0
  1. --[[ SeedBreeder.lua
  2.    Robot's running instructions
  3.    For use in SkunkWorks AgriCraft Seed Breeder
  4.    Version 1.3
  5. ]]
  6.  
  7. --[[ SETUP
  8. Place three blocks of appropriate soil in a line. Hoe if necessary.
  9. On the blocks next to and above the soil - next to where the crop sticks will
  10. go - place a line of three seed analyzers.
  11.  
  12. The following directions will be written from the perspective of someone
  13. standing on the central soil block, facing the analyzers:
  14.  *On top of the left-hand analyzer, place a container of crop sticks. This
  15.   should either contain a very large number or be automatically restocked in
  16.   some manner.
  17.  *On top of the right-hand analyzer, place a container for surplus, non-maxed
  18.   seeds. These should be disposed of before the container fills up, e.g. an XU
  19.   trash can, TE nullifier, Garden Stuff compost bin, dropper over lava...
  20.  *Two blocks FORWARD and 1 block off the ground, place a container for crops
  21.   obtained during the breeding process. This too should be kept from filling.
  22.  *Two blocks to the LEFT and one block off the ground, place a container
  23.   holding the seeds to be bred up.
  24.  *Two blocks to the RIGHT and one block off the ground, place a container for
  25.   fully-bred seeds. As with the others, keep it from overfilling.
  26.  
  27. The robot requires an inventory upgrade, inventory controller upgrade, and
  28. wireless modem, therefore it must be in at least a tier 2 case.
  29.  
  30. A computer should be attached to the analyzers which is equipped with a
  31. wireless network card or attached
  32.  
  33. Place the robot 1 block off the ground, in the space just used as a reference.
  34. It should face the analyzers, with empty space to its front, left, and right,
  35. and have an active charger positioned behind or above it. It will spend most
  36. of its time in this spot. If playing with weeds enabled, place a handrake
  37. in the 16th slot of the robot's inventory - the lower right slot if only 1
  38. inventory upgrade is installed.
  39. Crop sticks can be placed in slot 1 of the inventory, however take care not
  40. to overfill this slot, as it must have room to collect the sticks from any
  41. harvested crops in the breeding process. Other slots should be empty.
  42.  
  43. Install Lua boot environments, then install this file on the robot and
  44. "FarmRemote.lua" on the analyzer computer by whatever means is convenient.
  45. I have attempted to supply default configuration options that will work, but
  46. if you are using several within transmission range of each other, or if these
  47. values would interfere with other wireless equipment, edit the variables in
  48. the CONFIGURATION section appropriately.
  49. No two installations should use the same CommandPort if they could possibly
  50. detect one another.
  51. The CommandPort and SignalStr values should match on both ends.
  52. Pay careful attention to the "AnaSide" variable on the remote program.
  53. It must be set to match your own configuration.
  54.  
  55. Once all configuration is complete, run "SeedRemote.lua" on the
  56. analyzer computer, then stand clear of the robot and run this file.
  57. ]]
  58.  
  59. -- CONFIGURATION --
  60.  
  61. -- Wireless settings - these should match those on the host computer
  62. CommandPort=9090 -- Network port used to send/receive commands
  63. local SignalStr=100 -- Wireless signal strength
  64.  
  65. -- END CONFIGURATION --
  66.  
  67. -- FUNCTION DEFINITIONS --
  68. function netSend (msg)
  69.    wNet.broadcast(CommandPort, msg)
  70. end
  71.  
  72. function netListen (timeout)
  73.    if timeout then -- Was a timeout value passed? Used for handshake
  74.       local _, _, _, _, _, msg = event.pull(timeout, "modem")
  75.       return msg
  76.    else -- Unlimited time, for main sequence
  77.       local _, _, _, _, _, msg = event.pull("modem")
  78.       return msg
  79.    end
  80. end
  81.    
  82.  
  83. function harvestMid() -- Dig up whatever's in the middle plot, below
  84.    robot.select(1) -- Ensure cropsticks get stacked
  85.    robot.swingDown()
  86. end
  87.  
  88. function harvestRight() -- Move to and harvest the right-hand plot
  89.    robot.turnRight()
  90.    robot.forward()
  91.    robot.select(1)
  92.    robot.swingDown()
  93.    robot.back()
  94.    robot.turnLeft()
  95. end
  96.  
  97. function harvestLeft() -- As above but for the left plot
  98.    robot.turnLeft()
  99.    robot.forward()
  100.    robot.select(1)
  101.    robot.swingDown()
  102.    robot.back()
  103.    robot.turnRight()
  104. end
  105.  
  106. --checkCropSticks should only be executed when the robot is already moved
  107. --forward, next to the crop-sticks input container
  108. function checkCropSticks() --Check count in the "cropstick" slot
  109.    robot.select(1)
  110.    if robot.count(1) < 20 then -- Need to grab more crop sticks
  111.       print "Low on cropsticks, replenishing..."
  112.       robot.turnLeft()
  113.       robot.suck(20) -- Keep a working range of 20-40, avoid overfilling
  114.       robot.turnRight()
  115.    end
  116. end
  117.  
  118. function stackName ( slot) -- Shorthand for lowercase stack name in a slot
  119.    if robot.count(slot) > 0 then
  120.       return string.lower(iCont.getStackInInternalSlot(slot).name)
  121.    else return nil end
  122. end
  123.  
  124. function moveSeed() -- Searches pickups for a seed, moves it to slot 13
  125.    local seedFound = false
  126.    for i=2, 12 do
  127.       if string.find(stackName(i),"seed") then
  128.          robot.select(i)
  129.          robot.transferTo(13)
  130.          seedFound=true
  131.          break
  132.       end
  133.    end
  134.    if not seedFound then print "ERROR: No seed found!" end
  135. end
  136.  
  137. function deliverSeed() -- Deliver the current "active seed" for analysis
  138.    robot.forward()
  139.    robot.select(13)
  140.    robot.dropDown()
  141. end   -- Robot will remain in place for the analysis
  142.  
  143.  
  144. function dumpStuff() -- Clear the misc inventory space of any dropped items
  145.    robot.forward()
  146.    for i=2, 12 do
  147.       if robot.count(i) > 0 then -- If the slot is not empty...
  148.          robot.select(i)
  149.          if string.find(stackName(i),"seed") then -- Surplus seed
  150.             robot.turnRight() -- Face the "surplus seeds" box
  151.             robot.drop()
  152.             robot.turnLeft()
  153.          else -- Harvested crops
  154.             robot.drop()
  155.          end
  156.       end
  157.    end
  158.    checkCropSticks() -- Since we're here...
  159.    robot.back()
  160. end
  161.  
  162. function retrieveSeed() -- Pick up analyzed seed
  163.    checkCropSticks() -- Stock up while we're moved forward if necessary
  164.    robot.select(13) -- Ensure it goes to the seed slot
  165.    robot.suckDown()
  166.    robot.back()
  167. end
  168.  
  169. function plantLeft() -- Plant the current seed in the left plot
  170.    robot.turnLeft()
  171.    robot.forward()
  172.    robot.select(1) -- Crop sticks
  173.    iCont.equip()
  174.    robot.useDown()
  175.    iCont.equip() -- Put the crop sticks back in inventory
  176.    robot.useDown() -- A single use makes cross crops, grab the spare stick
  177.    robot.select(13) -- Active seed
  178.    iCont.equip()
  179.    robot.useDown() -- Plant the seed, should only have one
  180.    robot.back()
  181.    robot.turnRight()
  182. end
  183.  
  184. function plantRight() -- As above but for the right
  185.    robot.turnRight()
  186.    robot.forward()
  187.    robot.select(1) -- Crop sticks
  188.    iCont.equip()
  189.    robot.useDown()
  190.    iCont.equip() -- Put the crop sticks back in inventory
  191.    robot.useDown() -- A single use makes cross crops, grab the spare stick
  192.    robot.select(13) -- Active seed
  193.    iCont.equip()
  194.    robot.useDown() -- Plant the seed, should only have one
  195.    robot.back()
  196.    robot.turnLeft()
  197. end
  198.  
  199. function crossMid() -- Place cross crops
  200.    robot.select(1) -- Crop sticks
  201.    iCont.equip()
  202.    robot.useDown() -- A single use places cross crops
  203.    iCont.equip() -- Put the sticks back in inventory
  204. end
  205.  
  206. function waitForFix() -- Wait for the user to change something
  207.    os.sleep(1) -- Buffer period to make sure this doesn't immediately go off
  208.    print("Press a key to retry...")
  209.    event.pull("key") -- Wait for a keyboard event
  210. end
  211.  
  212. --EXPERIMENTAL: Program was not developed with weeds active!
  213. --Only the middle plot should ever have a chance to grow weeds
  214. function removeWeeds() -- Rake weeds until they're gone
  215.    local hasWeeds = true -- Control variable
  216.    robot.select(16) -- Handrake
  217.    iCont.equip()
  218.    repeat -- Weed multiple times if necessary
  219.       robot.useDown()
  220.       netSend("CheckMidWeeds")
  221.       if netListen() == "HasNoWeeds" then hasWeeds=false end
  222.    until not hasWeeds
  223.    iCont.equip() -- Put the handrake away
  224. end
  225.  
  226. function getNextSeed() -- Fetch the next seed for breeding up
  227.    local gotSeed=false
  228.    robot.turnLeft()
  229.    robot.forward()
  230.    robot.select(13)
  231.    if robot.suck() then gotSeed=true end -- Successful
  232.    robot.back()
  233.    robot.turnRight()
  234.    if not gotSeed then
  235.       print "No seeds found! Insert seeds for breeding."
  236.       waitForFix()
  237.    end
  238.    return gotSeed
  239. end
  240.  
  241. function doneWithSeed() -- Deliver the active seed as finished
  242.    robot.turnRight()
  243.    robot.forward()
  244.    robot.select(13)
  245.    robot.drop()
  246.    robot.back()
  247.    robot.turnLeft()
  248. end
  249.  
  250. function waitForMid() -- Wait for the middle crop to sprout, clear any weeds
  251.    local midStat="Empty"
  252.    repeat
  253.       midStat=netListen()
  254.       if midStat=="HasWeeds" then
  255.          print("WARNING! Weeds detected! Raking...")
  256.          removeWeeds()
  257.          local cropSticks=robot.count(1)
  258.          print("Weeds cleared. Current cropsticks: "..cropSticks)
  259.          if cropSticks < 20 then
  260.             print "Fetching more cropsticks..."
  261.             robot.forward() -- Special case since the robot won't
  262.             robot.turnLeft() -- already be moved forward
  263.             robot.select(1)
  264.             robot.suck(20)
  265.             robot.turnRight()
  266.             robot.back()
  267.             print "Cropsticks replenished."
  268.          end
  269.          print "Replacing cross crop."
  270.          robot.select(1)
  271.          iCont.equip()
  272.          robot.useDown()
  273.          iCont.equip()
  274.          print "Continuing to wait..."
  275.       end
  276.    until midStat=="HasPlant"
  277. end
  278.  
  279.    
  280.  
  281. -- END FUNCTION DEFINITIONS --
  282.  
  283. local version="1.3"
  284. event = require("event")
  285. component = require("component")
  286. robot = require("robot")
  287.  
  288. wNet = component.proxy(component.modem.address)
  289. iCont = component.proxy(component.inventory_controller.address)
  290.  
  291. print("---------------------------------------------")
  292. print("SkunkWorks Logistics - AgriCraft Seed Breeder")
  293. print("Robot/main control program")
  294. print("Version "..version)
  295. print("---------------------------------------------")
  296.  
  297. wNet.setStrength(SignalStr)
  298. wNet.close()
  299. wNet.open(CommandPort)
  300. print("Communicating on port "..CommandPort.." with power "..SignalStr)
  301.  
  302. print("Attempting handshake with analyzer control station.")
  303.  
  304. handshake=false -- Control to break out of the handshake loop
  305. while true do
  306.    for i=1, 10 do
  307.       netSend("Handshake")
  308.       local thisMsg=netListen(10) or "<no response>"
  309.       print("Try "..i.. " of 10: "..thisMsg)
  310.       if thisMsg=="Confirm" then
  311.          print "Success!"
  312.          handshake=true
  313.          break
  314.       end
  315.    end
  316.    if handshake then break end
  317.    print("Handshake failed! Is the computer running?")
  318.    waitForFix()
  319. end
  320. handshake=nil
  321.  
  322. while true do -- Main sequence starts here
  323.    repeat
  324.       print "Fetching next seed."
  325.    until getNextSeed()
  326.    deliverSeed()
  327.    print "Analyzing new seed..."
  328.    netSend("Analyze")
  329.    seedSum=netListen()
  330.    if seedSum==30 then -- Seed already maxed?
  331.       print("Seed already at max stats!")
  332.       retrieveSeed()
  333.       doneWithSeed()
  334.    else -- Begin main breeding cycle
  335.       print("Seed stat total: "..seedSum)
  336.       print("Planting in LEFT patch.")
  337.       retrieveSeed()
  338.       plantLeft()
  339.       leftSum=seedSum
  340.       netSend("GrowLeft")
  341.       print("Waiting for growth...")
  342.       repeat until netListen() == "LeftGrown" -- Wait for plant to grow
  343.       print("Left patch grown!")
  344.       print("Placing cross crops.")
  345.       crossMid() -- Place cross crops to get duplicate seed
  346.       netSend("WatchMid")
  347.       print("Waiting for sprout...")
  348.       waitForMid()
  349.       print "Sprout detected! Collecting."
  350.       harvestMid()
  351.       moveSeed()
  352.       deliverSeed()
  353.       netSend("Analyze")
  354.       print "Analyzing seed..." -- This should be a clone of a non-max seed
  355.       seedSum = netListen() -- But let's check it again just in case
  356.       retrieveSeed()
  357.       print "Planting in RIGHT plot."
  358.       plantRight()
  359.       rightSum=seedSum
  360.       print "Waiting for growth..."
  361.       netSend("GrowRight")
  362.       repeat until netListen() == "RightGrown"
  363.       print "Right patch grown!"
  364.       nextPlot = "LEFT" -- Which side will we be replacing next? Alternate
  365.       while true do -- Main breeding routine starts here
  366.          print "Placing cross crops."
  367.          crossMid()
  368.          netSend("WatchMid")
  369.          waitForMid()
  370.          print "Sprout detected! Collecting."
  371.          harvestMid()
  372.          moveSeed()
  373.          deliverSeed()
  374.          netSend("Analyze")
  375.          print "Analyzing seed..."
  376.          local sumNew = netListen()
  377.          print ("Total seed values: "..sumNew)
  378.          if sumNew == 30 then -- Seed's done!
  379.             print "Seed fully bred! Delivering and cleaning up..."
  380.             retrieveSeed()
  381.             doneWithSeed()
  382.             harvestLeft() -- Clear the soil
  383.             harvestRight()
  384.             dumpStuff() -- Drop off the spare seeds & harvested stuff
  385.             break
  386.          elseif nextPlot=="LEFT" then
  387.             print ("Total in LEFT plot: "..leftSum)
  388.             if sumNew > leftSum then -- Seed has improved
  389.                print "Replacing LEFT seed..."
  390.                retrieveSeed()
  391.                harvestLeft() -- Selects 1. The new seed is still in 13
  392.                plantLeft()
  393.                leftSum = sumNew
  394.                print "Clearing inventory..."
  395.                dumpStuff() -- Drop off the old seed & harvested stuff
  396.                nextPlot="RIGHT" -- Switch sides for next time
  397.                netSend("GrowLeft")
  398.                print "Waiting for new seed to grow..."
  399.                repeat until netListen() == "LeftGrown"
  400.             else   -- Seed hasn't improved, dump it
  401.                print "New seed is not an improvement. Discarding..."
  402.                robot.suckDown()
  403.                robot.turnRight()
  404.                robot.drop()
  405.                robot.turnLeft()
  406.                checkCropSticks()
  407.                robot.back()
  408.             end -- comparing to left seed
  409.          elseif nextPlot=="RIGHT" then
  410.             print ("Total in RIGHT plot: "..rightSum)
  411.             if sumNew > rightSum then -- Seed has improved
  412.                print "Replacing RIGHT seed..."
  413.                retrieveSeed()
  414.                harvestRight() -- Selects 1. The new seed is still in 13
  415.                plantRight()
  416.                rightSum = sumNew
  417.                print "Clearing inventory..."
  418.                dumpStuff() -- Drop off the old seed & harvested stuff
  419.                nextPlot="LEFT" -- Switch sides for next time
  420.                netSend("GrowRight")
  421.                print "Waiting for new seed to grow..."
  422.                repeat until netListen() == "RightGrown"
  423.             else   -- Seed hasn't improved, dump it
  424.                print "New seed is not an improvement. Discarding..."
  425.                robot.suckDown()
  426.                robot.turnRight()
  427.                robot.drop()
  428.                robot.turnLeft()
  429.                checkCropSticks()
  430.                robot.back()
  431.             end -- comparing to right seed
  432.          else -- Shouldn't happen
  433.             print "ERROR: nextPlot was unassigned! Picking LEFT"
  434.             print ("Total in LEFT plot: "..leftSum)
  435.             if sumNew > leftSum then -- Seed has improved
  436.                print "Replacing LEFT seed..."
  437.                retrieveSeed()
  438.                harvestLeft() -- Selects 1. The new seed is still in 13
  439.                plantLeft()
  440.                leftSum = sumNew
  441.                print "Clearing inventory..."
  442.                dumpStuff() -- Drop off the old seed & harvested stuff
  443.                nextPlot="RIGHT" -- Pick a side for next time
  444.                netSend("GrowLeft")
  445.                print "Waiting for new seed to grow..."
  446.                repeat until netListen() == "LeftGrown"
  447.             else   -- Seed hasn't improved, dump it
  448.                print "New seed is not an improvement. Discarding..."
  449.                robot.suckDown()
  450.                robot.turnRight()
  451.                robot.drop()
  452.                robot.turnLeft()
  453.                checkCropSticks()
  454.                robot.back()
  455.             end -- comparing to left seed
  456.          end -- End inspecting new seed
  457.       end -- main breeding sequence
  458.    end -- processing new seed
  459. end -- main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement