rdalkire

SortAndBlock 1.0.1

Dec 30th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1. --[[ SortAndBlock 1.0.1
  2.  
  3. Takes the items from the chest
  4. fore of it, puts non-blockable to the
  5. port chest, Nine-block blocks to the
  6. starboard. Blockables are crafted to
  7. blocks.
  8.  
  9. CopyLeft (MIT License) 2014
  10. Robert David Alkire II
  11. ]]
  12.  
  13. -- assign fake/real turtle
  14. local t
  15. if turtle then
  16.   t = turtle
  17. else
  18.   t = require "mockTurtle"
  19. end
  20.  
  21. local deadReckoner = {}
  22.  
  23. deadReckoner.FORE = 0
  24. deadReckoner.STARBOARD = 1
  25. deadReckoner.AFT = 2
  26. deadReckoner.PORT = 3
  27.  
  28. deadReckoner.heading= deadReckoner.FORE
  29.  
  30. -- Turns as needed to face the
  31. -- target direction indicated
  32. deadReckoner.bearTo= function(target)
  33.  
  34.   local WAYS = {}
  35.   WAYS[deadReckoner.FORE] = "FORE"
  36.   WAYS[deadReckoner.STARBOARD]= "STARBOARD"
  37.   WAYS[deadReckoner.AFT] = "AFT"
  38.   WAYS[deadReckoner.PORT] = "PORT"
  39.  
  40.   local trnsRght =
  41.       target - deadReckoner.heading
  42.  
  43.   local trns = math.abs( trnsRght )
  44.   if trns ~= 0 then
  45.     local i = 0
  46.     while i < trns do
  47.       if trnsRght >= 0 then
  48.         t.turnRight()
  49.       else
  50.         t.turnLeft()
  51.       end -- which way
  52.       i = i + 1
  53.     end -- turn loop
  54.   end -- there were any turns
  55.  
  56.   deadReckoner.heading = target
  57. end
  58.  
  59. local dr = deadReckoner
  60.  
  61. --[[ Drops this much from inventory ]]
  62. local function dropFromInv( cntToDrop )
  63.  
  64.   --[[ loops through inventory until
  65.   cntToDrop reached ]]
  66.   local i = 16
  67.   while i >= 1 and cntToDrop > 0 do
  68.     local iCnt = t.getItemCount(i)
  69.     local drAmtThs = 0
  70.     if iCnt >= cntToDrop then
  71.       drAmtThs = cntToDrop
  72.     else -- iCnt must be < cntToDrop
  73.       drAmtThs = iCnt
  74.     end
  75.    
  76.     -- If applicable, drop & update
  77.     if drAmtThs > 0 then
  78.       t.select(i)
  79.       t.drop(drAmtThs)
  80.       cntToDrop = cntToDrop - drAmtThs
  81.     end
  82.    
  83.     i = i - 1
  84.   end
  85.  
  86. end
  87.  
  88.   -- From Table (list) of blockables
  89.   local blockables = {}
  90.   blockables["minecraft:melon"] = 0
  91.   blockables["minecraft:wheat"] = 0
  92.   blockables["minecraft:diamond"] = 0
  93.   blockables["minecraft:emerald"] = 0
  94.   blockables["minecraft:iron_ingot"]=0
  95.   blockables["minecraft:gold_ingot"]=0
  96.   blockables["minecraft:gold_nugget"]=0
  97.   blockables["minecraft:redstone"] = 0
  98.   blockables["minecraft:coal"] = 0
  99.  
  100. --[[ For items in the turtle's
  101. inventory, which one would be best for
  102. crafting into 9-item blocks.
  103. @return table with name = blockable
  104.   item, and nameBlock name of the item
  105.   which is the block. ]]
  106. local function findBlockable()
  107.  
  108. --  NOTE to check for lapis, would
  109. --  have to check for data value 4
  110.  
  111.   -- Tracks the blockable counts
  112.   for i = 1, 16 do --through inventory
  113.     local itm =  t.getItemDetail(i)
  114.    
  115.     if itm ~= nill then
  116.       local nme = itm.name
  117.      
  118.       -- If item name is in the list
  119.       if blockables[ nme ]~= nill then
  120.         -- Add the count
  121.         blockables[ nme ] =
  122.             blockables[ nme ] +
  123.             itm.count
  124.            
  125.       end -- if blckble
  126.     end -- not nill
  127.   end --invtry loop
  128.  
  129.   -- Loop through blockable list
  130.   local max = 0
  131.   local rtrn = nill
  132.   for nme, cnt in pairs(blockables) do
  133.     -- If found count > max
  134.     if cnt > max then
  135.       -- Assign result name
  136.       max = cnt
  137.       rtrn = nme
  138.     end -- if higher is found
  139.    
  140.   end --blockable loop
  141.  
  142.   if rtrn ~= nill then
  143.     print("Most blockable item: ".. rtrn)
  144.   end
  145.  
  146.   return rtrn
  147.  
  148. end
  149.  
  150. --[[ From its inventory, removes all
  151. but the most blockable kind of item
  152. according to quantity, putting all
  153. non-wanted items into the appropriate
  154. chests: already-blocked into the right
  155. one, non-blockable into left one, and
  156. remaining blockable back to the source
  157. chest.
  158. @return number of remaining items,
  159.   evenly divisible by 9 ]]
  160. local function sortAllButBlockables()
  161.  
  162.   local blckbl = findBlockable()
  163.  
  164.   -- Going through the inventory
  165.   local blckblCnt = 0
  166.   for i = 1, 16 do
  167.     local itm = t.getItemDetail(i)
  168.    
  169.     if itm ~= nill then
  170.      
  171. --      Uses whatever is most
  172. --      blockable to craft.
  173.       if itm.name == blckbl then
  174.         -- add to the slice count
  175.         blckblCnt = blckblCnt + itm.count
  176.      
  177.       -- If it's blockable, yet not
  178.       -- the *most* blockable
  179.       elseif blockables[ itm.name ] ~=
  180.           nill then
  181.          -- put it back into the source
  182.         dr.bearTo( dr.FORE )
  183.         t.select(i)
  184.         t.drop()
  185.        
  186.       -- If item name has "_block"
  187.       elseif string.find( itm.name,
  188.           "_block", 1, true ) then
  189.         -- put stack in starboard chest
  190.         dr.bearTo(dr.STARBOARD)
  191.         t.select(i)
  192.         t.drop()
  193.      
  194.       else
  195.         -- put stack in the port chest
  196.         dr.bearTo(dr.PORT)
  197.         t.select(i)
  198.         t.drop()
  199.       end
  200.    
  201.     end -- if there are items
  202.    
  203.   end -- loop of slots
  204.  
  205.   dr.bearTo(dr.FORE)
  206.  
  207.   -- Limit is 64 * 9 for crafting
  208.   local rmndr = blckblCnt % 9
  209.   if blckblCnt > 576 then
  210.     dropFromInv( blckblCnt - 576 )
  211.     blckblCnt = 576
  212.   elseif rmndr > 0 then
  213.     dropFromInv( rmndr )
  214.     blckblCnt = blckblCnt - rmndr
  215.     print("Returned: ".. rmndr)
  216.   end
  217.  
  218.   print("Blockable: ".. blckblCnt )
  219.  
  220.   return blckblCnt
  221. end
  222.  
  223. --[[ Slots used as crafting table ]]
  224. local crftSlts = { 1, 2, 3, 5, 6, 7,
  225.     9, 10, 11 }
  226.  
  227. --[[ Transfers from the selected slot
  228. to any crafting-table slot that doesn't
  229. yet have the amount needed.
  230. @param rightAmt is the amount that the
  231.     current slot should have
  232. @param blCnt is number of items per
  233.     crafting table slot, which will
  234.     be the number of blocks produced ]]
  235. local function trnsfr(rightAmt, blCnt)
  236.  
  237.   local slcCount = t.getItemCount()
  238.   local toTrnsfr = slcCount - rightAmt
  239.  
  240.   -- Until down to rightAmt
  241.   while(toTrnsfr> 0) do
  242.  
  243.     -- Finds a crafting table slot that
  244.     -- has less than blCnt
  245.     local i = 1
  246.     local cSlt = 0
  247.     local isFound = false
  248.     local csWants = 0
  249.     while i <= 9 and isFound==false do
  250.       cSlt = crftSlts[i]
  251.       local csAmt= t.getItemCount(cSlt)
  252.       csWants = blCnt - csAmt
  253.       if csWants > 0 then
  254.         isFound = true
  255.       end
  256.       i = i + 1
  257.     end
  258.    
  259.     -- Transfers needed amount
  260.     local thisTrnsf = 0
  261.     if csWants > toTrnsfr then
  262.       thisTrnsf = toTrnsfr
  263.     else
  264.       thisTrnsf = csWants
  265.     end
  266.    
  267.     t.transferTo( cSlt, thisTrnsf )
  268.    
  269.     -- updates toTransfr
  270.     toTrnsfr = toTrnsfr - thisTrnsf
  271.    
  272.   end -- trnsfr loop
  273.    
  274. end -- function
  275.  
  276.  
  277. --[[ Crafts the blockable items into
  278. blocks.
  279. @param count number of craftable itmes
  280. in the inventory ]]
  281. local function craftBlocks( count )
  282.   -- Set up the slots and craft
  283.   local blCnt = math.floor(count / 9)
  284.  
  285.   print("Block count: ".. blCnt)
  286.  
  287.   --[[ Slots that must be empty ]]
  288.   local emptySlts = {4, 8, 12, 13, 14,
  289.       15, 16 }
  290.    
  291.   --[Loop through empty-able slts]]
  292.   for i = 1, 7 do
  293.     local slotIndx = emptySlts[i]
  294.     t.select(slotIndx)
  295.     trnsfr( 0, blCnt )
  296.   end --empty slots loop
  297.  
  298.   --[[Loops through the craftable slts]]
  299.   for i = 1, 9 do
  300.     local slotIndx = crftSlts[i]
  301.     t.select(slotIndx)
  302.     trnsfr( blCnt, blCnt )
  303.   end
  304.  
  305.   t.craft()
  306.  
  307. end
  308.  
  309.  
  310. local function main( tArgs )
  311.  
  312.   --[[ Pulls items from chest ]]
  313.   local stillSucks = true
  314.   while stillSucks do
  315.     stillSucks = t.suck()
  316.   end
  317.  
  318.   local blckblCnt=sortAllButBlockables()
  319.  
  320.   -- If there are enough slices, craft!
  321.   if blckblCnt >= 9 then
  322.     craftBlocks( blckblCnt )
  323.     -- Put away the resulting blocks
  324.     dr.bearTo(dr.STARBOARD)
  325.     t.drop()
  326.     dr.bearTo(dr.FORE)
  327.   end
  328.  
  329.   print("Finished sorting; see chests.")
  330.  
  331. end
  332.  
  333. local tArgs = {...}
  334. main(tArgs)
Advertisement
Add Comment
Please, Sign In to add comment