RigsofRods
Soft-body Physics Simulation
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 
30 using namespace RoR;
31 
33 {
34  int colourNum = -1;
35  Ogre::UTFString 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 
51 void CharacterFactory::createRemoteInstance(int sourceid, int streamid)
52 {
53 #ifdef USE_SOCKETW
54  RoRnet::UserInfo info;
55  App::GetNetwork()->GetUserInfo(sourceid, info);
56  int colour = info.colournum;
57  Ogre::UTFString 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
108 void 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
RoR::App::GetNetwork
Network * GetNetwork()
Definition: Application.cpp:284
RoR::GfxScene::RegisterGfxCharacter
void RegisterGfxCharacter(RoR::GfxCharacter *gfx_character)
Definition: GfxScene.cpp:325
RoR::CharacterFactory::handleStreamData
void handleStreamData(std::vector< RoR::NetRecvPacket > packet)
Definition: CharacterFactory.cpp:108
RoR::MpState::CONNECTED
@ CONNECTED
RoR::CharacterFactory::UndoRemoteActorCoupling
void UndoRemoteActorCoupling(ActorPtr actor)
Definition: CharacterFactory.cpp:90
RoRnet::UserInfo
Definition: RoRnet.h:168
RoR::CharacterFactory::CreateLocalCharacter
Character * CreateLocalCharacter()
Definition: CharacterFactory.cpp:32
Utils.h
RefCountingObjectPtr< Actor >
RoR::CharacterFactory::removeStreamSource
void removeStreamSource(int sourceid)
Definition: CharacterFactory.cpp:67
RoR::Network::GetUserInfo
bool GetUserInfo(int uid, RoRnet::UserInfo &result)
Definition: Network.cpp:709
Actor.h
CharacterFactory.h
RoR::App::mp_state
CVar * mp_state
Definition: Application.cpp:115
TOSTRING
#define TOSTRING(x)
Definition: Application.h:56
RoR::CharacterFactory::Update
void Update(float dt)
Definition: CharacterFactory.cpp:80
RoRnet::MSG2_STREAM_REGISTER
@ MSG2_STREAM_REGISTER
create new stream
Definition: RoRnet.h:63
RoR::Character
Definition: Character.h:40
GfxScene.h
Character.h
RoR::CharacterFactory::createRemoteInstance
void createRemoteInstance(int sourceid, int streamid)
Definition: CharacterFactory.cpp:51
Application.h
Central state/object manager and communications hub.
RoR::Network::GetLocalUserData
RoRnet::UserInfo GetLocalUserData()
Definition: Network.cpp:697
RoR::CharacterFactory::m_remote_characters
std::vector< std::unique_ptr< Character > > m_remote_characters
Definition: CharacterFactory.h:55
RoR::CharacterFactory::m_local_character
std::unique_ptr< Character > m_local_character
Definition: CharacterFactory.h:54
RoR::CharacterFactory::DeleteAllCharacters
void DeleteAllCharacters()
Definition: CharacterFactory.cpp:101
RoRnet::StreamRegister::type
int32_t type
stream type
Definition: RoRnet.h:142
RoRnet::UserInfo::colournum
int32_t colournum
colour set by server
Definition: RoRnet.h:173
RoRnet::UserInfo::username
char username[RORNET_MAX_USERNAME_LEN]
the nickname of the user (UTF-8)
Definition: RoRnet.h:175
RoR::tryConvertUTF
Ogre::UTFString tryConvertUTF(const char *buffer)
Definition: Utils.cpp:58
RoR
Definition: AppContext.h:36
RoR::App::GetGfxScene
GfxScene * GetGfxScene()
Definition: Application.cpp:276
RoRnet::MSG2_USER_LEAVE
@ MSG2_USER_LEAVE
user leaves
Definition: RoRnet.h:58
RoR::Character::SetupGfx
GfxCharacter * SetupGfx()
Definition: Character.cpp:552
RoRnet::StreamRegister
< Sent from the client to server and vice versa, to broadcast a new stream
Definition: RoRnet.h:140