Can now load a map type

This commit is contained in:
Fantasmos 2017-10-28 09:16:28 +11:00
parent 823d3112ad
commit f10feb77fd
3 changed files with 45 additions and 39 deletions

View File

@ -29,6 +29,7 @@ static PtrVector<UserConfigParam, REF> all_params;
// X-macros
#define PARAM_PREFIX
#define PARAM_DEFAULT(X) = X
#include "config/user_config.hpp"
#include "config/saved_grand_prix.hpp"
@ -345,8 +346,11 @@ MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name,
// add the default list
va_list arguments;
va_start(arguments, nb_elements);
for (int i = 0; i < nb_elements; i++)
m_elements.push_back(T(va_arg(arguments, U)));
typedef std::pair<T, U> DictThing;
for (int i = 0; i < nb_elements; i++) {
std::pair<T, U> key_value_pair = va_arg(arguments, DictThing);
m_elements.insert(key_value_pair);
}
va_end(arguments); // Cleans up the list
} // MapUserConfigParam
@ -376,8 +380,11 @@ MapUserConfigParam<T, U>::MapUserConfigParam(const char* param_name,
// add the default list
va_list arguments;
va_start(arguments, nb_elements);
for (int i = 0; i < nb_elements; i++)
m_elements.push_back(va_arg(arguments, T));
for (int i = 0; i < nb_elements; i++) {
std::pair<T, U> key_value_pair = va_arg(arguments, DictThing);
m_elements.insert(key_value_pair);
}
va_end(arguments); // Cleans up the list
} // MapUserConfigParam
@ -395,9 +402,8 @@ void MapUserConfigParam<T, U>::write(std::ofstream& stream) const
// actual elements
//for (int n = 0; n<elts_amount; n++)
for (std::map<T, U>::iterator it = m_elements.begin(); it != m_elements.end(); ++it)
{
stream << " " << it->first << "=\"" << it->second << "\"\n";
for (const auto& kv : m_elements) {
stream << " " << kv.first << "=\"" << kv.second << "\"\n";
}
stream << " >\n";
stream << " </" << m_param_name.c_str() << ">\n\n";
@ -417,29 +423,29 @@ void MapUserConfigParam<T, U>::findYourDataInAChildOf(const XMLNode* node)
int attr_count = 0;
child->get("Size", &attr_count);
for (int n = 0; n<attr_count; n++)
{
T elt;
for (const auto& kv : m_elements) {
std::pair<T,U> elt;
std::string str;
child->get(StringUtils::toString(n), &str);
StringUtils::fromString<T>(str, elt);
// check if the element is already there :
child->get(kv.first, &str);
elt.first = kv.first;
elt.second = str.c_str();
bool there = false;
for (unsigned int i = 0; i < m_elements.size(); i++)
{
if (elt == m_elements[i])
for (const auto& kvRHS : m_elements) {
if (elt.second == kvRHS.second)
{
there = true;
break;
}
}
if (!there)
{
m_elements.push_back(elt);
m_elements.insert(elt);
}
}
} // findYourDataInAChildOf
// ----------------------------------------------------------------------------

View File

@ -178,7 +178,7 @@ public:
m_elements = std::map<T,U>(v); return m_elements;
}
}; // ListUserConfigParam
typedef MapUserConfigParam<std::string, const char*> StringToStringUserConfigParam;
typedef MapUserConfigParam<const char*, const char*> StringToStringUserConfigParam;
// ============================================================================
class IntUserConfigParam : public UserConfigParam
@ -416,20 +416,7 @@ namespace UserConfigParams
// ---- Gamemode setup
PARAM_PREFIX StringToStringUserConfigParam m_karts_per_gamemode
PARAM_DEFAULT(StringToStringUserConfigParam("karts_per_gamemode", "The karts per gamemode"
"Number of karts per gamemode. Order corresponds to Enum value",
10,
"3",
"3",
"3",
"3",
"3",
"3",
"3",
"3",
"3",
"3"));
// ---- Wiimote data
PARAM_PREFIX GroupUserConfigParam m_wiimote_group
@ -766,7 +753,15 @@ namespace UserConfigParams
"stun.voxalot.com",
"stun.voxgratia.org",
"stun.xten.com") );
PARAM_PREFIX StringToStringUserConfigParam m_karts_per_gamemode
PARAM_DEFAULT(StringToStringUserConfigParam("karts_per_gamemode", "The karts per gamemode"
"Number of karts per gamemode. Order corresponds to Enum value",
4,
std::make_pair("3", "1"),
std::make_pair("f", "2"),
std::make_pair("e", "4"),
std::make_pair("te", "5")
));
PARAM_PREFIX BoolUserConfigParam m_log_packets
PARAM_DEFAULT( BoolUserConfigParam(false, "log-network-packets",
"If all network packets should be logged") );

View File

@ -164,14 +164,19 @@ void TrackInfoScreen::init()
m_ai_kart_spinner->setVisible(has_AI);
getWidget<LabelWidget>("ai-text")->setVisible(has_AI);
std::vector<std::string> karts_per_gamemode;//UserConfigParams::m_karts_per_gamemode;
UserConfigParams::m_num_karts;
std::map<const char* , const char*> karts_per_gamemode = UserConfigParams::m_karts_per_gamemode;
auto data = UserConfigParams::m_karts_per_gamemode;
if (has_AI)
{
m_ai_kart_spinner->setActive(true);
int gamemode = race_manager->getMinorMode();
int num_ai = stoi(karts_per_gamemode[gamemode]) - local_players; //TODO error caused by enums not being sequential
// Avoid negative numbers (which can happen if e.g. the number of karts
//Temp fix
//int num_ai = stoi(karts_per_gamemode[ "3"/*gamemode*/]) - local_players; //TODO error caused by enums not being sequential
int num_ai = 1;
// Avoid negative numbers (which can happen if e.g. the number of karts
// in a previous race was lower than the number of players now.
if (num_ai < 0) num_ai = 0;
@ -247,7 +252,7 @@ void TrackInfoScreen::init()
race_manager->setNumKarts(race_manager->getNumLocalPlayers());
//This is causing an error
karts_per_gamemode[race_manager->getMinorMode()] = std::to_string(race_manager->getNumLocalPlayers());
/// karts_per_gamemode[race_manager->getMinorMode()] = std::to_string(race_manager->getNumLocalPlayers());
// UserConfigParams::m_karts_per_gamemode = karts_per_gamemode; //TODO Causes error
UserConfigParams::m_num_karts = race_manager->getNumLocalPlayers();
}