Megaddd

Megaddd - quarry server, bridged with OC - ComputerCraft

Jun 12th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.35 KB | None | 0 0
  1. --Deluxe W.E.Autoquarry Control Hub script by Megaddd. 2013-11-11
  2. --Major protip: do NOT run servers within networking range of one-another.
  3.  
  4. --[[
  5. Notice: coordinates such as homePos or refuelPos are relative to the turtle spawn-point,
  6. meaning they do not need to be world coordinates.
  7. (though, it will help turtles if you use the real 'y' value)
  8.  
  9. 2013-11-14: first somewhat functional release. Saving broken for the most part.
  10. 2014-06-12: Modified to work with OpenComputers robots and wireless peripheral.
  11. --]]
  12. local MODEMSIDE = "back"
  13. local PORT = 1337
  14.  
  15. local NETSIDE = "back"               --Side of the rednet modem on the computer/turtle (why would you be using a turtle as a server, though?)
  16. local MSGTIME = 60                   --How long to leave rednet message logs on screen for (0 for no limit)
  17. local FLASHTIME = 5                  --How long new messages stay highlighted for
  18.  
  19. --local CrashIfNoAPI = true          --Whether to immediatelly close the program if list api isn't found (will prevent adding new turtles if false.
  20. local FILENAME = "QuarryData.dat"    --Quarry data storage, changing will void all current data. (turtle IDs, quarry progress, etc..)
  21. 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.
  22. local APINAME2 = "tu"                --Improved TextUtils by ElvishJerricco, as default textutils may have saving problems with QuarryData.dat
  23. local quarryCellSize = 3             --Size of each turtle's individual mini-quarry.
  24. local quarryWidthLength = {7,24}     --number of quarry cells on x and z to be quarried. (effectively - quarry size)
  25. local quarryDepth = 9                --does this really need a tooltip? fine. Quarry depth in blocks (not cells).
  26. local homePos = {0,0,0}              --Location of turtle spawn-point, x/y/z. (where every turtle's MAWQ program is first run at)
  27. 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)
  28. local dumpPos = {-5,0,0}             --Location of the quarry output chest.
  29. local turtleList = {}                --ID's of all disposable turtles.
  30. local idleTurtles = {}               --ID's of currently inactive, ready turtles.
  31. local activeTurtles = {}             --{["turtleID"]=qI,..} (probably obsolete, might be used in the future for something useful)
  32. local quarryProgress = 0             --Number of cells quarried.
  33.  
  34. net = peripheral.wrap(MODEMSIDE)
  35. net.open(PORT)
  36.  
  37. --os.loaAPI("tu") --do not use unless serializing becomes broken. (does not effect unserializing)
  38. os.loadAPI("mlf")
  39.  
  40. textLinesLeft = {}  --textLines = {{"text in line1 bla bla potato",uptime},{"Hey Joey, how's the omelette?",uptime}...}
  41. textLinesLeftLast = {}
  42.  
  43. --[[function pw()
  44.     return (32768*2 - math.sin(math.ceil((os.time())*10))*32768)
  45. end]]--
  46. --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)
  47.    
  48. local function leftMonitor()
  49.     local rmon = peripheral.wrap("left")
  50.     rmon.setTextScale(1)
  51.     while true do
  52.         local monx, mony = rmon.getSize()
  53.         if textLinesLeft[1] ~= nil then
  54.             rmon.setBackgroundColor(colors.black)
  55.             rmon.clear()
  56.             for i=1,#textLinesLeft do
  57.                 rmon.setCursorPos(1,mony+1-i)
  58.                 local textcol
  59.                 local uptime = textLinesLeft[i][2]
  60.                 if uptime < MSGTIME/2 then rmon.setTextColor(colors.red) else rmon.setTextColor(colors.gray) end
  61.                 if uptime < FLASHTIME then
  62.                     for i=1,monx do write(" ") end
  63.                     rmon.setBackgroundColor(colors.gray)
  64.                 else
  65.                     rmon.setBackgroundColor(colors.black)
  66.                 end
  67.                 rmon.setCursorPos(1,mony+1-i)
  68.                 rmon.write(textLinesLeft[i][1])
  69.                 textLinesLeftLast = textLinesLeft
  70.             end
  71.         end
  72.         for i=1,mony do
  73.             if textLinesLeft[i] ~= nil then
  74.                 textLinesLeft[i][2] = textLinesLeft[i][2] + 1
  75.                 if MSGTIME > 0 then
  76.                     if textLinesLeft[i][2] >= MSGTIME then
  77.                         textLinesLeft[i] = nil
  78.                     end
  79.                 end
  80.             end
  81.         end
  82.         sleep(1)
  83.     end
  84. end
  85.  
  86. function printLeft(Text)
  87.     local mon = peripheral.wrap("left")
  88.     local monx, mony = mon.getSize()
  89.     for i=mony,1,-1 do
  90.         textLinesLeft[i] = textLinesLeft[i-1]
  91.     end
  92.     textLinesLeft[1] = {Text,0}
  93. end
  94.  
  95. textLinesRight = {}  --textLines = {{"text in line1 bla bla potato",uptime},{"Hey Joey, how's the omelette?",uptime}...}
  96. textLinesRightLast = {}
  97. local function rightMonitor()
  98.     local rmon = peripheral.wrap("right")
  99.     rmon.setTextScale(1)
  100.     while true do
  101.         local monx, mony = rmon.getSize()
  102.         if textLinesRight[1] ~= nil then
  103.             rmon.setBackgroundColor(colors.black)
  104.             rmon.clear()
  105.             for i=1,#textLinesRight do
  106.                 rmon.setCursorPos(1,mony+1-i)
  107.                 local textcol
  108.                 local uptime = textLinesRight[i][2]
  109.                 if uptime < MSGTIME/2 then rmon.setTextColor(colors.lime) else rmon.setTextColor(colors.gray) end
  110.                 if uptime < FLASHTIME then
  111.                     for i=1,monx do write(" ") end
  112.                     rmon.setBackgroundColor(colors.gray)
  113.                 else
  114.                     rmon.setBackgroundColor(colors.black)
  115.                 end
  116.                 rmon.setCursorPos(1,mony+1-i)
  117.                 rmon.write(textLinesRight[i][1])
  118.                 textLinesRightLast = textLinesRight
  119.             end
  120.         end
  121.         for i=1,mony do
  122.             if textLinesRight[i] ~= nil then
  123.                 textLinesRight[i][2] = textLinesRight[i][2] + 1
  124.                 if MSGTIME > 0 then
  125.                     if textLinesRight[i][2] >= MSGTIME then
  126.                         textLinesRight[i] = nil
  127.                     end
  128.                 end
  129.             end
  130.         end
  131.         sleep(1)
  132.     end
  133. end
  134.  
  135. function printRight(Text)
  136.     local mon = peripheral.wrap("left")
  137.     local monx, mony = mon.getSize()
  138.     for i=mony,1,-1 do
  139.         textLinesRight[i] = textLinesRight[i-1]
  140.     end
  141.     textLinesRight[1] = {Text,0}
  142. end
  143.        
  144.  
  145. function send(p1, p2, p3, p4, p5)
  146.     net.transmit(PORT,500,p1, p2, p3, p4, p5)
  147. end
  148.  
  149. function saveData() --saves most of the quarry data, changing anything here will most likely break something/everything down the line.
  150.     local in7 = quarryProgress
  151.     h = fs.open(FILENAME,"w")
  152.     h.writeLine(textutils.serialize(in7))
  153.     h.close()
  154. end
  155.  
  156. if fs.exists(FILENAME) then --Touching this breaks it way too often for some reason.
  157.     h = fs.open(FILENAME,"r")
  158.     quarryProgress = textutils.unserialize(h.readLine())
  159.     h.close()
  160. else
  161.     saveData()
  162. end
  163.  
  164. function deployTurtle(ID,qI) --left/right alternating quarry placement. Sends turtles on their merry way.
  165.     local deployLoc = {9,homePos[2],0}
  166.     local row = math.floor((qI-1) / quarryWidthLength[1])
  167.     local rowDecrement = quarryCellSize * row
  168.     deployLoc[1] = 9 + rowDecrement
  169.     qI = qI % quarryWidthLength[1]
  170.     if qI == 0 then qI = quarryWidthLength[1] end
  171.     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 :>)
  172.         deployLoc[3] = math.floor(quarryCellSize/2) - (math.floor(qI/2)*quarryCellSize)
  173.     else
  174.         deployLoc[3] = math.floor(quarryCellSize/2) + (math.floor(qI/2)*quarryCellSize)
  175.     end
  176.    
  177.     if net.isOpen(PORT) == false then net.open(PORT) end
  178.     print("turtle ?"..ID.." sent to: ("..deployLoc[1]..","..deployLoc[2]..","..deployLoc[3]..")")
  179.     send("deploy","{"..deployLoc[1]..","..deployLoc[2]..","..deployLoc[3]..",false,"..quarryCellSize..","..quarryCellSize..","..quarryDepth.."}")
  180.     return true
  181. end
  182.  
  183. local function netGreeter() --Huge bulk of the program #2: manages 99%* of incoming communication. *except for when it doesn't.
  184.     printLeft("netInit")
  185.     net.open(PORT)
  186.     while true do
  187.         local _, _, _, _, msg, msg2 = os.pullEvent("modem_message")
  188.         printLeft("modem_receive: "..msg)
  189.         if msg == "init" then
  190.             send("shake")
  191.             sleep(0.5)
  192.             deployTurtle(-1,quarryProgress+1)
  193.             quarryProgress = quarryProgress + 1
  194.         end
  195.         if msg == "done" then
  196.             deployTurtle(-1,quarryProgress+1)
  197.             quarryProgress = quarryProgress + 1
  198.         end
  199.     end
  200. end
  201.  
  202. local function autoSave() --self explanatory
  203.     sleep(15)
  204.     while true do
  205.         saveData()
  206.         sleep(15)
  207.     end
  208. end
  209.  
  210. parallel.waitForAny(netGreeter,leftMonitor,rightMonitor,autoSave)
  211. --My favourite API ever, allows simultaneous running of all functions specified in parameters (imagine having 3 computers/programs in one)
  212. --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!
  213. --^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