STAR TREK 2d20
  1. #1

    [Programming] Random Initiative

    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.

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

  2. #2
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,361
    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...ble/index.html
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  3. #3
    Quote Originally Posted by Trenloe View Post
    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...ble/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!

    Code:
    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!
    Code:
    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.
    Code:
    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
    Last edited by pr6i6e6st; September 13th, 2019 at 05:37.

  4. #4
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,361
    Quote Originally Posted by pr6i6e6st View Post
    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!
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  5. #5
    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

  6. #6
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,361
    Quote Originally Posted by pr6i6e6st View Post
    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.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  7. #7
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,361
    Quote Originally Posted by pr6i6e6st View Post
    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?
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  8. #8
    Quote Originally Posted by Trenloe View Post
    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

  9. #9
    Trenloe's Avatar
    Join Date
    May 2011
    Location
    Colorado, USA
    Posts
    33,361
    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.
    Private Messages: My inbox is forever filling up with PMs. Please don't send me PMs unless they are actually private/personal messages. General FG questions should be asked in the forums - don't be afraid, the FG community don't bite and you're giving everyone the chance to respond and learn!

  10. #10
    Quote Originally Posted by Trenloe View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
DICE PACKS BUNDLE

Log in

Log in