Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sw,sh = term.getSize()
- local function createMap(width, height, dir)
- local map = {}
- map.width = width
- map.height = height
- map.dir = dir
- map.char = " "
- map.chars = " "
- map.cur = 1
- map.offset = {0,0}
- for x=1,width do
- map[x] = {}
- for y=1,height do
- map[x][y] = " "
- end
- end
- map.draw = function(self)
- term.setCursorPos(1,1)
- term.write(self.char)
- local width = self.width
- local height = self.height
- if width > sw then width = sw end
- if height > sh then height = sh end
- for x=1+map.offset[1],width+map.offset[1] do
- for y=1+map.offset[2],height+map.offset[2] do
- term.setCursorPos(x-map.offset[1],y + 1 - map.offset[2])
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.write(self[x][y])
- end
- end
- end
- map.save = function(self)
- local exists = fs.exists(self.dir)
- local file = fs.open(self.dir, "a")
- local maptab = {}
- for x=1,self.width do
- for y=1,self.width do
- if maptab[y] == nil then maptab[y] = self[x][y] else
- maptab[y] = maptab[y]..self[x][y]
- end
- end
- end
- if exists then file.writeLine("") end
- for i=1,#maptab do
- file.writeLine(maptab[i])
- end
- file.writeLine("###")
- file.close()
- term.setCursorPos(2,1)
- term.write("SAVED TO:"..self.dir)
- end
- map.move = function(self, dx, dy)
- local ox = self.offset[1] + dx
- local oy = self.offset[2] + dy
- if ox < 0 then ox = 0 end
- if self.width > sw then
- if ox + sw > self.width then ox = self.width - sw end
- else
- ox = 0
- end
- if oy < 0 then oy = 0 end
- if self.height > sh then
- if oy + sh - 1 > self.height then oy = self.width - sh + 1 end
- else
- oy = 0
- end
- self.offset[1] = ox
- self.offset[2] = oy
- self:draw()
- end
- return map
- end
- local stuff = {...}
- if #stuff < 3 then
- print("Usage SAD: mapper [dir] [width] [height]")
- else
- local dir = stuff[1]
- if dir:sub(-4,-1) ~= ".nmp" then dir = dir..".nmp" end
- local width = tonumber(stuff[2])
- local height = tonumber(stuff[3])
- local map = createMap(width, height, dir)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- map:draw()
- local on = true
- while on do
- local event,p1,p2,p3 = os.pullEvent()
- if event == "key" then
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(2,1)
- print("KEY_PRESSED")
- if p1 == 56 then --alt
- term.setCursorPos(2,1)
- term.write("SAVING")
- map:save()
- elseif p1 == 29 then --left ctrl
- on = false
- elseif p1 == 15 then --tab
- --term.setCursorPos(2, 1)
- --term.write("REFRESHING")
- map = createMap(width, height, dir)
- map:draw()
- elseif p1 == 157 then --right ctrl
- term.setCursorPos(1,1)
- print("Chars:")
- term.setCursorPos(7,1)
- local chars = read()
- map.chars = chars
- map.cur = 1
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- print(" ")
- elseif p1 == 200 then --up
- map:move(0, -1)
- elseif p1 == 203 then --left
- map:move(-1, 0)
- elseif p1 == 208 then --down
- map:move(0, 1)
- elseif p1 == 205 then --right
- map:move(1, 0)
- end
- elseif event == "char" then
- map.char = p1
- map:draw()
- elseif event == "mouse_click" or event == "mouse_drag" then
- local x = p2 + map.offset[1]
- local y = p3 - 1 + map.offset[2]
- if map[x] ~= nil then
- if map[x][y] ~= nil then
- map[x][y] = map.char
- map:draw()
- end
- end
- elseif event == "mouse_scroll" then
- local dir = p1
- map.cur = map.cur + dir
- if map.cur > string.len(map.chars) then map.cur = 1 end
- if map.cur < 1 then map.cur = string.len(map.chars) end
- map.char = map.chars:sub(map.cur, map.cur)
- map:draw()
- end
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement