normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / anti-idler_mojo_v1.4.txt
1 
2 // huffware script: anti-idler mojo, by fred huffhines
3 //
4 // original version was by kwipper manimal.  that script has been augmented with
5 // a better anti-idle animation and some other additions.
6 //
7 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
8 // do not use it in objects without fully realizing you are implicitly accepting that license.
9 //
10
11 float TIMER_INTERVAL = 28.0;  // number of seconds between checks on avatar health.
12
13 float JUMP_AROUND_INTERVAL = 241.0;  // how often (in seconds) to move around a bit.
14
15 float JUMP_AROUND_RUN_TIME = 4.0;  // number of seconds to do the animation.
16
17 integer last_jumped = 0;  // when did we last jump jump jump around.
18
19 integer animation_is_running = FALSE;  // is there an animation playing right now?
20
21 get_perms()
22 {
23     llRequestPermissions(llGetOwner(), PERMISSION_TRIGGER_ANIMATION);
24     llSetTimerEvent(TIMER_INTERVAL);  // always set a timer, so we can check on state.
25 }
26
27 //////////////
28 // huffware script: auto-retire, by fred huffhines, version 2.5.
29 // distributed under BSD-like license.
30 //   !!  keep in mind that this code must be *copied* into another
31 //   !!  script that you wish to add auto-retirement capability to.
32 // when a script has auto_retire in it, it can be dropped into an
33 // object and the most recent version of the script will destroy
34 // all older versions.
35 //
36 // the version numbers are embedded into the script names themselves.
37 // the notation for versions uses a letter 'v', followed by two numbers
38 // in the form "major.minor".
39 // major and minor versions are implicitly considered as a floating point
40 // number that increases with each newer version of the script.  thus,
41 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
42 // and "hazmap v3.2" is a more recent version.
43 //
44 // example usage of the auto-retirement script:
45 //     default {
46 //         state_entry() {
47 //            auto_retire();  // make sure newest addition is only version of script.
48 //        }
49 //     }
50 // this script is partly based on the self-upgrading scripts from markov brodsky
51 // and jippen faddoul.
52 //////////////
53 auto_retire() {
54     string self = llGetScriptName();  // the name of this script.
55     list split = compute_basename_and_version(self);
56     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
57     string basename = llList2String(split, 0);  // script name with no version attached.
58     string version_string = llList2String(split, 1);  // the version found.
59     integer posn;
60     // find any scripts that match the basename.  they are variants of this script.
61     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
62 //log_it("invpo=" + (string)posn);
63         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
64         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
65             // found a basic match at least.
66             list inv_split = compute_basename_and_version(curr_script);
67             if (llGetListLength(inv_split) == 2) {
68                 // see if this script is more ancient.
69                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
70                 // must make sure that the retiring script is completely the identical basename;
71                 // just matching in the front doesn't make it a relative.
72                 if ( (llList2String(inv_split, 0) == basename)
73                     && ((float)inv_version_string < (float)version_string) ) {
74                     // remove script with same name from inventory that has inferior version.
75                     llRemoveInventory(curr_script);
76                 }
77             }
78         }
79     }
80 }
81 //
82 // separates the base script name and version number.  used by auto_retire.
83 list compute_basename_and_version(string to_chop_up)
84 {
85     // minimum script name is 2 characters plus a version.
86     integer space_v_posn;
87     // find the last useful space and 'v' combo.
88     for (space_v_posn = llStringLength(to_chop_up) - 3;
89         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
90         space_v_posn--) {
91         // look for space and v but do nothing else.
92 //log_it("pos=" + (string)space_v_posn);
93     }
94     if (space_v_posn < 2) return [];  // no space found.
95 //log_it("space v@" + (string)space_v_posn);
96     // now we zoom through the stuff after our beloved v character and find any evil
97     // space characters, which are most likely from SL having found a duplicate item
98     // name and not so helpfully renamed it for us.
99     integer indy;
100     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
101 //log_it("indy=" + (string)space_v_posn);
102         if (llGetSubString(to_chop_up, indy, indy) == " ") {
103             // found one; zap it.  since we're going backwards we don't need to
104             // adjust the loop at all.
105             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
106 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
107         }
108     }
109     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
110     // ditch the space character for our numerical check.
111     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
112     // strip out a 'v' if there is one.
113     if (llGetSubString(chop_suffix, 0, 0) == "v")
114         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
115     // if valid floating point number and greater than zero, that works for our version.
116     string basename = to_chop_up;  // script name with no version attached.
117     if ((float)chop_suffix > 0.0) {
118         // this is a big success right here.
119         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
120         return [ basename, chop_suffix ];
121     }
122     // seems like we found nothing useful.
123     return [];
124 }
125 //
126 //////////////
127
128 default {
129     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
130     on_rez(integer parm) { state rerun; }
131 }
132 state rerun { state_entry() { state default; } }
133
134 state real_default
135 {
136     state_entry() {
137         get_perms();
138     }
139     
140     changed(integer change){
141         if (change & 128){
142 //ugly use of constant
143             llResetScript();
144         }
145     }
146     
147     attach(key id) {
148         if (id != NULL_KEY) {
149             get_perms();
150         }
151     }
152     
153     run_time_permissions(integer perm) {
154         if (perm & PERMISSION_TRIGGER_ANIMATION) {
155 ////not here.            llSetTimerEvent(TIMER_INTERVAL);
156             // anything we do want to do here?
157         }
158     }
159     
160     timer() {
161         if (llGetPermissions() & PERMISSION_TRIGGER_ANIMATION) {
162             // turn off the away animation, just in case.
163             llStopAnimation("away");
164             // were we doing an animation in the previous loop cycle?
165             if (animation_is_running) {
166                 animation_is_running = FALSE;
167                 llStopAnimation(llGetInventoryName(INVENTORY_ANIMATION, 0));
168                 // back to normal timing now.
169                 llSetTimerEvent(TIMER_INTERVAL);
170             }
171             if (llAbs(last_jumped - llGetUnixTime()) >= JUMP_AROUND_INTERVAL) {
172                 // time to jump around with our expected animation.
173                 // hopefully this makes the avatar seem more awake to sl.
174                 if (llGetInventoryNumber(INVENTORY_ANIMATION) > 0) {
175 //llOwnerSay("found animation to play: " + llGetInventoryName(INVENTORY_ANIMATION, 0));
176                     llStartAnimation(llGetInventoryName(INVENTORY_ANIMATION, 0));
177                 }
178                 last_jumped = llGetUnixTime();
179                 animation_is_running = TRUE;
180                 // only let the animation play for our specified timing.
181                 llSetTimerEvent(JUMP_AROUND_RUN_TIME);
182             }
183         } else {
184 llOwnerSay("croaking in timer because we don't have animation permissions; resetting.");
185             llResetScript();            
186         }
187     }
188 }
189