Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Deluxe W.E.Autoquarry Control Hub script by Megaddd. 2013-11-11
- --Major protip: do NOT run servers within networking range of one-another.
- --[[
- Notice: coordinates such as homePos or refuelPos are relative to the turtle spawn-point,
- meaning they do not need to be world coordinates.
- (though, it will help turtles if you use the real 'y' value)
- 2013-11-14: first somewhat functional release. Saving broken for the most part.
- 2014-06-12: Modified to work with OpenComputers robots and wireless peripheral.
- --]]
- local MODEMSIDE = "back"
- local PORT = 1337
- local NETSIDE = "back" --Side of the rednet modem on the computer/turtle (why would you be using a turtle as a server, though?)
- local MSGTIME = 60 --How long to leave rednet message logs on screen for (0 for no limit)
- local FLASHTIME = 5 --How long new messages stay highlighted for
- --local CrashIfNoAPI = true --Whether to immediatelly close the program if list api isn't found (will prevent adding new turtles if false.
- local FILENAME = "QuarryData.dat" --Quarry data storage, changing will void all current data. (turtle IDs, quarry progress, etc..)
- local APINAME = "mlf" --My List Function API. Contains a few useful list functions, such as locating a member in a list by it's value.
- local APINAME2 = "tu" --Improved TextUtils by ElvishJerricco, as default textutils may have saving problems with QuarryData.dat
- local quarryCellSize = 3 --Size of each turtle's individual mini-quarry.
- local quarryWidthLength = {7,24} --number of quarry cells on x and z to be quarried. (effectively - quarry size)
- local quarryDepth = 9 --does this really need a tooltip? fine. Quarry depth in blocks (not cells).
- local homePos = {0,0,0} --Location of turtle spawn-point, x/y/z. (where every turtle's MAWQ program is first run at)
- local refuelPos = {5,0,0} --Location of the fuel item chest. (notice: locations are not actual in-world coordinates, and even if they were, it wouldn't change much)
- local dumpPos = {-5,0,0} --Location of the quarry output chest.
- local turtleList = {} --ID's of all disposable turtles.
- local idleTurtles = {} --ID's of currently inactive, ready turtles.
- local activeTurtles = {} --{["turtleID"]=qI,..} (probably obsolete, might be used in the future for something useful)
- local quarryProgress = 0 --Number of cells quarried.
- net = peripheral.wrap(MODEMSIDE)
- net.open(PORT)
- --os.loaAPI("tu") --do not use unless serializing becomes broken. (does not effect unserializing)
- os.loadAPI("mlf")
- textLinesLeft = {} --textLines = {{"text in line1 bla bla potato",uptime},{"Hey Joey, how's the omelette?",uptime}...}
- textLinesLeftLast = {}
- --[[function pw()
- return (32768*2 - math.sin(math.ceil((os.time())*10))*32768)
- end]]--
- --encrypted password removed for now because os.time() is different on OpenComputers. (not global, each computer runs on it's own thread from a pool)
- local function leftMonitor()
- local rmon = peripheral.wrap("left")
- rmon.setTextScale(1)
- while true do
- local monx, mony = rmon.getSize()
- if textLinesLeft[1] ~= nil then
- rmon.setBackgroundColor(colors.black)
- rmon.clear()
- for i=1,#textLinesLeft do
- rmon.setCursorPos(1,mony+1-i)
- local textcol
- local uptime = textLinesLeft[i][2]
- if uptime < MSGTIME/2 then rmon.setTextColor(colors.red) else rmon.setTextColor(colors.gray) end
- if uptime < FLASHTIME then
- for i=1,monx do write(" ") end
- rmon.setBackgroundColor(colors.gray)
- else
- rmon.setBackgroundColor(colors.black)
- end
- rmon.setCursorPos(1,mony+1-i)
- rmon.write(textLinesLeft[i][1])
- textLinesLeftLast = textLinesLeft
- end
- end
- for i=1,mony do
- if textLinesLeft[i] ~= nil then
- textLinesLeft[i][2] = textLinesLeft[i][2] + 1
- if MSGTIME > 0 then
- if textLinesLeft[i][2] >= MSGTIME then
- textLinesLeft[i] = nil
- end
- end
- end
- end
- sleep(1)
- end
- end
- function printLeft(Text)
- local mon = peripheral.wrap("left")
- local monx, mony = mon.getSize()
- for i=mony,1,-1 do
- textLinesLeft[i] = textLinesLeft[i-1]
- end
- textLinesLeft[1] = {Text,0}
- end
- textLinesRight = {} --textLines = {{"text in line1 bla bla potato",uptime},{"Hey Joey, how's the omelette?",uptime}...}
- textLinesRightLast = {}
- local function rightMonitor()
- local rmon = peripheral.wrap("right")
- rmon.setTextScale(1)
- while true do
- local monx, mony = rmon.getSize()
- if textLinesRight[1] ~= nil then
- rmon.setBackgroundColor(colors.black)
- rmon.clear()
- for i=1,#textLinesRight do
- rmon.setCursorPos(1,mony+1-i)
- local textcol
- local uptime = textLinesRight[i][2]
- if uptime < MSGTIME/2 then rmon.setTextColor(colors.lime) else rmon.setTextColor(colors.gray) end
- if uptime < FLASHTIME then
- for i=1,monx do write(" ") end
- rmon.setBackgroundColor(colors.gray)
- else
- rmon.setBackgroundColor(colors.black)
- end
- rmon.setCursorPos(1,mony+1-i)
- rmon.write(textLinesRight[i][1])
- textLinesRightLast = textLinesRight
- end
- end
- for i=1,mony do
- if textLinesRight[i] ~= nil then
- textLinesRight[i][2] = textLinesRight[i][2] + 1
- if MSGTIME > 0 then
- if textLinesRight[i][2] >= MSGTIME then
- textLinesRight[i] = nil
- end
- end
- end
- end
- sleep(1)
- end
- end
- function printRight(Text)
- local mon = peripheral.wrap("left")
- local monx, mony = mon.getSize()
- for i=mony,1,-1 do
- textLinesRight[i] = textLinesRight[i-1]
- end
- textLinesRight[1] = {Text,0}
- end
- function send(p1, p2, p3, p4, p5)
- net.transmit(PORT,500,p1, p2, p3, p4, p5)
- end
- function saveData() --saves most of the quarry data, changing anything here will most likely break something/everything down the line.
- local in7 = quarryProgress
- h = fs.open(FILENAME,"w")
- h.writeLine(textutils.serialize(in7))
- h.close()
- end
- if fs.exists(FILENAME) then --Touching this breaks it way too often for some reason.
- h = fs.open(FILENAME,"r")
- quarryProgress = textutils.unserialize(h.readLine())
- h.close()
- else
- saveData()
- end
- function deployTurtle(ID,qI) --left/right alternating quarry placement. Sends turtles on their merry way.
- local deployLoc = {9,homePos[2],0}
- local row = math.floor((qI-1) / quarryWidthLength[1])
- local rowDecrement = quarryCellSize * row
- deployLoc[1] = 9 + rowDecrement
- qI = qI % quarryWidthLength[1]
- if qI == 0 then qI = quarryWidthLength[1] end
- if qI % 2 == 1 then --The magic that expands left and right from the center, instead of going from left to right (Why? Because it's pretty to watch (and saves fuel at first), someone pls do a timelapse :>)
- deployLoc[3] = math.floor(quarryCellSize/2) - (math.floor(qI/2)*quarryCellSize)
- else
- deployLoc[3] = math.floor(quarryCellSize/2) + (math.floor(qI/2)*quarryCellSize)
- end
- if net.isOpen(PORT) == false then net.open(PORT) end
- print("turtle ?"..ID.." sent to: ("..deployLoc[1]..","..deployLoc[2]..","..deployLoc[3]..")")
- send("deploy","{"..deployLoc[1]..","..deployLoc[2]..","..deployLoc[3]..",false,"..quarryCellSize..","..quarryCellSize..","..quarryDepth.."}")
- return true
- end
- local function netGreeter() --Huge bulk of the program #2: manages 99%* of incoming communication. *except for when it doesn't.
- printLeft("netInit")
- net.open(PORT)
- while true do
- local _, _, _, _, msg, msg2 = os.pullEvent("modem_message")
- printLeft("modem_receive: "..msg)
- if msg == "init" then
- send("shake")
- sleep(0.5)
- deployTurtle(-1,quarryProgress+1)
- quarryProgress = quarryProgress + 1
- end
- if msg == "done" then
- deployTurtle(-1,quarryProgress+1)
- quarryProgress = quarryProgress + 1
- end
- end
- end
- local function autoSave() --self explanatory
- sleep(15)
- while true do
- saveData()
- sleep(15)
- end
- end
- parallel.waitForAny(netGreeter,leftMonitor,rightMonitor,autoSave)
- --My favourite API ever, allows simultaneous running of all functions specified in parameters (imagine having 3 computers/programs in one)
- --Huge protip: write your programs in functions. Commenting this line allows you to use your whole program as an API (for lua> debugging, etc.). ???. Profit!
- --^Learned the hard way that this is not true everywhere else Lua other than Computercraft, such as OpenComputers, hence bridging both mods for one purpose.
Advertisement
Add Comment
Please, Sign In to add comment