Advertisement
_DudeWhat_

Daytime Keeper

Jan 7th, 2022 (edited)
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. local sleepminutes = 2
  2. local rituals = { sunrise = { tablet = "ars_nouveau:ritual_sunrise", name = "Ritual of Sunrise", augments = {} } -- augments not implemented
  3.                 , moonfall = { tablet = "ars_nouveau:ritual_moonfall", name = "Ritual of Moonfall", augments = {} }
  4.                 }
  5. local automata = peripheral.find("weakAutomata")
  6.  
  7. local function isItemSelected(item, name)
  8.     if not item then
  9.         return name == nil
  10.     else
  11.         return item.name == name
  12.     end
  13. end
  14.  
  15. local function selectItem(name)
  16.     if isItemSelected(turtle.getItemDetail(), name) then return true end
  17.     for slot=1,16 do
  18.         turtle.select(slot)
  19.         if isItemSelected(turtle.getItemDetail(), name) then return true end
  20.     end
  21.        
  22.     -- Additionally check external inventories
  23.     --print("Not found internally! Looking in external inventories...")
  24.     --for _, inv in pairs({ peripheral.find("inventory") }) do
  25.     --  print("Found inventory
  26.     --  for slot, item in pairs(inv.list()) do
  27.     --      if isItemSelected(item, name)) then
  28.     --          -- select free slot
  29.     --          requestItem(nil)
  30.     --          local transferred = inv.pullItems(peripheral.getName(inv), slot, nil, turtle.getSelectedSlot())
  31.     --          if transferred > 0 then return true end
  32.     --      end
  33.     --  end
  34.     --end
  35.     return false
  36. end
  37.  
  38. local function requestItem(name)
  39.     while true do
  40.         if selectItem(name) then return true end
  41.         if not name then
  42.             printError("Please make one slot empty!")
  43.         else
  44.             printError(name, " was not found. Give me moar!")
  45.         end
  46.         os.pullEvent("turtle_inventory")
  47.     end
  48. end
  49.  
  50. local function ensureOff(side)
  51.     if rs.getOutput(side) then
  52.         print("Oops, the output was still one before pulse!")
  53.         rs.setOutput(side, false)
  54.         sleep(0.5)
  55.     end
  56. end
  57.  
  58. local function redstonePulse(side, length)
  59.     if not length then length = 0.5 end
  60.     ensureOff(side)
  61.     rs.setOutput(side, true)
  62.     sleep(length)
  63.     rs.setOutput(side, false)
  64. end
  65.  
  66. local function ensureFuel(minAmount)
  67.     if not minAmount then minAmount = 1 end
  68.     while turtle.getFuelLevel() < minAmount do
  69.         requestItem("minecraft:coal")
  70.         local success, reason = turtle.refuel()
  71.         if not success then printError("Refueling failed:", reason) end
  72.     end
  73. end
  74.  
  75. local function performRitual(ritual)
  76.     print("Performing", ritual.name)
  77.     requestItem(ritual.tablet)
  78.     turtle.place()
  79.     if not automata then
  80.         redstonePulse("bottom")
  81.     else
  82.         requestItem(nil) -- request empty hand
  83.         ensureFuel()
  84.         automata.useOnBlock()
  85.     end
  86. end
  87.  
  88. local function makeDay()
  89.     performRitual(rituals.sunrise)
  90. end
  91.  
  92. local function isNight()
  93.     darkness = rs.getAnalogInput("top")
  94.     return darkness > 3
  95. end
  96.  
  97. local function waitForNight()
  98.     while true do
  99.         os.pullEvent("redstone")
  100.         if isNight() then break end
  101.         print("Still not night...")
  102.     end
  103. end
  104.  
  105. local function main()
  106.     while true do
  107.         print("Waiting for nighttime!")
  108.         waitForNight()
  109.         makeDay()
  110.         print("Sleeping for", sleepminutes, "minutes!")
  111.         sleep(sleepminutes * 60)
  112.     end
  113. end
  114.  
  115. print("Ars Nouveau Daylight Keeper")
  116. print("Place computer in front of a Ritual Brazier.")
  117. print("Place an inverted daylight sensor on top of the computer.")
  118. if not automata then
  119.     print("Place an interaction spell turret facing the brazier with redstone signal on the bottom side!")
  120. end
  121. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement