-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.lua
More file actions
308 lines (268 loc) · 7.16 KB
/
Copy pathMap.lua
File metadata and controls
308 lines (268 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
--[[ Map data wrapper - created by Zonz
This module wraps map data into fields to make code look nicer. It also provides a wrapper class for the map cells.
You can also write custom values to the map table, and it will save them on a per-map basis.
For instance, doing "map.seen = true" in Pallet Town will save that value, however it will only be readable while in Pallet Town.
You could then save the same variable while in a different map, and the 2 values will be independent of each other.
Map fields:
map.name - getMapName()
map.width - getMapWidth()
map.height - getMapHeight()
map.links - getMapLinks()
map.npcs - getNpcData()
map.cells - A 2D table containing map cell data
map.battlers - getActiveBattlers() - Doesn't cache
map.digSpots - getActiveDigSpots() - Doesn't cache
map.headbuttTrees - getActiveHeadbuttTrees() - Doesn't cache
map.berries - getActiveBerryTrees() - Doesn't cache
map.items - getDiscoverableItems() - Doesn't cache
Cell fields:
cell.x - Cell x coordinate
cell.y - Cell y coordinate
cell.type - getCellType(cell.x, cell.y)
cell.isWalkable - Whether or not the cell can be walked (or surfed) on
cell.neighbours - Directly adjacent cells that are walkable
Cell functions:
cell:moveTo() - moveToCell(cell.x, cell.y)
Usage:
function onPathAction()
if map.name == "Pallet Town" then
-- do something
elseif map.name == "Route 1" then
-- etc.
end
end
-- Standard iteration
for cell in pairs(map.cells) do
log("Cell (" .. cell.x .. ", " .. cell.y .. ") " .. cell.type)
end
-- Numerical iteration
for x = 0, map.width do
for y = 0, map.height do
local cell = map.cells[x][y]
log("Cell (" .. x .. ", " .. y .. ") " .. cell.type)
end
end
#map.cells - The total number of all cells in the map
]]
local map = {}
local cachedMaps = {}
local callbacks
local metatables
local walkableTypes =
{
["Link"] = true,
["Grass"] = true,
["Water"] = true,
["Normal Ground"] = true,
["Ice"] = true,
["Tree"] = true,
["Rock"] = true,
}
local function newCell(x, y)
local cell = {}
cell.x = x
cell.y = y
cell.type = getCellType(x, y)
cell.isWalkable = walkableTypes[cell.type] or false
cell.neighbours = {}
setmetatable(cell, metatables.cell)
return cell
end
local cellFunctions = {}
function cellFunctions:moveTo()
return moveToCell(self.x, self.y)
end
function cellFunctions:highlight()
if highlightCell ~= nil then -- only Zonz has this. Sorry
highlightCell(self.x, self.y)
end
end
local function setNeighbours(cell, cellMap)
local left
local right
local top
local bottom
local x, y = cell.x, cell.y
if x > 0 then
left = cellMap[x - 1][y]
if left.isWalkable then
table.insert(cell.neighbours, left)
end
end
if x < map.width then
right = cellMap[x + 1][y]
if right.isWalkable then
table.insert(cell.neighbours, right)
end
end
if y > 0 then
top = cellMap[x][y - 1]
if top.isWalkable then
table.insert(cell.neighbours, top)
end
end
if y < map.height then
bottom = cellMap[x][y + 1]
if bottom.isWalkable then
table.insert(cell.neighbours, bottom)
end
end
if x > 1 and left.type == "Ledge West" then
left = cellMap[x - 2][y]
if left.isWalkable and left.type ~= "Water" then
table.insert(cell.neighbours, left)
end
end
if x < map.width - 1 and right.type == "Ledge East" then
right = cellMap[x + 2][y]
if right.isWalkable and right.type ~= "Water" then
table.insert(cell.neighbours, right)
end
end
if y < map.height - 1 and bottom.type == "Ledge South" then
bottom = cellMap[x][y + 2]
if bottom.isWalkable and bottom.type ~= "Water" then
table.insert(cell.neighbours, bottom)
end
end
end
local function buildMap()
local cells = {}
-- Loop through the map twice. Once to create the cells, then again to set their neighbours.
for x = 0, map.width do
cells[x] = {}
for y = 0, map.height do
cells[x][y] = newCell(x, y)
end
end
for x = 0, map.width do
for y = 0, map.height do
setNeighbours(cells[x][y], cells)
end
end
setmetatable(cells, metatables.cells)
return cells
end
callbacks =
{
name = getMapName,
width = getMapWidth,
height = getMapHeight,
links = getMapLinks,
npcs = getNpcData,
cells = buildMap,
dynamic =
{
battlers = getActiveBattlers,
digSpots = getActiveDigSpots,
headbuttTrees = getActiveHeadbuttTrees,
berries = getActiveBerryTrees,
items = getDiscoverableItems,
},
}
metatables =
{
cell =
{
__index = cellFunctions,
__eq = function(left, right)
return left.x == right.x and left.y == right.y
end,
__tostring = function(self)
return "Cell (" .. self.x .. ", " .. self.y .. ") type: " .. self.type .. ", neighbours: " .. #self.neighbours
end,
__concat = function(left, right)
return tostring(left) .. tostring(right)
end,
},
cells =
{
__pairs = function(self)
local x = 0
local y = 0
return function()
if x > map.width then
if y == map.height then
return nil
end
x = 0
y = y + 1
end
local cell = self[x][y]
x = x + 1
return cell, cell
end
end,
__ipairs = function(self)
local x = 0
local y = 0
local index = 0
return function()
if x > map.width then
if y == map.height then
return nil
end
x = 0
y = y + 1
end
index = index + 1
local cell = self[x][y]
x = x + 1
return index, cell
end
end,
__len = function(self)
if self.__length ~= nil then
return self.__length
end
self.__length = (map.width + 1) * (map.height + 1)
return self.__length
end,
__tostring = function(self)
return map.name .. " - " .. #self .. " cells"
end,
__concat = function(left, right)
return tostring(left) .. tostring(right)
end,
},
map =
{
__index = function(self, key)
if callbacks[key] then
local data = callbacks[key]()
self[key] = data
return data
end
return nil
end,
},
map_main =
{
__index = function(self, key)
if callbacks.dynamic[key] then
return callbacks.dynamic[key]()
end
local currentMap = getMapName()
cachedMaps[currentMap] = cachedMaps[currentMap] or setmetatable({}, self.__map_meta)
return cachedMaps[currentMap][key]
end,
__newindex = function(self, key, value)
assert(callbacks[key] == nil and callbacks.dynamic[key] == nil, "Map fields are read only. (Tried to set " .. tostring(key) .. " to " .. tostring(value) .. ")")
local currentMap = getMapName()
cachedMaps[currentMap] = cachedMaps[currentMap] or setmetatable({}, self.__map_meta)
cachedMaps[currentMap][key] = value
end,
__tostring = function(self)
return map.name .. "\nWidth: " .. self.width .. "\nHeight: " .. self.height .. "\n" .. #self.links .. " links\n" .. #self.npcs .. " npcs"
end,
__concat = function(left, right)
return tostring(left) .. tostring(right)
end,
}
}
map.__map_meta = metatables.map
setmetatable(map, metatables.map_main)
-- Set global variable
_G.map = map
-- Return table
return map