corrade-lsl-templates – Blame information for rev 15

Subversion Repositories:
Rev:
Rev Author Line No. Line
8 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A module that evaluates a mathematical expression for Corrade Eggdrop.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
10 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
11 ///////////////////////////////////////////////////////////////////////////
12 string wasKeyValueGet(string k, string data) {
13 if(llStringLength(data) == 0) return "";
14 if(llStringLength(k) == 0) return "";
15 list a = llParseString2List(data, ["&", "="], []);
16 integer i = llListFindList(a, [ k ]);
17 if(i != -1) return llList2String(a, i+1);
18 return "";
19 }
20  
21 ///////////////////////////////////////////////////////////////////////////
22 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
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 ///////////////////////////////////////////////////////////////////////////
37 // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 //
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 ///////////////////////////////////////////////////////////////////////////
49 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
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 ///////////////////////////////////////////////////////////////////////////
73 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
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 ///////////////////////////////////////////////////////////////////////////
112 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
113 ///////////////////////////////////////////////////////////////////////////
114 string wasListToCSV(list l) {
115 list v = [];
116 do {
117 string a = llDumpList2String(
118 llParseStringKeepNulls(
119 llList2String(
120 l,
121  
122 ),
123 ["\""],
124 []
125 ),
126 "\"\""
127 );
128 if(llParseStringKeepNulls(
129 a,
130 [" ", ",", "\n", "\""], []
131 ) !=
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 ///////////////////////////////////////////////////////////////////////////
141 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
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(
150 i,
151 ["+"],
152 []
153 ),
154 " "
155 ),
156 ["%0D%0A"],
157 []
158 ),
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 }
171  
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 }
188  
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 }
197  
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);
238 return result;
239 }
240  
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 // callback URL
330 string URL = "";
331 // store message over state.
332 string data = "";
333  
334 // Notecard reading.
335 key nQuery = NULL_KEY;
336 integer nLine = 0;
337 list nList = [];
338  
339 default {
340 state_entry() {
341 llOwnerSay("[Eval] Starting module...");
342 llSetTimerEvent(10);
343 }
344 link_message(integer sender, integer num, string message, key id) {
345 if(id != "configuration") return;
346 llOwnerSay("[Eval] Got configuration...");
347 configuration = message;
348 state listen_group;
349 }
350 timer() {
351 llOwnerSay("[Eval] Requesting configuration...");
352 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
353 }
354 on_rez(integer num) {
355 llResetScript();
356 }
357 changed(integer change) {
358 if((change & CHANGED_INVENTORY) ||
359 (change & CHANGED_REGION_START) ||
360 (change & CHANGED_OWNER)) {
361 llResetScript();
362 }
363 }
364 state_exit() {
365 llSetTimerEvent(0);
366 }
367 }
368  
369 state listen_group {
370 state_entry() {
371 // DEBUG
372 llOwnerSay("[Eval] Waiting for group messages...");
373 }
374 link_message(integer sender, integer num, string message, key id) {
375 // We only care about notifications now.
376 if(id != "notification")
377 return;
378  
379 // This script only processes group notifications.
380 if(wasKeyValueGet("type", message) != "group")
381 return;
382  
383 // Get the sent message.
384 data = wasURLUnescape(
385 wasKeyValueGet(
386 "message",
387 message
388 )
389 );
390  
11 office 391 // Check if this is an eggdrop command.
8 office 392 if(llGetSubString(data, 0, 0) !=
393 wasKeyValueGet("command", configuration))
394 return;
11 office 395  
396 // Check if the command matches the current module.
15 office 397 list command = llParseString2List(data, [" "], []);
398 if(llList2String(command, 0) !=
399 wasKeyValueGet("command", configuration) + "eval")
8 office 400 return;
15 office 401  
8 office 402 // Remove command.
403 command = llDeleteSubList(command, 0, 0);
404  
405 // Dump the rest of the message.
406 data = (string)wasPostfixEval(
407 wasInfixToPostfix(
408 wasInfixTokenize(
409 llDumpList2String(
410 command,
411 " "
412 )
413 )
414 )
415 );
416  
417 state tell;
418 }
419 on_rez(integer num) {
420 llResetScript();
421 }
422 changed(integer change) {
423 if((change & CHANGED_INVENTORY) ||
424 (change & CHANGED_REGION_START) ||
425 (change & CHANGED_OWNER)) {
426 llResetScript();
427 }
428 }
429 }
430  
431 state tell {
432 state_entry() {
433 // DEBUG
434 llOwnerSay("[Eval] Sending to group.");
435 llInstantMessage(
436 wasKeyValueGet(
437 "corrade",
438 configuration
439 ),
440 wasKeyValueEncode(
441 [
442 "command", "tell",
443 "group", wasURLEscape(
444 wasKeyValueGet(
445 "group",
446 configuration
447 )
448 ),
449 "password", wasURLEscape(
450 wasKeyValueGet(
451 "password",
452 configuration
453 )
454 ),
455 "entity", "group",
456 "message", wasURLEscape(data)
457 ]
458 )
459 );
460 state listen_group;
461 }
462 }