SuperTuxKart
flyable.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2007-2015 Joerg Henrichs
4 //
5 // Linear item-kart intersection function written by
6 // Copyright (C) 2009-2015 David Mikos.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 3
11 // of the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 
22 #ifndef HEADER_FLYABLE_HPP
23 #define HEADER_FLYABLE_HPP
24 
25 #include "items/powerup_manager.hpp"
26 #include "karts/moveable.hpp"
27 #include "network/rewinder.hpp"
28 #include "tracks/terrain_info.hpp"
29 #include "utils/cpp2011.hpp"
30 
31 #include <irrString.h>
32 namespace irr
33 {
34  namespace scene { class IMesh; }
35 }
36 using namespace irr;
37 
38 class AbstractKart;
40 class HitEffect;
41 class PhysicalObject;
42 class SFXBase;
43 class XMLNode;
44 
48 class Flyable : public Moveable, public TerrainInfo,
49  public Rewinder
50 {
51 private:
52  bool m_has_hit_something;
53 
58 
64 
71 
72  /* Used in network to restore previous gravity in compressed form. */
73  uint32_t m_compressed_gravity_vector;
74 
78 
79 protected:
82 
84  PowerupManager::PowerupType
86 
88  btCollisionShape *m_shape;
89 
91  float m_max_height;
92 
94  float m_min_height;
95 
98 
101 
103  float m_speed;
104 
106  const float m_mass;
107 
110 
114 
115  /* True if this flyable exists in server, and will trigger a rewind.
116  * For each local state it will reset it to false and call moveToInfinity,
117  * and for each restoreState it will set it to true. Also when re-fire the
118  * flyable during rewind it will set to true too. */
119  bool m_has_server_state;
120 
124 
125  /* Set to true once when onDeleteFlyable, this is used to create HitEffect
126  * only once. */
127  bool m_deleted_once;
128 
129  /* Save the locally detected deleted ticks, if the confirmed state world
130  * ticks in computeError > this, the flyable can be deleted in client. */
131  int m_last_deleted_ticks;
132 
133  // The flyable class stores the values for each flyable type, e.g.
134  // speed, min_height, max_height. These variables must be static,
135  // so we need arrays of these variables to have different values
136  // for bowling balls, missiles, ...
137 
139  static float m_st_speed[PowerupManager::POWERUP_MAX];
140 
142  static scene::IMesh *m_st_model[PowerupManager::POWERUP_MAX];
143 
145  static float m_st_min_height[PowerupManager::POWERUP_MAX];
146 
148  static float m_st_max_height[PowerupManager::POWERUP_MAX];
149 
151  static float m_st_force_updown[PowerupManager::POWERUP_MAX];
152 
154  static Vec3 m_st_extend[PowerupManager::POWERUP_MAX];
155 
159 
160  /* For debugging purpose */
161  int m_created_ticks;
162 
163  void getClosestKart(const AbstractKart **minKart,
164  float *minDistSquared,
165  Vec3 *minDelta,
166  const AbstractKart* inFrontOf=NULL,
167  const bool backwards=false) const;
168 
169  void getLinearKartItemIntersection(const Vec3 &origin,
170  const AbstractKart *target_kart,
171  float item_XY_velocity, float gravity,
172  float forw_offset,
173  float *fire_angle, float *up_velocity);
174 
175 
177  void createPhysics(float y_offset,
178  const Vec3 &velocity,
179  btCollisionShape *shape,
180  float restitution,
181  const btVector3& gravity=btVector3(0.0f,0.0f,0.0f),
182  const bool rotates=false,
183  const bool turn_around=false,
184  const btTransform* customDirection=NULL);
185 
186  void moveToInfinity(bool set_moveable_trans = true);
187  void removePhysics();
188  void fixSFXSplitscreen(SFXBase* sfx);
189 public:
190 
191  Flyable (AbstractKart* kart,
192  PowerupManager::PowerupType type,
193  float mass=1.0f);
194  virtual ~Flyable ();
195  static void init (const XMLNode &node, scene::IMesh *model,
196  PowerupManager::PowerupType type);
197  void updateGraphics(float dt) OVERRIDE;
198  virtual bool updateAndDelete(int ticks);
199  virtual void setAnimation(AbstractKartAnimation *animation);
200  virtual HitEffect* getHitEffect() const;
201  bool isOwnerImmunity(const AbstractKart *kart_hit) const;
202  virtual bool hit(AbstractKart* kart, PhysicalObject* obj=NULL);
203  void explode(AbstractKart* kart, PhysicalObject* obj=NULL,
204  bool secondary_hits=true);
205  unsigned int getOwnerId();
206  // ------------------------------------------------------------------------
208  bool hasAnimation() const { return m_animation != NULL; }
209  // ------------------------------------------------------------------------
213  void setAdjustUpVelocity(bool f) { m_adjust_up_velocity = f; }
214  // ------------------------------------------------------------------------
218  void setPositionOffset(const Vec3 &o) {m_position_offset = o; }
219  // ------------------------------------------------------------------------
221  virtual void hitTrack () {};
222  // ------------------------------------------------------------------------
226  bool hasHit () { return m_has_hit_something; }
227  // ------------------------------------------------------------------------
230  void setHasHit () { m_has_hit_something = true; }
231  // ------------------------------------------------------------------------
233  void reset() OVERRIDE { Moveable::reset(); }
234  // ------------------------------------------------------------------------
236  PowerupManager::PowerupType getType() const {return m_type;}
237 
238  // ------------------------------------------------------------------------
240  AbstractKart *getOwner() const { return m_owner;}
241  // ------------------------------------------------------------------------
245  void setDoTerrainInfo(bool d) { m_do_terrain_info = d; }
246  // ------------------------------------------------------------------------
248  const Vec3 &getExtend() const { return m_extend; }
249  // ------------------------------------------------------------------------
250  void addForRewind(const std::string& uid);
251  // ------------------------------------------------------------------------
252  virtual void undoEvent(BareNetworkString *buffer) OVERRIDE {}
253  // ------------------------------------------------------------------------
254  virtual void rewindToEvent(BareNetworkString *buffer) OVERRIDE {}
255  // ------------------------------------------------------------------------
256  virtual void undoState(BareNetworkString *buffer) OVERRIDE {}
257  // ------------------------------------------------------------------------
258  virtual void saveTransform() OVERRIDE;
259  // ------------------------------------------------------------------------
260  virtual void computeError() OVERRIDE;
261  // ------------------------------------------------------------------------
262  virtual BareNetworkString* saveState(std::vector<std::string>* ru)
263  OVERRIDE;
264  // ------------------------------------------------------------------------
265  virtual void restoreState(BareNetworkString *buffer, int count) OVERRIDE;
266  // ------------------------------------------------------------------------
267  /* Return true if still in game state, or otherwise can be deleted. */
268  bool hasServerState() const { return m_has_server_state; }
269  // ------------------------------------------------------------------------
272  virtual void onFireFlyable();
273  // ------------------------------------------------------------------------
274  virtual void onDeleteFlyable();
275  // ------------------------------------------------------------------------
276  void setCreatedTicks(int ticks) { m_created_ticks = ticks; }
277 }; // Flyable
278 
279 #endif
The base class for all kart animation, like rescue, explosion, or cannon.
Definition: abstract_kart_animation.hpp:60
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:62
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
Definition: flyable.hpp:50
virtual void undoState(BareNetworkString *buffer) OVERRIDE
Undo the effects of the given state, but do not rewind to that state (which is done by rewindTo).
Definition: flyable.hpp:256
void setPositionOffset(const Vec3 &o)
Sets the offset to be used when determining the terrain under the flyable.
Definition: flyable.hpp:218
PowerupManager::PowerupType getType() const
Returns the type of flyable.
Definition: flyable.hpp:236
const Vec3 & getExtend() const
Returns the size (extend) of the mesh.
Definition: flyable.hpp:248
bool m_owner_has_temporary_immunity
If set to true, the kart that throwns this flyable can't collide with it for a short time.
Definition: flyable.hpp:123
float m_min_height
Minimum height above terrain.
Definition: flyable.hpp:94
float m_average_height
Average of average of m_{min,ax}_height.
Definition: flyable.hpp:97
float m_speed
Speed of this Flyable.
Definition: flyable.hpp:103
int m_max_lifespan
Set to something > -1 if this flyable should auto-destrcut after that may ticks.
Definition: flyable.hpp:158
bool m_adjust_up_velocity
If this flag is set, the up velocity of the kart will not be adjusted in case that the objects is too...
Definition: flyable.hpp:57
AbstractKart * getOwner() const
Returns the owner's kart.
Definition: flyable.hpp:240
void setHasHit()
Indicates that something was hit and that this object must be removed.
Definition: flyable.hpp:230
PowerupManager::PowerupType m_type
Type of the powerup.
Definition: flyable.hpp:85
uint16_t m_ticks_since_thrown
Time since thrown.
Definition: flyable.hpp:113
bool m_do_terrain_info
If this variable is set to true (which is the default) flyable will update the height of terrain when...
Definition: flyable.hpp:70
AbstractKart * m_owner
Kart which shot this flyable.
Definition: flyable.hpp:81
float m_max_height
Maximum height above terrain.
Definition: flyable.hpp:91
void setAdjustUpVelocity(bool f)
If true the up velocity of the flyable will be adjust so that the flyable stays at a height close to ...
Definition: flyable.hpp:213
Vec3 m_position_offset
An offset that is added when doing the raycast for terrain.
Definition: flyable.hpp:63
const float m_mass
Mass of this Flyable.
Definition: flyable.hpp:106
Vec3 m_extend
Size of this flyable.
Definition: flyable.hpp:109
bool hasHit()
Enables/disables adjusting ov velocity depending on height above terrain.
Definition: flyable.hpp:226
void setDoTerrainInfo(bool d)
Sets wether Flyable should update TerrainInfo as part of its update call, or if the inheriting object...
Definition: flyable.hpp:245
btCollisionShape * m_shape
Collision shape of this Flyable.
Definition: flyable.hpp:88
virtual void undoEvent(BareNetworkString *buffer) OVERRIDE
Called when an event needs to be undone.
Definition: flyable.hpp:252
AbstractKartAnimation * m_animation
If the flyable is in a cannon, this is the pointer to the cannon animation.
Definition: flyable.hpp:77
void reset() OVERRIDE
Resets this flyable.
Definition: flyable.hpp:233
virtual void hitTrack()
Called when this flyable hits the track.
Definition: flyable.hpp:221
float m_force_updown
Force pushing the Flyable up.
Definition: flyable.hpp:100
bool hasAnimation() const
Returns if this flyable has an animation playing (e.g.
Definition: flyable.hpp:208
virtual void rewindToEvent(BareNetworkString *buffer) OVERRIDE
Called when an event needs to be replayed.
Definition: flyable.hpp:254
A small interface for effects to be used when a kart is hit.
Definition: hit_effect.hpp:33
Definition: moveable.hpp:46
virtual void reset()
The reset position must be set before calling reset.
Definition: moveable.cpp:104
Definition: physical_object.hpp:40
Definition: rewinder.hpp:44
The base class for sound effects.
Definition: sfx_base.hpp:43
This class stores information about the triangle that's under an object, i.e.
Definition: terrain_info.hpp:32
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
utility class used to parse XML files
Definition: xml_node.hpp:48