"Fairmath System": SugarCube (v2.18)

Summary

"Fairmath System" demonstrates how to re-create the Fairmath system found in ChoiceScript. Based on a percentage operation, increase and decrease changes the value by a percentage as the difference between the original and adjusted value.

This example uses the <<widget>> macro in SugarCube to separate operations for increasing and decreasing.

Twee Code

:: StoryTitle
Fairmath in SugarCube

:: Start
<!-- Fairmath formulas based on http://choicescriptdev.wikia.com/wiki/Arithmetic_operators#Fairmath -->

<<set \$valueToAdjust to 100>>
The inital value is \$valueToAdjust

<!-- Call the decrease widget -->
The adjusted value is \$resultValue.

<!-- Save the changed value -->
<<set \$valueToAdjust to \$resultValue>>
<!-- Call the increase widget -->
The adjusted value is \$resultValue.

:: Fairmath Operations[widget]
<<widget "increase">>
<<set \$resultValue to Math.round(\$args+((100-\$args)*(\$args/100))) >>
<</widget>>

<<widget "decrease">>
<<set \$resultValue to Math.round(\$args-(\$args*(\$args/100) )) >>
<</widget>>