Advertisement
Hystel

birch logs cc

Nov 23rd, 2024 (edited)
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. local motor = peripheral.wrap("electric_motor_1")
  2. local storage = peripheral.wrap("inventory_1")
  3. local modem = peripheral.find("modem") or error("No modem attached", 0)
  4. modem.open(43)
  5. modem.open(45)
  6. local totalLogs = 0
  7. local previousLogs = 0
  8. local previousTime = os.clock()
  9. local logConsumptionRates = {}
  10. local maxSamples = 10 -- Number of samples for moving average
  11. local isShutdown = false
  12. print("Thinking of Woo. Just Woo. Just Woo..")
  13.  
  14. -- Function to calculate motor speed based on log consumption rate
  15. local function adjustMotorSpeed()
  16.     local currentTime = os.clock()
  17.     local timeElapsed = currentTime - previousTime
  18.  
  19.     -- Calculate the current total logs
  20.     totalLogs = 0
  21.     for slot, item in pairs(storage.list()) do
  22.         if (item.name == "minecraft:birch_log") then
  23.             totalLogs = totalLogs + item.count
  24.         end
  25.     end
  26.  
  27.     -- Calculate the rate of log consumption
  28.     local logConsumptionRate = (previousLogs - totalLogs) / timeElapsed
  29.  
  30.     -- Add the current rate to the list of rates
  31.     table.insert(logConsumptionRates, logConsumptionRate)
  32.     if #logConsumptionRates > maxSamples then
  33.         table.remove(logConsumptionRates, 1)
  34.     end
  35.  
  36.     -- Calculate the moving average of the log consumption rates
  37.     local sum = 0
  38.     for _, rate in ipairs(logConsumptionRates) do
  39.         sum = sum + rate
  40.     end
  41.     local averageLogConsumptionRate = sum / #logConsumptionRates
  42.  
  43.     -- Adjust motor speed based on the average log consumption rate
  44.     local motorSpeed = math.min(math.max(averageLogConsumptionRate * 10, 0), 256) -- Scale and clamp the speed between 0 and 256
  45.  
  46.     -- Ensure motor speed increases if logs are below 3000
  47.     if totalLogs < 3000 then
  48.         motorSpeed = math.min(motorSpeed + 10, 256)
  49.     end
  50.  
  51.     modem.transmit(43, 43, "BL1 " .. motor.getEnergyConsumption())
  52.     -- Print debug information
  53.     print("Current Time: " .. currentTime)
  54.     print("Time Elapsed: " .. timeElapsed)
  55.     print("Total Logs: " .. totalLogs)
  56.     print("Previous Logs: " .. previousLogs)
  57.     print("Log Consumption Rate: " .. logConsumptionRate)
  58.     print("Average Log Consumption Rate: " .. averageLogConsumptionRate)
  59.     print("Motor Speed: " .. motorSpeed)
  60.  
  61.     motor.setSpeed(motorSpeed)
  62.  
  63.     -- Update previous logs and time
  64.     previousLogs = totalLogs
  65.     previousTime = currentTime
  66. end
  67.  
  68. -- Initial log count
  69. for slot, item in pairs(storage.list()) do
  70.     if (item.name == "minecraft:birch_log") then
  71.         totalLogs = totalLogs + item.count
  72.     end
  73. end
  74. previousLogs = totalLogs
  75.  
  76. -- Function to handle modem messages
  77. local function handleModemMessages()
  78.     while true do
  79.         local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  80.         if channel == 45 then
  81.             if message == "SHUTDOWN" then
  82.                 isShutdown = true
  83.             elseif message == "OPERATE" then
  84.                 isShutdown = false
  85.             end
  86.         end
  87.     end
  88. end
  89.  
  90. -- Main loop to adjust motor speed
  91. local function adjustMotorSpeedLoop()
  92.     while true do
  93.         if not isShutdown then
  94.             adjustMotorSpeed()
  95.         end
  96.         -- Wait for a short period before the next update
  97.         sleep(0.1)
  98.     end
  99. end
  100.  
  101. -- Run both functions concurrently
  102. parallel.waitForAny(handleModemMessages, adjustMotorSpeedLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement