SuperTuxKart
capture_the_flag.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2018 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 CAPTURE_THE_FLAG_HPP
19 #define CAPTURE_THE_FLAG_HPP
20 
21 #include "modes/free_for_all.hpp"
22 
23 #include <vector>
24 #include <string>
25 
26 namespace irr
27 {
28  namespace scene
29  {
30  class IAnimatedMeshSceneNode; class IAnimatedMesh; class ISceneNode;
31  }
32 }
33 
34 class CTFFlag;
35 enum FlagColor : unsigned int;
36 
37 class CaptureTheFlag : public FreeForAll
38 {
39 private:
40  scene::IAnimatedMeshSceneNode* m_red_flag_node;
41  scene::IAnimatedMeshSceneNode* m_blue_flag_node;
42 
43  scene::IAnimatedMesh* m_red_flag_mesh;
44  scene::IAnimatedMesh* m_blue_flag_mesh;
45 
46  scene::ISceneNode* m_red_flag_indicator;
47  scene::ISceneNode* m_blue_flag_indicator;
48 
49  SFXBase* m_scored_sound;
50 
51  int m_red_scores, m_blue_scores;
52 
53  /* Save the last captured flag ticks */
54  int m_last_captured_flag_ticks;
55 
56  /* Used in updateGraphics to add race gui message for flag */
57  int m_red_flag_status, m_blue_flag_status;
58 
59  std::map<int, int> m_swatter_reset_kart_ticks;
60 
61  std::shared_ptr<CTFFlag> m_red_flag, m_blue_flag;
62 
63  // ------------------------------------------------------------------------
64  bool getDroppedFlagTrans(const btTransform& kt, btTransform* out) const;
65  // ------------------------------------------------------------------------
66  void resetRedFlagToOrigin();
67  // ------------------------------------------------------------------------
68  void resetBlueFlagToOrigin();
69  // ------------------------------------------------------------------------
70  virtual video::SColor getColor(unsigned int kart_id) const OVERRIDE;
71 
72  void checkScoring(FlagColor color);
73 
74 public:
75  // ------------------------------------------------------------------------
77  // ------------------------------------------------------------------------
78  virtual ~CaptureTheFlag();
79  // ------------------------------------------------------------------------
80  virtual void init() OVERRIDE;
81  // ------------------------------------------------------------------------
82  virtual void reset(bool restart=false) OVERRIDE;
83  // ------------------------------------------------------------------------
84  virtual void update(int ticks) OVERRIDE;
85  // ------------------------------------------------------------------------
86  virtual void updateGraphics(float dt) OVERRIDE;
87  // ------------------------------------------------------------------------
88  virtual bool hasTeam() const OVERRIDE { return true; }
89  // ------------------------------------------------------------------------
90  virtual bool isRaceOver() OVERRIDE;
91  // ------------------------------------------------------------------------
92  virtual bool kartHit(int kart_id, int hitter = -1) OVERRIDE;
93  // ------------------------------------------------------------------------
94  virtual unsigned int getRescuePositionIndex(AbstractKart *kart) OVERRIDE;
95  // ------------------------------------------------------------------------
96  virtual const std::string& getIdent() const OVERRIDE;
97  // ------------------------------------------------------------------------
98  bool getKartCTFResult(unsigned int kart_id) const
99  {
100  if (m_red_scores == m_blue_scores)
101  return true;
102 
103  bool red_win = m_red_scores > m_blue_scores;
104  KartTeam team = getKartTeam(kart_id);
105 
106  if ((red_win && team == KART_TEAM_RED) ||
107  (!red_win && team == KART_TEAM_BLUE))
108  return true;
109  else
110  return false;
111  }
112  // ------------------------------------------------------------------------
113  int getRedScore() const { return m_red_scores; }
114  // ------------------------------------------------------------------------
115  int getBlueScore() const { return m_blue_scores; }
116  // ------------------------------------------------------------------------
117  int getRedHolder() const;
118  // ------------------------------------------------------------------------
119  int getBlueHolder() const;
120  // ------------------------------------------------------------------------
121  bool isRedFlagInBase() const;
122  // ------------------------------------------------------------------------
123  bool isBlueFlagInBase() const;
124  // ------------------------------------------------------------------------
125  const Vec3& getRedFlag() const;
126  // ------------------------------------------------------------------------
127  const Vec3& getBlueFlag() const;
128  // ------------------------------------------------------------------------
129  void ctfScored(int kart_id, bool red_team_scored, int new_kart_score,
130  int new_red_score, int new_blue_score);
131  // ------------------------------------------------------------------------
132  void loseFlagForKart(int kart_id);
133  // ------------------------------------------------------------------------
134  void resetKartForSwatterHit(int kart_id, int at_world_ticks)
135  { m_swatter_reset_kart_ticks[kart_id] = at_world_ticks; }
136  // ------------------------------------------------------------------------
137  virtual std::pair<uint32_t, uint32_t> getGameStartedProgress() const
138  OVERRIDE
139  {
140  std::pair<uint32_t, uint32_t> progress(
141  std::numeric_limits<uint32_t>::max(),
142  std::numeric_limits<uint32_t>::max());
143  if (RaceManager::get()->hasTimeTarget())
144  {
145  progress.first = (uint32_t)m_time;
146  }
147  if (m_red_scores > m_blue_scores)
148  {
149  progress.second = (uint32_t)((float)m_red_scores /
150  (float)RaceManager::get()->getHitCaptureLimit() * 100.0f);
151  }
152  else
153  {
154  progress.second = (uint32_t)((float)m_blue_scores /
155  (float)RaceManager::get()->getHitCaptureLimit() * 100.0f);
156  }
157  return progress;
158  }
159  // ------------------------------------------------------------------------
160  virtual void saveCompleteState(BareNetworkString* bns,
161  STKPeer* peer) OVERRIDE;
162  // ------------------------------------------------------------------------
163  virtual void restoreCompleteState(const BareNetworkString& b) OVERRIDE;
164 }; // CaptureTheFlag
165 
166 #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: ctf_flag.hpp:44
Definition: capture_the_flag.hpp:38
CaptureTheFlag()
Constructor.
Definition: capture_the_flag.cpp:50
virtual bool isRaceOver() OVERRIDE
The battle is over if only one kart is left, or no player kart.
Definition: capture_the_flag.cpp:457
virtual const std::string & getIdent() const OVERRIDE
Returns the internal identifier for this race.
Definition: capture_the_flag.cpp:540
virtual bool kartHit(int kart_id, int hitter=-1) OVERRIDE
Called when a kart is hit.
Definition: capture_the_flag.cpp:518
virtual void reset(bool restart=false) OVERRIDE
Called when a battle is restarted.
Definition: capture_the_flag.cpp:138
virtual unsigned int getRescuePositionIndex(AbstractKart *kart) OVERRIDE
Determines the rescue position for a kart.
Definition: capture_the_flag.cpp:532
virtual void update(int ticks) OVERRIDE
Updates the physics, all karts, the track, and projectile manager.
Definition: capture_the_flag.cpp:225
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: capture_the_flag.hpp:137
virtual void init() OVERRIDE
call just after instanciating.
Definition: capture_the_flag.cpp:93
virtual void updateGraphics(float dt) OVERRIDE
This updates all only graphical elements.
Definition: capture_the_flag.cpp:163
Definition: free_for_all.hpp:29
The base class for sound effects.
Definition: sfx_base.hpp:43
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
double m_time
Elasped/remaining time in seconds.
Definition: world_status.hpp:94
KartTeam getKartTeam(unsigned int kart_id) const
Get the team of kart in world (including AIs)
Definition: world.cpp:1670