| 1 |
--[[ Copyright (c) 2013 William "sadger" Gatens |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
class "EntityMap" |
| 22 |
|
| 23 |
---@type EntityMap |
| 24 |
local EntityMap = _G["EntityMap"] |
| 25 |
|
| 26 |
--[[ An entity map is a structure is a 2 dimensional structure created from a |
| 27 |
game map, it has the same dimensions as the game map which intitalises it. |
| 28 |
The purpose of the map is store the location of entities in the map in |
| 29 |
real-time. Each cell given by an (x, y) coordinate is a |
| 30 |
table {humanoids ={}, objects = {}} where the tables of humanoids/objects |
| 31 |
may be empty or contain the entity/entities that currently exist in that tile.]] |
| 32 |
function EntityMap:EntityMap(map) |
| 33 |
self.width, self.height = map.th:size() |
| 34 |
self.entity_map = {} |
| 35 |
for x = 1, self.width do |
| 36 |
self.entity_map[x] = {} |
| 37 |
for y = 1, self.height do |
| 38 |
self.entity_map[x][y] = {humanoids = {}, objects = {}} |
| 39 |
end |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
local function add_entity_to_table(entity, entity_table) |
| 44 |
entity_table[#entity_table+1] = entity |
| 45 |
end |
| 46 |
|
| 47 |
--[[Adds an entity to the entity map in a specified location |
| 48 |
@param x (integer) the x coordinate of the entity |
| 49 |
@param y (integer) the y coordinate of the entity |
| 50 |
@param entity (Entity) the entity to be added to the map in the (x, y) position]] |
| 51 |
function EntityMap:addEntity(x, y, entity) |
| 52 |
-- Distinguish between entity types and add them |
| 53 |
-- to the respective tables |
| 54 |
if x and y and entity then |
| 55 |
if class.is(entity, Humanoid) then |
| 56 |
add_entity_to_table(entity, self:getHumanoidsAtCoordinate(x, y)) |
| 57 |
elseif class.is(entity, Object) then |
| 58 |
add_entity_to_table(entity, self:getObjectsAtCoordinate(x, y)) |
| 59 |
end |
| 60 |
end |
| 61 |
end |
| 62 |
|
| 63 |
-- Iterates through a table and removes the given entity |
| 64 |
local function remove_entity_from_table(entity, entity_table) |
| 65 |
local index = -1 |
| 66 |
for i, h in ipairs(entity_table) do |
| 67 |
if h == entity then |
| 68 |
-- If we have found it no need to keep looking |
| 69 |
index = i |
| 70 |
break |
| 71 |
end |
| 72 |
end |
| 73 |
-- We haven't found it so we have incorrectly tried to remove it |
| 74 |
assert(index ~= -1, "Tried to remove entity from entity_map - entity not found") |
| 75 |
table.remove(entity_table, index) |
| 76 |
end |
| 77 |
|
| 78 |
--[[Removes an entity from the entity map given specified location |
| 79 |
Assumes the entity already has been added in the (x, y) position or will cause |
| 80 |
the assertion that the entity is contained in the map to fail. |
| 81 |
@param x (integer) the x coordinate of the entity |
| 82 |
@param y (integer) the y coordinate of the entity |
| 83 |
@param entity (Entity) the entity to be removed from the map in the (x, y) position ]] |
| 84 |
function EntityMap:removeEntity(x, y, entity) |
| 85 |
-- Distinguish between entity types to find table to remove from |
| 86 |
-- then remove the entity from that table |
| 87 |
if x and y and entity then |
| 88 |
if class.is(entity, Humanoid) then |
| 89 |
remove_entity_from_table(entity, self:getHumanoidsAtCoordinate(x, y)) |
| 90 |
elseif class.is(entity, Object) then |
| 91 |
remove_entity_from_table(entity, self:getObjectsAtCoordinate(x, y)) |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
|
| 96 |
|
| 97 |
--[[Returns a map of all entities (objects and humanoids) at a specified coordinate |
| 98 |
@param x (integer) the x coordinate to retrieve entities from |
| 99 |
@param y (integer) the y coordinate to retrieve entities from |
| 100 |
@return entity_map (table) containing the entities at the (x, y) coordinate ]] |
| 101 |
function EntityMap:getEntitiesAtCoordinate(x, y) |
| 102 |
--Add all the humanoids |
| 103 |
local entity_table = {} |
| 104 |
for _, obj in ipairs(self:getHumanoidsAtCoordinate(x, y)) do |
| 105 |
table.insert(entity_table, obj) |
| 106 |
end |
| 107 |
|
| 108 |
--Add all the objects |
| 109 |
for _, obj in ipairs(self:getObjectsAtCoordinate(x, y)) do |
| 110 |
table.insert(entity_table, obj) |
| 111 |
end |
| 112 |
|
| 113 |
return entity_table |
| 114 |
end |
| 115 |
|
| 116 |
--[[Returns a table of all humanoids at a specified coordinate |
| 117 |
! Please note that when a humanoid moves, they will only be |
| 118 |
attached to one tile at the moment. When moving from tile to |
| 119 |
tile, they will only attach to the next tile after they has fully |
| 120 |
completed the animation for moving to the that next tile. |
| 121 |
Therefore, at the last frame of the transition animation from |
| 122 |
tile to tile, they will still attached to the previous one tile, on |
| 123 |
which they is no longer visually located. |
| 124 |
@param x (integer) the x coordinate to retrieve entities from |
| 125 |
@param y (integer) the y coordinate to retrieve entities from |
| 126 |
@return (table) containing the humanoids at the (x, y) coordinate ]] |
| 127 |
function EntityMap:getHumanoidsAtCoordinate(x, y) |
| 128 |
assert(x >= 1 and y >= 1 and x <= self.width and y <= self.height, |
| 129 |
"Coordinate requested is out of the entity map bounds") |
| 130 |
return self.entity_map[x][y]["humanoids"] |
| 131 |
end |
| 132 |
|
| 133 |
--[[Returns a table of all objects at a specified coordinate |
| 134 |
@param x (integer) the x coordinate to retrieve entities from |
| 135 |
@param y (integer) the y coordinate to retrieve entities from |
| 136 |
@return (table) containing the objects at the (x, y) coordinate ]] |
| 137 |
function EntityMap:getObjectsAtCoordinate(x, y) |
| 138 |
assert(x >= 1 and y >= 1 and x <= self.width and y <= self.height, |
| 139 |
"Coordinate requested is out of the entity map bounds") |
| 140 |
return self.entity_map[x][y]["objects"] |
| 141 |
end |
| 142 |
|
| 143 |
|
| 144 |
--[[ Returns a map of coordinates {{x1, y1}, ... {xn, yn}} directly |
| 145 |
adjacent to a given (x, y) coordinate - no diagonals |
| 146 |
@param x (integer) the x coordinate to obtain adjacent tiles from |
| 147 |
@param y (integer) the y coordinate to obtain adjacent tiles from |
| 148 |
@return (table) containing coordinates {x=integer, y=integer} ]] |
| 149 |
function EntityMap:getAdjacentSquares(x, y) |
| 150 |
local adjacent_squares = {} |
| 151 |
if x and y then |
| 152 |
if x ~= 1 then |
| 153 |
adjacent_squares[#adjacent_squares+1] = {x = x - 1, y = y} |
| 154 |
end |
| 155 |
if x ~= self.width then |
| 156 |
adjacent_squares[#adjacent_squares+1] = {x = x + 1, y = y} |
| 157 |
end |
| 158 |
if y ~= 1 then |
| 159 |
adjacent_squares[#adjacent_squares+1] = {x = x, y = y - 1} |
| 160 |
end |
| 161 |
if y ~= self.height then |
| 162 |
adjacent_squares[#adjacent_squares+1] = {x = x, y = y + 1} |
| 163 |
end |
| 164 |
end |
| 165 |
return adjacent_squares |
| 166 |
end |
| 167 |
|
| 168 |
--[[ Returns all humanoids in the squares directly adjacent |
| 169 |
to the given (x, y) coordinate - diagonals are not considered |
| 170 |
@param x (integer) the x coordinate to obtain adjacent patients from |
| 171 |
@param y (integer) the y coordinate to obtain adjacent patients from ]] |
| 172 |
function EntityMap:getHumanoidsInSquareAndInAdjacentSquares(x, y) |
| 173 |
local adjacent_patients = {} |
| 174 |
local tiles = table_merge({{x = x , y = y}}, self:getAdjacentSquares(x, y)) |
| 175 |
for _, coord in ipairs(tiles) do |
| 176 |
for _, humanoid in ipairs(self:getHumanoidsAtCoordinate(coord.x, coord.y)) do |
| 177 |
adjacent_patients[#adjacent_patients + 1] = humanoid |
| 178 |
end |
| 179 |
end |
| 180 |
return adjacent_patients |
| 181 |
end |
| 182 |
|
| 183 |
--[[ Returns all the entities which are patients in the squares directly adjacent |
| 184 |
to the given (x, y) coordinate - diagonals are not considered |
| 185 |
@param x (integer) the x coordinate to obtain adjacent patients from |
| 186 |
@param y (integer) the y coordinate to obtain adjacent patients from ]] |
| 187 |
function EntityMap:getPatientsInAdjacentSquares(x, y) |
| 188 |
local adjacent_patients = {} |
| 189 |
for _, coord in ipairs(self:getAdjacentSquares(x, y)) do |
| 190 |
for _, humanoid in ipairs(self:getHumanoidsAtCoordinate(coord.x, coord.y)) do |
| 191 |
if class.is(humanoid, Patient) then |
| 192 |
adjacent_patients[#adjacent_patients + 1] = humanoid |
| 193 |
end |
| 194 |
end |
| 195 |
end |
| 196 |
return adjacent_patients |
| 197 |
end |
| 198 |
|
| 199 |
--[[ Returns a map of coordinates {{x1, y1}, ... {xn, yn}} of |
| 200 |
the adjacent tiles (no diagonals) which do not contain any humanoids or objects |
| 201 |
does NOT determine if the tile is reachable from (x, y) may even be in a |
| 202 |
different room |
| 203 |
@param x (integer) the x coordinate to obtain free tiles from |
| 204 |
@param y (integer) the y coordinate to obtain free tiles from ]] |
| 205 |
function EntityMap:getAdjacentFreeTiles(x, y) |
| 206 |
local adjacent_free_tiles = {} |
| 207 |
for _, coord in ipairs(self:getAdjacentSquares(x, y)) do |
| 208 |
local x_coord = coord['x'] |
| 209 |
local y_coord = coord['y'] |
| 210 |
-- If no object or humanoid occupies the tile_coordinate, it is a free tile. |
| 211 |
if #self:getHumanoidsAtCoordinate(x_coord, y_coord) == 0 and |
| 212 |
#self:getObjectsAtCoordinate(x_coord, y_coord) == 0 then |
| 213 |
adjacent_free_tiles[#adjacent_free_tiles+1] = coord |
| 214 |
end |
| 215 |
end |
| 216 |
return adjacent_free_tiles |
| 217 |
end |
| 218 |
|