1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-17 00:07:41 +02:00

Draw random artifact as an atomic operation

This commit is contained in:
Tomasz Zieliński
2024-06-24 20:06:50 +02:00
parent c328007687
commit d7a4ea9f32
3 changed files with 26 additions and 17 deletions

View File

@ -38,6 +38,7 @@ void CMapUndoManager::redo()
void CMapUndoManager::clearAll() void CMapUndoManager::clearAll()
{ {
//FIXME: Will crash if an object was added twice to actions
undoStack.clear(); undoStack.clear();
redoStack.clear(); redoStack.clear();
onUndoRedo(); onUndoRedo();

View File

@ -64,6 +64,22 @@ std::vector<CGObjectInstance*> QuestArtifactPlacer::getPossibleArtifactsToReplac
return artifactsToReplace; return artifactsToReplace;
} }
CGObjectInstance * QuestArtifactPlacer::drawObjectToReplace()
{
RecursiveLock lock(externalAccessMutex);
if (artifactsToReplace.empty())
{
return nullptr;
}
else
{
auto ret = *RandomGeneratorUtil::nextItem(artifactsToReplace, zone.getRand());
vstd::erase_if_present(artifactsToReplace, ret);
return ret;
}
}
void QuestArtifactPlacer::findZonesForQuestArts() void QuestArtifactPlacer::findZonesForQuestArts()
{ {
const auto& distances = generator.getZonePlacer()->getDistanceMap().at(zone.getId()); const auto& distances = generator.getZonePlacer()->getDistanceMap().at(zone.getId());
@ -87,14 +103,14 @@ void QuestArtifactPlacer::placeQuestArtifacts(CRandomGenerator & rand)
for (auto zone : questArtZones) for (auto zone : questArtZones)
{ {
auto* qap = zone->getModificator<QuestArtifactPlacer>(); auto* qap = zone->getModificator<QuestArtifactPlacer>();
std::vector<CGObjectInstance *> artifactsToReplace = qap->getPossibleArtifactsToReplace();
if (artifactsToReplace.empty()) auto objectToReplace = qap->drawObjectToReplace();
if (!objectToReplace)
continue; continue;
auto artifactToReplace = *RandomGeneratorUtil::nextItem(artifactsToReplace, rand);
logGlobal->trace("Replacing %s at %s with the quest artifact %s", logGlobal->trace("Replacing %s at %s with the quest artifact %s",
artifactToReplace->getObjectName(), objectToReplace->getObjectName(),
artifactToReplace->getPosition().toString(), objectToReplace->getPosition().toString(),
VLC->artifacts()->getById(artifactToPlace)->getNameTranslated()); VLC->artifacts()->getById(artifactToPlace)->getNameTranslated());
//Update appearance. Terrain is irrelevant. //Update appearance. Terrain is irrelevant.
@ -103,24 +119,15 @@ void QuestArtifactPlacer::placeQuestArtifacts(CRandomGenerator & rand)
auto templates = handler->getTemplates(); auto templates = handler->getTemplates();
//artifactToReplace->appearance = templates.front(); //artifactToReplace->appearance = templates.front();
newObj->appearance = templates.front(); newObj->appearance = templates.front();
newObj->pos = artifactToReplace->pos; newObj->pos = objectToReplace->pos;
mapProxy->insertObject(newObj); mapProxy->insertObject(newObj);
mapProxy->removeObject(objectToReplace);
for (auto z : map.getZones())
{
//Every qap has its OWN collection of artifacts
auto * localQap = zone->getModificator<QuestArtifactPlacer>();
if (localQap)
{
localQap->dropReplacedArtifact(artifactToReplace);
}
}
mapProxy->removeObject(artifactToReplace);
break; break;
} }
} }
} }
// TODO: Unused?
void QuestArtifactPlacer::dropReplacedArtifact(CGObjectInstance* obj) void QuestArtifactPlacer::dropReplacedArtifact(CGObjectInstance* obj)
{ {
RecursiveLock lock(externalAccessMutex); RecursiveLock lock(externalAccessMutex);

View File

@ -31,6 +31,7 @@ public:
void addQuestArtifact(const ArtifactID& id); void addQuestArtifact(const ArtifactID& id);
void removeQuestArtifact(const ArtifactID& id); void removeQuestArtifact(const ArtifactID& id);
void rememberPotentialArtifactToReplace(CGObjectInstance* obj); void rememberPotentialArtifactToReplace(CGObjectInstance* obj);
CGObjectInstance * drawObjectToReplace();
std::vector<CGObjectInstance*> getPossibleArtifactsToReplace() const; std::vector<CGObjectInstance*> getPossibleArtifactsToReplace() const;
void placeQuestArtifacts(CRandomGenerator & rand); void placeQuestArtifacts(CRandomGenerator & rand);
void dropReplacedArtifact(CGObjectInstance* obj); void dropReplacedArtifact(CGObjectInstance* obj);