Advertisement
jille_Jr

CC: Treefarm - item.lua

Jun 17th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.32 KB | None | 0 0
  1.  
  2. inventory = {}
  3. types = {
  4.     unknown = "unknown",
  5.     dirt = "dirt",
  6.     grass = "dirt",
  7.     sapling = "sapling",
  8.     log = "wood",
  9.     wood = "wood",
  10.     dropoff = "dropoff", --not actual item type, but can be refered to
  11. }
  12. -- position for each type
  13. -- while standing at target location facing desired direction
  14. -- turtle should be able to do a turtle.suck() to grab the material
  15. -- if dir=up suckUp() if dir=down suckDown()
  16. position = {}
  17.  
  18. -- if you have a small tunnel towards your storage for the turtle
  19. -- addStoragePathCheckpoint(x,y,z) to add enterance
  20. -- ^ can be used multiple times if you have a complicated path
  21. -- turtle will follow the path in revese when it exits
  22. storageEnterancePath = {}
  23.  
  24. local differenceRecord = {}
  25.  
  26. function update()
  27.     for slot = 1,16 do
  28.         local oldType = nil
  29.         if type(inventory[slot])=="table" then oldType=inventory[slot].type end
  30.         inventory[slot] = {
  31.             type = oldType or types.unknown,
  32.             qty = turtle.getItemCount(slot),
  33.         }
  34.         if inventory[slot].qty==0 then
  35.             inventory[slot].type = nil
  36.         end
  37.     end
  38. end
  39.  
  40. -- gets all global variables
  41. function init( global )
  42.     update()
  43.     setfenv(1,global)
  44. end
  45.  
  46. function recordDifference()
  47.     differenceRecord = {}
  48.     for slot=1,16 do
  49.         differenceRecord[slot] = {
  50.             type = inventory[slot].type,
  51.             qty = inventory[slot].qty,
  52.         }
  53.     end
  54. end
  55.  
  56. function getDifference()
  57.     local difference = {}
  58.     update()
  59.     for slot=1,16 do
  60.         local qtyDifference = inventory[slot].qty - differenceRecord[slot].qty
  61.         if qtyDifference~=0 or inventory[slot].type~=differenceRecord[slot].type then
  62.             difference[slot] = {}
  63.             difference[slot].qty = qtyDifference
  64.             difference[slot].type = differenceRecord[slot].type
  65.  
  66.             if inventory[slot].type~=differenceRecord[slot].type then
  67.                 difference[slot].oldType = differenceRecord[slot].type
  68.             end
  69.         end
  70.     end
  71.     return difference
  72. end
  73.  
  74. function setItemAccess( item,x,y,z,dir )
  75.     position[item] = {x=x,y=y,z=z,dir=dir}
  76. end
  77.  
  78. function getItemAccess( item )
  79.     return position[item].x,position[item].y,position[item].z,position[item].dir
  80. end
  81.  
  82. function addStoragePathCheckpoint( x,y,z )
  83.     storageEnterancePath[#storageEnterancePath+1] = {x=x,y=y,z=z}
  84. end
  85.  
  86. function getStoragePathCheckpoint( step )
  87.     if type(storageEnterancePath[step])=="table" then
  88.         return (storageEnterancePath[step].x or _),(storageEnterancePath[step].y or _),(storageEnterancePath[step].z or _)
  89.     end
  90. end
  91.  
  92. function resetStoragePath()
  93.     storageEnterancePath = {}
  94. end
  95.  
  96. -- reverse = boolean
  97. function followStoragePath( reverse )
  98.     if type(reverse)~="boolean" then reverse = false end
  99.  
  100.     local start = 1
  101.     local stop = #storageEnterancePath
  102.     if reverse then start=stop stop=1 end
  103.  
  104.     for step = start,stop,mult(stop-start) do
  105.         gps.goto(getStoragePathCheckpoint(step))
  106.     end
  107. end
  108.  
  109. function getItemCount( item )
  110.     update()
  111.     local count = 0
  112.     for slot = 1,16 do
  113.         if inventory[slot].type == item then
  114.             count = count + inventory[slot].qty
  115.         end
  116.     end
  117.     return count
  118. end
  119.  
  120. -- gotoStorageRoom = (boolean) wether or not it should follow the storage path
  121. function dropoff( gotoStorageRoom )
  122.     if type(gotoStorageRoom)~="boolean" then gotoStorageRoom = false end
  123.  
  124.     -- dropoff must have a location
  125.     local x,y,z,dir = getItemAccess(types.dropoff)
  126.     if type(x)~="number" or type(y)~="number"
  127.     or type(z)~="number" or type(dir)~="string" then
  128.         return false
  129.     end
  130.  
  131.     if gotoStorageRoom then
  132.         followStoragePath()
  133.     end
  134.  
  135.     gps.goto(x,y,z,dir)
  136.  
  137.     for slot=1,16 do
  138.         local count = turtle.getItemCount(slot)
  139.         while count~=0 do
  140.             turtle.select(slot)
  141.             if dir==gps.up then
  142.                 turtle.dropUp(count)
  143.             elseif dir==gps.down then
  144.                 turtle.dropDown(count)
  145.             else
  146.                 turtle.drop(count)
  147.             end
  148.             count = turtle.getItemCount(slot)
  149.         end
  150.     end
  151.  
  152.     -- exit the storage room
  153.     if gotoStorageRoom then
  154.         followStoragePath(true)
  155.     end
  156. end
  157.  
  158. -- tries to get item from storage canceles if it has enough
  159. -- returns true on success and false when it fails for some reason
  160. -- noEnterancePath=true if you dont want it to go through the storage enterance path
  161. function getItem( item,minQty,noEnterancePath )
  162.     if type(minQty)~="number" then minQty=64 end
  163.     if type(noEnterancePath)~="boolean" then noEnterancePath=false end
  164.  
  165.     -- count quantity already in inventory
  166.     local qtyRequired = minQty - getItemCount(item)
  167.     if qtyRequired < 1 then
  168.         -- already got enough of target item
  169.         return true
  170.     end
  171.  
  172.     -- item must be obtainable
  173.     local x,y,z,dir = getItemAccess(item)
  174.     if type(x)~="number" or type(y)~="number"
  175.     or type(z)~="number" or type(dir)~="string" then
  176.         return false
  177.     end
  178.  
  179.     if not noEnterancePath then
  180.         followStoragePath()
  181.     end
  182.  
  183.     -- check if inv is full
  184.     update()
  185.     local invFull = true
  186.     for slot=1,16 do
  187.         if inventory[slot].qty == 0 then
  188.             invFull = false
  189.             break
  190.         end
  191.     end
  192.  
  193.     if invFull then
  194.         dropoff(false)
  195.     end
  196.  
  197.     gps.goto(x,y,z,dir)
  198.  
  199.     local qtyInInv-- number
  200.     repeat
  201.         recordDifference()
  202.  
  203.         if dir == gps.up then
  204.             turtle.suckUp()
  205.         elseif dir == gps.down then
  206.             turtle.suckDown()
  207.         else
  208.             turtle.suck()
  209.         end
  210.        
  211.         -- check how many slots changed when you sucked for items
  212.         local diff = getDifference()
  213.         local slotsChanged = 0
  214.         for slot,item in pairs(diff) do
  215.             slotsChanged = slotsChanged + 1
  216.         end
  217.  
  218.         if slotsChanged == 0 then
  219.             -- didn't obtain anything when trying to get items
  220.             -- gonna have to loop through again and again
  221.             --break
  222.         elseif slotsChanged > 1 then
  223.             -- more than one slot changed, might be 'cause of player interaction
  224.             -- go empty out and try again just so we know for sure we can keep track of the item
  225.             dropoff(false)
  226.             gps.goto(x,y,z,dir)
  227.         elseif slotsChanged == 1 then
  228.             -- one slot changed, record it, gotta keep track of it
  229.             for slot in pairs(diff) do
  230.                 inventory[slot].type = item
  231.             end
  232.         end
  233.  
  234.         qtyInInv = getItemCount(item)
  235.     until qtyInInv >= qtyRequired
  236.  
  237.     if not noEnterancePath then
  238.         followStoragePath(true)
  239.     end
  240.  
  241.     return true
  242. end
  243.  
  244. -- example input
  245. -- t = { "dirt", "sapling" } minQty = 64: gets 64 dirt and 64 saplings
  246. function getItems( t,minQty )
  247.     for num,item in ipairs(t) do
  248.         if num == 1 then
  249.             followStoragePath(false)
  250.         end
  251.         getItem(item,minQty,true)
  252.         if num == #t then
  253.             followStoragePath(true)
  254.         end
  255.     end
  256. end
  257.  
  258. function select( item )
  259.     for slot=1,16 do
  260.         if inventory[slot].type == item then
  261.             turtle.select(slot)
  262.             return true
  263.         end
  264.     end
  265.     return false
  266. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement