From 8f4cb4e8ad954ba73fb78a030c969c933a7ed60c Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 8 Feb 2013 17:05:22 +0100 Subject: [PATCH] feed victory messages from upgrade --- src/battle/Battle.h | 2 ++ src/battle/states/VictoryState.cpp | 57 ++++++++++++++++++++++++++++-- src/battle/states/VictoryState.h | 5 +++ src/common/Capsule.cpp | 25 +++++++++++++ src/common/Capsule.h | 4 +++ src/common/Hero.cpp | 24 +++++++++++++ src/common/Hero.h | 22 ++++++++++++ test-data/test.l2s | 2 +- 8 files changed, 137 insertions(+), 4 deletions(-) diff --git a/src/battle/Battle.h b/src/battle/Battle.h index cd2894f..314288c 100644 --- a/src/battle/Battle.h +++ b/src/battle/Battle.h @@ -47,7 +47,9 @@ public: bool MonsterAlive(int index) const; bool CapsuleAlive() const; + std::vector::iterator HeroesBegin() { return heroes.begin(); } std::vector::const_iterator HeroesBegin() const { return heroes.begin(); } + std::vector::iterator HeroesEnd() { return heroes.end(); } std::vector::const_iterator HeroesEnd() const { return heroes.end(); } Hero &HeroAt(int index); const Hero &HeroAt(int index) const; diff --git a/src/battle/states/VictoryState.cpp b/src/battle/states/VictoryState.cpp index a0b09ce..409f084 100644 --- a/src/battle/states/VictoryState.cpp +++ b/src/battle/states/VictoryState.cpp @@ -8,7 +8,6 @@ #include "../../common/Capsule.h" #include "../../common/GameConfig.h" #include "../../common/GameState.h" -#include "../../common/Hero.h" #include "../../math/Vector.h" #include "../../graphics/Font.h" #include "../../graphics/Frame.h" @@ -43,11 +42,15 @@ void VictoryState::LoadResults() { lines.push_back(""); - for (std::vector::const_iterator + vector upgrade; + for (std::vector::iterator i(battle->HeroesBegin()), end(battle->HeroesEnd()); i != end; ++i) { if (i->Health() <= 0) continue; - const common::Hero &hero = i->Master(); + upgrade.clear(); + common::Hero &hero = i->Master(); + hero.AddExperience(battle->ExpReward(), upgrade); + LoadResults(hero.Name(), upgrade, lines); s.str(""); s << hero.Name() << " next level " << hero.NextLevel(); lines.push_back(s.str()); @@ -68,6 +71,54 @@ void VictoryState::LoadResults() { lines.push_back(s.str()); } +void VictoryState::LoadResults( + const char *who, + const vector &upgrade, + vector &lines) { + std::stringstream s; + for (vector::const_iterator + i(upgrade.begin()), end(upgrade.end()); + i != end; ++i) { + s.str(""); + switch (i->type) { + case common::Hero::UPGRADE_LVL: + s << who << " levels up."; + break; + case common::Hero::UPGRADE_MHP: + s << "Max. HP increases by " << i->amount; + break; + case common::Hero::UPGRADE_MMP: + s << "Max. MP increases by " << i->amount; + break; + case common::Hero::UPGRADE_ATK: + s << "ATK increases by " << i->amount; + break; + case common::Hero::UPGRADE_DFP: + s << "DFP increases by " << i->amount; + break; + case common::Hero::UPGRADE_STR: + s << "STR increases by " << i->amount; + break; + case common::Hero::UPGRADE_AGL: + s << "AGL increases by " << i->amount; + break; + case common::Hero::UPGRADE_INT: + s << "INT increases by " << i->amount; + break; + case common::Hero::UPGRADE_GUT: + s << "GUT increases by " << i->amount; + break; + case common::Hero::UPGRADE_MGR: + s << "MGR increases by " << i->amount; + break; + default: + s << "There's an error in common::Hero::" + "AddExperience()"; + } + lines.push_back(s.str()); + } +} + void VictoryState::OnExitState(SDL_Surface *screen) { } diff --git a/src/battle/states/VictoryState.h b/src/battle/states/VictoryState.h index 4dca6e9..41a541e 100644 --- a/src/battle/states/VictoryState.h +++ b/src/battle/states/VictoryState.h @@ -7,6 +7,7 @@ namespace battle { } #include "../../app/State.h" +#include "../../common/Hero.h" #include "../../math/Vector.h" #include @@ -41,6 +42,10 @@ private: private: void LoadResults(); + void LoadResults( + const char *, + const std::vector &, + std::vector &); void RenderFrame(SDL_Surface *screen); void RenderLines(SDL_Surface *screen); diff --git a/src/common/Capsule.cpp b/src/common/Capsule.cpp index 6667068..083931c 100644 --- a/src/common/Capsule.cpp +++ b/src/common/Capsule.cpp @@ -17,6 +17,7 @@ using graphics::Sprite; using loader::FieldDescription; using loader::Interpreter; using loader::TypeDescription; +using std::vector; namespace common { @@ -81,6 +82,30 @@ int Capsule::NextLevel() const { } } +void Capsule::AddExperience(int exp, vector &info) { + if (level > numLevels) { + // don't award any experience if at highest level + return; + } + int remain = exp; + while (remain >= NextLevel()) { + int added = NextLevel(); + experience += added; + remain -= added; + ++level; + + info.push_back(Hero::UpgradeInfo(Hero::UPGRADE_LVL, level)); + + // TODO: upgrade attributes and push info + + if (level > numLevels) { + return; + } + } + experience += remain; +} + + void Capsule::UpgradeClass() { ++maxClass; ++curClass; diff --git a/src/common/Capsule.h b/src/common/Capsule.h index 5aa5f34..a1afc1b 100644 --- a/src/common/Capsule.h +++ b/src/common/Capsule.h @@ -9,9 +9,11 @@ namespace graphics { class Sprite; } +#include "Hero.h" #include "../common/Stats.h" #include "../math/Vector.h" +#include #include namespace common { @@ -65,6 +67,8 @@ public: int Experience() const { return experience; } int NextLevel() const; + void AddExperience(int, std::vector &); + graphics::Sprite *BattleSprite(); const graphics::Sprite *BattleSprite() const; graphics::Animation *MeleeAnimation(); diff --git a/src/common/Hero.cpp b/src/common/Hero.cpp index cca9d43..4e7b5b9 100644 --- a/src/common/Hero.cpp +++ b/src/common/Hero.cpp @@ -17,6 +17,7 @@ using loader::Interpreter; using loader::TypeDescription; using map::Entity; using std::memset; +using std::vector; namespace common { @@ -69,6 +70,29 @@ int Hero::NextLevel() const { } } +void Hero::AddExperience(int exp, vector &info) { + if (level > numLevels) { + // don't award any experience if at highest level + return; + } + int remain = exp; + while (remain >= NextLevel()) { + int added = NextLevel(); + experience += added; + remain -= added; + ++level; + + info.push_back(UpgradeInfo(UPGRADE_LVL, level)); + + // TODO: upgrade attributes and push info + + if (level > numLevels) { + return; + } + } + experience += remain; +} + bool Hero::CanEquip(const Item &item) const { return useMask & item.HeroMask(); diff --git a/src/common/Hero.h b/src/common/Hero.h index aa12b25..2016396 100644 --- a/src/common/Hero.h +++ b/src/common/Hero.h @@ -37,6 +37,19 @@ public: EQUIP_COUNT, }; + enum UpgradeType { + UPGRADE_LVL, + UPGRADE_MHP, + UPGRADE_MMP, + UPGRADE_ATK, + UPGRADE_DFP, + UPGRADE_STR, + UPGRADE_AGL, + UPGRADE_INT, + UPGRADE_GUT, + UPGRADE_MGR, + }; + const char *Name() const { return name; } Uint16 MaxHealth() const { return maxHealth; } @@ -60,6 +73,14 @@ public: int Experience() const { return experience; } int NextLevel() const; + struct UpgradeInfo { + UpgradeType type; + int amount; + UpgradeInfo(UpgradeType t, int a = 0) + : type(t), amount(a) { } + }; + void AddExperience(int, std::vector &); + bool CanEquip(const Item &) const; bool CanInvoke(const Spell &) const; @@ -100,6 +121,7 @@ private: int level; int experience; + // TODO: ladder should contain hp, mp, and stats mods. int *levelLadder; int numLevels; diff --git a/test-data/test.l2s b/test-data/test.l2s index 4fd4e97..83baa77 100644 --- a/test-data/test.l2s +++ b/test-data/test.l2s @@ -45,7 +45,7 @@ export Monster lizard { gut: 6, mgr: 6 }, - expReward: 2, + expReward: 8, goldReward: 5, attackAnimation: ComplexAnimation { sprite: lizardSprite, -- 2.39.2