Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function addFunctions(map)
- map.save = function(self)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- local lines = {}
- lines[#lines + 1] = "local "..self.name.." = {"
- local mapl = {}
- for x=1,#self do
- local tab = self[x]
- for y=1,#tab do
- if mapl[y] == nil then mapl[y] = "" end
- mapl[y] = mapl[y]..self[x][y]
- end
- end
- for i=1,#mapl do
- lines[#lines + 1] = '"'..mapl[i]..'"'
- if i ~= #mapl then
- lines[#lines] = lines[#lines]..","
- end
- end
- lines[#lines + 1] = "}"
- lines[#lines + 1] = self.name..".pos = {"..self.start[1]..","..self.start[2].."}"
- lines[#lines + 1] = self.name..".vision = 4"
- lines[#lines + 1] = self.name..".convert = {"
- lines[#lines + 1] = " ["..'"'.."-"..'"'.."] = 0,"
- lines[#lines + 1] = " ["..'"'.."+"..'"'.."] = 1,"
- lines[#lines + 1] = " ["..'"'.."#"..'"'.."] = 2,"
- lines[#lines + 1] = " ["..'"'.."*"..'"'.."] = 3,"
- lines[#lines + 1] = " ["..'"'..":"..'"'.."] = 4,"
- lines[#lines + 1] = " ["..'"'.."@"..'"'.."] = 5,"
- lines[#lines + 1] = " ["..'"'.."~"..'"'.."] = 6,"
- lines[#lines + 1] = " ["..'"'.."0"..'"'.."] = 0,"
- lines[#lines + 1] = " ["..'"'.."1"..'"'.."] = 1,"
- lines[#lines + 1] = " ["..'"'.."2"..'"'.."] = 2,"
- lines[#lines + 1] = " ["..'"'.."3"..'"'.."] = 3,"
- lines[#lines + 1] = " ["..'"'.."4"..'"'.."] = 4,"
- lines[#lines + 1] = " ["..'"'.."5"..'"'.."] = 5,"
- lines[#lines + 1] = " ["..'"'.."~"..'"'.."] = 6"
- lines[#lines + 1] = "}"
- lines[#lines + 1] = self.name..".texts = {"
- for i=1,#self.texts do
- local info = self.texts[i]
- local pos = info[1]
- local text = info[2]
- lines[#lines + 1] = "{{"..pos[1]..","..pos[2].."}, "..'"'..text..'"'.."},"
- end
- lines[#lines + 1] = "}"
- lines[#lines + 1] = self.name..".keys = {"
- for i=1,#self.keys do
- local info = self.keys[i]
- local pos1 = info[1]
- local pos2 = info[2]
- local typ = info[3]
- local conv = "-+#*:@"
- local ityp = 0
- for i=1,string.len(conv) do if conv:sub(i, i) == typ then ityp = (i-1) end end
- lines[#lines + 1] = "{{"..pos1[1]..","..pos1[2].."}, {"..pos2[1]..","..pos2[2].."}, "..ityp.."},"
- end
- lines[#lines + 1] = "}"
- lines[#lines + 1] = self.name..".ikeys = {"
- for i=1,#self.ikeys do
- local info = self.ikeys[i]
- local pos1 = info[1]
- local pos2 = info[2]
- local typ = info[3]
- local iname = info[4]
- local conv = "-+#*:@"
- local ityp = 0
- for i=1,string.len(conv) do if conv:sub(i, i) == typ then ityp = (i-1) end end
- lines[#lines + 1] = "{{"..pos1[1]..","..pos1[2].."}, {"..pos2[1]..","..pos2[2].."}, "..ityp..", itemList["..'"'..iname..'"'.."].info},"
- end
- lines[#lines + 1] = "}"
- lines[#lines + 1] = self.name..".items = {"
- for i=1,#self.items do
- local info = self.items[i]
- local pos = info[1]
- local name = info[2]
- lines[#lines + 1] = "{{"..pos[1]..","..pos[2].."}, itemList["..'"'..name..'"'.."]},"
- end
- lines[#lines + 1] = "}"
- lines[#lines + 1] = self.name..".tele = {"
- for i=1,#self.doors do
- local info = self.doors[i]
- local pos = info[1]
- local mname = info[2]
- local spos = info[3]
- lines[#lines + 1] = "{{"..pos[1]..","..pos[2].."}, "..mname..", {"..spos[1]..","..spos[2].."}},"
- end
- lines[#lines + 1] = "}"
- return lines
- end
- map.paint = function(self, x, y)
- local pos = {x+self.offset[1], y+self.offset[2]}
- if self[pos[1]] ~= nil then
- if self[pos[1]][pos[2]] ~= nil then
- self[pos[1]][pos[2]] = self.brs:sub(self.brush, self.brush)
- end
- end
- end
- map.changeBrush = function(self, i)
- self.brush = self.brush + i
- if self.brush < 1 then self.brush = string.len(self.brs) end
- if self.brush > string.len(self.brs) then self.brush = 1 end
- end
- map.draw = function(self, pos)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- if self.color == 3 then
- local tabs = {map.texts, map.keys, map.doors, map.items, map.ikeys}
- local tcolor = {colors.white, colors.lightBlue, colors.brown, colors.orange, colors.blue}
- for k=1,#tabs do
- local tab = tabs[k]
- local col = tcolor[k]
- --local text = " "
- local ntab = {}
- for i=1,#tab do
- text = " "
- if k == 2 or k == 5 then text = tostring(i) end
- local info = tab[i]
- local ipos = info[1]
- 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
- term.setCursorPos(pos[1]+ipos[1]-self.offset[1], pos[2]+ipos[2]-self.offset[2])
- term.setBackgroundColor(col)
- term.setTextColor(colors.black)
- term.write(text)
- end
- if k == 2 or k == 5 then
- ipos = info[2]
- 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
- term.setCursorPos(pos[1]+ipos[1]-self.offset[1], pos[2]+ipos[2]-self.offset[2])
- term.setBackgroundColor(col)
- term.setTextColor(colors.black)
- term.write(text)
- end
- end
- end
- end
- else
- for x=self.offset[1]+1,pos[1]+self.screen[1]+self.offset[1] do
- for y=self.offset[2]+1,pos[2]+self.screen[2]+self.offset[2] do
- if self[x] ~= nil then
- if self[x][y] ~= nil then
- local char = self[x][y]
- term.setCursorPos(pos[1]+x-self.offset[1], pos[2]+y-self.offset[2])
- local bcol = colors.white
- local tcol = colors.black
- local text = char
- if self.color == 2 then
- bcol = self.convert[char][1]
- tcol = self.convert[char][2]
- text = self.convert[char][3]
- end
- term.setBackgroundColor(bcol)
- term.setTextColor(tcol)
- term.write(text)
- end
- end
- end
- end
- end
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.orange)
- term.setTextColor(colors.black)
- term.write(self.brs:sub(self.brush, self.brush))
- end
- map.scroll = function(self, dir)
- local noff = {self.offset[1]+dir[1], self.offset[2]+dir[2]}
- if noff[1] < 1 then noff[1] = 1 end
- if noff[2] < 1 then noff[2] = 1 end
- if noff[1]+self.screen[1] > self.width then noff[1] = self.width-self.screen[1] end
- if noff[2]+self.screen[2] > self.height then noff[2] = self.height-self.screen[2] end
- self.offset = noff
- end
- end
- local function getLines(dir)
- local lines = nil
- if fs.exists(dir) then
- lines = {}
- local file = fs.open(dir, "r")
- local line = file.readLine()
- while line ~= nil do
- lines[#lines + 1] = line
- line = file.readLine()
- end
- file.close()
- end
- return lines
- end
- local function getPos(line, start)
- if line:sub(start, start) == "{" then
- local x1 = start+1
- local x2 = x1
- while line:sub(x2, x2) ~= "," do
- x2 = x2 + 1
- end
- local y1 = x2 + 1
- x2 = x2 - 1
- local y2 = y1
- while line:sub(y2, y2) ~= "}" do
- y2 = y2 + 1
- end
- local endi = y2
- y2 = y2 - 1
- local x = tonumber(line:sub(x1, x2))
- local y = tonumber(line:sub(y1, y2))
- return true, x, y, endi
- else
- return false
- end
- end
- local function loadMap(dir, screen)
- local lines = getLines(dir)
- if lines ~= nil then
- local map = {}
- local width = nil
- local height = nil
- -- vvv LOADING MAP LAYOUT vvv
- local i = 2
- while lines[i] ~= "}" do
- local line = lines[i]
- line = line:sub(2, -3)
- local y = (i-1)
- for x=1,string.len(line) do
- if map[x] == nil then map[x] = {} end
- map[x][y] = line:sub(x, x)
- end
- if width == nil then width = string.len(line) end
- i = i + 1
- end
- if height == nil then height = i-2 end
- -- ^^^ LOADED MAP LAYOUT ^^^
- -- vvv LOADING STARTING_POSITION vvv
- i = i + 1
- local posLine = lines[i]
- local x2 = -1
- while posLine:sub(x2, x2) ~= "," do x2 = x2 -1 end
- x2 = x2 - 1
- local x1 = x2
- while posLine:sub(x1, x1) ~= "{" do x1 = x1 -1 end
- x1 = x1 + 1
- local x = tonumber(posLine:sub(x1, x2))
- local y1 = x2+2
- local y2 = -2
- local y = tonumber(posLine:sub(y1, y2))
- map.start = {x, y}
- --^^^ LOADED STARTING_POSITION ^^^
- --vvv LOADING VISION_NUMBER vvv
- i = i + 1
- local vLine = lines[i]
- local j = -1
- while vLine:sub(j, j) ~= " " do j = j - 1 end
- j = j + 1
- local vision = tonumber(vLine:sub(j, -1))
- map.vision = vision
- --^^^ LOADED VISION_NUMBER ^^^
- --vvv LOADING CONVERTER vvv
- i = i + 16
- map.convert = {
- ["+"] = {colors.white, colors.white, " "},
- ["-"] = {colors.black, colors.black, " "},
- ["*"] = {colors.white, colors.lightBlue, "*"},
- ["#"] = {colors.white, colors.lightBlue, "#"},
- [":"] = {colors.white, colors.brown, ":"},
- ["@"] = {colors.white, colors.lightBlue, "@"},
- ["~"] = {colors.black, colors.black, " "}
- }
- --^^^ LOADED CONVERTER ^^^
- --vvv LOADING TEXTTS vvv
- map.texts = {}
- i = i + 2
- while lines[i] ~= "}" do
- local result, x, y, endi = getPos(lines[i], 2)
- if result == true then
- local pos = {x, y}
- local text = lines[i]:sub(endi+4, -4)
- map.texts[#map.texts + 1] = {pos, text}
- print("Loaded text at: "..pos[1]..","..pos[2].." saying:")
- print("(TEXT)"..text)
- else
- print("Failed to load text")
- end
- i = i + 1
- end
- --^^^ LOADED TEXTS ^^^
- --vvv LOADING KEYS vvv
- map.keys = {}
- i = i + 2
- while lines[i] ~= "}" do
- local result1, x1, y1, end1 = getPos(lines[i], 2)
- if result1 == true then
- local pos1 = {x1, y1}
- local result2, x2, y2, end2 = getPos(lines[i], end1+3)
- if result2 == true then
- local pos2 = {x2, y2}
- local typ = tonumber(lines[i]:sub(end2+3, -3))
- map.keys[#map.keys + 1] = {pos1, pos2, typ}
- print("Loaded key at: "..pos1[1]..","..pos1[2].." with door at "..pos2[1]..","..pos2[2].." to type "..typ)
- else
- print("Failed to load key (stage 2)")
- end
- else
- print("Failed to load key (stage 1)")
- end
- i = i + 1
- end
- --^^^ LOADED KEYS ^^^
- --vvv LOADING IKEYS vvv
- map.ikeys = {}
- i = i + 2
- while lines[i] ~= "}" do
- local result1, x1, y1, end1 = getPos(lines[i], 2)
- if result1 == true then
- local pos1 = {x1, y1}
- local result2, x2, y2, end2 = getPos(lines[i], end1+3)
- if result2 == true then
- local pos2 = {x2, y2}
- local start = end2+3
- local en = start
- while lines[i]:sub(en, en) ~= ',' do
- en = en + 1
- end
- en = en - 1
- local typ = tonumber(lines[i]:sub(start, en))
- local iname = lines[i]:sub(en+13, -10)
- map.ikeys[#map.ikeys + 1] = {pos1, pos2, typ, iname}
- print("Loaded ikey at: "..pos1[1]..","..pos1[2].." with door at "..pos2[1]..","..pos2[2].." to type "..typ.." when used by item "..iname)
- else
- print("Failed to load ikey (stage 2)")
- end
- else
- print("Failed to load ikey (stage 1)")
- end
- i = i + 1
- end
- --^^^ LOADED IKEYS ^^^
- --vvv LOADING ITEMS vvv
- i = i + 2
- map.items = {}
- while lines[i] ~= "}" do
- local result, x, y, endi = getPos(lines[i], 2)
- if result == true then
- local pos = {x, y}
- local name = lines[i]:sub(endi+13, -5)
- map.items[#map.items + 1] = {pos, name}
- print("Loaded item at: "..pos[1]..","..pos[2].." called "..name)
- else
- print("Failed to load item")
- end
- i = i + 1
- end
- --^^^ LOADED ITEMS ^^^
- --vvv LOADING DOORS vvv
- map.doors = {}
- i = i + 2
- while lines[i] ~= "}" do
- local line = lines[i]
- local result1, x1, y1, end1 = getPos(line, 2)
- if result1 == true then
- local pos1 = {x1, y1}
- local si1 = end1 + 3
- local si2 = si1
- while line:sub(si2, si2) ~= "," do
- si2 = si2 + 1
- end
- local pstart = si2 + 2
- si2 = si2 - 1
- local mname = line:sub(si1, si2)
- local result2, x2, y2, end2 = getPos(line, pstart)
- if result2 == true then
- local pos2 = {x2, y2}
- map.doors[#map.doors + 1] = {pos1, mname, pos2}
- print("Loaded door at: "..pos1[1]..","..pos1[2].." to "..mname.." "..pos2[1]..","..pos2[2])
- else
- print("Failed to load door")
- end
- else
- print("Failed to load door")
- end
- i = i + 1
- end
- --^^^ LOADED DOORS ^^^
- sleep(5)
- map.name = dir
- map.offset = {0,0}
- map.brush = 1
- map.screen = screen
- map.width = width
- map.height = height
- map.brs = "+-*#:@~"
- map.color = 1
- addFunctions(map)
- return map
- else return nil end
- end
- local function createMap(width, height, name, screen)
- local map = {}
- for x=1,width do
- map[x] = {}
- for y=1,height do
- map[x][y] = "-"
- end
- end
- map.name = name
- map.offset = {0,0}
- map.start = {1,1}
- map.brush = 1
- map.screen = screen
- map.width = width
- map.height = height
- map.brs = "+-*#:@~"
- map.convert = {
- ["+"] = {colors.white, colors.white, " "},
- ["-"] = {colors.black, colors.black, " "},
- ["*"] = {colors.white, colors.lightBlue, "*"},
- ["#"] = {colors.white, colors.lightBlue, "#"},
- [":"] = {colors.white, colors.brown, ":"},
- ["@"] = {colors.white, colors.lightBlue, "@"},
- ["~"] = {colors.black, colors.black, " "}
- }
- map.texts = {}
- map.keys = {}
- map.ikeys = {}
- map.doors = {}
- map.items = {}
- map.color = 1
- addFunctions(map)
- return map
- end
- local function writeLines(dir, lines)
- local file = fs.open(dir, "w")
- for i=1,#lines do
- file.writeLine(lines[i])
- end
- file.close()
- return true
- end
- local function openMenu(map, pos, dpos)
- local layout =
- {"|1|spawn|",
- "|2|text |",
- "|3|key |",
- "|4|door |",
- "|5|item |",
- "|6|clear|",
- "|7|ikey |"}
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.blue)
- for i=1,#layout do
- term.setCursorPos(dpos[1], dpos[2]+i-1)
- term.write(layout[i])
- end
- local empty = ""
- for i=1,map.screen[2] do empty = empty.." " end
- local ch = 0
- local step = 0
- local tempon = true
- while tempon do
- local event, p1 = os.pullEvent()
- if event == "key" then
- local key = p1
- if key == 29 then tempon = false
- elseif key >= 2 and key <= 8 then
- if ch == 0 then
- ch = key-1
- end
- elseif ch ~= 0 then
- end
- end
- if ch ~= 0 then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(2,1)
- term.write(empty)
- if ch == 1 then --spawn
- term.setCursorPos(2, 1)
- map.start = pos
- term.write("Spawn set")
- tempon = false
- elseif ch == 2 then --text
- local line = ""
- local start = false
- local text = "..."
- local ison = true
- while ison == true do
- if start == true then line = line.." "..text else start = true end
- term.setCursorPos(2, 1)
- term.write(empty)
- term.setCursorPos(2, 1)
- term.write("Text:")
- term.setCursorPos(8, 1)
- text = read()
- --print("Text = "..text)
- --print("Result = "..tostring((text:gsub(" ", "") ~= "")))
- if text:gsub(" ", "") == "" then ison = false end
- end
- map.texts[#map.texts + 1] = {pos, line}
- tempon = false
- elseif ch == 3 then --key
- term.setCursorPos(2, 1)
- term.write("Click door position")
- local event, button, xp, yp = os.pullEvent("mouse_click")
- local pos2 = {xp-1+map.offset[1], yp-1+map.offset[2]}
- map.keys[#map.keys + 1] = {pos, pos2, map.brs:sub(map.brush, map.brush)}
- tempon = false
- elseif ch == 4 then --door
- term.setCursorPos(2, 1)
- term.write("Map name:")
- term.setCursorPos(12, 1)
- local mname = read()
- term.setCursorPos(2, 1)
- term.write(empty)
- term.setCursorPos(2, 1)
- term.write("Start X:")
- term.setCursorPos(11, 1)
- local sx = tonumber(read())
- term.setCursorPos(2, 1)
- term.write(empty)
- term.setCursorPos(2, 1)
- term.write("Start Y:")
- term.setCursorPos(11, 1)
- local sy = tonumber(read())
- map.doors[#map.doors + 1] = {pos, mname, {sx, sy}}
- tempon = false
- elseif ch == 5 then --item
- term.setCursorPos(2, 1)
- term.write("Item name:")
- term.setCursorPos(13, 1)
- local iname = read()
- map.items[#map.items + 1] = {pos, iname}
- tempon = false
- elseif ch == 6 then --clear
- local tabs = {map.texts, map.keys, map.doors, map.items}
- for k=1,#tabs do
- local tab = tabs[k]
- local ntab = {}
- for i=1,#tab do
- local info = tab[i]
- local cpos = info[1]
- if cpos[1] ~= pos[1] and cpos[2] ~= pos[2] then
- ntab[#ntab + 1] = info
- end
- end
- if k == 1 then map.texts = ntab
- elseif k == 2 then map.keys = ntab
- elseif k == 3 then map.doors = ntab
- elseif k == 4 then map.items = ntab end
- end
- term.setCursorPos(2, 1)
- term.write("Cleared tile contents")
- tempon = false
- elseif ch == 7 then
- term.setCursorPos(2, 1)
- term.write("Click door position:")
- local event, button, xp, yp = os.pullEvent("mouse_click")
- local pos2 = {xp-1+map.offset[1], yp-1+map.offset[2]}
- term.setCursorPos(2, 1)
- term.write(empty)
- term.setCursorPos(2,1)
- term.write("Enter item name:")
- term.setCursorPos(19, 1)
- local name = read()
- map.ikeys[#map.ikeys + 1] = {pos, pos2, map.brs:sub(map.brush, map.brush), name}
- tempon = false
- end
- end
- end
- end
- local map = nil
- local stuff = {...}
- local cmd = stuff[1]
- local name = stuff[2]
- local tw, th = term.getSize()
- if cmd == "new" or cmd == "n" or cmd == "1" then
- local w = tonumber(stuff[3])
- local h = tonumber(stuff[4])
- local map = createMap(w, h, name, {tw, th})
- elseif cmd == "load" or cmd == "l" or cmd == "2" then
- map = loadMap(name, {tw, th})
- end
- if map ~= nil then
- map:draw({1,1})
- local on = true
- while on do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "key" then
- local key = p1
- if key == 17 then --w
- map:scroll({0, -1})
- elseif key == 30 then --a
- map:scroll({-1, 0})
- elseif key == 31 then --s
- map:scroll({0, 1})
- elseif key == 32 then --d
- map:scroll({1, 0})
- elseif key == 16 then --q
- print("changing brush")
- map:changeBrush(-1)
- print("done")
- elseif key == 18 then --e
- print("changing brush")
- map:changeBrush(1)
- print("done")
- elseif key == 15 then --tab
- map.color = map.color + 1
- if map.color > 3 then map.color = 1 end
- elseif key == 29 then --end
- on = false
- elseif key == 41 then --save
- local lines = map:save()
- writeLines(map.name, lines)
- end
- map:draw({1,1})
- elseif event == "mouse_click" or event == "mouse_drag" then
- local pos = {p2-1, p3-1}
- if p1 == 1 then
- map:paint(pos[1], pos[2])
- elseif event == "mouse_click" and p1 == 2 then
- openMenu(map, {pos[1]+map.offset[1], pos[2]+map.offset[2]}, {map.screen[1]-9, 2})
- end
- map:draw({1,1})
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement