/
entity_map.lua
196 lines (175 loc) · 7.69 KB
/
entity_map.lua
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
--[[ Copyright (c) 2013 William "sadger" Gatens
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. --]]
class "EntityMap"
---@type EntityMap
local EntityMap = _G["EntityMap"]
--[[ An entity map is a structure is a 2 dimensional structure created from a
game map, it has the same dimensions as the game map which intitalises it.
The purpose of the map is store the location of entities in the map in
real-time. Each cell given by an (x, y) coordinate is a
table {humanoids ={}, objects = {}} where the tables of humanoids/objects
may be empty or contain the entity/entities that currently exist in that tile.]]
function EntityMap:EntityMap(map)
self.width, self.height = map.th:size()
self.entity_map = {}
for x = 1, self.width do
self.entity_map[x] = {}
for y = 1, self.height do
self.entity_map[x][y] = {humanoids = {}, objects = {}}
end
end
end
local function add_entity_to_table(entity, entity_table)
entity_table[#entity_table+1] = entity
end
--[[Adds an entity to the entity map in a specified location
@param x (integer) the x coordinate of the entity
@param y (integer) the y coordinate of the entity
@param entity (Entity) the entity to be added to the map in the (x, y) position]]
function EntityMap:addEntity(x, y, entity)
-- Distinguish between entity types and add them
-- to the respective tables
if x and y and entity then
if class.is(entity, Humanoid) then
add_entity_to_table(entity, self:getHumanoidsAtCoordinate(x, y))
elseif class.is(entity, Object) then
add_entity_to_table(entity, self:getObjectsAtCoordinate(x, y))
end
end
end
-- Iterates through a table and removes the given entity
local function remove_entity_from_table(entity, entity_table)
local index = -1
for i, h in ipairs(entity_table) do
if h == entity then
-- If we have found it no need to keep looking
index = i
break
end
end
-- We haven't found it so we have incorrectly tried to remove it
assert(index ~= -1, "Tried to remove entity from entity_map - entity not found")
table.remove(entity_table, index)
end
--[[Removes an entity from the entity map given specified location
Assumes the entity already has been added in the (x, y) position or will cause
the assertion that the entity is contained in the map to fail.
@param x (integer) the x coordinate of the entity
@param y (integer) the y coordinate of the entity
@param entity (Entity) the entity to be removed from the map in the (x, y) position ]]
function EntityMap:removeEntity(x, y, entity)
-- Distinguish between entity types to find table to remove from
-- then remove the entity from that table
if x and y and entity then
if class.is(entity, Humanoid) then
remove_entity_from_table(entity, self:getHumanoidsAtCoordinate(x, y))
elseif class.is(entity, Object) then
remove_entity_from_table(entity, self:getObjectsAtCoordinate(x, y))
end
end
end
--[[Returns a map of all entities (objects and humanoids) at a specified coordinate
@param x (integer) the x coordinate to retrieve entities from
@param y (integer) the y coordinate to retrieve entities from
@return entity_map (table) containing the entities at the (x, y) coordinate ]]
function EntityMap:getEntitiesAtCoordinate(x, y)
--Add all the humanoids
local entity_table = {}
for _, obj in ipairs(self:getHumanoidsAtCoordinate(x, y)) do
table.insert(entity_table, obj)
end
--Add all the objects
for _, obj in ipairs(self:getObjectsAtCoordinate(x, y)) do
table.insert(entity_table, obj)
end
return entity_table
end
--[[Returns a table of all humanoids at a specified coordinate
@param x (integer) the x coordinate to retrieve entities from
@param y (integer) the y coordinate to retrieve entities from
@return (table) containing the humanoids at the (x, y) coordinate ]]
function EntityMap:getHumanoidsAtCoordinate(x, y)
assert(x >= 1 and y >= 1 and x <= self.width and y <= self.height,
"Coordinate requested is out of the entity map bounds")
return self.entity_map[x][y]["humanoids"]
end
--[[Returns a table of all objects at a specified coordinate
@param x (integer) the x coordinate to retrieve entities from
@param y (integer) the y coordinate to retrieve entities from
@return (table) containing the objects at the (x, y) coordinate ]]
function EntityMap:getObjectsAtCoordinate(x, y)
assert(x >= 1 and y >= 1 and x <= self.width and y <= self.height,
"Coordinate requested is out of the entity map bounds")
return self.entity_map[x][y]["objects"]
end
--[[ Returns a map of coordinates {{x1, y1}, ... {xn, yn}} directly
adjacent to a given (x, y) coordinate - no diagonals
@param x (integer) the x coordinate to obtain adjacent tiles from
@param y (integer) the y coordinate to obtain adjacent tiles from ]]
function EntityMap:getAdjacentSquares(x, y)
-- Table of coordinates {x=integer, y=integer} representing (x, y) coordinates
local adjacent_squares = {}
if x and y then
if x ~= 1 then
adjacent_squares[#adjacent_squares+1] = {x = x-1, y = y}
end
if x ~= self.width then
adjacent_squares[#adjacent_squares+1] = {x = x+1, y = y}
end
if y ~= 1 then
adjacent_squares[#adjacent_squares+1] = {x = x, y = y-1}
end
if y ~= self.height then
adjacent_squares[#adjacent_squares+1] = {x = x, y = y+1}
end
end
return adjacent_squares
end
--[[ Returns all the entities which are patients in the squares directly adjacent
to the given (x, y) coordinate - diagonals are not considered
@param x (integer) the x coordinate to obtain adjacent patients from
@param y (integer) the y coordinate to obtain adjacent patients from ]]
function EntityMap:getPatientsInAdjacentSquares(x, y)
local adjacent_patients = {}
for _, coord in ipairs(self:getAdjacentSquares(x, y)) do
for _, humanoid in ipairs(self:getHumanoidsAtCoordinate(coord.x, coord.y)) do
if class.is(humanoid, Patient) then
adjacent_patients[#adjacent_patients + 1] = humanoid
end
end
end
return adjacent_patients
end
--[[ Returns a map of coordinates {{x1, y1}, ... {xn, yn}} of
the adjacent tiles (no diagonals) which do not contain any humanoids or objects
does NOT determine if the tile is reachable from (x, y) may even be in a
different room
@param x (integer) the x coordinate to obtain free tiles from
@param y (integer) the y coordinate to obtain free tiles from ]]
function EntityMap:getAdjacentFreeTiles(x, y)
local adjacent_free_tiles = {}
for _, coord in ipairs(self:getAdjacentSquares(x, y)) do
local x_coord = coord['x']
local y_coord = coord['y']
-- If no object or humanoid occupy the til_coorde
if #self:getHumanoidsAtCoordinate(x_coord, y_coord) == 0 and
#self:getObjectsAtCoordinate(x_coord, y_coord) == 0 then
adjacent_free_tiles[#adjacent_free_tiles+1] = coord
end
end
return adjacent_free_tiles
end