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:
#include "nw_i0_tool"
void signalThePixies(int iEvent);
void signalPixie(string pixieID, event evt);
void TeleportPCToArea(object oPC, string tagOfWPInArea);
void main()
{
signalThePixies(101);
object oPC = GetFirstPC();
while (GetIsObjectValid(oPC)) {
TeleportPCToArea(oPC, "WP_PIXIE_GLADE");
oPC = GetNextPC();
}
SignalEvent(OBJECT_SELF, EventUserDefined(101));
}
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) {
location currentLocation = GetLocation(oPC);
vector myVec = GetPositionFromLocation(currentLocation);
float myOrientation = GetFacingFromLocation(currentLocation);
object targetAreaWP = GetObjectByTag(tagOfWPInArea);
location wpLoc = GetLocation(targetAreaWP);
object targetArea = GetAreaFromLocation(wpLoc);
location newLoc = Location(targetArea, myVec, myOrientation);
AssignCommand(oPC,JumpToLocation(newLoc));
}
The user defined script for NPCs.
NWScript:
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)
{
}
else if(nUser == 1002)
{
}
else if(nUser == 1003)
{
}
else if(nUser == 1004)
{
}
else if(nUser == 1005)
{
}
else if(nUser == 1006)
{
}
else if(nUser == 1007)
{
}
else if(nUser == 1008)
{
}
else if (nUser == 101)
{
SpeakString("And this is our glade!");
TeleportToArea("WP_PIXIE_GLADE");
}
}
void TeleportToArea(string tagOfWPInArea) {
location currentLocation = GetLocation(OBJECT_SELF);
vector myVec = GetPositionFromLocation(currentLocation);
float myOrientation = GetFacingFromLocation(currentLocation);
object targetAreaWP = GetObjectByTag(tagOfWPInArea);
location wpLoc = GetLocation(targetAreaWP);
object targetArea = GetAreaFromLocation(wpLoc);
location newLoc = Location(targetArea, myVec, myOrientation);
JumpToLocation(newLoc);
}
Please let me know if there are any problems with these scripts. I hope they
help you.
|