SuperTuxKart
powerup_manager.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2006-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_POWERUPMANAGER_HPP
20 #define HEADER_POWERUPMANAGER_HPP
21 
22 #include "utils/leak_check.hpp"
23 #include "utils/no_copy.hpp"
24 #include "utils/types.hpp"
25 
26 #include "btBulletDynamicsCommon.h"
27 
28 #include <atomic>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 class Material;
34 class XMLNode;
35 namespace irr
36 {
37  namespace scene { class IMesh; }
38 }
39 
78 class PowerupManager : public NoCopy
79 {
80 public:
81  LEAK_CHECK();
82 private:
83  // ------------------------------------------------------------------------
88  {
89  private:
91  unsigned int m_num_karts;
92 
94  std::vector < std::vector<int> > m_weights_for_section;
95 
99  std::vector < std::vector<unsigned> > m_summed_weights_for_rank;
100 
101  public:
102  // The friend declaration gives the PowerupManager access to the
103  // internals, which is ONLY used for testing!!
104  friend PowerupManager;
105  WeightsData() { m_num_karts = 0; }
106  void reset();
107  void readData(int num_karts, const XMLNode *node);
108  void interpolate(WeightsData *prev, WeightsData *next, int num_karts);
109  void convertRankToSection(int rank, int *prev, int *next,
110  float *weight);
111  void precomputeWeights();
112  int getRandomItem(int rank, uint64_t random_number);
113  // --------------------------------------------------------------------
115  void setNumKarts(int num_karts) { m_num_karts = num_karts; }
116  // --------------------------------------------------------------------
118  int getNumKarts() const { return m_num_karts; }
119  }; // class WeightsData
120  // ------------------------------------------------------------------------
121 
126  std::map<std::string, std::vector<WeightsData*> > m_all_weights;
127 
128 public:
129  // The anvil and parachute must be at the end of the enum, and the
130  // zipper just before them (see Powerup::hitBonusBox).
131  enum PowerupType {POWERUP_NOTHING,
132  POWERUP_FIRST,
133  POWERUP_BUBBLEGUM = POWERUP_FIRST,
134  POWERUP_CAKE,
135  POWERUP_BOWLING, POWERUP_ZIPPER, POWERUP_PLUNGER,
136  POWERUP_SWITCH, POWERUP_SWATTER, POWERUP_RUBBERBALL,
137  POWERUP_PARACHUTE,
138  POWERUP_ANVIL, //powerup.cpp assumes these two come last
139  POWERUP_LAST=POWERUP_ANVIL,
140  POWERUP_MAX
141  };
142 
143 private:
144 
146  Material* m_all_icons [POWERUP_MAX];
147 
150  irr::scene::IMesh *m_all_meshes[POWERUP_MAX];
151 
154 
155  PowerupType getPowerupType(const std::string &name) const;
156 
159  std::atomic<uint64_t> m_random_seed;
160 
161 public:
162  static void unitTesting();
163 
164  PowerupManager ();
165  ~PowerupManager ();
166  void loadPowerupsModels ();
167  void loadWeights(const XMLNode *node, const std::string &category);
168  void unloadPowerups ();
169  void computeWeightsForRace(int num_karts);
170  void loadPowerup (PowerupType type, const XMLNode &node);
171  PowerupManager::PowerupType
172  getRandomPowerup(unsigned int pos, unsigned int *n,
173  uint64_t random_number);
174  // ------------------------------------------------------------------------
176  Material* getIcon(int type) const {return m_all_icons [type];}
177  // ------------------------------------------------------------------------
180  irr::scene::IMesh *getMesh(int type) const {return m_all_meshes[type];}
181  // ------------------------------------------------------------------------
182  uint64_t getRandomSeed() const { return m_random_seed.load(); }
183  // ------------------------------------------------------------------------
184  void setRandomSeed(uint64_t seed) { m_random_seed.store(seed); }
185 
186 }; // class PowerupManager
187 
188 extern PowerupManager* powerup_manager;
189 
190 #endif
Definition: material.hpp:48
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
This object stores all the weights for one particular number of karts.
Definition: powerup_manager.hpp:88
unsigned int m_num_karts
The number of karts for which this entry is to be used.
Definition: powerup_manager.hpp:91
std::vector< std::vector< unsigned > > m_summed_weights_for_rank
This field is only populated for the WeightData class that is used during a race.
Definition: powerup_manager.hpp:99
std::vector< std::vector< int > > m_weights_for_section
Stores for each of the sections the weights from the XML file.
Definition: powerup_manager.hpp:94
void setNumKarts(int num_karts)
Sets the number of karts.
Definition: powerup_manager.hpp:115
int getNumKarts() const
Returns for how many karts this entry is meant for.
Definition: powerup_manager.hpp:118
void convertRankToSection(int rank, int *prev, int *next, float *weight)
For a given rank in the current race this computes the previous and next entry in the weight list,...
Definition: powerup_manager.cpp:312
void readData(int num_karts, const XMLNode *node)
Reads in all weights for a given category and number of karts.
Definition: powerup_manager.cpp:216
int getRandomItem(int rank, uint64_t random_number)
Computes a random item dependent on the rank of the kart and a given random number.
Definition: powerup_manager.cpp:415
void precomputeWeights()
This function computes the item distribution for each possible rank in the race.
Definition: powerup_manager.cpp:384
void interpolate(WeightsData *prev, WeightsData *next, int num_karts)
Defines the weights for this WeightsData object based on a linear interpolation between the previous ...
Definition: powerup_manager.cpp:271
void reset()
Deletes all data stored in a WeightsData objects.
Definition: powerup_manager.cpp:204
This class manages all powerups.
Definition: powerup_manager.hpp:79
std::map< std::string, std::vector< WeightsData * > > m_all_weights
The first key is the race type: race, battle, soccer etc.
Definition: powerup_manager.hpp:126
Material * m_all_icons[POWERUP_MAX]
The icon for each powerup.
Definition: powerup_manager.hpp:146
irr::scene::IMesh * getMesh(int type) const
Returns the mesh for a certain powerup.
Definition: powerup_manager.hpp:180
WeightsData m_current_item_weights
The weight distribution to be used for the current race.
Definition: powerup_manager.hpp:153
void loadPowerupsModels()
Loads powerups models and icons from the powerup.xml file.
Definition: powerup_manager.cpp:122
void loadPowerup(PowerupType type, const XMLNode &node)
Loads the data for one particular powerup.
Definition: powerup_manager.cpp:457
void computeWeightsForRace(int num_karts)
Create a (potentially interpolated) WeightsData objects for the current race based on the number of k...
Definition: powerup_manager.cpp:523
Material * getIcon(int type) const
Returns the icon(material) for a powerup.
Definition: powerup_manager.hpp:176
void loadWeights(const XMLNode *node, const std::string &category)
Loads the powerups weights for a given category (race, ft, ...).
Definition: powerup_manager.cpp:175
PowerupManager::PowerupType getRandomPowerup(unsigned int pos, unsigned int *n, uint64_t random_number)
Returns a random powerup for a kart at a given position.
Definition: powerup_manager.cpp:605
PowerupManager()
The constructor initialises everything to zero.
Definition: powerup_manager.cpp:44
void unloadPowerups()
Removes any textures so that they can be reloaded.
Definition: powerup_manager.cpp:84
PowerupType getPowerupType(const std::string &name) const
Determines the powerup type for a given name.
Definition: powerup_manager.cpp:103
static void unitTesting()
Unit testing is based on deterministic item distributions: if all random numbers from 0 till sum_of_a...
Definition: powerup_manager.cpp:634
irr::scene::IMesh * m_all_meshes[POWERUP_MAX]
The mesh for each model (if the powerup has a model), e.g.
Definition: powerup_manager.hpp:150
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 th...
Definition: powerup_manager.hpp:159
~PowerupManager()
Destructor, frees all meshes.
Definition: powerup_manager.cpp:56
utility class used to parse XML files
Definition: xml_node.hpp:48
Declares the general types that are used by the network.