Advertisement
SirBaconBitz

Gordon Ramsbot V. 2.0

Aug 7th, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. -- Requirements
  2.  
  3.   -- Default OpenC
  4.  
  5. local component = require("component")
  6. local term = require("term")
  7. local robot = require("robot")
  8. local serialization = require("serialization")
  9. local table = require("table")
  10. local invCon = component.inventory_controller
  11. local c = component.crafting
  12.  
  13.   -- Keeping track of stored items
  14.  
  15. craftables = {}
  16. for i = 1,27 do
  17.   table.insert(craftables,{taken=0,name="",slot=i})
  18. end
  19.   --API to be returned
  20.  
  21. local gr = {}
  22.  
  23. -- End
  24.  
  25. -- Positioning
  26.  
  27. fDir = 1
  28.  
  29. xPos = 0
  30.  
  31. yPos = 0
  32.  
  33. -- End
  34.  
  35. function gr.face( sDir )
  36.   if fDir == 2 then
  37.     robot.turnLeft()
  38.     fDir = 1
  39.   elseif fDir == 3 then
  40.     robot.turnRight()
  41.     fDir = 1
  42.   elseif fDir == 4 then
  43.     robot.turnAround()
  44.     fDir = 1
  45.   end
  46.   if sDir == 2 then
  47.     robot.turnRight()
  48.     fDir = 2
  49.   elseif sDir == 3 then
  50.     robot.turnLeft()
  51.     fDir = 3
  52.   elseif sDir == 4 then
  53.     robot.turnAround()
  54.     fDir = 4
  55.   end
  56. end
  57.  
  58. function gr.go( sX , sY )
  59. sX = tonumber(sX)
  60. sY = tonumber(sY)
  61. -- X Movement
  62.   if sX > xPos then
  63.     local xDest = sX - xPos
  64.     gr.face(1)
  65.     for i = 1,xDest do
  66.       local succ = robot.forward()
  67.      
  68.       --Insert accounting for turtle being blocked here later.
  69.      
  70.       xPos = xPos + 1
  71.     end
  72.   end
  73.  
  74.   if xPos > sX then
  75.     local xDest = xPos - sX
  76.     gr.face(1)
  77.     for i = 1,xDest do
  78.       local succ = robot.back()
  79.      
  80.       --Insert accounting for turtle being blocked here later.
  81.      
  82.       xPos = xPos - 1
  83.     end
  84.   end
  85.  
  86. -- Y Movement
  87.   if sY > yPos then
  88.     local yDest = sY - yPos
  89.     gr.face(1)
  90.     for i = 1,yDest do
  91.       local succ = robot.up()
  92.      
  93.       --Insert accounting for turtle being blocked here later.
  94.      
  95.       yPos = yPos + 1
  96.     end
  97.   end
  98.  
  99.   if yPos > sY then
  100.     local yDest = yPos - sY
  101.     gr.face(1)
  102.     for i = 1,yDest do
  103.       local succ = robot.down()
  104.      
  105.       --Insert accounting for turtle being blocked here later.
  106.      
  107.       yPos = yPos - 1
  108.     end
  109.   end
  110. end
  111.  
  112. function gr.getTool( tName , tSlot )
  113.   local tHandle = io.open("/gr/tools.lua","r")
  114.   local tTable = serialization.unserialize(tHandle:read("*all"))
  115.   tHandle:close()
  116.   local tFound = {}
  117.   for k,v in pairs(tTable) do
  118.     if v.tName == tName then
  119.       tFound = v
  120.     end
  121.   end
  122.   gr.go(tFound.x,tFound.y)
  123.   gr.face(2)
  124.   robot.select(tSlot)
  125.   robot.suck(1)
  126.   gr.face(1)
  127. end
  128.  
  129. function gr.retTool( tName , tSlot )
  130.   local tHandle = io.open("/gr/tools.lua","r")
  131.   local tTable = serialization.unserialize(tHandle:read("*all"))
  132.   tHandle:close()
  133.   local tFound = {}
  134.   for k,v in pairs(tTable) do
  135.     if v.tName == tName then
  136.       tFound = v
  137.     end
  138.   end
  139.   gr.go(tFound.x,tFound.y)
  140.   gr.face(2)
  141.   robot.select(tSlot)
  142.   robot.drop(1)
  143.   gr.face(1)
  144. end
  145.  
  146. function gr.getFood( fName , fSlot , fQty )
  147.   local fHandle = io.open("/gr/foods.lua", "r")
  148.   local fTable = serialization.unserialize(fHandle:read("*all"))
  149.   fHandle:close()
  150.   local fFound = {}
  151.   for k,v in pairs(fTable) do
  152.     if v.fName == fName then
  153.       fFound = v
  154.     end
  155.   end
  156.   gr.go(fFound.x, fFound.y)
  157.   gr.face(3)
  158.   robot.select(fSlot)
  159.   robot.suck(fQty)
  160.   gr.face(1)
  161. end
  162.  
  163. function gr.craft( cName , cQty )
  164.  
  165.   if cQty == nil then cQty = 1 end
  166.  
  167.   local cHandle = io.open("/gr/recipes.lua","r")
  168.   local cTable = serialization.unserialize(cHandle:read("*all"))
  169.   cHandle:close()
  170.   local cRecipe = {}
  171.   for k,v in pairs(cTable) do
  172.     if v[1].rName == cName then
  173.       cRecipe = v
  174.     end
  175.   end
  176.  
  177.   print(cRecipe[1].rName)
  178.  
  179.   for k,v in pairs(cRecipe) do
  180.     if v.cType == 3 then
  181.       local finding = true
  182.       gr.craft(v.cName, cQty)
  183.       for kC, vC in pairs(craftables) do
  184.         if vC.taken == 0 and finding == true then
  185.           vC.taken = 1
  186.           vC.name = v.cName
  187.           print("Added: "..vC.name.." to craftables.")
  188.           print("In Slot:"..vC.slot)
  189.           finding = false
  190.         end
  191.       end
  192.     end
  193.   end
  194.  
  195.   for k,v in pairs(craftables) do
  196.     if v.taken == 1 then
  197.       print("Name: "..v.name.." Slot: "..v.slot)
  198.     end
  199.   end
  200.   toget = {}
  201.   for k,v in pairs(cRecipe) do
  202.     if v.cType == 1 then
  203.       gr.getFood(v.cName, v.cSlot, cQty)
  204.     elseif v.cType == 2 then
  205.       gr.getTool(v.cName, v.cSlot)
  206.     elseif v.cType == 3 then
  207.       print("Getting: "..v.cName.." for slot "..v.cSlot)
  208.       usecSlot = v.cSlot
  209.       for kC,vC in pairs(craftables) do
  210.         if v.cName == vC.name then
  211.           print("Retrieving component: "..v.cName)
  212.           gr.go(0,0)
  213.           robot.select(usecSlot)
  214.           invCon.suckFromSlot(0,vC.slot,cQty)
  215.           vC.taken = 0
  216.           vC.name = ""
  217.         end
  218.       end
  219.     end      
  220.   end
  221.  
  222.   -- Travel to above output chest
  223.  
  224.   robot.select(16)
  225.   gr.go(0,0)
  226.   gr.face(1)
  227.   if tonumber(cRecipe[1].rType) == 2 then
  228.     print("Type = 2")
  229.     gr.go(-1,0)
  230.     robot.select(1)
  231.     robot.dropDown(cQty)
  232.     gr.go(0,0)
  233.     gr.face(1)
  234.     os.sleep(2.5 * cQty)
  235.   elseif tonumber(cRecipe[1].rType) == 3 then
  236.   print("Type = 3")
  237.     gr.go(-2,0)
  238.     robot.select(1)
  239.     robot.dropDown(cQty)
  240.     gr.go(0,0)
  241.     gr.face(1)
  242.     os.sleep(6 * cQty)
  243.   elseif cRecipe[1].rType == nil or 1 then
  244.     print("Type = 1")
  245.     for i = 1,cQty do
  246.       c.craft()
  247.     end
  248.     robot.dropDown(cQty)
  249.   end
  250.  
  251.   for k,v in pairs(cRecipe) do
  252.     if v.cType == 2 then
  253.       gr.retTool(v.cName, v.cSlot)
  254.     end
  255.   end
  256.   gr.go(0,0)
  257.   gr.face(1)
  258. end
  259. return gr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement