Script cannonball cannon

From The Schommunity Wiki
Jump to: navigation, search

This is a script which shoots cannon balls from a cannon. It still needs a bit of work :) The cannon can be anything you want. Simply put this script in the cannon.

//Cannon ball Cannon v0.7.1
//Author: Olly Butters
//11 January 2007
//Part of the schome initiative. (www.schome.ac.uk)
//You may copy/edit this, but please credit us.
//This script moves an object via the avatars key strokes. It has a power variable which is controls the speed the avatar is shot.
//Controls:
//Left/Right - rotate cannon left/right 
//Forward/Back - rotate cannon up/down
//Mouse-click - FIRE!!


float muzzle_velocity = 10;             //speed of the cannon ball
string gBulletName = "dead_blob";       //name of 'bullet object'
integer no_of_inclination_moves = 0;    //keeps track of how much the cannon inclination
integer no_of_rotation_moves = 0;       //keeps track of the cannons rotation
float angle_increment = 10;             //amount the cannon rotates by
integer first_touch = 0;
key id;

default 
{
    state_entry() 
    {
        //sets up the initial position
        vector eul = <0, 0, 0>;
        eul *= DEG_TO_RAD; //convert to radians rotation
        rotation quat = llEuler2Rot( eul ); //convert to quaternion
        llSetRot(quat); //rotate the object
        
    }

   
   run_time_permissions(integer perms)
    {
        integer desired_controls =
                CONTROL_FWD |
                CONTROL_BACK |
                CONTROL_LEFT |
                CONTROL_RIGHT |
                CONTROL_ROT_LEFT |
                CONTROL_ROT_RIGHT;

        if (perms & PERMISSION_TAKE_CONTROLS)
        {
            llTakeControls(desired_controls, TRUE, FALSE);
        }
    }
   
   
   //Move the cannon around.
    control( key ida, integer held, integer change )  // event for processing 
                                                     // key press.
    {
        vector position = llGetPos();
        
        //if left then rotate about the <0,0,1> axis (z)
        if ( change & held & (CONTROL_LEFT | CONTROL_ROT_LEFT) ) 
        {                         
            rotation new_rot = llGetRot()*llAxisAngle2Rot(<0,0,1>, angle_increment*DEG_TO_RAD);
            llSetRot(new_rot); 
            no_of_rotation_moves = no_of_rotation_moves+1;
        } 
        else if ( change & held & (CONTROL_RIGHT |CONTROL_ROT_RIGHT) ) 
        {  
            rotation new_rot = llGetRot()*llAxisAngle2Rot(<0,0,1>, -angle_increment*DEG_TO_RAD);
            llSetRot(new_rot); 
            no_of_rotation_moves = no_of_rotation_moves - 1;
        }
         else if ( change & held & CONTROL_FWD ) 
        {  
            rotation z_inc = llEuler2Rot( <0, angle_increment * DEG_TO_RAD, 0> );
            rotation new_rot =  z_inc*llGetRot() ;  // compute global rotation            
            
            llSetRot(new_rot);          
            
            no_of_inclination_moves = no_of_inclination_moves - 1;
        }
         else if ( change & held & CONTROL_BACK ) 
        {  
            rotation z_inc = llEuler2Rot( <0, -angle_increment * DEG_TO_RAD, 0> );
            rotation new_rot = z_inc*llGetRot()  ;  // compute global rotation
            
            llSetRot(new_rot);          
            
            no_of_inclination_moves = no_of_inclination_moves + 1;
        }        
    }

    //Ask permission to take control, then once thats been done the fun part.....
    //SHOOT!!! This outputs an aweful lot of detail about positions etc which will dissapper later.
    //The main part is the maths part in the llRezObject fn. This works out the velocity to pass to the Rezzed object by looking at the position of the cannon now.    
    touch_start(integer steve)
    {
        if(first_touch==0)
        {
            llSetTimerEvent(60); //makes it reset itself after 1 minute
            integer number;
            id = llDetectedKey(number);
            llSay(0, id);
            llRequestPermissions(id, PERMISSION_TAKE_CONTROLS );
            first_touch = 1;
        }
        else
        {
           
            llRezObject(gBulletName, llGetPos(), <muzzle_velocity*llCos(no_of_rotation_moves*angle_increment*DEG_TO_RAD)*llCos((no_of_inclination_moves*angle_increment)*DEG_TO_RAD),
                                                 muzzle_velocity*llSin(no_of_rotation_moves*angle_increment*DEG_TO_RAD)*llCos((no_of_inclination_moves*angle_increment)*DEG_TO_RAD),
                                                  muzzle_velocity*llSin((no_of_inclination_moves*angle_increment)*DEG_TO_RAD)>, ZERO_ROTATION, 1);
                                              
        }
        
    }
    
    timer()
    {
        llSay(0, "You've had your fun, script resetting.");
        llResetScript();
    }   
}

 

Make a physical cannonball with the same name as in gBulletName in the above script and put this script in it:

script in the cannon.

<pre>
//Cannon ball v1.0
//Author: Olly Butters
//11 January 2007
//Part of the schome initiative. (www.schome.ac.uk)
//You may copy/edit this, but please credit us.
//This is a cannon ball that gets shot from the cannon. It's name MUST corresspond to the name in the cannon script above.
//The cannon ball must also be a PHYSICAL object.
default
{ 
    on_rez(integer start_param)
    {
        llSetTimerEvent(4);  //  Time until shot deletes itself         
    }
    
    timer()
    {
        llDie();   
    }

} 

Then drag the cannonball into the cannon screen where the cannon script is.

--Olly 20:06, 5 February 2007 (GMT)