/**
 * @author(s): Reconx86, Cubey Terra
 * @version: N/A
 * @created: 2009.12.01
 * @date: 2016.03.22
 *
 * RCX Cam Control (based on Cubey Terra's Cam for biplanes)
 *
 * @license:
 * You are allowed to share this script full-perm with others and even use it in your own projects.
 * But do not sell the script seperately nor as part of a script package
 */

integer camOn = FALSE;
integer currCamNum = 0;//Default camera
integer active = FALSE;
integer ejected;
integer started;
integer seated;
key pilot = NULL_KEY;

startCam(integer camNum)
{
    camOn = TRUE;
    llClearCameraParams();
    if (camNum == 0) {
        stopCam();
        
    } else if (camNum == 1) {
        llSetCameraParams([
            CAMERA_ACTIVE, 1, // 1 is active, 0 is inactive
            CAMERA_BEHINDNESS_LAG, 2.0, // (0 to 3) seconds
            CAMERA_DISTANCE, 10.0, // ( 0.5 to 10) meters
            CAMERA_FOCUS_LAG, 0.0 , // (0 to 3) seconds
            CAMERA_FOCUS_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_FOCUS_THRESHOLD, 3.0, // (0 to 4) meters
            CAMERA_PITCH, 7.0, // (-45 to 80) degrees
            CAMERA_POSITION_LAG, 0.0, // (0 to 3) seconds
            CAMERA_POSITION_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_POSITION_THRESHOLD, 2.0 // (0 to 4) meters
        ]);
        
    } else if (camNum == 2) {
        llSetCameraParams([
            CAMERA_ACTIVE, 1, // 1 is active, 0 is inactive
            CAMERA_BEHINDNESS_LAG, 0.1, // (0 to 3) seconds
            CAMERA_DISTANCE, 12.0, // ( 0.5 to 10) meters
            CAMERA_FOCUS_LAG, 0.0 , // (0 to 3) seconds
            CAMERA_FOCUS_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_FOCUS_THRESHOLD, 0.0, // (0 to 4) meters
            CAMERA_PITCH, 2.0, // (-45 to 80) degrees
            CAMERA_POSITION_LAG, 0.0, // (0 to 3) seconds
            CAMERA_POSITION_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_POSITION_THRESHOLD, 0.0 // (0 to 4) meters
        ]);
        
    } else if (camNum == 3) {
        llSetCameraParams([
            CAMERA_ACTIVE, 1, // 1 is active, 0 is inactive
            CAMERA_BEHINDNESS_LAG, 1.0, // (0 to 3) seconds
            CAMERA_DISTANCE, 20.0, // ( 0.5 to 10) meters
            CAMERA_FOCUS_LAG, 0.0 , // (0 to 3) seconds
            CAMERA_FOCUS_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_FOCUS_THRESHOLD, 0.0, // (0 to 4) meters
            CAMERA_PITCH, 4.0, // (-45 to 80) degrees
            CAMERA_POSITION_LAG, 0.0, // (0 to 3) seconds
            CAMERA_POSITION_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_POSITION_THRESHOLD, 0.0 // (0 to 4) meters
        ]);
        
    } else if (camNum == 4) {
        llSetCameraParams([
            CAMERA_ACTIVE, 1, // 1 is active, 0 is inactive
            //CAMERA_BEHINDNESS_ANGLE, 45.0, // (0 to 180) degrees
            CAMERA_BEHINDNESS_LAG, 0.5, // (0 to 3) seconds
            CAMERA_DISTANCE, 10.0, // ( 0.5 to 10) meters
            //CAMERA_FOCUS, <0,0,5>, // region relative position
            CAMERA_FOCUS_LAG, 0.05 , // (0 to 3) seconds
            CAMERA_FOCUS_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_FOCUS_THRESHOLD, 0.0, // (0 to 4) meters
            CAMERA_PITCH, 20.0, // (-45 to 80) degrees
            //CAMERA_POSITION, <0,0,0>, // region relative position
            CAMERA_POSITION_LAG, 0.1, // (0 to 3) seconds
            CAMERA_POSITION_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_POSITION_THRESHOLD, 0.0 // (0 to 4) meters
            //CAMERA_FOCUS_OFFSET, <3,0,2> // <-10,-10,-10> to <10,10,10> meters
        ]);
        
    } else if (camNum == 5) {
        llSetCameraParams([
            CAMERA_ACTIVE, 1, // 1 is active, 0 is inactive
            //CAMERA_BEHINDNESS_ANGLE, 45.0, // (0 to 180) degrees
            CAMERA_BEHINDNESS_LAG, 0.5, // (0 to 3) seconds
            CAMERA_DISTANCE, 15.0, // ( 0.5 to 10) meters
            //CAMERA_FOCUS, <0,0,5>, // region relative position
            CAMERA_FOCUS_LAG, 0.05 , // (0 to 3) seconds
            CAMERA_FOCUS_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_FOCUS_THRESHOLD, 0.0, // (0 to 4) meters
            CAMERA_PITCH, 50.0, // (-45 to 80) degrees
            //CAMERA_POSITION, <0,0,0>, // region relative position
            CAMERA_POSITION_LAG, 0.1, // (0 to 3) seconds
            CAMERA_POSITION_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_POSITION_THRESHOLD, 0.0 // (0 to 4) meters
            //CAMERA_FOCUS_OFFSET, <3,0,2> // <-10,-10,-10> to <10,10,10> meters
        ]);
        
    } else if (camNum == 6) {  
        llSetCameraParams([
            CAMERA_ACTIVE, 1, // 1 is active, 0 is inactive
            //CAMERA_BEHINDNESS_ANGLE, 45.0, // (0 to 180) degrees
            CAMERA_BEHINDNESS_LAG, 0.5, // (0 to 3) seconds
            CAMERA_DISTANCE, 15.0, // ( 0.5 to 10) meters
            //CAMERA_FOCUS, <0,0,5>, // region relative position
            CAMERA_FOCUS_LAG, 0.05 , // (0 to 3) seconds
            CAMERA_FOCUS_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_FOCUS_THRESHOLD, 0.0, // (0 to 4) meters
            CAMERA_PITCH, -20.0, // (-45 to 80) degrees
            //CAMERA_POSITION, <0,0,0>, // region relative position
            CAMERA_POSITION_LAG, 0.1, // (0 to 3) seconds
            CAMERA_POSITION_LOCKED, FALSE, // (TRUE or FALSE)
            CAMERA_POSITION_THRESHOLD, 0.0 // (0 to 4) meters
            //CAMERA_FOCUS_OFFSET, <3,0,2> // <-10,-10,-10> to <10,10,10> meters
        ]);
    }
}

stopCam()
{
    camOn = FALSE;
    llClearCameraParams();
}

default {
    
    state_entry() 
    {
        llSetMemoryLimit(0x3000);
        llSetCameraEyeOffset(<-10, 0, 3>);
        llSetCameraAtOffset(<0, 0, 1.9>);
    }
    
    on_rez(integer num)
    {
        llResetScript();
    }
    
    link_message(integer sender, integer num, string msg, key id) 
    {
        integer perm = llGetPermissions();
        if (perm & PERMISSION_CONTROL_CAMERA) {
            if (msg == "#cam") {
                currCamNum = num;
                if (currCamNum == -1 && camOn) {
                    stopCam();    
                } else {
                    startCam(currCamNum); 
                }
                
            } else if (msg == "#cam++") {
                if (currCamNum < 6) {
                    currCamNum++;   
                } else {
                    currCamNum = -1;
                }
                if (currCamNum == -1) {
                    stopCam();
                } else {
                    camOn = TRUE;
                    startCam(currCamNum);
                }
            }
        
        } else if (msg == "#pilot") {
            pilot = id;
            if (pilot != NULL_KEY) {
                llRequestPermissions(pilot, PERMISSION_CONTROL_CAMERA);
                active = TRUE;
                seated = TRUE;
            
            } else {
                llResetScript();
            }
            
        } else if (msg == "#engine") {
            started = num;
        }
    }
    
    run_time_permissions(integer perm) 
    {       
        if (perm & PERMISSION_CONTROL_CAMERA) {
            startCam(currCamNum);
        }    
    }
    
    changed(integer change)
    {
        if (change & CHANGED_REGION) {
            if (currCamNum != 0) {
                llSetTimerEvent(0.1);
            }
        }
    }
    
    timer()
    {
        //Check if avatar is in region
        if (llGetAgentSize(pilot) != ZERO_VECTOR) {
            
            //Restart camera and reset timer
            startCam(currCamNum);
            llSetTimerEvent(0);
        }
    }
}