· 8 years ago · Aug 30, 2018, 06:56 PM
1--Map API. Revised.
2--[[
3Cheat sheet
4loadMap(MapName) -- Load a file into the API
5cView(Viewer, Map, xWide, yHigh) -- Create a view point on this map which is x chars wide and y chars high
6dView(Viewer) -- delete the viewer
7draw(Viewer) -- Draws the viewpoint from the viewer.
8editMap(Map, X, Y, Char) -- Edit the map at this point to replace the char there with this char
9returnView(Viewer) -- Returns the view to whatever called it. Catch like this Stuff = {returnView("ME!!")} then its loaded into the table "stuff" in your program
10moveScreen(Viewer, X, Y) Moves the viewpoint of the viewer to that x, y
11saveMap(Map, Location) Saves the loaded map into the location
12]]
13Maps = {}
14Views = {}
15function replaceLine(Map, y, from, to, Stuff)
16 Maps[Map][y] = string.sub(Maps[Map][y], 1, from)..Stuff..string.sub(Maps[Map][y], to, string.len(Maps[Map][y]))
17end
18
19function insertLine(Map, y)
20 table.insert(Maps[Map], y)
21end
22
23function removeLine(Map, y)
24 table.remove(Maps[Map], y)
25end
26
27function loadMap(MapName) -- Loads a map into a table, The info included is loaded into another table
28 if not fs.exists(MapName) then error("Please use a map that exists! Use something like Folder/Folder/File") end
29 Maps[MapName] = {}
30 local MapLoadVari = io.open(MapName, "r")
31 local i = 1 -- how many its done.
32 local a = 1 -- x len
33 local b = 1 -- y len
34 for line in MapLoadVari:lines() do
35 i = i+1
36 Maps[MapName][i] = line
37 b = b+1
38 if string.len(line) > a then
39 a = string.len(line)
40 end
41 end
42 Maps[MapName]["X"] = a
43 Maps[MapName]["Y"] = b
44end
45
46function cView(Viewer, Map, xWide, yHigh)
47 Views[Viewer] = {}
48 Views[Viewer]["Wide"] = xWide
49 Views[Viewer]["Map"] = Map
50 Views[Viewer]["High"] = yHigh
51end
52
53function dView(Name)
54 Views[Viewer] = nil
55end
56
57function draw(Viewer)
58 for n=1,Views[Viewer]["High"] do
59 if Maps[Views[Viewer]["Map"]][(Views[Viewer]["Y"]+n)] then
60 Views[Viewer][n] = string.sub(Maps[(Views[Viewer]["Map"])][(Views[Viewer]["Y"]+n)], Views[Viewer]["X"], Views[Viewer]["X"]+Views[Viewer]["Wide"])
61 end
62 end
63end
64
65function editMap(Map, X, Y, Char)
66 Maps[Map][Y] = string.sub(Maps[Map][Y], 1, X-1)..Char..string.sub(Maps[Map][Y], X+1, string.len(Maps[Map][Y]))
67end
68
69function returnView(Viewer)
70 local Sec = {}
71 for n=1,Views[Viewer]["High"] do
72 Sec[n] = Views[Viewer][n]
73 end
74 return unpack(Sec)
75end
76
77function returnChar(Map, x, y)
78 return string.sub(Maps[Map][y], x, x)
79end
80
81function moveScreen(Viewer, xAmount, yAmount) -- Was going to add in checking if bla bla exists but..
82 Views[Viewer]["X"] = xAmount
83 Views[Viewer]["Y"] = yAmount
84end
85
86function saveMap(Map, Location)
87 local Beauty = io.open(Location, "w")
88 for n=1,#Maps[Map] do
89 Beauty:write(Maps[Map][n])
90 if #Maps[Map]+1 then
91 Beauty:write("\n")
92 end
93 end
94 Beauty:close()
95end