Elin:Code Analysis/General Function

Version EA23.115: This article is at least 20 minor version or a major version behind the latest stable release of Elin. Please help update the page.


CURVE function

 
input(Orange)vs. Output(Navy)です。Output = Curve(Input, 50, 25, 75)

CURVE function is an unique function used in attenuating values in graduation.

The CURVE is defined in EClass.cs as follow.

public static int curve(int a, int start, int step, int rate = 75)
{
	if (a <= start)
	{
		return a;
	}
	for (int i = 0; i < 10; i++)
	{
		int num = start + i * step;
		if (a > num)
		{
			a = num + (a - num) * rate / 100;
			continue;
		}
		return a;
	}
	return a;
}

"a" is the input.(input )

"start" is the starting point of the attenuation. (starting point ) If the input is smaller than or equal to the "start", CURVE function just sends back inout as output.

"rate" indicates the rate (in %) at which the input gets attenuated. (rate ) EXceeded fraction is multiplied with rate / 100.

"step" is an interval. step length everytime the input exceeds line defined by step, the input gets attenuted one more time.

Bellow is a mathematically-equivalet rephrasing of the coding.

0. Set index of iteration (number of iteration ) to 1.

1. Calculates the fraction that will be multiplied by the rate. (attenuation subject)

Attenuation subject = input - starting point

2. if Attenuation subject is <= 0, then return Attenuation subject as output.

3. Applies attenuation.

Attenuated value = Attenuation subject × rate / 100

4. Checks whether Attenuated value needs big enough for further attenuation; The following number is returned as output if the value is <= step length or the number of iteration == 10 and the loop is over.

Output = starting point + Attenuated value + step length× (number of attenuation - 1)

5. Re-defines the Attenuation subject.

Attenuation subject = Attenuation subject - step length
The number of iteration increases by 1.

6. Loop 3. to 5.

The slope of the function gradually decreases and finally reaches (rate / 100)^10

Dice Roll

Template:EA 23.109 Stable Patch 2

public class Dice
{
   public static Dice Null = new Dice();


   public int num;

This is the number of the dices.


   public int sides;

This is the sides of each dice.


   public int bonus;

This is a constant that is added to the ressult of roll.

   public Card card;

Here starts the definition of the Roll()

   public static int Roll(int num, int sides, int bonus = 0, Card card = null)
   {
       int a = 1;
       bool flag = true;
       int num2 = 0;
       if (card != null)
       {
           int num3 = card.Evalue(78);
           flag = num3 >= 0;
           a = 1 + Mathf.Abs(num3 / 100) + ((Mathf.Abs(num3 % 100) > rnd(100)) ? 1 : 0);
       }

Evalue(78) is luck, flag is used to show luck is greater than 0.

"a" is an integer that indicates how many tries the dices are thrown.

"a" is approximately |luck| / 100, but "a" increases by 1 at the chance of rest of |luck| / 100


       for (int i = 0; i < Mathf.Min(a, 20); i++)
       {
           int num4 = Roll();
           if (i == 0 || (flag && num4 > num2) || (!flag && num4 < num2))
           {
               num2 = num4;
           }
       }
       return num2;

This part generates the results of dice rolls and records the best/worst result depending on flag.

There are "a"es of results (up to 20) and the final result of the whole Roll() process is the best result of this process if the luck is positive.

For a character with negative luck, the result will be the worst one among all the results.


       int Roll()
       {
           int num5 = 0;
           for (int j = 0; j < num; j++)
           {
               num5 += rnd(sides) + 1;
           }
           return num5 + bonus;
       }
   }

This is just a dice roll simulatiion, but bonus being added to the roll is an important point.

   public static int RollMax(int num, int sides, int bonus = 0)
   {
       return num * sides + bonus;
   }

This is the possible best ressult of roll + bonus as well.

   public static int rnd(int a)
   {
       return Rand.Range(0, a);
   }

This function emits a random number that is { 0, 1,..., a }.