Advertisement
nagoL2015

mon.lua

Jul 26th, 2017
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.32 KB | None | 0 0
  1. modem1 = 6565
  2. modem2 = 6566
  3. modem3 = 6567
  4. modem4 = 6568
  5.  
  6. local gatherChannel = 52523 --This channel will be *heavily* spammed; so be cautious of which channel you use...
  7.  
  8.  
  9. local fuelLevelGreenMin = 50000
  10. local fuelLevelYellowMin = 15000
  11.  
  12.  
  13. local modems = { peripheral.find("modem") }
  14. if #modems == 0 then error("No modems found") end
  15.  
  16. local modem = modems[1]
  17.  
  18. local map = {}
  19.  
  20. local screen = "map"
  21. local viewTarget = ""
  22. local requestTarget = ""
  23.  
  24. local requestCount = 1
  25. local maxRequestCount = 100
  26.  
  27. local mapButtons = {}
  28.  
  29. local buttons = {}
  30.  
  31. local w,h = term.getSize()
  32.  
  33. local offsetX = 0 --Offsets used to automatically offset the picture; making it fit on the screen.
  34. local offsetZ = 0
  35.  
  36. local requestButtons = {}
  37.  
  38. local updatesEnabled = true
  39.  
  40. function updateVisuals()
  41.     if not updatesEnabled then return end
  42.     if screen == "map" then
  43.         local totalX = 0
  44.         local totalZ = 0
  45.         local count = 0
  46.         for i,v in pairs(map) do
  47.             if v.location ~= nil then
  48.                 totalX = totalX + v.location.x
  49.                 totalZ = totalZ + v.location.z
  50.                 count = count + 1
  51.             end
  52.         end
  53.         local midpointX = math.floor(totalX/count)
  54.         local midpointZ = math.floor(totalZ/count)
  55.        
  56.         local topLeftX = math.floor(midpointX - (w/2))
  57.         local topLeftZ = math.floor(midpointZ - (h/2))
  58.        
  59.         offsetX = 0
  60.         offsetY = 0
  61.        
  62.         for i,v in pairs(map) do
  63.             local x = v.location.x-topLeftX
  64.             local z = v.location.z-topLeftZ
  65.             if x < 1 then
  66.                 offsetX = offsetX + math.abs(x) + 1
  67.             end
  68.             if z < 1 then
  69.                 offsetY = offsetY + math.abs(z) + 1
  70.             end
  71.         end
  72.        
  73.         term.setBackgroundColor(colors.white)
  74.         term.clear()
  75.         mapButtons = {}
  76.         for i,v in pairs(map) do
  77.             if v.location ~= nil then
  78.                 local x = v.location.x-topLeftX+offsetX
  79.                 local z = v.location.z-topLeftZ+offsetY
  80.                 if mapButtons[x] == nil then mapButtons[x] = {} end
  81.                 mapButtons[x][z] = i
  82.                 term.setCursorPos(x,z)
  83.                 term.setTextColor(colors.black)
  84.                
  85.                 if v.fuel ~= nil and v.fuel >= fuelLevelGreenMin then
  86.                     term.setBackgroundColor(colors.green)
  87.                 elseif v.fuel ~= nil and v.fuel >= fuelLevelYellowMin then
  88.                     term.setBackgroundColor(colors.yellow)
  89.                 elseif v.fuel ~= nil then
  90.                     term.setBackgroundColor(colors.red)
  91.                 else
  92.                     term.setBackgroundColor(colors.white)
  93.                 end
  94.                
  95.                 if v.age ~= nil and v.age >= 5 then
  96.                     term.write("?")
  97.                 elseif v.type == "hopper" then
  98.                     term.write("H")
  99.                 elseif v.type == "stocker" then
  100.                     term.write("S")
  101.                 elseif v.type == "row" then
  102.                     term.write("R")
  103.                 end
  104.             end
  105.         end
  106.         term.setBackgroundColor(colors.green)
  107.         term.setTextColor(colors.white)
  108.         term.setCursorPos(1,1)
  109.         term.write("Request")
  110.     elseif screen == "view" then
  111.         term.setBackgroundColor(colors.white)
  112.         term.setTextColor(colors.black)
  113.         term.clear()
  114.        
  115.         term.setBackgroundColor(colors.red)
  116.         term.setTextColor(colors.white)
  117.         term.setCursorPos(1,1)
  118.         term.write("Back")
  119.        
  120.         term.setBackgroundColor(colors.white)
  121.         term.setTextColor(colors.black)
  122.         local y = 1
  123.         for i,v in pairs(map[viewTarget]) do
  124.             term.setCursorPos(1,y+2)
  125.             if type(v) == "string" or type(v) == "number" then
  126.                 term.write(i:sub(1,1):upper() .. i:sub(2):lower() .. ": " .. v)
  127.                 y = y + 1
  128.             end
  129.         end
  130.        
  131.         if map[viewTarget].location ~= nil then
  132.             local loc = map[viewTarget].location
  133.             term.setCursorPos(1,y+4)
  134.             term.write("Location:")
  135.             term.setCursorPos(2,y+5)
  136.             term.write("X: "..loc.x)
  137.             term.setCursorPos(2,y+6)
  138.             term.write("Y: "..loc.y)
  139.             term.setCursorPos(2,y+7)
  140.             term.write("Z: "..loc.z)
  141.         end
  142.        
  143.         if map[viewTarget].inventory ~= nil then
  144.            
  145.            
  146.             local leftCornerX = w-21
  147.             local leftCornerY = h-15
  148.            
  149.             term.setCursorPos(leftCornerX+2,leftCornerY-1)
  150.             term.write("Inventory")
  151.            
  152.             local inv = map[viewTarget].inventory
  153.             --probably a way more efficient way to do this but... y'know...
  154.             term.setCursorPos(leftCornerX,leftCornerY)
  155.             term.setTextColor(colors.lightGray)
  156.             term.write("+--+--+--+--+")
  157.             for dif = 0,3,1 do
  158.                 term.setCursorPos(leftCornerX,leftCornerY+1+(dif*2))
  159.                 term.setTextColor(colors.lightGray)
  160.                 term.write("|")
  161.                
  162.                 for xslot = 0,3,1 do
  163.                     local item = inv[(dif*4)+xslot+1]
  164.                     term.setTextColor(colors.black)
  165.                     if item ~= nil and item.count ~= nil then
  166.                         if item.count > 9 then
  167.                             term.write(""..math.floor(item.count))
  168.                         else
  169.                             term.write(" "..math.floor(item.count))
  170.                         end
  171.                     else
  172.                         term.write("  ")
  173.                     end
  174.                     term.setTextColor(colors.lightGray)
  175.                     term.write("|")
  176.                 end
  177.                
  178.                 term.setCursorPos(leftCornerX,leftCornerY+2+(dif*2))
  179.                 term.setTextColor(colors.lightGray)
  180.                 term.write("+--+--+--+--+")
  181.             end
  182.         end
  183.     elseif screen == "requestMenu" then
  184.         requestButtons = {}
  185.         term.setBackgroundColor(colors.white)
  186.         term.setTextColor(colors.black)
  187.         term.clear()
  188.        
  189.         term.setCursorPos(1,1)
  190.         term.setTextColor(colors.white)
  191.         term.setBackgroundColor(colors.green)
  192.         term.write("Map")
  193.        
  194.         term.setBackgroundColor(colors.blue)
  195.         term.setTextColor(colors.white)
  196.        
  197.         local align = "left"
  198.         local ny = 4
  199.        
  200.         for i,v in pairs(map)do
  201.             if v.type == "row" then
  202.                 if align == "left" then
  203.                     term.setCursorPos(5,ny)
  204.                     term.write("  "..v.item:sub(11,11):upper()..v.item:sub(12):gsub("_"," ").."  ")
  205.                     term.setCursorPos(5,ny+1)
  206.                     local countStr = ""..v.count
  207.                     local totl = #v.item-10
  208.                     local btn = {}
  209.                     btn.minX = 5
  210.                     btn.minY = ny
  211.                     btn.maxX = #v.item-1
  212.                     btn.maxY = ny+1
  213.                     btn.item = v.item
  214.                     table.insert(requestButtons,btn)
  215.                     term.write(string.rep(" ",math.floor(((totl-#countStr)/2)+2))..countStr..string.rep(" ",math.ceil(((totl-#countStr)/2)+2)))
  216.                     align = "right"
  217.                 elseif align == "right" then
  218.                     term.setCursorPos(w-5-#v.item+10,ny) -- w-(offset)-(length of item name)+(length of minecraft:)
  219.                     term.write("  "..v.item:sub(11,11):upper()..v.item:sub(12):gsub("_"," ").."  ")
  220.                     term.setCursorPos(w-5-#v.item+10,ny+1)
  221.                     local countStr = ""..v.count
  222.                     local totl = #v.item-10
  223.                     local btn = {}
  224.                     btn.minX = w-5-#v.item+10
  225.                     btn.minY = ny
  226.                     btn.maxX = w-3
  227.                     btn.maxY = ny+1
  228.                     btn.item = v.item
  229.                     table.insert(requestButtons,btn)
  230.                     term.write(string.rep(" ",math.floor(((totl-#countStr)/2)+2))..countStr..string.rep(" ",math.ceil(((totl-#countStr)/2)+2)))
  231.                     align = "left"
  232.                     ny = ny + 3
  233.                 end
  234.             end
  235.         end
  236.     elseif screen == "request" then
  237.         local item = nil
  238.         for i,v in pairs(map) do
  239.             if v.item == requestTarget then
  240.                 item = v
  241.                 break
  242.             end
  243.         end
  244.         if item == nil then
  245.             term.clear()
  246.             term.setCursorPos(1,1)
  247.             term.write("Failure")
  248.             updatesEnabled = false
  249.             sleep(3)
  250.             updatesEnabled = true
  251.             screen="map"
  252.         end
  253.        
  254.         term.setBackgroundColor(colors.white)
  255.         term.setTextColor(colors.black)
  256.         term.clear()
  257.        
  258.         term.setBackgroundColor(colors.green)
  259.         term.setTextColor(colors.white)
  260.         term.setCursorPos(1,1)
  261.         term.write("Back")
  262.        
  263.         local beautyname = item.item:sub(11,11):upper()..item.item:sub(12):gsub("_"," ")
  264.         local amount = "Maximum: "..item.count
  265.         term.setBackgroundColor(colors.white)
  266.         term.setTextColor(colors.black)
  267.         term.setCursorPos(math.floor((w/2)-(#beautyname/2)),3)
  268.         term.write(beautyname)
  269.         term.setCursorPos(math.floor((w/2)-(#amount/2)),4)
  270.         term.write(amount)
  271.        
  272.         maxRequestCount = item.count
  273.        
  274.         local cnt = requestCount..""
  275.        
  276.         term.setCursorPos(math.floor(w/2)-10,6)
  277.         term.setBackgroundColor(colors.blue)
  278.         term.setTextColor(colors.white)
  279.         term.write("-")
  280.        
  281.         term.setCursorPos(math.floor((w/2)-(#cnt/2)),6)
  282.         term.setBackgroundColor(colors.white)
  283.         term.setTextColor(colors.black)
  284.         term.write(cnt)
  285.        
  286.         term.setCursorPos(math.floor(w/2)+10,6)
  287.         term.setBackgroundColor(colors.blue)
  288.         term.setTextColor(colors.white)
  289.         term.write("+")
  290.        
  291.         term.setCursorPos(math.floor(w/2)-11,8)
  292.         term.setBackgroundColor(colors.blue)
  293.         term.setTextColor(colors.white)
  294.         term.write("-16")
  295.        
  296.         term.setCursorPos(math.floor(w/2)+9,8)
  297.         term.setBackgroundColor(colors.blue)
  298.         term.setTextColor(colors.white)
  299.         term.write("+16")
  300.        
  301.         term.setCursorPos(math.floor((w/2)-3),10)
  302.         term.setBackgroundColor(colors.blue)
  303.         term.setTextColor(colors.white)
  304.         term.write("Request")
  305.     end
  306. end
  307.  
  308. function buttonListener()
  309.     while true do
  310.         local e,side,x,y = os.pullEvent("monitor_touch")
  311.        
  312.         if screen == "map" then
  313.             if x >= 1 and x <= 7 and y == 1 then
  314.                 screen = "requestMenu"
  315.                 updateVisuals()
  316.             else
  317.                 if mapButtons[x] ~= nil and mapButtons[x][y] ~= nil and map[mapButtons[x][y]] ~= nil then
  318.                     viewTarget = mapButtons[x][y]
  319.                     screen = "view"
  320.                     updateVisuals()
  321.                 end
  322.             end
  323.         elseif screen == "view" then
  324.             if x >= 1 and x <= 4 and y == 1 then
  325.                 screen = "map"
  326.                 viewTarget = ""
  327.                 updateVisuals()
  328.             end
  329.         elseif screen == "requestMenu" then
  330.             if x >= 1 and x <= 3 and y == 1 then
  331.                 screen = "map"
  332.                 updateVisuals()
  333.             else
  334.                 for i,v in pairs(requestButtons)do
  335.                     if v.minX <= x and v.maxX >= x and v.minY <= y and v.maxY >= y then
  336.                         requestTarget = v.item
  337.                         screen = "request"
  338.                         updateVisuals()
  339.                         break
  340.                     end
  341.                 end
  342.             end
  343.         elseif screen == "request" then
  344.             if x >= 1 and x <= 4 and y == 1 then
  345.                 screen = "requestMenu"
  346.                 updateVisuals()
  347.             elseif x == math.floor(w/2)-10 and y == 6 then
  348.                 if requestCount > 1 then
  349.                     requestCount = requestCount - 1
  350.                     updateVisuals()
  351.                 end
  352.             elseif x == math.floor(w/2)+10 and y == 6 then
  353.                 if requestCount < maxRequestCount then
  354.                     requestCount = requestCount + 1
  355.                     updateVisuals()
  356.                 end
  357.             elseif x >= math.floor(w/2)-11 and x <= math.floor(w/2)-9 and y == 8 then
  358.                 if requestCount-16 > 0 then
  359.                     requestCount = requestCount - 16
  360.                     updateVisuals()
  361.                 else
  362.                     requestCount = 1
  363.                     updateVisuals()
  364.                 end
  365.             elseif x >= math.floor(w/2)+9 and x <= math.floor(w/2)+11 and y == 8 then
  366.                 if requestCount+16 <= maxRequestCount then
  367.                     requestCount = requestCount + 16
  368.                     updateVisuals()
  369.                 else
  370.                     requestCount = maxRequestCount
  371.                     updateVisuals()
  372.                 end
  373.             elseif x >= math.floor((w/2)-3) and x <= math.floor((w/2)+3) and y == 10 then
  374.                 updatesEnabled = false
  375.                
  376.                 os.startTimer(2)
  377.                 modem.open(gatherChannel+1)
  378.                
  379.                 modem.transmit(modem3,gatherChannel+1,{name=requestTarget,count=requestCount})
  380.                
  381.                 while true do
  382.                     local e,side,chnl,rChnl,msg,dist = os.pullEvent()
  383.                    
  384.                     if e == "modem_message" and chnl == gatherChannel+1 and type(msg) == "table" and type(msg.type) == "string" and msg.type == "requestResponse" then
  385.                         if type(msg.success) == "boolean" and msg.success then
  386.                             term.setTextColor(colors.black)
  387.                             term.setBackgroundColor(colors.white)
  388.                             term.clear()
  389.                            
  390.                             local response = "Success!"
  391.                             term.setCursorPos(math.floor((w/2)-(#response/2)),4)
  392.                             term.write(response)
  393.                            
  394.                             screen = "map"
  395.                             updateVisuals()
  396.                            
  397.                             modem.close(gatherChannel+1)
  398.                            
  399.                             break
  400.                         else
  401.                             if type(msg.error) == "string" then
  402.                                 term.setTextColor(colors.black)
  403.                                 term.setBackgroundColor(colors.white)
  404.                                 term.clear()
  405.                                
  406.                                 local response = "Failure: "..msg.error
  407.                                 term.setCursorPos(math.floor((w/2)-(#response/2)),4)
  408.                                 term.write(response)
  409.                                
  410.                                 screen = "map"
  411.                                 updateVisuals()
  412.                                
  413.                                 modem.close(gatherChannel+1)
  414.                                
  415.                                 break
  416.                             else
  417.                                 term.setTextColor(colors.black)
  418.                                 term.setBackgroundColor(colors.white)
  419.                                 term.clear()
  420.                                
  421.                                 local response = "Failure: Unknown"
  422.                                 term.setCursorPos(math.floor((w/2)-(#response/2)),4)
  423.                                 term.write(response)
  424.                                
  425.                                 screen = "map"
  426.                                 updateVisuals()
  427.                                
  428.                                 modem.close(gatherChannel+1)
  429.                                
  430.                                 break
  431.                             end
  432.                         end
  433.                     elseif e == "timer" then
  434.                         term.setTextColor(colors.black)
  435.                         term.setBackgroundColor(colors.white)
  436.                         term.clear()
  437.                        
  438.                         local response = "Failure: Timed out"
  439.                         term.setCursorPos(math.floor((w/2)-(#response/2)),4)
  440.                         term.write(response)
  441.                        
  442.                         screen = "map"
  443.                         updateVisuals()
  444.                        
  445.                         modem.close(gatherChannel+1)
  446.                        
  447.                         break
  448.                     end
  449.                 end
  450.                
  451.                 updatesEnabled = true
  452.             end
  453.         end
  454.     end
  455. end
  456.  
  457. function updateData()
  458.     while true do
  459.         for i,v in pairs(map) do
  460.             if v.age == nil then
  461.                 v.age = 1
  462.             else
  463.                 v.age = v.age + 1
  464.             end
  465.         end
  466.    
  467.         modem.transmit(modem1,gatherChannel,"")
  468.         sleep(.5)
  469.         updateVisuals()
  470.         sleep(1.5)
  471.     end
  472. end
  473.  
  474. function gatherData()
  475.     while true do
  476.         local e,side,chnl,rChnl,msg,dist = os.pullEvent("modem_message")
  477.        
  478.         if chnl == gatherChannel and type(msg) == "table" and type(msg.type) == "string" then
  479.             for i,v in pairs(msg) do
  480.                 if type(v) == "boolean" then
  481.                     if v then
  482.                         msg[i] = "true"
  483.                     else
  484.                         msg[i] = "false"
  485.                     end
  486.                 end
  487.             end
  488.             msg.distance = dist
  489.             msg.age = 0
  490.             if msg.type == "infoHopperResponse" and msg.channel ~= nil then
  491.                 msg.type = "hopper"
  492.                 map["hopperChnl"..msg.channel] = msg
  493.             elseif msg.type == "infoStockerResponse" then
  494.                 msg.type = "stocker"
  495.                 map["stocker"] = msg
  496.             elseif msg.type == "infoRowResponse" and msg.item ~= nil then
  497.                 msg.type = "row"
  498.                 map["rowItem"..msg.item] = msg
  499.             end
  500.         end
  501.     end
  502. end
  503.  
  504. modem.open(gatherChannel)
  505.  
  506. parallel.waitForAll(updateData,gatherData,buttonListener)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement