Author Sticky: Post Working Scripts Here
Jaks Posted: Thursday, 11 July 08:32AM
Script Name: GetNearestNPC

What it does: Will return an object representing the closest (starting at nNth) NPC or Creature to the calling object

Notes: Takes three parameters -
nGender: only get NPCs of a certain gender
GENDER_MALE
GENDER_FEMALE
GENDER_BOTH (Default)
nBasicRacesOnly: Only consider creatures of a playable race?
nNth: Return the nNth closest NPC

Contains one support function call IsBasicRace that defaults to return TRUE if it is one of the player races, but can be set to ignore Half Orcs.

NWScript:
//Function Prototypes

//Checks if oCreature is one of the playable races.

//  nRacist = TRUE means not to consider a HalfOrc a Basic Race

int IsBasicRace(object oCreature, int nRacist);

//Returns the nearest NPC to calling object

// -nGender: only get NPCs of a certain gender

//    GENDER_MALE

//    GENDER_FEMALE

//    GENDER_BOTH (Default)

// -nBasicRacesOnly: Only consider creatures of a playable race

// -nNth: Return the nNth closest NPC

object GetNearestNPC(int nGender = GENDER_BOTH, int nBasicRacesOnly = TRUE, int nNth=1);

//Functions

int IsBasicRace(object oCreature, int nRacist = FALSE){

    if(GetRacialType(oCreature) == RACIAL_TYPE_DWARF){

        //SpeakString("oCreature is a Dwarf");

        return TRUE;

    }

    if(GetRacialType(oCreature) == RACIAL_TYPE_ELF){

        //SpeakString("oCreature is an Elf");

        return TRUE;

    }

    if(GetRacialType(oCreature) == RACIAL_TYPE_GNOME){

        //SpeakString("oCreature is a Gnome");

        return TRUE;

    }

    if(GetRacialType(oCreature) == RACIAL_TYPE_HALFELF){

        //SpeakString("oCreature is a Half Elf");

        return TRUE;

    }

    if(GetRacialType(oCreature) == RACIAL_TYPE_HALFLING){

        //SpeakString("oCreature is a Halfling");

        return TRUE;

    }

    if(GetRacialType(oCreature) == RACIAL_TYPE_HUMAN){

        //SpeakString("oCreature is a Human");

        return TRUE;

    }

    if(GetRacialType(oCreature) == RACIAL_TYPE_HALFORC && nRacist == FALSE){

        //SpeakString("oCreature is a Half Orc");

        return TRUE;

    }

    return FALSE;

}

object GetNearestNPC(int nGender = GENDER_BOTH, int nBasicRacesOnly = TRUE, int nNth=1){

    int nFound = FALSE;

    object oCreature;

    while(nFound == FALSE){

        object oCreature = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_NOT_PC, OBJECT_SELF, nNth);

        //SpeakString("Checking out " + GetTag(oCreature));

        if(GetIsObjectValid(oCreature) && oCreature != OBJECT_SELF & !GetIsDM(oCreature)){

            if(GetRacialType(oCreature) == GetRacialType(OBJECT_SELF)){

                if(GetGender(oCreature) == nGender){

                    //SpeakString("oCreature is a Basic Race");

                    return oCreature;

                }

            }else{

                if(IsBasicRace(oCreature) && GetGender(oCreature) == nGender){

                    //SpeakString("oCreature is a Basic Race");

                    return oCreature;

                }

            }

        }

        nNth++;

    }

    //SpeakString("Found one!");

    if(nFound == TRUE){

        return oCreature;

    }else{

        return OBJECT_INVALID;

    }

}



I use this to cause NPCs to initiate conversations with other nearby NPCs. Since I do not want them wandering a mile just to talk with another NPC. It is also a bit racist in that the NPC will prefer another NPC of it's own race. Still some work left for that part but works as is.

_________________
----
Jaks
A mind so closed can only find life a small, sad cage