SuperTuxKart
ctf_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 HEADER_CTF_FLAG_HPP
19 #define HEADER_CTF_FLAG_HPP
20 
21 #include "network/rewinder.hpp"
22 #include "utils/types.hpp"
23 #include "utils/vec3.hpp"
24 
25 #include "LinearMath/btTransform.h"
26 #include <cstring>
27 
28 enum FlagColor : unsigned int
29 {
30  FC_RED = 0,
31  FC_BLUE = 1
32 };
33 
34 namespace GE { class GERenderInfo; }
35 
36 namespace irr
37 {
38  namespace scene
39  {
40  class IAnimatedMeshSceneNode;
41  }
42 }
43 class CTFFlag : public Rewinder
44 {
45 public:
46  static const int IN_BASE = -1;
47  static const int OFF_BASE = -2;
48 private:
49  /* Either save the above 2 status, or kart id holder this flag. */
50  int8_t m_flag_status;
51 
52  /* Currnet flag transformation. */
53  btTransform m_flag_trans;
54 
55  /* Transformation of IN_BASE. */
56  const btTransform m_flag_base_trans;
57 
58  /* If OFF_BASE of m_flag_status, save the ticks since off base, use for
59  * auto return base for a flag (see ServerConfig). */
60  uint16_t m_ticks_since_off_base;
61 
62  FlagColor m_flag_color;
63 
64  /* Set by dropFlagAt to pre-compressed the dropped flag transformation. */
65  int m_off_base_compressed[4];
66 
67  /* Determine if the flag should be drawn translucently */
68  std::shared_ptr<GE::GERenderInfo> m_flag_render_info;
69 
70  uint16_t m_deactivated_ticks;
71 public:
72  // ------------------------------------------------------------------------
73  CTFFlag(FlagColor fc, const btTransform& base_trans)
74  : Rewinder(fc == FC_RED ?
75  std::string{RN_RED_FLAG} : std::string{RN_BLUE_FLAG}),
76  m_flag_base_trans(base_trans)
77  {
78  // updateFlagTrans is called after kart is rewound, see rewinder name
79  // defined in rewinder header
80  m_flag_status = IN_BASE;
81  m_flag_trans.setOrigin(Vec3(0.0f));
82  m_flag_trans.setRotation(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
83  m_flag_color = fc;
84  m_ticks_since_off_base = 0;
85  memset(m_off_base_compressed, 0, 16);
86  }
87  // ------------------------------------------------------------------------
88  virtual void saveTransform() {}
89  // ------------------------------------------------------------------------
90  virtual void computeError() {}
91  // ------------------------------------------------------------------------
92  virtual BareNetworkString* saveState(std::vector<std::string>* ru);
93  // ------------------------------------------------------------------------
94  virtual void undoEvent(BareNetworkString* buffer) {}
95  // ------------------------------------------------------------------------
96  virtual void rewindToEvent(BareNetworkString* buffer) {}
97  // ------------------------------------------------------------------------
98  virtual void restoreState(BareNetworkString* buffer, int count);
99  // ------------------------------------------------------------------------
100  virtual void undoState(BareNetworkString* buffer) {}
101  // ------------------------------------------------------------------------
102  int getHolder() const
103  {
104  if (m_flag_status >= 0)
105  return m_flag_status;
106  return -1;
107  }
108  // ------------------------------------------------------------------------
109  int getStatus() const { return m_flag_status; }
110  // ------------------------------------------------------------------------
111  const Vec3& getOrigin() const { return (Vec3&)m_flag_trans.getOrigin(); }
112  // ------------------------------------------------------------------------
113  const Vec3& getBaseOrigin() const
114  { return (Vec3&)m_flag_base_trans.getOrigin(); }
115  // ------------------------------------------------------------------------
116  const btTransform& getBaseTrans() const { return m_flag_base_trans; }
117  // ------------------------------------------------------------------------
118  void resetToBase(unsigned deactivated_ticks = 0)
119  {
120  // 11 bit for deactivated_ticks saved with flag status (5 bits)
121  if (deactivated_ticks > 2047)
122  deactivated_ticks = 2047;
123  m_deactivated_ticks = deactivated_ticks;
124  m_flag_status = IN_BASE;
125  m_ticks_since_off_base = 0;
126  updateFlagTrans();
127  }
128  // ------------------------------------------------------------------------
129  void setCapturedByKart(int kart_id)
130  {
131  m_flag_status = (int8_t)kart_id;
132  m_ticks_since_off_base = 0;
133  updateFlagTrans();
134  }
135  // ------------------------------------------------------------------------
136  void dropFlagAt(const btTransform& t);
137  // ------------------------------------------------------------------------
138  bool isInBase() const { return m_flag_status == IN_BASE; }
139  // ------------------------------------------------------------------------
140  bool isActivated() const { return m_deactivated_ticks == 0; }
141  // ------------------------------------------------------------------------
142  bool canBeCaptured() const
143  { return !(m_flag_status >= 0) && isActivated(); }
144  // ------------------------------------------------------------------------
145  void update(int ticks);
146  // ------------------------------------------------------------------------
147  void updateFlagTrans(const btTransform& off_base_trans = btTransform());
148  // ------------------------------------------------------------------------
149  void updateFlagGraphics(irr::scene::IAnimatedMeshSceneNode* flag_node);
150  // ------------------------------------------------------------------------
151  void initFlagRenderInfo(irr::scene::IAnimatedMeshSceneNode* flag_node);
152 }; // CTFFlag
153 #endif
154 
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
Definition: ctf_flag.hpp:44
virtual void rewindToEvent(BareNetworkString *buffer)
Called when an event needs to be replayed.
Definition: ctf_flag.hpp:96
virtual void undoEvent(BareNetworkString *buffer)
Called when an event needs to be undone.
Definition: ctf_flag.hpp:94
virtual BareNetworkString * saveState(std::vector< std::string > *ru)
Provides a copy of the state of the object in one memory buffer.
Definition: ctf_flag.cpp:35
virtual void computeError()
Called when a rewind is finished, and is used to compute the error caused by the rewind (which is the...
Definition: ctf_flag.hpp:90
virtual void restoreState(BareNetworkString *buffer, int count)
Called when a state needs to be replayed.
Definition: ctf_flag.cpp:56
virtual void undoState(BareNetworkString *buffer)
Undo the effects of the given state, but do not rewind to that state (which is done by rewindTo).
Definition: ctf_flag.hpp:100
virtual void saveTransform()
Called before a rewind.
Definition: ctf_flag.hpp:88
Definition: rewinder.hpp:44
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:35
Declares the general types that are used by the network.