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 m_start_function = start_function;
102 Ogre::SimpleSpline* start_spline = cfg.
GetSplineByName(start_function);
103 if (start_spline !=
nullptr)
104 m_start_spline = start_spline;
106 RoR::LogFormat(
"[RoR|Inertia] Start Function '%s' not found", start_function.c_str());
108 m_stop_function = stop_function;
110 if (stop_spline !=
nullptr)
111 m_stop_spline = stop_spline;
113 RoR::LogFormat(
"[RoR|Inertia] Stop Function '%s' not found", stop_function.c_str());
120 time = std::min(time, 1.0f);
124 Ogre::Vector3 output = spline->interpolate(time);
125 return output.y * 0.001f;
133 auto itor = m_splines.find(model);
134 if (itor != m_splines.end())
135 return &itor->second;
144 Ogre::DataStreamPtr ds = Ogre::ResourceGroupManager::getSingleton().openResource(
"inertia_models.cfg", Ogre::RGN_AUTODETECT);
145 std::string current_model;
149 Ogre::StringUtil::trim(line);
151 if (line.empty() || line[0] ==
';')
154 Ogre::StringVector args = Ogre::StringUtil::split(line,
",");
155 if (args.size() == 1)
157 current_model = line;
159 else if (args.size() == 2 && !current_model.empty())
162 if (m_splines.find(current_model) == m_splines.end())
164 m_splines[current_model] = Ogre::SimpleSpline();
168 const float point_x = Ogre::StringConverter::parseReal(args[0]);
169 const float point_y = Ogre::StringConverter::parseReal(args[1]);
172 m_splines[current_model].addPoint(Ogre::Vector3(point_x, point_y, 0.0f));
176 catch (std::exception& e)
178 RoR::LogFormat(
"[RoR|Inertia] Failed to load 'inertia_models.cfg', message: '%s'", e.what());
195 m_start_delay = start_delay;
197 RoR::LogFormat(
"[RoR|SimpleInertia] Warning: Start Delay '%f', should be >0, using 0", start_delay);
200 m_stop_delay = stop_delay;
202 RoR::LogFormat(
"[RoR|SimpleInertia] Warning: Stop Delay '%f', should be >0, using 0", start_delay);
205 Ogre::SimpleSpline* start_spline = cfg.
GetSplineByName(start_function);
206 if (start_spline !=
nullptr)
207 m_start_spline = start_spline;
209 RoR::LogFormat(
"[RoR|SimpleInertia] Start Function '%s' not found", start_function.c_str());
212 if (stop_spline !=
nullptr)
213 m_stop_spline = stop_spline;
215 RoR::LogFormat(
"[RoR|SimpleInertia] Stop Function '%s' not found", stop_function.c_str());
222 if (m_spline_time < 1.f)
224 m_spline_time += (dt / m_start_delay);
225 if (m_spline_time > 1.f)
233 if (m_spline_time > 0.f)
235 m_spline_time -= (dt / m_stop_delay);
236 if (m_spline_time < 0.f)
245 return m_start_spline->interpolate(m_spline_time).y;
249 return m_stop_spline->interpolate(m_spline_time).y;