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