Advertisement
AdslHouba

Imprimante 3D (ComputerCraft)

Apr 12th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1. os.loadAPI("fichier")
  2.  
  3. compass=peripheral.find('compass')
  4. comptage={}
  5. typeComptage={}
  6. typeData={}
  7. curs=0
  8. index=0
  9. plusPetitX=99999
  10. plusPetitY=99999
  11. for k,ligne in pairs(fichier.block) do
  12.     if plusPetitX>ligne[1] then
  13.         plusPetitX=ligne[1]
  14.     end
  15.     if plusPetitY>ligne[3] then
  16.         plusPetitY=ligne[3]
  17.     end
  18.     if typeComptage[ligne[4]..':'..ligne[5]]==nil then
  19.         curs=curs+1
  20.         typeComptage[ligne[4]..':'..ligne[5]]=curs
  21.         index=curs
  22.         typeData[index]={name=ligne[4],metadata=ligne[5]}
  23.         comptage[index]=0
  24.     else
  25.         index=typeComptage[ligne[4]..':'..ligne[5]]
  26.     end
  27.     comptage[index]=comptage[index]+1
  28. end
  29. if plusPetitX~=0 or plusPetitY~=0 then
  30.     print("Decallage de "..plusPetitX.." "..plusPetitY)
  31.     read()
  32. end
  33.  
  34. term.clear();
  35. term.setCursorPos(1,1)
  36. term.write("Afficher les blocks ? Y/N")
  37. test=read()
  38. if test=="Y" then
  39.     for index,qte in pairs(comptage) do
  40.         term.clear();
  41.         term.setCursorPos(1,1)
  42.         term.write(typeData[index].name..' '..typeData[index].metadata)
  43.         term.setCursorPos(1,2)
  44.         stack=math.floor(qte/64)
  45.         term.write(stack.." Stacks "..(qte-(stack*64)).." blocs")
  46.         read()
  47.     end
  48. end
  49.  
  50. function reaproAdd(chest,index,qteManquante)
  51.     chest.condenseItems()
  52.     stacks=chest.getAllStacks()
  53.     for i=1, #stacks do
  54.         info=stacks[i].all()
  55.         if typeData[index].name==info.id and typeData[index].metadata==info.dmg then                       
  56.             chest.pushItemIntoSlot("UP",i,qteManquante,index)
  57.             return
  58.         end
  59.     end
  60.     term.clear();
  61.     term.setCursorPos(1,1)
  62.     term.write(typeData[index].name.." "..typeData[index].metadata.." PAS TROUVER "..qteManquante)
  63.     read()
  64.     reaproAdd(chest,index,qteManquante)
  65.     return
  66. end
  67. function reapro()
  68.    
  69.     local chest=peripheral.wrap("bottom")
  70.     for index,qte in pairs(comptage) do
  71.         if qte~=0 then
  72.             info=turtle.getItemDetail(index)
  73.             if info~=nil then
  74.                 if typeData[index].name==info.name and typeData[index].metadata==info.damage then
  75.                     qteAct=turtle.getItemCount(index)      
  76.                 else
  77.                     term.clear();
  78.                     term.setCursorPos(1,1)
  79.                     term.write("INDEX "..index.." n'est pas "..typeData[index].name.." "..typeData[index].metadata)
  80.                     read()
  81.                     qteAct=0
  82.                 end                
  83.             else
  84.                 qteAct=0       
  85.             end
  86.            
  87.             if qteAct~=comptage[index] then
  88.                 if comptage[index]>64 then
  89.                     qteManquante=64-qteAct
  90.                 else
  91.                     qteManquante=comptage[index]-qteAct
  92.                 end
  93.                 if qteManquante~=0 then
  94.                     reaproAdd(chest,index,qteManquante)
  95.                 end
  96.             end
  97.         end
  98.     end
  99.     while turtle.getFuelLimit()~=turtle.getFuelLevel() do
  100.         infof("Attente fuel")
  101.         os.sleep(1)
  102.     end
  103. end
  104. function infof(text)
  105.     term.clear();
  106.     term.setCursorPos(1,1)
  107.     print(text)
  108. end
  109. reapro()
  110.  
  111. local bx, by, bz=gps.locate(5)
  112.  
  113. function suivant()
  114.     slot=typeComptage[fichier.block[indexS][4]..':'..fichier.block[indexS][5]]
  115.     if turtle.getItemCount(slot)==0 then
  116.         infof("Retour en cours")
  117.         deplacement(-1,0,-1,false)
  118.         reapro()
  119.         return false
  120.     else
  121.         infof("Deplacement"..(fichier.block[indexS][1]).." "..(fichier.block[indexS][2]).." "..(fichier.block[indexS][3]))
  122.         deplacement(tonumber(fichier.block[indexS][1])-plusPetitX,tonumber(fichier.block[indexS][2])+1,tonumber(fichier.block[indexS][3])-plusPetitY,true)
  123.         turtle.select(slot)
  124.         turtle.placeDown()
  125.         comptage[slot]=comptage[slot]-1
  126.         return true
  127.     end
  128. end
  129. function deplacement(ox,oy,oz,aller)
  130.     rx=ox+bx+1
  131.     ry=oy+by
  132.     rz=oz+bz+1
  133.     while true do
  134.         local x, y, z=gps.locate(5)
  135.         local orientation=compass.getFacing();
  136.         if aller and y~=ry then
  137.             if y<ry then
  138.                 turtle.up()
  139.             else
  140.                 turtle.down()
  141.             end
  142.         elseif x~=rx then
  143.             if rx>x then
  144.                 if orientation=="east" then
  145.                     turtle.forward()
  146.                 elseif orientation=="west" then
  147.                     turtle.back()
  148.                 else
  149.                     turtle.turnLeft()
  150.                 end
  151.             else
  152.                 if orientation=="east" then
  153.                     turtle.back()
  154.                 elseif orientation=="west" then
  155.                     turtle.forward()
  156.                 else
  157.                     turtle.turnLeft()
  158.                 end
  159.             end
  160.         elseif z~=rz then
  161.             if rz>z then
  162.                 if orientation=="south" then
  163.                     turtle.forward()
  164.                 elseif orientation=="north" then
  165.                     turtle.back()
  166.                 else
  167.                     turtle.turnLeft()
  168.                 end
  169.             else
  170.                 if orientation=="south" then
  171.                     turtle.back()
  172.                 elseif orientation=="north" then
  173.                     turtle.forward()
  174.                 else
  175.                     turtle.turnLeft()
  176.                 end
  177.             end
  178.         elseif not(aller) and y~=ry then
  179.             if y<ry then
  180.                 turtle.up()
  181.             else
  182.                 turtle.down()
  183.             end
  184.         else
  185.             return
  186.         end
  187.     end
  188. end
  189.  
  190. yActuel=-1
  191. xPrec=0
  192. zPrec=0
  193. indexS=-1
  194. while not(#fichier.block==0) do
  195.     if fichier.block[1][2]==yActuel then
  196.         recherche=true
  197.         indexR=1
  198.         indexS=1
  199.         meilleur=99999
  200.         while recherche do
  201.             calc=math.abs(fichier.block[indexR][1]-xPrec)+math.abs(fichier.block[indexR][3]-zPrec)
  202.             if calc<meilleur then
  203.                 indexS=indexR
  204.                 meilleur=calc
  205.             end        
  206.             indexR=indexR+1
  207.             if fichier.block[indexR][2]~=yActuel then
  208.                 recherche=false
  209.             end
  210.         end
  211.     else
  212.         indexS=1
  213.     end
  214.     yActuel=fichier.block[indexS][2]
  215.     xPrec=fichier.block[indexS][1]
  216.     zPrec=fichier.block[indexS][3]
  217.    
  218.     reussi=suivant()
  219.     if reussi then
  220.         table.remove(fichier.block,indexS)
  221.     end
  222. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement