From a7327625dff825279b1ba67aab1c9e35d196a983 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Wed, 18 Mar 2026 11:10:35 -0400 Subject: [PATCH] Refactor how sim calculate tyre diameter data if necessary --- src/monocoque/gameloop/gameloop.c | 117 ++++++++++++++++++------------ src/monocoque/gameloop/loopdata.h | 7 ++ src/monocoque/simulatorapi/simapi | 2 +- 3 files changed, 78 insertions(+), 48 deletions(-) diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 4df447f..08c5acb 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -29,6 +29,7 @@ uv_idle_t idler; uv_timer_t datachecktimer; uv_timer_t showstatstimer; uv_timer_t datamaptimer; +uv_timer_t tyrediametertimer; uv_udp_t recv_socket; bool doui = false; @@ -37,6 +38,7 @@ int appstate = 0; void shmdatamapcallback(uv_timer_t* handle); void showstatscallback(uv_timer_t* handle); void datacheckcallback(uv_timer_t* handle); +void tyrediametercheckcallback(uv_timer_t* handle); void startdatalogger(MonocoqueSettings* ms, loop_data* l); static void close_walk_cb(uv_handle_t* handle, void* arg) @@ -227,9 +229,44 @@ void devicetimercallback(uv_timer_t* handle) device->update(device, simdata); } + +void tyrediametercheckcallback(uv_timer_t* handle) +{ + void* b = uv_handle_get_data((uv_handle_t*) handle); + device_loop_data* f = (device_loop_data*) b; + SimData* simdata = f->simdata; + + if(simdata->car != NULL || simdata->car[0] != 0) + { + // check for saved tyre diameter in config file + // if not saved version exists get tyre diameter and save it + // use config check variable to track if the config check has been performed + // avoid many opens of the same file + int error = 0; + if(hasTyreDiameter(simdata)==false && f->ms->configcheck == 0) + { + slogi("attempting load of tyre diameter config"); + error = loadtyreconfig(simdata, f->ms->tyre_diameter_config, true); + f->ms->configcheck = 1; + } + + if(hasTyreDiameter(simdata)==false) + { + slogt("could not find tyre diameter in config file, attempting to calculate new"); + getTyreDiameter(simdata); + // if this successfully calculates data the diameters will be saved to the file after the + // sim stops actively mapping data + } + } + if(hasTyreDiameter(simdata)==true) + { + uv_timer_stop(handle); + } + +} + void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata) { - if (doui == true) { slogi("looking for ui config %s pass 1 simapi %i", ms->config_str, f->siminfo.simulatorapi); @@ -271,6 +308,7 @@ void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata) SimDevice* devices = f->simdevices; f->device_timers = (uv_timer_t*) (malloc(uv_handle_size(UV_TIMER) * numdevices)); f->device_batons = (device_loop_data*) (malloc(sizeof(device_loop_data) * numdevices)); + f->started_tyre_calc_thread = false; for (int x = 0; x < numdevices; x++) { @@ -285,10 +323,26 @@ void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata) int interval = 1000/devices[x].fps; uv_timer_start(dt, devicetimercallback, 0, interval); slogi("starting device type %i at id at %i fps: %i (%i ms ticks)", devices[x].type, x, devices[x].fps, interval); - } - else - { - slogw("skipped id %i", x); + + SimInfo siminfo = f->siminfo; + if(f->started_tyre_calc_thread == false) + { + if(devices[x].hapticeffect.effecttype == EFFECT_TYRELOCK || devices[x].hapticeffect.effecttype == EFFECT_TYRESLIP || devices[x].hapticeffect.effecttype == EFFECT_ABSBRAKES) + { + if(siminfo.SimCalculatesTyreDiameter == false && siminfo.SimSupportsHapticEffects == true && siminfo.SimCalculatesSlipRatio == false && f->ms->useconfig == 1 && f->ms->tyre_diameter_config != NULL) + { + slogi("Starting thread to calculate tyre diameters and save to config file"); + f->started_tyre_calc_thread = true; + + f->tyrebaton = (device_loop_data*) malloc(sizeof(device_loop_data)); + f->tyrebaton->simdata = simdata; + f->tyrebaton->ms = f->ms; + + uv_handle_set_data((uv_handle_t*) &tyrediametertimer, (void*) f->tyrebaton); + uv_timer_start(&tyrediametertimer, tyrediametercheckcallback, 0, 1000); + } + } + } } } @@ -296,43 +350,6 @@ void looprun(MonocoqueSettings* ms, loop_data* f, SimData* simdata) uv_timer_start(&showstatstimer, showstatscallback, 0, 100); doui = false; } - else - { - //showstats(simdata); - //SimDevice* devices = f->simdevices; - //int numdevices = f->numdevices; - //for (int x = 0; x < numdevices; x++) - //{ - // if (devices[x].initialized == true && devices[x].type == 2) - // { - // devices[x].update(&devices[x], simdata); - // } - //} - - if(f->ms->useconfig == 1 && f->ms->tyre_diameter_config != NULL) - { - if(simdata->car != NULL || simdata->car[0] != 0) - { - // check for saved tyre diameter in config file - // if not saved version exists get tyre diameter and save it - // use config check variable to track if the config check has been performed - // avoid many opens of the same file - int error = 0; - if(hasTyreDiameter(simdata)==false && f->ms->configcheck == 0) - { - slogi("attempting load of tyre diameter config"); - error = loadtyreconfig(simdata, f->ms->tyre_diameter_config, true); - f->ms->configcheck = 1; - } - - if(hasTyreDiameter(simdata)==false) - { - slogt("could not find tyre diameter in config file, attempting to calculate new"); - getTyreDiameter(simdata); - } - } - } - } } void showstatscallback(uv_timer_t* handle) @@ -366,14 +383,19 @@ void releaseloop(loop_data* f, SimData* simdata, SimMap* simmap) slogi("releasing devices, please wait"); //attempt tyre diameter saving - if(hasTyreDiameter(simdata) == true) + uv_timer_stop(&tyrediametertimer); + if(f->started_tyre_calc_thread == true) { - int a = loadtyreconfig(simdata, f->ms->tyre_diameter_config, false); - if(a < 0) + if(hasTyreDiameter(simdata) == true) { - slogi("saving new tyre diameter config"); - savetyreconfig(simdata, f->ms->tyre_diameter_config); + int a = loadtyreconfig(simdata, f->ms->tyre_diameter_config, false); + if(a < 0) + { + slogi("saving new tyre diameter config"); + savetyreconfig(simdata, f->ms->tyre_diameter_config); + } } + free(f->tyrebaton); } f->uion = false; @@ -649,6 +671,7 @@ int monocoque_mainloop(MonocoqueSettings* ms) uv_timer_init(uv_default_loop(), &datachecktimer); uv_timer_init(uv_default_loop(), &showstatstimer); uv_timer_init(uv_default_loop(), &datamaptimer); + uv_timer_init(uv_default_loop(), &tyrediametertimer); slogd("setting initial app state"); appstate = 1; diff --git a/src/monocoque/gameloop/loopdata.h b/src/monocoque/gameloop/loopdata.h index 85e5acf..801f2de 100644 --- a/src/monocoque/gameloop/loopdata.h +++ b/src/monocoque/gameloop/loopdata.h @@ -10,9 +10,11 @@ typedef struct device_loop_data { + // these are all pointers to pointers and freed independently, usually in releaseloop uv_work_t req; SimData* simdata; SimDevice* simdevice; + MonocoqueSettings* ms; SimInfo siminfo; } device_loop_data; @@ -22,14 +24,19 @@ typedef struct loop_data bool use_udp; bool uion; bool releasing; + bool started_tyre_calc_thread; int numdevices; SimInfo siminfo; + // monocoque settings is a pointer from monocoque.c and freed there MonocoqueSettings* ms; + // these are freed at the end of the main gameloop SimData* simdata; SimMap* simmap; + // these all get malloced in looprun and freed in releaseloop SimDevice* simdevices; uv_timer_t* device_timers; device_loop_data* device_batons; + device_loop_data* tyrebaton; } loop_data; diff --git a/src/monocoque/simulatorapi/simapi b/src/monocoque/simulatorapi/simapi index 9f732a7..1c0b788 160000 --- a/src/monocoque/simulatorapi/simapi +++ b/src/monocoque/simulatorapi/simapi @@ -1 +1 @@ -Subproject commit 9f732a735d35b77aff4ce564f8f792e0ad7628d2 +Subproject commit 1c0b788adb10aaa10c6a5bd63fdeb5811e8ab1ca