Advertisement
billysback

Pixel Boy Map Editor

Jun 3rd, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.66 KB | None | 0 0
  1.  
  2. local function addFunctions(map)
  3.     map.save = function(self)
  4.         term.setBackgroundColor(colors.black)
  5.         term.setTextColor(colors.white)
  6.         local lines = {}
  7.         lines[#lines + 1] = "local "..self.name.." = {"
  8.         local mapl = {}
  9.         for x=1,#self do
  10.             local tab = self[x]
  11.             for y=1,#tab do
  12.                 if mapl[y] == nil then mapl[y] = "" end
  13.                 mapl[y] = mapl[y]..self[x][y]
  14.             end
  15.         end
  16.         for i=1,#mapl do
  17.             lines[#lines + 1] = '"'..mapl[i]..'"'
  18.             if i ~= #mapl then
  19.                 lines[#lines] = lines[#lines]..","
  20.             end
  21.         end
  22.         lines[#lines + 1] = "}"
  23.         lines[#lines + 1] = self.name..".pos = {"..self.start[1]..","..self.start[2].."}"
  24.         lines[#lines + 1] = self.name..".vision = 4"
  25.         lines[#lines + 1] = self.name..".convert = {"
  26.         lines[#lines + 1] = " ["..'"'.."-"..'"'.."] = 0,"
  27.         lines[#lines + 1] = " ["..'"'.."+"..'"'.."] = 1,"
  28.         lines[#lines + 1] = " ["..'"'.."#"..'"'.."] = 2,"
  29.         lines[#lines + 1] = " ["..'"'.."*"..'"'.."] = 3,"
  30.         lines[#lines + 1] = " ["..'"'..":"..'"'.."] = 4,"
  31.         lines[#lines + 1] = " ["..'"'.."@"..'"'.."] = 5,"
  32.         lines[#lines + 1] = " ["..'"'.."~"..'"'.."] = 6,"
  33.         lines[#lines + 1] = " ["..'"'.."0"..'"'.."] = 0,"
  34.         lines[#lines + 1] = " ["..'"'.."1"..'"'.."] = 1,"
  35.         lines[#lines + 1] = " ["..'"'.."2"..'"'.."] = 2,"
  36.         lines[#lines + 1] = " ["..'"'.."3"..'"'.."] = 3,"
  37.         lines[#lines + 1] = " ["..'"'.."4"..'"'.."] = 4,"
  38.         lines[#lines + 1] = " ["..'"'.."5"..'"'.."] = 5,"
  39.         lines[#lines + 1] = " ["..'"'.."~"..'"'.."] = 6"
  40.         lines[#lines + 1] = "}"
  41.         lines[#lines + 1] = self.name..".texts = {"
  42.         for i=1,#self.texts do
  43.             local info = self.texts[i]
  44.             local pos = info[1]
  45.             local text = info[2]
  46.             lines[#lines + 1] = "{{"..pos[1]..","..pos[2].."}, "..'"'..text..'"'.."},"
  47.         end
  48.         lines[#lines + 1] = "}"
  49.         lines[#lines + 1] = self.name..".keys = {"
  50.         for i=1,#self.keys do
  51.             local info = self.keys[i]
  52.             local pos1 = info[1]
  53.             local pos2 = info[2]
  54.             local typ = info[3]
  55.             local conv = "-+#*:@"
  56.             local ityp = 0
  57.             for i=1,string.len(conv) do if conv:sub(i, i) == typ then ityp = (i-1) end end
  58.             lines[#lines + 1] = "{{"..pos1[1]..","..pos1[2].."}, {"..pos2[1]..","..pos2[2].."}, "..ityp.."},"
  59.         end
  60.         lines[#lines + 1] = "}"
  61.         lines[#lines + 1] = self.name..".ikeys = {"
  62.         for i=1,#self.ikeys do
  63.             local info = self.ikeys[i]
  64.             local pos1 = info[1]
  65.             local pos2 = info[2]
  66.             local typ = info[3]
  67.             local iname = info[4]
  68.             local conv = "-+#*:@"
  69.             local ityp = 0
  70.             for i=1,string.len(conv) do if conv:sub(i, i) == typ then ityp = (i-1) end end
  71.             lines[#lines + 1] = "{{"..pos1[1]..","..pos1[2].."}, {"..pos2[1]..","..pos2[2].."}, "..ityp..", itemList["..'"'..iname..'"'.."].info},"
  72.         end
  73.         lines[#lines + 1] = "}"
  74.         lines[#lines + 1] = self.name..".items = {"
  75.         for i=1,#self.items do
  76.             local info = self.items[i]
  77.             local pos = info[1]
  78.             local name = info[2]
  79.             lines[#lines + 1] = "{{"..pos[1]..","..pos[2].."}, itemList["..'"'..name..'"'.."]},"
  80.         end
  81.         lines[#lines + 1] = "}"
  82.         lines[#lines + 1] = self.name..".tele = {"
  83.         for i=1,#self.doors do
  84.             local info = self.doors[i]
  85.             local pos = info[1]
  86.             local mname = info[2]
  87.             local spos = info[3]
  88.             lines[#lines + 1] = "{{"..pos[1]..","..pos[2].."}, "..mname..", {"..spos[1]..","..spos[2].."}},"
  89.         end
  90.         lines[#lines + 1] = "}"
  91.         return lines
  92.     end
  93.     map.paint = function(self, x, y)
  94.         local pos = {x+self.offset[1], y+self.offset[2]}
  95.         if self[pos[1]] ~= nil then
  96.             if self[pos[1]][pos[2]] ~= nil then
  97.                 self[pos[1]][pos[2]] = self.brs:sub(self.brush, self.brush)
  98.             end
  99.         end
  100.     end
  101.     map.changeBrush = function(self, i)
  102.         self.brush = self.brush + i
  103.         if self.brush < 1 then self.brush = string.len(self.brs) end
  104.         if self.brush > string.len(self.brs) then self.brush = 1 end
  105.     end
  106.     map.draw = function(self, pos)
  107.         term.setBackgroundColor(colors.black)
  108.         term.setTextColor(colors.white)
  109.         term.clear()
  110.         term.setCursorPos(1,1)
  111.         if self.color == 3 then
  112.             local tabs = {map.texts, map.keys, map.doors, map.items, map.ikeys}
  113.             local tcolor = {colors.white, colors.lightBlue, colors.brown, colors.orange, colors.blue}
  114.            
  115.             for k=1,#tabs do
  116.                 local tab = tabs[k]
  117.                 local col = tcolor[k]
  118.                 --local text = " "
  119.                 local ntab = {}
  120.                 for i=1,#tab do
  121.                     text = " "
  122.                     if k == 2 or k == 5 then text = tostring(i) end
  123.                     local info = tab[i]
  124.                     local ipos = info[1]
  125.                     if ipos[1] >= (self.offset[1]+1) and ipos[1] <= (pos[1]+self.screen[1]+self.offset[1]) and ipos[2] >= (self.offset[2]+1) and ipos[2] <= (pos[2]+self.screen[2]+self.offset[2]) then
  126.                         term.setCursorPos(pos[1]+ipos[1]-self.offset[1], pos[2]+ipos[2]-self.offset[2])
  127.                         term.setBackgroundColor(col)
  128.                         term.setTextColor(colors.black)
  129.                         term.write(text)
  130.                     end
  131.                     if k == 2 or k == 5 then
  132.                         ipos = info[2]
  133.                         if ipos[1] >= (self.offset[1]+1) and ipos[1] <= (pos[1]+self.screen[1]+self.offset[1]) and ipos[2] >= (self.offset[2]+1) and ipos[2] <= (pos[2]+self.screen[2]+self.offset[2]) then
  134.                             term.setCursorPos(pos[1]+ipos[1]-self.offset[1], pos[2]+ipos[2]-self.offset[2])
  135.                             term.setBackgroundColor(col)
  136.                             term.setTextColor(colors.black)
  137.                             term.write(text)
  138.                         end
  139.                     end
  140.                 end
  141.             end
  142.         else
  143.             for x=self.offset[1]+1,pos[1]+self.screen[1]+self.offset[1] do
  144.                 for y=self.offset[2]+1,pos[2]+self.screen[2]+self.offset[2] do
  145.                     if self[x] ~= nil then
  146.                         if self[x][y] ~= nil then
  147.                             local char = self[x][y]
  148.                             term.setCursorPos(pos[1]+x-self.offset[1], pos[2]+y-self.offset[2])
  149.                             local bcol = colors.white
  150.                             local tcol = colors.black
  151.                             local text = char
  152.                             if self.color == 2 then
  153.                                 bcol = self.convert[char][1]
  154.                                 tcol = self.convert[char][2]
  155.                                 text = self.convert[char][3]
  156.                             end
  157.                             term.setBackgroundColor(bcol)
  158.                             term.setTextColor(tcol)
  159.                             term.write(text)
  160.                         end
  161.                     end
  162.                 end
  163.             end
  164.         end
  165.         term.setCursorPos(1,1)
  166.         term.setBackgroundColor(colors.orange)
  167.         term.setTextColor(colors.black)
  168.         term.write(self.brs:sub(self.brush, self.brush))
  169.     end
  170.     map.scroll = function(self, dir)
  171.         local noff = {self.offset[1]+dir[1], self.offset[2]+dir[2]}
  172.         if noff[1] < 1 then noff[1] = 1 end
  173.         if noff[2] < 1 then noff[2] = 1 end
  174.         if noff[1]+self.screen[1] > self.width then noff[1] = self.width-self.screen[1] end
  175.         if noff[2]+self.screen[2] > self.height then noff[2] = self.height-self.screen[2] end
  176.         self.offset = noff
  177.     end
  178. end
  179.  
  180. local function getLines(dir)
  181.     local lines = nil
  182.     if fs.exists(dir) then
  183.         lines = {}
  184.         local file = fs.open(dir, "r")
  185.         local line = file.readLine()
  186.         while line ~= nil do
  187.             lines[#lines + 1] = line
  188.             line = file.readLine()
  189.         end
  190.         file.close()
  191.     end
  192.     return lines
  193. end
  194.  
  195. local function getPos(line, start)
  196.     if line:sub(start, start) == "{" then
  197.         local x1 = start+1
  198.         local x2 = x1
  199.         while line:sub(x2, x2) ~= "," do
  200.             x2 = x2 + 1
  201.         end
  202.         local y1 = x2 + 1
  203.         x2 = x2 - 1
  204.         local y2 = y1
  205.         while line:sub(y2, y2) ~= "}" do
  206.             y2 = y2 + 1
  207.         end
  208.         local endi = y2
  209.         y2 = y2 - 1
  210.        
  211.         local x = tonumber(line:sub(x1, x2))
  212.         local y = tonumber(line:sub(y1, y2))
  213.         return true, x, y, endi
  214.     else
  215.         return false
  216.     end
  217. end
  218.  
  219. local function loadMap(dir, screen)
  220.     local lines = getLines(dir)
  221.     if lines ~= nil then
  222.         local map = {}
  223.         local width = nil
  224.         local height = nil
  225.         -- vvv LOADING MAP LAYOUT vvv
  226.         local i = 2
  227.         while lines[i] ~= "}" do
  228.             local line = lines[i]
  229.             line = line:sub(2, -3)
  230.             local y = (i-1)
  231.             for x=1,string.len(line) do
  232.                 if map[x] == nil then map[x] = {} end
  233.                 map[x][y] = line:sub(x, x)
  234.             end
  235.             if width == nil then width = string.len(line) end
  236.             i = i + 1
  237.         end
  238.         if height == nil then height = i-2 end
  239.         -- ^^^ LOADED MAP LAYOUT ^^^
  240.         -- vvv LOADING STARTING_POSITION vvv
  241.         i = i + 1
  242.         local posLine = lines[i]
  243.         local x2 = -1
  244.         while posLine:sub(x2, x2) ~= "," do x2 = x2 -1 end
  245.         x2 = x2 - 1
  246.         local x1 = x2
  247.         while posLine:sub(x1, x1) ~= "{" do x1 = x1 -1 end
  248.         x1 = x1 + 1
  249.         local x = tonumber(posLine:sub(x1, x2))
  250.         local y1 = x2+2
  251.         local y2 = -2
  252.         local y = tonumber(posLine:sub(y1, y2))
  253.         map.start = {x, y}
  254.         --^^^ LOADED STARTING_POSITION ^^^
  255.         --vvv LOADING VISION_NUMBER vvv
  256.         i = i + 1
  257.         local vLine = lines[i]
  258.         local j = -1
  259.         while vLine:sub(j, j) ~= " " do j = j - 1 end
  260.         j = j + 1
  261.         local vision = tonumber(vLine:sub(j, -1))
  262.         map.vision = vision
  263.         --^^^ LOADED VISION_NUMBER ^^^
  264.         --vvv LOADING CONVERTER vvv
  265.         i = i + 16
  266.         map.convert = {
  267.             ["+"] = {colors.white, colors.white, " "},
  268.             ["-"] = {colors.black, colors.black, " "},
  269.             ["*"] = {colors.white, colors.lightBlue, "*"},
  270.             ["#"] = {colors.white, colors.lightBlue, "#"},
  271.             [":"] = {colors.white, colors.brown, ":"},
  272.             ["@"] = {colors.white, colors.lightBlue, "@"},
  273.             ["~"] = {colors.black, colors.black, " "}
  274.         }
  275.         --^^^ LOADED CONVERTER ^^^
  276.         --vvv LOADING TEXTTS vvv
  277.         map.texts = {}
  278.         i = i + 2
  279.         while lines[i] ~= "}" do
  280.             local result, x, y, endi = getPos(lines[i], 2)
  281.             if result == true then
  282.                 local pos = {x, y}
  283.                 local text = lines[i]:sub(endi+4, -4)
  284.                 map.texts[#map.texts + 1] = {pos, text}
  285.                 print("Loaded text at: "..pos[1]..","..pos[2].." saying:")
  286.                 print("(TEXT)"..text)
  287.             else
  288.                 print("Failed to load text")
  289.             end
  290.             i = i + 1
  291.         end
  292.         --^^^ LOADED TEXTS ^^^
  293.         --vvv LOADING KEYS vvv
  294.         map.keys = {}
  295.         i = i + 2
  296.         while lines[i] ~= "}" do
  297.             local result1, x1, y1, end1 = getPos(lines[i], 2)
  298.             if result1 == true then
  299.                 local pos1 = {x1, y1}
  300.                 local result2, x2, y2, end2 = getPos(lines[i], end1+3)
  301.                 if result2 == true then
  302.                     local pos2 = {x2, y2}
  303.                     local typ = tonumber(lines[i]:sub(end2+3, -3))
  304.                     map.keys[#map.keys + 1] = {pos1, pos2, typ}
  305.                     print("Loaded key at: "..pos1[1]..","..pos1[2].." with door at "..pos2[1]..","..pos2[2].." to type "..typ)
  306.                 else
  307.                     print("Failed to load key (stage 2)")
  308.                 end
  309.             else
  310.                 print("Failed to load key (stage 1)")
  311.             end
  312.             i = i + 1
  313.         end
  314.         --^^^ LOADED KEYS ^^^
  315.         --vvv LOADING IKEYS vvv
  316.         map.ikeys = {}
  317.         i = i + 2
  318.         while lines[i] ~= "}" do
  319.             local result1, x1, y1, end1 = getPos(lines[i], 2)
  320.             if result1 == true then
  321.                 local pos1 = {x1, y1}
  322.                 local result2, x2, y2, end2 = getPos(lines[i], end1+3)
  323.                 if result2 == true then
  324.                     local pos2 = {x2, y2}
  325.                     local start = end2+3
  326.                     local en = start
  327.                     while lines[i]:sub(en, en) ~= ',' do
  328.                         en = en + 1
  329.                     end
  330.                     en = en - 1
  331.                     local typ = tonumber(lines[i]:sub(start, en))
  332.                     local iname = lines[i]:sub(en+13, -10)
  333.                     map.ikeys[#map.ikeys + 1] = {pos1, pos2, typ, iname}
  334.                     print("Loaded ikey at: "..pos1[1]..","..pos1[2].." with door at "..pos2[1]..","..pos2[2].." to type "..typ.." when used by item "..iname)
  335.                 else
  336.                     print("Failed to load ikey (stage 2)")
  337.                 end
  338.             else
  339.                 print("Failed to load ikey (stage 1)")
  340.             end
  341.             i = i + 1
  342.         end
  343.         --^^^ LOADED IKEYS ^^^
  344.         --vvv LOADING ITEMS vvv
  345.             i = i + 2
  346.             map.items = {}
  347.             while lines[i] ~= "}" do
  348.                 local result, x, y, endi = getPos(lines[i], 2)
  349.                 if result == true then
  350.                     local pos = {x, y}
  351.                     local name = lines[i]:sub(endi+13, -5)
  352.                     map.items[#map.items + 1] = {pos, name}
  353.                     print("Loaded item at: "..pos[1]..","..pos[2].." called "..name)
  354.                 else
  355.                     print("Failed to load item")
  356.                 end
  357.                 i = i + 1
  358.             end
  359.         --^^^ LOADED ITEMS ^^^
  360.         --vvv LOADING DOORS vvv
  361.             map.doors = {}
  362.             i = i + 2
  363.             while lines[i] ~= "}" do
  364.                 local line = lines[i]
  365.                 local result1, x1, y1, end1 = getPos(line, 2)
  366.                 if result1 == true then
  367.                     local pos1 = {x1, y1}
  368.                     local si1 = end1 + 3
  369.                     local si2 = si1
  370.                     while line:sub(si2, si2) ~= "," do
  371.                         si2 = si2 + 1
  372.                     end
  373.                     local pstart = si2 + 2
  374.                     si2 = si2 - 1
  375.                     local mname = line:sub(si1, si2)
  376.                     local result2, x2, y2, end2 = getPos(line, pstart)
  377.                     if result2 == true then
  378.                         local pos2 = {x2, y2}
  379.                         map.doors[#map.doors + 1] = {pos1, mname, pos2}
  380.                         print("Loaded door at: "..pos1[1]..","..pos1[2].." to "..mname.." "..pos2[1]..","..pos2[2])
  381.                     else
  382.                         print("Failed to load door")
  383.                     end
  384.                 else
  385.                     print("Failed to load door")
  386.                 end
  387.                 i = i + 1
  388.             end
  389.         --^^^ LOADED DOORS ^^^
  390.         sleep(5)
  391.         map.name = dir
  392.         map.offset = {0,0}
  393.         map.brush = 1
  394.         map.screen = screen
  395.         map.width = width
  396.         map.height = height
  397.         map.brs = "+-*#:@~"
  398.         map.color = 1
  399.         addFunctions(map)
  400.         return map
  401.     else return nil end
  402. end
  403.  
  404. local function createMap(width, height, name, screen)
  405.     local map = {}
  406.     for x=1,width do
  407.         map[x] = {}
  408.         for y=1,height do
  409.             map[x][y] = "-"
  410.         end
  411.     end
  412.     map.name = name
  413.     map.offset = {0,0}
  414.     map.start = {1,1}
  415.     map.brush = 1
  416.     map.screen = screen
  417.     map.width = width
  418.     map.height = height
  419.     map.brs = "+-*#:@~"
  420.     map.convert = {
  421.         ["+"] = {colors.white, colors.white, " "},
  422.         ["-"] = {colors.black, colors.black, " "},
  423.         ["*"] = {colors.white, colors.lightBlue, "*"},
  424.         ["#"] = {colors.white, colors.lightBlue, "#"},
  425.         [":"] = {colors.white, colors.brown, ":"},
  426.         ["@"] = {colors.white, colors.lightBlue, "@"},
  427.         ["~"] = {colors.black, colors.black, " "}
  428.     }
  429.     map.texts = {}
  430.     map.keys = {}
  431.     map.ikeys = {}
  432.     map.doors = {}
  433.     map.items = {}
  434.     map.color = 1
  435.     addFunctions(map)
  436.     return map
  437. end
  438.  
  439.  
  440.  
  441. local function writeLines(dir, lines)
  442.     local file = fs.open(dir, "w")
  443.     for i=1,#lines do
  444.         file.writeLine(lines[i])
  445.     end
  446.     file.close()
  447.     return true
  448. end
  449.  
  450. local function openMenu(map, pos, dpos)
  451.     local layout =
  452.     {"|1|spawn|",
  453.      "|2|text |",
  454.      "|3|key  |",
  455.      "|4|door |",
  456.      "|5|item |",
  457.      "|6|clear|",
  458.      "|7|ikey |"}
  459.      
  460.      term.setBackgroundColor(colors.lightBlue)
  461.      term.setTextColor(colors.blue)
  462.      for i=1,#layout do
  463.         term.setCursorPos(dpos[1], dpos[2]+i-1)
  464.         term.write(layout[i])
  465.      end
  466.      
  467.      local empty = ""
  468.      for i=1,map.screen[2] do empty = empty.." " end
  469.      
  470.      local ch = 0
  471.      local step = 0
  472.      
  473.      local tempon = true
  474.      while tempon do
  475.         local event, p1 = os.pullEvent()
  476.         if event == "key" then
  477.             local key = p1
  478.             if key == 29 then tempon = false
  479.             elseif key >= 2 and key <= 8 then
  480.                 if ch == 0 then
  481.                     ch = key-1
  482.                 end
  483.             elseif ch ~= 0 then
  484.                
  485.             end
  486.         end
  487.         if ch ~= 0 then
  488.             term.setBackgroundColor(colors.black)
  489.             term.setTextColor(colors.white)
  490.             term.setCursorPos(2,1)
  491.             term.write(empty)
  492.             if ch == 1 then --spawn
  493.                 term.setCursorPos(2, 1)
  494.                 map.start = pos
  495.                 term.write("Spawn set")
  496.                 tempon = false
  497.             elseif ch == 2 then --text
  498.                 local line = ""
  499.                 local start = false
  500.                 local text = "..."
  501.                 local ison = true
  502.                 while ison == true do
  503.                     if start == true then line = line.." "..text else start = true end
  504.                     term.setCursorPos(2, 1)
  505.                     term.write(empty)
  506.                     term.setCursorPos(2, 1)
  507.                     term.write("Text:")
  508.                     term.setCursorPos(8, 1)
  509.                     text = read()
  510.                     --print("Text = "..text)
  511.                     --print("Result = "..tostring((text:gsub(" ", "") ~= "")))
  512.                     if text:gsub(" ", "") == "" then ison = false end
  513.                 end
  514.                 map.texts[#map.texts + 1] = {pos, line}
  515.                 tempon = false
  516.             elseif ch == 3 then --key
  517.                 term.setCursorPos(2, 1)
  518.                 term.write("Click door position")
  519.                 local event, button, xp, yp = os.pullEvent("mouse_click")
  520.                 local pos2 = {xp-1+map.offset[1], yp-1+map.offset[2]}
  521.                 map.keys[#map.keys + 1] = {pos, pos2, map.brs:sub(map.brush, map.brush)}
  522.                 tempon = false
  523.             elseif ch == 4 then --door
  524.                 term.setCursorPos(2, 1)
  525.                 term.write("Map name:")
  526.                 term.setCursorPos(12, 1)
  527.                 local mname = read()
  528.                 term.setCursorPos(2, 1)
  529.                 term.write(empty)
  530.                 term.setCursorPos(2, 1)
  531.                 term.write("Start X:")
  532.                 term.setCursorPos(11, 1)
  533.                 local sx = tonumber(read())
  534.                 term.setCursorPos(2, 1)
  535.                 term.write(empty)
  536.                 term.setCursorPos(2, 1)
  537.                 term.write("Start Y:")
  538.                 term.setCursorPos(11, 1)
  539.                 local sy = tonumber(read())
  540.                 map.doors[#map.doors + 1] = {pos, mname, {sx, sy}}
  541.                 tempon = false
  542.             elseif ch == 5 then --item
  543.                 term.setCursorPos(2, 1)
  544.                 term.write("Item name:")
  545.                 term.setCursorPos(13, 1)
  546.                 local iname = read()
  547.                 map.items[#map.items + 1] = {pos, iname}
  548.                 tempon = false
  549.             elseif ch == 6 then --clear
  550.                 local tabs = {map.texts, map.keys, map.doors, map.items}
  551.                 for k=1,#tabs do
  552.                     local tab = tabs[k]
  553.                     local ntab = {}
  554.                     for i=1,#tab do
  555.                         local info = tab[i]
  556.                         local cpos = info[1]
  557.                         if cpos[1] ~= pos[1] and cpos[2] ~= pos[2] then
  558.                             ntab[#ntab + 1] = info
  559.                         end
  560.                     end
  561.                     if k == 1 then map.texts = ntab
  562.                     elseif k == 2 then map.keys = ntab
  563.                     elseif k == 3 then map.doors = ntab
  564.                     elseif k == 4 then map.items = ntab end
  565.                 end
  566.                 term.setCursorPos(2, 1)
  567.                 term.write("Cleared tile contents")
  568.                 tempon = false
  569.             elseif ch == 7 then
  570.                 term.setCursorPos(2, 1)
  571.                 term.write("Click door position:")
  572.                 local event, button, xp, yp = os.pullEvent("mouse_click")
  573.                 local pos2 = {xp-1+map.offset[1], yp-1+map.offset[2]}
  574.                 term.setCursorPos(2, 1)
  575.                 term.write(empty)
  576.                 term.setCursorPos(2,1)
  577.                 term.write("Enter item name:")
  578.                 term.setCursorPos(19, 1)
  579.                 local name = read()
  580.                 map.ikeys[#map.ikeys + 1] = {pos, pos2, map.brs:sub(map.brush, map.brush), name}
  581.                 tempon = false
  582.             end
  583.         end
  584.      end
  585. end
  586.  
  587. local map = nil
  588.  
  589. local stuff = {...}
  590. local cmd = stuff[1]
  591. local name = stuff[2]
  592. local tw, th = term.getSize()
  593. if cmd == "new" or cmd == "n" or cmd == "1" then
  594.     local w = tonumber(stuff[3])
  595.     local h = tonumber(stuff[4])
  596.  
  597.     local map = createMap(w, h, name, {tw, th})
  598. elseif cmd == "load" or cmd == "l" or cmd == "2" then
  599.     map = loadMap(name, {tw, th})
  600. end
  601. if map ~= nil then
  602.     map:draw({1,1})
  603.     local on = true
  604.     while on do
  605.         local event, p1, p2, p3 = os.pullEvent()
  606.         if event == "key" then
  607.             local key = p1
  608.             if key == 17 then --w
  609.                 map:scroll({0, -1})
  610.             elseif key == 30 then --a
  611.                 map:scroll({-1, 0})
  612.             elseif key == 31 then --s
  613.                 map:scroll({0, 1})
  614.             elseif key == 32 then --d
  615.                 map:scroll({1, 0})
  616.             elseif key == 16 then --q
  617.                 print("changing brush")
  618.                 map:changeBrush(-1)
  619.                 print("done")
  620.             elseif key == 18 then --e
  621.                 print("changing brush")
  622.                 map:changeBrush(1)
  623.                 print("done")
  624.             elseif key == 15 then --tab
  625.                 map.color = map.color + 1
  626.                 if map.color > 3 then map.color = 1 end
  627.             elseif key == 29 then --end
  628.                 on = false
  629.             elseif key == 41 then --save
  630.                 local lines = map:save()
  631.                 writeLines(map.name, lines)
  632.             end
  633.             map:draw({1,1})
  634.         elseif event == "mouse_click" or event == "mouse_drag" then
  635.             local pos = {p2-1, p3-1}
  636.             if p1 == 1 then
  637.                 map:paint(pos[1], pos[2])
  638.             elseif event == "mouse_click" and p1 == 2 then
  639.                 openMenu(map, {pos[1]+map.offset[1], pos[2]+map.offset[2]}, {map.screen[1]-9, 2})
  640.             end
  641.             map:draw({1,1})
  642.         end
  643.     end
  644.     term.setBackgroundColor(colors.black)
  645.     term.setTextColor(colors.white)
  646.     term.clear()
  647.     term.setCursorPos(1,1)
  648. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement