ID:195052
 
//Title: Decrement Until/Increment Until
//Credit to: Jtgibson
//Contributed by: Jtgibson


/*
Often in my code, I wind up writing a code construct which goes something
like this:

if(value - decrementor < 0)
value = 0
else
value -= decrementor

I realised that this was a common-enough procedure that I could write a
shorthand which didn't make excessive use of the min() and max() statements.

Note that you should use the proper procedure for the right circumstance. For
instance, if you will never be absolutely certain you will be modifying the
number by only a positive number, you should not use the increment_until()
proc as it is designed to constrain to an upper limit only.

See the Bound Constraint snippet if for a slightly more intensive alternative
that works with both positive and negative modifications. You would use:
value = CONSTRAIN(0, value+modifier, 100)
*/



proc/decrement_to_zero(value, decrement=1)
return ((value-decrement>0)*(value-decrement))


proc/decrement_until(value, decrement=1, minimum=0)
return ((value-decrement>minimum)*(value-decrement) + \
(value-decrement<=minimum)*minimum)

proc/increment_until(value, increment=1, maximum=100)
return ((value+increment<maximum)*(value+increment) + \
(value+increment>=maximum)*maximum)



///*
//Testing code/sample implementation:

mob
var/value = 50

verb/test_random_decrement()
//Read as "decrement 'value' by 'rand(1,5)' to zero"
// or "decrement to zero 'value' by 'rand(1,5)'"
value = decrement_to_zero(value, rand(1,5))
usr << value

verb/test_random_increment()
//Read as "increment 'value' by 'rand(1,5)' until it equals '100'"
// or "increment until 'value' plus 'rand(1,5)' equals '100'"
value = increment_until(value, rand(1,5), 100)
usr << value

verb/test_custom_decrement(decrement as num)
//Read as "decrement 'value' by 'decrement' to zero"
// or "decrement to zero 'value' by 'decrement'"
value = decrement_to_zero(value, decrement)
usr << value

verb/test_custom_increment(increment as num)
//Read as "increment 'value' by 'increment' until it equals '100'"
// or "increment until 'value' plus 'increment' equals '100'"
value = increment_until(value, increment, 100)
usr << value

//*/