PDA

View Full Version : [Programming] Random Initiative



pr6i6e6st
September 12th, 2019, 16:44
Hey guys! so for my Alien RPG ruleset, I almost have a random initiative button working in the combat tracker; I'm just not sure how to get it to be actually random.

The way initiative works in the ARPG is you have 10 cards, everyone draws a card, some NPC's draw more than one (this can be simulated by dropping the entity in the combat tracker twice i suppose). So unlike with dice, we can't have repeating initiatives.

Is there a way to perhaps rig this to Trenloe's Basic Card Extension? or how might i set it up so that a number can't be used again?

this is what i've put into the CombatManager. nInitNum is set up that way just so i could see if it worked, i'm just not sure how to do the randomization with elimination of results.



function rollInit()
nInitNum = 0;
for _,v in pairs(getCombatantNodes()) do
nInitNum = nInitNum + 1;
DB.setValue(v, "initresult", "number", nInitNum);
end
end

Trenloe
September 12th, 2019, 16:59
Look at using a LUA table - with the 10 card values (assuming it's something like 1,2, 3, 4 ... 10?), then "shuffle" that LUA table and step through each entry in order assigning the value as the initiative.

Info on "shuffling" a table here: https://docs.coronalabs.com/tutorial/data/shuffleTable/index.html

pr6i6e6st
September 13th, 2019, 03:24
Look at using a LUA table - with the 10 card values (assuming it's something like 1,2, 3, 4 ... 10?), then "shuffle" that LUA table and step through each entry in order assigning the value as the initiative.

Info on "shuffling" a table here: https://docs.coronalabs.com/tutorial/data/shuffleTable/index.html


ok, so i've been messing around for a little while here. the math.randomseed( (os.time) ) bit is giving me errors that it can't call global math, and if i remove that completely, i instead get errors that it's failing to get global "type". It looks simple, but i'm clearly missing something.
edit: got it!



function shuffleTable( t )
if ( type(t) ~= "table" ) then
print( "WARNING: shuffleTable() function expects a table" )
return false
end

local j

for i = #t, 2, -1 do
j = math.random( i )
t[i], t[j] = t[j], t[i]
end
return t
end



currentIndex = 1 -- Deck starts at top card (1)

function drawCards( num, deck )

local cardsDrawn = {} -- Empty table to begin

for i = currentIndex, num do
cardsDrawn[#cardsDrawn+1] = deck[i]
end

currentIndex = currentIndex + num
return cardsDrawn
end


function rollInit()
cardDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
cardDeck = shuffleTable( cardDeck )
currentIndex = 1 -- Deck starts at top card (1)
for _,v in pairs(getCombatantNodes()) do
drawCards( 1, cardDeck );
DB.setValue(v, "initresult", "number", cardDeck[currentIndex]);
end
end

function rollInitFifteen()
cardDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
cardDeck = shuffleTable( cardDeck )
currentIndex = 1 -- Deck starts at top card (1)
for _,v in pairs(getCombatantNodes()) do
drawCards( 1, cardDeck );
DB.setValue(v, "initresult", "number", cardDeck[currentIndex]);
end
end

function rollInitTwenty()
cardDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
cardDeck = shuffleTable( cardDeck )
currentIndex = 1 -- Deck starts at top card (1)
for _,v in pairs(getCombatantNodes()) do
drawCards( 1, cardDeck );
DB.setValue(v, "initresult", "number", cardDeck[currentIndex]);
end
end


edit: this almost works!


local cardDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
function shuffleTable( t )
if ( type(t) ~= "table" ) then
print( "WARNING: shuffleTable() function expects a table" )
return false
end

local j

for i = #t, 2, -1 do
j = math.random( i )
t[i], t[j] = t[j], t[i]
end
return t
end



local currentIndex = 1 -- Deck starts at top card (1)

function drawCards( num, deck )

local cardsDrawn = {} -- Empty table to begin

for i = currentIndex, num do
cardsDrawn[#cardsDrawn+1] = deck[i]
end

currentIndex = currentIndex + num
return cardsDrawn
end


function rollInit()
cardDeck = shuffleTable( cardDeck )
for _,v in pairs(getCombatantNodes()) do
local playerHand = drawCards( 1, cardDeck );
DB.setValue(v, "initresult", "number", dardsDrawn);
end
end


this is what i have:
i have tried removing the quotation marks from around the numbers as well, but the code isn't getting far enough to know if that makes a difference.


local cardDeck = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }
function shuffleTable( t )
if ( type(t) ~= "table" ) then
print( "WARNING: shuffleTable() function expects a table" )
return false
end

local j

for i = #t, 2, -1 do
j = math.random( i )
t[i], t[j] = t[j], t[i]
end
return t
end

cardDeck = shuffleTable( cardDeck )

local currentIndex = 1 -- Deck starts at top card (1)

function drawCards( num, deck )

local cardsDrawn = {} -- Empty table to begin

for i = currentIndex, num do
cardsDrawn[#cardsDrawn+1] = deck[i]
end

currentIndex = currentIndex + num
return cardsDrawn
end


function rollInit()
for _,v in pairs(getCombatantNodes()) do
local playerHand = drawCards( 1, cardDeck );
DB.setValue(v, "initresult", "number", playerHand);
end
end

Trenloe
September 13th, 2019, 14:35
edit: got it!
So, is everything OK or do you still have issues?

You're also missing a lot of semi-colon line terminators in that code!

pr6i6e6st
September 13th, 2019, 14:46
It seems like it’s working fine. Only concern is something I’m not sure we can do, giving npcs multiple initiatives.

And shoot, is that a problem? I didn’t realize I missed those

Trenloe
September 13th, 2019, 14:53
And shoot, is that a problem? I didn’t realize I missed those
If it's working 100% correctly probably not. I'm not sure what the FG LUA interpreter expects and if it will process all code correctly without semi-colons.

Trenloe
September 13th, 2019, 14:54
Only concern is something I’m not sure we can do, giving npcs multiple initiatives.
Does multiple initiatives for NPCs mean they go twice? Or that they select the best?

pr6i6e6st
September 13th, 2019, 14:59
Does multiple initiatives for NPCs mean they go twice? Or that they select the best?

They get to go a number of times in a round based on their speed. So a xenomorph with a speed of two draws two initiative cards, 5 and 8, he gets to act on turn 5 and 8.

Reminds me of lair/legendary actions in d&d which I don’t think there’s anything programmed specifically for

Trenloe
September 13th, 2019, 15:05
There's nothing programmed for that in CoreRPG. Maybe consider "spawning" a dummy NPC record in the CT - either manually or via code, that is essentially a placeholder for the NPC's second turn in the round, but this would mess with auto targeting and effects.

pr6i6e6st
September 13th, 2019, 15:10
There's nothing programmed for that in CoreRPG. Maybe consider "spawning" a dummy NPC record in the CT - either manually or via code, that is essentially a placeholder for the NPC's second turn in the round, but this would mess with auto targeting and effects.

Right, I think I’ll just leave that alone. If d&D5e ruleset gets something similar I might try ripping it apart, but like you said, probably just going to screw things up, easier just to add a dummy in game.