legg0028

blood_altar

Aug 14th, 2025 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. -- Blood Altar Turtle (LP-based crafting, serialized record)
  2. local RECORD_FILE = "altar_times_lp.txt"
  3. local itemTimesLP = {}
  4.  
  5. local altar = peripheral.wrap("bottom") -- adjust if altar is elsewhere
  6.  
  7. ---------------------------------------------------
  8. -- Record Load / Save
  9. ---------------------------------------------------
  10. local function loadRecord()
  11.     if fs.exists(RECORD_FILE) then
  12.         local file = fs.open(RECORD_FILE, "r")
  13.         local content = file.readAll()
  14.         file.close()
  15.         local ok, data = pcall(textutils.unserialize, content)
  16.         if ok and type(data) == "table" then
  17.             itemTimesLP = data
  18.         end
  19.     end
  20. end
  21.  
  22. local function saveRecord()
  23.     local file = fs.open(RECORD_FILE, "w")
  24.     file.writeLine(textutils.serialize(itemTimesLP))
  25.     file.close()
  26. end
  27.  
  28. ---------------------------------------------------
  29. -- Item Helpers
  30. ---------------------------------------------------
  31. -- Get item name for slot 1 (ignoring damage)
  32. local function getSlot1ItemId()
  33.     local data = turtle.getItemDetail(1)
  34.     if not data then return nil end
  35.     return data.name
  36. end
  37.  
  38. -- Prompt user for LP cost for unknown items
  39. local function promptForItemData(itemId)
  40.     term.clear()
  41.     term.setCursorPos(1,1)
  42.     print("Blood Altar Turtle")
  43.     print("-----------------")
  44.     print("No record for item:", itemId)
  45.  
  46.     local lpCost
  47.     repeat
  48.         print("Enter LP cost to craft this item:")
  49.         lpCost = tonumber(io.read())
  50.     until lpCost
  51.  
  52.     itemTimesLP[itemId] = {lpCost = lpCost}
  53.     saveRecord()
  54.     return itemTimesLP[itemId]
  55. end
  56.  
  57. -- Get current LP in altar
  58. local function getAltarLP()
  59.     local tanks = altar.tanks()
  60.     if #tanks > 0 then
  61.         return tanks[1].amount
  62.     end
  63.     return 0
  64. end
  65.  
  66. ---------------------------------------------------
  67. -- Crafting Logic
  68. ---------------------------------------------------
  69. local function processOne()
  70.     if turtle.getItemCount(1) == 0 then return false end
  71.  
  72.     local itemId = getSlot1ItemId()
  73.     if not itemId then return false end
  74.  
  75.     local data = itemTimesLP[itemId] or promptForItemData(itemId)
  76.  
  77.     -- Wait until enough LP to start crafting
  78.     while getAltarLP() < data.lpCost do
  79.         term.clear()
  80.         term.setCursorPos(1,1)
  81.         print("Waiting for LP...")
  82.         print("Item:", itemId)
  83.         print("Required LP:", data.lpCost)
  84.         print("Current LP:", getAltarLP())
  85.         sleep(5)
  86.     end
  87.  
  88.     -- Record starting LP
  89.     local startLP = getAltarLP()
  90.  
  91.     -- Drop item into altar
  92.     turtle.select(1)
  93.     while not turtle.dropDown(1) do
  94.         print("Altar occupied. Waiting...")
  95.         sleep(1)
  96.     end
  97.     turtle.select(16)
  98.  
  99.     -- Wait until LP used equals LP cost
  100.     while getAltarLP() > startLP - data.lpCost do
  101.         term.clear()
  102.         term.setCursorPos(1,1)
  103.         print("Blood Altar Turtle")
  104.         print("-----------------")
  105.         print("Processing item:", itemId)
  106.         print("LP used:", startLP - getAltarLP(), "/", data.lpCost)
  107.         sleep(1)
  108.     end
  109.  
  110.     -- Pull finished item
  111.     turtle.suckDown()
  112.  
  113.     -- Exit if slot 1 is empty
  114.     if turtle.getItemCount(1) == 0 then
  115.         term.clear()
  116.         term.setCursorPos(1,1)
  117.         print("No more items in slot 1. Exiting program.")
  118.         error("Slot 1 empty, exiting.")  -- stops the program
  119.     end
  120.  
  121.     return true
  122. end
  123.  
  124. ---------------------------------------------------
  125. -- Main Loop
  126. ---------------------------------------------------
  127. loadRecord()
  128. term.clear()
  129. term.setCursorPos(1,1)
  130. print("Blood Altar Turtle")
  131. print("-----------------")
  132. print("Put raw items in slot 1.")
  133.  
  134. while true do
  135.     if not processOne() then
  136.         sleep(1)
  137.     end
  138. end
  139.  
Advertisement
Add Comment
Please, Sign In to add comment