ChoiceScript Wiki
No edit summary
m (→‎Fairmath Subtraction: Minor correction.)
Tags: Visual edit apiedit
 
(20 intermediate revisions by 6 users not shown)
Line 1: Line 1:
  +
{{tutorial}}
There are several arithmetic operators you can use as a ChoiceScript programmer. For each example below, I'm going to use a variable named "var" with a value of 50. The basic ones:
 
  +
This article is aimed at beginners to ChoiceScript. However, if you have not already done so then the best place to start would be with the very first article in this '''''Introduction To ChoiceScript Game Development''''' series of linked articles: [http://choicescriptdev.wikia.com/wiki/A_Basic_Tutorial A Basic Tutorial].
   
  +
Manipulation of numbers is a fundamental part of all programming and scripting. This article will discuss the correct format by which we can apply these mathematical methods to both previously stored and new data.
'''Addition:'''
 
  +
  +
There are several arithmetic operators you can use as a ChoiceScript game developer.
  +
  +
==Basic math operations==
  +
Let's say we have a ''numeric'' variable called ''var'' with a current ''value'' of '''50''', to which we apply any one of the following operations:
  +
 
===Addition===
 
*set var + 5
 
*set var + 5
  +
A var with a value of 50 would now have a value of 55.
Var is now equal to 55.
 
  +
  +
The current value of one variable can also be added to another:
 
*set var1 + var2
  +
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 55. The value of var2 remains unchanged.
   
  +
===Subtraction===
'''Substraction:'''
 
 
*set var - 5
 
*set var - 5
  +
A var with a value of 50 would now have a value of 45.
Var is now equal to 45.
 
   
  +
The current value of one variable can also be subtracted from another:
'''Multiplication:'''
 
 
*set var1 - var2
  +
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 45. The value of var2 remains unchanged.
  +
 
===Multiplication===
 
*set var * 5
 
*set var * 5
Var is now equal to 250.
+
A var with a value of 50 would now have a value of 250.
  +
  +
The current value of one variable can also be multiplied by another:
 
*set var1 * var2
  +
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 250. The value of var2 remains unchanged.
   
'''Division:'''
+
===Division===
 
*set var / 5
 
*set var / 5
  +
A var with a value of 50 would now have a value of 10.
Var is now equal to 10.
 
   
  +
The current value of one variable can also be divided by another:
Then the more advanced ones:
 
  +
*set var1 / var2
  +
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 10. The value of var2 remains unchanged.
   
'''Fairmath:'''
+
==Fairmath==
  +
Fairmath is a percentile operator and only works on numeric values in the range of 0-100. Attempting to apply a Fairmath  change to any value currently outside this range will result in a game-stopping error. It is used as follows:
   
 
*set var %+ 20
This operator is a little different than the others; it can be used on variables that are percentages only (from 0 to 100). Fairmath is of two types, "%+" and "%-".
 
   
  +
To increase the current value of a numeric variable by 20%; or,
They are used this way:
 
*set var %+ 20
 
Var is now equal to 60.
 
*set var %- 20
 
Var is now equal to 40.
 
   
  +
*set var %- 20
The idea of fairmath is that the closer a variable is to 100, the harder it is to increase, and the closer a variable is to 0, the harder it is to decrease:
 
   
  +
To decrease the current value of a numeric variable by 20%.
''Fair Addition: ''(x %+ y) = (x + (100-x)*(y/100))
 
   
  +
Anywhere between 5% (a relatively small change) and 30% (very significant) is common in Choice Games, with higher values usually reserved for rare or extreme circumstances.
*High variables are harder to increase: (90 %+ 20) = (92 + 2) = 92
 
   
  +
===Fairmath Addition===
*Low variables are easier to increase: (10 %+ 20) = (10 + 18) = 28
 
   
  +
The formula Fairmath uses for increasing a value -
''Fair Substraction: ''(x %- y) = (x - x*(y/100))
 
   
  +
round(x+((100-x)*(y/100)))
*High variables are easier to decrease: (90 %- 20) = 90 - 18) = 72
 
   
  +
- ensures three things:
*Low variables are harder to decrese: (10 %- 20) = (10 - 2 = 8
 
  +
* The resulting value will never be higher than 99, regardless of the percentage change applied.
  +
* The closer the variable value currently is to 100, the less it will increase.
  +
* The closer the variable value currently is to 0, the greater it will increase.
   
  +
<nowiki> </nowiki>As an example, let's assume the following current values - [[File:Fairmath1.JPG|none|thumb|300x300px|Strength 10, Intelligence 50, Dexterity 90]]
50 is equally easy to increase or decrease, as you've seen above.
 
  +
- and apply a 20% Fairmath ''increase'' to each of these (e.g. *set strength %+ 20). The end result is:
  +
[[File:Fairmath2.JPG|none|thumb|300x300px|Strength 28, Intelligence 60, Dexterity 92]]
  +
Essentially, when increasing a value with %+, Fairmath uses the ''difference'' between the value itself and 100.
   
  +
For instance, the difference between Strength (10) and 100 is 90, so Fairmath added 20% of 90 (18) to Strength, giving us a resulting value of (10+18) = '''28''' - a big jump.
'''Modulo:'''
 
   
  +
The difference between Intelligence (50) and 100 is 50, so Fairmath added 20% of 50 (10) to Intelligence, giving us a resulting value of (50+10) = '''60''' - a moderate increase.
This operator is indeed a little weird, but it is useful in two main ways; you can check if a number is evenly divisible to another by writing:
 
X % Y = 0
 
so that if we do:
 
var % 10 = 0
 
it is true, because 50 / 10 = 5, but if we do:
 
var % 40 = 0
 
it is false, because 50 / 40 = 1.25.
 
   
  +
The difference between Dexterity (90) and 100 is only 10, so Fairmath added 20% of 10 (2) to Dexterity, giving us a resulting value of (90+2) = '''92''' - a very small rise.
You can also get the fractional part of a number, so that if we do this:
 
*set var / 40
 
the result will be 1.25; then if we do:
 
var % 1
 
the result will be 0.25.
 
   
  +
===Fairmath Subtraction===
'''Round()'''
 
  +
  +
The formula fairmath uses for decreasing a value -
  +
  +
round(x-(x*(y/100)))
  +
  +
- also ensures three things:
  +
  +
* The resulting value will never be lower than 1, regardless of the percentage change applied.
  +
* The closer the variable value currently is to 0, the less it will decrease.
  +
* The closer the variable value currently is to 100, the greater it will decrease.
  +
  +
As an example, let's once again assume the following current values -
  +
[[File:Fairmath1.JPG|none|thumb|300x300px|Strength 10, Intelligence 50, Dexterity 90]]
  +
- and instead apply a 20% ''decrease'' to each of these (e.g. *set strength %- 20). The end result is:
  +
[[File:Fairmath3.JPG|none|thumb|300x300px|Strength 8, Intelligence 40, Dexterity 72]]
  +
Unlike for increasing a value, where Fairmath uses the difference between the value and 100 to determine the degree of change, when ''decreasing'' a value Fairmath instead uses the value itself.
  +
  +
20% of Strength (10) is only 2, giving us a resulting value of (10-2)=  '''8''' - a very small drop.
  +
  +
20% of Intelligence (50) is 10, giving us a resulting value of (50-10)= '''40''' - a moderate fall.
  +
  +
20% of Dexterity (90) is 18, giving us a resulting value of (90-18)= '''72''' - a significant drop.
  +
  +
===Fairmath Notes===
  +
  +
Bear in mind that the Fairmath formulae will never result in a value above 99 or below 1, no matter what percentage change you try to apply. Likewise, it's equally important to remember that Fairmath can only be applied to a value currently within the range of 0-100, otherwise it will cause a game-stopping error.
  +
  +
The difficulty in working out possible results on the fly while coding in ChoiceScript prompted CoG community member @Twiger_Fluff to devise an [https://scratch.mit.edu/projects/168459345/ Online Fairmath Calculator].
  +
  +
Together with fellow member @Eleckar they also contrived to provide ChoiceScript writers with a ''Google Sheets''-based [https://docs.google.com/spreadsheets/d/1VxI6kj68RW5ipuA0ydkURWrcl-OCzawoUQxhXJ13mw4/edit#gid=0 Fairmath Multiple Calculator]. With this tool we can track Fairmath stat increases or decreases through successive changes (say, along a particular story path, or for a particular character build) to also see the final current value, allowing us to more easily tweak and balance stats throughout.
  +
  +
Users of the ''Choicescript Integrated Development Environment'' ([https://choicescriptide.github.io/ CSIDE]) - see our [[Development Tools]] article for further details - should note that they can also use that application's powerful ''Console'' feature as (among many practical uses) a handy Fairmath calculator.
  +
 
==Round()==
   
 
The *round() is used for setting a variable to the nearest integer, as following:
 
The *round() is used for setting a variable to the nearest integer, as following:
 
*set var round(30.5)
 
*set var round(30.5)
this will automatically set "var" to 31.
+
this will automatically set ''var'' to 31.
  +
 
You can also use it with variables instead of numbers, as following:
  +
*set var1 round(var2)
 
where ''var2'' is equal to 20.5; ''var1'' will be equal to 21.
  +
  +
Remember, never make any spaces between "round" and the "(var1)" brackets.
  +
 
==Modulo==
  +
  +
The modulo is used to find the remainder when one variable is divided by another. It is used by simply typing the word "modulo" between the first variable (the dividend or numerator), and the variable you want to divide by (divisor or denominator). It is a fairly obscure and weird command, but useful in two main ways. First, you can check if a number is evenly divisible to another by writing:
  +
*if (var1 modulo var2) = 0
  +
For example, we can try:
  +
*if (50 modulo 10) = 0
 
which is true, because 50 divided by 10 leaves no remainder, but if we try:
  +
*if (50 modulo 40) = 0
 
it is false, because 50 divided by 40 leaves 10 as a remainder.
  +
 
Secondly, we can also get the fractional part of a number, so that if we do this:
  +
*set var 50
  +
<nowiki>*</nowiki>set var / 40
 
the result will be 1.25; then if we do:
  +
*set var modulo 1
 
the result will be 0.25.
   
  +
Note that the above example rewrites the variable ''var'' when the module is used. The quick way around this is to create a second variable, set it to equal the first, then use the modulo. So if we have var1 set to 50, and var2 set to 40 and want to see what the remainder if we divide var1 by var2, we'll need something like this:
You can use it with variables instead of numbers, as following:
 
  +
*temp var3
*set var round (var1)
 
  +
<nowiki>*set var3 var1
where "var1" is equal to 20.5; "var" will be equal to 21.
 
  +
*</nowiki>set var3 modulo var2
  +
This will leave us with the var1 and var2 intact, and additionally var3 with a value of 10 (the remainder of 50 divided by 40).
   
  +
'''N.B.''' When used with an *if, the modulo requires parentheses. However, when used with a *set, it requires that there be no parentheses.
   
  +
Note: As of October 2016, the operator for a modulo operation was changed from the percent sign (%) to the word "modulo". The change was made to avoid confusion with the more commonly used Fairmath operations that also use the percent sign.<ref>Dan Fabulich, [https://forum.choiceofgames.com/t/choicescripts-modulo-operator-is-changing-from-to-modulo/21176 ChoiceScript’s modulo operator is changing from “%” to “modulo”], October 14, 2016.</ref>
[[Index|Back to the index.]]
 
   
  +
'''Next Tutorial Article:''' [[Concatenation]]
'''Related Articles:'''
 
   
  +
== Related articles ==
*[[Data types|Data Types]]
+
*[[Data types]]
 
*[[Variable types]]
 
*[[Set|*set]]
   
  +
== References ==
*[[Variable types|Variable Types]]
 
  +
<references />
*[[Set|set]]
 
  +
[[Category:Tutorials]]
  +
[[Category:Commands]]

Latest revision as of 14:29, 18 July 2017

      This article is aimed at beginners to ChoiceScript. However, if you have not already done so then the best place to start would be with the very first article in this Introduction To ChoiceScript Game Development series of linked articles: A Basic Tutorial.

Manipulation of numbers is a fundamental part of all programming and scripting. This article will discuss the correct format by which we can apply these mathematical methods to both previously stored and new data.

There are several arithmetic operators you can use as a ChoiceScript game developer.

Basic math operations

Let's say we have a numeric variable called var with a current value of 50, to which we apply any one of the following operations:

Addition

*set var + 5

A var with a value of 50 would now have a value of 55.

The current value of one variable can also be added to another:

*set var1 + var2

If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 55. The value of var2 remains unchanged.

Subtraction

*set var - 5

A var with a value of 50 would now have a value of 45.

The current value of one variable can also be subtracted from another:

*set var1 - var2

If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 45. The value of var2 remains unchanged.

Multiplication

*set var * 5

A var with a value of 50 would now have a value of 250.

The current value of one variable can also be multiplied by another:

*set var1 * var2

If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 250. The value of var2 remains unchanged.

Division

*set var / 5

A var with a value of 50 would now have a value of 10.

The current value of one variable can also be divided by another:

*set var1 / var2

If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 10. The value of var2 remains unchanged.

Fairmath

Fairmath is a percentile operator and only works on numeric values in the range of 0-100. Attempting to apply a Fairmath  change to any value currently outside this range will result in a game-stopping error. It is used as follows:

 *set var %+ 20

To increase the current value of a numeric variable by 20%; or,

 *set var %- 20

To decrease the current value of a numeric variable by 20%.

Anywhere between 5% (a relatively small change) and 30% (very significant) is common in Choice Games, with higher values usually reserved for rare or extreme circumstances.

Fairmath Addition

The formula Fairmath uses for increasing a value -

 round(x+((100-x)*(y/100)))

- ensures three things:

  • The resulting value will never be higher than 99, regardless of the percentage change applied.
  • The closer the variable value currently is to 100, the less it will increase.
  • The closer the variable value currently is to 0, the greater it will increase.

As an example, let's assume the following current values -

Fairmath1

Strength 10, Intelligence 50, Dexterity 90

- and apply a 20% Fairmath increase to each of these (e.g. *set strength %+ 20). The end result is:

Fairmath2

Strength 28, Intelligence 60, Dexterity 92

Essentially, when increasing a value with %+, Fairmath uses the difference between the value itself and 100.

For instance, the difference between Strength (10) and 100 is 90, so Fairmath added 20% of 90 (18) to Strength, giving us a resulting value of (10+18) = 28 - a big jump.

The difference between Intelligence (50) and 100 is 50, so Fairmath added 20% of 50 (10) to Intelligence, giving us a resulting value of (50+10) = 60 - a moderate increase.

The difference between Dexterity (90) and 100 is only 10, so Fairmath added 20% of 10 (2) to Dexterity, giving us a resulting value of (90+2) = 92 - a very small rise.

Fairmath Subtraction

The formula fairmath uses for decreasing a value -

 round(x-(x*(y/100)))

- also ensures three things:

  • The resulting value will never be lower than 1, regardless of the percentage change applied.
  • The closer the variable value currently is to 0, the less it will decrease.
  • The closer the variable value currently is to 100, the greater it will decrease.

As an example, let's once again assume the following current values -

Fairmath1

Strength 10, Intelligence 50, Dexterity 90

- and instead apply a 20% decrease to each of these (e.g. *set strength %- 20). The end result is:

Fairmath3

Strength 8, Intelligence 40, Dexterity 72

Unlike for increasing a value, where Fairmath uses the difference between the value and 100 to determine the degree of change, when decreasing a value Fairmath instead uses the value itself.

20% of Strength (10) is only 2, giving us a resulting value of (10-2)=  8 - a very small drop.

20% of Intelligence (50) is 10, giving us a resulting value of (50-10)= 40 - a moderate fall.

20% of Dexterity (90) is 18, giving us a resulting value of (90-18)= 72 - a significant drop.

Fairmath Notes

Bear in mind that the Fairmath formulae will never result in a value above 99 or below 1, no matter what percentage change you try to apply. Likewise, it's equally important to remember that Fairmath can only be applied to a value currently within the range of 0-100, otherwise it will cause a game-stopping error.

The difficulty in working out possible results on the fly while coding in ChoiceScript prompted CoG community member @Twiger_Fluff to devise an Online Fairmath Calculator.

Together with fellow member @Eleckar they also contrived to provide ChoiceScript writers with a Google Sheets-based Fairmath Multiple Calculator. With this tool we can track Fairmath stat increases or decreases through successive changes (say, along a particular story path, or for a particular character build) to also see the final current value, allowing us to more easily tweak and balance stats throughout.

Users of the Choicescript Integrated Development Environment (CSIDE) - see our Development Tools article for further details - should note that they can also use that application's powerful Console feature as (among many practical uses) a handy Fairmath calculator.

Round()

The *round() is used for setting a variable to the nearest integer, as following:

*set var round(30.5)

this will automatically set var to 31.

You can also use it with variables instead of numbers, as following:

*set var1 round(var2)

where var2 is equal to 20.5; var1 will be equal to 21.

Remember, never make any spaces between "round" and the "(var1)" brackets.

Modulo

The modulo is used to find the remainder when one variable is divided by another. It is used by simply typing the word "modulo" between the first variable (the dividend or numerator), and the variable you want to divide by (divisor or denominator). It is a fairly obscure and weird command, but useful in two main ways. First, you can check if a number is evenly divisible to another by writing:

*if (var1 modulo var2) = 0

For example, we can try:

*if (50 modulo 10) = 0

which is true, because 50 divided by 10 leaves no remainder, but if we try:

*if (50 modulo 40) = 0

it is false, because 50 divided by 40 leaves 10 as a remainder.

Secondly, we can also get the fractional part of a number, so that if we do this:

*set var 50
*set var / 40

the result will be 1.25; then if we do:

*set var modulo 1

the result will be 0.25.

Note that the above example rewrites the variable var when the module is used. The quick way around this is to create a second variable, set it to equal the first, then use the modulo. So if we have var1 set to 50, and var2 set to 40 and want to see what the remainder if we divide var1 by var2, we'll need something like this:

*temp var3
*set var3 var1
 *set var3 modulo var2

This will leave us with the var1 and var2 intact, and additionally var3 with a value of 10 (the remainder of 50 divided by 40).

N.B. When used with an *if, the modulo requires parentheses. However, when used with a *set, it requires that there be no parentheses.

Note: As of October 2016, the operator for a modulo operation was changed from the percent sign (%) to the word "modulo". The change was made to avoid confusion with the more commonly used Fairmath operations that also use the percent sign.[1]

Next Tutorial Article: Concatenation

Related articles

References