|
|
@@ -5,70 +5,328 @@
|
|
|
#include "../filesystem/CResourceLoader.h"
|
|
|
#include "../CDefObjInfoHandler.h"
|
|
|
|
|
|
-CMapEditManager::CMapEditManager(CMap * map, int randomSeed /*= std::time(nullptr)*/)
|
|
|
+CMapOperation::CMapOperation(CMap * map) : map(map)
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+std::string CMapOperation::getLabel() const
|
|
|
+{
|
|
|
+ return "";
|
|
|
+}
|
|
|
+
|
|
|
+CMapUndoManager::CMapUndoManager() : undoRedoLimit(10)
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void CMapUndoManager::undo()
|
|
|
+{
|
|
|
+ doOperation(undoStack, redoStack, true);
|
|
|
+}
|
|
|
+
|
|
|
+void CMapUndoManager::redo()
|
|
|
+{
|
|
|
+ doOperation(redoStack, undoStack, false);
|
|
|
+}
|
|
|
+
|
|
|
+void CMapUndoManager::clearAll()
|
|
|
+{
|
|
|
+ undoStack.clear();
|
|
|
+ redoStack.clear();
|
|
|
+}
|
|
|
+
|
|
|
+int CMapUndoManager::getUndoRedoLimit() const
|
|
|
+{
|
|
|
+ return undoRedoLimit;
|
|
|
+}
|
|
|
+
|
|
|
+void CMapUndoManager::setUndoRedoLimit(int value)
|
|
|
+{
|
|
|
+ if(value < 0) throw std::runtime_error("Cannot set a negative value for the undo redo limit.");
|
|
|
+ undoStack.resize(std::min(undoStack.size(), static_cast<TStack::size_type>(value)));
|
|
|
+ redoStack.resize(std::min(redoStack.size(), static_cast<TStack::size_type>(value)));
|
|
|
+}
|
|
|
+
|
|
|
+const CMapOperation * CMapUndoManager::peekRedo() const
|
|
|
+{
|
|
|
+ return peek(redoStack);
|
|
|
+}
|
|
|
+
|
|
|
+const CMapOperation * CMapUndoManager::peekUndo() const
|
|
|
+{
|
|
|
+ return peek(undoStack);
|
|
|
+}
|
|
|
+
|
|
|
+void CMapUndoManager::addOperation(unique_ptr<CMapOperation> && operation)
|
|
|
+{
|
|
|
+ undoStack.push_front(std::move(operation));
|
|
|
+ if(undoStack.size() > undoRedoLimit) undoStack.pop_back();
|
|
|
+ redoStack.clear();
|
|
|
+}
|
|
|
+
|
|
|
+void CMapUndoManager::doOperation(TStack & fromStack, TStack & toStack, bool doUndo)
|
|
|
+{
|
|
|
+ if(fromStack.empty()) return;
|
|
|
+ auto & operation = fromStack.front();
|
|
|
+ if(doUndo)
|
|
|
+ {
|
|
|
+ operation->undo();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ operation->redo();
|
|
|
+ }
|
|
|
+ toStack.push_front(std::move(operation));
|
|
|
+ fromStack.pop_front();
|
|
|
+}
|
|
|
+
|
|
|
+const CMapOperation * CMapUndoManager::peek(const TStack & stack) const
|
|
|
+{
|
|
|
+ if(stack.empty()) return nullptr;
|
|
|
+ return stack.front().get();
|
|
|
+}
|
|
|
+
|
|
|
+CMapEditManager::CMapEditManager(CMap * map)
|
|
|
: map(map)
|
|
|
{
|
|
|
- gen.seed(randomSeed);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
-void CMapEditManager::clearTerrain()
|
|
|
+void CMapEditManager::clearTerrain(CRandomGenerator * gen)
|
|
|
{
|
|
|
for(int i = 0; i < map->width; ++i)
|
|
|
{
|
|
|
for(int j = 0; j < map->height; ++j)
|
|
|
{
|
|
|
- map->terrain[i][j][0].terType = ETerrainType::WATER;
|
|
|
- map->terrain[i][j][0].terView = gen.getInteger(20, 32);
|
|
|
+ map->getTile(int3(i, j, 0)).terType = ETerrainType::WATER;
|
|
|
+ map->getTile(int3(i, j, 0)).terView = gen->getInteger(20, 32);
|
|
|
|
|
|
if(map->twoLevel)
|
|
|
{
|
|
|
- map->terrain[i][j][1].terType = ETerrainType::ROCK;
|
|
|
- map->terrain[i][j][1].terView = 0;
|
|
|
+ map->getTile(int3(i, j, 1)).terType = ETerrainType::ROCK;
|
|
|
+ map->getTile(int3(i, j, 1)).terView = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CMapEditManager::drawTerrain(const MapRect & rect, ETerrainType terType, CRandomGenerator * gen)
|
|
|
+{
|
|
|
+ execute(make_unique<DrawTerrainOperation>(map, rect, terType, gen));
|
|
|
+}
|
|
|
+
|
|
|
+void CMapEditManager::insertObject(const int3 & pos, CGObjectInstance * obj)
|
|
|
+{
|
|
|
+ execute(make_unique<InsertObjectOperation>(map, pos, obj));
|
|
|
+}
|
|
|
+
|
|
|
+void CMapEditManager::execute(unique_ptr<CMapOperation> && operation)
|
|
|
+{
|
|
|
+ operation->execute();
|
|
|
+ undoManager.addOperation(std::move(operation));
|
|
|
+}
|
|
|
+
|
|
|
+void CMapEditManager::undo()
|
|
|
+{
|
|
|
+ undoManager.undo();
|
|
|
+}
|
|
|
+
|
|
|
+void CMapEditManager::redo()
|
|
|
+{
|
|
|
+ undoManager.redo();
|
|
|
+}
|
|
|
+
|
|
|
+CMapUndoManager & CMapEditManager::getUndoManager()
|
|
|
+{
|
|
|
+ return undoManager;
|
|
|
+}
|
|
|
+
|
|
|
+const std::string TerrainViewPattern::FLIP_MODE_SAME_IMAGE = "sameImage";
|
|
|
+const std::string TerrainViewPattern::FLIP_MODE_DIFF_IMAGES = "diffImages";
|
|
|
+
|
|
|
+const std::string TerrainViewPattern::RULE_DIRT = "D";
|
|
|
+const std::string TerrainViewPattern::RULE_SAND = "S";
|
|
|
+const std::string TerrainViewPattern::RULE_TRANSITION = "T";
|
|
|
+const std::string TerrainViewPattern::RULE_NATIVE = "N";
|
|
|
+const std::string TerrainViewPattern::RULE_ANY = "?";
|
|
|
+
|
|
|
+TerrainViewPattern::TerrainViewPattern() : minPoints(0), flipMode(FLIP_MODE_SAME_IMAGE),
|
|
|
+ terGroup(ETerrainGroup::NORMAL)
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+TerrainViewPattern::WeightedRule::WeightedRule() : points(0)
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+bool TerrainViewPattern::WeightedRule::isStandardRule() const
|
|
|
+{
|
|
|
+ return TerrainViewPattern::RULE_ANY == name || TerrainViewPattern::RULE_DIRT == name
|
|
|
+ || TerrainViewPattern::RULE_NATIVE == name || TerrainViewPattern::RULE_SAND == name
|
|
|
+ || TerrainViewPattern::RULE_TRANSITION == name;
|
|
|
+}
|
|
|
+
|
|
|
+boost::mutex CTerrainViewPatternConfig::smx;
|
|
|
+
|
|
|
+CTerrainViewPatternConfig & CTerrainViewPatternConfig::get()
|
|
|
+{
|
|
|
+ TLockGuard _(smx);
|
|
|
+ static CTerrainViewPatternConfig instance;
|
|
|
+ return instance;
|
|
|
+}
|
|
|
+
|
|
|
+CTerrainViewPatternConfig::CTerrainViewPatternConfig()
|
|
|
+{
|
|
|
+ const JsonNode config(ResourceID("config/terrainViewPatterns.json"));
|
|
|
+ const std::map<std::string, ETerrainGroup::ETerrainGroup> terGroups
|
|
|
+ = boost::assign::map_list_of("normal", ETerrainGroup::NORMAL)("dirt", ETerrainGroup::DIRT)
|
|
|
+ ("sand", ETerrainGroup::SAND)("water", ETerrainGroup::WATER)("rock", ETerrainGroup::ROCK);
|
|
|
+ BOOST_FOREACH(auto terMapping, terGroups)
|
|
|
+ {
|
|
|
+ BOOST_FOREACH(const JsonNode & ptrnNode, config[terMapping.first].Vector())
|
|
|
+ {
|
|
|
+ TerrainViewPattern pattern;
|
|
|
+
|
|
|
+ // Read pattern data
|
|
|
+ const JsonVector & data = ptrnNode["data"].Vector();
|
|
|
+ if(data.size() != 9)
|
|
|
+ {
|
|
|
+ throw std::runtime_error("Size of pattern's data vector has to be 9.");
|
|
|
}
|
|
|
+ for(int i = 0; i < data.size(); ++i)
|
|
|
+ {
|
|
|
+ std::string cell = data[i].String();
|
|
|
+ boost::algorithm::erase_all(cell, " ");
|
|
|
+ std::vector<std::string> rules;
|
|
|
+ boost::split(rules, cell, boost::is_any_of(","));
|
|
|
+ BOOST_FOREACH(std::string ruleStr, rules)
|
|
|
+ {
|
|
|
+ std::vector<std::string> ruleParts;
|
|
|
+ boost::split(ruleParts, ruleStr, boost::is_any_of("-"));
|
|
|
+ TerrainViewPattern::WeightedRule rule;
|
|
|
+ rule.name = ruleParts[0];
|
|
|
+ if(ruleParts.size() > 1)
|
|
|
+ {
|
|
|
+ rule.points = boost::lexical_cast<int>(ruleParts[1]);
|
|
|
+ }
|
|
|
+ pattern.data[i].push_back(rule);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Read mapping
|
|
|
+ std::string mappingStr = ptrnNode["mapping"].String();
|
|
|
+ boost::algorithm::erase_all(mappingStr, " ");
|
|
|
+ std::vector<std::string> mappings;
|
|
|
+ boost::split(mappings, mappingStr, boost::is_any_of(","));
|
|
|
+ BOOST_FOREACH(std::string mapping, mappings)
|
|
|
+ {
|
|
|
+ std::vector<std::string> range;
|
|
|
+ boost::split(range, mapping, boost::is_any_of("-"));
|
|
|
+ pattern.mapping.push_back(std::make_pair(boost::lexical_cast<int>(range[0]),
|
|
|
+ boost::lexical_cast<int>(range.size() > 1 ? range[1] : range[0])));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Read optional attributes
|
|
|
+ pattern.id = ptrnNode["id"].String();
|
|
|
+ pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
|
|
|
+ pattern.flipMode = ptrnNode["flipMode"].String();
|
|
|
+ if(pattern.flipMode.empty())
|
|
|
+ {
|
|
|
+ pattern.flipMode = TerrainViewPattern::FLIP_MODE_SAME_IMAGE;
|
|
|
+ }
|
|
|
+
|
|
|
+ pattern.terGroup = terMapping.second;
|
|
|
+ patterns[terMapping.second].push_back(pattern);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void CMapEditManager::drawTerrain(ETerrainType terType, int posx, int posy, int width, int height, bool underground)
|
|
|
+CTerrainViewPatternConfig::~CTerrainViewPatternConfig()
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+const std::vector<TerrainViewPattern> & CTerrainViewPatternConfig::getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const
|
|
|
+{
|
|
|
+ return patterns.find(terGroup)->second;
|
|
|
+}
|
|
|
+
|
|
|
+const TerrainViewPattern & CTerrainViewPatternConfig::getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const
|
|
|
+{
|
|
|
+ const std::vector<TerrainViewPattern> & groupPatterns = getPatternsForGroup(terGroup);
|
|
|
+ BOOST_FOREACH(const TerrainViewPattern & pattern, groupPatterns)
|
|
|
+ {
|
|
|
+ if(id == pattern.id)
|
|
|
+ {
|
|
|
+ return pattern;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw std::runtime_error("Pattern with ID not found: " + id);
|
|
|
+}
|
|
|
+
|
|
|
+DrawTerrainOperation::DrawTerrainOperation(CMap * map, const MapRect & rect, ETerrainType terType, CRandomGenerator * gen)
|
|
|
+ : CMapOperation(map), rect(rect), terType(terType), gen(gen)
|
|
|
+{
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void DrawTerrainOperation::execute()
|
|
|
{
|
|
|
- bool mapLevel = underground ? 1 : 0;
|
|
|
- for(int i = posx; i < posx + width; ++i)
|
|
|
+ for(int i = rect.pos.x; i < rect.pos.x + rect.width; ++i)
|
|
|
{
|
|
|
- for(int j = posy; j < posy + height; ++j)
|
|
|
+ for(int j = rect.pos.y; j < rect.pos.y + rect.height; ++j)
|
|
|
{
|
|
|
- map->terrain[i][j][mapLevel].terType = terType;
|
|
|
+ map->getTile(int3(i, j, rect.pos.z)).terType = terType;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//TODO there are situations where more tiles are affected implicitely
|
|
|
//TODO add coastal bit to extTileFlags appropriately
|
|
|
|
|
|
- updateTerrainViews(posx - 1, posy - 1, width + 2, height + 2, mapLevel);
|
|
|
+ updateTerrainViews(MapRect(int3(rect.pos.x - 1, rect.pos.y - 1, rect.pos.z), rect.width + 2, rect.height + 2));
|
|
|
}
|
|
|
|
|
|
-void CMapEditManager::updateTerrainViews(int posx, int posy, int width, int height, int mapLevel)
|
|
|
+void DrawTerrainOperation::undo()
|
|
|
{
|
|
|
- for(int i = posx; i < posx + width; ++i)
|
|
|
+ //TODO
|
|
|
+}
|
|
|
+
|
|
|
+void DrawTerrainOperation::redo()
|
|
|
+{
|
|
|
+ //TODO
|
|
|
+}
|
|
|
+
|
|
|
+std::string DrawTerrainOperation::getLabel() const
|
|
|
+{
|
|
|
+ return "Draw Terrain";
|
|
|
+}
|
|
|
+
|
|
|
+void DrawTerrainOperation::updateTerrainViews(const MapRect & rect)
|
|
|
+{
|
|
|
+ for(int i = rect.pos.x; i < rect.pos.x + rect.width; ++i)
|
|
|
{
|
|
|
- for(int j = posy; j < posy + height; ++j)
|
|
|
+ for(int j = rect.pos.y; j < rect.pos.y + rect.height; ++j)
|
|
|
{
|
|
|
- const std::vector<TerrainViewPattern> & patterns =
|
|
|
- CTerrainViewPatternConfig::get().getPatternsForGroup(getTerrainGroup(map->terrain[i][j][mapLevel].terType));
|
|
|
+ const auto & patterns =
|
|
|
+ CTerrainViewPatternConfig::get().getPatternsForGroup(getTerrainGroup(map->getTile(int3(i, j, rect.pos.z)).terType));
|
|
|
|
|
|
// Detect a pattern which fits best
|
|
|
int bestPattern = -1, bestFlip = -1;
|
|
|
std::string transitionReplacement;
|
|
|
for(int k = 0; k < patterns.size(); ++k)
|
|
|
{
|
|
|
- const TerrainViewPattern & pattern = patterns[k];
|
|
|
+ const auto & pattern = patterns[k];
|
|
|
|
|
|
for(int flip = 0; flip < 4; ++flip)
|
|
|
{
|
|
|
- ValidationResult valRslt = validateTerrainView(i, j, mapLevel, flip > 0 ? getFlippedPattern(pattern, flip) : pattern);
|
|
|
+ auto valRslt = validateTerrainView(int3(i, j, rect.pos.z), flip > 0 ? getFlippedPattern(pattern, flip) : pattern);
|
|
|
if(valRslt.result)
|
|
|
{
|
|
|
- logGlobal->debugStream() << "Pattern detected at pos " << i << "x" << j << "x" << mapLevel << ": P-Nr. " << k
|
|
|
- << ", Flip " << flip << ", Repl. " << valRslt.transitionReplacement;
|
|
|
+ logGlobal->debugStream() << "Pattern detected at pos " << i << "x" << j << "x" << rect.pos.z << ": P-Nr. " << k
|
|
|
+ << ", Flip " << flip << ", Repl. " << valRslt.transitionReplacement;
|
|
|
|
|
|
bestPattern = k;
|
|
|
bestFlip = flip;
|
|
|
@@ -80,7 +338,7 @@ void CMapEditManager::updateTerrainViews(int posx, int posy, int width, int heig
|
|
|
if(bestPattern == -1)
|
|
|
{
|
|
|
// This shouldn't be the case
|
|
|
- logGlobal->warnStream() << "No pattern detected at pos " << i << "x" << j << "x" << mapLevel;
|
|
|
+ logGlobal->warnStream() << "No pattern detected at pos " << i << "x" << j << "x" << rect.pos.z;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -97,23 +355,24 @@ void CMapEditManager::updateTerrainViews(int posx, int posy, int width, int heig
|
|
|
}
|
|
|
|
|
|
// Set terrain view
|
|
|
+ auto & tile = map->getTile(int3(i, j, rect.pos.z));
|
|
|
if(pattern.flipMode == TerrainViewPattern::FLIP_MODE_SAME_IMAGE)
|
|
|
{
|
|
|
- map->terrain[i][j][mapLevel].terView = gen.getInteger(mapping.first, mapping.second);
|
|
|
- map->terrain[i][j][mapLevel].extTileFlags = bestFlip;
|
|
|
+ tile.terView = gen->getInteger(mapping.first, mapping.second);
|
|
|
+ tile.extTileFlags = bestFlip;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
int range = (mapping.second - mapping.first) / 4;
|
|
|
- map->terrain[i][j][mapLevel].terView = gen.getInteger(mapping.first + bestFlip * range,
|
|
|
+ tile.terView = gen->getInteger(mapping.first + bestFlip * range,
|
|
|
mapping.first + (bestFlip + 1) * range - 1);
|
|
|
- map->terrain[i][j][mapLevel].extTileFlags = 0;
|
|
|
+ tile.extTileFlags = 0;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-ETerrainGroup::ETerrainGroup CMapEditManager::getTerrainGroup(ETerrainType terType) const
|
|
|
+ETerrainGroup::ETerrainGroup DrawTerrainOperation::getTerrainGroup(ETerrainType terType) const
|
|
|
{
|
|
|
switch(terType)
|
|
|
{
|
|
|
@@ -130,9 +389,9 @@ ETerrainGroup::ETerrainGroup CMapEditManager::getTerrainGroup(ETerrainType terTy
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-CMapEditManager::ValidationResult CMapEditManager::validateTerrainView(int posx, int posy, int mapLevel, const TerrainViewPattern & pattern, int recDepth /*= 0*/) const
|
|
|
+DrawTerrainOperation::ValidationResult DrawTerrainOperation::validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth /*= 0*/) const
|
|
|
{
|
|
|
- ETerrainType centerTerType = map->terrain[posx][posy][mapLevel].terType;
|
|
|
+ ETerrainType centerTerType = map->getTile(pos).terType;
|
|
|
int totalPoints = 0;
|
|
|
std::string transitionReplacement;
|
|
|
|
|
|
@@ -145,8 +404,8 @@ CMapEditManager::ValidationResult CMapEditManager::validateTerrainView(int posx,
|
|
|
}
|
|
|
|
|
|
// Get terrain group of the current cell
|
|
|
- int cx = posx + (i % 3) - 1;
|
|
|
- int cy = posy + (i / 3) - 1;
|
|
|
+ int cx = pos.x + (i % 3) - 1;
|
|
|
+ int cy = pos.y + (i / 3) - 1;
|
|
|
bool isAlien = false;
|
|
|
ETerrainType terType;
|
|
|
if(cx < 0 || cx >= map->width || cy < 0 || cy >= map->height)
|
|
|
@@ -155,7 +414,7 @@ CMapEditManager::ValidationResult CMapEditManager::validateTerrainView(int posx,
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- terType = map->terrain[cx][cy][mapLevel].terType;
|
|
|
+ terType = map->getTile(int3(cx, cy, pos.z)).terType;
|
|
|
if(terType != centerTerType)
|
|
|
{
|
|
|
isAlien = true;
|
|
|
@@ -171,8 +430,8 @@ CMapEditManager::ValidationResult CMapEditManager::validateTerrainView(int posx,
|
|
|
{
|
|
|
if(recDepth == 0)
|
|
|
{
|
|
|
- const TerrainViewPattern & patternForRule = CTerrainViewPatternConfig::get().getPatternById(pattern.terGroup, rule.name);
|
|
|
- ValidationResult rslt = validateTerrainView(cx, cy, mapLevel, patternForRule, 1);
|
|
|
+ const auto & patternForRule = CTerrainViewPatternConfig::get().getPatternById(pattern.terGroup, rule.name);
|
|
|
+ auto rslt = validateTerrainView(int3(cx, cy, pos.z), patternForRule, 1);
|
|
|
if(!rslt.result)
|
|
|
{
|
|
|
return ValidationResult(false);
|
|
|
@@ -260,7 +519,7 @@ CMapEditManager::ValidationResult CMapEditManager::validateTerrainView(int posx,
|
|
|
return ValidationResult(true, transitionReplacement);
|
|
|
}
|
|
|
|
|
|
-bool CMapEditManager::isSandType(ETerrainType terType) const
|
|
|
+bool DrawTerrainOperation::isSandType(ETerrainType terType) const
|
|
|
{
|
|
|
switch(terType)
|
|
|
{
|
|
|
@@ -273,7 +532,7 @@ bool CMapEditManager::isSandType(ETerrainType terType) const
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-TerrainViewPattern CMapEditManager::getFlippedPattern(const TerrainViewPattern & pattern, int flip) const
|
|
|
+TerrainViewPattern DrawTerrainOperation::getFlippedPattern(const TerrainViewPattern & pattern, int flip) const
|
|
|
{
|
|
|
if(flip == 0)
|
|
|
{
|
|
|
@@ -300,149 +559,45 @@ TerrainViewPattern CMapEditManager::getFlippedPattern(const TerrainViewPattern &
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-void CMapEditManager::insertObject(CGObjectInstance * obj, int posx, int posy, bool underground)
|
|
|
-{
|
|
|
- obj->pos = int3(posx, posy, underground ? 1 : 0);
|
|
|
- obj->id = ObjectInstanceID(map->objects.size());
|
|
|
- map->objects.push_back(obj);
|
|
|
- if(obj->ID == Obj::TOWN)
|
|
|
- {
|
|
|
- map->towns.push_back(static_cast<CGTownInstance *>(obj));
|
|
|
- }
|
|
|
- if(obj->ID == Obj::HERO)
|
|
|
- {
|
|
|
- map->heroes.push_back(static_cast<CGHeroInstance*>(obj));
|
|
|
- }
|
|
|
- map->addBlockVisTiles(obj);
|
|
|
-}
|
|
|
-
|
|
|
-CMapEditManager::ValidationResult::ValidationResult(bool result, const std::string & transitionReplacement /*= ""*/)
|
|
|
+DrawTerrainOperation::ValidationResult::ValidationResult(bool result, const std::string & transitionReplacement /*= ""*/)
|
|
|
: result(result), transitionReplacement(transitionReplacement)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
-const std::string TerrainViewPattern::FLIP_MODE_SAME_IMAGE = "sameImage";
|
|
|
-const std::string TerrainViewPattern::FLIP_MODE_DIFF_IMAGES = "diffImages";
|
|
|
-
|
|
|
-const std::string TerrainViewPattern::RULE_DIRT = "D";
|
|
|
-const std::string TerrainViewPattern::RULE_SAND = "S";
|
|
|
-const std::string TerrainViewPattern::RULE_TRANSITION = "T";
|
|
|
-const std::string TerrainViewPattern::RULE_NATIVE = "N";
|
|
|
-const std::string TerrainViewPattern::RULE_ANY = "?";
|
|
|
-
|
|
|
-TerrainViewPattern::TerrainViewPattern() : minPoints(0), flipMode(FLIP_MODE_SAME_IMAGE),
|
|
|
- terGroup(ETerrainGroup::NORMAL)
|
|
|
+InsertObjectOperation::InsertObjectOperation(CMap * map, const int3 & pos, CGObjectInstance * obj)
|
|
|
+ : CMapOperation(map), pos(pos), obj(obj)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
-TerrainViewPattern::WeightedRule::WeightedRule() : points(0)
|
|
|
-{
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-bool TerrainViewPattern::WeightedRule::isStandardRule() const
|
|
|
-{
|
|
|
- return TerrainViewPattern::RULE_ANY == name || TerrainViewPattern::RULE_DIRT == name
|
|
|
- || TerrainViewPattern::RULE_NATIVE == name || TerrainViewPattern::RULE_SAND == name
|
|
|
- || TerrainViewPattern::RULE_TRANSITION == name;
|
|
|
-}
|
|
|
-
|
|
|
-boost::mutex CTerrainViewPatternConfig::smx;
|
|
|
-
|
|
|
-CTerrainViewPatternConfig & CTerrainViewPatternConfig::get()
|
|
|
-{
|
|
|
- TLockGuard _(smx);
|
|
|
- static CTerrainViewPatternConfig instance;
|
|
|
- return instance;
|
|
|
-}
|
|
|
-
|
|
|
-CTerrainViewPatternConfig::CTerrainViewPatternConfig()
|
|
|
+void InsertObjectOperation::execute()
|
|
|
{
|
|
|
- const JsonNode config(ResourceID("config/terrainViewPatterns.json"));
|
|
|
- const std::map<std::string, ETerrainGroup::ETerrainGroup> terGroups
|
|
|
- = boost::assign::map_list_of("normal", ETerrainGroup::NORMAL)("dirt", ETerrainGroup::DIRT)
|
|
|
- ("sand", ETerrainGroup::SAND)("water", ETerrainGroup::WATER)("rock", ETerrainGroup::ROCK);
|
|
|
- BOOST_FOREACH(auto terMapping, terGroups)
|
|
|
+ obj->pos = pos;
|
|
|
+ obj->id = ObjectInstanceID(map->objects.size());
|
|
|
+ map->objects.push_back(obj);
|
|
|
+ if(obj->ID == Obj::TOWN)
|
|
|
{
|
|
|
- BOOST_FOREACH(const JsonNode & ptrnNode, config[terMapping.first].Vector())
|
|
|
- {
|
|
|
- TerrainViewPattern pattern;
|
|
|
-
|
|
|
- // Read pattern data
|
|
|
- const JsonVector & data = ptrnNode["data"].Vector();
|
|
|
- if(data.size() != 9)
|
|
|
- {
|
|
|
- throw std::runtime_error("Size of pattern's data vector has to be 9.");
|
|
|
- }
|
|
|
- for(int i = 0; i < data.size(); ++i)
|
|
|
- {
|
|
|
- std::string cell = data[i].String();
|
|
|
- boost::algorithm::erase_all(cell, " ");
|
|
|
- std::vector<std::string> rules;
|
|
|
- boost::split(rules, cell, boost::is_any_of(","));
|
|
|
- BOOST_FOREACH(std::string ruleStr, rules)
|
|
|
- {
|
|
|
- std::vector<std::string> ruleParts;
|
|
|
- boost::split(ruleParts, ruleStr, boost::is_any_of("-"));
|
|
|
- TerrainViewPattern::WeightedRule rule;
|
|
|
- rule.name = ruleParts[0];
|
|
|
- if(ruleParts.size() > 1)
|
|
|
- {
|
|
|
- rule.points = boost::lexical_cast<int>(ruleParts[1]);
|
|
|
- }
|
|
|
- pattern.data[i].push_back(rule);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Read mapping
|
|
|
- std::string mappingStr = ptrnNode["mapping"].String();
|
|
|
- boost::algorithm::erase_all(mappingStr, " ");
|
|
|
- std::vector<std::string> mappings;
|
|
|
- boost::split(mappings, mappingStr, boost::is_any_of(","));
|
|
|
- BOOST_FOREACH(std::string mapping, mappings)
|
|
|
- {
|
|
|
- std::vector<std::string> range;
|
|
|
- boost::split(range, mapping, boost::is_any_of("-"));
|
|
|
- pattern.mapping.push_back(std::make_pair(boost::lexical_cast<int>(range[0]),
|
|
|
- boost::lexical_cast<int>(range.size() > 1 ? range[1] : range[0])));
|
|
|
- }
|
|
|
-
|
|
|
- // Read optional attributes
|
|
|
- pattern.id = ptrnNode["id"].String();
|
|
|
- pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
|
|
|
- pattern.flipMode = ptrnNode["flipMode"].String();
|
|
|
- if(pattern.flipMode.empty())
|
|
|
- {
|
|
|
- pattern.flipMode = TerrainViewPattern::FLIP_MODE_SAME_IMAGE;
|
|
|
- }
|
|
|
-
|
|
|
- pattern.terGroup = terMapping.second;
|
|
|
- patterns[terMapping.second].push_back(pattern);
|
|
|
- }
|
|
|
+ map->towns.push_back(static_cast<CGTownInstance *>(obj));
|
|
|
+ }
|
|
|
+ if(obj->ID == Obj::HERO)
|
|
|
+ {
|
|
|
+ map->heroes.push_back(static_cast<CGHeroInstance*>(obj));
|
|
|
}
|
|
|
+ map->addBlockVisTiles(obj);
|
|
|
}
|
|
|
|
|
|
-CTerrainViewPatternConfig::~CTerrainViewPatternConfig()
|
|
|
+void InsertObjectOperation::undo()
|
|
|
{
|
|
|
-
|
|
|
+ //TODO
|
|
|
}
|
|
|
|
|
|
-const std::vector<TerrainViewPattern> & CTerrainViewPatternConfig::getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const
|
|
|
+void InsertObjectOperation::redo()
|
|
|
{
|
|
|
- return patterns.find(terGroup)->second;
|
|
|
+ execute();
|
|
|
}
|
|
|
|
|
|
-const TerrainViewPattern & CTerrainViewPatternConfig::getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const
|
|
|
+std::string InsertObjectOperation::getLabel() const
|
|
|
{
|
|
|
- const std::vector<TerrainViewPattern> & groupPatterns = getPatternsForGroup(terGroup);
|
|
|
- BOOST_FOREACH(const TerrainViewPattern & pattern, groupPatterns)
|
|
|
- {
|
|
|
- if(id == pattern.id)
|
|
|
- {
|
|
|
- return pattern;
|
|
|
- }
|
|
|
- }
|
|
|
- throw std::runtime_error("Pattern with ID not found: " + id);
|
|
|
+ return "Insert Object";
|
|
|
}
|