SuperTuxKart
item.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 //
3 // Copyright (C) 2004-2015 Steve Baker <sjbaker1@airmail.net>
4 // Copyright (C) 2006-2015 SuperTuxKart-Team
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 3
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 #ifndef HEADER_ITEM_HPP
21 #define HEADER_ITEM_HPP
22 
27 #include "utils/cpp2011.hpp"
28 #include "utils/leak_check.hpp"
29 #include "utils/log.hpp"
30 #include "utils/no_copy.hpp"
31 #include "utils/vec3.hpp"
32 
33 #include <line3d.h>
34 
35 class BareNetworkString;
36 class AbstractKart;
37 class LODNode;
38 
39 namespace irr
40 {
41  namespace scene { class IMesh; class ISceneNode; }
42 }
43 using namespace irr;
44 
45 // ============================================================================
52 class ItemState
53 {
54  LEAK_CHECK();
55 public:
61  enum ItemType
62  {
63  ITEM_FIRST,
64  ITEM_BONUS_BOX = ITEM_FIRST,
65  ITEM_BANANA,
66  ITEM_NITRO_BIG,
67  ITEM_NITRO_SMALL,
68  ITEM_BUBBLEGUM,
69  ITEM_BUBBLEGUM_NOLOK,
70 
73  ITEM_LAST = ITEM_EASTER_EGG,
74  ITEM_COUNT,
75  ITEM_NONE
76  };
77 
78 private:
81 
85 
90 
93  int m_item_id;
94 
98 
105 
108 
115  btQuaternion m_original_rotation;
116 
120 
121 protected:
122 
123  friend class ItemManager;
124  friend class NetworkItemManager;
125  // ------------------------------------------------------------------------
126  virtual void setType(ItemType type) { m_type = type; }
127  // ------------------------------------------------------------------------
128  // Some convenient functions for the AI only
129  friend class SkiddingAI;
130  friend class TestAI;
136  bool hitLine(const core::line3df &line,
137  const AbstractKart *kart = NULL) const
138  {
139  if (getPreviousOwner() == kart && getDeactivatedTicks() > 0)
140  return false;
141 
142  Vec3 closest = line.getClosestPoint(getXYZ().toIrrVector());
143  return hitKart(closest, kart);
144  } // hitLine
145 
146 public:
147  // ------------------------------------------------------------------------
148  ItemState(ItemType type, const AbstractKart *owner=NULL, int id = -1);
149  // ------------------------------------------------------------------------
150  ItemState(const BareNetworkString& buffer);
151  // ------------------------------------------------------------------------
152  void initItem(ItemType type, const Vec3& xyz, const Vec3& normal);
153  void update(int ticks);
154  void setDisappearCounter();
155  virtual void collected(const AbstractKart *kart);
156  // ------------------------------------------------------------------------
157  virtual ~ItemState() {}
158 
159  // -----------------------------------------------------------------------
162  virtual void updateGraphics(float dt)
163  {
164  Log::fatal("ItemState", "updateGraphics() called for ItemState.");
165  } // updateGraphics
166 
167  // -----------------------------------------------------------------------
168  virtual bool hitKart(const Vec3 &xyz,
169  const AbstractKart *kart = NULL) const
170  {
171  Log::fatal("ItemState", "hitKart() called for ItemState.");
172  return false;
173  } // hitKart
174 
175  // -----------------------------------------------------------------------
176  virtual int getGraphNode() const
177  {
178  Log::fatal("ItemState", "getGraphNode() called for ItemState.");
179  return 0;
180  } // getGraphNode
181 
182  // -----------------------------------------------------------------------
183  virtual const Vec3 *getAvoidancePoint(bool left) const
184  {
185  Log::fatal("ItemState", "getAvoidancePoint() called for ItemState.");
186  // Return doesn't matter, fatal aborts
187  return &m_xyz;
188  } // getAvoidancePoint
189 
190  // -----------------------------------------------------------------------
191  virtual float getDistanceFromCenter() const
192  {
193  Log::fatal("itemState",
194  "getDistanceFromCentre() called for ItemState.");
195  return 0;
196  } // getDistanceFromCentre
197 
198  // -----------------------------------------------------------------------
200  virtual void reset()
201  {
202  m_deactive_ticks = 0;
203  m_ticks_till_return = 0;
204  setDisappearCounter();
205  // If the item was switched:
206  if (m_original_type != ITEM_NONE)
207  {
208  setType(m_original_type);
209  m_original_type = ITEM_NONE;
210  }
211  } // reset
212 
213  // -----------------------------------------------------------------------
218  virtual void switchTo(ItemType type)
219  {
220  // triggers and easter eggs should not be switched
221  if (m_type == ITEM_EASTER_EGG) return;
222  m_original_type = m_type;
223  setType(type);
224  return;
225  } // switchTo
226 
227  // ------------------------------------------------------------------------
230  virtual bool switchBack()
231  {
232  // If the item is not switched, do nothing. This can happen if a bubble
233  // gum is dropped while items are switched - when switching back, this
234  // bubble gum has no original type.
235  if (m_original_type == ITEM_NONE)
236  return true;
237  setType(m_original_type);
238  m_original_type = ITEM_NONE;
239  return false;
240  } // switchBack
241 
242  // ------------------------------------------------------------------------
244  bool isNegativeItem() const
245  {
246  return m_type == ITEM_BANANA || m_type == ITEM_BUBBLEGUM ||
247  m_type == ITEM_BUBBLEGUM_NOLOK;
248  }
249  // ------------------------------------------------------------------------
258  void setTicksTillReturn(int t) { m_ticks_till_return = t; }
259  // ------------------------------------------------------------------------
261  int getTicksTillReturn() const { return m_ticks_till_return; }
262  // ------------------------------------------------------------------------
264  bool isAvailable() const { return m_ticks_till_return <= 0; }
265  // ------------------------------------------------------------------------
267  ItemType getType() const { return m_type; }
268  // ------------------------------------------------------------------------
269  ItemType getGrahpicalType() const;
270  // ------------------------------------------------------------------------
272  ItemType getOriginalType() const { return m_original_type; }
273  // ------------------------------------------------------------------------
275  void setItemId(unsigned int n) { m_item_id = n; }
276  // ------------------------------------------------------------------------
278  unsigned int getItemId() const { return m_item_id; }
279  // ------------------------------------------------------------------------
281  bool isUsedUp() const { return m_used_up_counter == 0; }
282  // ------------------------------------------------------------------------
285  bool canBeUsedUp() const { return m_used_up_counter>-1; }
286  // ------------------------------------------------------------------------
289  int getDeactivatedTicks() const { return m_deactive_ticks; }
290  // ------------------------------------------------------------------------
293  void setDeactivatedTicks(int ticks) { m_deactive_ticks = ticks; }
294  // ------------------------------------------------------------------------
297  const AbstractKart *getPreviousOwner() const { return m_previous_owner; }
298  // ------------------------------------------------------------------------
299  void setXYZ(const Vec3& xyz) { m_xyz = xyz; }
300  // ------------------------------------------------------------------------
302  const Vec3& getXYZ() const { return m_xyz; }
303  // ------------------------------------------------------------------------
305  const Vec3 getNormal() const
306  {
307  return quatRotate(m_original_rotation, Vec3(0.0f, 1.0f, 0.0f));
308  }
309  // ------------------------------------------------------------------------
311  const btQuaternion& getOriginalRotation() const
312  {
313  return m_original_rotation;
314  }
315  // ------------------------------------------------------------------------
316  void saveCompleteState(BareNetworkString* buffer) const;
317 }; // class ItemState
318 
319 // ============================================================================
323 class Item : public ItemState, public NoCopy
324 {
325 
326 private:
329 
332 
334  std::vector<scene::ISceneNode*> m_spark_nodes;
335 
337  scene::ISceneNode* m_icon_node;
338 
341 
344 
347 
352 
355 
358  Vec3 *m_avoidance_points[2];
359 
360  void initItem(ItemType type, const Vec3 &xyz, const Vec3 &normal);
361  void setMesh(scene::IMesh* mesh, scene::IMesh* lowres_mesh);
362  void handleNewMesh(ItemType type);
363 
364 public:
365  Item(ItemType type, const Vec3& xyz, const Vec3& normal,
366  scene::IMesh* mesh, scene::IMesh* lowres_mesh,
367  const std::string& icon, const AbstractKart *owner);
368  virtual ~Item ();
369  virtual void updateGraphics(float dt) OVERRIDE;
370  virtual void reset() OVERRIDE;
371 
372  //-------------------------------------------------------------------------
377  virtual void collected(const AbstractKart *kart) OVERRIDE
378  {
379  ItemState::collected(kart);
380  } // isCollected
381  //-------------------------------------------------------------------------
387  virtual bool switchBack() OVERRIDE
388  {
389  if (ItemState::switchBack())
390  return true;
391  return false;
392  } // switchBack
393  // ------------------------------------------------------------------------
401  virtual bool hitKart(const Vec3 &xyz, const AbstractKart *kart=NULL) const
402  OVERRIDE
403  {
404  if (getPreviousOwner() == kart && getDeactivatedTicks() > 0)
405  return false;
406  Vec3 lc = quatRotate(getOriginalRotation(), xyz - getXYZ());
407  // Don't be too strict if the kart is a bit above the item
408  lc.setY(lc.getY() / 2.0f);
409  return lc.length2() < m_distance_2;
410  } // hitKart
411  // ------------------------------------------------------------------------
412  bool rotating() const { return getType() != ITEM_BUBBLEGUM; }
413 
414 public:
415  // ------------------------------------------------------------------------
417  virtual int getGraphNode() const OVERRIDE { return m_graph_node; }
418  // ------------------------------------------------------------------------
421  virtual float getDistanceFromCenter() const OVERRIDE
422  {
423  return m_distance_from_center;
424  } // getDistanceFromCenter
425  // ------------------------------------------------------------------------
430  virtual const Vec3 *getAvoidancePoint(bool left) const OVERRIDE
431  {
432  if(left) return m_avoidance_points[0];
433  return m_avoidance_points[1];
434  } // getAvoidancePoint
435  // ------------------------------------------------------------------------
436  scene::ISceneNode *getSceneNode()
437  {
438  return (scene::ISceneNode *) m_node;
439  }
440 }; // class Item
441 
442 #endif
ItemType getType() const
Returns the type of this item.
Definition: item.hpp:267
void setItemId(unsigned int n)
Sets the index of this item in the item manager list.
Definition: item.hpp:275
int m_item_id
Index in item_manager field.
Definition: item.hpp:93
int getDeactivatedTicks() const
Returns the number of ticks during which the item is deactivated (i.e.
Definition: item.hpp:289
std::vector< scene::ISceneNode * > m_spark_nodes
Vector containing the sparks.
Definition: item.hpp:334
A wrapper around bullets btVector3 to include conventient conversion functions (e.g.
Definition: vec3.hpp:34
virtual void reset()
Resets an item to its start state.
Definition: item.hpp:200
const AbstractKart * getPreviousOwner() const
Returns the kart that dropped this item (or NULL if the item was not dropped by a kart...
Definition: item.hpp:297
const Vec3 & getXYZ() const
Returns the XYZ position of the item.
Definition: item.hpp:302
bool m_was_available_previously
Stores if the item was available in the previously rendered frame.
Definition: item.hpp:340
virtual float getDistanceFromCenter() const OVERRIDE
Returns the distance from center: negative means left of center, positive means right of center...
Definition: item.hpp:421
int m_graph_node
The graph node this item is on.
Definition: item.hpp:346
int m_deactive_ticks
Optionally if item was placed by a kart, a timer can be used to temporarly deactivate collision so a ...
Definition: item.hpp:97
Definition: three_d_animation.hpp:32
ItemType
The list of all items.
Definition: item.hpp:61
bool hitLine(const core::line3df &line, const AbstractKart *kart=NULL) const
Returns true if the specified line segment would come close enough to this item so that this item wou...
Definition: item.hpp:136
virtual const Vec3 * getAvoidancePoint(bool left) const OVERRIDE
Returns a point to the left or right of the item which will not trigger a collection of this item...
Definition: item.hpp:430
virtual void update(int ticks)
Updates the ai base controller each time step.
scene::ISceneNode * m_icon_node
Billboard that shows when the item is about to respawn.
Definition: item.hpp:337
int getTicksTillReturn() const
Returns the time the item is disabled for.
Definition: item.hpp:261
virtual void updateGraphics(float dt)
Dummy implementation, causing an abort if it should be called to catch any errors early...
Definition: item.hpp:162
float m_distance_2
square distance at which item is collected
Definition: item.hpp:343
LODNode * m_node
Scene node of this item.
Definition: item.hpp:328
void setDeactivatedTicks(int ticks)
Sets the number of ticks during which the item is deactivated (i.e.
Definition: item.hpp:293
unsigned int getItemId() const
Returns the index of this item in the item manager list.
Definition: item.hpp:278
This is a test version of the AI, which can be used to create new AIs, and compare them with the curr...
Definition: test_ai.hpp:74
void setTicksTillReturn(int t)
Sets how long an item should be disabled.
Definition: item.hpp:258
ItemType m_graphical_type
Graphical type of the mesh.
Definition: item.hpp:331
Vec3 m_xyz
The position of this ItemState.
Definition: item.hpp:107
manages level-of-detail
Definition: lod_node.hpp:48
int m_animation_start_ticks
Time ticks since the item last respawned.
Definition: item.hpp:354
const btQuaternion & getOriginalRotation() const
Returns the original rotation of the item.
Definition: item.hpp:311
bool isNegativeItem() const
Returns if this item is negative, i.e.
Definition: item.hpp:244
Definition: item.hpp:323
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
int m_used_up_counter
Counts how often an item is used before it disappears.
Definition: item.hpp:104
virtual int getGraphNode() const OVERRIDE
Returns the index of the graph node this item is on.
Definition: item.hpp:417
const Vec3 getNormal() const
Returns the normal of the ItemState.
Definition: item.hpp:305
int m_ticks_till_return
Time till a collected item reappears.
Definition: item.hpp:89
btQuaternion m_original_rotation
The original rotation of the item.
Definition: item.hpp:115
bool isUsedUp() const
Returns true if this item is used up and can be removed.
Definition: item.hpp:281
virtual void collected(const AbstractKart *kart)
Called when the item is collected.
Definition: item.cpp:135
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:52
ItemType m_type
Item type.
Definition: item.hpp:80
virtual void switchTo(ItemType type)
Switches an item to be of a different type.
Definition: item.hpp:218
Definition: item_manager.hpp:46
float m_distance_from_center
Distance from the center of the quad this item is in.
Definition: item.hpp:351
For easter egg mode only.
Definition: item.hpp:72
virtual bool switchBack() OVERRIDE
Switch backs to the original item.
Definition: item.hpp:387
bool isAvailable() const
Returns true if this item is currently collected.
Definition: item.hpp:264
ItemType m_original_type
If the item is switched, this contains the original type.
Definition: item.hpp:84
virtual bool switchBack()
Returns true if this item was not actually switched (e.g.
Definition: item.hpp:230
bool canBeUsedUp() const
Returns true if this item can be used up, and therefore needs to be removed when the game is reset...
Definition: item.hpp:285
virtual bool hitKart(const Vec3 &xyz, const AbstractKart *kart=NULL) const OVERRIDE
Returns true if the Kart is close enough to hit this item, the item is not deactivated anymore...
Definition: item.hpp:401
const AbstractKart * m_previous_owner
The &#39;owner&#39; of the item, i.e.
Definition: item.hpp:119
Contains the state information of an item, i.e.
Definition: item.hpp:52
ItemType getOriginalType() const
Returns the original type of this item.
Definition: item.hpp:272
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:61
The network item manager is responsible for handling all network related item manager tasks - synchro...
Definition: network_item_manager.hpp:44
This is the actual racing AI.
Definition: skidding_ai.hpp:116