Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
CharacterFactory.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2018 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "CharacterFactory.h"
23
24#include "Actor.h"
25#include "Application.h"
26#include "Character.h"
27#include "GfxScene.h"
28#include "Utils.h"
29
30using namespace RoR;
31
33{
34 int colourNum = -1;
35 std::string playerName = "";
36
37#ifdef USE_SOCKETW
38 if (App::mp_state->getEnum<MpState>() == MpState::CONNECTED)
39 {
41 colourNum = info.colournum;
42 playerName = tryConvertUTF(info.username);
43 }
44#endif // USE_SOCKETW
45
46 m_local_character = std::unique_ptr<Character>(new Character(-1, 0, playerName, colourNum, false));
48 return m_local_character.get();
49}
50
51void CharacterFactory::createRemoteInstance(int sourceid, int streamid)
52{
53#ifdef USE_SOCKETW
55 App::GetNetwork()->GetUserInfo(sourceid, info);
56 int colour = info.colournum;
57 std::string name = tryConvertUTF(info.username);
58
59 LOG(" new character for " + TOSTRING(sourceid) + ":" + TOSTRING(streamid) + ", colour: " + TOSTRING(colour));
60
61 Character* ch = new Character(sourceid, streamid, name, colour, true);
63 m_remote_characters.push_back(std::unique_ptr<Character>(ch));
64#endif // USE_SOCKETW
65}
66
68{
69 for (auto it = m_remote_characters.begin(); it != m_remote_characters.end(); it++)
70 {
71 if ((*it)->getSourceID() == sourceid)
72 {
73 (*it).reset();
74 m_remote_characters.erase(it);
75 return;
76 }
77 }
78}
79
81{
82 m_local_character->update(dt);
83
84 for (auto& c : m_remote_characters)
85 {
86 c->update(dt);
87 }
88}
89
91{
92 for (auto& c : m_remote_characters)
93 {
94 if (c->GetActorCoupling() == actor)
95 {
96 c->SetActorCoupling(false, nullptr);
97 }
98 }
99}
100
102{
103 m_remote_characters.clear(); // std::unique_ptr<> will do the cleanup...
104 m_local_character.reset(); // ditto
105}
106
107#ifdef USE_SOCKETW
108void CharacterFactory::handleStreamData(std::vector<RoR::NetRecvPacket> packet_buffer)
109{
110 for (auto packet : packet_buffer)
111 {
112 if (packet.header.command == RoRnet::MSG2_STREAM_REGISTER)
113 {
114 RoRnet::StreamRegister* reg = (RoRnet::StreamRegister *)packet.buffer;
115 if (reg->type == 1)
116 {
117 createRemoteInstance(packet.header.source, packet.header.streamid);
118 }
119 }
120 else if (packet.header.command == RoRnet::MSG2_USER_LEAVE)
121 {
122 removeStreamSource(packet.header.source);
123 }
124 else
125 {
126 for (auto& c : m_remote_characters)
127 {
128 c->receiveStreamData(packet.header.command, packet.header.source, packet.header.streamid, packet.buffer);
129 }
130 }
131 }
132}
133#endif // USE_SOCKETW
Central state/object manager and communications hub.
#define TOSTRING(x)
Definition Application.h:57
void LOG(const char *msg)
Legacy alias - formerly a macro.
Character * CreateLocalCharacter()
void createRemoteInstance(int sourceid, int streamid)
void handleStreamData(std::vector< RoR::NetRecvPacket > packet)
void UndoRemoteActorCoupling(ActorPtr actor)
void removeStreamSource(int sourceid)
std::unique_ptr< Character > m_local_character
std::vector< std::unique_ptr< Character > > m_remote_characters
GfxCharacter * SetupGfx()
void RegisterGfxCharacter(RoR::GfxCharacter *gfx_character)
Definition GfxScene.cpp:360
RoRnet::UserInfo GetLocalUserData()
Definition Network.cpp:707
bool GetUserInfo(int uid, RoRnet::UserInfo &result)
Definition Network.cpp:725
GfxScene * GetGfxScene()
CVar * mp_state
Network * GetNetwork()
std::string tryConvertUTF(const char *buffer)
Definition Utils.cpp:61
@ MSG2_USER_LEAVE
user leaves
Definition RoRnet.h:58
@ MSG2_STREAM_REGISTER
create new stream
Definition RoRnet.h:63
< Sent from the client to server and vice versa, to broadcast a new stream
Definition RoRnet.h:160
int32_t type
0 = Actor, 1 = Character, 3 = ChatSystem
Definition RoRnet.h:161
char username[RORNET_MAX_USERNAME_LEN]
the nickname of the user (UTF-8)
Definition RoRnet.h:196
int32_t colournum
colour set by server
Definition RoRnet.h:194