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
WeightsData m_current_item_weights
The weight distribution to be used for the current race.
Definition: powerup_manager.hpp:153
int getNumKarts() const
Returns for how many karts this entry is meant for.
Definition: powerup_manager.hpp:118
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
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
Definition: three_d_animation.hpp:32
void setNumKarts(int num_karts)
Sets the number of karts.
Definition: powerup_manager.hpp:115
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
Declares the general types that are used by the network.
This object stores all the weights for one particular number of karts.
Definition: powerup_manager.hpp:87
unsigned int m_num_karts
The number of karts for which this entry is to be used.
Definition: powerup_manager.hpp:91
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
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
Definition: material.hpp:47
This class manages all powerups.
Definition: powerup_manager.hpp:78
utility class used to parse XML files
Definition: xml_node.hpp:47
Material * getIcon(int type) const
Returns the icon(material) for a powerup.
Definition: powerup_manager.hpp:176
irr::scene::IMesh * getMesh(int type) const
Returns the mesh for a certain powerup.
Definition: powerup_manager.hpp:180