Advertisement
pedrosgali

NewTurtleMonitor

Mar 18th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 59.76 KB | None | 0 0
  1. os.loadAPI("API/draw")
  2.  
  3. --CONSTANTS--
  4.  
  5. SCALE = 0.5
  6.  
  7. --VARIABLES--
  8.  
  9. --Counters
  10. tickCount = 0
  11. --Switches
  12. drawn = false
  13. monitorBool = false
  14. buttonInit = false
  15. baseBool = true
  16. turtleBool = false
  17. itemBool = false
  18. bpBool = false
  19. overlayBool = false
  20. facing = false
  21. --Lengths
  22. buttonLen = 15
  23. chunkTileX = 10
  24. chunkTileY = 8
  25. turtleTileX = 10
  26. turtleTileY = 9
  27. bpTileX = 4
  28. bpTileY = 3
  29.  
  30. --UTILITY FUNCTIONS--
  31.  
  32. function getPeripherals()
  33.     perData = peripheral.getNames()
  34.     for i = 1, #perData do
  35.         if peripheral.getType(perData[i]) == "modem" then
  36.             m = peripheral.wrap(perData[i])
  37.             if m.isWireless() then
  38.                 wifiSide = perData[i]
  39.                 rednet.open(wifiSide)
  40.             end
  41.         elseif peripheral.getType(perData[i]) == "monitor" then
  42.             mon = peripheral.wrap(perData[i])
  43.             mon.setTextScale(SCALE)
  44.             x, y = mon.getSize()
  45.         end
  46.     end
  47. end
  48.  
  49. function switchMon()
  50.     if monitorBool then
  51.         term.restore()
  52.     else
  53.         term.redirect(mon)
  54.     end
  55.     monitorBool = not monitorBool
  56. end
  57.  
  58. function debugMsg(text)
  59.     switchMon()
  60.     if text ~= " " then
  61.         timeNow = os.time()
  62.         timeStamp = textutils.formatTime(timeNow, true)
  63.         if #timeStamp < 5 then
  64.             timeStamp = "0"..timeStamp
  65.         end
  66.         term.setBackgroundColor(draw.black)
  67.         term.setTextColor(draw.lime)
  68.         print(timeStamp..": "..text)
  69.     else
  70.         print()
  71.     end
  72.     switchMon()
  73. end
  74.  
  75. function locNames()
  76.     cLabel = {}
  77.     cLabel[1] = "nwnw"
  78.     cLabel[2] = "nnw"
  79.     cLabel[3] = "nn"
  80.     cLabel[4] = "nne"
  81.     cLabel[5] = "nene"
  82.     cLabel[6] = "nww"
  83.     cLabel[7] = "nw"
  84.     cLabel[8] = "n"
  85.     cLabel[9] = "ne"
  86.     cLabel[10] = "nee"
  87.     cLabel[11] = "ww"
  88.     cLabel[12] = "w"
  89.     cLabel[13] = "HQ"
  90.     cLabel[14] = "e"
  91.     cLabel[15] = "ee"
  92.     cLabel[16] = "sww"
  93.     cLabel[17] = "sw"
  94.     cLabel[18] = "s"
  95.     cLabel[19] = "se"
  96.     cLabel[20] = "see"
  97.     cLabel[21] = "swsw"
  98.     cLabel[22] = "ssw"
  99.     cLabel[23] = "ss"
  100.     cLabel[24] = "sse"
  101.     cLabel[25] = "sese"
  102. end
  103.  
  104. function getStartData()
  105.     draw.clear()
  106.     draw.inputBox("Home chunk X pos:")
  107.     x = tonumber(read())
  108.     draw.inputBox("Home chunk Y pos:")
  109.     y = tonumber(read())
  110.     draw.inputBox("Home chunk Z pos:")
  111.     z = tonumber(read())
  112.     xmod = x - 32
  113.     zmod = z - 32
  114.     grX = 10
  115.     grY = 2
  116.     griMagic = {}
  117.     for i = 1, 25 do
  118.         griMagic[i] = {
  119.             xPos = xmod;
  120.         zPos = zmod;
  121.         scrX = grX;
  122.         scrY = grY;
  123.         }
  124.         if i % 5 == 0 then
  125.             xmod = xmod - 64
  126.             zmod = zmod + 16
  127.             grX = 10
  128.             grY = grY + 7
  129.         else
  130.             xmod = xmod + 16
  131.             grX = grX + 9
  132.         end
  133.     end
  134. end
  135.  
  136. function saveLocs()
  137.     data = fs.open("Tables/locData", "w")
  138.     data.writeLine(textutils.serialize(locData))
  139.     data.close()
  140. end
  141.  
  142. function loadLocs()
  143.     if fs.exists("Tables/locData") then
  144.         data = fs.open("Tables/locData", "r")
  145.         locData = textutils.unserialize(data.readLine())
  146.         data.close()
  147.         --debugMsg("SYSTEM: Location data loaded...TRUE")
  148.     else
  149.         term.restore()
  150.         locNames()
  151.         getStartData()
  152.         locData = {}
  153.         for i = 1, 25 do
  154.             locData[i] = {
  155.                 name = cLabel[i];
  156.             scrX = griMagic[i]["scrX"];
  157.             scrY = griMagic[i]["scrY"];
  158.             xPos = griMagic[i]["xPos"];
  159.             yPos = y;
  160.             zPos = griMagic[i]["zPos"];
  161.             hPos = 0;
  162.             selected = false;
  163.             mined = false;
  164.             field = false;
  165.             planted = false;
  166.             built = false;
  167.             colour = draw.red;
  168.             turtle = nil;
  169.             jobs = {
  170.                     miner = {};
  171.                    sorter = {};
  172.                    utility = {};
  173.                    };
  174.             }
  175.         end
  176.         term.redirect(mon)
  177.     end
  178.     --debugMsg(" ")
  179.     saveLocs()
  180. end
  181.  
  182. function saveTurtles()
  183.     data = fs.open("Tables/tData", "w")
  184.     data.writeLine(textutils.serialize(tData))
  185.     data.close()
  186. end
  187.  
  188. function loadTurtles()
  189.     if fs.exists("Tables/tData") then
  190.         data = fs.open("Tables/tData", "r")
  191.         tData = textutils.unserialize(data.readLine())
  192.         data.close()
  193.         --debugMsg("SYSTEM: Turtle list loaded...TRUE")
  194.     else
  195.         tData = {}
  196.         debugMsg("SYSTEM: New turtle list generated...TRUE")
  197.     end
  198.     saveTurtles()
  199. end
  200.  
  201. function saveChests()
  202.     data = fs.open("Tables/chests", "w")
  203.     data.writeLine(textutils.serialize(chestTab))
  204.     data.close()
  205. end
  206.  
  207. function loadChestData()
  208.     if fs.exists("Tables/chests") then
  209.         data = fs.open("Tables/chests", "r")
  210.         chestTab = textutils.unserialize(data.readLine())
  211.         data.close()
  212.     else
  213.         chestTab = {}
  214.     end
  215. end
  216.  
  217. --BUTTON FUNCTIONS--
  218.  
  219. function buttonSetup()
  220.     scrTab = {}
  221.     buttonX = x - buttonLen
  222.     buttonY = 1
  223.     spaceLength = " "
  224.     for i = 1, buttonLen do
  225.         spaceLength = spaceLength.." "
  226.     end
  227.     for i = 1, y do
  228.         draw.writeText(x - buttonLen, i, draw.black, draw.black, spaceLength)
  229.     end
  230. end
  231.  
  232. function addButton(label, command, colour, opt1, opt2)
  233.     if scrTab[1] ~= nil then
  234.         newMax = #scrTab + 1
  235.     else
  236.         newMax = 1
  237.     end
  238.     scrTab[newMax] = {
  239.         text = label;
  240.         order = command;
  241.         colour = colour;
  242.         opt1 = opt1;
  243.         opt2 = opt2;
  244.         scrX = buttonX;
  245.         scrY = buttonY;
  246.     }
  247.     if buttonY < y - 3 then
  248.         buttonY = buttonY + 3
  249.     else
  250.         buttonY = 1
  251.         buttonX = buttonX - buttonLen
  252.     end
  253. end
  254.  
  255. function drawButtons()
  256.     if scrTab == nil then
  257.         buttonSetup()
  258.     else
  259.         if not buttonInit then
  260.             dashLen = "-"
  261.             spaceLen = " "
  262.             for i = 1, buttonLen - 3 do
  263.                 dashLen = dashLen.."-"
  264.                 spaceLen = spaceLen.." "
  265.             end
  266.             buttonInit = true
  267.         end
  268.         for i = 1, #scrTab do
  269.             draw.writeText(scrTab[i]["scrX"], scrTab[i]["scrY"], draw.black, scrTab[i]["colour"], "+"..dashLen.."+")
  270.             draw.writeText(scrTab[i]["scrX"], scrTab[i]["scrY"] + 1, draw.black, scrTab[i]["colour"], ":"..spaceLen..":")
  271.             draw.writeText(scrTab[i]["scrX"], scrTab[i]["scrY"] + 2, draw.black, scrTab[i]["colour"], "+"..dashLen.."+")
  272.             draw.box(scrTab[i]["scrX"] + 1, scrTab[i]["scrY"] + 1, scrTab[i]["scrX"] + (buttonLen - 2), scrTab[i]["scrY"] + 1, scrTab[i]["colour"])
  273.             draw.centerText(scrTab[i]["scrX"] + 1, scrTab[i]["scrX"] + (buttonLen - 2), scrTab[i]["scrY"] + 1, scrTab[i]["colour"], draw.black, scrTab[i]["text"])
  274.         end
  275.     end
  276. end
  277.  
  278. --BUTTON GROUPS--
  279.  
  280. function mainMenu()
  281.     buttonSetup()
  282.     addButton("Base", "Base", draw.lime)  
  283.     addButton("Items", "Item", draw.lime)
  284.     addButton("Turtles", "Turtle", draw.lime)
  285. end
  286.  
  287. function BPMenu()
  288.     buttonSetup()
  289.     addButton("Exit", "none", draw.red)
  290.     if not gridView then
  291.         addButton("Back", "none", draw.red)
  292.     end
  293.     addButton("Load", "none", draw.lightBlue)
  294.     addButton("Load Preset", "none", draw.lightBlue)
  295.     addButton("Save", "none", draw.lightBlue)
  296.     addButton("Up Level", "none", draw.green)
  297.     addButton("Down Level", "none", draw.green)
  298.     if overlayBool then
  299.         addButton("Overlay", "none", draw.white)
  300.     else
  301.         addButton("Overlay", "none", draw.blue)
  302.     end
  303.     if facing then
  304.         addButton("Facing", "none", draw.white)
  305.     else
  306.         addButton("Facing", "none", draw.blue)
  307.     end
  308. end
  309.  
  310. function BPButtons()
  311.     buttonSetup()
  312.     addButton("Menu", "none", draw.white)
  313.     addButton("Clear", "air", draw.blue, "ai", draw.blue)
  314.     addButton("Plants", "none", draw.lime)
  315.     addButton("Wood", "none", draw.brown)
  316.     addButton("Stone", "none", draw.grey)
  317.     addButton("Fancy", "none", draw.lightBlue)
  318.     addButton("Utility", "none", draw.orange)
  319.     addButton("Access", "none", draw.green)
  320.     addButton("Mods", "none", draw.yellow)
  321. end
  322.  
  323. function plantMenu()
  324.     buttonSetup()
  325.     addButton("Menu", "none", draw.white)
  326.     addButton("Back", "none", draw.red)
  327.     addButton("Dirt", "Dirt", draw.brown, "Di", draw.brown)
  328.     addButton("Oak Sap", "Oak Sapling", draw.green, "Sa", draw.green)
  329.     addButton("Seeds", "Seeds", draw.lime, "Se", draw.yellow)
  330.     addButton("Sugar", "Sugar Canes", draw.lime, "Su", draw.lime)
  331.     addButton("Melon", "Melon Seeds", draw.green, "Me", draw.green)
  332.     addButton("Pumpkin", "Pumpkin Seeds", draw.orange, "Pu", draw.orange)
  333. end
  334.  
  335. function woodMenu()
  336.     buttonSetup()
  337.     addButton("Menu", "none", draw.white)
  338.     addButton("Back", "none", draw.red)
  339.     addButton("Oak", "Oak Wood", draw.brown, "OW", draw.brown)
  340.     addButton("Oak Planks", "Oak Wood Planks", draw.brown, "OP", draw.brown)
  341.     addButton("Oak Stairs", "Oak Wood Stairs", draw.brown, "ST", draw.brown)
  342.     addButton("Fence", "Fence", draw.brown, "Fe", draw.brown)
  343. end
  344.  
  345. function stoneMenu()
  346.     buttonSetup()
  347.     addButton("Menu", "none", draw.white)
  348.     addButton("Back", "none", draw.red)
  349.     addButton("Cobble", "Cobblestone", draw.lightGrey, "CO", draw.lightGrey)
  350.     addButton("Cobble St", "Stone Stairs", draw.lightGrey, "ST", draw.lightGrey)
  351.     addButton("Stone", "Stone", draw.lightGrey, "ST", draw.lightGrey)
  352.     addButton("Stone Br", "Stone Bricks", draw.grey, "SB", draw.grey)
  353.     addButton("Stone Br St", "Stone Bricks Stairs", draw.grey, "SB", draw.grey)
  354. end
  355.  
  356. function fancyMenu()
  357.     buttonSetup()
  358.     addButton("Menu", "none", draw.white)
  359.     addButton("Back", "none", draw.red)
  360.     addButton("Glass", "Glass", draw.lightBlue, "GL", draw.lightBlue)
  361.     addButton("Glass Pane", "Glass Pane", draw.lightBlue, "GP", draw.lightBlue)
  362.     addButton("Torch", "Torch", draw.orange, "TO", draw.orange)
  363. end
  364.  
  365. function utilityMenu()
  366.     buttonSetup()
  367.     addButton("Menu", "none", draw.white)
  368.     addButton("Back", "none", draw.red)
  369.     addButton("Workbench", "Crafting Table", draw.orange, "CR", draw.orange)
  370.     addButton("Furnace", "Furnace", draw.grey, "FU", draw.grey)
  371.     addButton("Chest", "Chest", draw.yellow, "CH", draw.yellow)
  372.     addButton("Iron Chest", "Iron Chest", draw.lightGrey, "CH", draw.lightGrey)
  373.     addButton("Gold Chest", "Gold Chest", draw.yellow, "CH", draw.yellow)
  374.     addButton("Diamond Chest", "Diamond Chest", draw.lightBlue, "CH", draw.lightBlue)
  375. end
  376.  
  377. function accessMenu()
  378.     buttonSetup()
  379.     addButton("Menu", "none", draw.white)
  380.     addButton("Back", "none", draw.red)
  381.     addButton("Storage", "storage", draw.yellow, "CA", draw.yellow)
  382.     addButton("Furnace", "furnace", draw.grey, "FA", draw.grey)
  383. end
  384.  
  385. function modsMenu()
  386.     buttonSetup()
  387.     addButton("Menu", "none", draw.white)
  388.     addButton("Back", "none", draw.red)
  389.     addButton("IC2", "none", draw.lightGrey)
  390.     --addButton("Buildcraft", "none", draw.blue)
  391.     --addButton("Forestry", "none", draw.blue)
  392. end
  393.  
  394. function IC2Menu()
  395.     buttonSetup()
  396.     addButton("Menu", "none", draw.white)
  397.     addButton("Back", "none", draw.red)
  398.     addButton("Generator", "Generator", draw.lightGrey, "MA", draw.lightGrey)
  399.     addButton("Macerator", "Macerator", draw.lightGrey, "MA", draw.lightGrey)
  400.     addButton("Elec Furnace", "Electric Furnace", draw.lightGrey, "EF", draw.lightGrey)
  401.     addButton("Tin Cable", "Insulated Tin Cable", draw.lightGrey, "TC", draw.lightGrey)
  402.     addButton("Cop Cable", "Copper Cable", draw.orange, "CC", draw.orange)
  403.     addButton("Bat Box", "Bat Box", draw.brown, "BB", draw.brown)
  404. end
  405.  
  406. function turtleMenu()
  407.     mainMenu()
  408.     if selChunk == nil then
  409.         chunkFound = false
  410.         for i = 1, #locData do
  411.             if not locData[i]["mined"] then
  412.                 chunkFound = true
  413.                 selChunk = locData[i]["name"]
  414.                 break
  415.             end
  416.         end
  417.         if not chunkFound then
  418.             selChunk = "HQ"
  419.         end
  420.     end
  421.     for i = 1, #tData do
  422.         if tData[i]["selected"] then
  423.             if tData[i]["type"] == "Unassigned" then
  424.                 addButton("Set Miner", "setMiner", draw.lightGrey)
  425.                 addButton("Set Sorter", "setSorter", draw.orange)
  426.                 addButton("set Utility", "setUtility", draw.blue)
  427.             elseif tData[i]["type"] == "miner" then
  428.                 miningMenu(selChunk)
  429.             elseif tData[i]["type"] == "sorter" then
  430.                 sortMenu()
  431.             elseif tData[i]["type"] == "utility" then
  432.                 utilMenu()
  433.             end
  434.         end
  435.     end
  436. end
  437.  
  438. function baseMenu()
  439.     mainMenu()
  440. end
  441.  
  442. function zoomMenu()
  443.     mainMenu()
  444.     addButton("Edit BP "..selChunk, "Blueprints/"..selChunk, draw.lightBlue, 16, 16)
  445. end
  446.  
  447. function miningMenu(label)
  448.     mainMenu()
  449.     if baseBool then
  450.         addButton("Zoom", "none", draw.purple)
  451.         addButton("Edit BP "..label, "Blueprints/"..label, draw.lightBlue, 16, 16)
  452.     elseif turtleBool then
  453.         addButton("Recall", "recall", draw.red)
  454.     end
  455.     addButton("Mine "..label, "mine "..label.." 16 16", draw.lightGrey, label, "mine")
  456.     addButton("Clear "..label, "clear "..label.." 16 16 "..locData[1]["yPos"] + 1, draw.white, label, "clear")
  457.     addButton("Chop "..label, "clear "..label.." 16 16 "..locData[1]["yPos"] + 2, draw.white, label, "chop")
  458.     addButton("Plant "..label, "plant "..label.." justTrees "..locData[1]["yPos"] + 2, draw.white, label, "chop")
  459.     addButton("Build "..label, "build "..label.." "..label, draw.lightBlue, label, "built")
  460. end
  461.  
  462. function sortMenu()
  463.    
  464. end
  465.  
  466. function utilMenu()
  467.    
  468. end
  469.  
  470. function presetMenu()
  471.     buttonSetup()
  472.     addButton("Menu", "none", draw.white)
  473.     addButton("Back", "none", draw.red)
  474.     fileTab = fs.list("Presets")
  475.     for i = 1, #fileTab do
  476.         addButton(fileTab[i], "Preset", draw.green)
  477.     end
  478. end
  479.  
  480. --JOB FUNCTIONS--
  481.  
  482. function addJob(chunk, order, tType)
  483.     for i = 1, #locData do
  484.         if locData[i]["name"] == chunk then
  485.             if locData[i]["jobs"][tType][1] == nil then
  486.                 newMax = 1
  487.             else
  488.                 newMax = #locData[i]["jobs"][tType] + 1
  489.             end
  490.             locData[i]["jobs"][tType][newMax] = {}
  491.             locData[i]["jobs"][tType][newMax]["order"] = order
  492.             locData[i]["jobs"][tType][newMax]["type"] = tType
  493.             break
  494.         end
  495.     end
  496.     debugMsg("ADDJOB: "..order.." added.")
  497.     saveLocs()
  498. end
  499.  
  500. function removeJob(chunk, tType)
  501.     for i = 1, #locData do
  502.         if locData[i]["name"] == chunk then
  503.             if #locData[i]["jobs"][tType] > 1 then
  504.                 for j = 2, #locData[i]["jobs"][tType] do
  505.                     locData[i]["jobs"][tType][j - 1] = locData[i]["jobs"][tType][j]
  506.                     locData[i]["jobs"][tType][j] = nil
  507.                 end
  508.             else
  509.                 locData[i]["jobs"][tType][1] = nil
  510.             end
  511.         end
  512.     end
  513.     saveLocs()
  514. end
  515.  
  516. function checkJobs(tType)
  517.     --debugMsg("JOBDAT: Checking "..tType.." jobs.")
  518.     loadLocs()
  519.     for i = 1, #locData do
  520.         if locData[i]["jobs"][tType][1] ~= nil then
  521.             --debugMsg("JOBDAT: Job entry Found... "..locData[i]["name"])
  522.             if checkSend(locData[i]["jobs"][tType][1]["order"], locData[i]["jobs"][tType][1]["type"]) then
  523.                 debugMsg("JOBDAT: "..locData[i][tType][1]["order"].." sent.")
  524.                 removeJob(locData[i]["name"], tType)
  525.             end
  526.         end
  527.     end
  528.     --debugMsg(" ")
  529. end
  530.  
  531. function setType(id, tType)
  532.     for i = 1, #tData do
  533.         if tData[i]["identifier"] == id then
  534.             tData[i]["type"] = tType
  535.             if tType == "miner" then
  536.                 tData[i]["colour"] = draw.lightGrey
  537.             elseif tType == "support" then
  538.                 tData[i]["colour"] = draw.orange
  539.             elseif tType == "utility" then
  540.                 tData[i]["colour"] = draw.blue
  541.             end
  542.         end
  543.     end
  544.     saveTurtles()
  545. end
  546.  
  547. --TURTLE FUNCTIONS--
  548.  
  549. function saveTurtleChests()
  550.     data = fs.open("Tables/tChests", "w")
  551.     data.writeLine(textutils.serialize(tcTab))
  552.     data.close()
  553. end
  554.  
  555. function loadTurtleChests()
  556.     if fs.exists("Tables/tChests") then
  557.         data = fs.open("Tables/tChests", "r")
  558.         tcTab = textutils.unserialize(data.readLine())
  559.         data.close()
  560.     else
  561.         tcTab = {}
  562.     end
  563. end
  564.  
  565. function readFile()
  566.     tTab = nil
  567.     data = fs.open("Tables/tSheet", "r")
  568.     tTab = textutils.unserialize(data.readLine())
  569.     data.close()
  570.     fs.delete("Tables/tSheet")
  571. end
  572.  
  573. function addTurtle(tId)
  574.     loadTurtles()
  575.     if tData[1] == nil then
  576.         newMax = 1
  577.     else
  578.         newMax = #tData + 1
  579.     end
  580.     tData[newMax] = {}
  581.     tData[newMax] = tTab
  582.     tData[newMax]["identifier"] = tId
  583.     tData[newMax]["action"] = "No task"
  584.     tData[newMax]["colour"] = draw.purple
  585.     tData[newMax]["type"] = "Unassigned"
  586.     tData[newMax]["selected"] = false
  587.     tData[newMax]["scrX"] = 1
  588.     tData[newMax]["scrY"] = 1
  589.     saveTurtles()
  590.     debugMsg("TURTLE: "..tData[newMax]["name"].." added.")
  591.     makeTurtleTable()
  592.     signalUpdate()
  593. end
  594.  
  595. function addTurtleChest(chLabel, xLoc, yLoc, zLoc, hLoc)
  596.     debugMsg("ADD TC: Adding new turtle chest for "..chLabel..".")
  597.     loadTurtleChests()
  598.     for i = 1, #tcTab do
  599.         if tcTab[i]["name"] == chLabel then
  600.             debugMsg("ADD TC: Chest already exists.")
  601.         end
  602.     end
  603.     if tcTab[1] == nil then
  604.         newMax = 1
  605.     else
  606.         newMax = #tcTab + 1
  607.     end
  608.     tcTab[newMax] = {
  609.         name = chLabel;
  610.         xPos = xLoc;
  611.         yPos = yLoc;
  612.         zPos = zLoc;
  613.         hPos = hLoc;
  614.     }
  615.     saveTurtleChests()
  616.     debugMsg("ADD TC: Chest added.")
  617. end
  618.  
  619. function makeTurtleTable()
  620.     loadTurtles()
  621.     debugMsg("MACHTA: Begin Turtle update...TRUE")
  622.     for i = 1, #tData do
  623.         debugMsg("MACHTA: Checking "..tData[i]["name"].."'s stored data.")
  624.         if tData[i]["homeH"] == 0 then
  625.             addTurtleChest(tData[i]["name"].."Chest", tData[i]["homeX"] + 1, tData[i]["homeY"], tData[i]["homeZ"] + 1, tData[i]["homeH"])
  626.             addTurtleChest(tData[i]["name"].."Fuel", tData[i]["homeX"] - 1, tData[i]["homeY"], tData[i]["homeZ"] + 1, tData[i]["homeH"])
  627.         elseif tData[i]["homeH"] == 1 then
  628.             addTurtleChest(tData[i]["name"].."Chest", tData[i]["homeX"] - 1, tData[i]["homeY"], tData[i]["homeZ"] + 1, tData[i]["homeH"])
  629.             addTurtleChest(tData[i]["name"].."Fuel", tData[i]["homeX"] - 1, tData[i]["homeY"], tData[i]["homeZ"] - 1, tData[i]["homeH"])
  630.         elseif tData[i]["homeH"] == 2 then
  631.             addTurtleChest(tData[i]["name"].."Chest", tData[i]["homeX"] - 1, tData[i]["homeY"], tData[i]["homeZ"] - 1, tData[i]["homeH"])
  632.             addTurtleChest(tData[i]["name"].."Fuel", tData[i]["homeX"] + 1, tData[i]["homeY"], tData[i]["homeZ"] - 1, tData[i]["homeH"])
  633.         elseif tData[i]["homeH"] == 3 then
  634.             addTurtleChest(tData[i]["name"].."Chest", tData[i]["homeX"] + 1, tData[i]["homeY"], tData[i]["homeZ"] - 1, tData[i]["homeH"])
  635.             addTurtleChest(tData[i]["name"].."Fuel", tData[i]["homeX"] + 1, tData[i]["homeY"], tData[i]["homeZ"] + 1, tData[i]["homeH"])
  636.         end
  637.         debugMsg("MACHTA: Check complete.")
  638.     end
  639.     debugMsg("MACHTA: All updates complete.")
  640.     saveTurtleChests()
  641. end
  642.  
  643. function turtleLookup(tId, label)
  644.     loadTurtleChests()
  645.     debugMsg("TUR LU: Checking turtle chests for "..label..".")
  646.     for i = 1, #tcTab do
  647.         debugMsg("TUR LU: found "..tcTab[i]["name"]..".")
  648.         if tcTab[i]["name"] == label then
  649.             debugMsg("TUR LU: "..label.." found.")
  650.             rednet.send(tId, "travel "..tcTab[i]["name"])
  651.         end
  652.     end
  653. end
  654.  
  655. function signalUpdate()
  656.     debugMsg("UPDATE: Update triggered.")
  657.     for i = 1, #tData do
  658.         tData[i]["updated"] = false
  659.         if not tData[i]["updated"] then
  660.             debugMsg("UPDATE: "..tData[i]["name"].." needs update.")
  661.         end
  662.     end
  663.     update = true
  664.     saveTurtles()
  665. end
  666.  
  667. function updateTurtleChests(tId, tName)
  668.     loadTurtleChests()
  669.     if tcTab[1] == nil then
  670.         makeTurtleTable()
  671.     end
  672.     debugMsg("UPD TC: Updating "..tName.."'s turtle data.")
  673.     for i = 1, #tcTab do
  674.         rednet.send(tId, "updateLocs "..tcTab[i]["name"].." "..tcTab[i]["xPos"].." "..tcTab[i]["yPos"].." "..tcTab[i]["zPos"].." "..tcTab[i]["hPos"])
  675.         sleep(.1)
  676.     end
  677.     debugMsg("LOCUPD: "..tName.."'s turtle data fully updated.")
  678.     saveTurtles()
  679. end
  680.  
  681. function updateLocs(tId, tName)
  682.     --debugMsg("UPD LO: Updating "..tName.."'s location data.")
  683.     for i = 1, #locData do
  684.         rednet.send(tId, "updateLocs "..locData[i]["name"].." "..locData[i]["xPos"].." "..locData[i]["yPos"].." "..locData[i]["zPos"].." "..locData[i]["hPos"])
  685.         sleep(.1)
  686.     end
  687.     --debugMsg("LOCUPD: "..tName.."'s location data fully updated.")
  688.     saveTurtles()
  689. end
  690.  
  691. function updateBP(tId, tName)
  692.     --debugMsg("UPD BP: Updating "..tName.."'s blueprint data.")
  693.     fileTab = fs.list("Blueprints")
  694.     for i = 1, #fileTab do
  695.         debugMsg("UPD BP: Updating "..fileTab[i].." blueprint data.")
  696.         loadBlueprint(fileTab[i])
  697.         bpTab = textutils.serialize(bpTab)
  698.         rednet.send(tId, "updateBP "..fileTab[i])
  699.         sleep(.1)
  700.         rednet.send(tId, bpTab)
  701.         sleep(.1)
  702.     end
  703.     --debugMsg("UPD BP: Updating "..tName.."'s blueprints done.")
  704. end
  705.  
  706. function updateChests(tId, tName)
  707.     --debugMsg("UPD CH: Updating "..tName.."'s chest data.")
  708.     loadChestData()
  709.     for i = 1, #chestTab do
  710.         rednet.send(tId, "updateLocs "..chestTab[i]["id"].." "..chestTab[i]["xPos"].." "..chestTab[i]["yPos"].." "..chestTab[i]["zPos"].." "..chestTab[i]["hPos"])
  711.         sleep(.1)
  712.     end
  713.     --debugMsg("LOCUPD: "..tName.."'s chest data fully updated.")
  714. end
  715.  
  716. function checkUpdates(tId, tName)
  717.     for i = 1, #tData do
  718.         if tData[i]["identifier"] == tId then
  719.             if not tData[i]["updated"] then
  720.                 debugMsg("UPDATE: Updating "..tName..".")
  721.                 updateTurtleChests(tId, tName)
  722.                 updateLocs(tId, tName)
  723.                 updateChests(tId, tName)
  724.                 updateBP(tId, tName)
  725.                 tData[i]["updated"] = true
  726.                 return
  727.             end
  728.         end
  729.     end
  730. end
  731.  
  732. function pushUpdates()
  733.     for i = 1, #tData do
  734.         if tData[i]["action"] == "No task" then
  735.             if not tData[i]["updated"] then
  736.                 debugMsg("UPDATE: Updating "..tData[i]["name"]..".")
  737.                 updateLocs(tData[i]["identifier"], tData[i]["name"])
  738.                 updateTurtleChests(tData[i]["identifier"], tData[i]["name"])
  739.                 updateChests(tData[i]["identifier"], tData[i]["name"])
  740.                 updateBP(tData[i]["identifier"], tData[i]["name"])
  741.                 tData[i]["updated"] = true
  742.                 return
  743.             end
  744.         end
  745.     end
  746. end
  747.  
  748. function checkTurtles(tId) --All turtle state messages here
  749.     loadTurtles()
  750.     readFile()
  751.     found = false
  752.     for i = 1, #tData do
  753.         if tData[i]["identifier"] == tId then
  754.             --debugMsg("TURTLE: "..tData[newMax]["name"].." reported in.")
  755.             found = true
  756.             tData[i]["xPos"] = tTab["xPos"]
  757.             tData[i]["yPos"] = tTab["yPos"]
  758.             tData[i]["zPos"] = tTab["zPos"]
  759.             tData[i]["hPos"] = tTab["hPos"]
  760.             tData[i]["state"] = tTab["state"]
  761.             tData[i]["home"] = tTab["home"]
  762.             tData[i]["opt1"] = tTab["opt1"]
  763.             tData[i]["opt2"] = tTab["opt2"]
  764.             if tData[i]["state"] == "Idle" then --Start of turtle task updates
  765.                 tData[i]["action"] = "No task"
  766.                 debugMsg("TURTLE: "..tData[i]["name"].." idle.")
  767.                 checkUpdates(tId, tData[i]["name"])
  768.             elseif tData[i]["state"] == "Digging" then
  769.                 tData[i]["action"] = "Digging"
  770.             elseif tData[i]["state"] == "Inbound" then
  771.                 tData[i]["action"] = "Inbound"
  772.             elseif tData[i]["state"] == "Outbound" then
  773.                 tData[i]["action"] = "Outbound"
  774.             elseif tData[i]["state"] == "Need sort" then
  775.                 if tData[i]["type"] ~= "sorter" then
  776.                     addJob("HQ", "sort "..tData[i]["name"].."Chest", "sorter")
  777.                 end
  778.             elseif tData[i]["state"] == "Need update" then
  779.                 checkUpdates(tData[i]["identifier"])
  780.             elseif tData[i]["state"] == "Chest lookup" then
  781.                 chestLookup(tData[i]["identifier"], tData[i]["opt1"])
  782.             elseif tData[i]["state"] == "Turtle lookup" then
  783.                 turtleLookup(tData[i]["identifier"], tData[i]["opt1"])
  784.                 tData[i]["action"] = "Sorting"
  785.             elseif tData[i]["state"] == "Item add" then
  786.                 chestModify(tData[i]["opt2"], tData[i]["opt1"])
  787.                 if itemBool then
  788.                     drawCurrent()
  789.                 end
  790.             elseif tData[i]["state"] == "Item take" then
  791.                 chestModify(tData[i]["opt2"], tData[i]["opt1"], "subtract")
  792.                 if itemBool then
  793.                     drawCurrent()
  794.                 end
  795.             end
  796.             saveTurtles()
  797.             break
  798.         end
  799.     end
  800.     if not found then
  801.         addTurtle(tId)
  802.     end
  803. end
  804.  
  805. function checkSend(order, tType)
  806.     --debugMsg("TURTLE: Checking for "..tType.." Turtles.")
  807.     for i = 1, #tData do
  808.         if tData[i]["action"] == "No task" and tData[i]["type"] == tType then
  809.             --debugMsg("TURTLE: Found "..tData[i]["name"].." sending job.")
  810.             tData[i]["action"] = "Got Job"
  811.             rednet.send(tData[i]["identifier"], order)
  812.             return true
  813.         end
  814.     end
  815.     --debugMsg("TURTLE: No idle Turtles found.")
  816.     return false
  817. end
  818.  
  819. --ITEM FUNCTIONS--
  820.  
  821. function addChest(itemType, xLoc, yLoc, zLoc, hLoc)
  822.     loadChestData()
  823.     if chestTab[1] == nil then
  824.         newMax = 1
  825.     else
  826.         for i = 1, #chestTab do
  827.             if chestTab[i]["xPos"] == xLoc and chestTab[i]["yPos"] == yLoc and chestTab[i]["zPos"] == zLoc and chestTab[i]["hPos"] == hLoc then
  828.                 debugMsg("ADDCHE: Chest exists.")
  829.                 return
  830.             end
  831.         end
  832.         newMax = #chestTab + 1
  833.     end
  834.     label = newMax
  835.     chestTab[newMax] = {
  836.         id = label;
  837.         type = itemType;
  838.         xPos = xLoc;
  839.         yPos = yLoc;
  840.         zPos = zLoc;
  841.         hPos = hLoc;
  842.         label = "Nowt";
  843.         amt = 0;
  844.     }
  845.     saveChests()
  846.     debugMsg("ADDCHE: "..itemType.." added X: "..xLoc.." Y: "..yLoc.." Z: "..zLoc)
  847. end
  848.  
  849. function chestLookup(tId, itemName)
  850.     loadChestData()
  851.     debugMsg("CHE LU: Checking for "..itemName)
  852.     chestFound = false
  853.     for i = 1, #chestTab do
  854.         if chestTab[i]["label"] == itemName then
  855.             debugMsg("CHE LU: "..itemName.." found.")
  856.             chestFound = true
  857.             rednet.send(tId, "travel "..i)
  858.             break
  859.         end
  860.     end
  861.     if not chestFound then
  862.         debugMsg("CHE LU: "..itemName.." not found.")
  863.         debugMsg("CHE LU: Setting up new chest.")
  864.         for i = 1, #chestTab do
  865.             if chestTab[i]["label"] == "Nowt" then
  866.                 rednet.send(tId, "travel "..i)
  867.                 chestTab[i]["label"] = itemName
  868.                 debugMsg("CHE LU: Chest added, ID: "..i)
  869.                 saveChests()
  870.                 break
  871.             end
  872.         end
  873.     end
  874. end
  875.  
  876. function chestModify(amount, itemType)
  877.     loadChestData()
  878.     debugMsg("CHEMOD: Checking for "..itemType)
  879.     for i = 1, #chestTab do
  880.         if chestTab[i]["label"] == itemType then
  881.             debugMsg("CHEMOD: "..itemType.." found.")
  882.             if modify == nil then
  883.                 chestTab[i]["amt"] = chestTab[i]["amt"] + amount
  884.                 debugMsg("CHEMOD: "..chestTab[i]["amt"].." "..itemType..".")
  885.                 saveChests()
  886.                 return
  887.             else
  888.                 chestTab[i]["amt"] = chestTab[i]["amt"] - amount
  889.                 debugMsg("CHEMOD: "..chestTab[i]["amt"].." "..itemType..".")
  890.                 saveChests()
  891.                 return
  892.             end
  893.         end
  894.     end
  895. end
  896.  
  897. --BLUEPRINT FUNCTIONS--
  898.  
  899. function newBlueprint(sizeX, sizeZ)
  900.     bpTab = {}
  901.     newBPLayer(1, sizeX, sizeZ, "air")
  902.     turtleGridUpdate()
  903. end
  904.  
  905. function newBPLayer(level, sizeX, sizeZ, itemType)
  906.     bpTab[level] = {}
  907.     local scrX, scrY = 8, 4
  908.     for j = 1, sizeX do
  909.         bpTab[level][j] = {}
  910.         for k = 1, sizeZ do --this might need reversing to the X axis loop
  911.         bpTab[level][j][k] = {}
  912.         bpTab[level][j][k][1] = itemType
  913.         bpTab[level][j][k][2] = draw.blue
  914.         bpTab[level][j][k][3] = scrX
  915.         bpTab[level][j][k][4] = scrY
  916.         bpTab[level][j][k][5] = nil
  917.         bpTab[level][j][k][6] = string.sub(itemType, 1, 2)
  918.         if k ~= sizeX then
  919.             scrX = scrX + 3
  920.         else
  921.             scrX = 8
  922.             scrY = scrY + 2
  923.         end
  924.     end
  925. end
  926. debugMsg("Layer "..level.." added.")
  927. end
  928.  
  929. function makeChestTable()
  930.     debugMsg("MACHTA: Begin Table update...TRUE")
  931.     for h = 1, #bpTab do
  932.         debugMsg("MACHTA: Checking layer"..h)
  933.         for i = 1, #bpTab[h] do
  934.             for j = 1, #bpTab[h][i] do
  935.                 if bpTab[h][i][j][1] == "storage" then
  936.                     addChest("storage", bpTab[1][i][j]["xPos"], locData[1]["yPos"] + (h - 1), bpTab[1][i][j]["zPos"], bpTab[h][i][j][5])
  937.                 elseif bpTab[h][i][j][1] == "furnace" then
  938.                     addChest("furnace", bpTab[1][i][j]["xPos"], locData[1]["yPos"] + (h - 1), bpTab[1][i][j]["zPos"], bpTab[h][i][j][5])
  939.                 end
  940.             end
  941.         end
  942.     end
  943.     debugMsg(" ")
  944. end
  945.  
  946. function saveBlueprint(filename)
  947.     data = fs.open("Blueprints/"..filename, "w")
  948.     data.writeLine(textutils.serialize(bpTab))
  949.     data.close()
  950. end
  951.  
  952. function loadBlueprint(label)
  953.     if fs.exists("Blueprints/"..label) then
  954.         data = fs.open("Blueprints/"..label, "r")
  955.         bpTab = textutils.unserialize(data.readLine())
  956.         data.close()
  957.     else
  958.         newBlueprint(16, 16)
  959.         newBPLayer(2, 16, 16)
  960.     end
  961. end
  962.  
  963. function loadPreset(label)
  964.     data = fs.open("Presets/"..label, "r")
  965.     bpTab = textutils.unserialize(data.readLine())
  966.     data.close()
  967. end
  968.  
  969. function loadLastBP()
  970.     if fs.exists("Blueprints/"..selChunk) then
  971.         data = fs.open("Blueprints/"..selChunk, "r")
  972.         bpTab = textutils.unserialize(data.readLine())
  973.         data.close()
  974.         layer = 1
  975.         item = "air"
  976.         lab = "ai"
  977.         itemColour = draw.blue
  978.     else
  979.         newBlueprint(1, 16, 16, "Dirt")
  980.     end
  981. end
  982.  
  983. --SCREEN FUNCTIONS--
  984.  
  985. function updateChunkState(chunk, state)
  986.     for i = 1, #locData do
  987.         if locData[i]["name"] == chunk then
  988.             if state == "mine" then
  989.                 locData[i]["mined"] = true
  990.                 locData[i]["field"] = false
  991.                 locData[i]["planted"] = false
  992.                 locData[i]["built"] = false
  993.                 break
  994.             elseif state == "field" then
  995.                 locData[i]["field"] = true
  996.                 break
  997.             elseif state == "planted" then
  998.                 locData[i]["planted"] = true
  999.                 break
  1000.             elseif state == "chop" or state == clear then
  1001.                 locData[i]["planted"] = false
  1002.                 locData[i]["built"] = false
  1003.                 break
  1004.             elseif state == "built" then
  1005.                 locData[i]["built"] = true
  1006.                 break
  1007.             end
  1008.         end
  1009.     end
  1010.     debugMsg("SCRDAT: "..chunk.." state updated.")
  1011. end
  1012.  
  1013. function updateBaseScreen()
  1014.     loadLocs()
  1015.     loadTurtles()
  1016.     for i = 1, #locData do
  1017.         if not locData[i]["selected"] then
  1018.             locData[i]["colour"] = draw.purple
  1019.         end
  1020.         if locData[i]["mined"] then
  1021.             locData[i]["colour"] = draw.lightGrey
  1022.         end
  1023.         if locData[i]["field"] then
  1024.             locData[i]["colour"] = draw.brown
  1025.         end
  1026.         if locData[i]["planted"] then
  1027.             locData[i]["colour"] = draw.green
  1028.         end
  1029.         if locData[i]["built"] then
  1030.             locData[i]["colour"] = draw.grey
  1031.         end
  1032.         if locData[i]["turtle"] ~= "None" then
  1033.             locData[i]["colour"] = draw.green
  1034.         end
  1035.         if locData[i]["selected"] then
  1036.             locData[i]["colour"] = draw.lime
  1037.         end
  1038.         locData[i]["turtle"] = "None"
  1039.         tCount = 0
  1040.         for j = 1, #tData do
  1041.             if tData[j]["xPos"] >= locData[i]["xPos"] and tData[j]["xPos"] <= locData[i]["xPos"] + 15 then
  1042.                 if tData[j]["zPos"] <= locData[i]["zPos"] and tData[j]["zPos"] >= locData[i]["zPos"] - 15 then
  1043.                     locData[i]["turtle"] = tData[j]["name"]
  1044.                     tCount = tCount + 1
  1045.                 end
  1046.             end
  1047.         end
  1048.         if tCount > 1 then
  1049.             locData[i]["turtle"] = tCount.." x"
  1050.         end
  1051.         saveLocs()
  1052.     end
  1053. end
  1054.  
  1055. function baseScreen()
  1056.     if not drawn then
  1057.         draw.clear()
  1058.     end
  1059.     updateBaseScreen()
  1060.     if selChunk ~= nil then
  1061.         draw.centerText(1, x - buttonLen, 2, draw.black, draw.lime, selChunk.." selected.")
  1062.     end
  1063.     for i = 1, #locData do
  1064.         if not drawn then
  1065.             local dashLenBase = "-"
  1066.             local spaceLenBase = " "
  1067.             for i = 1, chunkTileX - 3 do
  1068.                 dashLenBase = dashLenBase.."-"
  1069.                 spaceLenBase = spaceLenBase.." "
  1070.             end
  1071.             draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"], draw.black, draw.lime, "+"..dashLenBase.."+")
  1072.             for j = 1, chunkTileY - 2 do
  1073.                 draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + j, draw.black, draw.lime, ":"..spaceLenBase..":")
  1074.             end
  1075.             draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + (chunkTileY - 1), draw.black, draw.lime, "+"..dashLenBase.."+")
  1076.         end
  1077.         draw.box(locData[i]["scrX"] + 1, locData[i]["scrY"] + 1, locData[i]["scrX"] + chunkTileX - 2, locData[i]["scrY"] + chunkTileY - 2, locData[i]["colour"])
  1078.         draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + 1, locData[i]["colour"], draw.black, locData[i]["name"])
  1079.         draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + 2, locData[i]["colour"], draw.black, locData[i]["turtle"])
  1080.         if locData[i]["mined"] then
  1081.             draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + 3, locData[i]["colour"], draw.black, "Mined")
  1082.         end
  1083.         if locData[i]["field"] then
  1084.             draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + 4, locData[i]["colour"], draw.black, "Field")
  1085.         end
  1086.         if locData[i]["planted"] then
  1087.             draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + 5, locData[i]["colour"], draw.black, "Planted")
  1088.         end
  1089.         if locData[i]["built"] then
  1090.             draw.centerText(locData[i]["scrX"], locData[i]["scrX"] + chunkTileX, locData[i]["scrY"] + 6, locData[i]["colour"], draw.black, "Built")
  1091.         end
  1092.     end
  1093.     if not drawn then
  1094.         drawn = true
  1095.     end
  1096.     drawButtons()
  1097. end
  1098.  
  1099. function turtleScreen()
  1100.     loadTurtles()
  1101.     local stx, sty = 3, 2
  1102.     if not drawn then
  1103.         draw.clear()
  1104.         dashLenTur = "-"
  1105.         spaceLenTur = " "
  1106.         for i = 1, turtleTileX - 3 do
  1107.             dashLenTur = dashLenTur.."-"
  1108.             spaceLenTur = spaceLenTur.." "
  1109.         end
  1110.     end
  1111.     for i = 1, #tData do
  1112.         tData[i]["scrX"] = stx
  1113.         tData[i]["scrY"] = sty
  1114.         if not drawn then
  1115.             draw.centerText(stx, stx + turtleTileX, sty, draw.black, draw.lime, "+"..dashLenTur.."+")
  1116.             for j = 1, turtleTileY - 2 do
  1117.                 draw.centerText(stx, stx + turtleTileX, sty + j, draw.black, draw.lime, ":"..spaceLenTur..":")
  1118.             end
  1119.             draw.centerText(stx, stx + turtleTileX, sty + (turtleTileY - 1), draw.black, draw.lime, "+"..dashLenTur.."+")
  1120.             draw.box(stx + 1, sty + 1, stx + (turtleTileX - 2), sty + (turtleTileY - 2), tData[i]["colour"])
  1121.         end
  1122.         draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 1, tData[i]["colour"], draw.black, tData[i]["name"])
  1123.         draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 2, tData[i]["colour"], draw.black, string.sub(tData[i]["state"], 1, turtleTileX - 2))
  1124.         draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 3, tData[i]["colour"], draw.black, tData[i]["action"])
  1125.         draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 4, tData[i]["colour"], draw.black, tData[i]["xPos"])
  1126.         draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 5, tData[i]["colour"], draw.black, tData[i]["yPos"])
  1127.         draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 6, tData[i]["colour"], draw.black, tData[i]["zPos"])
  1128.         if tData[i]["hPos"] == 0 then
  1129.             draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 7, tData[i]["colour"], draw.black, "North")
  1130.         elseif tData[i]["hPos"] == 1 then
  1131.             draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 7, tData[i]["colour"], draw.black, "East")
  1132.         elseif tData[i]["hPos"] == 2 then
  1133.             draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 7, tData[i]["colour"], draw.black, "South")
  1134.         elseif tData[i]["hPos"] == 3 then
  1135.             draw.centerText(stx + 1, stx + (turtleTileX - 1), sty + 7, tData[i]["colour"], draw.black, "West")
  1136.         end
  1137.         if i % 5 ~= 0 then
  1138.             stx = stx + (turtleTileX - 1)
  1139.         else
  1140.             stx = 3
  1141.             sty = sty + (turtleTileY - 1)
  1142.         end
  1143.     end
  1144. end
  1145.  
  1146. function itemScreen()
  1147.     if not drawn then
  1148.         draw.clear()
  1149.     end
  1150.     loadChestData()
  1151.     chestCount = 0
  1152.     scrX = 4
  1153.     scrY = 4
  1154.     for i = 1, #chestTab do
  1155.         if chestTab[i]["type"] == "storage" then
  1156.             if chestTab[i]["amt"] < 64 then
  1157.                 if chestTab[i]["label"] ~= "Nowt" then
  1158.                     colour = draw.red
  1159.                 else
  1160.                     colour = draw.purple
  1161.                 end
  1162.             elseif chestTab[i]["amt"] >= 64 then
  1163.                 colour = draw.orange
  1164.             end
  1165.             if chestTab[i]["amt"] >= 256 then
  1166.                 colour = draw.green
  1167.             end
  1168.             if chestTab[i]["amt"] >= 1728 then
  1169.                 colour = draw.lightGrey
  1170.             end
  1171.             if chestTab[i]["amt"] >= 3456 then
  1172.                 colour = draw.yellow
  1173.             end
  1174.             if chestTab[i]["amt"] >= 5184 then
  1175.                 colour = draw.lightBlue
  1176.             end
  1177.             draw.box(scrX, scrY, scrX + 16, scrY, colour)
  1178.             draw.writeText(scrX, scrY, colour, draw.black, string.sub(chestTab[i]["label"], 1, 10))
  1179.             draw.writeText(scrX + 11, scrY, colour, draw.black, ": "..chestTab[i]["amt"])
  1180.             if scrY < y then
  1181.                 scrY = scrY + 1
  1182.             else
  1183.                 scrX = scrX + 17
  1184.                 scrY = 4
  1185.             end
  1186.             chestCount = chestCount + 1
  1187.             debugMsg("ITMSCR: "..i.." "..chestCount..", "..chestTab[i]["amt"].." "..chestTab[i]["label"])
  1188.         end
  1189.     end
  1190.     draw.centerText(1, x - buttonLen, 2, draw.black, draw.lime, chestCount.." storage chests.")
  1191. end
  1192.  
  1193. function gridSquare(gsX, gsY, colour, text)
  1194.     if not drawn then
  1195.         draw.writeText(gsX, gsY, draw.black, draw.lightBlue, "+--+")
  1196.         draw.writeText(gsX, gsY + 1, draw.black, draw.lightBlue, ":  :")
  1197.         draw.writeText(gsX, gsY + 2, draw.black, draw.lightBlue, "+--+")
  1198.     end
  1199.     if text ~= "ai" then
  1200.         draw.writeText(gsX + 1, gsY + 1, colour, draw.black, text)
  1201.     else
  1202.         draw.box(gsX + 1, gsY + 1, gsX + 2, gsY + 1, colour)
  1203.     end
  1204. end
  1205.  
  1206. function bpScreen(level, overlay)
  1207.     x, y = mon.getSize()
  1208.     if not drawn then
  1209.         draw.clear()
  1210.     end
  1211.     if bpBool then
  1212.         draw.centerText(1, x - buttonLen, 2, draw.black, draw.lime, "Level: "..layer.."/"..#bpTab..", "..item.." selected.")
  1213.     else
  1214.         if selTurtleName ~= nil then
  1215.             draw.centerText(1, x - buttonLen, 2, draw.black, draw.lime, selChunk.." "..selTurtleName.." selected.")
  1216.         else
  1217.             draw.centerText(1, x - buttonLen, 2, draw.black, draw.lime, selChunk.." selected.")
  1218.         end
  1219.     end
  1220.     for i = 1, #bpTab[level] do
  1221.         for j = 1, #bpTab[level][i] do
  1222.             if bpTab[level][i][j][5] ~= nil then
  1223.                 label = string.sub(bpTab[level][i][j][6], 1, 1)
  1224.                 if bpTab[level][i][j][5] == 0 then
  1225.                     label = label..">"
  1226.                 elseif bpTab[level][i][j][5] == 1 then
  1227.                     label = label.."v"
  1228.                 elseif bpTab[level][i][j][5] == 2 then
  1229.                     label = label.."<"
  1230.                 elseif bpTab[level][i][j][5] == 3 then
  1231.                     label = label.."^"
  1232.                 end
  1233.             else
  1234.                 if bpBool then
  1235.                     label = bpTab[level][i][j][6]
  1236.                 else
  1237.                     label = "  "
  1238.                 end
  1239.             end
  1240.             if overlay ~= nil then
  1241.                 if bpTab[level][i][j][1] ~= "air" then
  1242.                     gridSquare(bpTab[level][i][j][3], bpTab[level][i][j][4], bpTab[level][i][j][2], label)
  1243.                 else
  1244.                     gridSquare(bpTab[level - 1][i][j][3], bpTab[level - 1][i][j][4], bpTab[level - 1][i][j][2], label)
  1245.                 end
  1246.             else
  1247.                 gridSquare(bpTab[level][i][j][3], bpTab[level][i][j][4], bpTab[level][i][j][2], label)
  1248.             end
  1249.         end
  1250.     end
  1251.     drawButtons()
  1252.     if not drawn then
  1253.         drawn = true
  1254.     end
  1255. end
  1256.  
  1257. function drawCurrent()
  1258.     if baseBool then
  1259.         baseScreen()
  1260.     elseif itemBool then
  1261.         itemScreen()
  1262.     elseif turtleBool then
  1263.         turtleScreen()
  1264.     elseif bpBool or gridView then
  1265.         if overlayBool then
  1266.             --bpScreen(layer - 1)
  1267.             bpScreen(layer, true)
  1268.         else
  1269.             bpScreen(layer)
  1270.         end
  1271.         if gridView then
  1272.             if not turtleOverlay then
  1273.                 turtleGridUpdate()
  1274.             end
  1275.             drawTurtles()
  1276.         end
  1277.     end
  1278.     drawButtons()
  1279. end
  1280.  
  1281. function turtleGridUpdate()
  1282.     for i = 1, #locData do
  1283.         if locData[i]["name"] == selChunk then
  1284.             xStart = locData[i]["xPos"]
  1285.             zStart = locData[i]["zPos"]
  1286.             break
  1287.         end
  1288.     end
  1289.     for i = 1, #bpTab[1] do
  1290.         for j = 1, #bpTab[1][i] do
  1291.             bpTab[1][i][j]["xPos"] = xStart + (i - 1)
  1292.             bpTab[1][i][j]["zPos"] = zStart - (j - 1)
  1293.         end
  1294.     end
  1295.     turtleOverlay = true
  1296. end
  1297.  
  1298. function drawTurtles()
  1299.     --debugMsg("OVRLAY: Checking for turtles in "..selChunk..".")
  1300.     for i = 1, #locData do
  1301.         if locData[i]["name"] == selChunk then
  1302.             if locData[i]["turtle"] ~= "None" then
  1303.                 --debugMsg("OVRLAY: Turtles suspected in chunk.")
  1304.                 for j = 1, #tData do
  1305.                     if tData[j]["xPos"] >= locData[i]["xPos"] and tData[j]["xPos"] <= locData[i]["xPos"] + 15 then
  1306.                         if tData[j]["zPos"] <= locData[i]["zPos"] and tData[j]["zPos"] >= locData[i]["zPos"] - 15 then
  1307.                             --debugMsg("OVRLAY: "..tData[j]["name"].." found in chunk.")
  1308.                             for k = 1, #bpTab[1] do
  1309.                                 for l = 1, #bpTab[1][k] do
  1310.                                     if tData[j]["xPos"] == bpTab[1][k][l]["xPos"] then
  1311.                                         if tData[j]["zPos"] == bpTab[1][k][l]["zPos"] then
  1312.                                             if tData[j]["type"] == "Unassigned" then
  1313.                                                 dCol = draw.purple
  1314.                                             elseif tData[j]["type"] == "miner" then
  1315.                                                 dCol = draw.lightGrey
  1316.                                             elseif tData[j]["type"] == "sorter" then
  1317.                                                 dCol = draw.orange
  1318.                                             elseif tData[j]["type"] == "sorter" then
  1319.                                                 dCol = draw.blue
  1320.                                             end
  1321.                                             if tData[j]["selected"] then
  1322.                                                 dCol = draw.lime
  1323.                                             end
  1324.                                             if tData[j]["hPos"] == 0 then
  1325.                                                 gridSquare(bpTab[1][k][l][3], bpTab[1][k][l][4], dCol, j..">")
  1326.                                             elseif tData[j]["hPos"] == 1 then
  1327.                                                 gridSquare(bpTab[1][k][l][3], bpTab[1][k][l][4], dCol, j.."v")
  1328.                                             elseif tData[j]["hPos"] == 2 then
  1329.                                                 gridSquare(bpTab[1][k][l][3], bpTab[1][k][l][4], dCol, j.."<")
  1330.                                             elseif tData[j]["hPos"] == 3 then
  1331.                                                 gridSquare(bpTab[1][k][l][3], bpTab[1][k][l][4], dCol, j.."^")
  1332.                                             end
  1333.                                         end
  1334.                                     end
  1335.                                 end
  1336.                             end
  1337.                         end
  1338.                     end
  1339.                 end
  1340.             end
  1341.         end
  1342.     end
  1343. end
  1344.  
  1345. --RUN FUNCTIONS--
  1346.  
  1347. function changeScreen(screenName)
  1348.     drawn = false
  1349.     baseBool = false
  1350.     itemBool = false
  1351.     turtleBool = false
  1352.     bpBool = false
  1353.     gridView = false
  1354.     overlayBool = false
  1355.     turtleOverlay = false
  1356.     if screenName == "Base" then
  1357.         baseScreen()
  1358.         baseBool = true
  1359.         mainMenu()
  1360.     elseif screenName == "Items" then
  1361.         itemScreen()
  1362.         itemBool = true
  1363.         mainMenu()
  1364.     elseif screenName == "Turtles" then
  1365.         turtleScreen()
  1366.         turtleBool = true
  1367.         mainMenu()
  1368.     elseif screenName == "BP" then
  1369.         loadBlueprint(selChunk)
  1370.         turtleGridUpdate()
  1371.         layer = 1
  1372.         item = "air"
  1373.         lab = "ai"
  1374.         itemColour = draw.blue
  1375.         BPButtons()
  1376.         draw.clear()
  1377.         bpScreen(layer)
  1378.         bpBool = true
  1379.         return
  1380.     elseif screenName == "Zoom" then
  1381.         debugMsg("RUNSYS: "..selChunk)
  1382.         loadBlueprint(selChunk)
  1383.         zoomMenu()
  1384.         gridView = true
  1385.         overlayBool = true
  1386.         layer = 2
  1387.         drawCurrent()
  1388.     end
  1389. end
  1390.  
  1391. function listener(tId, msg) --Handles all rednet messages--
  1392.     if string.sub(msg, 1, 9) == "itemTally" then
  1393.         shell.run(msg)
  1394.     elseif msg == "tick" then
  1395.         tickCount = tickCount + 1
  1396.         if tickCount >= 10 then
  1397.             tickCount = 0
  1398.             if update then
  1399.                 pushUpdates()
  1400.                 update = false
  1401.             end
  1402.             checkJobs("miner")
  1403.             checkJobs("sorter")
  1404.             checkJobs("utility")
  1405.         end
  1406.     else
  1407.         data = fs.open("Tables/tSheet", "w")
  1408.         data.write(msg)
  1409.         data.close()
  1410.         checkTurtles(tId)
  1411.         updateBaseScreen()
  1412.         if turtleBool or baseBool or gridView then
  1413.             drawCurrent()
  1414.         end
  1415.     end
  1416. end
  1417.  
  1418. function clearTurtleSelect()
  1419.     selTurtle = nil
  1420.     selTurtleName = nil
  1421.     for j = 1, #tData do
  1422.         tData[j]["selected"] = false
  1423.         if tData[j]["type"] == "miner" then
  1424.             tData[j]["colour"] = draw.lightGrey
  1425.         elseif tData[j]["type"] == "sorter" then
  1426.             tData[j]["colour"] = draw.orange
  1427.         elseif tData[j]["type"] == "utility" then
  1428.             tData[j]["colour"] = draw.blue
  1429.         else
  1430.             tData[j]["colour"] = draw.purple
  1431.         end
  1432.     end
  1433.     saveTurtles()
  1434. end
  1435.  
  1436. function sensor(xClick, yClick) --Handles all monitor touches
  1437.     if xClick > x - buttonLen then
  1438.         for i = 1, #scrTab do --ALL SIDE BUTTON CHECKING DONE HERE
  1439.             if scrTab[i] ~= nil then
  1440.                 if xClick >= tonumber(scrTab[i]["scrX"]) and xClick <= x - 2 then
  1441.                     if yClick == scrTab[i]["scrY"] + 1 then
  1442.                         if scrTab[i]["text"] == "Base" then
  1443.                             changeScreen(scrTab[i]["text"])
  1444.                             break
  1445.                         elseif scrTab[i]["text"] == "Items" then
  1446.                             changeScreen(scrTab[i]["text"])
  1447.                             break
  1448.                         elseif scrTab[i]["text"] == "Turtles" then
  1449.                             changeScreen(scrTab[i]["text"])
  1450.                             break
  1451.                         elseif scrTab[i]["text"] == "Set Miner" then
  1452.                             setType(selTurtle, "miner")
  1453.                             mainMenu()
  1454.                             break
  1455.                         elseif scrTab[i]["text"] == "Set Sorter" then
  1456.                             setType(selTurtle, "sorter")
  1457.                             mainMenu()
  1458.                             break
  1459.                         elseif scrTab[i]["text"] == "Set Utility" then
  1460.                             setType(selTurtle, "utility")
  1461.                             mainMenu()
  1462.                             break
  1463.                         elseif scrTab[i]["text"] == "Recall" then
  1464.                             rednet.send(selTurtle, "recall")
  1465.                         elseif scrTab[i]["text"] == "Mine "..selChunk then
  1466.                             addJob(selChunk, scrTab[i]["order"], "miner")
  1467.                             updateChunkState(selChunk, scrTab[i]["opt2"])
  1468.                             mainMenu()
  1469.                             break
  1470.                         elseif scrTab[i]["text"] == "Clear "..selChunk then
  1471.                             addJob(selChunk, scrTab[i]["order"], "miner")
  1472.                             updateChunkState(selChunk, scrTab[i]["opt2"])
  1473.                             mainMenu()
  1474.                             break
  1475.                         elseif scrTab[i]["text"] == "Chop "..selChunk then
  1476.                             addJob(selChunk, scrTab[i]["order"], "miner")
  1477.                             updateChunkState(selChunk, scrTab[i]["opt2"])
  1478.                             mainMenu()
  1479.                             break
  1480.                         elseif scrTab[i]["text"] == "Plant "..selChunk then
  1481.                             addJob(selChunk, scrTab[i]["order"], "miner")
  1482.                             updateChunkState(selChunk, scrTab[i]["opt2"])
  1483.                             mainMenu()
  1484.                             break
  1485.                         elseif scrTab[i]["text"] == "Build "..selChunk then
  1486.                             addJob(selChunk, scrTab[i]["order"], "miner")
  1487.                             updateChunkState(selChunk, scrTab[i]["opt2"])
  1488.                             mainMenu()
  1489.                             break
  1490.                         elseif scrTab[i]["text"] == "Zoom" then
  1491.                             changeScreen("Zoom")
  1492.                             break
  1493.                         elseif scrTab[i]["text"] == "Load Preset" then
  1494.                             presetMenu()
  1495.                             break
  1496.                         elseif scrTab[i]["order"] == "Preset" then
  1497.                             loadPreset(scrTab[i]["text"])
  1498.                             layer = 1
  1499.                             bpScreen(layer)
  1500.                             break
  1501.                         elseif scrTab[i]["text"] == "Edit BP "..selChunk then --Start of Blueprint functions
  1502.                             changeScreen("BP")
  1503.                             break
  1504.                         elseif scrTab[i]["text"] == "Menu" then
  1505.                             BPMenu()
  1506.                             break
  1507.                         elseif scrTab[i]["text"] == "Exit" then
  1508.                             changeScreen("Base")
  1509.                             break
  1510.                         elseif scrTab[i]["text"] == "Save" then
  1511.                             makeChestTable()
  1512.                             saveBlueprint(selChunk)
  1513.                             signalUpdate()
  1514.                             break
  1515.                         elseif scrTab[i]["text"] == "Load" then
  1516.                             loadLastBP()
  1517.                             break
  1518.                         elseif scrTab[i]["text"] == "Back" then
  1519.                             BPButtons()
  1520.                             break
  1521.                         elseif scrTab[i]["text"] == "Up Level" then
  1522.                             layer = layer + 1
  1523.                             if bpTab[layer] == nil then
  1524.                                 newBPLayer(layer, 16, 16)
  1525.                             end
  1526.                             drawCurrent()
  1527.                             break
  1528.                         elseif scrTab[i]["text"] == "Down Level" then
  1529.                             if layer > 1 then
  1530.                                 overlayBool = false
  1531.                                 layer = layer - 1
  1532.                             end
  1533.                             drawCurrent()
  1534.                             BPMenu()
  1535.                             break
  1536.                         elseif scrTab[i]["text"] == "Overlay" then
  1537.                             if layer == 1 then
  1538.                                 overlayBool = false
  1539.                             else
  1540.                                 overlayBool = not overlayBool
  1541.                             end
  1542.                             drawCurrent()
  1543.                             BPMenu()
  1544.                             break
  1545.                         elseif scrTab[i]["text"] == "Facing" then
  1546.                             facing = not facing
  1547.                             BPMenu()
  1548.                             break
  1549.                         elseif scrTab[i]["text"] == "Plants" then
  1550.                             plantMenu()
  1551.                             break
  1552.                         elseif scrTab[i]["text"] == "Wood" then
  1553.                             woodMenu()
  1554.                             break
  1555.                         elseif scrTab[i]["text"] == "Stone" then
  1556.                             stoneMenu()
  1557.                             break
  1558.                         elseif scrTab[i]["text"] == "Fancy" then
  1559.                             fancyMenu()
  1560.                             break
  1561.                         elseif scrTab[i]["text"] == "Utility" then
  1562.                             utilityMenu()
  1563.                             break
  1564.                         elseif scrTab[i]["text"] == "Access" then
  1565.                             accessMenu()
  1566.                             break
  1567.                         elseif scrTab[i]["text"] == "Mods" then
  1568.                             modsMenu()
  1569.                             break
  1570.                         elseif scrTab[i]["text"] == "IC2" then
  1571.                             IC2Menu()
  1572.                             break
  1573.                         else
  1574.                             debugMsg("SENSOR: "..scrTab[i]["order"])
  1575.                             if bpBool then
  1576.                                 drawn = false
  1577.                                 debugMsg("SENSOR: "..scrTab[i]["order"].." "..scrTab[i]["opt1"].." "..scrTab[i]["opt2"])
  1578.                                 item = scrTab[i]["order"]
  1579.                                 lab = scrTab[i]["opt1"]
  1580.                                 itemColour = tonumber(scrTab[i]["opt2"])
  1581.                                 drawCurrent()
  1582.                                 break
  1583.                             end
  1584.                         end
  1585.                     end
  1586.                 end
  1587.             end
  1588.         end
  1589.     else
  1590.         if baseBool then
  1591.             for i = 1, #locData do
  1592.                 if xClick >= locData[i]["scrX"] + 1 and xClick <= locData[i]["scrX"] + (chunkTileX - 1) then
  1593.                     if yClick >= locData[i]["scrY"] + 1 and yClick <= locData[i]["scrY"] + (chunkTileY - 1) then
  1594.                         debugMsg("SENSOR: "..locData[i]["name"].." Selected.")
  1595.                         for j = 1, #locData do
  1596.                             locData[j]["selected"] = false
  1597.                         end
  1598.                         locData[i]["selected"] = true
  1599.                         selChunk = locData[i]["name"]
  1600.                         miningMenu(locData[i]["name"])
  1601.                         saveLocs()
  1602.                         drawCurrent()
  1603.                         break
  1604.                     end
  1605.                 end
  1606.             end
  1607.         end
  1608.         if itemBool then
  1609.            
  1610.         end
  1611.         if turtleBool then
  1612.             turtleFound = false
  1613.             for i = 1, #tData do
  1614.                 if xClick >= tData[i]["scrX"] + 1 and xClick <= tData[i]["scrX"] + (turtleTileX - 1) then
  1615.                     if yClick >= tData[i]["scrY"] + 1 and yClick <= tData[i]["scrY"] + (turtleTileY - 1) then
  1616.                         debugMsg("SYSRUN: "..tData[i]["name"].." selected.")
  1617.                         clearTurtleSelect()
  1618.                         tData[i]["selected"] = true
  1619.                         selTurtle = tData[i]["identifier"]
  1620.                         selTurtleName = tData[i]["name"]
  1621.                         tData[i]["colour"] = draw.lime
  1622.                         turtleMenu()
  1623.                         turtleFound = true
  1624.                         saveTurtles()
  1625.                         break
  1626.                     end
  1627.                 end
  1628.             end
  1629.             if not turtleFound then
  1630.                 clearTurtleSelect()
  1631.                 mainMenu()
  1632.             end
  1633.             drawCurrent()
  1634.         end
  1635.         if bpBool then
  1636.             for i = 1, #bpTab[layer] do
  1637.                 for j = 1, #bpTab[layer] do
  1638.                     if xClick >= bpTab[layer][i][j][3] and xClick <= bpTab[layer][i][j][3] + 2 then
  1639.                         if yClick == bpTab[layer][i][j][4] + 1 then
  1640.                             bpTab[layer][i][j][1] = item
  1641.                             bpTab[layer][i][j][2] = itemColour
  1642.                             bpTab[layer][i][j][6] = lab
  1643.                             if facing then
  1644.                                 if bpTab[layer][i][j][5] == nil or bpTab[layer][i][j][5] == 3 then
  1645.                                     bpTab[layer][i][j][5] = 0
  1646.                                 elseif bpTab[layer][i][j][5] == 1 then
  1647.                                     bpTab[layer][i][j][5] = 2
  1648.                                 elseif bpTab[layer][i][j][5] == 2 then
  1649.                                     bpTab[layer][i][j][5] = 3
  1650.                                 elseif bpTab[layer][i][j][5] == 0 then
  1651.                                     bpTab[layer][i][j][5] = 1
  1652.                                 end
  1653.                             else
  1654.                                 bpTab[layer][i][j][5] = nil
  1655.                             end
  1656.                         end
  1657.                     end
  1658.                 end
  1659.             end
  1660.         end
  1661.         if gridView then
  1662.             for i = 1, #bpTab[layer] do
  1663.                 for j = 1, #bpTab[layer] do
  1664.                     if xClick >= bpTab[layer][i][j][3] and xClick <= bpTab[layer][i][j][3] + 2 then
  1665.                         if yClick == bpTab[layer][i][j][4] + 1 then
  1666.                             debugMsg("SYSRUN: i: "..i..", j: "..j)
  1667.                             debugMsg("SYSRUN: x: "..bpTab[1][i][j]["xPos"]..", z: "..bpTab[1][i][j]["zPos"])
  1668.                         end
  1669.                     end
  1670.                 end
  1671.             end
  1672.         end
  1673.     end
  1674. end
  1675.  
  1676. function run()
  1677.     event, p1, p2, p3, p4, p5 = os.pullEvent()
  1678.     if event == "rednet_message" then
  1679.         listener(p1, p2)
  1680.     elseif event == "monitor_touch" then
  1681.         --debugMsg("SENSOR: "..p2..", "..p3)
  1682.         sensor(p2, p3)
  1683.         drawCurrent()
  1684.     elseif event == "key" then
  1685.         running = false
  1686.     end
  1687. end
  1688.  
  1689. --PROGRAM START--
  1690.  
  1691. draw.clear()
  1692. getPeripherals()
  1693. switchMon()
  1694. loadLocs()
  1695. loadChestData()
  1696. loadTurtles()
  1697. loadTurtleChests()
  1698. mainMenu()
  1699. drawCurrent()
  1700. running = true
  1701. while running do
  1702.     run()
  1703. end
  1704. switchMon()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement