EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Development::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=590)
-   -   New NPC Random Code... (https://www.eqemulator.org/forums/showthread.php?t=17212)

mByte 11-30-2004 07:11 PM

New NPC Random Code...
 
Noticed it wasnt very random so I added some fixes, tested it out looked pretty random if you ask me. :)

add this line to the private member of SpawnGroup
spawngroup.h
Code:

uint32 SelectSpawn(int minRand, int maxRand);
add the SelectSpawn method
spawngroup.cpp
Code:

uint32 SpawnGroup::SelectSpawn(int minRand, int maxRand)
{

        if(minRand > maxRand)
        {
                if(maxRand < 10)
                        maxRand = 11;

                minRand = (rand()%maxRand);
        }

        int currentPick[1];
        int npcId = 0;

        currentPick[0] = 0; //npcId
        currentPick[1] = 1; //npcChance

        LinkedListIterator<SpawnEntry*> iterator(list_);
        iterator.Reset();
        while(iterator.MoreElements())
        {

                //cout << iterator.GetData()->NPCType << " " << iterator.GetData()->chance << " " << minRand << " " << maxRand << "\n";
                //First NPC is always the current pick
                if(currentPick[0] == 0){
                        currentPick[0] = iterator.GetData()->NPCType;
                        currentPick[1] = iterator.GetData()->chance;
                }
                else{
                //Now check for best spawn matching random!
                        if((iterator.GetData()->chance >= minRand)){
                                        if(currentPick[1] > maxRand)
                                        {
                                                currentPick[0] = iterator.GetData()->NPCType;
                                                currentPick[1] = iterator.GetData()->chance;
                                        }
                                        else if(iterator.GetData()->chance > currentPick[1])
                                        {
                                                currentPick[0] = iterator.GetData()->NPCType;
                                                currentPick[1] = iterator.GetData()->chance;
                                        }
                          }
                }
                iterator.Advance();
        }

        npcId = currentPick[0];
        return npcId;
}

Replace the GetNPCType() method
spawngroup.cpp
Code:

uint32 SpawnGroup::GetNPCType() {
#if EQDEBUG >= 10
        LogFile->write(EQEMuLog::Debug, "SpawnGroup[%08x]::GetNPCType()", (int32) this);
#endif

        int npcType = -1;
        int minChance = (rand() % 100) % 50;
        int maxChance = (rand() % 100);

        //Check for 100% Chance
        LinkedListIterator<SpawnEntry*> iterator(list_);
        iterator.Reset();
        while(iterator.MoreElements()) {
                if(iterator.GetData()->chance == 100)
                {
                        //cout << "Found a spawn that has 100% chance\n";
                        npcType = iterator.GetData()->NPCType;
                        break;
                }
                iterator.Advance();
        }

        //No 100% found - Find npc to load.
        if(npcType == -1)
        {
                //cout << "Looking for a good spawn!\n";
                npcType = SelectSpawn(minChance, maxChance);
        }

        return npcType;
}



All times are GMT -4. The time now is 03:31 AM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.