-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.cpp
467 lines (384 loc) · 18.6 KB
/
plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
///
///
/// fffb_scs
/// plugin.cpp
///
/// STD
#include <cstdlib>
#include <cassert>
#include <cstdarg>
#include <cstring>
/// SDK
#include <scssdk_telemetry.h>
#include <eurotrucks2/scssdk_eut2.h>
#include <eurotrucks2/scssdk_telemetry_eut2.h>
#include <amtrucks/scssdk_ats.h>
#include <amtrucks/scssdk_telemetry_ats.h>
/// UTI
#include <uti/container/tuple.hpp>
/// FLT
#include <flt/device.hpp>
#include <flt/wheel.hpp>
bool g_telemetry_paused { true } ;
scs_timestamp_t g_last_timestamp { static_cast< scs_timestamp_t >( -1 ) } ;
struct telemetry_state_t
{
scs_timestamp_t timestamp ;
scs_timestamp_t raw_rendering_timestamp ;
scs_timestamp_t raw_simulation_timestamp ;
scs_timestamp_t raw_paused_simulation_timestamp ;
bool orientation_available ;
float heading ;
float pitch ;
float roll ;
float steering ;
float throttle ;
float brake ;
float clutch ;
float speed ;
float rpm ;
int gear ;
} ;
telemetry_state_t g_telemetry_state ;
scs_log_t g_game_log { nullptr } ;
flt::vector< flt::wheel > g_wheels {} ;
bool init_wheels ()
{
flt::device_manager manager ;
flt::vector< flt::hid_device > wheels = manager.find_known_wheels() ;
g_wheels.clear() ;
for( auto const & device : wheels )
{
flt::wheel wheel( device ) ;
if( !wheel.calibrate() )
{
g_game_log( SCS_LOG_TYPE_warning, "fffb::warning : failed initializing device" ) ;
}
else
{
g_game_log( SCS_LOG_TYPE_message, "fffb::info : wheel initialized" ) ;
g_wheels.emplace_back( device ) ;
}
}
return !g_wheels.empty() ;
}
bool update_leds ( flt::wheel & wheel, float rpm )
{
static constexpr uti::u8_t led_0 { 0x00 } ;
static constexpr uti::u8_t led_1 { 0x01 } ;
static constexpr uti::u8_t led_2 { 0x03 } ;
static constexpr uti::u8_t led_3 { 0x07 } ;
static constexpr uti::u8_t led_4 { 0x0F } ;
static constexpr uti::u8_t led_5 { 0x1F } ;
if( rpm == 0 ) { return wheel.set_led_pattern( led_0 ) ; }
else if( rpm < 1000 ) { return wheel.set_led_pattern( led_1 ) ; }
else if( rpm < 1300 ) { return wheel.set_led_pattern( led_2 ) ; }
else if( rpm < 1600 ) { return wheel.set_led_pattern( led_3 ) ; }
else if( rpm < 1800 ) { return wheel.set_led_pattern( led_4 ) ; }
else if( rpm < 1900 ) { return wheel.set_led_pattern( led_5 ) ; }
else { return wheel.set_led_pattern( led_5 ) ; }
}
uti::u8_t map_rpm_to_freq ( float rpm )
{
return ( 255 - ( rpm / 3000.0f * 255.0f ) ) / 4 ;
}
uti::tuple< uti::u8_t, uti::u8_t > calculate_resonance_params ( float speed, float rpm, float throttle )
{
uti::u8_t amplitude { 0 } ;
uti::u8_t frequency { 0 } ;
if( rpm > 1 )
{
frequency = map_rpm_to_freq( rpm ) ;
if( speed < 5 ) { amplitude = 3 ;}
else if( speed < 45 ) { amplitude = 2 ;}
else if( speed < 75 ) { amplitude = 1 ;}
else { amplitude = 0 ;}
if( throttle < 0.10f ) { amplitude -= 1 ; }
else if( throttle < 0.25f ) { }
else if( throttle < 0.50f ) { amplitude += 1 ; }
else if( throttle < 0.75f ) { amplitude += 2 ; }
else { amplitude += 3 ; }
}
return uti::tuple{ amplitude, frequency } ;
}
uti::u8_t calculate_damper_force ( float speed, float rpm )
{
if( rpm != 0 ) return 0 ;
if( speed < 1 ) { return 3 ; }
else if( speed < 5 ) { return 2 ; }
else if( speed < 45 ) { return 1 ; }
else if( speed < 75 ) { return 1 ; }
else { return 0 ; }
}
uti::tuple< uti::u8_t, uti::u8_t > calculate_autocentering_force ( float speed )
{
uti::u8_t force ;
uti::u8_t slope ;
if( speed < 1 )
{
force = 0 ;
slope = 0 ;
}
else
{
if( speed < 5 ) { slope = 2 ; }
else if( speed < 45 ) { slope = 2 ; }
else if( speed < 75 ) { slope = 3 ; }
else { slope = 4 ; }
force = speed * speed / 2000.0f * 255 ;
if( force < 24 ) force = 24 ;
if( force > 64 ) force = 64 ;
}
return uti::tuple{ slope, force } ;
}
bool update_forces ( flt::vector< flt::wheel > & wheels, telemetry_state_t const & telemetry )
{
static uti::tuple< uti::u8_t, uti::u8_t > prev_resonance_params { 0, 0 } ;
auto resonance_params = calculate_resonance_params ( telemetry.speed, telemetry.rpm, telemetry.throttle ) ;
auto damper_force = calculate_damper_force ( telemetry.speed, telemetry.rpm ) ;
auto autocenter_params = calculate_autocentering_force( telemetry.speed ) ;
bool succ { true } ;
for( auto & wheel : wheels )
{
if( !update_leds( wheel, telemetry.rpm ) )
{
g_game_log( SCS_LOG_TYPE_error, "fffb : failed updating leds" ) ;
succ = false ;
}
if( uti::get< 0 >( resonance_params ) > 0 )
{
if( uti::get< 0 >( resonance_params ) != uti::get< 0 >( prev_resonance_params ) ||
uti::get< 1 >( resonance_params ) != uti::get< 1 >( prev_resonance_params ) )
{
if( !wheel.set_trapezoid( 128 - uti::get< 0 >( resonance_params ) ,
128 + uti::get< 0 >( resonance_params ) ,
uti::get< 1 >( resonance_params ) ,
uti::get< 1 >( resonance_params ) ,
6, 6 ) )
{
g_game_log( SCS_LOG_TYPE_error, "fffb : failed setting resonance force" ) ;
succ = false ;
}
}
}
else if( damper_force > 0 )
{
uti::get< 0 >( resonance_params ) = 0 ;
uti::get< 1 >( resonance_params ) = 0 ;
if( !wheel.set_damper( damper_force, damper_force, 0, 0 ) )
{
g_game_log( SCS_LOG_TYPE_error, "fffb : failed setting damper force" ) ;
succ = false ;
}
}
else
{
uti::get< 0 >( resonance_params ) = 0 ;
uti::get< 1 >( resonance_params ) = 0 ;
if( !wheel.stop_forces() )
{
g_game_log( SCS_LOG_TYPE_error, "fffb : failed stopping forces" ) ;
succ = false ;
}
}
if( uti::get< 1 >( autocenter_params ) > 0 )
{
if( !wheel.enable_autocenter() || !wheel.set_autocenter_spring( uti::get< 0 >( autocenter_params ), uti::get< 0 >( autocenter_params ), uti::get< 1 >( autocenter_params ) ) )
{
g_game_log( SCS_LOG_TYPE_error, "fffb : failed setting autocenter spring force" ) ;
succ = false ;
}
}
else
{
if( !wheel.disable_autocenter() )
{
g_game_log( SCS_LOG_TYPE_error, "fffb : failed disabling autocenter spring" ) ;
succ = false ;
}
}
}
uti::get< 0 >( prev_resonance_params ) = uti::get< 0 >( resonance_params ) ;
uti::get< 1 >( prev_resonance_params ) = uti::get< 1 >( resonance_params ) ;
return succ ;
}
bool update_wheels ( telemetry_state_t const & telemetry )
{
static uti::i32_t ffb_rate { 16 } ;
static uti::i32_t ffb_rate_count { 16 } ;
--ffb_rate_count ;
if( ffb_rate_count == 0 )
{
if( !update_forces( g_wheels, telemetry ) ) { g_game_log( SCS_LOG_TYPE_error, "update_forces" ) ; return false ; }
ffb_rate_count = ffb_rate ;
}
return true ;
}
bool reset_wheels ()
{
bool succ { true } ;
for( auto & wheel : g_wheels )
{
if( !wheel. stop_forces() ) succ = false ;
if( !wheel.disable_autocenter() ) succ = false ;
if( !update_leds( wheel, 0 ) ) succ = false ;
}
return succ ;
}
void deinit_wheels ()
{
g_wheels.clear() ;
}
SCSAPI_VOID telemetry_frame_start ( [[ maybe_unused ]] scs_event_t const event, void const * const event_info, [[ maybe_unused ]] scs_context_t const context )
{
scs_telemetry_frame_start_t const * const info = static_cast< scs_telemetry_frame_start_t const * >( event_info ) ;
if( g_last_timestamp == static_cast< scs_timestamp_t >( -1 ) )
{
g_last_timestamp = info->paused_simulation_time ;
}
if( info->flags & SCS_TELEMETRY_FRAME_START_FLAG_timer_restart )
{
g_last_timestamp = 0 ;
}
g_telemetry_state.timestamp += ( info->paused_simulation_time - g_last_timestamp ) ;
g_last_timestamp = info->paused_simulation_time ;
g_telemetry_state. raw_rendering_timestamp = info-> render_time ;
g_telemetry_state. raw_simulation_timestamp = info-> simulation_time ;
g_telemetry_state.raw_paused_simulation_timestamp = info->paused_simulation_time ;
}
SCSAPI_VOID telemetry_frame_end ( [[ maybe_unused ]] scs_event_t const event, [[ maybe_unused ]] void const * const event_info, [[ maybe_unused ]] scs_context_t const context )
{
if( g_telemetry_paused )
{
reset_wheels() ;
return ;
}
if( !update_wheels( g_telemetry_state ) )
{
g_game_log( SCS_LOG_TYPE_error, "fffb::error : failed updating forces!" ) ;
}
}
SCSAPI_VOID telemetry_pause ( scs_event_t const event, [[ maybe_unused ]] void const * const event_info, [[ maybe_unused ]] scs_context_t const context )
{
g_telemetry_paused = ( event == SCS_TELEMETRY_EVENT_paused ) ;
if( g_telemetry_paused )
{
reset_wheels() ;
g_game_log( SCS_LOG_TYPE_message, "fffb::info : telemetry paused, stopped forces" ) ;
}
else {}
}
SCSAPI_VOID telemetry_store_orientation ( [[ maybe_unused ]] scs_string_t const name, [[ maybe_unused ]] scs_u32_t const index, scs_value_t const * const value, scs_context_t const context )
{
assert( context ) ;
telemetry_state_t * const state = static_cast< telemetry_state_t * >( context ) ;
if( !value )
{
state->orientation_available = false ;
return ;
}
assert( value ) ;
assert( value->type == SCS_VALUE_TYPE_euler ) ;
state->orientation_available = true ;
state->heading = value->value_euler.heading * 360.0f ;
state-> pitch = value->value_euler. pitch * 360.0f ;
state-> roll = value->value_euler. roll * 360.0f ;
}
SCSAPI_VOID telemetry_store_float ( [[ maybe_unused ]] scs_string_t const name, [[ maybe_unused ]] scs_u32_t const index, scs_value_t const * const value, scs_context_t const context )
{
assert( value ) ;
assert( value->type == SCS_VALUE_TYPE_float ) ;
assert( context ) ;
*static_cast< float * >( context ) = value->value_float.value ;
}
SCSAPI_VOID telemetry_store_s32 ( [[ maybe_unused ]] scs_string_t const name, [[ maybe_unused ]] scs_u32_t const index, scs_value_t const * const value, scs_context_t const context )
{
assert( value ) ;
assert( value->type == SCS_VALUE_TYPE_s32 ) ;
assert( context ) ;
*static_cast< int * >( context ) = value->value_s32.value ;
}
SCSAPI_RESULT scs_telemetry_init ( scs_u32_t const version, scs_telemetry_init_params_t const * const params )
{
if( version != SCS_TELEMETRY_VERSION_1_01 )
{
return SCS_RESULT_unsupported ;
}
scs_telemetry_init_params_v101_t const * const version_params = static_cast< scs_telemetry_init_params_v101_t const * >( params ) ;
g_game_log = version_params->common.log ;
g_game_log( SCS_LOG_TYPE_message, "fffb::info : version " FLT_VERSION " starting initialization..." ) ;
if( strcmp( version_params->common.game_id, SCS_GAME_ID_EUT2 ) == 0 )
{
scs_u32_t const MIN_VERSION = SCS_TELEMETRY_EUT2_GAME_VERSION_1_00 ;
if( version_params->common.game_version < MIN_VERSION )
{
g_game_log( SCS_LOG_TYPE_warning, "ftl_ffb::warning : game version too old, some stuff might be broken" ) ;
}
scs_u32_t const IMPLD_VERSION = SCS_TELEMETRY_EUT2_GAME_VERSION_CURRENT ;
if( SCS_GET_MAJOR_VERSION( version_params->common.game_version ) > SCS_GET_MAJOR_VERSION( IMPLD_VERSION ) )
{
g_game_log( SCS_LOG_TYPE_warning, "ftl_ffb::warning : game major version too new, some stuff might be broken" ) ;
}
}
else if( strcmp( version_params->common.game_id, SCS_GAME_ID_ATS ) == 0 )
{
scs_u32_t const MIN_VERSION = SCS_TELEMETRY_ATS_GAME_VERSION_1_00 ;
if( version_params->common.game_version < MIN_VERSION )
{
g_game_log( SCS_LOG_TYPE_warning, "ftl_ffb::warning : game version too old, some stuff might be broken" ) ;
}
scs_u32_t const IMPLD_VERSION = SCS_TELEMETRY_ATS_GAME_VERSION_CURRENT ;
if( SCS_GET_MAJOR_VERSION( version_params->common.game_version ) > SCS_GET_MAJOR_VERSION( IMPLD_VERSION ) )
{
g_game_log( SCS_LOG_TYPE_warning, "ftl_ffb::warning : game major version too new, some stuff might be broken" ) ;
}
}
else
{
g_game_log( SCS_LOG_TYPE_warning, "ftl_ffb::warning : unknown game, some stuff might be broken" ) ;
}
g_game_log( SCS_LOG_TYPE_message, "fffb::info : version checks passed" ) ;
g_game_log( SCS_LOG_TYPE_message, "fffb::info : registering to events..." ) ;
bool const events_registered =
( version_params->register_for_event( SCS_TELEMETRY_EVENT_frame_start, telemetry_frame_start, nullptr ) == SCS_RESULT_ok ) &&
( version_params->register_for_event( SCS_TELEMETRY_EVENT_frame_end , telemetry_frame_end , nullptr ) == SCS_RESULT_ok ) &&
( version_params->register_for_event( SCS_TELEMETRY_EVENT_paused , telemetry_pause , nullptr ) == SCS_RESULT_ok ) &&
( version_params->register_for_event( SCS_TELEMETRY_EVENT_started , telemetry_pause , nullptr ) == SCS_RESULT_ok ) ;
if( !events_registered )
{
g_game_log( SCS_LOG_TYPE_error, "fffb::error : failed to register event callbacks" ) ;
return SCS_RESULT_generic_error ;
}
g_game_log( SCS_LOG_TYPE_message, "fffb::info : event registration successful" ) ;
g_game_log( SCS_LOG_TYPE_message, "fffb::info : registering to channels..." ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_world_placement, SCS_U32_NIL, SCS_VALUE_TYPE_euler, SCS_TELEMETRY_CHANNEL_FLAG_no_value, telemetry_store_orientation, &g_telemetry_state ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_speed , SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none , telemetry_store_float , &g_telemetry_state.speed ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_engine_rpm , SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none , telemetry_store_float , &g_telemetry_state.rpm ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_engine_gear , SCS_U32_NIL, SCS_VALUE_TYPE_s32 , SCS_TELEMETRY_CHANNEL_FLAG_none , telemetry_store_s32 , &g_telemetry_state.gear ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_effective_steering, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &g_telemetry_state.steering ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_effective_throttle, SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &g_telemetry_state.throttle ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_effective_brake , SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &g_telemetry_state.brake ) ;
version_params->register_for_channel( SCS_TELEMETRY_TRUCK_CHANNEL_effective_clutch , SCS_U32_NIL, SCS_VALUE_TYPE_float, SCS_TELEMETRY_CHANNEL_FLAG_none, telemetry_store_float, &g_telemetry_state.clutch ) ;
g_game_log( SCS_LOG_TYPE_message, "fffb::info : channel registration completed" ) ;
g_game_log( SCS_LOG_TYPE_message, "fffb::info : initializing wheel..." ) ;
if( !init_wheels() )
{
g_game_log( SCS_LOG_TYPE_error, "ftl_ffb::error : failed to initialize wheel" ) ;
return SCS_RESULT_generic_error ;
}
g_game_log( SCS_LOG_TYPE_message, "fffb::info : wheel initialization successful" ) ;
memset( &g_telemetry_state, 0, sizeof( g_telemetry_state ) ) ;
g_last_timestamp = static_cast< scs_timestamp_t >( -1 ) ;
g_telemetry_paused = true ;
g_game_log( SCS_LOG_TYPE_message, "fffb::info : successfully initialized" ) ;
return SCS_RESULT_ok ;
}
SCSAPI_VOID scs_telemetry_shutdown ()
{
g_game_log = nullptr ;
deinit_wheels() ;
}
void __attribute__(( destructor )) unload ()
{
deinit_wheels() ;
}