SuperTuxKart
kart_control.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2008-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_KART_CONTROL_HPP
20 #define HEADER_KART_CONTROL_HPP
21 
22 #include "utils/types.hpp"
23 
24 class BareNetworkString;
25 
30 {
31 public:
35  enum SkidControl {SC_NONE, SC_NO_DIRECTION, SC_LEFT, SC_RIGHT};
36 
37 private:
39  int16_t m_steer;
41  uint16_t m_accel;
43  bool m_brake;
45  bool m_nitro;
49  bool m_rescue;
51  bool m_fire;
54 public:
55  void setSteer(float f);
56  void setAccel(float f);
57  void setBrake(bool b);
58  void setNitro(bool b);
59  void setSkidControl(SkidControl sc);
60  void setRescue(bool b);
61  void setFire(bool b);
62  void setLookBack(bool b);
63 
64  // ------------------------------------------------------------------------
65  KartControl()
66  {
67  reset();
68  }
69  // ------------------------------------------------------------------------
71  void reset()
72  {
73  m_steer = 0;
74  m_accel = 0;
75  m_brake = false;
76  m_nitro = false;
77  m_skid = SC_NONE;
78  m_rescue = false;
79  m_fire = false;
80  m_look_back = false;
81  } // reset
82  // ------------------------------------------------------------------------
85  bool operator==(const KartControl &other)
86  {
87  return m_steer == other.m_steer &&
88  m_accel == other.m_accel &&
89  m_brake == other.m_brake &&
90  m_nitro == other.m_nitro &&
91  m_skid == other.m_skid &&
92  m_rescue == other.m_rescue &&
93  m_fire == other.m_fire &&
94  m_look_back == other.m_look_back;
95  } // operator==
96  // ------------------------------------------------------------------------
98  void saveState(BareNetworkString *buffer) const;
99  // ------------------------------------------------------------------------
101  void rewindTo(BareNetworkString *buffer);
102  // ------------------------------------------------------------------------
104  char getButtonsCompressed() const
105  {
106  return (m_brake ? 1 : 0)
107  + (m_nitro ? 2 : 0)
108  + (m_rescue ? 4 : 0)
109  + (m_fire ? 8 : 0)
110  + (m_look_back ? 16 : 0)
111  + (m_skid<<5); // m_skid is in {0,1,2,3}
112  } // getButtonsCompressed
113  // ------------------------------------------------------------------------
117  void setButtonsCompressed(char c)
118  {
119  m_brake = (c & 1) != 0;
120  m_nitro = (c & 2) != 0;
121  m_rescue = (c & 4) != 0;
122  m_fire = (c & 8) != 0;
123  m_look_back = (c & 16) != 0;
124  m_skid = (SkidControl)((c & 96) >> 5);
125  } // setButtonsCompressed
126  // ------------------------------------------------------------------------
128  float getSteer() const { return (float)m_steer / 32767.0f; }
129  // ------------------------------------------------------------------------
131  float getAccel() const { return (float)m_accel / 65535.0f; }
132  // ------------------------------------------------------------------------
134  bool getBrake() const { return m_brake; }
135  // ------------------------------------------------------------------------
137  bool getNitro() const { return m_nitro; }
138  // ------------------------------------------------------------------------
142  SkidControl getSkidControl() const { return m_skid; }
143  // ------------------------------------------------------------------------
145  bool getRescue() const { return m_rescue; }
146  // ------------------------------------------------------------------------
148  bool getFire() const { return m_fire; }
149  // ------------------------------------------------------------------------
152  bool getLookBack() const { return m_look_back; }
153 };
154 
155 #endif
156 
void saveState(BareNetworkString *buffer) const
Copies the important data from this objects into a memory buffer.
Definition: kart_control.cpp:84
SkidControl getSkidControl() const
Returns the skidding control state: SC_NONE: not pressed; SC_NO_DIRECTION: pressed,...
Definition: kart_control.hpp:142
bool m_nitro
True if the kart activates nitro.
Definition: kart_control.hpp:45
bool m_fire
True if fire is selected.
Definition: kart_control.hpp:51
SkidControl
The skidding control state: SC_NONE: not pressed; SC_NO_DIRECTION: pressed, but no steering; SC_LEFT/...
Definition: kart_control.hpp:35
void setBrake(bool b)
Sets if the kart is braking.
Definition: kart_control.cpp:43
void setFire(bool b)
Sets if the kart wants to fire.
Definition: kart_control.cpp:71
void reset()
Resets all controls.
Definition: kart_control.hpp:71
bool getFire() const
Returns if fire is selected.
Definition: kart_control.hpp:148
void setButtonsCompressed(char c)
Sets the buttons from a compressed (1 byte) representation.
Definition: kart_control.hpp:117
Declares the general types that are used by the network.
void setSteer(float f)
Sets the current steering value.
Definition: kart_control.cpp:27
char getButtonsCompressed() const
Compresses all buttons into a single byte.
Definition: kart_control.hpp:104
Definition: kart_control.hpp:29
bool m_brake
True if the kart brakes.
Definition: kart_control.hpp:43
SkidControl m_skid
Skidding control state.
Definition: kart_control.hpp:47
bool getLookBack() const
Returns if the kart wants to look back (which also implies that it will fire backwards.
Definition: kart_control.hpp:152
bool getBrake() const
Returns if the kart is braking.
Definition: kart_control.hpp:134
int16_t m_steer
The current steering value in [-32767, 32767].
Definition: kart_control.hpp:39
float getAccel() const
Returns current acceleration in [0, 1].
Definition: kart_control.hpp:131
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:52
bool getRescue() const
Returns true if the kart triggered rescue.
Definition: kart_control.hpp:145
void setRescue(bool b)
Returns if this kart wants to get rescued.
Definition: kart_control.cpp:64
void setSkidControl(SkidControl sc)
Sets the skid control for this kart.
Definition: kart_control.cpp:57
void setLookBack(bool b)
Sets if the kart wants to look (and therefore also fires) backwards.
Definition: kart_control.cpp:78
float getSteer() const
Returns the current steering value in [-1, 1].
Definition: kart_control.hpp:128
bool operator==(const KartControl &other)
Tests if two KartControls are equal.
Definition: kart_control.hpp:85
bool getNitro() const
Returns if the kart activates nitro.
Definition: kart_control.hpp:137
void setAccel(float f)
Sets the acceleration.
Definition: kart_control.cpp:35
bool m_rescue
True if rescue is selected.
Definition: kart_control.hpp:49
bool m_look_back
True if the kart looks (and shoots) backwards.
Definition: kart_control.hpp:53
void rewindTo(BareNetworkString *buffer)
Restores this object from a previously saved memory buffer.
Definition: kart_control.cpp:93
uint16_t m_accel
Acceleration, in [0, 65535].
Definition: kart_control.hpp:41
void setNitro(bool b)
Sets if the kart activates nitro.
Definition: kart_control.cpp:50