PDA

View Full Version : Coding question



Paul Pratt
August 14th, 2016, 01:12
I am working on the Warhammer FRPG 2.0 set and need to capture a "doubles" result when the dice are rolled for some chat reports.

in the onRoll function I have been able to capture two dice and compare them for a doubles result. I have been able to isolate it so when only a single die is rolled its not looking to compare. Then I realized I need to compare 2-6 dice throws for any possible doubles.

How can I cycle thru the die and compare them to find matches?

Looked thru a few extensions, nothing popped, not sure of other games doing this. I tried tweaking some of the success counters I have come across to no avail. Any have an idea?

Trenloe
August 14th, 2016, 01:18
Have a look at the dropDiceResults function in this extension: https://www.fantasygrounds.com/forums/showthread.php?31425-Basic-success-counting-extension

It steps through each dice in the rRoll.aDice table. This is the "dice" table described in the "Message Data structure" here: https://www.fantasygrounds.com/refdoc/Comm.xcp

Hope that helps you get to grips with multiple dice in a roll (assuming that's what you're looking for).

Paul Pratt
August 14th, 2016, 08:25
Hello Trenloe, thanks for the reply. I had already looked through your count success extension. I can step through the dice, I can compare two dice and find a pair. I am stuck on how to compare more than two dice for the same result.

Here is the situation, I make a roll containing 5d10's. I need to capture if any of the 5 dice have the same result. If they do, I need to send a chat message. I have everything working fine with two dice, as soon as I go 3+ I have an issue. It boils down how can I compare the results and find matches.

damned
August 14th, 2016, 08:35
Is it always the same message or is it a different message if the pairs are 1s or 4s or 6s or if there are two pairs or three pairs etc?
Are the dice always 10s?

Could you take this?

for _,v in ipairs(rRoll.aDice) do
if v.result >= nSuccessLevel then
nSuccesses = nSuccesses + 1;
end
end


and instead count how many times a 10 is rolled
then count how many times a 9 is rolled
etc
and depending on your logic either stop walking thru it when you have nCount10 >= 1 or nCount9, or nCount8 etc or even keep counting the nCount etc

Paul Pratt
August 14th, 2016, 09:26
Damned,
Always d10's in this case.
The number rolled does not matter, I just need to find the first pair. So, in a 5d10 roll with results of 3, 4, 3, 7, 3 I just need to know that a single pair showed up. I could count up the individual results and compare. I was working through something similar when I wondered if there was a way to compare the die results directly.
Something similar to
if die1.result == die2.result or die3.result etc
I can get it to work with two dice, not 3+.

When we use rRoll.aDice[1] or rRoll.aDice[2] is the value in the [] the position in the table or a reference to the amount of dice being rolled? I think it is the latter.

the counting will work, it just seemed long-handed so to speak.

damned
August 14th, 2016, 09:40
Yes - its long handed - cos Im terrible at this stuff! I just try to find a way that works and its usually cumbersome... ;)

Valarian posted this snippet of code in another post where he references first die result and second die result... maybe that would help?


-- This file is part of the Fantasy Grounds Open Foundation Ruleset project.
-- For the latest information, see https://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.

function onInit()
ActionsManager.registerResultHandler("skilldice", handleSkillDice);
end

function handleSkillDice(rSource, rTarget, rRoll)
local reroll = false;
local total = {};

local firstResult = 0;
local secondResult = 0;
local dicetable = rRoll.aDice;
if dicetable then
for n = 1, table.maxn(dicetable) do
if dicetable[n].type == "d10" then
if firstResult > 0 and dicetable[n].result > secondResult then
secondResult = dicetable[n].result;
total[2] = dicetable[n];
elseif dicetable[n].result > firstResult then
firstResult = dicetable[n].result;
total[1] = dicetable[n];
end
end
end
end

if firstResult == 10 or secondResult == 10 then
repeat
local reroll = false;
local dieRoll = math.random(1,10);
local explode = {};
explode.type = "d10";
explode.result = dieRoll;
table.insert(total, explode);
-- Purely for effect - roll a d10
local dice = {};
table.insert(dice, "d10");
Comm.throwDice("explode", dice, 0, "explode");
if dieRoll == 10 then
reroll = true;
end
until not reroll
end

local rMessage = ChatManager.createBaseMessage(rSource, rRoll.sUser);
rMessage.type = rRoll.sType;
rMessage.text = rMessage.text .. rRoll.sDesc;
rMessage.dice = total;
rMessage.diemodifier = rRoll.nMod;

-- Check to see if this roll should be secret (GM or dice tower tag)
if rRoll.bSecret then
rMessage.secret = true;
if rRoll.bTower then
rMessage.icon = "dicetower_icon";
end
elseif User.isHost() and OptionsManager.isOption("REVL", "off") then
rMessage.secret = true;
end

-- Show total if option enabled
if OptionsManager.isOption("TOTL", "on") and total and #(total) > 0 then
rMessage.dicedisplay = 1;
end

Comm.deliverChatMessage(rMessage);
ModifierStack.reset();
return true;
end

Paul Pratt
August 14th, 2016, 17:28
Damned,
That's a great find, I think I can tweak that. What did you search to find that? I searched die, dice, doubles in the forums and never came across this one. Thanks!

Paul Pratt
August 14th, 2016, 18:59
I ended up with this:


local nRoll = table.maxn(rRoll.aDice);
if nRoll == 2 then
local nDie1 = rRoll.aDice[1].result;
local nDie2 = rRoll.aDice[2].result;
if nDie1 == nDie2 then
rMessage.text = rMessage.text .. "\r -->TZEENTCH's CURSE!<-- ";
else
rMessage.text = rMessage.text
end
elseif nRoll == 3 then
local nDie1 = rRoll.aDice[1].result;
local nDie2 = rRoll.aDice[2].result;
local nDie3 = rRoll.aDice[3].result;
if (nDie1 == nDie2) or (nDie1 == nDie3) or (nDie2 == nDie3) then
rMessage.text = rMessage.text .. "\r -->TZEENTCH's CURSE!<--";
else
rMessage.text = rMessage.text
end
end

I can extend that out as many dice as I need. It works, but seems rather long-handed. If anyone sees a better method, please, let me know. Thanks!

damned
August 14th, 2016, 22:31
Damned,
That's a great find, I think I can tweak that. What did you search to find that? I searched die, dice, doubles in the forums and never came across this one. Thanks!

Valarian proffered it up in another thread - I think one about Qin dice.

pindercarl
August 14th, 2016, 23:01
You'd think I'd know the LUA side of Fantasy Grounds better (but that's not really where I spend my time). The cleanest way to check might be to sort the table first. You can then just iterate through the table and compare each die roll with the next die in the sequence. If you find a match, you can bail on the loop.

https://www.lua.org/pil/19.3.html