SuperTuxKart
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
PowerupManager Class Reference

This class manages all powerups. More...

#include <powerup_manager.hpp>

Inheritance diagram for PowerupManager:
Inheritance graph
[legend]

Classes

class  WeightsData
 This object stores all the weights for one particular number of karts. More...
 

Public Types

enum  PowerupType {
  POWERUP_NOTHING, POWERUP_FIRST, POWERUP_BUBBLEGUM = POWERUP_FIRST, POWERUP_CAKE,
  POWERUP_BOWLING, POWERUP_ZIPPER, POWERUP_PLUNGER, POWERUP_SWITCH,
  POWERUP_SWATTER, POWERUP_RUBBERBALL, POWERUP_PARACHUTE, POWERUP_ANVIL,
  POWERUP_LAST =POWERUP_ANVIL, POWERUP_MAX
}
 

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...
 
MaterialgetIcon (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...
 
Materialm_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...
 

Detailed Description

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 2-5 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).

Constructor & Destructor Documentation

◆ PowerupManager()

PowerupManager::PowerupManager ( )

The constructor initialises everything to zero.

◆ ~PowerupManager()

PowerupManager::~PowerupManager ( )

Destructor, frees all meshes.

Member Function Documentation

◆ computeWeightsForRace()

void PowerupManager::computeWeightsForRace ( int  num_karts)

Create a (potentially interpolated) WeightsData objects for the current race based on the number of karts.

Parameters
num_kartsNumber of karts in the current race.

◆ getIcon()

Material* PowerupManager::getIcon ( int  type) const
inline

Returns the icon(material) for a powerup.

◆ getMesh()

irr::scene::IMesh* PowerupManager::getMesh ( int  type) const
inline

Returns the mesh for a certain powerup.

Parameters
typeMesh type for which the model is returned.

◆ getPowerupType()

PowerupManager::PowerupType PowerupManager::getPowerupType ( const std::string &  name) const
private

Determines the powerup type for a given name.

Parameters
nameName of the powerup to look up.
Returns
The type, or POWERUP_NOTHING if the name is not found

◆ getRandomPowerup()

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()

Parameters
posPosition of the kart (1<=pos<=number of karts).
nNumber of times this item is given to the kart.
random_numberA random number used to select the item. Important for networking to be able to reproduce item selection.

◆ loadPowerup()

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.

Parameters
typeThe type of the powerup.
nodeThe XML node with the data for this powerup.

◆ loadWeights()

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.

Parameters
nodeThe top node of the powerup xml file.
class_nameThe name of the attribute with the weights for the class.

Member Data Documentation

◆ m_all_icons

Material* PowerupManager::m_all_icons[POWERUP_MAX]
private

The icon for each powerup.

◆ m_all_meshes

irr::scene::IMesh* PowerupManager::m_all_meshes[POWERUP_MAX]
private

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

a switch has none.

◆ m_all_weights

std::map<std::string, std::vector<WeightsData*> > PowerupManager::m_all_weights
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.

◆ m_current_item_weights

WeightsData PowerupManager::m_current_item_weights
private

The weight distribution to be used for the current race.

◆ m_random_seed

std::atomic<uint64_t> PowerupManager::m_random_seed
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.


The documentation for this class was generated from the following files: