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  FreeForceVec_t::iterator FindFreeForce(FreeForceID_t id);
75  FreeForceID_t GetFreeForceNextId() { return m_free_force_next_id++; }
76 
78  void UpdateActors(ActorPtr player_actor);
79  void SyncWithSimThread();
81  void WakeUpAllActors();
82  void SendAllActorsSleeping();
83  unsigned long GetNetTime() { return m_net_timer.getMilliseconds(); };
84  int GetNetTimeOffset(int sourceid);
85  void UpdateNetTimeOffset(int sourceid, int offset);
86  void AddStreamMismatch(int sourceid, int streamid) { m_stream_mismatches[sourceid].insert(streamid); };
87  int CheckNetworkStreamsOk(int sourceid);
88  int CheckNetRemoteStreamsOk(int sourceid);
89  void SetTrucksForcedAwake(bool forced) { m_forced_awake = forced; };
90  bool AreTrucksForcedAwake() const { return m_forced_awake; }
91  void SetSimulationSpeed(float speed) { m_simulation_speed = std::max(0.0f, speed); };
92  float GetSimulationSpeed() const { return m_simulation_speed; };
93  bool IsSimulationPaused() const { return m_simulation_paused; }
95  float GetTotalTime() const { return m_total_sim_time; }
97 
98 
99  void CleanUpSimulation();
100 
101  void RepairActor(Collisions* collisions, const Ogre::String& inst, const Ogre::String& box, bool keepPosition = false);
102  void UpdateSleepingState(ActorPtr player_actor, float dt);
103 
104 
105  void UpdateInputEvents(float dt);
107 
108 #ifdef USE_SOCKETW
109  void HandleActorStreamData(std::vector<RoR::NetRecvPacket> packet);
110 #endif
111 
112  // Savegames (defined in Savegame.cpp)
113 
114  bool LoadScene(Ogre::String filename);
115  bool SaveScene(Ogre::String filename);
116  void RestoreSavedState(ActorPtr actor, rapidjson::Value const& j_entry);
117 
119  std::vector<ActorPtr> GetLocalActors();
120 
121  std::pair<ActorPtr, float> GetNearestActor(Ogre::Vector3 position);
122 
123  std::map<beam_t*, std::pair<ActorPtr, ActorPtr>> inter_actor_links;
124  bool AreActorsDirectlyLinked(const ActorPtr& a1, const ActorPtr& a2);
125 
126  static const ActorPtr ACTORPTR_NULL; // Dummy value to be returned as const reference.
127 
128 private:
129 
130  bool CheckActorCollAabbIntersect(int a, int b);
131  bool PredictActorCollAabbIntersect(int a, int b);
132  void RemoveStreamSource(int sourceid);
133  void RecursiveActivation(int j, std::vector<bool>& visited);
134  void ForwardCommands(ActorPtr source_actor);
135  void UpdateTruckFeatures(ActorPtr vehicle, float dt);
136  void CalcFreeForces();
137 
138  // Networking
139  std::map<int, std::set<int>> m_stream_mismatches;
140  std::map<int, int> m_stream_time_offsets;
141  Ogre::Timer m_net_timer;
142 
143  // 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:92
RoR::ActorManager::CheckNetworkStreamsOk
int CheckNetworkStreamsOk(int sourceid)
Definition: ActorManager.cpp:521
RoR::ActorManager::GetNetTime
unsigned long GetNetTime()
Definition: ActorManager.h:83
RoR::ActorManager::SaveScene
bool SaveScene(Ogre::String filename)
Definition: Savegame.cpp:418
RoR::ActorManager::GetNetTimeOffset
int GetNetTimeOffset(int sourceid)
Definition: ActorManager.cpp:503
RoR::ActorManager::ACTORPTR_NULL
static const ActorPtr ACTORPTR_NULL
Definition: ActorManager.h:126
RoR::ActorManager::m_actors
ActorPtrVec m_actors
Use MSG_SIM_{SPAWN/DELETE}_ACTOR_REQUESTED
Definition: ActorManager.h:144
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:1594
RoR::ActorManager::inter_actor_links
std::map< beam_t *, std::pair< ActorPtr, ActorPtr > > inter_actor_links
Definition: ActorManager.h:123
RoR::ActorManager::AreTrucksForcedAwake
bool AreTrucksForcedAwake() const
Definition: ActorManager.h:90
RoR::ActorManager::RemoveStreamSource
void RemoveStreamSource(int sourceid)
Definition: ActorManager.cpp:336
RoR::ActorManager::FindActorInsideBox
ActorPtr FindActorInsideBox(Collisions *collisions, const Ogre::String &inst, const Ogre::String &box)
Definition: ActorManager.cpp:809
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:572
RoR::ActorManager::GetActors
ActorPtrVec & GetActors()
Definition: ActorManager.h:118
RoR::ActorManager::GetActorByNetworkLinks
const ActorPtr & GetActorByNetworkLinks(int source_id, int stream_id)
Definition: ActorManager.cpp:559
RoR::ActorManager::FetchPreviousVehicleOnList
const ActorPtr & FetchPreviousVehicleOnList(ActorPtr player, ActorPtr prev_player)
Definition: ActorManager.cpp:977
RoR::ActorManager::GetNearestActor
std::pair< ActorPtr, float > GetNearestActor(Ogre::Vector3 position)
Definition: ActorManager.cpp:842
RoR::ActorManager::SyncWithSimThread
void SyncWithSimThread()
Definition: ActorManager.cpp:1224
RoR::ActorManager::AreActorsDirectlyLinked
bool AreActorsDirectlyLinked(const ActorPtr &a1, const ActorPtr &a2)
Definition: ActorManager.cpp:726
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:140
RefCountingObjectPtr< Actor >
RoR::ActorSpawnRequest
Definition: SimData.h:813
RoR::ActorManager::SendAllActorsSleeping
void SendAllActorsSleeping()
Definition: ActorManager.cpp:797
RoR::ActorManager::GetTotalTime
float GetTotalTime() const
Definition: ActorManager.h:95
RoR::ActorManager::AddStreamMismatch
void AddStreamMismatch(int sourceid, int streamid)
Definition: ActorManager.h:86
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:139
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:1484
RoR::ActorManager::UpdateSleepingState
void UpdateSleepingState(ActorPtr player_actor, float dt)
Definition: ActorManager.cpp:740
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:601
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:1628
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:1589
RoR::ActorPtrVec
std::vector< ActorPtr > ActorPtrVec
Definition: ForwardDeclarations.h:237
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:353
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:1322
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:145
RoR::ActorManager::ActorManager
ActorManager()
Definition: ActorManager.cpp:63
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:513
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:1004
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:91
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:630
RoR::ActorManager::DeleteActorInternal
void DeleteActorInternal(ActorPtr actor)
Do not call directly; use GameContext::DeleteActor()
Definition: ActorManager.cpp:871
RoR::FreeForceRequest
Common for ADD and MODIFY requests; tailored for use with AngelScript thru GameScript::pushMessage().
Definition: SimData.h:785
RoR::ActorManager::m_net_timer
Ogre::Timer m_net_timer
Definition: ActorManager.h:141
RoR::ActorManager::~ActorManager
~ActorManager()
Definition: ActorManager.cpp:73
RoR::ActorManager::UpdateInputEvents
void UpdateInputEvents(float dt)
Definition: ActorManager.cpp:1333
RoR::ActorManager::SetTrucksForcedAwake
void SetTrucksForcedAwake(bool forced)
Definition: ActorManager.h:89
RoR::ActorManager::ModifyFreeForce
void ModifyFreeForce(FreeForceRequest *rq)
Definition: ActorManager.cpp:1611
RoR::ActorPtr
RefCountingObjectPtr< Actor > ActorPtr
Definition: ForwardDeclarations.h:219
RoR::CmdKeyInertiaConfig
Loads and manages 'inertia_models.cfg'.
Definition: CmdKeyInertia.h:34
RoR::ActorManager::SetSimulationPaused
void SetSimulationPaused(bool v)
Definition: ActorManager.h:94
RoR::ActorManager::UpdateTruckFeatures
void UpdateTruckFeatures(ActorPtr vehicle, float dt)
Definition: ActorManager.cpp:1423
RoR::ActorManager::ForwardCommands
void ForwardCommands(ActorPtr source_actor)
Fowards things to trailers.
Definition: ActorManager.cpp:655
RoR::ActorManager::FetchActorDef
RigDef::DocumentPtr FetchActorDef(RoR::ActorSpawnRequest &rq)
Definition: ActorManager.cpp:1243
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:828
RigDef::DocumentPtr
std::shared_ptr< Document > DocumentPtr
Definition: ForwardDeclarations.h:272
RoR::ActorManager::GetInertiaConfig
RoR::CmdKeyInertiaConfig & GetInertiaConfig()
Definition: ActorManager.h:96
RoR::ActorManager::m_forced_awake
bool m_forced_awake
disables sleep counters
Definition: ActorManager.h:146
RoR::ActorManager::WakeUpAllActors
void WakeUpAllActors()
Definition: ActorManager.cpp:785
RoR::ActorManager::UpdateActors
void UpdateActors(ActorPtr player_actor)
Definition: ActorManager.cpp:1016
RoR::ActorManager::IsSimulationPaused
bool IsSimulationPaused() const
Definition: ActorManager.h:93
RoR::ActorManager::FetchNextVehicleOnList
const ActorPtr & FetchNextVehicleOnList(ActorPtr player, ActorPtr prev_player)
Definition: ActorManager.cpp:952
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:1132
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:858
RoR::ActorManager::CreateNewActor
ActorPtr CreateNewActor(ActorSpawnRequest rq, RigDef::DocumentPtr def)
Definition: ActorManager.cpp:78
RoR::ActorManager::UpdatePhysicsSimulation
void UpdatePhysicsSimulation()
Definition: ActorManager.cpp:1144
RoR::ActorManager::CheckNetRemoteStreamsOk
int CheckNetRemoteStreamsOk(int sourceid)
Definition: ActorManager.cpp:540