corrade-lsl-templates – Blame information for rev 42
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
8 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
42 | office | 2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
8 | office | 3 | /////////////////////////////////////////////////////////////////////////// |
4 | // |
||
5 | // A module that evaluates a mathematical expression for Corrade Eggdrop. |
||
6 | // |
||
7 | /////////////////////////////////////////////////////////////////////////// |
||
8 | |||
9 | /////////////////////////////////////////////////////////////////////////// |
||
41 | office | 10 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
8 | office | 11 | /////////////////////////////////////////////////////////////////////////// |
12 | string wasKeyValueGet(string k, string data) { |
||
13 | if(llStringLength(data) == 0) return ""; |
||
14 | if(llStringLength(k) == 0) return ""; |
||
41 | office | 15 | list a = llParseStringKeepNulls(data, ["&", "="], []); |
16 | integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]); |
||
17 | if(i != -1) return llList2String(a, 2*i+1); |
||
8 | office | 18 | return ""; |
19 | } |
||
42 | office | 20 | |
8 | office | 21 | /////////////////////////////////////////////////////////////////////////// |
42 | office | 22 | // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 // |
8 | office | 23 | /////////////////////////////////////////////////////////////////////////// |
24 | string wasKeyValueEncode(list data) { |
||
25 | list k = llList2ListStrided(data, 0, -1, 2); |
||
26 | list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2); |
||
27 | data = []; |
||
28 | do { |
||
29 | data += llList2String(k, 0) + "=" + llList2String(v, 0); |
||
30 | k = llDeleteSubList(k, 0, 0); |
||
31 | v = llDeleteSubList(v, 0, 0); |
||
32 | } while(llGetListLength(k) != 0); |
||
33 | return llDumpList2String(data, "&"); |
||
34 | } |
||
35 | |||
36 | /////////////////////////////////////////////////////////////////////////// |
||
42 | office | 37 | // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 // |
8 | office | 38 | /////////////////////////////////////////////////////////////////////////// |
39 | // http://was.fm/secondlife/wanderer |
||
40 | vector wasCirclePoint(float radius) { |
||
41 | float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2); |
||
42 | float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2); |
||
43 | if(llPow(x,2) + llPow(y,2) <= llPow(radius,2)) |
||
44 | return <x, y, 0>; |
||
45 | return wasCirclePoint(radius); |
||
46 | } |
||
47 | |||
48 | /////////////////////////////////////////////////////////////////////////// |
||
42 | office | 49 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
8 | office | 50 | /////////////////////////////////////////////////////////////////////////// |
51 | // escapes a string in conformance with RFC1738 |
||
52 | string wasURLEscape(string i) { |
||
53 | string o = ""; |
||
54 | do { |
||
55 | string c = llGetSubString(i, 0, 0); |
||
56 | i = llDeleteSubString(i, 0, 0); |
||
57 | if(c == "") jump continue; |
||
58 | if(c == " ") { |
||
59 | o += "+"; |
||
60 | jump continue; |
||
61 | } |
||
62 | if(c == "\n") { |
||
63 | o += "%0D" + llEscapeURL(c); |
||
64 | jump continue; |
||
65 | } |
||
66 | o += llEscapeURL(c); |
||
67 | @continue; |
||
68 | } while(i != ""); |
||
69 | return o; |
||
70 | } |
||
71 | |||
72 | /////////////////////////////////////////////////////////////////////////// |
||
42 | office | 73 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
8 | office | 74 | /////////////////////////////////////////////////////////////////////////// |
75 | list wasCSVToList(string csv) { |
||
76 | list l = []; |
||
77 | list s = []; |
||
78 | string m = ""; |
||
79 | do { |
||
80 | string a = llGetSubString(csv, 0, 0); |
||
81 | csv = llDeleteSubString(csv, 0, 0); |
||
82 | if(a == ",") { |
||
83 | if(llList2String(s, -1) != "\"") { |
||
84 | l += m; |
||
85 | m = ""; |
||
86 | jump continue; |
||
87 | } |
||
88 | m += a; |
||
89 | jump continue; |
||
90 | } |
||
91 | if(a == "\"" && llGetSubString(csv, 0, 0) == a) { |
||
92 | m += a; |
||
93 | csv = llDeleteSubString(csv, 0, 0); |
||
94 | jump continue; |
||
95 | } |
||
96 | if(a == "\"") { |
||
97 | if(llList2String(s, -1) != a) { |
||
98 | s += a; |
||
99 | jump continue; |
||
100 | } |
||
101 | s = llDeleteSubList(s, -1, -1); |
||
102 | jump continue; |
||
103 | } |
||
104 | m += a; |
||
105 | @continue; |
||
106 | } while(csv != ""); |
||
107 | // postcondition: length(s) = 0 |
||
108 | return l + m; |
||
109 | } |
||
110 | |||
111 | /////////////////////////////////////////////////////////////////////////// |
||
42 | office | 112 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
8 | office | 113 | /////////////////////////////////////////////////////////////////////////// |
114 | string wasListToCSV(list l) { |
||
115 | list v = []; |
||
116 | do { |
||
117 | string a = llDumpList2String( |
||
118 | llParseStringKeepNulls( |
||
119 | llList2String( |
||
42 | office | 120 | l, |
8 | office | 121 | |
42 | office | 122 | ), |
123 | ["\""], |
||
8 | office | 124 | [] |
125 | ), |
||
126 | "\"\"" |
||
127 | ); |
||
128 | if(llParseStringKeepNulls( |
||
42 | office | 129 | a, |
8 | office | 130 | [" ", ",", "\n", "\""], [] |
42 | office | 131 | ) != |
8 | office | 132 | (list) a |
133 | ) a = "\"" + a + "\""; |
||
134 | v += a; |
||
135 | l = llDeleteSubList(l, 0, 0); |
||
136 | } while(l != []); |
||
137 | return llDumpList2String(v, ","); |
||
138 | } |
||
139 | |||
140 | /////////////////////////////////////////////////////////////////////////// |
||
42 | office | 141 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
8 | office | 142 | /////////////////////////////////////////////////////////////////////////// |
143 | // unescapes a string in conformance with RFC1738 |
||
144 | string wasURLUnescape(string i) { |
||
145 | return llUnescapeURL( |
||
146 | llDumpList2String( |
||
147 | llParseString2List( |
||
148 | llDumpList2String( |
||
149 | llParseString2List( |
||
42 | office | 150 | i, |
151 | ["+"], |
||
8 | office | 152 | [] |
42 | office | 153 | ), |
8 | office | 154 | " " |
155 | ), |
||
42 | office | 156 | ["%0D%0A"], |
8 | office | 157 | [] |
42 | office | 158 | ), |
8 | office | 159 | "\n" |
160 | ) |
||
161 | ); |
||
162 | } |
||
163 | |||
164 | ////////////////////////////////////////////////////////// |
||
165 | // Returns a reversed list. |
||
166 | ////////////////////////////////////////////////////////// |
||
167 | list wasListReverse(list lst) { |
||
168 | if(llGetListLength(lst)<=1) return lst; |
||
169 | return wasListReverse(llList2List(lst, 1, llGetListLength(lst))) + llList2List(lst,0,0); |
||
170 | } |
||
42 | office | 171 | |
8 | office | 172 | ////////////////////////////////////////////////////////// |
173 | // Deletes elements delete from list input. |
||
174 | ////////////////////////////////////////////////////////// |
||
175 | list wasSubtractSubList(list input, list delete) { |
||
176 | do { |
||
177 | string tok = llList2String(delete, 0); |
||
178 | list clean = input; |
||
179 | do { |
||
180 | if(llList2String(clean, 0) == tok) { |
||
181 | integer idx = llListFindList(input, (list)tok); |
||
182 | input = llDeleteSubList(input, idx, idx); |
||
183 | } |
||
184 | } while(clean = llDeleteSubList(clean, 0, 0)); |
||
185 | } while(delete = llDeleteSubList(delete, 0, 0)); |
||
186 | return input; |
||
187 | } |
||
42 | office | 188 | |
8 | office | 189 | ////////////////////////////////////////////////////////// |
190 | // Returns a list of operators and operands. |
||
191 | ////////////////////////////////////////////////////////// |
||
192 | list wasInfixTokenize(string input) { |
||
193 | list op = [ "+", "-", "(", ")", "%", "*", "/", "^", "sin", "asin", "cos", "acos", "tan", "sqrt", "ln" ]; |
||
194 | list result = llParseString2List(input, [], op); |
||
195 | return wasSubtractSubList(result, [" "]); |
||
196 | } |
||
42 | office | 197 | |
8 | office | 198 | ////////////////////////////////////////////////////////// |
199 | // Transforms an infix expression to a postfix expression. |
||
200 | ////////////////////////////////////////////////////////// |
||
201 | list wasInfixToPostfix(list infix) { |
||
202 | list op = [ "+", "-", "%", "*", "/", "^", "sin", "asin", "cos", "acos", "tan", "sqrt", "ln" ]; |
||
203 | list opStack = []; |
||
204 | list result = []; |
||
205 | do { |
||
206 | string t = llList2String(infix, 0); |
||
207 | infix = llDeleteSubList(infix, 0, 0); |
||
208 | if(t == "(") { |
||
209 | opStack += "("; |
||
210 | jump continue; |
||
211 | } |
||
212 | if(t == ")") { |
||
213 | while(llGetListLength(opStack) != 0) { |
||
214 | string topa = llList2String(opStack, llGetListLength(opStack)-1); |
||
215 | opStack = llDeleteSubList(opStack, llGetListLength(opStack)-1, llGetListLength(opStack)-1); |
||
216 | if(topa != "(" && topa != ")") result += topa; |
||
217 | } |
||
218 | opStack = llDeleteSubList(opStack, llGetListLength(opStack)-1, llGetListLength(opStack)-1); |
||
219 | jump continue; |
||
220 | } |
||
221 | integer idx = llListFindList(op, (list)t); |
||
222 | if(idx == -1) { |
||
223 | result += t; |
||
224 | jump continue; |
||
225 | } |
||
226 | @repeat; |
||
227 | string topb = llList2String(opStack, llGetListLength(opStack)-1); |
||
228 | integer odx = llListFindList(op, (list)topb); |
||
229 | if(odx >= idx) { |
||
230 | opStack = llDeleteSubList(opStack, llGetListLength(opStack)-1, llGetListLength(opStack)-1); |
||
231 | result += topb; |
||
232 | if(llGetListLength(opStack) != 0) jump repeat; |
||
233 | } |
||
234 | opStack += t; |
||
235 | @continue; |
||
236 | } while(llGetListLength(infix) != 0); |
||
237 | result += wasListReverse(opStack); |
||
42 | office | 238 | return result; |
8 | office | 239 | } |
42 | office | 240 | |
8 | office | 241 | ////////////////////////////////////////////////////////// |
242 | // Evaluate a postfix expression. |
||
243 | ////////////////////////////////////////////////////////// |
||
244 | float wasPostfixEval(list postfix) { |
||
245 | list op = [ "+", "-", "%", "*", "/", "^", "sin", "asin", "cos", "acos", "tan", "sqrt", "ln" ]; |
||
246 | list orStack = []; |
||
247 | do { |
||
248 | string t = llList2String(postfix, 0); |
||
249 | postfix = llDeleteSubList(postfix, 0, 0); |
||
250 | integer idx = llListFindList(op, (list)t); |
||
251 | if(idx == -1) { |
||
252 | orStack += t; |
||
253 | jump continue; |
||
254 | } |
||
255 | float a = llList2Float(orStack, llGetListLength(orStack)-1); |
||
256 | orStack = llDeleteSubList(orStack, llGetListLength(orStack)-1, llGetListLength(orStack)-1); |
||
257 | float b = llList2Float(orStack, llGetListLength(orStack)-1); |
||
258 | float r = 0; |
||
259 | if(t == "+") { |
||
260 | orStack = llDeleteSubList(orStack, llGetListLength(orStack)-1, llGetListLength(orStack)-1); |
||
261 | r = b + a; |
||
262 | jump push; |
||
263 | } |
||
264 | if(t == "-") { |
||
265 | orStack = llDeleteSubList(orStack, llGetListLength(orStack)-1, llGetListLength(orStack)-1); |
||
266 | r = b - a; |
||
267 | jump push; |
||
268 | } |
||
269 | if(t == "*") { |
||
270 | orStack = llDeleteSubList(orStack, llGetListLength(orStack)-1, llGetListLength(orStack)-1); |
||
271 | r = b * a; |
||
272 | jump push; |
||
273 | } |
||
274 | if(t == "/") { |
||
275 | orStack = llDeleteSubList(orStack, llGetListLength(orStack)-1, llGetListLength(orStack)-1); |
||
276 | if(a == 0) { |
||
277 | r = (float)"NaN"; |
||
278 | jump push; |
||
279 | } |
||
280 | r = b / a; |
||
281 | jump push; |
||
282 | } |
||
283 | if(t == "^") { |
||
284 | orStack = llDeleteSubList(orStack, llGetListLength(orStack)-1, llGetListLength(orStack)-1); |
||
285 | r = llPow(b,a); |
||
286 | jump push; |
||
287 | } |
||
288 | if(t == "%") { |
||
289 | orStack = llDeleteSubList(orStack, llGetListLength(orStack)-1, llGetListLength(orStack)-1); |
||
290 | r = (integer)b % (integer)a; |
||
291 | jump push; |
||
292 | } |
||
293 | if(t == "sin") { |
||
294 | r = llSin(a * DEG_TO_RAD); |
||
295 | jump push; |
||
296 | } |
||
297 | if(t == "asin") { |
||
298 | r = llAsin(a * DEG_TO_RAD); |
||
299 | jump push; |
||
300 | } |
||
301 | if(t == "cos") { |
||
302 | r = llCos(a * DEG_TO_RAD); |
||
303 | jump push; |
||
304 | } |
||
305 | if(t == "acos") { |
||
306 | r = llAcos(a * DEG_TO_RAD); |
||
307 | jump push; |
||
308 | } |
||
309 | if(t == "tan") { |
||
310 | r = llTan(a * DEG_TO_RAD); |
||
311 | jump push; |
||
312 | } |
||
313 | if(t == "ln") { |
||
314 | r = llLog(a); |
||
315 | jump push; |
||
316 | } |
||
317 | if(t == "sqrt") { |
||
318 | r = llSqrt(a); |
||
319 | } |
||
320 | @push; |
||
321 | orStack += r; |
||
322 | @continue; |
||
323 | } while(llGetListLength(postfix) != 0); |
||
324 | return llList2Float(orStack, 0); |
||
325 | } |
||
326 | |||
327 | // configuration data |
||
328 | string configuration = ""; |
||
329 | // store message over state. |
||
330 | string data = ""; |
||
331 | |||
332 | // Notecard reading. |
||
333 | key nQuery = NULL_KEY; |
||
334 | integer nLine = 0; |
||
335 | list nList = []; |
||
336 | |||
337 | default { |
||
338 | state_entry() { |
||
339 | llOwnerSay("[Eval] Starting module..."); |
||
340 | llSetTimerEvent(10); |
||
341 | } |
||
342 | link_message(integer sender, integer num, string message, key id) { |
||
343 | if(id != "configuration") return; |
||
344 | llOwnerSay("[Eval] Got configuration..."); |
||
345 | configuration = message; |
||
346 | state listen_group; |
||
347 | } |
||
348 | timer() { |
||
349 | llOwnerSay("[Eval] Requesting configuration..."); |
||
350 | llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY); |
||
351 | } |
||
352 | on_rez(integer num) { |
||
353 | llResetScript(); |
||
354 | } |
||
355 | changed(integer change) { |
||
42 | office | 356 | if((change & CHANGED_INVENTORY) || |
357 | (change & CHANGED_REGION_START) || |
||
8 | office | 358 | (change & CHANGED_OWNER)) { |
359 | llResetScript(); |
||
360 | } |
||
361 | } |
||
362 | state_exit() { |
||
363 | llSetTimerEvent(0); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | state listen_group { |
||
368 | state_entry() { |
||
369 | // DEBUG |
||
370 | llOwnerSay("[Eval] Waiting for group messages..."); |
||
371 | } |
||
372 | link_message(integer sender, integer num, string message, key id) { |
||
373 | // We only care about notifications now. |
||
374 | if(id != "notification") |
||
375 | return; |
||
42 | office | 376 | |
8 | office | 377 | // This script only processes group notifications. |
42 | office | 378 | if(wasKeyValueGet("type", message) != "group" || |
379 | (wasKeyValueGet("type", message) == "group" && |
||
380 | wasURLUnescape(wasKeyValueGet("group", message)) != |
||
381 | wasKeyValueGet("group", configuration))) |
||
8 | office | 382 | return; |
42 | office | 383 | |
8 | office | 384 | // Get the sent message. |
385 | data = wasURLUnescape( |
||
386 | wasKeyValueGet( |
||
42 | office | 387 | "message", |
8 | office | 388 | message |
389 | ) |
||
390 | ); |
||
42 | office | 391 | |
11 | office | 392 | // Check if this is an eggdrop command. |
42 | office | 393 | if(llGetSubString(data, 0, 0) != |
8 | office | 394 | wasKeyValueGet("command", configuration)) |
395 | return; |
||
42 | office | 396 | |
11 | office | 397 | // Check if the command matches the current module. |
15 | office | 398 | list command = llParseString2List(data, [" "], []); |
42 | office | 399 | if(llList2String(command, 0) != |
15 | office | 400 | wasKeyValueGet("command", configuration) + "eval") |
8 | office | 401 | return; |
42 | office | 402 | |
8 | office | 403 | // Remove command. |
404 | command = llDeleteSubList(command, 0, 0); |
||
42 | office | 405 | |
8 | office | 406 | // Dump the rest of the message. |
407 | data = (string)wasPostfixEval( |
||
408 | wasInfixToPostfix( |
||
409 | wasInfixTokenize( |
||
410 | llDumpList2String( |
||
411 | command, |
||
412 | " " |
||
413 | ) |
||
414 | ) |
||
415 | ) |
||
416 | ); |
||
42 | office | 417 | |
8 | office | 418 | state tell; |
419 | } |
||
420 | on_rez(integer num) { |
||
421 | llResetScript(); |
||
422 | } |
||
423 | changed(integer change) { |
||
42 | office | 424 | if((change & CHANGED_INVENTORY) || |
425 | (change & CHANGED_REGION_START) || |
||
8 | office | 426 | (change & CHANGED_OWNER)) { |
427 | llResetScript(); |
||
428 | } |
||
429 | } |
||
430 | } |
||
431 | |||
432 | state tell { |
||
433 | state_entry() { |
||
434 | // DEBUG |
||
435 | llOwnerSay("[Eval] Sending to group."); |
||
436 | llInstantMessage( |
||
437 | wasKeyValueGet( |
||
42 | office | 438 | "corrade", |
8 | office | 439 | configuration |
42 | office | 440 | ), |
8 | office | 441 | wasKeyValueEncode( |
442 | [ |
||
443 | "command", "tell", |
||
444 | "group", wasURLEscape( |
||
445 | wasKeyValueGet( |
||
42 | office | 446 | "group", |
8 | office | 447 | configuration |
448 | ) |
||
449 | ), |
||
450 | "password", wasURLEscape( |
||
451 | wasKeyValueGet( |
||
42 | office | 452 | "password", |
8 | office | 453 | configuration |
454 | ) |
||
455 | ), |
||
456 | "entity", "group", |
||
457 | "message", wasURLEscape(data) |
||
458 | ] |
||
459 | ) |
||
460 | ); |
||
461 | state listen_group; |
||
462 | } |
||
463 | } |