| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685 | 
							- /*
 
-  * CMapOperation.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "CMapOperation.h"
 
- #include "../VCMI_Lib.h"
 
- #include "../TerrainHandler.h"
 
- #include "CMap.h"
 
- #include "MapEditUtils.h"
 
- VCMI_LIB_NAMESPACE_BEGIN
 
- CMapOperation::CMapOperation(CMap* map) : map(map)
 
- {
 
- }
 
- std::string CMapOperation::getLabel() const
 
- {
 
- 	return "";
 
- }
 
- MapRect CMapOperation::extendTileAround(const int3 & centerPos) const
 
- {
 
- 	return MapRect(int3(centerPos.x - 1, centerPos.y - 1, centerPos.z), 3, 3);
 
- }
 
- MapRect CMapOperation::extendTileAroundSafely(const int3& centerPos) const
 
- {
 
- 	return extendTileAround(centerPos) & MapRect(int3(0, 0, centerPos.z), map->width, map->height);
 
- }
 
- CComposedOperation::CComposedOperation(CMap* map) : CMapOperation(map)
 
- {
 
- }
 
- void CComposedOperation::execute()
 
- {
 
- 	for(auto & operation : operations)
 
- 	{
 
- 		operation->execute();
 
- 	}
 
- }
 
- void CComposedOperation::undo()
 
- {
 
- 	//reverse order
 
- 	for(auto operation = operations.rbegin(); operation != operations.rend(); operation++)
 
- 	{
 
- 		operation->get()->undo();
 
- 	}
 
- }
 
- void CComposedOperation::redo()
 
- {
 
- 	for(auto & operation : operations)
 
- 	{
 
- 		operation->redo();
 
- 	}
 
- }
 
- std::string CComposedOperation::getLabel() const
 
- {
 
- 	std::string ret = "Composed operation: ";
 
- 	for(const auto & operation : operations)
 
- 	{
 
- 		ret.append(operation->getLabel() + ";");
 
- 	}
 
- 	return ret;
 
- }
 
- void CComposedOperation::addOperation(std::unique_ptr<CMapOperation>&& operation)
 
- {
 
- 	operations.push_back(std::move(operation));
 
- }
 
- CDrawTerrainOperation::CDrawTerrainOperation(CMap * map, CTerrainSelection terrainSel, TerrainId terType, CRandomGenerator * gen):
 
- 	CMapOperation(map),
 
- 	terrainSel(std::move(terrainSel)),
 
- 	terType(terType),
 
- 	gen(gen)
 
- {
 
- }
 
- void CDrawTerrainOperation::execute()
 
- {
 
- 	for(const auto & pos : terrainSel.getSelectedItems())
 
- 	{
 
- 		auto & tile = map->getTile(pos);
 
- 		tile.terType = const_cast<TerrainType*>(VLC->terrainTypeHandler->getById(terType));
 
- 		invalidateTerrainViews(pos);
 
- 	}
 
- 	updateTerrainTypes();
 
- 	updateTerrainViews();
 
- }
 
- void CDrawTerrainOperation::undo()
 
- {
 
- 	//TODO
 
- }
 
- void CDrawTerrainOperation::redo()
 
- {
 
- 	//TODO
 
- }
 
- std::string CDrawTerrainOperation::getLabel() const
 
- {
 
- 	return "Draw Terrain";
 
- }
 
- void CDrawTerrainOperation::updateTerrainTypes()
 
- {
 
- 	auto positions = terrainSel.getSelectedItems();
 
- 	while(!positions.empty())
 
- 	{
 
- 		const auto & centerPos = *(positions.begin());
 
- 		auto centerTile = map->getTile(centerPos);
 
- 		//logGlobal->debug("Set terrain tile at pos '%s' to type '%s'", centerPos, centerTile.terType);
 
- 		auto tiles = getInvalidTiles(centerPos);
 
- 		auto updateTerrainType = [&](const int3& pos)
 
- 		{
 
- 			map->getTile(pos).terType = centerTile.terType;
 
- 			positions.insert(pos);
 
- 			invalidateTerrainViews(pos);
 
- 			//logGlobal->debug("Set additional terrain tile at pos '%s' to type '%s'", pos, centerTile.terType);
 
- 		};
 
- 		// Fill foreign invalid tiles
 
- 		for(const auto & tile : tiles.foreignTiles)
 
- 		{
 
- 			updateTerrainType(tile);
 
- 		}
 
- 		tiles = getInvalidTiles(centerPos);
 
- 		if(tiles.nativeTiles.find(centerPos) != tiles.nativeTiles.end())
 
- 		{
 
- 			// Blow up
 
- 			auto rect = extendTileAroundSafely(centerPos);
 
- 			std::set<int3> suitableTiles;
 
- 			int invalidForeignTilesCnt = std::numeric_limits<int>::max();
 
- 			int invalidNativeTilesCnt = 0;
 
- 			bool centerPosValid = false;
 
- 			rect.forEach([&](const int3& posToTest)
 
- 				{
 
- 					auto & terrainTile = map->getTile(posToTest);
 
- 					if(centerTile.terType->getId() != terrainTile.terType->getId())
 
- 					{
 
- 						const auto * formerTerType = terrainTile.terType;
 
- 						terrainTile.terType = centerTile.terType;
 
- 						auto testTile = getInvalidTiles(posToTest);
 
- 						int nativeTilesCntNorm = testTile.nativeTiles.empty() ? std::numeric_limits<int>::max() : static_cast<int>(testTile.nativeTiles.size());
 
- 						bool putSuitableTile = false;
 
- 						bool addToSuitableTiles = false;
 
- 						if(testTile.centerPosValid)
 
- 						{
 
- 							if(!centerPosValid)
 
- 							{
 
- 								centerPosValid = true;
 
- 								putSuitableTile = true;
 
- 							}
 
- 							else
 
- 							{
 
- 								if(testTile.foreignTiles.size() < invalidForeignTilesCnt)
 
- 								{
 
- 									putSuitableTile = true;
 
- 								}
 
- 								else
 
- 								{
 
- 									addToSuitableTiles = true;
 
- 								}
 
- 							}
 
- 						}
 
- 						else if(!centerPosValid)
 
- 						{
 
- 							if((nativeTilesCntNorm > invalidNativeTilesCnt) ||
 
- 								(nativeTilesCntNorm == invalidNativeTilesCnt && testTile.foreignTiles.size() < invalidForeignTilesCnt))
 
- 							{
 
- 								putSuitableTile = true;
 
- 							}
 
- 							else if(nativeTilesCntNorm == invalidNativeTilesCnt && testTile.foreignTiles.size() == invalidForeignTilesCnt)
 
- 							{
 
- 								addToSuitableTiles = true;
 
- 							}
 
- 						}
 
- 						if(putSuitableTile)
 
- 						{
 
- 							//if(!suitableTiles.empty())
 
- 							//{
 
- 							//	logGlobal->debug("Clear suitables tiles.");
 
- 							//}
 
- 							invalidNativeTilesCnt = nativeTilesCntNorm;
 
- 							invalidForeignTilesCnt = static_cast<int>(testTile.foreignTiles.size());
 
- 							suitableTiles.clear();
 
- 							addToSuitableTiles = true;
 
- 						}
 
- 						if(addToSuitableTiles)
 
- 						{
 
- 							suitableTiles.insert(posToTest);
 
- 						}
 
- 						terrainTile.terType = formerTerType;
 
- 					}
 
- 				});
 
- 			if(suitableTiles.size() == 1)
 
- 			{
 
- 				updateTerrainType(*suitableTiles.begin());
 
- 			}
 
- 			else
 
- 			{
 
- 				static const int3 directions[] = { int3(0, -1, 0), int3(-1, 0, 0), int3(0, 1, 0), int3(1, 0, 0),
 
- 											int3(-1, -1, 0), int3(-1, 1, 0), int3(1, 1, 0), int3(1, -1, 0) };
 
- 				for(const auto & direction : directions)
 
- 				{
 
- 					auto it = suitableTiles.find(centerPos + direction);
 
- 					if (it != suitableTiles.end())
 
- 					{
 
- 						updateTerrainType(*it);
 
- 						break;
 
- 					}
 
- 				}
 
- 			}
 
- 		}
 
- 		else
 
- 		{
 
- 			// add invalid native tiles which are not in the positions list
 
- 			for(const auto & nativeTile : tiles.nativeTiles)
 
- 			{
 
- 				if (positions.find(nativeTile) == positions.end())
 
- 				{
 
- 					positions.insert(nativeTile);
 
- 				}
 
- 			}
 
- 			positions.erase(centerPos);
 
- 		}
 
- 	}
 
- }
 
- void CDrawTerrainOperation::updateTerrainViews()
 
- {
 
- 	for(const auto & pos : invalidatedTerViews)
 
- 	{
 
- 		const auto & patterns = VLC->terviewh->getTerrainViewPatterns(map->getTile(pos).terType->getId());
 
- 		// 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];
 
- 			//(ETerrainGroup::ETerrainGroup terGroup, const std::string & id)
 
- 			valRslt = validateTerrainView(pos, &pattern);
 
- 			if (valRslt.result)
 
- 			{
 
- 				bestPattern = k;
 
- 				break;
 
- 			}
 
- 		}
 
- 		//assert(bestPattern != -1);
 
- 		if(bestPattern == -1)
 
- 		{
 
- 			// This shouldn't be the case
 
- 			logGlobal->warn("No pattern detected at pos '%s'.", pos.toString());
 
- 			CTerrainViewPatternUtils::printDebuggingInfoAboutTile(map, pos);
 
- 			continue;
 
- 		}
 
- 		// Get mapping
 
- 		const TerrainViewPattern& pattern = patterns[bestPattern][valRslt.flip];
 
- 		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)
 
- 		{
 
- 			tile.terView = gen->nextInt(mapping.first, mapping.second);
 
- 			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;
 
- 			tile.terView = gen->nextInt(firstFrame, firstFrame + framesPerRot - 1);
 
- 			tile.extTileFlags = 0;
 
- 		}
 
- 	}
 
- }
 
- CDrawTerrainOperation::ValidationResult CDrawTerrainOperation::validateTerrainView(const int3& pos, const std::vector<TerrainViewPattern>* pattern, int recDepth) const
 
- {
 
- 	for(int flip = 0; flip < 4; ++flip)
 
- 	{
 
- 		auto valRslt = validateTerrainViewInner(pos, pattern->at(flip), recDepth);
 
- 		if(valRslt.result)
 
- 		{
 
- 			valRslt.flip = flip;
 
- 			return valRslt;
 
- 		}
 
- 	}
 
- 	return ValidationResult(false);
 
- }
 
- CDrawTerrainOperation::ValidationResult CDrawTerrainOperation::validateTerrainViewInner(const int3& pos, const TerrainViewPattern& pattern, int recDepth) const
 
- {
 
- 	const auto * centerTerType = map->getTile(pos).terType;
 
- 	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
 
- 		int cx = pos.x + (i % 3) - 1;
 
- 		int cy = pos.y + (i / 3) - 1;
 
- 		int3 currentPos(cx, cy, pos.z);
 
- 		bool isAlien = false;
 
- 		const TerrainType * terType = nullptr;
 
- 		if(!map->isInTheMap(currentPos))
 
- 		{
 
- 			// position is not in the map, so take the ter type from the neighbor tile
 
- 			bool widthTooHigh = currentPos.x >= map->width;
 
- 			bool widthTooLess = currentPos.x < 0;
 
- 			bool heightTooHigh = currentPos.y >= map->height;
 
- 			bool heightTooLess = currentPos.y < 0;
 
- 			if((widthTooHigh && heightTooHigh) || (widthTooHigh && heightTooLess) || (widthTooLess && heightTooHigh) || (widthTooLess && heightTooLess))
 
- 			{
 
- 				terType = centerTerType;
 
- 			}
 
- 			else if(widthTooHigh)
 
- 			{
 
- 				terType = map->getTile(int3(currentPos.x - 1, currentPos.y, currentPos.z)).terType;
 
- 			}
 
- 			else if(heightTooHigh)
 
- 			{
 
- 				terType = map->getTile(int3(currentPos.x, currentPos.y - 1, currentPos.z)).terType;
 
- 			}
 
- 			else if(widthTooLess)
 
- 			{
 
- 				terType = map->getTile(int3(currentPos.x + 1, currentPos.y, currentPos.z)).terType;
 
- 			}
 
- 			else if(heightTooLess)
 
- 			{
 
- 				terType = map->getTile(int3(currentPos.x, currentPos.y + 1, currentPos.z)).terType;
 
- 			}
 
- 		}
 
- 		else
 
- 		{
 
- 			terType = map->getTile(currentPos).terType;
 
- 			if(terType != centerTerType && (terType->isPassable() || centerTerType->isPassable()))
 
- 			{
 
- 				isAlien = true;
 
- 			}
 
- 		}
 
- 		// Validate all rules per cell
 
- 		int topPoints = -1;
 
- 		for(const auto & elem : pattern.data[i])
 
- 		{
 
- 			TerrainViewPattern::WeightedRule rule = elem;
 
- 			if(!rule.isStandardRule())
 
- 			{
 
- 				if(recDepth == 0 && map->isInTheMap(currentPos))
 
- 				{
 
- 					if(terType->getId() == centerTerType->getId())
 
- 					{
 
- 						const auto patternForRule = VLC->terviewh->getTerrainViewPatternsById(centerTerType->getId(), rule.name);
 
- 						if(auto p = patternForRule)
 
- 						{
 
- 							auto rslt = validateTerrainView(currentPos, &(p->get()), 1);
 
- 							if(rslt.result) topPoints = std::max(topPoints, rule.points);
 
- 						}
 
- 					}
 
- 					continue;
 
- 				}
 
- 				else
 
- 				{
 
- 					rule.setNative();
 
- 				}
 
- 			}
 
- 			auto applyValidationRslt = [&](bool rslt)
 
- 			{
 
- 				if(rslt)
 
- 				{
 
- 					topPoints = std::max(topPoints, rule.points);
 
- 				}
 
- 			};
 
- 			// Validate cell with the ruleset of the pattern
 
- 			bool nativeTestOk = false;
 
- 			bool nativeTestStrongOk = false;
 
- 			nativeTestOk = nativeTestStrongOk = (rule.isNativeStrong() || rule.isNativeRule()) && !isAlien;
 
- 			if(centerTerType->getId() == ETerrainId::DIRT)
 
- 			{
 
- 				nativeTestOk = rule.isNativeRule() && !terType->isTransitionRequired();
 
- 				bool sandTestOk = (rule.isSandRule() || rule.isTransition())
 
- 					&& terType->isTransitionRequired();
 
- 				applyValidationRslt(rule.isAnyRule() || sandTestOk || nativeTestOk || nativeTestStrongOk);
 
- 			}
 
- 			else if(centerTerType->getId() == ETerrainId::SAND)
 
- 			{
 
- 				applyValidationRslt(true);
 
- 			}
 
- 			else if(centerTerType->isTransitionRequired()) //water, rock and some special terrains require sand transition
 
- 			{
 
- 				bool sandTestOk = (rule.isSandRule() || rule.isTransition())
 
- 					&& isAlien;
 
- 				applyValidationRslt(rule.isAnyRule() || sandTestOk || nativeTestOk);
 
- 			}
 
- 			else
 
- 			{
 
- 				bool dirtTestOk = (rule.isDirtRule() || rule.isTransition())
 
- 					&& isAlien && !terType->isTransitionRequired();
 
- 				bool sandTestOk = (rule.isSandRule() || rule.isTransition())
 
- 					&& terType->isTransitionRequired();
 
- 				if(transitionReplacement.empty() && rule.isTransition()
 
- 					&& (dirtTestOk || sandTestOk))
 
- 				{
 
- 					transitionReplacement = dirtTestOk ? TerrainViewPattern::RULE_DIRT : TerrainViewPattern::RULE_SAND;
 
- 				}
 
- 				if(rule.isTransition())
 
- 				{
 
- 					applyValidationRslt((dirtTestOk && transitionReplacement != TerrainViewPattern::RULE_SAND) ||
 
- 						(sandTestOk && transitionReplacement != TerrainViewPattern::RULE_DIRT));
 
- 				}
 
- 				else
 
- 				{
 
- 					applyValidationRslt(rule.isAnyRule() || dirtTestOk || sandTestOk || nativeTestOk);
 
- 				}
 
- 			}
 
- 		}
 
- 		if(topPoints == -1)
 
- 		{
 
- 			return ValidationResult(false);
 
- 		}
 
- 		else
 
- 		{
 
- 			totalPoints += topPoints;
 
- 		}
 
- 	}
 
- 	if(totalPoints >= pattern.minPoints && totalPoints <= pattern.maxPoints)
 
- 	{
 
- 		return ValidationResult(true, transitionReplacement);
 
- 	}
 
- 	else
 
- 	{
 
- 		return ValidationResult(false);
 
- 	}
 
- }
 
- 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
 
- {
 
- 	//TODO: this is very expensive function for RMG, needs optimization
 
- 	InvalidTiles tiles;
 
- 	const auto * centerTerType = map->getTile(centerPos).terType;
 
- 	auto rect = extendTileAround(centerPos);
 
- 	rect.forEach([&](const int3& pos)
 
- 		{
 
- 			if(map->isInTheMap(pos))
 
- 			{
 
- 				auto * ptrConfig = VLC->terviewh;
 
- 				const auto * terType = map->getTile(pos).terType;
 
- 				auto valid = validateTerrainView(pos, ptrConfig->getTerrainTypePatternById("n1")).result;
 
- 				// Special validity check for rock & water
 
- 				if(valid && (terType->isWater() || !terType->isPassable()))
 
- 				{
 
- 					static const std::string patternIds[] = { "s1", "s2" };
 
- 					for(const auto & patternId : patternIds)
 
- 					{
 
- 						valid = !validateTerrainView(pos, ptrConfig->getTerrainTypePatternById(patternId)).result;
 
- 						if(!valid) break;
 
- 					}
 
- 				}
 
- 				// Additional validity check for non rock OR water
 
- 				else if(!valid && (terType->isLand() && terType->isPassable()))
 
- 				{
 
- 					static const std::string patternIds[] = { "n2", "n3" };
 
- 					for(const auto & patternId : patternIds)
 
- 					{
 
- 						valid = validateTerrainView(pos, ptrConfig->getTerrainTypePatternById(patternId)).result;
 
- 						if(valid) break;
 
- 					}
 
- 				}
 
- 				if(!valid)
 
- 				{
 
- 					if(terType == centerTerType) tiles.nativeTiles.insert(pos);
 
- 					else tiles.foreignTiles.insert(pos);
 
- 				}
 
- 				else if(centerPos == pos)
 
- 				{
 
- 					tiles.centerPosValid = true;
 
- 				}
 
- 			}
 
- 		});
 
- 	return tiles;
 
- }
 
- CDrawTerrainOperation::ValidationResult::ValidationResult(bool result, std::string transitionReplacement)
 
- 	: result(result)
 
- 	, transitionReplacement(std::move(transitionReplacement))
 
- 	, flip(0)
 
- {
 
- }
 
- CClearTerrainOperation::CClearTerrainOperation(CMap* map, CRandomGenerator* gen) : CComposedOperation(map)
 
- {
 
- 	CTerrainSelection terrainSel(map);
 
- 	terrainSel.selectRange(MapRect(int3(0, 0, 0), map->width, map->height));
 
- 	addOperation(std::make_unique<CDrawTerrainOperation>(map, terrainSel, ETerrainId::WATER, gen));
 
- 	if(map->twoLevel)
 
- 	{
 
- 		terrainSel.clearSelection();
 
- 		terrainSel.selectRange(MapRect(int3(0, 0, 1), map->width, map->height));
 
- 		addOperation(std::make_unique<CDrawTerrainOperation>(map, terrainSel, ETerrainId::ROCK, gen));
 
- 	}
 
- }
 
- std::string CClearTerrainOperation::getLabel() const
 
- {
 
- 	return "Clear Terrain";
 
- }
 
- CInsertObjectOperation::CInsertObjectOperation(CMap* map, CGObjectInstance* obj)
 
- 	: CMapOperation(map), obj(obj)
 
- {
 
- }
 
- void CInsertObjectOperation::execute()
 
- {
 
- 	obj->id = ObjectInstanceID(map->objects.size());
 
- 	do
 
- 	{
 
- 		map->setUniqueInstanceName(obj);
 
- 	} while(vstd::contains(map->instanceNames, obj->instanceName));
 
- 	map->addNewObject(obj);
 
- }
 
- void CInsertObjectOperation::undo()
 
- {
 
- 	map->removeObject(obj);
 
- }
 
- void CInsertObjectOperation::redo()
 
- {
 
- 	execute();
 
- }
 
- std::string CInsertObjectOperation::getLabel() const
 
- {
 
- 	return "Insert Object";
 
- }
 
- CMoveObjectOperation::CMoveObjectOperation(CMap* map, CGObjectInstance* obj, const int3& targetPosition)
 
- 	: CMapOperation(map),
 
- 	obj(obj),
 
- 	initialPos(obj->pos),
 
- 	targetPos(targetPosition)
 
- {
 
- }
 
- void CMoveObjectOperation::execute()
 
- {
 
- 	map->moveObject(obj, targetPos);
 
- }
 
- void CMoveObjectOperation::undo()
 
- {
 
- 	map->moveObject(obj, initialPos);
 
- }
 
- void CMoveObjectOperation::redo()
 
- {
 
- 	execute();
 
- }
 
- std::string CMoveObjectOperation::getLabel() const
 
- {
 
- 	return "Move Object";
 
- }
 
- CRemoveObjectOperation::CRemoveObjectOperation(CMap* map, CGObjectInstance* obj)
 
- 	: CMapOperation(map), obj(obj)
 
- {
 
- }
 
- CRemoveObjectOperation::~CRemoveObjectOperation()
 
- {
 
- 	//when operation is destroyed and wasn't undone, the object is lost forever
 
- 	if(!obj)
 
- 	{
 
- 		return;
 
- 	}
 
- 	//do not destroy an object that belongs to map
 
- 	if(!vstd::contains(map->instanceNames, obj->instanceName))
 
- 	{
 
- 		delete obj;
 
- 		obj = nullptr;
 
- 	}
 
- }
 
- void CRemoveObjectOperation::execute()
 
- {
 
- 	map->removeObject(obj);
 
- }
 
- void CRemoveObjectOperation::undo()
 
- {
 
- 	try
 
- 	{
 
- 		//set new id, but do not rename object
 
- 		obj->id = ObjectInstanceID(static_cast<si32>(map->objects.size()));
 
- 		map->addNewObject(obj);
 
- 	}
 
- 	catch(const std::exception& e)
 
- 	{
 
- 		logGlobal->error(e.what());
 
- 	}
 
- }
 
- void CRemoveObjectOperation::redo()
 
- {
 
- 	execute();
 
- }
 
- std::string CRemoveObjectOperation::getLabel() const
 
- {
 
- 	return "Remove Object";
 
- }
 
- VCMI_LIB_NAMESPACE_END
 
 
  |