PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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