26 #include <OgreDataStream.h>
27 #include <OgreResourceGroupManager.h>
28 #include <OgreSimpleSpline.h>
29 #include <OgreVector3.h>
32 : m_start_spline(nullptr)
33 , m_stop_spline(nullptr)
42 if (!m_start_spline || !m_stop_spline)
47 float calculated_output = m_last_output;
48 float last_output = m_last_output;
50 float rel_diff = fabs(cmd_input) - fabs(last_output);
52 float abs_diff = cmd_input - last_output;
54 if (fabs(abs_diff) < 0.002)
59 const float start_factor = m_start_delay * m_time;
60 const float stop_factor = m_stop_delay * m_time;
65 calculated_output = last_output + this->CalculateCmdOutput(start_factor, m_start_spline);
68 calculated_output = last_output + this->CalculateCmdOutput(stop_factor, m_stop_spline);
69 if (calculated_output > cmd_input)
71 calculated_output = cmd_input;
77 calculated_output = last_output - this->CalculateCmdOutput(start_factor, m_start_spline);
79 calculated_output = last_output - this->CalculateCmdOutput(stop_factor, m_stop_spline);
80 if (calculated_output < cmd_input)
81 calculated_output = cmd_input;
83 m_last_output = calculated_output;
84 return calculated_output;
91 m_start_delay = start_delay;
93 RoR::LogFormat(
"[RoR|Inertia] Warning: Start Delay '%f', should be >0, using 0", start_delay);
96 m_stop_delay = stop_delay;
98 RoR::LogFormat(
"[RoR|Inertia] Warning: Stop Delay '%f', should be >0, using 0", start_delay);
101 Ogre::SimpleSpline* start_spline = cfg.
GetSplineByName(start_function);
102 if (start_spline !=
nullptr)
103 m_start_spline = start_spline;
105 RoR::LogFormat(
"[RoR|Inertia] Start Function '%s' not found", start_function.c_str());
108 if (stop_spline !=
nullptr)
109 m_stop_spline = stop_spline;
111 RoR::LogFormat(
"[RoR|Inertia] Stop Function '%s' not found", stop_function.c_str());
118 time = std::min(time, 1.0f);
122 Ogre::Vector3 output = spline->interpolate(time);
123 return output.y * 0.001f;
131 auto itor = m_splines.find(model);
132 if (itor != m_splines.end())
133 return &itor->second;
142 Ogre::DataStreamPtr ds = Ogre::ResourceGroupManager::getSingleton().openResource(
"inertia_models.cfg", Ogre::RGN_AUTODETECT);
143 std::string current_model;
147 Ogre::StringUtil::trim(line);
149 if (line.empty() || line[0] ==
';')
152 Ogre::StringVector args = Ogre::StringUtil::split(line,
",");
153 if (args.size() == 1)
155 current_model = line;
157 else if (args.size() == 2 && !current_model.empty())
160 if (m_splines.find(current_model) == m_splines.end())
162 m_splines[current_model] = Ogre::SimpleSpline();
166 const float point_x = Ogre::StringConverter::parseReal(args[0]);
167 const float point_y = Ogre::StringConverter::parseReal(args[1]);
170 m_splines[current_model].addPoint(Ogre::Vector3(point_x, point_y, 0.0f));
174 catch (std::exception& e)
176 RoR::LogFormat(
"[RoR|Inertia] Failed to load 'inertia_models.cfg', message: '%s'", e.what());
193 m_start_delay = start_delay;
195 RoR::LogFormat(
"[RoR|SimpleInertia] Warning: Start Delay '%f', should be >0, using 0", start_delay);
198 m_stop_delay = stop_delay;
200 RoR::LogFormat(
"[RoR|SimpleInertia] Warning: Stop Delay '%f', should be >0, using 0", start_delay);
203 Ogre::SimpleSpline* start_spline = cfg.
GetSplineByName(start_function);
204 if (start_spline !=
nullptr)
205 m_start_spline = start_spline;
207 RoR::LogFormat(
"[RoR|SimpleInertia] Start Function '%s' not found", start_function.c_str());
210 if (stop_spline !=
nullptr)
211 m_stop_spline = stop_spline;
213 RoR::LogFormat(
"[RoR|SimpleInertia] Stop Function '%s' not found", stop_function.c_str());
220 if (m_spline_time < 1.f)
222 m_spline_time += (dt / m_start_delay);
223 if (m_spline_time > 1.f)
231 if (m_spline_time > 0.f)
233 m_spline_time -= (dt / m_stop_delay);
234 if (m_spline_time < 0.f)
243 return m_start_spline->interpolate(m_spline_time).y;
247 return m_stop_spline->interpolate(m_spline_time).y;