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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
|
/***************************************************************************
* Copyright (C) 2005-2019 by the FIFE team *
* http://www.fifengine.net *
* This file is part of FIFE. *
* *
* FIFE is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
// Standard C++ library includes
// 3rd party library includes
// FIFE includes
// These includes are split up in two parts, separated by one empty line
// First block: files included from the FIFE root src directory
// Second block: files included from the same folder
#include "util/log/logger.h"
#include "util/structures/purge.h"
#include "model/metamodel/grids/cellgrid.h"
#include "layer.h"
#include "instance.h"
#include "map.h"
#include "instancetree.h"
#include "cell.h"
#include "cellcache.h"
#include "trigger.h"
namespace FIFE {
/** Logger to use for this source file.
* @relates Logger
*/
static Logger _log(LM_STRUCTURES);
Layer::Layer(const std::string& identifier, Map* map, CellGrid* grid)
: m_id(identifier),
m_map(map),
m_instancesVisibility(true),
m_transparency(0),
m_instanceTree(new InstanceTree()),
m_grid(grid),
m_pathingStrategy(CELL_EDGES_ONLY),
m_sortingStrategy(SORTING_CAMERA),
m_walkable(false),
m_interact(false),
m_walkableId(""),
m_cellCache(NULL),
m_changeListeners(),
m_changedInstances(),
m_changed(false),
m_static(false) {
}
Layer::~Layer() {
// if this is a walkable layer
destroyCellCache();
// if this is a interact layer
if (m_interact) {
Layer* temp = m_map->getLayer(m_walkableId);
if (temp) {
temp->removeInteractLayer(this);
}
}
purge(m_instances);
delete m_instanceTree;
}
const std::string& Layer::getId() const {
return m_id;
}
void Layer::setId(const std::string& id) {
m_id = id;
}
Map* Layer::getMap() const {
return m_map;
}
CellGrid* Layer::getCellGrid() const {
return m_grid;
}
void Layer::setCellGrid(CellGrid* grid) {
m_grid = grid;
}
InstanceTree* Layer::getInstanceTree(void) const {
return m_instanceTree;
}
bool Layer::hasInstances() const {
return !m_instances.empty();
}
Instance* Layer::createInstance(Object* object, const ModelCoordinate& p, const std::string& id) {
ExactModelCoordinate emc(static_cast<double>(p.x), static_cast<double>(p.y), static_cast<double>(p.z));
return createInstance(object, emc, id);
}
Instance* Layer::createInstance(Object* object, const ExactModelCoordinate& p, const std::string& id) {
Location location(this);
location.setExactLayerCoordinates(p);
Instance* instance = new Instance(object, location, id);
if(instance->isActive()) {
setInstanceActivityStatus(instance, instance->isActive());
}
m_instances.push_back(instance);
m_instanceTree->addInstance(instance);
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
(*i)->onInstanceCreate(this, instance);
++i;
}
m_changed = true;
return instance;
}
bool Layer::addInstance(Instance* instance, const ExactModelCoordinate& p){
if( !instance ){
FL_ERR(_log, "Tried to add an instance to layer, but given instance is invalid");
return false;
}
Location& location = instance->getLocationRef();
location.setLayer(this);
location.setExactLayerCoordinates(p);
m_instances.push_back(instance);
m_instanceTree->addInstance(instance);
if(instance->isActive()) {
setInstanceActivityStatus(instance, instance->isActive());
}
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
(*i)->onInstanceCreate(this, instance);
++i;
}
m_changed = true;
return true;
}
void Layer::removeInstance(Instance* instance) {
// If the instance is changed and removed on the same pump,
// it can happen that the instance can not cleanly be removed,
// to avoid this we have to update the instance first and send
// the result to the LayerChangeListeners.
if (instance->isActive()) {
if (instance->update() != ICHANGE_NO_CHANGES) {
std::vector<Instance*> updateInstances;
updateInstances.push_back(instance);
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
(*i)->onLayerChanged(this, updateInstances);
++i;
}
}
}
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
(*i)->onInstanceDelete(this, instance);
++i;
}
setInstanceActivityStatus(instance, false);
std::vector<Instance*>::iterator it = m_instances.begin();
for(; it != m_instances.end(); ++it) {
if(*it == instance) {
m_instanceTree->removeInstance(*it);
m_instances.erase(it);
break;
}
}
m_changed = true;
}
void Layer::deleteInstance(Instance* instance) {
// If the instance is changed and deleted on the same pump,
// it can happen that the instance can not cleanly be removed,
// to avoid this we have to update the instance first and send
// the result to the LayerChangeListeners.
if (instance->isActive()) {
if (instance->update() != ICHANGE_NO_CHANGES) {
std::vector<Instance*> updateInstances;
updateInstances.push_back(instance);
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
(*i)->onLayerChanged(this, updateInstances);
++i;
}
}
}
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
(*i)->onInstanceDelete(this, instance);
++i;
}
setInstanceActivityStatus(instance, false);
std::vector<Instance*>::iterator it = m_instances.begin();
for(; it != m_instances.end(); ++it) {
if(*it == instance) {
m_instanceTree->removeInstance(*it);
delete *it;
m_instances.erase(it);
break;
}
}
m_changed = true;
}
const std::vector<Instance*>& Layer::getInstances() const {
return m_instances;
}
void Layer::setInstanceActivityStatus(Instance* instance, bool active) {
if(active) {
m_activeInstances.insert(instance);
} else {
m_activeInstances.erase(instance);
}
}
Instance* Layer::getInstance(const std::string& id) {
std::vector<Instance*>::iterator it = m_instances.begin();
for(; it != m_instances.end(); ++it) {
if((*it)->getId() == id)
return *it;
}
return 0;
}
std::vector<Instance*> Layer::getInstances(const std::string& id) {
std::vector<Instance*> matching_instances;
std::vector<Instance*>::iterator it = m_instances.begin();
for(; it != m_instances.end(); ++it) {
if((*it)->getId() == id)
matching_instances.push_back(*it);
}
return matching_instances;
}
std::vector<Instance*> Layer::getInstancesAt(Location& loc, bool use_exactcoordinates) {
std::vector<Instance*> matching_instances;
std::vector<Instance*>::iterator it = m_instances.begin();
for(; it != m_instances.end(); ++it) {
if (use_exactcoordinates) {
if ((*it)->getLocationRef().getExactLayerCoordinatesRef() == loc.getExactLayerCoordinatesRef()) {
matching_instances.push_back(*it);
}
} else {
if ((*it)->getLocationRef().getLayerCoordinates() == loc.getLayerCoordinates()) {
matching_instances.push_back(*it);
}
}
}
return matching_instances;
}
std::list<Instance*> Layer::getInstancesIn(Rect& rec) {
std::list<Instance*> matching_instances;
ModelCoordinate mc(rec.x, rec.y);
m_instanceTree->findInstances(mc, rec.w, rec.h, matching_instances);
return matching_instances;
}
std::vector<Instance*> Layer::getInstancesInLine(const ModelCoordinate& pt1, const ModelCoordinate& pt2) {
std::vector<Instance*> instances;
std::list<Instance*> matchingInstances;
std::vector<ModelCoordinate> coords = m_grid->getCoordinatesInLine(pt1, pt2);
for (std::vector<ModelCoordinate>::iterator it = coords.begin(); it != coords.end(); ++it) {
m_instanceTree->findInstances(*it, 0, 0, matchingInstances);
if (!matchingInstances.empty()) {
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end());
}
}
return instances;
}
std::vector<Instance*> Layer::getInstancesInCircle(const ModelCoordinate& center, uint16_t radius) {
std::vector<Instance*> instances;
std::list<Instance*> matchingInstances;
//radius power 2
uint16_t radiusp2 = (radius+1) * radius;
ModelCoordinate current(center.x-radius, center.y-radius);
ModelCoordinate target(center.x+radius, center.y+radius);
for (; current.y < center.y; current.y++) {
current.x = center.x-radius;
for (; current.x < center.x; current.x++) {
uint16_t dx = center.x - current.x;
uint16_t dy = center.y - current.y;
uint16_t distance = dx*dx + dy*dy;
if (distance <= radiusp2) {
m_instanceTree->findInstances(current, 0, 0, matchingInstances);
if (!matchingInstances.empty()) {
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end());
}
current.x = center.x + dx;
m_instanceTree->findInstances(current, 0, 0, matchingInstances);
if (!matchingInstances.empty()) {
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end());
}
current.y = center.y + dy;
m_instanceTree->findInstances(current, 0, 0, matchingInstances);
if (!matchingInstances.empty()) {
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end());
}
current.x = center.x-dx;
m_instanceTree->findInstances(current, 0, 0, matchingInstances);
if (!matchingInstances.empty()) {
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end());
}
current.y = center.y-dy;
}
}
}
current.x = center.x;
current.y = center.y-radius;
for (; current.y <= target.y; current.y++) {
m_instanceTree->findInstances(current, 0, 0, matchingInstances);
if (!matchingInstances.empty()) {
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end());
}
}
current.y = center.y;
current.x = center.x-radius;
for (; current.x <= target.x; current.x++) {
m_instanceTree->findInstances(current, 0, 0, matchingInstances);
if (!matchingInstances.empty()) {
instances.insert(instances.end(), matchingInstances.begin(), matchingInstances.end());
}
}
return instances;
}
std::vector<Instance*> Layer::getInstancesInCircleSegment(const ModelCoordinate& center, uint16_t radius, int32_t sangle, int32_t eangle) {
std::vector<Instance*> instances;
ExactModelCoordinate exactCenter(center.x, center.y);
std::vector<Instance*> tmpInstances = getInstancesInCircle(center, radius);
int32_t s = (sangle + 360) % 360;
int32_t e = (eangle + 360) % 360;
bool greater = (s > e) ? true : false;
for (std::vector<Instance*>::iterator it = tmpInstances.begin(); it != tmpInstances.end(); ++it) {
int32_t angle = getAngleBetween(exactCenter, intPt2doublePt((*it)->getLocationRef().getLayerCoordinates()));
if (greater) {
if (angle >= s || angle <= e) {
instances.push_back(*it);
}
} else {
if (angle >= s && angle <= e) {
instances.push_back(*it);
}
}
}
return instances;
}
void Layer::getMinMaxCoordinates(ModelCoordinate& min, ModelCoordinate& max, const Layer* layer) const {
if (!layer) {
layer = this;
}
if (m_instances.empty()) {
min = ModelCoordinate();
max = min;
} else {
min = m_instances.front()->getLocationRef().getLayerCoordinates(layer);
max = min;
for (std::vector<Instance*>::const_iterator i = m_instances.begin(); i != m_instances.end(); ++i) {
ModelCoordinate coord = (*i)->getLocationRef().getLayerCoordinates(layer);
min.x = std::min(min.x, coord.x);
max.x = std::max(max.x, coord.x);
min.y = std::min(min.y, coord.y);
max.y = std::max(max.y, coord.y);
}
}
}
float Layer::getZOffset() const {
static const float globalmax = 100.0;
static const float globalrange = 200.0;
int32_t numlayers = m_map->getLayerCount();
int32_t thislayer = 1; // we don't need 0 indexed
const std::list<Layer*>& layers = m_map->getLayers();
std::list<Layer*>::const_iterator iter = layers.begin();
for (; iter != layers.end(); ++iter, ++thislayer) {
if (*iter == this) {
break;
}
}
float offset = globalmax - (numlayers - (thislayer - 1)) * (globalrange/numlayers);
return offset;
}
uint32_t Layer::getLayerCount() const {
return m_map->getLayerCount();
}
void Layer::setInstancesVisible(bool vis) {
if (m_instancesVisibility != vis) {
m_instancesVisibility = vis;
std::vector<Instance*>::iterator it = m_instances.begin();
for (; it != m_instances.end(); ++it) {
(*it)->callOnVisibleChange();
}
}
}
void Layer::setLayerTransparency(uint8_t transparency) {
if (m_transparency != transparency) {
m_transparency = transparency;
std::vector<Instance*>::iterator it = m_instances.begin();
for (; it != m_instances.end(); ++it) {
(*it)->callOnTransparencyChange();
}
}
}
uint8_t Layer::getLayerTransparency() {
return m_transparency;
}
void Layer::toggleInstancesVisible() {
setInstancesVisible(!m_instancesVisibility);
}
bool Layer::areInstancesVisible() const {
return m_instancesVisibility;
}
bool Layer::cellContainsBlockingInstance(const ModelCoordinate& cellCoordinate) {
bool blockingInstance = false;
if (m_cellCache) {
Cell* cell = m_cellCache->getCell(cellCoordinate);
if (cell) {
return cell->getCellType() != CTYPE_NO_BLOCKER;
}
} else {
std::list<Instance*> adjacentInstances;
m_instanceTree->findInstances(cellCoordinate, 0, 0, adjacentInstances);
for(std::list<Instance*>::const_iterator j = adjacentInstances.begin(); j != adjacentInstances.end(); ++j) {
if((*j)->isBlocking() && (*j)->getLocationRef().getLayerCoordinates() == cellCoordinate) {
blockingInstance = true;
break;
}
}
}
return blockingInstance;
}
std::vector<Instance*> Layer::getBlockingInstances(const ModelCoordinate& cellCoordinate) {
std::vector<Instance*> blockingInstances;
if (m_cellCache) {
Cell* cell = m_cellCache->getCell(cellCoordinate);
if (cell) {
const std::set<Instance*>& blocker = cell->getInstances();
for (std::set<Instance*>::const_iterator it = blocker.begin(); it != blocker.end(); ++it) {
if ((*it)->isBlocking()) {
blockingInstances.push_back(*it);
}
}
}
} else {
std::list<Instance*> adjacentInstances;
m_instanceTree->findInstances(cellCoordinate, 0, 0, adjacentInstances);
for(std::list<Instance*>::const_iterator j = adjacentInstances.begin(); j != adjacentInstances.end(); ++j) {
if((*j)->isBlocking() && (*j)->getLocationRef().getLayerCoordinates() == cellCoordinate) {
blockingInstances.push_back(*j);
}
}
}
return blockingInstances;
}
void Layer::setPathingStrategy(PathingStrategy strategy) {
m_pathingStrategy = strategy;
m_grid->setAllowDiagonals(m_pathingStrategy != CELL_EDGES_ONLY);
}
PathingStrategy Layer::getPathingStrategy() const {
return m_pathingStrategy;
}
void Layer::setSortingStrategy(SortingStrategy strategy) {
m_sortingStrategy = strategy;
}
SortingStrategy Layer::getSortingStrategy() const {
return m_sortingStrategy;
}
void Layer::setWalkable(bool walkable) {
m_walkable = walkable;
}
bool Layer::isWalkable() {
return m_walkable;
}
void Layer::setInteract(bool interact, const std::string& id) {
m_interact = interact;
m_walkableId = id;
}
bool Layer::isInteract() {
return m_interact;
}
const std::string& Layer::getWalkableId() {
return m_walkableId;
}
void Layer::addInteractLayer(Layer* layer) {
if (m_walkable) {
m_interacts.push_back(layer);
}
}
const std::vector<Layer*>& Layer::getInteractLayers() {
return m_interacts;
}
void Layer::removeInteractLayer(Layer* layer) {
if (m_walkable) {
std::vector<Layer*>::iterator it = m_interacts.begin();
for (; it != m_interacts.end(); ++it) {
if (*it == layer) {
(*it)->removeChangeListener(m_cellCache->getCellCacheChangeListener());
m_interacts.erase(it);
break;
}
}
}
}
void Layer::createCellCache() {
if (!m_cellCache && m_walkable) {
m_cellCache = new CellCache(this);
}
}
CellCache* Layer::getCellCache() {
return m_cellCache;
}
void Layer::destroyCellCache() {
if (m_walkable) {
removeChangeListener(m_cellCache->getCellCacheChangeListener());
if (!m_interacts.empty()) {
std::vector<Layer*>::iterator it = m_interacts.begin();
for (; it != m_interacts.end(); ++it) {
(*it)->removeChangeListener(m_cellCache->getCellCacheChangeListener());
(*it)->setInteract(false, "");
}
m_interacts.clear();
}
delete m_cellCache;
m_cellCache = NULL;
m_walkable = false;
}
}
bool Layer::update() {
m_changedInstances.clear();
std::vector<Instance*> inactiveInstances;
std::set<Instance*>::iterator it = m_activeInstances.begin();
for(; it != m_activeInstances.end(); ++it) {
if ((*it)->update() != ICHANGE_NO_CHANGES) {
m_changedInstances.push_back(*it);
m_changed = true;
} else if (!(*it)->isActive()) {
inactiveInstances.push_back(*it);
}
}
if (!m_changedInstances.empty()) {
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
(*i)->onLayerChanged(this, m_changedInstances);
++i;
}
//std::cout << "Layer named " << Id() << " changed = 1\n";
}
// remove inactive instances from m_activeInstances
if (!inactiveInstances.empty()) {
std::vector<Instance*>::iterator i = inactiveInstances.begin();
while (i != inactiveInstances.end()) {
m_activeInstances.erase(*i);
++i;
}
}
//std::cout << "Layer named " << Id() << " changed = 0\n";
bool retval = m_changed;
m_changed = false;
return retval;
}
void Layer::addChangeListener(LayerChangeListener* listener) {
m_changeListeners.push_back(listener);
}
void Layer::removeChangeListener(LayerChangeListener* listener) {
std::vector<LayerChangeListener*>::iterator i = m_changeListeners.begin();
while (i != m_changeListeners.end()) {
if ((*i) == listener) {
m_changeListeners.erase(i);
return;
}
++i;
}
}
bool Layer::isChanged() {
return m_changed;
}
std::vector<Instance*>& Layer::getChangedInstances() {
return m_changedInstances;
}
void Layer::setStatic(bool stati) {
m_static = stati;
}
bool Layer::isStatic() {
return m_static;
}
} // FIFE
|