1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2008-2016 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 #include "karts/controller/kart_control.hpp"
20 #include "network/network_string.hpp"
21
22 #include "irrMath.h"
23 #include <algorithm>
24
25 // ------------------------------------------------------------------------
26 /** Sets the current steering value. */
setSteer(float f)27 void KartControl::setSteer(float f)
28 {
29 int steer = irr::core::clamp((int)(f * 32767.0f), -32767, 32767);
30 m_steer = (int16_t)steer;
31 } // setSteer
32
33 // ----------------------------------------------------------------------------
34 /** Sets the acceleration. */
setAccel(float f)35 void KartControl::setAccel(float f)
36 {
37 int accel = std::min((int)(f * 65535.0f), 65535);
38 m_accel = (uint16_t)accel;
39 } // setAccel
40
41 // ----------------------------------------------------------------------------
42 /** Sets if the kart is braking. */
setBrake(bool b)43 void KartControl::setBrake(bool b)
44 {
45 m_brake = b;
46 } // setBrake
47
48 // ----------------------------------------------------------------------------
49 /** Sets if the kart activates nitro. */
setNitro(bool b)50 void KartControl::setNitro(bool b)
51 {
52 m_nitro = b;
53 } // setNitro
54
55 // ----------------------------------------------------------------------------
56 /** Sets the skid control for this kart. */
setSkidControl(SkidControl sc)57 void KartControl::setSkidControl(SkidControl sc)
58 {
59 m_skid = sc;
60 } // seSkidControl
61
62 // ----------------------------------------------------------------------------
63 /** Returns if this kart wants to get rescued. */
setRescue(bool b)64 void KartControl::setRescue(bool b)
65 {
66 m_rescue = b;
67 } // setRescue
68
69 // ----------------------------------------------------------------------------
70 /** Sets if the kart wants to fire. */
setFire(bool b)71 void KartControl::setFire(bool b)
72 {
73 m_fire = b;
74 } // setFire
75
76 // ----------------------------------------------------------------------------
77 /** Sets if the kart wants to look (and therefore also fires) backwards. */
setLookBack(bool b)78 void KartControl::setLookBack(bool b)
79 {
80 m_look_back = b;
81 } // setLookBack
82 // ----------------------------------------------------------------------------
83 /** Copies the important data from this objects into a memory buffer. */
saveState(BareNetworkString * buffer) const84 void KartControl::saveState(BareNetworkString *buffer) const
85 {
86 buffer->addUInt16(m_steer);
87 buffer->addUInt16(m_accel);
88 buffer->addChar(getButtonsCompressed());
89 } // saveState
90
91 // ----------------------------------------------------------------------------
92 /** Restores this object from a previously saved memory buffer. */
rewindTo(BareNetworkString * buffer)93 void KartControl::rewindTo(BareNetworkString *buffer)
94 {
95 m_steer = buffer->getUInt16();
96 m_accel = buffer->getUInt16();
97 setButtonsCompressed(buffer->getUInt8());
98 } // setFromMemory
99