1 //
2 //  SuperTuxKart - a fun racing game with go-kart
3 //  Copyright (C) 2018 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 "items/item_event_info.hpp"
20 
21 #include "network/network_config.hpp"
22 #include "network/network_string.hpp"
23 #include "network/protocols/game_protocol.hpp"
24 #include "network/rewind_manager.hpp"
25 #include "network/stk_host.hpp"
26 
27 
28 /** Loads an event from a server message. It helps encapsulate the encoding
29  *  of events from and into a message buffer.
30  *  \param buffer A network string with the event data.
31  *  \param count The number of bytes read will be subtracted from this value.
32  */
ItemEventInfo(BareNetworkString * buffer,int * count)33 ItemEventInfo::ItemEventInfo(BareNetworkString *buffer, int *count)
34 {
35     m_ticks_till_return = 0;
36     m_type    = (EventType)buffer->getUInt8();
37     m_ticks   = buffer->getTime();
38     *count   -= 5;
39     if (m_type != IEI_SWITCH)
40     {
41         m_kart_id = buffer->getInt8();
42         m_index = buffer->getUInt16();
43         *count -= 3;
44         if (m_type == IEI_NEW)
45         {
46             m_xyz = buffer->getVec3();
47             m_normal = buffer->getVec3();
48             *count -= 24;
49         }
50         else   // IEI_COLLECT
51         {
52             m_ticks_till_return = buffer->getUInt16();
53             *count -= 2;
54         }
55     }   // is not switch
56     else   // switch
57     {
58         m_index = -1;
59         m_kart_id = -1;
60     }
61 }   // ItemEventInfo(BareNetworkString, int *count)
62 
63 //-----------------------------------------------------------------------------
64 /** Stores this event into a network string.
65  *  \param buffer The network string to which the data should be appended.
66  */
saveState(BareNetworkString * buffer)67 void ItemEventInfo::saveState(BareNetworkString *buffer)
68 {
69     assert(NetworkConfig::get()->isServer());
70     buffer->addUInt8(m_type).addTime(m_ticks);
71     if (m_type != IEI_SWITCH)
72     {
73         // Only new item and collecting items need the index and kart id:
74         buffer->addUInt8(m_kart_id).addUInt16(m_index);
75         if (m_type == IEI_NEW)
76         {
77             buffer->add(m_xyz);
78             buffer->add(m_normal);
79         }
80         else if (m_type == IEI_COLLECT)
81             buffer->addUInt16(m_ticks_till_return);
82     }
83 }   // saveState
84