Guest User

Central Control Computer - Global Builders Project

a guest
Sep 20th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 25.36 KB | None | 0 0
  1. --[[ Central Construction Control Computer
  2.  
  3.  TO DO:
  4.  
  5. -- change cID to distinguish between ID and channel
  6. -- move "action" menu selections out of setMenuList
  7. - add 'resend last' routines for repeating
  8.  a coordinate block that was assigned but
  9.  not completed
  10. - move project management routines to
  11.   central computer (off turtle)
  12.    
  13.     blockRegistry contains information on all
  14.     computers and turtles communicating with
  15.     the central PC.  Codes for first value in
  16.     block registry as follows:
  17.     1 - central PC (this PC)
  18.     2 - satellite control PC
  19.     3 - gps relay PC
  20.     4 - unassigned
  21.     5 - building turtle
  22.     6 - supply turtle
  23.     7 - survey turtle
  24.     8+ - undefined
  25.  
  26.     Communication packets use format:
  27.  
  28.     msgType, senderBlockType, senderLabel,
  29.     senderID, senderChannel, msgID
  30.    
  31.     (table indexes 7+ are for message specific information)
  32.    
  33.     msgTypes as follows:
  34.    
  35.     0 - satellite registration request
  36.     1 - request device status
  37.     2 - relocate turtle
  38.     3 - assign build task to turtle
  39.     4 - assign resupply task to turtle
  40.     5 - assign survey task to turtle
  41.     6+ - undefined
  42.    
  43. --]]
  44.  
  45. local maxMenuItems = 3
  46. local modemChan = 1
  47.  
  48. local masterCoordTable = {}
  49. local blockIndex = {}
  50. local blockRegistry = {}
  51. local monitorOutput, termOutput, menuList = {}, {}, {}
  52. local gctFileList = {}
  53. local rawEventTab = {}
  54. local m, v, d = nil, nil, nil
  55. -- handles for modem, monitor, and disk drive
  56.  
  57. local mPointer, mPtIndex, mPointerStat, listOffset = 1, 1, 1, 0
  58. -- variable for list location of menu pointer,
  59. -- index of current menu, and status of pointer
  60.  
  61. local cID, cLabel = os.getComputerID (), os.getComputerLabel ()
  62.  
  63.  
  64. function outMon (monMsg)
  65.     local vX, vY = 0, 0
  66.     if not (monMsg == nil) then
  67.         if not (v == nil) then
  68.             vX, vY = v.getSize ()
  69.             vY = vY - 16
  70.             if not (type (monMsg) == string) then
  71.                 monMsg = tostring (monMsg)
  72.             end
  73.             local newStr = monMsg..(string.rep (" ", ((vX - 33) - #monMsg)))
  74.             table.insert (monitorOutput, newStr)
  75.        
  76.             if #monitorOutput > vY then
  77.                 table.remove (monitorOutput, 1)
  78.             end
  79.         end
  80.        
  81.         vX, vY = term.getSize ()
  82.         local newStr = monMsg..(string.rep((" "), (vX - #monMsg)))
  83.         table.insert (termOutput, newStr)
  84.        
  85.         if #termOutput > vY then
  86.             table.remove(termOutput, 1)
  87.         end
  88.     end
  89.     if not (monitorOutput == {}) then
  90.         for i = 1, #monitorOutput do
  91.             v.setCursorPos (32, 15 + i)
  92.             v.write (monitorOutput[i])
  93.         end
  94.     end
  95.     local mOptions = #menuList.options
  96.     term.clear()
  97.     if not (menuList == {}) then
  98.         term.setCursorPos (1, 1)
  99.         write (menuList.header)
  100.         term.setCursorPos (1, 2)
  101.         term.clearLine ()
  102.         if mOptions > maxMenuItems then
  103.             mOptions = maxMenuItems
  104.         end
  105.         for i = 1, mOptions do
  106.             term.setCursorPos (1, i + 2)
  107.             write ('  '..menuList.options[i + listOffset][1])
  108.         end
  109.         term.setCursorPos (1, mOptions + 3)
  110.         term.clearLine ()
  111.         term.setCursorPos (1, mOptions + 4)
  112.         term.write (menuList.footer..tostring(mOptions))
  113.     end
  114.     vX, vY = term.getSize ()
  115.     local ttY = mOptions + 5
  116.  
  117.     if not (termOutput == {}) then
  118.         for i = ttY, vY, 1 do
  119.             term.setCursorPos (1, i + 1)
  120.             if not (termOutput[i + 1] == nil) then
  121.                 write (termOutput[i])
  122.             else
  123.                 term.clearLine ()
  124.             end
  125.         end
  126.     end
  127. end
  128.  
  129.  
  130. function getGCTFileList ()
  131.    
  132.     gctFileList = {}
  133.     local tFileTab = fs.list("")
  134.     for i = 1, #tFileTab do
  135.         if not (string.find(tFileTab[i], ".gct") == nil) then
  136.             table.insert(gctFileList, { tFileTab[i], 0 } )
  137.         end
  138.     end
  139. end
  140.  
  141.  
  142. function initCentralPC ()
  143.  
  144.     local cStr, nStr = "", ""
  145.     nStr = 'Central_PC_'..tostring(cID)
  146.     if (cLabel == nil) or (cLabel ~= nStr) then
  147.         os.setComputerLabel (nStr)
  148.         cLabel = nStr
  149.     end
  150.    
  151.     local periLoc = {"top", "bottom", "left",
  152.         "right", "front", "back"}
  153.     local periStr = ""    
  154.        
  155.     print ('Device ID: '..tostring(cID))
  156.     print ('Adjacent peripherals:')
  157.     for i = 1, 6, 1 do
  158.         periStr = peripheral.getType(periLoc[i])
  159.         if periStr == "modem" then
  160.             m = peripheral.wrap(periLoc[i])
  161.             m.open (modemChan)
  162.         elseif periStr == "monitor" then
  163.             v = peripheral.wrap(periLoc[i])
  164.         elseif periStr == "drive" then
  165.             d = peripheral.wrap(periLoc[i])
  166.         end    
  167.         if periStr == nil then
  168.             print (periLoc[i]..':  none')
  169.         else print (periLoc[i]..':  '..periStr)    
  170.         end
  171.     end  
  172.     if m == nil then
  173.         print ('WARNING!  No modem detected!')
  174.         print ('Please install a Wireless Modem')
  175.         print ('on the computer and run this')
  176.         print ('startup script again.')
  177.         return false            
  178.     else
  179.         for i = 1, 7, 1 do
  180.             blockRegistry[i] = {}
  181.         end
  182. -- first block registered is always central PC
  183. -- registry stores by [block type][index][label, ID,
  184. --     modem channel, comm log table]
  185.         blockRegistry[1][1] = {cLabel, cID, 0, {}}
  186.         return true
  187.     end
  188. end
  189.    
  190.  
  191. function getCoordTable (fileName)
  192.  
  193.     local coordFile = fs.open (fileName, "r")
  194.     local coordStr = ""
  195.    
  196.     local nextLine = coordFile.readLine()
  197.     while not (nextLine == nil) do
  198.         coordStr = coordStr..nextLine
  199.         nextLine = coordFile.readLine ()
  200.     end    
  201.     coordFile.close ()
  202.  
  203.     local cTable = textutils.unserialize (coordStr)
  204.     local ctI, ctG, ctC = 0, 0, 0
  205.     local tx, ty, tz = 0, 0, 0
  206.    
  207.     for ctI = 1, #cTable, 1 do
  208.         ty = ty + cTable[ctI][1]
  209.         for ctG = 1, #cTable[ctI][2], 1 do
  210.             if cTable[ctI][2][ctG][1] > tx
  211.                 then tx = cTable[ctI][2][ctG][1]
  212.             end
  213.             if cTable[ctI][2][ctG][2] > tz
  214.                 then tz = cTable[ctI][2][ctG][2]
  215.             end
  216.         end                    
  217.     end        
  218.     outMon ('Coordinate table parsed.')
  219.     outMon ('Structure dimensions: y='..tostring(ty)..' x='..tostring(tx)..' z='..tostring(tz))
  220.     local bX = math.ceil ((tx / 16))
  221.     local bZ = math.ceil ((tz / 16))
  222.     local bY = math.ceil ((ty / 16))
  223.     outMon ('Coordinate block dimensions: y='..tostring(bY)..' x='..tostring(bX)..' z='..tostring(bZ))        
  224.    
  225.     for i = 1, (bY * 16), 1 do
  226.         masterCoordTable[i] = {}
  227.         for ii = 1, (bX * 16), 1 do
  228.             masterCoordTable[i][ii] = {}
  229.             for iii = 1, (bZ * 16), 1 do            
  230.                 masterCoordTable[i][ii][iii] = {0, 0}
  231.             end
  232.         end
  233.     end        
  234.  
  235.     for i = 1, bY, 1 do
  236.         blockIndex[i] = {}
  237.         for ii = 1, bX, 1 do
  238.             blockIndex[i][ii] = {}
  239.             for iii = 1, bZ, 1 do
  240.                 blockIndex[i][ii][iii] = {0, 0, 0}
  241.             end
  242.         end
  243.     end    
  244.    
  245.     local aY, aX, aZ, aM, aD = 0, 0, 0, 0, 0
  246.    
  247.     for ctI = 1, #cTable, 1 do
  248.         for i = 1, cTable[ctI][1], 1 do
  249.             aY = aY + 1
  250.             for ctG = 1, #cTable[ctI][2], 1 do
  251.                 aX = cTable[ctI][2][ctG][1]
  252.                 aZ = cTable[ctI][2][ctG][2]
  253.                 aM = cTable[ctI][2][ctG][3]
  254.                 aD = cTable[ctI][2][ctG][4]
  255.                 masterCoordTable[aY][aX][aZ] = {aM, aD}
  256.             end
  257.         end
  258.     end  
  259. end                                
  260.  
  261.  
  262. function prepStrBlock (bY, bX, bZ)
  263.    
  264.     local aY, aX, aZ = ((bY - 1) * 16), ((bX - 1) * 16),
  265.                        ((bZ - 1) * 16)
  266.  
  267.     print ('prepStrBlock absolute: ', aY, '; ', aX, '; ', aZ)
  268.                    
  269.     local blockCoordTable = {}  
  270.          
  271.     for i = 1, 16, 1 do
  272.         blockCoordTable[i] = {}
  273.         for ii = 1, 16, 1 do
  274.             blockCoordTable[i][ii] = {}
  275.             for iii = 1, 16, 1 do
  276.                 blockCoordTable[i][ii][iii] =
  277.                     (masterCoordTable[aY + i][aX + ii][aZ + iii])
  278.             end
  279.         end
  280.     end              
  281.  
  282.     return (blockCoordTable)  
  283. end
  284.  
  285.  
  286. function assignBlock (turtleID)
  287.  
  288.     if not (blockIndex[1] == nil) then
  289.         for i = 1, #blockIndex[1][1], 1 do
  290.             for ii = 1, #blockIndex[1], 1 do
  291.                 for iii = 1, #blockIndex, 1 do
  292.                     if (blockIndex[iii][i][ii][1] == 0) and
  293.                         (not (blockIndex[iii][i][ii][2] == 1)) then
  294.                         blockIndex[iii][i][ii] = {0, 1, turtleID}
  295.                         print ('assignBlock returning: ', iii, '; ', i, '; ', ii)
  296.                         return iii, i, ii
  297.                     end
  298.                 end
  299.             end
  300.         end
  301.     end
  302.     return 0, 0, 0
  303. end
  304.  
  305.  
  306. function reqMessageID ()
  307.  
  308.     local IDStr = tostring( os.day() )..'-'..tostring( os.time () )
  309.     return IDStr
  310. end
  311.  
  312.  
  313. function logComm (tPacket)
  314.  
  315.     local isRegd, isDupe = false, false
  316.     local sType = tPacket[2]
  317.     local numBlocks = #blockRegistry[sType]
  318.     for i = 1, numBlocks, 1 do
  319.         if blockRegistry[sType][i][1] ==
  320.             tPacket[3] then
  321.             isRegd = true
  322.             local numEntries = #blockRegistry[sType][i][4]
  323.             for ii = 1, numEntries, 1 do
  324.                 if tPacket[6] == blockRegistry[sType][i][4][ii] then
  325.                     isDupe = true
  326.                 end
  327.             end
  328.             if not (isDupe) then
  329.                 if numEntries > 4 then
  330.                     table.remove (blockRegistry[sType][i][4], 1)
  331.                 end
  332.                 table.insert (blockRegistry[sType][i][4], tPacket[6])
  333.             end
  334.         end
  335.     end
  336.     if not (isRegd) then
  337.         return 1
  338.     elseif isDupe then
  339.         return 2
  340.     else return 0
  341.     end
  342. end
  343.  
  344. function listen (inMsgTab)
  345.  
  346.     local wy, wx, wz = 0, 0, 0
  347.     local outMsgTab = {}
  348.     local retNum = logComm (inMsgTab)
  349.     local msgCode = inMsgTab[1]
  350.     if (retNum == 2) then
  351.         outMon ('Rejected message: duplicate ID.')
  352.     elseif (msgCode == 0) and (retNum ~= 1) then
  353.         outMon ('Registration request rejected:')
  354.         outMon ('Duplicate label specified.')
  355.         outMon (inMsgTab[3])
  356.     elseif (msgCode == 0) and (retNum == 1) then
  357.         outMon ('Registration requested:')
  358.         outMon (inMsgTab[3]..' ('..tostring(inMsgTab[4])..') on channel '..tostring(inMsgTab[5])..' | '..tostring(inMsgTab[6]))
  359.         local blockType = inMsgTab[2]
  360.         local tRgDat = {inMsgTab[3], inMsgTab[4], inMsgTab[5], { inMsgTab[6] } }
  361.         table.insert (blockRegistry[blockType], tRgDat)
  362.         outMon ('Successfully registered.')
  363.         -- TO DO
  364.         -- add acknowledged response
  365.         return 0, 0
  366.     elseif msgCode == 1 then
  367.         outMon ('Builder: new block request.')
  368.         wy, wx, wz = assignBlock (inMsgTab[4])
  369.         outMon ('Assigning block at: '..wy..'; '..wx..'; '..wz)
  370.         if not (wy == 0) and not (wx == 0) and not (wz == 0) then
  371.             outMsgTab = {3, 1, cLabel, cID, cID, reqMessageID (),
  372.                 prepStrBlock (wy, wx, wz)}    
  373.             outMon (outMsgTab[6])
  374.             outMon (inMsgTab[5])
  375.             m.transmit (inMsgTab[5], cID, textutils.serialize(outMsgTab))
  376.         else
  377.             outMon ('No unassigned blocks available.')
  378.             outMsgTab = {3, 1, cLabel, cID, cID, reqMessageID (), nil}
  379.             m.transmit (inMsgTab[5], cID, textutils.serialize (outMsgTab))
  380.         end    
  381.         return 1, inMsgTab[4]
  382.     elseif msgCode == 2 then
  383.         outMon ('Builder: block complete.')
  384.         for i = 1, #blockIndex, 1 do
  385.             for ii = 1, #blockIndex[i], 1 do
  386.                 for iii = 1, #blockIndex[ii], 1 do
  387.                     if (blockIndex[i][ii][iii][1] == 0)
  388.                         and (blockIndex[i][ii][iii][2] == 1)
  389.                         and (blockIndex[i][ii][iii][3] == inMsgTab[4]) then
  390.                             blockIndex[i][ii][iii][1] = 1
  391.                     end
  392.                 end
  393.             end
  394.         end
  395.         return 2, inMsgTab[4]  
  396.     end
  397. end
  398.  
  399.  
  400. function sendReloc (sCh, sMsg)
  401.  
  402.     m.transmit (sCh, modemChan, sMsg)
  403. end
  404.  
  405.  
  406. function setMenuList (mIndex)
  407. -- Primary menus are indexed by integer values < 100
  408. -- Secondary menus, and especially menus containing dynamic lists,
  409. -- are indexed by integer values > 100, where the truncated number
  410. -- derived from dividing the index number by 100 provides the
  411. -- 'context', and the remainder integer value provides the list index.
  412. -- ie An index value of 503 would be referencing a builder turtle (menu
  413. -- index 5) third in the list.
  414. -- Index values over 1000 are the last tier, indicating a
  415. -- specific task for a specific device of a specific type.
  416.  
  417.     local tBlockDat = {}
  418.     local oMsg = {}
  419.  
  420.     if (mIndex == nil) then
  421.         mIndex = 1
  422.     end
  423.    
  424.     if mIndex <= 99 then
  425.         if mIndex == 1 then
  426.             menuList = {header = "Main Menu:",
  427.             options = { {"Load .gct file", 2}, {"Turtle menu", 3}, {'Help Index', 9} },
  428.             previous = 0,
  429.             footer = "Up/Down or W/S to Select, Enter/D to Confirm"}
  430.         elseif mIndex == 2 then
  431.             getGCTFileList ()
  432.             local tGCTList = {}
  433.             if gctFileList == {} then
  434.                 tGCTList = {'no .GCT files found', 2}
  435.             else
  436.                 for i = 1, #gctFileList do
  437.                     table.insert (tGCTList, { gctFileList[i][1], ((mIndex * 100) + i) } )
  438.                 end
  439.             end
  440.             menuList = {header = "Available .GCT Files:",
  441.             options = tGCTList,
  442.             previous = 1,
  443.             footer = "Left/A for previous menu"}
  444.         elseif mIndex == 3 then
  445.             menuList = {header = "Turtle Menu",
  446.             options = { {"Select turtle", 4}, {"Emergency halt", 301} },
  447.             previous = 1,
  448.             footer = "Left/A for previous menu"}
  449.         elseif mIndex == 4 then
  450.             menuList = {header = "Turtle Selection Menu",
  451.             options = { {"Builders", 5}, {"Suppliers", 6}, {"Surveyors", 7} },
  452.             previous = 3,
  453.             footer = "Left/A for previous menu"}
  454.         elseif mIndex == 5 then
  455.             tBlockDat = {}
  456.             if (blockRegistry[5][1] == nil) or (blockRegistry[5][1] == {}) then
  457.                 tBlockDat = { {"none registered", 5} }
  458.             else
  459.                 for i = 1, #blockRegistry[5] do
  460.                     table.insert (tBlockDat, {blockRegistry[5][i][1], ((mIndex * 100) + i)})
  461.                 end
  462.             end
  463.             menuList = {header = "Registered Builder Turtles:",
  464.             options = tBlockDat,
  465.             previous = 4,
  466.             footer = "Left/A for previous menu"}
  467.         elseif mIndex == 6 then
  468.             tBlockDat = {}
  469.             if (blockRegistry[6][1] == nil) or (blockRegistry[6][1] == {}) then
  470.                 tBlockDat = { {"none registered", 6} }
  471.             else
  472.                 for i = 1, #blockRegistry[6] do
  473.                     table.insert (tBlockDat, {blockRegistry[6][i][1], ((mIndex * 100) + i)})
  474.                 end
  475.             end
  476.             menuList = {header = "Registered Supply Turtles:",
  477.             options = tBlockDat,
  478.             previous = 4,
  479.             footer = "Left/A for previous menu"}    
  480.         elseif mIndex == 7 then
  481.             tBlockDat = {}
  482.             if (blockRegistry[7][1] == nil) or (blockRegistry[7][1] == {}) then
  483.                 tBlockDat = { {"none registered", 7} }
  484.             else
  485.                 for i = 1, #blockRegistry[7] do
  486.                     table.insert (tBlockDat, {blockRegistry[7][i][1], ((mIndex * 100) + i)})
  487.                 end
  488.             end
  489.             menuList = {header = "Registered Survey Turtles:",
  490.             options = tBlockDat,
  491.             previous = 4,
  492.             footer = "Left/A for previous menu"}
  493.         elseif mIndex == 9 then
  494.             menuList = {header = "Help Categories:",
  495.             options = { {"Central PC", 1}, {"Turtles", 2}, {"GPS Relay PCs", 3}, {".GCT Files", 4} },
  496.             previous = 1,
  497.             footer = "Left/A for previous menu"}
  498.         end
  499.         local mX, mY = term.getSize ()
  500.         menuList.header = menuList.header..string.rep (" ", mX - #menuList.header)
  501.         for i = 1, #menuList.options do
  502.             menuList.options[i][1] = menuList.options[i][1]..string.rep (" ", mX - #menuList.options[1])
  503.         end
  504.         menuList.footer = menuList.footer..string.rep (" ", mX - #menuList.footer)
  505.     elseif (mIndex >= 100) and (mIndex < 1000) then
  506.         local mContext = math.floor((mIndex / 100))
  507.         local dIndex = math.fmod (mIndex, 100)
  508.         local aIndex = mIndex * 10
  509.         outMon ('mIndex = '..tostring(mIndex)..'  mContext = '..tostring(mContext)..'  dIndex = '..tostring(dIndex))
  510.         if mContext == 5 then
  511.             menuList = {header = "Builder Turtle: "..tostring(blockRegistry[5][dIndex][1]),
  512.             options = { {"View status", (aIndex + 1)}, {"Relocate", (aIndex + 2)}, {"Assign to Build", (aIndex + 3)},
  513.             {"Force Resupply", (aIndex + 4)} },
  514.             footer = "Left/A for previous menu"}
  515.         elseif mContext == 6 then    
  516.             menuList = {header = "Supply Turtle: "..tostring(blockRegistry[6][dIndex][1]),
  517.             options = { {"View status", (aIndex + 1)}, {"Relocate", (aIndex + 2)}, {"Assign to Supply Post", (aIndex + 3)},
  518.             {"Force Resupply", (aIndex + 4)} },
  519.             footer = "Left/A for previous menu"}
  520.         elseif mContext == 7 then
  521.             menuList = {header = "Survey Turtle: "..tostring(blockRegistry[7][dIndex][1]),
  522.             options = { {"View status", (aIndex + 1)}, {"Relocate", (aIndex + 2)}, {"Assign to Survey Job", (aIndex + 3)},
  523.             {"Force Resupply", (aIndex + 4)} },
  524.             footer = "Left/A for previous menu"}
  525.         end
  526.     elseif (mIndex >= 1000) then
  527.         outMon ('Reached individual system command peak.')
  528.         local mContext = math.floor (mIndex / 1000)
  529.         local dIndex = math.fmod (mIndex, 1000)
  530.         local tIndex = math.fmod(dIndex, 10)
  531.         local dIndex = math.floor(dIndex / 10)
  532.         outMon ('Device category: '..tostring(mContext)..' | Device Index: '..tostring(dIndex))
  533.         outMon ('Task Index: '..tostring(tIndex))
  534.         if mContext == 5 then
  535.             if tIndex == 1 then
  536.                 outMon ('Request and display status from turtle')
  537.             elseif tIndex == 2 then
  538.                 local oCh = blockRegistry[5][dIndex][3]
  539.                 outMon ('Relocate builder turtle '..tostring(oCh))
  540.                 oMsg = {2, 1, cLabel, cID, modemChan, reqMessageID(), {100, 71, -205, 113}}
  541.                 local oStr = textutils.serialize (oMsg)
  542.                 sendReloc (oCh, oStr)
  543.                 outMon ('Should have sent...')
  544.                 --above transmit is for test purposes only
  545.             elseif tIndex == 3 then
  546.                 outMon ('Assign build job to turtle')
  547.             elseif tIndex == 4 then
  548.                 outMon ('Force resupply on builder')
  549.             end
  550.         elseif mContext == 6 then
  551.             if tIndex == 1 then
  552.                 outMon ('Request and display status from turtle')
  553.             elseif tIndex == 2 then
  554.                 outMon ('Relocate builder turtle')
  555.             elseif tIndex == 3 then
  556.                 outMon ('Assign build job to turtle')
  557.             elseif tIndex == 4 then
  558.                 outMon ('Force resupply on builder')
  559.             end
  560.         elseif mContext == 7 then
  561.             if tIndex == 1 then
  562.                 outMon ('Request and display status from turtle')
  563.             elseif tIndex == 2 then
  564.                 outMon ('Relocate builder turtle')
  565.             elseif tIndex == 3 then
  566.                 outMon ('Assign build job to turtle')
  567.             elseif tIndex == 4 then
  568.                 outMon ('Force resupply on builder')
  569.             end    
  570.         end
  571.     end    
  572. end
  573.  
  574.  
  575. function updateMonitor ()
  576.     local vX, vY = v.getSize ()
  577.     v.clear ()
  578.     v.setCursorPos (1, 1)
  579.     v.write ('   Registered Satellites')
  580.     v.setCursorPos (1, 3)
  581.     v.write (' GPS Relays:   | Builders:     | Surveyors:    | Suppliers:    | Sub Control:')
  582.     v.setCursorPos (4, 18)
  583.     v.write ('GCT Files:')
  584.     for i = 4, 14 do
  585.         v.setCursorPos (16, i)
  586.         v.write ('|')
  587.         v.setCursorPos (32, i)
  588.         v.write ('|')
  589.         v.setCursorPos (48, i)
  590.         v.write ('|')
  591.         v.setCursorPos (64, i)
  592.         v.write ('|')
  593.     end
  594.    
  595.     for i = 1, vX do
  596.         v.setCursorPos (i, 15)
  597.         v.write ('-')
  598.     end
  599.     for i = 16, vY do
  600.         v.setCursorPos (30, i)
  601.         v.write ('|')
  602.     end
  603.     for i = 1, #blockRegistry[3] do
  604.         v.setCursorPos (2, 4 + i)
  605.         v.write (blockRegistry[3][i][1])
  606.     end
  607.     for i = 1, #blockRegistry[5] do
  608.         v.setCursorPos (18, 4 + i)
  609.         v.write (blockRegistry[5][i][1])
  610.     end
  611.     for i = 1, #blockRegistry[7] do
  612.         v.setCursorPos (34, 4 + i)
  613.         v.write (blockRegistry[7][i][1])
  614.     end
  615.     for i = 1, #blockRegistry[6] do
  616.         v.setCursorPos (50, 4 + i)
  617.         v.write (blockRegistry[6][i][1])
  618.     end
  619.     for i = 1, #blockRegistry[2] do
  620.         v.setCursorPos (66, 4 + i)
  621.         v.write (blockRegistry[2][i][1])
  622.     end
  623.     for i = 1, #gctFileList do
  624.         v.setCursorPos (2, 19 + i)
  625.         v.write (gctFileList[i][1])
  626.     end
  627.     outMon()
  628. end
  629.  
  630.  
  631. function pointerSpin ()
  632.  
  633.     term.setCursorPos (1, 2 + mPointer)
  634.     if mPointerStat == 1 then
  635.         write ('--')
  636.         mPointerStat = 2
  637.     elseif mPointerStat == 2 then
  638.         write ('->')
  639.         mPointerStat = 1
  640.     end
  641.     os.startTimer(0.5)
  642. end
  643.  
  644.  
  645. function eventHandler ()
  646.    
  647.     local haltNow = false
  648.     local globalTable = {}
  649.     while not haltNow do
  650.         pointerSpin ()
  651.         rawEventTab = {}
  652.         rawEventTab = { os.pullEvent () }
  653.         --rawEventTab[1], rawEventTab[2], rawEventTab[3], rawEventTab[4],
  654.         --    rawEventTab[5], rawEventTab[6], rawEventTab[7], rawEventTab[8] = os.pullEvent ()
  655.         if rawEventTab[1] == "key" then
  656.             outMon ('Key event...')
  657.             if (rawEventTab[2] == 17) or (rawEventTab[2] == 200) then
  658.                 local oldPtr = mPointer
  659.                 mPointer = mPointer - 1
  660.                 if mPointer < 1 then
  661.                     mPointer = 1
  662.                     if listOffset > 0 then
  663.                         listOffset = listOffset - 1
  664.                     end
  665.                     if mPtIndex > 1 then
  666.                         mPtIndex = mPtIndex - 1
  667.                     end
  668.                 else
  669.                     mPtIndex = mPtIndex - 1
  670.                 end
  671.                 if mPtIndex < 1 then
  672.                     mPtIndex = 1
  673.                 end
  674.                 term.setCursorPos (1, 2 + oldPtr)
  675.                 term.write ('  ')
  676.                 outMon ('mPointer '..tostring(mPointer)..'  mPtIndex '..tostring(mPtIndex))
  677.             elseif (rawEventTab[2] == 31) or (rawEventTab[2] == 208) then
  678.                 local oldPtr = mPointer
  679.                 mPointer = mPointer + 1
  680.                 mPtIndex = mPtIndex + 1
  681.                 if mPtIndex > #menuList.options then
  682.                     mPtIndex = #menuList.options
  683.                     mPointer = mPointer - 1
  684.                 end
  685.                 if mPointer > maxMenuItems then
  686.                     mPointer = mPointer - 1
  687.                     if not (listOffset == #menuList.options) then
  688.                         listOffset = listOffset + 1
  689.                     end
  690.                 end
  691.                 term.setCursorPos (1, 2 + oldPtr)
  692.                 write ('  ')
  693.                 outMon ()
  694.             elseif (rawEventTab[2] == 28) or (rawEventTab[2] == 32)
  695.                 or (rawEventTab[2] == 205) then
  696.                 local oldIndex = mPtIndex
  697.                 mPointer = 1
  698.                 mPtIndex = 1
  699.                 listOffset = 0
  700.                 if menuList.options[oldIndex][2] == 0 then
  701.                     outMon ('0 index option selected')
  702.                 else
  703.                     setMenuList (menuList.options[oldIndex][2])
  704.                 end
  705.                 outMon ()
  706.             elseif (rawEventTab[2] == 14) or (rawEventTab[2] == 30)
  707.                 or (rawEventTab[2] == 203)then
  708.                 mPointer = 1
  709.                 mPtIndex = 1
  710.                 listOffset = 0
  711.                 setMenuList (menuList.previous)
  712.                 outMon ()
  713.             else
  714.                 outMon ('Unassigned key pressed.  '..tostring(rawEventTab[2]))
  715.             end
  716.         elseif rawEventTab[1] == "modem_message" then
  717.             outMon ("Modem event..."..tostring(#rawEventTab))
  718.            
  719.             listen (textutils.unserialize (rawEventTab[5]))
  720.         end
  721.         updateMonitor ()
  722.     end
  723. end
  724.  
  725.  
  726. term.clear ()
  727. setMenuList ()
  728. if not (initCentralPC ()) then
  729.     print ('Script halted.')
  730. else    
  731.     getGCTFileList ()
  732.     if gctFileList == {} then
  733.         outMon ('No .GCT files found.')
  734.     end
  735.     updateMonitor ()
  736.     eventHandler ()
  737. end
  738.  
  739. --[[
  740. getCoordTable ("gpstower.gct")
  741. while true do
  742.     local tCode, tID = listen()
  743.     updateMonitor ()
  744.     if tCode == 0 then
  745.         print ('Processed registration request.')
  746.     elseif tCode == 1 then
  747.         outMon ('Processed coordinate block request.')
  748.     elseif tCode == 2 then
  749.         outMon ('Received block complete notice.')
  750.     end
  751. end--]]
Advertisement
Add Comment
Please, Sign In to add comment