Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.wrap("modem_2") or error("No modem attached", 0)
- local monitor = peripheral.wrap("monitor_2") or error("No monitor attached", 0)
- local bg1 = peripheral.wrap("bioGenerator_1") or error("Bio Gen 1 not attached", 0)
- local bg2 = peripheral.wrap("bioGenerator_2") or error("Bio Gen 2 not attached", 0)
- local bg3 = peripheral.wrap("bioGenerator_3") or error("Bio Gen 3 not attached", 0)
- local bg4 = peripheral.wrap("bioGenerator_4") or error("Bio Gen 4 not attached", 0)
- local bg5 = peripheral.wrap("bioGenerator_5") or error("Bio Gen 5 not attached", 0)
- local bg6 = peripheral.wrap("bioGenerator_6") or error("Bio Gen 6 not attached", 0)
- local bg7 = peripheral.wrap("bioGenerator_7") or error("Bio Gen 7 not attached", 0)
- local bg8 = peripheral.wrap("bioGenerator_8") or error("Bio Gen 8 not attached", 0)
- local bg9 = peripheral.wrap("bioGenerator_9") or error("Bio Gen 9 not attached", 0)
- local generatorMotor = peripheral.wrap("electric_motor_2") or error("Generator Motor not attached!", 0)
- local sugarcaneStorage = peripheral.wrap("inventory_2") or error("Sugarcane storage not attached", 0)
- local pumpkinSeedStorage = peripheral.wrap("inventory_3") or error("Pumpkin seed storage not attached", 0)
- modem.open(43) -- Open 43 so we can receive replies
- monitor.clear()
- print("Just think of Woo. Just Woo. Just Woo. Just Woo.")
- local baselineRPM = 100 -- Initial baseline RPM
- local adjustmentFactor = 0.1 -- Factor to adjust the baseline RPM
- local adjustmentThreshold = 0.05 -- Threshold for biofuel percentage to trigger adjustment
- local function getAverageBioFuelPercentage()
- local totalPercentage = 0
- local generators = {bg1, bg2, bg3, bg4, bg5, bg6, bg7, bg8, bg9}
- for _, bg in ipairs(generators) do
- totalPercentage = totalPercentage + bg.getBioFuelFilledPercentage()
- end
- return totalPercentage / #generators
- end
- local function getTotalEnergyGenerated()
- local totalEnergy = 0
- local generators = {bg1, bg2, bg3, bg4, bg5, bg6, bg7, bg8, bg9}
- for _, bg in ipairs(generators) do
- if bg.getBioFuelFilledPercentage() > 0 then
- totalEnergy = totalEnergy + 140
- end
- end
- return totalEnergy
- end
- local function getTotalBioFuelNeeded()
- local totalBioFuelNeeded = 0
- local generators = {bg1, bg2, bg3, bg4, bg5, bg6, bg7, bg8, bg9}
- for _, bg in ipairs(generators) do
- totalBioFuelNeeded = totalBioFuelNeeded + bg.getBioFuelNeeded()
- end
- return totalBioFuelNeeded
- end
- local function checkStorage()
- local sugarcaneEmpty = true
- local pumpkinSeedsEmpty = true
- local sugarcaneList = sugarcaneStorage.list()
- if sugarcaneList then
- for slot, item in pairs(sugarcaneList) do
- if item.name == "minecraft:sugar_cane" then
- sugarcaneEmpty = false
- break
- end
- end
- else
- print("sugarcaneStorage.list() is nil!")
- end
- local pumpkinSeedList = pumpkinSeedStorage.list()
- if pumpkinSeedList then
- for slot, item in pairs(pumpkinSeedList) do
- if item.name == "minecraft:pumpkin_seeds" then
- pumpkinSeedsEmpty = false
- break
- end
- end
- else
- print("pumpkinSeedStorage.list() is nil!")
- end
- return sugarcaneEmpty, pumpkinSeedsEmpty
- end
- local function adjustGeneratorMotorSpeed()
- local totalBioFuelNeeded = getTotalBioFuelNeeded()
- local motorSpeed = baselineRPM + math.min(math.max(totalBioFuelNeeded * 0.1, 0), 156) -- Adjust speed based on biofuel needed, clamped to a max of 256 RPM
- generatorMotor.setSpeed(motorSpeed)
- print("Total BioFuel Needed: " .. totalBioFuelNeeded)
- print("Generator Motor Speed: " .. motorSpeed)
- end
- local function adjustBaselineRPM()
- local averagePercentage = getAverageBioFuelPercentage()
- if averagePercentage < 0.5 - adjustmentThreshold then
- baselineRPM = baselineRPM + adjustmentFactor * baselineRPM
- elseif averagePercentage > 0.5 + adjustmentThreshold then
- baselineRPM = baselineRPM - adjustmentFactor * baselineRPM
- end
- baselineRPM = math.max(baselineRPM, 100) -- Ensure baseline RPM does not go below 100
- print("Adjusted Baseline RPM: " .. baselineRPM)
- end
- local totalConsumption = 0
- while true do
- sleep(1)
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if channel == 43 then
- -- power usage channel
- local identifier, powerUsage = message:match("^(%w+)%s+(%d+)$")
- if identifier == "BL1" then
- totalConsumption = tonumber(powerUsage)
- monitor.setCursorPos(1, 7)
- monitor.write("Birch Log Farm #1: " .. powerUsage .. " FE/t")
- end
- end
- adjustGeneratorMotorSpeed()
- adjustBaselineRPM()
- monitor.setCursorPos(1, 8)
- monitor.write("Biomass Generation #1: " .. generatorMotor.getEnergyConsumption() .. " FE/t")
- totalConsumption = totalConsumption + generatorMotor.getEnergyConsumption()
- -- Display average biomass filled percentage and total energy generated
- local averagePercentage = getAverageBioFuelPercentage()
- local totalEnergy = getTotalEnergyGenerated()
- monitor.setTextColor(colors.green)
- monitor.setCursorPos(1, 1)
- monitor.write("Avg BioFuel %: " .. string.format("%.2f", averagePercentage * 100) .. "%")
- monitor.setCursorPos(1, 2)
- monitor.write("Generating: " .. totalEnergy .. " FE/t") -- the 246 is from the water wheels
- monitor.setCursorPos(1, 3)
- monitor.write("Free Energy: " .. totalEnergy - totalConsumption .. " FE/t")
- monitor.setCursorPos(1, 4)
- monitor.setTextColor(colors.lightGray)
- monitor.write("-----------------------------")
- monitor.setTextColor(colors.white)
- -- Check storage and warn if empty
- local sugarcaneEmpty, pumpkinSeedsEmpty = checkStorage()
- monitor.setCursorPos(1, 5)
- if sugarcaneEmpty then
- monitor.setTextColor(colors.red)
- monitor.write("WARNING: Sugarcane storage is empty!")
- monitor.setTextColor(colors.white)
- else
- monitor.clearLine()
- end
- monitor.setCursorPos(1, 6)
- if pumpkinSeedsEmpty then
- monitor.setTextColor(colors.red)
- monitor.write("WARNING: Pumpkin seed storage is empty!")
- monitor.setTextColor(colors.white)
- else
- monitor.clearLine()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement