Random Perk Script Help

Post » Sat Oct 25, 2014 12:39 pm

Hey!

I'm hoping someone a bit more logical and script-savvy will be able to help me out with something. I know what I want to do, but don't know to go about it properly...

The gist is : My Mod adds a new 'Magic' which is inside the Player (plot wise). As time passes, the Magic grows stronger, and will periodically result in one of two events: Either the Player will gain a new Perk, or they will gain the next version of a Perk they already have. All the Perks are added by my Mod.

So for example, we have Perk A:1, Perk A:2, Perk B:1 and Perk B:2. The Player has Perk B:1. When the next update occurs, they can either gain Perk B:2, or Perk A:1.

Here's a rough code I put together, untested/checked, but it should explain what I'm trying to do:

If ( PerkUnlock >= 1 )  WhichOption = Utility.RandomInt(1, 100)  If ( WhichOption <= 30 )   WhichPerk = Utility.RandomInt(1, 100)   If ( WhichPerk <= 33 )    If ( PlayerRef.HasPerk(ZCDMDECloudGiftPerk01) == 0 )     PlayerRef.AddPerk(ZCDMDECloudGiftPerk01)     PerkUnlock = ( PerkUnlock - 1 )    EndIf   ElseIf ( WhichPerk <= 66 )    If ( PlayerRef.HasPerk(ZCDMDEFireGiftPerk01) == 0 )     PlayerRef.AddPerk(ZCDMDEFireGiftPerk01)     PerkUnlock = ( PerkUnlock - 1 )    EndIf   ElseIf ( WhichPerk <= 100 )    If ( PlayerRef.HasPerk(ZCDMDETimeGiftPerk01) == 0 )     PlayerRef.AddPerk(ZCDMDETimeGiftPerk01)     PerkUnlock = ( PerkUnlock - 1 )    EndIf   EndIf    ElseIf ( WhichOption > 30 )   If ( PlayerRef.HasPerk(ZCDMDECloudGiftPerk01) == 1 )    PlayerRef.AddPerk(ZCDMDECloudGiftPerk02)    PerkUnlock = ( PerkUnlock - 1 )   ElseIf ( PlayerRef.HasPerk(ZCDMDECloudGiftPerk02) == 1 )    PlayerRef.AddPerk(ZCDMDECloudGiftPerk03)    PerkUnlock = ( PerkUnlock - 1 )      ElseIf ( PlayerRef.HasPerk(ZCDMDECloudGiftPerk01) == 1 )    PlayerRef.AddPerk(ZCDMDECloudGiftPerk02)    PerkUnlock = ( PerkUnlock - 1 )   ElseIf ( PlayerRef.HasPerk(ZCDMDECloudGiftPerk02) == 1 )    PlayerRef.AddPerk(ZCDMDECloudGiftPerk03)    PerkUnlock = ( PerkUnlock - 1 )      ElseIf ( PlayerRef.HasPerk(ZCDMDETimeGiftPerk01) == 1 )    PlayerRef.AddPerk(ZCDMDETimeGiftPerk02)    PerkUnlock = ( PerkUnlock - 1 )   ElseIf ( PlayerRef.HasPerk(ZCDMDETimeGiftPerk02) == 1 )    PlayerRef.AddPerk(ZCDMDETimeGiftPerk03)    PerkUnlock = ( PerkUnlock - 1 )      Else    WhichOption = 1   EndIf   EndIfEndIf

What I'm having trouble with is randomising which Perk gets enhanced, and still keeping it able to check through every Perk the player might have. Fr now, I have it just go through in order, but that's not really suitable for what I want to happen in-game... If anyone could help, that would be great.

I would also like to maybe be able to 'weight' certain Perks, so that they have a higher chance of being enhanced over other Perks.

User avatar
kirsty williams
 
Posts: 3509
Joined: Sun Oct 08, 2006 5:56 am

Post » Sat Oct 25, 2014 9:15 am

What I would do, is make a formlist for each of your perks, put all the "levels" of that perk in the list. Make sure they're in order So, for instance:

Formlist01   Formlist02   Formlist03----------   ----------   ----------CoolPerk01   FunPerk01    PowerPerk01CoolPerk02   FunPerk02    PowerPerk02CoolPerk03   FunPerk03             FunPerk04

Then you make an array of Formlists, that holds all these lists. And you use a script like this to randomly select a perk from among your lists (untested, but should work)

Spoiler
Scriptname SelectRandomPerk extends WhateverYouWantToExtendActor property PlayerRef autoFormlist[] property PerkLists auto ;fill this list in the CK with all of the formlists you create for each perk;This is the function you will call to get a random new perk. Call from another function.;Syntax would be:;;    Perk randomPerk = PickRandomPerkToLearn();    PlayerRef.AddPerk(randomPerk);Perk Function PickRandomPerkToLearn()    Formlist[] TempPerkLists = CopyFormlistArray(PerkLists)    int perkCount = PerkLists.Length    while (perkCount > 0)        perkCount -= 1        int randomPick = Utility.RandomInt(0, perkCount)        Perk unknownPerk = GetFirstUnknownPerkInList(TempPerkLists[randomPick])        if (unknownPerk != NONE)            return unknownPerk ;return first unknown perk we find        else            ;remove the list we just checked from this array, then continue checking a new random list in this loop            RemoveListFromArray(TempPerkLists, randomPick)        endif    endWhile    return NONE ;this will only happen if the player knows every perk in all the listsEndFunction;Returns the first perk in the formlist that the player does not already knowPerk Function GetFirstUnknownPerkInList(Formlist perkList)    int i = 0    int max = perkList.GetSize()    while (i < max)        Perk thisPerk = perkList.GetAt(i) as Perk        if (PlayerRef.HasPerk(thisPerk) == false)            return thisPerk ;return unknown perk        endif        i += 1    endwhile    return NONE ;player knows all perks in this listEndFunction;This function shifts all elements of the array down one, starting at index;(thus it deletes the entry we just checked). It then returns the count of;formlists that are still left in the arrayFunction RemoveListFromArray(Formlist[] listArray, int index)    while (listArray[index] != none && index < 127)        listArray[index] = listArray[index + 1] ;shift all the elements down one slot (deletes the formlist we just checked)        index += 1    endWhileEndFunctionFormlist[] Function CopyFormlistArray(Formlist[] arrayToCopy)    Formlist[] copy = new Formlist[128]    int i = 0    int max = arrayToCopy.Length    while (i < max)        copy[i] = arrayToCopy[i]        i += 1    endWhile    return copyEndFunction 
User avatar
Maya Maya
 
Posts: 3511
Joined: Wed Jul 05, 2006 7:35 pm


Return to V - Skyrim