Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Divide by zero issue with Accuracy/Difficulty roll system when input is zero

October 02 (6 years ago)

Edited October 02 (6 years ago)

Hi! I've exhausted my understanding of roll20's macros to make a macro for my players that performs attack rolls with player inputs. I am going to run a game of LANCER: Mech RPG which has a system where d6s are added to an attack roll based on a conditional value called Accuracy/Difficulty. It works like so:

1d20 + (x)d6kh1

If Player inputs positive integer X -> you roll Xd6, keep the highest roll, and add it to the operation

If Player inputs negative integer X -> you roll Xd6, keep the highest roll, and subtract it from the operation 

If Player inputs zero/default -> simple d20 roll

My macro works; when a player inputs a negative integer (Difficulty), it subtracts the appropriate d6 roll, and adds it for a positive integer (Accuracy). However, 0 does not work, because I think it is dividing by zero. Here's what I've got.

/gmroll 1d20 + [[[[(?{Accuracy|0}/abs(?{Accuracy}))*abs(?{Accuracy})]]d6kh1]]+@{Targeting} 

I found this page of useful macros that included a discussion of how to do conditional checks but for the life of me I don't understand how to integrate it. To do an (if X equals A output T, else output F) appears to work like

[[({0,floor(1-abs(x-A))}dl1)*(T-F) +F]]

But I don't understand how this works or how to integrate my current macro into this so that it doesn't perform the division operation if the ?{Accuracy} query returns 0.

I looked around the forums but didn't see this particular issue resolved. I understand the Accuracy/Difficulty system was taken from the RPG Shadow of the Demon Lord, so perhaps the issue is already resolved for that RPG although I haven't found anything related to that on roll20.

Thank you in advance for your help! Sorry if this has already been answered and I didn't search hard enough!

*Edited to clarify the intended output

October 02 (6 years ago)

Edited October 02 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

Why is your macro so complicated? You could do it much more simply and not have the problem with dividing by zero, which is definitely your issue.

Unless there's something I'm missing about your formula and/or the rules as you've outlined them, this will do what you want:

[[1d20+?{accuracy|0}d6kh1 + @{targeting}]]

It will of course error out if the user enters a non-integer, but I believe your current one would as well.

EDIT: Realized you were using gmroll and I had missed the targeting attribute, so just in case you need it:

/gmroll 1d20+?{accuracy|0}d6kh1 + @{targeting}
October 02 (6 years ago)

If you use the formula you suggest, the macro can't accept negative numbers (difficulty). Roll20 does not understand (-2)d6 as "roll 2d6 and then subtract the result from the operation". It thinks you are trying to roll a negative number of dice, which it cannot do, and does not perform the roll.

To circumvent this, I roll the absolute value of dice (-3 and 3 as Accuracy will both roll 3 dice) and then divide the variable by its absolute value to get 1 or -1 (making it positive or negative, in accordance with the variable value) and then multiply ty hat to the dice result to get the appropriate roll.

The only problem is if the input is zero the you are dividing zero by zero which is an illegal operation of course. I'm trying to figure out a way to allow players to input x, -x, or 0 without crashing.

I know they can just roll a d20 in that case but there are other stats involved that I want to include in the roll, regardless of accuracy/difficulty. 

October 02 (6 years ago)

Edited October 02 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

I tested my solution before providing it, but your post prompted me to go back and do some more in depth testing as well, here's the results (linked as forums won't accept my image drop atm).

The summary is that any pure dice roll with negative numbers works fine (e.g. -2d6, -4d100, 1d20 - 2d6). It stops working if you add special handling on to the roll. I only tested with the keep highest/lowest syntax, but if you try to just do -2d6kh1, it will always return -2 as it doesn't roll. However, you can solve this by putting a number before the -2d6kh1 so that it becomes something minus the 2d6kh1 rather than negative 2d6kh1. So, 1d20 -2d6kh1 does in fact work.

October 02 (6 years ago)

Edited October 02 (6 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator

And, the forums suddenly decide to accept my image drop (:sigh:). The 1d20+-5d6kh1 was done using the solution to your issue that I posted.

To see how this works in your own game, here's the code from the test displayed above:

&{template:default} {{name=negative rolls}} {{-1d20=[[-1d20]]}} {{1d20 -2d6=[[1d20-2d6]]}} {{0-2d6kh1=[[0-2d6kh1]]}} {{1d20+?{accuracy|0}d6kh1=[[1d20+?{accuracy|0}d6kh1]]}} {{Improper=**Rolls**}} {{-2d6kh1=[[-2d6kh1]]}}
October 02 (6 years ago)

Edited October 02 (6 years ago)


Scott C. said:

And, the forums suddenly decide to accept my image drop (:sigh:). The 1d20+-5d6kh1 was done using the solution to your issue that I posted.

To see how this works in your own game, here's the code from the test displayed above:

&{template:default} {{name=negative rolls}} {{-1d20=[[-1d20]]}} {{1d20 -2d6=[[1d20-2d6]]}} {{0-2d6kh1=[[0-2d6kh1]]}} {{1d20+?{accuracy|0}d6kh1=[[1d20+?{accuracy|0}d6kh1]]}} {{Improper=**Rolls**}} {{-2d6kh1=[[-2d6kh1]]}}

You're totally right. I must have had some sort of syntax error because when I first tried it with just a query it refused to work, it just output x. That must be it, because if you look on your table of results, the rolls that have the syntax "-xd20" aren't rolling, they are just returning "-x". (See your results for -1d20 and -2d6kh1). But when you write the negative roll as the second operand, it works, even the syntax 1d20+-5d6kh1.

I'm not sure what syntax error I was making but /roll 1d20+?{Accuracy|0}d6kh1+etc. works for positive, negative, and zero. Thank you very much for your help, I appreciate you taking the time!