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

Remove pointer to CGObjectInstance from map header

This commit is contained in:
Ivan Savenko 2023-11-17 16:39:15 +02:00
parent f53a53051b
commit 9f906ff1d2
7 changed files with 29 additions and 34 deletions

View File

@ -54,15 +54,15 @@ TSubgoal Win::whatToDoToAchieve()
return sptr(GetArtOfType(goal.objectType.as<ArtifactID>()));
case EventCondition::DESTROY:
{
if(goal.object)
if(goal.objectID != ObjectInstanceID::NONE)
{
auto obj = cb->getObj(goal.object->id);
auto obj = cb->getObj(goal.objectID);
if(obj)
if(obj->getOwner() == ai->playerID) //we can't capture our own object
return sptr(Conquer());
return sptr(VisitObj(goal.object->id.getNum()));
return sptr(VisitObj(goal.objectID.getNum()));
}
else
{
@ -124,13 +124,13 @@ TSubgoal Win::whatToDoToAchieve()
}
case EventCondition::CONTROL:
{
if(goal.object)
if(goal.objectID != ObjectInstanceID::NONE)
{
auto objRelations = cb->getPlayerRelations(ai->playerID, goal.object->tempOwner);
auto obj = cb->getObj(goal.objectID);
if(objRelations == PlayerRelations::ENEMIES)
if(obj && cb->getPlayerRelations(ai->playerID, obj->tempOwner) == PlayerRelations::ENEMIES)
{
return sptr(VisitObj(goal.object->id.getNum()));
return sptr(VisitObj(goal.objectID.getNum()));
}
else
{

View File

@ -1411,9 +1411,9 @@ bool CGameState::checkForVictory(const PlayerColor & player, const EventConditio
}
case EventCondition::HAVE_BUILDING:
{
if (condition.object) // specific town
if (condition.objectID != ObjectInstanceID::NONE) // specific town
{
const auto * t = dynamic_cast<const CGTownInstance *>(condition.object);
const auto * t = getTown(condition.objectID);
return (t->tempOwner == player && t->hasBuilt(condition.objectType.as<BuildingID>()));
}
else // any town
@ -1428,12 +1428,12 @@ bool CGameState::checkForVictory(const PlayerColor & player, const EventConditio
}
case EventCondition::DESTROY:
{
if (condition.object) // mode A - destroy specific object of this type
if (condition.objectID != ObjectInstanceID::NONE) // mode A - destroy specific object of this type
{
if(const auto * hero = dynamic_cast<const CGHeroInstance *>(condition.object))
if(const auto * hero = getHero(condition.objectID))
return boost::range::find(gs->map->heroesOnMap, hero) == gs->map->heroesOnMap.end();
else
return getObj(condition.object->id) == nullptr;
return getObj(condition.objectID) == nullptr;
}
else
{
@ -1451,9 +1451,9 @@ bool CGameState::checkForVictory(const PlayerColor & player, const EventConditio
// NOTE: cgameinfocallback specified explicitly in order to get const version
const auto & team = CGameInfoCallback::getPlayerTeam(player)->players;
if (condition.object) // mode A - flag one specific object, like town
if (condition.objectID != ObjectInstanceID::NONE) // mode A - flag one specific object, like town
{
return team.count(condition.object->tempOwner) != 0;
return team.count(getObjInstance(condition.objectID)->tempOwner) != 0;
}
else
{
@ -1468,7 +1468,7 @@ bool CGameState::checkForVictory(const PlayerColor & player, const EventConditio
}
case EventCondition::TRANSPORT:
{
const auto * t = dynamic_cast<const CGTownInstance *>(condition.object);
const auto * t = getTown(condition.objectID);
return (t->visitingHero && t->visitingHero->hasArt(condition.objectType.as<ArtifactID>())) ||
(t->garrisonHero && t->garrisonHero->hasArt(condition.objectType.as<ArtifactID>()));
}

View File

@ -1793,11 +1793,8 @@ bool CGHeroInstance::isMissionCritical() const
auto const & testFunctor = [&](const EventCondition & condition)
{
if ((condition.condition == EventCondition::CONTROL) && condition.object)
{
const auto * hero = dynamic_cast<const CGHeroInstance *>(condition.object);
return (hero != this);
}
if ((condition.condition == EventCondition::CONTROL) && condition.objectID != ObjectInstanceID::NONE)
return (id != condition.objectID);
if(condition.condition == EventCondition::IS_HUMAN)
return true;

View File

@ -448,19 +448,19 @@ void CMap::checkForObjectives()
case EventCondition::HAVE_BUILDING:
if (isInTheMap(cond.position))
cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
cond.objectID = getObjectiveObjectFrom(cond.position, Obj::TOWN)->id;
break;
case EventCondition::CONTROL:
if (isInTheMap(cond.position))
cond.object = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>());
cond.objectID = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>())->id;
if (cond.object)
if (cond.objectID != ObjectInstanceID::NONE)
{
const auto * town = dynamic_cast<const CGTownInstance *>(cond.object);
const auto * town = dynamic_cast<const CGTownInstance *>(objects[cond.objectID].get());
if (town)
event.onFulfill.replaceRawString(town->getNameTranslated());
const auto * hero = dynamic_cast<const CGHeroInstance *>(cond.object);
const auto * hero = dynamic_cast<const CGHeroInstance *>(objects[cond.objectID].get());
if (hero)
event.onFulfill.replaceRawString(hero->getNameTranslated());
}
@ -468,17 +468,17 @@ void CMap::checkForObjectives()
case EventCondition::DESTROY:
if (isInTheMap(cond.position))
cond.object = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>());
cond.objectID = getObjectiveObjectFrom(cond.position, cond.objectType.as<MapObjectID>())->id;
if (cond.object)
if (cond.objectID != ObjectInstanceID::NONE)
{
const auto * hero = dynamic_cast<const CGHeroInstance *>(cond.object);
const auto * hero = dynamic_cast<const CGHeroInstance *>(objects[cond.objectID].get());
if (hero)
event.onFulfill.replaceRawString(hero->getNameTranslated());
}
break;
case EventCondition::TRANSPORT:
cond.object = getObjectiveObjectFrom(cond.position, Obj::TOWN);
cond.objectID = getObjectiveObjectFrom(cond.position, Obj::TOWN)->id;
break;
//break; case EventCondition::DAYS_PASSED:
//break; case EventCondition::IS_HUMAN:

View File

@ -68,7 +68,6 @@ bool PlayerInfo::hasCustomMainHero() const
}
EventCondition::EventCondition(EWinLoseType condition):
object(nullptr),
value(-1),
position(-1, -1, -1),
condition(condition)
@ -76,7 +75,6 @@ EventCondition::EventCondition(EWinLoseType condition):
}
EventCondition::EventCondition(EWinLoseType condition, si32 value, TargetTypeID objectType, const int3 & position):
object(nullptr),
value(value),
objectType(objectType),
position(position),

View File

@ -120,7 +120,7 @@ struct DLL_LINKAGE EventCondition
EventCondition(EWinLoseType condition = STANDARD_WIN);
EventCondition(EWinLoseType condition, si32 value, TargetTypeID objectType, const int3 & position = int3(-1, -1, -1));
const CGObjectInstance * object; // object that was at specified position or with instance name on start
ObjectInstanceID objectID; // object that was at specified position or with instance name on start
si32 value;
TargetTypeID objectType;
std::string objectInstanceName;
@ -130,7 +130,7 @@ struct DLL_LINKAGE EventCondition
template <typename Handler>
void serialize(Handler & h, const int version)
{
h & object;
h & objectID;
h & value;
h & objectType;
h & position;

View File

@ -1215,7 +1215,7 @@ void RemoveObject::applyGs(CGameState *gs)
{
auto patcher = [&](EventCondition cond) -> EventExpression::Variant
{
if (cond.object == obj)
if (cond.objectID == obj->id)
{
if (cond.condition == EventCondition::DESTROY)
{