PDA

View Full Version : New NPC Random Code...


mByte
11-30-2004, 07:11 PM
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

uint32 SelectSpawn(int minRand, int maxRand);


add the SelectSpawn method
spawngroup.cpp

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

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;
}