View difference between Paste ID: uEaRpsdV and jxwQsiHD
SHOW: | | - or go back to the newest paste.
1-
            --Zi55cb9y
1+
            --jxwQsiHD
2
            --(multi) ME Level Emitter using turtle
3
            --supports custom functions on high or low level, compress cobble is implemented
4
	    --optional different id not a very userful option since it doesnt know how much to drop how often, craft would work better
5
6
7
            local config = {}
8
            local turtlefacing = "south"
9
            local meside = "south"
10
            local polltimer = 5
11-
            local compressadjustment = 0.6
11+
12-
            local droptosideadjustment = 0.4
12+
13
                    -- edit this function to change your configuration
14
                    -- {"ItemIDToMeasure", "> or <", ThresholdAmount, FunctionToCall, "DropSide/Arg1", "OptionalDifferentItemIDToExport"}
15
                    -- valid sides are north, south, east, west, up, and down
16
                           
17
                     
18
                    config[1] = {"3:0",      ">",  64, dropItemToSide, "up"                    }
19
                    config[2] = {"14276:9",  "<",  64, dropItemToSide, "north",   "8565:0"     }
20
                    config[3] = {"297:0",    "<",  64, craftItem,      nil,       "297:0"      } 
21
                    config[4] = {"4:0",      ">",  96, compress                                }  --compress cobble
22
                    config[5] = {"2506:0",   ">",  8, compress                                 }
23
                    config[6] = {"2506:1",   ">",  8, compress                                 }
24-
                    config[4] = {"2506:0",   ">",  8, compress                                 }
24+
                    config[7] = {"2506:2",   ">",  8, compress                                 }
25-
                    config[4] = {"2506:1",   ">",  8, compress                                 }
25+
                    config[8] = {"2506:3",   ">",  8, compress                                 }
26-
                    config[4] = {"2506:2",   ">",  8, compress                                 }
26+
                    config[9] = {"2506:4",   ">",  8, compress                                 }
27-
                    config[4] = {"2506:3",   ">",  8, compress                                 }
27+
                    config[10] = {"2506:5",   ">",  8, compress                                 }
28-
                    config[4] = {"2506:4",   ">",  8, compress                                 }
28+
                    config[11] = {"2506:6",   ">",  8, compress                                 }  --up to octuple
29-
                    config[4] = {"2506:5",   ">",  8, compress                                 }
29+
30-
                    config[4] = {"2506:6",   ">",  8, compress                                 }  --up to octuple
30+
31
                    -- crafting turtle required to use compress() for compressed cobblestone
32
            end
33
                     
34
                     
35
            local me      
36
            local mepull
37
            local count = 0
38
            local stack = {}
39-
            local timeradjustment = 0
39+
40
                     
41
            function dropItemToSide(info, count)                     
42
                    local maxreps = 10
43
                    local chest = peripheral.wrap(info.side)
44-
            function dropItemToSide(info, count)
44+
45
                    local size = chest.getInventorySize()
46
                    local stacks = chest.getAllStacks()
47
                    local stacksize
48
49
                    stack.id = info.id2
50
                    stack.dmg = info.dmg2
51
                    stack.qty = me.countOfItemType(stack.id, stack.dmg)
52
                    if count < stack.qty then stack.qty = count end
53
                 
54
                    while (stack.qty > 0) and (maxreps > 0) do
55
                        maxreps = maxreps - 1
56
			stacksize = me.extractItem(stack, mepull)
57-
                    while (stack.qty > 0) and (polltimer - timeradjustment > 1) do
57+
58
                           break
59
                        end
60
			stack.qty = stack.qty - stacksize
61
                        for slot = 1, size do
62
                            if stacks[slot] == nil or (stacks[slot].id == stack.id and stacks[slot].dmg == stack.dmg) then
63
                                    stacksize = stacksize - chest.pullItem(info.pull, 1, stacksize, slot)
64
                                    if stacksize  <= 0 then
65
                                            break
66
                                    end
67
                            end
68
                        end
69
                        if stacksize ~= 0 then
70
                           break
71
                        end
72
                    end
73
                    returnInventory()
74-
                        timeradjustment = timeradjustment + droptosideadjustment
74+
75
                         
76
            function craftItem(info, count)
77
                    stack.id = info.id2
78
                    stack.dmg = info.dmg2
79
                    stack.qty = count
80
                     
81
                    local jobs = me.getJobList()
82
                    for k, v in pairs(jobs) do
83
                            if v.id == stack.id and v.dmg == stack.dmg then
84
                                    stack.qty = stack.qty - v.qty
85
                            end
86
                    end
87
                     
88
                    me.requestCrafting(stack)
89
            end
90
                         
91
            function returnInventory()
92
                        print("Returning Inventory To ME")
93
                    for slot = 1, 16 do
94
                            if turtle.getItemCount(slot) > 0 then
95
                                    me.insertItem(slot, 64, mepull)
96
                            end
97
                    end
98
            end
99
                     
100
            local craftingSlot = {1,2,3,5,6,7,9,10,11}
101
            function compressSmall(info, count)
102
                    if count < 9 then return end
103
                    if count > 63 then count = 63 end    
104
                    count = math.floor(count / 9)
105
                    stack.id = info.id2
106
                    stack.dmg = info.dmg2
107
                    stack.qty = count * 9
108
                         
109
                    me.extractItem(stack, mepull)
110
                    for slot = 2, 9 do
111
                    turtle.transferTo(craftingSlot[slot], count)
112
                    end
113
                    turtle.craft()
114
                    returnInventory()
115
            end
116
                         
117
            function compress(info, count)
118
                    if count < 704 then
119
                            compressSmall(info, count)
120
                    end    
121
                    stack.id = info.id2
122
                    stack.dmg = info.dmg2
123
                    stack.qty = 64
124
125
                    local maxreps = 10
126
                     
127
                    while (count >= 704) and (maxreps > 0) do
128-
                    while (count >= 704) and (polltimer - timeradjustment > 1) do
128+
                            maxreps = maxreps - 1
129
                            for slot = 1, 11 do
130
                                    me.extractItem(stack, mepull)
131
                            end
132
                            me.insertItem(4, 64, mepull)
133
                            me.insertItem(8, 64, mepull)
134
                            turtle.craft()
135
                            me.insertItem(1, 64, mepull)
136
                            count = count - 576            
137
                    end
138-
                            timeradjustment = timeradjustment + compressadjustment
138+
139
             
140
            local direction = {[0]="east", [1]="south", [2]="west", [3]="north"}
141
            local side = {[0]="front", [1]="right", [2]="back", [3]="left"}
142
             
143
            local function sideToDirection(side)
144
                if side == "front" then
145
                            return direction[turtlefacing%4]
146
                end
147
                if side == "right" then
148
                            return direction[(turtlefacing+1)%4]
149
                end
150
                if side == "back" then
151
                            return direction[(turtlefacing+2)%4]
152
                end
153
                if side == "left" then
154
                            return direction[(turtlefacing+3)%4]
155
                end
156
                if side == "top" then
157
                            return "up"
158
                end
159
                if side == "bottom" then
160
                            return "down"
161
                end
162
                return side
163
            end
164
             
165
            local function directionToSide(dir)
166
                if dir == "east" then
167
                            return side[(-turtlefacing+4)%4]
168
                end
169
                if dir == "south" then
170
                            return side[(-turtlefacing+5)%4]
171
                end
172
                if dir == "west" then
173
                            return side[(-turtlefacing+6)%4]
174
                end
175
                if dir == "north" then
176
                            return side[(-turtlefacing+7)%4]
177
                end
178
                if dir == "up" then
179
                            return "top"
180
                end
181
                if dir == "down" then
182
                            return "bottom"
183
                end
184
                return dir
185
            end
186
             
187
            local function negateDirection(dir)
188
             
189
                if dir == "east"   then return "west"  end
190
                if dir == "west"   then return "east"  end
191
                if dir == "north"  then return "south" end
192
                if dir == "south"  then return "north" end
193
                if dir == "up"     then return "down"  end
194
                if dir == "down"   then return "up"    end
195
                    return dir
196
             
197
            end
198
                   
199
            local function processConfig()
200
                    local id, dmg
201
                    local pretty = {}
202
                           
203
                    if turtlefacing == "east" then turtlefacing = 0 end
204
                    if turtlefacing == "south" then turtlefacing = 1 end
205
                    if turtlefacing == "west" then turtlefacing = 2 end
206
                    if turtlefacing == "north" then turtlefacing = 3 end
207
                     
208
                            mepull = negateDirection(meside)
209
                            meside = directionToSide(meside)
210
                               
211
                    for k, v in pairs(config) do
212
                            print("Processing config item " .. k)
213
                            pretty[k] = {}
214
                     
215
                            pretty[k].id1, pretty[k].dmg1 = string.match(v[1], "([0-9-]+):([0-9-]+)")
216
                     
217
                            pretty[k].compare = v[2]
218
                            pretty[k].threshold = v[3]
219
                            pretty[k].func = v[4]
220
                     
221
                                            pretty[k].pull = negateDirection(v[5])
222
                            pretty[k].side = directionToSide(v[5])
223
                   
224
                            if v[6] then
225
                                    pretty[k].id2, pretty[k].dmg2 = string.match(v[6], "([0-9-]+):([0-9-]+)")
226
                            else
227
                                    pretty[k].id2 = pretty[k].id1
228
                                    pretty[k].dmg2 = pretty[k].dmg1
229
                            end
230
                     
231
                            pretty[k].id1 = tonumber(pretty[k].id1)
232
                            pretty[k].dmg1 = tonumber(pretty[k].dmg1)
233
                            pretty[k].id2 = tonumber(pretty[k].id2)
234
                            pretty[k].dmg2 = tonumber(pretty[k].dmg2)
235
236
                            if (not pretty[k].id1) or (not pretty[k].dmg1) then
237
                                    print("Failed getting ItemID:Metadata on item " .. k)
238
                                    pretty[k] = nil
239
                            else
240
241
                            end
242
                    end
243
                         
244
                    config = pretty
245
            end
246
                     
247
            print("Running Setup")
248
            setup()
249
            print("Processing Config")
250
            processConfig()
251
                 
252
            me = peripheral.wrap(meside)
253
            if me == nil then
254
                print("ME Controller Not Found")
255
                return
256
            end
257
                 
258
            turtle.select(1)
259
            returnInventory()
260
                     
261
            local hitthreshold = "false"
262
            while true do
263
                    timeradjustment = 0
264
                    for k, v in pairs(config) do
265
                                    print("Processing Item: " .. k)
266
                                    hitthreshold = "false"
267
                                    count = me.countOfItemType(v.id1, v.dmg1)
268
269
                                    if v.compare == ">" then
270
                                            if count > v.threshold then  
271
                                                    hitthreshold = "true"                                  
272
                                                    v.func(v, count - v.threshold)
273
                                            end
274
                                    else
275
                                            if count < v.threshold then
276
                                                    hitthreshold = "true"                                  
277
                                                    v.func(v, v.threshold - count)
278
                                            end
279
                                    end
280
                                    print(v.id1 .. ":" .. v.dmg1 .. " " .. v.compare .. " " .. v.threshold .. " = " .. hitthreshold)
281
                     
282
                    end
283
                    sleep(polltimer)
284
            end