kLeeNex24

turtlex API for ComputerCraft

Sep 16th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.04 KB | None | 0 0
  1. local native=turtle
  2.  
  3. --[[ unchanged methods
  4. function attack()
  5. function attackUp()
  6. function attackDown()
  7. --]]
  8.  
  9. local dirMap={north=0, east=1, south=2, west=3}
  10. local dirVMap={}
  11. dirVMap[0]=   { x= 0, y= 0, z=-1, }
  12. dirVMap[1]=   { x= 1, y= 0, z= 0, }
  13. dirVMap[2]=   { x= 0, y= 0, z= 1, }
  14. dirVMap[3]=   { x=-1, y= 0, z= 0, }
  15.  
  16. local position=vector.new(0,0,0)
  17. local direction=0
  18. local selectedSlot = 1
  19. local myInventory=nil
  20. local posStack={}
  21.  
  22. --load and setup the inventory
  23.  
  24. local function loadPosition()
  25.  
  26.   if fs.exists(".position") then
  27.     local file=fs.open(".position","r")
  28.     position.x=file.readLine()+0
  29.     position.y=file.readLine()+0
  30.     position.z=file.readLine()+0
  31.     direction=file.readLine()+0
  32.     file.close()
  33.     directionV=dirVMap[direction]
  34.   end
  35. end
  36.  
  37. local function loadSelected()
  38.   if fs.exists(".selectedSlot") then
  39.     file=fs.open(".selectedSlot","r")
  40.     selectedSlot=file.readLine()+0
  41.     file.close()
  42.   end
  43. end
  44.  
  45. local function loadPosStack()
  46.   if fs.exists(".posStack") then
  47.     posStack={}
  48.     local file=fs.open(".posStack","r")
  49.     local count=file.readLine()+0
  50.     for i=1,count,1 do
  51.       posStack[i]={vector.new(0,0,0),0}
  52.       posStack[i].x=file.readLine()+0
  53.       posStack[i].y=file.readLine()+0
  54.       posStack[i].z=file.readLine()+0
  55.       posStack[i][2]=file.readLine()+0
  56.       file.close()
  57.     end
  58.   end  
  59.  
  60. end
  61.  
  62. local function loadState()
  63.   loadPosition()
  64.   loadSelected()
  65.   loadPosStack()
  66.   --[[if fs.exists(".inventory") then
  67.     myInventory=inventory.load(".inventory")
  68.     myInventory.isMe=true
  69.   else
  70.     myInventory=inventory.create(16,true)
  71.   end]]
  72.   myInventory=inventory.load(".inventory")
  73.   if myInventory==nil then
  74.     myInventory=inventory.create(16,true)
  75.   else
  76.     myInventory.isMe=true
  77.   end
  78.   myInventory.autoSave=true
  79. end
  80.  
  81. loadState()  
  82.  
  83. local function savePosition()
  84.   local file=fs.open(".position","w")
  85.   file.write(position.x.."\n"..position.y.."\n"..position.z.."\n"..direction.."\n")
  86.   file.close()
  87. end
  88.  
  89. local function saveSelected()
  90.   local file=fs.open(".selectedSlot","w")
  91.   file.write(selectedSlot.."\n")
  92.   file.close()
  93. end
  94.  
  95. local function savePosStack()
  96.   local file=fs.open(".posStack","w")
  97.   file.write(#posStack.."\n")
  98.   for i=1,#posStack,1 do
  99.     file.write(posStack[i][1].x.."\n"..posStack[i][1].y.."\n"..posStack[i][1].z.."\n"..posStack[i][2])
  100.   end
  101.   file.close()
  102. end
  103.  
  104. local function saveState()
  105.   savePosition()
  106.   saveSelected()
  107.   savePosStack()
  108.   inventory.save(myInventory,".inventory")
  109. end
  110.  
  111. --[[
  112. compares actual turtle inventory to myInventory, updates all quantities,
  113. 0s type for newly-empty slots, and attempts to identify any new items, attaching "unknown" tags
  114. to those that do not match available items
  115. --]]
  116. function updateInventory()
  117.   --save selected slot to reselect when done
  118.   local changed=false
  119.   local prevSlot=selectedSlot
  120.   local addedStuff={}
  121.   local removedStuff={}
  122.   for i=1,16,1 do
  123.     local turtleCount=native.getItemCount(i)
  124.     local invCount=myInventory.slots[i].quantity
  125.     if invCount~=turtleCount then
  126.       --item count has changed...
  127.       changed=true
  128.       if invCount==0 then
  129.         --we had none before, have to compare to other types
  130.         select(i)
  131.         local found=false      
  132.         for j=1,16,1 do
  133.           if i~=j then
  134.             if myInventory.slots[j].type~=0 and compareTo(j) then
  135.               --got a match
  136.               myInventory.slots[i].type=myInventory.slots[j].type
  137.               found=true
  138.               break
  139.             end
  140.           end
  141.         end
  142.         if not found then
  143.           myInventory.slots[i].type=myInventory:getNextUnknownType()
  144.         end
  145.       elseif turtleCount==0 then
  146.         --add to dropped
  147.         removedStuff[#removedStuff+1]={
  148.           type=myInventory.slots[i].type,
  149.           count=myInventory.slots[i].quantity,
  150.           slot=i,
  151.         }
  152.         --we don't have anymore, zero type
  153.         myInventory.slots[i].type=0
  154.       end
  155.       --if we have more now...
  156.       if turtleCount>invCount then
  157.         --add to newstuff list
  158.         addedStuff[#addedStuff+1]= {
  159.           type=myInventory.slots[i].type,
  160.           count=turtleCount-invCount,
  161.           slot=i,
  162.         }
  163.       end
  164.       --in ALL cases, update quantity
  165.       myInventory.slots[i].quantity=native.getItemCount(i)
  166.     end
  167.   end
  168.   if changed then
  169.     inventory.save(myInventory,".inventory")
  170.   end
  171.   select(prevSlot)
  172.   return changed, addedStuff, removedStuff
  173. end
  174.  
  175.  
  176.  
  177. local function movehelp(moveFunc, unmoveFunc, digFunc, detectFunc, dirv, count, onFail)
  178.   if count==nil then count=1 end
  179.   for i=1,count,1 do
  180.     if moveFunc()==false then
  181.       if onFail=="return" then
  182.         local succ,dist
  183.         succ,dist=unmoveFunc(i-1,"stop")
  184.         return false, i-1-dist
  185.       elseif onFail=="dig" then
  186.         local res=false
  187.         local tries=0
  188.         repeat
  189.           --retry..
  190.           if detectFunc() then
  191.             digFunc()
  192.           end
  193.           os.sleep(.2)          
  194.           res=moveFunc()
  195.           tries=tries+1
  196.         until res==true or tries>=10
  197.         if res==false then
  198.           return false,i-1
  199.         end
  200.       else --default stop
  201.         return false, i-1
  202.       end
  203.     end
  204.     position=position+dirv
  205.   end
  206.   return true, count
  207. end
  208.  
  209. function forward(count, onFail)
  210.   local res1,res2=movehelp(native.forward,back,dig,native.detect,dirVMap[direction], count, onFail)
  211.   savePosition()
  212.   return res1,res2
  213. end
  214.  
  215. function back(count, onFail)
  216.   local res1,res2=movehelp(native.back,forward,digBack,detectBack,(dirVMap[(direction+2)%4]), count, onFail)
  217.   savePosition()
  218.   return res1,res2
  219. end
  220.  
  221. function up(count, onFail)
  222.   local res1,res2=movehelp(native.up,down,digUp,native.detectUp,vector.new(0,1,0), count, onFail)
  223.   savePosition()
  224.   return res1,res2
  225. end
  226.  
  227. function down(count, onFail)
  228.   local res1,res2=movehelp(native.down,up,digDown,native.detectDown,vector.new(0,-1,0), count, onFail)
  229.   savePosition()
  230.   return res1,res2
  231. end
  232.  
  233. function left(count, onFail)
  234.   turnLeft()
  235.   local res,c=forward(count,onFail)
  236.   turnRight()
  237.   savePosition()
  238.   return res,c
  239. end
  240.  
  241. function right(count, onFail)
  242.   turnRight()
  243.   local res,c=forward(count,onFail)
  244.   turnLeft()
  245.   return res,c
  246. end
  247.  
  248. function turnLeft()
  249.   native.turnLeft()
  250.   direction=(direction+3)%4
  251.   savePosition()
  252. end
  253.  
  254. function turnRight()
  255.   native.turnRight()
  256.   direction=(direction+1)%4
  257.   savePosition()
  258. end
  259.  
  260. function turnAround()
  261.   native.turnLeft()
  262.   direction=(direction+1)%4
  263.   native.turnLeft()
  264.   direction=(direction+1)%4
  265.   savePosition()
  266. end
  267.  
  268.  
  269. function getPosition()
  270.    return position.x,position.y,position.z
  271. end
  272.  
  273. function getDirection()
  274.    return direction
  275. end
  276.  
  277.  
  278. function move(x, y, z, onBlocked)
  279.   local succ, dist, blocked, done
  280.   local prevDir=direction
  281.   local moves={}
  282.   local dig=false
  283.   while true do
  284.     blocked=true
  285.     done=true
  286.     if x>0 then
  287.       face("east")
  288.       succ,dist=forward(x,"stop")
  289.       x=x-dist
  290.       if dist>0 then
  291.         moves[#moves+1]={dir="east",dist=dist}
  292.         blocked=false
  293.       end        
  294.       done=done and succ
  295.     elseif x<0 then
  296.       face("west")
  297.       succ,dist=forward(-x,"stop")
  298.       x=x+dist
  299.       if dist>0 then
  300.         moves[#moves+1]={dir="west",dist=dist}        
  301.         blocked=false
  302.       end
  303.       done=done and succ
  304.     end
  305.     if y>0 then
  306.       succ,dist=up(y,"stop")
  307.       y=y-dist
  308.       if dist>0 then
  309.         moves[#moves+1]={dir="up",dist=dist}
  310.         blocked=false
  311.       end
  312.       done=done and succ
  313.     elseif y<0 then
  314.       succ,dist=down(-y,"stop")
  315.       y=y+dist
  316.       if dist>0 then
  317.         moves[#moves+1]={dir="down",dist=dist}
  318.         blocked=false
  319.       end
  320.       done=done and succ
  321.     end
  322.     if z>0 then
  323.       face("south")
  324.       succ,dist=forward(z,"stop")
  325.       z=z-dist
  326.       if dist>0 then
  327.         moves[#moves+1]={dir="south",dist=dist}
  328.         blocked=false
  329.       end
  330.       done=done and succ
  331.     elseif z<0 then
  332.       face("north")
  333.       succ,dist=forward(-z,"stop")
  334.       z=z+dist
  335.       if dist>0 then
  336.         moves[#moves+1]={dir="north",dist=dist}
  337.         blocked=false
  338.       end
  339.       done=done and succ
  340.     end
  341.     if done then
  342.       return true
  343.     end
  344.     if blocked then
  345.       if onBlocked=="return" then
  346.         for i=#moves,1,-1 do
  347.           if moves[i].dir=="north" then
  348.             face("south")
  349.             forward(moves[i].dist)
  350.           elseif moves[i].dir=="south" then
  351.             face("north")
  352.             forward(moves[i].dist)
  353.           elseif moves[i].dir=="east" then
  354.             face("west")
  355.             forward(moves[i].dist)
  356.           elseif moves[i].dir=="west" then
  357.             face("east")
  358.             forward(moves[i].dist)
  359.           elseif moves[i].dir=="up" then
  360.             down(moves[i].dist)
  361.           elseif moves[i].dir=="down" then
  362.             up(moves[i].dist)
  363.           end
  364.         end
  365.         face(prevDir)
  366.         return false
  367.       elseif onBlocked=="dig" then
  368.         succ=true
  369.         if x>0 then
  370.           face("east")
  371.           succ=succ and forward(x,"dig")
  372.         elseif x<0 then
  373.           face("west")
  374.           succ=succ and forward(x,"dig")
  375.         end
  376.         if z>0 then
  377.           face("north")
  378.           succ=succ and forward(z,"dig")
  379.         elseif x<0 then
  380.           face("south")
  381.           succ=succ and forward(z,"dig")
  382.         end
  383.         if y>0 then
  384.           succ=succ and up(y,"dig")
  385.         elseif y<0 then
  386.           succ=succ and down(y,"dig")
  387.         end
  388.         return succ
  389.       else
  390.         return false
  391.       end
  392.     end
  393.   end
  394. end
  395.  
  396. function goto(x,y,z,onBlocked)
  397.   return move(x-position.x,y-position.y,z-position.z,onBlocked)
  398. end
  399.  
  400. function face(newDirection)
  401.   if type(newDirection)=="string" then
  402.     newDirection=dirMap[newDirection]
  403.   end
  404.   turns=newDirection-direction
  405.   if turns>2 then turns=turns-4 end
  406.   if turns<-2 then turns=turns+4 end
  407.   if turns<0 then
  408.     for i=1,-turns,1 do
  409.       native.turnLeft()
  410.     end
  411.   elseif turns>0 then
  412.     for i=1,turns,1 do
  413.       native.turnRight()
  414.     end
  415.   end
  416.   direction=newDirection
  417.   savePosition()
  418. end
  419.  
  420.  
  421. function pushPos()
  422.   posStack[#posStack+1]={position,direction}
  423.   savePosStack()
  424. end
  425.  
  426. function popPos(go)
  427.   if go==nil then go=true end
  428.   local t=posStack[#posStack]
  429.   local pos=t[1]
  430.   local dir=t[2]
  431.   posStack[#posStack]=nil
  432.   savePosStack()
  433.   if go then
  434.     local res=goto(pos.x,pos.y,pos.z)
  435.     face(dir)
  436.     return res
  437.   else
  438.     return true
  439.   end
  440. end
  441.  
  442. function topPos()
  443.   return posStack[#posStack]
  444. end
  445.  
  446. function getPosStackSize()
  447.   return #posStack
  448. end
  449.  
  450. function getPosStackItem(index)
  451.   if type(index)~="number" then
  452.     error("turtlex:getPosStackItem: parameter must be a number!")
  453.   end
  454.   if index<=0 or index>#posStack then
  455.     error("turtlex:getPosStackItem: index out of range!")
  456.   end
  457.   return posStack[index]
  458. end
  459.  
  460. function clearStack()
  461.   posStack={}
  462.   savePosStack()
  463. end
  464.  
  465.  
  466. function select(index)
  467.   succ,err = pcall(native.select,index)
  468.   if succ then
  469.     selectedSlot=index
  470.     saveSelected()
  471.     return succ
  472.   else
  473.     return false, err
  474.   end
  475. end
  476.  
  477. function getSelected()
  478.   return selectedSlot, myInventory.slots[selectedSlot].type
  479. end
  480.  
  481. function getInventory()
  482.   return myInventory
  483. end
  484.  
  485. function getItemType(slot)
  486.   local prevSlot=selectedSlot
  487.   if slot~=nil then
  488.     select(slot)
  489.   else
  490.     slot=selectedSlot
  491.   end
  492.   local res=myInventory.slots[selectedSlot].type
  493.   if slot~=prevSlot then
  494.     select(prevSlot)
  495.   end
  496.   return res
  497. end
  498.  
  499. function setItemType(type, slot)
  500.   if slot==nil then
  501.     slot=selectedSlot
  502.   end
  503.   myInventory:setType(slot,type)
  504. end
  505.  
  506.  
  507. function getItemCount(slot)
  508.   if slot==nil then slot=selectedSlot end
  509.   return native.getItemCount(slot)
  510. end
  511.  
  512. function getItemSpace(slot)
  513.   if slot==nil then slot=selectedSlot end
  514.   return native.getItemSpace(slot)
  515. end
  516.  
  517. function findItemType(type, after)
  518.   return myInventory:findSlot(type,after)
  519. end
  520.  
  521. function displayInv()
  522.   myInventory:display()
  523. end
  524.  
  525. local function digHelp(slot,digFunc)
  526.   local prev=selectedSlot
  527.   local items={}
  528.   if slot~=nil then
  529.     select(slot)
  530.   else
  531.     slot=prev
  532.   end
  533.  
  534.   local res=digFunc()
  535.   if res then
  536.     --successfully dug, attempt to identify
  537.     local suc,add,sub=updateInventory()
  538.     if suc then
  539.       items=add
  540.     end
  541.   end
  542.  
  543.   --reselect previous slot
  544.   if slot~=prev then
  545.     select(prev)
  546.   end
  547.  
  548.   return res, items
  549. end
  550.  
  551. function dig(slot)
  552.   return digHelp(slot,native.dig)
  553. end
  554.  
  555. function digUp(slot)
  556.   return digHelp(slot,native.digUp)  
  557. end
  558.  
  559. function digDown(slot)
  560.   return digHelp(slot,native.digDown)
  561. end
  562.  
  563. function digBack(slot)
  564.   turnAround()
  565.   res=dig(slot)
  566.   turnAround()
  567.   return res
  568. end
  569.  
  570. function digLeft(slot)
  571.   turnLeft()
  572.   res=dig(slot)
  573.   turnRight()
  574.   return res
  575. end
  576.  
  577. function digRight(slot)
  578.   turnRight()
  579.   res=dig(slot)
  580.   turnLeft()
  581.   return res
  582. end
  583.  
  584. local function placeHelp(slot,placeFunc)
  585.   local prev=selectedSlot
  586.   if slot~=nil then
  587.     select(slot)
  588.   else
  589.     slot=prev
  590.   end
  591.  
  592.   res=placeFunc()
  593.   if res then
  594.     --successfully placed
  595.     updateInventory()
  596.   end
  597.  
  598.   --restore selected slot
  599.   if slot~=prev then
  600.     select(prev)
  601.   end
  602.   return res
  603.  
  604. end
  605.  
  606. function place(index)
  607.   placeHelp(index,native.place)
  608. end
  609.  
  610. function placeUp(index)
  611.   placeHelp(index,native.placeUp)
  612. end
  613.  
  614. function placeDown(index)
  615.   placeHelp(index,native.placeDown)
  616. end
  617.  
  618. function placeBack(slot)
  619.   turnAround()
  620.   res=place(slot)
  621.   turnAround()
  622.   return res
  623. end
  624.  
  625. function placeLeft(slot)
  626.   turnLeft()
  627.   res=place(slot)
  628.   turnRight()
  629.   return res
  630. end
  631.  
  632. function placeRight(slot)
  633.   turnRight()
  634.   res=place(slot)
  635.   turnLeft()
  636.   return res
  637. end
  638.  
  639.  
  640. local function indexHelper(index)
  641.   local slot=selectedSlot
  642.   if index~=nil then
  643.     if type(index)=="string" then
  644.       slot=findItemType(index)
  645.       if slot==0 then
  646.         return nil,"turtlex:compare: no reference sample for type '"..index.."' in inventory!"
  647.       end
  648.     elseif type(index)=="number" then
  649.       slot=index
  650.     else
  651.       return nil, "turtlex:compare: invalid parameter, expected number or string, got "..type(index)
  652.     end
  653.   end
  654.   return slot
  655. end
  656.  
  657. local function compareHelp(index,compareFunc)
  658.   local prevSlot=selectedSlot
  659.   local slot, err=indexHelper(index)
  660.   if slot==nil then
  661.     return slot, err
  662.   end
  663.   if prevSlot~=slot then
  664.     select(slot)  
  665.   end
  666.   local res=compareFunc()
  667.   if slot~=prevSlot then
  668.     select(prevSlot)
  669.   end
  670.   return res
  671. end
  672.  
  673. function compare(index)
  674.   return compareHelp(index,native.compare)
  675. end
  676.  
  677. function compareUp(index)
  678.   return compareHelp(index,native.compareUp)
  679. end
  680.  
  681. function compareDown(index)
  682.   return compareHelp(index,native.compareDown)
  683. end
  684.  
  685. function compareLeft(index)
  686.   turtle.turnLeft()
  687.   local res,err=compare(index)
  688.   turtle.turnRight()
  689.   return res, err
  690. end
  691.  
  692. function compareRight(index)
  693.   turtle.turnRight()
  694.   local res,err=compare(index)
  695.   turtle.turnLeft()
  696.   return res
  697. end
  698.  
  699. function compareBack(index)
  700.   turtle.turnAround()
  701.   local res,err=compare(index)
  702.   turtle.turnAround()
  703.   return res, err
  704. end
  705.  
  706. function compareAll(index)
  707.   local prevSlot=selectedSlot
  708.   local slot, err=indexHelper(index)
  709.   if slot==nil then
  710.     return slot, err
  711.   end
  712.   if prevSlot~=slot then
  713.     select(slot)  
  714.   end
  715.   dirs={"forward","right","back","left"}
  716.   matches={}
  717.   if native.compareUp() then
  718.     matches[#matches+1]="up"
  719.   end
  720.   if native.compareDown() then
  721.     matches[#matches+1]="down"
  722.   end
  723.   for i=1,4,1 do  
  724.     if native.compare() then
  725.       matches[#matches+1]=dirs[i]
  726.     end
  727.     turtle.turnRight()
  728.   end
  729.    
  730.   if prevSlot~=slot then
  731.     select(prevSlot)
  732.   end
  733.   return #matches, matches
  734. end
  735.  
  736.  
  737. function compareTo(slotNum, index)
  738.   local prevSlot=selectedSlot
  739.   local slot,err=indexHelper(index)
  740.  
  741.   if slot==nil then
  742.     return slot,err
  743.   end
  744.  
  745.   if prevSlot~=slot then
  746.     select(slot)  
  747.   end
  748.   res=native.compareTo(slotNum)
  749.   if prevSlot~=slot then
  750.     select(prevSlot)
  751.   end  
  752.  
  753.   return res
  754. end
  755.  
  756.  
  757. detect=native.detect
  758. detectUp=detectUp
  759. detectDown=detectDown
  760.  
  761. function detectLeft()
  762.   turnLeft()
  763.   local res=detect()
  764.   turnRight()
  765.   return res
  766. end
  767.  
  768.  
  769. function detectRight()
  770.   turnRight()
  771.   local res=detect()
  772.   turnLeft()
  773.   return res
  774. end
  775.  
  776. function detectBack()
  777.   turnAround()
  778.   local res=detect()
  779.   turnAround()
  780.   return res
  781. end
  782.  
  783.  
  784. local function dropHelp(quantity, index, dropFunc)
  785.   if quantity==0 then
  786.     return true
  787.   end
  788.   local prevSlot=selectedSlot
  789.   local slot,err=indexHelper(index)
  790.   if slot==nil then
  791.     return false, err
  792.   end
  793.   if slot~=prevSlot then
  794.     select(slot)
  795.   end
  796.   local res=turtle.drop(quantity)
  797.   if res then
  798.     updateInventory()
  799.   end
  800.   if slot~=prevSlot then
  801.     select(prevSlot)
  802.   end
  803.   return res
  804. end
  805.  
  806. function drop(quantity, index)
  807.   return dropHelp(quantity,index,native.drop)
  808. end
  809.  
  810. function dropUp(quantity, index)
  811.   return dropHelp(quantity,index,native.dropUp)
  812. end
  813.  
  814. function dropDown(quantity, index)
  815.   return dropHelp(quantity,index,native.dropDown)
  816. end
  817.  
  818. function dropLeft(quantity,index)
  819.   turnLeft()
  820.   local res=dropHelp(quantity,index,native.drop)
  821.   turnRight()
  822.   return res
  823. end
  824.  
  825. function dropRight(quantity,index)
  826.   turnRight()
  827.   local res=dropHelp(quantity,index,native.drop)
  828.   turnLeft()
  829.   return res
  830. end
  831.  
  832. function dropBack(quantity,index)
  833.   turnAround()
  834.   local res=dropHelp(quantity,index,native.drop)
  835.   turnAround()
  836.   return res
  837. end
  838.  
  839.  
  840. local function suckHelp(slot)
  841.   local prevSlot=selectedSlot
  842.   if slot==nil then
  843.     slot=selectedSlot
  844.   end
  845.   if slot~=prevSlot then
  846.     select(slot)
  847.   end
  848.   local res=suckFunc()
  849.  
  850.   local succ, add={}, rem;
  851.   if res then
  852.     succ,add,rem=updateInventory()
  853.   end
  854.  
  855.   if slot~=prevSlot then
  856.     select(prevSlot)
  857.   end
  858.   return res, add
  859. end
  860.  
  861. function suck(slot)
  862.   return suckHelp(slot,native.suck)
  863. end
  864.  
  865. function suckUp(slot)
  866.   return suckHelp(slot,native.suckUp)
  867. end
  868.  
  869. function suckDown(slot)
  870.   return suckHelp(slot,native.suckDown)
  871. end
  872.  
  873. function suckLeft(slot)
  874.   turnLeft()
  875.   local res, qty=suckHelp(slot,native.suck)
  876.   turnRight()
  877.   return res,qty
  878. end
  879.  
  880. function suckRight(slot)
  881.   turnRight()
  882.   local res, qty=suckHelp(slot,native.suck)
  883.   turnLeft()
  884.   return res,qty
  885. end
  886.  
  887. function suckBack(slot)
  888.   turnAround()
  889.   local res, qty=suckHelp(slot,native.suck)
  890.   turnAround()
  891.   return res,qty
  892. end
  893.  
  894.  
  895. getFuelLevel=native.getFuelLevel
  896.  
  897.  
  898. function refuel(quantity, index)
  899.   local prevSlot=selectedSlot
  900.   local slot,err=indexHelper(index)
  901.   local res
  902.   if slot==nil then
  903.     return false, err
  904.   end
  905.   if prevSlot~=slot then
  906.     select(slot)
  907.   end
  908.   res=native.refuel(quantity)
  909.   if res==true then
  910.     updateInventory()
  911.   end
  912.   if prevSlot~=slot then
  913.     select(prevSlot)
  914.   end
  915.   return res
  916. end
  917.  
  918. function craft(quantity, slot, outputType)
  919.   if turtle.craft==nil then
  920.     return false, "turtlex:craft: Only crafting turtles can craft!"
  921.   end
  922.  
  923.   local res
  924.   prevSlot=selectedSlot
  925.   if slot==nil then
  926.     slot=selectedSlot
  927.   elseif slot~=prevSlot then
  928.     select(slot)
  929.   end
  930.   if quantity==nil then
  931.     res=native.craft()
  932.   else
  933.     res=native.craft(quantity)
  934.   end
  935.   if slot~=prevSlot then
  936.     select(prevSlot)
  937.   end  
  938.   if res==true then
  939.     local wasChange, addedStuff, removedStuff = updateInventory()
  940.     if wasChange==false then
  941.       return res, "inconsistent results from updateInventory - craft reports success, but no change or no added items reported?"
  942.     end
  943.     --did they give us an expected output type?
  944.     if outputType~=nil and outputType~="" and outputType~=0 and outputType~="unknown" then
  945.       local addedType
  946.       if #addedStuff>0 then
  947.         addedType=addedStuff[1].type
  948.       else
  949.         addedType=myInventory.getItemType(slot)
  950.       end
  951.       for i=2,#addedStuff,1 do
  952.         if addedStuff[i].type~=addedType then
  953.           return res, "inconsistent results from updateInventory - multiple new item types detected!"
  954.         end
  955.       end
  956.       setItemType(outputType,addedStuff[1].slot)
  957.     end
  958.   end
  959.   return res
  960. end
Advertisement
Add Comment
Please, Sign In to add comment