Author Sticky: Post Working Scripts Here
rben Posted: Monday, 08 July 02:58PM
1. Name: TeleportToArea and TeleportPCToArea

2. Teleports NPC or PC to the same postion in a new area while preserving relative postion and facing. If the NPC or PC is in location (12,22) of one area, they will be teleported to location (12,22) of the other area. Relative position and facing are preserved. I developed this script to change areas when some pixies reveal themselves and drop the illusion that their forest glade is a farm.

3. There are actually a couple of scripts needed to make this effect work. I'll explain and present each one. Briefly, the scripts work as follows:
a) The TeleportToArea function is added to the user defined script of the NPCs that will use the script. I used signal 102 to trigger the teleport for NPCs.
b) The triggering script sends the appropriate user defined signal to the NPCs which is caught by their user defined scripts.
c) The TeleportToArea function is called with one argument, a string containing the tag name of a waypoint somewhere inside the target area. I used a waypoint that I put roughly in the center of the new area called "WP_PIXIE_GLADE", but the waypoint can really be anywhere in the area.
d) Next, the function grabs the current location of the NPC and extracts the position and facing information.
e) Then the function grabs the location of the waypoint and extracts the area information from it.
f) Subsequently, the function combines the area from the waypoint and the other position information from the current location to create a new location object, which is used as the target of the Jump.

For PCs, things are handled slightly differently. The same script that sends signals to the NPCs to teleport themselves handles teleporting the PC. I tried to get the PCs user defined script to do the teleporting, which would have allowed me to use the same function for all teleporting, but it wouldn't work. For that reason, the TeleportPCToArea function requires two arguments, the PC object and the tag of the waypoint.

4. The Scripts.

The triggering script:
NWScript:
//::///////////////////////////////////////////////

//:: FileName at_fay_generous

//:://////////////////////////////////////////////

//:://////////////////////////////////////////////

//:: Created By: Ray Benjamin

//:: Created On: 7/7/2002

//:://////////////////////////////////////////////

#include "nw_i0_tool"

void signalThePixies(int iEvent);

void signalPixie(string pixieID, event evt);

void TeleportPCToArea(object oPC, string tagOfWPInArea);

void main()

{

  // Tell the pixies to teleport to a new area

  signalThePixies(101);

  // Now teleport PCs

  object oPC = GetFirstPC();

  while (GetIsObjectValid(oPC)) {

    TeleportPCToArea(oPC, "WP_PIXIE_GLADE");

    oPC = GetNextPC();

  }

  // Finally, teleport this NPC

  SignalEvent(OBJECT_SELF, EventUserDefined(101));

}

// Send a signal to all the pixies

void signalThePixies(int iEvent) {

  event evt = EventUserDefined(iEvent);

  signalPixie("TEAK", evt);

  signalPixie("VIOLET", evt);

  signalPixie("PUPPY", evt);

  signalPixie("MOOCOW", evt);

  signalPixie("PECK", evt);

  signalPixie("SCRATCHER", evt);

  signalPixie("CHICK", evt);

}

void signalPixie(string pixieID, event evt) {

  object oPixie = GetObjectByTag(pixieID);

  if (GetIsObjectValid(oPixie)) {

    ActionDoCommand(SignalEvent(oPixie,evt));

  }

}

void TeleportPCToArea(object oPC, string tagOfWPInArea) {

  // grab current coordinates of object

  location currentLocation = GetLocation(oPC);

  vector myVec = GetPositionFromLocation(currentLocation);

  float myOrientation = GetFacingFromLocation(currentLocation);

  // get the area where we want to go

  object targetAreaWP = GetObjectByTag(tagOfWPInArea);

  location wpLoc = GetLocation(targetAreaWP);

  object targetArea = GetAreaFromLocation(wpLoc);

  location newLoc = Location(targetArea, myVec, myOrientation);

  // transfer to new area

  AssignCommand(oPC,JumpToLocation(newLoc));

}



The user defined script for NPCs.

NWScript:
//::///////////////////////////////////////////////

//:: Custom User Defined Event

//:: pixie_userdef

//:: Copyright (c) 2002 Ray Benjamin

//:://////////////////////////////////////////////

/*

  This is the user defined event script used for

  pixies in the module Brenton's Hold Gladiator Training Camp.

  This script handles the following events:

  101 - Teleporting to the new Pixie Forest Glade area.

*/

//:://////////////////////////////////////////////

//:: Created By: Ray Benjamin

//:: Created On: July 7, 2002

//:://////////////////////////////////////////////

void TeleportToArea(string tagOfWPInArea);

void main()

{

    int nUser = GetUserDefinedEventNumber();

    object oPC = GetFirstPC();

    string str = "Got user defined signal " + IntToString(nUser);

    SendMessageToPC(oPC, str);

    if(nUser == 1001) //HEARTBEAT

    {

    }

    else if(nUser == 1002) // PERCEIVE

    {

    }

    else if(nUser == 1003) // END OF COMBAT

    {

    }

    else if(nUser == 1004) // ON DIALOGUE

    {

    }

    else if(nUser == 1005) // ATTACKED

    {

    }

    else if(nUser == 1006) // DAMAGED

    {

    }

    else if(nUser == 1007) // DEATH

    {

    }

    else if(nUser == 1008) // DISTURBED

    {

    }

    else if (nUser == 101) // Teleport to new area

    {

      SpeakString("And this is our glade!");

      TeleportToArea("WP_PIXIE_GLADE");

    }

}

void TeleportToArea(string tagOfWPInArea) {

  // grab current coordinates of object

  location currentLocation = GetLocation(OBJECT_SELF);

  vector myVec = GetPositionFromLocation(currentLocation);

  float myOrientation = GetFacingFromLocation(currentLocation);

  // get the area where we want to go

  object targetAreaWP = GetObjectByTag(tagOfWPInArea);

  location wpLoc = GetLocation(targetAreaWP);

  object targetArea = GetAreaFromLocation(wpLoc);

  location newLoc = Location(targetArea, myVec, myOrientation);

  // transfer to new area

  JumpToLocation(newLoc);

}



Please let me know if there are any problems with these scripts. I hope they help you.