Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Blood Altar Turtle (LP-based crafting, serialized record)
- local RECORD_FILE = "altar_times_lp.txt"
- local itemTimesLP = {}
- local altar = peripheral.wrap("bottom") -- adjust if altar is elsewhere
- ---------------------------------------------------
- -- Record Load / Save
- ---------------------------------------------------
- local function loadRecord()
- if fs.exists(RECORD_FILE) then
- local file = fs.open(RECORD_FILE, "r")
- local content = file.readAll()
- file.close()
- local ok, data = pcall(textutils.unserialize, content)
- if ok and type(data) == "table" then
- itemTimesLP = data
- end
- end
- end
- local function saveRecord()
- local file = fs.open(RECORD_FILE, "w")
- file.writeLine(textutils.serialize(itemTimesLP))
- file.close()
- end
- ---------------------------------------------------
- -- Item Helpers
- ---------------------------------------------------
- -- Get item name for slot 1 (ignoring damage)
- local function getSlot1ItemId()
- local data = turtle.getItemDetail(1)
- if not data then return nil end
- return data.name
- end
- -- Prompt user for LP cost for unknown items
- local function promptForItemData(itemId)
- term.clear()
- term.setCursorPos(1,1)
- print("Blood Altar Turtle")
- print("-----------------")
- print("No record for item:", itemId)
- local lpCost
- repeat
- print("Enter LP cost to craft this item:")
- lpCost = tonumber(io.read())
- until lpCost
- itemTimesLP[itemId] = {lpCost = lpCost}
- saveRecord()
- return itemTimesLP[itemId]
- end
- -- Get current LP in altar
- local function getAltarLP()
- local tanks = altar.tanks()
- if #tanks > 0 then
- return tanks[1].amount
- end
- return 0
- end
- ---------------------------------------------------
- -- Crafting Logic
- ---------------------------------------------------
- local function processOne()
- if turtle.getItemCount(1) == 0 then return false end
- local itemId = getSlot1ItemId()
- if not itemId then return false end
- local data = itemTimesLP[itemId] or promptForItemData(itemId)
- -- Wait until enough LP to start crafting
- while getAltarLP() < data.lpCost do
- term.clear()
- term.setCursorPos(1,1)
- print("Waiting for LP...")
- print("Item:", itemId)
- print("Required LP:", data.lpCost)
- print("Current LP:", getAltarLP())
- sleep(5)
- end
- -- Record starting LP
- local startLP = getAltarLP()
- -- Drop item into altar
- turtle.select(1)
- while not turtle.dropDown(1) do
- print("Altar occupied. Waiting...")
- sleep(1)
- end
- turtle.select(16)
- -- Wait until LP used equals LP cost
- while getAltarLP() > startLP - data.lpCost do
- term.clear()
- term.setCursorPos(1,1)
- print("Blood Altar Turtle")
- print("-----------------")
- print("Processing item:", itemId)
- print("LP used:", startLP - getAltarLP(), "/", data.lpCost)
- sleep(1)
- end
- -- Pull finished item
- turtle.suckDown()
- -- Exit if slot 1 is empty
- if turtle.getItemCount(1) == 0 then
- term.clear()
- term.setCursorPos(1,1)
- print("No more items in slot 1. Exiting program.")
- error("Slot 1 empty, exiting.") -- stops the program
- end
- return true
- end
- ---------------------------------------------------
- -- Main Loop
- ---------------------------------------------------
- loadRecord()
- term.clear()
- term.setCursorPos(1,1)
- print("Blood Altar Turtle")
- print("-----------------")
- print("Put raw items in slot 1.")
- while true do
- if not processOne() then
- sleep(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment