Advertisement
Dragonfyre173

Glass Box

Aug 28th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.96 KB | None | 0 0
  1. --A foreword: This is a REALLY FUCKING UGLY program.
  2. --Terminal Glasses Box
  3. local modem = peripheral.wrap("right")
  4. local glass = peripheral.wrap("top")
  5. glass.clear()
  6.  
  7. local FORMAT_CHAR = "\194\167"
  8. local FORMAT_BOLD = FORMAT_CHAR .. 'l'
  9.  
  10. local keyboardActive = false
  11.  
  12. local file = nil
  13.  
  14. local file = fs.open("ieMap","r")
  15. local fullMap = file.readAll()
  16. fullMap = textutils.unserialize(fullMap)
  17.  
  18. if fullMap == nil then
  19.     fullMap = { }
  20. end
  21.  
  22. file.close()
  23.  
  24. --This is stupid
  25. file = fs.open("ieMap","w")
  26.  
  27. local gColors = {
  28.     red = 0xff3333,
  29.     blue = 0x7dd2e4,
  30.     yellow = 0xffff4d,
  31.     green = 0x4dff4d,
  32.     gray = 0xe0e0e0,
  33.     textGray = 0x818181,
  34.     text = 0x606060,
  35.     rain = 0x2e679f
  36. }
  37.  
  38. local depositTypes = {
  39.     {
  40.         name = "None",
  41.         color = 0xffffff,
  42.         { name = "None", id = "minecraft:stone", meta = 0, percent = 1.0 }
  43.     },
  44.     {
  45.         name = "Bauxite",
  46.         color = 0x999cc6,
  47.         { name = "Aluminum", id = "ImmersiveEngineering:metal", meta = 1, percent = 1.0 }    
  48.     },
  49.     {
  50.         name = "Cassiterite",
  51.         color = 0xb3b4b5,
  52.         { name = "Tin", id = "RotaryCraft:rotarycraft_item_modingots", meta = 0, percent = 1.0 }
  53.     },
  54.     {
  55.         name = "Coal",
  56.         color = 0x303045,
  57.         { name = "Coal", id = "minecraft:coal", meta = 0, percent = 0.9684 },
  58.         { name = "Diamond", id = "minecraft:diamond", meta = 0, percent = 0.0158 },
  59.         { name = "Emerald", id = "minecraft:emerald", meta = 0, percent = 0.0158 }
  60.     },
  61.     {
  62.         name = "Copper",
  63.         color = 0xb87333,
  64.         { name = "Copper", id = "ImmersiveEngineering:metal", meta = 0, percent = 0.6842 },
  65.         { name = "Gold", id = "minecraft:gold_ingot", meta = 0, percent = 0.2632 },
  66.         { name = "Nickel", id ="ImmersiveEngineering:metal", meta = 4, percent = 0.0526 }
  67.     },
  68.     {
  69.         name = "Galena",
  70.         color = 0x7788bb,
  71.         { name = "Lead", id ="ImmersiveEngineering:metal", meta = 2, percent = 0.5 },
  72.         { name = "Silver", id ="ImmersiveEngineering:metal", meta = 3, percent = 0.5 }
  73.     },
  74.     {
  75.         name = "Gold",
  76.         color = 0xdec900,
  77.         { name = "Gold", id = "minecraft:gold_ingot", meta = 0, percent = 0.6842 },
  78.         { name = "Copper", id = "ImmersiveEngineering:metal", meta = 0, percent = 0.2632 },
  79.         { name = "Nickel", id ="ImmersiveEngineering:metal", meta = 4, percent = 0.0526 }
  80.     },
  81.     {
  82.         name = "Iron",
  83.         color = 0x6c7b8b,
  84.         { name = "Iron", id = "minecraft:iron_ingot", meta = 0, percent = 0.5263 },
  85.         { name = "Nickel", id ="ImmersiveEngineering:metal", meta = 4, percent = 0.2632 },
  86.         { name = "Tin", id = "RotaryCraft:rotarycraft_item_modingots", meta = 0, percent = 0.2105 }
  87.     },
  88.     {
  89.         name = "Lapis",
  90.         color = 0x26619c,
  91.         { name = "Lapis", id = "minecraft:dye", meta = 4, percent = 0.7027 },
  92.         { name = "Iron", id = "minecraft:iron_ingot", meta = 0, percent = 0.2973 }
  93.     },
  94.     {
  95.         name = "Lead",
  96.         color = 0x5e5c59,
  97.         { name = "Lead", id ="ImmersiveEngineering:metal", meta = 2, percent = 0.5789 },
  98.         { name = "Silver", id ="ImmersiveEngineering:metal", meta = 3, percent = 0.4211 }
  99.     },
  100.     {
  101.         name = "Magnetite",
  102.         color = 0xcdbe70,
  103.         { name = "Iron", id = "minecraft:iron_ingot", meta = 0, percent = 0.85 },
  104.         { name = "Gold", id = "minecraft:gold_ingot", meta = 0, percent = 0.15 }
  105.     },
  106.     {
  107.         name = "Nickel",
  108.         color = 0xb8b8b6,
  109.         { name = "Nickel", id ="ImmersiveEngineering:metal", meta = 4, percent = 0.8947 },
  110.         { name = "Platinum", id = "RotaryCraft:rotarycraft_item_modingots", meta = 44, percent = 0.0526 },
  111.         { name = "Iron", id = "minecraft:iron_ingot", meta = 0, percent = 0.0527 }
  112.     },
  113.     {
  114.         name = "Platinum",
  115.         color = 0xafafae,
  116.         { name = "Platinum", id = "RotaryCraft:rotarycraft_item_modingots", meta = 44, percent = 0.5625 },
  117.         { name = "Nickel", id ="ImmersiveEngineering:metal", meta = 4, percent = 0.4375 }
  118.     },
  119.     {
  120.         name = "Pyrite",
  121.         color = 0xADA67E,
  122.         { name = "Iron", id = "minecraft:iron_ingot", meta = 0, percent = 1.0 }
  123.     },
  124.     {
  125.         name = "Quarzite",
  126.         color = 0xaa98a9,
  127.         { name = "Quartz", id = "minecraft:quartz", meta = 0, percent = 1.0 }
  128.     },
  129.     {
  130.         name = "Silver",
  131.         color = 0xc0c0c0,
  132.         { name = "Silver", id ="ImmersiveEngineering:metal", meta = 3, percent = 0.5789 },
  133.         { name = "Lead", id ="ImmersiveEngineering:metal", meta = 2, percent = 0.4211 }    
  134.     },
  135.     {
  136.         name = "Uranium",
  137.         color = 0x5dca31,
  138.         { name = "Uranium", id = "RotaryCraft:rotarycraft_item_modingots", meta = 10, percent = 0.6471 },
  139.         { name = "Lead", id ="ImmersiveEngineering:metal", meta = 2, percent = 0.3529 }
  140.     }
  141. }
  142.  
  143. term.clear()
  144. term.setCursorPos(1,1)
  145. --Create the ore buttons
  146. local buttons = { }
  147. local depositColors = { }
  148. local btnlabels = { }
  149.  
  150. local graphs = { }
  151. local graphBase = { }
  152. local nDeposits = #depositTypes
  153. local textHeight = 16
  154. local itr = 1
  155. local currentOre = 0
  156.  
  157. local dbugtext = glass.addText(5,5,"",gColors.red)
  158. dbugtext.setScreenAnchor("middle","middle")
  159.  
  160. local tDetails = glass.addText(3,30,"",gColors.yellow)
  161. local tIcons = {
  162.     glass.addIcon(3,48,0,0),
  163.     glass.addIcon(21,48,0,0),
  164.     glass.addIcon(39,48,0,0)
  165. }
  166. local tDeposit = glass.addText(3,66,"",gColors.yellow)
  167.  
  168. --Create the chunk grid
  169. local chunkGrid = { }
  170. local chunkContents = { }
  171. -- Size of each chunk in px
  172. local chunkSize = 15
  173. local chunkDisplayOffset = 15
  174.  
  175. local cgridSize = 7 * (chunkSize + 1) + 1
  176.  
  177. local pmax = chunkDisplayOffset + 8 * (1 + chunkSize)
  178. local pmin = chunkDisplayOffset + 1 + chunkSize
  179.  
  180. for k,v in ipairs(depositTypes) do
  181.     graphs[k] = { icons = { }, bars = { }, text = { } }
  182.    
  183.     local posY = textHeight * itr
  184.    
  185.     buttons[k] = glass.addBox(8, posY, 100, 15, gColors.text, 0.6)
  186.     buttons[k].setZ(5)
  187.     buttons[k].setUserdata(k)
  188.     buttons[k].setScreenAnchor("left","top")
  189.     buttons[k].setObjectAnchor("left","top")
  190.    
  191.     btnlabels[k] = glass.addText(16, posY + 4, v.name, v.color)
  192.     btnlabels[k].setScreenAnchor("left","top")
  193.     btnlabels[k].setZ(1)
  194.    
  195.     depositColors[k] = glass.addBox(109, posY, 15, 15, v.color, 0.9)
  196.     depositColors[k].setZ(6)
  197.     depositColors[k].setScreenAnchor("left","top")
  198.    
  199.     graphs[k].icons = { }
  200.    
  201.     for item = 1, #v do
  202.         local posX = 75 + 18 * (item - 1)
  203.         graphs[k].icons[item] = glass.addIcon(-24 * (4 - item) - 69, -68 ,v[item].id,v[item].meta)
  204.         graphs[k].icons[item].setScreenAnchor("right","bottom")
  205.         graphs[k].icons[item].setVisible(false)
  206.        
  207.         local barheight = math.floor(100 * v[item].percent) + 1
  208.         graphs[k].bars[item] = glass.addBox(-24 * (4 - item) - 69, -70 - barheight, 16, barheight, gColors.green, 0.75)
  209.         graphs[k].bars[item].setScreenAnchor("right","bottom")
  210.         graphs[k].bars[item].setVisible(false)
  211.        
  212.         graphs[k].text[item] = glass.addText(-24 * (4 - item) - 60, -50, v[item].name, gColors.yellow)
  213.         graphs[k].text[item].setRotation(45)
  214.         graphs[k].text[item].setScreenAnchor("right","bottom")
  215.         graphs[k].text[item].setVisible(false)
  216.     end
  217.    
  218.     itr = itr + 1
  219. end
  220.  
  221. graphBase.color = gColors.green
  222.  
  223.  
  224. for bar = 1, 8 do
  225.     pos = chunkDisplayOffset + bar * (1 + chunkSize)
  226.    
  227.     hor = 2 * bar
  228.     ver = hor - 1
  229.    
  230.     chunkGrid[ver] = glass.addBox(-pmax,pos,cgridSize,1,gColors.text,0.75)
  231.     chunkGrid[ver].setScreenAnchor("right","top")
  232.     chunkGrid[ver].setVisible(false)
  233.    
  234.     chunkGrid[hor] = glass.addBox(-pos,pmin,1,cgridSize,gColors.text,0.75)
  235.     chunkGrid[hor].setScreenAnchor("right","top")
  236.     chunkGrid[hor].setVisible(false)
  237. end
  238.  
  239. local pl = { x = 0, y = 0, box = nil }
  240.  
  241. pl.x = -pmin - 4 * (1 + chunkSize) + 1
  242. pl.y = chunkDisplayOffset + 4 * (1 + chunkSize) + 1
  243.  
  244. pl.box = glass.addBox(pl.x,pl.y,1,1,0xff0000,1.0)
  245. pl.box.setScreenAnchor("right","top")
  246.  
  247. for i = -3, 3 do
  248.     chunkContents[i] = { }
  249.     for j = -3, 3 do
  250.         local xxx = -pmin - (4 + i) * (1 + chunkSize) + 1
  251.         local yyy = chunkDisplayOffset + (4 + j) * (1 + chunkSize) + 1
  252.         chunkContents[i][j] = glass.addBox(xxx,yyy,chunkSize,chunkSize,0x000000,0.9)
  253.         chunkContents[i][j].setScreenAnchor("right","top")
  254.         chunkContents[i][j].setVisible(false)
  255.     end
  256. end
  257.  
  258. local graphOffset = -pmax
  259.  
  260. graphBase.xAxis = glass.addBox(graphOffset - 1, -70, 73, 1, graphBase.color, 0.75)
  261. graphBase.xAxis.setScreenAnchor("right","bottom")
  262. graphBase.xAxis.setVisible(false)
  263.  
  264. graphBase.yAxis = glass.addBox(graphOffset - 2, -70 - 101, 1, 102, graphBase.color, 0.75)
  265. graphBase.yAxis.setScreenAnchor("right","bottom")
  266. graphBase.yAxis.setVisible(false)
  267.  
  268. local increments = 10
  269. local incrementBy = 100 / increments
  270.  
  271. graphBase.notches = { }
  272. graphBase.notchText = { }
  273.  
  274. for i = 1, increments do
  275.     local offset = i * incrementBy
  276.     graphBase.notches[i] = glass.addBox(graphOffset - 1, -70 - offset - 1, 2, 1, graphBase.color, 0.75)
  277.     graphBase.notches[i].setScreenAnchor("right","bottom")
  278.     graphBase.notches[i].setVisible(false)
  279. end
  280.  
  281. --Make sure that these match the Pocket's channels
  282. --Send must be Pocket's reply and vice versa
  283. local sendChannel  = 1554
  284. local replyChannel = 1555
  285.  
  286. modem.open(sendChannel)
  287. modem.open(replyChannel)
  288.  
  289. local chunk = {x = nil, z = nil}
  290.  
  291. local timeout = 5 -- Timeout in seconds
  292. local timer = os.startTimer(timeout)
  293.  
  294. local function updateChunkMap()
  295.     if chunk.x ~= nil and chunk.z ~= nil then
  296.         for i = -3, 3 do
  297.             for j = -3, 3 do
  298.                 index = { x = chunk.x - i, z = chunk.z + j }
  299.                 indstr = index.x..","..index.z
  300.                 code = fullMap[indstr]
  301.                 if code == nil then
  302.                     chunkContents[i][j].setOpacity(0)
  303.                 else
  304.                     chunkContents[i][j].setOpacity(0.75)
  305.                     chunkContents[i][j].setColor(depositTypes[code].color)
  306.                 end
  307.             end
  308.         end
  309.     else
  310.         for i = -3, 3 do
  311.             for j = -3, 3 do
  312.                 chunkContents[i][j].setOpacity(0)
  313.             end
  314.         end
  315.     end
  316. end
  317.  
  318. local function updateGraphVisibility(visible)
  319.     pl.box.setVisible(visible)
  320.     for k,v in ipairs(depositTypes) do
  321.         for item = 1, #v do
  322.             graphs[k].icons[item].setVisible(visible and k == currentOre)
  323.             graphs[k].bars[item].setVisible(visible and k == currentOre)
  324.             graphs[k].text[item].setVisible(visible and k == currentOre)
  325.            
  326.             if currentOre == nil then currentOre = -1 end
  327.             graphBase.xAxis.setVisible(visible and currentOre > 0)
  328.             graphBase.yAxis.setVisible(visible and currentOre > 0)
  329.         end
  330.     end
  331.    
  332.     for i = 1, increments do
  333.         graphBase.notches[i].setVisible(visible and currentOre > 0)
  334.     end
  335.    
  336.     for k,v in ipairs(chunkGrid) do
  337.         v.setVisible(visible)
  338.     end
  339.    
  340.     for i = -3, 3 do
  341.         for j = -3, 3 do
  342.             chunkContents[i][j].setVisible(visible)
  343.         end
  344.     end
  345.    
  346.     updateChunkMap()
  347.     glass.sync()
  348. end
  349.  
  350. local function setKeyboardMode(visible)
  351.     for k,v in ipairs(depositTypes) do
  352.         buttons[k].setVisible(visible)
  353.         depositColors[k].setVisible(visible)
  354.         btnlabels[k].setVisible(visible)
  355.     end
  356.     tDetails.setVisible(not visible)
  357.     for i = 1, 3 do
  358.         tIcons[i].setVisible((not visible) and i <= #depositTypes[i])
  359.     end
  360.     tDeposit.setVisible(not visible)
  361.     updateGraphVisibility(visible)
  362. end
  363.  
  364. local function handleTimer()
  365.     tDetails.setText(FORMAT_BOLD.."GPS not found")
  366.     tDetails.setColor(gColors.red)
  367.     timer = os.startTimer(timeout)
  368.     glass.sync()
  369. end
  370.  
  371. local function handleMessage(event)
  372.     local pos = textutils.unserialize(event[5])
  373.     if pos == nil or pos.x == nil or pos.y == nil or pos.z == nil then
  374.         tDetails.setText("Out of range!")
  375.     else
  376.         pl.box.setX(pl.x + math.floor(pos.x) % 16 - 1)
  377.         pl.box.setY(pl.y + math.floor(pos.z) % 16 - 1)
  378.         chunk.x = math.floor(pos.x / 16)
  379.         chunk.z = math.floor(pos.z / 16)
  380.         tDetails.setText("Chunk: x="..chunk.x.." z="..chunk.z)
  381.         tDetails.setColor(gColors.text)
  382.  
  383.         entry = fullMap[chunk.x..","..chunk.z]
  384.         if entry == nil then
  385.             tDeposit.setText("")
  386.             tDeposit.setVisible(false)
  387.             for i = 1, 3 do
  388.                 tIcons[i].setVisible(false)
  389.             end
  390.         else
  391.             tDeposit.setText(depositTypes[entry].name)
  392.             tDeposit.setVisible(true)
  393.             for i = 1, 3 do
  394.                 if depositTypes[entry][i] == nil then
  395.                     tIcons[i].setVisible(false)
  396.                 else
  397.                     tIcons[i].setVisible(true)
  398.                     tIcons[i].setItemId(depositTypes[entry][i].id)
  399.                     tIcons[i].setMeta(depositTypes[entry][i].meta)
  400.                 end
  401.             end
  402.         end
  403.     end
  404.     glass.sync()
  405. end
  406.  
  407. --Handles a player using the wireless keyboard
  408. local function handleOpenKeyboard(event)
  409.     keyboardActive = true
  410.     setKeyboardMode(true)
  411. end
  412.  
  413. local function handleCloseKeyboard(event)
  414.     setKeyboardMode(false)
  415.     keyboardActive = false
  416.     currentOre = 0
  417. end
  418.  
  419. local function handleComponentClick(event)
  420.     component = glass.getObjectById(event[5])
  421.     currentOre = component.getUserdata()
  422.     glass.sync()
  423.     if chunk.x ~= nil and chunk.z ~= nil then
  424.         index = chunk.x..","..chunk.z
  425.         fullMap[index] = currentOre
  426.         file.flush()
  427.     end
  428.     updateGraphVisibility(true)
  429. end
  430.  
  431. setKeyboardMode(false)
  432. --Main loop
  433. while true do
  434.     glass.sync()
  435.     local event = {os.pullEvent()}
  436.     if event[1] ~= "timer" then
  437.         timer = os.startTimer(timeout)
  438.     end
  439.     if event[1] == "timer" and event[2] == timer then
  440.             handleTimer()
  441.     elseif event[1] == "glasses_capture" then
  442.         handleOpenKeyboard(event)
  443.     elseif event[1] == "glasses_release" then
  444.         handleCloseKeyboard(event)
  445.     elseif event[1] == "glasses_component_mouse_down" then
  446.         handleComponentClick(event)
  447.     elseif event[1] == "modem_message" and not keyboardActive then
  448.         handleMessage(event)
  449.     elseif event[1] == "key" then
  450.         modem.transmit(sendChannel,replyChannel,"s")
  451.         break
  452.     end
  453.     modem.transmit(sendChannel,replyChannel,"ack")
  454. end
  455.  
  456. glass.clear()
  457. glass.sync()
  458.  
  459. fullMap = textutils.serialize(fullMap)
  460. file.write(fullMap)
  461. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement