1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Reindex objects to show interactive objects on top

This commit is contained in:
Tomasz Zieliński 2024-04-03 14:48:04 +02:00
parent c2f160326c
commit 2a213f2276
4 changed files with 49 additions and 0 deletions

View File

@ -571,13 +571,21 @@ void CMap::removeObject(CGObjectInstance * obj)
removeBlockVisTiles(obj);
instanceNames.erase(obj->instanceName);
// FIXME: This is inefficient when bacth-deleting objects, as objects are updated one by one.
//update indeces
/*
auto iter = std::next(objects.begin(), obj->id.getNum());
iter = objects.erase(iter);
for(int i = obj->id.getNum(); iter != objects.end(); ++i, ++iter)
{
(*iter)->id = ObjectInstanceID(i);
}
*/
// TODO: Check if it doesn't break here
reindexObjects();
obj->afterRemoveFromMap(this);
@ -704,4 +712,40 @@ void CMap::resolveQuestIdentifiers()
questIdentifierToId.clear();
}
void CMap::reindexObjects()
{
// TODO: Call at map save?
// Only reindex at editor / RMG operations
std::sort(objects.begin(), objects.end(), [](const CGObjectInstance * lhs, const CGObjectInstance * rhs)
{
// Obstacles first, then visitable, at the end - removable
if (!lhs->isVisitable() && rhs->isVisitable())
return true;
if (lhs->isVisitable() && !rhs->isVisitable())
return false;
// Special case for Windomill - draw on top of other objects
if (lhs->ID != Obj::WINDMILL && rhs->ID == Obj::WINDMILL)
return true;
if (lhs->ID == Obj::WINDMILL && rhs->ID != Obj::WINDMILL)
return false;
if (!lhs->isRemovable() && rhs->isRemovable())
return true;
if (lhs->isRemovable() && !rhs->isRemovable())
return false;
return lhs->pos.y < rhs->pos.y;
});
// instanceNames don't change
for (size_t i = 0; i < objects.size(); ++i)
{
objects[i]->id = ObjectInstanceID(i);
}
}
VCMI_LIB_NAMESPACE_END

View File

@ -128,6 +128,8 @@ public:
void resetStaticData();
void resolveQuestIdentifiers();
void reindexObjects();
ui32 checksum;
std::vector<Rumor> rumors;
std::vector<DisposedHero> disposedHeroes;

View File

@ -47,6 +47,8 @@ CComposedOperation::CComposedOperation(CMap* map) : CMapOperation(map)
void CComposedOperation::execute()
{
// FIXME: Only reindex objects at the end of composite operation
for(auto & operation : operations)
{
operation->execute();

View File

@ -89,6 +89,7 @@ void CMapService::saveMap(const std::unique_ptr<CMap> & map, boost::filesystem::
tmp.flush();
tmp.close();
}
map->reindexObjects();
}
ModCompatibilityInfo CMapService::verifyMapHeaderMods(const CMapHeader & map)