//  OSCC_TouchMeObject script by Azwaldo Villota
/*
A basic "giver" object designed to ease set-up, for beginning scripters. Place script in a sign or poster and it reacts to Content changes.

Drag items that you want delivered on touch from your Inventory into "Content" tab (notecard, clothing, landmarks, etc.), or delete items from the "Content" tab, and the script commits those items for delivery later. This script can be modified easily in 4 ways...
*/
///////////// CUSTOM CONFIGURATION   ///////////
/*  1) Edit the "INFO" variable to contain the text that users will receive.*/
string      INFO = "This is the text that users will see.";
  
/*  2) Decide how INFO will be presented. By default, "INFO" text will be written to Public Chat. Change the "IM_ONLY" variable to "1" to present "INFO" as Instant Message*/
integer     IM_ONLY     = 0;

/* Change "DIALOG_ONLY" variable to "1" to present "INFO" in Dialog prompt. (Blue Dialog prompt window will simply have "OK" as button option)*/
integer     DIALOG_ONLY = 0;
        
/*  3) If more than one item is to be delivered, a folder will be create to contain all items. Unless the next variable is changed, the object's name will be used to create a folder in which to deliver the GIFTS.*/
string      FOLDER_NAME = "";//Leave blank to assign obj. name to GIFTS folder

/*  4) Decide whether the object's name should appear as floating text above the object itself. Assign 0 (zero) for no float text, 1 to display name. The color of that text. (For color vectors, see http://bit.ly/1whvn80) */
integer     DISPLAY_FLOATING_TEXT = 1;
vector      FLOAT_TEXT_COLOR = <1.0,1.0,1.0>;

///////////////////////    Beyond Here, Lay Dragons   ///////////////////////
///////////// VARIABLES       ///////////////
list        GIFTS;        //STORES THE NAMES OF ITEMS FOUND
integer     TOTAL_GIFTS;  //STORES THE NUMBER OF ITEMS FOUND
integer     EARS_OPEN;    //STORES LISTENER HANDLE FOR REMOVAL WHEN DONE
float       REASONABLE_DELAY = 30.0;//SECONDS WE'LL WAIT FOR DIALOG RESPONSE

///////////// FUNCTIONS       //////////////////
debug(string s) {llOwnerSay(s);}//debug("");
checkInventory() {
    //This fxn evaluates items in Contents for delivery later
    //CLEAR THE LIST TO START FRESH
    GIFTS = [];
    
    //STORE NAME OF SCRIPT TO AVOID DELIVERING IT LATER
    string thisScript = llGetScriptName( );
    integer n = llGetInventoryNumber(INVENTORY_ALL);
    
    //NUMBER OF ITEMS TO DELIVER IS TOTAL INVENTORY MINUS ONE (SCRIPT)
    TOTAL_GIFTS = n - 1;
    
    if (TOTAL_GIFTS) {
        integer i;
        string myName;
        //LOOP THRU ALL ITEMS, STORE NAME (IF NOT THIS SCRIPT!)
        for ( i = 0 ; i < n ; i++ ) {
            myName = llGetInventoryName(INVENTORY_ALL,i);
            if ( myName != thisScript ) {
                GIFTS += myName;
            }
        }
        //REPORT WHAT WAS FOUND TO OWNER FOR VERIFICATION
        llOwnerSay("GIFTS found in Contents:\n" + llDumpList2String(GIFTS,"\n"));
    } else {
        //OR, HOLLER IF NOTHING'S THERE...
        llOwnerSay("Nothing found in Contents.");
    }
}
presentInfo(key user){
    //This fxn presents "INFO" then delivers objects, UNLESS
    //set to DIALOG_ONLY when items are delivered in listen
    //event handler.
    
    //PRESENT THE INFO TEXT (ASSIGNED AT TOP OF SCRIPT)
    if (IM_ONLY) {
        deliverGifts(user);
        llInstantMessage(user,INFO);
    } else if (DIALOG_ONLY) {
        if (!EARS_OPEN) EARS_OPEN = llListen( -1, llKey2Name(user), NULL_KEY, "");
        llSetTimerEvent(REASONABLE_DELAY);
        llDialog( user, "\n" + INFO, ["OK"], -1 );
    } else {
        //NO PREFERENCE FOR INFO DELIVERY, WRITE TO PUBLIC CHAT
        deliverGifts(user);
        llSay(0,INFO);
    }
}
deliverGifts(key user){
    //NEXT, WE STORED A LIST OF ITEMS, AND THE NUMBER OF ITEMS...
    //FIRST SEE IF MULTIPLE ITEMS WERE FOUND
    if ( TOTAL_GIFTS > 1 ) {
        
        //...AND ASSIGN A NAME FOR THE FOLDER, IF MULTIPLE
        string myFolderName = FOLDER_NAME;
        if ( FOLDER_NAME == "" ) myFolderName = llGetObjectName();
        
        //...AND DELIVER THOSE ITEMS
        llGiveInventoryList( user, myFolderName, GIFTS );
        
    } else if ( TOTAL_GIFTS == 1 ) {
        //IF ONLY ONE ITEM WAS FOUND, NO FOLDER IS USED
        llGiveInventory( user, llList2String(GIFTS,0) );
    }
}
//  STATES          //////////////////////
default
{
    state_entry()
    {
        //WAS FLOATING TEXT ASSIGNED ABOVE?
        if (DISPLAY_FLOATING_TEXT) llSetText( llGetObjectName(), FLOAT_TEXT_COLOR, 1.0 );
        else llSetText( "", FLOAT_TEXT_COLOR, 1.0 );
        //SEE WHAT'S BEEN ADDED TO CONTENTS
        checkInventory();
    }
    on_rez(integer start_param) {
        checkInventory();
    }
    touch_start(integer total_number)
    {
        integer i;
        for ( i = 0; i < total_number ; i++ ) {
            //IS SCRIPT ALREADY TENDING TO ONE USER?
            presentInfo( llDetectedKey(i) );
        }
    }
    listen(integer channel, string name, key id, string message) {
        //ONLY LISTENING FOR ONE USER, LET'S CHECK IF ITEMS ARE TO
        //BE GIVEN, AND TEST FOR DIALOG BUTTON AT SAME TIME ANYWAY
        if (( message == "OK" ) && ( TOTAL_GIFTS )) {
            deliverGifts(id);
        }
    }
    changed(integer change) { // something has changed
        //IF A CHANGE TO INVENTORY (CONTENTS), SEE WHAT'S THERE
        if (change & CHANGED_INVENTORY) checkInventory();
    }
    timer() {
        llSay(0,"No response. Object will now reset.");
        llListenRemove(EARS_OPEN);
        EARS_OPEN = 0;
        llSetTimerEvent(0.0);
    }
}
//////////////////////////////////////////////////////////
//     RELEASED UNDER A CREATIVE COMMONS LICENSE      ////
//////////////////////////////////////////////////////////
//  This work is licensed under the CREATIVE COMMONS  ////
//  ATTRIBUTION 3.0 UNPORTED LICENSE. You are free    ////
//  to Share (to copy, distribute and transmit the    ////
//  work), to Remix (to adapt the work), and to make  ////
//  commercial use of the work                        ////
//////////////////////////////////////////////////////////
//  To view a copy of this license, visit             ////
//  http://creativecommons.org/licenses/by/3.0/       ////
//  or send a letter to                               ////
//      Creative Commons                              ////
//      171 Second Street, Suite 300,                 ////
//      San Francisco, California, 94105, USA.        ////
//////////////////////////////////////////////////////////