Skip to content

Commit

Permalink
Codechange: Added SyncLinkedActors() for clarity.
Browse files Browse the repository at this point in the history
Syncing various states was fragmented across the code (skeletonview in `tie/rope/hookToggle()`, pausing/unpausing in `ButtonsUI + UpdateCommonInputEvents()` etc...)
It's now all synced/reset in `ActorManager::SyncLinkedActors()`

Minor changes:
- deleted dead `m_linked_gfx_actors` from GfxActor
- moved vars in Actor.h
  • Loading branch information
ohlidalp authored and Petr Ohlídal committed Jul 16, 2023
1 parent 01572cf commit 9431e1b
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 140 deletions.
15 changes: 3 additions & 12 deletions source/main/GameContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,19 +1329,13 @@ void GameContext::UpdateCommonInputEvents(float dt)
if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_TOGGLE_DEBUG_VIEW))
{
m_player_actor->GetGfxActor()->ToggleDebugView();
for (ActorPtr& actor : m_player_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(m_player_actor->GetGfxActor()->GetDebugView());
}
// NOTE: Syncing with linked actors is done in `SyncLinkedActors()`
}

if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_CYCLE_DEBUG_VIEWS))
{
m_player_actor->GetGfxActor()->CycleDebugViews();
for (ActorPtr& actor : m_player_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(m_player_actor->GetGfxActor()->GetDebugView());
}
// NOTE: Syncing with linked actors is done in `SyncLinkedActors()`
}

if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_RESCUE_TRUCK, 0.5f) &&
Expand Down Expand Up @@ -1400,10 +1394,7 @@ void GameContext::UpdateCommonInputEvents(float dt)
// toggle physics
if (RoR::App::GetInputEngine()->getEventBoolValueBounce(EV_TRUCK_TOGGLE_PHYSICS))
{
for (ActorPtr& actor : App::GetGameContext()->GetPlayerActor()->ar_linked_actors)
{
actor->ar_physics_paused = !App::GetGameContext()->GetPlayerActor()->ar_physics_paused;
}
// NOTE: Syncing with linked actors is done in `SyncLinkedActors()`
App::GetGameContext()->GetPlayerActor()->ar_physics_paused = !App::GetGameContext()->GetPlayerActor()->ar_physics_paused;
}

Expand Down
18 changes: 2 additions & 16 deletions source/main/gameplay/RepairMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,14 @@ void RepairMode::UpdateInputEvents(float dt)

App::GetGameContext()->GetPlayerActor()->requestRotation(rotation, rotation_center);
App::GetGameContext()->GetPlayerActor()->requestTranslation(translation);

if (App::sim_soft_reset_mode->getBool())
{
for (ActorPtr& actor : App::GetGameContext()->GetPlayerActor()->ar_linked_actors)
{
actor->requestRotation(rotation, rotation_center);
actor->requestTranslation(translation);
}
}
// NOTE: Syncing with linked actors is done in `SyncLinkedActors()`

m_live_repair_timer = 0.0f;
}
else if (App::GetInputEngine()->isKeyDownValueBounce(OIS::KC_SPACE))
{
App::GetGameContext()->GetPlayerActor()->requestAngleSnap(45);
if (App::sim_soft_reset_mode->getBool())
{
for (ActorPtr& actor : App::GetGameContext()->GetPlayerActor()->ar_linked_actors)
{
actor->requestAngleSnap(45);
}
}
// NOTE: Syncing with linked actors is done in `SyncLinkedActors()`
}
else
{
Expand Down
7 changes: 1 addition & 6 deletions source/main/gfx/GfxActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,12 +1794,7 @@ void RoR::GfxActor::UpdateSimDataBuffer()
m_simbuf.simbuf_speedo_highest_kph = m_actor->ar_speedo_max_kph;
m_simbuf.simbuf_speedo_use_engine_max_rpm = m_actor->ar_gui_use_engine_max_rpm;

// Linked Actors
m_linked_gfx_actors.clear();
for (ActorPtr& actor : m_actor->ar_linked_actors)
{
m_linked_gfx_actors.insert(actor->GetGfxActor());
}

}

bool RoR::GfxActor::IsActorLive() const
Expand Down
2 changes: 0 additions & 2 deletions source/main/gfx/GfxActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ class GfxActor
Ogre::MaterialPtr& GetCabTransMaterial() { return m_cab_mat_visual_trans; }
VideoCamState GetVideoCamState() const { return m_vidcam_state; }
DebugViewType GetDebugView() const { return m_debug_view; }
std::set<GfxActor*> GetLinkedGfxActors() { return m_linked_gfx_actors; }
Ogre::String GetResourceGroup() { return m_custom_resource_group; }
ActorPtr GetActor() { return m_actor; } // Watch out for multithreading with this!
Ogre::TexturePtr GetHelpTex() { return m_help_tex; }
Expand All @@ -162,7 +161,6 @@ class GfxActor
Ogre::SceneNode* m_gfx_beams_parent_scenenode = nullptr;

// Game state
std::set<GfxActor*> m_linked_gfx_actors;
bool m_initialized = false;
VideoCamState m_vidcam_state = VideoCamState::VCSTATE_ENABLED_ONLINE;
DebugViewType m_debug_view = DebugViewType::DEBUGVIEW_NONE;
Expand Down
5 changes: 1 addition & 4 deletions source/main/gui/panels/GUI_VehicleButtons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,7 @@ void VehicleButtons::DrawActorPhysicsButton(RoR::GfxActor* actorx)

if (ImGui::ImageButton(reinterpret_cast<ImTextureID>(m_actor_physics_icon->getHandle()), ImVec2(24, 24)))
{
for (ActorPtr& actor : actorx->GetActor()->ar_linked_actors)
{
actor->ar_physics_paused = !actorx->GetActor()->ar_physics_paused;
}
// NOTE: Syncing with linked actors is done in `SyncLinkedActors()`
actorx->GetActor()->ar_physics_paused = !actorx->GetActor()->ar_physics_paused;
}

Expand Down
3 changes: 3 additions & 0 deletions source/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,9 @@ int main(int argc, char *argv[])
App::GetSoundScriptManager()->update(dt); // update 3d audio listener position
#endif // USE_OPENAL

// Sync shared state (lights, brakes, debugviews, pause/reset) between linked actors.
App::GetGameContext()->GetActorManager()->SyncLinkedActors();

#ifdef USE_ANGELSCRIPT
App::GetScriptEngine()->framestep(dt);
#endif // USE_ANGELSCRIPT
Expand Down
101 changes: 5 additions & 96 deletions source/main/physics/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3376,27 +3376,7 @@ void Actor::tieToggle(int group)
if (it->ti_locked_actor != this)
{
this->RemoveInterActorBeam(it->ti_beam);
// update skeletonview on the untied actors
auto linked_actors = it->ti_locked_actor->ar_linked_actors;
if (!(std::find(linked_actors.begin(), linked_actors.end(), this) != linked_actors.end()))
{
if (this == player_actor.GetRef())
{
it->ti_locked_actor->GetGfxActor()->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
for (ActorPtr& actor : it->ti_locked_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
}
}
else if (it->ti_locked_actor == player_actor)
{
m_gfx_actor->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
for (ActorPtr& actor : this->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
}
}
}
// NOTE: updating skeletonview on the tied actors is now done in `SyncLinkedActors()`
}
it->ti_locked_actor = nullptr;
}
Expand Down Expand Up @@ -3467,23 +3447,7 @@ void Actor::tieToggle(int group)
if (it->ti_beam->bm_inter_actor)
{
AddInterActorBeam(it->ti_beam, this, nearest_actor);
// update skeletonview on the tied actors
if (this == player_actor.GetRef())
{
nearest_actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
for (ActorPtr& actor : nearest_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
}
}
else if (nearest_actor == player_actor)
{
m_gfx_actor->SetDebugView(player_actor->GetGfxActor()->GetDebugView());
for (ActorPtr& actor : this->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(player_actor->GetGfxActor()->GetDebugView());
}
}
// NOTE: updating skeletonview on the tied actors is now done in `SyncLinkedActors()`
}
}
}
Expand Down Expand Up @@ -3515,27 +3479,7 @@ void Actor::ropeToggle(int group)
if (it->rp_locked_actor != this)
{
this->RemoveInterActorBeam(it->rp_beam);
// update skeletonview on the unroped actors
auto linked_actors = it->rp_locked_actor->ar_linked_actors;
if (!(std::find(linked_actors.begin(), linked_actors.end(), this) != linked_actors.end()))
{
if (this == player_actor.GetRef())
{
it->rp_locked_actor->GetGfxActor()->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
for (ActorPtr& actor : it->rp_locked_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
}
}
else if (it->rp_locked_actor == player_actor)
{
m_gfx_actor->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
for (ActorPtr& actor : this->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
}
}
}
// NOTE: updating skeletonview on the unroped actors is now done in `SyncLinkedActors()`
}
it->rp_locked_actor = nullptr;
it->rp_locked_ropable = nullptr;
Expand Down Expand Up @@ -3580,23 +3524,7 @@ void Actor::ropeToggle(int group)
if (nearest_actor != this)
{
AddInterActorBeam(it->rp_beam, this, nearest_actor);
// update skeletonview on the roped up actors
if (this == player_actor.GetRef())
{
nearest_actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
for (ActorPtr& actor : nearest_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
}
}
else if (nearest_actor == player_actor)
{
m_gfx_actor->SetDebugView(player_actor->GetGfxActor()->GetDebugView());
for (ActorPtr& actor : this->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(player_actor->GetGfxActor()->GetDebugView());
}
}
// NOTE: updating skeletonview on the roped up actor is now done in `SyncLinkedActors()`
}
}
}
Expand Down Expand Up @@ -3728,26 +3656,7 @@ void Actor::hookToggle(int group, HookAction mode, NodeNum_t mousenode /*=NODENU
it->hk_beam->bm_disabled = true;
}

// update skeletonview on the (un)hooked actor
if (it->hk_locked_actor != prev_locked_actor)
{
if (it->hk_locked_actor)
{
it->hk_locked_actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
for (ActorPtr& actor : it->hk_locked_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
}
}
else if (prev_locked_actor != this)
{
prev_locked_actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
for (ActorPtr& actor : prev_locked_actor->ar_linked_actors)
{
actor->GetGfxActor()->SetDebugView(m_gfx_actor->GetDebugView());
}
}
}
// NOTE: updating skeletonview on the (un)hooked actor is now done in `SyncLinkedActors()`
}
}

Expand Down
10 changes: 6 additions & 4 deletions source/main/physics/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ ActorPtrVec ar_linked_actors; //!< Sim state
// Gameplay state
ActorState ar_state = ActorState::LOCAL_SIMULATED;

// Repair state
Ogre::Vector3 m_rotation_request_center = Ogre::Vector3::ZERO;
float m_rotation_request = 0.f; //!< Accumulator
int m_anglesnap_request = 0; //!< Accumulator
Ogre::Vector3 m_translation_request = Ogre::Vector3::ZERO; //!< Accumulator

// Realtime node/beam structure editing helpers
bool ar_nb_initialized = false;
std::vector<float> ar_nb_optimum; //!< Temporary storage of the optimum search result
Expand Down Expand Up @@ -532,10 +538,6 @@ ActorPtrVec ar_linked_actors; //!< Sim state
float m_mouse_grab_move_force = 0.f;
float m_spawn_rotation = 0.f;
Ogre::Timer m_reset_timer;
Ogre::Vector3 m_rotation_request_center = Ogre::Vector3::ZERO;
float m_rotation_request = 0.f; //!< Accumulator
int m_anglesnap_request = 0; //!< Accumulator
Ogre::Vector3 m_translation_request = Ogre::Vector3::ZERO; //!< Accumulator
Ogre::Vector3 m_camera_gforces_accu = Ogre::Vector3::ZERO; //!< Accumulator for 'camera' G-forces
Ogre::Vector3 m_camera_gforces = Ogre::Vector3::ZERO; //!< Physics state (global)
Ogre::Vector3 m_camera_local_gforces_cur = Ogre::Vector3::ZERO; //!< Physics state (camera local)
Expand Down
55 changes: 55 additions & 0 deletions source/main/physics/ActorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,61 @@ void ActorManager::ForwardCommands(ActorPtr source_actor)
}
}

// Internal helper for future extensions
void SyncSlaveActorWithMasterActor(const ActorPtr& slave, const ActorPtr& master)
{
// Always sync the paused state
slave->ar_physics_paused = master->ar_physics_paused;

// Always sync skeletonview
slave->GetGfxActor()->SetDebugView(master->GetGfxActor()->GetDebugView());

// Sync live repair movement if requested
if (App::sim_soft_reset_mode->getBool())
{
slave->requestRotation(master->m_rotation_request, master->m_rotation_request_center);
slave->requestTranslation(master->m_translation_request);
slave->requestAngleSnap(master->m_anglesnap_request);
}
}

// internal helper for future extensions
void ResetUnlinkedActor(const ActorPtr& loner)
{
// Always unpause
loner->ar_physics_paused = false;

// Always disable skeletonview
loner->GetGfxActor()->SetDebugView(DebugViewType::DEBUGVIEW_NONE);
}

void ActorManager::SyncLinkedActors()
{
// Sync shared state (debugviews, pause, repair) between linked actors
// Reset state of non-linked actors
// -------------------------------------------------------------------

// For now, we only sync state of player-driven actor with all it's linked actors
// TBD: Maintain a graph of master->slave relations so AI vehicles sync too.
if (App::GetGameContext()->GetPlayerActor())
{
for (const ActorPtr& actor : App::GetGameContext()->GetPlayerActor()->ar_linked_actors)
{
SyncSlaveActorWithMasterActor(actor, App::GetGameContext()->GetPlayerActor());
}
}

// Reset unlinked actors, except the player actor
for (const ActorPtr& actor: m_actors)
{
if (actor->ar_linked_actors.size() == 0
&& actor != App::GetGameContext()->GetPlayerActor())
{
ResetUnlinkedActor(actor);
}
}
}

void ActorManager::UpdateSleepingState(ActorPtr player_actor, float dt)
{
if (!m_forced_awake)
Expand Down
4 changes: 4 additions & 0 deletions source/main/physics/ActorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ class ActorManager

std::pair<ActorPtr, float> GetNearestActor(Ogre::Vector3 position);

/// @name Actor grouping
/// @{
// A list of all beams interconnecting two actors
std::map<beam_t*, std::pair<ActorPtr, ActorPtr>> inter_actor_links;
void SyncLinkedActors();
/// @}

static const ActorPtr ACTORPTR_NULL; // Dummy value to be returned as const reference.

Expand Down

0 comments on commit 9431e1b

Please sign in to comment.