30 #include "ext/tinyxml/fife_tinyxml.h"
75 void MapSaver::save(
const Map& map,
const std::string& filename,
const std::vector<std::string>& importFiles) {
79 TiXmlDeclaration* decl =
new TiXmlDeclaration(
"1.0",
"ascii",
"");
80 doc.LinkEndChild(decl);
83 TiXmlElement* mapElement =
new TiXmlElement(
"map");
84 mapElement->SetAttribute(
"id", map.
getId());
85 mapElement->SetAttribute(
"format",
"1.0");
86 doc.LinkEndChild(mapElement);
88 for (std::vector<std::string>::const_iterator iter = importFiles.begin(); iter != importFiles.end(); ++iter)
90 TiXmlElement* importElement =
new TiXmlElement(
"import");
91 importElement->SetAttribute(
"file", *iter);
94 mapElement->LinkEndChild(importElement);
97 typedef std::list<Layer*> LayerList;
99 for (LayerList::iterator iter = layers.begin(); iter != layers.end(); ++iter)
101 TiXmlElement* layerElement =
new TiXmlElement(
"layer");
102 CellGrid* grid = (*iter)->getCellGrid();
103 layerElement->SetAttribute(
"id", (*iter)->getId());
104 layerElement->SetDoubleAttribute(
"x_offset", grid->
getXShift());
105 layerElement->SetDoubleAttribute(
"y_offset", grid->
getYShift());
106 layerElement->SetDoubleAttribute(
"z_offset", grid->
getZShift());
107 layerElement->SetDoubleAttribute(
"x_scale", grid->
getXScale());
108 layerElement->SetDoubleAttribute(
"y_scale", grid->
getYScale());
109 layerElement->SetDoubleAttribute(
"rotation", grid->
getRotation());
110 layerElement->SetAttribute(
"grid_type", grid->
getType());
111 layerElement->SetAttribute(
"transparency", (*iter)->getLayerTransparency());
113 std::string pathingStrategy;
114 switch ((*iter)->getPathingStrategy())
118 pathingStrategy =
"cell_edges_only";
123 pathingStrategy =
"cell_edges_and_diagonals";
128 pathingStrategy =
"cell_edges_only";
132 layerElement->SetAttribute(
"pathing", pathingStrategy);
134 std::string sortingStrategy;
135 switch ((*iter)->getSortingStrategy()) {
138 sortingStrategy =
"camera";
143 sortingStrategy =
"location";
148 sortingStrategy =
"camera_and_location";
153 sortingStrategy =
"camera";
157 layerElement->SetAttribute(
"sorting", sortingStrategy);
159 if ((*iter)->isWalkable()) {
160 layerElement->SetAttribute(
"layer_type",
"walkable");
161 }
else if ((*iter)->isInteract()) {
162 layerElement->SetAttribute(
"layer_type",
"interact");
163 layerElement->SetAttribute(
"layer_type_id", (*iter)->getWalkableId());
167 mapElement->LinkEndChild(layerElement);
170 TiXmlElement* instancesElement =
new TiXmlElement(
"instances");
171 layerElement->LinkEndChild(instancesElement);
173 std::string currentNamespace =
"";
174 typedef std::vector<Instance*> InstancesContainer;
175 InstancesContainer instances = (*iter)->getInstances();
176 for (InstancesContainer::iterator iter = instances.begin(); iter != instances.end(); ++iter)
178 Object* obj = (*iter)->getObject();
185 TiXmlElement* instanceElement =
new TiXmlElement(
"i");
189 instanceElement->SetAttribute(
"ns", obj->
getNamespace());
195 if (!(*iter)->getId().empty())
197 instanceElement->SetAttribute(
"id", (*iter)->getId());
200 instanceElement->SetAttribute(
"o", obj->
getId());
203 instanceElement->SetDoubleAttribute(
"x", position.
x);
204 instanceElement->SetDoubleAttribute(
"y", position.
y);
205 instanceElement->SetDoubleAttribute(
"z", position.
z);
206 instanceElement->SetAttribute(
"r", (*iter)->getRotation());
208 if ((*iter)->isBlocking())
210 instanceElement->SetAttribute(
"blocking", (*iter)->isBlocking());
214 instanceElement->SetAttribute(
"cellstack", (*iter)->getCellStackPosition());
217 if ((*iter)->isVisitor()) {
218 instanceElement->SetAttribute(
"visitor_radius", (*iter)->getVisitorRadius());
219 std::string shape(
"none");
225 instanceElement->SetAttribute(
"visitor_shape", shape);
228 if ((*iter)->isSpecialCost()) {
230 instanceElement->SetAttribute(
"cost_id", (*iter)->getCostId());
231 instanceElement->SetDoubleAttribute(
"cost", (*iter)->getCost());
232 }
else if ((*iter)->getCostId() != obj->
getCostId() ||
234 instanceElement->SetAttribute(
"cost_id", (*iter)->getCostId());
235 instanceElement->SetDoubleAttribute(
"cost", (*iter)->getCost());
240 instanceElement->SetAttribute(
"stackpos", instanceVisual->
getStackPosition());
242 instancesElement->LinkEndChild(instanceElement);
246 TiXmlElement* cellcachesElement =
new TiXmlElement(
"cellcaches");
247 mapElement->LinkEndChild(cellcachesElement);
248 for (LayerList::iterator iter = layers.begin(); iter != layers.end(); ++iter) {
249 CellCache* cache = (*iter)->getCellCache();
254 TiXmlElement* cellcacheElement =
new TiXmlElement(
"cellcache");
255 cellcacheElement->SetAttribute(
"id", (*iter)->getId());
263 const std::vector<std::vector<Cell*> >& cells = cache->
getCells();
264 std::vector<std::vector<Cell*> >::const_iterator it = cells.begin();
265 for (; it != cells.end(); ++it) {
266 std::vector<Cell*>::const_iterator cit = (*it).begin();
267 for (; cit != (*it).end(); ++cit) {
269 std::list<std::string> costIds = cache->
getCosts();
270 bool costsEmpty = costIds.empty();
275 std::vector<std::string> areaIds = cache->
getCellAreas(cell);
276 std::vector<std::string> cellAreaIds;
277 bool areasEmpty = areaIds.empty();
279 const std::set<Instance*>& cellInstances = cell->
getInstances();
280 if (!cellInstances.empty()) {
281 std::vector<std::string>::iterator area_it = areaIds.begin();
282 for (; area_it != areaIds.end(); ++area_it) {
283 bool objectArea =
false;
284 std::set<Instance*>::const_iterator instance_it = cellInstances.begin();
285 for (; instance_it != cellInstances.end(); ++instance_it) {
286 if ((*instance_it)->getObject()->getArea() == *area_it) {
292 cellAreaIds.push_back(*area_it);
296 cellAreaIds = areaIds;
298 areasEmpty = cellAreaIds.empty();
306 bool isNarrow =
false;
308 std::set<Cell*>::const_iterator narrow_it = narrowCells.find(cell);
309 if (narrow_it != narrowCells.end()) {
313 if (costsEmpty && defaultCost && defaultSpeed && areasEmpty &&
314 cellVisual && cellBlocker && !transition && !isNarrow) {
319 TiXmlElement* cellElement =
new TiXmlElement(
"cell");
320 cellElement->SetAttribute(
"x", cellCoord.
x);
321 cellElement->SetAttribute(
"y", cellCoord.
y);
330 cellElement->SetAttribute(
"state",
"revealed");
332 cellElement->SetAttribute(
"state",
"masked");
337 cellElement->SetAttribute(
"blocker_type",
"no_blocker");
339 cellElement->SetAttribute(
"blocker_type",
"blocker");
343 cellElement->SetAttribute(
"narrow",
true);
347 std::list<std::string>::iterator cost_it = costIds.begin();
348 for (; cost_it != costIds.end(); ++cost_it) {
350 TiXmlElement* costElement =
new TiXmlElement(
"cost");
351 costElement->SetAttribute(
"id", *cost_it);
352 costElement->SetDoubleAttribute(
"value", cache->
getCost(*cost_it));
353 cellElement->LinkEndChild(costElement);
359 std::vector<std::string>::iterator area_it = cellAreaIds.begin();
360 for (; area_it != cellAreaIds.end(); ++area_it) {
361 TiXmlElement* areaElement =
new TiXmlElement(
"area");
362 areaElement->SetAttribute(
"id", *area_it);
363 areaElement->LinkEndChild(areaElement);
368 TiXmlElement* transitionElement =
new TiXmlElement(
"transition");
369 transitionElement->SetAttribute(
"id", transition->
m_layer->
getId());
370 transitionElement->SetAttribute(
"x", transition->
m_mc.
x);
371 transitionElement->SetAttribute(
"y", transition->
m_mc.
y);
372 if (transition->
m_mc.
z != 0) {
373 transitionElement->SetAttribute(
"z", transition->
m_mc.
z);
376 transitionElement->SetAttribute(
"immediate",
true);
378 transitionElement->SetAttribute(
"immediate",
false);
380 cellElement->LinkEndChild(transitionElement);
382 cellcacheElement->LinkEndChild(cellElement);
385 cellcachesElement->LinkEndChild(cellcacheElement);
389 std::vector<Trigger*> triggers = triggerController->
getAllTriggers();
390 if (!triggers.empty()) {
392 TiXmlElement* triggersElement =
new TiXmlElement(
"triggers");
393 mapElement->LinkEndChild(triggersElement);
394 for (std::vector<Trigger*>::iterator iter = triggers.begin(); iter != triggers.end(); ++iter) {
396 TiXmlElement* triggerElement =
new TiXmlElement(
"trigger");
397 triggerElement->SetAttribute(
"name", (*iter)->getName());
398 triggerElement->SetAttribute(
"triggered", (*iter)->isTriggered());
399 triggerElement->SetAttribute(
"all_instances", (*iter)->isEnabledForAllInstances());
400 if ((*iter)->getAttached()) {
401 triggerElement->SetAttribute(
"attached_instance", (*iter)->getAttached()->getId());
402 triggerElement->SetAttribute(
"attached_layer", (*iter)->getAttached()->getLocationRef().getLayer()->getId());
404 const std::vector<Cell*>& cells = (*iter)->getAssignedCells();
405 if (!cells.empty()) {
406 for (std::vector<Cell*>::const_iterator citer = cells.begin(); citer != cells.end(); ++citer) {
407 TiXmlElement* cellElement =
new TiXmlElement(
"assign");
408 cellElement->SetAttribute(
"layer_id", (*citer)->getLayer()->getId());
409 cellElement->SetAttribute(
"x", (*citer)->getLayerCoordinates().x);
410 cellElement->SetAttribute(
"y", (*citer)->getLayerCoordinates().y);
411 triggerElement->LinkEndChild(cellElement);
414 const std::vector<Instance*>& instances = (*iter)->getEnabledInstances();
415 if (!instances.empty()) {
416 for (std::vector<Instance*>::const_iterator citer = instances.begin(); citer != instances.end(); ++citer) {
417 TiXmlElement* instanceElement =
new TiXmlElement(
"enabled");
418 instanceElement->SetAttribute(
"layer_id", (*citer)->getLocationRef().getLayer()->getId());
419 instanceElement->SetAttribute(
"instance_id", (*citer)->getId());
420 triggerElement->LinkEndChild(instanceElement);
423 const std::vector<TriggerCondition>& conditions = (*iter)->getTriggerConditions();
424 if (!conditions.empty()) {
425 for (std::vector<TriggerCondition>::const_iterator citer = conditions.begin(); citer != conditions.end(); ++citer) {
426 TiXmlElement* conditionElement =
new TiXmlElement(
"condition");
427 conditionElement->SetAttribute(
"id", (*citer));
428 triggerElement->LinkEndChild(conditionElement);
431 triggersElement->LinkEndChild(triggerElement);
435 typedef std::vector<Camera*> CameraContainer;
437 for (CameraContainer::iterator iter = cameras.begin(); iter != cameras.end(); ++iter)
439 if ((*iter)->getLocationRef().getMap()->getId() == map.
getId())
441 TiXmlElement* cameraElement =
new TiXmlElement(
"camera");
443 cameraElement->SetAttribute(
"id", (*iter)->getId());
444 cameraElement->SetAttribute(
"ref_layer_id", (*iter)->getLocation().getLayer()->getId());
445 cameraElement->SetDoubleAttribute(
"zoom", (*iter)->getZoom());
446 cameraElement->SetDoubleAttribute(
"tilt", (*iter)->getTilt());
447 cameraElement->SetDoubleAttribute(
"rotation", (*iter)->getRotation());
448 if ((*iter)->isZToYEnabled()) {
449 cameraElement->SetDoubleAttribute(
"ztoy", (*iter)->getZToY());
452 Rect viewport = (*iter)->getViewPort();
453 std::ostringstream viewportString;
454 viewportString << viewport.
x <<
","
459 cameraElement->SetAttribute(
"viewport", viewportString.str());
461 Point p = (*iter)->getCellImageDimensions();
462 cameraElement->SetAttribute(
"ref_cell_width", p.
x);
463 cameraElement->SetAttribute(
"ref_cell_height", p.
y);
465 std::vector<float> lightingColor = (*iter)->getLightingColor();
466 bool writeLightingColor =
false;
467 for (
uint32_t i=0; i < lightingColor.size(); ++i)
469 if (lightingColor[i] < 1.0)
471 writeLightingColor =
true;
476 if (writeLightingColor)
478 std::ostringstream lightingColorString;
479 for (
uint32_t i=0; i < lightingColor.size(); ++i)
483 lightingColorString <<
",";
486 lightingColorString << lightingColor[i];
488 cameraElement->SetAttribute(
"light_color", lightingColorString.str());
492 mapElement->LinkEndChild(cameraElement);
497 doc.SaveFile(filename);
double getCost() const
Returns the cost.
double getCostMultiplier()
Returns the current cell cost.
virtual void setAnimationSaver(const FIFE::AnimationSaverPtr &animationSaver)
allows setting which animation saver will be used to save animation files
Layer * m_layer
target layer
const std::string & getNamespace() const
int32_t getStackPosition()
Gets current stack position of instance.
T h
Height of the rectangle.
Instance visual contains data that is needed to visualize the instance on screen. ...
double getSpeedMultiplier()
Returns the current cell speed.
AtlasSaverPtr m_atlasSaver
const std::string & getId() const
A CellCache is an abstract depiction of one or a few layers and contains additional information...
ObjectSaverPtr m_objectSaver
virtual const std::string & getType() const =0
Type of cellgrid.
static Logger _log(LM_AUDIO)
std::string getCostId() const
Returns the cost id.
bool isMultiPart() const
Gets if object is a part of a multi object.
bool defaultCost()
Returns if cell use default cost.
virtual void setObjectSaver(const FIFE::ObjectSaverPtr &objectSaver)
allows setting which object saver will be used to save object files
TriggerController * getTriggerController() const
std::vector< Trigger * > getAllTriggers()
Returns a vector with all trigger pointers.
const double getZShift() const
Get the cellgrid z shift.
const double getYScale() const
Get the cellgrid y-scaling.
Simple class to hold the data for transistions.
virtual void setAtlasSaver(const FIFE::AtlasSaverPtr &atlasSaver)
allows setting which atlas saver will be used to save atlas files
static bool Equal(T _val1, T _val2)
double getDefaultSpeedMultiplier()
Gets default speed for this CellCache.
const std::set< Instance * > & getInstances()
Returns all instances on this cell.
std::list< std::string > getCosts()
Returns all registered cost ids.
CellTypeInfo getCellType()
Returns blocker type.
const double getXScale() const
Get the cellgrid x-scaling.
bool existsCostForCell(const std::string &costId, Cell *cell)
Gets if cell is assigned to cost identifier.
const double getRotation() const
Get the cellgrid rotation.
const double getXShift() const
Get the cellgrid x shift.
std::vector< std::string > getCellAreas(Cell *cell)
Returns all areas of a cell.
A basic cell on a CellCache.
const std::string & getId() const
Get the identifier for this map.
CellVisualEffect getFoWType()
Returns the cell visual.
const ModelCoordinate getLayerCoordinates() const
Returns the layer coordinates of this cell.
uint8_t getCellStackPosition() const
Returns cell stack position.
const std::string & getId() const
Get the id of this layer.
const std::list< Layer * > & getLayers() const
Get the layers on this map.
const std::vector< std::vector< Cell * > > & getCells()
Returns all cells of this CellCache.
bool isSpecialCost() const
Gets if object uses special cost.
const double getYShift() const
Get the cellgrid y shift.
double getCost(const std::string &costId)
Returns the cost value for the given id.
virtual void save(const Map &map, const std::string &filename, const std::vector< std::string > &importFiles)
responsible for saving the map resource used to save map files
TransitionInfo * getTransition()
Returns the transition.
This class serves as a central place to manage triggers for a Map.
const std::set< Cell * > & getNarrowCells()
Returns narrow cells.
double getDefaultCostMultiplier()
Gets default cost for this CellCache.
const std::vector< Camera * > & getCameras() const
Get a list containing all cameras.
bool defaultSpeed()
Returns if cell use default speed.
T w
Width of the rectangle.
bool m_immediate
use immediate
bool isSearchNarrowCells()
Gets if narrow cells should be searched automatic.
AnimationSaverPtr m_animationSaver
ModelCoordinate m_mc
target coordinates