/*

Bird Wanderer - Randomly fly over *water only* 

This can be used to make an animesh bird that flies randomly over water. This example is an eagle

Change the animations and sounds to match your animal.

*/


float water;
float ground;

vector regionSize;
integer isOn;
float tick;


string oAnim;

anim( string an)
{
    
        llStopObjectAnimation(oAnim);
        oAnim = an;
        llStartObjectAnimation(oAnim);
        
}


default
{
    on_rez(integer n)
    {
        llResetScript();
    }
    
    state_entry()
    {
        list curr_anims = llGetObjectAnimationNames();
        integer length = llGetListLength(curr_anims);
        while (--length>=0)
        {
            llStopObjectAnimation(llList2String(curr_anims, length));
        }
        
       
        regionSize = osGetRegionSize();
        water = llWater(ZERO_VECTOR);

 
        llSay(0, "Touch me to activate. I will fly above water. ");
        llSetKeyframedMotion([], []);
        
        llListen(0, "", "", "eagle come"); // Shouting "eagle come" will bring the eagle near you. Change  it to whatever call you want to use

    }
    
    
    listen(integer chan, string w, key id, string msg)
    {
        {

            list userData=llGetObjectDetails(id, [OBJECT_NAME,OBJECT_POS, OBJECT_ROT]);
            llSetKeyframedMotion([], []);

            vector v = llList2Vector(userData, 1);
            vector size  = llGetAgentSize(id);
            llSetRegionPos( v + <1, 0,-size.z/2 + 0.25>*llList2Rot(userData,2) );
            llSetTimerEvent(1*isOn);

        }
    }
    
    
    touch_start(integer n) 
    {
        isOn=!isOn;
        llSetKeyframedMotion([], []);
        llSetTimerEvent(1*isOn);
        llSay(0,"Active="+(string)isOn);
    }
    
    
    timer()
    {
        llSetKeyframedMotion([], []);
        
        // Do something randomly
        integer rnd = (integer)llFrand(5);
        if (rnd ==1)
        {
            // Stay idle
            if (llFrand(1) < 0.5)
            {
                // Howl
                llTriggerSound("eagle", 1); 
                
            }
            else
            {
                //just stand
            }
            llSetTimerEvent(3+llFrand(6));
        }
        else
        {
            
            
            vector fwd;
            float ang;
            
            vector pos = llGetPos();        
            vector nxt = pos;
            rotation rot = llGetRot();
            float time=0;
            integer i;
            list kf;
            
            fwd = <1,0,0>*rot;
            
            for (i=0; i < 30; i++)
            {
                
                nxt = nxt + 3*fwd;
                float grnd = llGround(nxt - llGetPos());
                
                if (grnd >= water
                || nxt.x <=3 || nxt.x >= regionSize.x-3
                || nxt.y <=3 || nxt.y >= regionSize.y-3
                )
                {
                    jump outside;
                }
            }
            
            @outside;
            
            vector newpos = pos + 3*i*fwd;
            newpos.z = water+ 2 + llFrand(50);
            
            if (i>1)
            {
                kf += newpos - pos; 
                kf += ZERO_ROTATION; 
                float flytime =llVecMag(newpos - pos) / 4.0;
                kf += flytime; // time
                time += flytime;
            }
    
            //rotate at the end
            ang = 0.3 + llFrand(1);
            rotation r2 =  llEuler2Rot(<0.0,0, ang>);
            kf += ZERO_VECTOR;
            kf += r2;
            kf += 3;
            time += 3;
            
            llSetKeyframedMotion( kf, [KFM_DATA, KFM_TRANSLATION|KFM_ROTATION, KFM_MODE, KFM_FORWARD]);
            llSetTimerEvent(time+0.1);
            
            string s = "Flying"; // Change flying animation
            anim(s);
        }
    }
}