SuperTuxKart
item_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_ITEMMANAGER_HPP
20 #define HEADER_ITEMMANAGER_HPP
21 
22 #include "LinearMath/btTransform.h"
23 
24 #include "items/item.hpp"
25 #include "utils/aligned_array.hpp"
26 #include "utils/no_copy.hpp"
27 #include "utils/vec3.hpp"
28 
29 #include <SColor.h>
30 
31 #include <assert.h>
32 #include <algorithm>
33 
34 #include <map>
35 #include <memory>
36 #include <random>
37 #include <string>
38 #include <vector>
39 
40 class Kart;
41 class STKPeer;
42 
46 class ItemManager : public NoCopy
47 {
48  // Some static data and functions to initialise it:
49 private:
51  static std::vector<video::SColorf> m_glow_color;
52 
55 
56  static std::mt19937 m_random_engine;
57 
58  static uint32_t m_random_seed;
59 
60  static bool preloadIcon(const std::string& name);
61 public:
62  static void loadDefaultItemMeshes();
63  static void removeTextures();
64  static void updateRandomSeed(uint32_t seed_number)
65  {
66  m_random_engine.seed(seed_number);
67  m_random_seed = seed_number;
68  } // updateRandomSeed
69  // ------------------------------------------------------------------------
70  static uint32_t getRandomSeed()
71  {
72  return m_random_seed;
73  } // getRandomSeed
74 
75  // ------------------------------------------------------------------------
76 
79  static void disableItemCollection()
80  {
82  } // disableItemCollection
83 
84  // ------------------------------------------------------------------------
86  static scene::IMesh* getItemModel(ItemState::ItemType type)
87  { return m_item_mesh[type]; }
88  // ------------------------------------------------------------------------
91  { return m_item_lowres_mesh[type]; }
92  // ------------------------------------------------------------------------
94  static std::string getIcon(ItemState::ItemType type)
95  { return m_icon[type]; }
96  // ------------------------------------------------------------------------
98  static video::SColorf& getGlowColor(ItemState::ItemType type)
99  { return m_glow_color[type]; }
100 
101  // ========================================================================
102 protected:
104  typedef std::vector<ItemState*> AllItemTypes;
105  AllItemTypes m_all_items;
106 
108  std::vector<ItemState::ItemType> m_switch_to;
109 
110 private:
114  std::vector< AllItemTypes > *m_items_in_quads;
115 
117  static std::vector<scene::IMesh *> m_item_mesh;
118 
120  static std::vector<scene::IMesh *> m_item_lowres_mesh;
121 
123  static std::vector<std::string> m_icon;
124 
125 protected:
129 
130  void deleteItem(ItemState *item);
131  void switchItemsInternal(std::vector < ItemState*> &all_items);
132  void setSwitchItems(const std::vector<int> &switch_items);
133  void insertItemInQuad(Item *item);
134  void deleteItemInQuad(ItemState *item);
135 public:
136  ItemManager();
137  virtual ~ItemManager();
138 
139  virtual Item* placeItem (ItemState::ItemType type, const Vec3& xyz,
140  const Vec3 &normal);
141  virtual Item* dropNewItem (ItemState::ItemType type,
142  const AbstractKart* parent,
143  const Vec3 *server_xyz = NULL,
144  const Vec3 *normal = NULL);
145  void update (int ticks);
146  void updateGraphics (float dt);
147  void checkItemHit (AbstractKart* kart);
148  void reset ();
149  virtual void collectedItem (ItemState *item, AbstractKart *kart);
150  virtual void switchItems ();
151  bool randomItemsForArena(const AlignedArray<btTransform>& pos);
152 
153  // ------------------------------------------------------------------------
155  bool areItemsSwitched() { return (m_switch_ticks > 0); }
156  // ------------------------------------------------------------------------
158  virtual void setItemConfirmationTime(std::weak_ptr<STKPeer> peer,
159  int ticks)
160  {
161  assert(false);
162  }
163  // ------------------------------------------------------------------------
165  unsigned int getNumberOfItems() const
166  {
167  return (unsigned int) m_all_items.size();
168  }
169  // ------------------------------------------------------------------------
171  const ItemState* getItem(unsigned int n) const
172  {
173  return dynamic_cast<Item*>(m_all_items[n]);
174  };
175  // ------------------------------------------------------------------------
177  ItemState* getItem(unsigned int n)
178  {
179  return dynamic_cast<Item*>(m_all_items[n]);
180  }
181  // ------------------------------------------------------------------------
182  bool itemExists(const ItemState* is) const
183  {
184  if (!is)
185  return false;
186  auto it = std::find(m_all_items.begin(), m_all_items.end(), is);
187  return it != m_all_items.end();
188  }
189  // ------------------------------------------------------------------------
192  const AllItemTypes& getItemsInQuads(unsigned int n) const
193  {
194  assert(m_items_in_quads);
195  assert(n<(*m_items_in_quads).size());
196  return (*m_items_in_quads)[n];
197  } // getItemsInQuads
198  // ------------------------------------------------------------------------
201  Item* getFirstItemInQuad(unsigned int n) const
202  {
203  assert(m_items_in_quads);
204  assert(n < m_items_in_quads->size());
205  return ((*m_items_in_quads)[n]).empty()
206  ? NULL
207  : dynamic_cast<Item*>((*m_items_in_quads)[n].front());
208  } // getFirstItemInQuad
209  // ------------------------------------------------------------------------
210  unsigned int insertItem(Item *item);
211 }; // ItemManager
212 
213 #endif
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:62
Definition: item_manager.hpp:47
std::vector< ItemState * > AllItemTypes
The vector of all items of the current track.
Definition: item_manager.hpp:104
void updateGraphics(float dt)
Updates the graphics, called once per rendered frame.
Definition: item_manager.cpp:513
static void removeTextures()
Clean up all textures.
Definition: item_manager.cpp:146
unsigned int insertItem(Item *item)
Inserts the new item into the items management data structures, if possible reusing an existing,...
Definition: item_manager.cpp:232
static std::vector< scene::IMesh * > m_item_lowres_mesh
Stores all low-resolution item models.
Definition: item_manager.hpp:120
ItemManager()
Creates a new instance of the item manager.
Definition: item_manager.cpp:171
void reset()
Resets all items and removes bubble gum that is stuck on the track.
Definition: item_manager.cpp:434
std::vector< AllItemTypes > * m_items_in_quads
Stores which items are on which quad.
Definition: item_manager.hpp:114
void checkItemHit(AbstractKart *kart)
Checks if any item was collected by the given kart.
Definition: item_manager.cpp:389
const AllItemTypes & getItemsInQuads(unsigned int n) const
Returns a reference to the array of all items on the specified quad.
Definition: item_manager.hpp:192
static bool preloadIcon(const std::string &name)
Preload icon materials to avoid hangs when firstly insert item.
Definition: item_manager.cpp:132
void setSwitchItems(const std::vector< int > &switch_items)
Sets which objects is getting switched to what.
Definition: item_manager.cpp:202
static void disableItemCollection()
Disable item collection, useful to test client mispreditions or client/server disagreements.
Definition: item_manager.hpp:79
void update(int ticks)
Updates all items, and handles switching items back if the switch time is over.
Definition: item_manager.cpp:479
const ItemState * getItem(unsigned int n) const
Returns a pointer to the n-th item.
Definition: item_manager.hpp:171
void deleteItemInQuad(ItemState *item)
Removes an items from the items-in-quad list only.
Definition: item_manager.cpp:541
ItemState * getItem(unsigned int n)
Returns a pointer to the n-th item.
Definition: item_manager.hpp:177
virtual void setItemConfirmationTime(std::weak_ptr< STKPeer > peer, int ticks)
Only used in the NetworkItemManager.
Definition: item_manager.hpp:158
static std::vector< video::SColorf > m_glow_color
Stores the glow color for all items.
Definition: item_manager.hpp:51
virtual void collectedItem(ItemState *item, AbstractKart *kart)
Set an item as collected.
Definition: item_manager.cpp:375
void insertItemInQuad(Item *item)
Insert into the appropriate quad list, if there is a quad list (i.e.
Definition: item_manager.cpp:258
bool areItemsSwitched()
Returns true if the items are switched atm.
Definition: item_manager.hpp:155
static video::SColorf & getGlowColor(ItemState::ItemType type)
Returns the glow color for an item.
Definition: item_manager.hpp:98
unsigned int getNumberOfItems() const
Returns the number of items.
Definition: item_manager.hpp:165
void deleteItem(ItemState *item)
Removes an items from the items-in-quad list, from the list of all items, and then frees the item its...
Definition: item_manager.cpp:528
static bool m_disable_item_collection
Disable item collection (for debugging purposes).
Definition: item_manager.hpp:54
virtual Item * placeItem(ItemState::ItemType type, const Vec3 &xyz, const Vec3 &normal)
Places a new item on the track/arena.
Definition: item_manager.cpp:345
virtual Item * dropNewItem(ItemState::ItemType type, const AbstractKart *parent, const Vec3 *server_xyz=NULL, const Vec3 *normal=NULL)
Creates a new item at the location of the kart (e.g.
Definition: item_manager.cpp:281
static std::string getIcon(ItemState::ItemType type)
Returns the mesh for a certain item.
Definition: item_manager.hpp:94
virtual ~ItemManager()
Destructor.
Definition: item_manager.cpp:211
void switchItemsInternal(std::vector< ItemState * > &all_items)
Switches all items: boxes become bananas and vice versa for a certain amount of time (as defined in s...
Definition: item_manager.cpp:569
static scene::IMesh * getItemModel(ItemState::ItemType type)
Returns the mesh for a certain item.
Definition: item_manager.hpp:86
std::vector< ItemState::ItemType > m_switch_to
What item this item is switched to.
Definition: item_manager.hpp:108
Item * getFirstItemInQuad(unsigned int n) const
Returns the first item (NULL if none) on the specified quad.
Definition: item_manager.hpp:201
static std::vector< std::string > m_icon
Stores all item models.
Definition: item_manager.hpp:123
static scene::IMesh * getItemLowResolutionModel(ItemState::ItemType type)
Returns the low resolution mesh for a certain item.
Definition: item_manager.hpp:90
static void loadDefaultItemMeshes()
Loads the default item meshes (high- and low-resolution).
Definition: item_manager.cpp:60
static std::vector< scene::IMesh * > m_item_mesh
Stores all item models.
Definition: item_manager.hpp:117
virtual void switchItems()
Switches all items: boxes become bananas and vice versa for a certain amount of time (as defined in s...
Definition: item_manager.cpp:560
int m_switch_ticks
Remaining time that items should remain switched.
Definition: item_manager.hpp:128
Contains the state information of an item, i.e.
Definition: item.hpp:53
ItemType
The list of all items.
Definition: item.hpp:62
Definition: item.hpp:324
The main kart class.
Definition: kart.hpp:72
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35