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