ID:1373325
 
Keep your numbers within the given bounds.
proc/clamp(n, low, high)
return min(max(n, low), high)

// or
#define clamp(n, low, high) min(max(n, low), high)


This is to shorten this kind of code:
if(n > high)
n = high
else if(n < low)
n = low

// shortened into
n = clamp(n, low, high)


Example:
mob
var max_health = 100
var health = 100

proc/TakeDamage(damage)
// We don't want negative health, and
// we don't want health to be over max_health
health = clamp(health - damage, 0, max_health)

if(!health)
Die()
Slightly more on-topic, I have a library that abstracts a lot of this and provides some nice functions (like getting percentages, checking the value in various formats, etc).