diff --git a/lib/CGameState.cpp b/lib/CGameState.cpp index b7c897596..7d6100c46 100644 --- a/lib/CGameState.cpp +++ b/lib/CGameState.cpp @@ -1798,7 +1798,7 @@ void CGameState::initTowns() for(ui32 z=0; zobligatorySpells.size();z++) { - CSpell *s = vti->obligatorySpells[z].toSpell(); + auto s = vti->obligatorySpells[z].toSpell(); vti->spells[s->level-1].push_back(s->id); vti->possibleSpells -= s->id; } @@ -1826,7 +1826,7 @@ void CGameState::initTowns() if(sel<0) sel=0; - CSpell *s = vti->possibleSpells[sel].toSpell(); + auto s = vti->possibleSpells[sel].toSpell(); vti->spells[s->level-1].push_back(s->id); vti->possibleSpells -= s->id; } diff --git a/lib/CTownHandler.cpp b/lib/CTownHandler.cpp index 170a99510..1439515a7 100644 --- a/lib/CTownHandler.cpp +++ b/lib/CTownHandler.cpp @@ -605,7 +605,7 @@ void CTownHandler::loadTown(CTown &town, const JsonNode & source) VLC->modh->identifiers.requestIdentifier(node.second.meta, "spell", node.first, [=, &town](si32 spellID) { - SpellID(spellID).toSpell()->probabilities[town.faction->index] = chance; + VLC->spellh->objects.at(spellID)->probabilities[town.faction->index] = chance; }); } diff --git a/lib/GameConstants.cpp b/lib/GameConstants.cpp index 31ed055f5..85526755d 100644 --- a/lib/GameConstants.cpp +++ b/lib/GameConstants.cpp @@ -31,17 +31,17 @@ const PlayerColor PlayerColor::NEUTRAL = PlayerColor(255); const PlayerColor PlayerColor::PLAYER_LIMIT = PlayerColor(PLAYER_LIMIT_I); const TeamID TeamID::NO_TEAM = TeamID(255); -CArtifact * ArtifactID::toArtifact() const +const CArtifact * ArtifactID::toArtifact() const { - return VLC->arth->artifacts[*this]; + return VLC->arth->artifacts.at(*this); } -CCreature * CreatureID::toCreature() const +const CCreature * CreatureID::toCreature() const { - return VLC->creh->creatures[*this]; + return VLC->creh->creatures.at(*this); } -CSpell * SpellID::toSpell() const +const CSpell * SpellID::toSpell() const { if(num < 0 || num >= VLC->spellh->objects.size()) { diff --git a/lib/GameConstants.h b/lib/GameConstants.h index 237bf66e0..10317059d 100644 --- a/lib/GameConstants.h +++ b/lib/GameConstants.h @@ -961,7 +961,7 @@ public: ArtifactID(EArtifactID _num = NONE) : num(_num) {} - DLL_LINKAGE CArtifact * toArtifact() const; + DLL_LINKAGE const CArtifact * toArtifact() const; ID_LIKE_CLASS_COMMON(ArtifactID, EArtifactID) @@ -1006,7 +1006,7 @@ public: CreatureID(ECreatureID _num = NONE) : num(_num) {} - DLL_LINKAGE CCreature * toCreature() const; + DLL_LINKAGE const CCreature * toCreature() const; ID_LIKE_CLASS_COMMON(CreatureID, ECreatureID) @@ -1049,8 +1049,7 @@ public: SpellID(ESpellID _num = NONE) : num(_num) {} - //TODO: should this be const? - DLL_LINKAGE CSpell * toSpell() const; + DLL_LINKAGE const CSpell * toSpell() const; ID_LIKE_CLASS_COMMON(SpellID, ESpellID) diff --git a/lib/spells/BattleSpellMechanics.cpp b/lib/spells/BattleSpellMechanics.cpp index d744e4157..e20c52fec 100644 --- a/lib/spells/BattleSpellMechanics.cpp +++ b/lib/spells/BattleSpellMechanics.cpp @@ -196,8 +196,8 @@ bool CureMechanics::dispellSelector(const Bonus * b) { if(b->source == Bonus::SPELL_EFFECT) { - CSpell * sp = SpellID(b->sid).toSpell(); - return sp->isNegative(); + const CSpell * sp = SpellID(b->sid).toSpell(); + return sp && sp->isNegative(); } return false; //not a spell effect } diff --git a/lib/spells/CreatureSpellMechanics.cpp b/lib/spells/CreatureSpellMechanics.cpp index 7418c5175..2c8bd96d3 100644 --- a/lib/spells/CreatureSpellMechanics.cpp +++ b/lib/spells/CreatureSpellMechanics.cpp @@ -99,8 +99,8 @@ bool DispellHelpfulMechanics::positiveSpellEffects(const Bonus *b) { if(b->source == Bonus::SPELL_EFFECT) { - CSpell *sp = SpellID(b->sid).toSpell(); - return sp->isPositive(); + const CSpell * sp = SpellID(b->sid).toSpell(); + return sp && sp->isPositive(); } return false; //not a spell effect } diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 4ddcbfd40..8ce6b2e82 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -4426,14 +4426,13 @@ bool CGameHandler::makeCustomAction( BattleAction &ba ) const CGHeroInstance *h = gs->curB->battleGetFightingHero(ba.side); COMPLAIN_RET_FALSE_IF((!h), "Wrong caster!"); - if(ba.additionalInfo < 0 || ba.additionalInfo >= VLC->spellh->objects.size()) + const CSpell * s = SpellID(ba.additionalInfo).toSpell(); + if(!s) { logGlobal->error("Wrong spell id (%d)!", ba.additionalInfo); return false; } - const CSpell * s = SpellID(ba.additionalInfo).toSpell(); - BattleSpellCastParameters parameters(gs->curB, h, s); parameters.aimToHex(ba.destinationTile);//todo: allow multiple destinations parameters.mode = ECastingMode::HERO_CASTING;