Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple spellbook-related issues in the Battle Only mode #8160

Merged
merged 3 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix multiple spellbook-related issues in the Battle Only mode
  • Loading branch information
oleg-derevenetz committed Dec 22, 2023
commit a575b893b8e508305e6619d54ae2cf9dc6b45079
36 changes: 25 additions & 11 deletions src/fheroes2/battle/battle_only.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )
attackedArmyControlInfo->Redraw();
}

bool exit = false;
bool redraw = false;
bool result = false;

// hide the shadow from the original EXIT button
const fheroes2::Sprite buttonOverride = fheroes2::Crop( fheroes2::AGG::GetICN( ICN::SWAPWIN, 0 ), 122, 428, 84, 32 );
fheroes2::Blit( buttonOverride, display, cur_pt.x + 276, cur_pt.y + 428 );
Expand All @@ -214,23 +210,30 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )

display.render();

while ( !exit && le.HandleEvents() ) {
bool result = false;

while ( le.HandleEvents() ) {
bool updateSpellPoints = false;
bool redraw = false;

buttonOkay.isEnabled() && le.MousePressLeft( buttonOkay.area() ) ? buttonOkay.drawOnPress() : buttonOkay.drawOnRelease();
buttonCancel.isEnabled() && le.MousePressLeft( buttonCancel.area() ) ? buttonCancel.drawOnPress() : buttonCancel.drawOnRelease();
buttonReset.isEnabled() && le.MousePressLeft( buttonReset.area() ) ? buttonReset.drawOnPress() : buttonReset.drawOnRelease();

if ( ( buttonOkay.isEnabled() && le.MouseClickLeft( buttonOkay.area() ) ) || Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_OKAY ) ) {
result = true;
exit = true;

break;
}
if ( le.MouseClickLeft( buttonReset.area() ) ) {
reset = true;
result = true;

break;
}

if ( le.MouseClickLeft( buttonCancel.area() ) || Game::HotKeyPressEvent( Game::HotKeyEvent::DEFAULT_CANCEL ) ) {
exit = true;
break;
}

for ( const auto & ids : { std::pair<int32_t, int32_t>( 0, 1 ), std::pair<int32_t, int32_t>( 1, 0 ) } ) {
Expand Down Expand Up @@ -293,8 +296,8 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )
uint32_t value = hero->knowledge;
if ( Dialog::SelectCount( _( "Set Knowledge Skill" ), 0, primaryMaxValue, value ) ) {
hero->knowledge = value;
hero->SetSpellPoints( hero->knowledge * 10 );

updateSpellPoints = true;
redraw = true;
}
}
Expand Down Expand Up @@ -332,6 +335,7 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )
secondUI.army->ResetSelected();
}

updateSpellPoints = true;
redraw = true;
}
else if ( firstUI.morale != nullptr && le.MouseCursor( firstUI.morale->GetArea() ) ) {
Expand Down Expand Up @@ -365,6 +369,16 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )
}
}

if ( updateSpellPoints ) {
for ( Heroes * hero : { armyInfo[0].hero, armyInfo[1].hero } ) {
if ( hero == nullptr ) {
continue;
}

hero->SetSpellPoints( hero->GetMaxSpellPoints() );
}
}

if ( !redraw ) {
continue;
}
Expand All @@ -384,8 +398,6 @@ bool Battle::Only::setup( const bool allowBackup, bool & reset )
buttonCancel.draw();
buttonReset.draw();
display.render();

redraw = false;
}

armyInfo[0].ui = {};
Expand Down Expand Up @@ -501,7 +513,7 @@ void Battle::Only::reset()
attackedArmyControlInfo.reset();
}

void Battle::Only::copyHero( Heroes & in, Heroes & out )
void Battle::Only::copyHero( const Heroes & in, Heroes & out )
{
out.attack = in.attack;
out.defense = in.defense;
Expand All @@ -516,6 +528,8 @@ void Battle::Only::copyHero( Heroes & in, Heroes & out )

out.bag_artifacts = in.bag_artifacts;
out.spell_book = in.spell_book;

out.SetSpellPoints( out.GetMaxSpellPoints() );
}

void Battle::Only::updateArmyUI( ArmyUI & ui, Heroes * hero, const fheroes2::Point & offset, const uint8_t armyId )
Expand Down
2 changes: 1 addition & 1 deletion src/fheroes2/battle/battle_only.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace Battle

static void updateArmyUI( ArmyUI & ui, Heroes * hero, const fheroes2::Point & offset, const uint8_t armyId );

static void copyHero( Heroes & in, Heroes & out );
static void copyHero( const Heroes & in, Heroes & out );
};
}

Expand Down
3 changes: 1 addition & 2 deletions src/fheroes2/heroes/heroes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ void Heroes::LoadFromMP2( const int32_t mapIndex, const int colorType, const int
//

// Clear the initial spell
spell_book.clear();
bag_artifacts.RemoveArtifact( Artifact::MAGIC_BOOK );
SpellBookDeactivate();

// Reset primary skills and initial spell to defaults
HeroBase::LoadDefaults( HeroBase::HEROES, _race );
Expand Down
10 changes: 10 additions & 0 deletions src/fheroes2/heroes/heroes_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ bool HeroBase::SpellBookActivate()
return !HaveSpellBook() && bag_artifacts.PushArtifact( Artifact::MAGIC_BOOK );
}

void HeroBase::SpellBookDeactivate()
{
bag_artifacts.RemoveArtifact( Artifact::MAGIC_BOOK );

// Hero should not have more than one spell book
assert( !HaveSpellBook() );

spell_book.clear();
}

bool HeroBase::hasArtifact( const Artifact & art ) const
{
return bag_artifacts.isPresentArtifact( art );
Expand Down
4 changes: 4 additions & 0 deletions src/fheroes2/heroes/heroes_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ class HeroBase : public Skill::Primary, public MapPosition, public BitModes, pub
bool HaveSpell( const Spell & spell, const bool skip_bag = false ) const;
void AppendSpellToBook( const Spell &, const bool without_wisdom = false );
void AppendSpellsToBook( const SpellStorage &, const bool without_wisdom = false );

// Adds the spell book to the artifact bag if it is not already there. Returns true if the spell book was actually added to the artifact bag, otherwise returns false.
bool SpellBookActivate();
// Removes the spell book artifact from the artifact bag, if it is there, and removes all spells from the hero's spell book.
void SpellBookDeactivate();

BagArtifacts & GetBagArtifacts()
{
Expand Down
6 changes: 6 additions & 0 deletions src/fheroes2/heroes/skill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,12 @@ std::vector<Skill::Secondary> & Skill::SecSkills::ToVector()
return v;
}

const std::vector<Skill::Secondary> & Skill::SecSkills::ToVector() const
{
const std::vector<Secondary> & v = *this;
return v;
}

std::string Skill::SecSkills::String() const
{
std::string output;
Expand Down
11 changes: 8 additions & 3 deletions src/fheroes2/heroes/skill.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,21 @@ namespace Skill
SecSkills & operator=( const SecSkills & ) = delete;
SecSkills & operator=( SecSkills && ) = default;

int Count() const;
int GetLevel( int skill ) const;
int GetTotalLevel() const;
uint32_t GetValues( int skill ) const;

Secondary * FindSkill( int );

void AddSkill( const Skill::Secondary & );
void FindSkillsForLevelUp( int race, uint32_t seedSkill1, uint32_t seedSkill2, Secondary &, Secondary & ) const;
void FillMax( const Skill::Secondary & );
Secondary * FindSkill( int );

std::string String() const;
int Count() const;
int GetTotalLevel() const;

std::vector<Secondary> & ToVector();
const std::vector<Secondary> & ToVector() const;

protected:
friend StreamBase & operator<<( StreamBase &, const SecSkills & );
Expand Down
15 changes: 12 additions & 3 deletions src/fheroes2/resource/artifact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,11 @@ bool BagArtifacts::PushArtifact( const Artifact & art )
void BagArtifacts::RemoveArtifact( const Artifact & art )
{
iterator it = std::find( begin(), end(), art );
if ( it != end() )
( *it ).Reset();
if ( it == end() ) {
return;
}

it->Reset();
}

bool BagArtifacts::isFull() const
Expand Down Expand Up @@ -1188,6 +1191,7 @@ bool ArtifactsBar::ActionBarLeftMouseSingleClick( Artifact & art )

if ( isMagicBook( art ) ) {
art.Reset();

const_cast<Heroes *>( _hero )->SpellBookActivate();
}
else if ( art.GetID() == Artifact::SPELL_SCROLL ) {
Expand Down Expand Up @@ -1226,7 +1230,12 @@ bool ArtifactsBar::ActionBarRightMouseHold( Artifact & art )

if ( art.isValid() ) {
if ( can_change ) {
art.Reset();
if ( isMagicBook( art ) ) {
const_cast<Heroes *>( _hero )->SpellBookDeactivate();
}
else {
art.Reset();
}
}
else {
fheroes2::ArtifactDialogElement( art ).showPopup( Dialog::ZERO );
Expand Down
1 change: 1 addition & 0 deletions src/fheroes2/resource/artifact.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ class BagArtifacts : public std::vector<Artifact>

bool PushArtifact( const Artifact & );

// Removes the first found instance of the specified artifact from the artifact bag
void RemoveArtifact( const Artifact & art );

bool isFull() const;
Expand Down