Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
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 "CmdKeyInertia.h"
29#include "Network.h"
31#include "ScriptEvents.h"
32#include "SimData.h"
33#include "ThreadPool.h"
34
35#include <string>
36#include <vector>
37
38namespace RoR {
39
42
45{
46public:
47
48 typedef std::vector<FreeForce> FreeForceVec_t;
49
52
56 void DeleteActorInternal(ActorPtr actor);
58
62 const ActorPtr& GetActorByNetworkLinks(int source_id, int stream_id);
63 ActorPtr FindActorInsideBox(Collisions* collisions, const Ogre::String& inst, const Ogre::String& box);
64 const ActorPtr& FetchNextVehicleOnList(ActorPtr player, ActorPtr prev_player);
65 const ActorPtr& FetchPreviousVehicleOnList(ActorPtr player, ActorPtr prev_player);
69
75 bool FindFreeForce(FreeForceID_t id, FreeForceVec_t::iterator& out_itor);
79
80 void UpdateActors(ActorPtr player_actor);
81 void SyncWithSimThread();
83 void WakeUpAllActors();
85 void SetTrucksForcedAwake(bool forced) { m_forced_awake = forced; };
86 bool AreTrucksForcedAwake() const { return m_forced_awake; }
87 void SetSimulationSpeed(float speed) { m_simulation_speed = std::max(0.0f, speed); };
88 float GetSimulationSpeed() const { return m_simulation_speed; };
89 bool IsSimulationPaused() const { return m_simulation_paused; }
91 float GetTotalTime() const { return m_total_sim_time; }
93
94
95 void CleanUpSimulation();
96
97 void RepairActor(Collisions* collisions, const Ogre::String& inst, const Ogre::String& box, bool keepPosition = false);
98 void UpdateSleepingState(ActorPtr player_actor, float dt);
99
100
101 void UpdateInputEvents(float dt);
103 // Truck file handling
104 void ExportActorDef(RigDef::DocumentPtr def, std::string filename, std::string rg_name);
105
108#ifdef USE_SOCKETW
109 void HandleActorStreamData(std::vector<RoR::NetRecvPacket> packet);
110#endif
111 unsigned long GetNetTime() { return m_net_timer.getMilliseconds(); };
112 int GetNetTimeOffset(int sourceid);
113 void UpdateNetTimeOffset(int sourceid, int offset);
116 void RequestSpawnRemoteActor(RoRnet::ActorStreamRegister* reg, const CacheEntryPtr& cache_entry, const RoRnet::UserInfo& userinfo, BitMask_t peeropts);
120
121 // Savegames (defined in Savegame.cpp)
122
123 bool LoadScene(Ogre::String filename);
124 bool SaveScene(Ogre::String filename);
125 void RestoreSavedState(ActorPtr actor, rapidjson::Value const& j_entry);
126
128 std::vector<ActorPtr> GetLocalActors();
129
130 std::pair<ActorPtr, float> GetNearestActor(Ogre::Vector3 position);
131
132 std::map<beam_t*, std::pair<ActorPtr, ActorPtr>> inter_actor_links;
133 bool AreActorsDirectlyLinked(const ActorPtr& a1, const ActorPtr& a2);
134
135 static const ActorPtr ACTORPTR_NULL; // Dummy value to be returned as const reference.
136
137private:
138
139 bool CheckActorCollAabbIntersect(int a, int b);
140 bool PredictActorCollAabbIntersect(int a, int b);
141 void RemoveStreamSource(int sourceid);
142 void RemoveStream(int sourceid, int streamid);
143 void RecursiveActivation(int j, std::vector<bool>& visited);
144 void ForwardCommands(ActorPtr source_actor);
145 void UpdateTruckFeatures(ActorPtr vehicle, float dt);
146 void CalcFreeForces();
147
148 // Networking
149 std::map<int, std::set<int>> m_stream_mismatches;
150 std::vector<RoRnet::ActorStreamRegister> m_stream_mismatched_regs;
151 std::map<int, int> m_stream_time_offsets;
152 Ogre::Timer m_net_timer;
153
154 // Physics
157 bool m_forced_awake = false;
159 float m_dt_remainder = 0.f;
160 float m_simulation_speed = 1.f;
162 float m_simulation_time = 0.f;
164 float m_total_sim_time = 0.f;
167
168 // Utils
169 std::unique_ptr<ThreadPool> m_sim_thread_pool;
170 std::shared_ptr<Task> m_sim_task;
172};
173
175
176} // namespace RoR
Central state/object manager and communications hub.
uint32_t BitMask_t
Definition BitFlags.h:7
Core data structures for simulation; Everything affected by by either physics, network or user intera...
Builds and manages softbody actors (physics on background thread, networking)
ActorPtrVec & GetActors()
void UpdateSleepingState(ActorPtr player_actor, float dt)
FreeForceID_t GetFreeForceNextId()
Script proxy: game.getFreeForceNextId()
RigDef::DocumentPtr FetchActorDef(RoR::ActorSpawnRequest &rq)
bool LoadScene(Ogre::String filename)
Definition Savegame.cpp:240
float m_simulation_time
Amount of time the physics simulation is going to be advanced.
void SetSimulationSpeed(float speed)
void RestoreSavedState(ActorPtr actor, rapidjson::Value const &j_entry)
Definition Savegame.cpp:787
void RequestSpawnRemoteActor(RoRnet::ActorStreamRegister *reg, const CacheEntryPtr &cache_entry, const RoRnet::UserInfo &userinfo, BitMask_t peeropts)
void DeleteActorInternal(ActorPtr actor)
Do not call directly; use GameContext::DeleteActor()
RoRnet::UiStreamsHealth CheckNetworkStreamsOk(int sourceid)
RoRnet::UiStreamsHealth CheckNetRemoteStreamsOk(int sourceid)
const ActorPtr & FetchNextVehicleOnList(ActorPtr player, ActorPtr prev_player)
std::map< beam_t *, std::pair< ActorPtr, ActorPtr > > inter_actor_links
bool SaveScene(Ogre::String filename)
Definition Savegame.cpp:418
void RemoveFreeForce(FreeForceID_t id)
std::shared_ptr< Task > m_sim_task
void ModifyFreeForce(FreeForceRequest *rq)
RoR::CmdKeyInertiaConfig & GetInertiaConfig()
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.
const ActorPtr & GetActorByNetworkLinks(int source_id, int stream_id)
void UpdateTruckFeatures(ActorPtr vehicle, float dt)
float m_simulation_speed
slow motion < 1.0 < fast motion
void CleanUpSimulation()
Call this after simulation loop finishes.
ActorPtrVec m_actors
Use MSG_SIM_{SPAWN/DELETE}_ACTOR_REQUESTED
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...
const ActorPtr & FetchPreviousVehicleOnList(ActorPtr player, ActorPtr prev_player)
void RemoveStreamSource(int sourceid)
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...
ActorInstanceID_t m_actor_next_instance_id
Unique sequential ID for each Actor.
void UpdateNetTimeOffset(int sourceid, int offset)
unsigned long GetNetTime()
void ForwardCommands(ActorPtr source_actor)
Fowards things to trailers.
bool m_forced_awake
disables sleep counters
void SetTrucksForcedAwake(bool forced)
void ExportActorDef(RigDef::DocumentPtr def, std::string filename, std::string rg_name)
void SetSimulationPaused(bool v)
bool IsSimulationPaused() const
ActorPtr FindActorInsideBox(Collisions *collisions, const Ogre::String &inst, const Ogre::String &box)
FreeForceID_t m_free_force_next_id
Unique ID for each FreeForce.
void UpdateActors(ActorPtr player_actor)
bool AreTrucksForcedAwake() const
FreeForceVec_t m_free_forces
Global forces added ad-hoc by scripts.
FreeForceVec_t & GetFreeForces()
void RetryFailedStreamRegistrations(ScriptEventArgs *args)
float GetSimulationSpeed() const
int GetNetTimeOffset(int sourceid)
float GetTotalTime() const
const ActorPtr & GetActorById(ActorInstanceID_t actor_id)
void RemoveStream(int sourceid, int streamid)
ActorPtr CreateNewActor(ActorSpawnRequest rq, RigDef::DocumentPtr def)
void AddStreamMismatch(RoRnet::ActorStreamRegister *reg)
void HandleActorStreamData(std::vector< RoR::NetRecvPacket > packet)
std::vector< FreeForce > FreeForceVec_t
std::pair< ActorPtr, float > GetNearestActor(Ogre::Vector3 position)
void UpdateInputEvents(float dt)
float m_last_simulation_speed
previously used time ratio between real time (evt.timeSinceLastFrame) and physics time ('dt' used in ...
void AddFreeForce(FreeForceRequest *rq)
std::map< int, int > m_stream_time_offsets
Networking: A network time offset for each stream source.
void RecursiveActivation(int j, std::vector< bool > &visited)
ActorInstanceID_t GetActorNextInstanceId()
Script proxy: game.getActorNextInstanceId()
void RepairActor(Collisions *collisions, const Ogre::String &inst, const Ogre::String &box, bool keepPosition=false)
void CalcFreeForces()
Apply FreeForces - intentionally as a separate pass over all actors.
const ActorPtr & FetchRescueVehicle()
std::vector< RoRnet::ActorStreamRegister > m_stream_mismatched_regs
Networking: Remember mismatched stream regs to re-process after downloading the missing mods.
std::vector< ActorPtr > GetLocalActors()
float m_dt_remainder
Keeps track of the rounding error in the time step calculation.
RoR::CmdKeyInertiaConfig m_inertia_config
bool FindFreeForce(FreeForceID_t id, FreeForceVec_t::iterator &out_itor)
Ogre::Timer m_net_timer
std::unique_ptr< ThreadPool > m_sim_thread_pool
static const ActorPtr ACTORPTR_NULL
bool AreActorsDirectlyLinked(const ActorPtr &a1, const ActorPtr &a2)
Loads and manages 'inertia_models.cfg'.
std::shared_ptr< Document > DocumentPtr
std::vector< ActorPtr > ActorPtrVec
int ActorInstanceID_t
Unique sequentially generated ID of an actor in session. Use ActorManager::GetActorById()
int FreeForceID_t
Unique sequentially generated ID of FreeForce; use ActorManager::GetFreeForceNextId().
UiStreamsHealth
Definition RoRnet.h:138
Common for ADD and MODIFY requests; tailored for use with AngelScript thru GameScript::pushMessage().
Definition SimData.h:784
Args for eventCallbackEx() queued via MSG_SIM_SCRIPT_EVENT_TRIGGERED See descriptions at enum RoR::sc...
< Must preserve mem. layout of RoRnet::StreamRegister
Definition RoRnet.h:170