Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fate die support #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ These are the atoms used at the base of the syntax tree.
| Name | Syntax | Description | Examples |
|---------|-----------------------------------|-----------------------|--------------------------------|
| literal | `INT`, `DECIMAL` | A literal number. | `1`, `0.5`, `3.14` |
| dice | `INT? "d" (INT \| "%")` | A set of die. | `d20`, `3d6` |
| dice | `INT? "d" (INT \| "%" \| "F" \| "f")` | A set of die. | `d20`, `3d6` |
| set | `"(" (num ("," num)* ","?)? ")"` | A set of expressions. | `()`, `(2,)`, `(1, 3+3, 1d20)` |

Note that `(3d6)` is equivalent to `3d6`, but `(3d6,)` is the set containing the one element `3d6`.
Expand Down
2 changes: 2 additions & 0 deletions d20/diceast.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ def __init__(self, num, size):
self.num = int(num)
if str(size) == "%":
self.size = str(size)
elif str(size) == "F" or str(size) == "f":
self.size = "F"
else:
self.size = int(size)

Expand Down
16 changes: 15 additions & 1 deletion d20/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ def __repr__(self):
return f"<Literal {self.number}>"


class FateLiteral(Literal):
"""A literal for Fate dice"""

def __repr__(self):
if self.number == -1:
return "<FateLiteral ->"
elif self.number == 0:
return "<FateLiteral 0>"
else:
return "<FateLiteral +>"


class UnOp(Number):
"""Represents a unary operation."""

Expand Down Expand Up @@ -416,12 +428,14 @@ def children(self):
return []

def _add_roll(self):
if self.size != "%" and self.size < 1:
if self.size != "%" and self.size != "F" and self.size < 1:
raise errors.RollValueError("Cannot roll a 0-sided die.")
if self._context:
self._context.count_roll()
if self.size == "%":
n = Literal(random.randrange(10) * 10)
if self.size == "F":
n = FateLiteral(random.choice([-1, 0, 1])) # Technically it's -/0/+, but this also works with 4dF
else:
n = Literal(random.randrange(self.size) + 1) # 200ns faster than randint(1, self._size)
self.values.append(n)
Expand Down
2 changes: 1 addition & 1 deletion d20/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ DICE_OPERATOR: "rr" | "ro" | "ra" | "e" | "mi" | "ma"

diceexpr: INTEGER? "d" DICE_VALUE

DICE_VALUE: INTEGER | "%"
DICE_VALUE: INTEGER | "%" | "F" | "f"

selector: [SELTYPE] INTEGER

Expand Down
4 changes: 4 additions & 0 deletions tests/test_dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
STANDARD_EXPRESSIONS = [
"1d20",
"1d%",
"1dF",
"1df"
"1+1",
"4d6kh3",
"(1)",
Expand All @@ -13,6 +15,7 @@
"4*(3d8kh2+9[fire]+(9d2e2+3[cold])/2)",
"(1d4, 2+2, 3d6kl1)kh1",
"((10d6kh5)kl2)kh1",
"17dFkh3"
]


Expand Down Expand Up @@ -46,6 +49,7 @@ def test_sane_totals():
assert 1 <= r("(((1d6)))") <= 6
assert 4 <= r("(1d4, 2+2, 3d6kl1)kh1") <= 6
assert 1 <= r("((10d6kh5)kl2)kh1") <= 6
assert -1 <= r("1dF") <= 1


def test_pemdas():
Expand Down