SuperTuxKart
max_speed.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2010-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_MAX_SPEED_HPP
20 #define HEADER_MAX_SPEED_HPP
21 
22 #include "utils/types.hpp"
23 #include <limits>
24 
27 class AbstractKart;
28 class BareNetworkString;
29 
30 class MaxSpeed
31 {
32 friend class KartRewinder;
33 public:
37  enum {MS_INCREASE_MIN,
38  MS_INCREASE_ZIPPER = MS_INCREASE_MIN,
39  MS_INCREASE_SLIPSTREAM,
40  MS_INCREASE_NITRO,
41  MS_INCREASE_RUBBER,
42  MS_INCREASE_SKIDDING,
43  MS_INCREASE_RED_SKIDDING,
44  MS_INCREASE_MAX};
45 
48  enum {MS_DECREASE_MIN,
49  MS_DECREASE_TERRAIN = MS_DECREASE_MIN,
50  MS_DECREASE_AI,
51  MS_DECREASE_BUBBLE,
52  MS_DECREASE_SQUASH,
53  MS_DECREASE_MAX};
54 
55 private:
58 
61 
64 
66  float m_min_speed;
67 
68  // ------------------------------------------------------------------------
71  {
72  public:
74  uint16_t m_max_add_speed;
79  int16_t m_duration;
81  int16_t m_fade_out_time;
85  uint16_t m_engine_force;
89  {
90  reset();
91  } // SpeedIncrease
92  // --------------------------------------------------------------------
94  void reset()
95  {
96  m_max_add_speed = 0;
97  m_duration = std::numeric_limits<int16_t>::min();
98  m_fade_out_time = 0;
100  m_engine_force = 0;
101  } // reset
102  // --------------------------------------------------------------------
103  void update(int ticks);
104  void saveState(BareNetworkString *buffer) const;
105  void rewindTo(BareNetworkString *buffer, bool is_active);
106  // --------------------------------------------------------------------
108  float getSpeedIncrease() const {return m_current_speedup;}
109  // --------------------------------------------------------------------
114  int getTimeLeft() const {return m_duration; }
115  // --------------------------------------------------------------------
117  float getEngineForce() const
118  {
119  return m_duration > 0 ? (float)m_engine_force / 10.0f : 0;
120  } // getEngineForce
121  // --------------------------------------------------------------------
123  bool isActive() const { return m_duration > -m_fade_out_time; }
124  }; // SpeedIncrease
125 
126  // ------------------------------------------------------------------------
129  {
130  public:
136 
139 
142  int16_t m_duration;
143 
147  {
148  reset();
149  } // SpeedDecrease
150  // --------------------------------------------------------------------
152  void reset()
153  {
154  m_max_speed_fraction = 1000;
155  m_current_fraction = 1.0f;
156  m_fade_in_ticks = 0;
157  m_duration = 0;
158  } //reset
159  // --------------------------------------------------------------------
160  void update(int ticks);
161  void saveState(BareNetworkString *buffer) const;
162  void rewindTo(BareNetworkString *buffer, bool is_active);
163  // --------------------------------------------------------------------
166  float getSlowdownFraction() const { return m_current_fraction; }
167  // --------------------------------------------------------------------
168  int getTimeLeft() const { return m_duration; }
169  // --------------------------------------------------------------------
172  bool isActive() const { return m_duration > 0 || m_duration <= -1; }
173  }; // SpeedDecrease
174 
175  // ------------------------------------------------------------------------
178  SpeedDecrease m_speed_decrease[MS_DECREASE_MAX];
179 
182  SpeedIncrease m_speed_increase[MS_INCREASE_MAX];
183 
184 
185 public:
186  MaxSpeed(AbstractKart *kart);
187 
188  void increaseMaxSpeed(unsigned int category, float add_speed,
189  float engine_force, int duration,
190  int fade_out_time);
191  void instantSpeedIncrease(unsigned int category,
192  float add_speed, float speed_boost,
193  float engine_force, int duration,
194  int fade_out_time);
195  void setSlowdown(unsigned int category, float max_speed_fraction,
196  int fade_in_time, int duration=-1);
197  int getSpeedIncreaseTicksLeft(unsigned int category);
198  int isSpeedIncreaseActive(unsigned int category);
199  int isSpeedDecreaseActive(unsigned int category);
200  void update(int ticks);
201  void reset();
202  void saveState(BareNetworkString *buffer) const;
203  void rewindTo(BareNetworkString *buffer);
204  // ------------------------------------------------------------------------
209  void setMinSpeed(float s) { m_min_speed = s; }
210  // ------------------------------------------------------------------------
212  float getCurrentMaxSpeed() const { return m_current_max_speed; }
213  // ------------------------------------------------------------------------
216 }; // MaxSpeed
217 #endif
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: kart_rewinder.hpp:30
An internal class to store and handle speed decrease related data.
Definition: max_speed.hpp:129
float m_current_fraction
The current slowdown fraction, taking the fade-in time into account.
Definition: max_speed.hpp:135
void rewindTo(BareNetworkString *buffer, bool is_active)
Restores a previously saved state for an active speed decrease category.
Definition: max_speed.cpp:350
void saveState(BareNetworkString *buffer) const
Saves the state of an (active) speed decrease category.
Definition: max_speed.cpp:339
SpeedDecrease()
The constructor initialises the data with data that won't affect top speed at all.
Definition: max_speed.hpp:146
bool isActive() const
Returns if this speed decrease is active atm.
Definition: max_speed.hpp:172
void reset()
Resets the state to be inactive.
Definition: max_speed.hpp:152
float getSlowdownFraction() const
Returns the current slowdown fracftion, taking a 'fade in' into account.
Definition: max_speed.hpp:166
int16_t m_fade_in_ticks
How long it should take for the full slowdown to take effect.
Definition: max_speed.hpp:138
uint16_t m_max_speed_fraction
The maximum slowdown to apply, 3 digits precision.
Definition: max_speed.hpp:132
int16_t m_duration
How long the effect should last.
Definition: max_speed.hpp:142
void update(int ticks)
Handles the speed increase for a certain category.
Definition: max_speed.cpp:306
An internal class to store and handle speed increase related data.
Definition: max_speed.hpp:71
float m_current_speedup
The current max speed increase value, updated with duration.
Definition: max_speed.hpp:83
bool isActive() const
Returns if this speed increase is active atm.
Definition: max_speed.hpp:123
int16_t m_duration
How long this speed will apply.
Definition: max_speed.hpp:79
int getTimeLeft() const
Returns the remaining time till the fade out time starts.
Definition: max_speed.hpp:114
float getSpeedIncrease() const
Returns the current speedup for this category.
Definition: max_speed.hpp:108
uint16_t m_max_add_speed
The maximum additional speed allowed, 3 digits precision.
Definition: max_speed.hpp:74
int16_t m_fade_out_time
The fadeout time.
Definition: max_speed.hpp:81
uint16_t m_engine_force
Additional engine force, 1 digit precision.
Definition: max_speed.hpp:85
SpeedIncrease()
The constructor initialised the values with a no-increase entry, i.e.
Definition: max_speed.hpp:88
void update(int ticks)
Handles the update of speed increase objects.
Definition: max_speed.cpp:194
float getEngineForce() const
Returns the additional engine force for this speed increase.
Definition: max_speed.hpp:117
void reset()
Resets this increase category to be not active.
Definition: max_speed.hpp:94
Definition: max_speed.hpp:31
float m_min_speed
If >0 then the minimum speed a kart should have (used for zippers).
Definition: max_speed.hpp:66
int getSpeedIncreaseTicksLeft(unsigned int category)
Returns how much increased speed time is left over in the given category.
Definition: max_speed.cpp:370
void instantSpeedIncrease(unsigned int category, float add_speed, float speed_boost, float engine_force, int duration, int fade_out_time)
This adjusts the top speed using increaseMaxSpeed, but additionally causes an instant speed boost,...
Definition: max_speed.cpp:166
float m_current_max_speed
The current maximum speed.
Definition: max_speed.hpp:60
SpeedIncrease m_speed_increase[MS_INCREASE_MAX]
Stores all speed increase related information for each possible category.
Definition: max_speed.hpp:182
int isSpeedIncreaseActive(unsigned int category)
Returns if increased speed is active in the given category.
Definition: max_speed.cpp:379
SpeedDecrease m_speed_decrease[MS_DECREASE_MAX]
Stores all speed decrease related information for each possible category.
Definition: max_speed.hpp:178
AbstractKart * m_kart
A pointer to the kart to which this speed handling object belongs.
Definition: max_speed.hpp:57
float getCurrentMaxSpeed() const
Returns the current maximum speed for this kart.
Definition: max_speed.hpp:212
MaxSpeed(AbstractKart *kart)
This class handles maximum speed for karts.
Definition: max_speed.cpp:51
int isSpeedDecreaseActive(unsigned int category)
Returns if decreased speed is active in the given category.
Definition: max_speed.cpp:388
void reset()
Reset to prepare for a restart.
Definition: max_speed.cpp:66
float m_add_engine_force
Additional engine force, summed from all SpeedIncrease engine forces.
Definition: max_speed.hpp:63
void rewindTo(BareNetworkString *buffer)
Restore a saved state.
Definition: max_speed.cpp:510
void setSlowdown(unsigned int category, float max_speed_fraction, int fade_in_time, int duration=-1)
Defines a slowdown, which is in fraction of top speed.
Definition: max_speed.cpp:256
void setMinSpeed(float s)
Sets the minimum speed a kart should have.
Definition: max_speed.hpp:209
void increaseMaxSpeed(unsigned int category, float add_speed, float engine_force, int duration, int fade_out_time)
Sets an increased maximum speed for a category.
Definition: max_speed.cpp:93
float getCurrentAdditionalEngineForce() const
Returns the additional engine force.
Definition: max_speed.hpp:215
void saveState(BareNetworkString *buffer) const
Saves the speed data in a network string for rewind.
Definition: max_speed.cpp:461
void update(int ticks)
Updates all speed increase and decrease objects, and determines the current maximum speed.
Definition: max_speed.cpp:400
Declares the general types that are used by the network.