Advertisement
billysback

mapper

Jul 10th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. local sw,sh = term.getSize()
  2.  
  3. local function createMap(width, height, dir)
  4.     local map = {}
  5.     map.width = width
  6.     map.height = height
  7.     map.dir = dir
  8.     map.char = " "
  9.     map.chars = " "
  10.     map.cur = 1
  11.     map.offset = {0,0}
  12.     for x=1,width do
  13.         map[x] = {}
  14.         for y=1,height do
  15.             map[x][y] = " "
  16.         end
  17.     end
  18.    
  19.     map.draw = function(self)
  20.         term.setCursorPos(1,1)
  21.         term.write(self.char)
  22.         local width = self.width
  23.         local height = self.height
  24.         if width > sw then width = sw end
  25.         if height > sh then height = sh end
  26.         for x=1+map.offset[1],width+map.offset[1] do
  27.             for y=1+map.offset[2],height+map.offset[2] do
  28.                 term.setCursorPos(x-map.offset[1],y + 1 - map.offset[2])
  29.                 term.setBackgroundColor(colors.white)
  30.                 term.setTextColor(colors.black)
  31.                 term.write(self[x][y])
  32.             end
  33.         end
  34.     end
  35.    
  36.     map.save = function(self)
  37.         local exists = fs.exists(self.dir)
  38.         local file = fs.open(self.dir, "a")
  39.         local maptab = {}
  40.         for x=1,self.width do
  41.             for y=1,self.width do
  42.                 if maptab[y] == nil then maptab[y] = self[x][y] else
  43.                     maptab[y] = maptab[y]..self[x][y]
  44.                 end
  45.             end
  46.         end
  47.         if exists then file.writeLine("") end
  48.         for i=1,#maptab do
  49.             file.writeLine(maptab[i])
  50.         end
  51.         file.writeLine("###")
  52.         file.close()
  53.         term.setCursorPos(2,1)
  54.         term.write("SAVED TO:"..self.dir)
  55.     end
  56.    
  57.     map.move = function(self, dx, dy)
  58.         local ox = self.offset[1] + dx
  59.         local oy = self.offset[2] + dy
  60.         if ox < 0 then ox = 0 end
  61.         if self.width > sw then
  62.             if ox + sw > self.width then ox = self.width - sw end
  63.         else
  64.             ox = 0
  65.         end
  66.         if oy < 0 then oy = 0 end
  67.         if self.height > sh then
  68.             if oy + sh - 1 > self.height then oy = self.width - sh + 1 end
  69.         else
  70.             oy = 0
  71.         end
  72.         self.offset[1] = ox
  73.         self.offset[2] = oy
  74.         self:draw()
  75.     end
  76.     return map
  77. end
  78.  
  79. local stuff = {...}
  80. if #stuff < 3 then
  81.     print("Usage SAD: mapper [dir] [width] [height]")
  82. else
  83.     local dir = stuff[1]
  84.     if dir:sub(-4,-1) ~= ".nmp" then dir = dir..".nmp" end
  85.     local width = tonumber(stuff[2])
  86.     local height = tonumber(stuff[3])
  87.     local map = createMap(width, height, dir)
  88.     term.setBackgroundColor(colors.black)
  89.     term.clear()
  90.     term.setCursorPos(1,1)
  91.     map:draw()
  92.     local on = true
  93.     while on do
  94.         local event,p1,p2,p3 = os.pullEvent()
  95.         if event == "key" then
  96.             term.setTextColor(colors.white)
  97.             term.setBackgroundColor(colors.black)
  98.             term.setCursorPos(2,1)
  99.             print("KEY_PRESSED")
  100.             if p1 == 56 then --alt
  101.                 term.setCursorPos(2,1)
  102.                 term.write("SAVING")
  103.                 map:save()
  104.             elseif p1 == 29 then --left ctrl
  105.                 on = false
  106.             elseif p1 == 15 then --tab
  107.                 --term.setCursorPos(2, 1)
  108.                 --term.write("REFRESHING")
  109.                 map = createMap(width, height, dir)
  110.                 map:draw()
  111.             elseif p1 == 157 then --right ctrl
  112.                 term.setCursorPos(1,1)
  113.                 print("Chars:")
  114.                 term.setCursorPos(7,1)
  115.                 local chars = read()
  116.                 map.chars = chars
  117.                 map.cur = 1
  118.                 term.setCursorPos(1,1)
  119.                 term.setBackgroundColor(colors.black)
  120.                 print("                                                  ")
  121.             elseif p1 == 200 then --up
  122.                 map:move(0, -1)
  123.             elseif p1 == 203 then --left
  124.                 map:move(-1, 0)
  125.             elseif p1 == 208 then --down
  126.                 map:move(0, 1)
  127.             elseif p1 == 205 then --right
  128.                 map:move(1, 0)
  129.             end
  130.         elseif event == "char" then
  131.             map.char = p1
  132.             map:draw()
  133.         elseif event == "mouse_click" or event == "mouse_drag" then
  134.             local x = p2 + map.offset[1]
  135.             local y = p3 - 1 + map.offset[2]
  136.             if map[x] ~= nil then
  137.                 if map[x][y] ~= nil then
  138.                     map[x][y] = map.char
  139.                     map:draw()
  140.                 end
  141.             end
  142.         elseif event == "mouse_scroll" then
  143.             local dir = p1
  144.             map.cur = map.cur + dir
  145.             if map.cur > string.len(map.chars) then map.cur = 1 end
  146.             if map.cur < 1 then map.cur = string.len(map.chars) end
  147.             map.char = map.chars:sub(map.cur, map.cur)
  148.             map:draw()
  149.         end
  150.     end
  151.     term.setBackgroundColor(colors.black)
  152.     term.clear()
  153.     term.setCursorPos(1,1)
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement