Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- expected setup (from the side)
- -- G = cobble generator
- -- C = chest
- -- IT = item chest / crafty turtle facing I
- -- M = chest for storing minium stone (in first slot)
- -- prerequisites:
- -- computercraft, OpenPeripherals, EE3 (for minium)
- -- MANDATORY CONFIGURATION!!!!
- -- direction where turtle is located relative to the main item chest I
- -- TURTLE_DIRECTION() loaded from config file when first needed
- TURTLE_DIRECTION_CONFIG = nil
- function TURTLE_DIRECTION()
- if TURTLE_DIRECTION_CONFIG == nil then
- local h = fs.open("diacraft.config", "r")
- assert(h ~= nil, "Config file open failed. Write 'north', 'south', 'east' or 'west' into diacraft.config file")
- TURTLE_DIRECTION_CONFIG = h.readLine()
- h.close()
- assert(TURTLE_DIRECTION_CONFIG ~= nil, "Turtle direction not loaded")
- end
- return TURTLE_DIRECTION_CONFIG
- end
- PROGNAME_UPDATE = "diacraft.update"
- PROGNAME_BACKUP = "diacraft.backup"
- PROGNAME = "diacraft"
- PASTEBIN_CODE = "uUSD1U4k"
- --Algorithm:
- --4x Cobble => Flint
- --4x Flint => Clay
- --4x Clay => Clay Block
- --4x Clay Block => Iron ingot
- --8x Iron ingot => Gold
- --4x Gold => Diamond
- -- define metatable for items - we want readable to string
- function item(id, meta)
- local i = {id = id,
- meta = meta,
- metaIgnore = false,
- minCount = 4,
- miniumPatternId = 4
- }
- return i
- end
- -- Required input:
- ITEM = {}
- local itemmt = {__index = function(table, key) error("Unexisting key: "..key) end}
- setmetatable(ITEM,itemmt)
- ------------------------ Farm -----------------
- ITEM.COBBLE = item(4, 0)
- ITEM.FLINT = item(318, 0)
- ITEM.CLAY = item(337, 0)
- ITEM.CLAYBLOCK = item(82, 0)
- ITEM.IRON_INGOT = item(265, 0)
- ITEM.IRON_INGOT.minCount = 8
- ITEM.IRON_INGOT.miniumPatternId = 8
- ITEM.GOLD_INGOT = item(266, 0)
- ITEM.DIAMOND = item(264, 0)
- ITEM.MINIUM_STONE = item(27002,0)
- ITEM.MINIUM_STONE.metaIgnore = true
- local craftingOrder = {ITEM.COBBLE, ITEM.FLINT, ITEM.CLAY, ITEM.CLAYBLOCK, ITEM.IRON_INGOT, ITEM.GOLD_INGOT, ITEM.DIAMOND}
- for i=1,#craftingOrder - 1 do
- craftingOrder[i+1].prevOne = craftingOrder[i]
- craftingOrder[i].nextOne = craftingOrder[i+1]
- end
- for _, item in pairs(ITEM) do
- item.registered = true
- end
- function itemEquals(item, item2)
- --print ("Item1: id="..item.id..", meta="..item.meta)
- --print ("Item2: id="..item2.id..", meta="..item2.meta)
- -- if first item is not registered, then work with second item as registered
- -- if none is registered, it doesn't matter... but ignoreMeta won't work
- if not(item.registered) then
- item, item2 = item2, item
- end
- if (item.id == item2.id and (item.ignoreMeta or (item.meta == item2.meta))) then
- return true
- else
- return false
- end
- end
- function findItem(id, meta)
- assert(id ~= nil, "Id is nil")
- assert(meta ~= nil, "Meta is nil")
- --print("Looking for id="..id..", meta="..meta)
- local lookingFor = item(id, meta)
- for _,item in pairs(ITEM) do
- --print("Comparing ITEM id="..item.id..", meta="..item.meta)
- if itemEquals(item, lookingFor) then
- return item
- end
- end
- return nil
- end
- local itemChest = peripheral.wrap("front")
- local cobbleChest = peripheral.wrap("top")
- local miniumChest = peripheral.wrap("bottom")
- function craftWithMinium(index, craftCount, miniumPatternId)
- if miniumPatternId == 4 then
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 1)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 4)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 8)
- elseif miniumPatternId == 8 then
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 1)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 2)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 4)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 6)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 8)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 9)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 10)
- else
- error("Unkonwn pattern ID:"..miniumPatternId)
- end
- turtle.select(12)
- for i = 1,craftCount do
- turtle.craft()
- turtle.drop()
- end
- end
- function craftSquare(index, craftCount)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 0)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 1)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 4)
- itemChest.pushIntoSlot(TURTLE_DIRECTION(), index,craftCount, 5)
- turtle.select(12)
- for i = 1,craftCount do
- turtle.craft()
- turtle.drop()
- end
- end
- function craftWithItem(index)
- local slot = itemChest.getStackInSlot(index)
- local originalItem
- originalItem = findItem(slot.id, slot.dmg)
- local needMinium = not(itemEquals(ITEM.CLAY, item(slot.id, slot.dmg)))
- if needMinium then
- assert(miniumChest.pushIntoSlot("up", 0, 1, 0) == 1, "minium grab failed")
- end
- while(slot ~= nil and slot.qty >= originalItem.minCount and itemEquals(originalItem, item(slot.id, slot.dmg))) do
- local craftCount = math.floor(slot.qty / originalItem.minCount)
- if needMinium then
- craftWithMinium(index, craftCount, originalItem.miniumPatternId)
- else
- craftSquare(index, craftCount)
- end
- for turtleSlotNum = 2, 12 do
- turtle.select(turtleSlotNum)
- turtle.drop()
- end
- itemChest.condense()
- slot = itemChest.getStackInSlot(index)
- end
- if needMinium then
- assert(miniumChest.pullIntoSlot("up", 0, 1, 0) == 1, "minium return failed")
- end
- end
- function iterateOverChest()
- -- OpenPeripherals index from 0
- for i = 0,itemChest.getSizeInventory()-1 do
- local slot = itemChest.getStackInSlot(i)
- if slot ~= nil then
- print("Slot "..i.." contains item: "..slot.name)
- local item = findItem(slot.id, slot.dmg)
- if (item ~= nil and item.nextOne ~= nil and slot.qty >= item.minCount) then
- craftWithItem(i)
- end
- end
- end
- end
- function update()
- fs.delete(PROGNAME_UPDATE)
- shell.run("pastebin","get",PASTEBIN_CODE, PROGNAME_UPDATE)
- if fs.exists(PROGNAME_UPDATE) then
- fs.delete(PROGNAME_BACKUP)
- fs.move(PROGNAME,PROGNAME_BACKUP)
- fs.move(PROGNAME_UPDATE, PROGNAME)
- end
- end
- local argv = {...}
- if #argv == 1 and argv[1] == "update" then
- update()
- error()
- end
- local slot = miniumChest.getStackInSlot(0)
- assert(slot ~= nil and itemEquals(ITEM.MINIUM_STONE, item(slot.id, slot.dmg)) == true, "Chest below us must contain minium stone")
- while(true) do
- cobbleChest.condense()
- turtle.select(13)
- turtle.suckUp()
- turtle.drop()
- itemChest.condense()
- iterateOverChest()
- sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement