corrade-lsl-templates – Blame information for rev 38

Subversion Repositories:
Rev:
Rev Author Line No. Line
4 office 1 ///////////////////////////////////////////////////////////////////////////
37 office 2 // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
4 office 3 ///////////////////////////////////////////////////////////////////////////
4  
5 ///////////////////////////////////////////////////////////////////////////
38 office 6 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
4 office 7 ///////////////////////////////////////////////////////////////////////////
8 string wasKeyValueGet(string k, string data) {
9 if(llStringLength(data) == 0) return "";
10 if(llStringLength(k) == 0) return "";
38 office 11 list a = llParseStringKeepNulls(data, ["&", "="], []);
12 integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]);
13 if(i != -1) return llList2String(a, 2*i+1);
4 office 14 return "";
15 }
38 office 16  
4 office 17 ///////////////////////////////////////////////////////////////////////////
37 office 18 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
4 office 19 ///////////////////////////////////////////////////////////////////////////
20 string wasKeyValueEncode(list data) {
21 list k = llList2ListStrided(data, 0, -1, 2);
22 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
23 data = [];
24 do {
25 data += llList2String(k, 0) + "=" + llList2String(v, 0);
26 k = llDeleteSubList(k, 0, 0);
27 v = llDeleteSubList(v, 0, 0);
28 } while(llGetListLength(k) != 0);
29 return llDumpList2String(data, "&");
30 }
31  
32 ///////////////////////////////////////////////////////////////////////////
37 office 33 // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 //
4 office 34 ///////////////////////////////////////////////////////////////////////////
35 vector wasCirclePoint(float radius) {
36 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
37 float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
38 if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
39 return <x, y, 0>;
40 return wasCirclePoint(radius);
41 }
42  
43 ///////////////////////////////////////////////////////////////////////////
37 office 44 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
4 office 45 ///////////////////////////////////////////////////////////////////////////
46 integer wasIsAvatarInSensorRange(key avatar) {
47 return llListFindList(
48 llGetAgentList(
38 office 49 AGENT_LIST_REGION,
4 office 50 []
38 office 51 ),
4 office 52 (list)((key)avatar)
38 office 53 ) != -1 &&
4 office 54 llVecDist(
38 office 55 llGetPos(),
4 office 56 llList2Vector(
57 llGetObjectDetails(
38 office 58 avatar,
4 office 59 [OBJECT_POS]
38 office 60 ),
4 office 61  
62 )
63 ) <= 96;
64 }
65  
66 ///////////////////////////////////////////////////////////////////////////
37 office 67 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
4 office 68 ///////////////////////////////////////////////////////////////////////////
69 // escapes a string in conformance with RFC1738
70 string wasURLEscape(string i) {
71 string o = "";
72 do {
73 string c = llGetSubString(i, 0, 0);
74 i = llDeleteSubString(i, 0, 0);
75 if(c == "") jump continue;
76 if(c == " ") {
77 o += "+";
78 jump continue;
79 }
80 if(c == "\n") {
81 o += "%0D" + llEscapeURL(c);
82 jump continue;
83 }
84 o += llEscapeURL(c);
85 @continue;
86 } while(i != "");
87 return o;
88 }
89  
90 // corrade data
91 string CORRADE = "";
92 string GROUP = "";
93 string PASSWORD = "";
38 office 94 integer RANGE = 5;
4 office 95  
96 // for holding the callback URL
97 string callback = "";
98  
99 // for notecard reading
100 integer line = 0;
38 office 101  
4 office 102 // key-value data will be read into this list
103 list tuples = [];
38 office 104  
4 office 105 default {
106 state_entry() {
107 // set color for button
108 llSetColor(<1,1,0>, ALL_SIDES);
109 if(llGetInventoryType("configuration") != INVENTORY_NOTECARD) {
110 llOwnerSay("Sorry, could not find an inventory notecard.");
111 return;
112 }
113 // DEBUG
114 llOwnerSay("Reading configuration file...");
115 llGetNotecardLine("configuration", line);
116 }
117 dataserver(key id, string data) {
118 if(data == EOF) {
119 // invariant, length(tuples) % 2 == 0
120 if(llGetListLength(tuples) % 2 != 0) {
121 llOwnerSay("Error in configuration notecard.");
122 return;
123 }
124 CORRADE = llList2String(
125 tuples,
126 llListFindList(
38 office 127 tuples,
4 office 128 [
129 "corrade"
130 ]
131 )
132 +1);
133 if(CORRADE == "") {
134 llOwnerSay("Error in configuration notecard: corrade");
135 return;
136 }
137 GROUP = llList2String(
138 tuples,
139 llListFindList(
38 office 140 tuples,
4 office 141 [
142 "group"
143 ]
144 )
145 +1);
146 if(GROUP == "") {
147 llOwnerSay("Error in configuration notecard: password");
148 return;
149 }
150 PASSWORD = llList2String(
151 tuples,
152 llListFindList(
38 office 153 tuples,
4 office 154 [
155 "password"
156 ]
157 )
158 +1);
159 if(GROUP == "") {
160 llOwnerSay("Error in configuration notecard: group");
161 return;
162 }
38 office 163 RANGE = llList2Integer(
4 office 164 tuples,
165 llListFindList(
38 office 166 tuples,
4 office 167 [
168 "range"
169 ]
170 )
171 +1);
38 office 172 if(RANGE == 0) {
4 office 173 llOwnerSay("Error in configuration notecard: range");
174 return;
175 }
176 // DEBUG
177 llOwnerSay("Read configuration file...");
178 state url;
179 }
180 if(data == "") jump continue;
181 integer i = llSubStringIndex(data, "#");
182 if(i != -1) data = llDeleteSubString(data, i, -1);
183 list o = llParseString2List(data, ["="], []);
184 // get rid of starting and ending quotes
185 string k = llDumpList2String(
186 llParseString2List(
187 llStringTrim(
188 llList2String(
38 office 189 o,
4 office 190  
38 office 191 ),
192 STRING_TRIM),
4 office 193 ["\""], []
194 ), "\"");
195 string v = llDumpList2String(
196 llParseString2List(
197 llStringTrim(
198 llList2String(
38 office 199 o,
4 office 200 1
38 office 201 ),
202 STRING_TRIM),
4 office 203 ["\""], []
204 ), "\"");
205 if(k == "" || v == "") jump continue;
206 tuples += k;
207 tuples += v;
208 @continue;
209 llGetNotecardLine("configuration", ++line);
210 }
211 on_rez(integer num) {
212 llResetScript();
213 }
214 changed(integer change) {
215 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
216 llResetScript();
217 }
218 }
219 }
38 office 220  
4 office 221 state url {
222 state_entry() {
223 // DEBUG
224 llOwnerSay("Requesting URL...");
225 llRequestURL();
226 }
227 http_request(key id, string method, string body) {
228 if(method != URL_REQUEST_GRANTED) return;
229 callback = body;
230 // DEBUG
231 llOwnerSay("Got URL...");
232 state off;
233 }
234 on_rez(integer num) {
235 llResetScript();
236 }
237 changed(integer change) {
238 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
239 llResetScript();
240 }
241 }
242 }
243  
244 state off {
245 state_entry() {
246 // set color for button
247 llSetColor(<1,0,0>, ALL_SIDES);
248 }
249 touch_start(integer num) {
250 state on;
251 }
252 on_rez(integer num) {
253 llResetScript();
254 }
255 changed(integer change) {
256 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
257 llResetScript();
258 }
259 }
260 }
38 office 261  
4 office 262 state on {
263 state_entry() {
264 // set color for button
265 llSetColor(<0,1,0>, ALL_SIDES);
266 // if Corrade is in-range then just follow
38 office 267 if(wasIsAvatarInSensorRange(CORRADE)) {
268 state follow;
269 }
4 office 270 // DEBUG
271 llOwnerSay("Detecting if Corrade is online...");
272 llSetTimerEvent(5);
273 }
274 timer() {
38 office 275 llRequestAgentData(CORRADE, DATA_ONLINE);
4 office 276 }
277 dataserver(key id, string data) {
278 if(data != "1") {
279 // DEBUG
280 llOwnerSay("Corrade is not online, sleeping...");
281 llSetTimerEvent(30);
282 return;
283 }
38 office 284 llSensorRepeat("", CORRADE, AGENT, RANGE, TWO_PI, 5);
4 office 285 }
286 no_sensor() {
287 // DEBUG
288 llOwnerSay("Teleporting Corrade...");
38 office 289 llInstantMessage(CORRADE,
4 office 290 wasKeyValueEncode(
291 [
292 "command", "teleport",
293 "group", wasURLEscape(GROUP),
294 "password", wasURLEscape(PASSWORD),
295 "entity", "region",
296 "region", wasURLEscape(llGetRegionName()),
297 "position", llGetPos() + wasCirclePoint((integer)RANGE),
298 "callback", callback
299 ]
300 )
301 );
302 }
303 sensor(integer num) {
304 state follow;
305 }
306 http_request(key id, string method, string body) {
307 llHTTPResponse(id, 200, "OK");
38 office 308 if(wasKeyValueGet("command", body) == "teleport") {
309 integer success = wasKeyValueGet("success", body) == "True";
310 if(success) {
311 // DEBUG
312 llOwnerSay("Teleport succeeded...");
313 state follow;
314 }
4 office 315 // DEBUG
316 llOwnerSay("Teleport failed...");
317 return;
318 }
319 }
320 on_rez(integer num) {
321 llResetScript();
322 }
323 changed(integer change) {
324 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
325 llResetScript();
326 }
327 }
38 office 328 state_exit() {
329 llSetTimerEvent(0);
330 }
4 office 331 }
38 office 332  
4 office 333 state follow {
334 state_entry() {
335 // DEBUG
336 llOwnerSay("In follow state...");
337 // check every second whether Corrade is online
338 llRequestAgentData(CORRADE, DATA_ONLINE);
339 }
340 touch_start(integer num) {
341 state off;
342 }
343 dataserver(key id, string data) {
344 // if Corrade is not online
345 if(data != "1") state on;
346 // Corrade is online, so attempt to dectect
38 office 347 llSensorRepeat("", CORRADE, AGENT, RANGE, TWO_PI, 1);
4 office 348 }
349 no_sensor() {
350 // check if Corrade is in range, and if not, start detecting
38 office 351 if(!wasIsAvatarInSensorRange(CORRADE)) {
352 state on;
353 }
4 office 354 // Corrade is in sensor range, so execute move.
38 office 355 llInstantMessage(CORRADE,
4 office 356 wasKeyValueEncode(
357 [
37 office 358 "command", "walkto",
4 office 359 "group", wasURLEscape(GROUP),
360 "password", wasURLEscape(PASSWORD),
361 // move in a radius around the current primitive.
362 "position", llGetPos() + wasCirclePoint((integer)RANGE),
37 office 363 "vicinity", 1,
364 "timeout", 2500
4 office 365 ]
366 )
367 );
368 llSensorRepeat("", CORRADE, AGENT, (integer)RANGE, TWO_PI, 1);
369 llRequestAgentData(CORRADE, DATA_ONLINE);
370 }
371 on_rez(integer num) {
372 llResetScript();
373 }
374 changed(integer change) {
375 if((change & CHANGED_INVENTORY) || (change & CHANGED_REGION_START)) {
376 llResetScript();
377 }
378 }
38 office 379 }