From ff0f22ef44d5e06671d2dc2cb8990fcb3760f935 Mon Sep 17 00:00:00 2001 From: tomangelo2 Date: Wed, 7 Mar 2018 15:46:30 +0100 Subject: [PATCH 1/4] Sort scoreboard First, the team with more points, then which team scored points faster --- src/level/robotmain.cpp | 19 ++++++++++++++----- src/level/scoreboard.cpp | 7 ++++--- src/level/scoreboard.h | 14 ++++++++++++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index 3df62fe5..a19879af 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -4963,7 +4963,7 @@ Error CRobotMain::ProcessEndMissionTake() { if (!details.empty()) details += ", "; - details += StrUtils::Format(details_line.c_str(), GetTeamName(team).c_str(), m_scoreboard->GetScore(team)); + details += StrUtils::Format(details_line.c_str(), GetTeamName(team).c_str(), m_scoreboard->GetScore(team).points); } m_ui->GetDialog()->StartInformation( title, @@ -5870,7 +5870,7 @@ void CRobotMain::CreateCodeBattleInterface() : static_cast(pw->CreateEdit( pos, ddim, 0, static_cast(EVENT_SCOREBOARD+2*(numTeams-i-1)+1))); pl->SetTextAlign(Gfx::TEXT_ALIGN_RIGHT); pl->SetFontSize(m_codeBattleStarted ? Gfx::FONT_SIZE_SMALL : Gfx::FONT_SIZE_SMALL*0.75f); - m_codeBattleStarted ? pl->SetName(StrUtils::ToString(m_scoreboard->GetScore(team))) : static_cast(pl)->SetText(StrUtils::ToString(m_scoreboard->GetScore(team))); + m_codeBattleStarted ? pl->SetName(StrUtils::ToString(m_scoreboard->GetScore(team).points)) : static_cast(pl)->SetText(StrUtils::ToString(m_scoreboard->GetScore(team).points)); pos.x -= 57.5f/640.0f; pos.y += ddim.y; i++; @@ -5910,9 +5910,18 @@ void CRobotMain::UpdateCodeBattleInterface() Ui::CWindow* pw = static_cast(m_interface->SearchControl(EVENT_WINDOW6)); assert(pw != nullptr); - + std::set teams = GetAllTeams(); + std::vector sortedTeams(teams.begin(), teams.end()); + std::sort(sortedTeams.begin(), sortedTeams.end(), [this](int teamA, int teamB) + { + if (m_scoreboard->GetScore(teamA).points > m_scoreboard->GetScore(teamB).points) return true; //Team A have more points than B? + if (m_scoreboard->GetScore(teamA).points < m_scoreboard->GetScore(teamB).points) return false; //Team A have less points than B? + + if (m_scoreboard->GetScore(teamA).time < m_scoreboard->GetScore(teamB).time) return true; //Team A scored faster than B? + else return false; //Team A scored slower than B? + }); int i = 0; - for (int team : GetAllTeams()) + for (int team : sortedTeams) { Ui::CControl* pl; @@ -5922,7 +5931,7 @@ void CRobotMain::UpdateCodeBattleInterface() pl = pw->SearchControl(static_cast(EVENT_SCOREBOARD+2*i+1)); assert(pl != nullptr); - pl->SetName(StrUtils::ToString(m_scoreboard->GetScore(team))); + pl->SetName(StrUtils::ToString(m_scoreboard->GetScore(team).points)); i++; } diff --git a/src/level/scoreboard.cpp b/src/level/scoreboard.cpp index 7e4b5b04..83b74fe1 100644 --- a/src/level/scoreboard.cpp +++ b/src/level/scoreboard.cpp @@ -99,15 +99,16 @@ void CScoreboard::AddPoints(int team, int points) text = StrUtils::Format(text.c_str(), main->GetTeamName(team).c_str(), points); main->GetDisplayText()->DisplayText(text.c_str(), Math::Vector(0.0f,0.0f,0.0f), 15.0f, 60.0f, 10.0f, Ui::TT_WARNING); - m_score[team] += points; + m_score[team].points += points; + m_score[team].time = main->GetGameTime(); } -int CScoreboard::GetScore(int team) +Score CScoreboard::GetScore(int team) { return m_score[team]; } void CScoreboard::SetScore(int team, int points) { - m_score[team] = points; + m_score[team].points = points; } diff --git a/src/level/scoreboard.h b/src/level/scoreboard.h index 58a5acb5..97477e8b 100644 --- a/src/level/scoreboard.h +++ b/src/level/scoreboard.h @@ -32,6 +32,16 @@ class CObject; +/** + * \struct Score + * \brief Struct containing score of individual team and additional variables to allow sorting teams through different criteria +*/ +struct Score +{ + int points = 0; //! Team score + float time = 0; //! Time when points were scored +}; + /** * \class CScoreboard * \brief Scoreboard used to score complex code battles @@ -114,12 +124,12 @@ public: void ProcessEndTake(int team); void AddPoints(int team, int points); - int GetScore(int team); + Score GetScore(int team); void SetScore(int team, int score); private: std::vector> m_rulesKill = {}; std::vector> m_rulesEndTake = {}; - std::map m_score; + std::map m_score; int m_finishCounter = 0; }; \ No newline at end of file From 1b79e8409f5a1b6d4f050730dce2110e1bc32be3 Mon Sep 17 00:00:00 2001 From: tomangelo2 Date: Wed, 7 Mar 2018 21:25:35 +0100 Subject: [PATCH 2/4] Add switch to determine sort type You can set it in scene file with ScoreboardSortType SortBy="Name" or "Points" --- src/level/robotmain.cpp | 36 ++++++++++++++++++++++++++++-------- src/level/scoreboard.cpp | 10 ++++++++++ src/level/scoreboard.h | 13 ++++++++++++- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index a19879af..d12d8af5 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -3563,6 +3563,22 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) } continue; } + + if (line->GetCommand() == "ScoreboardSortType" && !resetObject) + { + if (line->GetParam("SortBy")->AsString() == "Points") + { + // Sort teams by points + m_scoreboard->SetSortType(SORT_POINTS); + } + else if (line->GetParam("SortBy")->AsString() == "Name") + { + // Sort teams alphabetically + m_scoreboard->SetSortType(SORT_ID); + } + continue; + } + if (line->GetCommand() == "ScoreboardKillRule" && !resetObject) { if (!m_scoreboard) @@ -5912,14 +5928,18 @@ void CRobotMain::UpdateCodeBattleInterface() assert(pw != nullptr); std::set teams = GetAllTeams(); std::vector sortedTeams(teams.begin(), teams.end()); - std::sort(sortedTeams.begin(), sortedTeams.end(), [this](int teamA, int teamB) - { - if (m_scoreboard->GetScore(teamA).points > m_scoreboard->GetScore(teamB).points) return true; //Team A have more points than B? - if (m_scoreboard->GetScore(teamA).points < m_scoreboard->GetScore(teamB).points) return false; //Team A have less points than B? - - if (m_scoreboard->GetScore(teamA).time < m_scoreboard->GetScore(teamB).time) return true; //Team A scored faster than B? - else return false; //Team A scored slower than B? - }); + if(m_scoreboard->GetSortType() == SORT_POINTS) + { + std::sort(sortedTeams.begin(), sortedTeams.end(), [this](int teamA, int teamB) + { + if (m_scoreboard->GetScore(teamA).points > m_scoreboard->GetScore(teamB).points) return true; //Team A have more points than B? + if (m_scoreboard->GetScore(teamA).points < m_scoreboard->GetScore(teamB).points) return false; //Team A have less points than B? + + if (m_scoreboard->GetScore(teamA).time < m_scoreboard->GetScore(teamB).time) return true; //Team A scored faster than B? + else return false; //Team A scored slower than B? + }); + } + int i = 0; for (int team : sortedTeams) { diff --git a/src/level/scoreboard.cpp b/src/level/scoreboard.cpp index 83b74fe1..8db62ff8 100644 --- a/src/level/scoreboard.cpp +++ b/src/level/scoreboard.cpp @@ -112,3 +112,13 @@ void CScoreboard::SetScore(int team, int points) { m_score[team].points = points; } + +SortType CScoreboard::GetSortType() +{ + return m_sorttype; +} + +void CScoreboard::SetSortType(SortType type) +{ + m_sorttype = type; +} diff --git a/src/level/scoreboard.h b/src/level/scoreboard.h index 97477e8b..f691e176 100644 --- a/src/level/scoreboard.h +++ b/src/level/scoreboard.h @@ -42,6 +42,12 @@ struct Score float time = 0; //! Time when points were scored }; +enum SortType +{ + SORT_ID, //Sort by team ID + SORT_POINTS, //Sort by points +}; + /** * \class CScoreboard * \brief Scoreboard used to score complex code battles @@ -55,6 +61,7 @@ struct Score * \section example Usage example * \code{.scene} * Scoreboard enable=true // enable the scoreboard + * ScoreboardSortType SortBy="Name" // sort teams alphabetically, another option is SortBy="Points", sorting teams in order of points * ScoreboardKillRule type=WheeledShooter team=1 score=500 // destruction of team 1's WheeledShooter gives 100 points to the team that destroyed it * ScoreboardKillRule type=TargetBot score=100 // destruction of TargetBot (any team) gives 100 points * ScoreboardEndTakeRule score=1000 // completion of EndMissionTake objectives for any team results in 1000 points for that team @@ -127,9 +134,13 @@ public: Score GetScore(int team); void SetScore(int team, int score); + SortType GetSortType(); + void SetSortType(SortType type); + private: std::vector> m_rulesKill = {}; std::vector> m_rulesEndTake = {}; std::map m_score; int m_finishCounter = 0; -}; \ No newline at end of file + SortType m_sorttype = SORT_ID; +}; From d371338920b3c00303c2d211ebdce0c7da40f4a4 Mon Sep 17 00:00:00 2001 From: tomangelo2 Date: Sun, 11 Mar 2018 17:00:17 +0100 Subject: [PATCH 3/4] Fix scoreboard sorting parameters --- src/level/parser/parserparam.cpp | 21 +++++++++++++++++++++ src/level/parser/parserparam.h | 3 +++ src/level/robotmain.cpp | 11 +---------- src/level/scoreboard.h | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/level/parser/parserparam.cpp b/src/level/parser/parserparam.cpp index 37773efa..3ccd7f9c 100644 --- a/src/level/parser/parserparam.cpp +++ b/src/level/parser/parserparam.cpp @@ -28,6 +28,7 @@ #include "common/resources/resourcemanager.h" #include "level/robotmain.h" +#include "level/scoreboard.h" #include "level/parser/parser.h" @@ -916,6 +917,26 @@ int CLevelParserParam::AsResearchFlag(int def) return AsResearchFlag(); } +int CLevelParserParam::ToSortType(std::string value) +{ + if (value == "Points" ) return SORT_POINTS; + if (value == "Name" ) return SORT_ID; + return Cast(value, "sorttype"); +} + +int CLevelParserParam::AsSortType() +{ + if (m_empty) + throw CLevelParserExceptionMissingParam(this); + return ToSortType(m_value); +} + +int CLevelParserParam::AsSortType(int def) +{ + if (m_empty) + return def; + return AsSortType(); +} Gfx::PyroType CLevelParserParam::ToPyroType(std::string value) { diff --git a/src/level/parser/parserparam.h b/src/level/parser/parserparam.h index c72be4f0..1261bb65 100644 --- a/src/level/parser/parserparam.h +++ b/src/level/parser/parserparam.h @@ -85,6 +85,7 @@ public: Gfx::EngineObjectType AsTerrainType(); int AsBuildFlag(); int AsResearchFlag(); + int AsSortType(); Gfx::PyroType AsPyroType(); Gfx::CameraType AsCameraType(); MissionType AsMissionType(); @@ -108,6 +109,7 @@ public: Gfx::EngineObjectType AsTerrainType(Gfx::EngineObjectType def); int AsBuildFlag(int def); int AsResearchFlag(int def); + int AsSortType(int def); Gfx::PyroType AsPyroType(Gfx::PyroType def); Gfx::CameraType AsCameraType(Gfx::CameraType def); MissionType AsMissionType(MissionType def); @@ -139,6 +141,7 @@ private: Gfx::EngineObjectType ToTerrainType(std::string value); int ToBuildFlag(std::string value); int ToResearchFlag(std::string value); + int ToSortType(std::string value); Gfx::PyroType ToPyroType(std::string value); Gfx::CameraType ToCameraType(std::string value); MissionType ToMissionType(std::string value); diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index d12d8af5..eed48aae 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -3566,16 +3566,7 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) if (line->GetCommand() == "ScoreboardSortType" && !resetObject) { - if (line->GetParam("SortBy")->AsString() == "Points") - { - // Sort teams by points - m_scoreboard->SetSortType(SORT_POINTS); - } - else if (line->GetParam("SortBy")->AsString() == "Name") - { - // Sort teams alphabetically - m_scoreboard->SetSortType(SORT_ID); - } + m_scoreboard->SetSortType(static_cast(line->GetParam("sort")->AsSortType() ) ); continue; } diff --git a/src/level/scoreboard.h b/src/level/scoreboard.h index f691e176..cd964de3 100644 --- a/src/level/scoreboard.h +++ b/src/level/scoreboard.h @@ -61,7 +61,7 @@ enum SortType * \section example Usage example * \code{.scene} * Scoreboard enable=true // enable the scoreboard - * ScoreboardSortType SortBy="Name" // sort teams alphabetically, another option is SortBy="Points", sorting teams in order of points + * ScoreboardSortType sort=Name // sort teams alphabetically, another option is sort=Points, sorting teams in order of points * ScoreboardKillRule type=WheeledShooter team=1 score=500 // destruction of team 1's WheeledShooter gives 100 points to the team that destroyed it * ScoreboardKillRule type=TargetBot score=100 // destruction of TargetBot (any team) gives 100 points * ScoreboardEndTakeRule score=1000 // completion of EndMissionTake objectives for any team results in 1000 points for that team From ad6dd002753925c0bc6ed2dbdc9076be4e7700a9 Mon Sep 17 00:00:00 2001 From: tomangelo2 Date: Sun, 8 Apr 2018 23:43:22 +0200 Subject: [PATCH 4/4] Change new functions return type and switch to enum class As suggested by @krzys_h --- src/level/parser/parserparam.cpp | 12 ++++++------ src/level/parser/parserparam.h | 8 +++++--- src/level/robotmain.cpp | 2 +- src/level/scoreboard.h | 4 ++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/level/parser/parserparam.cpp b/src/level/parser/parserparam.cpp index 3ccd7f9c..479f7cec 100644 --- a/src/level/parser/parserparam.cpp +++ b/src/level/parser/parserparam.cpp @@ -917,21 +917,21 @@ int CLevelParserParam::AsResearchFlag(int def) return AsResearchFlag(); } -int CLevelParserParam::ToSortType(std::string value) +SortType CLevelParserParam::ToSortType(std::string value) { - if (value == "Points" ) return SORT_POINTS; - if (value == "Name" ) return SORT_ID; - return Cast(value, "sorttype"); + if (value == "Points" ) return SortType::SORT_POINTS; + if (value == "Name" ) return SortType::SORT_ID; + return SortType::SORT_ID; } -int CLevelParserParam::AsSortType() +SortType CLevelParserParam::AsSortType() { if (m_empty) throw CLevelParserExceptionMissingParam(this); return ToSortType(m_value); } -int CLevelParserParam::AsSortType(int def) +SortType CLevelParserParam::AsSortType(SortType def) { if (m_empty) return def; diff --git a/src/level/parser/parserparam.h b/src/level/parser/parserparam.h index 1261bb65..b5af93d4 100644 --- a/src/level/parser/parserparam.h +++ b/src/level/parser/parserparam.h @@ -31,6 +31,8 @@ #include "graphics/engine/pyro_type.h" #include "graphics/engine/water.h" +#include "level/scoreboard.h" + #include "math/point.h" #include "object/drive_type.h" @@ -85,7 +87,7 @@ public: Gfx::EngineObjectType AsTerrainType(); int AsBuildFlag(); int AsResearchFlag(); - int AsSortType(); + SortType AsSortType(); Gfx::PyroType AsPyroType(); Gfx::CameraType AsCameraType(); MissionType AsMissionType(); @@ -109,7 +111,7 @@ public: Gfx::EngineObjectType AsTerrainType(Gfx::EngineObjectType def); int AsBuildFlag(int def); int AsResearchFlag(int def); - int AsSortType(int def); + SortType AsSortType(SortType def); Gfx::PyroType AsPyroType(Gfx::PyroType def); Gfx::CameraType AsCameraType(Gfx::CameraType def); MissionType AsMissionType(MissionType def); @@ -141,7 +143,7 @@ private: Gfx::EngineObjectType ToTerrainType(std::string value); int ToBuildFlag(std::string value); int ToResearchFlag(std::string value); - int ToSortType(std::string value); + SortType ToSortType(std::string value); Gfx::PyroType ToPyroType(std::string value); Gfx::CameraType ToCameraType(std::string value); MissionType ToMissionType(std::string value); diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index eed48aae..ad37c611 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -5919,7 +5919,7 @@ void CRobotMain::UpdateCodeBattleInterface() assert(pw != nullptr); std::set teams = GetAllTeams(); std::vector sortedTeams(teams.begin(), teams.end()); - if(m_scoreboard->GetSortType() == SORT_POINTS) + if(m_scoreboard->GetSortType() == SortType::SORT_POINTS) { std::sort(sortedTeams.begin(), sortedTeams.end(), [this](int teamA, int teamB) { diff --git a/src/level/scoreboard.h b/src/level/scoreboard.h index cd964de3..993418e5 100644 --- a/src/level/scoreboard.h +++ b/src/level/scoreboard.h @@ -42,7 +42,7 @@ struct Score float time = 0; //! Time when points were scored }; -enum SortType +enum class SortType { SORT_ID, //Sort by team ID SORT_POINTS, //Sort by points @@ -142,5 +142,5 @@ private: std::vector> m_rulesEndTake = {}; std::map m_score; int m_finishCounter = 0; - SortType m_sorttype = SORT_ID; + SortType m_sorttype = SortType::SORT_ID; };