I had a free day yesterday. First one I have had in months, and so... I spent sometime tinkering with NWN again. One thing I tried was to generate a random location inside a trigger, and I failed. Here's my function:
location GetRandomLocationInSubArea(object oSubArea, object oOrigin, float fMaxDistance)
{
object oArea = GetArea(oSubArea);
if(DEBUG)
SendMessageToPC(GetFirstPC(),"oSubArea("+GetName(oSubArea)+") oOrigin("+GetName(oOrigin)+") fDist("+FloatToString(fMaxDistance)+")");
location lRandomLocation;
int nIt;
object oTemp;
while(nIt<=5)
{
lRandomLocation = GetRandomLocation(oArea,oOrigin,fMaxDistance);
oTemp = CreateObject(OBJECT_TYPE_CREATURE,"invisible",lRandomLocation);
if(GetIsInSubArea(oTemp,oSubArea))
{
if(DEBUG)
SendMessageToPC(GetFirstPC(),"Successful Random Location in SubArea oTemp("+ObjectToString(oTemp)+")");
DestroyObject(oTemp,0.1);
break;
}
else
{
if(DEBUG)
SendMessageToPC(GetFirstPC(),"Unsuccessful Random Location in SubArea oTemp("+ObjectToString(oTemp)+")");
DestroyObject(oTemp,0.1);
}
++nIt;
}
return lRandomLocation;
}
Some context: the trigger did not have any scripts in its events. the creature being created has no AI scripts.
My guess is you can't test whether a creature is in a subarea immediately after it is created, but since my time is short these days I didn't create tests to figure out the limitations.
What I was doing was creating farmers who work a field at random after completing predetermined tasks in the field. Rather than select random points for them inside the field, I decided to have them head back to the field once they wandered out of it.