diff --git a/src/k_kart.c b/src/k_kart.c index 858db0b12..84c2cceae 100644 --- a/src/k_kart.c +++ b/src/k_kart.c @@ -16063,20 +16063,16 @@ fixed_t K_GetGradingFactorMinMax(player_t *player, boolean max) UINT16 K_GetEXP(player_t *player) { UINT32 numgradingpoints = K_GetNumGradingPoints(); - UINT16 targetminexp = (MINEXP*player->gradingpointnum/max(1,numgradingpoints)); // about what a last place player should be at this stage of the race - UINT16 targetexp = (MAXEXP*player->gradingpointnum/max(1,numgradingpoints)); // about what a 1.0 factor should be at this stage of the race + fixed_t targetminexp = (MINEXP*player->gradingpointnum<gradingpointnum<gradingfactor)); - fixed_t range = factormax - factormin; - fixed_t normalizedfactor = FixedDiv(clampedfactor - factormin, range); - fixed_t easedexp = Easing_Linear(normalizedfactor, targetminexp, targetexp); - // fixed_t easedexp = Easing_Linear(normalizedfactor, MINEXP*FRACUNIT, MAXEXP*FRACUNIT); - UINT16 exp = easedexp; - // CONS_Printf("Player %s numgradingpoints=%d targetminexp=%d targetexp=%d factormin=%.2f factormax=%.2f clampedfactor=%.2f normalizedfactor=%.2f easedexp=%d\n", - // player_names[player - players], numgradingpoints, targetminexp, targetexp, FIXED_TO_FLOAT(factormin), FIXED_TO_FLOAT(factormax), - // FIXED_TO_FLOAT(clampedfactor), FIXED_TO_FLOAT(normalizedfactor), easedexp); - // UINT16 exp = (player->gradingfactor*100)>>FRACBITS; + + UINT16 exp = FixedRescale(player->gradingfactor, factormin, factormax, Easing_Linear, targetminexp, targetmaxexp)>>FRACBITS; + + // CONS_Printf("Player %s numgradingpoints=%d gradingpoint=%d targetminexp=%d targetmaxexp=%d factor=%.2f factormin=%.2f factormax=%.2f exp=%d\n", + // player_names[player - players], numgradingpoints, player->gradingpointnum, targetminexp, targetmaxexp, FIXED_TO_FLOAT(player->gradingfactor), FIXED_TO_FLOAT(factormin), FIXED_TO_FLOAT(factormax), exp); + return exp; } diff --git a/src/m_easing.c b/src/m_easing.c index ef48abe51..e1e225ecd 100644 --- a/src/m_easing.c +++ b/src/m_easing.c @@ -430,3 +430,93 @@ const char *easing_funcnames[EASE_MAX] = #undef COMMA #undef EASINGFUNC + +// ================== +// FEATURE RESCALING +// ================== + +/*-------------------------------------------------- + fixed_t Rescale(fixed_t value, fixed_t inmin, fixed_t inmax, easingfunc_t easing_func, fixed_t outmin, fixed_t outmax) + + Rescales a feature value from [min, max] to [start, end] using + a custom easing function pointer. + + Input Arguments:- + value - The input value to rescale + inmin - Minimum value of the input range + inmax - Maximum value of the input range + easing_func - Pointer to the easing function to use + outmin - Start value of the output range + outmax - End value of the output range + + Return:- + The rescaled value using the specified easing function. +--------------------------------------------------*/ +fixed_t FixedRescale(fixed_t value, fixed_t inmin, fixed_t inmax, easingfunc_t easing_func, fixed_t outmin, fixed_t outmax) +{ + // Handle edge case where min == max + if (inmin == inmax) + return outmin; + + // Clamp the input value to the range + max(inmin, min(inmax, value)); + + // Normalize the value to [0, FRACUNIT] range + fixed_t t = FixedDiv(value - inmin, inmax - inmin); + + // Apply the easing function if provided + if (easing_func != NULL) + { + return easing_func(t, outmin, outmax); + } + + // Fallback to linear if no function provided + return Easing_Linear(t, outmin, outmax); +} + +/*-------------------------------------------------- + INT16 IntRescale(INT16 value, INT16 inmin, INT16 inmax, easingfunc_t easing_func, INT16 outmin, INT16 outmax) + + Rescales a feature value from [min, max] to [start, end] using + a custom easing function pointer. + Can only take in up to INT16 because it uses fixed_t internally + + Input Arguments:- + value - The input value to rescale + inmin - Minimum value of the input range + inmax - Maximum value of the input range + easing_func - Pointer to the easing function to use + outmin - Start value of the output range + outmax - End value of the output range + + Return:- + The rescaled value using the specified easing function. +--------------------------------------------------*/ +INT16 IntRescale(INT16 value, INT16 inmin, INT16 inmax, easingfunc_t easing_func, INT16 outmin, INT16 outmax) +{ + // Handle edge case where min == max + if (inmin == inmax) + return outmin; + + // Clamp the input value to the range + max(inmin, min(inmax, value)); + + // Conversion shit + value = value<>FRACBITS; + } + + // Fallback to linear if no function provided + return Easing_Linear(t, outmin, outmax)>>FRACBITS; +} diff --git a/src/m_easing.h b/src/m_easing.h index 3685ed192..bae0029c6 100644 --- a/src/m_easing.h +++ b/src/m_easing.h @@ -105,6 +105,9 @@ EASINGFUNC(InOutBackParameterized) /* Easing_InOutBackParameterized */ #undef EASINGFUNC +fixed_t FixedRescale(fixed_t value, fixed_t inmin, fixed_t inmax, easingfunc_t easing_func, fixed_t outmin, fixed_t outmax); +INT16 IntRescale(INT16 value, INT16 inmin, INT16 inmax, easingfunc_t easing_func, INT16 outmin, INT16 outmax); + #ifdef __cplusplus } // extern "C" #endif