| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
|
| 24 |
// 3rd party library includes |
| 25 |
|
| 26 |
// FIFE includes |
| 27 |
// These includes are split up in two parts, separated by one empty line |
| 28 |
// First block: files included from the FIFE root src directory |
| 29 |
// Second block: files included from the same folder |
| 30 |
#include "model/metamodel/modelcoords.h" |
| 31 |
#include "util/log/logger.h" |
| 32 |
|
| 33 |
#include "cell.h" |
| 34 |
#include "cellcache.h" |
| 35 |
#include "map.h" |
| 36 |
#include "trigger.h" |
| 37 |
#include "triggercontroller.h" |
| 38 |
|
| 39 |
namespace FIFE { |
| 40 |
|
| 41 |
static Logger _log(LM_STRUCTURES); |
| 42 |
|
| 43 |
TriggerController::TriggerController(Map* map) : |
| 44 |
m_map(map) { |
| 45 |
} |
| 46 |
|
| 47 |
TriggerController::~TriggerController() { |
| 48 |
TriggerNameMapIterator it = m_triggerNameMap.begin(); |
| 49 |
for (; it != m_triggerNameMap.end(); ++it) { |
| 50 |
delete it->second; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
Trigger* TriggerController::createTrigger(const std::string& triggerName) { |
| 55 |
//assert(!exists(triggerName)); |
| 56 |
|
| 57 |
Trigger* trigger = new Trigger(triggerName); |
| 58 |
|
| 59 |
std::pair<TriggerNameMapIterator, bool> returnValue; |
| 60 |
returnValue = m_triggerNameMap.insert ( TriggerNameMapPair(triggerName, trigger)); |
| 61 |
|
| 62 |
if (!returnValue.second) { |
| 63 |
delete trigger; |
| 64 |
FL_WARN(_log, LMsg("TriggerController::createTrigger() - ") << "Trigger " << triggerName << " already exists.... ignoring."); |
| 65 |
} |
| 66 |
|
| 67 |
return returnValue.first->second; |
| 68 |
} |
| 69 |
|
| 70 |
Trigger* TriggerController::createTriggerOnCoordinate(const std::string& triggerName, Layer* layer, const ModelCoordinate& pt) { |
| 71 |
assert(layer); |
| 72 |
|
| 73 |
Trigger* trigger = createTrigger(triggerName); |
| 74 |
trigger->assign(layer, pt); |
| 75 |
return trigger; |
| 76 |
} |
| 77 |
|
| 78 |
Trigger* TriggerController::createTriggerOnCoordinates(const std::string& triggerName, Layer* layer, const std::vector<ModelCoordinate>& coords) { |
| 79 |
assert(layer); |
| 80 |
|
| 81 |
Trigger* trigger = createTrigger(triggerName); |
| 82 |
for (std::vector<ModelCoordinate>::const_iterator it = coords.begin(); it != coords.end(); ++it) { |
| 83 |
trigger->assign(layer, *it); |
| 84 |
} |
| 85 |
return trigger; |
| 86 |
} |
| 87 |
|
| 88 |
Trigger* TriggerController::createTriggerOnRect(const std::string& triggerName, Layer* layer, const Rect& rec) { |
| 89 |
assert(layer); |
| 90 |
assert(layer->getCellCache()); |
| 91 |
|
| 92 |
Trigger* trigger = createTrigger(triggerName); |
| 93 |
std::vector<Cell*> cells = layer->getCellCache()->getCellsInRect(rec); |
| 94 |
for (std::vector<Cell*>::iterator it = cells.begin(); it != cells.end(); ++it) { |
| 95 |
trigger->assign(*it); |
| 96 |
} |
| 97 |
return trigger; |
| 98 |
} |
| 99 |
|
| 100 |
Trigger* TriggerController::createTriggerOnLocation(const std::string& triggerName, const Location& loc) { |
| 101 |
assert(loc.getLayer()); |
| 102 |
assert(loc.getLayer()->getCellCache()); |
| 103 |
|
| 104 |
Trigger* trigger = createTrigger(triggerName); |
| 105 |
trigger->assign(loc.getLayer(), loc.getLayerCoordinates()); |
| 106 |
return trigger; |
| 107 |
} |
| 108 |
|
| 109 |
Trigger* TriggerController::createTriggerOnLocations(const std::string& triggerName, const std::vector<Location>& locs) { |
| 110 |
Trigger* trigger = createTrigger(triggerName); |
| 111 |
for (std::vector<Location>::const_iterator it = locs.begin(); it != locs.end(); ++it) { |
| 112 |
trigger->assign((*it).getLayer(), (*it).getLayerCoordinates()); |
| 113 |
} |
| 114 |
return trigger; |
| 115 |
} |
| 116 |
|
| 117 |
Trigger* TriggerController::createTriggerOnCell(const std::string& triggerName, Cell* cell) { |
| 118 |
assert(cell); |
| 119 |
|
| 120 |
Trigger* trigger = createTrigger(triggerName); |
| 121 |
trigger->assign(cell); |
| 122 |
return trigger; |
| 123 |
} |
| 124 |
|
| 125 |
Trigger* TriggerController::createTriggerOnCells(const std::string& triggerName, const std::vector<Cell*>& cells) { |
| 126 |
Trigger* trigger = createTrigger(triggerName); |
| 127 |
for (std::vector<Cell*>::const_iterator it = cells.begin(); it != cells.end(); ++it) { |
| 128 |
trigger->assign(*it); |
| 129 |
} |
| 130 |
return trigger; |
| 131 |
} |
| 132 |
|
| 133 |
Trigger* TriggerController::createTriggerOnInstance(const std::string& triggerName, Instance* instance) { |
| 134 |
assert(instance); |
| 135 |
|
| 136 |
Trigger* trigger = createTrigger(triggerName); |
| 137 |
trigger->attach(instance); |
| 138 |
return trigger; |
| 139 |
} |
| 140 |
|
| 141 |
Trigger* TriggerController::getTrigger(const std::string& triggerName) { |
| 142 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 143 |
if (it != m_triggerNameMap.end()) { |
| 144 |
return it->second; |
| 145 |
} |
| 146 |
return NULL; |
| 147 |
} |
| 148 |
|
| 149 |
void TriggerController::deleteTrigger(const std::string& triggerName) { |
| 150 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 151 |
if (it != m_triggerNameMap.end()) { |
| 152 |
delete it->second; |
| 153 |
m_triggerNameMap.erase(it); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
void TriggerController::removeTriggerFromCoordinate(const std::string& triggerName, Layer* layer, const ModelCoordinate& pt) { |
| 158 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 159 |
if (it != m_triggerNameMap.end()) { |
| 160 |
it->second->remove(layer, pt); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void TriggerController::removeTriggerFromCoordinates(const std::string& triggerName, Layer* layer, const std::vector<ModelCoordinate>& coords) { |
| 165 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 166 |
if (it != m_triggerNameMap.end()) { |
| 167 |
for (std::vector<ModelCoordinate>::const_iterator cit = coords.begin(); cit != coords.end(); ++cit) { |
| 168 |
it->second->remove(layer, *cit); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void TriggerController::removeTriggerFromRect(const std::string& triggerName, Layer* layer, const Rect& rec) { |
| 174 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 175 |
if (it != m_triggerNameMap.end()) { |
| 176 |
std::vector<Cell*> cells = layer->getCellCache()->getCellsInRect(rec); |
| 177 |
for (std::vector<Cell*>::iterator cit = cells.begin(); cit != cells.end(); ++cit) { |
| 178 |
it->second->remove(*cit); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void TriggerController::removeTriggerFromLocation(const std::string& triggerName, const Location& loc) { |
| 184 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 185 |
if (it != m_triggerNameMap.end()) { |
| 186 |
it->second->remove(loc.getLayer(), loc.getLayerCoordinates()); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void TriggerController::removeTriggerFromLocations(const std::string& triggerName, const std::vector<Location>& locs) { |
| 191 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 192 |
if (it != m_triggerNameMap.end()) { |
| 193 |
for (std::vector<Location>::const_iterator cit = locs.begin(); cit != locs.end(); ++cit) { |
| 194 |
it->second->remove((*cit).getLayer(), (*cit).getLayerCoordinates()); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
void TriggerController::removeTriggerFromCell(const std::string& triggerName, Cell* cell) { |
| 200 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 201 |
if (it != m_triggerNameMap.end()) { |
| 202 |
it->second->remove(cell); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void TriggerController::removeTriggerFromCells(const std::string& triggerName, const std::vector<Cell*>& cells) { |
| 207 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 208 |
if (it != m_triggerNameMap.end()) { |
| 209 |
for (std::vector<Cell*>::const_iterator cit = cells.begin(); cit != cells.end(); ++cit) { |
| 210 |
it->second->remove(*cit); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void TriggerController::removeTriggerFromInstance(const std::string& triggerName, Instance* instance) { |
| 216 |
TriggerNameMapIterator it = m_triggerNameMap.find(triggerName); |
| 217 |
if (it != m_triggerNameMap.end()) { |
| 218 |
if (it->second->getAttached() == instance) { |
| 219 |
it->second->detach(); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
std::vector<Trigger*> TriggerController::getAllTriggers() { |
| 225 |
std::vector<Trigger*> triggers; |
| 226 |
TriggerNameMapIterator it = m_triggerNameMap.begin(); |
| 227 |
for (; it != m_triggerNameMap.end(); ++it) { |
| 228 |
triggers.push_back(it->second); |
| 229 |
} |
| 230 |
return triggers; |
| 231 |
} |
| 232 |
|
| 233 |
std::vector<std::string> TriggerController::getAllTriggerNames() { |
| 234 |
std::vector<std::string> names; |
| 235 |
TriggerNameMapIterator it = m_triggerNameMap.begin(); |
| 236 |
for (; it != m_triggerNameMap.end(); ++it) { |
| 237 |
names.push_back(it->first); |
| 238 |
} |
| 239 |
return names; |
| 240 |
} |
| 241 |
|
| 242 |
bool TriggerController::exists(const std::string& name) { |
| 243 |
TriggerNameMapIterator it = m_triggerNameMap.find(name); |
| 244 |
if (it != m_triggerNameMap.end()) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
} //FIFE |