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 XMLNode;
43 
47 class Flyable : public Moveable, public TerrainInfo,
48  public Rewinder
49 {
50 private:
51  bool m_has_hit_something;
52 
57 
63 
70 
71  /* Used in network to restore previous gravity in compressed form. */
72  uint32_t m_compressed_gravity_vector;
73 
77 
78 protected:
81 
83  PowerupManager::PowerupType
85 
87  btCollisionShape *m_shape;
88 
90  float m_max_height;
91 
93  float m_min_height;
94 
97 
100 
102  float m_speed;
103 
105  const float m_mass;
106 
109 
113 
114  /* True if this flyable exists in server, and will trigger a rewind.
115  * For each local state it will reset it to false and call moveToInfinity,
116  * and for each restoreState it will set it to true. Also when re-fire the
117  * flyable during rewind it will set to true too. */
118  bool m_has_server_state;
119 
123 
124  /* Set to true once when onDeleteFlyable, this is used to create HitEffect
125  * only once. */
126  bool m_deleted_once;
127 
128  /* Save the locally detected deleted ticks, if the confirmed state world
129  * ticks in computeError > this, the flyable can be deleted in client. */
130  int m_last_deleted_ticks;
131 
132  // The flyable class stores the values for each flyable type, e.g.
133  // speed, min_height, max_height. These variables must be static,
134  // so we need arrays of these variables to have different values
135  // for bowling balls, missiles, ...
136 
138  static float m_st_speed[PowerupManager::POWERUP_MAX];
139 
141  static scene::IMesh *m_st_model[PowerupManager::POWERUP_MAX];
142 
144  static float m_st_min_height[PowerupManager::POWERUP_MAX];
145 
147  static float m_st_max_height[PowerupManager::POWERUP_MAX];
148 
150  static float m_st_force_updown[PowerupManager::POWERUP_MAX];
151 
153  static Vec3 m_st_extend[PowerupManager::POWERUP_MAX];
154 
158 
159  /* For debugging purpose */
160  int m_created_ticks;
161 
162  void getClosestKart(const AbstractKart **minKart,
163  float *minDistSquared,
164  Vec3 *minDelta,
165  const AbstractKart* inFrontOf=NULL,
166  const bool backwards=false) const;
167 
168  void getLinearKartItemIntersection(const Vec3 &origin,
169  const AbstractKart *target_kart,
170  float item_XY_velocity, float gravity,
171  float forw_offset,
172  float *fire_angle, float *up_velocity);
173 
174 
176  void createPhysics(float y_offset,
177  const Vec3 &velocity,
178  btCollisionShape *shape,
179  float restitution,
180  const btVector3& gravity=btVector3(0.0f,0.0f,0.0f),
181  const bool rotates=false,
182  const bool turn_around=false,
183  const btTransform* customDirection=NULL);
184 
185  void moveToInfinity(bool set_moveable_trans = true);
186  void removePhysics();
187 public:
188 
189  Flyable (AbstractKart* kart,
190  PowerupManager::PowerupType type,
191  float mass=1.0f);
192  virtual ~Flyable ();
193  static void init (const XMLNode &node, scene::IMesh *model,
194  PowerupManager::PowerupType type);
195  void updateGraphics(float dt) OVERRIDE;
196  virtual bool updateAndDelete(int ticks);
197  virtual void setAnimation(AbstractKartAnimation *animation);
198  virtual HitEffect* getHitEffect() const;
199  bool isOwnerImmunity(const AbstractKart *kart_hit) const;
200  virtual bool hit(AbstractKart* kart, PhysicalObject* obj=NULL);
201  void explode(AbstractKart* kart, PhysicalObject* obj=NULL,
202  bool secondary_hits=true);
203  unsigned int getOwnerId();
204  // ------------------------------------------------------------------------
206  bool hasAnimation() const { return m_animation != NULL; }
207  // ------------------------------------------------------------------------
211  void setAdjustUpVelocity(bool f) { m_adjust_up_velocity = f; }
212  // ------------------------------------------------------------------------
216  void setPositionOffset(const Vec3 &o) {m_position_offset = o; }
217  // ------------------------------------------------------------------------
219  virtual void hitTrack () {};
220  // ------------------------------------------------------------------------
224  bool hasHit () { return m_has_hit_something; }
225  // ------------------------------------------------------------------------
228  void setHasHit () { m_has_hit_something = true; }
229  // ------------------------------------------------------------------------
231  void reset() OVERRIDE { Moveable::reset(); }
232  // ------------------------------------------------------------------------
234  PowerupManager::PowerupType getType() const {return m_type;}
235 
236  // ------------------------------------------------------------------------
238  AbstractKart *getOwner() const { return m_owner;}
239  // ------------------------------------------------------------------------
243  void setDoTerrainInfo(bool d) { m_do_terrain_info = d; }
244  // ------------------------------------------------------------------------
246  const Vec3 &getExtend() const { return m_extend; }
247  // ------------------------------------------------------------------------
248  void addForRewind(const std::string& uid);
249  // ------------------------------------------------------------------------
250  virtual void undoEvent(BareNetworkString *buffer) OVERRIDE {}
251  // ------------------------------------------------------------------------
252  virtual void rewindToEvent(BareNetworkString *buffer) OVERRIDE {}
253  // ------------------------------------------------------------------------
254  virtual void undoState(BareNetworkString *buffer) OVERRIDE {}
255  // ------------------------------------------------------------------------
256  virtual void saveTransform() OVERRIDE;
257  // ------------------------------------------------------------------------
258  virtual void computeError() OVERRIDE;
259  // ------------------------------------------------------------------------
260  virtual BareNetworkString* saveState(std::vector<std::string>* ru)
261  OVERRIDE;
262  // ------------------------------------------------------------------------
263  virtual void restoreState(BareNetworkString *buffer, int count) OVERRIDE;
264  // ------------------------------------------------------------------------
265  /* Return true if still in game state, or otherwise can be deleted. */
266  bool hasServerState() const { return m_has_server_state; }
267  // ------------------------------------------------------------------------
270  virtual void onFireFlyable();
271  // ------------------------------------------------------------------------
272  virtual void onDeleteFlyable();
273  // ------------------------------------------------------------------------
274  void setCreatedTicks(int ticks) { m_created_ticks = ticks; }
275 }; // Flyable
276 
277 #endif
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:122
const Vec3 & getExtend() const
Returns the size (extend) of the mesh.
Definition: flyable.hpp:246
void setDoTerrainInfo(bool d)
Sets wether Flyable should update TerrainInfo as part of its update call, or if the inheriting object...
Definition: flyable.hpp:243
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:34
void reset() OVERRIDE
Resets this flyable.
Definition: flyable.hpp:231
Definition: rewinder.hpp:43
int m_max_lifespan
Set to something > -1 if this flyable should auto-destrcut after that may ticks.
Definition: flyable.hpp:157
A small interface for effects to be used when a kart is hit.
Definition: hit_effect.hpp:32
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:211
bool hasHit()
Enables/disables adjusting ov velocity depending on height above terrain.
Definition: flyable.hpp:224
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:69
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:56
btCollisionShape * m_shape
Collision shape of this Flyable.
Definition: flyable.hpp:87
AbstractKart * m_owner
Kart which shot this flyable.
Definition: flyable.hpp:80
AbstractKart * getOwner() const
Returns the owner's kart.
Definition: flyable.hpp:238
AbstractKartAnimation * m_animation
If the flyable is in a cannon, this is the pointer to the cannon animation.
Definition: flyable.hpp:76
PowerupManager::PowerupType getType() const
Returns the type of flyable.
Definition: flyable.hpp:234
void setPositionOffset(const Vec3 &o)
Sets the offset to be used when determining the terrain under the flyable.
Definition: flyable.hpp:216
virtual void hitTrack()
Called when this flyable hits the track.
Definition: flyable.hpp:219
Definition: physical_object.hpp:40
This class stores information about the triangle that's under an object, i.e.
Definition: terrain_info.hpp:31
Vec3 m_extend
Size of this flyable.
Definition: flyable.hpp:108
virtual void rewindToEvent(BareNetworkString *buffer) OVERRIDE
Called when an event needs to be replayed.
Definition: flyable.hpp:252
const float m_mass
Mass of this Flyable.
Definition: flyable.hpp:105
float m_average_height
Average of average of m_{min,ax}_height.
Definition: flyable.hpp:96
bool hasAnimation() const
Returns if this flyable has an animation playing (e.g.
Definition: flyable.hpp:206
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:52
uint16_t m_ticks_since_thrown
Time since thrown.
Definition: flyable.hpp:112
PowerupManager::PowerupType m_type
Type of the powerup.
Definition: flyable.hpp:84
float m_speed
Speed of this Flyable.
Definition: flyable.hpp:102
utility class used to parse XML files
Definition: xml_node.hpp:47
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:254
float m_min_height
Minimum height above terrain.
Definition: flyable.hpp:93
virtual void reset()
The reset position must be set before calling reset.
Definition: moveable.cpp:102
float m_force_updown
Force pushing the Flyable up.
Definition: flyable.hpp:99
float m_max_height
Maximum height above terrain.
Definition: flyable.hpp:90
virtual void undoEvent(BareNetworkString *buffer) OVERRIDE
Called when an event needs to be undone.
Definition: flyable.hpp:250
Definition: moveable.hpp:44
void setHasHit()
Indicates that something was hit and that this object must be removed.
Definition: flyable.hpp:228
Definition: flyable.hpp:47
The base class for all kart animation, like rescue, explosion, or cannon.
Definition: abstract_kart_animation.hpp:59
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:61
Vec3 m_position_offset
An offset that is added when doing the raycast for terrain.
Definition: flyable.hpp:62