2 // fred huffhines mods:
4 // took away the universal skeleton key that was lodged in this script.
6 // stopped considering door's scale; this is not usually needed, plus we were blowing
7 // past the SL limit on object names.
9 // moved to only storing a couple digits after the decimal point; this is another
10 // crucial thing to limit the size of the object name.
12 // added a "toggle" command that behaves like touch, in that the door will be opened
13 // or closed based on current state.
15 // made the sensor distance required before the door will listen to someone into a
16 // configurable parameter, instead of the woefully tiny, hard-coded 5 meters.
18 // added debugging flag and switchable logging for debugging mode.
20 // *original license and author info below...*
22 // plus, timeless prototype said this about using the script in osgrid and elsewhere:
23 // "hi, thanks for asking, yes you may use the door script in other grids."
25 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
26 // do not use it in objects without fully realizing you are implicitly accepting that license.
28 // more fred huffhines mods: (circa march 2012)
29 // added PASS_COMMANDS flag, which can be used to pass along any commands we
30 // hear to anyone else on our same channel within the object.
31 // added stifling of commands heard so they don't get re-sent, causing endless
32 // loops of door openings.
35 //------------------------------------------------------
36 // Timeless Linked Door Script by Timeless Prototype
37 //------------------------------------------------------
38 // The latest version of this script can always be found
39 // in the Library section of the wiki:
40 // http://www.secondlife.com/badgeo/
41 // This script is free to use, but whereever it is used
42 // the SCRIPT's permissions MUST be set to:
43 // [x] Next owner can modify
44 // [x] Next owner can copy
45 // [x] Next owner can transfer
46 // [x] Allow anyone to copy
47 // [x] Share with group
49 //------------------------------------------------------
50 // USAGE INSTRUCTIONS FOR EVERYDAY USE:
51 //------------------------------------------------------
52 // Say the following commands on channel 0:
53 // 'unlock' - Unlocks all doors in range.
54 // 'lock' - Locks all doors in range and allows
55 // only the permitted users to open it.
56 // To open the door, either Touch it, Walk into it or
57 // say 'open' or say 'close'.
59 //------------------------------------------------------
60 // USAGE INSTRUCTIONS FOR BUILDERS:
61 //------------------------------------------------------
62 // 1. Copy and paste this script into the door prim and
63 // change the settings (see further down).
64 // 2. The door prim must be linked to at least one other
65 // prim (could be linked to the house for example).
66 // 3. The door prim MUST NOT be the root prim.
67 // 4. Use Edit Linked Parts to move, rotate and size the
68 // door prim for the closed state.
69 // 5. When ready, stand close to the door and say
70 // '/door closed' (this records the closed door
71 // position, rotation and size to the object's
72 // name and description).
73 // 6. Use the Edit Linked parts to move, rotate and size
74 // the door prim for the opened state.
75 // 7. When ready, stand close to the door and say
76 // '/door opened' (this records the opened door
77 // position, rotation and size).
78 // 8. Once recorded it will not accept these commands
79 // again. If you do need to redo the settings then
80 // delete the Name and Description of the door prim
81 // (these are where the position information is
82 // stored), and then follow the steps above again.
83 // Note: deleting the object name won't save, so set
84 // the object name to 'Object' to reset the object
87 //------------------------------------------------------
88 // Change these settings to suit your needs.
89 //------------------------------------------------------
90 // To mute any/all of the sounds set the sound string(s)
91 // to "" (empty string).
92 // To get the UUID of a sound, right click on the sound
93 // in your inventory and choose "Copy Asset UUID", then
94 // paste the UUID in here.
95 string doorOpenSound = "Door open";
96 string doorCloseSound = "Door close";
97 string confirmedSound = "69743cb2-e509-ed4d-4e52-e697dc13d7ac";
98 string accessDeniedSound = "58da0f9f-42e5-8a8f-ee51-4fac6c247c98";
99 string doorBellSound = ""; // Setting to empty stops door announcements too.
100 float autoCloseTime = 120.0; // 0 seconds to disable auto close.
101 integer allowGroupToo = TRUE; // Set to FALSE to disallow same group access to door.
102 list allowedAgentUUIDs = []; // Comma-separated, quoted list of avatar UUIDs who are allowed access to this door.
103 // was allowing this sneaky guy: "8efecbac-35de-4f40-89c1-2c772b83cafa"
104 integer listenChannel = 10106;
105 float RESPONSE_DISTANCE = 120.0; // how far to allow a command from users with permission.
106 integer DEBUGGING = FALSE;
107 integer PASS_COMMANDS = TRUE;
108 // if true, then we will order other doors to open when we do.
109 // we use the listenChannel as the id to pass commands on, so that only the doors listening to
110 // the same place will hear our commands.
111 integer command_is_a_response = FALSE;
112 // if this is true, then the door open and close must not re-echo their actions.
114 //------------------------------------------------------
115 // Leave the rest of the settings alone, these are
116 // handled by the script itself.
117 //------------------------------------------------------
118 integer isLocked = FALSE; // Only when the door is locked do the permissions apply.
119 integer isOpen = TRUE;
120 vector openPos = ZERO_VECTOR;
121 rotation openRot = ZERO_ROTATION;
122 vector closedPos = ZERO_VECTOR;
123 rotation closedRot = ZERO_ROTATION;
124 key openerKey = NULL_KEY;
125 key closerKey = NULL_KEY;
126 integer isSetup = FALSE;
127 integer listenHandle = 0;
128 string avatarName = "";
130 // zooms the sub-prim to a new rotation and position.
131 jump_to_position(vector position, rotation new_rot)
133 list config_blast = [
134 PRIM_SIZE, llGetScale(), ///????
135 // first jump away from where we started, trying to get past an opensim bug.
136 // PRIM_POSITION, ZERO_VECTOR,
137 // PRIM_POSITION, ZERO_VECTOR,
138 // PRIM_POSITION, ZERO_VECTOR,
139 // PRIM_POSITION, ZERO_VECTOR,
140 // PRIM_POSITION, ZERO_VECTOR,
141 PRIM_ROTATION, ZERO_ROTATION * new_rot / llGetRootRotation(),
142 // PRIM_POSITION, position,
143 // PRIM_POSITION, position,
144 // PRIM_POSITION, position,
145 // PRIM_POSITION, position,
146 // PRIM_POSITION, position,
147 PRIM_POSITION, position
149 llSetLinkPrimitiveParams(llGetLinkNumber(), config_blast);
150 if (DEBUGGING) llOwnerSay("want pos=" + (string)position + ", got=" + (string)llGetLocalPos()
151 + ", and want rot=" + (string)new_rot + ", got=" + (string)llGetLocalRot());
154 mySayName(integer channel, string objectName, string message)
156 string name = llGetObjectName();
157 llSetObjectName(objectName);
158 llSay(0, "/me " + message);
159 llSetObjectName(name);
162 mySay(integer channel, string message)
164 string name = llGetObjectName();
165 llSetObjectName("Door");
167 llSetObjectName(name);
170 myOwnerSay(string message)
172 string name = llGetObjectName();
173 llSetObjectName("Door");
175 llSetObjectName(name);
180 if (confirmedSound != "")
182 llTriggerSound(confirmedSound, 1.0);
186 mySoundAccessDenied()
188 if (accessDeniedSound != "")
190 llTriggerSound(accessDeniedSound, 1.0);
197 if (llSubStringIndex(llGetObjectDesc(), "D;") == 0 && llSubStringIndex(llGetObjectName(), "D;") == 0)
199 list nameWords = llParseString2List(llGetObjectName(), [";"], []);
200 list descWords = llParseString2List(llGetObjectDesc(), [";"], []);
201 if (llGetListLength(nameWords) != 3 || llGetListLength(descWords) != 3)
203 myOwnerSay("The door prim's name and/or description has invalid syntax and/or number of parameters. Delete the door prim's name and description and setup the door prim again.");
207 openPos = (vector)llList2String(nameWords, 1);
208 openRot = (rotation)llList2String(nameWords, 2);
209 closedPos = (vector)llList2String(descWords, 1);
210 closedRot = (rotation)llList2String(descWords, 2);
213 //llSay(0, "got open pos=" + (string)(openPos) + " rot=" + (string)(openRot));
214 //llSay(0, "got close pos=" + (string)(closedPos) + " rot=" + (string)(closedRot));
219 // if open_state is true, the parms are for an open door.
220 mySetDoorParams(integer open_state, vector Pos, rotation Rot)
223 // parms for open state.
224 llSetObjectName("D;" + vector_chop(Pos) + ";" + rotation_chop(Rot));
226 // parms for closed state.
227 llSetObjectDesc("D;" + vector_chop(Pos) + ";" + rotation_chop(Rot));
232 integer myPermissionCheck(key id)
234 integer hasPermission = FALSE;
235 if (isLocked == FALSE) {
236 if (DEBUGGING) llOwnerSay("perm--unlocked: okay");
237 hasPermission = TRUE;
238 } else if (llGetOwnerKey(id) == llGetOwner()) {
239 if (DEBUGGING) llOwnerSay("perm--is owner: okay");
240 hasPermission = TRUE;
241 } else if (allowGroupToo == TRUE && llSameGroup(id)) {
242 if (DEBUGGING) llOwnerSay("perm--same group: okay");
243 hasPermission = TRUE;
244 } else if (llListFindList(allowedAgentUUIDs, [(string)id]) != -1) {
245 if (DEBUGGING) llOwnerSay("perm--in list: okay");
246 hasPermission = TRUE;
248 if (DEBUGGING) llOwnerSay("perm--not found anywhere: bad perms");
250 return hasPermission;
267 if (isSetup == FALSE)
269 myOwnerSay("The door prim has not been configured yet. Please read the usage instructions in the door script.");
271 else if (llGetLinkNumber() == 0 || llGetLinkNumber() == 1)
273 myOwnerSay("The door prim must be linked to at least one other prim and the door prim must not be the root prim");
278 if (DEBUGGING) llOwnerSay("door open state is now=" + (string)isOpen);
281 if (DEBUGGING) llOwnerSay("opening the door.");
282 if (doorBellSound != "")
284 llTriggerSound(doorBellSound, 1.0);
285 if (avatarName != "")
287 mySayName(0, avatarName, "is at the door.");
291 if (doorOpenSound != "")
293 llTriggerSound(doorOpenSound, 1.0);
295 jump_to_position(openPos, openRot);
296 // list config_blast = [ PRIM_POSITION, llGetLocalPos() + <4, 4, 4>,
297 // PRIM_ROTATION, ZERO_ROTATION * openRot / llGetRootRotation(),
298 // PRIM_POSITION, openPos
299 //// PRIM_SIZE, llGetScale()
301 // llSetPrimitiveParams(config_blast);
302 //if (DEBUGGING) llOwnerSay("want pos=" + (string)openPos + ", got=" + (string)llGetLocalPos()
303 //+ ", and want rot=" + (string)openRot + ", got=" + (string)llGetLocalRot());
305 if (PASS_COMMANDS && !command_is_a_response) {
307 llMessageLinked(LINK_SET, listenChannel, "cmd|door|open", llGetKey());
309 command_is_a_response = FALSE; // took care of that one.
313 if (DEBUGGING) llOwnerSay("closing the door.");
314 if (doorCloseSound != "")
316 llTriggerSound(doorCloseSound, 1.0);
318 jump_to_position(closedPos, closedRot);
319 // list config_blast = [
320 // PRIM_ROTATION, ZERO_ROTATION * closedRot / llGetRootRotation(),
321 // PRIM_POSITION, closedPos
322 //// PRIM_SIZE, llGetScale()
324 // llSetPrimitiveParams(config_blast);
325 //if (DEBUGGING) llOwnerSay("want pos=" + (string)closedPos + ", got=" + (string)llGetLocalPos()
326 //+ ", and want rot=" + (string)closedRot + ", got=" + (string)llGetLocalRot());
327 if (PASS_COMMANDS && !command_is_a_response) {
329 llMessageLinked(LINK_SET, listenChannel, "cmd|door|close", llGetKey());
331 command_is_a_response = FALSE; // took care of that one.
334 llSetTimerEvent(0.0);
335 if (isOpen == TRUE && autoCloseTime != 0.0)
337 llSetTimerEvent(autoCloseTime);
345 // returns the index of the first occurrence of "pattern" inside
346 // the "full_string". if it is not found, then a negative number is returned.
347 integer find_substring(string full_string, string pattern)
348 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
350 // returns text for a floating point number, but includes only
351 // three digits after the decimal point.
352 string float_chop(float to_show)
354 integer mant = llAbs(llRound(to_show * 1000.0) / 1000);
356 if (to_show < 0.0) neg_sign = "-";
357 string dec_s = (string)((llRound(to_show * 1000.0) - mant * 1000) / 1000.0);
358 dec_s = llGetSubString(llGetSubString(dec_s, find_substring(dec_s, ".") + 1, -1), 0, 2);
359 // strip off all trailing zeros.
360 while (llGetSubString(dec_s, -1, -1) == "0")
361 dec_s = llDeleteSubString(dec_s, -1, -1);
362 string to_return = neg_sign + (string)mant;
363 if (llStringLength(dec_s)) to_return += "." + dec_s;
367 // returns a prettier form for vector text, with chopped floats.
368 string vector_chop(vector to_show)
370 return "<" + float_chop(to_show.x) + ","
371 + float_chop(to_show.y) + ","
372 + float_chop(to_show.z) + ">";
375 // similarly, for a rotation.
376 string rotation_chop(rotation to_show)
378 return "<" + float_chop(to_show.x) + ","
379 + float_chop(to_show.y) + ","
380 + float_chop(to_show.z) + ","
381 + float_chop(to_show.s) + ">";
385 // huffware script: auto-retire, by fred huffhines, version 2.5.
386 // distributed under BSD-like license.
387 // !! keep in mind that this code must be *copied* into another
388 // !! script that you wish to add auto-retirement capability to.
389 // when a script has auto_retire in it, it can be dropped into an
390 // object and the most recent version of the script will destroy
391 // all older versions.
393 // the version numbers are embedded into the script names themselves.
394 // the notation for versions uses a letter 'v', followed by two numbers
395 // in the form "major.minor".
396 // major and minor versions are implicitly considered as a floating point
397 // number that increases with each newer version of the script. thus,
398 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
399 // and "hazmap v3.2" is a more recent version.
401 // example usage of the auto-retirement script:
404 // auto_retire(); // make sure newest addition is only version of script.
407 // this script is partly based on the self-upgrading scripts from markov brodsky
408 // and jippen faddoul.
411 string self = llGetScriptName(); // the name of this script.
412 list split = compute_basename_and_version(self);
413 if (llGetListLength(split) != 2) return; // nothing to do for this script.
414 string basename = llList2String(split, 0); // script name with no version attached.
415 string version_string = llList2String(split, 1); // the version found.
417 // find any scripts that match the basename. they are variants of this script.
418 for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
419 //log_it("invpo=" + (string)posn);
420 string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
421 if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
422 // found a basic match at least.
423 list inv_split = compute_basename_and_version(curr_script);
424 if (llGetListLength(inv_split) == 2) {
425 // see if this script is more ancient.
426 string inv_version_string = llList2String(inv_split, 1); // the version found.
427 // must make sure that the retiring script is completely the identical basename;
428 // just matching in the front doesn't make it a relative.
429 if ( (llList2String(inv_split, 0) == basename)
430 && ((float)inv_version_string < (float)version_string) ) {
431 // remove script with same name from inventory that has inferior version.
432 llRemoveInventory(curr_script);
439 // separates the base script name and version number. used by auto_retire.
440 list compute_basename_and_version(string to_chop_up)
442 // minimum script name is 2 characters plus a version.
443 integer space_v_posn;
444 // find the last useful space and 'v' combo.
445 for (space_v_posn = llStringLength(to_chop_up) - 3;
446 (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
448 // look for space and v but do nothing else.
449 //log_it("pos=" + (string)space_v_posn);
451 if (space_v_posn < 2) return []; // no space found.
452 //log_it("space v@" + (string)space_v_posn);
453 // now we zoom through the stuff after our beloved v character and find any evil
454 // space characters, which are most likely from SL having found a duplicate item
455 // name and not so helpfully renamed it for us.
457 for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
458 //log_it("indy=" + (string)space_v_posn);
459 if (llGetSubString(to_chop_up, indy, indy) == " ") {
460 // found one; zap it. since we're going backwards we don't need to
461 // adjust the loop at all.
462 to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
463 //log_it("saw case of previously redundant item, aieee. flattened: " + to_chop_up);
466 string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
467 // ditch the space character for our numerical check.
468 string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
469 // strip out a 'v' if there is one.
470 if (llGetSubString(chop_suffix, 0, 0) == "v")
471 chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
472 // if valid floating point number and greater than zero, that works for our version.
473 string basename = to_chop_up; // script name with no version attached.
474 if ((float)chop_suffix > 0.0) {
475 // this is a big success right here.
476 basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
477 return [ basename, chop_suffix ];
479 // seems like we found nothing useful.
487 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
488 on_rez(integer parm) { state rerun; }
490 state rerun { state_entry() { state default; } }
497 listenHandle = llListen(listenChannel, "", NULL_KEY, "");
502 on_rez(integer parm) { llResetScript(); }
504 touch_start(integer total_number)
506 command_is_a_response = FALSE;
507 if (myPermissionCheck(llDetectedKey(0)) == TRUE)
509 avatarName = llDetectedName(0);
514 mySoundAccessDenied();
523 link_message(integer sender_num, integer num, string str, key id)
525 // Door API. The API is here in case you want to create PIN entry keypads or whatever.
526 if (num == listenChannel) {
527 if (id == llGetKey()) return; // don't listen to our own commands.
528 command_is_a_response = TRUE;
529 if (str == "cmd|door|open") myOpenDoor();
530 else if (str == "cmd|door|close") myCloseDoor();
531 else if (str == "cmd|door|discover")
532 llMessageLinked(LINK_SET, listenChannel, "cmd|door|discovered|" + (string)llGetKey(),
534 else command_is_a_response = FALSE;
535 //hmmm: above protocol seems redundant, but sending back the original id (like this did before)
536 // in the id field does not fit in with our usual schemes very well.
540 listen(integer channel, string name, key id, string message)
542 // if (DEBUGGING) llOwnerSay("heard: " + message);
543 command_is_a_response = FALSE; // don't get involved with the link message checking.
545 // Performance note: it's quicker to compare the strings than to compare permissions each time anyone says anything on this channel.
546 if (message == "open")
548 if (myPermissionCheck(id) == TRUE)
550 // Only open the door if the person is quite close to this door.
552 closerKey = NULL_KEY;
554 llSensor(name, id, AGENT, RESPONSE_DISTANCE, TWO_PI);
558 mySoundAccessDenied();
561 else if (message == "close")
563 if (myPermissionCheck(id) == TRUE)
565 openerKey = NULL_KEY;
568 // Only close the door if the person is quite close to this door.
569 llSensor(name, id, AGENT, RESPONSE_DISTANCE, TWO_PI);
573 mySoundAccessDenied();
576 else if (message == "lock")
578 if (myPermissionCheck(id) == TRUE)
585 mySoundAccessDenied();
588 else if (message == "unlock")
590 if (myPermissionCheck(id) == TRUE)
597 mySoundAccessDenied();
600 else if (message == "toggle")
602 if (myPermissionCheck(id) == TRUE)
609 mySoundAccessDenied();
612 else if (message == "/door opened" && llSubStringIndex(llGetObjectName(), "D;") == -1)
614 if (llGetOwnerKey(id) == llGetOwner())
617 openPos = llGetLocalPos();
618 openRot = llGetLocalRot();
620 mySetDoorParams(TRUE, openPos, openRot);
621 //llSay(0, "set open pos=" + (string)(openPos) + " rot=" + (string)(openRot));
625 mySoundAccessDenied();
628 else if (message == "/door closed" && llSubStringIndex(llGetObjectDesc(), "D;") == -1)
630 if (llGetOwnerKey(id) == llGetOwner())
633 closedPos = llGetLocalPos();
634 closedRot = llGetLocalRot();
636 mySetDoorParams(FALSE, closedPos, closedRot);
637 //llSay(0, "set close pos=" + (string)(closedPos) + " rot=" + (string)(closedRot));
641 mySoundAccessDenied();
646 sensor(integer num_detected)
648 if (openerKey != NULL_KEY)
651 for (i = 0; i < num_detected; i++)
653 if (llDetectedKey(i) == openerKey && myPermissionCheck(llDetectedKey(i)) == TRUE)
658 openerKey = NULL_KEY;
663 for (i = 0; i < num_detected; i++)
665 if (llDetectedKey(i) == closerKey && myPermissionCheck(llDetectedKey(i)) == TRUE)
670 closerKey = NULL_KEY;
674 //------------------------------------------------------
675 // Uncomment the following code if you particularly want
676 // collisions to affect the door state.
677 //------------------------------------------------------
679 // collision_start(integer num_detected)
682 // for (i = 0; i < num_detected; i++)
684 // if (myPermissionCheck(llDetectedKey(i)) == TRUE)
686 // avatarName = llDetectedName(i);
689 // else if (llDetectedType(i) & AGENT)
691 // mySoundAccessDenied();