Advertisement
Chaos_Cash

seedManager.lua

Feb 3rd, 2025 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.37 KB | None | 0 0
  1. orderFunctions = {}
  2.  
  3.  
  4. function startup()
  5. seedNameOfDrop = {}
  6.  
  7.  
  8. scrollmenuLen={}
  9. scrollmenuInfos={}
  10.  
  11. dropdown = {}
  12. dropdown["loaded"] = {}
  13. dropdown["loaded"]["len"] = 0
  14.  
  15. orders = {}
  16.  
  17.  
  18. requestsFromInterface = {}
  19.  
  20.  
  21. potPeripheralName = "botanypots:botany_pot"
  22. farmlandStorageName = "minecraft:barrel_0"
  23. farmlandStoragePeripheral = peripheral.wrap(farmlandStorageName)
  24. outputStorageName = "sophisticatedstorage:barrel_1"
  25. outputStoragePeripheral = peripheral.wrap(outputStorageName)
  26. seedStorageName = "sophisticatedstorage:barrel_0"
  27. seedStoragePeripheral = peripheral.wrap(seedStorageName)
  28. trashStorageName = "sophisticatedstorage:barrel_2"
  29. trashStoragePeripheral = peripheral.wrap(trashStorageName)
  30.  
  31. seedSlot = 2
  32.  
  33. maxEssenceAmount = 1000000000
  34.  
  35.  
  36. loadSeeds()
  37. getPotData()
  38. checkPeripherals()
  39. loadOrders()
  40. refreshPots()
  41. processOrders()
  42.  
  43. mainIsDoneWithStartup = true
  44. end
  45.  
  46.  
  47.  
  48. function main()
  49.  
  50.     while true do
  51.     processRequestsFromInterface()
  52.     processOrders()
  53.     createNewOrders()
  54.     sleep(1)
  55.     end
  56.    
  57. end
  58.  
  59.  
  60.  
  61. function createNewOrders()
  62. --needThisSeed = checkIfNotEnaughSeeds()
  63.  
  64.  
  65.     if not needThisSeed then
  66.     local foundFreePot = false
  67.         for k,v in pairs(potData) do
  68.             if v["isFree"] then
  69.             foundFreePot = true
  70.             end
  71.         end
  72.        
  73.         if foundFreePot then
  74.         getLowestEssenceCount()
  75.         end
  76.     end
  77. end
  78.  
  79.  
  80.  
  81. function getLowestEssenceCount()
  82. local storageList = outputStoragePeripheral.list()
  83. local curLowestAmount = maxEssenceAmount
  84.  
  85.     for k, essence in pairs(essenceNames) do
  86.     local curEssenceAmount = 0
  87.         for slot, slotInfo in pairs(storageList) do
  88.             if slotInfo["name"] == essence then
  89.             curEssenceAmount = curEssenceAmount + slotInfo["count"]
  90.             end
  91.         end
  92.    
  93.    
  94.     orderIsAlreadyThere = false
  95.         for k, order in pairs(orders) do
  96.             if (order["functionName"] == "produceEssence" and order["param1"] == essence) or (order["functionName"] == "restockSeeds" and order["param1"] == seedsOfDrops[essence]) then
  97.             orderIsAlreadyThere = true
  98.             end
  99.         end
  100.    
  101.         if curEssenceAmount < curLowestAmount and (not orderIsAlreadyThere) then
  102.         curLowestAmount = curEssenceAmount
  103.         curLowestEssence = essence
  104.         end
  105.     end
  106.  
  107.  
  108.     if curLowestEssence then
  109.     createOrder("produceEssence", 3, curLowestEssence, math.min(table.maxn(pots),getStoredSeedAmount(seedsOfDrops[curLowestEssence])) * 5)
  110.     end
  111.  
  112. end
  113.  
  114.  
  115.  
  116. function checkIfNotEnaughSeeds()
  117.  
  118.     for seedName, v in pairs(seeds) do
  119.     seedAmount = getStoredSeedAmount(seedName)
  120.         for k, order in pairs(orders) do
  121.             if order["param1"] == seedName or seedsOfDrops[order["param1"]] == seedName then
  122.             seedAmount = seedAmount + order["potAmount"]
  123.             end
  124.         end
  125.        
  126.        
  127.    
  128.        
  129.         if seedAmount < table.maxn(pots) and (not v["dontRestock"]) then
  130.         local orderIsAlreadyThere = false
  131.             for k, order in pairs(orders) do
  132.                 if order["functionName"] == "restockSeeds" and order["param1"] == seedName then
  133.                 orderIsAlreadyThere = true
  134.                 end
  135.             end
  136.            
  137.             if not orderIsAlreadyThere then
  138.             createOrder("restockSeeds", 4, seedName)
  139.             return seedName
  140.             end
  141.         end
  142.     end
  143.    
  144.  
  145. end
  146.  
  147.  
  148.  
  149. function loadSeeds()
  150. seeds = readFolder("seeds")
  151. seedNames = {}
  152. essenceNames = {}
  153. seedsOfDrops = {}
  154.     for k, v in pairs(seeds) do
  155.     table.insert(seedNames,k)
  156.     table.insert(essenceNames,v["drop"])
  157.     seedsOfDrops[v["drop"]] = k
  158.     end
  159. end
  160.  
  161.  
  162.  
  163. function processRequestsFromInterface()
  164.  
  165.     for k, request in pairs(requestsFromInterface) do
  166.         if request["action"] == "cancelOrder" then
  167.         cancelOrder(request["orderKey"])
  168.         end
  169.        
  170.        
  171.         if request["action"] == "createOrder" then
  172.         createOrder(request["functionName"],request["priority"],request["param1"],request["param2"])
  173.         end
  174.        
  175.        
  176.         if request["action"] == "registerSeed" then
  177.         registerSeed(request["seedName"],request["drop"],request["restockSeed"])
  178.         end
  179.        
  180.        
  181.     requestsFromInterface[k] = nil
  182.     end
  183.  
  184. end
  185.  
  186.  
  187.  
  188. function registerSeed(seedName,drop,restockSeed)
  189.  
  190. seedsOfDrops[drop] = seedName
  191. table.insert(seedNames,seedName)
  192. table.insert(essenceNames,drop)
  193. seeds[seedName] = {["drop"] = drop, ["restockSeed"] = restockSeed}
  194. saveTable("seeds/"..seedName,seeds[seedName])
  195.  
  196. end
  197.  
  198.  
  199.  
  200. function checkPeripherals()
  201. peripheralNames = peripheral.getNames()
  202.  
  203.     if not table.contains(peripheralNames,farmlandStorageName) then
  204.     error("no farmland storage found")
  205.     end
  206.  
  207.     if not table.contains(peripheralNames,seedStorageName) then
  208.     error("no seed storage found")
  209.     end
  210.  
  211.     if not table.contains(peripheralNames,outputStorageName) then
  212.     error("no output storage found")
  213.     end
  214.    
  215.     if not table.contains(peripheralNames,trashStorageName) then
  216.     error("no trash storage found")
  217.     end
  218.  
  219.     for k,v in pairs(potData) do
  220.         if not table.contains(peripheralNames,k) then
  221.         error(k .. " is not connected")
  222.         end
  223.     end
  224.  
  225. end
  226.  
  227.  
  228.  
  229. function processOrders()
  230. sortedOrders = {}
  231.     for k,order in pairs(orders) do
  232.     sortedOrders[order["priority"]] = (sortedOrders[order["priority"]] or {})
  233.     table.insert(sortedOrders[order["priority"]], k)
  234.     end
  235.  
  236.     for i = 0, table.maxn(sortedOrders)-1 do
  237.         if sortedOrders[table.maxn(sortedOrders)-i] then
  238.             for k, v in pairs(sortedOrders[table.maxn(sortedOrders)-i]) do
  239.             order = orders[v]
  240.             _ENV["orderFunctions"][order["functionName"]](v, order["param1"], order["param2"])
  241.             end
  242.         end
  243.     end
  244. end
  245.  
  246.  
  247.  
  248. function orderFunctions.restockSeeds(orderKey,seedName)
  249. collectDrops(orderKey)
  250.  
  251. storedSeedAmount = getStoredSeedAmount(seedName)
  252.  
  253.     if storedSeedAmount + orders[orderKey]["potAmount"] >= table.maxn(pots) then  -- check if done
  254.     cancelOrder(orderKey)
  255.     else
  256.     fillFreePots(seedName,orderKey,storedSeedAmount)
  257.     end
  258.  
  259. end
  260.  
  261.  
  262.  
  263. function orderFunctions.produceEssence(orderKey)
  264. local essenceName = orders[orderKey]["param1"]
  265. local seedName = seedsOfDrops[essenceName]
  266.  
  267. orders[orderKey]["param2"] = orders[orderKey]["param2"] - collectDrops(orderKey,essenceName)
  268. saveOrder(orderKey)
  269.  
  270.     if orders[orderKey]["param2"] < 1 then
  271.     cancelOrder(orderKey)
  272.     else
  273.     fillFreePots(seedName,orderKey)
  274.     end
  275. end
  276.  
  277.  
  278.  
  279. function fillFreePots(seedName,orderKey,storedSeedAmount)
  280. seedList = seedStoragePeripheral.list()
  281.  
  282.     if not storedSeedAmount then
  283.     storedSeedAmount = getStoredSeedAmount(seedName)
  284.     end
  285.  
  286.     if storedSeedAmount > 0 then
  287.    
  288.         for potName,v in pairs(potData) do
  289.             if v["isFree"] or orders[v["processingOrder"]]["priority"] < orders[orderKey]["priority"] then
  290.            
  291.                 if not v["isFree"] then
  292.                     if freePot(potName) then
  293.                     fillPot(orderKey,potName,seedName)
  294.                     end
  295.                 else
  296.                 fillPot(orderKey,potName,seedName)
  297.                 end
  298.                
  299.             local seed = pots[v["peripheralKey"]].list()[seedSlot]
  300.                
  301.                 if not seed then
  302.                     if potData[potName]["processingOrder"] == orderKey then
  303.                     potData[potName]["processingOrder"] = nil
  304.                     potData[potName]["isFree"] = true
  305.                     savePotData(potName)
  306.                     orders[orderKey]["potAmount"] = orders[orderKey]["potAmount"] - 1
  307.                     saveOrder(orderKey)
  308.                     end
  309.                 end
  310.                
  311.             end
  312.         end
  313.     end
  314. end
  315.  
  316.  
  317.  
  318. function fillPot(orderKey,potName,seedName)
  319.  
  320. filledPot = false
  321.     for slot, item in pairs(seedStoragePeripheral.list()) do
  322.         if item["name"] == seedName then
  323.        
  324.         filledPot = pots[potData[potName]["peripheralKey"]].pullItems(seedStorageName,slot,1)
  325.         filledPot = filledPot > 0
  326.         break
  327.         end
  328.     end
  329.  
  330.     if filledPot then
  331.    
  332.     potData[potName]["isFree"] = false
  333.     potData[potName]["processingOrder"] = orderKey
  334.     orders[orderKey]["potAmount"] = orders[orderKey]["potAmount"] + 1
  335.     saveOrder(orderKey)
  336.     end
  337. savePotData(potName)
  338.  
  339. return filledPot
  340.  
  341. end
  342.  
  343.  
  344.  
  345. function freePot(potName)
  346. local returnValue = seedStoragePeripheral.pullItems(potName,seedSlot)
  347. returnValue = returnValue > 0
  348.     if returnValue then
  349.     orders[potData[potName]["processingOrder"]]["potAmount"] = orders[potData[potName]["processingOrder"]]["potAmount"] - 1
  350.     saveOrder(potData[potName]["processingOrder"])
  351.     potData[potName]["isFree"] = true
  352.     potData[potName]["processingOrder"] = nil
  353.     savePotData(potName)
  354.     end
  355. return returnValue
  356. end
  357.  
  358.  
  359.  
  360. function getStoredSeedAmount(seedName)
  361. local returnValue = 0
  362.     for k,v in pairs(seedStoragePeripheral.list()) do
  363.         if v["name"] == seedName then
  364.         returnValue = returnValue + v["count"]
  365.         end
  366.     end
  367.  
  368. return returnValue
  369. end
  370.  
  371.  
  372.  
  373. function cancelOrder(orderKey)
  374. orders[orderKey] = nil
  375. fs.delete("orders/" .. orderKey)
  376.  
  377.     for k,v in pairs(potData) do
  378.         if v["processingOrder"] == orderKey then
  379.         potData[k]["processingOrder"] = nil
  380.         potData[k]["isFree"] = true
  381.         pots[v["peripheralKey"]].pushItems(seedStorageName,seedSlot)
  382.         savePotData(k)
  383.         end
  384.     end
  385.  
  386. end
  387.  
  388.  
  389.  
  390. function refreshPots()
  391. pots = {}
  392.  
  393.     for k, v in pairs(peripheral.getNames()) do
  394.         if string.find(v,potPeripheralName) then
  395.         table.insert(pots,peripheral.wrap(v))
  396.        
  397.         firstFarmlandSlot, farmland = pairs({})(farmlandStoragePeripheral.list())
  398.         farmlandName = farmland["name"]
  399.         potItemList = pots[table.maxn(pots)].list()
  400.             if not potItemList[1] then
  401.             pots[table.maxn(pots)].pullItems(farmlandStorageName,firstFarmlandSlot,1)
  402.             elseif potItemList[1]["name"] ~= farmlandName then
  403.             pots[table.maxn(pots)].pushItems(trashStorageName,1)
  404.             pots[table.maxn(pots)].pullItems(farmlandStorageName,firstFarmlandSlot,1)
  405.             end
  406.        
  407.        
  408.        
  409.             if not potData[v] then
  410.                 if potItemList[seedSlot] then
  411.                 pots[table.maxn(pots)].pushItems(seedStorageName,seedSlot)
  412.                 end
  413.             potData[v] = {}
  414.             potData[v]["isFree"] = true
  415.             elseif (not potData[v]["isFree"]) and (not potItemList[seedSlot]) then
  416.             orders[potData[v]["processingOrder"]]["potAmount"] = orders[potData[v]["processingOrder"]]["potAmount"] - 1
  417.             saveOrder(potData[v]["processingOrder"])
  418.            
  419.             potData[v]["isFree"] = true
  420.             potData[v]["processingOrder"] = nil
  421.             end
  422.         potData[v]["peripheralKey"] = table.maxn(pots) 
  423.        
  424.         savePotData(v)
  425.         end
  426.     end
  427.  
  428. end
  429.  
  430.  
  431.  
  432. function collectDrops(orderKey,lookForDrop)
  433. local returnValue = 0
  434.  
  435.     for k,v in pairs(potData) do
  436.         if v["processingOrder"] == orderKey then
  437.         local pot = pots[v["peripheralKey"]]
  438.             for slot, slotInfo in pairs(pot.list()) do
  439.            
  440.                 if slot > seedSlot then
  441.                     if table.contains(seedNames,slotInfo["name"]) then
  442.                     pushedAmount = pot.pushItems(seedStorageName,slot)
  443.                     else
  444.                     pushedAmount = pot.pushItems(outputStorageName,slot)
  445.                     end
  446.                    
  447.                     if slotInfo["name"] == lookForDrop then
  448.                     returnValue = returnValue + pushedAmount
  449.                     end
  450.                 end
  451.             end
  452.         end
  453.     end
  454.  
  455. return returnValue
  456. end
  457.  
  458.  
  459.  
  460. function getPotData()
  461. potData = readFolder("potData")
  462. end
  463.  
  464.  
  465.  
  466. function savePotData(potName)
  467.  
  468.     if potName then
  469.     saveTable("potData/" .. potName, potData[potName])
  470.     else
  471.     saveTable("potData", potData)
  472.     end
  473.  
  474. end
  475.  
  476.  
  477.  
  478. function createOrder(functionName, priority, param1, param2)
  479. k = table.maxn(orders) + 1
  480.  
  481. orders[k] = {}
  482. orders[k]["functionName"] = functionName
  483. orders[k]["priority"] = priority
  484. orders[k]["param1"] = param1
  485. orders[k]["param2"] = param2
  486. orders[k]["potAmount"] = 0
  487.  
  488. saveOrder(k)
  489. end
  490.  
  491.  
  492.  
  493. function saveOrder(orderKey)
  494.  
  495.     if orderKey then
  496.     saveTable("orders/" .. orderKey, orders[orderKey])
  497.     else
  498.     saveTable("orders", orders)
  499.     end
  500.  
  501. end
  502.  
  503.  
  504.  
  505. function loadOrders()
  506. local ordersSave = readFolder("orders")
  507. local deleteThese = {}
  508.  
  509.     for k,v in pairs(ordersSave) do
  510.     orders[tonumber(k)] = v
  511.     end
  512. end
  513.  
  514.  
  515.  
  516.  
  517. -- buttons and all that stuff
  518.  
  519.  
  520. function table.contains(inputTable,value)
  521.     for k,v in pairs(inputTable) do
  522.         if v == value then
  523.         return true
  524.         end
  525.     end
  526. return false
  527. end
  528.  
  529.  
  530.  
  531. function saveTable(folderName,inputTable)
  532. folderName = string.gsub(folderName,":","##.")
  533.  
  534. fs.delete(folderName)
  535. local file = false
  536.  
  537.     for k,v in pairs(inputTable) do
  538.         if type(v) ~= "table" then
  539.             if not file then
  540.             file = fs.open(folderName .. "/variables","w")
  541.             end
  542.            
  543.             vType = type(v)
  544.                 if vType == "boolean" then
  545.                     if v then
  546.                     v = "true"
  547.                     else
  548.                     v = "false"
  549.                     end
  550.                 end
  551.            
  552.         file.writeLine(k .. string.sub(type(k),1,1) .. "=" .. string.sub(vType,1,1) .. v)
  553.         else
  554.         saveTable(folderName .. "/" .. k,v)
  555.         end
  556.     end
  557.  
  558.     if file then
  559.     file.flush()
  560.     file.close()
  561.     end
  562. end
  563.  
  564.  
  565.  
  566. function readFile(fileName)
  567. local file = fs.open(fileName,"r")
  568. local returnTable = {}
  569.  
  570.  
  571.     if file then
  572.         repeat
  573.         local curLine = file.readLine()
  574.         local equalPos = string.find(curLine or "","=")
  575.        
  576.        
  577.             if equalPos then
  578.             local indexType = string.sub(curLine,equalPos-1,equalPos-1)
  579.             local valueType = string.sub(curLine,equalPos+1,equalPos+1)
  580.             local index = string.sub(curLine,1,equalPos-2)
  581.            
  582.                 if indexType == "i" then
  583.                 index = tonumber(index)
  584.                 elseif indexType == "b" then
  585.                     if index == "true" then
  586.                     index = true
  587.                     else
  588.                     index = false
  589.                     end
  590.                 end
  591.                
  592.             local value = string.sub(curLine,equalPos+2)
  593.                 if valueType == "n" then
  594.                 value = tonumber(value)
  595.                 elseif valueType == "b" then
  596.                     if value == "true" then
  597.                     value = true
  598.                     else
  599.                     value = false
  600.                     end
  601.                 end
  602.             returnTable[index] = value
  603.             end
  604.            
  605.         until not curLine
  606.     file.close()
  607.     end
  608.    
  609. return returnTable
  610. end
  611.  
  612.  
  613.  
  614. function readFolder(folderName)
  615. folderName = string.gsub(folderName,":","##.")
  616.  
  617. local returnTable = {}
  618.     if fs.isDir(folderName) then
  619.         for k,v in pairs(fs.list(folderName)) do
  620.             if fs.isDir(folderName .. "/" .. v) then
  621.             returnTable[string.gsub(v,"##.",":")] = readFolder(folderName .. "/" .. v)
  622.             else
  623.                 for k2,v2 in pairs(readFile(folderName .. "/" .. v)) do
  624.                 returnTable[k2] = v2
  625.                 end
  626.             end
  627.         end
  628.     end
  629.  
  630. return returnTable
  631. end
  632.  
  633. -------------------------------------------------
  634.  
  635.  
  636.  
  637. startup()
  638. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement