Advertisement
klassekatze

invtest

Aug 5th, 2020 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3. local colors = require("colors")
  4. local computer = require("computer")
  5.  
  6. do_debug = true
  7. DEBUG = function(msg)
  8. if do_debug then
  9. print(msg)
  10. end
  11. end
  12.  
  13. local robot = nil--require("robot")
  14. local rs = nil-- = component.list("redstone")()
  15.  
  16. function loadrs()
  17. rs = component.redstone
  18. end
  19. if pcall(loadrs) then
  20. -- no errors while running `foo'
  21. print("loaded component.redstone!")
  22. else
  23. print("unable to load component.redstone")
  24. rs = nil
  25. end
  26. function loadrst2()
  27. rs = component.list("redstone")()
  28. end
  29. if rs == nil then
  30. if pcall(loadrst2) then
  31. print("loaded component.list(\"redstone\")()")
  32. else
  33. print("unable to load component.list(\"redstone\")()")
  34. end
  35. end
  36.  
  37. local inv = nil
  38. function loadinv()
  39. inv = component.inventory_controller
  40. end
  41. if pcall(loadinv) then
  42. -- no errors while running `foo'
  43. print("loaded component.inventory_controller!")
  44. else
  45. print("unable to load component.inventory_controller")
  46. inv = nil
  47. end
  48. function loadrobot()
  49. robot = require("robot")
  50. end
  51. if pcall(loadrobot) then
  52. print("loaded robot module!")
  53. -- no errors while running `foo'
  54. else
  55. print("unable to load robot module")
  56. robot = nil
  57. end
  58.  
  59. local NOP0 = function(...) return 0 end
  60. local NOP1 = function(...) return 3 end
  61. local NOPT = function(...) return {label = "testlabel", size = 1} end
  62. -- OCEmu has no redstone.
  63. if rs == nil then
  64. print("generating dummy redstone module")
  65. rs = {}
  66. rs.getInput = NOP0
  67. rs.setOutput = NOP0
  68. end
  69. if inv == nil then
  70. print("generating dummy inv module")
  71. inv = {}
  72. inv.getInventorySize = NOP1
  73. inv.getStackInSlot = NOPT
  74. end
  75. if robot == nil then
  76. print("generating dummy robot module")
  77. robot = {}
  78. robot.inventorySize = NOP
  79. robot.select = NOP
  80. robot.suckUp = NOP
  81. robot.compareTo = NOP
  82. robot.dropDown = NOP
  83. robot.use = NOP
  84. end
  85.  
  86.  
  87.  
  88.  
  89.  
  90. -- Template is on top
  91.  
  92. --local template_inv_size = inv.getInventorySize(sides.top)
  93. --local interface_inv_size = inv.getInventorySize(sides.east)
  94. --local dest_inv_size = inv.getInventorySize(sides.north)
  95.  
  96. getInvSummary = function(side)
  97. local inv_slots = inv.getInventorySize(side)
  98. if not inv_slots then
  99. DEBUG(tostring(side).." has no inv\n")
  100. return nil
  101. end
  102. local summary = {}
  103. for slot = 1, inv_slots do
  104. local item = inv.getStackInSlot(side, slot)
  105. if item ~= nil then
  106. if summary[item.label] ~= nil then
  107. summary[item.label] = summary[item.label] + item.size;
  108. else
  109. summary[item.label] = item.size;
  110. end
  111. end
  112. end
  113. return summary;
  114. end
  115.  
  116. local left_sum = getInvSummary(sides.north);
  117. local right_sum = getInvSummary(sides.south);
  118.  
  119. retrieveItem = function(label, amount)
  120. local side = nil
  121. if left_sum[label] ~= nil then side = sides.north end
  122. if right_sum[label] ~= nil then side = sides.south end
  123. if side ~= nil then
  124. local inv_slots = inv.getInventorySize(side)
  125. for slot = 1, inv_slots do
  126. local item = inv.getStackInSlot(side, slot)
  127. if item ~= nil then
  128. if item.label == label then
  129. suckFromSlot(side, slot, amount)
  130. return true
  131. end
  132. end
  133. end
  134. --suckFromSlot(side:number, slot:number[, count:number])
  135.  
  136. end
  137. return false
  138. end
  139.  
  140. insertItem = function()
  141. --sides.north
  142. --we're lazy today
  143. local inv_slots = inv.getInventorySize(sides.west)
  144. for slot = 1, inv_slots do
  145. local item = inv.getStackInSlot(side, slot)
  146. if item == nil then
  147. dropIntoSlot(sides.north, slot)
  148. return true
  149. end
  150. end
  151. return false
  152. end
  153.  
  154. compactInventory = function(side)
  155. local changed = true
  156. while changed do
  157. changed = false
  158. local inv_slots = inv.getInventorySize(side)
  159. print("slots: "..inv_slots)
  160. local first_with_space = {}
  161. for slot_start = 1, inv_slots do
  162. local item = inv.getStackInSlot(side, slot_start)
  163. if item ~= nil then
  164. --print("scanning "..item.label.." amt "..item.size)
  165. if item.size < 64 then
  166. --print("under 64")
  167. if first_with_space[item.label] == nil then
  168. --print("adding as backslot")
  169. first_with_space[item.label] = {slot = slot_start, amount = item.size}
  170. end
  171. end
  172. local fin = first_with_space[item.label]
  173. if fin ~= nil then
  174. --print("found it in backtable")
  175. local slot_end = fin["slot"]
  176. local slot_end_amt = fin["amount"]
  177. if slot_end ~= slot_start then
  178. --print("compacting slot "..slot_start.." into "..slot_end)
  179. inv.suckFromSlot(side, slot_start, 64-slot_end_amt)
  180. inv.dropIntoSlot(side, slot_end)
  181. first_with_space[item.label] = nil
  182. --print("skipping back to "..slot_end)
  183. slot_start = slot_end - 1
  184. changed = true
  185. end
  186. else
  187. --print("no prior for "..item.label.." at slot "..slot_start)
  188. end
  189. end
  190. end
  191. if changed then
  192. --print("looping back")
  193. end
  194. end
  195. end
  196.  
  197. stockRightFromLeftWithUp = function()
  198. local sum_top = getInvSummary(sides.top)
  199. local sum_west = getInvSummary(sides.west)
  200. local up = computer.uptime()
  201. print("compacting on dest inventory... (may take a bit)")
  202. compactInventory(sides.west)
  203. print("compacting completed in "..(computer.uptime() - up).." seconds")
  204. up = computer.uptime()
  205. for k,v in pairs(sum_top) do
  206. local name = k
  207. local amount = v
  208. if sum_west[name] == nil or sum_west[name] < amount then
  209. -- gotta add some
  210. while sum_west[name] < amount do
  211. retrieveItem(name)
  212. insertItem()
  213. sum_west[name] = sum_west[name] + 64
  214. print("added 64 "..name)
  215. end
  216. end
  217. end
  218. end
  219.  
  220.  
  221. local up = computer.uptime()
  222. --compactInventory(sides.top)
  223. --print("done, in "..(computer.uptime() - up))
  224. --suckFromSlot(sides.top, 0)
  225. stockRightFromLeftWithUp()
  226. print("stockRightFromLeftWithUp completed in "..(computer.uptime() - up).." seconds")
  227.  
  228.  
  229. --local up = computer.uptime()
  230. --print("test")
  231. --local sum = getInvSummary(sides.top)
  232. --for k,v in pairs(sum) do
  233. -- io.write(k.." : "..v.."\n")
  234. --end
  235. --print("done, in "..(computer.uptime() - up))
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement