From: Martin Taibr Date: Thu, 10 Aug 2017 22:02:21 +0000 (+0200) Subject: map_ranges() X-Git-Tag: xonotic-v0.8.5~2430^2~35 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=5f964d1c1c47777cf4b703f9b2f4061fbb329423;p=xonotic%2Fxonotic-data.pk3dir.git map_ranges() --- diff --git a/qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc b/qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc index fbb6bcb75..53042b8ef 100644 --- a/qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc +++ b/qcsrc/common/mutators/mutator/damagetext/cl_damagetext.qc @@ -158,13 +158,9 @@ CLASS(DamageText, Object) if (this.text) strunzone(this.text); this.text = strzone(s); - float size_range = autocvar_cl_damagetext_size_max - autocvar_cl_damagetext_size_min; - float damage_range = autocvar_cl_damagetext_size_max_damage - autocvar_cl_damagetext_size_min_damage; - float scale_factor = size_range / damage_range; - this.m_size = bound( - autocvar_cl_damagetext_size_min, - (potential - autocvar_cl_damagetext_size_min_damage) * scale_factor + autocvar_cl_damagetext_size_min, - autocvar_cl_damagetext_size_max); + this.m_size = map_bound_ranges(potential, + autocvar_cl_damagetext_size_min_damage, autocvar_cl_damagetext_size_max_damage, + autocvar_cl_damagetext_size_min, autocvar_cl_damagetext_size_max); } CONSTRUCTOR(DamageText, int _group, vector _origin, bool _screen_coords, int _health, int _armor, int _potential_damage, int _deathtype, bool _friendlyfire) { diff --git a/qcsrc/lib/math.qh b/qcsrc/lib/math.qh index 8ba31516d..85ae496e3 100644 --- a/qcsrc/lib/math.qh +++ b/qcsrc/lib/math.qh @@ -322,3 +322,22 @@ vector solve_quadratic(float a, float b, float c) } return v; } + +/// Maps values between the src and dest range: src_min to dest_min, src_max to dest_max, values between them +/// to the curresponding values between and extrapolates for values outside the range. +/// +/// Max can be lower than min if you want to invert the range, values can be negative. +ERASEABLE +float map_ranges(float value, float src_min, float src_max, float dest_min, float dest_max) { + float velocity_diff = src_max - src_min; + float force_diff = dest_max - dest_min; + float ratio = (value - src_min) / velocity_diff; + return dest_min + force_diff * ratio; +} + +/// Same as `map_ranges` except that values outside the source range are clamped to min or max. +ERASEABLE +float map_bound_ranges(float value, float src_min, float src_max, float dest_min, float dest_max) { + value = bound(src_min, value, src_max); + return map_ranges(value, src_min, src_max, dest_min, dest_max); +}