Advertisement
RedStoneMan77

sbData

Mar 18th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.19 KB | None | 0 0
  1. ------------------SOME VARIABLES--------------------
  2. t = turtle
  3. dataF = "sbdata/data"
  4. redstoneSleep = 1.0
  5. redstone.setOutput("bottom",false)
  6.  
  7. seed = {g = 0, y = 0, s = 0, name=""}
  8.  
  9. slot = {fuel = 1, sticks1 = 2, sticks2 = 3, seed = 4, extra = 5}
  10. pos={x = 1, z = 0, seed={} , actv={}, f = 2} --F(Face) = 2(North)
  11. pos.seed[0] = {x = -1, z = 0}
  12. pos.actv[0] = {x = -1, z = -1}
  13.  
  14. pos.seed[1] = {x = 0, z = 1}
  15. pos.actv[1] = {x = 1, z = 1}
  16.  
  17. pos.seed[2] = {x = 0, z = 0}
  18. pos.actv[2] = {x = 0, z = -1}
  19.  
  20. pos.anlzer = {x=1,z=0}
  21. pos.chest = {x=-1,z=1}
  22. pos.bin = {x=1,z=-1}
  23.  
  24. seedSlot = 4
  25.  
  26. data={}
  27.  
  28. -----------------LANG VARIABLES---------------------
  29. lang_noFuel = "Please insert a valid fuel in slot "..slot.fuel.."!"
  30. lang_noSticks = "Please insert Crop Sticks in slot "..slot.sticks1.." or "..slot.sticks2.."!"
  31. --lang_noRake = "Please insert a Hand Rake in slot "..slot.rake.."!"
  32. lang_noSeed = "Please insert a valid seeds in slot "..slot.seed.."!"
  33. lang_manySeeds = "There are too many seeds! Use 1 or 2 only."
  34. lang_maxedOut = "This seed is now 10/10/10!"
  35. lang_line = "---------------------------------------"
  36. lang_placingStick = "Placing sticks"
  37. lang_placingSeedsInAcv = "Placing seeds in Autonomous Activator"
  38. lang_placingSticksInAcv = "Placing sticks in Autonomous Activator"
  39.  
  40.  
  41. CCSA = "Computer Controlled Seed Analyzer"
  42. TRSHC = "Trash Can"
  43. CRPSTCK = "Crop Sticks without seeds"
  44. ----------------------------------------------------
  45. ----------------------------------------------------
  46.  
  47. function loadData()
  48.    if fs.exists(dataF) then
  49.     local file = fs.open(dataF, "r")
  50.     local data = file.readAll()
  51.     file.close()
  52.     pos.x     = textutils.unserialize(data).x
  53.     pos.z     = textutils.unserialize(data).z
  54.     pos.f     = textutils.unserialize(data).f
  55.     seed.g    = textutils.unserialize(data).g
  56.     seed.y    = textutils.unserialize(data).y
  57.     seed.s    = textutils.unserialize(data).s
  58.     seed.name = textutils.unserialize(data).name
  59.     return true
  60.    else
  61.     -- data = {x = 1, z = 0, name = "", g = 0, y = 0, s = 0,}
  62.     -- fs.makeDir("sbdata")
  63.     -- local file = fs.open(dataF,"w")
  64.     -- file.write(textutils.serialize(data))
  65.     -- file.close()
  66.     return false
  67.    end
  68. end
  69. function saveData()
  70.    local file = fs.open(dataF, "w")
  71.    local data = {x = pos.x, z = pos.z, name = seed.name, g = seed.g, y = seed.y, s = seed.s, f = pos.f}
  72.    file.write(textutils.serialize(data))
  73.    file.close()
  74.    sleep(0.1)
  75. end
  76. function clearData()
  77.     print("Clear data")
  78.     fs.delete(dataF)
  79. end
  80.  
  81. ---------------BASIC TURTLE COMMANDS----------------
  82. function resetScreen()
  83.    term.clear()
  84.    term.setCursorPos(1,1)
  85. end
  86.  
  87. function putInAnlzer()
  88.   if checkCount(slot.seed,1) then
  89.     lastSl = select(slot.seed)
  90.   elseif checkCount(slot.sticks2,1) then
  91.     lastSl = select(slot.sticks2)
  92.   end
  93.   succes = t.dropDown()
  94.   select(lastSl)
  95.   return succes
  96. end
  97. function wrapAnlzer()
  98.   return peripheral.wrap("bottom")
  99. end
  100. function wrapModem()
  101.   return peripheral.wrap("right")
  102. end
  103. function takeFromAnlzer()
  104.   lastSl = select(slot.seed)
  105.   succes = suckDown(64)
  106.   select(lastSl)
  107.   return succes
  108. end
  109. function isMaxedOut()
  110.   if seed.g == 10 and seed.y == 10 and seed.s == 10 then
  111.     return true
  112.   end
  113.   return false
  114. end
  115. function updateSeedMaxData(letEqualUpdate)
  116.   local anl = wrapAnlzer()
  117.   local modem = wrapModem()
  118.   local g, y, s = anl.getSpecimenStats()
  119.     if (seed.g+seed.s+seed.y)/3 < (g+y+s)/3 and not letEqualUpdate then
  120.       seed.g = g
  121.       seed.y = y
  122.       seed.s = s
  123.       modem.transmit(77, 78, "Seed is "..g.." / "..y.." / "..s)
  124.       return true
  125.     elseif (seed.g+seed.s+seed.y)/3 <= (g+y+s)/3 and letEqualUpdate then
  126.       seed.g = g
  127.       seed.y = y
  128.       seed.s = s
  129.       modem.transmit(77, 78, "Seed is "..g.." / "..y.." / "..s)
  130.       return true
  131.     end
  132.   return false
  133. end
  134. function analyze(letEqualUpdate)
  135.   move(pos.anlzer)
  136.   print("Analyzing")
  137.   if putInAnlzer() then
  138.     local anl = wrapAnlzer()
  139.     anl.analyze()
  140.     while not anl.isAnalyzed() do
  141.         os.sleep(0.2)
  142.     end
  143.     isUpdated = updateSeedMaxData(letEqualUpdate)
  144.     if isMaxedOut()then
  145.       takeFromAnlzer()
  146.       seedMaxedOut()
  147.     end
  148.     takeFromAnlzer()
  149.     return isUpdated
  150.   end
  151.   return false
  152. end
  153. function analyseSeed()
  154.   if analyze() then
  155.     if seedRepl > 1 then
  156.       seedRepl = 0
  157.     end
  158.     breakStick(pos.seed[seedRepl])
  159.     placeSeeds(pos.seed[seedRepl],pos.actv[seedRepl])
  160.     trashSeed()
  161.     storeYeld()
  162.     seedRepl = seedRepl + 1
  163.   else
  164.     trashSeed()
  165.   end
  166. end
  167. function redstoneOn()
  168.   return redstone.setOutput("bottom",true)
  169. end
  170. function redstoneOff()
  171.   return redstone.setOutput("bottom",false)
  172. end
  173.  
  174.  
  175. function placeSticks(tablePos)
  176.   move(tablePos)
  177.   print("Placing sticks")
  178.   if sticks() then
  179.       print(lang_placingStick)
  180.       lastSelected = select(slot.sticks1)
  181.       t.placeDown()
  182.       select(lastSelected)
  183.   end
  184. end
  185. function breakStick(tablePos)
  186.   move(tablePos)
  187.   print("Breaking sticks")
  188.   t.digDown()
  189. end
  190. function placeSticksInActv(tablePos,doubleSticks)
  191.   print("Placing sticks in right clicky machine")
  192.   move(tablePos)
  193.     if doubleSticks then
  194.       sticks()
  195.       dropDownFromSlot(slot.sticks1,1)
  196.     end
  197.     sticks()
  198.     dropDownFromSlot(slot.sticks1,1)
  199.     redstoneOn()
  200.     sleep(redstoneSleep)
  201.   redstoneOff()
  202. end
  203.  
  204. function dropSeedsInActv(tablePos)
  205.   move(tablePos)
  206.   print("Placing seeds in right clicky machine")
  207.   redstoneOn()
  208.     dropDownFromSlot(slot.seed,1)
  209.     sleep(redstoneSleep)
  210.   redstoneOff()
  211.   suckDownInSlot(12,64)
  212.   if checkCount(12,1) then
  213.     transferItem(12,slot.seed)
  214.     print("Weeds detected ")
  215.     return false
  216.   end
  217.   return true
  218. end
  219.  
  220. function placeSeeds(seedPosTbl,actvPosTbl)
  221.   placeSticks(seedPosTbl,false)
  222.   while not dropSeedsInActv(actvPosTbl) do
  223.     breakStick(seedPosTbl)
  224.     placeSticks(seedPosTbl,false)
  225.   end
  226. end
  227.  
  228. function try(f, catch_f)
  229.     local status, exception = pcall(f)
  230.     if not status then
  231.         catch_f(exception)
  232.     end
  233. end
  234.  
  235. function waitForSeedToGrow()
  236.   move(pos.anlzer)
  237.   print("Waiting for seeds to grow")
  238.   local anl = wrapAnlzer()
  239.   local modem = wrapModem()
  240.   local hasPlant = false
  241.   while not hasPlant do
  242.     try(function()
  243.         hasPlant = anl.hasPlant("WEST")
  244.     end, function()
  245.         placeSticksInActv(pos.actv[2],true)
  246.         move(pos.anlzer)
  247.     end)
  248.  
  249.     try(function()
  250.         if anl.hasWeeds("WEST") then
  251.             print("Removing weeds")
  252.             breakStick(pos.seed[2])
  253.             placeSticksInActv(pos.actv[2],true)
  254.             move(pos.anlzer)
  255.         end
  256.     end, function()
  257.         placeSticksInActv(pos.actv[2],true)
  258.         move(pos.anlzer)
  259.     end)
  260.    
  261.     sleep(1)
  262.     modem.transmit(77, 78, "The turtle is still running")
  263.     modem.transmit(77, 78, seed.name)
  264.     modem.transmit(77, 78, "Seed is "..seed.g.." / "..seed.y.." / "..seed.s)
  265.   end
  266.   breakStick(pos.seed[2])
  267.   return true
  268. end
  269.  
  270. function trashSeed()
  271.   move(pos.bin)
  272.   print("Trashing seeds")
  273.   --print(lang_)
  274.   if compareItemInSlot(seed.name,slot.extra) then
  275.     dropDownFromSlot(slot.extra,64)
  276.   elseif compareItemInSlot(seed.name,slot.sticks2) then
  277.     dropDownFromSlot(slot.sticks2,64)
  278.   else
  279.     dropDownFromSlot(slot.seed,64)
  280.   end
  281. end
  282. function trashItem(slot)
  283.   move(pos.bin)
  284.   print("Trashing item")
  285.   dropDownFromSlot(slot,64)
  286. end
  287. function storeYeld()
  288.   move(pos.chest)
  289.   print("Storing yeld")
  290.   if not compareItemInSlot(seed.name,slot.extra) then
  291.     dropDownFromSlot(slot.extra,64)
  292.   elseif not compareItemInSlot("AgriCraft:cropsItem",slot.sticks2) then
  293.     dropDownFromSlot(slot.sticks2,64)
  294.   else
  295.     dropDownFromSlot(slot.extra,64)
  296.   end
  297.     dropDownFromSlot(6,64)
  298.     dropDownFromSlot(7,64)
  299.     dropDownFromSlot(8,64)
  300. end
  301.  
  302. --------------INVENTORY CONTROLLLER-----------------
  303. function select(slot)
  304.   lastSl = t.getSelectedSlot()
  305.   t.select(slot)
  306.   return lastSl
  307. end
  308. function count(slot)
  309.     return t.getItemCount(slot)
  310. end
  311. function checkCount(slot,number)
  312.   if count(slot) >= number then
  313.     return true
  314.   end
  315.   return false
  316. end
  317. function dropDown(number)
  318.     return t.dropDown(number)
  319. end
  320. function dropDownFromSlot(slot,number)
  321.     lastSelected = select(slot)
  322.     if dropDown(number) then
  323.         select(lastSelected)
  324.         return true
  325.     end
  326.     select(lastSelected)
  327.     return false
  328. end
  329. function suckDown(number)
  330.     return t.suckDown(number)
  331. end
  332. function suckDownInSlot(slot,number)
  333.     lastSelected = select(slot)
  334.     if suckDown(number) then
  335.         t.select(lastSelected)
  336.         return true
  337.     end
  338.     select(lastSelected)
  339.     return false
  340. end
  341. -----------------------------------------------
  342. function transferItem(fromSlot,toSlot)
  343.   lastSl = select(fromSlot)
  344.   t.transferTo(toSlot,64)
  345.   select(lastSl)
  346. end
  347. function compareItemInSlot(item,slot)
  348.   local itemInfo = t.getItemDetail(slot)
  349.   if itemInfo ~= nil then
  350.     --print("Comparing: "..item.." AND: "..itemInfo.name)
  351.     if item == itemInfo.name then
  352.       return true
  353.     end
  354.   end
  355.   return false
  356. end
  357. function matchItemInSlot(item,slot)
  358.   itemInfo = turtle.getItemDetail(slot)
  359.   if itemInfo.name == item then
  360.    return true
  361.   end
  362.   return false
  363. end
  364. ------------------TURTLE CHECKS-----------------------
  365.  
  366. function fuel()
  367.    lastSelected =   t.getSelectedSlot()
  368.    if t.getFuelLevel() < 70 then
  369.         lastSelected = select(slot.fuel)
  370.        if t.refuel(slot.fuel) then
  371.         select(lastSelected)
  372.         return true
  373.        else
  374.         noFuel()
  375.         t.select(lastSelected)
  376.        end
  377.    end
  378.     return true
  379. end
  380.  
  381. function tidySticks()
  382.   if compareItemInSlot("AgriCraft:cropsItem",slot.sticks1) then
  383.     return true
  384.   else
  385.     if compareItemInSlot("AgriCraft:cropsItem",slot.sticks2) then
  386.       transferItem(slot.sticks2,slot.sticks1)
  387.       return true
  388.     end
  389.   end
  390.   return false
  391. end
  392. function sticks()
  393.   if tidySticks() then
  394.     return true
  395.   end
  396.   noSticks()
  397.   return true
  398. end
  399. function seeds()
  400.   local seedCount = count(slot.seed)
  401.   if seedCount > 2 then
  402.     tooManySeeds()
  403.   elseif seedCount == 2 or seedCount == 1 then
  404.     if analyze() then
  405.       local lastSelected = select(slot.seed)
  406.       local seedInfo = t.getItemDetail()
  407.       select(lastSelected)
  408.       seed.name = seedInfo.name
  409.       print("Seed set to: "..seed.name)
  410.       return seedCount
  411.     end
  412.   end
  413.   return 0
  414. end
  415. function rake()
  416.   if compareItemInSlot("AgriCraft:handRake",slot.rake) then
  417.     return true
  418.   end
  419.   noRake()
  420.   return true
  421. end
  422. -------------------MESSAGES---------------------
  423. function noFuel()
  424.     local modem = wrapModem()
  425.     while not t.refuel(slot.fuel) do
  426.       resetScreen()
  427.       print (lang_noFuel)
  428.       modem.transmit(77, 78, "Program stopped.")
  429.       sleep(1)
  430.     end
  431. end
  432. function noSticks()
  433.     local modem = wrapModem()
  434.     while not tidySticks() do
  435.       resetScreen()
  436.       print (lang_noSticks)
  437.       modem.transmit(77, 78, "Program stopped.")
  438.       sleep(1)
  439.     end
  440. end
  441. function noRake()
  442.   while not compareItemInSlot("AgriCraft:handRake",slot.rake) do
  443.     resetScreen()
  444.     print(lang_noRake)
  445.     sleep(1)
  446.   end
  447.   return true
  448. end
  449. function noSeeds()
  450.   local modem = wrapModem()
  451.   while not checkCount(slot.seed,1) do
  452.     resetScreen()
  453.     print(lang_noSeed)
  454.     modem.transmit(77, 78, "Program stopped.")
  455.     sleep(1)
  456.   end
  457.   if seeds() >= 1 then
  458.     resetScreen()
  459.     return count(slot.seed)
  460.   else
  461.     noSeeds()
  462.   end
  463. end
  464. function tooManySeeds()
  465.     while checkCount(slot.seed,3) do
  466.       resetScreen()
  467.       print (lang_manySeeds)
  468.       sleep(1)
  469.     end
  470.     return true
  471. end
  472.  
  473. function seedMaxedOut()
  474.   print("Seed is 10/10/10. Clearing crop sticks")
  475.   local modem = wrapModem()
  476.   modem.transmit(77, 78, "Seed is 10 / 10 / 10. Stopping program.")
  477.   breakStick(pos.seed[0])
  478.   if compareItemInSlot(seed.name,slot.sticks2) then
  479.     trashItem(slot.sticks2)
  480.   else
  481.     trashItem(slot.extra)
  482.   end
  483.   storeYeld()
  484.   breakStick(pos.seed[1])
  485.   if compareItemInSlot(seed.name,slot.sticks2) then
  486.     trashItem(slot.sticks2)
  487.   else
  488.     trashItem(slot.extra)
  489.   end
  490.   storeYeld()
  491.   placeSeeds(pos.seed[1],pos.actv[1])
  492.   placeSticksInActv(pos.actv[2],true)
  493.   placeSticks(pos.seed[0])
  494.   placeSticksInActv(pos.actv[0],false)
  495.   move(pos.anlzer)
  496.   clearData()
  497.   modem.transmit(77, 78, "Program stopped.")
  498.   error(lang_maxedOut)
  499. end
  500.  
  501.  -----------------TURTLE MOVEMENT----------------------
  502. function forward(n)
  503.   for i = 1,n do
  504.    fuel()
  505.    if t.forward() then
  506.       pos.z = pos.z - 1
  507.       saveData()
  508.    end
  509.   end
  510. end
  511. function back(n)
  512.   for i = 1,n do
  513.    fuel()
  514.    if t.back() then
  515.     pos.z = pos.z + 1
  516.     saveData()
  517.    end
  518.   end
  519. end
  520. function left(n)
  521.    fuel()
  522.    if t.turnLeft() then
  523.     pos.f = pos.f - 1
  524.     saveData()
  525.    end
  526.   for i = 1,n do
  527.    fuel()
  528.    if t.forward() then
  529.     pos.x = pos.x - 1
  530.     saveData()
  531.    end
  532.   end
  533.    if t.turnRight() then
  534.     pos.f = pos.f + 1
  535.     saveData()
  536.    end
  537. end
  538. function right(n)
  539.    fuel()
  540.    if t.turnRight() then
  541.     pos.f = pos.f + 1
  542.     saveData()
  543.    end
  544.   for i = 1,n do
  545.    fuel()
  546.    if t.forward() then
  547.     pos.x = pos.x + 1
  548.     saveData()
  549.    end 
  550.   end
  551.   if t.turnLeft() then
  552.     pos.f = pos.f - 1
  553.     saveData()
  554.   end
  555. end
  556.  
  557. function move(x,z)
  558.   if z == nil then
  559.     tbl = x
  560.     x = tbl.x - pos.x
  561.     z = tbl.z - pos.z
  562.   else
  563.     x = x - pos.x
  564.     z = z - pos.z
  565.   end
  566.   if x > 0 then
  567.     right(x)
  568.     sleep(0.1)
  569.   end
  570.   if x < 0 then
  571.     left(math.abs(x))
  572.     sleep(0.1)
  573.   end
  574.   if z > 0 then
  575.     back(z)
  576.     sleep(0.1)
  577.   end
  578.   if z < 0 then
  579.     forward(math.abs(z))
  580.     sleep(0.1)
  581.   end
  582. end
  583. ------------------------------------------------------
  584. ------------------------------------------------------
  585.  
  586.  
  587. resetScreen()
  588. seedRepl = 0
  589. select(1)
  590. function main()
  591.   local modem = wrapModem()
  592.   resetScreen()
  593.   if fuel() and sticks() then
  594.     if not loadData() then
  595.       numOfSeeds = seeds()
  596.       if numOfSeeds == 0 then
  597.         numOfSeeds = noSeeds()
  598.       end
  599.       saveData()
  600.       modem.transmit(77, 78, "The turtle is still running")
  601.       modem.transmit(77, 78, seed.name)
  602.       if numOfSeeds == 1 then
  603.         placeSeeds(pos.seed[0],pos.actv[0])
  604.         placeSticksInActv(pos.actv[2],true)
  605.         waitForSeedToGrow()
  606.         analyze(true)
  607.         placeSeeds(pos.seed[1],pos.actv[1])
  608.       end
  609.       if numOfSeeds == 2 then
  610.         placeSeeds(pos.seed[0],pos.actv[0])
  611.         placeSeeds(pos.seed[1],pos.actv[1])
  612.       end
  613.     else
  614.       modem.transmit(77, 78, "The turtle is still running")
  615.       modem.transmit(77, 78, seed.name)
  616.       modem.transmit(77, 78, "Seed is "..seed.g.." / "..seed.y.." / "..seed.s)
  617.       if pos.f < 2 then
  618.         t.turnRight()
  619.       elseif pos.f > 2 then
  620.         t.turnLeft()
  621.       end
  622.       move(pos.anlzer)
  623.       local anl = wrapAnlzer()
  624.       anl.analyze()
  625.       for i=0,50 do
  626.         if anl.isAnalyzed() then
  627.             break
  628.         end
  629.         os.sleep(0.2)
  630.       end
  631.       if compareItemInSlot(seed.name,slot.seed) or compareItemInSlot(seed.name,slot.sticks2) or suckDownInSlot(slot.seed, 64) then
  632.         analyseSeed()
  633.       end
  634.       move(pos.anlzer)
  635.     end
  636.     while not isMaxedOut() do
  637.       modem.transmit(77, 78, "The turtle is still running")
  638.       modem.transmit(77, 78, seed.name)
  639.       placeSticksInActv(pos.actv[2],true)
  640.       waitForSeedToGrow()
  641.       analyseSeed()
  642.     end
  643.   end
  644. end --MAIN END
  645.  
  646. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement