Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- orderFunctions = {}
- function startup()
- seedNameOfDrop = {}
- scrollmenuLen={}
- scrollmenuInfos={}
- dropdown = {}
- dropdown["loaded"] = {}
- dropdown["loaded"]["len"] = 0
- orders = {}
- requestsFromInterface = {}
- potPeripheralName = "botanypots:botany_pot"
- farmlandStorageName = "minecraft:barrel_0"
- farmlandStoragePeripheral = peripheral.wrap(farmlandStorageName)
- outputStorageName = "sophisticatedstorage:barrel_1"
- outputStoragePeripheral = peripheral.wrap(outputStorageName)
- seedStorageName = "sophisticatedstorage:barrel_0"
- seedStoragePeripheral = peripheral.wrap(seedStorageName)
- trashStorageName = "sophisticatedstorage:barrel_2"
- trashStoragePeripheral = peripheral.wrap(trashStorageName)
- seedSlot = 2
- maxEssenceAmount = 1000000000
- loadSeeds()
- getPotData()
- checkPeripherals()
- loadOrders()
- refreshPots()
- processOrders()
- mainIsDoneWithStartup = true
- end
- function main()
- while true do
- processRequestsFromInterface()
- processOrders()
- createNewOrders()
- sleep(1)
- end
- end
- function createNewOrders()
- --needThisSeed = checkIfNotEnaughSeeds()
- if not needThisSeed then
- local foundFreePot = false
- for k,v in pairs(potData) do
- if v["isFree"] then
- foundFreePot = true
- end
- end
- if foundFreePot then
- getLowestEssenceCount()
- end
- end
- end
- function getLowestEssenceCount()
- local storageList = outputStoragePeripheral.list()
- local curLowestAmount = maxEssenceAmount
- for k, essence in pairs(essenceNames) do
- local curEssenceAmount = 0
- for slot, slotInfo in pairs(storageList) do
- if slotInfo["name"] == essence then
- curEssenceAmount = curEssenceAmount + slotInfo["count"]
- end
- end
- orderIsAlreadyThere = false
- for k, order in pairs(orders) do
- if (order["functionName"] == "produceEssence" and order["param1"] == essence) or (order["functionName"] == "restockSeeds" and order["param1"] == seedsOfDrops[essence]) then
- orderIsAlreadyThere = true
- end
- end
- if curEssenceAmount < curLowestAmount and (not orderIsAlreadyThere) then
- curLowestAmount = curEssenceAmount
- curLowestEssence = essence
- end
- end
- if curLowestEssence then
- createOrder("produceEssence", 3, curLowestEssence, math.min(table.maxn(pots),getStoredSeedAmount(seedsOfDrops[curLowestEssence])) * 5)
- end
- end
- function checkIfNotEnaughSeeds()
- for seedName, v in pairs(seeds) do
- seedAmount = getStoredSeedAmount(seedName)
- for k, order in pairs(orders) do
- if order["param1"] == seedName or seedsOfDrops[order["param1"]] == seedName then
- seedAmount = seedAmount + order["potAmount"]
- end
- end
- if seedAmount < table.maxn(pots) and (not v["dontRestock"]) then
- local orderIsAlreadyThere = false
- for k, order in pairs(orders) do
- if order["functionName"] == "restockSeeds" and order["param1"] == seedName then
- orderIsAlreadyThere = true
- end
- end
- if not orderIsAlreadyThere then
- createOrder("restockSeeds", 4, seedName)
- return seedName
- end
- end
- end
- end
- function loadSeeds()
- seeds = readFolder("seeds")
- seedNames = {}
- essenceNames = {}
- seedsOfDrops = {}
- for k, v in pairs(seeds) do
- table.insert(seedNames,k)
- table.insert(essenceNames,v["drop"])
- seedsOfDrops[v["drop"]] = k
- end
- end
- function processRequestsFromInterface()
- for k, request in pairs(requestsFromInterface) do
- if request["action"] == "cancelOrder" then
- cancelOrder(request["orderKey"])
- end
- if request["action"] == "createOrder" then
- createOrder(request["functionName"],request["priority"],request["param1"],request["param2"])
- end
- if request["action"] == "registerSeed" then
- registerSeed(request["seedName"],request["drop"],request["restockSeed"])
- end
- requestsFromInterface[k] = nil
- end
- end
- function registerSeed(seedName,drop,restockSeed)
- seedsOfDrops[drop] = seedName
- table.insert(seedNames,seedName)
- table.insert(essenceNames,drop)
- seeds[seedName] = {["drop"] = drop, ["restockSeed"] = restockSeed}
- saveTable("seeds/"..seedName,seeds[seedName])
- end
- function checkPeripherals()
- peripheralNames = peripheral.getNames()
- if not table.contains(peripheralNames,farmlandStorageName) then
- error("no farmland storage found")
- end
- if not table.contains(peripheralNames,seedStorageName) then
- error("no seed storage found")
- end
- if not table.contains(peripheralNames,outputStorageName) then
- error("no output storage found")
- end
- if not table.contains(peripheralNames,trashStorageName) then
- error("no trash storage found")
- end
- for k,v in pairs(potData) do
- if not table.contains(peripheralNames,k) then
- error(k .. " is not connected")
- end
- end
- end
- function processOrders()
- sortedOrders = {}
- for k,order in pairs(orders) do
- sortedOrders[order["priority"]] = (sortedOrders[order["priority"]] or {})
- table.insert(sortedOrders[order["priority"]], k)
- end
- for i = 0, table.maxn(sortedOrders)-1 do
- if sortedOrders[table.maxn(sortedOrders)-i] then
- for k, v in pairs(sortedOrders[table.maxn(sortedOrders)-i]) do
- order = orders[v]
- _ENV["orderFunctions"][order["functionName"]](v, order["param1"], order["param2"])
- end
- end
- end
- end
- function orderFunctions.restockSeeds(orderKey,seedName)
- collectDrops(orderKey)
- storedSeedAmount = getStoredSeedAmount(seedName)
- if storedSeedAmount + orders[orderKey]["potAmount"] >= table.maxn(pots) then -- check if done
- cancelOrder(orderKey)
- else
- fillFreePots(seedName,orderKey,storedSeedAmount)
- end
- end
- function orderFunctions.produceEssence(orderKey)
- local essenceName = orders[orderKey]["param1"]
- local seedName = seedsOfDrops[essenceName]
- orders[orderKey]["param2"] = orders[orderKey]["param2"] - collectDrops(orderKey,essenceName)
- saveOrder(orderKey)
- if orders[orderKey]["param2"] < 1 then
- cancelOrder(orderKey)
- else
- fillFreePots(seedName,orderKey)
- end
- end
- function fillFreePots(seedName,orderKey,storedSeedAmount)
- seedList = seedStoragePeripheral.list()
- if not storedSeedAmount then
- storedSeedAmount = getStoredSeedAmount(seedName)
- end
- if storedSeedAmount > 0 then
- for potName,v in pairs(potData) do
- if v["isFree"] or orders[v["processingOrder"]]["priority"] < orders[orderKey]["priority"] then
- if not v["isFree"] then
- if freePot(potName) then
- fillPot(orderKey,potName,seedName)
- end
- else
- fillPot(orderKey,potName,seedName)
- end
- local seed = pots[v["peripheralKey"]].list()[seedSlot]
- if not seed then
- if potData[potName]["processingOrder"] == orderKey then
- potData[potName]["processingOrder"] = nil
- potData[potName]["isFree"] = true
- savePotData(potName)
- orders[orderKey]["potAmount"] = orders[orderKey]["potAmount"] - 1
- saveOrder(orderKey)
- end
- end
- end
- end
- end
- end
- function fillPot(orderKey,potName,seedName)
- filledPot = false
- for slot, item in pairs(seedStoragePeripheral.list()) do
- if item["name"] == seedName then
- filledPot = pots[potData[potName]["peripheralKey"]].pullItems(seedStorageName,slot,1)
- filledPot = filledPot > 0
- break
- end
- end
- if filledPot then
- potData[potName]["isFree"] = false
- potData[potName]["processingOrder"] = orderKey
- orders[orderKey]["potAmount"] = orders[orderKey]["potAmount"] + 1
- saveOrder(orderKey)
- end
- savePotData(potName)
- return filledPot
- end
- function freePot(potName)
- local returnValue = seedStoragePeripheral.pullItems(potName,seedSlot)
- returnValue = returnValue > 0
- if returnValue then
- orders[potData[potName]["processingOrder"]]["potAmount"] = orders[potData[potName]["processingOrder"]]["potAmount"] - 1
- saveOrder(potData[potName]["processingOrder"])
- potData[potName]["isFree"] = true
- potData[potName]["processingOrder"] = nil
- savePotData(potName)
- end
- return returnValue
- end
- function getStoredSeedAmount(seedName)
- local returnValue = 0
- for k,v in pairs(seedStoragePeripheral.list()) do
- if v["name"] == seedName then
- returnValue = returnValue + v["count"]
- end
- end
- return returnValue
- end
- function cancelOrder(orderKey)
- orders[orderKey] = nil
- fs.delete("orders/" .. orderKey)
- for k,v in pairs(potData) do
- if v["processingOrder"] == orderKey then
- potData[k]["processingOrder"] = nil
- potData[k]["isFree"] = true
- pots[v["peripheralKey"]].pushItems(seedStorageName,seedSlot)
- savePotData(k)
- end
- end
- end
- function refreshPots()
- pots = {}
- for k, v in pairs(peripheral.getNames()) do
- if string.find(v,potPeripheralName) then
- table.insert(pots,peripheral.wrap(v))
- firstFarmlandSlot, farmland = pairs({})(farmlandStoragePeripheral.list())
- farmlandName = farmland["name"]
- potItemList = pots[table.maxn(pots)].list()
- if not potItemList[1] then
- pots[table.maxn(pots)].pullItems(farmlandStorageName,firstFarmlandSlot,1)
- elseif potItemList[1]["name"] ~= farmlandName then
- pots[table.maxn(pots)].pushItems(trashStorageName,1)
- pots[table.maxn(pots)].pullItems(farmlandStorageName,firstFarmlandSlot,1)
- end
- if not potData[v] then
- if potItemList[seedSlot] then
- pots[table.maxn(pots)].pushItems(seedStorageName,seedSlot)
- end
- potData[v] = {}
- potData[v]["isFree"] = true
- elseif (not potData[v]["isFree"]) and (not potItemList[seedSlot]) then
- orders[potData[v]["processingOrder"]]["potAmount"] = orders[potData[v]["processingOrder"]]["potAmount"] - 1
- saveOrder(potData[v]["processingOrder"])
- potData[v]["isFree"] = true
- potData[v]["processingOrder"] = nil
- end
- potData[v]["peripheralKey"] = table.maxn(pots)
- savePotData(v)
- end
- end
- end
- function collectDrops(orderKey,lookForDrop)
- local returnValue = 0
- for k,v in pairs(potData) do
- if v["processingOrder"] == orderKey then
- local pot = pots[v["peripheralKey"]]
- for slot, slotInfo in pairs(pot.list()) do
- if slot > seedSlot then
- if table.contains(seedNames,slotInfo["name"]) then
- pushedAmount = pot.pushItems(seedStorageName,slot)
- else
- pushedAmount = pot.pushItems(outputStorageName,slot)
- end
- if slotInfo["name"] == lookForDrop then
- returnValue = returnValue + pushedAmount
- end
- end
- end
- end
- end
- return returnValue
- end
- function getPotData()
- potData = readFolder("potData")
- end
- function savePotData(potName)
- if potName then
- saveTable("potData/" .. potName, potData[potName])
- else
- saveTable("potData", potData)
- end
- end
- function createOrder(functionName, priority, param1, param2)
- k = table.maxn(orders) + 1
- orders[k] = {}
- orders[k]["functionName"] = functionName
- orders[k]["priority"] = priority
- orders[k]["param1"] = param1
- orders[k]["param2"] = param2
- orders[k]["potAmount"] = 0
- saveOrder(k)
- end
- function saveOrder(orderKey)
- if orderKey then
- saveTable("orders/" .. orderKey, orders[orderKey])
- else
- saveTable("orders", orders)
- end
- end
- function loadOrders()
- local ordersSave = readFolder("orders")
- local deleteThese = {}
- for k,v in pairs(ordersSave) do
- orders[tonumber(k)] = v
- end
- end
- -- buttons and all that stuff
- function table.contains(inputTable,value)
- for k,v in pairs(inputTable) do
- if v == value then
- return true
- end
- end
- return false
- end
- function saveTable(folderName,inputTable)
- folderName = string.gsub(folderName,":","##.")
- fs.delete(folderName)
- local file = false
- for k,v in pairs(inputTable) do
- if type(v) ~= "table" then
- if not file then
- file = fs.open(folderName .. "/variables","w")
- end
- vType = type(v)
- if vType == "boolean" then
- if v then
- v = "true"
- else
- v = "false"
- end
- end
- file.writeLine(k .. string.sub(type(k),1,1) .. "=" .. string.sub(vType,1,1) .. v)
- else
- saveTable(folderName .. "/" .. k,v)
- end
- end
- if file then
- file.flush()
- file.close()
- end
- end
- function readFile(fileName)
- local file = fs.open(fileName,"r")
- local returnTable = {}
- if file then
- repeat
- local curLine = file.readLine()
- local equalPos = string.find(curLine or "","=")
- if equalPos then
- local indexType = string.sub(curLine,equalPos-1,equalPos-1)
- local valueType = string.sub(curLine,equalPos+1,equalPos+1)
- local index = string.sub(curLine,1,equalPos-2)
- if indexType == "i" then
- index = tonumber(index)
- elseif indexType == "b" then
- if index == "true" then
- index = true
- else
- index = false
- end
- end
- local value = string.sub(curLine,equalPos+2)
- if valueType == "n" then
- value = tonumber(value)
- elseif valueType == "b" then
- if value == "true" then
- value = true
- else
- value = false
- end
- end
- returnTable[index] = value
- end
- until not curLine
- file.close()
- end
- return returnTable
- end
- function readFolder(folderName)
- folderName = string.gsub(folderName,":","##.")
- local returnTable = {}
- if fs.isDir(folderName) then
- for k,v in pairs(fs.list(folderName)) do
- if fs.isDir(folderName .. "/" .. v) then
- returnTable[string.gsub(v,"##.",":")] = readFolder(folderName .. "/" .. v)
- else
- for k2,v2 in pairs(readFile(folderName .. "/" .. v)) do
- returnTable[k2] = v2
- end
- end
- end
- end
- return returnTable
- end
- -------------------------------------------------
- startup()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement