FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
maploader.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 #include <string>
24 #include <vector>
25 
26 // 3rd party includes
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "ext/tinyxml/fife_tinyxml.h"
33 #include "model/model.h"
34 #include "model/structures/layer.h"
36 #include "model/structures/cell.h"
40 #include "model/metamodel/action.h"
42 #include "vfs/vfs.h"
43 #include "vfs/vfsdirectory.h"
44 #include "vfs/raw/rawdata.h"
45 #include "util/base/exception.h"
46 #include "util/log/logger.h"
47 #include "util/resource/resource.h"
48 #include "util/structures/rect.h"
49 #include "video/imagemanager.h"
50 #include "video/image.h"
51 #include "video/renderbackend.h"
52 #include "view/visual.h"
53 #include "view/camera.h"
55 #include "util/base/stringutils.h"
56 
57 #include "atlasloader.h"
58 #include "maploader.h"
59 #include "animationloader.h"
60 #include "objectloader.h"
61 
62 namespace FIFE {
66  static Logger _log(LM_NATIVE_LOADERS);
67 
68  MapLoader::MapLoader(Model* model, VFS* vfs, ImageManager* imageManager, RenderBackend* renderBackend)
69  : m_model(model), m_vfs(vfs), m_imageManager(imageManager), m_renderBackend(renderBackend),
70  m_loaderName("fife"), m_mapDirectory("") {
74  }
75 
77 
78  }
79 
80  Map* MapLoader::load(const std::string& filename) {
81  Map* map = NULL;
82 
83  // reset percent done listener just in case
84  // it has residual data from last load
86 
87  bfs::path mapPath(filename);
88 
89  if (HasParentPath(mapPath)) {
90  if (GetParentPath(mapPath).string() != m_mapDirectory) {
91  // save the directory where the map file is located
92  m_mapDirectory = GetParentPath(mapPath).string();
93  }
94  }
95 
96  TiXmlDocument mapFile;
97 
98  std::string mapFilename = mapPath.string();
99 
100  try {
101  RawData* data = m_vfs->open(mapFilename);
102 
103  if (data) {
104  if (data->getDataLength() != 0) {
105  mapFile.Parse(data->readString(data->getDataLength()).c_str());
106 
107  if (mapFile.Error()) {
108  std::ostringstream oss;
109  oss << " Failed to load"
110  << mapFilename
111  << " : " << __FILE__
112  << " [" << __LINE__ << "]"
113  << std::endl;
114  FL_ERR(_log, oss.str());
115 
116  return map;
117  }
118  }
119 
120  // done with data delete resource
121  delete data;
122  data = 0;
123  }
124  }
125  catch (NotFound& e)
126  {
127  FL_ERR(_log, e.what());
128 
129  // TODO - should we abort here
130  // or rethrow the exception
131  // or just keep going
132 
133  return map;
134  }
135 
136  // if we get here then everything loaded properly
137  // so we can just parse out the contents
138  const TiXmlElement* root = mapFile.RootElement();
139 
140  if (root) {
141  const std::string* loaderName = root->Attribute(std::string("loaderName"));
142 
143  if (loaderName) {
144  m_loaderName = *loaderName;
145  }
146 
147  int numElements = 0;
148  root->QueryValueAttribute("elements", &numElements);
150 
151  const std::string* mapName = root->Attribute(std::string("id"));
152 
153  if (mapName) {
154  try {
155  map = m_model->createMap(*mapName);
156  }
157  catch (NameClash& e) {
158  FL_ERR(_log, e.what());
159 
160  // just rethrow to client
161  throw;
162  }
163 
164  if (map) {
165  map->setFilename(mapFilename);
166 
167  std::string ns = "";
168  for (const TiXmlElement *importElement = root->FirstChildElement("import"); importElement; importElement = importElement->NextSiblingElement("import")) {
169  const std::string* importDir = importElement->Attribute(std::string("dir"));
170  const std::string* importFile = importElement->Attribute(std::string("file"));
171 
172  std::string directory = "";
173  if (importDir) {
174  directory = *importDir;
175  }
176 
177  std::string file = "";
178  if (importFile) {
179  file = *importFile;
180  }
181 
182  if (importDir && !importFile) {
183  bfs::path fullPath(m_mapDirectory);
184  fullPath /= directory;
185  loadImportDirectory(fullPath.string());
186  }
187  else if (importFile) {
188  bfs::path fullFilePath(file);
189  bfs::path fullDirPath(directory);
190  if (importDir) {
191  fullDirPath = bfs::path(m_mapDirectory);
192  fullDirPath /= directory;
193  }
194  else {
195  fullFilePath = bfs::path(m_mapDirectory);
196  fullFilePath /= file;
197  }
198  loadImportFile(fullFilePath.string(), fullDirPath.string());
199  }
200  }
201  // converts multiobject part id to object pointer
202  std::list<std::string> namespaces = m_model->getNamespaces();
203  std::list<std::string>::iterator name_it = namespaces.begin();
204  for (; name_it != namespaces.end(); ++name_it) {
205  std::list<Object*> objects = m_model->getObjects(*name_it);
206  std::list<Object*>::iterator object_it = objects.begin();
207  for (; object_it != objects.end(); ++object_it) {
208  if ((*object_it)->isMultiObject()) {
209  const std::list<std::string>& multiParts = (*object_it)->getMultiPartIds();
210  std::list<std::string>::const_iterator multi_it = multiParts.begin();
211  for (; multi_it != multiParts.end(); ++multi_it) {
212  Object* partObj = m_model->getObject(*multi_it, *name_it);
213  if (partObj) {
214  partObj->setMultiPart(true);
215  (*object_it)->addMultiPart(partObj);
216  }
217  }
218  }
219  }
220  }
221 
222  // iterate over elements looking for layers
223  for (const TiXmlElement* layerElement = root->FirstChildElement("layer"); layerElement; layerElement = layerElement->NextSiblingElement("layer")) {
224  // defaults
225  double xOffset = 0.0;
226  double yOffset = 0.0;
227  double zOffset = 0.0;
228  double xScale = 1.0;
229  double yScale = 1.0;
230  double zScale = 1.0;
231  double rotation = 0.0;
232 
233  int xOffsetRetVal = layerElement->QueryValueAttribute("x_offset", &xOffset);
234  int yOffsetRetVal = layerElement->QueryValueAttribute("y_offset", &yOffset);
235  layerElement->QueryValueAttribute("z_offset", &zOffset);
236  int xScaleRetVal = layerElement->QueryValueAttribute("x_scale", &xScale);
237  int yScaleRetVal = layerElement->QueryValueAttribute("y_scale", &yScale);
238  layerElement->QueryValueAttribute("z_scale", &zScale);
239  int rotationRetVal = layerElement->QueryValueAttribute("rotation", &rotation);
240 
241  const std::string* layerName = layerElement->Attribute(std::string("id"));
242  const std::string* pathing = layerElement->Attribute(std::string("pathing"));
243  const std::string* sorting = layerElement->Attribute(std::string("sorting"));
244  const std::string* gridType = layerElement->Attribute(std::string("grid_type"));
245  const std::string* layerType = layerElement->Attribute(std::string("layer_type"));
246  const std::string* layerTypeName = layerElement->Attribute(std::string("layer_type_id"));
247 
248  if (xOffsetRetVal == TIXML_SUCCESS &&
249  yOffsetRetVal == TIXML_SUCCESS &&
250  xScaleRetVal == TIXML_SUCCESS &&
251  yScaleRetVal == TIXML_SUCCESS &&
252  rotationRetVal == TIXML_SUCCESS &&
253  layerName &&
254  pathing &&
255  gridType) {
256 
257  PathingStrategy pathStrategy = CELL_EDGES_ONLY;
258  if ("cell_edges_and_diagonals" == *pathing) {
259  pathStrategy = CELL_EDGES_AND_DIAGONALS;
260  }
261 
262  SortingStrategy sortStrategy = SORTING_CAMERA;
263  if (sorting) {
264  if (*sorting == "location") {
265  sortStrategy = SORTING_LOCATION;
266  } else if (*sorting == "camera_and_location") {
267  sortStrategy = SORTING_CAMERA_AND_LOCATION;
268  }
269  }
270 
271  CellGrid* grid = NULL;
272  if (gridType) {
273  grid = m_model->getCellGrid(*gridType);
274  }
275  else {
276  grid = m_model->getCellGrid("square");
277  }
278 
279  if (grid) {
280  grid->setXShift(xOffset);
281  grid->setXScale(xScale);
282  grid->setYShift(yOffset);
283  grid->setYScale(yScale);
284  grid->setZShift(zOffset);
285  grid->setZScale(zScale);
286  grid->setRotation(rotation);
287 
288  Layer *layer = NULL;
289  try {
290  layer = map->createLayer(*layerName, grid);
291  }
292  catch (NameClash&) {
293  // TODO - handle exception
294  assert(false);
295  }
296 
297  if (layer) {
298  layer->setPathingStrategy(pathStrategy);
299  layer->setSortingStrategy(sortStrategy);
300  if (layerType) {
301  if (*layerType == "walkable") {
302  layer->setWalkable(true);
303  } else if (*layerType == "interact") {
304  layer->setInteract(true, *layerTypeName);
305  }
306  }
307 
308  double curr_x = 0;
309  double curr_y = 0;
310 
311  for (const TiXmlElement* instances = layerElement->FirstChildElement("instances"); instances; instances = instances->NextSiblingElement("instances")) {
312  for (const TiXmlElement* instance = instances->FirstChildElement("i"); instance; instance = instance->NextSiblingElement("i")) {
313  double x = 0;
314  double y = 0;
315  double z = 0;
316  int r = 0;
317  int stackpos = 0;
318  int cellStack = 0;
319  int visitorRadius = 0;
320 
321  const std::string* instanceId = instance->Attribute(std::string("id"));
322  const std::string* objectId = instance->Attribute(std::string("o"));
323  const std::string* costId = instance->Attribute(std::string("cost_id"));
324 
325  if (!objectId) {
326  objectId = instance->Attribute(std::string("object"));
327  }
328 
329  if (!objectId) {
330  objectId = instance->Attribute(std::string("obj"));
331  }
332 
333  const std::string* namespaceId = instance->Attribute(std::string("ns"));
334 
335  if (!namespaceId) {
336  namespaceId = instance->Attribute(std::string("namespace"));
337  }
338 
339  int xRetVal = instance->QueryValueAttribute("x", &x);
340  int yRetVal = instance->QueryValueAttribute("y", &y);
341  instance->QueryValueAttribute("z", &z);
342  int rRetVal = instance->QueryValueAttribute("r", &r);
343 
344  if (xRetVal == TIXML_SUCCESS) {
345  curr_x = x;
346  }
347  else {
348  x = ++curr_x;
349  }
350 
351  if (yRetVal == TIXML_SUCCESS) {
352  curr_y = y;
353  }
354  else {
355  y = curr_y;
356  }
357 
358  if (rRetVal != TIXML_SUCCESS) {
359  rRetVal = instance->QueryValueAttribute("rotation", &r);
360  }
361 
362  int stackRetVal = instance->QueryValueAttribute("stackpos", &stackpos);
363  int cellStackRetVal = instance->QueryValueAttribute("cellstack", &cellStack);
364  int visitorRetVal = instance->QueryValueAttribute("visitor_radius", &visitorRadius);
365  const std::string* shapeType = instance->Attribute(std::string("visitor_shape"));
366  VisitorShapeInfo visitorShape = ITYPE_NO_SHAPE;
367  if (shapeType) {
368  if ("quad" == *shapeType) {
369  visitorShape = ITYPE_QUAD_SHAPE;
370  } else if ("circle" == *shapeType) {
371  visitorShape = ITYPE_CIRCLE_SHAPE;
372  }
373  }
374 
375  if (objectId) {
376  if (namespaceId) {
377  ns = *namespaceId;
378  }
379 
380  Object* object = m_model->getObject(*objectId, ns);
381 
382  if (object) {
383  Instance* inst = NULL;
384  if (instanceId) {
385  inst = layer->createInstance(object, ExactModelCoordinate(x,y,z), *instanceId);
386  }
387  else {
388  inst = layer->createInstance(object, ExactModelCoordinate(x,y,z));
389  }
390 
391  if (inst) {
392  if (rRetVal != TIXML_SUCCESS) {
393  ObjectVisual* objVisual = object->getVisual<ObjectVisual>();
394  std::vector<int> angles;
395  objVisual->getStaticImageAngles(angles);
396  if (!angles.empty()) {
397  r = angles[0];
398  }
399  }
400 
401  inst->setRotation(r);
402 
403  InstanceVisual* instVisual = InstanceVisual::create(inst);
404 
405  if (instVisual && (stackRetVal == TIXML_SUCCESS)) {
406  instVisual->setStackPosition(stackpos);
407  }
408 
409  if (cellStackRetVal == TIXML_SUCCESS) {
410  inst->setCellStackPosition(cellStack);
411  }
412 
413  if (visitorRetVal == TIXML_SUCCESS) {
414  inst->setVisitor(true);
415  inst->setVisitorRadius(visitorRadius);
416  inst->setVisitorShape(visitorShape);
417  }
418 
419  if (costId) {
420  double cost = 0;
421  int costRetVal = instance->QueryValueAttribute("cost", &cost);
422  if (costRetVal == TIXML_SUCCESS) {
423  inst->setCost(*costId, cost);
424  }
425  }
426 
427  if (object->getAction("default")) {
428  Location target(layer);
429 
430  inst->act("default", target, true);
431  }
432  }
433  else
434  {
435  std::ostringstream oss;
436  oss << " Failed to create instance of object "
437  << *objectId
438  << " : " << __FILE__
439  << " [" << __LINE__ << "]"
440  << std::endl;
441  FL_ERR(_log, oss.str());
442  }
443  }
444  }
445 
446  // increment % done counter
448  }
449  }
450  }
451  }
452  }
453 
454  // increment % done counter
456  }
457 
458  // init CellCaches
459  map->initializeCellCaches();
460  // add Cells from xml File
461  for (const TiXmlElement* cacheElements = root->FirstChildElement("cellcaches"); cacheElements; cacheElements = cacheElements->NextSiblingElement("cellcaches")) {
462  for (const TiXmlElement* cacheElement = cacheElements->FirstChildElement("cellcache"); cacheElement; cacheElement = cacheElement->NextSiblingElement("cellcache")) {
463  double cacheCost = 1.0;
464  double cacheSpeed = 1.0;
465  const std::string* layerId = cacheElement->Attribute(std::string("id"));
466 
467  if (layerId) {
468  cacheElement->QueryDoubleAttribute("default_cost", &cacheCost);
469  cacheElement->QueryDoubleAttribute("default_speed", &cacheSpeed);
470 
471  Layer* layer = map->getLayer(*layerId);
472  if (layer) {
473  CellCache* cache = layer->getCellCache();
474  if (cache) {
475  int searchNarrow = 0;
476  cacheElement->QueryIntAttribute("search_narrow", &searchNarrow);
477  cache->setSearchNarrowCells(searchNarrow != 0);
478 
479  cache->setDefaultCostMultiplier(cacheCost);
480  cache->setDefaultSpeedMultiplier(cacheSpeed);
481  for (const TiXmlElement* cellElement = cacheElement->FirstChildElement("cell"); cellElement; cellElement = cellElement->NextSiblingElement("cell")) {
482  int cellX = 0;
483  int cellY = 0;
484  int success = cellElement->QueryIntAttribute("x", &cellX);
485  success &= cellElement->QueryIntAttribute("y", &cellY);
486  if (success == TIXML_SUCCESS) {
487  ModelCoordinate mc(cellX, cellY);
488  Cell* cell = cache->createCell(mc);
489 
490  const std::string* cellVisual = cellElement->Attribute(std::string("state"));
491  if (cellVisual) {
493  if (*cellVisual == "concealed") {
494  cve = CELLV_CONCEALED;
495  } else if (*cellVisual == "masked") {
496  cve = CELLV_MASKED;
497  }
498  cell->setFoWType(cve);
499  }
500 
501  const std::string* cellBlocker = cellElement->Attribute(std::string("blocker_type"));
502  if (cellBlocker) {
503  if (*cellBlocker == "no_blocker") {
505  cell->setCellType(cti);
506  } else if (*cellBlocker == "blocker") {
508  cell->setCellType(cti);
509  }
510  }
511 
512  double cellCost = 1.0;
513  double cellSpeed = 1.0;
514  success = cellElement->QueryDoubleAttribute("default_cost", &cellCost);
515  if (success == TIXML_SUCCESS) {
516  cell->setCostMultiplier(cellCost);
517  }
518  success = cellElement->QueryDoubleAttribute("default_speed", &cellSpeed);
519  if (success == TIXML_SUCCESS) {
520  cell->setSpeedMultiplier(cellSpeed);
521 
522  }
523 
524  int isNarrow = 0;
525  cellElement->QueryIntAttribute("narrow", &isNarrow);
526  if (isNarrow != 0) {
527  cache->addNarrowCell(cell);
528  }
529  // add cost with given id to cell
530  for (const TiXmlElement* costElement = cellElement->FirstChildElement("cost"); costElement; costElement = costElement->NextSiblingElement("cost")) {
531  const std::string* costId = costElement->Attribute(std::string("id"));
532  double cost = 1.0;
533  success = costElement->QueryDoubleAttribute("value", &cost);
534  if (costId && success == TIXML_SUCCESS) {
535  cache->registerCost(*costId, cost);
536  cache->addCellToCost(*costId, cell);
537  }
538  }
539  // add area to cell
540  for (const TiXmlElement* areaElement = cellElement->FirstChildElement("area"); areaElement; areaElement = areaElement->NextSiblingElement("area")) {
541  const std::string* areaId = areaElement->Attribute(std::string("id"));
542  if (areaId) {
543  cache->addCellToArea(*areaId, cell);
544  }
545  }
546  }
547  }
548  }
549  }
550  }
551  }
552  }
553  // finalize CellCaches
554  map->finalizeCellCaches();
555  // add Transistions
556  for (const TiXmlElement* cacheElements = root->FirstChildElement("cellcaches"); cacheElements; cacheElements = cacheElements->NextSiblingElement("cellcaches")) {
557  for (const TiXmlElement* cacheElement = cacheElements->FirstChildElement("cellcache"); cacheElement; cacheElement = cacheElement->NextSiblingElement("cellcache")) {
558  const std::string* layerId = cacheElement->Attribute(std::string("id"));
559  if (layerId) {
560  Layer* layer = map->getLayer(*layerId);
561  if (layer) {
562  CellCache* cache = layer->getCellCache();
563  if (cache) {
564  for (const TiXmlElement* cellElement = cacheElement->FirstChildElement("cell"); cellElement; cellElement = cellElement->NextSiblingElement("cell")) {
565  int cellX = 0;
566  int cellY = 0;
567  int success = cellElement->QueryIntAttribute("x", &cellX);
568  success &= cellElement->QueryIntAttribute("y", &cellY);
569  if (success == TIXML_SUCCESS) {
570  ModelCoordinate mc(cellX, cellY);
571  Cell* cell = cache->getCell(mc);
572  if (!cell) {
573  continue;
574  }
575  for (const TiXmlElement* transitionElement = cellElement->FirstChildElement("transition"); transitionElement; transitionElement = transitionElement->NextSiblingElement("transition")) {
576  int targetX = 0;
577  int targetY = 0;
578  int targetZ = 0;
579  success = transitionElement->QueryIntAttribute("x", &targetX);
580  success &= transitionElement->QueryIntAttribute("y", &targetY);
581  transitionElement->QueryIntAttribute("z", &targetZ);
582  if (success == TIXML_SUCCESS) {
583  ModelCoordinate mc(targetX, targetY, targetZ);
584  Layer* targetLayer = NULL;
585  const std::string* targetLayerId = transitionElement->Attribute(std::string("id"));
586  if (targetLayerId) {
587  targetLayer = map->getLayer(*targetLayerId);
588  }
589  if (!targetLayer) {
590  targetLayer = layer;
591  }
592 
593  int immediate = 0;
594  transitionElement->QueryIntAttribute("immediate", &immediate);
595  cell->createTransition(targetLayer, mc, immediate != 0);
596  }
597  }
598  }
599  }
600  }
601  }
602  }
603  }
604  }
605 
606  for (const TiXmlElement* cameraElement = root->FirstChildElement("camera"); cameraElement; cameraElement = cameraElement->NextSiblingElement("camera")) {
607  const std::string* cameraId = cameraElement->Attribute(std::string("id"));
608  const std::string* refLayerId = cameraElement->Attribute(std::string("ref_layer_id"));
609 
610  int refCellWidth = 0;
611  int refCellHeight = 0;
612  int success = cameraElement->QueryIntAttribute("ref_cell_width", &refCellWidth);
613  success &= cameraElement->QueryIntAttribute("ref_cell_height", &refCellHeight);
614 
615  if (cameraId && refLayerId && success == TIXML_SUCCESS) {
616  double tilt = 0.0;
617  double zoom = 1.0;
618  double rotation = 0.0;
619  double zToY = 0.0;
620  cameraElement->QueryDoubleAttribute("tilt", &tilt);
621  cameraElement->QueryDoubleAttribute("zoom", &zoom);
622  cameraElement->QueryDoubleAttribute("rotation", &rotation);
623  success = cameraElement->QueryDoubleAttribute("ztoy", &zToY);
624 
625  const std::string* viewport = cameraElement->Attribute(std::string("viewport"));
626 
627  Layer* layer = NULL;
628  try {
629  layer = map->getLayer(*refLayerId);
630  }
631  catch (NotFound&) {
632  // TODO - handle exception
633  assert(false);
634  }
635 
636  Camera* cam = NULL;
637  if (layer) {
638  if (viewport) {
639  // parse out the viewport parameters
640  IntVector viewportParameters = tokenize(*viewport, ',');
641 
642  // make sure the right number of viewport parameters were parsed
643  if (viewportParameters.size() == 4) {
644  Rect rect(viewportParameters[0], viewportParameters[1],
645  viewportParameters[2], viewportParameters[3]);
646 
647  try {
648  cam = map->addCamera(*cameraId, layer, rect);
649  }
650  catch (NameClash&) {
651  // TODO - handle exception
652  assert(false);
653  }
654  }
655  }
656  else {
658 
659  try {
660  cam = map->addCamera(*cameraId, layer, rect);
661  }
662  catch (NameClash&) {
663  // TODO - handle exception
664  assert(false);
665  }
666  }
667  }
668 
669  if (cam) {
670  cam->setCellImageDimensions(refCellWidth, refCellHeight);
671  cam->setRotation(rotation);
672  cam->setTilt(tilt);
673  cam->setZoom(zoom);
674  if (success == TIXML_SUCCESS) {
675  cam->setZToY(zToY);
676  }
677 
678  // active instance renderer for camera
679  InstanceRenderer* instanceRenderer = InstanceRenderer::getInstance(cam);
680  if (instanceRenderer)
681  {
682  instanceRenderer->activateAllLayers(map);
683  }
684  }
685  }
686 
687  // increment % done counter
689  }
690  }
691  }
692  }
693 
694  return map;
695  }
696 
698  assert(objectLoader);
699 
700  m_objectLoader = objectLoader;
701  }
702 
703 
705  assert(animationLoader);
706 
707  m_objectLoader->setAnimationLoader(animationLoader);
708  }
709 
711  assert(atlasLoader);
712 
713  m_atlasLoader = atlasLoader;
714  }
715 
716  bool MapLoader::isLoadable(const std::string& filename) const {
717  bfs::path mapPath(filename);
718 
719  TiXmlDocument mapFile;
720 
721  std::string mapFilename = mapPath.string();
722 
723  try {
724  RawData* data = m_vfs->open(mapFilename);
725 
726  if (data) {
727  if (data->getDataLength() != 0) {
728  mapFile.Parse(data->readString(data->getDataLength()).c_str());
729 
730  if (mapFile.Error()) {
731  return false;
732  }
733 
734  const TiXmlElement* root = mapFile.RootElement();
735 
736  if (root) {
737  const std::string* loaderName = root->Attribute(std::string("loader"));
738 
739  // if the file does not specify a loader but was opened and parsed
740  // correctly then we know we have a compatible extension so we will
741  // attempt to load it, if it does specify a loader then the loader
742  // name will be checked
743  if (!loaderName || (loaderName && *loaderName == getLoaderName())) {
744  return true;
745  }
746  }
747  }
748 
749  // done with file delete the resource
750  delete data;
751  data = 0;
752  }
753  }
754  catch (NotFound& e) {
755  FL_ERR(_log, e.what());
756 
757  return false;
758  }
759 
760  return false;
761  }
762 
763  void MapLoader::loadImportFile(const std::string& file, const std::string& directory) {
764  if (!file.empty()) {
765  bfs::path importFilePath(directory);
766  importFilePath /= file;
767 
768  std::string importFileString = importFilePath.string();
769  if (m_objectLoader && m_objectLoader->isLoadable(importFileString)) {
770  m_objectLoader->load(importFileString);
771  }
772  else if (m_atlasLoader && m_atlasLoader->isLoadable(importFileString)) {
773  m_atlasLoader->load(importFileString);
774  }
775  }
776  }
777 
778  void MapLoader::loadImportDirectory(const std::string& directory) {
779  if (!directory.empty()) {
780  bfs::path importDirectory(directory);
781  std::string importDirectoryString = importDirectory.string();
782 
783  std::set<std::string> files = m_vfs->listFiles(importDirectoryString);
784 
785  // load all xml files in the directory
786  std::set<std::string>::iterator iter;
787  for (iter = files.begin(); iter != files.end(); ++iter) {
788  // TODO - vtchill - may need a way to allow clients to load things other
789  // than .xml and .zip files
790  std::string ext = bfs::extension(*iter);
791  if (ext == ".xml" || ext == ".zip") {
792  loadImportFile(*iter, importDirectoryString);
793  }
794  }
795 
796  std::set<std::string> nestedDirectories = m_vfs->listDirectories(importDirectoryString);
797  for (iter = nestedDirectories.begin(); iter != nestedDirectories.end(); ++iter) {
798  // do not attempt to load anything from a .svn directory
799  if ((*iter).find(".svn") == std::string::npos) {
800  loadImportDirectory(importDirectoryString + "/" + *iter);
801  }
802  }
803  }
804  }
805 
808  }
809 
810  const std::string& MapLoader::getLoaderName() const {
811  return m_loaderName;
812 
813  }
814 
815  MapLoader* createDefaultMapLoader(Model* model, VFS* vfs, ImageManager* imageManager, RenderBackend* renderBackend) {
816  return (new MapLoader(model, vfs, imageManager, renderBackend));
817  }
818 }
std::list< std::string > getNamespaces() const
Get a list of namespaces currently referenced by objects in the metamodel.
Definition: model.cpp:140
void setZScale(const double scale)
Set the cellgrid z-scaling.
Definition: cellgrid.h:185
static InstanceVisual * create(Instance *instance)
Constructs and assigns it to the passed item.
Definition: visual.cpp:98
Abstract interface for all the renderbackends.
Definition: renderbackend.h:92
void setZToY(double zToY)
Sets zToY value for the camera and enables their use.
Definition: camera.cpp:187
std::set< std::string > listFiles(const std::string &path) const
Get a filelist of the given directory.
Definition: vfs.cpp:167
uint32_t getDataLength() const
get the complete datalength
Definition: rawdata.cpp:75
uint32_t getScreenHeight() const
void addPercentDoneListener(PercentDoneListener *listener)
allows adding a listener to the map loader for percent completed events
Definition: maploader.cpp:806
ImageManager.
Definition: imagemanager.h:54
Map * load(const std::string &filename)
Definition: maploader.cpp:80
Layer * getLayer(const std::string &identifier)
Get the layer with the given id.
Definition: map.cpp:69
Camera * addCamera(const std::string &id, Layer *layer, const Rect &viewport)
Adds camera to the map.
Definition: map.cpp:241
Object class.
Definition: object.h:51
Instance visual contains data that is needed to visualize the instance on screen. ...
Definition: visual.h:111
void setZShift(const double zshift)
Set the cellgrid z shift.
Definition: cellgrid.h:156
void activateAllLayers(Map *elevation)
Activates all layers from given elevation.
void setSearchNarrowCells(bool search)
Sets if narrow cells should be searched automatic.
Definition: cellcache.cpp:1398
void reset(T *ptr=0)
reset this pointer to a null shared pointer this can be used to lower the reference count of the shar...
Definition: sharedptr.h:164
void setAnimationLoader(const FIFE::AnimationLoaderPtr &animationLoader)
Definition: maploader.cpp:704
A CellCache is an abstract depiction of one or a few layers and contains additional information...
Definition: cellcache.h:111
MapLoader(Model *model, VFS *vfs, ImageManager *imageManager, RenderBackend *renderBackend)
Definition: maploader.cpp:68
void setSortingStrategy(SortingStrategy strategy)
Sets sorting strategy for the layer.
Definition: layer.cpp:395
void setTilt(double tilt)
Sets tilt for the camera.
Definition: camera.cpp:132
void setDefaultCostMultiplier(double multi)
Sets default cost for this CellCache.
Definition: cellcache.cpp:1150
void setSpeedMultiplier(double multi)
Changes the cell speed.
Definition: cell.cpp:435
void addCellToCost(const std::string &costId, Cell *cell)
Assigns a cell to a cost identifier.
Definition: cellcache.cpp:1027
RawData * open(const std::string &path)
Open a file.
Definition: vfs.cpp:157
CellCache * getCellCache()
Returns the CellCache of this layer.
Definition: layer.cpp:453
void setCellType(CellTypeInfo type)
Sets blocker type.
Definition: cell.cpp:480
std::string m_loaderName
Definition: maploader.h:114
void setAtlasLoader(const FIFE::AtlasLoaderPtr &atlasLoader)
Definition: maploader.cpp:710
Instance * createInstance(Object *object, const ModelCoordinate &p, const std::string &id="")
Add an instance of an object at a specific position.
Definition: layer.cpp:108
static Logger _log(LM_AUDIO)
void setVisitorRadius(uint16_t radius)
Sets the range for a visitor.
Definition: instance.cpp:549
void registerCost(const std::string &costId, double cost)
Adds a cost with the given id and value.
Definition: cellcache.cpp:980
void loadImportFile(const std::string &file, const std::string &directory="")
used to load an object file if directory is provided then file is assumed relative to directory if re...
Definition: maploader.cpp:763
void setXShift(const double &xshift)
Set the cellgrid x shift.
Definition: cellgrid.h:130
void setRotation(double rotation)
Sets rotation for the camera.
Definition: camera.cpp:145
MapLoader * createDefaultMapLoader(Model *model, VFS *vfs, ImageManager *imageManager, RenderBackend *renderBackend)
convenience function for creating the default fife map loader deleting the object returned from this ...
Definition: maploader.cpp:815
Camera describes properties of a view port shown in the main screen Main screen can have multiple cam...
Definition: camera.h:58
void setYScale(const double scale)
Set the cellgrid y-scaling.
Definition: cellgrid.h:177
void setObjectLoader(const FIFE::ObjectLoaderPtr &objectLoader)
Definition: maploader.cpp:697
virtual void load(const std::string &filename)=0
responsible for loading the object resource and populating the engine
void setXScale(const double scale)
Set the cellgrid x-scaling.
Definition: cellgrid.h:169
#define FL_ERR(logger, msg)
Definition: logger.h:73
void setPathingStrategy(PathingStrategy strategy)
Sets pathing strategy for the layer.
Definition: layer.cpp:386
PercentDoneCallback m_percentDoneListener
Definition: maploader.h:112
virtual bool isLoadable(const std::string &filename)=0
determines whether the resource is in the correct format for this loader
bool HasParentPath(const bfs::path &path)
Helper function to determine if a path object has a parent path.
void setMultiPart(bool part)
Sets the object as a part of a multi object.
Definition: object.cpp:227
std::vector< int32_t > IntVector
Definition: stringutils.h:32
bfs::path GetParentPath(const bfs::path &path)
Helper function to retrieve a parent path object from a path object.
void setYShift(const double yshift)
Set the cellgrid y shift.
Definition: cellgrid.h:143
virtual bool isLoadable(const std::string &filename) const =0
determines whether the resource is in the correct format for this loader
Cell * getCell(const ModelCoordinate &mc)
Returns cell on this coordinate.
Definition: cellcache.cpp:706
uint8_t CellTypeInfo
Definition: cell.h:65
ObjectLoaderPtr m_objectLoader
Definition: maploader.h:109
virtual AtlasPtr load(const std::string &filename)=0
responsible for loading the atlas returns a shared pointer to an image resource
uint8_t VisitorShapeInfo
Definition: instance.h:92
void getStaticImageAngles(std::vector< int32_t > &angles)
Returns list of available static image angles for this object.
Definition: visual.cpp:82
uint32_t getScreenWidth() const
A basic layer on a map.
Definition: layer.h:98
const std::string & getLoaderName() const
returns the loader name associated with this map file loader, this will only be populated after the l...
Definition: maploader.cpp:810
Layer * createLayer(const std::string &identifier, CellGrid *grid)
Add a Layer to this Map.
Definition: map.cpp:82
ImageManager * m_imageManager
Definition: maploader.h:108
SortingStrategy
Definition: layer.h:62
A model is a facade for everything in the model.
Definition: model.h:53
RenderBackend * m_renderBackend
Definition: maploader.h:111
A basic cell on a CellCache.
Definition: cell.h:136
uint8_t CellVisualEffect
Definition: cell.h:78
void setInteract(bool interact, const std::string &id)
Sets interact for the layer.
Definition: layer.cpp:411
void act(const std::string &actionName, const Location &direction, bool repeating=false)
Performs given named action to the instance.
Definition: instance.cpp:573
void setDefaultSpeedMultiplier(double multi)
Sets default speed for this CellCache.
Definition: cellcache.cpp:1158
void setCellStackPosition(uint8_t stack)
Sets the cell stack position.
Definition: instance.cpp:557
void setWalkable(bool walkable)
Sets walkable for the layer.
Definition: layer.cpp:403
Object visual contains data that is needed for visualizing objects.
Definition: visual.h:65
std::string readString(size_t len)
read a string with len bytes, not assuming a terminating 0 Appends a null terminator character to the...
Definition: rawdata.cpp:128
DoublePoint3D ExactModelCoordinate
Definition: modelcoords.h:36
virtual void setAnimationLoader(const AnimationLoaderPtr &animationLoader)=0
allows setting which animation loader will be used to load animation files
CellGrid * getCellGrid(const std::string &gridtype)
Returns new copy of cellgrid corresponding given name.
Definition: model.cpp:96
void setFoWType(CellVisualEffect type)
Sets the cell visual.
Definition: cell.cpp:379
void addCellToArea(const std::string &id, Cell *cell)
Adds a cell to a specific area group.
Definition: cellcache.cpp:1402
IntVector tokenize(const std::string &str, char delim, char group)
Definition: stringutils.cpp:38
void setCostMultiplier(double multi)
Changes the cell cost.
Definition: cell.cpp:419
std::string m_mapDirectory
Definition: maploader.h:115
A 3D Point.
Definition: point.h:202
Action * getAction(const std::string &identifier) const
Gets action with given id.
Definition: object.cpp:92
PathingStrategy
Defines how pathing can be performed on this layer.
Definition: layer.h:57
the main VFS (virtual file system) class
Definition: vfs.h:58
void setStackPosition(int32_t stackposition)
Sets stack position of the instance Stack position is used to define the order in which instances res...
Definition: visual.cpp:133
Model * m_model
Definition: maploader.h:106
void finalizeCellCaches()
Creates cellcaches for this map.
Definition: map.cpp:335
void setRotation(const double rotation)
Set the cellgrid rotation.
Definition: cellgrid.h:208
void createTransition(Layer *layer, const ModelCoordinate &mc, bool immediate=false)
Creates a transistion from this cell to the given layer and coordinates.
Definition: cell.cpp:525
void setRotation(int32_t rotation)
Set the rotation offset of this instance.
Definition: instance.cpp:311
Cell * createCell(const ModelCoordinate &mc)
Creates cell on this CellCache.
Definition: cellcache.cpp:697
void addNarrowCell(Cell *cell)
Adds cell to narrow cells.
Definition: cellcache.cpp:1367
Object * getObject(const std::string &id, const std::string &name_space)
Get an object by its id.
Definition: model.cpp:224
AtlasLoaderPtr m_atlasLoader
Definition: maploader.h:110
void loadImportDirectory(const std::string &directory)
used to load a directory of object files recursively if relativeToMap is true then the directory is a...
Definition: maploader.cpp:778
void setTotalNumberOfElements(unsigned int totalElements)
void initializeCellCaches()
Creates cellcaches for this map.
Definition: map.cpp:311
void addListener(PercentDoneListener *listener)
void setCellImageDimensions(uint32_t width, uint32_t height)
Sets screen cell image dimensions.
Definition: camera.cpp:208
A container of Layer(s).
Definition: map.h:87
static InstanceRenderer * getInstance(IRendererContainer *cnt)
Gets instance for interface access.
An Instance is an &quot;instantiation&quot; of an Object at a Location.
Definition: instance.h:97
std::list< Object * > getObjects(const std::string &name_space) const
Get all the objects in the given namespace.
Definition: model.cpp:234
bool isLoadable(const std::string &filename) const
Definition: maploader.cpp:716
void setFilename(const std::string &file)
Definition: map.h:196
Map * createMap(const std::string &identifier)
Add a map this model, and get a pointer to it.
Definition: model.cpp:64
void setCost(const std::string &id, double cost)
Sets for the given cost id a cost.
Definition: instance.cpp:968
Used to access diffrent kinds of data.
Definition: rawdata.h:48
void setZoom(double zoom)
Sets zoom for the camera.
Definition: camera.cpp:157
std::set< std::string > listDirectories(const std::string &path) const
Get a directorylist of the given directory.
Definition: vfs.cpp:183
void setVisitor(bool visit)
Marks this instance as a visitor.
Definition: instance.cpp:533
void setVisitorShape(VisitorShapeInfo info)
Sets the shape type for a visitor.
Definition: instance.cpp:541