RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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);
66  ActorInstanceID_t GetActorNextInstanceId() { return m_actor_next_instance_id++; }
67 
74  bool FindFreeForce(FreeForceID_t id, FreeForceVec_t::iterator& out_itor);
75  FreeForceID_t GetFreeForceNextId() { return m_free_force_next_id++; }
78 
79  void UpdateActors(ActorPtr player_actor);
80  void SyncWithSimThread();
82  void WakeUpAllActors();
83  void SendAllActorsSleeping();
84  unsigned long GetNetTime() { return m_net_timer.getMilliseconds(); };
85  int GetNetTimeOffset(int sourceid);
86  void UpdateNetTimeOffset(int sourceid, int offset);
87  void AddStreamMismatch(int sourceid, int streamid) { m_stream_mismatches[sourceid].insert(streamid); };
88  int CheckNetworkStreamsOk(int sourceid);
89  int CheckNetRemoteStreamsOk(int sourceid);
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  // Truck file handling
109  void ExportActorDef(RigDef::DocumentPtr def, std::string filename, std::string rg_name);
110 
111 #ifdef USE_SOCKETW
112  void HandleActorStreamData(std::vector<RoR::NetRecvPacket> packet);
113 #endif
114 
115  // Savegames (defined in Savegame.cpp)
116 
117  bool LoadScene(Ogre::String filename);
118  bool SaveScene(Ogre::String filename);
119  void RestoreSavedState(ActorPtr actor, rapidjson::Value const& j_entry);
120 
122  std::vector<ActorPtr> GetLocalActors();
123 
124  std::pair<ActorPtr, float> GetNearestActor(Ogre::Vector3 position);
125 
126  std::map<beam_t*, std::pair<ActorPtr, ActorPtr>> inter_actor_links;
127  bool AreActorsDirectlyLinked(const ActorPtr& a1, const ActorPtr& a2);
128 
129  static const ActorPtr ACTORPTR_NULL; // Dummy value to be returned as const reference.
130 
131 private:
132 
133  bool CheckActorCollAabbIntersect(int a, int b);
134  bool PredictActorCollAabbIntersect(int a, int b);
135  void RemoveStreamSource(int sourceid);
136  void RecursiveActivation(int j, std::vector<bool>& visited);
137  void ForwardCommands(ActorPtr source_actor);
138  void UpdateTruckFeatures(ActorPtr vehicle, float dt);
139  void CalcFreeForces();
140 
141  // Networking
142  std::map<int, std::set<int>> m_stream_mismatches;
143  std::map<int, int> m_stream_time_offsets;
144  Ogre::Timer m_net_timer;
145 
146  // Physics
149  bool m_forced_awake = false;
151  float m_dt_remainder = 0.f;
152  float m_simulation_speed = 1.f;
153  float m_last_simulation_speed = 0.1f;
154  float m_simulation_time = 0.f;
155  bool m_simulation_paused = false;
156  float m_total_sim_time = 0.f;
159 
160  // Utils
161  std::unique_ptr<ThreadPool> m_sim_thread_pool;
162  std::shared_ptr<Task> m_sim_task;
164 };
165 
167 
168 } // namespace RoR
RoR::ActorManager::GetSimulationSpeed
float GetSimulationSpeed() const
Definition: ActorManager.h:93
RoR::ActorManager::CheckNetworkStreamsOk
int CheckNetworkStreamsOk(int sourceid)
Definition: ActorManager.cpp:528
RoR::ActorManager::GetNetTime
unsigned long GetNetTime()
Definition: ActorManager.h:84
RoR::ActorManager::SaveScene
bool SaveScene(Ogre::String filename)
Definition: Savegame.cpp:418
RoR::ActorManager::GetNetTimeOffset
int GetNetTimeOffset(int sourceid)
Definition: ActorManager.cpp:510
RoR::ActorManager::ACTORPTR_NULL
static const ActorPtr ACTORPTR_NULL
Definition: ActorManager.h:129
RoR::ActorManager::m_actors
ActorPtrVec m_actors
Use MSG_SIM_{SPAWN/DELETE}_ACTOR_REQUESTED
Definition: ActorManager.h:147
RoR::Collisions
Definition: Collisions.h:80
RigDef_Prerequisites.h
RoR::ActorManager::m_inertia_config
RoR::CmdKeyInertiaConfig m_inertia_config
Definition: ActorManager.h:163
ThreadPool.h
RoR::ActorManager::AddFreeForce
void AddFreeForce(FreeForceRequest *rq)
Definition: ActorManager.cpp:1767
RoR::ActorManager::inter_actor_links
std::map< beam_t *, std::pair< ActorPtr, ActorPtr > > inter_actor_links
Definition: ActorManager.h:126
RoR::ActorManager::AreTrucksForcedAwake
bool AreTrucksForcedAwake() const
Definition: ActorManager.h:91
RoR::ActorManager::RemoveStreamSource
void RemoveStreamSource(int sourceid)
Definition: ActorManager.cpp:343
RoR::ActorManager::FindActorInsideBox
ActorPtr FindActorInsideBox(Collisions *collisions, const Ogre::String &inst, const Ogre::String &box)
Definition: ActorManager.cpp:816
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:579
RoR::ActorManager::GetActors
ActorPtrVec & GetActors()
Definition: ActorManager.h:121
RoR::ActorManager::GetActorByNetworkLinks
const ActorPtr & GetActorByNetworkLinks(int source_id, int stream_id)
Definition: ActorManager.cpp:566
RoR::ActorManager::FetchPreviousVehicleOnList
const ActorPtr & FetchPreviousVehicleOnList(ActorPtr player, ActorPtr prev_player)
Definition: ActorManager.cpp:984
RoR::ActorManager::GetNearestActor
std::pair< ActorPtr, float > GetNearestActor(Ogre::Vector3 position)
Definition: ActorManager.cpp:849
RoR::ActorManager::SyncWithSimThread
void SyncWithSimThread()
Definition: ActorManager.cpp:1231
RoR::ActorManager::AreActorsDirectlyLinked
bool AreActorsDirectlyLinked(const ActorPtr &a1, const ActorPtr &a2)
Definition: ActorManager.cpp:733
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:143
RefCountingObjectPtr< Actor >
RoR::ActorSpawnRequest
Definition: SimData.h:817
RoR::ActorManager::SendAllActorsSleeping
void SendAllActorsSleeping()
Definition: ActorManager.cpp:804
RoR::ActorManager::GetTotalTime
float GetTotalTime() const
Definition: ActorManager.h:96
RoR::ActorManager::AddStreamMismatch
void AddStreamMismatch(int sourceid, int streamid)
Definition: ActorManager.h:87
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:152
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:142
RoR::ActorInstanceID_t
int ActorInstanceID_t
Unique sequentially generated ID of an actor in session. Use ActorManager::GetActorById()
Definition: ForwardDeclarations.h:38
RoR::ActorManager::CalcFreeForces
void CalcFreeForces()
Apply FreeForces - intentionally as a separate pass over all actors.
Definition: ActorManager.cpp:1522
RoR::ActorManager::UpdateSleepingState
void UpdateSleepingState(ActorPtr player_actor, float dt)
Definition: ActorManager.cpp:747
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:608
CmdKeyInertia.h
RoR::ActorManager::m_simulation_paused
bool m_simulation_paused
Definition: ActorManager.h:155
RoR::ActorManager::m_dt_remainder
float m_dt_remainder
Keeps track of the rounding error in the time step calculation.
Definition: ActorManager.h:151
RoR::ActorManager::RemoveFreeForce
void RemoveFreeForce(FreeForceID_t id)
Definition: ActorManager.cpp:1804
SimData.h
Core data structures for simulation; Everything affected by by either physics, network or user intera...
RoR::ActorPtrVec
std::vector< ActorPtr > ActorPtrVec
Definition: ForwardDeclarations.h:246
RoR::ActorManager::m_sim_task
std::shared_ptr< Task > m_sim_task
Definition: ActorManager.h:162
RoR::ActorManager::HandleActorStreamData
void HandleActorStreamData(std::vector< RoR::NetRecvPacket > packet)
Definition: ActorManager.cpp:360
RoR::ActorManager::m_free_force_next_id
FreeForceID_t m_free_force_next_id
Unique ID for each FreeForce.
Definition: ActorManager.h:158
RoR::ActorManager::m_sim_thread_pool
std::unique_ptr< ThreadPool > m_sim_thread_pool
Definition: ActorManager.h:161
RoR::ActorManager::GetLocalActors
std::vector< ActorPtr > GetLocalActors()
Definition: ActorManager.cpp:1360
RoR::FreeForceID_t
int FreeForceID_t
Unique sequentially generated ID of FreeForce; use ActorManager::GetFreeForceNextId().
Definition: ForwardDeclarations.h:67
RoR::ActorManager::m_actor_next_instance_id
ActorInstanceID_t m_actor_next_instance_id
Unique sequential ID for each Actor.
Definition: ActorManager.h:148
RoR::ActorManager::ActorManager
ActorManager()
Definition: ActorManager.cpp:67
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:520
RoR::ActorManager::m_physics_steps
int m_physics_steps
Definition: ActorManager.h:150
Application.h
Central state/object manager and communications hub.
RoR::ActorManager::GetFreeForces
FreeForceVec_t & GetFreeForces()
Definition: ActorManager.h:76
RoR::ActorManager::FetchRescueVehicle
const ActorPtr & FetchRescueVehicle()
Definition: ActorManager.cpp:1011
RoR::ActorManager::m_total_sim_time
float m_total_sim_time
Definition: ActorManager.h:156
RoR::ActorManager::m_simulation_time
float m_simulation_time
Amount of time the physics simulation is going to be advanced.
Definition: ActorManager.h:154
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:153
RoR::ActorManager::ExportActorDef
void ExportActorDef(RigDef::DocumentPtr def, std::string filename, std::string rg_name)
Definition: ActorManager.cpp:1329
RoR::ActorManager::RecursiveActivation
void RecursiveActivation(int j, std::vector< bool > &visited)
Definition: ActorManager.cpp:637
RoR::ActorManager::DeleteActorInternal
void DeleteActorInternal(ActorPtr actor)
Do not call directly; use GameContext::DeleteActor()
Definition: ActorManager.cpp:878
RoR::FreeForceRequest
Common for ADD and MODIFY requests; tailored for use with AngelScript thru GameScript::pushMessage().
Definition: SimData.h:783
RoR::ActorManager::m_net_timer
Ogre::Timer m_net_timer
Definition: ActorManager.h:144
RoR::ActorManager::~ActorManager
~ActorManager()
Definition: ActorManager.cpp:77
RoR::ActorManager::UpdateInputEvents
void UpdateInputEvents(float dt)
Definition: ActorManager.cpp:1371
RoR::ActorManager::SetTrucksForcedAwake
void SetTrucksForcedAwake(bool forced)
Definition: ActorManager.h:90
RoR::ActorManager::ModifyFreeForce
void ModifyFreeForce(FreeForceRequest *rq)
Definition: ActorManager.cpp:1786
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:1461
RoR::ActorManager::ForwardCommands
void ForwardCommands(ActorPtr source_actor)
Fowards things to trailers.
Definition: ActorManager.cpp:662
RoR::ActorManager::FetchActorDef
RigDef::DocumentPtr FetchActorDef(RoR::ActorSpawnRequest &rq)
Definition: ActorManager.cpp:1250
RoR::ActorManager::RestoreSavedState
void RestoreSavedState(ActorPtr actor, rapidjson::Value const &j_entry)
Definition: Savegame.cpp:787
RoR::ActorManager::RepairActor
void RepairActor(Collisions *collisions, const Ogre::String &inst, const Ogre::String &box, bool keepPosition=false)
Definition: ActorManager.cpp:835
RigDef::DocumentPtr
std::shared_ptr< Document > DocumentPtr
Definition: ForwardDeclarations.h:281
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:149
RoR::ActorManager::FindFreeForce
bool FindFreeForce(FreeForceID_t id, FreeForceVec_t::iterator &out_itor)
Definition: ActorManager.cpp:1761
RoR::ActorManager::WakeUpAllActors
void WakeUpAllActors()
Definition: ActorManager.cpp:792
RoR::ActorManager::UpdateActors
void UpdateActors(ActorPtr player_actor)
Definition: ActorManager.cpp:1023
RoR::ActorManager::IsSimulationPaused
bool IsSimulationPaused() const
Definition: ActorManager.h:94
RoR::ActorManager::FetchNextVehicleOnList
const ActorPtr & FetchNextVehicleOnList(ActorPtr player, ActorPtr prev_player)
Definition: ActorManager.cpp:959
RoR::ActorManager::LoadScene
bool LoadScene(Ogre::String filename)
Definition: Savegame.cpp:240
RoR
Definition: AppContext.h:36
Network.h
RoR::ActorManager::GetActorById
const ActorPtr & GetActorById(ActorInstanceID_t actor_id)
Definition: ActorManager.cpp:1139
RoR::ActorManager::m_free_forces
FreeForceVec_t m_free_forces
Global forces added ad-hoc by scripts.
Definition: ActorManager.h:157
RoR::ActorManager::CleanUpSimulation
void CleanUpSimulation()
Call this after simulation loop finishes.
Definition: ActorManager.cpp:865
RoR::ActorManager::CreateNewActor
ActorPtr CreateNewActor(ActorSpawnRequest rq, RigDef::DocumentPtr def)
Definition: ActorManager.cpp:82
RoR::ActorManager::UpdatePhysicsSimulation
void UpdatePhysicsSimulation()
Definition: ActorManager.cpp:1151
RoR::ActorManager::CheckNetRemoteStreamsOk
int CheckNetRemoteStreamsOk(int sourceid)
Definition: ActorManager.cpp:547