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 26 /** 27 * \ingroup controller 28 */ 29 class KartControl 30 { 31 public: 32 /** The skidding control state: SC_NONE: not pressed; 33 * SC_NO_DIRECTION: pressed, but no steering; 34 * SC_LEFT/RIGHT: pressed in the specified direction. */ 35 enum SkidControl {SC_NONE, SC_NO_DIRECTION, SC_LEFT, SC_RIGHT}; 36 37 private: 38 /** The current steering value in [-32767, 32767]. */ 39 int16_t m_steer; 40 /** Acceleration, in [0, 65535]. */ 41 uint16_t m_accel; 42 /** True if the kart brakes. */ 43 bool m_brake; 44 /** True if the kart activates nitro. */ 45 bool m_nitro; 46 /** Skidding control state. */ 47 SkidControl m_skid; 48 /** True if rescue is selected. */ 49 bool m_rescue; 50 /** True if fire is selected. */ 51 bool m_fire; 52 /** True if the kart looks (and shoots) backwards. */ 53 bool m_look_back; 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 // ------------------------------------------------------------------------ KartControl()65 KartControl() 66 { 67 reset(); 68 } 69 // ------------------------------------------------------------------------ 70 /** Resets all controls. */ reset()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 // ------------------------------------------------------------------------ 83 /** Tests if two KartControls are equal. 84 */ operator ==(const KartControl & other)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 // ------------------------------------------------------------------------ 97 /** Copies the important data from this objects into a memory buffer. */ 98 void saveState(BareNetworkString *buffer) const; 99 // ------------------------------------------------------------------------ 100 /** Restores this object from a previously saved memory buffer. */ 101 void rewindTo(BareNetworkString *buffer); 102 // ------------------------------------------------------------------------ 103 /** Compresses all buttons into a single byte. */ getButtonsCompressed() const104 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 // ------------------------------------------------------------------------ 114 /** Sets the buttons from a compressed (1 byte) representation. 115 * /param c Character containing the compressed representation. 116 */ setButtonsCompressed(char c)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 // ------------------------------------------------------------------------ 127 /** Returns the current steering value in [-1, 1]. */ getSteer() const128 float getSteer() const { return (float)m_steer / 32767.0f; } 129 // ------------------------------------------------------------------------ 130 /** Returns current acceleration in [0, 1]. */ getAccel() const131 float getAccel() const { return (float)m_accel / 65535.0f; } 132 // ------------------------------------------------------------------------ 133 /** Returns if the kart is braking. */ getBrake() const134 bool getBrake() const { return m_brake; } 135 // ------------------------------------------------------------------------ 136 /** Returns if the kart activates nitro. */ getNitro() const137 bool getNitro() const { return m_nitro; } 138 // ------------------------------------------------------------------------ 139 /** Returns the skidding control state: SC_NONE: not pressed; 140 * SC_NO_DIRECTION: pressed, but no steering; 141 * SC_LEFT/RIGHT: pressed in the specified direction. */ getSkidControl() const142 SkidControl getSkidControl() const { return m_skid; } 143 // ------------------------------------------------------------------------ 144 /** Returns true if the kart triggered rescue. */ getRescue() const145 bool getRescue() const { return m_rescue; } 146 // ------------------------------------------------------------------------ 147 /** Returns if fire is selected. */ getFire() const148 bool getFire() const { return m_fire; } 149 // ------------------------------------------------------------------------ 150 /** Returns if the kart wants to look back (which also implies that it 151 * will fire backwards. */ getLookBack() const152 bool getLookBack() const { return m_look_back; } 153 }; 154 155 #endif 156 157