2 // huffware script: chaos picks a number, by fred huffhines.
3 // (was formerly called "raffles asks chaos a question")
5 // produces a random number between 0 and the MAXIMUM_VALUE defined below.
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.
11 integer MAXIMUM_VALUE = 100;
12 // this is the highest number the generator will produce.
13 // the range is from zero, a very poor pick, to the number above
14 // (the best pick), inclusive.
20 // locates the string "text" in the list to "search_in".
21 integer find_in_list(list search_in, string text)
23 integer len = llGetListLength(search_in);
25 for (i = 0; i < len; i++) {
26 if (llList2String(search_in, i) == text)
33 // huffware script: auto-retire, by fred huffhines, version 2.5.
34 // distributed under BSD-like license.
35 // !! keep in mind that this code must be *copied* into another
36 // !! script that you wish to add auto-retirement capability to.
37 // when a script has auto_retire in it, it can be dropped into an
38 // object and the most recent version of the script will destroy
39 // all older versions.
41 // the version numbers are embedded into the script names themselves.
42 // the notation for versions uses a letter 'v', followed by two numbers
43 // in the form "major.minor".
44 // major and minor versions are implicitly considered as a floating point
45 // number that increases with each newer version of the script. thus,
46 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
47 // and "hazmap v3.2" is a more recent version.
49 // example usage of the auto-retirement script:
52 // auto_retire(); // make sure newest addition is only version of script.
55 // this script is partly based on the self-upgrading scripts from markov brodsky
56 // and jippen faddoul.
59 string self = llGetScriptName(); // the name of this script.
60 list split = compute_basename_and_version(self);
61 if (llGetListLength(split) != 2) return; // nothing to do for this script.
62 string basename = llList2String(split, 0); // script name with no version attached.
63 string version_string = llList2String(split, 1); // the version found.
65 // find any scripts that match the basename. they are variants of this script.
66 for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
67 //log_it("invpo=" + (string)posn);
68 string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
69 if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
70 // found a basic match at least.
71 list inv_split = compute_basename_and_version(curr_script);
72 if (llGetListLength(inv_split) == 2) {
73 // see if this script is more ancient.
74 string inv_version_string = llList2String(inv_split, 1); // the version found.
75 // must make sure that the retiring script is completely the identical basename;
76 // just matching in the front doesn't make it a relative.
77 if ( (llList2String(inv_split, 0) == basename)
78 && ((float)inv_version_string < (float)version_string) ) {
79 // remove script with same name from inventory that has inferior version.
80 llRemoveInventory(curr_script);
87 // separates the base script name and version number. used by auto_retire.
88 list compute_basename_and_version(string to_chop_up)
90 // minimum script name is 2 characters plus a version.
92 // find the last useful space and 'v' combo.
93 for (space_v_posn = llStringLength(to_chop_up) - 3;
94 (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
96 // look for space and v but do nothing else.
97 //log_it("pos=" + (string)space_v_posn);
99 if (space_v_posn < 2) return []; // no space found.
100 //log_it("space v@" + (string)space_v_posn);
101 // now we zoom through the stuff after our beloved v character and find any evil
102 // space characters, which are most likely from SL having found a duplicate item
103 // name and not so helpfully renamed it for us.
105 for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
106 //log_it("indy=" + (string)space_v_posn);
107 if (llGetSubString(to_chop_up, indy, indy) == " ") {
108 // found one; zap it. since we're going backwards we don't need to
109 // adjust the loop at all.
110 to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
111 //log_it("saw case of previously redundant item, aieee. flattened: " + to_chop_up);
114 string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
115 // ditch the space character for our numerical check.
116 string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
117 // strip out a 'v' if there is one.
118 if (llGetSubString(chop_suffix, 0, 0) == "v")
119 chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
120 // if valid floating point number and greater than zero, that works for our version.
121 string basename = to_chop_up; // script name with no version attached.
122 if ((float)chop_suffix > 0.0) {
123 // this is a big success right here.
124 basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
125 return [ basename, chop_suffix ];
127 // seems like we found nothing useful.
133 // variables used in the script...
135 list participants = [];
136 // the people who have already received a number in this drawing.
138 integer highest_picked = 0;
139 // the highest picked so far.
141 string winner_so_far = "";
142 // the name of the person who got the highest number to this point.
144 integer lottery_finished = FALSE;
145 // true when the lottery has been completed.
152 lottery_finished = FALSE;
153 llSay(0, "ready to draw random numbers from 0 to "
154 + (string)MAXIMUM_VALUE + "...");
155 llListen(0, "", llGetOwner(), "");
158 // reports the winner, but adds "prefix" to the front of our text and
159 // will say 'so far' if "provisional" flag is true.
160 call_winner(string prefix, integer provisional)
162 string disclaimer = ""; // extra text if not final result.
163 if (provisional) disclaimer = "so far ";
164 string winner = winner_so_far;
165 if (!llStringLength(winner)) winner = "no one(!)";
166 llSay(0, prefix + "winner " + disclaimer + "is " + winner
167 + " who picked " + (string)highest_picked + ".");
170 announce_final_winner()
172 call_winner("The **final** ", FALSE);
177 state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
178 on_rez(integer parm) { state rerun; }
180 state rerun { state_entry() { state default; } }
190 on_rez(integer param) { llResetScript(); }
192 touch_start(integer total_number)
194 integer which_picker;
195 for (which_picker = 0; which_picker < total_number; which_picker++) {
196 if (llDetectedKey(which_picker) == llGetOwner()) {
197 llOwnerSay("as owner you can say\n'#winner' to hear the current winner,\n'#final' to close the drawing and,\n'#reset lottery' to start a new drawing.");
198 call_winner("the ", TRUE);
202 if (lottery_finished) {
203 // just report that this one's finished.
204 announce_final_winner();
208 // if we got to here, this is a normal person (not the owner) who
209 // might still be allowed to participate in an active drawing.
211 // get a random number between 0 and almost up to the max.
212 float rando = llFrand(MAXIMUM_VALUE);
213 // convert to int, which makes the range from zero to the max inclusive.
214 integer int_rando = llRound(rando);
215 string letter = ""; // default is no extra letter.
216 // pick the right article for english.
217 if ( (int_rando == 8)
220 || (int_rando / 10 == 8) ) {
221 // these cases like an "an" instead of an "a".
222 // e.g., an eleven, an eight, an eighty-three, an eighteen.
226 // make sure this person hasn't already played.
227 string name = llDetectedName(which_picker);
228 if (find_in_list(participants, name) >= 0) {
229 call_winner(name + " already picked a number,\nand the ", TRUE);
233 // add the new participant to the list.
234 participants += [ name ];
235 // list kludge for better memory usage.
236 llSay(0, name + " drew a" + letter + " " + (string)int_rando + ".");
237 if (highest_picked < int_rando) {
238 llSay(0, name + " has the new highest number!!!");
239 highest_picked = int_rando;
240 winner_so_far = name;
241 } else if (highest_picked == int_rando) {
242 call_winner("oh, so close; " + name + " tied with the ", FALSE);
244 call_winner("that's not quite enough " + name + ", since\nthe ", TRUE);
249 listen(integer channel, string name, key id, string msg) {
250 if (msg == "#reset lottery") {
251 llSay(0, "resetting the drawing now...");
252 if (winner_so_far != "")
253 call_winner("forgetting previous ", FALSE);
255 } else if (msg == "#winner") {
256 call_winner("the ", TRUE);
257 } else if (msg == "#final") {
258 lottery_finished = TRUE;
259 announce_final_winner();