Advertisement
NanoBob

Replicator

Apr 19th, 2015
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.89 KB | None | 0 0
  1. local pos={["x"]=0,["y"]=0,["z"]=0}
  2. local facingDirection=nil
  3. local updating=false
  4. local inventory={}
  5. local inventorySave=true
  6. rednet.open("right")
  7.  
  8. if fs.exists("database")==false then
  9.     shell.run("pastebin get PdhBaBdZ database")
  10. end
  11. local success=os.loadAPI("database")
  12.  
  13. if success==false or database==nil then
  14.     error "The database system failed to load"
  15. end
  16.  
  17. if fs.exists("databases/data")==false then
  18.     database.createDataFile("data")
  19. end
  20.  
  21. if fs.exists("databases/position")==false then
  22.     database.createDataFile("position")
  23. end
  24.  
  25. function changePos(key,change)
  26.     if pos[key]==nil then
  27.         pos[key]=0
  28.     end
  29.     pos[key]=pos[key]+change
  30.     database.setData("position",key,pos[key])
  31. end
  32.  
  33. function updatePos(key)
  34.     database.setData("position",key,pos[key])
  35. end
  36.  
  37. function getFilePos()
  38.     if database.getData("position","x")==false then
  39.         pos.x=0
  40.         pos.y=0
  41.         pos.z=0
  42.         database.setData("position","x",0)
  43.         database.setData("position","y",0)
  44.         database.setData("position","z",0)
  45.     else
  46.         pos.x=database.getData("position","x")
  47.         pos.y=database.getData("position","y")
  48.         pos.z=database.getData("position","z")
  49.     end
  50.     bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  51. end
  52.  
  53. function tunnel(direction,blocks)
  54.     local digging=true
  55.     local fuelLevel=turtle.getFuelLevel()
  56.     if fuelLevel<blocks then
  57.         local coalCount=getItemCount("minecraft:coal")
  58.         if coalCount<10 then
  59.             selectSlotWith("minecraft:coal")
  60.             turtle.refuel(coalCount/2)     
  61.         else
  62.             selectSlotWith("minecraft:coal")
  63.             turtle.refuel(coalCount)
  64.         end
  65.     end
  66.     updateInventory()
  67.     if direction=="up" then
  68.         for i=1,blocks do
  69.             if digging==true then
  70.                 for i=1,10 do
  71.                     turtle.digUp()
  72.                     turtle.dig()
  73.                     turtle.attackUp()
  74.                     if turtle.detectUp()==false then
  75.                         break
  76.                     end
  77.                 end
  78.             end
  79.             moved=turtle.up()
  80.             if moved==true then
  81.                 changePos("y",1)
  82.                 bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  83.             else
  84.                 return false
  85.             end
  86.         end
  87.     elseif direction=="down" then
  88.         for i=1,blocks do
  89.             if digging==true then
  90.                 for i=1,10 do
  91.                     turtle.digDown()
  92.                     turtle.dig()
  93.                     turtle.attackDown()
  94.                     if turtle.detectDown()==false then
  95.                         break
  96.                     end
  97.                 end
  98.             end
  99.             moved=turtle.down()
  100.             if moved==true then
  101.                 changePos("y",-1)
  102.                 bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  103.             else
  104.                 return false
  105.             end
  106.         end
  107.     elseif direction=="back" then
  108.         local facing=string.lower(getFacing())
  109.         for i=1,blocks do  
  110.             local moved=turtle.back()
  111.             if moved==false then return false end  
  112.             if facing=="north" then
  113.                 changePos("z",1)
  114.             elseif facing=="east" then
  115.                 changePos("x",-1)
  116.             elseif facing=="south" then
  117.                 changePos("z",-1)
  118.             elseif facing=="west" then
  119.                 changePos("x",1)       
  120.             end
  121.             bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  122.         end
  123.     else
  124.         local facing=string.lower(getFacing())
  125.         for i=1,blocks do
  126.             if digging==true then
  127.                 turtle.dig()
  128.                 turtle.digUp()
  129.                 turtle.digDown()
  130.                 turtle.attack()
  131.             end
  132.             local moved=turtle.forward()
  133.             if moved==false then return false end
  134.             if facing=="north" then
  135.                 changePos("z",-1)
  136.             elseif facing=="east" then
  137.                 changePos("x",1)   
  138.             elseif facing=="south" then
  139.                 changePos("z",1)
  140.             elseif facing=="west" then
  141.                 changePos("x",-1)      
  142.             end
  143.             bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  144.         end
  145.     end
  146.     return true
  147. end
  148.  
  149. function move(direction,blocks,digging)
  150.     local fuelLevel=turtle.getFuelLevel()
  151.     if fuelLevel<blocks then
  152.         local coalCount=getItemCount("minecraft:coal")
  153.         if coalCount<10 then
  154.             selectSlotWith("minecraft:coal")
  155.             turtle.refuel(coalCount/2)     
  156.         else
  157.             selectSlotWith("minecraft:coal")
  158.             turtle.refuel(coalCount)
  159.         end
  160.     end
  161.     updateInventory()
  162.     if direction=="up" then
  163.         for i=1,blocks do
  164.             if digging==true then
  165.                 for i=1,10 do
  166.                     turtle.digUp()
  167.                     turtle.attackUp()
  168.                     if turtle.detectUp()==false then
  169.                         break
  170.                     end
  171.                 end
  172.             end
  173.             moved=turtle.up()
  174.             if moved==true then
  175.                 changePos("y",1)
  176.                 bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  177.             else
  178.                 return false
  179.             end
  180.         end
  181.     elseif direction=="down" then
  182.         for i=1,blocks do
  183.             if digging==true then
  184.                 for i=1,10 do
  185.                     turtle.digDown()
  186.                     turtle.attackDown()
  187.                     if turtle.detectDown()==false then
  188.                         break
  189.                     end
  190.                 end
  191.             end
  192.             moved=turtle.down()
  193.             if moved==true then
  194.                 changePos("y",-1)
  195.                 bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  196.             else
  197.                 return false
  198.             end
  199.         end
  200.     elseif direction=="back" then
  201.         local facing=string.lower(getFacing())
  202.         for i=1,blocks do  
  203.             local moved=turtle.back()
  204.             if moved==false then return false end  
  205.             if facing=="north" then
  206.                 changePos("z",1)
  207.             elseif facing=="east" then
  208.                 changePos("x",-1)
  209.             elseif facing=="south" then
  210.                 changePos("z",-1)
  211.             elseif facing=="west" then
  212.                 changePos("x",1)       
  213.             end
  214.             bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  215.         end
  216.     else
  217.         local facing=string.lower(getFacing())
  218.         for i=1,blocks do
  219.             if digging==true then
  220.                 turtle.dig()
  221.                 turtle.attack()
  222.             end
  223.             local moved=turtle.forward()
  224.             if moved==false then return false end
  225.             if facing=="north" then
  226.                 changePos("z",-1)
  227.             elseif facing=="east" then
  228.                 changePos("x",1)   
  229.             elseif facing=="south" then
  230.                 changePos("z",1)
  231.             elseif facing=="west" then
  232.                 changePos("x",-1)      
  233.             end
  234.             bluenet.broadcast(pos.x..","..pos.y..","..pos.z,"replicator,position")
  235.         end
  236.     end
  237.     return true
  238. end
  239.  
  240. function updateInventory()
  241.     inventory={}
  242.     for i=1,16 do
  243.         local detail=turtle.getItemDetail(i)
  244.         inventory[i]=detail
  245.     end
  246.     if updating==true then
  247.         inventoryString=""
  248.         for i=1,16 do
  249.             local detail=inventory[i]
  250.             if detail==nil then
  251.                 inventoryString=inventoryString.."nil|"    
  252.             else
  253.                 inventoryString=inventoryString..detail.name..","..detail.count..","..detail.damage.."|"
  254.             end
  255.         end
  256.         bluenet.broadcast(inventoryString,"replicator,inventory")
  257.     end
  258. end
  259.  
  260. bluenet={}
  261. bluenet.equipModem=function(id,message,protocol)
  262.     local modem=false
  263.     for slot=1,16 do
  264.         local info=turtle.getItemDetail(slot)
  265.         if info~=nil and info.name=="ComputerCraft:CC-Peripheral" and info.damage==1 then
  266.             turtle.select(slot)
  267.             turtle.equipRight()
  268.             rednet.open("right")
  269.             modem=true
  270.             return true
  271.         end
  272.     end
  273.     if modem==false then
  274.         return false,"No modem found in inventory"
  275.     end
  276. end
  277.    
  278. bluenet.send=function()
  279.     if updating==true then
  280.         --modem=bluenet.equipModem()
  281.         --if not modem then return end
  282.         rednet.send(id,message,protocol)
  283.         --turtle.equipRight()
  284.     end
  285. end
  286. bluenet.broadcast=function(message,protocol)
  287.     if updating==true then
  288.         --modem=bluenet.equipModem()
  289.         --if not modem then return end
  290.         rednet.broadcast(message,protocol)
  291.         --turtle.equipRight()
  292.     end
  293. end
  294. bluenet.receive=function(protocol)
  295.     if updating==true then
  296.         --modem=bluenet.equipModem()
  297.         --if not modem then return end
  298.         rednet.receive(protocol)
  299.         --turtle.equipRight()
  300.     end
  301. end
  302.    
  303.  
  304.  
  305. local facingTable={
  306.     ["north"]=1,
  307.     ["east"]=2,
  308.     ["south"]=3,
  309.     ["west"]=4,
  310. }
  311.  
  312. local facingTable2={
  313.     [0]="west","north","east","south","west","north"
  314. }
  315.  
  316. function turn(direction)
  317.     if direction=="right" then
  318.         turtle.turnRight()
  319.         local currentFacingID=facingTable[facingDirection]
  320.         facingDirection=facingTable2[currentFacingID+1]
  321.     elseif direction=="left" then
  322.         turtle.turnLeft()
  323.         local currentFacingID=facingTable[facingDirection]
  324.         facingDirection=facingTable2[currentFacingID-1]
  325.     end
  326. end
  327.  
  328. function setFacing(facing)
  329.     local facing=string.lower(facing)
  330.     local currentFacing=string.lower(getFacing())
  331.     if facing==currentFacing then
  332.         return
  333.     else
  334.         local currentFacingIndex=facingTable[currentFacing]
  335.         local facingIndex=facingTable[facing]
  336.         local turns=facingIndex-currentFacingIndex
  337.         if turns<0 then
  338.             turns=turns+4
  339.         end
  340.         if turns==3 then
  341.             turn("left")
  342.         else
  343.             for i=1,turns do
  344.                 turn("right")
  345.             end
  346.         end
  347.     end
  348.     database.setData("position","facing",facing)
  349.     bluenet.broadcast(facing,"replicator,facing")
  350.    
  351. end
  352.  
  353. function getFacing()
  354.     return facingDirection
  355. end
  356.  
  357. function initFacing()
  358.     local heading=database.getData("position","facing")
  359.     if heading==false then
  360.         database.setData("position","facing","north")
  361.     end
  362.     facingDirection=heading
  363.     bluenet.broadcast(heading,"replicator,facing")
  364. end
  365.  
  366. function outputMessage(message,range,language)
  367.     if talking==true and type(message)=="string" then
  368.         if range==nil then
  369.             speaker.speak(message)
  370.         elseif lanugage==nil then
  371.             speaker.speak(message,range)
  372.         else
  373.             speaker.speak(message,range,language)
  374.         end
  375.     end
  376. end
  377.  
  378. function getItemCount(name)
  379.     local count=0
  380.     for i=1,16 do
  381.         local detail=turtle.getItemDetail(i)
  382.         if detail~=nil and detail.name==name then
  383.             count=count+detail.count
  384.         end
  385.     end
  386.     print(count)
  387.     return count
  388. end
  389.  
  390. function dumpItems(name,count)
  391.     if count<0 then
  392.         count=getItemCount(name)+count
  393.     end
  394.     for i=1,16 do
  395.         local detail=turtle.getItemDetail(i)
  396.         if detail~=nil and detail.name==name then
  397.             turtle.select(i)
  398.             if count>64 then
  399.                 turtle.dropDown(64)
  400.             else
  401.                 turtle.dropDown(count) 
  402.             end
  403.             count=count-detail.count
  404.             if count<=0 then
  405.                 break
  406.             end
  407.         end
  408.     end
  409.    
  410. end
  411.  
  412. function getInventoryStacks()
  413.     local items={}
  414.     for i=1,16 do
  415.         local detail=turtle.getItemDetail(i)
  416.         if detail~=nil then
  417.             if items[detail.name]==nil then
  418.                 items[detail.name]=detail.count
  419.             else
  420.                 items[detail.name]=items[detail.name]+detail.count
  421.             end
  422.         end
  423.     end
  424.     return items
  425. end
  426.  
  427. function selectSlotWith(name)
  428.     for i=1,16 do
  429.         local detail=turtle.getItemDetail(i)
  430.         if detail~=nil then
  431.             if detail.name==name then
  432.                 turtle.select(i)
  433.                 return true
  434.             end
  435.         end
  436.     end
  437.     return false
  438. end
  439.  
  440. function handleInventory()
  441.     local items=getInventoryStacks()
  442.     for item,count in pairs(items) do
  443.         local maxCount=database.getData("resources",item)
  444.         if maxCount==false then
  445.             if inventorySave==true then
  446.                 if database.getData("recipes",item)==false then
  447.                     maxCount=0
  448.                 else
  449.                     maxCount=1024
  450.                 end
  451.             else
  452.                 maxCount=1024
  453.             end
  454.         else
  455.             maxCount=tonumber(splitString(maxCount,",",2))
  456.         end
  457.         if item~=nil then
  458.             if item=="minecraft:coal" then
  459.                 if count>maxCount then
  460.                     for i=1,16 do
  461.                         local detail=turtle.getItemDetail(i)
  462.                         if detail~=nil and detail.name=="minecraft:coal" then
  463.                             turtle.select(i)
  464.                             turtle.refuel(count-maxCount)
  465.                         end
  466.                         break
  467.                     end
  468.                     --dumpItems(item,(count-maxCount))
  469.                 end
  470.             else
  471.                 if count>maxCount then
  472.                     dumpItems(item,(count-maxCount))
  473.                 end
  474.             end
  475.         end
  476.     end
  477. end
  478.  
  479. function condenseInventory()
  480.     for i=1,16 do
  481.         local detail=turtle.getItemDetail(i)
  482.         if detail~=nil then
  483.             local name=detail.name
  484.             for slot=i,16 do
  485.                 local slotDetail=turtle.getItemDetail(slot)
  486.                 if slotDetail~=nil and slotDetail.name==name then
  487.                 print(slotDetail.name)
  488.                     local freeCount=turtle.getItemSpace(slot)
  489.                     if freeCount>0 then
  490.                         if turtle.getSelectedSlot()~=i then
  491.                             turtle.select(i)
  492.                         end
  493.                         turtle.transferTo(slot,freeCount)
  494.                         if turtle.getItemCount(i)==0 then
  495.                             break
  496.                         end
  497.                     end
  498.                 end
  499.             end
  500.         end
  501.     end
  502. end
  503.  
  504. function findResource(name)
  505.     itemData=database.getData("resources",name)
  506.     local startCount=getItemCount(name)
  507.     local layers=splitString(itemData,",",1)
  508.     local bottom=splitString(layers,"/",1)
  509.     local top=splitString(layers,"/",2)
  510.     if tonumber(pos.y)<tonumber(bottom) or tonumber(pos.y)>tonumber(top) then
  511.         pathFind(pos.x,bottom,pos.z,true)
  512.     end
  513.     local startRow=pos.z
  514.     local row=1
  515.     local collumn=1
  516.     repeat
  517.         if row==16 then
  518.             if math.floor(collumn/2)==math.ceil(collumn/2) then
  519.                 turn("right")
  520.                 move("forward",1,true)
  521.                 turn("right")
  522.             else   
  523.                 turn("left")
  524.                 move("forward",1,true)
  525.                 turn("left")
  526.             end
  527.             collumn=collumn+1
  528.             row=0
  529.         end
  530.         local wentUp=false
  531.         repeat
  532.             local bool,inspection=turtle.inspect()
  533.             if bool~=false and inspection~=nil and inspection.name~=name then
  534.                 move("up",1,true)
  535.                 wentUp=true
  536.             end
  537.         until bool==false or  inspection==nil or inspection.name==name
  538.        
  539.         while turtle.detectDown()==false and wentUp==false do
  540.             move("down",1,false)
  541.         end    
  542.        
  543.         move("forward",1,false)
  544.         row=row+1
  545.         local bool,inspection=turtle.inspect() 
  546.     until inspection~=nil and inspection.name==name
  547. end
  548.  
  549. function cutTree()
  550.     bluenet.broadcast("Attempting to cut down a tree.")
  551.     local bool,block=turtle.inspect()
  552.     if block~=nil and block.name=="minecraft:log" then
  553.         turtle.dig()
  554.         move("forward",1)
  555.         repeat
  556.             move("up",1,true)
  557.             local bool,block=turtle.inspectUp()
  558.         until block==nil or block["name"]~="minecraft:log"
  559.         repeat
  560.             move("down",1,false)
  561.         until turtle.detectDown()
  562.     end
  563.     updateInventory()  
  564.     bluenet.broadcast("Finished attempting to cut down a tree.")
  565. end
  566.  
  567. local movementFails=0
  568.  
  569. function pathFind(x,y,z,digging)
  570.     movementFails=0
  571.     local digging=digging
  572.     if digging==nil then
  573.         digging=false
  574.     end
  575.     repeat
  576.         if movementFails>=2 then
  577.             digging=true
  578.         end
  579.         local yDistance=(pos.y-y)*-1
  580.         local yTravel=true
  581.         if yDistance>0 then
  582.             yTravel=move("up",yDistance,digging)
  583.         elseif yDistance<0 then
  584.             yTravel=move("down",yDistance*-1,digging)
  585.         end
  586.         local xDistance=(pos.x-x)*-1
  587.         local xTravel=true
  588.         if xDistance>0 then
  589.             setFacing("east")
  590.         elseif xDistance<0 then
  591.             setFacing("west")
  592.             xDistance=xDistance*-1
  593.         end
  594.         xTravel=move("forward",xDistance,digging)
  595.         local zDistance=(pos.z-z)*-1
  596.         local zTravel=true
  597.         if zDistance>0 then
  598.             setFacing("south")
  599.         elseif zDistance<0 then
  600.             setFacing("north")
  601.             zDistance=zDistance*-1
  602.         end
  603.         zTravel=move("forward",zDistance,digging)
  604.         movementFails=movementFails+1
  605.     until xTravel==true and zTravel==true and yTravel==true
  606.     sleep(0.1)
  607.     bluenet.broadcast("Finshed pathfinding to: "..x..","..y..","..z)
  608. end
  609.  
  610. function pathMine(x,y,z)
  611.     movementFails=0
  612.     local digging=true
  613.     repeat
  614.         local yDistance=(pos.y-y)*-1
  615.         local yTravel=true
  616.         if yDistance>0 then
  617.             yTravel=tunnel("up",yDistance,digging)
  618.         elseif yDistance<0 then
  619.             yTravel=tunnel("down",yDistance*-1,digging)
  620.         end
  621.         local xDistance=(pos.x-x)*-1
  622.         local xTravel=true
  623.         if xDistance>0 then
  624.             setFacing("east")
  625.         elseif xDistance<0 then
  626.             setFacing("west")
  627.             xDistance=xDistance*-1
  628.         end
  629.         xTravel=tunnel("forward",xDistance,digging)
  630.         local zDistance=(pos.z-z)*-1
  631.         local zTravel=true
  632.         if zDistance>0 then
  633.             setFacing("south")
  634.         elseif zDistance<0 then
  635.             setFacing("north")
  636.             zDistance=zDistance*-1
  637.         end
  638.         zTravel=tunnel("forward",zDistance,digging)
  639.         movementFails=movementFails+1
  640.     until xTravel==true and zTravel==true and yTravel==true
  641.     sleep(0.1)
  642.     bluenet.broadcast("Finshed pathfinding to: "..x..","..y..","..z)
  643. end
  644.  
  645. local craftingSlots={1,2,3,5,6,7,9,10,11,}
  646.  
  647. local craftSlot={1,2,3,nil,4,5,6,nil,7,8,9,nil,nil,nil,nil,nil}
  648.  
  649. local slotsForCrafting={
  650. [1]=true,
  651. [2]=true,
  652. [3]=true,
  653. [5]=true,
  654. [6]=true,
  655. [7]=true,
  656. [9]=true,
  657. [10]=true,
  658. [11]=true,
  659. }
  660.  
  661. function getRecipe(name,count)
  662.     local recipeData=database.getData("recipes",name)
  663.     if recipeData==false then
  664.         return false,"No recipe found for the item "..name.."!"
  665.     end
  666.     local recipeString=splitString(recipeData,"|",1)
  667.     local recipeType=splitString(recipeData,"|",2)
  668.     local recipeOutcome=splitString(recipeData,"|",3)
  669.     if recipeOutcome==false then
  670.         recipeOutcome=1
  671.     end
  672.     local recipe={}
  673.     if recipeType=="crafting" then
  674.         for i=1,9 do
  675.             itemName=splitString(recipeString,",",i)
  676.             if itemName=="air" or itemName=="nil" then
  677.                 recipe[i]=nil          
  678.             else
  679.                 recipe[i]=itemName
  680.             end
  681.         end
  682.         local requirements={}
  683.         for id,item in pairs(recipe) do
  684.             if tonumber(recipeOutcome)>1 then
  685.                 if requirements[item]==nil then
  686.                     requirements[item]=count/recipeOutcome
  687.                 else
  688.                     requirements[item]=requirements[item]+count/recipeOutcome
  689.                 end
  690.             else
  691.                 if requirements[item]==nil then
  692.                     requirements[item]=count
  693.                 else
  694.                     requirements[item]=requirements[item]+count
  695.                 end
  696.             end
  697.         end
  698.         return recipe,requirements,recipeType
  699.     elseif recipeType=="smelting" then
  700.         itemName=splitString(recipeString,",",1)
  701.         if itemName=="air" or itemName=="nil" then
  702.             recipe[1]=nil          
  703.         else
  704.             recipe[1]=itemName
  705.         end
  706.         local requirements={[itemName]=count,["minecraft:coal"]=math.ceil(count/8)}
  707.         return recipe,requirements,recipeType,recipeOutcome
  708.     end
  709. end
  710.  
  711. function equipPeripheral(peripheral,damage)
  712.     local modem=false
  713.     for slot=1,16 do
  714.         local info=turtle.getItemDetail(slot)
  715.         if info~=nil and info.name==peripheral and info.damage==damage or info~=nil and info.name==peripheral and damage==nil then
  716.             turtle.select(slot)
  717.             turtle.equipRight()
  718.             if peripheral=="ComputerCraft:CC-Peripheral" and damage==1 then
  719.                 rednet.open("right")
  720.             end
  721.             modem=true
  722.             return true
  723.         end
  724.     end
  725.     if modem==false then
  726.         return false,"No peripheral of that type found in inventory"
  727.     end
  728. end
  729.  
  730. function craft(name,craftingCount)
  731.     if craftingCount==nil then
  732.         craftingCount=1
  733.     end
  734.     local recipe,requirements,recipeType,recipeOutcome=getRecipe(name,craftingCount)
  735.     if recipe==false then
  736.         print(requirements)
  737.         return false
  738.     end
  739.     for id,data in pairs(requirements) do
  740.         print(id.."*"..data)
  741.     end
  742.     shortOnResources=false
  743.     for item,count in pairs(requirements) do
  744.         print(item.."*"..count)
  745.         local countNeeded=count
  746.         for slot=1,16 do
  747.             turtle.select(slot)
  748.             local slotItem=turtle.getItemDetail(slot)
  749.             if slotItem~=nil then
  750.                 if slotItem.name==item then
  751.                     countNeeded=countNeeded-slotItem.count
  752.                 end
  753.                 if countNeeded<=0 then
  754.                     break
  755.                 end
  756.             end
  757.         end
  758.         if countNeeded>0 then
  759.             shortOnResources=true
  760.             break
  761.         end
  762.     end
  763.     if shortOnResources==true then
  764.         bluenet.broadcast("Not enough resources for crafting procedure.")
  765.         print("Not enough resources for crafting procedure.")
  766.         return false
  767.     else
  768.         if recipeType=="crafting" then
  769.             turtle.digDown()
  770.             local chest=selectSlotWith("minecraft:chest")
  771.             if chest==true then
  772.                 turtle.placeDown()
  773.             end
  774.             for slot=1,16 do
  775.                 if turtle.getItemCount(slot)>0 then
  776.                     for i=1,16 do
  777.                         if turtle.getItemCount(i)==0 and slotsForCrafting[i]==nil then
  778.                             turtle.select(slot)
  779.                             local detail=turtle.getItemDetail(slot)
  780.                             if requirements[detail.name]==nil and detail.name~="minecraft:crafting_table" then
  781.                                 turtle.dropDown(turtle.getItemCount(slot))
  782.                             else
  783.                                 turtle.transferTo(i,turtle.getItemCount(slot))
  784.                             end
  785.                             updateInventory()  
  786.                             break
  787.                         end
  788.                     end            
  789.                 end
  790.             end
  791.             equipPeripheral("minecraft:crafting_table")
  792.             for slot=1,9 do
  793.                 local item=recipe[slot]
  794.                 for i=1,16 do
  795.                     local itemDetail=turtle.getItemDetail(i)
  796.                     if itemDetail~=nil and itemDetail.name==item and slotsForCrafting[i]==nil then
  797.                         print(i..":::"..craftingSlots[slot])
  798.                         if turtle.getItemCount(craftingSlots[slot])>0 then
  799.                             turtle.select(craftingSlots[slot])
  800.                             turtle.dropDown(turtle.getItemCount(craftingSlots[slot]))
  801.                         end
  802.                         turtle.select(i)
  803.                         turtle.transferTo(craftingSlots[slot],craftingCount)
  804.                         break
  805.                     end
  806.                 end
  807.             end
  808.             for i=1,16 do
  809.                 if slotsForCrafting[i]==nil and turtle.getItemCount(i)>0 or slotsForCrafting[i]==true and recipe[craftSlot[i]]==nil and turtle.getItemCount(i)>0 then
  810.                     turtle.select(i)
  811.                     turtle.dropDown(turtle.getItemCount(i))
  812.                 end
  813.             end
  814.             turtle.select(1)
  815.             turtle.craft(craftingCount)
  816.             for i=1,16 do
  817.                 turtle.suckDown()  
  818.             end
  819.             turtle.digDown()
  820.             equipPeripheral("ComputerCraft:CC-Peripheral",1)
  821.             updateInventory()  
  822.             sleep(0.1)
  823.             bluenet.broadcast("Finished crafting a "..name)
  824.         elseif recipeType=="smelting" then
  825.             local furnaceSlot=nil
  826.             local coalSlot=nil
  827.             for i=1,16 do
  828.                 local itemDetail=turtle.getItemDetail(i)
  829.                 if itemDetail~=nil then
  830.                     if itemDetail.name=="minecraft:furnace" then
  831.                         furnaceSlot=i
  832.                     elseif itemDetail.name=="minecraft:furnace" then
  833.                         local coalSlot=i
  834.                     end
  835.                 end
  836.             end
  837.             if furnaceSlot==nil then
  838.                 craft("minecraft:furnace")
  839.                
  840.             end
  841.             local furnaceSlot=nil
  842.             local coalSlot=nil
  843.             for i=1,16 do
  844.                 local itemDetail=turtle.getItemDetail(i)
  845.                 if itemDetail~=nil then
  846.                     if itemDetail.name=="minecraft:furnace" then
  847.                         furnaceSlot=i
  848.                     elseif itemDetail.name=="minecraft:coal" then
  849.                         coalSlot=i
  850.                     end
  851.                 end
  852.             end
  853.             if furnaceSlot==nil or coalSlot==nil then
  854.                 bluenet.broadcast("Failed to smelt. Coal:"..tostring(coalSlot).." furnace:"..tostring(furnaceSlot))
  855.                 print("Failed to smelt. Coal:"..tostring(coalSlot).." furnace:"..tostring(furnaceSlot))
  856.                 return false
  857.             end
  858.             turtle.select(furnaceSlot)
  859.             turtle.dig()
  860.             turtle.place()
  861.            
  862.             turtle.digUp()
  863.             move("up",1,true)
  864.             turtle.dig()
  865.             move("forwards",1,true)
  866.             for i=1,16 do
  867.                 local itemDetail=turtle.getItemDetail(i)
  868.                 if itemDetail~=nil and itemDetail.name==recipe[1] then
  869.                     turtle.select(i)
  870.                     turtle.dropDown(craftingCount)
  871.                     break
  872.                 end
  873.             end
  874.             move("back",1,true)
  875.             move("down",2,true)
  876.             move("forwards",1,true)
  877.             for i=1,16 do
  878.                 local itemDetail=turtle.getItemDetail(i)
  879.                 if itemDetail~=nil and itemDetail.name=="minecraft:coal" then
  880.                     turtle.select(i)
  881.                     turtle.dropUp(math.ceil(craftingCount/8))
  882.                     break
  883.                 end
  884.             end
  885.             sleep(10*craftingCount)
  886.             for i=1,16 do
  887.                 turtle.suckUp()
  888.             end
  889.             move("back",1,true)
  890.             move("up",1,true)
  891.             turtle.dig()
  892.             bluenet.broadcast("Finished smelting a "..name)
  893.         end
  894.     end
  895. end
  896.  
  897. function splitString(sourceString,splittingChar,index)
  898.     print(tostring(sourceString)..","..splittingChar..","..index)
  899.     results={}
  900.     currentWord=""
  901.     for i=1,string.len(sourceString) do
  902.         letter=string.sub(sourceString,i,i)
  903.         if letter==splittingChar then
  904.             results[#results+1]=currentWord
  905.             currentWord=""
  906.         else
  907.             currentWord=currentWord..letter
  908.         end
  909.     end
  910.     results[#results+1]=currentWord
  911.     return results[index]
  912. end
  913.  
  914. function gather(item,count)
  915.     itemData=database.getData("resources",item)
  916.     local startCount=getItemCount(item)
  917.     local layers=splitString(itemData,",",1)
  918.     local bottom=splitString(layers,"/",1)
  919.     local top=splitString(layers,"/",2)
  920.     local process=splitString(itemData,",",4)
  921.     if item=="minecraft:log" then
  922.         repeat
  923.             findResource(item)
  924.             cutTree()
  925.         until getItemCount(item)>=count+startCount
  926.         return true
  927.     elseif itemData==false then
  928.         return false,"No knowledge on that item"
  929.     elseif process=="finding" then
  930.         repeat
  931.             findResource(item)
  932.             turtle.dig()
  933.             turtle.suck(64)
  934.         until getItemCount(item)>=count+startCount
  935.     elseif process=="mining" then
  936.         local layer=math.random(bottom,top)
  937.         pathFind(pos.x,layer,pos.z,true)
  938.         local startRow=pos.z
  939.         local row=1
  940.         local length=16
  941.         repeat
  942.             pathMine(pos.x+length*-1,pos.y,startRow+row,true)
  943.             row=row+1
  944.             length=length*-1
  945.             print(getItemCount(item)..":"..count+startCount)
  946.             handleInventory()
  947.         until getItemCount(item)>=count+startCount     
  948.     end
  949.     handleInventory()
  950.     condenseInventory()
  951. end
  952.  
  953. function createTurtle()
  954.     local startPosition={pos.x,pos.y,pos.z}
  955.     if getItemCount("minecraft:log")<16 then
  956.         gather("minecraft:log",16)
  957.     end
  958.     craft("minecraft:planks",2)
  959.     craft("minecraft:chest")
  960.     if getItemCount("minecraft:coal")<16 then
  961.         gather("minecraft:coal",16)
  962.     end
  963.     if getItemCount("minecraft:iron_ore")<7 then
  964.         gather("minecraft:iron_ore",7)
  965.     end
  966.     if getItemCount("minecraft:redstone")<1 then
  967.         gather("minecraft:redstone",1)
  968.     end
  969.     if getItemCount("minecraft:sand")<6 then
  970.         --findResource("minecraft:water","minecraft:flowing_water")
  971.         pathFind(startPosition[1],startPosition[2],startPosition[3],true)
  972.         move("down",1,true)
  973.         gather("minecraft:sand",6)
  974.     end
  975.     if getItemCount("minecraft:cobblestone")<20 then
  976.         gather("minecraft:cobblestone",20)
  977.     end
  978.     if getItemCount("minecraft:iron_ingot")<7 then
  979.         craft("minecraft:iron_ingot",7)
  980.     end
  981.     if getItemCount("minecraft:glass")<6 then
  982.         craft("minecraft:glass",6)
  983.     end
  984.     if getItemCount("minecraft:glass_pane")<1 then
  985.         craft("minecraft:glass_pane",1)
  986.     end
  987.     craft("minecraft:planks",3)
  988.     craft("minecraft:crafting_table")
  989.     craft("minecraft:chest")
  990.     craft("minecraft:stone",7)
  991.     craft("ComputerCraft:CCComputer")
  992.     craft("ComputerCraft:CCTurtle")
  993. end
  994.  
  995. function main()
  996.     local startPosition={pos.x,pos.y,pos.z}
  997.     createTurtle()
  998.     pathFind(startPosition[1],startPosition[2],startPosition[3],true)
  999.     --[[
  1000.     pathFind(98,69,200)
  1001.     cutTree()
  1002.     pathFind(105,62,192)
  1003.     craft("minecraft:planks",12)
  1004.     craft("minecraft:crafting_table")
  1005.     craft("minecraft:chest")
  1006.     craft("minecraft:iron_ingot",9)
  1007.     craft("ComputerCraft:CCComputer")
  1008.     craft("ComputerCraft:CCTurtle")
  1009.     ]]
  1010.     --gather("minecraft:iron_ore",10)
  1011. end
  1012.  
  1013.  
  1014. bluenet.broadcast("Booting up")
  1015. sleep(0.1)
  1016. updateInventory()  
  1017. sleep(0.1)
  1018. initFacing()
  1019. sleep(0.1)
  1020. getFilePos()
  1021. sleep(0.1)
  1022. condenseInventory()
  1023. sleep(0.1)
  1024. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement