Jetski V2.1

Written by: Headmaster
vector angular_motor;
float speed=0;
float lift = 0.04;
float tilt=15;
float turn=1.0;
vector moving;
integer turnnow = FALSE;
integer ts = FALSE;
integer cs = FALSE;
integer sit=FALSE;
vector velocity=<0,0,0>;
vector impulse;
float turbo=10;
string Bname; // Name of Vehicle
integer own=FALSE;
float volume=1.0;
key passenger;
key driver;

help()
{
    llWhisper(0,"Key Actions:");
    llWhisper(0,"Right click and 'Ride' <-- Starts " + Bname);
    llWhisper(0,"Click STAND UP button <-- Stops " + Bname + " and returns contols");
    llWhisper(0,"W or Forward <-- Accelerates or goes faster forwards");
    llWhisper(0,"S or Backwards <-- Slows or goes faster backwards");
    llWhisper(0,"W or Forward & S or Backwards <-- Brakes or stops the " + Bname);
    llWhisper(0,"E or Page up <-- Toggles on and off Turbo Mode");
    llWhisper(0,"C or Page down <-- Toggles on and off Cruise mode");
    llWhisper(0,"A or Left <-- Turns left");
    llWhisper(0,"D or Right <-- Turns right");
}

default
{
    state_entry()
    {
        Bname=llGetObjectName();
        llWhisper(0,"Preparing " + Bname);
        llMessageLinked(LINK_ALL_CHILDREN, 0, "stop", NULL_KEY);
        llSetStatus(STATUS_PHYSICS, FALSE);
        llStopSound();
        llSetTimerEvent(0.0);
        velocity = <10, 0, 0>;
        turnnow = FALSE;
        ts = FALSE;
        cs = FALSE;
        llCollisionSound("", 0.0);
        llSitTarget(<-0.4,0.05,0.7>, <0,0.0,0,1>);
        llSetCameraEyeOffset(<-9.0, 0.0, 3.0>);
        llSetCameraAtOffset(<0.0, 0.0, 1.0>);
        llPreloadSound("Ducati Idle");
        llPreloadSound("D0");
        llPreloadSound("D1");
        llPreloadSound("D2");
        llPreloadSound("D3");
        llPreloadSound("D4");
        llPreloadSound("D5");
        llPreloadSound("D6");
        llSetSitText("Ride");
        llSetVehicleFlags(-1);
        llSetVehicleType(VEHICLE_TYPE_BOAT);
        llSetVehicleFlags(VEHICLE_FLAG_HOVER_UP_ONLY | VEHICLE_FLAG_HOVER_WATER_ONLY);
        llRemoveVehicleFlags( VEHICLE_FLAG_HOVER_TERRAIN_ONLY 
                              | VEHICLE_FLAG_LIMIT_ROLL_ONLY 
                              | VEHICLE_FLAG_HOVER_GLOBAL_HEIGHT);
        llSetVehicleVectorParam( VEHICLE_LINEAR_FRICTION_TIMESCALE, <8, 3, 2> );
        llSetVehicleFloatParam( VEHICLE_ANGULAR_FRICTION_TIMESCALE, 2 );
        llSetVehicleFloatParam( VEHICLE_LINEAR_MOTOR_TIMESCALE, 2 );
        llSetVehicleFloatParam( VEHICLE_LINEAR_MOTOR_DECAY_TIMESCALE, 60 );
        llSetVehicleFloatParam( VEHICLE_ANGULAR_MOTOR_TIMESCALE, 1 );
        llSetVehicleFloatParam( VEHICLE_ANGULAR_MOTOR_DECAY_TIMESCALE, 5 );
        llSetVehicleFloatParam( VEHICLE_HOVER_HEIGHT, 0.15);
        llSetVehicleFloatParam( VEHICLE_HOVER_EFFICIENCY,.5 );
        llSetVehicleFloatParam( VEHICLE_HOVER_TIMESCALE, 2.0 );
        llSetVehicleFloatParam( VEHICLE_BUOYANCY, 0.5 );
        llSetVehicleFloatParam( VEHICLE_LINEAR_DEFLECTION_EFFICIENCY, 0.5 );
        llSetVehicleFloatParam( VEHICLE_LINEAR_DEFLECTION_TIMESCALE, 3 );
        llSetVehicleFloatParam( VEHICLE_ANGULAR_DEFLECTION_EFFICIENCY, 0.5 );
        llSetVehicleFloatParam( VEHICLE_ANGULAR_DEFLECTION_TIMESCALE, 10 );
        llSetVehicleFloatParam( VEHICLE_VERTICAL_ATTRACTION_EFFICIENCY, 0.5 );
        llSetVehicleFloatParam( VEHICLE_VERTICAL_ATTRACTION_TIMESCALE, 2 );
        llSetVehicleFloatParam( VEHICLE_BANKING_EFFICIENCY, 1 );
        llSetVehicleFloatParam( VEHICLE_BANKING_MIX, 0.1 );
        llSetVehicleFloatParam( VEHICLE_BANKING_TIMESCALE, .5 );
        llSetVehicleRotationParam( VEHICLE_REFERENCE_FRAME, <0, 0, 0, 1> );
        llWhisper(0,Bname + " is now ready to ride");
    }
    
    touch(integer total_number)
    {
        key owner=llGetOwner();
        if (llDetectedKey(0)==owner)
        {
            help();
        }
        else
        {
            llWhisper(0,llDetectedName(0) + ", Please step away from the " + Bname);
        } 
    }
    
    changed(integer change)
    {
        if (change & CHANGED_LINK)
        {
            llStopSound();
            llSetStatus(STATUS_PHYSICS, FALSE);
            driver = llAvatarOnSitTarget();
            if (driver)
            {
                if (driver != llGetOwner() && own==FALSE)
                {
                    string ownername=llKey2Name(llGetOwner());
                    string sitter=llKey2Name(driver);
                    llWhisper(0,sitter + ", you may only drive the " + Bname + " after " + ownername + " is seated");
                }
                if(driver == llGetOwner())
                {
                    //llWhisper(0,"1" + (string)own);
                    own=TRUE;
                }
                llSetTimerEvent(0.1);
            }
            else
            {
                llSetTimerEvent(0.0);
                //llWhisper(0,"4" + (string)own);
                own=FALSE; 
                llStopAnimation("motorcycle_sit");
                llMessageLinked(LINK_ALL_CHILDREN, 0, "stop", NULL_KEY);
                llSetStatus(STATUS_PHYSICS, FALSE);
                llReleaseControls();
                llStopSound();
                sit=FALSE;
                ts = FALSE;
                cs = FALSE;
            }
        }
        
    }
    
    run_time_permissions(integer perm)
    {
        if (perm && own)
        {
            //llWhisper(0,"3" + (string)own);
            sit=TRUE;
            speed=0;
            ts = FALSE;
            cs = FALSE;
            llStopAnimation("sit");
            llStartAnimation("motorcycle_sit");
            llTakeControls(CONTROL_FWD | CONTROL_BACK | CONTROL_RIGHT | CONTROL_LEFT | CONTROL_ROT_RIGHT | CONTROL_ROT_LEFT | CONTROL_UP | CONTROL_DOWN, TRUE, FALSE);
        }
    }
    
    control(key id, integer level, integer edge)
    {
        if(level & CONTROL_FWD)
        {
            if(ts)
            {
                if(velocity.x < 100)
                {
                    if(cs == FALSE) { velocity.x += (turbo * 3);}
                    else { velocity.x += turbo;}
                    speed = 0; 
                }   
            }
            else
            {    
                if(speed < 35)
                {
                    if(cs == FALSE) { speed += 5;}
                    else { speed += 1; }
                    
                }
            }
        }
        if(level & CONTROL_BACK)
        {
            if(ts)
            {
                if(velocity.x > 0)
                {
                    velocity.x -= turbo; 
                }   
                else 
                {
                    velocity.x = 0;
                    speed = 0;
                }
            }        
            if(speed > -15 && velocity.x < 1)
            {
                speed -= 3;
            }
        }
        if((level & CONTROL_BACK) && (level & CONTROL_FWD))
        {
            speed=0;
            velocity=<0,0,0>;
        }
        if(level & CONTROL_UP)
        {
            if(ts)
            {
                llWhisper(0,"Turbo off");
                ts = FALSE;
                lift=0.04;
                speed=0;
                turn=1.0;
                velocity=<0,0,0>;
            }
            else
            {
                llWhisper(0,"Turbo on");
                ts = TRUE;
                lift=0.08;
                speed=0;
                turn=2.0;
                velocity=<0,0,0>;
            }
        }
        if(level & CONTROL_DOWN)
        {
            if(cs)
            {
                llWhisper(0,"Cruise off");
                cs = FALSE;
                speed=0;
                velocity=<0,0,0>;
            }
            else
            {
                llWhisper(0,"Cruise on");
                cs = TRUE;
                speed=0;
                velocity=<0,0,0>;
            }
        }
        if(level & (CONTROL_RIGHT|CONTROL_ROT_RIGHT))
        {
            turnnow = TRUE;
            angular_motor.x += tilt;
            angular_motor.z -= turn;
        }
        if(level & (CONTROL_LEFT|CONTROL_ROT_LEFT))
        {
            turnnow = TRUE;
            angular_motor.x -= tilt;
            angular_motor.z += turn;
        }
        
    }
    
    timer()
    {
        if (sit==FALSE)
        {
            if(own)
            {
                llSetStatus(STATUS_PHYSICS, TRUE);
                llRequestPermissions(driver,PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS);
                llLoopSoundMaster("Ducati Idle",volume - 0.5);
                llMessageLinked(LINK_ALL_CHILDREN, 0, "start", NULL_KEY);
            }
        }
        else if(sit)
        {
            llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, ZERO_VECTOR);
            if(turnnow)
            {
                llSetVehicleVectorParam(VEHICLE_ANGULAR_MOTOR_DIRECTION, angular_motor);
                angular_motor = ZERO_VECTOR;
                turnnow = FALSE;
            }
            
            if((velocity.x > 9) || (speed > 1))
            {
                llSetVehicleRotationParam( VEHICLE_REFERENCE_FRAME, <0, lift, 0, 1> );
            }
            else
            {
                llSetVehicleRotationParam( VEHICLE_REFERENCE_FRAME, <0, 0, 0, 1> );
            }
            
            if(ts) { llApplyImpulse( velocity, TRUE ); }
            else
            {
                llSetVehicleVectorParam(VEHICLE_LINEAR_MOTOR_DIRECTION, ); 
            }   
            moving=llGetVel(); 
            float total = llVecMag(moving);
            if (total < 1)
            {
                llLoopSoundSlave("Ducati Idle",volume - 0.5);
            }
            else if (total < 3) 
            {
                llLoopSoundSlave("D0",volume);
            }
            else if (total < 6) 
            {
                llLoopSoundSlave("D1",volume);
            }
            else if (total < 9) 
            {
                llLoopSoundSlave("D2",volume);
            }
            else if (total < 12) 
            {
                llLoopSoundSlave("D3",volume);
            }
            else if (total < 15) 
            {
                llLoopSoundSlave("D4",volume);
            }
            else if (total < 18) 
            {
                llLoopSoundSlave("D5",volume);
            }
            else if (total > 17) 
            {
                llLoopSoundSlave("D6",volume);
            }
            if(cs == FALSE)
            {
                speed *= 0.75;
                velocity.x *= 0.75;
            }
                
        }
    }
    
    link_message(integer sender_num, integer num, string str, key id)
    {
        if(num==21)
        {
            passenger=str;
            sit=FALSE;
            if(passenger == llGetOwner())
            {
                own=TRUE;
            }
        }
        else if(num==20)
        {
            if(passenger == llGetOwner())
            {
                own=FALSE;
            }
            passenger=NULL_KEY;
            sit=FALSE;
        }
    }
    
    on_rez(integer start_param)
    {
        llResetScript();
    } 
}