Advertisement
Hystel

modem energy

Nov 23rd, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. local modem = peripheral.wrap("modem_2") or error("No modem attached", 0)
  2. local monitor = peripheral.wrap("monitor_2") or error("No monitor attached", 0)
  3. local bg1 = peripheral.wrap("bioGenerator_1") or error("Bio Gen 1 not attached", 0)
  4. local bg2 = peripheral.wrap("bioGenerator_2") or error("Bio Gen 2 not attached", 0)
  5. local bg3 = peripheral.wrap("bioGenerator_3") or error("Bio Gen 3 not attached", 0)
  6. local bg4 = peripheral.wrap("bioGenerator_4") or error("Bio Gen 4 not attached", 0)
  7. local bg5 = peripheral.wrap("bioGenerator_5") or error("Bio Gen 5 not attached", 0)
  8. local bg6 = peripheral.wrap("bioGenerator_6") or error("Bio Gen 6 not attached", 0)
  9. local bg7 = peripheral.wrap("bioGenerator_7") or error("Bio Gen 7 not attached", 0)
  10. local bg8 = peripheral.wrap("bioGenerator_8") or error("Bio Gen 8 not attached", 0)
  11. local bg9 = peripheral.wrap("bioGenerator_9") or error("Bio Gen 9 not attached", 0)
  12. local generatorMotor = peripheral.wrap("electric_motor_2") or error("Generator Motor not attached!", 0)
  13. local sugarcaneStorage = peripheral.wrap("inventory_2") or error("Sugarcane storage not attached", 0)
  14. local pumpkinSeedStorage = peripheral.wrap("inventory_3") or error("Pumpkin seed storage not attached", 0)
  15. modem.open(43) -- Open 43 so we can receive replies
  16. monitor.clear()
  17.  
  18. print("Just think of Woo. Just Woo. Just Woo. Just Woo.")
  19.  
  20. local baselineRPM = 100 -- Initial baseline RPM
  21. local adjustmentFactor = 0.1 -- Factor to adjust the baseline RPM
  22. local adjustmentThreshold = 0.05 -- Threshold for biofuel percentage to trigger adjustment
  23.  
  24. local function getAverageBioFuelPercentage()
  25. local totalPercentage = 0
  26. local generators = {bg1, bg2, bg3, bg4, bg5, bg6, bg7, bg8, bg9}
  27. for _, bg in ipairs(generators) do
  28. totalPercentage = totalPercentage + bg.getBioFuelFilledPercentage()
  29. end
  30. return totalPercentage / #generators
  31. end
  32.  
  33. local function getTotalEnergyGenerated()
  34. local totalEnergy = 0
  35. local generators = {bg1, bg2, bg3, bg4, bg5, bg6, bg7, bg8, bg9}
  36. for _, bg in ipairs(generators) do
  37. if bg.getBioFuelFilledPercentage() > 0 then
  38. totalEnergy = totalEnergy + 140
  39. end
  40. end
  41. return totalEnergy
  42. end
  43.  
  44. local function getTotalBioFuelNeeded()
  45. local totalBioFuelNeeded = 0
  46. local generators = {bg1, bg2, bg3, bg4, bg5, bg6, bg7, bg8, bg9}
  47. for _, bg in ipairs(generators) do
  48. totalBioFuelNeeded = totalBioFuelNeeded + bg.getBioFuelNeeded()
  49. end
  50. return totalBioFuelNeeded
  51. end
  52.  
  53. local function checkStorage()
  54. local sugarcaneEmpty = true
  55. local pumpkinSeedsEmpty = true
  56.  
  57. local sugarcaneList = sugarcaneStorage.list()
  58. if sugarcaneList then
  59. for slot, item in pairs(sugarcaneList) do
  60. if item.name == "minecraft:sugar_cane" then
  61. sugarcaneEmpty = false
  62. break
  63. end
  64. end
  65. else
  66. print("sugarcaneStorage.list() is nil!")
  67. end
  68.  
  69. local pumpkinSeedList = pumpkinSeedStorage.list()
  70. if pumpkinSeedList then
  71. for slot, item in pairs(pumpkinSeedList) do
  72. if item.name == "minecraft:pumpkin_seeds" then
  73. pumpkinSeedsEmpty = false
  74. break
  75. end
  76. end
  77. else
  78. print("pumpkinSeedStorage.list() is nil!")
  79. end
  80.  
  81. return sugarcaneEmpty, pumpkinSeedsEmpty
  82. end
  83.  
  84. local function adjustGeneratorMotorSpeed()
  85. local totalBioFuelNeeded = getTotalBioFuelNeeded()
  86. 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
  87.  
  88. generatorMotor.setSpeed(motorSpeed)
  89. print("Total BioFuel Needed: " .. totalBioFuelNeeded)
  90. print("Generator Motor Speed: " .. motorSpeed)
  91. end
  92.  
  93. local function adjustBaselineRPM()
  94. local averagePercentage = getAverageBioFuelPercentage()
  95. if averagePercentage < 0.5 - adjustmentThreshold then
  96. baselineRPM = baselineRPM + adjustmentFactor * baselineRPM
  97. elseif averagePercentage > 0.5 + adjustmentThreshold then
  98. baselineRPM = baselineRPM - adjustmentFactor * baselineRPM
  99. end
  100. baselineRPM = math.max(baselineRPM, 100) -- Ensure baseline RPM does not go below 100
  101. print("Adjusted Baseline RPM: " .. baselineRPM)
  102. end
  103.  
  104. local totalConsumption = 0
  105. while true do
  106. sleep(1)
  107. local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  108.  
  109. if channel == 43 then
  110. -- power usage channel
  111. local identifier, powerUsage = message:match("^(%w+)%s+(%d+)$")
  112. if identifier == "BL1" then
  113. totalConsumption = tonumber(powerUsage)
  114. monitor.setCursorPos(1, 7)
  115. monitor.write("Birch Log Farm #1: " .. powerUsage .. " FE/t")
  116. end
  117. end
  118.  
  119. adjustGeneratorMotorSpeed()
  120. adjustBaselineRPM()
  121.  
  122. monitor.setCursorPos(1, 8)
  123. monitor.write("Biomass Generation #1: " .. generatorMotor.getEnergyConsumption() .. " FE/t")
  124. totalConsumption = totalConsumption + generatorMotor.getEnergyConsumption()
  125.  
  126. -- Display average biomass filled percentage and total energy generated
  127. local averagePercentage = getAverageBioFuelPercentage()
  128. local totalEnergy = getTotalEnergyGenerated()
  129. monitor.setTextColor(colors.green)
  130. monitor.setCursorPos(1, 1)
  131. monitor.write("Avg BioFuel %: " .. string.format("%.2f", averagePercentage * 100) .. "%")
  132. monitor.setCursorPos(1, 2)
  133. monitor.write("Generating: " .. totalEnergy .. " FE/t") -- the 246 is from the water wheels
  134. monitor.setCursorPos(1, 3)
  135. monitor.write("Free Energy: " .. totalEnergy - totalConsumption .. " FE/t")
  136. monitor.setCursorPos(1, 4)
  137. monitor.setTextColor(colors.lightGray)
  138. monitor.write("-----------------------------")
  139. monitor.setTextColor(colors.white)
  140.  
  141. -- Check storage and warn if empty
  142. local sugarcaneEmpty, pumpkinSeedsEmpty = checkStorage()
  143. monitor.setCursorPos(1, 5)
  144. if sugarcaneEmpty then
  145. monitor.setTextColor(colors.red)
  146. monitor.write("WARNING: Sugarcane storage is empty!")
  147. monitor.setTextColor(colors.white)
  148. else
  149. monitor.clearLine()
  150. end
  151.  
  152. monitor.setCursorPos(1, 6)
  153. if pumpkinSeedsEmpty then
  154. monitor.setTextColor(colors.red)
  155. monitor.write("WARNING: Pumpkin seed storage is empty!")
  156. monitor.setTextColor(colors.white)
  157. else
  158. monitor.clearLine()
  159. end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement