User:Allen Kerensky/Myriad Lite Preview3/Myriad Lite Melee v0.0.2 20110813

From Second Life Wiki
Jump to navigation Jump to search

Myriad Lite Melee v0.0.2 20110813

<lsl> //============================================================================ // Myriad Lite Melee v0.0.2 20110813 // Copyright (c) 2011 By Allen Kerensky (OSG/SL) // The Myriad RPG System was designed, written, and illustrated by Ashok Desai // Myriad published under a: // Creative Commons License (Attribution 2.0 UK: England and Wales) // Myriad Lite published under a // Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported //============================================================================

//============================================================================ // MESSAGE FORMAT REFERENCE //============================================================================ // CHANMYRIAD OUT - RPEVENT|message // CHANATTACH IN - REGISTERATTACHMENTS // CHANATTACH OUT - ATTACHMELEE|int attackdice|int attachpoint|str name // CHANATTACH OUT - DETACHMELEE|int attackdice|int attachpoint|str name // CHANPLAYER OUT - CLOSECOMBAT|int attackdice|key victim|key attacker|str weaponname

//============================================================================ // GLOBAL VARIABLES //============================================================================ key WEARER = NULL_KEY; // UUID of melee weapon user integer ATTACHPOINT = 0; // attachment point integer MINMELEE = 1; // minimum attack dice for melee weapons integer MAXMELEE = 5; // maximum attack dice for melee weapons // Melee Attack Dice // 1D - Unarmed Combat // 2D - Short animal claws, knife, dagger, blackjack, knuckledusters // 3D - Long animal claws, short sword, machete, wooden baseball bat, staff // 4D - Fire axe, long sword, katana, aluminum baseball bat, poleaxe // 5D - Battleaxe, claymore, laser sword, daikatana, double-ended polearm integer MELEEATTACKDICE = 4; // long sword string DIV="|"; // Myriad message field divider string ANIM_SWORD = "sword_strike_r"; // use built in right handed sword strike animation integer CHANATTACH; // hold the dynamically calculated attachment channel number integer HANDATTACH; // hold a handle to remove the attachment channel if needed integer CHANMYRIAD = -999; // the Myriad RPEVENT channel number integer ANIM = FALSE; // do we have permission to animate the avatar? float WEAPON_LENGTH = 2.0; // a six-foot longsword is the default

//============================================================================ // DEFAULT STATE //============================================================================ default {

   //------------------------------------------------------------------------
   // STATE_ENTRY EVENT - called on reset
   //------------------------------------------------------------------------    
   state_entry() {
       CHANATTACH = (integer)("0x"+llGetSubString((string)llGetOwner(),1,7)); // calculate dynamic attachment channel
       HANDATTACH = llListen(CHANATTACH,"",NULL_KEY,""); // start listening on attachment channel
   }
   //------------------------------------------------------------------------
   // ATTACH EVENT
   //------------------------------------------------------------------------
   attach(key id) {
       if ( id != NULL_KEY ) { // if id not equal null key, this is an attach
           WEARER = id; // save the id we attached to
           ATTACHPOINT = llGetAttached(); // get the position we attached to
           // tell the region we're arming a weapon
           llRegionSay(CHANMYRIAD,"RPEVENT"+DIV+llKey2Name(WEARER)+" equips their "+llGetObjectName());
           integer dynchan = (integer)("0x"+llGetSubString((string)WEARER,1,7)); // calculate dynamic attach channel
           // send attach melee message to HUD which disables fist fighter
           llWhisper(dynchan,"ATTACHMELEE"+DIV+(string)MELEEATTACKDICE+DIV+(string)ATTACHPOINT+DIV+llGetObjectName());
           llRequestPermissions(id,PERMISSION_TAKE_CONTROLS|PERMISSION_TRIGGER_ANIMATION); // request permissions
       } else { // the id was null key, so this is a detach
           integer dynchan = (integer)("0x"+llGetSubString((string)WEARER,1,7)); // calculate dynamic attach channel
           if ( dynchan != 0 ) { // is channel valid
               // tell the region we're disarming
               llRegionSay(CHANMYRIAD,"RPEVENT"+DIV+llKey2Name(WEARER)+" disarms their "+llGetObjectName());
               // send detach message to HUD which re-enables fist fighter
               llWhisper(dynchan,"DETACHMELEE"+DIV+(string)MELEEATTACKDICE+DIV+(string)ATTACHPOINT+DIV+llGetObjectName());
           } else { // channel was not valid
               llSay(DEBUG_CHANNEL,"DETACH EVENT WITHOUT PREVIOUS ATTACH?"); // report the problem
           }
           WEARER = NULL_KEY; // cleanup wearer key on detach
           ATTACHPOINT = 0; // cleanup attachpoint on detach
       }
   }
   //------------------------------------------------------------------------
   // LISTEN EVENT - whisper, say, show, regionsay...
   //------------------------------------------------------------------------    
   listen(integer channel,string speakername,key speakerid,string message) {
       if ( channel == CHANATTACH ) { // did this message come in on attachment channel?
           if ( message == "REGISTERATTACHMENTS" ) { // is this a HUD asking for attachments to register?
               WEARER = llGetOwner(); // who is wielding me?
               ATTACHPOINT = llGetAttached(); // where am I attached?
               integer dynchan = (integer)("0x"+llGetSubString((string)WEARER,1,7)); // calculate wearer's dynamic attachment channel
               llWhisper(dynchan,"ATTACHMELEE"+DIV+(string)MELEEATTACKDICE+DIV+(string)ATTACHPOINT+DIV+llGetObjectName()); // tell HUD we're attached to disable fist fighter
           }
       }
   }
   //------------------------------------------------------------------------
   // RUN_TIME_PERMISSIONS EVENT - what is object allowed to do to avatar?
   //------------------------------------------------------------------------    
   run_time_permissions(integer perm) {
       if ( perm & PERMISSION_TAKE_CONTROLS ) { // can object read controls?
           llTakeControls(CONTROL_LBUTTON|CONTROL_ML_LBUTTON,TRUE,TRUE); // start watching the left mouse button in both views
       }
       if ( perm & PERMISSION_TRIGGER_ANIMATION ) { // can we also trigger animations for sword swings?
           ANIM = TRUE; // remember that we have permission now
       }
   }
   //------------------------------------------------------------------------
   // CONTROL EVENT - read arrow keys and mouse button
   //------------------------------------------------------------------------    
   control(key id,integer level,integer edge) {
       if ( id == WEARER ) { // make sure controls come from wielder
           if ( ( level & CONTROL_LBUTTON && edge & CONTROL_LBUTTON ) || ( level & CONTROL_ML_LBUTTON && edge & CONTROL_ML_LBUTTON ) ) { // if left mouse button pressed in regular or mouselook mode
               llSensor("",NULL_KEY,(AGENT|ACTIVE|PASSIVE),WEAPON_LENGTH,PI/6); // trigger a sensor sweep for avatars within weapon range, and standing in front of us
               if ( ANIM == TRUE ) llStartAnimation(ANIM_SWORD); // if we can show sword swing, go ahead
               llSleep(0.2); // pause to let animation run
               return; // return now to skip processing any more events we add later
           }
       }
   }
   //------------------------------------------------------------------------
   // SENSOR EVENT - what did we find?
   //------------------------------------------------------------------------    
   sensor(integer num_detected) {
       while(num_detected--) { // step through all AGENTS returned by sensor sweep
           integer dynchan = (integer)("0x"+llGetSubString((string)llGetOwner(),0,6)); // calculate attackers HUD dynamic channel
           // send the close combat skill check message to attacker to start close combat skill check
           // attackers hud adds attacker stat and skill and sends the information to the victim to finish the opposed close combat skill check
           // we region say this so others can keep score or detect cheaters, etc
           llRegionSay(dynchan,"CLOSECOMBAT"+DIV+(string)MELEEATTACKDICE+DIV+(string)llDetectedKey(num_detected)+DIV+(string)llGetOwner()+DIV+llGetObjectName());
           
           key who = llDetectedKey(num_detected); // who did we hit with sensor?
           key owner = llList2Key(llGetObjectDetails(who,[OBJECT_OWNER]),0); // is this an agent/avatar for sure?
           if ( who == owner ) { // yep, we hit an avatar
               // tell the region an attempted attack is underway
               llRegionSay(CHANMYRIAD,"RPEVENT"+DIV+llKey2Name(llGetOwner())+" strikes at "+llDetectedName(num_detected)+" in Close Combat!");
           }
       }
   }
   //------------------------------------------------------------------------
   // NO_SENSOR EVENT - we didn't find anything to hit
   //------------------------------------------------------------------------    
   no_sensor() {
       // we do nothing in this - but having a nosensor block avoids bugs where sensors would fail if nonsensor was missing
   }

} // end default //============================================================================ // END //============================================================================ </lsl>