Recipe 3.3: Owner Locked Door | Heaton Research

Recipe 3.3: Owner Locked Door

    It is easy enough to create a simple locking door. The door presented in this Recipe will only open for its owner. Of course, in Second Life, locked doors are not totally secure. There are ways to get around them, due to limitations in the Second Life client program.

    The easiest way to step around a locked door is to position the avatar right against it. Then press the cursor left or cursor right and rotate the avatar. When the correct angle reveals the inside of the building, find something to sit on and select it. The avatar will now be inside of the building!

    Despite these limitations, locked doors are still popular in Second Life. They keep casual or inexperienced users out. However, if a user is really determined to enter, they will be able to. The only way to keep a user out is to use the land tools and setup access controls. To do this, right click on the land and choose about. Then, under the access tab specify who may access this land.

    The owner locked door is presented in Listing 3.3.

Listing 3.3: Owner Locked Door (OwnerLockedDoor.lsl)

// From the book:
//
// Scripting Recipes for Second Life
// by Jeff Heaton (Encog Dod in SL)
// ISBN: 160439000X
// Copyright 2007 by Heaton Research, Inc.
//
// This script may be freely copied and modified so long as this header
// remains unmodified.
//
// For more information about this book visit the following web site:
//
// http://www.heatonresearch.com/articles/series/22/

float       TIMER_CLOSE = 5.0;      
integer     DIRECTION   = -1;       // direction door opens in. Either 1 (outwards) or -1 (inwards);

integer     DOOR_OPEN   = 1;
integer     DOOR_CLOSE  = 2;

vector      originalPos;      
                      
door(integer what) 
{
    rotation    rot;
    rotation    delta;
    vector eul;
    
    llSetTimerEvent(0); 
    
    if ( what == DOOR_OPEN ) 
    {
        llTriggerSound("doorOpen", 1);  
        eul = <0, 0, 90*DIRECTION>; //90 degrees around the z-axis, in Euler form 
           
    } else if ( what == DOOR_CLOSE) 
    {
        llTriggerSound("doorClose", 1);  
        eul = <0, 0, 90*-DIRECTION>; //90 degrees around the z-axis, in Euler form
    }
    
    eul *= DEG_TO_RAD; //convert to radians rotation
    rot = llGetRot();
    delta = llEuler2Rot(eul);
    rot = delta * rot;                  
    llSetRot(rot); 
}
        

default 
{   
    on_rez(integer start_param) 
    { 
        llResetScript();
    }
    
    state_entry() 
    {
        originalPos = llGetPos();     
    }
    
    touch_start(integer total_number) 
    {
        key who = llDetectedKey(0);
        if( who==llGetOwner() )
        {
            llSay(0,"Hello " + llDetectedName(0) );
            door(DOOR_OPEN);
            state open_state;
        }
        else
        {
            llSay(0,llDetectedName(0) + " is at the door." );
            llTriggerSound("doorbell", 0.8);
        }
    }
    
    moving_end() 
    {  
        originalPos = llGetPos();
    }
}

state open_state
{
    state_entry() 
    {
        llSetTimerEvent(TIMER_CLOSE);
    }
    
    touch_start(integer num) 
    {
        door(DOOR_CLOSE);
        llSetPos(originalPos);            
        state default;
    }
    
    timer() 
    { 
        door(DOOR_CLOSE);
        llSetPos(originalPos);            
        state default;
    }
    
    moving_start() 
    { 
        door(DOOR_CLOSE);
        state default;
    }
}

    A good part of this Recipe is the same as Recipe 3.2. Because of this, only the new code will be explained. This Recipe only explains the security aspects of the door. For a discussion on how the door opens, refer to Recipe 3.2.

    All security for the owner-locked door is implemented inside the touch_start event handler. The first thing that the touch_start event handler does is to obtain the key to the avatar that touched the door. This key must match the owner of the door. The owner is obtained by calling llGetOwner and the touching user is obtained by calling llDetectedKey. If the owner is the one who touched the door, begin the opening procedure.

touch_start(integer total_number) 
{
	key who = llDetectedKey(0);
     if( who==llGetOwner() )
     {
		llSay(0,"Hello " + llDetectedName(0) );
		door(DOOR_OPEN);
		state open_state;
	}

    If the owner is not the one opening the door, play a doorbell sound and say who is at the door.

	else
	{
		llSay(0,llDetectedName(0) + " is at the door." );
		llTriggerSound("doorbell", 0.8);
	}
}

    This door can only be opened by one person. To create a door that opens with a larger number of people, refer to the next Recipe.

Copyright 2005-2009 by Heaton Research, Inc.