一个解析和评估d&d启发的roll表达式的引擎
dndice的Python项目详细描述
滚动
这个包处理的是roll表达式,其灵感来自于d&d使用的语法。
最基本的是,有类似1d20
的表达式,意思是“滚动一个20边的模具”。
D&D在添加修饰符时停止,例如1d6+2
。
这个包将与它一起运行,并将这些骰子表达式引入到整个算术框架中。
你可以加,减,乘,甚至指数滚动在一起,更不用说所有的滚动特定的操作,如采取最高或最低滚动或重新滚动给定的条件。
由于这些都是数学表达式,就像普通表达式一样,请注意,它们可能会变得任意复杂。
唯一的限制是python可以为计算(9^9^9^9^9)d10000
或类似的荒谬表达式带来多少资源。
下面是支持哪些运算符以及它们所做操作的完整说明。
Operator | Format | Meaning |
---|---|---|
! | x! | Calculate the factorial of x. |
d | xdy | Take a y-sided die and roll x of them. y can be an integer, and works just as you would expect. It can also be a list of arbitrary numbers (delineated by ^{ |
da | xday | Take a y-sided die and return the average as if x of them had been rolled. This returns an unrounded number. |
dc | xdcy | Roll a critical hit, where the number of dice rolled is doubled. |
dm | xdmy | Roll the maximum on every die rolled. |
h | ROLLhn | After making a roll, discard all but the highest n of the rolls. Hint: 2d20h1 is advantage. |
l | ROLLln | After making a roll, discard all but the lowest n of the rolls. Hint: 2d20l1 is disadvantage. |
f | ROLLfn | After making a roll, treat any value that is less than n as n. |
c | ROLLcn | After making a roll, treat any value that is greater than n as n. |
r or ro | ROLLron | After making a roll, look at all of them and reroll any that are equal to n, reroll those, and take the result. |
R or Ro | ROLLRon | After making a roll, look at all of them and reroll any that are equal to n and reroll those. If that number comes up again, continue rerolling until you get something different. |
r> or rh | ROLLrhn | After making a roll, look at all of them and reroll any that are strictly greater than n, reroll those, and take the result. |
R> or Rh | ROLLRhn | After making a roll, look at all of them and reroll any that are greater than n and reroll those. If a number greater than n comes up again, continue rerolling until you get something different. |
r< or rl | ROLLrln | After making a roll, look at all of them and reroll any that are strictly less than n, reroll those, and take the result. |
R< or Rl | ROLLRln | After making a roll, look at all of them and reroll any that are less than n and reroll those. If a number less than n comes up again, continue rerolling until you get something different. |
t | ROLLtn | After making the roll, count the number of rolls that were at least n. |
T | ROLLTn | After making the roll, count the number of rolls that were at most n. |
^ | x^y | Raise x to the y power. This operation is right-associative, meaning that the right side of the expression is evaluated before the left. This really only comes up when chained, for example in ^{ |
* | x*y | x times y. |
/ | x/y | x divided by y. This returns an unrounded number. |
% | x%y | x modulo y. That is, the remainder after x is divided by y. |
+ | x+y | x plus y. |
- | x-y | x minus y. |
> or gt | x>y | Check if x is greater than y. Returns a 1 for yes and 0 for no. |
>= or ge | x>=y | Check if x is greater than or equal to y. Returns a 1 for yes and 0 for no. |
< or lt | x<y | Check if x is less than y. Returns a 1 for yes and 0 for no. |
<= or le | x<=y | Check if x is less than or equal to y. Returns a 1 for yes and 0 for no. |
= | x=y | Check if x is equal to y. Returns a 1 for yes and 0 for no. |
& | x&y | Check if x and y are both nonzero. |
| | x|y | Check if at least one of x or y is nonzero. |
使用此软件包
作为用户或玩家
从pypi安装这个包还将把脚本roll
安装到您的路径。这是一个简单的命令行脚本,允许您使用这个包的所有功能。
对于同样的gui,请查看我的存储库DnD,这是一个围绕d&d 5e的大型项目,允许您跟踪您的角色和怪物。
作为开发人员
由于根目录中的__init__.py
文件,整个存储库可以用作包。
这将在顶层公开包的所有有用内容,以便您可以在任何需要访问的位置克隆此存储库。
当然,您也可以像平常一样通过PyPI将其作为标准python包安装
安装poetry进行依赖项管理。没有运行时依赖项,唯一的开发依赖项是文档的sphinx和测试的nose2。