RigsofRods
Soft-body Physics Simulation
ActorManager.h
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 
6  For more information, see http://www.rigsofrods.org/
7 
8  Rigs of Rods is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License version 3, as
10  published by the Free Software Foundation.
11 
12  Rigs of Rods is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
24 
25 #pragma once
26 
27 #include "Application.h"
28 #include "SimData.h"
29 #include "CmdKeyInertia.h"
30 #include "Network.h"
31 #include "RigDef_Prerequisites.h"
32 #include "ThreadPool.h"
33 
34 #include <string>
35 #include <vector>
36 
37 namespace RoR {
38 
41 
44 {
45 public:
46 
47  typedef std::vector<FreeForce> FreeForceVec_t;
48 
49  ActorManager();
50  ~ActorManager();
51 
55  void DeleteActorInternal(ActorPtr actor);
56 
60  const ActorPtr& GetActorById(ActorInstanceID_t actor_id);
61  const ActorPtr& GetActorByNetworkLinks(int source_id, int stream_id); // used by character
62  ActorPtr FindActorInsideBox(Collisions* collisions, const Ogre::String& inst, const Ogre::String& box);
63  const ActorPtr& FetchNextVehicleOnList(ActorPtr player, ActorPtr prev_player);
64  const ActorPtr& FetchPreviousVehicleOnList(ActorPtr player, ActorPtr prev_player);
67 
73  FreeForceVec_t::iterator FindFreeForce(FreeForceID_t id);
76 
77  void UpdateActors(ActorPtr player_actor);
78  void SyncWithSimThread();
80  void WakeUpAllActors();
81  void SendAllActorsSleeping();
82  unsigned long GetNetTime() { return m_net_timer.getMilliseconds(); };
83  int GetNetTimeOffset(int sourceid);
84  void UpdateNetTimeOffset(int sourceid, int offset);
85  void AddStreamMismatch(int sourceid, int streamid) { m_stream_mismatches[sourceid].insert(streamid); };
86  int CheckNetworkStreamsOk(int sourceid);
87  int CheckNetRemoteStreamsOk(int sourceid);
88  void MuteAllActors();
89  void UnmuteAllActors();
90  void SetTrucksForcedAwake(bool forced) { m_forced_awake = forced; };
91  bool AreTrucksForcedAwake() const { return m_forced_awake; }
92  void SetSimulationSpeed(float speed) { m_simulation_speed = std::max(0.0f, speed); };
93  float GetSimulationSpeed() const { return m_simulation_speed; };
94  bool IsSimulationPaused() const { return m_simulation_paused; }
96  float GetTotalTime() const { return m_total_sim_time; }
98 
99 
100  void CleanUpSimulation();
101 
102  void RepairActor(Collisions* collisions, const Ogre::String& inst, const Ogre::String& box, bool keepPosition = false);
103  void UpdateSleepingState(ActorPtr player_actor, float dt);
104 
105 
106  void UpdateInputEvents(float dt);
108 
109 #ifdef USE_SOCKETW
110  void HandleActorStreamData(std::vector<RoR::NetRecvPacket> packet);
111 #endif
112 
113  // Savegames (defined in Savegame.cpp)
114 
115  bool LoadScene(Ogre::String filename);
116  bool SaveScene(Ogre::String filename);
117  void RestoreSavedState(ActorPtr actor, rapidjson::Value const& j_entry);
118 
120  std::vector<ActorPtr> GetLocalActors();
121 
122  std::pair<ActorPtr, float> GetNearestActor(Ogre::Vector3 position);
123 
124  std::map<beam_t*, std::pair<ActorPtr, ActorPtr>> inter_actor_links;
125  bool AreActorsDirectlyLinked(const ActorPtr& a1, const ActorPtr& a2);
126 
127  static const ActorPtr ACTORPTR_NULL; // Dummy value to be returned as const reference.
128 
129 private:
130 
131  bool CheckActorCollAabbIntersect(int a, int b);
132  bool PredictActorCollAabbIntersect(int a, int b);
133  void RemoveStreamSource(int sourceid);
134  void RecursiveActivation(int j, std::vector<bool>& visited);
135  void ForwardCommands(ActorPtr source_actor);
136  void UpdateTruckFeatures(ActorPtr vehicle, float dt);
137  void CalcFreeForces();
138 
139  // Networking
140  std::map<int, std::set<int>> m_stream_mismatches;
141  std::map<int, int> m_stream_time_offsets;
142  Ogre::Timer m_net_timer;
143 
144  // Physics
146  bool m_forced_awake = false;
148  float m_dt_remainder = 0.f;
149  float m_simulation_speed = 1.f;
150  float m_last_simulation_speed = 0.1f;
151  float m_simulation_time = 0.f;
152  bool m_simulation_paused = false;
153  float m_total_sim_time = 0.f;
156 
157  // Utils
158  std::unique_ptr<ThreadPool> m_sim_thread_pool;
159  std::shared_ptr<Task> m_sim_task;
161 };
162 
164 
165 } // namespace RoR
RoR::ActorManager::GetSimulationSpeed
float GetSimulationSpeed() const
Definition: ActorManager.h:93
RoR::ActorManager::CheckNetworkStreamsOk
int CheckNetworkStreamsOk(int sourceid)
Definition: ActorManager.cpp:515
RoR::ActorManager::GetNetTime
unsigned long GetNetTime()
Definition: ActorManager.h:82
RoR::ActorManager::SaveScene
bool SaveScene(Ogre::String filename)
Definition: Savegame.cpp:416
RoR::ActorManager::GetNetTimeOffset
int GetNetTimeOffset(int sourceid)
Definition: ActorManager.cpp:497
RoR::ActorManager::ACTORPTR_NULL
static const ActorPtr ACTORPTR_NULL
Definition: ActorManager.h:127
RoR::ActorManager::m_actors
ActorPtrVec m_actors
Definition: ActorManager.h:145
RoR::Collisions
Definition: Collisions.h:80
RigDef_Prerequisites.h
RoR::ActorManager::m_inertia_config
RoR::CmdKeyInertiaConfig m_inertia_config
Definition: ActorManager.h:160
ThreadPool.h
RoR::ActorManager::AddFreeForce
void AddFreeForce(FreeForceRequest *rq)
Definition: ActorManager.cpp:1604
RoR::ActorManager::inter_actor_links
std::map< beam_t *, std::pair< ActorPtr, ActorPtr > > inter_actor_links
Definition: ActorManager.h:124
RoR::ActorManager::AreTrucksForcedAwake
bool AreTrucksForcedAwake() const
Definition: ActorManager.h:91
RoR::ActorManager::RemoveStreamSource
void RemoveStreamSource(int sourceid)
Definition: ActorManager.cpp:333
RoR::ActorManager::FindActorInsideBox
ActorPtr FindActorInsideBox(Collisions *collisions, const Ogre::String &inst, const Ogre::String &box)
Definition: ActorManager.cpp:803
RoR::ActorManager::CheckActorCollAabbIntersect
bool CheckActorCollAabbIntersect(int a, int b)
Returns whether or not the bounding boxes of truck a and truck b intersect. Based on the truck collis...
Definition: ActorManager.cpp:566
RoR::ActorManager::GetFreeForceNextId
FreeForceID_t GetFreeForceNextId()
Definition: ActorManager.h:74
RoR::ActorManager::GetActors
ActorPtrVec & GetActors()
Definition: ActorManager.h:119
RoR::ActorManager::GetActorByNetworkLinks
const ActorPtr & GetActorByNetworkLinks(int source_id, int stream_id)
Definition: ActorManager.cpp:553
RoR::ActorManager::FetchPreviousVehicleOnList
const ActorPtr & FetchPreviousVehicleOnList(ActorPtr player, ActorPtr prev_player)
Definition: ActorManager.cpp:987
RoR::ActorManager::GetNearestActor
std::pair< ActorPtr, float > GetNearestActor(Ogre::Vector3 position)
Definition: ActorManager.cpp:852
RoR::ActorManager::SyncWithSimThread
void SyncWithSimThread()
Definition: ActorManager.cpp:1234
RoR::ActorManager::AreActorsDirectlyLinked
bool AreActorsDirectlyLinked(const ActorPtr &a1, const ActorPtr &a2)
Definition: ActorManager.cpp:720
RoR::ActorManager::m_stream_time_offsets
std::map< int, int > m_stream_time_offsets
Networking: A network time offset for each stream source.
Definition: ActorManager.h:141
RefCountingObjectPtr< Actor >
RoR::ActorSpawnRequest
Definition: SimData.h:832
RoR::ActorManager::SendAllActorsSleeping
void SendAllActorsSleeping()
Definition: ActorManager.cpp:791
RoR::ActorManager::GetTotalTime
float GetTotalTime() const
Definition: ActorManager.h:96
RoR::ActorManager::AddStreamMismatch
void AddStreamMismatch(int sourceid, int streamid)
Definition: ActorManager.h:85
RoR::ActorManager
Builds and manages softbody actors (physics on background thread, networking)
Definition: ActorManager.h:43
RoR::ActorManager::m_simulation_speed
float m_simulation_speed
slow motion < 1.0 < fast motion
Definition: ActorManager.h:149
RoR::ActorManager::m_stream_mismatches
std::map< int, std::set< int > > m_stream_mismatches
Networking: A set of streams without a corresponding actor in the actor-array for each stream source.
Definition: ActorManager.h:140
RoR::ActorInstanceID_t
int ActorInstanceID_t
Unique sequentially generated ID of an actor in session. Use ActorManager::GetActorById()
Definition: ForwardDeclarations.h:37
RoR::ActorManager::CalcFreeForces
void CalcFreeForces()
Apply FreeForces - intentionally as a separate pass over all actors.
Definition: ActorManager.cpp:1494
RoR::ActorManager::UpdateSleepingState
void UpdateSleepingState(ActorPtr player_actor, float dt)
Definition: ActorManager.cpp:734
RoR::ActorManager::PredictActorCollAabbIntersect
bool PredictActorCollAabbIntersect(int a, int b)
Returns whether or not the bounding boxes of truck a and truck b might intersect during the next fram...
Definition: ActorManager.cpp:595
CmdKeyInertia.h
RoR::ActorManager::m_simulation_paused
bool m_simulation_paused
Definition: ActorManager.h:152
RoR::ActorManager::m_dt_remainder
float m_dt_remainder
Keeps track of the rounding error in the time step calculation.
Definition: ActorManager.h:148
RoR::ActorManager::RemoveFreeForce
void RemoveFreeForce(FreeForceID_t id)
Definition: ActorManager.cpp:1638
SimData.h
Core data structures for simulation; Everything affected by by either physics, network or user intera...
RoR::ActorManager::FindFreeForce
FreeForceVec_t::iterator FindFreeForce(FreeForceID_t id)
Definition: ActorManager.cpp:1599
RoR::ActorManager::UnmuteAllActors
void UnmuteAllActors()
Definition: ActorManager.cpp:844
RoR::ActorPtrVec
std::vector< ActorPtr > ActorPtrVec
Definition: ForwardDeclarations.h:210
RoR::ActorManager::m_sim_task
std::shared_ptr< Task > m_sim_task
Definition: ActorManager.h:159
RoR::ActorManager::HandleActorStreamData
void HandleActorStreamData(std::vector< RoR::NetRecvPacket > packet)
Definition: ActorManager.cpp:350
RoR::ActorManager::m_free_force_next_id
FreeForceID_t m_free_force_next_id
Unique ID for each FreeForce.
Definition: ActorManager.h:155
RoR::ActorManager::m_sim_thread_pool
std::unique_ptr< ThreadPool > m_sim_thread_pool
Definition: ActorManager.h:158
RoR::ActorManager::GetLocalActors
std::vector< ActorPtr > GetLocalActors()
Definition: ActorManager.cpp:1332
RoR::FreeForceID_t
int FreeForceID_t
Unique sequentially generated ID of FreeForce; use ActorManager::GetFreeForceNextId().
Definition: ForwardDeclarations.h:65
RoR::ActorManager::ActorManager
ActorManager()
Definition: ActorManager.cpp:64
RoR::ActorManager::FreeForceVec_t
std::vector< FreeForce > FreeForceVec_t
Definition: ActorManager.h:47
RoR::ActorManager::UpdateNetTimeOffset
void UpdateNetTimeOffset(int sourceid, int offset)
Definition: ActorManager.cpp:507
RoR::ActorManager::m_physics_steps
int m_physics_steps
Definition: ActorManager.h:147
Application.h
Central state/object manager and communications hub.
RoR::ActorManager::FetchRescueVehicle
const ActorPtr & FetchRescueVehicle()
Definition: ActorManager.cpp:1014
RoR::ActorManager::m_total_sim_time
float m_total_sim_time
Definition: ActorManager.h:153
RoR::ActorManager::m_simulation_time
float m_simulation_time
Amount of time the physics simulation is going to be advanced.
Definition: ActorManager.h:151
RoR::ActorManager::SetSimulationSpeed
void SetSimulationSpeed(float speed)
Definition: ActorManager.h:92
RoR::ActorManager::m_last_simulation_speed
float m_last_simulation_speed
previously used time ratio between real time (evt.timeSinceLastFrame) and physics time ('dt' used in ...
Definition: ActorManager.h:150
RoR::ActorManager::RecursiveActivation
void RecursiveActivation(int j, std::vector< bool > &visited)
Definition: ActorManager.cpp:624
RoR::ActorManager::DeleteActorInternal
void DeleteActorInternal(ActorPtr actor)
Do not call directly; use GameContext::DeleteActor()
Definition: ActorManager.cpp:881
RoR::ActorManager::MuteAllActors
void MuteAllActors()
Definition: ActorManager.cpp:836
RoR::FreeForceRequest
Common for ADD and MODIFY requests; tailored for use with AngelScript thru GameScript::pushMessage().
Definition: SimData.h:804
RoR::ActorManager::m_net_timer
Ogre::Timer m_net_timer
Definition: ActorManager.h:142
RoR::ActorManager::~ActorManager
~ActorManager()
Definition: ActorManager.cpp:74
RoR::ActorManager::UpdateInputEvents
void UpdateInputEvents(float dt)
Definition: ActorManager.cpp:1343
RoR::ActorManager::SetTrucksForcedAwake
void SetTrucksForcedAwake(bool forced)
Definition: ActorManager.h:90
RoR::ActorManager::ModifyFreeForce
void ModifyFreeForce(FreeForceRequest *rq)
Definition: ActorManager.cpp:1621
RoR::CmdKeyInertiaConfig
Loads and manages 'inertia_models.cfg'.
Definition: CmdKeyInertia.h:34
RoR::ActorManager::SetSimulationPaused
void SetSimulationPaused(bool v)
Definition: ActorManager.h:95
RoR::ActorManager::UpdateTruckFeatures
void UpdateTruckFeatures(ActorPtr vehicle, float dt)
Definition: ActorManager.cpp:1433
RoR::ActorManager::ForwardCommands
void ForwardCommands(ActorPtr source_actor)
Fowards things to trailers.
Definition: ActorManager.cpp:649
RoR::ActorManager::FetchActorDef
RigDef::DocumentPtr FetchActorDef(RoR::ActorSpawnRequest &rq)
Definition: ActorManager.cpp:1253
RoR::ActorManager::RestoreSavedState
void RestoreSavedState(ActorPtr actor, rapidjson::Value const &j_entry)
Definition: Savegame.cpp:785
RoR::ActorManager::RepairActor
void RepairActor(Collisions *collisions, const Ogre::String &inst, const Ogre::String &box, bool keepPosition=false)
Definition: ActorManager.cpp:822
RigDef::DocumentPtr
std::shared_ptr< Document > DocumentPtr
Definition: RigDef_Prerequisites.h:38
RoR::ActorManager::GetInertiaConfig
RoR::CmdKeyInertiaConfig & GetInertiaConfig()
Definition: ActorManager.h:97
RoR::ActorManager::m_forced_awake
bool m_forced_awake
disables sleep counters
Definition: ActorManager.h:146
RoR::ActorManager::WakeUpAllActors
void WakeUpAllActors()
Definition: ActorManager.cpp:779
RoR::ActorManager::UpdateActors
void UpdateActors(ActorPtr player_actor)
Definition: ActorManager.cpp:1026
RoR::ActorManager::IsSimulationPaused
bool IsSimulationPaused() const
Definition: ActorManager.h:94
RoR::ActorManager::FetchNextVehicleOnList
const ActorPtr & FetchNextVehicleOnList(ActorPtr player, ActorPtr prev_player)
Definition: ActorManager.cpp:962
RoR::ActorManager::LoadScene
bool LoadScene(Ogre::String filename)
Definition: Savegame.cpp:239
RoR
Definition: AppContext.h:36
Network.h
RoR::ActorManager::GetActorById
const ActorPtr & GetActorById(ActorInstanceID_t actor_id)
Definition: ActorManager.cpp:1142
RoR::ActorManager::m_free_forces
FreeForceVec_t m_free_forces
Global forces added ad-hoc by scripts.
Definition: ActorManager.h:154
RoR::ActorManager::CleanUpSimulation
void CleanUpSimulation()
Call this after simulation loop finishes.
Definition: ActorManager.cpp:868
RoR::ActorManager::CreateNewActor
ActorPtr CreateNewActor(ActorSpawnRequest rq, RigDef::DocumentPtr def)
Definition: ActorManager.cpp:79
RoR::ActorManager::UpdatePhysicsSimulation
void UpdatePhysicsSimulation()
Definition: ActorManager.cpp:1154
RoR::ActorManager::CheckNetRemoteStreamsOk
int CheckNetRemoteStreamsOk(int sourceid)
Definition: ActorManager.cpp:534