Keledan

Autocrafting da UU Matter

Mar 2nd, 2013
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.69 KB | None | 0 0
  1. -- Autocrafting da UU Matter, by youtube.com/Keledan75
  2. -- Impara a programmare con Minecraft Episodi 55-59
  3. -- http://www.youtube.com/watch?v=WSEmYOA6nE0
  4. -- Uso: metti una crafty turtle sopra uno scrigno pieno di UU matter; la turtle scarica gli oggetti prodotti in uno scrigno sopra di essa
  5. shallICraft = true
  6. ordered = 10
  7. maxUUperRecipe = 192
  8. max  = 4
  9. strErrorQuantity = "Quantita' di UU Matter insufficiente per la ricetta"
  10. strUnloading = "Scaricando Oggetti..."
  11. strQuantityOK = "Quantita' corretta"
  12. strWelcome = "\nScegli tua ricetta, X per uscire \n"
  13. strRecipe = ""
  14. strRequiredItem = ""
  15. strMessage = ""
  16. intNumeroOggetti = 0
  17. intMinQty = 0
  18. slots = {5,6,7,9,10,11,13,14,15}
  19. craftingInterval = 1
  20. oggettiOrdinati = 1
  21.  
  22. recipes = {redstone= {0, 0, 0, 0, 1, 0, 1, 1, 1, 4, 24, "r"},
  23.             copper = {0, 0, 1, 1, 0, 1, 0, 0, 0, 3, 10, "c"},
  24.             tin =    {0, 0, 0, 1, 0, 1, 0, 0, 1, 3, 10, "t"},
  25.             gold =   {0, 1, 0, 1, 1, 1, 0, 1, 0, 5, 2, "g"},   
  26.             diamond = {1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 1, "d"},
  27.             rubber = {1, 0, 1, 0, 0, 0, 1, 0, 1, 4, 21, "u"},
  28.             glass = {0, 1, 0, 1, 0, 1, 0, 1, 0, 4, 32, "v"},
  29.             iridium = {1, 1, 1, 0, 1, 0, 1, 1, 1, 7, 1, "i"},
  30.             wood = {0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 8, "w"},         
  31.             stone = {0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 16, "s"},                       
  32.             obsidian = {1, 0, 1, 1, 0, 1, 0, 0, 0, 4, 12, "o"},
  33.             flint =      {0, 1, 0, 1, 1, 0, 1, 1, 0, 5, 32, "f"},              
  34.             coal =   {0, 0, 1, 1, 0, 0, 0, 0, 1, 3, 20, "b"},
  35.             glowstone =  {0, 1, 0, 1, 0, 1, 1, 1, 1, 6, 8, "l"},
  36.             iron =   {1, 0, 1, 0, 1, 0, 1, 0, 1, 5, 2, "e"},
  37.             clay =  {1, 1, 0, 1, 0, 0, 1, 1, 0, 5, 48, "a"}        
  38.             }
  39.  
  40. -- se la lettera è quella nella tabella recipes ritorna vero, altrimenti false
  41. -- attribuisce alle variabili i valori di quantità minima e oggetto richiesto
  42. function checkKey(key)
  43.     for i in pairs(recipes) do
  44.         if recipes[i][12] == key then
  45. --          print (recipes[i][12] .. " - ")
  46.             intMinQty = recipes[i][11]
  47.             strRequiredItem = i
  48.             return true
  49.         end
  50.     end
  51.  
  52.     return false
  53. end        
  54.            
  55. -- Fetches the needed UU, sucks up to max times to get it. May fail if the first slots of a chest has small stacks
  56. function fetchUU(needed, max)
  57.     UUAmount = 0
  58.     for i = 1, max do
  59.         turtle.select(1)
  60.         turtle.suckDown()
  61.         UUAmount = UUAmount + turtle.getItemCount(1)
  62.         if UUAmount >= needed then break end
  63.     end
  64.     return UUAmount
  65. end
  66.  
  67. -- unloads up to the max slot - scarica fino allo slot max
  68. local function unload(side, max)
  69.     unloaded = 0
  70.     print( strUnloading )
  71.     for n=1,max do
  72.         unloaded = unloaded + turtle.getItemCount(n)
  73.         turtle.select(n)
  74.         if side == "up" then turtle.dropUp() else turtle.dropDown() end
  75.     end
  76.     turtle.select(1)
  77.     return unloaded
  78.  
  79. end
  80.  
  81. -- Accorpa i primi 3 slot e trasferisci negli slot secondo la ricetta
  82. function prepareForCrafting(UUperSlot, UUNeeded, recipe)
  83.     for i = 1, 9 do
  84.         if recipes[recipe][i] > 0 then
  85.             turtle.select(1)
  86.             turtle.transferTo(slots[i], UUperSlot)
  87.         end
  88.  
  89.         if UUNeeded > 64 and UUNeeded < 128 then
  90.             turtle.select(2)           
  91.             turtle.transferTo(1, 64)
  92.         elseif UUNeeded > 128 then
  93.             turtle.select(2)           
  94.             turtle.transferTo(1, 64)
  95.             turtle.select(3)   
  96.             turtle.transferTo(2, 64)       
  97.         end
  98.     end
  99. end
  100.  
  101. -- Gives back the unused UU
  102. -- Restituisce la UU non usata
  103. function returnUU()
  104.         for i = 1, max do
  105.             turtle.select(i)
  106.             turtle.dropDown()
  107.         end
  108.     turtle.select(1)
  109. end
  110.  
  111. -- Craft the required items
  112. -- Produce gli oggetti richiesti
  113. function crafting()
  114. finito = false
  115. -- se non ha finito il crafting o se lo slot 4 è ancora libero, continua il crafting
  116.     while not finito or turtle.getItemCount(4) > 0 do
  117.             if turtle.craft() then
  118.             -- scarica materiale già craftato ed esegue ancora crafting
  119.                 unload ("up", 3)
  120.             else
  121.                 finito = true
  122.             end
  123.     end
  124. end
  125.  
  126.  
  127. function executeCrafting (recipe, ordered)
  128.  
  129.     -- Execution - Esecuzione
  130.     requiredCrafting = math.ceil (ordered / recipes[recipe][11])
  131.     UUNeeded = recipes[recipe][10]  * requiredCrafting
  132.     -- If more than maxUUperRecipe asked, reset to maxUUPerRecipe, recalculates the requiredcrafting
  133.     if UUNeeded > maxUUperRecipe then
  134.         UUNeeded = maxUUperRecipe
  135.         requiredCrafting = math.floor (maxUUperRecipe / recipes[recipe][10])
  136.     end
  137.  
  138.     finalAmount = recipes[recipe][11] * requiredCrafting
  139.  
  140.  
  141.     if shallICraft then
  142.         print ("UU richiesta: " .. UUNeeded)
  143.         strMessage = "Oggetti prodotti: " .. finalAmount .. " di " .. recipe
  144.         fetchUU(UUNeeded, max)
  145.         prepareForCrafting(requiredCrafting, UUNeeded, recipe)
  146.         returnUU()
  147.         crafting()
  148.         --clean the whole table
  149.         unload("down", 16)
  150.     end
  151.     sleep (craftingInterval)
  152. end
  153.  
  154. -- print on screen the recipe list and specifies the key for each recipe
  155. function elencoRicette()
  156.     local strKey = ""
  157.     local strRecipe = ""
  158.     local strMenu = ""
  159.     local n = 0
  160.     for i in pairs(recipes) do
  161.         n = n + 1
  162.         -- mette in maiuscolo la lettera iniziale della ricetta
  163.         strRecipe = i:gsub("^%l", string.upper)
  164.         strKey = recipes[i][12]
  165.         -- alle righe pari vado a capo
  166.         if (n % 2 == 0) then
  167.             strMenu = strMenu .. " - " .. strRecipe ..": '" .. strKey .. "' "
  168.             print (strMenu)
  169.             strMenu = ""
  170.         else
  171.             strMenu = strRecipe ..": '" .. strKey .. "' "
  172.         end
  173.     end
  174.    
  175. end
  176.  
  177. -- esegue la routine principale - executes the main routine
  178. function routine ()
  179.     -- faccio apparire un messaggio di benvenuto
  180.     -- ripulisco lo schermo
  181.     term.clear()
  182.     print (strMessage)
  183.     print (strWelcome)
  184.     -- Descrivo i comandi disponibili
  185.     elencoRicette()
  186.  
  187.     -- accetto input da tastiera per il tipo di crafting richiesto
  188.     tipoEvento, tipoOggetti = os.pullEvent("char")
  189.  
  190.     -- controllo se ho premuto un tasto corretto
  191.     if tipoEvento == "char" then
  192.         print ("Hai premuto il tasto: " .. tipoOggetti)
  193.         if checkKey(tipoOggetti) then
  194.             -- richiedo il numero di oggetti da produrre
  195.             print ("Crafting minimo: " .. intMinQty .. " - Invio per minimo")
  196.             intNumeroOggetti = tonumber(read())
  197.             -- se si preme invio allora si fabbrica la qtà minima
  198.             if intNumeroOggetti == nil then intNumeroOggetti = intMinQty end
  199.             -- piazza un controllo e verifica se ho passato un valore numerico maggiore di 0 e inferiore di un massimo che decideremo
  200.  
  201.             print ("Hai ordinato " .. strRequiredItem .. " in quantità : " .. intNumeroOggetti)
  202.  
  203.             -- lancio execute crafting con i parametri: tipo di oggetto ordinato; la quantità minima ordinabile oppure la quantità ordinata se è superiore al minimo.
  204.  
  205.             if intNumeroOggetti < intMinQty then intNumeroOggetti = intMinQty end
  206.  
  207.             executeCrafting(strRequiredItem, intNumeroOggetti)
  208.    
  209.         elseif (tipoOggetti == "x" or tipoOggetti == "X") then
  210.             print ("Termino il programma")
  211.             shallICraft = false
  212.        
  213.         else
  214.             strMessage = "Il tasto premuto non corrisponde a una ricetta valida"
  215.         end
  216.     end
  217. end
  218.  
  219. while shallICraft do
  220.     routine()
  221. end
Advertisement
Add Comment
Please, Sign In to add comment