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) {
}
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);
+}