Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Requirements
- -- Default OpenC
- local component = require("component")
- local term = require("term")
- local robot = require("robot")
- local serialization = require("serialization")
- local table = require("table")
- local invCon = component.inventory_controller
- local c = component.crafting
- -- Keeping track of stored items
- craftables = {}
- for i = 1,27 do
- table.insert(craftables,{taken=0,name="",slot=i})
- end
- --API to be returned
- local gr = {}
- -- End
- -- Positioning
- fDir = 1
- xPos = 0
- yPos = 0
- -- End
- function gr.face( sDir )
- if fDir == 2 then
- robot.turnLeft()
- fDir = 1
- elseif fDir == 3 then
- robot.turnRight()
- fDir = 1
- elseif fDir == 4 then
- robot.turnAround()
- fDir = 1
- end
- if sDir == 2 then
- robot.turnRight()
- fDir = 2
- elseif sDir == 3 then
- robot.turnLeft()
- fDir = 3
- elseif sDir == 4 then
- robot.turnAround()
- fDir = 4
- end
- end
- function gr.go( sX , sY )
- sX = tonumber(sX)
- sY = tonumber(sY)
- -- X Movement
- if sX > xPos then
- local xDest = sX - xPos
- gr.face(1)
- for i = 1,xDest do
- local succ = robot.forward()
- --Insert accounting for turtle being blocked here later.
- xPos = xPos + 1
- end
- end
- if xPos > sX then
- local xDest = xPos - sX
- gr.face(1)
- for i = 1,xDest do
- local succ = robot.back()
- --Insert accounting for turtle being blocked here later.
- xPos = xPos - 1
- end
- end
- -- Y Movement
- if sY > yPos then
- local yDest = sY - yPos
- gr.face(1)
- for i = 1,yDest do
- local succ = robot.up()
- --Insert accounting for turtle being blocked here later.
- yPos = yPos + 1
- end
- end
- if yPos > sY then
- local yDest = yPos - sY
- gr.face(1)
- for i = 1,yDest do
- local succ = robot.down()
- --Insert accounting for turtle being blocked here later.
- yPos = yPos - 1
- end
- end
- end
- function gr.getTool( tName , tSlot )
- local tHandle = io.open("/gr/tools.lua","r")
- local tTable = serialization.unserialize(tHandle:read("*all"))
- tHandle:close()
- local tFound = {}
- for k,v in pairs(tTable) do
- if v.tName == tName then
- tFound = v
- end
- end
- gr.go(tFound.x,tFound.y)
- gr.face(2)
- robot.select(tSlot)
- robot.suck(1)
- gr.face(1)
- end
- function gr.retTool( tName , tSlot )
- local tHandle = io.open("/gr/tools.lua","r")
- local tTable = serialization.unserialize(tHandle:read("*all"))
- tHandle:close()
- local tFound = {}
- for k,v in pairs(tTable) do
- if v.tName == tName then
- tFound = v
- end
- end
- gr.go(tFound.x,tFound.y)
- gr.face(2)
- robot.select(tSlot)
- robot.drop(1)
- gr.face(1)
- end
- function gr.getFood( fName , fSlot , fQty )
- local fHandle = io.open("/gr/foods.lua", "r")
- local fTable = serialization.unserialize(fHandle:read("*all"))
- fHandle:close()
- local fFound = {}
- for k,v in pairs(fTable) do
- if v.fName == fName then
- fFound = v
- end
- end
- gr.go(fFound.x, fFound.y)
- gr.face(3)
- robot.select(fSlot)
- robot.suck(fQty)
- gr.face(1)
- end
- function gr.craft( cName , cQty )
- if cQty == nil then cQty = 1 end
- local cHandle = io.open("/gr/recipes.lua","r")
- local cTable = serialization.unserialize(cHandle:read("*all"))
- cHandle:close()
- local cRecipe = {}
- for k,v in pairs(cTable) do
- if v[1].rName == cName then
- cRecipe = v
- end
- end
- print(cRecipe[1].rName)
- for k,v in pairs(cRecipe) do
- if v.cType == 3 then
- local finding = true
- gr.craft(v.cName, cQty)
- for kC, vC in pairs(craftables) do
- if vC.taken == 0 and finding == true then
- vC.taken = 1
- vC.name = v.cName
- print("Added: "..vC.name.." to craftables.")
- print("In Slot:"..vC.slot)
- finding = false
- end
- end
- end
- end
- for k,v in pairs(craftables) do
- if v.taken == 1 then
- print("Name: "..v.name.." Slot: "..v.slot)
- end
- end
- toget = {}
- for k,v in pairs(cRecipe) do
- if v.cType == 1 then
- gr.getFood(v.cName, v.cSlot, cQty)
- elseif v.cType == 2 then
- gr.getTool(v.cName, v.cSlot)
- elseif v.cType == 3 then
- print("Getting: "..v.cName.." for slot "..v.cSlot)
- usecSlot = v.cSlot
- for kC,vC in pairs(craftables) do
- if v.cName == vC.name then
- print("Retrieving component: "..v.cName)
- gr.go(0,0)
- robot.select(usecSlot)
- invCon.suckFromSlot(0,vC.slot,cQty)
- vC.taken = 0
- vC.name = ""
- end
- end
- end
- end
- -- Travel to above output chest
- robot.select(16)
- gr.go(0,0)
- gr.face(1)
- if tonumber(cRecipe[1].rType) == 2 then
- print("Type = 2")
- gr.go(-1,0)
- robot.select(1)
- robot.dropDown(cQty)
- gr.go(0,0)
- gr.face(1)
- os.sleep(2.5 * cQty)
- elseif tonumber(cRecipe[1].rType) == 3 then
- print("Type = 3")
- gr.go(-2,0)
- robot.select(1)
- robot.dropDown(cQty)
- gr.go(0,0)
- gr.face(1)
- os.sleep(6 * cQty)
- elseif cRecipe[1].rType == nil or 1 then
- print("Type = 1")
- for i = 1,cQty do
- c.craft()
- end
- robot.dropDown(cQty)
- end
- for k,v in pairs(cRecipe) do
- if v.cType == 2 then
- gr.retTool(v.cName, v.cSlot)
- end
- end
- gr.go(0,0)
- gr.face(1)
- end
- return gr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement