1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Merge pull request #2458 from vcmi/fix_water_tavern

Fix crash at water tavern
This commit is contained in:
DjWarmonger 2023-08-02 20:03:02 +02:00 committed by GitHub
commit ca0eacc283
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 18 deletions

View File

@ -110,7 +110,11 @@ void CBuilding::addNewBonus(const std::shared_ptr<Bonus> & b, BonusList & bonusL
CFaction::~CFaction()
{
delete town;
if (town)
{
delete town;
town = nullptr;
}
}
int32_t CFaction::getIndex() const
@ -1029,7 +1033,7 @@ CFaction * CTownHandler::loadFromJson(const std::string & scope, const JsonNode
faction->creatureBg120 = source["creatureBackground"]["120px"].String();
faction->creatureBg130 = source["creatureBackground"]["130px"].String();
faction->boatType = EBoatId::NONE;
faction->boatType = EBoatId::CASTLE; //Do not crash
if (!source["boat"].isNull())
{
VLC->modh->identifiers.requestIdentifier("core:boat", source["boat"], [=](int32_t boatTypeID)

View File

@ -207,7 +207,7 @@ public:
/// Boat that will be used by town shipyard (if any)
/// and for placing heroes directly on boat (in map editor, water prisons & taverns)
BoatId boatType;
BoatId boatType = BoatId(EBoatId::CASTLE);
CTown * town = nullptr; //NOTE: can be null

View File

@ -1382,9 +1382,8 @@ void HeroRecruited::applyGs(CGameState * gs) const
auto * boat = dynamic_cast<CGBoat *>(obj);
if (boat)
{
h->boat = boat;
h->attachTo(*boat);
boat->hero = h;
gs->map->removeBlockVisTiles(boat);
h->attachToBoat(boat);
}
}
@ -1419,9 +1418,8 @@ void GiveHero::applyGs(CGameState * gs) const
auto * boat = dynamic_cast<CGBoat *>(obj);
if (boat)
{
h->boat = boat;
h->attachTo(*boat);
boat->hero = h;
gs->map->removeBlockVisTiles(boat);
h->attachToBoat(boat);
}
}

View File

@ -857,8 +857,7 @@ void CGameState::initHeroes()
map->objects.emplace_back(boat);
map->addBlockVisTiles(boat);
boat->hero = hero;
hero->boat = boat;
hero->attachToBoat(boat);
}
}

View File

@ -484,9 +484,12 @@ void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
if (cb->gameState()->map->getTile(boatPos).isWater())
{
smp.val = movementPointsLimit(false);
//Create a new boat for hero
cb->createObject(boatPos, Obj::BOAT, getBoatType().getNum());
boatId = cb->getTopObj(boatPos)->id;
if (!boat)
{
//Create a new boat for hero
cb->createObject(boatPos, Obj::BOAT, getBoatType().getNum());
boatId = cb->getTopObj(boatPos)->id;
}
}
else
{
@ -1119,6 +1122,15 @@ int CGHeroInstance::maxSpellLevel() const
return std::min(GameConstants::SPELL_LEVELS, valOfBonuses(Selector::type()(BonusType::MAX_LEARNABLE_SPELL_LEVEL)));
}
void CGHeroInstance::attachToBoat(CGBoat* newBoat)
{
assert(newBoat);
boat = newBoat;
attachTo(const_cast<CGBoat&>(*boat));
const_cast<CGBoat*>(boat)->hero = this;
}
void CGHeroInstance::deserializationFix()
{
artDeserializationFix(this);

View File

@ -282,6 +282,7 @@ public:
void getCastDescription(const spells::Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
void spendMana(ServerCallback * server, const int spellCost) const override;
void attachToBoat(CGBoat* newBoat);
void boatDeserializationFix();
void deserializationFix();

View File

@ -237,18 +237,19 @@ bool HeroPoolProcessor::hireHero(const ObjectInstanceID & objectID, const HeroTy
gameHandler->complain("Hero is not available for hiring!");
return false;
}
const auto targetPos = mapObject->visitablePos();
HeroRecruited hr;
hr.tid = mapObject->id;
hr.hid = recruitedHero->subID;
hr.player = player;
hr.tile = recruitedHero->convertFromVisitablePos(mapObject->visitablePos());
if(gameHandler->getTile(hr.tile)->isWater())
hr.tile = recruitedHero->convertFromVisitablePos(targetPos );
if(gameHandler->getTile(hr.tile)->isWater() && !recruitedHero->boat)
{
//Create a new boat for hero
gameHandler->createObject(mapObject->visitablePos(), Obj::BOAT, recruitedHero->getBoatType().getNum());
gameHandler->createObject(targetPos , Obj::BOAT, recruitedHero->getBoatType().getNum());
hr.boatId = gameHandler->getTopObj(hr.tile)->id;
hr.boatId = gameHandler->getTopObj(targetPos)->id;
}
// apply netpack -> this will remove hired hero from pool