/src/skill.cpp

https://code.google.com/p/dwarftherapist/ · C++ · 93 lines · 65 code · 5 blank · 23 comment · 9 complexity · 3e3e62a5daf58c0f3098d8f6a554dbbb MD5 · raw file

  1. /*
  2. Dwarf Therapist
  3. Copyright (c) 2009 Trey Stout (chmod)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "skill.h"
  21. #include "gamedatareader.h"
  22. Skill::Skill()
  23. : m_id(-1)
  24. , m_exp(0)
  25. , m_actual_exp(0)
  26. , m_exp_for_current_level(0)
  27. , m_exp_for_next_level(1)
  28. , m_rating(-1)
  29. , m_name("UNKNOWN")
  30. {}
  31. Skill::Skill(short id, uint exp, short rating)
  32. : m_id(id)
  33. , m_exp(exp)
  34. , m_actual_exp(exp)
  35. , m_exp_for_current_level(0)
  36. , m_exp_for_next_level(exp + 1)
  37. , m_rating(rating > 20 ? 20 : rating)
  38. , m_name("UNKNOWN")
  39. {
  40. // formula from http://df.magmawiki.com/index.php/40d:Attribute
  41. m_actual_exp = m_exp;
  42. for (int i = 0; i < m_rating; ++i) {
  43. m_actual_exp += 500 + (i * 100);
  44. }
  45. m_exp_for_current_level = 0;
  46. for (int i = 0; i < m_rating; ++i) {
  47. m_exp_for_current_level += 500 + (i * 100);
  48. }
  49. m_exp_for_next_level = 0;
  50. for (int i = 0; i < m_rating + 1; ++i) {
  51. m_exp_for_next_level += 500 + (i * 100);
  52. }
  53. m_name = GameDataReader::ptr()->get_skill_name(m_id);
  54. }
  55. QString Skill::to_string(bool include_level, bool include_exp_summary) const {
  56. GameDataReader *gdr = GameDataReader::ptr();
  57. QString out;
  58. if (include_level)
  59. out.append(QString("[%1] ").arg(m_rating));
  60. QString skill_level = gdr->get_skill_level_name(m_rating);
  61. QString skill_name = gdr->get_skill_name(m_id);
  62. if (skill_level.isEmpty())
  63. out.append(QString("<b>%1</b>").arg(skill_name));
  64. else
  65. out.append(QString("<b>%1 %2</b>").arg(skill_level, skill_name));
  66. if (include_exp_summary)
  67. out.append(QString(" %1").arg(exp_summary()));
  68. return out;
  69. }
  70. bool Skill::operator<(const Skill &s2) const {
  71. return m_rating < s2.m_rating;
  72. }
  73. QString Skill::exp_summary() const {
  74. if (m_rating >= 20) {
  75. return QString("TOTAL: %L1xp").arg(m_actual_exp);
  76. }
  77. float progress = 0.0f;
  78. if (m_exp_for_next_level && m_exp_for_current_level) {
  79. progress = ((float)m_exp / (float)(m_exp_for_next_level - m_exp_for_current_level)) * 100;
  80. }
  81. return QString("%L1xp / %L2xp (%L3%)")
  82. .arg(m_actual_exp)
  83. .arg(m_exp_for_next_level)
  84. .arg(progress, 0, 'f', 1);
  85. }