using UnityEngine;
using ORKFramework;
using ORKFramework.Formulas;
using System.Collections.Generic;
namespace ORKFramework.Formulas.Steps
{
[ORKEditorHelp("Roll Dice", "Roll dice and adjust the value", "")]
[ORKNodeInfo("Value")]
public class DiceStep : BaseFormulaStep
{
[ORKEditorHelp("Operator", "The operator decides how this formula step is calculated to the current value of the formula:\n" +
"- Add: Adds this step's result to the current value of the formula.\n" +
"- Sub: Subtracts this step's result from the current value of the formula.\n" +
"- Multiply: Multiplies the current value of the formula with this step's value.\n" +
"- Divide: Divides the current value of the formula by this step's value.\n" +
"- Modulo: Uses the modulo operator, current value of the formula % this step's value.\n" +
"- Power Of: The current formula value to the power of this step's value.\n" +
"- Log: The current formula value is used in a logarithmic calculation with this step's value as base.\n" +
"- Set: Sets the current formula value to this step's result.", "")]
public FormulaOperator formulaOperator = FormulaOperator.Set;
[ORKEditorInfo(separator = true, labelText = "Number of Dice")]
public FormulaFloat numDice = new FormulaFloat();
[ORKEditorInfo(separator = true, labelText = "Dice Type")]
public FormulaFloat diceType = new FormulaFloat();
public DiceStep()
{
}
public override int Calculate(FormulaCall call)
{
int result = 0;
for (int x = 0; x < (int)numDice.GetValue(call); x++)
{
// add one because exclusive max
result += Random.Range(1, (int)(diceType.GetValue(call)+ 1));
}
ValueHelper.UseOperator(ref call.result, result, formulaOperator);
return this.next;
}
public override string GetNodeDetails()
{
return this.formulaOperator.ToString() + " " + this.numDice.GetInfoText() + "d" + this.diceType.GetInfoText();
}
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!
If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
You can also invert this to simply run in a loop until the formula value itself reaches '0', subtracting '1' from the value every time it loops and calculating dice rolls as an additive variable change, then either set the final value of your formula to that of your dice variable or simply do that in whichever formula or event step calls the dice dice formula (****REMEMBER TO ALWAYS RESET YOUR VARIABLE'S BACK TO A NEUTRAL VALUE****).
At the start of my formula list I've got a string of such formulas for d2, d3, d4, d6, d8, d10 & d20, all marked as the same type and it's made my life exponentially simpler.
If you're enjoying my products, updates and support, please consider supporting me on patreon.com!