99d575d054147ee051cad6d0f70c7a47206fcbaf
[feisty_meow.git] / huffware / huffotronic_scripts / puffer_v1.3.txt
1 
2 // huffware script: puffer, by fred huffhines
3 //
4 // causes the prim that this script is located in to enlarge periodically.  this is
5 // mostly concerned with physical objects that are autonomous and can get stuck in
6 // prims as they move around.  thus the script also tracks the current position and
7 // ensures that it keeps changing.
8 //
9 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
10 // do not use it in objects without fully realizing you are implicitly accepting that license.
11 //
12
13 // constants...
14
15 vector NORMAL_SIZE = <1.0, 1.0, 0.04>;  // normal size when we're not puffing.
16 vector PUFFED_SIZE = <1.0, 1.0, 1.0>;  // size that we blow out to when puffing up.
17
18 float PUFFING_PERIOD = 8.4;  // number of seconds between puff-ups.
19 float STAY_PUFFED_TIME = 1.4;  // number of seconds to remain puffed up.
20
21 float PANIC_HOP_DISTANCE = 1.0;  // meters to jump upwards if feeling stuck.
22 float MINIMAL_DISTANCE_OF_MOTION = 0.1;  // must move at least this much to be considered unstuck.
23
24 // variables...
25
26 vector last_position;  // tracks where the object is hanging out.
27
28 // sets the object size to a new value.  to do this, we need to turn off physics
29 // temporarily.
30 change_size(vector new_size)
31 {
32     llSetStatus(STATUS_PHYSICS, FALSE);
33     llSetPrimitiveParams([PRIM_SIZE, new_size]);
34     llSetStatus(STATUS_PHYSICS, TRUE);
35 }
36
37 // jumps upwards by a bit to try to escape fat prims we might get stuck inside of.
38 hop_up()
39 {
40     llSetStatus(STATUS_PHYSICS, FALSE);
41     llSetPos(<last_position.x, last_position.y, last_position.z + PANIC_HOP_DISTANCE>);
42     llSetStatus(STATUS_PHYSICS, TRUE);
43 }
44
45 // this is our whole process for puffing out here.  we will make sure to
46 // change size briefly for the puffing.  we also track the last place we
47 // were at when puffing and if it's too close to our current position,
48 // then we'll jump up a bit.
49 emulate_puffer_fish()
50 {
51     change_size(PUFFED_SIZE);
52     list siz = llGetPrimitiveParams([PRIM_SIZE]);
53 //llOwnerSay("size puffed=" + llList2String(siz, 0));
54     llSleep(STAY_PUFFED_TIME);  // snooze a bit.
55     change_size(NORMAL_SIZE);
56     siz = llGetPrimitiveParams([PRIM_SIZE]);
57 //llOwnerSay("size normal=" + llList2String(siz, 0));
58     if (llVecDist(last_position, llGetPos()) < MINIMAL_DISTANCE_OF_MOTION) {
59         // we seem to be stuck, so hop up a bit and see if that helps.
60         hop_up();
61     }
62     // reset our last position to where we are now.
63     last_position = llGetPos();
64 }
65
66 //////////////
67 // from hufflets...
68
69 //////////////
70 // huffware script: auto-retire, by fred huffhines, version 2.8.
71 // distributed under BSD-like license.
72 //   !!  keep in mind that this code must be *copied* into another
73 //   !!  script that you wish to add auto-retirement capability to.
74 // when a script has auto_retire in it, it can be dropped into an
75 // object and the most recent version of the script will destroy
76 // all older versions.
77 //
78 // the version numbers are embedded into the script names themselves.
79 // the notation for versions uses a letter 'v', followed by two numbers
80 // in the form "major.minor".
81 // major and minor versions are implicitly considered as a floating point
82 // number that increases with each newer version of the script.  thus,
83 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
84 // and "hazmap v3.2" is a more recent version.
85 //
86 // example usage of the auto-retirement script:
87 //     default {
88 //         state_entry() {
89 //            auto_retire();  // make sure newest addition is only version of script.
90 //        }
91 //     }
92 // this script is partly based on the self-upgrading scripts from markov brodsky
93 // and jippen faddoul.
94 //////////////
95 auto_retire() {
96     string self = llGetScriptName();  // the name of this script.
97     list split = compute_basename_and_version(self);
98     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
99     string basename = llList2String(split, 0);  // script name with no version attached.
100     string version_string = llList2String(split, 1);  // the version found.
101     integer posn;
102     // find any scripts that match the basename.  they are variants of this script.
103     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
104         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
105         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
106             // found a basic match at least.
107             list inv_split = compute_basename_and_version(curr_script);
108             if (llGetListLength(inv_split) == 2) {
109                 // see if this script is more ancient.
110                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
111                 // must make sure that the retiring script is completely the identical basename;
112                 // just matching in the front doesn't make it a relative.
113                 if ( (llList2String(inv_split, 0) == basename)
114                     && ((float)inv_version_string < (float)version_string) ) {
115                     // remove script with same name from inventory that has inferior version.
116                     llRemoveInventory(curr_script);
117                 }
118             }
119         }
120     }
121 }
122 //
123 // separates the base script name and version number.  used by auto_retire.
124 list compute_basename_and_version(string to_chop_up)
125 {
126     // minimum script name is 2 characters plus a version.
127     integer space_v_posn;
128     // find the last useful space and 'v' combo.
129     for (space_v_posn = llStringLength(to_chop_up) - 3;
130         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
131         space_v_posn--) {
132         // look for space and v but do nothing else.
133     }
134     if (space_v_posn < 2) return [];  // no space found.
135     // now we zoom through the stuff after our beloved v character and find any evil
136     // space characters, which are most likely from SL having found a duplicate item
137     // name and not so helpfully renamed it for us.
138     integer indy;
139     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
140         if (llGetSubString(to_chop_up, indy, indy) == " ") {
141             // found one; zap it.  since we're going backwards we don't need to
142             // adjust the loop at all.
143             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
144         }
145     }
146     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
147     // ditch the space character for our numerical check.
148     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
149     // strip out a 'v' if there is one.
150     if (llGetSubString(chop_suffix, 0, 0) == "v")
151         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
152     // if valid floating point number and greater than zero, that works for our version.
153     string basename = to_chop_up;  // script name with no version attached.
154     if ((float)chop_suffix > 0.0) {
155         // this is a big success right here.
156         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
157         return [ basename, chop_suffix ];
158     }
159     // seems like we found nothing useful.
160     return [];
161 }
162 //
163 //////////////
164
165 default
166 {
167     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
168     on_rez(integer parm) { state rerun; }
169 }
170 state rerun { state_entry() { state default; } }
171
172 state real_default
173 {
174     state_entry()
175     {
176         auto_retire();
177         last_position = llGetPos();
178         change_size(NORMAL_SIZE);
179         llSetTimerEvent(PUFFING_PERIOD);  // puff out this frequently.
180     }
181     
182     on_rez(integer parm) { llResetScript(); }
183
184     timer() { emulate_puffer_fish(); }
185 }
186