normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / color_smoove_clock_v3.2.txt
1 
2 // huffware script: color smoove clock, by fred huffhines
3 //
4 // A clock with rotating color text, adjustable for your time zone.
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 // constants you might want to modify...
11
12 integer TIME_ZONE = -5;
13   // the time zone you want the clock to display, measured as an offset from GMT.
14
15 float TIMER_INTERVAL = 0.1;
16   // how frequently timer is hit to adjust color and possibly change time text.
17
18 float MAX_RANDOM_COLOR_JUMP = 0.07;
19   // how much the color might change in one timer hit for one R/G/B component.
20
21 // global variables...
22
23 // remembers the last time string we printed, since we want to be modifying the
24 // colors more frequently than we're changing the clock.
25 string global_time_string = "";
26
27 // track how many ticks have hit the timer.  only updates the time when a whole
28 // second has gone by.
29 integer global_tick_counter = 0;
30
31 // the current color for clock text; the default is just a starting point.
32 vector global_text_color = <0.3, 0.6, 0.8>;
33
34 // records whether the value is going up (positive) or down (negative).
35 float global_elevator_x = -1.0;
36 float global_elevator_y = 1.0;
37 float global_elevator_z = 1.0;
38
39 // do not change the following constants...
40
41 integer PST_TIME_ZONE = -8;  // this constant for the pacific time zone's offset from GMT.
42
43 // makes sure components of the color don't go out of range.  if they do, then
44 // the elevator direction is reversed.
45 normalize_color()
46 {
47     if (global_text_color.x > 1.0) { global_text_color.x = 1.0; global_elevator_x *= -1.0; }
48     if (global_text_color.x < 0.0) { global_text_color.x = 0.0; global_elevator_x *= -1.0; }
49     if (global_text_color.y > 1.0) { global_text_color.y = 1.0; global_elevator_y *= -1.0; }
50     if (global_text_color.y < 0.0) { global_text_color.y = 0.0; global_elevator_y *= -1.0; }
51     if (global_text_color.z > 1.0) { global_text_color.z = 1.0; global_elevator_z *= -1.0; }
52     if (global_text_color.z < 0.0) { global_text_color.z = 0.0; global_elevator_z *= -1.0; }
53 }
54
55 // show the current time string in our current color.
56 display_colored_text()
57 {
58     llSetText(global_time_string, global_text_color, 1.0);
59 }
60
61 rotate_text_color()
62 {
63     // gnarly version is totally random.  merely an example now.
64 //yuck; flashy    global_text_color = <llFrand(1.0), llFrand(1.0), llFrand(1.0)>;
65
66     // better; rotates the colors as slow as you like, but somewhat randomly
67     // for an interesting color glide effect.
68     global_text_color.x += llFrand(MAX_RANDOM_COLOR_JUMP) * global_elevator_x;
69     global_text_color.y += llFrand(MAX_RANDOM_COLOR_JUMP) * global_elevator_y;        
70     global_text_color.z += llFrand(MAX_RANDOM_COLOR_JUMP) * global_elevator_z;
71     
72     normalize_color();  // make sure we didn't go out of bounds.
73         
74     display_colored_text();  // update to the new color.
75 }
76
77 //////////////
78 // huffware script: auto-retire, by fred huffhines, version 2.5.
79 // distributed under BSD-like license.
80 //   !!  keep in mind that this code must be *copied* into another
81 //   !!  script that you wish to add auto-retirement capability to.
82 // when a script has auto_retire in it, it can be dropped into an
83 // object and the most recent version of the script will destroy
84 // all older versions.
85 //
86 // the version numbers are embedded into the script names themselves.
87 // the notation for versions uses a letter 'v', followed by two numbers
88 // in the form "major.minor".
89 // major and minor versions are implicitly considered as a floating point
90 // number that increases with each newer version of the script.  thus,
91 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
92 // and "hazmap v3.2" is a more recent version.
93 //
94 // example usage of the auto-retirement script:
95 //     default {
96 //         state_entry() {
97 //            auto_retire();  // make sure newest addition is only version of script.
98 //        }
99 //     }
100 // this script is partly based on the self-upgrading scripts from markov brodsky
101 // and jippen faddoul.
102 //////////////
103 auto_retire() {
104     string self = llGetScriptName();  // the name of this script.
105     list split = compute_basename_and_version(self);
106     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
107     string basename = llList2String(split, 0);  // script name with no version attached.
108     string version_string = llList2String(split, 1);  // the version found.
109     integer posn;
110     // find any scripts that match the basename.  they are variants of this script.
111     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
112 //log_it("invpo=" + (string)posn);
113         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
114         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
115             // found a basic match at least.
116             list inv_split = compute_basename_and_version(curr_script);
117             if (llGetListLength(inv_split) == 2) {
118                 // see if this script is more ancient.
119                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
120                 // must make sure that the retiring script is completely the identical basename;
121                 // just matching in the front doesn't make it a relative.
122                 if ( (llList2String(inv_split, 0) == basename)
123                     && ((float)inv_version_string < (float)version_string) ) {
124                     // remove script with same name from inventory that has inferior version.
125                     llRemoveInventory(curr_script);
126                 }
127             }
128         }
129     }
130 }
131 //
132 // separates the base script name and version number.  used by auto_retire.
133 list compute_basename_and_version(string to_chop_up)
134 {
135     // minimum script name is 2 characters plus a version.
136     integer space_v_posn;
137     // find the last useful space and 'v' combo.
138     for (space_v_posn = llStringLength(to_chop_up) - 3;
139         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
140         space_v_posn--) {
141         // look for space and v but do nothing else.
142 //log_it("pos=" + (string)space_v_posn);
143     }
144     if (space_v_posn < 2) return [];  // no space found.
145 //log_it("space v@" + (string)space_v_posn);
146     // now we zoom through the stuff after our beloved v character and find any evil
147     // space characters, which are most likely from SL having found a duplicate item
148     // name and not so helpfully renamed it for us.
149     integer indy;
150     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
151 //log_it("indy=" + (string)space_v_posn);
152         if (llGetSubString(to_chop_up, indy, indy) == " ") {
153             // found one; zap it.  since we're going backwards we don't need to
154             // adjust the loop at all.
155             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
156 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
157         }
158     }
159     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
160     // ditch the space character for our numerical check.
161     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
162     // strip out a 'v' if there is one.
163     if (llGetSubString(chop_suffix, 0, 0) == "v")
164         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
165     // if valid floating point number and greater than zero, that works for our version.
166     string basename = to_chop_up;  // script name with no version attached.
167     if ((float)chop_suffix > 0.0) {
168         // this is a big success right here.
169         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
170         return [ basename, chop_suffix ];
171     }
172     // seems like we found nothing useful.
173     return [];
174 }
175 //
176 //////////////
177
178 default
179 {
180     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
181     on_rez(integer parm) { state rerun; }
182 }
183 state rerun { state_entry() { state default; } }
184
185 state real_default
186 {
187     // state_entry() is an event handler, it executes whenever a state is entered.
188     state_entry()
189     {
190         auto_retire();
191         llSetTimerEvent(TIMER_INTERVAL);
192     }
193
194     timer()
195     {
196         // calculate our current rate of timer hits.
197         float ticks_per_second = 1.0 / TIMER_INTERVAL;
198         
199         rotate_text_color();  // spin the text color every timer hit.
200         
201         // check whether the clock time has changed since last updated.
202         global_tick_counter++;
203         if (global_tick_counter < ticks_per_second) {
204             return;
205         }
206         // yep, we need to update the text of the time.
207         global_tick_counter = 0;  // reset for rollover.
208         
209         // get the number of seconds off of the clock on the wall...
210         integer full_seconds = llFloor(llGetWallclock());
211
212         // switch to the chosen time zone.
213         full_seconds += -(PST_TIME_ZONE - TIME_ZONE) * 60 * 60;
214
215         // a constant for how many seconds exist in a day.
216         integer DAY_OF_SECONDS = 24 * 60 * 60;
217
218         // correct any tendency of the seconds to be below zero or above the maximum
219         // due to wacky time zones.
220         while (full_seconds < 0) full_seconds += DAY_OF_SECONDS;
221         while (full_seconds > DAY_OF_SECONDS) full_seconds -= DAY_OF_SECONDS;
222         
223         // calculate all the components of the time.
224         integer seconds = full_seconds;
225         integer minutes = llFloor(seconds / 60);
226         seconds -= minutes * 60;
227         integer hour = llFloor(minutes / 60);
228         minutes -= hour * 60;
229 //llOwnerSay("fs=" + (string)full_seconds + " h=" + (string)hour + " m=" + (string)minutes);
230
231         integer is_am = hour < 12;
232         if (hour > 12) hour -= 12;  // convert down to 12 hour time.
233         if (hour == 0) hour = 12;  // don't show a zero in 12 hour time.
234
235         // prepare the time update message...
236         string minutes_text = (string)minutes;
237         if (minutes < 10) minutes_text = "0" + minutes_text;
238         string seconds_text = (string)seconds;
239         if (seconds < 10) seconds_text = "0" + seconds_text;
240
241         string meridian_string = "am";
242         if (!is_am) meridian_string = "pm";
243
244         // update our time string now that we know all the pieces.        
245         global_time_string = (string)hour + ":" + minutes_text + ":"
246             + seconds_text + meridian_string;
247         // flip the text up above the object to show the time.
248         display_colored_text();
249     }
250
251 }
252
253 //credits:
254 // parts from lsl wiki originally?
255 // Enables an object to display the time for the time zone of your choosing.
256 // Thanks to Ben Linden for insight into timers, time and text setting.