1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00

Some preliminary support for object erase. But this will need a major refactor to split basic map data from in-game data.

This commit is contained in:
Tomasz Zieliński 2022-09-06 17:16:34 +02:00
parent b817e6509b
commit 6165bac516
9 changed files with 39 additions and 10 deletions

View File

@ -1403,6 +1403,11 @@ void CGHeroInstance::afterAddToMap(CMap * map)
if(ID == Obj::HERO) if(ID == Obj::HERO)
map->heroesOnMap.push_back(this); map->heroesOnMap.push_back(this);
} }
void CGHeroInstance::afterRemoveFromMap(CMap* map)
{
if (ID == Obj::HERO)
vstd::erase_if_present(map->heroesOnMap, this);
}
void CGHeroInstance::setHeroTypeName(const std::string & identifier) void CGHeroInstance::setHeroTypeName(const std::string & identifier)
{ {

View File

@ -268,6 +268,7 @@ public:
std::string getObjectName() const override; std::string getObjectName() const override;
void afterAddToMap(CMap * map) override; void afterAddToMap(CMap * map) override;
void afterRemoveFromMap(CMap* map) override;
void updateFrom(const JsonNode & data) override; void updateFrom(const JsonNode & data) override;

View File

@ -1441,6 +1441,12 @@ void CGTownInstance::afterAddToMap(CMap * map)
map->towns.push_back(this); map->towns.push_back(this);
} }
void CGTownInstance::afterRemoveFromMap(CMap* map)
{
if (ID == Obj::TOWN)
vstd::erase_if_present(map->towns, this);
}
void CGTownInstance::reset() void CGTownInstance::reset()
{ {
CGTownInstance::merchantArtifacts.clear(); CGTownInstance::merchantArtifacts.clear();

View File

@ -342,6 +342,7 @@ public:
std::string getObjectName() const override; std::string getObjectName() const override;
void afterAddToMap(CMap * map) override; void afterAddToMap(CMap * map) override;
void afterRemoveFromMap(CMap* map);
static void reset(); static void reset();
inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const inline bool isBattleOutsideTown(const CGHeroInstance * defendingHero) const

View File

@ -385,6 +385,11 @@ void CGObjectInstance::afterAddToMap(CMap * map)
//nothing here //nothing here
} }
void CGObjectInstance::afterRemoveFromMap(CMap* map)
{
//nothing here
}
void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler) void CGObjectInstance::serializeJsonOptions(JsonSerializeFormat & handler)
{ {
//nothing here //nothing here

View File

@ -197,6 +197,7 @@ public:
void setProperty(ui8 what, ui32 val) override final; void setProperty(ui8 what, ui32 val) override final;
virtual void afterAddToMap(CMap * map); virtual void afterAddToMap(CMap * map);
virtual void afterRemoveFromMap(CMap* map);
///Entry point of binary (de-)serialization ///Entry point of binary (de-)serialization
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)

View File

@ -1425,6 +1425,9 @@ void CGArtifact::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer)
void CGArtifact::afterAddToMap(CMap * map) void CGArtifact::afterAddToMap(CMap * map)
{ {
//Artifacts from map objects are never removed
//FIXME: This should be revertible in map editor
if(ID == Obj::SPELL_SCROLL && storedArtifact && storedArtifact->id.getNum() < 0) if(ID == Obj::SPELL_SCROLL && storedArtifact && storedArtifact->id.getNum() < 0)
map->addNewArtifactInstance(storedArtifact); map->addNewArtifactInstance(storedArtifact);
} }

View File

@ -594,6 +594,7 @@ void CMap::addNewArtifactInstance(CArtifactInstance * art)
void CMap::eraseArtifactInstance(CArtifactInstance * art) void CMap::eraseArtifactInstance(CArtifactInstance * art)
{ {
//TODO: handle for artifacts removed in map editor
assert(artInstances[art->id.getNum()] == art); assert(artInstances[art->id.getNum()] == art);
artInstances[art->id.getNum()].dellNull(); artInstances[art->id.getNum()].dellNull();
} }
@ -604,6 +605,20 @@ void CMap::addNewQuestInstance(CQuest* quest)
quests.push_back(quest); quests.push_back(quest);
} }
void CMap::removeQuestInstance(CQuest* quest)
{
//TODO: should be called only by map editor.
//During game, completed quests or quests from removed objects stay forever
//Shift indexes
auto iter = std::next(quests.begin(), quest->qid);
iter = quests.erase(iter);
for (int i = quest->qid; iter != quests.end(); ++i, ++iter)
{
(*iter)->qid = i;
}
}
void CMap::setUniqueInstanceName(CGObjectInstance* obj) void CMap::setUniqueInstanceName(CGObjectInstance* obj)
{ {
//this gives object unique name even if objects are removed later //this gives object unique name even if objects are removed later
@ -632,7 +647,6 @@ void CMap::addNewObject(CGObjectInstance * obj)
addBlockVisTiles(obj); addBlockVisTiles(obj);
//TODO: how about deafeated heroes recruited again? //TODO: how about deafeated heroes recruited again?
//TODO: How about objects restored with map editor?
obj->afterAddToMap(this); obj->afterAddToMap(this);
} }
@ -657,15 +671,7 @@ void CMap::removeObject(CGObjectInstance * obj)
(*iter)->id = ObjectInstanceID(i); (*iter)->id = ObjectInstanceID(i);
} }
auto iterTown = std::find(towns.begin(), towns.end(), obj); obj->afterRemoveFromMap(this);
if(iterTown != towns.end())
towns.erase(iterTown);
auto iterHero = std::find(allHeroes.begin(), allHeroes.end(), obj);
if(iterHero != allHeroes.end())
allHeroes.erase(iterHero);
iterHero = std::find(heroesOnMap.begin(), heroesOnMap.end(), obj);
if(iterHero != heroesOnMap.end())
heroesOnMap.erase(iterHero);
//TOOD: Clean artifact instances (mostly worn by hero?) and quests related to this object //TOOD: Clean artifact instances (mostly worn by hero?) and quests related to this object
} }

View File

@ -361,6 +361,7 @@ public:
void eraseArtifactInstance(CArtifactInstance * art); void eraseArtifactInstance(CArtifactInstance * art);
void addNewQuestInstance(CQuest * quest); void addNewQuestInstance(CQuest * quest);
void removeQuestInstance(CQuest* quest);
void setUniqueInstanceName(CGObjectInstance* obj); void setUniqueInstanceName(CGObjectInstance* obj);
///Use only this method when creating new map object instances ///Use only this method when creating new map object instances