funkd0ct0r

Untitled

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