76cbc3aa67342686be4df893e0149ceadc606c33
[feisty_meow.git] / huffware / huffotronic_scripts / change_click_action_v1.0.txt
1 
2 // huffware script: change click action, by fred huffhines.
3 //
4 // a simple script for sub-prims in an object where you want the whole object's
5 // click action to change.
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 // change click action's link message API...
12 //////////////
13 integer CHANGE_CLICK_ACTION_HUFFWARE_ID = 10024;
14     // a unique ID within the huffware system for this script.
15 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
16     // this pattern is an uncommon thing to see in text, so we use it to separate
17     // our commands in link messages.
18 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
19     // used to separate lists of items from each other when stored inside a parameter.
20     // this allows lists to be passed as single string parameters if needed.
21 //////////////
22 //
23 // the API only supports the service number; there are no commands to pass in the "msg"
24 // parameter.  the id does accept a parameter, which is the type of click action to begin
25 // using for this prim.
26 //
27 //////////////
28
29 //////////////
30 // huffware script: auto-retire, by fred huffhines, version 2.5.
31 // distributed under BSD-like license.
32 //   !!  keep in mind that this code must be *copied* into another
33 //   !!  script that you wish to add auto-retirement capability to.
34 // when a script has auto_retire in it, it can be dropped into an
35 // object and the most recent version of the script will destroy
36 // all older versions.
37 //
38 // the version numbers are embedded into the script names themselves.
39 // the notation for versions uses a letter 'v', followed by two numbers
40 // in the form "major.minor".
41 // major and minor versions are implicitly considered as a floating point
42 // number that increases with each newer version of the script.  thus,
43 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
44 // and "hazmap v3.2" is a more recent version.
45 //
46 // example usage of the auto-retirement script:
47 //     default {
48 //         state_entry() {
49 //            auto_retire();  // make sure newest addition is only version of script.
50 //        }
51 //     }
52 // this script is partly based on the self-upgrading scripts from markov brodsky
53 // and jippen faddoul.
54 //////////////
55 auto_retire() {
56     string self = llGetScriptName();  // the name of this script.
57     list split = compute_basename_and_version(self);
58     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
59     string basename = llList2String(split, 0);  // script name with no version attached.
60     string version_string = llList2String(split, 1);  // the version found.
61     integer posn;
62     // find any scripts that match the basename.  they are variants of this script.
63     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
64 //log_it("invpo=" + (string)posn);
65         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
66         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
67             // found a basic match at least.
68             list inv_split = compute_basename_and_version(curr_script);
69             if (llGetListLength(inv_split) == 2) {
70                 // see if this script is more ancient.
71                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
72                 // must make sure that the retiring script is completely the identical basename;
73                 // just matching in the front doesn't make it a relative.
74                 if ( (llList2String(inv_split, 0) == basename)
75                     && ((float)inv_version_string < (float)version_string) ) {
76                     // remove script with same name from inventory that has inferior version.
77                     llRemoveInventory(curr_script);
78                 }
79             }
80         }
81     }
82 }
83 //
84 // separates the base script name and version number.  used by auto_retire.
85 list compute_basename_and_version(string to_chop_up)
86 {
87     // minimum script name is 2 characters plus a version.
88     integer space_v_posn;
89     // find the last useful space and 'v' combo.
90     for (space_v_posn = llStringLength(to_chop_up) - 3;
91         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
92         space_v_posn--) {
93         // look for space and v but do nothing else.
94 //log_it("pos=" + (string)space_v_posn);
95     }
96     if (space_v_posn < 2) return [];  // no space found.
97 //log_it("space v@" + (string)space_v_posn);
98     // now we zoom through the stuff after our beloved v character and find any evil
99     // space characters, which are most likely from SL having found a duplicate item
100     // name and not so helpfully renamed it for us.
101     integer indy;
102     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
103 //log_it("indy=" + (string)space_v_posn);
104         if (llGetSubString(to_chop_up, indy, indy) == " ") {
105             // found one; zap it.  since we're going backwards we don't need to
106             // adjust the loop at all.
107             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
108 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
109         }
110     }
111     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
112     // ditch the space character for our numerical check.
113     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
114     // strip out a 'v' if there is one.
115     if (llGetSubString(chop_suffix, 0, 0) == "v")
116         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
117     // if valid floating point number and greater than zero, that works for our version.
118     string basename = to_chop_up;  // script name with no version attached.
119     if ((float)chop_suffix > 0.0) {
120         // this is a big success right here.
121         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
122         return [ basename, chop_suffix ];
123     }
124     // seems like we found nothing useful.
125     return [];
126 }
127 //
128 //////////////
129
130 default
131 {
132     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
133     on_rez(integer parm) { state rerun; }
134 }
135 state rerun { state_entry() { state default; } }
136
137 state real_default
138 {
139     state_entry() {
140         auto_retire();
141     }
142
143     link_message(integer which, integer num, string msg, key id)
144     {
145         // we only allow this action change on sub-prims.
146         if ( (num == CHANGE_CLICK_ACTION_HUFFWARE_ID) && (llGetLinkNumber() != 1) ) {
147             llSetClickAction((integer)((string)id));
148         }
149     }
150 }
151