normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / huffbee_bulb_v4.7.txt
1 
2 // huffware script: "huffbee bulb", by fred huffhines
3 //
4 // inspired by the higbee bulb script available in open source, but provides a few
5 // different functions.  this accepts a click to turn on and off, but it also listens
6 // to a special command to hear requests from linked objects.  this also shuts off
7 // at dusk and turns on at dawn.
8 // this script uses the party culiar script to get the particle system created,
9 // and hence relies on a notecard to define the lamp's particles.
10 //
11 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
12 // do not use it in objects without fully realizing you are implicitly accepting that license.
13 //
14
15 integer DUSK_CHECKING_PERIOD = 240;
16     // check for dusk this frequently.  in seconds, so 240 = 4 minutes.
17
18 integer SECRET_LIGHT_BULB_ILLUMINATI_CODE = -14058;
19     // a semi-secret code that is used in a linked message when some other script
20     // wants the lamp to turn on or off.
21
22 float BULB_GLOW_LEVEL = 0.5;  // how much glow should the bulb have when it's lit?
23
24 integer CHECK_FOR_DUSK = TRUE;  // true if this lamp should be driven by daylight.
25
26 integer PARTICLE_FIX_COG = 7;  // every N timer hits, we'll fix our particles.
27
28 // party culiar link message API.
29 //////////////
30 integer PARTYCULIAR_HUFFWARE_ID = 10018;
31     // a unique ID within the huffware system for this script.
32 string HUFFWARE_PARM_SEPARATOR = "~~~";
33     // three tildes is an uncommon thing to have otherwise, so we use it to separate
34     // our commands in linked messages.
35 //
36 string PARTYCULIAR_POWER_COMMAND = "#powrpcl";
37     // tells the particle system to either turn on or off.  the single parameter is
38     // either "on", "1", "true", or "off", "0", "false".
39 //////////////
40
41 // globals.
42
43 integer particle_cog = 0;
44
45 //////////////
46
47 // asks the particle system script for a favor, given by the command and the
48 // parameters.
49 send_partyculiar_request(string cmd, string params)
50 { llMessageLinked(LINK_THIS, PARTYCULIAR_HUFFWARE_ID, cmd, params); }
51
52 // shuts down the lamp.  there is no coming back from this method,
53 // since it resets the script.
54 turn_off_lamp()
55 {
56     turn_off_particles();
57     llResetScript();
58 }
59
60 turn_off_particles() { send_partyculiar_request(PARTYCULIAR_POWER_COMMAND, "off"); }
61
62 turn_on_particles() { send_partyculiar_request(PARTYCULIAR_POWER_COMMAND, "on"); }
63
64 //////////////
65 // huffware script: auto-retire, by fred huffhines, version 2.5.
66 // distributed under BSD-like license.
67 //   !!  keep in mind that this code must be *copied* into another
68 //   !!  script that you wish to add auto-retirement capability to.
69 // when a script has auto_retire in it, it can be dropped into an
70 // object and the most recent version of the script will destroy
71 // all older versions.
72 //
73 // the version numbers are embedded into the script names themselves.
74 // the notation for versions uses a letter 'v', followed by two numbers
75 // in the form "major.minor".
76 // major and minor versions are implicitly considered as a floating point
77 // number that increases with each newer version of the script.  thus,
78 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
79 // and "hazmap v3.2" is a more recent version.
80 //
81 // example usage of the auto-retirement script:
82 //     default {
83 //         state_entry() {
84 //            auto_retire();  // make sure newest addition is only version of script.
85 //        }
86 //     }
87 // this script is partly based on the self-upgrading scripts from markov brodsky
88 // and jippen faddoul.
89 //////////////
90 auto_retire() {
91     string self = llGetScriptName();  // the name of this script.
92     list split = compute_basename_and_version(self);
93     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
94     string basename = llList2String(split, 0);  // script name with no version attached.
95     string version_string = llList2String(split, 1);  // the version found.
96     integer posn;
97     // find any scripts that match the basename.  they are variants of this script.
98     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
99 //log_it("invpo=" + (string)posn);
100         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
101         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
102             // found a basic match at least.
103             list inv_split = compute_basename_and_version(curr_script);
104             if (llGetListLength(inv_split) == 2) {
105                 // see if this script is more ancient.
106                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
107                 // must make sure that the retiring script is completely the identical basename;
108                 // just matching in the front doesn't make it a relative.
109                 if ( (llList2String(inv_split, 0) == basename)
110                     && ((float)inv_version_string < (float)version_string) ) {
111                     // remove script with same name from inventory that has inferior version.
112                     llRemoveInventory(curr_script);
113                 }
114             }
115         }
116     }
117 }
118 //
119 // separates the base script name and version number.  used by auto_retire.
120 list compute_basename_and_version(string to_chop_up)
121 {
122     // minimum script name is 2 characters plus a version.
123     integer space_v_posn;
124     // find the last useful space and 'v' combo.
125     for (space_v_posn = llStringLength(to_chop_up) - 3;
126         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
127         space_v_posn--) {
128         // look for space and v but do nothing else.
129 //log_it("pos=" + (string)space_v_posn);
130     }
131     if (space_v_posn < 2) return [];  // no space found.
132 //log_it("space v@" + (string)space_v_posn);
133     // now we zoom through the stuff after our beloved v character and find any evil
134     // space characters, which are most likely from SL having found a duplicate item
135     // name and not so helpfully renamed it for us.
136     integer indy;
137     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
138 //log_it("indy=" + (string)space_v_posn);
139         if (llGetSubString(to_chop_up, indy, indy) == " ") {
140             // found one; zap it.  since we're going backwards we don't need to
141             // adjust the loop at all.
142             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
143 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
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     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
167     on_rez(integer parm) { state rerun; }
168 }
169 state rerun { state_entry() { state default; } }
170
171 state real_default
172 {
173     state_entry()
174     {
175         auto_retire();
176         turn_off_particles();
177         // reset the bulb to a fairly dark color with no glow and no light.
178         llSetPrimitiveParams([
179             PRIM_COLOR, ALL_SIDES, <0.42, 0.42, 0.42>, 0.5,
180             PRIM_FULLBRIGHT, ALL_SIDES, FALSE,
181             PRIM_GLOW, ALL_SIDES, 0.0,
182             PRIM_POINT_LIGHT, FALSE, <0, 0, 0>, 1.0, 10.0, 0.25
183             ]);        
184         llSetTimerEvent(DUSK_CHECKING_PERIOD);
185     }
186     on_rez(integer parm) {
187         state default;
188     }
189     timer() {
190         vector sun = llGetSunDirection();
191         if (CHECK_FOR_DUSK && (sun.z <= 0) )
192             state PowerUp;  // turn on at dusk.
193     }
194     touch_start(integer total_number) {
195         state PowerUp;
196     }
197
198     // handle requests to ignite the lamp.    
199     link_message(integer sender, integer num, string str, key id) {
200         // we only support one message really, which is to turn on/off.
201         if (num == SECRET_LIGHT_BULB_ILLUMINATI_CODE) {
202             state PowerUp;  // we know we aren't lit yet, so get that way.
203         }
204     }
205 }
206         
207 state PowerUp
208 {
209     state_entry() {
210         // light the bulb, with a full color / full bright assault on
211         // the sense, using real light and a glow effect.
212         llSetPrimitiveParams([
213             PRIM_COLOR, ALL_SIDES, <1, 1, 1>, 0.5,
214             PRIM_FULLBRIGHT, ALL_SIDES, TRUE,
215             PRIM_GLOW, ALL_SIDES, BULB_GLOW_LEVEL,
216             PRIM_POINT_LIGHT, TRUE, <.8, .8, .3>, 1.0, 20.0, .9
217             ]);
218         turn_on_particles();
219         llSetTimerEvent(DUSK_CHECKING_PERIOD);
220     }
221     on_rez(integer parm) {
222         // if we rezzed in this state, that's a mistake.  the lamp is intended
223         // to start in a dark state.
224         turn_off_lamp();
225     }
226     timer() {
227         particle_cog++;
228         if (particle_cog >= PARTICLE_FIX_COG) {
229             // periodically refresh the particle system.
230             turn_on_particles();
231             particle_cog = 0;  // reset cog count.
232         }
233         // also periodically check if we should shut off.
234         vector sun = llGetSunDirection();
235         if (CHECK_FOR_DUSK && (sun.z > 0) ) {
236             // turn off at dawn.
237             turn_off_lamp();
238         }
239     }
240     
241     touch_start(integer total_number)
242     {
243         turn_off_lamp();
244     }
245     
246     link_message(integer sender, integer num, string str, key id) {
247         // we only support one message really, which is to turn on/off.
248         if (num == SECRET_LIGHT_BULB_ILLUMINATI_CODE) {
249             turn_off_lamp();  // in this state, we are already lit, so get real dark.
250         }
251     }
252 }