Skip to content

Commit

Permalink
🎮 Key-triggered prop anims now propagate over network.
Browse files Browse the repository at this point in the history
RoRnet bumped.
  • Loading branch information
ohlidalp authored and Petr Ohlídal committed Oct 11, 2022
1 parent e583df2 commit aed38fd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion source/main/network/RoRnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace RoRnet {
#define RORNET_LAN_BROADCAST_PORT 13000 //!< port used to send the broadcast announcement in LAN mode
#define RORNET_MAX_USERNAME_LEN 40 //!< port used to send the broadcast announcement in LAN mode

#define RORNET_VERSION "RoRnet_2.43"
#define RORNET_VERSION "RoRnet_2.44"

enum MessageType
{
Expand Down
28 changes: 25 additions & 3 deletions source/main/physics/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void Actor::pushNetwork(char* data, int size)
update.wheel_data.resize(ar_num_wheels * sizeof(float));

// check if the size of the data matches to what we expected
if ((unsigned int)size == (m_net_buffer_size + sizeof(RoRnet::VehicleState)))
if ((unsigned int)size == (m_net_total_buffer_size + sizeof(RoRnet::VehicleState)))
{
// we walk through the incoming data and separate it a bit
char* ptr = data;
Expand All @@ -367,6 +367,15 @@ void Actor::pushNetwork(char* data, int size)
update.wheel_data[i] = wspeed;
ptr += sizeof(float);
}

// then process the prop animation keys
for (size_t i = 0; i < m_prop_anim_key_states.size(); i++)
{
// Unpack bit array
char byte = *(ptr + (i / 8));
char mask = char(1) << (7 - (i % 8));
m_prop_anim_key_states[i].anim_active = (byte & mask);
}
}
else
{
Expand Down Expand Up @@ -1897,7 +1906,7 @@ void Actor::sendStreamData()
ar_net_last_update_time = ar_net_timer.getMilliseconds();

//look if the packet is too big first
if (m_net_buffer_size + sizeof(RoRnet::VehicleState) > 8192)
if (m_net_total_buffer_size + sizeof(RoRnet::VehicleState) > RORNET_MAX_MESSAGE_LENGTH)
{
ErrorUtils::ShowError(_L("Actor is too big to be sent over the net."), _L("Network error!"));
exit(126);
Expand Down Expand Up @@ -2006,7 +2015,7 @@ void Actor::sendStreamData()
{
char* ptr = send_buffer + sizeof(RoRnet::VehicleState);
float* send_nodes = (float *)ptr;
packet_len += m_net_buffer_size;
packet_len += m_net_total_buffer_size;

// copy data into the buffer
int i;
Expand Down Expand Up @@ -2037,6 +2046,19 @@ void Actor::sendStreamData()
{
wfbuf[i] = ar_wheels[i].wh_net_rp;
}
ptr += ar_num_wheels * sizeof(float);

// Then the anim key states
for (size_t i = 0; i < m_prop_anim_key_states.size(); i++)
{
if (m_prop_anim_key_states[i].anim_active)
{
// Pack as bit array, starting with most signifficant bit
char& dst_byte = *(ptr + (i / 8));
char mask = ((char)m_prop_anim_key_states[i].anim_active) << (7 - (i % 8));
dst_byte |= mask;
}
}
}

App::GetNetwork()->AddPacket(ar_net_stream_id, MSG2_STREAM_DATA_DISCARDABLE, packet_len, send_buffer);
Expand Down
19 changes: 13 additions & 6 deletions source/main/physics/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,6 @@ class Actor : public ZeroedMemoryAllocator
Ogre::Vector3 m_mouse_grab_pos;
float m_mouse_grab_move_force;
float m_spawn_rotation;
Ogre::UTFString m_net_username;
int m_net_color_num;
Ogre::Timer m_reset_timer;
Ogre::Vector3 m_rotation_request_center;
float m_rotation_request; //!< Accumulator
Expand All @@ -511,10 +509,6 @@ class Actor : public ZeroedMemoryAllocator
Differential* m_wheel_diffs[MAX_WHEELS/2];//!< Physics
int m_num_wheel_diffs; //!< Physics attr
TransferCase* m_transfer_case; //!< Physics
float m_net_node_compression; //!< Sim attr;
int m_net_first_wheel_node; //!< Network attr; Determines data buffer layout
int m_net_node_buf_size; //!< Network attr; buffer size
int m_net_buffer_size; //!< Network attr; buffer size
int m_wheel_node_count; //!< Static attr; filled at spawn
int m_previous_gear; //!< Sim state; land vehicle shifting
float m_handbrake_force; //!< Physics attr; defined in truckfile
Expand All @@ -539,6 +533,19 @@ class Actor : public ZeroedMemoryAllocator
std::vector<std::string> m_description;
std::vector<PropAnimKeyState> m_prop_anim_key_states;

/// @name Networking
/// @{
size_t m_net_node_buf_size; //!< For incoming/outgoing traffic; calculated on spawn
size_t m_net_wheel_buf_size; //!< For incoming/outgoing traffic; calculated on spawn
size_t m_net_propanimkey_buf_size; //!< For incoming/outgoing traffic; calculated on spawn
size_t m_net_total_buffer_size; //!< For incoming/outgoing traffic; calculated on spawn
float m_net_node_compression; //!< For incoming/outgoing traffic; calculated on spawn
int m_net_first_wheel_node; //!< Network attr; Determines data buffer layout; calculated on spawn

Ogre::UTFString m_net_username;
int m_net_color_num;
/// @}

/// @name Light states
/// @{
GfxFlaresMode m_flares_mode = GfxFlaresMode::NONE; //!< Snapshot of cvar 'gfx_flares_mode' on spawn.
Expand Down
15 changes: 11 additions & 4 deletions source/main/physics/ActorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,20 @@ Actor* ActorManager::CreateNewActor(ActorSpawnRequest rq, RigDef::DocumentPtr de
if (App::mp_state->getEnum<MpState>() == RoR::MpState::CONNECTED)
{
// network buffer layout (without RoRnet::VehicleState):
//
// -----------------------------------------------------

// - 3 floats (x,y,z) for the reference node 0
// - ar_num_nodes - 1 times 3 short ints (compressed position info)
// - ar_num_wheels times a float for the wheel rotation
//
actor->m_net_node_buf_size = sizeof(float) * 3 + (actor->m_net_first_wheel_node - 1) * sizeof(short int) * 3;
actor->m_net_buffer_size = actor->m_net_node_buf_size + actor->ar_num_wheels * sizeof(float);
actor->m_net_total_buffer_size += actor->m_net_node_buf_size;
// - ar_num_wheels times a float for the wheel rotation
actor->m_net_wheel_buf_size = actor->ar_num_wheels * sizeof(float);
actor->m_net_total_buffer_size += actor->m_net_wheel_buf_size;
// - bit array (made of ints) for the prop animation key states
actor->m_net_propanimkey_buf_size =
(actor->m_prop_anim_key_states.size() / 8) + // whole chars
(size_t)(actor->m_prop_anim_key_states.size() % 8 != 0); // remainder: 0 or 1 chars
actor->m_net_total_buffer_size += actor->m_net_propanimkey_buf_size;

if (rq.asr_origin == ActorSpawnRequest::Origin::NETWORK)
{
Expand Down

0 comments on commit aed38fd

Please sign in to comment.