User:Allen Kerensky/Myriad Lite/Myriad Lite Turret-Preview6.lsl

From OpenSimulator

Jump to: navigation, search

Myriad_Lite_Turret-Preview6.lsl

// Myriad_Lite_Turret-v0.0.1-20120807.lsl
// Copyright (c) 2012 by Allen Kerensky (OSG/SL) All Rights Reserved.
// This work is dual-licensed under
// Creative Commons Attribution (CC BY) 3.0 Unported
// http://creativecommons.org/licenses/by/3.0/
// - or -
// Modified BSD License (3-clause)
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, 
//   this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
// * Neither the name of Myriad Lite nor the names of its contributors may be
//   used to endorse or promote products derived from this software without
//   specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
// NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The Myriad RPG System was designed, written, and illustrated by Ashok Desai
// Myriad RPG System licensed under:
// Creative Commons Attribution (CC BY) 2.0 UK: England and Wales
// http://creativecommons.org/licenses/by/2.0/uk/
 
string  VERSION = "0.0.1"; // version number
string  VERDATE = "20120807"; // version date
 
// MYRIAD COMBAT STATS
integer POWER = 1; // attack stat - should stay 1 like an NPC Critter Goon
integer ATTSKILL = 1; // attack skill 1-10, 1-5 is human range, 6+ is supernatural
 
integer DAMAGEDICE = 3; // 3 = automatic pistol damage class
 
// TURRENT CONFIGURATION
float RANGE = 30.0; // detection range to start shooting
float RATE = 0.5; // seconds between shots
float BULLET_VELOCITY = 30.0;                       // change this to change the speed of the bullet.
string GUNSOUND = "pistol_shot.wav";                            // string; name of sound in inventory
string AMMO =  "Myriad Lite Turret Bullet Preview 6"; //name of desired object to be shot out. Must be in the inventory of the "gun".
integer ANTIDELAY = TRUE; // use antidelay nodes for rapid fire?
 
// RUNTIME
integer FLAG_DEBUG;
integer ISACTIVE = FALSE;
integer CHANOBJECT;
integer HANDLE;
vector REZ_OFFSET = <0,0, 2.1>;                // rez offset for bullet
vector POS;
rotation ROT;
vector OFFSET;
string DIV = "|";
 
// DEBUG
DEBUG(string debugmsg) {
    if ( FLAG_DEBUG == TRUE ) llOwnerSay("TURRET: "+(string)debugmsg);
}
 
// SETUP
SETUP() {
    FLAG_DEBUG = FALSE;
    ANTIDELAY = FALSE;
    CHANOBJECT = (integer)("0x"+llGetSubString(llGetKey(),0,6)); // calculate turret's dynamic channel
    if ( HANDLE != 0 ) llListenRemove(HANDLE);
    HANDLE = llListen(CHANOBJECT,"",NULL_KEY,"");
}
 
default {
    //------------------------------------------------------------------------
    // LISTEN EVENT - whispers, says, shouts, regionsays
    //------------------------------------------------------------------------
    listen(integer channel,string speakername,key speakerid,string message) {
        DEBUG("listen: "+message);
        channel = 0; //LSLINT
        speakername = ""; //LSLINT
        speakerid = NULL_KEY; //LSLINT
        list fields = llParseString2List(message,[DIV],[]); // break line of text into = delimited fields
        string command = llToLower(llStringTrim(llList2String(fields,0),STRING_TRIM)); // field zero is the "command"
 
        // If Your Bullet has hit, fire a hitcheck regionwide at targetplayer's channel
        if ( command == "rangedcombat" ) {
            integer attdice = llList2Integer(fields,1); // get attack dice of weapon used
            string hitwho = llList2String(fields,2); // get UUID of who we hit
            string bywho = llList2String(fields,3); // should be our own UUID
            string bywhat = llList2String(fields,4); // name of item we hit with (good for bullets/missiles)
 
            integer victimchan = (integer)("0x"+llGetSubString(hitwho,0,6)); // calculate victim's dynamic channel
            llRegionSay(victimchan,"RANGEDHIT"+DIV+(string)POWER+DIV+(string)ATTSKILL+DIV+(string)attdice+DIV+(string)bywho+DIV+bywhat); // attack!
            DEBUG((string)victimchan+" RANGEDHIT"+DIV+(string)POWER+DIV+(string)ATTSKILL+DIV+(string)attdice+DIV+(string)bywho+DIV+bywhat);
            return;
        } // end if RANGEDCOMBAT/TOHIT
    }
 
    no_sensor() {
    }
 
    // OBJECT_REZ
    object_rez(key child) { // tell child turret bullet our key for combat resolution
        integer childchan = (integer)("0x"+llGetSubString((string)child,0,6)); // get turret bullet's dynamic channel
        llRegionSay(childchan,(string)llGetKey()); // tell turret bullet who shot them
    }    
 
    sensor(integer num_detected) {
        num_detected = 0; //LSLINT
        // use sensor detected index 0 to only shoot at closest 
        vector targetpos = llDetectedPos(0);
        llLookAt(targetpos,1,1);
        float dist = llVecDist(llGetPos(),llDetectedPos(0));
        if(llVecDist(llGetPos(),llDetectedPos(0)+llRot2Fwd(llDetectedRot(0))*dist) < 1.5) {
            // Fire 1 bullet,,  the heart of the firearm script.
            POS = llGetPos(); // get our current position
            ROT = llGetRot(); // get our current rotation
            OFFSET = REZ_OFFSET; // start with the base offset for the gun held in the right hand
            OFFSET *= ROT; // now, rotate the offset to match the avatar rotation
            POS += OFFSET; // now combine the rotated offset with avatar position 
            vector fwd = llRot2Up(ROT); // calculate the direction that is "avatar's facing" 
            fwd *= BULLET_VELOCITY; // now multiply that by bullet speed to tell bullet to push in that direction, that fast
            //rot *= llEuler2Rot(<0, PI_BY_TWO, 0>); // now, straighten rotation for object we're about to rez
            llPlaySound(GUNSOUND,1.0); // here "GUNSOUND"is a variable defined above.
            // DAMAGEDICE is passed to rez-param of bullet. Myriad Bullets read this as damage dice to do if they hit
            if ( ANTIDELAY == FALSE ) {
                llRezObject(AMMO, POS, fwd, ROT, DAMAGEDICE); // does the actual work rezzes the AMMO in the specified variables.
            } else {
                llMessageLinked(LINK_THIS,-123,AMMO+"~~~"+(string)POS+"~~~"+(string)fwd+"~~~"+(string)ROT+"~~~"+(string)DAMAGEDICE,"rezobject");
            }
            //llSleep(RATE); // force a pause between shots            
        }
    }
 
    state_entry() {
        SETUP();
    }
 
    touch_start(integer num_detected) {
        num_detected = 0; // LSLINT
        if ( ISACTIVE == FALSE ) {
            ISACTIVE = TRUE;
            CHANOBJECT = (integer)("0x"+llGetSubString(llGetKey(),0,6));
            HANDLE = llListen(CHANOBJECT,"",NULL_KEY,"");
            llSensorRepeat("",NULL_KEY,AGENT,RANGE,PI,RATE);
            llSay(PUBLIC_CHANNEL,"Turret active");
        } else {
            ISACTIVE = FALSE;
            llSensorRemove();
            llListenRemove(HANDLE);
            llSay(PUBLIC_CHANNEL,"Turret no longer active");
        }
    }
}
Personal tools
General
About This Wiki