Skip to content

Commit

Permalink
Toggling level statistics
Browse files Browse the repository at this point in the history
The coins, badguys and secrets level statistics can now be toggled from a level's settings. By default, coins and secrets are enabled, but badguys are not (due to a recent discussion regarding the difficulty of making it possible to kill all badguys in a level; can be discussed further).

A level is shown as "perfect" on a worldmap if only the enabled statistics are maxed out.

All code which draws statistics has been updated to incorporate this change as well. Statistics which are turned off will not be shown alongside others.
  • Loading branch information
Vankata453 committed Sep 20, 2024
1 parent c754f1c commit c907a08
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 149 deletions.
11 changes: 9 additions & 2 deletions src/supertux/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ Level::~Level()
}

void
Level::initialize()
Level::initialize(const Statistics::Preferences& stat_preferences)
{
if (m_sectors.empty())
throw std::runtime_error("Level has no sectors!");

m_stats.init(*this);
m_stats.init(*this, stat_preferences);

Savegame* savegame = (GameSession::current() && !Editor::current() ?
&GameSession::current()->get_savegame() : nullptr);
Expand Down Expand Up @@ -195,6 +195,13 @@ Level::save(Writer& writer)
if (!m_wmselect_bkg.empty())
writer.write("bkg", m_wmselect_bkg);

if (!m_is_worldmap)
{
writer.start_list("statistics");
m_stats.get_preferences().write(writer);
writer.end_list("statistics");
}

for (auto& sector : m_sectors) {
sector->save(writer);
}
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Level final
const std::string& get_license() const { return m_license; }

private:
void initialize();
void initialize(const Statistics::Preferences& stat_preferences);

void save(Writer& writer);
void load_old_format(const ReaderMapping& reader);
Expand Down
20 changes: 14 additions & 6 deletions src/supertux/level_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ LevelParser::load(const ReaderDocument& doc)
throw std::runtime_error("file is not a supertux-level file.");

auto level = root.get_mapping();
Statistics::Preferences stat_preferences;

int version = 1;
level.get("version", version);
if (version == 1) {
if (version == 1)
{
log_info << "[" << doc.get_filename() << "] level uses old format: version 1" << std::endl;
load_old_format(level);
} else if (version == 2 || version == 3) {
}
else if (version == 2 || version == 3)
{
level.get("tileset", m_level.m_tileset);

level.get("name", m_level.m_name);
Expand All @@ -173,6 +177,10 @@ LevelParser::load(const ReaderDocument& doc)
level.get("icon-locked", m_level.m_icon_locked);
level.get("bkg", m_level.m_wmselect_bkg);

std::optional<ReaderMapping> level_stat_preferences;
if (level.get("statistics", level_stat_preferences))
stat_preferences.parse(*level_stat_preferences);

auto iter = level.get_iter();
while (iter.next())
{
Expand All @@ -189,11 +197,13 @@ LevelParser::load(const ReaderDocument& doc)
<< m_level.m_name << "\". You might not be allowed to share it."
<< std::endl;
}
} else {
}
else
{
log_warning << "[" << doc.get_filename() << "] level format version " << version << " is not supported" << std::endl;
}

m_level.initialize();
m_level.initialize(stat_preferences);
}

void
Expand All @@ -204,8 +214,6 @@ LevelParser::load_old_format(const ReaderMapping& reader)

auto sector = SectorParser::from_reader_old_format(m_level, reader, m_editable);
m_level.add_sector(std::move(sector));

m_level.initialize();
}

void
Expand Down
28 changes: 19 additions & 9 deletions src/supertux/levelintro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,25 @@ LevelIntro::draw(Compositor& compositor)

py += static_cast<int>(Resources::normal_font->get_height());

draw_stats_line(context, py, _("Coins"),
Statistics::coins_to_string(m_best_level_statistics->get_coins(), stats.m_total_coins),
m_best_level_statistics->get_coins() >= stats.m_total_coins);
draw_stats_line(context, py, _("Badguys killed"),
Statistics::frags_to_string(m_best_level_statistics->get_badguys(), stats.m_total_badguys),
m_best_level_statistics->get_badguys() >= stats.m_total_badguys);
draw_stats_line(context, py, _("Secrets"),
Statistics::secrets_to_string(m_best_level_statistics->get_secrets(), stats.m_total_secrets),
m_best_level_statistics->get_secrets() >= stats.m_total_secrets);
const Statistics::Preferences& preferences = m_level.m_stats.get_preferences();
if (preferences.enable_coins)
{
draw_stats_line(context, py, _("Coins"),
Statistics::coins_to_string(m_best_level_statistics->get_coins(), stats.m_total_coins),
m_best_level_statistics->get_coins() >= stats.m_total_coins);
}
if (preferences.enable_badguys)
{
draw_stats_line(context, py, _("Badguys killed"),
Statistics::frags_to_string(m_best_level_statistics->get_badguys(), stats.m_total_badguys),
m_best_level_statistics->get_badguys() >= stats.m_total_badguys);
}
if (preferences.enable_secrets)
{
draw_stats_line(context, py, _("Secrets"),
Statistics::secrets_to_string(m_best_level_statistics->get_secrets(), stats.m_total_secrets),
m_best_level_statistics->get_secrets() >= stats.m_total_secrets);
}

bool targetTimeBeaten = m_level.m_target_time == 0.0f || (m_best_level_statistics->get_time() != 0.0f && m_best_level_statistics->get_time() < m_level.m_target_time);
draw_stats_line(context, py, _("Best time"),
Expand Down
6 changes: 5 additions & 1 deletion src/supertux/menu/editor_level_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ EditorLevelMenu::EditorLevelMenu() :
add_textfield(_("Level Note"), &(level->m_note));
add_file(_("Tileset"), &(level->m_tileset), std::vector<std::string>(1, ".strf"), {}, true);

if (!is_worldmap) {
if (!is_worldmap)
{
add_floatfield(_("Target Time"), &(level->m_target_time));

add_hl();
level->m_stats.add_preferences_to_menu(*this);
}

add_hl();
Expand Down
Loading

0 comments on commit c907a08

Please sign in to comment.