From a2af3b59429b5372734df048c317edc1915915f0 Mon Sep 17 00:00:00 2001 From: Eric Robinson <68359262+kphoenix137@users.noreply.github.com> Date: Sun, 9 Jul 2023 20:28:02 -0400 Subject: [PATCH] Refactor CreatePlayer() (#6288) --- Source/player.cpp | 51 ++++++++++++++++++++++---------------------- Source/player.h | 6 ++++++ Source/playerdat.cpp | 14 ++++++------ Source/playerdat.hpp | 16 ++++++++------ 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/Source/player.cpp b/Source/player.cpp index 70a772305..78b6f2486 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -2055,6 +2055,18 @@ void Player::UpdatePreviewCelSprite(_cmd_id cmdId, Point point, uint16_t wParam1 } } +int32_t Player::calculateBaseLife() const +{ + const PlayerData playerData = PlayersData[static_cast(_pClass)]; + return playerData.adjLife + (playerData.lvlLife * _pLevel) + (playerData.chrLife * _pBaseVit); +} + +int32_t Player::calculateBaseMana() const +{ + const PlayerData playerData = PlayersData[static_cast(_pClass)]; + return playerData.adjMana + (playerData.lvlMana * _pLevel) + (playerData.chrMana * _pBaseMag); +} + Player *PlayerAtPosition(Point position) { if (!InDungeonBounds(position)) @@ -2265,44 +2277,31 @@ void CreatePlayer(Player &player, HeroClass c) player = {}; SetRndSeed(SDL_GetTicks()); + PlayerData playerData = PlayersData[static_cast(c)]; + + player._pLevel = 1; player._pClass = c; - player._pBaseStr = PlayersData[static_cast(c)].baseStr; + player._pBaseStr = playerData.baseStr; player._pStrength = player._pBaseStr; - player._pBaseMag = PlayersData[static_cast(c)].baseMag; + player._pBaseMag = playerData.baseMag; player._pMagic = player._pBaseMag; - player._pBaseDex = PlayersData[static_cast(c)].baseDex; + player._pBaseDex = playerData.baseDex; player._pDexterity = player._pBaseDex; - player._pBaseVit = PlayersData[static_cast(c)].baseVit; + player._pBaseVit = playerData.baseVit; player._pVitality = player._pBaseVit; - player._pLevel = 1; - - player._pBaseToBlk = PlayersData[static_cast(c)].blockBonus; - - player._pHitPoints = (player._pVitality + 10) << 6; - if (player._pClass == HeroClass::Warrior || player._pClass == HeroClass::Barbarian) { - player._pHitPoints *= 2; - } else if (player._pClass == HeroClass::Rogue || player._pClass == HeroClass::Monk || player._pClass == HeroClass::Bard) { - player._pHitPoints += player._pHitPoints / 2; - } + player._pBaseToBlk = playerData.blockBonus; + player._pHitPoints = player.calculateBaseLife(); player._pMaxHP = player._pHitPoints; player._pHPBase = player._pHitPoints; player._pMaxHPBase = player._pHitPoints; - player._pMana = player._pMagic << 6; - if (player._pClass == HeroClass::Sorcerer) { - player._pMana *= 2; - } else if (player._pClass == HeroClass::Bard) { - player._pMana += player._pMana * 3 / 4; - } else if (player._pClass == HeroClass::Rogue || player._pClass == HeroClass::Monk) { - player._pMana += player._pMana / 2; - } - + player._pMana = player.calculateBaseMana(); player._pMaxMana = player._pMana; player._pManaBase = player._pMana; player._pMaxManaBase = player._pMana; @@ -2315,7 +2314,7 @@ void CreatePlayer(Player &player, HeroClass c) player._pInfraFlag = false; player._pRSplType = SpellType::Skill; - SpellID s = PlayersData[static_cast(c)].skill; + SpellID s = playerData.skill; player._pAblSpells = GetSpellBitmask(s); player._pRSpell = s; @@ -2401,7 +2400,7 @@ void NextPlrLevel(Player &player) } player._pNextExper = ExpLvlsTbl[std::min(player._pLevel, MaxCharacterLevel - 1)]; - int hp = PlayersData[static_cast(player._pClass)].lvlUpLife; + int hp = PlayersData[static_cast(player._pClass)].lvlLife; player._pMaxHP += hp; player._pHitPoints = player._pMaxHP; @@ -2412,7 +2411,7 @@ void NextPlrLevel(Player &player) RedrawComponent(PanelDrawComponent::Health); } - int mana = PlayersData[static_cast(player._pClass)].lvlUpMana; + int mana = PlayersData[static_cast(player._pClass)].lvlMana; player._pMaxMana += mana; player._pMaxManaBase += mana; diff --git a/Source/player.h b/Source/player.h index 36fc4c0b1..a89263c1e 100644 --- a/Source/player.h +++ b/Source/player.h @@ -772,6 +772,12 @@ struct Player { this->plrlevel = static_cast(level); this->plrIsOnSetLevel = true; } + + /** @brief Returns a character's life based on starting life, character level, and base vitality. */ + int32_t calculateBaseLife() const; + + /** @brief Returns a character's mana based on starting mana, character level, and base magic. */ + int32_t calculateBaseMana() const; }; extern DVL_API_FOR_TEST size_t MyPlayerId; diff --git a/Source/playerdat.cpp b/Source/playerdat.cpp index a0a4c674e..3c913e68f 100644 --- a/Source/playerdat.cpp +++ b/Source/playerdat.cpp @@ -83,15 +83,15 @@ const _sfx_id herosounds[enum_size::value][enum_size::val /** Contains the data related to each player class. */ const PlayerData PlayersData[] = { // clang-format off -// HeroClass className, classPath, baseStr, baseMag, baseDex, baseVit, maxStr, maxMag, maxDex, maxVit, blockBonus, lvlUpLife, lvlUpMana, chrLife, chrMana, itmLife, itmMana, skill, +// HeroClass className, classPath, baseStr, baseMag, baseDex, baseVit, maxStr, maxMag, maxDex, maxVit, blockBonus, adjLife, adjMana, lvlLife, lvlMana, chrLife, chrMana, itmLife, itmMana, skill, // TRANSLATORS: Player Block start -/* HeroClass::Warrior */ { N_("Warrior"), "warrior", 30, 10, 20, 25, 250, 50, 60, 100, 30, (2 << 6), (1 << 6), (2 << 6), (1 << 6), (2 << 6), (1 << 6), SpellID::ItemRepair }, -/* HeroClass::Rogue */ { N_("Rogue"), "rogue", 20, 15, 30, 20, 55, 70, 250, 80, 20, (2 << 6), (2 << 6), (1 << 6), (1 << 6), static_cast(1.5F * 64), static_cast(1.5F * 64), SpellID::TrapDisarm }, -/* HeroClass::Sorcerer */ { N_("Sorcerer"), "sorceror", 15, 35, 15, 20, 45, 250, 85, 80, 10, (1 << 6), (2 << 6), (1 << 6), (2 << 6), (1 << 6), (2 << 6), SpellID::StaffRecharge }, -/* HeroClass::Monk */ { N_("Monk"), "monk", 25, 15, 25, 20, 150, 80, 150, 80, 25, (2 << 6), (2 << 6), (1 << 6), (1 << 6), static_cast(1.5F * 64), static_cast(1.5F * 64), SpellID::Search, }, -/* HeroClass::Bard */ { N_("Bard"), "rogue", 20, 20, 25, 20, 120, 120, 120, 100, 25, (2 << 6), (2 << 6), (1 << 6), static_cast(1.5F * 64), static_cast(1.5F * 64), static_cast(1.75F * 64), SpellID::Identify }, -/* HeroClass::Barbarian */ { N_("Barbarian"), "warrior", 40, 0, 20, 25, 255, 0, 55, 150, 30, (2 << 6), (0 << 6), (2 << 6), (1 << 6), static_cast(2.5F * 64), (1 << 6), SpellID::Rage }, +/* HeroClass::Warrior */ { N_("Warrior"), "warrior", 30, 10, 20, 25, 250, 50, 60, 100, 30, (18 << 6), (-1 << 6), (2 << 6), (1 << 6), (2 << 6), (1 << 6), (2 << 6), (1 << 6), SpellID::ItemRepair }, +/* HeroClass::Rogue */ { N_("Rogue"), "rogue", 20, 15, 30, 20, 55, 70, 250, 80, 20, (23 << 6), static_cast(5.5F * 64), (2 << 6), (2 << 6), (1 << 6), (1 << 6), static_cast(1.5F * 64), static_cast(1.5F * 64), SpellID::TrapDisarm }, +/* HeroClass::Sorcerer */ { N_("Sorcerer"), "sorceror", 15, 35, 15, 20, 45, 250, 85, 80, 10, (9 << 6), (-2 << 6), (1 << 6), (2 << 6), (1 << 6), (2 << 6), (1 << 6), (2 << 6), SpellID::StaffRecharge }, +/* HeroClass::Monk */ { N_("Monk"), "monk", 25, 15, 25, 20, 150, 80, 150, 80, 25, (23 << 6), static_cast(5.5F * 64), (2 << 6), (2 << 6), (1 << 6), (1 << 6), static_cast(1.5F * 64), static_cast(1.5F * 64), SpellID::Search, }, +/* HeroClass::Bard */ { N_("Bard"), "rogue", 20, 20, 25, 20, 120, 120, 120, 100, 25, (23 << 6), (3 << 6), (2 << 6), (2 << 6), (1 << 6), static_cast(1.5F * 64), static_cast(1.5F * 64), static_cast(1.75F * 64), SpellID::Identify }, +/* HeroClass::Barbarian */ { N_("Barbarian"), "warrior", 40, 0, 20, 25, 255, 0, 55, 150, 30, (18 << 6), (0 << 6), (2 << 6), (0 << 6), (2 << 6), (1 << 6), static_cast(2.5F * 64), (1 << 6), SpellID::Rage }, // clang-format on }; diff --git a/Source/playerdat.hpp b/Source/playerdat.hpp index 95b847f8d..7e10c3af4 100644 --- a/Source/playerdat.hpp +++ b/Source/playerdat.hpp @@ -35,18 +35,22 @@ struct PlayerData { uint8_t maxVit; /* Class Block Bonus % */ uint8_t blockBonus; + /* Class Life Adjustment */ + int16_t adjLife; + /* Class Mana Adjustment */ + int16_t adjMana; /* Life gained on level up */ - uint16_t lvlUpLife; + int16_t lvlLife; /* Mana gained on level up */ - uint16_t lvlUpMana; + int16_t lvlMana; /* Life from base Vitality */ - uint16_t chrLife; + int16_t chrLife; /* Mana from base Magic */ - uint16_t chrMana; + int16_t chrMana; /* Life from item bonus Vitality */ - uint16_t itmLife; + int16_t itmLife; /* Mana from item bonus Magic */ - uint16_t itmMana; + int16_t itmMana; /* Class Skill */ SpellID skill; };