User:Allen Kerensky/Myriad Lite Dev/Myriad Lite Turret-v0.0.0-20120510.lsl

From OpenSimulator

Jump to: navigation, search

Myriad_Lite_Turret-v0.0.0-20120510.lsl

// Myriad_Lite_Turret-v0.0.0-20120510.lsl
// Copyright (c) 2012 By Allen Kerensky (OSG/SL)
// The Myriad RPG System was designed, written, and illustrated by Ashok Desai
// Myriad RPG licensed under the Creative Commons Attribution 2.0 UK: England and Wales
// http://creativecommons.org/licenses/by/2.0/uk/
// Myriad Lite software Copyright (c) 2011-2012 by Allen Kerensky (OSG/SL)
// Baroun's Adventure Machine Copyright (c) 2008-2011 by Baroun Tardis (SL)
// Myriad Lite and Baroun's Adventure Machine licensed under the
// Creative Commons Attribution-Share Alike-Non-Commercial 3.0 Unported
// http://creativecommons.org/licenses/by-nc-sa/3.0/
// You must agree to the terms of this license before making any use of this software.
// If you do not agree to this license, simply delete these materials.
// There is no warranty, express or implied, for your use of these materials.
 
// MYRIAD COMBAT STATS
integer POWER = 3; // attack stat
integer ATTSKILL = 1; // attack skill
integer DAMAGEDICE = 1;
 
// 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 Bullet Turret v0.0.0 20120511"; //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 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 = TRUE;
    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 = 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)fwd+"~~~"+(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