Untitled

mail@pastecode.io avatar
unknown
lsl
a year ago
6.8 kB
20
Indexable
Never
integer dialog_channel;
integer listen_handle;
list messages;
string notecard_name = "ingestion_strings";
integer current_line = 0;
integer deleteFlag = FALSE;
key notecard_query_id;
integer current_step = 0;
integer number_of_steps = 0;
string finish_message;
string halt_message;
integer message_mode = 0; // 0 for reading ingestion lines, 1 for reading finish and halt messages
string holdingAnim = "drink_hold";
string drinkingAnim = "drink_ingest";
float drinkingAnimDuration = 7.0; // Set this to the duration of your drinking animation

default
{
    state_entry()
    {
        dialog_channel = (integer)("0x" + llGetSubString((string)llGetKey(), -7, -1));
        notecard_query_id = llGetNotecardLine(notecard_name, current_line);
    }

    dataserver(key query_id, string data)
    {
        if(query_id == notecard_query_id)
        {
            if(data != EOF)
            {
                string trimmed_data = llStringTrim(data, STRING_TRIM); // Trim white spaces at the beginning and end of the line

                if(trimmed_data == "")
                {
                    // This line is empty, so ignore it
                }
                else if(llGetSubString(trimmed_data, 0, 0) == "#")
                {
                    // This line is a comment, so ignore it
                }
                else if(trimmed_data == "===")
                {
                    message_mode = 1; // switch to reading finish and halt messages
                }
                else
                {
                    if(message_mode == 0)
                    {
                        messages += trimmed_data; // append the line to the messages list
                        number_of_steps++; // increase the number of steps
                    }
                    else if(message_mode == 1)
                    {
                        finish_message = trimmed_data;
                        message_mode = 2;
                    }
                    else if(message_mode == 2)
                    {
                        halt_message = trimmed_data;
                    }
                }
                current_line++;
                notecard_query_id = llGetNotecardLine(notecard_name, current_line); // get the next line
            }
            else
            {
                llOwnerSay("Finished reading notecard. Number of steps is: " + (string)number_of_steps);
            }
        }
    }

    attach(key id)
    {
        if(id)
        {
            llRequestPermissions(id, PERMISSION_ATTACH | PERMISSION_TRIGGER_ANIMATION); // Request permissions
            llStartAnimation(holdingAnim); // Start the holding animation
            llListen(dialog_channel, "", id, "");
            llInstantMessage(id, (string)llList2String(messages, current_step));
            llDialog(id, "Would you like to continue drinking? " + (string)(current_step+1) + "/" + (string)number_of_steps, ["Continue", "Stop"], dialog_channel);
        }
        else
        {
            llListenRemove(dialog_channel);
            llStopAnimation(holdingAnim); // Stop the holding animation
            if (deleteFlag) // If the delete flag is set, remove the object
            {
                llDie();
            }
        }
    }
    
   touch_start(integer num_detected)
    {
        key id = llDetectedKey(0);
        if (id == llGetOwner() && llGetAttached()) // if the toucher is the owner and the object is attached
        {
            // close any existing dialog box
            llListenRemove(listen_handle);
    
            // Create a random channel to communicate on
            dialog_channel = (integer)llFrand(-9999.0) - 10000; 
    
            listen_handle = llListen(dialog_channel, "", NULL_KEY, "");
            
            if(current_step == number_of_steps - 1) {
                llDialog(id, llList2String(messages, current_step) + "\nThis is the last sip of your milkshake journey. Ready to finish? (Step " + (string)(current_step + 1) + "/" + (string)number_of_steps + ")", ["Finish"], dialog_channel);
            } else {
                llDialog(id, llList2String(messages, current_step) + "\nDo you wish to continue? (Step " + (string)(current_step + 1) + "/" + (string)number_of_steps + ")", ["Continue", "Stop"], dialog_channel);
            }
        }
    }

    listen(integer channel, string name, key id, string message)
    {
        if(message == "Continue" || message == "Finish")
        {

            current_step++;
            if(current_step < number_of_steps)
            {
                llInstantMessage(id, (string)llList2String(messages, current_step));
                llStopAnimation(holdingAnim); // Stop the holding animation
                llStartAnimation(drinkingAnim); // Start the drinking animation
                llSleep(drinkingAnimDuration); // Wait for the animation to complete
                llStopAnimation(drinkingAnim); // Stop the drinking animation
                llStartAnimation(holdingAnim); // Restart the holding animation
                // For the last dialog box, change the option to "Finish"
                if(current_step == number_of_steps - 1)
                {
                    llDialog(id, "This is the last sip of your milkshake journey. Ready to finish? " + (string)(current_step+1) + "/" + (string)number_of_steps, ["Finish"], dialog_channel);
                }
                else
                {
                    llDialog(id, "Would you like to continue drinking? " + (string)(current_step+1) + "/" + (string)number_of_steps, ["Continue", "Stop"], dialog_channel);
                }
            }
            else
            {
                llInstantMessage(id, finish_message);
                current_step = 0;
                dialog_channel = 0; // Close the dialog
                llDetachFromAvatar(); // Detach the object
            }
        }
        else if(message == "Stop")
        {
            llStopAnimation(holdingAnim); // Stop the holding animation
            llInstantMessage(id, halt_message);
            current_step = 0;
            dialog_channel = 0; // Close the dialog
            llDetachFromAvatar(); // Detach the object
        }
    }
    
    run_time_permissions(integer permissions)
    {
        if (!(permissions & PERMISSION_ATTACH))
        {
            llOwnerSay("This script requires attach permissions to function properly.");
        }
    }

    on_rez(integer start_param)
    {
        if (deleteFlag) // If the delete flag is set, remove the object
        {
            llDie();
        }
    }
}