Advertisement
davedumas0

mobSpawrControll

Jun 5th, 2023
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.78 KB | None | 0 0
  1. -- Load libraries
  2. local component = require("component")
  3.  
  4. local event = require("event")
  5. local term = require("term")
  6. local serialization = require("serialization")
  7. local rs = component.redstone
  8. local mobChest = "44f855ef-3668-484f-bde5-75b175ef4c5d"
  9. local duplicatorInv = "587a6a6b-21b4-410a-a530-08392b4daa68"
  10.  
  11.  
  12. -- Load peripherals
  13. local chest = component.proxy(mobChest)
  14. local dup = component.proxy(duplicatorInv)
  15. local gpu = component.gpu
  16. local modem = component.modem
  17.  
  18. local colors = {
  19.     ["black"] = 0x000000,
  20.     ["charcoal"] = 0x282828,
  21.     ["darkGray"] = 0x555555,
  22.     ["gray"] = 0x808080,
  23.     ["silver"] = 0xBFBFBF,
  24.     ["lightGray"] = 0xD3D3D3,
  25.     ["ghostWhite"] = 0xF8F8FF,
  26.     ["white"] = 0xFFFFFF,
  27.     ["navy"] = 0x000080,
  28.     ["darkBlue"] = 0x0000AA,
  29.     ["mediumBlue"] = 0x0000CD,
  30.     ["blue"] = 0x0000FF,
  31.     ["dodgerBlue"] = 0x1E90FF,
  32.     ["lightBlue"] = 0xADD8E6,
  33.     ["skyBlue"] = 0x87CEEB,
  34.     ["babyBlue"] = 0x89CFF0,
  35.     ["darkGreen"] = 0x006400,
  36.     ["green"] = 0x008000,
  37.     ["mediumSeaGreen"] = 0x3CB371,
  38.     ["limeGreen"] = 0x32CD32,
  39.     ["lime"] = 0x00FF00,
  40.     ["lightGreen"] = 0x90EE90,
  41.     ["paleGreen"] = 0x98FB98,
  42.     ["teal"] = 0x008080,
  43.     ["darkCyan"] = 0x008B8B,
  44.     ["cyan"] = 0x00FFFF,
  45.     ["mediumAquamarine"] = 0x66CDAA,
  46.     ["aquamarine"] = 0x7FFFD4,
  47.     ["maroon"] = 0x800000,
  48.     ["darkRed"] = 0x8B0000,
  49.     ["red"] = 0xFF0000,
  50.     ["firebrick"] = 0xB22222,
  51.     ["indianRed"] = 0xCD5C5C,
  52.     ["lightCoral"] = 0xF08080,
  53.     ["salmon"] = 0xFA8072,
  54.     ["lightSalmon"] = 0xFFA07A,
  55.     ["darkMagenta"] = 0x8B008B,
  56.     ["darkViolet"] = 0x9400D3,
  57.     ["darkPurple"] = 0x800080,
  58.     ["mediumPurple"] = 0x9370DB,
  59.     ["purple"] = 0x800080,
  60.     ["violet"] = 0xEE82EE,
  61.     ["orchid"] = 0xDA70D6,
  62.     ["thistle"] = 0xD8BFD8,
  63.     ["darkGoldenrod"] = 0xB8860B,
  64.     ["gold"] = 0xFFD700,
  65.     ["khaki"] = 0xF0E68C,
  66.     ["lightGoldenrod"] = 0xFAFAD2,
  67.     ["darkOrange"] = 0xFF8C00,
  68.     ["orange"] = 0xFFA500,
  69.     ["lightOrange"] = 0xFFAD60,
  70.     ["darkYellow"] = 0xDAA520,
  71.     ["yellow"] = 0xFFFF00,
  72.     ["lightYellow"] = 0xFFFFE0,
  73.     ["lemon"] = 0xFFFACD
  74. }
  75.  
  76.  
  77. --╔════════════════════════╗
  78. --║ set/load functions     ║
  79. --╚════════════════════════╝
  80.  
  81. function setBG(color)
  82.     gpu.setBackground(color)
  83. end
  84.  
  85. function setFG(color)
  86.     gpu.setForeground(color)
  87. end
  88.  
  89. function setCursorPos(x, y)
  90.   term.setCursor(x, y)
  91. end
  92.  
  93. function openDoor()
  94.  rs.setBundledOutput(3, 4, 255)
  95.  enableRedLight() -- Enable the red light when the door is open
  96.  diableGreenLight()
  97. end
  98.  
  99. function closeDoor()
  100.   rs.setBundledOutput(3, 4, 0)
  101.   diableRedLight()  -- Disable the red light when the door is closed
  102.   enableGreenLight()
  103. end
  104.  
  105. function enableRedLight()
  106.   rs.setBundledOutput(3, 2, 255)
  107. end
  108.  
  109. function diableRedLight()
  110.   rs.setBundledOutput(3, 2, 0)
  111. end
  112.  
  113. function enableGreenLight()
  114.   rs.setBundledOutput(3, 3, 255)
  115. end
  116.  
  117. function diableGreenLight()
  118.   rs.setBundledOutput(3, 3, 0)
  119. end
  120.  
  121. function enableGrinder()
  122.     if rs.getBundledInput(3, 4) > 0 then
  123.         -- The door is open, so do not enable the grinder
  124.         return
  125.     end
  126.   rs.setBundledOutput(3, 1, 255)
  127. end
  128.  
  129. function diableGrinder()
  130.     if rs.getBundledInput(3, 4) > 0 then
  131.         -- The door is open, so do not enable the duplicator
  132.         return
  133.     end
  134.   rs.setBundledOutput(3, 1, 0)
  135. end
  136.  
  137. function enableDuplicator()
  138.   rs.setBundledOutput(3, 0, 255)
  139. end
  140.  
  141. function disableDuplicator()
  142.   rs.setBundledOutput(3, 0, 0)
  143. end
  144.  
  145. function loadMobDuplicator()
  146.     mobDupPosX = 25
  147.     mobDupPosY = 38
  148.     mobDupSizeX = 20
  149.     mobDupSizeY = 10
  150.     mobDupPrimaryColor = colors["white"]
  151.     mobDupSecondaryColor = colors["cyan"]
  152.  
  153. end
  154.  
  155. function drawMobDuplicator()
  156.  
  157.   renderLine(mobDupPosX, mobDupPosY, mobDupSizeX, mobDupSizeY, mobDupPrimaryColor)
  158.   renderLine(mobDupPosX+1, mobDupPosY, mobDupSizeX-2, 1, colors["teal"])
  159.  
  160.  
  161.   renderLine(mobDupPosX+2, mobDupPosY+1, mobDupSizeX-4, 1, colors["charcoal"])
  162.   renderLine(mobDupPosX+3, mobDupPosY+2, mobDupSizeX-6, 1, colors["charcoal"])
  163.   renderLine(mobDupPosX+4, mobDupPosY+3, mobDupSizeX-8, 1, colors["charcoal"])
  164.   renderLine(mobDupPosX+5, mobDupPosY+4, mobDupSizeX-10, 1, colors["charcoal"])
  165.  
  166.   renderLine(mobDupPosX+5, mobDupPosY+1, 1, mobDupSizeY-6, colors["teal"])
  167.   renderLine(mobDupPosX+14, mobDupPosY+1, 1, mobDupSizeY-6, colors["teal"])
  168.   renderLine(mobDupPosX+6, mobDupPosY+3, 8, 1, colors["teal"])
  169.  
  170. end
  171.  
  172. function updateMobDuplicator()
  173.  
  174. end
  175.  
  176. function loadMobGrinder()
  177.     mobGrindPosX = 25
  178.     mobGrindPosY = 25
  179.     mobGrindSizeX = 20
  180.     mobGrindSizeY = 10
  181.     mobGrindPrimaryColor = colors["white"]
  182.     mobGrindSecondaryColor = colors["cyan"]
  183. end
  184. function drawMobGrinderWheels()
  185.     -- Draw two simple "wheels" in the center of the black box
  186.     -- This assumes that the box is empty apart from the border
  187.     -- Remember, the wheels won't look perfect due to text-based graphics limitations
  188.  
  189.     local wheelSymbol = "*"
  190.     local wheelColor = colors["gray"]
  191.  
  192.     -- Calculate the center positions for the wheels
  193.     local wheel1PosX = mobGrindPosX + 3
  194.     local wheel2PosX = mobGrindPosX + 7
  195.     local wheelPosY = mobGrindPosY + 2
  196.  
  197.     -- Draw the wheels
  198.     renderLine(wheel1PosX, wheelPosY, 1, 1, colors["orchid"])
  199.     --renderLine(wheel1PosX+1, wheelPosY, 1, 1, wheelColor)
  200.     --renderLine(wheel1PosX, wheelPosY+1, 1, 1, wheelColor)
  201.     --renderLine(wheel1PosX+1, wheelPosY+1, 1, 1, wheelColor)
  202.  
  203.     --renderLine(wheel2PosX, wheelPosY, 1, 1, wheelColor)
  204.     --renderLine(wheel2PosX+1, wheelPosY, 1, 1, wheelColor)
  205.     --renderLine(wheel2PosX, wheelPosY+1, 1, 1, wheelColor)
  206.     --renderLine(wheel2PosX+1, wheelPosY+1, 1, 1, wheelColor)
  207. end
  208.  
  209.  
  210.  
  211. function drawMobGrinder()
  212.     renderLine(mobGrindPosX, mobGrindPosY, mobGrindSizeX, mobGrindSizeY, mobGrindPrimaryColor)
  213.     renderLine(mobGrindPosX+1, mobGrindPosY+1, mobGrindSizeX-2, mobGrindSizeY-2, colors["charcoal"])
  214.     renderLine(mobGrindPosX+1, mobGrindPosY+1, 1, 1, colors["white"])
  215.     renderLine(mobGrindPosX+mobGrindSizeX-2, mobGrindPosY+1, 1, 1, colors["white"])
  216.     renderLine(mobGrindPosX+1, mobGrindPosY+mobGrindSizeY-2, 1, 1, colors["white"])
  217.     renderLine(mobGrindPosX+mobGrindSizeX-2, mobGrindPosY+mobGrindSizeY-2, 1, 1, colors["white"])
  218.     drawMobGrinderWheels()
  219. end
  220.  
  221. function updateMobGrinder()
  222.  
  223. end
  224.  
  225. function loadMainPanel()
  226.   mainPanelX = 120
  227.   mainPanelY = 8
  228.   mainPanelSizeX = 32
  229.   mainPanelSizeY = 31
  230.   mainPanelPrimaryColor = colors["aquamarine"]
  231.   mainPanelSecondaryColor = colors["black"]
  232.   mobs = {}
  233.   lookForMobTools()
  234.  
  235. end
  236.  
  237. function lookForMobTools()
  238.     --scan for mob tool
  239.     for slot = 1, 27 do
  240.       local item = chest.getStackInSlot(1, slot)
  241.       if item and item.name == "industrialforegoing:mob_imprisonment_tool" then
  242.         local mobName = item.label:gsub("Mob Imprisonment Tool ", "") -- Remove "Mob Imprisonment Tool "
  243.         if mobName == "(thermalfoundation.blitz)" then
  244.             mobName = "(blitz)"  -- Modify the mobName to just say "blitz"
  245.         end
  246.         table.insert(mobs, mobName)
  247.       elseif item == nil then
  248.         table.insert(mobs, "EMPTY")
  249.       end
  250.     end
  251. end
  252.  
  253.  
  254. function testFunc(text)
  255.   setCursorPos(1, 2)
  256.   setBG(0x000000)
  257.   renderText(1, 2, 0xFF0000, text)
  258. end
  259.  
  260. function buttonNew(label, posX, posY, sizeX, sizeY, ShowLabel, labelColor, buttonColor, func, func_DATA, buttonTable)
  261.  local button = {
  262.     label = label,
  263.     posX =posX,
  264.     posY = posY,
  265.     sizeX = sizeX,
  266.     sizeY = sizeY,
  267.     ShowLabel = ShowLabel,
  268.     labelColor = labelColor,
  269.     buttonColor = buttonColor,
  270.     func = func,
  271.     func_DATA = func_DATA
  272.  }
  273.   table.insert(buttonTable, button)
  274. end
  275.  
  276.  
  277.  
  278. function buttonLoad()
  279.    mainMenuButtons = {}
  280.    buttonNew("open door", 10, 10, 6, 1, true, colors["ghostWhite"], colors["darkBlue"], openDoor, _, mainMenuButtons)
  281.    buttonNew("close door", 10, 12, 6, 1, true, colors["ghostWhite"], colors["darkBlue"], closeDoor, _, mainMenuButtons)
  282.    buttonNew("enable spawner", 10, 14, 6, 1, true, colors["ghostWhite"], colors["darkBlue"], enableDuplicator, _, mainMenuButtons)
  283.    buttonNew("disable spawner", 10, 16, 6, 1, true, colors["ghostWhite"], colors["darkBlue"], disableDuplicator, _, mainMenuButtons)
  284.    buttonNew("enable grinder", 10, 18, 6, 1, true, colors["ghostWhite"], colors["darkBlue"], enableGrinder, _, mainMenuButtons)
  285.    buttonNew("disable grinder", 10, 20, 6, 1, true, colors["ghostWhite"], colors["darkBlue"], diableGrinder, _, mainMenuButtons)
  286.     local line = 1
  287.     for i =1, #mobs do
  288.        
  289.  
  290.         buttonNew(mobs[i], mainPanelX+1, mainPanelY+i, string.len(mobs[i])+2, 1, true, colors["black"],  colors["white"], testFunc, mobs[i], mainMenuButtons)
  291.     end
  292. end
  293.  
  294. function buttonDraw(buttonTable)
  295.  for i = 1, #buttonTable do
  296.     renderLine(buttonTable[i].posX, buttonTable[i].posY+1, string.len(buttonTable[i].label)+2, buttonTable[i].sizeY, buttonTable[i].buttonColor)
  297.     renderText(buttonTable[i].posX+1, buttonTable[i].posY+1, buttonTable[i].labelColor, buttonTable[i].label)
  298.  end
  299. end
  300.  
  301.  
  302.  
  303.  
  304. --╔════════════════════════╗
  305. --║ set/load functions     ║
  306. --╚════════════════════════╝
  307.  
  308. --╔════════════════════════╗
  309. --║ ui/draw Functions      ║
  310. --╚════════════════════════╝
  311.  
  312. function renderText(x, y, TextColor, text)
  313.   setCursorPos(x, y)
  314.   setFG(TextColor)
  315.   term.write(text)
  316. end
  317.  
  318. function renderLine (x, y, w, h, color)
  319.   setBG(color)
  320.   gpu.fill(x, y, w, h, " ")
  321. end
  322.  
  323. function drawLine(x0, y0, x1, y1, color)
  324.     local dx = math.abs(x1 - x0)
  325.     local sx = x0 < x1 and 1 or -1
  326.     local dy = -math.abs(y1 - y0)
  327.     local sy = y0 < y1 and 1 or -1
  328.     local err = dx + dy  -- error value e_xy
  329.  
  330.     while true do
  331.         renderLine(x0, y0, 1, 1, 0xff00ff)
  332.         if x0 == x1 and y0 == y1 then break end
  333.         local e2 = 2 * err
  334.         if e2 >= dy then
  335.             err = err + dy
  336.             x0 = x0 + sx
  337.         end
  338.         if e2 <= dx then
  339.             err = err + dx
  340.             y0 = y0 + sy
  341.         end
  342.     end
  343. end
  344.  
  345.  
  346. --Progress Bar          
  347.  
  348.  
  349. ProgressBar = {}
  350. ProgressBar.__index = ProgressBar
  351.  
  352. -- create a new progress bar
  353. function ProgressBar.new(id, maxVal, len, color)
  354.     local self = setmetatable({}, ProgressBar)
  355.     self.id = id
  356.     self.maxVal = maxVal
  357.     self.currVal = 0
  358.     self.len = len
  359.     self.color = color
  360.     return self
  361. end
  362.  
  363. -- update progress bar value
  364. function ProgressBar:update(val)
  365.     if val > self.maxVal then
  366.         self.currVal = self.maxVal
  367.     else
  368.         self.currVal = val
  369.     end
  370.     self:draw()
  371. end
  372.  
  373. -- draw the progress bar
  374. function ProgressBar:draw()
  375.     local filledLen = math.floor((self.currVal / self.maxVal) * self.len)
  376.     local unfilledLen = self.len - filledLen
  377.  
  378.     -- draw filled part
  379.     renderLine(1, self.id, filledLen, 1, self.color)
  380.  
  381.     -- draw unfilled part
  382.     if unfilledLen > 0 then
  383.         renderLine(filledLen + 1, self.id, unfilledLen, 1, 0xFFFFFF) -- assuming 0xFFFFFF is background color
  384.     end
  385. end
  386.  
  387. -- handle multiple progress bars
  388. ProgressBarManager = {}
  389. ProgressBarManager.bars = {}
  390.  
  391. -- add a new progress bar
  392. function ProgressBarManager.addBar(id, maxVal, len, color)
  393.     local bar = ProgressBar.new(id, maxVal, len, color)
  394.     ProgressBarManager.bars[id] = bar
  395. end
  396.  
  397. -- update a specific progress bar
  398. function ProgressBarManager.updateBar(id, val)
  399.     if ProgressBarManager.bars[id] then
  400.         ProgressBarManager.bars[id]:update(val)
  401.     else
  402.         print("Invalid progress bar ID.")
  403.     end
  404. end
  405.  
  406. function drawMainPanel()
  407.   renderLine(1, 1, 160, 50, mainPanelPrimaryColor)
  408.   renderLine(mainPanelX, mainPanelY, mainPanelSizeX, mainPanelSizeY, mainPanelSecondaryColor)
  409.  
  410. end
  411.  
  412.  
  413.  
  414. --╔════════════════════════╗
  415. --║ ui/draw Functions      ║
  416. --╚════════════════════════╝
  417.  
  418.  
  419.  
  420.  
  421.  
  422. --╔══════════════════════╗
  423. --║ LOOP functions       ║
  424. --╚══════════════════════╝
  425.  
  426.  
  427.  
  428. -- Check for various events and handle them accordingly
  429. function checkForEvents()
  430.   -- Open channel 5 on the modem
  431.   --modem.open(5)
  432.   -- Wait for the next event to occur and store the event information in eventData
  433.   local eventData = {event.pull(0.1)}
  434.   -- Check the type of event
  435.   if eventData[1] == "key_down" then
  436.     -- Handle keyboard events
  437.     -- eventData[2] will contain the ASCII value of the key that was pressed
  438.   elseif eventData[1] == "touch" then
  439.     -- Handle touch events
  440.     -- eventData[3] and eventData[4] will contain the X and Y coordinates of the touch event, respectively
  441.     buttonCheck(eventData[3], eventData[4], mainMenuButtons)
  442.   elseif eventData[1] == "modem_message" then
  443.     -- Handle modem message events
  444.     -- eventData[6] will contain the message sent over the modem
  445.     local message = serialization.unserialize(eventData[6])
  446.     if type(message) == "table" then  
  447.    
  448.     end
  449.   end
  450.  
  451.   -- Return true to continue checking for events
  452.   return true
  453. end
  454.  
  455. -- Check for button presses
  456. function buttonCheck(posX, posY, buttonTable)
  457.   -- t is a flag to keep track of button presses
  458.   local t = false
  459.  
  460.   -- Loop through all buttons in the buttonTable
  461.   for i = 1, #buttonTable do
  462.     -- Check if the touch event is within the bounds of the button
  463.     local buttonPosX = posX >= buttonTable[i].posX and posX <= buttonTable[i].posX + buttonTable[i].sizeX
  464.     local buttonPosY = posY >= buttonTable[i].posY and posY <= buttonTable[i].posY + buttonTable[i].sizeY
  465.    
  466.     -- If the touch event is within the bounds of the button
  467.     if buttonPosX and buttonPosY then
  468.       -- If the flag t is not set
  469.       if not t then
  470.         -- Set the flag t to true
  471.         t = true
  472.        
  473.         -- Call the function stored in the button's `func` field and pass it the data stored in the `func_DATA` field
  474.         buttonTable[i].func(buttonTable[i].func_DATA)
  475.       end
  476.     else
  477.       -- If the touch event is not within the bounds of the button, set the flag t to false
  478.       t = false
  479.     end
  480.   end
  481. end
  482.  
  483.  
  484. function updateMainPanel()
  485.    
  486. end
  487.  
  488.  
  489. --╔══════════════════════╗
  490. --║ LOOP functions       ║
  491. --╚══════════════════════╝
  492.  
  493.  
  494. --╔══════════════════════╗
  495. --║ MAIN functions       ║
  496. --╚══════════════════════╝
  497.  
  498.  
  499. function load()
  500.   -- This is where you would put any setup code that needs to run before the main loop starts.
  501.   -- For example, you might want to clear the terminal screen:
  502.   term.clear()
  503.   loadMainPanel()
  504.   buttonLoad()
  505.   loadMobDuplicator()
  506.   loadMobGrinder()
  507.  
  508. end
  509.  
  510. function update()
  511.     checkForEvents()
  512.     updateMainPanel()
  513.     updateMobDuplicator()
  514.    
  515.  
  516. end
  517.  
  518. function draw()
  519.   -- This is where you would put any code that needs to update the display.
  520.   -- For example, you might want to print a message to the screen:
  521.   renderText(1, 1, 0xFFFFFF, "Listening for events...")
  522.   drawMainPanel()
  523.   buttonDraw(mainMenuButtons)
  524.   drawMobDuplicator()
  525.   drawMobGrinder()
  526. end
  527.  
  528. --╔══════════════════════╗
  529. --║ MAIN functions       ║
  530. --╚══════════════════════╝
  531.  
  532.  
  533. function main()
  534.   load()
  535.   draw()
  536.   while true do
  537.     update()
  538.    
  539.     os.sleep(0.0)  -- Add a small delay to prevent the program from using 100% CPU.
  540.   end
  541. end
  542.  
  543. -- Start the program
  544. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement