corrade-lsl-templates – Blame information for rev 36

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 office 1 ///////////////////////////////////////////////////////////////////////////
29 office 2 // Copyright (C) Wizardry and Steamworks 2014 - License: CC BY 2.0 //
2 office 3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // This is an automatic teleporter, and patrol script for the Corrade
6 // Second Life / OpenSim bot. You can find more details about the bot
7 // by following the URL: http://was.fm/secondlife/scripted_agents/corrade
8 //
9 // The purpose of this script is to demonstrate patroling with Corrade and
29 office 10 // you are free to use, change, and commercialize it under the CC BY 2.0
11 // license which can be found at: https://creativecommons.org/licenses/by/2.0
2 office 12 //
13 ///////////////////////////////////////////////////////////////////////////
14  
15 ///////////////////////////////////////////////////////////////////////////
29 office 16 // Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0 //
2 office 17 ///////////////////////////////////////////////////////////////////////////
18 string wasKeyValueGet(string k, string data) {
19 if(llStringLength(data) == 0) return "";
20 if(llStringLength(k) == 0) return "";
21 list a = llParseString2List(data, ["&", "="], []);
22 integer i = llListFindList(a, [ k ]);
23 if(i != -1) return llList2String(a, i+1);
24 return "";
25 }
26  
27 ///////////////////////////////////////////////////////////////////////////
29 office 28 // Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0 //
2 office 29 ///////////////////////////////////////////////////////////////////////////
30 string wasKeyValueEncode(list data) {
31 list k = llList2ListStrided(data, 0, -1, 2);
32 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
33 data = [];
34 do {
35 data += llList2String(k, 0) + "=" + llList2String(v, 0);
36 k = llDeleteSubList(k, 0, 0);
37 v = llDeleteSubList(v, 0, 0);
38 } while(llGetListLength(k) != 0);
39 return llDumpList2String(data, "&");
40 }
41  
42 ///////////////////////////////////////////////////////////////////////////
29 office 43 // Copyright (C) 2011 Wizardry and Steamworks - License: CC BY 2.0 //
2 office 44 ///////////////////////////////////////////////////////////////////////////
45 // http://was.fm/secondlife/wanderer
46 vector wasCirclePoint(float radius) {
47 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
48 float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
49 if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
50 return <x, y, 0>;
51 return wasCirclePoint(radius);
52 }
53  
54 ///////////////////////////////////////////////////////////////////////////
29 office 55 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
2 office 56 ///////////////////////////////////////////////////////////////////////////
57 // escapes a string in conformance with RFC1738
58 string wasURLEscape(string i) {
59 string o = "";
60 do {
61 string c = llGetSubString(i, 0, 0);
62 i = llDeleteSubString(i, 0, 0);
63 if(c == "") jump continue;
64 if(c == " ") {
65 o += "+";
66 jump continue;
67 }
68 if(c == "\n") {
69 o += "%0D" + llEscapeURL(c);
70 jump continue;
71 }
72 o += llEscapeURL(c);
73 @continue;
74 } while(i != "");
75 return o;
76 }
77  
78 ///////////////////////////////////////////////////////////////////////////
29 office 79 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
2 office 80 ///////////////////////////////////////////////////////////////////////////
81 list wasCSVToList(string csv) {
82 list l = [];
83 list s = [];
84 string m = "";
85 do {
86 string a = llGetSubString(csv, 0, 0);
87 csv = llDeleteSubString(csv, 0, 0);
88 if(a == ",") {
89 if(llList2String(s, -1) != "\"") {
90 l += m;
91 m = "";
92 jump continue;
93 }
94 m += a;
95 jump continue;
96 }
97 if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
98 m += a;
99 csv = llDeleteSubString(csv, 0, 0);
100 jump continue;
101 }
102 if(a == "\"") {
103 if(llList2String(s, -1) != a) {
104 s += a;
105 jump continue;
106 }
107 s = llDeleteSubList(s, -1, -1);
108 jump continue;
109 }
110 m += a;
111 @continue;
112 } while(csv != "");
113 // postcondition: length(s) = 0
114 return l + m;
115 }
116  
117 ///////////////////////////////////////////////////////////////////////////
29 office 118 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
2 office 119 ///////////////////////////////////////////////////////////////////////////
120 // unescapes a string in conformance with RFC1738
121 string wasURLUnescape(string i) {
122 return llUnescapeURL(
123 llDumpList2String(
124 llParseString2List(
125 llDumpList2String(
126 llParseString2List(
127 i,
128 ["+"],
129 []
130 ),
131 " "
132 ),
133 ["%0D%0A"],
134 []
135 ),
136 "\n"
137 )
138 );
139 }
140  
141 // configuration data
142 string configuration = "";
143 // callback URL
144 string callback = "";
145 // local Corrade position
146 vector position = ZERO_VECTOR;
147  
148 default {
149 state_entry() {
150 llSetTimerEvent(1);
151 }
152 link_message(integer sender, integer num, string message, key id) {
153 if(sender != 1 || id != "configuration") return;
154 configuration = message;
155 state off;
156 }
157 timer() {
158 llMessageLinked(LINK_ROOT, 0, "configuration", NULL_KEY);
159 }
160 attach(key id) {
161 llResetScript();
162 }
163 on_rez(integer num) {
164 llResetScript();
165 }
166 changed(integer change) {
167 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START) || (change & CHANGED_OWNER)) {
168 llResetScript();
169 }
170 }
171 state_exit() {
172 llSetTimerEvent(0);
173 }
174 }
175  
176 state off {
177 state_entry() {
178 llSetColor(<.5,0,0>, ALL_SIDES);
179 }
180 touch_end(integer num) {
181 state on;
182 }
183 attach(key id) {
184 llResetScript();
185 }
186 on_rez(integer num) {
187 llResetScript();
188 }
189 changed(integer change) {
190 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START) || (change & CHANGED_OWNER)) {
191 llResetScript();
192 }
193 }
194 }
195  
196 state on {
197 state_entry() {
198 llSetColor(<0,.5,0>, ALL_SIDES);
199 state url;
200 }
201 attach(key id) {
202 llResetScript();
203 }
204 on_rez(integer num) {
205 llResetScript();
206 }
207 changed(integer change) {
208 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START) || (change & CHANGED_OWNER)) {
209 llResetScript();
210 }
211 }
212 }
213  
214 state url {
215 state_entry() {
216 // DEBUG
217 llOwnerSay("Requesting URL...");
218 llRequestURL();
219 }
220 touch_end(integer num) {
221 llSetColor(<.5,0,0>, ALL_SIDES);
222 llResetScript();
223 }
224 http_request(key id, string method, string body) {
225 if(method != URL_REQUEST_GRANTED) return;
226 callback = body;
227 // DEBUG
228 llOwnerSay("Got URL...");
229 state find;
230 }
231 attach(key id) {
232 llResetScript();
233 }
234 on_rez(integer num) {
235 llResetScript();
236 }
237 changed(integer change) {
238 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START) || (change & CHANGED_OWNER)) {
239 llResetScript();
240 }
241 }
242 }
243  
244 state find {
245 state_entry() {
246 // DEBUG
247 llOwnerSay("Getting local position...");
248 llInstantMessage(
249 wasKeyValueGet(
250 "corrade",
251 configuration
252 ),
253 wasKeyValueEncode(
254 [
255 "command", "getselfdata",
256 "group", wasURLEscape(
257 wasKeyValueGet(
258 "group",
259 configuration
260 )
261 ),
262 "password", wasURLEscape(
263 wasKeyValueGet(
264 "password",
265 configuration
266 )
267 ),
268 "data", "SimPosition",
269 "callback", wasURLEscape(callback)
270 ]
271 )
272 );
273 }
274 touch_end(integer num) {
275 llSetColor(<.5,0,0>, ALL_SIDES);
276 llResetScript();
277 }
278 http_request(key id, string method, string body) {
279 llHTTPResponse(id, 200, "OK");
280 if(wasKeyValueGet("command", body) != "getselfdata" ||
281 wasKeyValueGet("success", body) != "True") {
282 // DEBUG
283 llOwnerSay("Unable to query self data...");
284 return;
285 }
286 position = (vector)llList2String(
287 wasCSVToList(
288 wasKeyValueGet(
289 "data",
290 wasURLUnescape(body)
291 )
292 ),
293 1
294 );
295 state wander;
296 }
297 attach(key id) {
298 llResetScript();
299 }
300 on_rez(integer num) {
301 llResetScript();
302 }
303 changed(integer change) {
304 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START) || (change & CHANGED_OWNER)) {
305 llResetScript();
306 }
307 }
308 state_exit() {
309 llSetTimerEvent(0);
310 }
311 }
312  
313 state wander {
314 state_entry() {
315 // DEBUG
316 llOwnerSay("Wandering ready...");
317 llSetTimerEvent(1 +
318 llFrand(
319 (float)wasKeyValueGet(
320 "corrade",
321 configuration
322 )
323 )
324 );
325 }
326 touch_end(integer num) {
327 llSetColor(<.5,0,0>, ALL_SIDES);
328 llResetScript();
329 }
330 timer() {
331 llRequestAgentData(
332 (key)wasKeyValueGet(
333 "corrade",
334 configuration
335 ),
336 DATA_ONLINE
337 );
338 }
339 dataserver(key id, string data) {
340 if(data != "1") {
341 // DEBUG
342 llOwnerSay("Corrade is not online, sleeping...");
343 llResetScript();
344 return;
345 }
346 // DEBUG
347 //llOwnerSay("Sending stop...");
348 llInstantMessage(
349 wasKeyValueGet(
350 "corrade",
351 configuration
352 ),
353 wasKeyValueEncode(
354 [
36 office 355 "command", "walkto",
2 office 356 "group", wasURLEscape(
357 wasKeyValueGet(
358 "group",
359 configuration
360 )
361 ),
362 "password", wasURLEscape(
363 wasKeyValueGet(
364 "password",
365 configuration
366 )
367 ),
36 office 368 "position", wasURLEscape(
369 (string)(
370 position + wasCirclePoint(
371 (float)wasURLEscape(
372 wasKeyValueGet(
373 "radius",
374 configuration
375 )
376 )
377 )
378 )
379 ),
380 "vicinity", wasCirclePoint(
381 (integer)wasURLEscape(
382 wasKeyValueGet(
383 "range",
384 configuration
385 )
386 )
387 ),
388 "duration", "2500"
2 office 389 ]
390 )
391 );
392 }
393 http_request(key id, string method, string body) {
394 llHTTPResponse(id, 200, "OK");
36 office 395 if(wasKeyValueGet("command", body) != "walkto" ||
2 office 396 wasKeyValueGet("success", body) != "True") {
397 // DEBUG
398 llOwnerSay("Could not get Corrade to stop, restarting script...");
399 llResetScript();
400 }
401 // DEBUG
402 //llOwnerSay("Sending next move...");
403 llInstantMessage(
404 wasKeyValueGet(
405 "corrade",
406 configuration
407 ),
408 wasKeyValueEncode(
409 [
36 office 410 "command", "walkto",
2 office 411 "group", wasURLEscape(
412 wasKeyValueGet(
413 "group",
414 configuration
415 )
416 ),
417 "password", wasURLEscape(
418 wasKeyValueGet(
419 "password",
420 configuration
421 )
422 ),
423 "position", wasURLEscape(
424 (string)(
425 position + wasCirclePoint(
426 (float)wasURLEscape(
427 wasKeyValueGet(
428 "radius",
429 configuration
430 )
431 )
432 )
433 )
434 ),
36 office 435 "vicinity", wasCirclePoint(
436 (integer)wasURLEscape(
437 wasKeyValueGet(
438 "range",
439 configuration
440 )
441 )
442 ),
443 "duration", "2500"
2 office 444 ]
445 )
446 );
447 llSetTimerEvent(1 +
448 llFrand(
449 (float)wasURLEscape(
450 wasKeyValueGet(
451 "wait",
452 configuration
453 )
454 )
455 )
456 );
457 }
458 attach(key id) {
459 llResetScript();
460 }
461 on_rez(integer num) {
462 llResetScript();
463 }
464 changed(integer change) {
465 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START) || (change & CHANGED_OWNER)) {
466 llResetScript();
467 }
468 }
469 }