Several misc cleanups

This commit is contained in:
Charles Dang 2021-01-29 13:22:21 +11:00
parent 6260a48e40
commit c97d2cb6fb
5 changed files with 47 additions and 73 deletions

View file

@ -23,8 +23,6 @@
#include "preferences/advanced.hpp"
#include "preferences/game.hpp"
#include <boost/dynamic_bitset.hpp>
// This file is not named preferences.hpp in order -I conflicts with
// src/preferences.hpp.

View file

@ -16,14 +16,10 @@
#include "gui/core/widget_definition.hpp"
#include "gui/core/window_builder.hpp"
#include "gui/dialogs/drop_down_menu.hpp"
#include "gui/widgets/styled_widget.hpp"
#include "gui/widgets/selectable_item.hpp"
#include <boost/dynamic_bitset.hpp>
class config;
namespace gui2
@ -37,24 +33,24 @@ namespace implementation
/**
* @ingroup GUIWidgetWML
*
*
* A menu_button is a styled_widget to choose an element from a list of elements.
*
*
* When a menu_button has a return value it sets the return value for the window.
* Normally this closes the window and returns this value to the caller.
* The return value can either be defined by the user or determined from the id of the menu_button.
* The return value has a higher precedence as the one defined by the id.
* (Of course it's weird to give a menu_button an id and then override its return value.)
*
*
* When the menu_button doesn't have a standard id, but you still want to use the return value of that id, use return_value_id instead.
* This has a higher precedence as return_value.
*
*
* List with the menu_button specific variables:
* Key |Type |Default |Description
* Key |Type |Default |Description
* ---------------|------------------------------------|---------|-----------
* return_value_id| @ref guivartype_string "string" |"" |The return value id.
* return_value | @ref guivartype_int "int" |0 |The return value.
*
*
* The following states exist:
* * state_enabled - the menu_button is enabled.
* * state_disabled - the menu_button is disabled.

View file

@ -153,34 +153,34 @@ namespace { // Helpers for get_tiles_radius() without a radius filter.
{
// This should help optimize the insertions (since we will be
// processing hexes in their lexicographical order).
std::set<map_location>::const_iterator insert_hint = result.begin();
// Note: This hint will get incremented later, which is the only
// reason we require result to be initially non-empty.
auto insert_hint = result.begin();
for (const column_ranges::value_type & column : collected_tiles)
{
for(const auto& [column, range] : collected_tiles) {
// For this loop, the order within the set is crucial; we need
// rows.first to be non-decreasing with each iteration.
// Loop invariant: within this column, all rows before next_row
// have been processed and either added to result or skipped.
// There is no going back (nor a need to).
int next_row = row_begin;
for (const row_range &rows : column.second)
{
for(const auto& [row_index, num_rows] : range) {
// Skipping some rows?
if ( next_row < rows.first )
next_row = rows.first;
if(next_row < row_index) {
next_row = row_index;
}
// Add this range of hexes.
const int end = std::min(rows.first + static_cast<int>(rows.second),
row_end);
for ( ; next_row < end; ++next_row )
insert_hint = result.insert(++insert_hint,
map_location(column.first, next_row));
const int end = std::min(row_index + static_cast<int>(num_rows), row_end);
for(; next_row < end; ++next_row) {
insert_hint = result.insert(++insert_hint, map_location(column, next_row));
}
// Have we reached the end of the board?
if ( next_row >= row_end )
if(next_row >= row_end) {
break;
}
}
}
}

View file

@ -402,32 +402,35 @@ static config unit_abilities(const unit* u, const map_location& loc)
config res;
boost::dynamic_bitset<> active;
const std::vector<std::tuple<std::string, t_string,t_string,t_string>> &abilities = u->ability_tooltips(active, loc);
const auto abilities = u->ability_tooltips(active, loc);
const std::size_t abilities_size = abilities.size();
for ( std::size_t i = 0; i != abilities_size; ++i )
{
for(std::size_t i = 0; i != abilities_size; ++i) {
// Aliases for readability:
const std::string& id = std::get<0>(abilities[i]);
const std::string& base_name = std::get<1>(abilities[i]).base_str();
const t_string& display_name = std::get<2>(abilities[i]);
const t_string& description = std::get<3>(abilities[i]);
const auto& [id, base_name, display_name, description] = abilities[i];
std::ostringstream str, tooltip;
if ( active[i] )
if(active[i]) {
str << display_name;
else
} else {
str << span_color(font::inactive_ability_color) << display_name << naps;
if ( i + 1 != abilities_size )
}
if(i + 1 != abilities_size) {
str << ", ";
}
tooltip << _("Ability: ") << "<b>" << display_name << "</b>";
if ( !active[i] )
if(!active[i]) {
tooltip << "<i>" << _(" (inactive)") << "</i>";
}
tooltip << '\n' << description;
add_text(res, str.str(), tooltip.str(), "ability_" + id + base_name);
add_text(res, str.str(), tooltip.str(), "ability_" + id + base_name.base_str());
}
return res;
}
REPORT_GENERATOR(unit_abilities, rc)

View file

@ -50,27 +50,16 @@
#include "units/id.hpp"
#include "units/map.hpp" // for unit_map, etc
#include "units/types.hpp"
#include <functional>
#include "variable.hpp" // for vconfig, etc
#include <boost/dynamic_bitset.hpp>
#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4510 4610)
#endif
#include <boost/range/algorithm.hpp>
#ifdef _MSC_VER
#pragma warning (pop)
#endif
#include <array>
#include <cassert> // for assert
#include <cstdlib> // for rand
#include <exception> // for exception
#include <functional>
#include <iterator> // for back_insert_iterator, etc
#include <new> // for operator new
#include <ostream> // for operator<<, basic_ostream, etc
#include <string_view>
namespace t_translation { struct terrain_code; }
@ -83,7 +72,7 @@ static lg::log_domain log_unit("unit");
namespace
{
// "advance" only kept around for backwards compatibility; only "advancement" should be used
const std::array<std::string, 4> ModificationTypes {{ "advancement", "advance", "trait", "object" }};
const std::set<std::string_view> ModificationTypes { "advancement", "advance", "trait", "object" };
/**
* Pointers to units which have data in their internal caches. The
@ -93,7 +82,7 @@ namespace
static std::vector<const unit*> units_with_cache;
static const std::string leader_crown_path = "misc/leader-crown.png";
static std::array<std::string, 60> internalized_attrs {{
static const std::set<std::string_view> internalized_attrs {
"type",
"id",
"name",
@ -155,19 +144,8 @@ namespace
"language_name",
"image",
"image_icon"
}};
struct internalized_attrs_sorter
{
internalized_attrs_sorter()
{
std::sort(std::begin(internalized_attrs), std::end(internalized_attrs));
}
};
// Sort the array to make set_difference below work.
internalized_attrs_sorter sorter;
void warn_unknown_attribute(const config::const_attr_itors& cfg)
{
config::const_attribute_iterator cur = cfg.begin();
@ -399,7 +377,6 @@ unit::unit(unit_ctor_t)
, changed_attributes_(0)
, invisibility_cache_()
{
}
void unit::init(const config& cfg, bool use_traits, const vconfig* vcfg)
@ -2654,7 +2631,7 @@ std::string get_checksum(const unit& u)
config wcfg;
u.write(unit_config);
const std::array<std::string, 22> main_keys {{
static const std::set<std::string_view> main_keys {
"advances_to",
"alignment",
"cost",
@ -2677,24 +2654,24 @@ std::string get_checksum(const unit& u)
"undead_variation",
"upkeep",
"zoc"
}};
};
for(const std::string& main_key : main_keys) {
for(const std::string_view& main_key : main_keys) {
wcfg[main_key] = unit_config[main_key];
}
const std::array<std::string, 5> attack_keys {{
static const std::set<std::string_view> attack_keys {
"name",
"type",
"range",
"damage",
"number"
}};
};
for(const config& att : unit_config.child_range("attack")) {
config& child = wcfg.add_child("attack");
for(const std::string& attack_key : attack_keys) {
for(const std::string_view& attack_key : attack_keys) {
child[attack_key] = att[attack_key];
}
@ -2723,16 +2700,16 @@ std::string get_checksum(const unit& u)
child.recursive_clear_value("name");
}
const std::array<std::string, 6> child_keys {{
static const std::set<std::string_view> child_keys {
"advance_from",
"defense",
"movement_costs",
"vision_costs",
"jamming_costs",
"resistance"
}};
};
for(const std::string& child_key : child_keys) {
for(const std::string_view& child_key : child_keys) {
for(const config& c : unit_config.child_range(child_key)) {
wcfg.add_child(child_key, c);
}