Jump to content

Door script 0.4

From OSgrid Wiki
Revision as of 20:52, 20 July 2018 by Osgridadmin (talk | contribs) (Created page with "<source lang=lsl> /* Fu's talking door script v0.4 - Actual door animation swingeryness v0.3 - Access Control List (acl) for locking mechanism - a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<source lang=lsl>

/*

   Fu's talking door script 
   v0.4
   - Actual door animation swingeryness
   v0.3
   - Access Control List (acl) for locking mechanism
   - acl check function
   v0.2
   - locking door latch
   - "CONSTANTS" for code clarity
   v0.1
   - state changes
   - passing data between states
   - llListen() and listen event handler
   v0.0
   - basic states
   (c)2017 - Fu Barr (OSGrid)
  • /

integer DEBUG = FALSE;

string code_lock = "Lockus locke"; string code_unlock = "Unlockus locke";

list acl = ["Fu Barr"];

string latch;

string txt_unlocked = "This door is now unlocked."; string txt_locked = "This door is now locked."; string txt_noentry = "Locked. Your pixels may not pass."; string txt_noacl = "You may not command this door."; string txt_welcome = "Welcome, ";

vector door_rot_home = <0, 0, 0>; float door_rot_angle = -110; float door_rot_time = 2.0;

key avatar_uuid;


integer acl_check(key uuid) { // // an empty acl list implies a lock open to all.

   if(DEBUG){llOwnerSay("acl_check() checking: " + uuid);}
   if(llGetListLength(acl) > 0) {
       if(llListFindList(acl, [llKey2Name(uuid)]) > -1) {
           return TRUE;
       } else {
           return FALSE;
       }
   } else {
       if(DEBUG){llOwnerSay("acl_check(): No ACL entries.");}
       return TRUE;        
   }

}


door_swing(string direction) {

   list mode;
   if(direction == "OPEN") {
        mode = [KFM_MODE, KFM_FORWARD];
   } else {
        mode = [KFM_MODE, KFM_REVERSE];        
   }
   llSetKeyframedMotion([<0.0, 0.0, 0.0>, llEuler2Rot(<0, 0, door_rot_angle> * DEG_TO_RAD), door_rot_time], mode);    

}


default {

   on_rez(integer somevalue) {
       llResetScript();
   }
   state_entry() {
       latch = "UNLOCKED";
       llSetRot(llEuler2Rot(door_rot_home * DEG_TO_RAD));
       llOwnerSay("running " + llGetScriptName());
       state closed;
   }

}


state closed {

   state_entry() {
       llListen(0, "", NULL_KEY, code_unlock);     // using two llListen() for early channel 0 chat filter
       llListen(0, "", NULL_KEY, code_lock);
   }
   state_exit() {
       door_swing("OPEN");
   }
   touch_end(integer num_detected){
       if(latch == "LOCKED") {
           llSay(0, txt_noentry);
       } else {
           avatar_uuid = llDetectedKey(0);
           state open;
       }
   }
   listen(integer channel, string name, key uuid, string msg) {
       if(DEBUG){llOwnerSay("closed::listen() key: " + uuid);}
       if(acl_check(uuid)) {
           string txt;
           if(msg == code_unlock){
               latch = "UNLOCKED";    
               txt   = txt_unlocked;
           } else if(msg == code_lock) {
   
               latch = "LOCKED";
               txt   = txt_locked;
           }
           llSay(0, txt);
       } else {
           
           llSay(0, txt_noacl);
       }
   }

}


state open {

   state_entry() {
       llSay(0, txt_welcome + llKey2Name(avatar_uuid));
   }
   touch_end(integer num_detected) {
       state closed;    
   }

   state_exit() {
       door_swing("CLOSE");
   }

}

</source>