Tribe::Memory* Tribe::FindRandomMemory(const char* name, const csVector3 &pos, const iSector* sector, float range, float* foundRange) { csArray<Memory*> nearby; csArray<float> dist; float minRange = range*range; // Working with Squared values if(range == -1) minRange = -1; // -1*-1 = 1, will use -1 later csList<Memory*>::Iterator it(memories); while(it.HasNext()) { Memory* memory = it.Next(); if(memory->name == name && memory->npc == NULL) { float dist2 = npcclient->GetWorld()->Distance(pos,sector,memory->pos,memory->GetSector()); if(minRange < 0 || dist2 < minRange) { nearby.Push(memory); dist.Push(dist2); } } } if(nearby.GetSize()>0) // found one or more closer than range { size_t pick = psGetRandom((uint32)nearby.GetSize()); if(foundRange) *foundRange = sqrt(dist[pick]); return nearby[pick]; } return NULL; }
Tribe::Memory* Tribe::FindNearestMemory(const char* name, const csVector3 &pos, const iSector* sector, float range, float* foundRange) { Memory* nearest = NULL; float minRange = range*range; // Working with Squared values if(range == -1) minRange = -1; // -1*-1 = 1, will use -1 later csList<Memory*>::Iterator it(memories); while(it.HasNext()) { Memory* memory = it.Next(); if(memory->name == name && memory->npc == NULL) { float dist2 = npcclient->GetWorld()->Distance(pos,sector,memory->pos,memory->GetSector()); if(minRange < 0 || dist2 < minRange) { minRange = dist2; nearest = memory; } } } if(nearest && foundRange) // found closest one { *foundRange = sqrt(minRange); } return nearest; }
Tribe::Memory* Tribe::FindMemory(csString name,const csVector3 &pos, iSector* sector, float radius) { csList<Memory*>::Iterator it(memories); while(it.HasNext()) { Memory* memory = it.Next(); if(memory->name == name && memory->GetSector() == sector && memory->npc == NULL) { float dist = (memory->pos - pos).Norm(); if(dist <= radius) { return memory; } } } return NULL; // Found nothing }
void Tribe::ShareMemories(NPC* npc) { csList<Memory*>::Iterator it(memories); while(it.HasNext()) { Memory* memory = it.Next(); if(memory->npc == npc) { if(FindMemory(memory->name,memory->pos,memory->GetSector(),memory->radius)) { // Tribe know this so delete the memory. memories.Delete(it); delete memory; } else { memory->npc = NULL; // Remove private indicator. SaveMemory(memory); } } } }