2 // huffware script: stay at home, by fred huffhines
4 // this script attempts to keep an object at home, which is to say, near where
5 // it was rezzed. it is not perfect, and if the object quickly crosses a region
6 // boundary, the object may get away. there is an automatic termination option
7 // for cases where the object is taken away and cannot get back; otherwise the
8 // object will bleat its position out at regular intevals.
9 // this script mainly makes sense for physical objects, since non-physical
10 // objects are not as prone to being pushed around by other avatars. in addition,
11 // it really makes the most sense for an object that allows anyone to move it.
13 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
14 // do not use it in objects without fully realizing you are implicitly accepting that license.
17 // constants to be adjusted as needed...
19 float MAXIMUM_ROAMING_RANGE = 12.0;
20 // this is the farthest that we let them drag our object away, in meters.
22 integer AUTO_TERMINATE = FALSE;
23 // if the object crosses a region boundary (exits sim, enters new one), then
24 // it will zap itself if this flag is set to true. it will also go poof if
25 // it reaches escape velocity and travels off world.
27 integer KEEP_IT_PHYSICAL = TRUE;
28 // if this is true, then the object's state as physical is continually
31 integer ROTATE_TO_TARGET = FALSE;
32 // if this is true, the object will try to rotate to a set position.
34 integer ONLY_ROTATE_FIXED_AXES = FALSE;
35 // if this is true and rotate to target is true, then we will only try to
36 // set the rotation on the axes that are specified as "fixed" below.
38 vector TARGETED_ROTATION = <0.0, 0.0, 0.0>;
39 // if the control rotation flag is true, the object will try to reattain
40 // this value for its rotation.
42 // if these are set to true, the no rotation is allowed around the axis.
43 // note that this decision is totally independent of whether the object has
44 // a targeted rotation or not.
45 integer FIXATE_X_AXIS = FALSE;
46 integer FIXATE_Y_AXIS = FALSE;
47 integer FIXATE_Z_AXIS = FALSE;
49 integer MAXIMUM_FAILED_JUMPS = 200;
50 // if we are having to make this many attempts to get home, something is
51 // really wrong. treat it like leaving the region; totally awol.
53 float POSITION_CHECKING_INTERVAL = 8.0;
54 // how frequently the object will check where it is, in seconds.
56 float PANIC_EVERY_N_HOURS = 4.0;
57 // this is the number of hours between panic mewling at the owner, if the object
58 // did not get told to self-terminate upon leaving the region.
60 integer DEBUGGING = FALSE;
61 // if this is true, then the object will tell about it's problems when it
62 // goes off world or into another sim. note that an object that is not
63 // set to auto-terminate is considered important enough to tell the owner;
64 // if you don't want to hear from lost objects, always turn on auto-terminate
65 // and turn off debugging.
67 // these mysterious values control how the object rotates to its target.
68 float ROT_STRENGTH = 1.0;
69 float ROT_DAMPING = 0.25;
70 //these values don't work that great, but neither did any other ones.
71 // the rotlookat stuff seems overpowered by the physical nature of an object.
73 // global variables...
76 // where the object starts out inside the sim. we will try to get back
77 // to here if we exceed our maximum distance from home.
80 // which sim we start out in, in terms of the region corners. if this changes,
81 // the object has left the sim.
83 integer homeward_jump_attempts = 0;
84 // tracks how many jumps home we've tried to make.
86 integer last_alerted = 0;
87 // when the script last told the user about having gone missing.
88 // if this is zero, we presumably haven't been lost recently.
90 // requires jaunting library v10.5 or greater.
92 // do not redefine these constants.
93 integer JAUNT_HUFFWARE_ID = 10008;
94 // the unique id within the huffware system for the jaunt script to
95 // accept commands on. this is used in llMessageLinked as the num parameter.
96 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
97 // this pattern is an uncommon thing to see in text, so we use it to separate
98 // our commands in link messages.
99 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
100 // used to separate lists of items from each other when stored inside a parameter.
101 // this allows lists to be passed as single string parameters if needed.
102 integer REPLY_DISTANCE = 100008; // offset added to service's huffware id in reply IDs.
104 // commands available via the jaunting library:
105 string JAUNT_COMMAND = "#jaunt#";
106 // command used to tell jaunt script to move object. pass a vector with the location.
107 string JAUNT_LIST_COMMAND = "#jauntlist#";
108 // like regular jaunt, but expects a list of vectors as the first parameter; this list
109 // should be in the jaunter notecard format (separated by pipe characters).
110 // the second parameter, if any, should be 1 for forwards traversal and 0 for backwards.
113 // joins a list of parameters using the parameter sentinel for the library.
114 string wrap_parameters(list to_flatten)
115 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
117 // encases a list of vectors in the expected character for the jaunting library.
118 string wrap_vector_list(list to_wrap)
119 { return llDumpList2String(to_wrap, HUFFWARE_ITEM_SEPARATOR); }
122 // asks the jaunting library to take us to the target using a list of waypoints.
123 request_jaunt(list full_journey, integer forwards)
125 // jaunt_responses_awaited++;
126 llMessageLinked(LINK_THIS, JAUNT_HUFFWARE_ID, JAUNT_LIST_COMMAND,
127 wrap_vector_list(full_journey) + HUFFWARE_PARM_SEPARATOR + (string)forwards);
130 // jaunts back to our home location.
133 // jump back to home.
134 request_jaunt([llGetPos(), home_place], TRUE);
137 // we panic at the user this frequently, in seconds.
138 float panic_interval() { return PANIC_EVERY_N_HOURS * 60.0 * 60.0; }
141 // huffware script: auto-retire, by fred huffhines, version 2.5.
142 // distributed under BSD-like license.
143 // !! keep in mind that this code must be *copied* into another
144 // !! script that you wish to add auto-retirement capability to.
145 // when a script has auto_retire in it, it can be dropped into an
146 // object and the most recent version of the script will destroy
147 // all older versions.
149 // the version numbers are embedded into the script names themselves.
150 // the notation for versions uses a letter 'v', followed by two numbers
151 // in the form "major.minor".
152 // major and minor versions are implicitly considered as a floating point
153 // number that increases with each newer version of the script. thus,
154 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
155 // and "hazmap v3.2" is a more recent version.
157 // example usage of the auto-retirement script:
160 // auto_retire(); // make sure newest addition is only version of script.
163 // this script is partly based on the self-upgrading scripts from markov brodsky
164 // and jippen faddoul.
167 string self = llGetScriptName(); // the name of this script.
168 list split = compute_basename_and_version(self);
169 if (llGetListLength(split) != 2) return; // nothing to do for this script.
170 string basename = llList2String(split, 0); // script name with no version attached.
171 string version_string = llList2String(split, 1); // the version found.
173 // find any scripts that match the basename. they are variants of this script.
174 for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
175 //log_it("invpo=" + (string)posn);
176 string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
177 if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
178 // found a basic match at least.
179 list inv_split = compute_basename_and_version(curr_script);
180 if (llGetListLength(inv_split) == 2) {
181 // see if this script is more ancient.
182 string inv_version_string = llList2String(inv_split, 1); // the version found.
183 // must make sure that the retiring script is completely the identical basename;
184 // just matching in the front doesn't make it a relative.
185 if ( (llList2String(inv_split, 0) == basename)
186 && ((float)inv_version_string < (float)version_string) ) {
187 // remove script with same name from inventory that has inferior version.
188 llRemoveInventory(curr_script);
195 // separates the base script name and version number. used by auto_retire.
196 list compute_basename_and_version(string to_chop_up)
198 // minimum script name is 2 characters plus a version.
199 integer space_v_posn;
200 // find the last useful space and 'v' combo.
201 for (space_v_posn = llStringLength(to_chop_up) - 3;
202 (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
204 // look for space and v but do nothing else.
205 //log_it("pos=" + (string)space_v_posn);
207 if (space_v_posn < 2) return []; // no space found.
208 //log_it("space v@" + (string)space_v_posn);
209 // now we zoom through the stuff after our beloved v character and find any evil
210 // space characters, which are most likely from SL having found a duplicate item
211 // name and not so helpfully renamed it for us.
213 for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
214 //log_it("indy=" + (string)space_v_posn);
215 if (llGetSubString(to_chop_up, indy, indy) == " ") {
216 // found one; zap it. since we're going backwards we don't need to
217 // adjust the loop at all.
218 to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
219 //log_it("saw case of previously redundant item, aieee. flattened: " + to_chop_up);
222 string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
223 // ditch the space character for our numerical check.
224 string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
225 // strip out a 'v' if there is one.
226 if (llGetSubString(chop_suffix, 0, 0) == "v")
227 chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
228 // if valid floating point number and greater than zero, that works for our version.
229 string basename = to_chop_up; // script name with no version attached.
230 if ((float)chop_suffix > 0.0) {
231 // this is a big success right here.
232 basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
233 return [ basename, chop_suffix ];
235 // seems like we found nothing useful.
243 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
244 on_rez(integer parm) { state rerun; }
246 state rerun { state_entry() { state default; } }
251 auto_retire(); // make sure newest addition is only version of script.
252 // we just woke up here. remember where we started out.
253 home_place = llGetPos();
254 home_region = llGetRegionCorner();
255 if (AUTO_TERMINATE) {
256 // make the object go kerflooey if it leaves the world entirely.
257 llSetStatus(STATUS_DIE_AT_EDGE, TRUE);
259 // disallow rotation if we were requested to.
260 llSetStatus(STATUS_ROTATE_X, !FIXATE_X_AXIS);
261 llSetStatus(STATUS_ROTATE_Y, !FIXATE_Y_AXIS);
262 llSetStatus(STATUS_ROTATE_Z, !FIXATE_Z_AXIS);
264 if (ROTATE_TO_TARGET) {
265 //hmmm: maybe still offer gradual form as an option also?
267 // set our rotational target to the configured position.
268 // llRotTarget(llEuler2Rot(TARGETED_ROTATION), 0.01);
269 // if we're supposed to rotate, we need to start that movement
270 // towards our target rotation.
271 // llRotLookAt(llEuler2Rot(TARGETED_ROTATION), ROT_STRENGTH, ROT_DAMPING);
273 // start the timer that checks on our position.
274 llSetTimerEvent(POSITION_CHECKING_INTERVAL);
277 on_rez(integer parm) {
278 llResetScript(); // a cop-out; just reroutes rezzing into a script reset.
283 if (ROTATE_TO_TARGET) {
284 vector real_rot_target = TARGETED_ROTATION;
285 vector curr_rot = llRot2Euler(llGetRot());
287 if (ONLY_ROTATE_FIXED_AXES) {
288 // set our rotation to the target, but only for fixed axes.
289 if (!FIXATE_X_AXIS) real_rot_target.x = curr_rot.x;
290 if (!FIXATE_Y_AXIS) real_rot_target.y = curr_rot.y;
291 if (!FIXATE_Z_AXIS) real_rot_target.z = curr_rot.z;
294 // see if we even need to do any rotation.
295 if (curr_rot != real_rot_target) {
296 // yes, we need to move back to the preferred rotation for at least one axis.
297 llSetStatus(STATUS_PHYSICS, FALSE); // physics property gets fixed in next block.
298 llSetRot(llEuler2Rot(real_rot_target));
302 if (KEEP_IT_PHYSICAL) {
303 // remind the object that it's physical, or set it if it wasn't. sometimes
304 // a physical object can get jinxed when bouncing into other people's lands
305 // and lose its status as a physical object. we correct for that here.
306 llSetStatus(STATUS_PHYSICS, TRUE);
309 integer time_to_panic = FALSE; // set to true if we should panic now.
311 // check whether the object is still in the appropriate region.
312 integer region_okay = (home_region == llGetRegionCorner());
314 // check to see if we're watching for a specific time.
315 if (last_alerted != 0) {
316 // ooh, we've already hit panicky conditions in the past. let's see if
317 // it's time to tell the owner about it.
318 if (panic_interval() <= llAbs(llGetUnixTime() - last_alerted)) {
319 // time to tell them about this, oh yes.
320 time_to_panic = TRUE;
323 // evaluate whether we are in the right condition (right sim and
324 // without being boxed in).
325 if (!region_okay || (homeward_jump_attempts > MAXIMUM_FAILED_JUMPS)) {
326 // this is not a good situation. we will now set the last alerted
327 // timer so as to force a panic message next time through the timer
329 last_alerted = llAbs(llGetUnixTime() - (integer)panic_interval() - 1000);
330 // decide when to fire the timer next.
331 if (AUTO_TERMINATE) {
332 llSetTimerEvent(0.1); // fire very soon to zap.
334 llSetTimerEvent(POSITION_CHECKING_INTERVAL * 5);
335 // slow the checking down a bit, since we don't expect to
336 // magically get back to our sim (although it could happen!).
341 // time to panic just means that we should tell the owner about our problematic
342 // location, or auto-terminate if configured that way.
344 // well, we've got to say something about this dire situation.
345 string reason = "trapped by unfriendly turf";
346 if (!region_okay) reason = "crossed region boundary";
347 if (AUTO_TERMINATE) {
348 // enforce the rules; we left the building and we have no ticket back.
350 llOwnerSay(reason + " -- object death -- was in "
351 + llGetRegionName() + " at " + (string)llGetPos());
355 // whew, we ducked that deadly issue. let the owner know that we
356 // don't like this place.
357 llOwnerSay(reason + "; please come get me:\nlast in "
358 + llGetRegionName() + " at " + (string)llGetPos());
359 last_alerted = llGetUnixTime(); // reset, since we just alerted.
360 return; // don't bother trying to move anywhere.
363 if (region_okay && (last_alerted != 0) ) {
364 // hmmm, did we recover our position?
365 if (homeward_jump_attempts <= MAXIMUM_FAILED_JUMPS) {
366 // we didn't expend too many jumps, so try going home again.
368 // also go back to our normal timing cycle.
369 llSetTimerEvent(POSITION_CHECKING_INTERVAL);
373 if (!region_okay || (homeward_jump_attempts > MAXIMUM_FAILED_JUMPS)
374 || (last_alerted != 0) ) {
375 // we said our piece, if any. we can't be fuddling around here.
379 // we're still in the right region, so check how far we are from the starting
380 // place. we'll try to move back if someone dragged us too far away from it.
381 float distance = llVecDist(llGetPos(), home_place);
382 if (distance > MAXIMUM_ROAMING_RANGE) {
383 // this is not good; we need to get back to our starting point.
384 attempt_to_go_home();
385 homeward_jump_attempts++;
387 // reset our counter, since we are close enough to home.
388 homeward_jump_attempts = 0;