April_The_Sergal

Small Mob Farm

Jul 21st, 2025 (edited)
495
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 1 0
  1. -- Configuration
  2. local MONITOR_SIDE = "top"
  3. local REDSTONE_SIDE = "bottom"
  4. local STATUS_FILE = "farmStatus"
  5.  
  6. -- Colors
  7. local COLOR_RUNNING = colors.red
  8. local COLOR_STOPPED = colors.green
  9. local COLOR_BG = colors.black
  10. local COLOR_TEXT = colors.white
  11. local COLOR_BUTTON = colors.lime
  12. local COLOR_GROUND = colors.purple
  13. local COLOR_CREEPER = colors.lime
  14. local COLOR_X = colors.red
  15.  
  16. -- Monitor setup
  17. local monitor = peripheral.wrap(MONITOR_SIDE)
  18. monitor.setTextScale(1)
  19. local w, h = monitor.getSize()
  20.  
  21. -- State
  22. local farmRunning = false
  23. local creeperX = w - 3
  24. local blink = true
  25.  
  26. -- Save/load
  27. local function saveStatus()
  28.     local f = fs.open(STATUS_FILE, "w")
  29.     f.write(farmRunning and "running" or "stopped")
  30.     f.close()
  31. end
  32.  
  33. local function loadStatus()
  34.     if fs.exists(STATUS_FILE) then
  35.         local f = fs.open(STATUS_FILE, "r")
  36.         local data = f.readAll()
  37.         f.close()
  38.         return data == "running"
  39.     end
  40.     return false
  41. end
  42.  
  43. -- Redstone
  44. local function updateRedstone()
  45.     redstone.setOutput(REDSTONE_SIDE, not farmRunning)
  46. end
  47.  
  48. -- Draw 3-line header with centered label
  49. local function drawHeader()
  50.     local label = farmRunning and "RUNNING" or "STOPPED"
  51.     monitor.setBackgroundColor(farmRunning and COLOR_RUNNING or COLOR_STOPPED)
  52.     monitor.setTextColor(COLOR_TEXT)
  53.     for y = 1, 3 do
  54.         monitor.setCursorPos(1, y)
  55.         monitor.write((" "):rep(w))
  56.     end
  57.     local x = math.floor((w - #label) / 2) + 1
  58.     monitor.setCursorPos(x, 2) -- Middle line
  59.     monitor.write(label)
  60. end
  61.  
  62. -- Button
  63. local function drawButton()
  64.     local label = farmRunning and "STOP" or "START"
  65.     local x = math.floor((w - #label) / 2) + 1
  66.     monitor.setCursorPos(x, 5)
  67.     monitor.setBackgroundColor(COLOR_BUTTON)
  68.     monitor.setTextColor(COLOR_TEXT)
  69.     monitor.write(label)
  70. end
  71.  
  72. -- Button check
  73. local function isButtonPressed(x, y)
  74.     return y == 5
  75. end
  76.  
  77. -- Draw purple ground line at bottom
  78. local function drawGround()
  79.     monitor.setCursorPos(1, h)
  80.     monitor.setTextColor(COLOR_GROUND)
  81.     monitor.setBackgroundColor(COLOR_BG)
  82.     monitor.write(("⯀"):rep(w))
  83. end
  84.  
  85. -- Clear creeper line (above ground)
  86. local function clearCreeperRow()
  87.     monitor.setCursorPos(1, h - 1)
  88.     monitor.setBackgroundColor(COLOR_BG)
  89.     monitor.write((" "):rep(w))
  90. end
  91.  
  92. -- Draw creeper frame
  93. local function drawCreeper()
  94.     clearCreeperRow()
  95.     if creeperX > 3 then
  96.         monitor.setCursorPos(creeperX, h - 1)
  97.         monitor.setTextColor(COLOR_CREEPER)
  98.         monitor.write("C")
  99.     elseif creeperX == 3 then
  100.         monitor.setCursorPos(creeperX, h - 1)
  101.         monitor.setTextColor(COLOR_X)
  102.         monitor.write("X")
  103.         sleep(0.5)
  104.         clearCreeperRow()
  105.     end
  106. end
  107.  
  108. -- Safe to enter (stopped)
  109. local function drawSafeBlink()
  110.     local msg = "SAFE TO ENTER"
  111.     local x = math.floor((w - #msg) / 2) + 1
  112.     monitor.setCursorPos(x, h - 1)
  113.     monitor.setBackgroundColor(COLOR_BG)
  114.     monitor.setTextColor(COLOR_STOPPED)
  115.     if blink then
  116.         monitor.write(msg)
  117.     else
  118.         monitor.write((" "):rep(#msg))
  119.     end
  120. end
  121.  
  122. -- Full screen draw
  123. local function redraw()
  124.     monitor.setBackgroundColor(COLOR_BG)
  125.     monitor.clear()
  126.     drawHeader()
  127.     drawButton()
  128.     drawGround()
  129. end
  130.  
  131. -- Init
  132. farmRunning = loadStatus()
  133. updateRedstone()
  134. redraw()
  135.  
  136. -- Parallel logic
  137. parallel.waitForAny(
  138.     function() -- Animation
  139.         while true do
  140.             if farmRunning then
  141.                 drawGround()
  142.                 drawCreeper()
  143.                 creeperX = creeperX - 1
  144.                 if creeperX < 3 then
  145.                     creeperX = w - 3
  146.                 end
  147.             else
  148.                 drawGround()
  149.                 drawSafeBlink()
  150.                 blink = not blink
  151.             end
  152.             sleep(0.3)
  153.         end
  154.     end,
  155.     function() -- Touch handler
  156.         while true do
  157.             local _, _, x, y = os.pullEvent("monitor_touch")
  158.             if isButtonPressed(x, y) then
  159.                 farmRunning = not farmRunning
  160.                 creeperX = w - 3
  161.                 saveStatus()
  162.                 updateRedstone()
  163.                 redraw()
  164.             end
  165.         end
  166.     end
  167. )
  168.  
Advertisement
Add Comment
Please, Sign In to add comment