-
February 6th, 2020, 22:32 #1
- Join Date
- Apr 2009
- Location
- Fullerton, CA.
- Posts
- 625
Question on dice tables and matching results
Hello,
While working on Warhammer 2e, I ran into an interesting issue that I eventually solved, but that solution seems to be a rather labor intensive and frankly novice way to handle things. I am a hobbyist, I have no real practical skill in coding and when iterating over tables I find my self in the deep end of the pool with no flotation device. I am hoping someone within the community can show me a pro way of handling this.
In Warhammer, players can make casting roll checks to cast spells. They have a Magic characteristic, and this governs how many d10's they throw when making the test. The practical amount of d10's that can be thrown would be 1d10 up to 7d10. So, I have 1-7d10's to handle. The roll itself is simple, throw the amount of d10's desired and beat a target number. That works as intended and I have no issues with. After throwing the dice, the results of the dice can trigger a further series of events called Manifestations. Basically think of it as magical "fumbles". The results of the casting roll are evaluated and any pairs, triples or quadruples, or combinations of those are tallied. They are labeled - pairs (Minor), triples (Major), quadruples (Catastrophic).
For instance, if someone rolls 4d10 and the results are {2,5,5,2} - that would mean 2 Minor (pairs) Manifestations result from the cast.
I need to take the table aDice.Result and find all the possible combinations of pairs, triple, quadruples, then report the amount of Minor, Major, and Catastrophic results.
I solved it by using this basic format and extending it out from 2 die to 7 dice and all the combinations of results, i.e. when rolling 4d10 I had to cover Catastrophic, Major, Minor, and two Minor. Obviously the more dice the more complicated this became, with some entries being hundreds of comparisons.
Three dice rolled and checking for a triple:
Code: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) and (nDie1 == nDie3) then if bArcane == "Arcane" then rMessage.text = rMessage.text .. "\r TZEENTCH's CURSE! \r Major Manifestation";
Just looking to learn something here, so any help is appreciated.Last edited by Paul Pratt; February 6th, 2020 at 22:41.
-
February 6th, 2020, 23:22 #2
Supreme Deity
- Join Date
- Mar 2007
- Posts
- 20,558
I'd probably do something like below. Please note that I wrote this code off the cuff, so not sure if it works correctly or has errors. It's just for concept.
Code:local aResults = {}; for _,vDie in ipairs(rRoll.aDice) do if vDie.result then if aResults[vDie.result] then aResults[vDie.result] = aResults[vDie.result] + 1; else aResults[vDie.result] = 1; end end end local nDoubles = 0; local nTriples = 0; local nQuadruplesOrMore = 0; for i = 1,10 do if aResults[i] and (aResults[i] > 1) then if aResults[i] == 2 then nDoubles = nDoubles + 1; elseif aResults[i] == 3 then nTriples = nTriples + 1; elseif nQuadruplesOrMore = nQuadruplesOrMore + 1; end end end ...Do the right thing based on double/triple/quadruple counts....
Regards,
JPG
-
February 7th, 2020, 05:10 #3
- Join Date
- Apr 2009
- Location
- Fullerton, CA.
- Posts
- 625
Moon,
Thank you. I had tried many different things trying to iterate over the table. I had the Lua manual and tutorials up and tried building something more than a few times. Every attempt failed.
As soon as I read the first 5 lines of your sample the fog cleared a bit. I never thought about setting up a new table -- aResults --- to hold the die results and count them. Then iterate over that new table to find the matches. Simple. Again thank you for the sample, it really helps me learn new/better techniques.
-
February 7th, 2020, 07:50 #4
- Join Date
- Apr 2009
- Location
- Fullerton, CA.
- Posts
- 625
BTW, I added that sample code above to the Warhammer 2e ruleset and fired it up... worked right out of the box. So, thanks again Moon!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks