Ejemplo n.º 1
0
/** Loads a weight list specified in powerup.xml. The different position
 *  classes must be loaded in the right order
 *  \param root The root node of powerup.xml 
 *  \param class_name The name of the position class to load.
 *  \param position_class The class for which the weights are read.
 */
void PowerupManager::loadWeights(const XMLNode &root,
                                 const std::string &class_name,
                                 PositionClass position_class)
{
    const XMLNode *node = root.getNode(class_name);
    std::string s(""), s_multi("");
    if(node) node->get("w",       &s      );
    if(node) node->get("w-multi", &s_multi);

    if(!node || s=="" || s_multi=="")
    {
        printf("No weights found for class '%s' - probabilities will be incorrect.\n",
               class_name.c_str());
        return;
    }

    std::vector<std::string> weight_list = StringUtils::split(s+" "+s_multi,' ');

    std::vector<std::string>::iterator i=weight_list.begin();
    while(i!=weight_list.end())
    {
        if(*i=="")
        {
            std::vector<std::string>::iterator next=weight_list.erase(i);
            i=next;
        }
        else
            i++;
    }
    // Fill missing entries with 0s
    while(weight_list.size()<2*(int)POWERUP_LAST)
        weight_list.push_back(0);

    if(weight_list.size()!=2*(int)POWERUP_LAST)
    {
        printf("Incorrect number of weights found in class '%s':\n", 
               class_name.c_str());
        printf("%d instead of %d - probabilities will be incorrect.\n",
               (int)weight_list.size(), (int)POWERUP_LAST);
        return;
    }

    for(unsigned int i=0; i<weight_list.size(); i++)
    {
        int w = atoi(weight_list[i].c_str());
        m_weights[position_class].push_back(w);
    }

}   // loadWeights
Ejemplo n.º 2
0
/** Loads a weight list specified in powerup.xml. The different position
 *  classes must be loaded in the right order
 *  \param root The root node of powerup.xml
 *  \param class_name The name of the position class to load.
 *  \param position_class The class for which the weights are read.
 */
void PowerupManager::loadWeights(const XMLNode &root,
                                 unsigned int num_karts, 
                                 const std::string &class_name,
                                 PositionClass position_class)
{
    const XMLNode *node = root.getNode(class_name), *node2;
    std::string s(""), s_multi(""), k;
    std::string w("w"), w_multi("w-multi"), w_add("");
    
    /** Adds to w the suffixe of the num_karts_class 
     *  associated with num_karts
     */
    if(position_class!=POSITION_BATTLE_MODE 
    && position_class!=POSITION_TUTORIAL_MODE)
    {
        node2 = root.getNode("range_kart");
        k = StringUtils::toString(num_karts);
        k = "k" + k;//Xml nodes can't start with a number
        //k now contains XML attributes containing kart number class
        if(node2) node2->get(k, &w_add);
        if(!node2)
        {
            Log::error("[PowerupManager]","No class of weights found"
                       "for %d karts - probabilities will be incorrect",
                       num_karts);
        }
        if(race_manager->isFollowMode())
        {
            w_add = "f";    
        }
        if(w_add=="")
        {
            w_add = "d";//fallback values
            Log::warn("[PowerupManager]","powerup.xml do not support"
                      "%d karts - fallback probabilities will be used",
                      num_karts);
        }
        w = w + w_add;
        w_multi = w_multi + w_add;
    }//w is changed to associate with an arbitrary class of kart numbers
    
    if(node) node->get(w,       &s      );
    if(node) node->get(w_multi, &s_multi);

    if(!node || s=="" || s_multi=="")
    {
        Log::error("[PowerupManager]", "No weights found for class '%s'"
                    " - probabilities will be incorrect.",
                    class_name.c_str());
        return;
    }

    std::vector<std::string> weight_list = StringUtils::split(s+" "+s_multi,' ');

    std::vector<std::string>::iterator i=weight_list.begin();
    while(i!=weight_list.end())
    {
        if(*i=="")
        {
            std::vector<std::string>::iterator next=weight_list.erase(i);
            i=next;
        }
        else
            i++;
    }
    // Fill missing entries with 0s
    while(weight_list.size()<2*(int)POWERUP_LAST)
        weight_list.push_back(0);

    if(weight_list.size()!=2*(int)POWERUP_LAST)
    {
        Log::error("[PowerupManager]", "Incorrect number of weights found in class '%s':",
               class_name.c_str());
        Log::error("[PowerupManager]", "%d instead of %d - probabilities will be incorrect.",
               (int)weight_list.size(), (int)POWERUP_LAST);
        return;
    }

    for(unsigned int i=0; i<weight_list.size(); i++)
    {
        int w = atoi(weight_list[i].c_str());
        m_weights[position_class].push_back(w);
    }

}   // loadWeights