Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("right")
- local width = 20
- local height = 20
- local delay = 5
- local saveInterval = 10
- local viewX, viewY = 0, 0
- local function createEmptyMatrix()
- local matrix = {}
- return setmetatable(matrix, {
- __index = function(t, k)
- t[k] = {}
- setmetatable(t[k], { __index = function(tt, kk) tt[kk] = 0 return 0 end })
- return t[k]
- end
- })
- end
- local function printMatrix(matrix, viewX, viewY)
- term.clear()
- term.setCursorPos(1, 1)
- local w, h = term.getSize()
- local centerX, centerY = math.floor(w / 2), math.floor(h / 2)
- for y = 1, h do
- for x = 1, w do
- local matX = x - centerX + viewX
- local matY = viewY - (y - centerY)
- if matX == viewX and matY == viewY then
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.red)
- term.write("+")
- else
- local value = matrix[matX][matY]
- term.setBackgroundColor(value == 1 and colors.white or colors.black)
- term.write(" ")
- end
- end
- term.setCursorPos(1, y + 1)
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(w - 10, h)
- term.write(string.format("(%d, %d)", viewX, viewY))
- term.setCursorPos(math.floor(w/2), h - 2)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- term.write("w")
- term.setCursorPos(math.floor(w/2), h - 1)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- term.write("s")
- term.setCursorPos(math.floor(w/2) - 1, h - 1)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- term.write("a")
- term.setCursorPos(math.floor(w/2) + 1, h - 1)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.white)
- term.write("d")
- end
- local function saveMatrix(matrix)
- local filename = string.format("matrix_%d_%d.txt", os.day(), os.time())
- local file = fs.open(filename, "w")
- for x, row in pairs(matrix) do
- for y, value in pairs(row) do
- if value ~= 0 then
- file.writeLine(string.format("%d %d %d", x, y, value))
- end
- end
- end
- file.close()
- end
- local function loadMatrix(filename)
- local matrix = createEmptyMatrix()
- if fs.exists(filename) then
- local file = fs.open(filename, "r")
- while true do
- local line = file.readLine()
- if line == nil then break end
- local x, y, value = line:match("(%-?%d+) (%-?%d+) (%d+)")
- if x and y and value then
- x = tonumber(x)
- y = tonumber(y)
- value = tonumber(value)
- matrix[x][y] = value
- end
- end
- file.close()
- end
- return matrix
- end
- local matrix = loadMatrix("matrix.txt")
- local timers = {}
- local function handleMovement(key)
- if key == keys.w then
- viewY = viewY + 1
- elseif key == keys.s then
- viewY = viewY - 1
- elseif key == keys.a then
- viewX = viewX - 1
- elseif key == keys.d then
- viewX = viewX + 1
- end
- printMatrix(matrix, viewX, viewY)
- end
- printMatrix(matrix, viewX, viewY)
- local saveTimer = os.startTimer(saveInterval)
- while true do
- local event, param1, param2, param3 = os.pullEvent()
- if event == "rednet_message" then
- local senderId, message = param1, param2
- local x, y = message:match("(%-?%d+) (%-?%d+)")
- if x and y then
- x = tonumber(x) / os.day()
- y = tonumber(y) / os.day()
- matrix[x][y] = 1
- printMatrix(matrix, viewX, viewY)
- if timers[x .. "," .. y] then
- os.cancelTimer(timers[x .. "," .. y])
- end
- timers[x .. "," .. y] = os.startTimer(delay)
- else
- print("Incorrect message")
- sleep(5)
- end
- elseif event == "timer" then
- local timerId = param1
- if timerId == saveTimer then
- saveMatrix(matrix)
- saveTimer = os.startTimer(saveInterval)
- else
- for coord, id in pairs(timers) do
- if id == timerId then
- local x, y = coord:match("(%-?%d+),(%-?%d+)")
- x = tonumber(x)
- y = tonumber(y)
- matrix[x][y] = 0
- timers[coord] = nil
- printMatrix(matrix, viewX, viewY)
- break
- end
- end
- end
- elseif event == "key" then
- handleMovement(param1)
- elseif event == "mouse_click" then
- local button, x, y = param1, param2, param3
- local w, h = term.getSize()
- if y == h - 2 and x == math.floor(w/2) then
- handleMovement(keys.w)
- elseif y == h - 1 and x == math.floor(w/2) then
- handleMovement(keys.s)
- elseif y == h - 1 and x == math.floor(w/2) - 1 then
- handleMovement(keys.a)
- elseif y == h - 1 and x == math.floor(w/2) + 1 then
- handleMovement(keys.d)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement