Advertisement
moomoomoo309

DefragAE 1.0

Jan 25th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. --Uncomment to use given pipe side.
  2. --PipeSide="bottom"
  3. --Uncomment to use given monitor side(s). Put commas between for multiple sides, keep the curly braces.
  4. --MonitorSide={"top"}
  5. sleepTime=60 --In seconds. Default is 60.
  6. ItemsPerTick=10 --How many items it should search per tick. Default is 10.
  7. ItemThreshold=64 --The max amount of items it can have and still be requested. Default is 64.
  8. writeToConsole=true --Should it write to the computer itself?
  9. writeToMonitor=true --Should it write to connected monitors?
  10. local function getMonitors()
  11.   MonTbl={}
  12.   local Peripherals=peripheral.getNames()
  13.   for i=1,#Peripherals do
  14.     if peripheral.getType(Peripherals[i])=="monitor" then
  15.       table.insert(MonTbl,Peripherals[i])
  16.     end
  17.   end
  18.   return MonTbl
  19. end
  20.  
  21. local function findPeripheral(type)
  22.   local Peripherals=peripheral.getNames()
  23.   for i=1,#Peripherals do
  24.     if peripheral.getType(Peripherals[i])==type then
  25.       return Peripherals[i]
  26.     end
  27.   end
  28.   return nil
  29. end
  30.  
  31. local function draw(str)
  32.   if writeToMonitor then
  33.     MonTbl=MonitorSide or getMonitors()
  34.     for i=1,#MonTbl do
  35.       local p=peripheral.wrap(MonTbl[i])
  36.       p.write(str)
  37.     end
  38.   end
  39.   if writeToConsole then
  40.     term.write(str)
  41.   end
  42. end
  43.  
  44. local function Clear()
  45.   if writeToMonitor then
  46.     MonTbl=MonitorSide or getMonitors()
  47.     for i=1,#MonTbl do
  48.       local p=peripheral.wrap(MonTbl[i])
  49.       p.clear()
  50.     end
  51.   end
  52.   if writeToConsole then
  53.     term.clear()
  54.   end
  55. end
  56.  
  57. local function CursorPos(x,y)
  58.   if writeToMonitor then
  59.     MonTbl=MonitorSide or getMonitors()
  60.     for i=1,#MonTbl do
  61.       local p=peripheral.wrap(MonTbl[i])
  62.       p.setCursorPos(x,y)
  63.     end
  64.   end
  65.   if writeToConsole then
  66.     term.setCursorPos(x,y)
  67.   end
  68. end
  69.  
  70. local function countDown(sleepTime)
  71.   for i=(sleepTime*10),0,-1 do
  72.     CursorPos(1,1)
  73.     draw(clr)
  74.     CursorPos(1,1)
  75.     draw(i/10)
  76.     draw("s until next search.")
  77.     sleep(0.1)
  78.   end
  79.   CursorPos(1,1)
  80.   draw(clr)
  81.   CursorPos(1,1)
  82.   draw("Searching...")
  83. end
  84.  
  85. --Setup for the main loop
  86. local clr=""
  87. local MonTbl=MonitorSide or getMonitors()
  88. x,y=term.getSize()
  89. for i=1,#MonTbl do
  90.   local p=peripheral.wrap(MonTbl[i])
  91.   local x2,y2=p.getSize()
  92.   x=math.max(x,x2)
  93.   y=math.max(y,y2)
  94. end
  95. for i=1,x do
  96.   clr=clr.." " --String that will clear the whole line of a monitor.
  97. end
  98. if PipeSide and peripheral.getType(PipeSide)=="LogisticsPipes:Request" then
  99.   local m=peripheral.wrap(PipeSide)
  100. else
  101.   if peripheral.find then --Checks to see if peripheral.find exists at all
  102.     local m=peripheral.find("LogisticsPipes:Request")
  103.   else
  104.     local m=findPeripheral("LogisticsPipes:Request") --This isn't as nice as peripheral.find(), but it'll work.
  105.   end
  106. end
  107. if not m then
  108.   error("No request pipe found.")
  109. end
  110.  
  111. --Main loop
  112. while true do
  113.   local Items=m.getAvailableItems()
  114.   local Count=0
  115.   for k,v in pairs(Items) do
  116.     local id=v.getValue1()
  117.     local amt=m.getItemAmount(id)
  118.     if amt<=ItemThreshold then
  119.       Count=Count+1
  120.       m.makeRequest(id,amt)
  121.     end
  122.     if k%ItemsPerTick==0 then
  123.       sleep(0.05)
  124.     end
  125.   end
  126.   Clear()
  127.   CursorPos(1,2)
  128.   if Count==1 then
  129.     draw(Count.." item pulled w/<="..ItemThreshold.." items.")
  130.   else
  131.     draw(Count.." items pulled w/<="..ItemThreshold.." items.")
  132.   end
  133.   countDown(sleepTime)
  134. end
  135. --Program by moomoomoo3O9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement