406a998c06f372ea14c2df1bc0ab735634cba05a
[feisty_meow.git] / huffware / huffotronic_scripts / radio_tuner_v1.2.txt
1 
2 // huffware script: radio tuner, by fred huffhines
3 //
4 // a radio tuner with capability for overriding the channel.
5 // this is useful if you want it to stay tuned on a particular channel.
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 integer force_channel = 0;  // set to non-zero to force the channel to stay set.
12 integer channel_stays_on = 69;  // the channel that the radio will stay stuck on when forced.
13
14 integer listen_handle = 0;
15 integer channel = 0;
16
17 //hmmm: really broken implementation.
18 //  need to support multiple channels.
19 set_radio_channel(integer chan)
20 {
21     channel = chan;
22  
23     if (listen_handle)
24         llListenRemove(listen_handle);
25  
26     if (!chan)
27     {
28         listen_handle = 0;
29         llOwnerSay("Radio turned off.");
30         return;
31     }
32  
33     listen_handle = llListen(chan, "", NULL_KEY, "");
34     llOwnerSay("Listening to channel " + (string)chan);
35 }
36
37 //////////////
38 // huffware script: auto-retire, by fred huffhines, version 2.5.
39 // distributed under BSD-like license.
40 //   !!  keep in mind that this code must be *copied* into another
41 //   !!  script that you wish to add auto-retirement capability to.
42 // when a script has auto_retire in it, it can be dropped into an
43 // object and the most recent version of the script will destroy
44 // all older versions.
45 //
46 // the version numbers are embedded into the script names themselves.
47 // the notation for versions uses a letter 'v', followed by two numbers
48 // in the form "major.minor".
49 // major and minor versions are implicitly considered as a floating point
50 // number that increases with each newer version of the script.  thus,
51 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
52 // and "hazmap v3.2" is a more recent version.
53 //
54 // example usage of the auto-retirement script:
55 //     default {
56 //         state_entry() {
57 //            auto_retire();  // make sure newest addition is only version of script.
58 //        }
59 //     }
60 // this script is partly based on the self-upgrading scripts from markov brodsky
61 // and jippen faddoul.
62 //////////////
63 auto_retire() {
64     string self = llGetScriptName();  // the name of this script.
65     list split = compute_basename_and_version(self);
66     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
67     string basename = llList2String(split, 0);  // script name with no version attached.
68     string version_string = llList2String(split, 1);  // the version found.
69     integer posn;
70     // find any scripts that match the basename.  they are variants of this script.
71     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
72 //log_it("invpo=" + (string)posn);
73         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
74         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
75             // found a basic match at least.
76             list inv_split = compute_basename_and_version(curr_script);
77             if (llGetListLength(inv_split) == 2) {
78                 // see if this script is more ancient.
79                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
80                 // must make sure that the retiring script is completely the identical basename;
81                 // just matching in the front doesn't make it a relative.
82                 if ( (llList2String(inv_split, 0) == basename)
83                     && ((float)inv_version_string < (float)version_string) ) {
84                     // remove script with same name from inventory that has inferior version.
85                     llRemoveInventory(curr_script);
86                 }
87             }
88         }
89     }
90 }
91 //
92 // separates the base script name and version number.  used by auto_retire.
93 list compute_basename_and_version(string to_chop_up)
94 {
95     // minimum script name is 2 characters plus a version.
96     integer space_v_posn;
97     // find the last useful space and 'v' combo.
98     for (space_v_posn = llStringLength(to_chop_up) - 3;
99         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
100         space_v_posn--) {
101         // look for space and v but do nothing else.
102 //log_it("pos=" + (string)space_v_posn);
103     }
104     if (space_v_posn < 2) return [];  // no space found.
105 //log_it("space v@" + (string)space_v_posn);
106     // now we zoom through the stuff after our beloved v character and find any evil
107     // space characters, which are most likely from SL having found a duplicate item
108     // name and not so helpfully renamed it for us.
109     integer indy;
110     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
111 //log_it("indy=" + (string)space_v_posn);
112         if (llGetSubString(to_chop_up, indy, indy) == " ") {
113             // found one; zap it.  since we're going backwards we don't need to
114             // adjust the loop at all.
115             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
116 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
117         }
118     }
119     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
120     // ditch the space character for our numerical check.
121     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
122     // strip out a 'v' if there is one.
123     if (llGetSubString(chop_suffix, 0, 0) == "v")
124         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
125     // if valid floating point number and greater than zero, that works for our version.
126     string basename = to_chop_up;  // script name with no version attached.
127     if ((float)chop_suffix > 0.0) {
128         // this is a big success right here.
129         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
130         return [ basename, chop_suffix ];
131     }
132     // seems like we found nothing useful.
133     return [];
134 }
135 //
136 //////////////
137
138 default
139 {
140     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
141     on_rez(integer parm) { state rerun; }
142 }
143 state rerun { state_entry() { state default; } }
144
145 state real_default
146 {
147     state_entry()
148     {
149         auto_retire();
150 //hmmm: logic broken here.  subscribe to a default set.  let them add and remove from set.
151         if (!force_channel) {
152 //hmmm: terrible defaults treatment.
153             llListen(96, "", llGetOwner(), "");
154             llOwnerSay("Type \"/96 tune N\" to tune in to channel N, or \"/96 tune 0\" to turn radio off.");
155         } else {
156             llOwnerSay("Channel locked on " + (string)channel_stays_on + ".");
157             set_radio_channel(channel_stays_on);
158         }
159     }
160  
161     attach(key attached)
162     {
163         if (attached != NULL_KEY)
164             llResetScript();
165     }
166  
167     listen(integer ch, string name, key id, string msg)
168     {
169         if (!force_channel && ((ch == 96) && llGetSubString(msg, 0, 4) == "tune ") )
170             set_radio_channel((integer)llGetSubString(msg, 5, -1));
171         else if (ch == channel)
172             llOwnerSay("[" + (string)ch + " " + name + "]: " + msg);
173     }
174 }