// Sliding Door Script by Nebadon Izumi
// Modified for earflicking upon touch for the Shork Industries Serval Avatar
// The default subpage for serval avatars is:
// https://shork.for-more.biz/wiki/doku.php?id=avatars:avatars_big_cat#servals
// Base URL is:
// https://shork.for-more.biz
// llDegreeToRadians fix by Paela Argus
// change the rotation angle and axis here
// For right Ear, Uncomment the following 2 lines:
float RotationAngle = -45.0; // degrees to rotate
vector RotationAxis = <0, 1, 1>; // rotation axis (e.g., <1,0,0> for X, <0,1,0> for Y, <0,0,1> for Z)
// For left Ear, Uncomment the following two lines:
// float RotationAngle = 45.0; // degrees to rotate
// vector RotationAxis = <0, -1, 1>; // rotation axis (e.g., <1,0,0> for X, <0,1,0> for Y, <0,0,1> for Z)
float Timer = 1.0; // Set time how long door stays open and then closes again

// change sound settings here
string OpenSound = ""; // opening sound
float OpenVol = 1.0; // opening volume
string CloseSound = ""; // closing sound
float CloseVol = 1.0; // closing volume

rotation StartRot; // stores the initial rotation
rotation RotationOffset; // stores the rotation offset for the door
integer Open; // flag for whether the door is open

default
{
    state_entry()
    {
        StartRot = llGetLocalRot(); // save the initial rotation
        RotationOffset = llAxisAngle2Rot(RotationAxis, RotationAngle * (3.14159265359 / 180.0)); // calculate the rotation offset
    }

    touch_start(integer num)
    {
        Open = !Open; // toggle the door state
        if (Open)
        {
            if (OpenSound != "") llTriggerSound(OpenSound, OpenVol); // play opening sound
            llSetLocalRot(StartRot * RotationOffset); // rotate the door to the open position
            llSetTimerEvent(Timer); // set the timer to close the door
        }
        else
        {
            if (CloseSound != "") llTriggerSound(CloseSound, CloseVol); // play closing sound
            llSetLocalRot(StartRot); // reset to the original rotation
            llSetTimerEvent(0); // stop the timer
        }
    }

    collision_start(integer num)
    {
        Open = !Open; // toggle the door state
        if (Open)
        {
            if (OpenSound != "") llTriggerSound(OpenSound, OpenVol); // play opening sound
            llSetLocalRot(StartRot * RotationOffset); // rotate the door to the open position
            llSetTimerEvent(Timer); // set the timer to close the door
        }
        else
        {
            if (CloseSound != "") llTriggerSound(CloseSound, CloseVol); // play closing sound
            llSetLocalRot(StartRot); // reset to the original rotation
            llSetTimerEvent(0); // stop the timer
        }
    }

    on_rez(integer param)
    {
        llResetScript(); // reset the script on rezzing
    }

    timer()
    {
        if (CloseSound != "") llTriggerSound(CloseSound, CloseVol); // play closing sound
        llSetLocalRot(StartRot); // reset to the original rotation
        llSetTimerEvent(0); // stop the timer
        Open = 0; // set the door to closed state
    }
}
