SuperTuxKart
linear_world.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2004-2015 SuperTuxKart-Team
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 #ifndef HEADER_LINEAR_WORLD_HPP
19 #define HEADER_LINEAR_WORLD_HPP
20 
21 #include "modes/world_with_rank.hpp"
22 #include "utils/aligned_array.hpp"
23 
24 #include <climits>
25 #include <vector>
26 
27 class SFXBase;
28 
29 /*
30  * A 'linear world' is a subcategory of world used in 'standard' races, i.e.
31  * with a start line and a road that loops. This includes management of drivelines
32  * and lap counting.
33  * \ingroup modes
34  */
35 class LinearWorld : public WorldWithRank
36 {
37 private:
40 
43 
44  bool m_last_lap_sfx_playing;
45 
48 
51 
52  core::stringw m_fastest_lap_kart_name;
53 
62 
67 
70 
71  /* if set then the game will auto end after this time for networking */
72  float m_finish_timeout;
73 
78  void updateLiveDifference();
79 
80  // ------------------------------------------------------------------------
84  class KartInfo
85  {
86  public:
89 
92 
95 
98 
102 
106 
108  KartInfo() { reset(); }
109  // --------------------------------------------------------------------
111  void reset()
112  {
113  m_finished_laps = -1;
114  m_lap_start_ticks = 0;
115  m_ticks_at_last_lap = INT_MAX;
116  m_estimated_finish = -1.0f;
117  m_overall_distance = 0.0f;
118  m_wrong_way_timer = 0.0f;
119  } // reset
120  // --------------------------------------------------------------------
121  void saveCompleteState(BareNetworkString* bns);
122  // --------------------------------------------------------------------
123  void restoreCompleteState(const BareNetworkString& b);
124  };
125  // ------------------------------------------------------------------------
126 
127 protected:
128 
133  std::vector<KartInfo> m_kart_info;
134 
135  virtual void checkForWrongDirection(unsigned int i, float dt);
136  virtual float estimateFinishTimeForKart(AbstractKart* kart) OVERRIDE;
137 
138 public:
139  LinearWorld();
143  virtual void init() OVERRIDE;
144  virtual ~LinearWorld();
145 
146  virtual void update(int ticks) OVERRIDE;
147  virtual void updateGraphics(float dt) OVERRIDE;
148  float getDistanceDownTrackForKart(const int kart_id,
149  bool account_for_checklines) const;
150  void updateTrackSectors();
151  void updateRacePosition();
152  float getDistanceToCenterForKart(const int kart_id) const;
153  float getEstimatedFinishTime(const int kart_id) const;
154  int getLapForKart(const int kart_id) const;
155  int getTicksAtLapForKart(const int kart_id) const;
156  float getLiveTimeDifference() const { return m_live_time_difference; }
157  bool hasValidTimeDifference() const { return m_valid_reference_time; }
158 
159  virtual void getKartsDisplayInfo(
160  std::vector<RaceGUIBase::KartIconDisplayInfo> *info) OVERRIDE;
161 
162  virtual unsigned int getNumberOfRescuePositions() const OVERRIDE;
163  virtual unsigned int getRescuePositionIndex(AbstractKart *kart) OVERRIDE;
164  virtual btTransform getRescueTransform(unsigned int index) const OVERRIDE;
165  virtual void reset(bool restart=false) OVERRIDE;
166  virtual void newLap(unsigned int kart_index) OVERRIDE;
167 
168  // ------------------------------------------------------------------------
170  virtual bool raceHasLaps() OVERRIDE { return true; }
171  // ------------------------------------------------------------------------
173  virtual bool haveBonusBoxes() OVERRIDE { return true; }
174  // ------------------------------------------------------------------------
176  virtual bool useChecklineRequirements() const OVERRIDE { return true; }
177  // ------------------------------------------------------------------------
180  int getFinishedLapsOfKart(unsigned int kart_index) const OVERRIDE
181  {
182  assert(kart_index < m_kart_info.size());
183  return m_kart_info[kart_index].m_finished_laps;
184  } // getkartLap
185  // ------------------------------------------------------------------------
186  void setLastTriggeredCheckline(unsigned int kart_index, int index);
187  // ------------------------------------------------------------------------
191  float getOverallDistance(unsigned int kart_index) const
192  {
193  return m_kart_info[kart_index].m_overall_distance;
194  } // getOverallDistance
195  // ------------------------------------------------------------------------
197  float getFastestLap() const
198  {
199  return stk_config->ticks2Time(m_fastest_lap_ticks);
200  }
201  // ------------------------------------------------------------------------
203  stringw getFastestLapKartName() const
204  {
205  return m_fastest_lap_kart_name;
206  }
207  // ------------------------------------------------------------------------
209  int getFastestLapTicks() const
210  {
211  return m_fastest_lap_ticks;
212  }
213  // ------------------------------------------------------------------------
215  void setFastestLapTicks(int ticks)
216  {
217  m_fastest_lap_ticks = ticks;
218  }
219  // ------------------------------------------------------------------------
221  void setFastestKartName(const stringw& name)
222  {
223  m_fastest_lap_kart_name = name;
224  }
225  // ------------------------------------------------------------------------
226  virtual std::pair<uint32_t, uint32_t> getGameStartedProgress() const
227  OVERRIDE;
228  // ------------------------------------------------------------------------
229  virtual void saveCompleteState(BareNetworkString* bns,
230  STKPeer* peer) OVERRIDE;
231  // ------------------------------------------------------------------------
232  virtual void restoreCompleteState(const BareNetworkString& b) OVERRIDE;
233  // ------------------------------------------------------------------------
234  void updateCheckLinesServer(int check_id, int kart_id);
235  // ------------------------------------------------------------------------
236  void updateCheckLinesClient(const BareNetworkString& b);
237  // ------------------------------------------------------------------------
238  void handleServerCheckStructureCount(unsigned count);
239 }; // LinearWorld
240 
241 #endif
virtual unsigned int getNumberOfRescuePositions() const OVERRIDE
Returns the number of rescue positions on a given track, which in linear races is just the number of ...
Definition: linear_world.cpp:804
void reset()
Re-initialises all data.
Definition: linear_world.hpp:111
virtual void reset(bool restart=false) OVERRIDE
Called before a race is started (or restarted).
Definition: linear_world.cpp:106
int getFinishedLapsOfKart(unsigned int kart_index) const OVERRIDE
Returns the number of laps a kart has completed.
Definition: linear_world.hpp:180
float getDistanceDownTrackForKart(const int kart_id, bool account_for_checklines) const
Returns the distance the kart has travelled along the track since crossing the start line.
Definition: linear_world.cpp:558
float getEstimatedFinishTime(const int kart_id) const
Returns the estimated finishing time.
Definition: linear_world.cpp:584
bool m_check_structure_compatible
True if clients and server has the same check structure.
Definition: linear_world.hpp:47
float ticks2Time(int ticks)
Converts a tick value (in physics time step size) into seconds.
Definition: stk_config.hpp:277
SFXBase * m_last_lap_sfx
Sfx for the final lap.
Definition: linear_world.hpp:39
int m_finished_laps
Number of finished laps.
Definition: linear_world.hpp:88
void setFastestKartName(const stringw &name)
Network use: set fastest kart name.
Definition: linear_world.hpp:221
bool m_valid_reference_time
True if the live_time_difference is invalid.
Definition: linear_world.hpp:69
virtual void updateGraphics(float dt) OVERRIDE
This updates all only graphical elements.It is only called once per rendered frame,...
Definition: linear_world.cpp:302
bool m_last_lap_sfx_played
Last lap sfx should only be played once.
Definition: linear_world.hpp:42
stringw getFastestLapKartName() const
Returns the kart name that made the fastest lap time.
Definition: linear_world.hpp:203
float m_wrong_way_timer
Accumulates the time a kart has been driving in the wrong direction so that a message can be displaye...
Definition: linear_world.hpp:105
void updateRacePosition()
Find the position (rank) of every kart.
Definition: linear_world.cpp:853
int m_fastest_lap_ticks
The fastest lap time, in ticks of physics dt.
Definition: linear_world.hpp:50
std::vector< KartInfo > m_kart_info
This vector contains an 'KartInfo' struct for every kart in the race.
Definition: linear_world.hpp:133
A WorldWithRank is a world where the karts are ranked.
Definition: world_with_rank.hpp:38
virtual btTransform getRescueTransform(unsigned int index) const OVERRIDE
Returns the bullet transformation for the specified rescue index.
Definition: linear_world.cpp:832
float getFastestLap() const
Returns time for the fastest laps.
Definition: linear_world.hpp:197
float m_distance_increase
The track length returned by Track::getLength() only covers the distance from start line to finish li...
Definition: linear_world.hpp:61
virtual void newLap(unsigned int kart_index) OVERRIDE
Is called by check structures if a kart starts a new lap.
Definition: linear_world.cpp:374
void updateCheckLinesServer(int check_id, int kart_id)
Called in server whenever a kart cross a check line, it send server current kart lap count,...
Definition: linear_world.cpp:1208
KartInfo()
Initialises all fields.
Definition: linear_world.hpp:108
virtual void getKartsDisplayInfo(std::vector< RaceGUIBase::KartIconDisplayInfo > *info) OVERRIDE
Called by the code that draws the list of karts on the race GUI to know what needs to be drawn in the...
Definition: linear_world.cpp:598
float m_live_time_difference
This stores the live time difference between a ghost kart and a second kart racing against it (normal...
Definition: linear_world.hpp:66
int m_ticks_at_last_lap
Time at finishing last lap.
Definition: linear_world.hpp:91
virtual unsigned int getRescuePositionIndex(AbstractKart *kart) OVERRIDE
Determines the rescue position for a kart.
Definition: linear_world.cpp:810
LinearWorld()
Constructs the linear world.
Definition: linear_world.cpp:60
virtual bool haveBonusBoxes() OVERRIDE
Returns if this race mode has bonus items.
Definition: linear_world.hpp:173
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:52
Some additional info that needs to be kept for each kart in this kind of race.
Definition: linear_world.hpp:84
float m_overall_distance
How far the kart has travelled (this is (number-of-laps-1) times track-length plus distance-along-tra...
Definition: linear_world.hpp:101
virtual bool useChecklineRequirements() const OVERRIDE
Override settings from base class.
Definition: linear_world.hpp:176
float getDistanceToCenterForKart(const int kart_id) const
Gets the distance of the kart from the center of the driveline.
Definition: linear_world.cpp:568
virtual float estimateFinishTimeForKart(AbstractKart *kart) OVERRIDE
Estimate the arrival time of any karts that haven't arrived yet by using their average speed up to no...
Definition: linear_world.cpp:732
virtual void init() OVERRIDE
call just after instanciating.
Definition: linear_world.cpp:77
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:69
float getOverallDistance(unsigned int kart_index) const
Returns how far the kart has driven so far (i.e.
Definition: linear_world.hpp:191
Definition: linear_world.hpp:35
void setFastestLapTicks(int ticks)
Network use: set fastest lap in ticks.
Definition: linear_world.hpp:215
virtual bool raceHasLaps() OVERRIDE
Returns if this race mode has laps.
Definition: linear_world.hpp:170
virtual std::pair< uint32_t, uint32_t > getGameStartedProgress() const OVERRIDE
Used by server to get the current started game progress in either or both remaining time or progress ...
Definition: linear_world.cpp:1103
The base class for sound effects.
Definition: sfx_base.hpp:42
int getFastestLapTicks() const
Network use: get fastest lap in ticks.
Definition: linear_world.hpp:209
virtual void checkForWrongDirection(unsigned int i, float dt)
Checks if a kart is going in the wrong direction.
Definition: linear_world.cpp:1035
int m_lap_start_ticks
Time at start of a new lap.
Definition: linear_world.hpp:94
void updateLiveDifference()
This calculate the time difference between the second kart in the race (there must be at least two) a...
Definition: linear_world.cpp:335
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:61
float m_estimated_finish
During last lap only: estimated finishing time!
Definition: linear_world.hpp:97
virtual void update(int ticks) OVERRIDE
General update function called once per frame.
Definition: linear_world.cpp:173