Advertisement
karelvysinka

Ultimate Crafting Turtle

Aug 5th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.79 KB | None | 0 0
  1. local analyzer = peripheral.wrap("right")
  2. Z = {1,2,3,5,6,7,9,10,11}
  3. x={}
  4. tCraft={}
  5. function oddaj()
  6. for i=1,16 do
  7.  
  8. if turtle.getItemCount(i)>0 then
  9. turtle.select(i)
  10. turtle.drop()
  11. end
  12. end
  13. end
  14. function zlicz(recka2)
  15. ZLICZ={}
  16. for i=1,9 do
  17. ZLICZ[i]={ID,META,COUNT}
  18. end
  19. for i=1,9 do
  20. if recka2[i][1] == nil then
  21. else
  22. ZLICZ[1].ID = recka2[i][1]
  23. ZLICZ[1].META = recka2[i][2]
  24. ZLICZ[1].COUNT = 1
  25. w = i
  26. break
  27. end
  28. end
  29. k = 1
  30. for i = w+1,9 do
  31.         check = false
  32.         if recka2[i][1] == nil then
  33.         else
  34.         X = recka2[i][1]
  35.         Y = recka2[i][2]
  36.         --print("Wyraz nr " ..i.. " wynosi " ..X.."")
  37.                 for j = 1,k do
  38.                         if X == ZLICZ[j].ID and Y == ZLICZ[j].META then
  39.                                 ZLICZ[j].COUNT = ZLICZ[j].COUNT + 1  
  40.                                 --print("Zwiekszam ilosc liczb "..X.." o jeden")
  41.                                 check = true
  42.                         end
  43.                
  44.                 end
  45.         if check == false then
  46.                 k = k+1
  47.                 ZLICZ[k].ID = X
  48.                 ZLICZ[k].META = Y
  49.                 --print("Dopisuje element o nr " ..k.. " o wartosci "..X.."")
  50.                 ZLICZ[k].COUNT = 1
  51.                
  52.         end
  53.         end
  54. end
  55. return ZLICZ,k
  56. end
  57. function sprawdz(dane,rekordy)
  58. sprawdzenie = true
  59. for i=1,rekordy do
  60. slot = 0
  61. X = 0
  62. Y = 0
  63. ilosc = 0
  64. --print("Poszukuje bloku o ID rownym " ..dane[i].ID.. "")
  65. repeat
  66. slot = slot + 1
  67. X = analyzer.getBlockIdAt(slot)
  68. Y = analyzer.getBlockMetadataAt(slot)
  69. if X == dane[i].ID and dane[i].META == nil then
  70. ilosc = ilosc + analyzer.getBlockCountAt(slot)
  71. elseif X == dane[i].ID and Y == dane[i].META then
  72. --print("znalazlem w slocie "..slot.." blok o ID rownym "..ID.."")
  73. ilosc = ilosc + analyzer.getBlockCountAt(slot)
  74. end
  75. until slot == 109
  76. if ilosc < dane[i].COUNT then
  77. sprawdzenie = false
  78. if dane[i].META == nil then
  79. brak = dane[i].ID
  80. else
  81. brak = ""..dane[i].ID..":"..dane[i].META..""
  82. end
  83. print("braklo itemu o ID: "..dane[i].ID.." w liczbie: "..dane[i].COUNT - ilosc.."")
  84. end
  85. end
  86. return sprawdzenie, brak
  87. end
  88. function craft(recka3)
  89. for i=1,9 do
  90. if recka3[i][1] == nil then
  91. else
  92. slot = 0
  93. repeat
  94. slot = slot +1
  95. X = analyzer.getBlockIdAt(slot)
  96. Y = analyzer.getBlockMetadataAt(slot)
  97. until (X == recka3[i][1] and recka3[i][2] == nil) or (X == recka3[i][1] and Y == recka3[i][2])
  98. --print("Znalazlem w slocie "..slot.."")
  99. ilosc = analyzer.getBlockCountAt(slot)
  100. turtle.select(Z[i])
  101. analyzer.takeAt(slot)
  102. turtle.drop(ilosc - 1)
  103. end
  104. end
  105. turtle.craft()
  106. oddaj()
  107. end
  108. function craftuj(recka)
  109. print("craftuje "..b.."")
  110. a = false
  111. while not a do
  112. a,b = sprawdz(zlicz(recka))
  113. if a == false then
  114. print("braklo "..b.."")
  115. b = tostring(b)
  116. crafting()
  117. a = false
  118. else
  119. craft(recka)
  120. end
  121. end
  122. end
  123. function crafting()
  124. if tCraft[b] then
  125.     craftuj(tCraft[b])
  126.         else
  127.         print("Braklo: "..b.."") os.pullEvent()
  128.   end
  129. end
  130. function craftujemy()
  131. odczyt = io.open ("receptury","r")
  132. while true do -- odczyt recek
  133. tekst=odczyt:read()
  134. if tekst == nil then break end
  135. item1, x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9] = string.match(tekst, "([%w%:]+)=([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+);([%w%:]+)")
  136. tCraft[item1]={}
  137. for i=1,9 do
  138. if(x[i] == "0") then
  139. tCraft[item1][i]={nil}
  140. elseif(string.find(x[i],":")) then
  141. id,meta = string.match(x[i],"(%d+):(%d+)")
  142. tCraft[item1][i]={tonumber(id),tonumber(meta)}
  143. else
  144. tCraft[item1][i]={tonumber(x[i])}
  145. end
  146. end
  147. end
  148. odczyt:close()
  149. print("Co mam craftowac? Podaj ID lub ID:META")
  150. b = read()
  151. b1 = b
  152. print("ile? ")
  153. c = read()
  154. for zz=1,c do
  155. crafting()
  156. b = b1
  157. end
  158. end
  159. function menu()
  160. term.clear()
  161. opcja = 2
  162. term.setCursorPos(1,1)
  163. term.write("Ultimate Crafting Program")
  164. term.setCursorPos(1,2)
  165. term.write("[ ] Craftuj")
  166. term.setCursorPos(1,3)
  167. term.write("[ ] Dodaj recepture przez skanowanie")
  168. term.setCursorPos(1,4)
  169. term.write("[ ] Dodaj recepture przez wpisanie")
  170. end
  171. function odswiez()
  172. for i=2,10 do
  173. term.setCursorPos(2,i)
  174. term.write(" ")
  175. end
  176. term.setCursorPos(2,opcja)
  177. term.write("X")
  178. end
  179. function zapisz()
  180. print("Uloz recepture w inventory turtla")
  181. os.pullEvent()
  182. x={}
  183. for i=1,9 do
  184. turtle.select(Z[i]) -- wybierz slot
  185. x[i] = {} -- stworz tablice
  186. id=analyzer.getBlockId() -- pobierz id
  187. if(id == false) then -- jesli nie ma nic
  188. x[i].ID = nil -- przypisz nil
  189. else
  190. x[i].ID = id -- przypisz id
  191. x[i].META = analyzer.getBlockMetadata() -- przypisz meta
  192. end
  193. end
  194. z={}
  195. for i=1,9 do
  196. if x[i].ID == nil then -- jezli id = nil
  197. z[i] = 0; -- przypisz 0
  198. elseif x[i].META == nil then -- jezeli meta = nil
  199. z[i] = x[i].ID -- przypisz samo id
  200. else -- w przeciwnym wypadku
  201. z[i] = ""..x[i].ID..":"..x[i].META.."" -- wpisz ID:META
  202. end
  203. end
  204. turtle.craft()
  205. turtle.select(11)
  206. item = analyzer.getBlockId()..":"..analyzer.getBlockMetadata()
  207. zapis = io.open ("receptury","a") -- otworz plik
  208. zapis:write(string.format("%s=%s;%s;%s;%s;%s;%s;%s;%s;%s\n",item,z[1],z[2],z[3],z[4],z[5],z[6],z[7],z[8],z[9]))
  209. zapis:close()
  210. end
  211. function podaj()
  212. term.clear()
  213. print("podaj recepture wedlug skladni: ")
  214. print("ID:META=ID:META;ID:META; ... ;ID:META")
  215. print("mozna podawac rowniez ID bez podania Metadanych")
  216. nowa=read()
  217. zapis = io.open ("receptury","a") -- otworz plik
  218. zapis:write(nowa.."\n")
  219. zapis:close()
  220. end
  221. menu()
  222. odswiez()
  223. while true do
  224. przycisk,kod = os.pullEvent()
  225. if przycisk == "key" and kod == 208 and opcja < 4 then
  226. opcja=opcja+1
  227. end
  228. if przycisk == "key" and kod == 200 and opcja > 2 then
  229. opcja = opcja - 1
  230. end
  231. if przycisk == "key" and kod == 28 then
  232.         term.clear()
  233.         term.setCursorPos(1,1)
  234.         if opcja == 2 then
  235.         craftujemy()
  236.         elseif opcja == 3 then
  237.         zapisz()
  238.         elseif opcja == 4 then
  239.         podaj()
  240.         end
  241.         menu()
  242.         end
  243. odswiez()
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement