User:SignpostMarv Martin/LSL2/random:location

From Second Life Wiki
Jump to: navigation, search
//**
//*     @package sw_slr
//*     @subpackage teleport
//*     @author SignpostMarv Martin
//*     @license Creative Commons BY-SA UK 2.0 http://creativecommons.org/licenses/by-sa/2.0/uk/
//*     @link http://blog.signpostmarv.name/2008/08/15/random-location-script/
//*     Example implementation of random:location service.
//*     Example script is intended to be used in a HUD.
//**
 
//**
//*     @param key Used for storing the key returned by llHTTPRequest()
//**
key random_location_http;
 
//**
//*     @param list Used to store the parsed data of the last succesful random location query
//**
list data;
 
//**
//*     @param integer UNIX timestamp of last successful HTTP request response
//**
integer timestamp;
 
//**
//*     @param string URL for reduced liklihood of coming across a fellow traveller
//**
string url_solo = "http://svc.sl.net.marvulous.co.uk/place/random:location/";
 
//**
//*     @param string URL for increased liklihood of coming across a fellow traveller
//**
string url_flashmob = "http://svc.sl.net.marvulous.co.uk/place/random:location:flashmob/";
 
//**
//*     @param list foo The list data to format
//*     @return string Returns a newline seperated string of the input list.
//**
string debug_message(list foo)
{
    return ("\n" + llDumpList2String(foo,"\n"));   
}
 
//**
//*     @param string region The name of the region to get a random location from. Optionaly empty to get from entire grid.
//*     @return key A key returned by llHTTPRequest()
//**
key random_location(string region)
{
    return llHTTPRequest(url_flashmob + llEscapeURL(region),[],"");
}
default
{
    state_entry()
    {
//**
//*     Fires off a HTTP request for a random location
//**
        random_location_http = random_location("");
    }
    http_response(key q,integer s,list m,string r)
    {
//**
//*     Checks if the http_response event is expected
//**
        if(q == random_location_http)
        {
//**
//*         Checks if the HTTP request was succesful
//**
            if(s == 200)
            {
//**
//*             Stores current UNIX timestamp in a global variable
//**            
                timestamp = llGetUnixTime();
//**
//*             Seperates the response body by \n and stores it in a global variable
//**
                data = llParseString2List(r,["\n"],[]);
                llOwnerSay("Touch me to go to \"" + llList2String(data,1) + "\" in " + llList2String(data,0));
            }
            else
            {
//**
//*             If the HTTP Status code was not 200, give debugging information
//**
                llOwnerSay(debug_message(["Error getting random location:",s,r]));
            }
        }
    }
//**
//* @uses llMapDestination to load the world map to the destination point
//**
    touch_start(integer touched)
    {
        llMapDestination(
            llList2String(data,0),
            <
                (float)llList2String(data,2),
                (float)llList2String(data,3),
                (float)llList2String(data,4)
            >,
            ZERO_VECTOR
        );
    }
//**
//* Starts a timer on teleport to reset the script
//**
    changed(integer change)
    {
        if(change & CHANGED_TELEPORT)
        {
            llSetTimerEvent(30);
        }
    }
    timer()
    {
        llResetScript();   
    }
}