normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / base_note_read_v0.3.txt
1 
2 // add-in...  huffware script: notecard library, by fred huffhines
3 //
4 //   this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
5 //   do not use it in objects without fully realizing you are implicitly accepting that license.
6 //
7
8 // items controlled by the notecard...
9
10 string NOTECARD_SIGNATURE = "#hoopy";  // the first line of the notecard must be this.
11
12 string current_notecard_name = "";  // the name of the card we're reading now.
13 key current_query_id = NULL_KEY;  // the query ID for the current notecard.
14 list query_contents;  // the lines we have read from the notecard.
15 integer line_number;  // which line are we at in notecard?
16 integer debug = FALSE;
17
18 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
19     // this pattern is an uncommon thing to see in text, so we use it to separate
20     // our commands in link messages.
21     
22 initialize()
23 {
24     // we keep the same notecard name, in case it's still good.  we want to
25     // avoid re-reading the notecard unless we see an inventory change.
26     current_query_id = NULL_KEY;
27     query_contents = [];
28     line_number = 0;
29 }
30
31 // returns a non-empty string if "to_check" defines contents for "variable_name".
32 string defines_variable(string to_check, string variable_name)
33 {
34     // clean initial spaces.
35     while (llGetSubString(to_check, 0, 0) == " ")
36         to_check = llDeleteSubString(to_check, 0, 0);
37     if (!is_prefix(to_check, variable_name)) return "";
38     to_check = llDeleteSubString(to_check, 0, llStringLength(variable_name) - 1);
39     // clean any spaces or valid assignment characters.
40     while ( (llGetSubString(to_check, 0, 0) == " ")
41             || (llGetSubString(to_check, 0, 0) == "=")
42             || (llGetSubString(to_check, 0, 0) == ",") )
43         to_check = llDeleteSubString(to_check, 0, 0);
44     if (debug)
45         log_it("set " + variable_name + " = " + to_check);    
46     // return what's left of the string.
47     return to_check;
48 }
49
50 parse_variable_definition(string to_parse)
51 {
52     string content;  // filled after finding a variable name.
53     string texture_name;  // temporary used in reading texture name.
54
55 //etc.    
56 //    if ( (content = defines_variable(to_parse, "debug")) != "")
57 //        debug = (integer)content;
58
59 }
60
61 process_particle_settings(list particle_definitions)
62 {
63     integer current_item = 0;
64     integer max_items = llGetListLength(particle_definitions);
65     while (current_item < max_items) {
66         string curr_line = llList2String(particle_definitions, current_item);
67         parse_variable_definition(curr_line);
68         current_item++;
69     }
70 }
71
72 check_for_notecard()
73 {
74     if (current_notecard_name != "") return;
75     current_notecard_name = llGetInventoryName(INVENTORY_NOTECARD, 0);
76     // if the notecard is real, then we will start reading it.
77     if (current_notecard_name != "") {
78         line_number = 0;
79         query_contents = [];
80         current_query_id = llGetNotecardLine(current_notecard_name, 0);
81     }
82 }
83
84 //////////////
85 // from hufflets...
86
87 //////////////
88
89 integer debug_num = 0;
90
91 // a debugging output method.  can be disabled entirely in one place.
92 log_it(string to_say)
93 {
94     debug_num++;
95     // tell this to the owner.    
96     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
97     // say this on open chat, but use an unusual channel.
98 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
99 }
100
101 //////////////
102
103 // joins a list of parameters using the parameter sentinel for the library.
104 string wrap_parameters(list to_flatten)
105 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
106
107 // handles when blank strings need to come through the pipe.
108 string wrap_blank_string(string to_wrap)
109 {
110     if (llStringLength(to_wrap)) return to_wrap;  // that one is okay.
111     return "\"\"";  // return a quoted nothing as a signal for a blank.
112 }
113
114 // undoes a previously wrapped blank string.
115 string interpret_blank_string(string to_unwrap)
116 {
117     if (to_unwrap == "\"\"") return "";  // that was an encoded blank.
118     return to_unwrap;  // no encoding.
119 }
120
121 //////////////
122
123 // returns a number at most maximum and at least minimum.
124 // if "allow_negative" is TRUE, then the return may be positive or negative.
125 float randomize_within_range(float minimum, float maximum, integer allow_negative)
126 {
127     float to_return = minimum + llFrand(maximum - minimum);
128     if (allow_negative) {
129         if (llFrand(1.0) < 0.5) to_return *= -1.0;
130     }
131     return to_return;
132 }
133
134 // the string processing methods are not case sensitive.
135   
136 // returns TRUE if the "pattern" is found in the "full_string".
137 integer matches_substring(string full_string, string pattern)
138 { return (find_substring(full_string, pattern) >= 0); }
139
140 // returns the index of the first occurrence of "pattern" inside
141 // the "full_string".  if it is not found, then a negative number is returned.
142 integer find_substring(string full_string, string pattern)
143 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
144
145 // returns TRUE if the "prefix" string is the first part of "compare_with".
146 integer is_prefix(string compare_with, string prefix)
147 { return find_substring(compare_with, prefix) == 0; }
148
149 //////////////
150
151 default {
152     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
153     on_rez(integer parm) { state rerun; }
154 }
155 state rerun { state_entry() { state default; } }
156
157 state real_default
158 {
159     state_entry()
160     {
161 //        auto_retire();
162         initialize();
163         check_for_notecard();
164     }
165
166     changed(integer change_type) {
167         if (change_type != CHANGED_INVENTORY) {
168             // we only care about inventory changes here.
169             return;
170         }
171         if (current_query_id != NULL_KEY) {
172             // we're already reading a card right now.
173             return;
174         }
175         // make sure we reset the old name.
176         current_notecard_name = "";
177         check_for_notecard();
178     }
179     
180     dataserver(key query_id, string data) {
181         if (query_id != current_query_id) {
182 log_it("not our query id somehow?");
183 //log_it("weird query had: " + (string)data);
184             return;
185         }
186         // if we're not at the end of the notecard we're reading...
187         if (data != EOF) {
188             if (!line_number) {
189                 if (data != NOTECARD_SIGNATURE) {
190                     // this card has the wrong signature at the top.  quit bothering
191                     // with it now.
192                     return;
193                 }
194                 log_it("starting to read notecard " + current_notecard_name + "...");    
195             }
196             if (data != "") {
197                  // add the non-blank line to our destination list.
198                 query_contents += data;
199 //log_it("line " + (string)line_number + ": data=" + data);
200             }
201             line_number++;  // increase the line count.
202             // request the next line from the notecard.
203             current_query_id = llGetNotecardLine(current_notecard_name, line_number);
204         } else {
205             // no more data, so we're done with this card.
206             current_query_id = NULL_KEY;
207             if (!llGetListLength(query_contents)) {
208                 // nothing was read?  the heck with this card.
209                 current_notecard_name = "";  // toss bad card.
210                 return;
211             }
212 log_it("notecard said:\n" + (string)(query_contents));
213             log_it("done reading notecard " + current_notecard_name + ".");
214         }
215     }
216 }
217