Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --jxwQsiHD
- --(multi) ME Level Emitter using turtle
- --supports custom functions on high or low level, compress cobble is implemented
- --optional different id not a very userful option since it doesnt know how much to drop how often, craft would work better
- local config = {}
- local turtlefacing = "south" --must be set to turtles facing dir
- local meside = "south" --must be set to me controller side, should probably be same
- local polltimer = 0.1 --you can set this very low to compress maximum cobble, higher to reduce me system crash
- local function setup()
- -- you must edit this function to change your configuration
- -- {"ItemIDToMeasure", "> or <", ThresholdAmount, FunctionToCall, "DropSide/Arg1", "OptionalDifferentItemIDToExport"}
- -- valid sides are north, south, east, west, up, and down
- config[1] = {"3:0", ">", 64, dropItemToSide, "up" }
- config[2] = {"14276:9", "<", 64, dropItemToSide, "north", "8565:0" }
- config[3] = {"297:0", "<", 64, craftItem, nil, "297:0" }
- config[4] = {"4:0", ">", 96, compress } --compress cobble
- config[5] = {"2506:0", ">", 8, compress }
- config[6] = {"2506:1", ">", 8, compress }
- config[7] = {"2506:2", ">", 8, compress }
- config[8] = {"2506:3", ">", 8, compress }
- config[9] = {"2506:4", ">", 8, compress }
- config[10] = {"2506:5", ">", 8, compress }
- config[11] = {"2506:6", ">", 8, compress } --up to octuple
- -- turtle needs to be next to ME Controller to use craftItem
- -- crafting turtle required to use compress() for compressed cobblestone
- end
- local me
- local mepull
- local count = 0
- local stack = {}
- function dropItemToSide(info, count)
- local maxreps = 10
- local chest = peripheral.wrap(info.side)
- if(chest == nil) then print("Failed to wrap inventory side " .. info.side) end
- local size = chest.getInventorySize()
- local stacks = chest.getAllStacks()
- local stacksize
- stack.id = info.id2
- stack.dmg = info.dmg2
- stack.qty = me.countOfItemType(stack.id, stack.dmg)
- if count < stack.qty then stack.qty = count end
- while (stack.qty > 0) and (maxreps > 0) do
- maxreps = maxreps - 1
- stacksize = me.extractItem(stack, mepull)
- if stacksize == nil or stacksize < 1 then
- break
- end
- stack.qty = stack.qty - stacksize
- for slot = 1, size do
- if stacks[slot] == nil or (stacks[slot].id == stack.id and stacks[slot].dmg == stack.dmg) then
- stacksize = stacksize - chest.pullItem(info.pull, 1, stacksize, slot)
- if stacksize <= 0 then
- break
- end
- end
- end
- if stacksize ~= 0 then
- break
- end
- end
- returnInventory()
- end
- function craftItem(info, count)
- stack.id = info.id2
- stack.dmg = info.dmg2
- stack.qty = count
- local jobs = me.getJobList()
- for k, v in pairs(jobs) do
- if v.id == stack.id and v.dmg == stack.dmg then
- stack.qty = stack.qty - v.qty
- end
- end
- me.requestCrafting(stack)
- end
- function returnInventory()
- print("Returning Inventory To ME")
- for slot = 1, 16 do
- if turtle.getItemCount(slot) > 0 then
- me.insertItem(slot, 64, mepull)
- end
- end
- end
- local craftingSlot = {1,2,3,5,6,7,9,10,11}
- function compressSmall(info, count)
- if count < 9 then return end
- if count > 63 then count = 63 end
- count = math.floor(count / 9)
- stack.id = info.id2
- stack.dmg = info.dmg2
- stack.qty = count * 9
- me.extractItem(stack, mepull)
- for slot = 2, 9 do
- turtle.transferTo(craftingSlot[slot], count)
- end
- turtle.craft()
- returnInventory()
- end
- function compress(info, count)
- if count < 704 then
- compressSmall(info, count)
- end
- stack.id = info.id2
- stack.dmg = info.dmg2
- stack.qty = 64
- local maxreps = 10
- while (count >= 704) and (maxreps > 0) do
- maxreps = maxreps - 1
- for slot = 1, 11 do
- me.extractItem(stack, mepull)
- end
- me.insertItem(4, 64, mepull)
- me.insertItem(8, 64, mepull)
- turtle.craft()
- me.insertItem(1, 64, mepull)
- count = count - 576
- end
- end
- local direction = {[0]="east", [1]="south", [2]="west", [3]="north"}
- local side = {[0]="front", [1]="right", [2]="back", [3]="left"}
- local function sideToDirection(side)
- if side == "front" then
- return direction[turtlefacing%4]
- end
- if side == "right" then
- return direction[(turtlefacing+1)%4]
- end
- if side == "back" then
- return direction[(turtlefacing+2)%4]
- end
- if side == "left" then
- return direction[(turtlefacing+3)%4]
- end
- if side == "top" then
- return "up"
- end
- if side == "bottom" then
- return "down"
- end
- return side
- end
- local function directionToSide(dir)
- if dir == "east" then
- return side[(-turtlefacing+4)%4]
- end
- if dir == "south" then
- return side[(-turtlefacing+5)%4]
- end
- if dir == "west" then
- return side[(-turtlefacing+6)%4]
- end
- if dir == "north" then
- return side[(-turtlefacing+7)%4]
- end
- if dir == "up" then
- return "top"
- end
- if dir == "down" then
- return "bottom"
- end
- return dir
- end
- local function negateDirection(dir)
- if dir == "east" then return "west" end
- if dir == "west" then return "east" end
- if dir == "north" then return "south" end
- if dir == "south" then return "north" end
- if dir == "up" then return "down" end
- if dir == "down" then return "up" end
- return dir
- end
- local function processConfig()
- local id, dmg
- local pretty = {}
- if turtlefacing == "east" then turtlefacing = 0 end
- if turtlefacing == "south" then turtlefacing = 1 end
- if turtlefacing == "west" then turtlefacing = 2 end
- if turtlefacing == "north" then turtlefacing = 3 end
- mepull = negateDirection(meside)
- meside = directionToSide(meside)
- for k, v in pairs(config) do
- print("Processing config item " .. k)
- pretty[k] = {}
- pretty[k].id1, pretty[k].dmg1 = string.match(v[1], "([0-9-]+):([0-9-]+)")
- pretty[k].compare = v[2]
- pretty[k].threshold = v[3]
- pretty[k].func = v[4]
- pretty[k].pull = negateDirection(v[5])
- pretty[k].side = directionToSide(v[5])
- if v[6] then
- pretty[k].id2, pretty[k].dmg2 = string.match(v[6], "([0-9-]+):([0-9-]+)")
- else
- pretty[k].id2 = pretty[k].id1
- pretty[k].dmg2 = pretty[k].dmg1
- end
- pretty[k].id1 = tonumber(pretty[k].id1)
- pretty[k].dmg1 = tonumber(pretty[k].dmg1)
- pretty[k].id2 = tonumber(pretty[k].id2)
- pretty[k].dmg2 = tonumber(pretty[k].dmg2)
- if (not pretty[k].id1) or (not pretty[k].dmg1) then
- print("Failed getting ItemID:Metadata on item " .. k)
- pretty[k] = nil
- else
- end
- end
- config = pretty
- end
- print("Running Setup")
- setup()
- print("Processing Config")
- processConfig()
- me = peripheral.wrap(meside)
- if me == nil then
- print("ME Controller Not Found")
- return
- end
- turtle.select(1)
- returnInventory()
- local hitthreshold = "false"
- while true do
- timeradjustment = 0
- for k, v in pairs(config) do
- print("Processing Item: " .. k)
- hitthreshold = "false"
- count = me.countOfItemType(v.id1, v.dmg1)
- if v.compare == ">" then
- if count > v.threshold then
- hitthreshold = "true"
- v.func(v, count - v.threshold)
- end
- else
- if count < v.threshold then
- hitthreshold = "true"
- v.func(v, v.threshold - count)
- end
- end
- print(v.id1 .. ":" .. v.dmg1 .. " * " .. count .. " " .. v.compare .. " " .. v.threshold .. " = " .. hitthreshold)
- end
- sleep(polltimer)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement