RigsofRods
Soft-body Physics Simulation
RepairMode.cpp
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  Copyright 2015-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "RepairMode.h"
23 
24 #include "Actor.h"
25 #include "GameContext.h"
26 #include "InputEngine.h"
27 
28 using namespace RoR;
29 
31 {
32  if (App::sim_state->getEnum<SimState>() != SimState::RUNNING &&
33  App::GetGameContext()->GetPlayerActor() &&
34  App::GetGameContext()->GetPlayerActor()->ar_state != ActorState::NETWORKED_OK &&
35  App::GetGameContext()->GetPlayerActor()->ar_state != ActorState::LOCAL_REPLAY)
36  {
37  return;
38  }
39 
40  if (!App::GetGameContext()->GetPlayerActor())
41  {
42  m_quick_repair_active = false;
43  m_live_repair_active = false;
44  m_live_repair_timer = 0.0f;
45  return;
46  }
47 
48  if (App::GetInputEngine()->getEventBoolValue(EV_COMMON_LIVE_REPAIR_MODE))
49  {
50  m_live_repair_active = true;
51  // Hack to bypass the timer - because EV_COMMON_REPAIR_TRUCK (default Backspace) may not be 'EXPL' so the below condition may execute.
53  }
54 
55  // Update LiveRepair timer
58  {
59  if (App::sim_live_repair_interval->getFloat() > 0)
61  }
62  else
63  {
64  m_live_repair_timer = 0.f;
65  }
66 
67  // Handle repair controls
69  {
70  Ogre::Vector3 translation = Ogre::Vector3::ZERO;
71  float rotation = 0.0f;
72 
73  if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_ACCELERATE))
74  {
75  translation += 2.0f * Ogre::Vector3::UNIT_Y * dt;
76  }
77  else if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_BRAKE))
78  {
79  translation -= 2.0f * Ogre::Vector3::UNIT_Y * dt;
80  }
81  if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_STEER_LEFT))
82  {
83  rotation += 0.5f * dt;
84  }
85  else if (App::GetInputEngine()->getEventBoolValue(EV_TRUCK_STEER_RIGHT))
86  {
87  rotation -= 0.5f * dt;
88  }
89  if (App::GetInputEngine()->getEventBoolValue(EV_CHARACTER_FORWARD))
90  {
91  float curRot = App::GetGameContext()->GetPlayerActor()->getRotation();
92  translation.x += 2.0f * cos(curRot - Ogre::Math::HALF_PI) * dt;
93  translation.z += 2.0f * sin(curRot - Ogre::Math::HALF_PI) * dt;
94  }
95  else if (App::GetInputEngine()->getEventBoolValue(EV_CHARACTER_BACKWARDS))
96  {
97  float curRot = App::GetGameContext()->GetPlayerActor()->getRotation();
98  translation.x -= 2.0f * cos(curRot - Ogre::Math::HALF_PI) * dt;
99  translation.z -= 2.0f * sin(curRot - Ogre::Math::HALF_PI) * dt;
100  }
101  if (App::GetInputEngine()->getEventBoolValue(EV_CHARACTER_SIDESTEP_RIGHT))
102  {
103  float curRot = App::GetGameContext()->GetPlayerActor()->getRotation();
104  translation.x += 2.0f * cos(curRot) * dt;
105  translation.z += 2.0f * sin(curRot) * dt;
106  }
107  else if (App::GetInputEngine()->getEventBoolValue(EV_CHARACTER_SIDESTEP_LEFT))
108  {
109  float curRot = App::GetGameContext()->GetPlayerActor()->getRotation();
110  translation.x -= 2.0f * cos(curRot) * dt;
111  translation.z -= 2.0f * sin(curRot) * dt;
112  }
113 
114  if (translation != Ogre::Vector3::ZERO || rotation != 0.0f)
115  {
116  float scale = App::GetInputEngine()->isKeyDown(OIS::KC_LMENU) ? 0.1f : 1.0f;
117  scale *= App::GetInputEngine()->isKeyDown(OIS::KC_LSHIFT) ? 3.0f : 1.0f;
118  scale *= App::GetInputEngine()->isKeyDown(OIS::KC_LCONTROL) ? 10.0f : 1.0f;
119 
120  Ogre::Vector3 rotation_center = App::GetGameContext()->GetPlayerActor()->getRotationCenter();
121 
122  rotation *= Ogre::Math::Clamp(scale, 0.1f, 10.0f);
123  translation *= scale;
124 
125  App::GetGameContext()->GetPlayerActor()->requestRotation(rotation, rotation_center);
127 
128  if (App::sim_soft_reset_mode->getBool())
129  {
130  for (ActorPtr& actor : App::GetGameContext()->GetPlayerActor()->ar_linked_actors)
131  {
132  actor->requestRotation(rotation, rotation_center);
133  actor->requestTranslation(translation);
134  }
135  }
136 
137  m_live_repair_timer = 0.0f;
138  }
139  else if (App::GetInputEngine()->isKeyDownValueBounce(OIS::KC_SPACE))
140  {
142  if (App::sim_soft_reset_mode->getBool())
143  {
144  for (ActorPtr& actor : App::GetGameContext()->GetPlayerActor()->ar_linked_actors)
145  {
146  actor->requestAngleSnap(45);
147  }
148  }
149  }
150  else
151  {
152  m_live_repair_timer += dt;
153  }
154 
155  auto reset_type = ActorModifyRequest::Type::RESET_ON_SPOT;
156  if (App::sim_soft_reset_mode->getBool())
157  {
159  for (ActorPtr& actor : App::GetGameContext()->GetPlayerActor()->ar_linked_actors)
160  {
162  rq->amr_actor = actor->ar_instance_id;
163  rq->amr_type = reset_type;
165  }
166  }
167 
170  rq->amr_type = reset_type;
172  }
173 }
GameContext.h
Game state manager and message-queue provider.
RoR::MSG_SIM_MODIFY_ACTOR_REQUESTED
@ MSG_SIM_MODIFY_ACTOR_REQUESTED
Payload = RoR::ActorModifyRequest* (owner)
Definition: Application.h:120
RoR::EV_COMMON_REPAIR_TRUCK
@ EV_COMMON_REPAIR_TRUCK
repair truck to original condition
Definition: InputEngine.h:250
RoR::App::sim_soft_reset_mode
CVar * sim_soft_reset_mode
Definition: Application.cpp:109
RoR::Actor::ar_instance_id
ActorInstanceID_t ar_instance_id
Static attr; session-unique ID.
Definition: Actor.h:376
RoR::ActorState::LOCAL_REPLAY
@ LOCAL_REPLAY
RoR::App::sim_live_repair_interval
CVar * sim_live_repair_interval
Hold EV_COMMON_REPAIR_TRUCK to enter LiveRepair mode. 0 or negative interval disables.
Definition: Application.cpp:111
RoR::EV_CHARACTER_BACKWARDS
@ EV_CHARACTER_BACKWARDS
step backwards with the character
Definition: InputEngine.h:129
RoR::EV_COMMON_LIVE_REPAIR_MODE
@ EV_COMMON_LIVE_REPAIR_MODE
toggles live repair and recovery mode, controlled by keyboard
Definition: InputEngine.h:251
RoR::App::sim_state
CVar * sim_state
Definition: Application.cpp:96
RefCountingObjectPtr< Actor >
Actor.h
RoR::InputEngine::isKeyDown
bool isKeyDown(OIS::KeyCode mod)
Asks OIS directly.
Definition: InputEngine.cpp:1170
RoR::Actor::requestAngleSnap
void requestAngleSnap(int division)
Definition: Actor.h:104
RoR::ActorModifyRequest::amr_actor
ActorInstanceID_t amr_actor
Definition: SimData.h:886
RoR::EV_TRUCK_STEER_LEFT
@ EV_TRUCK_STEER_LEFT
steer left
Definition: InputEngine.h:357
Script2Game::KC_LSHIFT
enum Script2Game::inputEvents KC_LSHIFT
RoR::RepairMode::m_quick_repair_active
bool m_quick_repair_active
Definition: RepairMode.h:47
RoR::ActorModifyRequest
Definition: SimData.h:870
RoR::EV_CHARACTER_SIDESTEP_RIGHT
@ EV_CHARACTER_SIDESTEP_RIGHT
sidestep to the right
Definition: InputEngine.h:138
RoR::GameContext::PushMessage
void PushMessage(Message m)
Doesn't guarantee order! Use ChainMessage() if order matters.
Definition: GameContext.cpp:65
RoR::Actor::requestRotation
void requestRotation(float rotation, Ogre::Vector3 center)
Definition: Actor.h:103
Script2Game::KC_SPACE
enum Script2Game::inputEvents KC_SPACE
RoR::InputEngine::getEventBoolValue
bool getEventBoolValue(int eventID)
Definition: InputEngine.cpp:757
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:280
RoR::EV_CHARACTER_FORWARD
@ EV_CHARACTER_FORWARD
step forward with the character
Definition: InputEngine.h:130
RoR::EV_TRUCK_ACCELERATE
@ EV_TRUCK_ACCELERATE
accelerate the truck
Definition: InputEngine.h:297
RoR::Actor::getRotation
float getRotation()
Definition: Actor.cpp:408
RoR::Message
Unified game event system - all requests and state changes are reported using a message.
Definition: GameContext.h:51
RoR::EV_TRUCK_STEER_RIGHT
@ EV_TRUCK_STEER_RIGHT
steer right
Definition: InputEngine.h:358
RoR::ActorModifyRequest::Type::SOFT_RESET
@ SOFT_RESET
RoR::RepairMode::m_live_repair_timer
float m_live_repair_timer
Definition: RepairMode.h:49
RoR::Actor::getRotationCenter
Ogre::Vector3 getRotationCenter()
Definition: Actor.cpp:1493
RoR::Actor::requestTranslation
void requestTranslation(Ogre::Vector3 translation)
Definition: Actor.h:105
RoR::App::GetInputEngine
InputEngine * GetInputEngine()
Definition: Application.cpp:271
RoR::EV_CHARACTER_SIDESTEP_LEFT
@ EV_CHARACTER_SIDESTEP_LEFT
sidestep to the left
Definition: InputEngine.h:137
RoR::SimState::RUNNING
@ RUNNING
RepairMode.h
Actor feat - interactive recovery and repair mode.
InputEngine.h
Handles controller inputs from player. Defines input events and binding mechanism,...
RoR::CVar::getFloat
float getFloat() const
Definition: CVar.h:96
RoR::RepairMode::UpdateInputEvents
void UpdateInputEvents(float dt)
Definition: RepairMode.cpp:30
RoR::ActorState::NETWORKED_OK
@ NETWORKED_OK
not simulated (remote) actor
RoR::GameContext::GetPlayerActor
const ActorPtr & GetPlayerActor()
Definition: GameContext.h:134
RoR::ActorModifyRequest::amr_type
Type amr_type
Definition: SimData.h:887
RoR
Definition: AppContext.h:36
RoR::EV_TRUCK_BRAKE
@ EV_TRUCK_BRAKE
brake
Definition: InputEngine.h:306
RoR::RepairMode::m_live_repair_active
bool m_live_repair_active
Definition: RepairMode.h:48
RoR::ActorModifyRequest::Type::RESET_ON_SPOT
@ RESET_ON_SPOT