SuperTuxKart
world.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2004-2015 SuperTuxKart-Team
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_WORLD_HPP
20 #define HEADER_WORLD_HPP
21 
28 #include <cstring>
29 #include <limits>
30 #include <map>
31 #include <memory>
32 #include <vector>
33 #include <stdexcept>
34 
35 #include "graphics/weather.hpp"
36 #include "modes/world_status.hpp"
37 #include "race/highscores.hpp"
38 #include "states_screens/race_gui_base.hpp"
39 #include "states_screens/state_manager.hpp"
40 #include "utils/random_generator.hpp"
41 #include "utils/stk_process.hpp"
42 
43 #include "LinearMath/btTransform.h"
44 
45 class AbstractKart;
46 class BareNetworkString;
47 class btRigidBody;
48 class Controller;
49 class ItemState;
50 class PhysicalObject;
51 class STKPeer;
52 
53 namespace Scripting
54 {
55  class ScriptEngine;
56 }
57 
58 namespace irr
59 {
60  namespace scene { class ISceneNode; }
61 }
62 
63 class AbortWorldUpdateException : public std::runtime_error
64 {
65 public:
66  AbortWorldUpdateException() : std::runtime_error("race abort") { };
67 };
68 
87 class World : public WorldStatus
88 {
89 public:
90  typedef std::vector<std::shared_ptr<AbstractKart> > KartList;
91 private:
93  static World *m_world[PT_COUNT];
94  // ------------------------------------------------------------------------
95  void setAITeam();
96  // ------------------------------------------------------------------------
97  std::shared_ptr<AbstractKart> createKartWithTeam
98  (const std::string &kart_ident, int index, int local_player_id,
99  int global_player_id, RaceManager::KartType type,
100  HandicapLevel handicap);
101 
102 protected:
103 
104 #ifdef DEBUG
105  unsigned int m_magic_number;
106 #endif
107 
108  /* Team related variables. */
109  int m_red_ai;
110  int m_blue_ai;
111  std::map<int, KartTeam> m_kart_team_map;
112  std::map<int, unsigned int> m_kart_position_map;
113 
115  KartList m_karts;
116  RandomGenerator m_random;
117 
118  AbstractKart* m_fastest_kart;
125 
126  bool m_faster_music_active; // true if faster music was activated
127 
128  bool m_stop_music_when_dialog_open;
129 
130  bool m_unfair_team;
131 
136 
137  void updateHighscores (int* best_highscore_rank);
138  void resetAllKarts ();
139  Controller*
141 
142  virtual std::shared_ptr<AbstractKart> createKart
143  (const std::string &kart_ident, int index, int local_player_id,
144  int global_player_id, RaceManager::KartType type,
145  HandicapLevel handicap);
146 
149 
156 
163 
170 
171  bool m_schedule_exit_race;
172 
173  bool m_schedule_tutorial;
174 
175  Phase m_scheduled_pause_phase;
176 
177  bool m_restart_camera;
178 
183 
186 
187  bool m_ended_early;
188 
189  virtual void onGo() OVERRIDE;
191  virtual bool isRaceOver() = 0;
192  virtual void update(int ticks) OVERRIDE;
193  virtual void createRaceGUI();
194  void updateTrack(int ticks);
195  // ------------------------------------------------------------------------
204  {return getTime(); }
205  void updateAchievementDataEndRace();
206  void updateAchievementModeCounters(bool start);
207 
208 public:
209  World();
210  virtual ~World();
211  // Static functions to access world:
212  // =================================
213  // ------------------------------------------------------------------------
215  static World* getWorld()
216  {
217  ProcessType type = STKProcess::getType();
218  return m_world[type];
219  }
220  // ------------------------------------------------------------------------
224  static void deleteWorld()
225  {
226  ProcessType type = STKProcess::getType();
227  delete m_world[type];
228  m_world[type] = NULL;
229  }
230  // ------------------------------------------------------------------------
233  static void setWorld(World *world)
234  {
235  ProcessType type = STKProcess::getType();
236  m_world[type] = world;
237  }
238  // ------------------------------------------------------------------------
239  static void clear() { memset(m_world, 0, sizeof(m_world)); }
240  // ------------------------------------------------------------------------
241 
242  // Pure virtual functions
243  // ======================
244 
247  virtual const std::string& getIdent() const = 0;
248  // ------------------------------------------------------------------------
251  virtual unsigned int getNumberOfRescuePositions() const;
252  // ------------------------------------------------------------------------
254  virtual unsigned int getRescuePositionIndex(AbstractKart *kart) = 0;
255  // ------------------------------------------------------------------------
257  virtual btTransform getRescueTransform(unsigned int index) const;
258  // ------------------------------------------------------------------------
259  virtual void moveKartAfterRescue(AbstractKart* kart);
260  // ------------------------------------------------------------------------
263  virtual bool raceHasLaps() = 0;
264  // ------------------------------------------------------------------------
266  virtual bool showLapsTarget() { return false; }
267  // ------------------------------------------------------------------------
270  virtual int getFinishedLapsOfKart(unsigned int kart_index) const
271  {
272  assert(false); return -1; // remove compiler warning
273  } // getFinishedLapsOfKart
274  // ------------------------------------------------------------------------
277  virtual void getKartsDisplayInfo(
278  std::vector<RaceGUIBase::KartIconDisplayInfo> *info)= 0;
279  // ------------------------------------------------------------------------
280 
281  // Virtual functions
282  // =================
283  virtual void init();
284  virtual void updateGraphics(float dt);
285  virtual void terminateRace() OVERRIDE;
286  virtual void reset(bool restart=false) OVERRIDE;
287  virtual void pause(Phase phase) OVERRIDE;
288  virtual void unpause() OVERRIDE;
289  virtual void getDefaultCollectibles(int *collectible_type,
290  int *amount );
291  // ------------------------------------------------------------------------
293  virtual void collectedItem(const AbstractKart *kart,
294  const ItemState *item ) {}
295  // ------------------------------------------------------------------------
296  virtual void endRaceEarly() { return; }
297  // ------------------------------------------------------------------------
298  virtual bool hasRaceEndedEarly() const { return m_ended_early; }
299  // ------------------------------------------------------------------------
301  virtual bool haveBonusBoxes() { return true; }
302  // ------------------------------------------------------------------------
304  virtual bool useFastMusicNearEnd() const { return true; }
305  // ------------------------------------------------------------------------
308  virtual void kartAdded(AbstractKart* kart, scene::ISceneNode* node) {}
309  // ------------------------------------------------------------------------
313  virtual void newLap(unsigned int kart_index) {}
314  // ------------------------------------------------------------------------
316  virtual bool kartHit(int kart_id, int hitter = -1) { return false; }
317  // ------------------------------------------------------------------------
318  virtual void onMouseClick(int x, int y) {};
319 
320  // Other functions
321  // ===============
322  Highscores *getHighscores() const;
323  Highscores *getGPHighscores() const;
324  void schedulePause(Phase phase);
325  void scheduleUnpause();
326  void scheduleExitRace() { m_schedule_exit_race = true; }
327  void scheduleTutorial();
328  void updateWorld(int ticks);
329  void handleExplosion(const Vec3 &xyz, AbstractKart *kart_hit,
330  PhysicalObject *object);
331  AbstractKart* getPlayerKart(unsigned int player) const;
332  AbstractKart* getLocalPlayerKart(unsigned int n) const;
333  virtual const btTransform &getStartTransform(int index);
334  void moveKartTo(AbstractKart* kart, const btTransform &t);
335  void updateTimeTargetSound();
336  // ------------------------------------------------------------------------
338  RaceGUIBase *getRaceGUI() const { return m_race_gui;}
339  // ------------------------------------------------------------------------
341  unsigned int getNumKarts() const { return (unsigned int) m_karts.size(); }
342  // ------------------------------------------------------------------------
344  AbstractKart *getKart(int kartId) const {
345  assert(kartId >= 0 && kartId < int(m_karts.size()));
346  return m_karts[kartId].get(); }
347  // ------------------------------------------------------------------------
349  const KartList & getKarts() const { return m_karts; }
350  // ------------------------------------------------------------------------
352  unsigned int getCurrentNumKarts() const { return (int)m_karts.size() -
354  // ------------------------------------------------------------------------
356  unsigned int getCurrentNumPlayers() const { return m_num_players -
358  // ------------------------------------------------------------------------
359  void resetElimination()
360  {
361  m_eliminated_karts = 0;
363  }
364  // ------------------------------------------------------------------------
365  virtual void addReservedKart(int kart_id)
366  {
367  if (m_eliminated_karts > 0)
369  }
370  // ------------------------------------------------------------------------
371  virtual void saveCompleteState(BareNetworkString* bns, STKPeer* peer) {}
372  // ------------------------------------------------------------------------
373  virtual void restoreCompleteState(const BareNetworkString& buffer) {}
374  // ------------------------------------------------------------------------
377  virtual bool shouldDrawTimer() const
378  { return isActiveRacePhase() && getClockMode() != CLOCK_NONE; }
379  // ------------------------------------------------------------------------
381  virtual bool shouldDrawSpeedometerDigit() const { return false; }
382  // ------------------------------------------------------------------------
384  virtual std::pair<int, video::SColor> getSpeedometerDigit(const AbstractKart *kart) const
385  { return std::make_pair(0, video::SColor(255, 255, 255, 255)); }
386  // ------------------------------------------------------------------------
388  bool useHighScores() const { return m_use_highscores; }
389  // ------------------------------------------------------------------------
391  virtual void onFirePressed(Controller* who) {}
392  // ------------------------------------------------------------------------
395  virtual bool useChecklineRequirements() const { return false; }
396  // ------------------------------------------------------------------------
397  virtual void escapePressed();
398  // ------------------------------------------------------------------------
399  virtual void loadCustomModels() {}
400  // ------------------------------------------------------------------------
401  void eliminateKart(int kart_number, bool notify_of_elimination = true);
402  // ------------------------------------------------------------------------
403  void setUnfairTeam(bool val) { m_unfair_team = val; }
404  // ------------------------------------------------------------------------
405  virtual bool hasTeam() const { return false; }
406  // ------------------------------------------------------------------------
408  KartTeam getKartTeam(unsigned int kart_id) const;
409  // ------------------------------------------------------------------------
410  int getTeamNum(KartTeam team) const;
411  // ------------------------------------------------------------------------
413  void setNetworkWorld(bool is_networked) { m_is_network_world = is_networked; }
414  // ------------------------------------------------------------------------
415  bool isNetworkWorld() const { return m_is_network_world; }
416  // ------------------------------------------------------------------------
418  void initTeamArrows(AbstractKart* k);
419  // ------------------------------------------------------------------------
423  virtual std::pair<uint32_t, uint32_t> getGameStartedProgress() const
424  {
425  return std::make_pair(std::numeric_limits<uint32_t>::max(),
426  std::numeric_limits<uint32_t>::max());
427  }
428  // ------------------------------------------------------------------------
429  virtual bool isGoalPhase() const { return false; }
430 }; // World
431 
432 #endif
433 
434 /* EOF */
Definition: world.hpp:64
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
This is the base class for kart controller - that can be a player or a a robot.
Definition: controller.hpp:46
Represents one highscore entry, i.e.
Definition: highscores.hpp:41
Contains the state information of an item, i.e.
Definition: item.hpp:53
Definition: physical_object.hpp:40
An abstract base class for the two race guis (race_gui and race_result gui)
Definition: race_gui_base.hpp:50
KartType
Different kart types: A local player, a player connected via network, an AI kart, the leader kart (cu...
Definition: race_manager.hpp:243
A random number generator.
Definition: random_generator.hpp:33
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
A class that manages the clock (countdown, chrono, etc.) Also manages stuff like the 'ready/set/go' t...
Definition: world_status.hpp:33
float getTime() const
Returns the current race time.
Definition: world_status.hpp:204
int getClockMode() const
Returns the current clock mode.
Definition: world_status.hpp:200
base class for all game modes This class is responsible for running the actual race.
Definition: world.hpp:88
Controller * loadAIController(AbstractKart *kart)
Creates an AI controller for the kart.
Definition: world.cpp:582
virtual void init()
This function is called after instanciating.
Definition: world.cpp:155
bool m_schedule_pause
Pausing/unpausing are not done immediately, but at next udpdate.
Definition: world.hpp:162
virtual void kartAdded(AbstractKart *kart, scene::ISceneNode *node)
If you want to do something to karts or their graphics at the start of the race, override this.
Definition: world.hpp:308
virtual std::pair< uint32_t, uint32_t > getGameStartedProgress() const
Used by server to get the current started game progress in either or both remaining time or progress ...
Definition: world.hpp:423
static void setWorld(World *world)
Sets the pointer to the world object.
Definition: world.hpp:233
bool m_schedule_unpause
Pausing/unpausing are not done immediately, but at next udpdate.
Definition: world.hpp:169
virtual void terminateRace() OVERRIDE
Called at the end of a race.
Definition: world.cpp:739
void eliminateKart(int kart_number, bool notify_of_elimination=true)
Remove (eliminate) a kart from the race.
Definition: world.cpp:1421
virtual bool showLapsTarget()
If true lap counter shows lap count in format: 4/20 or if false then in format: 4.
Definition: world.hpp:266
int m_num_players
OVerall number of players.
Definition: world.hpp:124
void updateWorld(int ticks)
This is the main interface to update the world.
Definition: world.cpp:1024
unsigned int getCurrentNumKarts() const
Returns the number of currently active (i.e.non-elikminated) karts.
Definition: world.hpp:352
int m_eliminated_players
Number of eliminated players.
Definition: world.hpp:122
bool m_self_destruct
Set when the world needs to be deleted but you can't do it immediately because you are e....
Definition: world.hpp:182
static World * m_world[PT_COUNT]
A pointer to the global world object for a race.
Definition: world.hpp:93
const KartList & getKarts() const
Returns all karts.
Definition: world.hpp:349
virtual bool useFastMusicNearEnd() const
Returns if this mode should use fast music (if available).
Definition: world.hpp:304
virtual bool raceHasLaps()=0
Called when it is needed to know whether this kind of race involves counting laps.
virtual void getDefaultCollectibles(int *collectible_type, int *amount)
Called to determine the default collectibles to give each player at the start for this kind of race.
Definition: world.cpp:1478
KartList m_karts
The list of all karts.
Definition: world.hpp:115
virtual void onFirePressed(Controller *who)
Override if you want to know when a kart presses fire.
Definition: world.hpp:391
virtual int getFinishedLapsOfKart(unsigned int kart_index) const
Returns the number of laps for a given kart.
Definition: world.hpp:270
virtual std::shared_ptr< AbstractKart > createKart(const std::string &kart_ident, int index, int local_player_id, int global_player_id, RaceManager::KartType type, HandicapLevel handicap)
Creates a kart, having a certain position, starting location, and local and global player id (if appl...
Definition: world.cpp:467
bool m_is_network_world
Set when the world is online and counts network players.
Definition: world.hpp:185
RaceGUIBase * m_saved_race_gui
The actual race gui needs to be saved when the race result gui is displayed since it is still needed ...
Definition: world.hpp:155
virtual void pause(Phase phase) OVERRIDE
Pauses the music (and then pauses WorldStatus).
Definition: world.cpp:1487
virtual void onGo() OVERRIDE
Called when 'go' is being displayed for the first time.
Definition: world.cpp:706
virtual bool isRaceOver()=0
Returns true if the race is over.
virtual bool kartHit(int kart_id, int hitter=-1)
Called when a kart was hit by a projectile.
Definition: world.hpp:316
virtual const std::string & getIdent() const =0
Each game mode should have a unique identifier.
RaceGUIBase * getRaceGUI() const
Returns a pointer to the race gui.
Definition: world.hpp:338
static World * getWorld()
Returns a pointer to the (singleton) world object.
Definition: world.hpp:215
void setNetworkWorld(bool is_networked)
Set the network mode (true if networked)
Definition: world.hpp:413
AbstractKart * getLocalPlayerKart(unsigned int n) const
Returns the nth local player kart, i.e.
Definition: world.cpp:1413
unsigned int getCurrentNumPlayers() const
Returns the number of currently active (i.e.
Definition: world.hpp:356
virtual bool shouldDrawSpeedometerDigit() const
Definition: world.hpp:381
virtual float estimateFinishTimeForKart(AbstractKart *kart)
Used for AI karts that are still racing when all player kart finished.
Definition: world.hpp:203
bool useHighScores() const
Definition: world.hpp:388
virtual bool haveBonusBoxes()
Called to determine whether this race mode uses bonus boxes.
Definition: world.hpp:301
virtual void collectedItem(const AbstractKart *kart, const ItemState *item)
Receives notification if an item is collected.
Definition: world.hpp:293
void updateHighscores(int *best_highscore_rank)
Called at the end of a race.
Definition: world.cpp:1290
void initTeamArrows(AbstractKart *k)
Set the team arrow on karts if necessary.
Definition: world.cpp:313
AbstractKart * getKart(int kartId) const
Returns the kart with a given world id.
Definition: world.hpp:344
virtual std::pair< int, video::SColor > getSpeedometerDigit(const AbstractKart *kart) const
Definition: world.hpp:384
virtual unsigned int getRescuePositionIndex(AbstractKart *kart)=0
Determines the rescue position index of the specified kart.
virtual void reset(bool restart=false) OVERRIDE
This function is called before a race is started (i.e.
Definition: world.cpp:349
virtual void getKartsDisplayInfo(std::vector< RaceGUIBase::KartIconDisplayInfo > *info)=0
Called by the code that draws the list of karts on the race GUI to know what needs to be drawn in the...
unsigned int getNumKarts() const
Returns the number of karts in the race.
Definition: world.hpp:341
virtual void unpause() OVERRIDE
Switches back from a pause state to the previous state.
Definition: world.cpp:1497
KartTeam getKartTeam(unsigned int kart_id) const
Get the team of kart in world (including AIs)
Definition: world.cpp:1670
virtual bool shouldDrawTimer() const
The code that draws the timer should call this first to know whether the game mode wants a timer draw...
Definition: world.hpp:377
void updateTrack(int ticks)
Only updates the track.
Definition: world.cpp:1249
virtual void updateGraphics(float dt)
This updates all only graphical elements.
Definition: world.cpp:1112
void moveKartTo(AbstractKart *kart, const btTransform &t)
Places the kart at a given position and rotation.
Definition: world.cpp:935
RaceGUIBase * m_race_gui
Pointer to the race GUI.
Definition: world.hpp:148
virtual unsigned int getNumberOfRescuePositions() const
Returns the number of rescue positions on a given track and game mode.
Definition: world.cpp:1547
virtual const btTransform & getStartTransform(int index)
Returns the start coordinates for a kart with a given index.
Definition: world.cpp:573
virtual btTransform getRescueTransform(unsigned int index) const
Returns the bullet transformation for the specified rescue index.
Definition: world.cpp:1539
void resetAllKarts()
Waits till each kart is resting on the ground.
Definition: world.cpp:819
AbstractKart * getPlayerKart(unsigned int player) const
Returns the n-th player kart.
Definition: world.cpp:1391
virtual void update(int ticks) OVERRIDE
Updates the physics, all karts, the track, and projectile manager.
Definition: world.cpp:1158
virtual void newLap(unsigned int kart_index)
Called whenever a kart starts a new lap.
Definition: world.hpp:313
virtual void moveKartAfterRescue(AbstractKart *kart)
Places a kart that is rescued.
Definition: world.cpp:923
static void deleteWorld()
Delete the )singleton) world object, if it exists, and sets the singleton pointer to NULL.
Definition: world.hpp:224
int m_eliminated_karts
Number of eliminated karts.
Definition: world.hpp:120
virtual bool useChecklineRequirements() const
Whether to compute checkline requirements for each world on the quadgraph.
Definition: world.hpp:395
bool m_use_highscores
Whether highscores should be used for this kind of race.
Definition: world.hpp:135
World()
The main world class is used to handle the track and the karts.
Definition: world.cpp:126
HandicapLevel
Handicap per player.
Definition: remote_kart_info.hpp:43