davedumas0

my minecraft opencomputers control thingy - v0.2

Mar 4th, 2022 (edited)
1,316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.13 KB | None | 0 0
  1. --------------------------
  2. --required function libs
  3. --------------------------
  4.  
  5.  
  6. local os = require("os")
  7. local computer = require("computer")
  8. local term = require("term")
  9. local colors = require("colors")
  10. local filesystem = require("filesystem")
  11. local component = require("component")
  12. local keyboard = require("keyboard")
  13. local event = require("event")
  14. local gpu = component.gpu
  15. local redstone = component.redstone
  16.  
  17.  
  18.  
  19.  
  20.  
  21. local gpu1 = component.gpu  -- get the proxy of the primary GPU
  22. local addr1 = tostring(component.get("c9d9"))  -- the address of the first screen
  23. local addr2 = tostring(component.get("fb45"))  -- the address of the second screen
  24.  
  25. local touchLocation = ""
  26. local touchName = ""
  27. local touchX = 0
  28. local touchY = 0
  29. local meItems = component.me_controller.getItemsInNetwork()
  30. local meFluids = component.me_controller.getFluidsInNetwork()
  31. local tempColor = colors_magenta
  32.  
  33.  
  34. colors_white = 0xffffff
  35. colors_orange = 0xff6600
  36. colors_magenta = 0xff00ff
  37. colors_lightblue = 0x0099ff
  38. colors_yellow = 0xffff00
  39. colors_lime = 0x00ff00
  40. colors_pink = 0xff3399
  41. colors_gray = 0x737373
  42. colors_silver = 0xc0c0c0
  43. colors_cyan = 0x169c9d
  44. colors_purple = 0x8932b7
  45. colors_blue = 0x3c44a9
  46. colors_brown = 0x825432
  47. colors_green = 0x5d7c15
  48. colors_red = 0xb02e26
  49. colors_black = 0x000000
  50.  
  51. mobs = {}
  52.  
  53. lightToggle1 = false
  54. lightToggle2 = false
  55. lightToggle3 = false
  56. lightToggle4 = false
  57. lightToggle5 = false
  58.  
  59.  
  60. temp15 = 0
  61.  
  62.  
  63. function temp1()
  64.  if temp15 <=15 then
  65.  temp15 = temp15 +1
  66.  end
  67.  if temp15 == 15 then
  68.   temp15 = 0
  69.  end
  70. end
  71.  
  72.  
  73.  
  74. term.clear()
  75.  
  76. function isRedstoneChannelOpen(redstoneChannel)
  77. local redstoneChannelData = redstone.getBundledInput(5, redstoneChannel)
  78.  
  79.  if redstoneChannelData > 0 then
  80.   return true
  81.   else return false
  82.  end
  83. end
  84.  
  85.  
  86. function openRedstoneChannel(redstoneChannel)
  87. local redstoneChannelData = redstone.setBundledOutput(5, redstoneChannel, 255)
  88. end
  89.  
  90. function closeRedstoneChannel(redstoneChannel)
  91. local redstoneChannelData = redstone.setBundledOutput(5, redstoneChannel, 0)
  92. end
  93.  
  94. function isPlayerInCorridor ()
  95. local temp1 = isRedstoneChannelOpen(1)
  96.   if temp1 then
  97.    return true
  98.   else return false
  99.   end
  100.  
  101. end
  102.  
  103. function toggleCrusher()
  104.  
  105.   if not isRedstoneChannelOpen(2) then
  106.    lightToggle1 = true
  107.    openRedstoneChannel(2)
  108.   else
  109.    lightToggle1 = false
  110.    closeRedstoneChannel(2)
  111.   end
  112.  
  113. end
  114.  
  115. function toggleSpawner()
  116.  
  117.   if isRedstoneChannelOpen(7) then
  118.    lightToggle2 = false
  119.    closeRedstoneChannel(7)
  120.   else
  121.    lightToggle2 = true
  122.    openRedstoneChannel(7)
  123.   end
  124.  
  125. end
  126.  
  127. -----------------------------
  128. ------ functions for UI -----
  129. ------------\/---------------
  130.  
  131. function drawLine (posX, posY, length, highth, line_color)
  132.   gpu.setBackground(line_color)
  133.  
  134.   gpu.fill(posX, posY, length, highth, " ")
  135. end
  136.  
  137. function drawLight1(X, Y, length, hight)
  138.  
  139.  if lightToggle1 == true then
  140.    drawText(X-1, Y-2, "enabled", colors_blue, colors_white)
  141.    drawLine(X, Y, length, hight, colors_green)
  142.    
  143.  else
  144.    drawText(X-1, Y-2, "disabled", colors_red, colors_white)
  145.    drawLine(X, Y, length, hight, colors_red)
  146.  end
  147.  
  148. end
  149.  
  150. function drawLight2(X, Y, length, hight)
  151.  
  152.  if lightToggle2 == true then
  153.    drawText(X-1, Y-2, "enabled", colors_green, colors_white)
  154.    drawLine(X, Y, length, hight, colors_green)
  155.  
  156.  else
  157.    drawText(X-1, Y-2, "disabled", colors_red, colors_white)
  158.    drawLine(X, Y, length, hight, colors_red)
  159.  end
  160.  
  161. end
  162.  
  163. function drawText (X, Y, text, color_txt, color_bg)
  164.  term.setCursor(X, Y)
  165.  gpu.setBackground(color_bg)
  166.  
  167.  gpu.setForeground(color_txt, false)
  168.  term.write(tostring(text))
  169.  gpu.setBackground(colors_black)
  170. end
  171.  
  172. function drawButton (label, x, y, hight, func, fuc_data, txt_color, bttn_color, enabled)
  173.  length = #label+2
  174.   if not enabled then
  175.      drawLine(x, y, length, hight, colors_gray)
  176.      drawText(x, y, label, colors_gray, colors_gray)  
  177.   end
  178.   if enabled then
  179.     drawLine(x, y, length, hight, bttn_color)
  180.     drawText(x+1,y+hight/2, label, colors_white, bttn_color)
  181.    if touchX ~= nil or touchY ~= nil then
  182.     if (touchX >= x) and (touchX <= x + length-1)  and (touchY >= y) and (touchY <= y + hight-1) then
  183.       func(fuc_data)
  184.     end
  185.    end
  186.   end
  187. end
  188.  
  189.  
  190.  
  191.  
  192. function swichToScreen1()
  193.    gpu.bind(addr1)
  194. end
  195.  
  196. function swichToScreen2()
  197.    gpu.bind(addr2)
  198. end
  199.  
  200. function clearBothScreens()
  201.    swichToScreen1()
  202.     term.clear()
  203.      swichToScreen2()
  204.       term.clear()
  205.        swichToScreen1()
  206. end
  207.  
  208. function clearPrimaryScreen()
  209.    swichToScreen1()
  210.     gpu.setResolution(160, 50)
  211.     term.clear()
  212. end
  213.  
  214. function clearSecondaryScreen()
  215.    swichToScreen2()
  216.    gpu.setResolution(140, 50)
  217.     term.clear()
  218. end
  219.  
  220. function drawCorridorAccessPanel()
  221. local panelX = 3
  222. local panelY  = 10
  223. local panelSizeX = 14
  224. local panelSizeY = 13
  225. local buttonSizeY = 5
  226.  drawLine(panelX-1, panelY, panelSizeX+2, panelSizeY, colors_gray)
  227.  drawText(panelX-1, panelY-1, "corridor  access", colors_white, colors_gray)
  228.  drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_gray)
  229.  drawButton("open door ", panelX+1, panelY+1, buttonSizeY, openCoridorDoor, _, colors_green, colors_green, true)
  230.  drawButton("close door", panelX+1, panelY+buttonSizeY+2, buttonSizeY, closeCoridorDoor, _, colors_green, colors_red, true)
  231. end
  232.  
  233. function drawRoomAccessPanel()
  234. local panelX = 100
  235. local panelY  = 10
  236. local panelSizeX = 14
  237. local panelSizeY = 13
  238. local buttonSizeY = 5
  239.  drawLine(panelX-1, panelY, panelSizeX+2, panelSizeY, colors_gray)
  240.  drawText(panelX-1, panelY-1, "room  access", colors_white, colors_gray)
  241.  drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_gray)
  242.  drawButton("open door ", panelX+1, panelY+1, buttonSizeY, openCoridorDoor, _, colors_green, colors_green, true)
  243.  drawButton("close door", panelX+1, panelY+buttonSizeY+2, buttonSizeY, closeCoridorDoor, _, colors_green, colors_red, true)
  244. end
  245.  
  246. function drawStorageInfoPanel()
  247. local panelX = 80
  248. local panelY  = 5
  249. local panelSizeX = 55
  250. local panelSizeY = 5
  251.  
  252.  drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_gray)
  253.    drawText(panelX+1, panelY+1, "current stored essence:", colors_lightblue, colors_gray)
  254.    drawText(110, panelY+1, getCurrentEssenceInStorage().." mili-Buckets", colors_lightblue, colors_gray)
  255.    
  256.    drawText(panelX+1, panelY+3, "number of available mobs ", colors_lightblue, colors_gray)
  257.    drawText(panelX+30, panelY+3, #meItems, colors_lightblue, colors_gray)
  258.  
  259. end
  260.  
  261. function drawAvailableMobsListPanel()
  262. local panelX = 80
  263. local panelY  = 11
  264. local panelSizeX = 26
  265. local panelSizeY = 8
  266. mobs = {}
  267.  
  268. --this needs to be shrunk down to a "for loop"
  269.   table.insert(mobs, meItems[1]["entity"])
  270.   table.insert(mobs, meItems[2]["entity"])
  271.   table.insert(mobs, meItems[3]["entity"])
  272.   table.insert(mobs, meItems[4]["entity"])
  273.   table.insert(mobs, meItems[5]["entity"])
  274.  drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_gray)
  275.  drawText(panelX+6, panelY, "available mobs", colors_white, colors_gray)
  276.   drawText(panelX+1, panelY+1, mobs[1], colors_lime, colors_gray)
  277.    drawText(panelX+1, panelY+2, mobs[2], colors_lime, colors_gray)
  278.     drawText(panelX+1, panelY+3, mobs[3], colors_lime, colors_gray)
  279.      drawText(panelX+1, panelY+4, mobs[4], colors_lime, colors_gray)
  280.       drawText(panelX+1, panelY+5, mobs[5], colors_lime, colors_gray)
  281. end
  282.  
  283. function drawMobSpawnerInfoPanel()
  284. local panelX = 107
  285. local panelY  = 11
  286. local panelSizeX = 28
  287. local panelSizeY = 8
  288.  
  289.  
  290.  drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_gray)
  291.  drawText(panelX+1, panelY+1, "mob currently in spawner", colors_lime, colors_gray)
  292.  
  293.   drawText(panelX+11, panelY+3, "NONE", colors_lime, colors_gray)
  294.  
  295. end
  296.  
  297. function drawMobSpawnerControlPanel ()
  298. local panelX = 79
  299. local panelY  = 4
  300. local panelSizeX = 57
  301. local panelSizeY = 40
  302.  
  303.  
  304. drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_white)
  305.  
  306.     drawStorageInfoPanel()
  307.      drawAvailableMobsListPanel()
  308.      drawMobSpawnerInfoPanel()
  309.     drawMobSpawnerControls()
  310.     drawMobCrusherIndicatorLights()
  311.     drawMobSpawnerIndicatorLights()
  312. end
  313.  
  314. function drawMobCrusherIndicatorLights()
  315. local panelX = 80
  316. local panelY  = 20
  317. local panelSizeX = 55
  318. local panelSizeY = 9
  319.  
  320.   --drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_gray)
  321.  
  322.      drawLine(panelX+1, panelY+1, 10, 7, colors_white)
  323.       drawLight1(panelX+3, panelY+2, 6, 2)
  324.        drawText(panelX+3, panelY+5, "crusher", colors_black, colors_white)
  325.         drawText(panelX+3, panelY+6, "control", colors_black, colors_white)
  326.  
  327.  
  328. end
  329.  
  330. function drawMobSpawnerIndicatorLights()
  331. local panelX = 90
  332. local panelY  = 20
  333. local panelSizeX = 55
  334. local panelSizeY = 9
  335.  
  336.   --drawLine(panelX, panelY, panelSizeX, panelSizeY, colors_gray)
  337.    drawLine(panelX+1, panelY+1, 10, 7, colors_white)
  338.    drawLight2(panelX+3, panelY+2, 6, 2)
  339.     drawText(panelX+3, panelY+5, "spawner", colors_black, colors_white)
  340.      drawText(panelX+3, panelY+6, "control", colors_black, colors_white)
  341.  
  342.  
  343. end
  344.  
  345. function drawMobSpawnerControls ()
  346. local panelX = 81
  347. local panelY  = 37
  348. local panelSizeX = 58
  349. local panelSizeY = 16
  350.  
  351.   drawButton("crushers", panelX+1, panelY+1, 3, toggleCrusher, _, colors_green, colors_green, true)
  352.  
  353.   drawButton("spawner", panelX+12, panelY+1, 3, toggleSpawner, _, colors_green, colors_green, true)
  354. end
  355.  
  356.  
  357. function drawUIForScreen2()
  358.     drawCorridorAccessPanel()
  359.  
  360. end
  361.  
  362. function drawUIForScreen1()
  363.  
  364.   drawCorridorAccessPanel()
  365.  
  366. drawMobSpawnerControlPanel ()
  367.  
  368.    
  369. end
  370.  
  371. function touchCheck()
  372.   touchLocation,touchName, touchX, touchY = event.pull(0.01, "touch")
  373. end
  374.  
  375. -------------/\--------------
  376. ------ functions for UI -----
  377. -----------------------------
  378.  
  379. ----------------------------
  380. --- functions for buttons --
  381. -------------\/-------------
  382.  
  383.  
  384.  
  385. function openCoridorDoor()
  386.  openRedstoneChannel(0)
  387. end
  388. function closeCoridorDoor()
  389.  closeRedstoneChannel(0)
  390. end
  391.  
  392.  
  393. function closeAllRedstoneChannels()
  394.  closeRedstoneChannel(0)
  395.  closeRedstoneChannel(1)
  396.  closeRedstoneChannel(2)
  397.  closeRedstoneChannel(3)
  398.  closeRedstoneChannel(4)
  399.  closeRedstoneChannel(5)
  400.  closeRedstoneChannel(6)
  401.  closeRedstoneChannel(7)
  402.  closeRedstoneChannel(8)
  403.  closeRedstoneChannel(9)
  404.  closeRedstoneChannel(10)
  405.  closeRedstoneChannel(11)
  406.  closeRedstoneChannel(12)
  407.  closeRedstoneChannel(13)
  408.  closeRedstoneChannel(14)
  409.  closeRedstoneChannel(15)
  410. end
  411.  
  412.  
  413.  
  414.  
  415. function getCurrentEssenceInStorage()
  416.   if meFluids[1] == nil then
  417.    return "0"
  418.   else
  419.    return meFluids[1]["amount"]
  420.   end
  421. end
  422.  
  423.  
  424. ------------/\--------------
  425. --- functions for buttons --
  426. ----------------------------
  427.  
  428.  
  429.  
  430.  
  431. clearBothScreens()
  432. closeAllRedstoneChannels()
  433.  
  434. while true do
  435.    
  436.    touchCheck() -- returns x and y coords of cursor when screen is clicked
  437.    swichToScreen1() -- swiches to primary screen
  438.    drawUIForScreen1() -- draws UI for primary screen
  439.  
  440.    if isPlayerInCorridor () then -- if player is in coridor then
  441.     touchCheck() -- returns x and y coords of cursor when screen is clicked
  442.     swichToScreen2() -- swiches to secondary screen
  443.     drawUIForScreen2() -- draws UI for secondary screen
  444.    else
  445.     clearSecondaryScreen()
  446.     swichToScreen1() -- swiches to primary screen
  447.    end
  448.  
  449. drawText(1, 1, tostring(temp15), colors_cyan, colors_black)
  450.  os.sleep(0.0)
  451. end
Add Comment
Please, Sign In to add comment