normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / create_objects_v1.3.txt
1 
2 // huffware script: create object, by fred huffhines.
3 //
4 // this script rezzes new objects from inventory every time the user clicks.
5 //
6 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
7 // do not use it in objects without fully realizing you are implicitly accepting that license.
8 //
9
10 // from hufflets:
11
12 // returns a number at most "maximum" and at least "minimum".
13 // if "allow_negative" is TRUE, then the return may be positive or negative.
14 float randomize_within_range(float minimum, float maximum, integer allow_negative)
15 {
16     if (minimum > maximum) {
17         // flip the two if they are reversed.
18         float temp = minimum; minimum = maximum; maximum = temp;
19     }
20     float to_return = minimum + llFrand(maximum - minimum);
21     if (allow_negative) {
22         if (llFrand(1.0) < 0.5) to_return *= -1.0;
23     }
24     return to_return;
25 }
26
27 // returns a random vector where x,y,z will be between "minimums" and "maximums"
28 // x,y,z components.  if "allow_negative" is true, then any component will
29 // randomly be negative or positive.
30 vector random_bound_vector(vector minimums, vector maximums, integer allow_negative)
31 {
32     return <randomize_within_range(minimums.x, maximums.x, allow_negative),
33         randomize_within_range(minimums.y, maximums.y, allow_negative),
34         randomize_within_range(minimums.z, maximums.z, allow_negative)>;
35 }
36
37 // returns a vector whose components are between minimum and maximum.
38 // if allow_negative is true, then they can be either positive or negative.
39 vector random_vector(float minimum, float maximum, integer allow_negative)
40 {
41     return random_bound_vector(<minimum, minimum, minimum>,
42         <maximum, maximum, maximum>, allow_negative);
43 }
44
45 //////////////
46 // huffware script: auto-retire, by fred huffhines, version 2.5.
47 // distributed under BSD-like license.
48 //   !!  keep in mind that this code must be *copied* into another
49 //   !!  script that you wish to add auto-retirement capability to.
50 // when a script has auto_retire in it, it can be dropped into an
51 // object and the most recent version of the script will destroy
52 // all older versions.
53 //
54 // the version numbers are embedded into the script names themselves.
55 // the notation for versions uses a letter 'v', followed by two numbers
56 // in the form "major.minor".
57 // major and minor versions are implicitly considered as a floating point
58 // number that increases with each newer version of the script.  thus,
59 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
60 // and "hazmap v3.2" is a more recent version.
61 //
62 // example usage of the auto-retirement script:
63 //     default {
64 //         state_entry() {
65 //            auto_retire();  // make sure newest addition is only version of script.
66 //        }
67 //     }
68 // this script is partly based on the self-upgrading scripts from markov brodsky
69 // and jippen faddoul.
70 //////////////
71 auto_retire() {
72     string self = llGetScriptName();  // the name of this script.
73     list split = compute_basename_and_version(self);
74     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
75     string basename = llList2String(split, 0);  // script name with no version attached.
76     string version_string = llList2String(split, 1);  // the version found.
77     integer posn;
78     // find any scripts that match the basename.  they are variants of this script.
79     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
80 //log_it("invpo=" + (string)posn);
81         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
82         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
83             // found a basic match at least.
84             list inv_split = compute_basename_and_version(curr_script);
85             if (llGetListLength(inv_split) == 2) {
86                 // see if this script is more ancient.
87                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
88                 // must make sure that the retiring script is completely the identical basename;
89                 // just matching in the front doesn't make it a relative.
90                 if ( (llList2String(inv_split, 0) == basename)
91                     && ((float)inv_version_string < (float)version_string) ) {
92                     // remove script with same name from inventory that has inferior version.
93                     llRemoveInventory(curr_script);
94                 }
95             }
96         }
97     }
98 }
99 //
100 // separates the base script name and version number.  used by auto_retire.
101 list compute_basename_and_version(string to_chop_up)
102 {
103     // minimum script name is 2 characters plus a version.
104     integer space_v_posn;
105     // find the last useful space and 'v' combo.
106     for (space_v_posn = llStringLength(to_chop_up) - 3;
107         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
108         space_v_posn--) {
109         // look for space and v but do nothing else.
110 //log_it("pos=" + (string)space_v_posn);
111     }
112     if (space_v_posn < 2) return [];  // no space found.
113 //log_it("space v@" + (string)space_v_posn);
114     // now we zoom through the stuff after our beloved v character and find any evil
115     // space characters, which are most likely from SL having found a duplicate item
116     // name and not so helpfully renamed it for us.
117     integer indy;
118     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
119 //log_it("indy=" + (string)space_v_posn);
120         if (llGetSubString(to_chop_up, indy, indy) == " ") {
121             // found one; zap it.  since we're going backwards we don't need to
122             // adjust the loop at all.
123             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
124 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
125         }
126     }
127     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
128     // ditch the space character for our numerical check.
129     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
130     // strip out a 'v' if there is one.
131     if (llGetSubString(chop_suffix, 0, 0) == "v")
132         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
133     // if valid floating point number and greater than zero, that works for our version.
134     string basename = to_chop_up;  // script name with no version attached.
135     if ((float)chop_suffix > 0.0) {
136         // this is a big success right here.
137         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
138         return [ basename, chop_suffix ];
139     }
140     // seems like we found nothing useful.
141     return [];
142 }
143 //
144 //////////////
145
146 // end of hufflets.
147
148 default
149 {
150     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
151     on_rez(integer parm) { state rerun; }
152 }
153 state rerun { state_entry() { state default; } }
154
155 state real_default
156 {
157     state_entry() {
158         auto_retire();
159     }
160
161     touch_start(integer total_number) {
162         if (llDetectedKey(0) != llGetOwner()) return;
163         if (llGetInventoryNumber(INVENTORY_OBJECT) < 1) {
164             llSay(0, "This object needs contents to rez; currently it has none.");
165             return;
166         }
167         integer i;
168         for (i = 0; i < llGetInventoryNumber(INVENTORY_OBJECT); i++) {
169             vector addition = random_vector(0.2, 2.0, TRUE);
170             // only allow a new z position that is higher than current value.
171             if (addition.z < 0.0) addition.z *= -1.0;
172             llRezObject(llGetInventoryName(INVENTORY_OBJECT, i), llGetPos() + addition, ZERO_VECTOR, ZERO_ROTATION, 0);
173         }
174     }
175 }
176