Advertisement
SwellzD

chunk_spy_server

Jul 11th, 2024 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.31 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3.  
  4. local width = 20
  5. local height = 20
  6.  
  7.  
  8. local delay = 5
  9. local saveInterval = 10
  10.  
  11. local viewX, viewY = 0, 0
  12.  
  13. local function createEmptyMatrix()
  14.     local matrix = {}
  15.     return setmetatable(matrix, {
  16.         __index = function(t, k)
  17.             t[k] = {}
  18.             setmetatable(t[k], { __index = function(tt, kk) tt[kk] = 0 return 0 end })
  19.             return t[k]
  20.         end
  21.     })
  22. end
  23.  
  24. local function printMatrix(matrix, viewX, viewY)
  25.     term.clear()
  26.     term.setCursorPos(1, 1)
  27.     local w, h = term.getSize()
  28.     local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
  29.     for y = 1, h do
  30.         for x = 1, w do
  31.             local matX = x - centerX + viewX
  32.             local matY = viewY - (y - centerY)
  33.             if matX == viewX and matY == viewY then
  34.                 term.setBackgroundColor(colors.gray)
  35.                 term.setTextColor(colors.red)
  36.                 term.write("+")
  37.             else
  38.                 local value = matrix[matX][matY]
  39.                 term.setBackgroundColor(value == 1 and colors.white or colors.black)
  40.                 term.write(" ")
  41.             end
  42.         end
  43.         term.setCursorPos(1, y + 1)
  44.     end
  45.     term.setBackgroundColor(colors.black)
  46.     term.setTextColor(colors.white)
  47.     term.setCursorPos(w - 10, h)
  48.     term.write(string.format("(%d, %d)", viewX, viewY))
  49.    
  50.     term.setCursorPos(math.floor(w/2), h - 2)
  51.     term.setBackgroundColor(colors.lightGray)
  52.     term.setTextColor(colors.white)
  53.     term.write("w")
  54.  
  55.     term.setCursorPos(math.floor(w/2), h - 1)
  56.     term.setBackgroundColor(colors.lightGray)
  57.     term.setTextColor(colors.white)
  58.     term.write("s")
  59.  
  60.     term.setCursorPos(math.floor(w/2) - 1, h - 1)
  61.     term.setBackgroundColor(colors.lightGray)
  62.     term.setTextColor(colors.white)
  63.     term.write("a")
  64.  
  65.     term.setCursorPos(math.floor(w/2) + 1, h - 1)
  66.     term.setBackgroundColor(colors.lightGray)
  67.     term.setTextColor(colors.white)
  68.     term.write("d")
  69. end
  70.  
  71. local function saveMatrix(matrix)
  72.     local filename = string.format("matrix_%d_%d.txt", os.day(), os.time())
  73.     local file = fs.open(filename, "w")
  74.     for x, row in pairs(matrix) do
  75.         for y, value in pairs(row) do
  76.             if value ~= 0 then
  77.                 file.writeLine(string.format("%d %d %d", x, y, value))
  78.             end
  79.         end
  80.     end
  81.     file.close()
  82. end
  83.  
  84. local function loadMatrix(filename)
  85.     local matrix = createEmptyMatrix()
  86.     if fs.exists(filename) then
  87.         local file = fs.open(filename, "r")
  88.         while true do
  89.             local line = file.readLine()
  90.             if line == nil then break end
  91.             local x, y, value = line:match("(%-?%d+) (%-?%d+) (%d+)")
  92.             if x and y and value then
  93.                 x = tonumber(x)
  94.                 y = tonumber(y)
  95.                 value = tonumber(value)
  96.                 matrix[x][y] = value
  97.             end
  98.         end
  99.         file.close()
  100.     end
  101.     return matrix
  102. end
  103.  
  104. local matrix = loadMatrix("matrix.txt")
  105. local timers = {}
  106.  
  107. local function handleMovement(key)
  108.     if key == keys.w then
  109.         viewY = viewY + 1
  110.     elseif key == keys.s then
  111.         viewY = viewY - 1
  112.     elseif key == keys.a then
  113.         viewX = viewX - 1
  114.     elseif key == keys.d then
  115.         viewX = viewX + 1
  116.     end
  117.     printMatrix(matrix, viewX, viewY)
  118. end
  119.  
  120. printMatrix(matrix, viewX, viewY)
  121. local saveTimer = os.startTimer(saveInterval)
  122. while true do
  123.     local event, param1, param2, param3 = os.pullEvent()
  124.    
  125.     if event == "rednet_message" then
  126.         local senderId, message = param1, param2
  127.         local x, y = message:match("(%-?%d+) (%-?%d+)")
  128.         if x and y then
  129.             x = tonumber(x) / os.day()
  130.             y = tonumber(y) / os.day()
  131.            
  132.             matrix[x][y] = 1
  133.             printMatrix(matrix, viewX, viewY)
  134.            
  135.             if timers[x .. "," .. y] then
  136.                 os.cancelTimer(timers[x .. "," .. y])
  137.             end
  138.             timers[x .. "," .. y] = os.startTimer(delay)
  139.         else
  140.             print("Incorrect message")
  141.             sleep(5)
  142.         end
  143.     elseif event == "timer" then
  144.         local timerId = param1
  145.        
  146.         if timerId == saveTimer then
  147.             saveMatrix(matrix)
  148.             saveTimer = os.startTimer(saveInterval)
  149.         else
  150.             for coord, id in pairs(timers) do
  151.                 if id == timerId then
  152.                     local x, y = coord:match("(%-?%d+),(%-?%d+)")
  153.                     x = tonumber(x)
  154.                     y = tonumber(y)
  155.                     matrix[x][y] = 0
  156.                     timers[coord] = nil
  157.                     printMatrix(matrix, viewX, viewY)
  158.                     break
  159.                 end
  160.             end
  161.         end
  162.     elseif event == "key" then
  163.         handleMovement(param1)
  164.     elseif event == "mouse_click" then
  165.         local button, x, y = param1, param2, param3
  166.         local w, h = term.getSize()
  167.         if y == h - 2 and x == math.floor(w/2) then
  168.             handleMovement(keys.w)
  169.         elseif y == h - 1 and x == math.floor(w/2) then
  170.             handleMovement(keys.s)
  171.         elseif y == h - 1 and x == math.floor(w/2) - 1 then
  172.             handleMovement(keys.a)
  173.         elseif y == h - 1 and x == math.floor(w/2) + 1 then
  174.             handleMovement(keys.d)
  175.         end
  176.     end
  177. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement