The Battle for Wesnoth  1.19.0-dev
carryover_show_gold.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "carryover_show_gold.hpp"
17 
18 #include "log.hpp"
19 #include "team.hpp"
20 #include "game_state.hpp"
21 #include "gettext.hpp"
23 #include "formula/string_utils.hpp"
24 #include "map/map.hpp"
25 #include <cassert>
26 
27 static lg::log_domain log_engine("engine");
28 #define LOG_NG LOG_STREAM(info, log_engine)
29 #define ERR_NG LOG_STREAM(err, log_engine)
30 
31 
32 void carryover_show_gold(game_state& state, bool hidden, bool is_observer, bool is_test)
33 {
34  assert(state.end_level_data_);
35  game_board& board = state.board_;
36  const end_level_data& end_level = *state.end_level_data_;
37  const bool is_victory = end_level.is_victory;
38  // We need to write the carryover amount to the team that's why we need non const
39  std::vector<team>& teams = board.teams();
40 
41  // maybe this can be the case for scenario that only contain a story and end during the prestart event ?
42  if(teams.size() < 1) {
43  return;
44  }
45 
46  std::ostringstream report;
47  std::string title;
48 
49 
50  if(is_observer) {
51  title = _("Scenario Report");
52  } else if(is_victory) {
53  title = _("Victory");
54  report << "<b>" << _("You have emerged victorious!") << "</b>";
55  } else {
56  title = _("Defeat");
57  report << _("You have been defeated!");
58  }
59 
60  const std::string& next_scenario = state.get_game_data()->next_scenario();
61  const bool has_next_scenario = !next_scenario.empty() && next_scenario != "null";
62 
63  int persistent_teams = 0;
64  for(const team& t : teams) {
65  if(t.persistent()) {
66  ++persistent_teams;
67  }
68  }
69 
70  if(persistent_teams > 0 && ((has_next_scenario && end_level.proceed_to_next_level) || is_test)) {
71  const gamemap& map = board.map();
72  const tod_manager& tod = state.get_tod_man();
73 
74  const int turns_left = std::max<int>(0, tod.number_of_turns() - tod.turn());
75  for(team& t : teams) {
76  if(!t.persistent() || t.lost()) {
77  continue;
78  }
79 
80  const int finishing_bonus_per_turn = map.villages().size() * t.village_gold() + t.base_income();
81  const int finishing_bonus = t.carryover_bonus() * finishing_bonus_per_turn * turns_left;
82 
83  t.set_carryover_gold(div100rounded((t.gold() + finishing_bonus) * t.carryover_percentage()));
84 
85  if(!t.is_local_human()) {
86  continue;
87  }
88 
89  if(persistent_teams > 1) {
90  report << "\n\n<b>" << t.side_name() << "</b>";
91  }
92 
93  report << "<small>\n" << _("Remaining gold: ") << utils::half_signed_value(t.gold()) << "</small>";
94 
95  if(t.carryover_bonus() != 0) {
96  if(turns_left > -1) {
97  report << "\n\n<b>" << _("Turns finished early: ") << turns_left << "</b>\n"
98  << "<small>" << _("Early finish bonus: ") << finishing_bonus_per_turn << _(" per turn") << "</small>\n"
99  << "<small>" << _("Total bonus: ") << finishing_bonus << "</small>\n";
100  }
101 
102  report << "<small>" << _("Total gold: ") << utils::half_signed_value(t.gold() + finishing_bonus) << "</small>";
103  }
104 
105  if(t.gold() > 0) {
106  report << "\n<small>" << _("Carryover percentage: ") << t.carryover_percentage() << "</small>";
107  }
108 
109  if(t.carryover_add()) {
110  report << "\n\n<big><b>" << _("Bonus gold: ") << utils::half_signed_value(t.carryover_gold()) << "</b></big>";
111  } else {
112  report << "\n\n<big><b>" << _("Retained gold: ") << utils::half_signed_value(t.carryover_gold()) << "</b></big>";
113  }
114 
115  std::string goldmsg;
116  utils::string_map symbols;
117 
118  symbols["gold"] = lexical_cast_default<std::string>(t.carryover_gold());
119 
120  // Note that both strings are the same in English, but some languages will
121  // want to translate them differently.
122  if(t.carryover_add()) {
123  if(t.carryover_gold() > 0) {
124  goldmsg = VNGETTEXT(
125  "You will start the next scenario with $gold on top of the defined minimum starting gold.",
126  "You will start the next scenario with $gold on top of the defined minimum starting gold.",
127  t.carryover_gold(), symbols
128  );
129 
130  } else {
131  goldmsg = VNGETTEXT(
132  "You will start the next scenario with the defined minimum starting gold.",
133  "You will start the next scenario with the defined minimum starting gold.",
134  t.carryover_gold(), symbols
135  );
136  }
137  } else {
138  goldmsg = VNGETTEXT(
139  "You will start the next scenario with $gold or its defined minimum starting gold, "
140  "whichever is higher.",
141  "You will start the next scenario with $gold or its defined minimum starting gold, "
142  "whichever is higher.",
143  t.carryover_gold(), symbols
144  );
145  }
146 
147  // xgettext:no-c-format
148  report << "\n" << goldmsg;
149  }
150  }
151 
152  if(end_level.transient.carryover_report && !hidden) {
153  gui2::show_transient_message(title, report.str(), "", true);
154  }
155 }
double t
Definition: astarsearch.cpp:63
static lg::log_domain log_engine("engine")
void carryover_show_gold(game_state &state, bool hidden, bool is_observer, bool is_test)
calculates the amount of gold carried over for each team, stores the data in the team object and show...
Game board class.
Definition: game_board.hpp:46
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:79
virtual const gamemap & map() const override
Definition: game_board.hpp:96
const std::string & next_scenario() const
Definition: game_data.hpp:130
std::optional< end_level_data > end_level_data_
Definition: game_state.hpp:64
virtual const game_data * get_game_data() const override
Inherited from filter_context.
Definition: game_state.hpp:97
virtual const tod_manager & get_tod_man() const override
Inherited from filter_context.
Definition: game_state.hpp:91
game_board board_
Definition: game_state.hpp:44
Encapsulates the map of the game.
Definition: map.hpp:172
const std::vector< map_location > & villages() const
Return a list of the locations of villages on the map.
Definition: map.hpp:237
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
int number_of_turns() const
int turn() const
#define VNGETTEXT(msgid, msgid_plural, count,...)
static std::string _(const char *str)
Definition: gettext.hpp:93
Standard logging facilities (interface).
constexpr int div100rounded(int num)
Guarantees portable results for division by 100; round half up, to the nearest integer.
Definition: math.hpp:39
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup)
Shows a transient message to the user.
std::string half_signed_value(int val)
Sign with Unicode "−" if negative.
std::map< std::string, t_string > string_map
int turns_left
Definition: pathfind.cpp:156
Additional information on the game outcome which can be provided by WML.
bool proceed_to_next_level
whether to proceed to the next scenario, equals is_victory in sp.
transient_end_level transient
bool carryover_report
Should a summary of the scenario outcome be displayed?