statistics_dialog.cpp

Go to the documentation of this file.
00001 /* $Id: statistics_dialog.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2006 - 2012 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
00004    wesnoth playturn Copyright (C) 2003 by David White <dave@whitevine.net>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 #include "gettext.hpp"
00018 #include "marked-up_text.hpp"
00019 #include "statistics_dialog.hpp"
00020 #include "unit_types.hpp"
00021 #include "wml_separators.hpp"
00022 #include "game_display.hpp"
00023 
00024 
00025 namespace {
00026 #ifdef LOW_MEM
00027 std::vector<std::string> create_unit_table(const statistics::stats::str_int_map& m, unsigned int /*team*/)
00028 #else
00029 std::vector<std::string> create_unit_table(const statistics::stats::str_int_map& m, unsigned int team)
00030 #endif
00031 {
00032     std::vector<std::string> table;
00033     for(statistics::stats::str_int_map::const_iterator i = m.begin(); i != m.end(); ++i) {
00034         const unit_type *type = unit_types.find(i->first);
00035         if (!type) continue;
00036 
00037         std::stringstream str;
00038 
00039         str << IMAGE_PREFIX << type->image();
00040 #ifndef LOW_MEM
00041         str << "~RC(" << type->flag_rgb() << ">" << team << ")";
00042 #endif
00043         str << COLUMN_SEPARATOR << type->type_name() << COLUMN_SEPARATOR << i->second << "\n";
00044         table.push_back(str.str());
00045     }
00046 
00047     return table;
00048 }
00049 } //end anonymous namespace
00050 
00051 void statistics_dialog::action(gui::dialog_process_info &dp_info)
00052 {
00053     int sel = get_menu().selection();
00054     bool has_details = sel < 5 && sel >= 0 && unit_count_[sel] > 0;
00055     detail_btn_->enable(has_details);
00056     if(dp_info.double_clicked && has_details) {
00057         set_result(sel);
00058     } else if(dp_info.new_key_down && !dp_info.key_down) {
00059         set_result(gui::CLOSE_DIALOG);
00060     }
00061 
00062     // Prepare the sub-dialog for Statistic Details
00063     std::string title;
00064     std::vector<std::string> items_sub;
00065     switch(result()) {
00066     case gui::CLOSE_DIALOG:
00067         break;
00068     case 0:
00069         items_sub = create_unit_table(stats_.recruits, team_num_);
00070         title = _("Recruits");
00071         break;
00072     case 1:
00073         items_sub = create_unit_table(stats_.recalls, team_num_);
00074         title = _("Recalls");
00075         break;
00076     case 2:
00077         items_sub = create_unit_table(stats_.advanced_to, team_num_);
00078         title = _("Advancements");
00079         break;
00080     case 3:
00081         items_sub = create_unit_table(stats_.deaths, team_num_);
00082         title = _("Losses");
00083         break;
00084     case 4:
00085         items_sub = create_unit_table(stats_.killed, team_num_);
00086         /** @todo FIXME? Perhaps killed units shouldn't have the same team-color as your own. */
00087         title = _("Kills");
00088         break;
00089     default:
00090         break;
00091     }
00092     if (items_sub.empty() == false) {
00093         gui::dialog d(get_display(), title + " (" + player_name_ + ")", "", gui::CLOSE_ONLY);
00094         d.set_menu(items_sub);
00095         d.show();
00096         dp_info.clear_buttons();
00097         set_result(gui::CONTINUE_DIALOG);
00098     }
00099 }
00100 
00101 statistics_dialog::statistics_dialog(game_display &disp,
00102         const std::string& title,
00103         const unsigned int team,
00104         const std::string& team_id,
00105         const std::string& player) :
00106     dialog(disp, title, "", gui::NULL_DIALOG),
00107     detail_btn_(new gui::standard_dialog_button(disp.video(), _("Details"), 0 , false)),
00108     player_name_(player),
00109     stats_(),
00110     team_num_(team),
00111     unit_count_(5,0)
00112 {
00113     add_button(detail_btn_, gui::dialog::BUTTON_EXTRA);
00114     add_button(new gui::standard_dialog_button(disp.video(), _("Close"), 1, true),
00115                 gui::dialog::BUTTON_STANDARD);
00116 
00117     stats_ = statistics::calculate_stats(0, team_id);
00118     int n, cost;
00119     std::vector<std::string> items;
00120     // Prepare the menu items
00121     {
00122         std::stringstream str;
00123         n = statistics::sum_str_int_map(stats_.recruits);
00124         cost = stats_.recruit_cost;
00125         unit_count_[0] = n;
00126         str << _("Recruits") << COLUMN_SEPARATOR << n
00127             << COLUMN_SEPARATOR
00128             << COLUMN_SEPARATOR << IMAGE_PREFIX << "themes/gold-t.png"
00129             << COLUMN_SEPARATOR << cost;
00130         items.push_back(str.str());
00131     }
00132     {
00133         std::stringstream str;
00134         n = statistics::sum_str_int_map(stats_.recalls);
00135         cost = stats_.recall_cost;
00136         unit_count_[1] = n;
00137         str << _("Recalls") << COLUMN_SEPARATOR << n
00138             << COLUMN_SEPARATOR
00139             << COLUMN_SEPARATOR << IMAGE_PREFIX << "themes/gold-t.png"
00140             << COLUMN_SEPARATOR << cost;
00141         items.push_back(str.str());
00142     }
00143     {
00144         std::stringstream str;
00145         n = statistics::sum_str_int_map(stats_.advanced_to);
00146         unit_count_[2] = n;
00147         str << _("Advancements") << COLUMN_SEPARATOR << n;
00148         items.push_back(str.str());
00149     }
00150     {
00151         std::stringstream str;
00152         n = statistics::sum_str_int_map(stats_.deaths);
00153         unit_count_[3] = n;
00154         cost = statistics::sum_cost_str_int_map(stats_.deaths);
00155         str << _("Losses") << COLUMN_SEPARATOR << n
00156             << COLUMN_SEPARATOR
00157             << COLUMN_SEPARATOR << IMAGE_PREFIX << "themes/gold-t.png"
00158             << COLUMN_SEPARATOR << cost;
00159         items.push_back(str.str());
00160     }
00161     {
00162         std::stringstream str;
00163         n = statistics::sum_str_int_map(stats_.killed);
00164         unit_count_[4] = n;
00165         cost = statistics::sum_cost_str_int_map(stats_.killed);
00166         str << _("Kills") << COLUMN_SEPARATOR << n
00167             << COLUMN_SEPARATOR
00168             << COLUMN_SEPARATOR << IMAGE_PREFIX << "themes/gold-t.png"
00169             << COLUMN_SEPARATOR << cost;
00170         items.push_back(str.str());
00171     }
00172     items.push_back("");
00173     {
00174         std::stringstream str;
00175         str << font::BOLD_TEXT << _("Damage")
00176             << COLUMN_SEPARATOR << _("Overall") << COLUMN_SEPARATOR
00177             << COLUMN_SEPARATOR
00178             << COLUMN_SEPARATOR << _("This Turn");
00179         items.push_back(str.str());
00180     }
00181 
00182     statistics_dialog::make_damage_line(items, _("Inflicted"),
00183             stats_.damage_inflicted,
00184             stats_.expected_damage_inflicted,
00185             stats_.turn_damage_inflicted,
00186             stats_.turn_expected_damage_inflicted);
00187     statistics_dialog::make_damage_line(items, _("Taken"),
00188             stats_.damage_taken,
00189             stats_.expected_damage_taken,
00190             stats_.turn_damage_taken,
00191             stats_.turn_expected_damage_taken);
00192 
00193     set_menu(items);
00194 }
00195 
00196 statistics_dialog::~statistics_dialog()
00197 {
00198 }
00199 
00200 void statistics_dialog::make_damage_line(std::vector<std::string>& items,
00201                      const std::string& header,
00202                      const long long& damage,
00203                      const long long& expected,
00204                      const long long& turn_damage,
00205                      const long long& turn_expected)
00206 {
00207     int shift = statistics::stats::decimal_shift;
00208 
00209     long long dsa = shift * damage      - expected;
00210     long long dst = shift * turn_damage - turn_expected;
00211 
00212     std::ostringstream str;
00213     str << header << COLUMN_SEPARATOR
00214         << damage << " / "
00215         << (expected * 10 + shift / 2) / shift * 0.1
00216         << COLUMN_SEPARATOR
00217         << ((dsa < 0) ^ (expected < 0) ? "" : "+")
00218         << (expected == 0 ? 0 : 100 * dsa / expected)
00219         << '%' << COLUMN_SEPARATOR
00220         << COLUMN_SEPARATOR
00221         << turn_damage << " / "
00222         << (turn_expected * 10 + shift / 2) / shift * 0.1
00223         << COLUMN_SEPARATOR
00224         << ((dst < 0) ^ (turn_expected < 0) ? "" : "+")
00225         << (turn_expected == 0 ? 0 : 100 * dst / turn_expected)
00226         << '%';
00227     items.push_back(str.str());
00228 
00229 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:10 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs