Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Hive Maintenance
- -- ----------------------------------------------------------
- -- Tries to keep my barrels stocked with "real" resources,
- -- so's I don't have to worry about converting combs myself.
- -- ----------------------------------------------------------
- -- Initialise important values:
- -- ----------------------------------------------------------
- -- First three indexes are co-ords. All thereafter are linked nodes.
- local node = {{596,-237,64, 2}, -- 1, hive central.
- {596,-234,64, 1,3,10}, -- 2, processed resource dropoff.
- {603,-234,64, 2,14,5}, -- 3, mid-eastern barrel.
- {603,-218,64, 13}, -- 4, the recharge station.
- {610,-234,64, 3,6}, -- 5, phospher pickup (eastern barrel).
- {610,-230,64, 5,7}, -- 6, centrifuge output chest.
- {610,-228,64, 6,8}, -- 7, centrifuge.
- {610,-227,64, 7,9}, -- 8, redstone energy cell.
- {610,-226,64, 8,11}, -- 9, squeezer.
- {590,-234,64, 2,12}, -- 10, tiny dust collection (mid-western barrel).
- {610,-219,64, 9}, -- 11, compressor.
- {580,-234,64, 10}, -- 12, western barrel.
- {603,-220,64, 14,4}, -- 13, furnace.
- {603,-222,64, 3,13}} -- 14, furnace pickup.
- -- Stuff to centrifuge.
- local comb = {"tin comb","bronze comb","platinum comb","radioactive comb","lapis comb","diamond comb",
- "copper comb","titanium comb","gold comb","fossilised comb","iron comb","ruby comb",
- "electrum comb","glowing comb","brazen comb","clay comb","static comb","dripping comb",
- "silver comb","invar comb","leaden comb","stringy comb","propolis","simmering comb"}
- -- Stuff to combine (2x2 crafts).
- local tinydust = {"stone","stone brick","tiny pile of ruby dust","tiny pile of diamond dust",
- "tiny pile of electrum dust","tiny pile of silver dust","tiny pile of titanium dust",
- "tiny pile of platinum dust","tiny pile of invar dust","tiny pile of lead dust",
- "tiny pile of gold dust","tiny pile of copper dust","tiny pile of bronze dust",
- "tiny pile of brass dust","tiny pile of iron dust","tiny pile of tin dust"}
- -- Stuff to furnace.
- local furnace = {"cobble","electrum dust","silver dust","invar dust","lead dust","gold dust",
- "copper dust","bronze dust","brass dust","iron dust","tin dust","sticky resin"}
- -- Stuff to compress (and how much is needed per operation).
- local compression = {{"lapis lazuli",9},{"glowstone dust",4},{"clay dust",1}}
- -- Job timer IDs. [1] = furnace, [2] = compression, [3] = centrifuge.
- local worktimer = {0,0,0}
- -- Misc stuff.
- local facing = {"north","east","south","west"}
- local direction = 1
- local curnode = 1
- local curcombbarrel = 1
- local curcompressor = 1
- local curfurnace = 1
- local curdust = 1
- local getmagmafuel = true
- local x = 0
- local y = 0
- local z = 0
- local sender = 0
- local message = ""
- -- ----------------------------------------------------------
- -- Functions and stuff:
- -- ----------------------------------------------------------
- -- Returns true if the turtle is carrying anything.
- local function carrying()
- for i=1,16 do if turtle.getItemCount(i) ~= 0 then return true end end
- return false
- end
- -- Poll the inventory server re a given barrel.
- local function getBarrel(barreltype)
- message = nil
- rednet.open("right")
- while message == nil do
- rednet.send(invserver,barreltype)
- sender, message = rednet.receive(5)
- end
- rednet.close("right")
- return textutils.unserialize(message)
- end
- -- Returns true if the turtle is at the specified node.
- local function atNode(tnode) return (x == node[tnode][1] and y == node[tnode][2] and z == node[tnode][3]) end
- -- Accepts strings representing compass-facings to turn the turtle.
- local function faceDirection(targetdirection)
- local tardir = 1
- for i=1,4 do if targetdirection == facing[i] then tardir = i end end
- if tardir < direction then
- if tardir == 1 and direction == 4 then
- turtle.turnRight()
- else
- for i=1,direction-tardir do turtle.turnLeft() end
- end
- elseif tardir > direction then
- if tardir == 4 and direction == 1 then
- turtle.turnLeft()
- else
- for i=1,tardir-direction do turtle.turnRight() end
- end
- end
- direction = tardir
- end
- -- Travel to a co-ordinate.
- local function goToPos(targetx,targety,targetz)
- while (x ~= targetx) or (y ~= targety) or (z ~= targetz) do
- if z > targetz then
- if turtle.down() then z = z - 1 end
- elseif z < targetz then
- if turtle.up() then z = z + 1 end
- end
- if x > targetx then
- if direction ~= 4 then faceDirection("west") end
- if turtle.forward() then x = x - 1 end
- elseif x < targetx then
- if direction ~= 2 then faceDirection("east") end
- if turtle.forward() then x = x + 1 end
- end
- if y > targety then
- if direction ~= 1 then faceDirection("north") end
- if turtle.forward() then y = y - 1 end
- elseif y < targety then
- if direction ~= 3 then faceDirection("south") end
- if turtle.forward() then y = y + 1 end
- end
- end
- end
- -- Travel to a node and update the node tracker.
- local function goToNode(desnode)
- goToPos(node[desnode][1],node[desnode][2],node[desnode][3])
- curnode = desnode
- end
- -- Required by the below function.
- local function checkNextNode(priornode,testnode,desnode)
- local distance = 255
- local testdistance = 255
- for i=1,table.getn(node[testnode])-3 do if node[testnode][3+i] ~= priornode and node[testnode][3+i] ~= curnode then
- if node[testnode][3+i] == desnode then return desnode, (math.abs(node[testnode][1]-node[desnode][1]) + math.abs(node[testnode][2]-node[desnode][2])) end
- testdistance = checkNextNode(testnode,node[testnode][3+i],desnode) + (math.abs(node[testnode][1]-node[node[testnode][3+i]][1]) + math.abs(node[testnode][2]-node[node[testnode][3+i]][2]))
- if testdistance < distance then distance = testdistance end
- end end
- return distance
- end
- -- Travels one node, in a direction that's best to get to an ultimate destination node.
- -- Will hence tend to be called multiple times in order to get there.
- local function headToNode(desnode)
- if not atNode(desnode) then
- local distance = 255
- local testdistance = 255
- local nextnode = 255
- for i=1,table.getn(node[curnode])-3 do
- if node[curnode][3+i] == desnode then
- goToNode(desnode)
- return
- end
- testdistance = checkNextNode(curnode,node[curnode][3+i],desnode) + (math.abs(node[curnode][1]-node[node[curnode][3+i]][1]) + math.abs(node[curnode][2]-node[node[curnode][3+i]][2]))
- if testdistance < distance then
- distance = testdistance
- nextnode = node[curnode][3+i]
- end
- end
- goToNode(nextnode)
- end
- end
- -- Refuel the turtle itself.
- local function goGetFuel()
- print("I'm hungry. Off to eat some EU...")
- while not atNode(4) do headToNode(4) end
- faceDirection("east")
- print("OMNOMNOM")
- while turtle.getFuelLevel() < 10000 do sleep(30) end
- print("Ah, that hit the spot!")
- print("")
- end
- -- Dumps the turtle's inventory into the dropoff chest.
- local function dropOff()
- print("Dropping off loot...")
- while not atNode(2) do headToNode(2) end
- faceDirection("south")
- for i=1,16 do if turtle.getItemCount(i) ~= 0 then
- turtle.select(i)
- turtle.drop()
- end end
- turtle.select(1)
- print("")
- end
- local function fuelMagmaticEngines()
- barrelinfo = getBarrel("phosphor")
- print("I wonder how the lava production is going?")
- while not atNode(9) do headToNode(9) end
- faceDirection("west")
- turtle.select(1)
- turtle.suck()
- if turtle.getItemCount(1) ~= 0 then
- print("Guess it hasn't finished the last phosphor load. Good!")
- print("")
- turtle.drop()
- getmagmafuel = false
- return
- end
- print("Needs more phosphor!")
- while not atNode(barrelinfo[1]) do headToNode(barrelinfo[1]) end
- goToPos(barrelinfo[2],barrelinfo[3],barrelinfo[4])
- turtle.suckUp()
- if turtle.getItemCount(1) < 2 then
- print((turtle.getItemCount(1) == 0) and "Erk! Out of phosphor!" or "Only ONE phosphor?!")
- print("")
- curcombbarrel = table.getn(comb)
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- return
- end
- if bit.band(turtle.getItemCount(1),1) == 1 then turtle.transferTo(5,1) end
- while not atNode(9) do headToNode(9) end
- faceDirection("east")
- turtle.select(2)
- while turtle.getItemCount(2) < 64 do turtle.suck() end
- turtle.transferTo(6,turtle.getItemCount(2) - (turtle.getItemCount(1) / 2))
- faceDirection("west")
- turtle.select(1)
- turtle.drop()
- turtle.select(2)
- turtle.drop()
- print("Lava production restocked.")
- getmagmafuel = false
- if carrying() then dropOff() else print("") end
- end
- -- Combine things together (craft).
- local function combineDustPiles()
- print("Let's go combine some "..tinydust[curdust].."s.")
- turtle.select(1)
- barrelinfo = getBarrel(tinydust[curdust])
- if barrelinfo[5] then
- while not atNode(barrelinfo[1]) do headToNode(barrelinfo[1]) end
- goToPos(barrelinfo[2],barrelinfo[3],barrelinfo[4])
- if not turtle.suckUp() then
- print("No items here...")
- print("")
- elseif turtle.getItemCount(1) < 4 then
- print("Not enough items here! Bah!")
- else
- print("Able to get "..math.floor(turtle.getItemCount(1) / 4).." new items!")
- turtle.transferTo(2,math.floor(turtle.getItemCount(1) / 4))
- turtle.transferTo(5,math.floor(turtle.getItemCount(1) / 3))
- turtle.transferTo(6,math.floor(turtle.getItemCount(1) / 2))
- turtle.craft()
- end
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- if carrying() then dropOff() end
- else
- print("Current barrel is fully processed. Will skip it.")
- print("")
- end
- curdust = curdust + 1
- if curdust > table.getn(tinydust) then curdust = 1 end
- end
- -- Burn baby burn!
- local function cookStuff()
- print("Let's go smelt some "..furnace[curfurnace]..".")
- barrelinfo = getBarrel(furnace[curfurnace])
- if barrelinfo[5] then
- turtle.select(1)
- while not atNode(barrelinfo[1]) do headToNode(barrelinfo[1]) end
- goToPos(barrelinfo[2],barrelinfo[3],barrelinfo[4])
- if not turtle.suckUp() then
- print("Nothing to cook...")
- print("")
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- else
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- while not atNode(13) do headToNode(13) end
- goToPos(604,-220,65)
- worktimer[1] = os.startTimer(turtle.getItemCount(1) * 6.55)
- turtle.suckDown()
- turtle.dropDown()
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- while not atNode(14) do headToNode(14) end
- faceDirection("east")
- while turtle.suck() do end
- if carrying() then dropOff() else print("") end
- end
- else
- print("Current barrel is fully processed. Will skip it.")
- print("")
- end
- curfurnace = curfurnace + 1
- if curfurnace > table.getn(furnace) then curfurnace = 1 end
- end
- -- Some stuff needs to be packed down for efficient storage.
- local function compressStuff()
- print("Let's go compress some "..compression[curcompressor][1].."s.")
- barrelinfo = getBarrel(compression[curcompressor][1])
- if barrelinfo[5] then
- turtle.select(1)
- while not atNode(barrelinfo[1]) do headToNode(barrelinfo[1]) end
- goToPos(barrelinfo[2],barrelinfo[3],barrelinfo[4])
- if not turtle.suckUp() then
- print("Nothing to compress...")
- print("")
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- elseif turtle.getItemCount(1) < compression[curcompressor][2] then
- print("Not enough to compress! Bah!")
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- dropOff()
- else
- print("Able to get "..math.floor(turtle.getItemCount(1) / compression[curcompressor][2]).." condensed items!")
- turtle.transferTo(2,turtle.getItemCount(1) % compression[curcompressor][2])
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- while not atNode(11) do headToNode(11) end
- goToPos(609,-219,65)
- worktimer[2] = os.startTimer((turtle.getItemCount(1) / compression[curcompressor][2]) * 20)
- turtle.suckDown()
- turtle.dropDown()
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- faceDirection("south")
- while turtle.suck() do end
- if carrying() then dropOff() else print("") end
- end
- else
- print("Current barrel is fully processed. Will skip it.")
- print("")
- end
- curcompressor = curcompressor + 1
- if curcompressor > table.getn(compression) then curcompressor = 1 end
- end
- -- Grabs a comb stack and processes it.
- local function processComb()
- turtle.select(1)
- print("Off to grab a stack of "..comb[curcombbarrel].."s...")
- barrelinfo = getBarrel(comb[curcombbarrel])
- if barrelinfo[5] then
- while not atNode(barrelinfo[1]) do headToNode(barrelinfo[1]) end
- goToPos(barrelinfo[2],barrelinfo[3],barrelinfo[4])
- if not turtle.suckUp() then
- print("Huh, guess there aren't any combs of this type...")
- print("")
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- else
- print("Got "..turtle.getItemCount(1).." combs to process.")
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- while not atNode(7) do headToNode(7) end
- goToPos(609,-228,65)
- faceDirection("east")
- worktimer[3] = os.startTimer(turtle.getItemCount(1) * 5.25)
- turtle.suckDown()
- turtle.dropDown()
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- while not atNode(6) do headToNode(6) end
- faceDirection("west")
- while turtle.suck() do end
- end
- else
- print("Current barrel is fully processed. Will skip it.")
- print("")
- end
- curcombbarrel = curcombbarrel + 1
- if curcombbarrel > table.getn(comb) then
- getmagmafuel = true
- curcombbarrel = 1
- end
- end
- -- ----------------------------------------------------------
- -- Primary functions:
- -- ----------------------------------------------------------
- -- Reset the timer table as need be, flagging jobs as ready.
- local function catchTimers()
- local event = ""
- local timerID = 0
- while true do
- event, timerID = os.pullEvent("timer")
- for i=1,table.getn(worktimer) do if worktimer[i] == timerID then
- worktimer[i] = 0
- break
- end end
- end
- end
- -- Primary work loop.
- local function main()
- while true do
- if turtle.getFuelLevel() < 1000 then goGetFuel() end
- if carrying() then dropOff() end
- if getmagmafuel then fuelMagmaticEngines() end
- combineDustPiles()
- if worktimer[1] == 0 then cookStuff() end
- if worktimer[2] == 0 then compressStuff() end
- if worktimer[3] == 0 then processComb() end
- end
- end
- -- ----------------------------------------------------------
- -- I've just booted up. Where am I? Who am I? etc...
- -- ----------------------------------------------------------
- -- Ping the inventory server until it answers.
- rednet.open("right")
- while invserver == nil do
- rednet.broadcast("Hello InvServer")
- invserver, message = rednet.receive(5)
- end
- rednet.close("right")
- x, y, z = gps.locate(5)
- -- Ping the GPS servers until I get a valid reading.
- while x == nil or y == nil or z == nil do
- x, y, z = gps.locate(5)
- sleep(5)
- end
- -- Just a way of isolating some throw-away local variables.
- do
- while not turtle.forward() do turtle.turnLeft() end
- local tempx, tempy, tempz = gps.locate(5)
- if x > tempx then
- direction = 4
- elseif x < tempx then
- direction = 2
- elseif y > tempy then
- direction = 1
- else direction = 3 end
- end
- x, y, z = gps.locate(5)
- print("I'm at "..x..","..y..","..z..", I have "..turtle.getFuelLevel().." fuel and I'm facing "..facing[direction]..".")
- print("")
- -- Go to the nearest node first:
- do
- local temp1 = 255
- local temp2 = 0
- for i=1,table.getn(node) do
- temp2 = math.sqrt(math.pow(x - node[i][1],2) + math.pow(y - node[i][2],2))
- temp2 = math.sqrt(math.pow(temp2,2) + math.pow(z - node[i][3],2))
- if temp2 < temp1 then
- temp1 = temp2
- curnode = i
- end
- end
- end
- goToPos(node[curnode][1],node[curnode][2],node[curnode][3])
- parallel.waitForAny(main, catchTimers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement