SuperTuxKart

This class manages all powerups. More...
#include <powerup_manager.hpp>
Classes  
class  WeightsData 
This object stores all the weights for one particular number of karts. More...  
Public Member Functions  
LEAK_CHECK ()  
PowerupManager ()  
The constructor initialises everything to zero. More...  
~PowerupManager ()  
Destructor, frees all meshes. More...  
void  loadPowerupsModels () 
Loads powerups models and icons from the powerup.xml file.  
void  loadWeights (const XMLNode *node, const std::string &category) 
Loads the powerups weights for a given category (race, ft, ...). More...  
void  unloadPowerups () 
Removes any textures so that they can be reloaded.  
void  computeWeightsForRace (int num_karts) 
Create a (potentially interpolated) WeightsData objects for the current race based on the number of karts. More...  
void  loadPowerup (PowerupType type, const XMLNode &node) 
Loads the data for one particular powerup. More...  
PowerupManager::PowerupType  getRandomPowerup (unsigned int pos, unsigned int *n, uint64_t random_number) 
Returns a random powerup for a kart at a given position. More...  
Material *  getIcon (int type) const 
Returns the icon(material) for a powerup. More...  
irr::scene::IMesh *  getMesh (int type) const 
Returns the mesh for a certain powerup. More...  
uint64_t  getRandomSeed () const 
void  setRandomSeed (uint64_t seed) 
Static Public Member Functions  
static void  unitTesting () 
Unit testing is based on deterministic item distributions: if all random numbers from 0 till sum_of_all_weights  1 are used, the original weight distribution must be restored.  
Private Member Functions  
PowerupType  getPowerupType (const std::string &name) const 
Determines the powerup type for a given name. More...  
Private Attributes  
std::map< std::string, std::vector< WeightsData * > >  m_all_weights 
The first key is the race type: race, battle, soccer etc. More...  
Material *  m_all_icons [POWERUP_MAX] 
The icon for each powerup. More...  
irr::scene::IMesh *  m_all_meshes [POWERUP_MAX] 
The mesh for each model (if the powerup has a model), e.g. More...  
WeightsData  m_current_item_weights 
The weight distribution to be used for the current race. More...  
std::atomic< uint64_t >  m_random_seed 
Seed for random powerup, for local game it will use a random number, for network games it will use the start time from server. More...  
This class manages all powerups.
It reads in powerup.xml to get the data, initialise the static member of some flyables (i.e. powerup.xml contains info about cakes, plunger etc which needs to be stored), and maintains the 'weights' (used in randomly chosing which item was collected) for all items depending on position. The latter is done so that as the first player you get less advantageous items (but no useless ones either), while as the last you get more useful ones.
The weights distribution is described in the powerup.xml file in more detail. All weights are stored in the m_all_weights data structure, which maps the race mode (race, battle, ...) to a list of WeightsData instances. Each WeightsData instance stores the data for one specific number of karts. E.g. m_all_weights['race'] contains 5 WeightsData instances for 1, 5, 9, 14, and 20 karts. At race start a new instance of WeightsData is created in m_current_item_weights. It contains the interpolated values for the number of karts in the current race (e.g. if the race is with 6 karts if will use 3/4 the weights for 5 karts, and 1/4 the weights for 9 karts. Then m_current_item_weights will create a weight distribution for each possible rank in the race (1 to 6 in the example above). This is the interpolation of the values within one WeightsData. Atm there are also 5 entries in that list (though it does not have to be the same number as above  i.e. the 1, 5, 9, 14, 20 weights list). Similarly the actual distribution used for a kart with a specific rank is based on dividing the available ranks (so 6 karts > 6 ranks). With the 5 specified values the first entry is used for rank 1, the last entry for rank 6, and ranks 25 will be interpolated based on an equal distance: in a race with 6 karts for example, the 2nd weight list is used for rank 2.25, the 3nd for rank 3.5, the 4th for rank 4.75 (and the first and last for rank 1 and 6). It does not matter that the ranks are non integer: the actual weights used for say rank 2, will then be interplated between the weights of rank 1 and 2.25 (e.g. 0.8*weights_for 2.25 + 0.2*weights_for 1).
PowerupManager::PowerupManager  (  ) 
The constructor initialises everything to zero.
PowerupManager::~PowerupManager  (  ) 
Destructor, frees all meshes.
void PowerupManager::computeWeightsForRace  (  int  num_karts  ) 
Create a (potentially interpolated) WeightsData objects for the current race based on the number of karts.
num_karts  Number of karts in the current race. 

inline 
Returns the icon(material) for a powerup.

inline 
Returns the mesh for a certain powerup.
type  Mesh type for which the model is returned. 

private 
Determines the powerup type for a given name.
name  Name of the powerup to look up. 
PowerupManager::PowerupType PowerupManager::getRandomPowerup  (  unsigned int  pos, 
unsigned int *  n,  
uint64_t  random_number  
) 
Returns a random powerup for a kart at a given position.
If the race mode is a battle, the position is actually not used and a randomly selected item for POSITION_BATTLE_MODE is returned. This function takes the weights specified for all items into account by using a list which contains all items depending on the weights defined. See updateWeightsForRace()
pos  Position of the kart (1<=pos<=number of karts). 
n  Number of times this item is given to the kart. 
random_number  A random number used to select the item. Important for networking to be able to reproduce item selection. 
void PowerupManager::loadPowerup  (  PowerupType  type, 
const XMLNode &  node  
) 
Loads the data for one particular powerup.
For bowling ball, plunger, and cake static members in the appropriate classes are called to store additional information for those objects.
type  The type of the powerup. 
node  The XML node with the data for this powerup. 
void PowerupManager::loadWeights  (  const XMLNode *  powerup_node, 
const std::string &  class_name  
) 
Loads the powerups weights for a given category (race, ft, ...).
The data is stored in m_all_weights.
node  The top node of the powerup xml file. 
class_name  The name of the attribute with the weights for the class. 

private 
The icon for each powerup.

private 
The mesh for each model (if the powerup has a model), e.g.
a switch has none.

private 
The first key is the race type: race, battle, soccer etc.
The key then contains a mapping from the kart numbers to the WeightsData object that stores all data for the give kart number.

private 
The weight distribution to be used for the current race.

private 
Seed for random powerup, for local game it will use a random number, for network games it will use the start time from server.