2013-01-06 22:30:12 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CMapEditManager.h"
|
|
|
|
|
|
|
|
#include "../JsonNode.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../filesystem/Filesystem.h"
|
2014-06-05 14:19:47 +03:00
|
|
|
#include "../mapObjects/CObjectClassesHandler.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
2013-08-17 15:46:48 +03:00
|
|
|
#include "../VCMI_Lib.h"
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-22 17:49:28 +03:00
|
|
|
MapRect::MapRect() : x(0), y(0), z(0), width(0), height(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
MapRect::MapRect(int3 pos, si32 width, si32 height) : x(pos.x), y(pos.y), z(pos.z), width(width), height(height)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
MapRect MapRect::operator&(const MapRect & rect) const
|
|
|
|
{
|
|
|
|
bool intersect = right() > rect.left() && rect.right() > left() &&
|
|
|
|
bottom() > rect.top() && rect.bottom() > top() &&
|
|
|
|
z == rect.z;
|
|
|
|
if(intersect)
|
|
|
|
{
|
|
|
|
MapRect ret;
|
|
|
|
ret.x = std::max(left(), rect.left());
|
|
|
|
ret.y = std::max(top(), rect.top());
|
|
|
|
ret.z = rect.z;
|
|
|
|
ret.width = std::min(right(), rect.right()) - ret.x;
|
|
|
|
ret.height = std::min(bottom(), rect.bottom()) - ret.y;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return MapRect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 MapRect::left() const
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 MapRect::right() const
|
|
|
|
{
|
|
|
|
return x + width;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 MapRect::top() const
|
|
|
|
{
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 MapRect::bottom() const
|
|
|
|
{
|
|
|
|
return y + height;
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 MapRect::topLeft() const
|
|
|
|
{
|
|
|
|
return int3(x, y, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 MapRect::topRight() const
|
|
|
|
{
|
|
|
|
return int3(right(), y, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 MapRect::bottomLeft() const
|
|
|
|
{
|
|
|
|
return int3(x, bottom(), z);
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 MapRect::bottomRight() const
|
|
|
|
{
|
|
|
|
return int3(right(), bottom(), z);
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CTerrainSelection::CTerrainSelection(CMap * map) : CMapSelection(map)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerrainSelection::selectRange(const MapRect & rect)
|
|
|
|
{
|
|
|
|
rect.forEach([this](const int3 pos)
|
|
|
|
{
|
|
|
|
this->select(pos);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerrainSelection::deselectRange(const MapRect & rect)
|
|
|
|
{
|
|
|
|
rect.forEach([this](const int3 pos)
|
|
|
|
{
|
|
|
|
this->deselect(pos);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-05-24 15:06:08 +03:00
|
|
|
void CTerrainSelection::setSelection(std::vector<int3> & vec)
|
|
|
|
{
|
|
|
|
for (auto pos : vec)
|
|
|
|
this->select(pos);
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
void CTerrainSelection::selectAll()
|
|
|
|
{
|
|
|
|
selectRange(MapRect(int3(0, 0, 0), getMap()->width, getMap()->height));
|
|
|
|
selectRange(MapRect(int3(0, 0, 1), getMap()->width, getMap()->height));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTerrainSelection::clearSelection()
|
|
|
|
{
|
|
|
|
deselectRange(MapRect(int3(0, 0, 0), getMap()->width, getMap()->height));
|
|
|
|
deselectRange(MapRect(int3(0, 0, 1), getMap()->width, getMap()->height));
|
|
|
|
}
|
|
|
|
|
|
|
|
CObjectSelection::CObjectSelection(CMap * map) : CMapSelection(map)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
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)
|
|
|
|
{
|
2013-04-22 19:04:17 +03:00
|
|
|
assert(value >= 0);
|
2013-04-19 14:43:11 +03:00
|
|
|
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)
|
2013-04-29 18:51:39 +03:00
|
|
|
: map(map), terrainSel(map), objectSel(map)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CMap * CMapEditManager::getMap()
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
return map;
|
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
void CMapEditManager::clearTerrain(CRandomGenerator * gen/* = nullptr*/)
|
|
|
|
{
|
|
|
|
execute(make_unique<CClearTerrainOperation>(map, gen ? gen : &(this->gen)));
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
void CMapEditManager::drawTerrain(ETerrainType terType, CRandomGenerator * gen/* = nullptr*/)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
execute(make_unique<CDrawTerrainOperation>(map, terrainSel, terType, gen ? gen : &(this->gen)));
|
|
|
|
terrainSel.clearSelection();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
void CMapEditManager::insertObject(CGObjectInstance * obj, const int3 & pos)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
execute(make_unique<CInsertObjectOperation>(map, obj, pos));
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMapEditManager::execute(unique_ptr<CMapOperation> && operation)
|
|
|
|
{
|
|
|
|
operation->execute();
|
|
|
|
undoManager.addOperation(std::move(operation));
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CTerrainSelection & CMapEditManager::getTerrainSelection()
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
return terrainSel;
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CObjectSelection & CMapEditManager::getObjectSelection()
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
return objectSel;
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CMapUndoManager & CMapEditManager::getUndoManager()
|
|
|
|
{
|
|
|
|
return undoManager;
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CComposedOperation::CComposedOperation(CMap * map) : CMapOperation(map)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CComposedOperation::execute()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & operation : operations)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
|
|
|
operation->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CComposedOperation::undo()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & operation : operations)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
|
|
|
operation->undo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CComposedOperation::redo()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & operation : operations)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
|
|
|
operation->redo();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CComposedOperation::addOperation(unique_ptr<CMapOperation> && operation)
|
|
|
|
{
|
|
|
|
operations.push_back(std::move(operation));
|
|
|
|
}
|
|
|
|
|
2013-04-26 18:57:47 +03:00
|
|
|
const std::string TerrainViewPattern::FLIP_MODE_DIFF_IMAGES = "D";
|
2013-04-19 14:43:11 +03:00
|
|
|
|
|
|
|
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";
|
2013-05-03 13:15:59 +03:00
|
|
|
const std::string TerrainViewPattern::RULE_NATIVE_STRONG = "N!";
|
2013-04-19 14:43:11 +03:00
|
|
|
const std::string TerrainViewPattern::RULE_ANY = "?";
|
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
TerrainViewPattern::TerrainViewPattern() : diffImages(false), rotationTypesCount(0), minPoints(0)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-26 18:57:47 +03:00
|
|
|
maxPoints = std::numeric_limits<int>::max();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2013-05-03 13:15:59 +03:00
|
|
|
|| TerrainViewPattern::RULE_TRANSITION == name || TerrainViewPattern::RULE_NATIVE_STRONG == name;
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CTerrainViewPatternConfig::CTerrainViewPatternConfig()
|
|
|
|
{
|
|
|
|
const JsonNode config(ResourceID("config/terrainViewPatterns.json"));
|
2013-05-03 13:15:59 +03:00
|
|
|
static const std::string patternTypes[] = { "terrainView", "terrainType" };
|
|
|
|
for(int i = 0; i < ARRAY_COUNT(patternTypes); ++i)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
const auto & patternsVec = config[patternTypes[i]].Vector();
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto & ptrnNode : patternsVec)
|
2013-04-26 18:57:47 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
TerrainViewPattern pattern;
|
|
|
|
|
|
|
|
// Read pattern data
|
|
|
|
const JsonVector & data = ptrnNode["data"].Vector();
|
|
|
|
assert(data.size() == 9);
|
2013-06-20 00:26:27 +03:00
|
|
|
for(int j = 0; j < data.size(); ++j)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-06-20 00:26:27 +03:00
|
|
|
std::string cell = data[j].String();
|
2013-05-03 13:15:59 +03:00
|
|
|
boost::algorithm::erase_all(cell, " ");
|
|
|
|
std::vector<std::string> rules;
|
|
|
|
boost::split(rules, cell, boost::is_any_of(","));
|
2013-06-29 16:05:48 +03:00
|
|
|
for(std::string ruleStr : rules)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
std::vector<std::string> ruleParts;
|
|
|
|
boost::split(ruleParts, ruleStr, boost::is_any_of("-"));
|
|
|
|
TerrainViewPattern::WeightedRule rule;
|
|
|
|
rule.name = ruleParts[0];
|
|
|
|
assert(!rule.name.empty());
|
|
|
|
if(ruleParts.size() > 1)
|
|
|
|
{
|
|
|
|
rule.points = boost::lexical_cast<int>(ruleParts[1]);
|
|
|
|
}
|
2013-06-20 00:26:27 +03:00
|
|
|
pattern.data[j].push_back(rule);
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
}
|
2013-04-26 18:57:47 +03:00
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
// Read various properties
|
|
|
|
pattern.id = ptrnNode["id"].String();
|
|
|
|
assert(!pattern.id.empty());
|
|
|
|
pattern.minPoints = static_cast<int>(ptrnNode["minPoints"].Float());
|
|
|
|
pattern.maxPoints = static_cast<int>(ptrnNode["maxPoints"].Float());
|
|
|
|
if(pattern.maxPoints == 0) pattern.maxPoints = std::numeric_limits<int>::max();
|
2013-04-19 14:43:11 +03:00
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
// Read mapping
|
|
|
|
if(i == 0)
|
2013-04-26 18:57:47 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
const auto & mappingStruct = ptrnNode["mapping"].Struct();
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto & mappingPair : mappingStruct)
|
2013-05-03 13:15:59 +03:00
|
|
|
{
|
|
|
|
TerrainViewPattern terGroupPattern = pattern;
|
|
|
|
auto mappingStr = mappingPair.second.String();
|
|
|
|
boost::algorithm::erase_all(mappingStr, " ");
|
|
|
|
auto colonIndex = mappingStr.find_first_of(":");
|
|
|
|
const auto & flipMode = mappingStr.substr(0, colonIndex);
|
|
|
|
terGroupPattern.diffImages = TerrainViewPattern::FLIP_MODE_DIFF_IMAGES == &(flipMode[flipMode.length() - 1]);
|
|
|
|
if(terGroupPattern.diffImages)
|
|
|
|
{
|
|
|
|
terGroupPattern.rotationTypesCount = boost::lexical_cast<int>(flipMode.substr(0, flipMode.length() - 1));
|
|
|
|
assert(terGroupPattern.rotationTypesCount == 2 || terGroupPattern.rotationTypesCount == 4);
|
|
|
|
}
|
|
|
|
mappingStr = mappingStr.substr(colonIndex + 1);
|
|
|
|
std::vector<std::string> mappings;
|
|
|
|
boost::split(mappings, mappingStr, boost::is_any_of(","));
|
2013-06-29 16:05:48 +03:00
|
|
|
for(std::string mapping : mappings)
|
2013-05-03 13:15:59 +03:00
|
|
|
{
|
|
|
|
std::vector<std::string> range;
|
|
|
|
boost::split(range, mapping, boost::is_any_of("-"));
|
|
|
|
terGroupPattern.mapping.push_back(std::make_pair(boost::lexical_cast<int>(range[0]),
|
|
|
|
boost::lexical_cast<int>(range.size() > 1 ? range[1] : range[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add pattern to the patterns map
|
|
|
|
const auto & terGroup = getTerrainGroup(mappingPair.first);
|
|
|
|
terrainViewPatterns[terGroup].push_back(terGroupPattern);
|
|
|
|
}
|
2013-04-26 18:57:47 +03:00
|
|
|
}
|
2013-05-03 13:15:59 +03:00
|
|
|
else if(i == 1)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
terrainTypePatterns[pattern.id] = pattern;
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
CTerrainViewPatternConfig::~CTerrainViewPatternConfig()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-22 17:49:28 +03:00
|
|
|
ETerrainGroup::ETerrainGroup CTerrainViewPatternConfig::getTerrainGroup(const std::string & terGroup) const
|
|
|
|
{
|
|
|
|
static 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);
|
|
|
|
auto it = terGroups.find(terGroup);
|
|
|
|
if(it == terGroups.end()) throw std::runtime_error(boost::str(boost::format("Terrain group '%s' does not exist.") % terGroup));
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
const std::vector<TerrainViewPattern> & CTerrainViewPatternConfig::getTerrainViewPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
return terrainViewPatterns.find(terGroup)->second;
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
boost::optional<const TerrainViewPattern &> CTerrainViewPatternConfig::getTerrainViewPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
const std::vector<TerrainViewPattern> & groupPatterns = getTerrainViewPatternsForGroup(terGroup);
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const TerrainViewPattern & pattern : groupPatterns)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
if(id == pattern.id)
|
|
|
|
{
|
2013-04-26 18:57:47 +03:00
|
|
|
return boost::optional<const TerrainViewPattern &>(pattern);
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
}
|
2013-04-26 18:57:47 +03:00
|
|
|
return boost::optional<const TerrainViewPattern &>();
|
2013-04-19 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
const TerrainViewPattern & CTerrainViewPatternConfig::getTerrainTypePatternById(const std::string & id) const
|
|
|
|
{
|
|
|
|
auto it = terrainTypePatterns.find(id);
|
|
|
|
assert(it != terrainTypePatterns.end());
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CDrawTerrainOperation::CDrawTerrainOperation(CMap * map, const CTerrainSelection & terrainSel, ETerrainType terType, CRandomGenerator * gen)
|
|
|
|
: CMapOperation(map), terrainSel(terrainSel), terType(terType), gen(gen)
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
void CDrawTerrainOperation::execute()
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto & pos : terrainSel.getSelectedItems())
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
auto & tile = map->getTile(pos);
|
|
|
|
tile.terType = terType;
|
|
|
|
invalidateTerrainViews(pos);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
updateTerrainTypes();
|
|
|
|
updateTerrainViews();
|
2013-01-06 22:30:12 +03:00
|
|
|
//TODO add coastal bit to extTileFlags appropriately
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
void CDrawTerrainOperation::undo()
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
void CDrawTerrainOperation::redo()
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
std::string CDrawTerrainOperation::getLabel() const
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
return "Draw Terrain";
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
void CDrawTerrainOperation::updateTerrainTypes()
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
auto positions = terrainSel.getSelectedItems();
|
|
|
|
while(!positions.empty())
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
const auto & centerPos = *(positions.begin());
|
|
|
|
auto centerTile = map->getTile(centerPos);
|
|
|
|
auto tiles = getInvalidTiles(centerPos);
|
2013-05-03 13:15:59 +03:00
|
|
|
auto updateTerrainType = [&](const int3 & pos, bool tileRequiresValidation)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
map->getTile(pos).terType = centerTile.terType;
|
2013-05-03 13:15:59 +03:00
|
|
|
if(tileRequiresValidation) positions.insert(pos);
|
2013-04-29 18:51:39 +03:00
|
|
|
invalidateTerrainViews(pos);
|
|
|
|
logGlobal->debugStream() << boost::format("Update terrain tile at '%s' to type '%i'.") % pos % centerTile.terType;
|
|
|
|
};
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
// Fill foreign invalid tiles
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto & tile : tiles.foreignTiles)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
updateTerrainType(tile, true);
|
2013-04-29 18:51:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(tiles.nativeTiles.find(centerPos) != tiles.nativeTiles.end())
|
|
|
|
{
|
|
|
|
// Blow up
|
|
|
|
auto rect = extendTileAroundSafely(centerPos);
|
|
|
|
std::set<int3> suitableTiles;
|
2013-05-03 13:15:59 +03:00
|
|
|
int invalidForeignTilesCnt = std::numeric_limits<int>::max(), invalidNativeTilesCnt = 0;
|
2013-04-29 18:51:39 +03:00
|
|
|
rect.forEach([&](const int3 & posToTest)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
auto & terrainTile = map->getTile(posToTest);
|
|
|
|
if(centerTile.terType != terrainTile.terType)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
auto formerTerType = terrainTile.terType;
|
|
|
|
terrainTile.terType = centerTile.terType;
|
|
|
|
auto testTile = getInvalidTiles(posToTest);
|
|
|
|
auto addToSuitableTiles = [&](const int3 & pos)
|
|
|
|
{
|
|
|
|
suitableTiles.insert(pos);
|
2013-05-03 13:15:59 +03:00
|
|
|
logGlobal->debugStream() << boost::format(std::string("Found suitable tile '%s' for main tile '%s': ") +
|
|
|
|
"Invalid native tiles '%i', invalid foreign tiles '%i'.") % pos % centerPos % testTile.nativeTiles.size() %
|
|
|
|
testTile.foreignTiles.size();
|
2013-04-29 18:51:39 +03:00
|
|
|
};
|
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
int nativeTilesCntNorm = testTile.nativeTiles.empty() ? std::numeric_limits<int>::max() : testTile.nativeTiles.size();
|
|
|
|
if(nativeTilesCntNorm > invalidNativeTilesCnt ||
|
|
|
|
(nativeTilesCntNorm == invalidNativeTilesCnt && testTile.foreignTiles.size() < invalidForeignTilesCnt))
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
invalidNativeTilesCnt = nativeTilesCntNorm;
|
|
|
|
invalidForeignTilesCnt = testTile.foreignTiles.size();
|
2013-04-29 18:51:39 +03:00
|
|
|
suitableTiles.clear();
|
|
|
|
addToSuitableTiles(posToTest);
|
|
|
|
}
|
2013-05-03 13:15:59 +03:00
|
|
|
else if(nativeTilesCntNorm == invalidNativeTilesCnt &&
|
2013-04-29 18:51:39 +03:00
|
|
|
testTile.foreignTiles.size() == invalidForeignTilesCnt)
|
|
|
|
{
|
|
|
|
addToSuitableTiles(posToTest);
|
|
|
|
}
|
|
|
|
terrainTile.terType = formerTerType;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-04-29 18:51:39 +03:00
|
|
|
});
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-05-03 13:15:59 +03:00
|
|
|
bool tileRequiresValidation = invalidForeignTilesCnt > 0;
|
2013-04-29 18:51:39 +03:00
|
|
|
if(suitableTiles.size() == 1)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
updateTerrainType(*suitableTiles.begin(), tileRequiresValidation);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
static const int3 directions[] = { int3(0, -1, 0), int3(-1, 0, 0), int3(0, 1, 0), int3(1, 0, 0),
|
2013-04-29 18:51:39 +03:00
|
|
|
int3(-1, -1, 0), int3(-1, 1, 0), int3(1, 1, 0), int3(1, -1, 0)};
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & direction : directions)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
auto it = suitableTiles.find(centerPos + direction);
|
2013-04-29 18:51:39 +03:00
|
|
|
if(it != suitableTiles.end())
|
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
updateTerrainType(*it, tileRequiresValidation);
|
2013-04-29 18:51:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-04-29 18:51:39 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
positions.erase(centerPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
void CDrawTerrainOperation::updateTerrainViews()
|
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for(const auto & pos : invalidatedTerViews)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
|
|
|
const auto & patterns =
|
2013-08-17 15:46:48 +03:00
|
|
|
VLC->terviewh->getTerrainViewPatternsForGroup(getTerrainGroup(map->getTile(pos).terType));
|
2013-04-29 18:51:39 +03:00
|
|
|
|
|
|
|
// Detect a pattern which fits best
|
|
|
|
int bestPattern = -1;
|
|
|
|
ValidationResult valRslt(false);
|
|
|
|
for(int k = 0; k < patterns.size(); ++k)
|
|
|
|
{
|
|
|
|
const auto & pattern = patterns[k];
|
|
|
|
valRslt = validateTerrainView(pos, pattern);
|
|
|
|
if(valRslt.result)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-29 18:51:39 +03:00
|
|
|
/*logGlobal->debugStream() << boost::format("Pattern detected at pos '%s': Pattern '%s', Flip '%i', Repl. '%s'.") %
|
|
|
|
pos % pattern.id % valRslt.flip % valRslt.transitionReplacement;*/
|
|
|
|
bestPattern = k;
|
|
|
|
break;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
2013-05-03 13:15:59 +03:00
|
|
|
//assert(bestPattern != -1);
|
2013-04-29 18:51:39 +03:00
|
|
|
if(bestPattern == -1)
|
|
|
|
{
|
|
|
|
// This shouldn't be the case
|
|
|
|
logGlobal->warnStream() << boost::format("No pattern detected at pos '%s'.") % pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get mapping
|
|
|
|
const TerrainViewPattern & pattern = patterns[bestPattern];
|
|
|
|
std::pair<int, int> mapping;
|
|
|
|
if(valRslt.transitionReplacement.empty())
|
|
|
|
{
|
|
|
|
mapping = pattern.mapping[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mapping = valRslt.transitionReplacement == TerrainViewPattern::RULE_DIRT ? pattern.mapping[0] : pattern.mapping[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set terrain view
|
|
|
|
auto & tile = map->getTile(pos);
|
|
|
|
if(!pattern.diffImages)
|
|
|
|
{
|
2014-03-17 22:51:07 +03:00
|
|
|
tile.terView = gen->nextInt(mapping.first, mapping.second);
|
2013-04-29 18:51:39 +03:00
|
|
|
tile.extTileFlags = valRslt.flip;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const int framesPerRot = (mapping.second - mapping.first + 1) / pattern.rotationTypesCount;
|
|
|
|
int flip = (pattern.rotationTypesCount == 2 && valRslt.flip == 2) ? 1 : valRslt.flip;
|
|
|
|
int firstFrame = mapping.first + flip * framesPerRot;
|
2014-03-17 22:51:07 +03:00
|
|
|
tile.terView = gen->nextInt(firstFrame, firstFrame + framesPerRot - 1);
|
2013-04-29 18:51:39 +03:00
|
|
|
tile.extTileFlags = 0;
|
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
ETerrainGroup::ETerrainGroup CDrawTerrainOperation::getTerrainGroup(ETerrainType terType) const
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
switch(terType)
|
|
|
|
{
|
|
|
|
case ETerrainType::DIRT:
|
|
|
|
return ETerrainGroup::DIRT;
|
|
|
|
case ETerrainType::SAND:
|
|
|
|
return ETerrainGroup::SAND;
|
|
|
|
case ETerrainType::WATER:
|
|
|
|
return ETerrainGroup::WATER;
|
|
|
|
case ETerrainType::ROCK:
|
|
|
|
return ETerrainGroup::ROCK;
|
|
|
|
default:
|
|
|
|
return ETerrainGroup::NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
CDrawTerrainOperation::ValidationResult CDrawTerrainOperation::validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth /*= 0*/) const
|
2013-04-25 19:09:48 +03:00
|
|
|
{
|
2014-07-24 21:36:52 +03:00
|
|
|
//constructor for pattern object is very expensive, but we can't manipulate const object :(
|
|
|
|
|
2014-07-24 20:16:49 +03:00
|
|
|
auto flippedPattern = pattern;
|
2013-04-25 19:09:48 +03:00
|
|
|
for(int flip = 0; flip < 4; ++flip)
|
|
|
|
{
|
2014-07-24 21:36:52 +03:00
|
|
|
if (flip > 0)
|
|
|
|
flipPattern (flippedPattern, flip);
|
|
|
|
auto valRslt = validateTerrainViewInner(pos, flippedPattern, recDepth);
|
2013-04-25 19:09:48 +03:00
|
|
|
if(valRslt.result)
|
|
|
|
{
|
|
|
|
valRslt.flip = flip;
|
|
|
|
return valRslt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ValidationResult(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
CDrawTerrainOperation::ValidationResult CDrawTerrainOperation::validateTerrainViewInner(const int3 & pos, const TerrainViewPattern & pattern, int recDepth /*= 0*/) const
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
auto centerTerType = map->getTile(pos).terType;
|
|
|
|
auto centerTerGroup = getTerrainGroup(centerTerType);
|
2013-01-06 22:30:12 +03:00
|
|
|
int totalPoints = 0;
|
|
|
|
std::string transitionReplacement;
|
|
|
|
|
|
|
|
for(int i = 0; i < 9; ++i)
|
|
|
|
{
|
|
|
|
// The center, middle cell can be skipped
|
|
|
|
if(i == 4)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get terrain group of the current cell
|
2013-04-19 14:43:11 +03:00
|
|
|
int cx = pos.x + (i % 3) - 1;
|
|
|
|
int cy = pos.y + (i / 3) - 1;
|
2013-04-25 19:09:48 +03:00
|
|
|
int3 currentPos(cx, cy, pos.z);
|
2013-01-06 22:30:12 +03:00
|
|
|
bool isAlien = false;
|
2013-02-13 01:24:48 +03:00
|
|
|
ETerrainType terType;
|
2013-04-25 19:09:48 +03:00
|
|
|
if(!map->isInTheMap(currentPos))
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
terType = centerTerType;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-25 19:09:48 +03:00
|
|
|
terType = map->getTile(currentPos).terType;
|
2013-01-06 22:30:12 +03:00
|
|
|
if(terType != centerTerType)
|
|
|
|
{
|
|
|
|
isAlien = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all rules per cell
|
|
|
|
int topPoints = -1;
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & elem : pattern.data[i])
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
TerrainViewPattern::WeightedRule rule = elem;
|
2013-01-26 16:48:20 +03:00
|
|
|
if(!rule.isStandardRule())
|
|
|
|
{
|
2013-04-26 18:57:47 +03:00
|
|
|
if(recDepth == 0 && map->isInTheMap(currentPos))
|
2013-01-26 16:48:20 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
if(terType == centerTerType)
|
2013-01-26 16:48:20 +03:00
|
|
|
{
|
2013-08-17 15:46:48 +03:00
|
|
|
const auto & patternForRule = VLC->terviewh->getTerrainViewPatternById(getTerrainGroup(centerTerType), rule.name);
|
2013-05-03 13:15:59 +03:00
|
|
|
if(patternForRule)
|
|
|
|
{
|
|
|
|
auto rslt = validateTerrainView(currentPos, *patternForRule, 1);
|
|
|
|
if(rslt.result) topPoints = std::max(topPoints, rule.points);
|
|
|
|
}
|
2013-01-26 16:48:20 +03:00
|
|
|
}
|
2013-04-25 19:09:48 +03:00
|
|
|
continue;
|
2013-01-26 16:48:20 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rule.name = TerrainViewPattern::RULE_NATIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto applyValidationRslt = [&](bool rslt)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
if(rslt)
|
|
|
|
{
|
2013-01-26 16:48:20 +03:00
|
|
|
topPoints = std::max(topPoints, rule.points);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Validate cell with the ruleset of the pattern
|
2013-05-03 13:15:59 +03:00
|
|
|
bool nativeTestOk, nativeTestStrongOk;
|
|
|
|
nativeTestOk = nativeTestStrongOk = (rule.name == TerrainViewPattern::RULE_NATIVE_STRONG || rule.name == TerrainViewPattern::RULE_NATIVE) && !isAlien;
|
|
|
|
if(centerTerGroup == ETerrainGroup::NORMAL)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-26 18:57:47 +03:00
|
|
|
bool dirtTestOk = (rule.name == TerrainViewPattern::RULE_DIRT || rule.name == TerrainViewPattern::RULE_TRANSITION)
|
2013-01-06 22:30:12 +03:00
|
|
|
&& isAlien && !isSandType(terType);
|
2013-04-26 18:57:47 +03:00
|
|
|
bool sandTestOk = (rule.name == TerrainViewPattern::RULE_SAND || rule.name == TerrainViewPattern::RULE_TRANSITION)
|
2013-01-06 22:30:12 +03:00
|
|
|
&& isSandType(terType);
|
|
|
|
|
2013-04-25 19:09:48 +03:00
|
|
|
if(transitionReplacement.empty() && rule.name == TerrainViewPattern::RULE_TRANSITION
|
|
|
|
&& (dirtTestOk || sandTestOk))
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-01-26 16:48:20 +03:00
|
|
|
transitionReplacement = dirtTestOk ? TerrainViewPattern::RULE_DIRT : TerrainViewPattern::RULE_SAND;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-04-25 19:09:48 +03:00
|
|
|
if(rule.name == TerrainViewPattern::RULE_TRANSITION)
|
|
|
|
{
|
|
|
|
applyValidationRslt((dirtTestOk && transitionReplacement != TerrainViewPattern::RULE_SAND) ||
|
|
|
|
(sandTestOk && transitionReplacement != TerrainViewPattern::RULE_DIRT));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-26 18:57:47 +03:00
|
|
|
applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || dirtTestOk || sandTestOk || nativeTestOk);
|
2013-04-25 19:09:48 +03:00
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-05-03 13:15:59 +03:00
|
|
|
else if(centerTerGroup == ETerrainGroup::DIRT)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
nativeTestOk = rule.name == TerrainViewPattern::RULE_NATIVE && !isSandType(terType);
|
2013-04-26 18:57:47 +03:00
|
|
|
bool sandTestOk = (rule.name == TerrainViewPattern::RULE_SAND || rule.name == TerrainViewPattern::RULE_TRANSITION)
|
|
|
|
&& isSandType(terType);
|
2013-05-03 13:15:59 +03:00
|
|
|
applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk || nativeTestStrongOk);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-05-03 13:15:59 +03:00
|
|
|
else if(centerTerGroup == ETerrainGroup::SAND)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-26 18:57:47 +03:00
|
|
|
applyValidationRslt(true);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2013-05-03 13:15:59 +03:00
|
|
|
else if(centerTerGroup == ETerrainGroup::WATER || centerTerGroup == ETerrainGroup::ROCK)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-26 18:57:47 +03:00
|
|
|
bool sandTestOk = (rule.name == TerrainViewPattern::RULE_SAND || rule.name == TerrainViewPattern::RULE_TRANSITION)
|
|
|
|
&& isAlien;
|
2013-01-26 16:48:20 +03:00
|
|
|
applyValidationRslt(rule.name == TerrainViewPattern::RULE_ANY || sandTestOk || nativeTestOk);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(topPoints == -1)
|
|
|
|
{
|
|
|
|
return ValidationResult(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalPoints += topPoints;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 19:09:48 +03:00
|
|
|
if(totalPoints >= pattern.minPoints && totalPoints <= pattern.maxPoints)
|
|
|
|
{
|
|
|
|
return ValidationResult(true, transitionReplacement);
|
|
|
|
}
|
|
|
|
else
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
return ValidationResult(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
bool CDrawTerrainOperation::isSandType(ETerrainType terType) const
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
switch(terType)
|
|
|
|
{
|
|
|
|
case ETerrainType::WATER:
|
|
|
|
case ETerrainType::SAND:
|
|
|
|
case ETerrainType::ROCK:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 21:36:52 +03:00
|
|
|
void CDrawTerrainOperation::flipPattern(TerrainViewPattern & pattern, int flip) const
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2014-07-24 21:36:52 +03:00
|
|
|
//flip in place to avoid expensive constructor. Seriously.
|
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
if(flip == 0)
|
|
|
|
{
|
2014-07-24 21:36:52 +03:00
|
|
|
return;
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
2014-07-24 20:16:49 +03:00
|
|
|
//always flip horizontal
|
|
|
|
for(int i = 0; i < 3; ++i)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2014-07-24 20:16:49 +03:00
|
|
|
int y = i * 3;
|
|
|
|
std::swap(pattern.data[y], pattern.data[y + 2]);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
2014-07-24 20:16:49 +03:00
|
|
|
//flip vertical only at 2nd step
|
|
|
|
if(flip == FLIP_PATTERN_VERTICAL)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
for(int i = 0; i < 3; ++i)
|
|
|
|
{
|
2014-07-24 20:16:49 +03:00
|
|
|
std::swap(pattern.data[i], pattern.data[6 + i]);
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
void CDrawTerrainOperation::invalidateTerrainViews(const int3 & centerPos)
|
|
|
|
{
|
|
|
|
auto rect = extendTileAroundSafely(centerPos);
|
|
|
|
rect.forEach([&](const int3 & pos)
|
|
|
|
{
|
|
|
|
invalidatedTerViews.insert(pos);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CDrawTerrainOperation::InvalidTiles CDrawTerrainOperation::getInvalidTiles(const int3 & centerPos) const
|
|
|
|
{
|
|
|
|
InvalidTiles tiles;
|
|
|
|
auto centerTerType = map->getTile(centerPos).terType;
|
|
|
|
auto rect = extendTileAround(centerPos);
|
|
|
|
rect.forEach([&](const int3 & pos)
|
|
|
|
{
|
|
|
|
if(map->isInTheMap(pos))
|
|
|
|
{
|
2013-08-17 15:46:48 +03:00
|
|
|
auto ptrConfig = VLC->terviewh;
|
2013-04-29 18:51:39 +03:00
|
|
|
auto terType = map->getTile(pos).terType;
|
2013-08-17 15:46:48 +03:00
|
|
|
auto valid = validateTerrainView(pos, ptrConfig->getTerrainTypePatternById("n1")).result;
|
2013-05-03 13:15:59 +03:00
|
|
|
|
|
|
|
// Special validity check for rock & water
|
|
|
|
if(valid && centerTerType != terType && (terType == ETerrainType::WATER || terType == ETerrainType::ROCK))
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2013-05-03 13:15:59 +03:00
|
|
|
static const std::string patternIds[] = { "s1", "s2" };
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & patternId : patternIds)
|
2013-04-29 18:51:39 +03:00
|
|
|
{
|
2013-08-17 15:46:48 +03:00
|
|
|
valid = !validateTerrainView(pos, ptrConfig->getTerrainTypePatternById(patternId)).result;
|
2013-05-03 13:15:59 +03:00
|
|
|
if(!valid) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Additional validity check for non rock OR water
|
|
|
|
else if(!valid && (terType != ETerrainType::WATER && terType != ETerrainType::ROCK))
|
|
|
|
{
|
|
|
|
static const std::string patternIds[] = { "n2", "n3" };
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto & patternId : patternIds)
|
2013-05-03 13:15:59 +03:00
|
|
|
{
|
2013-08-17 15:46:48 +03:00
|
|
|
valid = validateTerrainView(pos, ptrConfig->getTerrainTypePatternById(patternId)).result;
|
2013-05-03 13:15:59 +03:00
|
|
|
if(valid) break;
|
|
|
|
}
|
2013-04-29 18:51:39 +03:00
|
|
|
}
|
2013-05-03 13:15:59 +03:00
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
if(!valid)
|
|
|
|
{
|
|
|
|
if(terType == centerTerType) tiles.nativeTiles.insert(pos);
|
|
|
|
else tiles.foreignTiles.insert(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return tiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapRect CDrawTerrainOperation::extendTileAround(const int3 & centerPos) const
|
|
|
|
{
|
|
|
|
return MapRect(int3(centerPos.x - 1, centerPos.y - 1, centerPos.z), 3, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
MapRect CDrawTerrainOperation::extendTileAroundSafely(const int3 & centerPos) const
|
|
|
|
{
|
|
|
|
return extendTileAround(centerPos) & MapRect(int3(0, 0, centerPos.z), map->width, map->height);
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
CDrawTerrainOperation::ValidationResult::ValidationResult(bool result, const std::string & transitionReplacement /*= ""*/)
|
2013-01-26 16:48:20 +03:00
|
|
|
: result(result), transitionReplacement(transitionReplacement)
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-04-16 16:16:58 +03:00
|
|
|
|
2013-04-29 18:51:39 +03:00
|
|
|
CClearTerrainOperation::CClearTerrainOperation(CMap * map, CRandomGenerator * gen) : CComposedOperation(map)
|
|
|
|
{
|
|
|
|
CTerrainSelection terrainSel(map);
|
|
|
|
terrainSel.selectRange(MapRect(int3(0, 0, 0), map->width, map->height));
|
|
|
|
addOperation(make_unique<CDrawTerrainOperation>(map, terrainSel, ETerrainType::WATER, gen));
|
2013-05-21 22:08:06 +03:00
|
|
|
if(map->twoLevel)
|
|
|
|
{
|
|
|
|
terrainSel.clearSelection();
|
|
|
|
terrainSel.selectRange(MapRect(int3(0, 0, 1), map->width, map->height));
|
|
|
|
addOperation(make_unique<CDrawTerrainOperation>(map, terrainSel, ETerrainType::ROCK, gen));
|
|
|
|
}
|
2013-04-29 18:51:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CClearTerrainOperation::getLabel() const
|
|
|
|
{
|
|
|
|
return "Clear Terrain";
|
|
|
|
}
|
|
|
|
|
|
|
|
CInsertObjectOperation::CInsertObjectOperation(CMap * map, CGObjectInstance * obj, const int3 & pos)
|
2013-04-19 14:43:11 +03:00
|
|
|
: CMapOperation(map), pos(pos), obj(obj)
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
void CInsertObjectOperation::execute()
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
obj->pos = pos;
|
|
|
|
obj->id = ObjectInstanceID(map->objects.size());
|
|
|
|
map->objects.push_back(obj);
|
|
|
|
if(obj->ID == Obj::TOWN)
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
map->towns.push_back(static_cast<CGTownInstance *>(obj));
|
|
|
|
}
|
|
|
|
if(obj->ID == Obj::HERO)
|
|
|
|
{
|
2013-05-19 01:30:48 +03:00
|
|
|
map->heroesOnMap.push_back(static_cast<CGHeroInstance*>(obj));
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
2013-04-19 14:43:11 +03:00
|
|
|
map->addBlockVisTiles(obj);
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
void CInsertObjectOperation::undo()
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
//TODO
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
void CInsertObjectOperation::redo()
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
execute();
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|
|
|
|
|
2013-04-22 18:08:04 +03:00
|
|
|
std::string CInsertObjectOperation::getLabel() const
|
2013-04-16 16:16:58 +03:00
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
return "Insert Object";
|
2013-04-16 16:16:58 +03:00
|
|
|
}
|