Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Map Map_on_a_Prim  

Map_on_a_Prim

This script uses the "Media on a Prim" f...

Category: Map
By : Honoured Resident
Created: 2013-06-14 Edited: 2013-06-14
Worlds: Second Life

the Zip file

Download all files for Map_on_a_Prim
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Map_on_a_Prim_1.lsl
1 // Map on a Prim
2 //
3 // Sim map displayed as "media on a prim"
4 //
5 // Author: Honoured Resident
6 // Date: 01-12-2012
7 //
8 // This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License (CC BY-NC 3.0).
9 // To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.
10 //
11 // Author and license headers must be left intact.
12
13 string title = "Map on a Prim"; // title
14 string version = "1.0"; // version
15
16 // Timer constants
17 //
18 // Will be assigned to variable "time" and checked in timer event
19 // Important: this only works if all timer intervals are different!
20 //
21 // Always remember to destroy or reassign the timer when:
22 // - the timed event has occurred within the allotted time (success)
23 // - the timer has expired (failure)
24
25 float cDialogTime = 300.0; // dialog timeout = 5 minutes
26 float cListenTime = 60.0; // listener timeout = 1 minute
27 float cRequestTime = 10.0; // dataserver timeout = 10 seconds
28 float cUpdateTime = 43200.0; // map update = 12 hours
29
30 // Access constants
31
32 integer cOwner = 4; // owner access bit
33 integer cGroup = 2; // group access bit
34 integer cWorld = 1; // world access bit
35
36 // Other constants
37
38 integer cDebug = FALSE; // show/hide debug messages
39 integer cPrim = LINK_ROOT; // prim for displaying media
40 integer cPrimFace = 1; // prim face for media
41 string cProxy = "http://anonymouse.org/cgi-bin/anon-www.cgi/"; // anonymouse proxy
42 string cSLURL = "http://slurl.com/get-region-name-by-coords?var=region"; // slurl.com
43 string cURL = "http://map.secondlife.com/"; // amazon S3
44
45 // Variables
46
47 float time; // timer interval for various timers (see timer constants above)
48 float vRequestTime; // variable timeout for dataserver event, ranging from 10 to 20 sec.
49 integer advanced; // disable or enable advanced features (TRUE or FALSE)
50 integer allow_touch; // disable or enable touching (TRUE or FALSE)
51 integer dlgChannel; // dialog channel
52 integer dlgHandle; // handle for dialog channel
53 integer lsn; // handle for listener
54 integer map_teleport; // teleport on touch (TRUE or FALSE)
55 integer menu_access; // access level for menu
56 integer seqnr; // sequence number for unique URLs
57 integer zoom_level; // zoom level (1-8)
58 integer zoom_level_old; // previous zoom level
59 key owner; // owner
60 key query_pos; // dataserver query, retrieves sim position
61 key query_status; // dataserver query, retrieves sim status
62 key request; // handle for HTTP request
63 key user; // key of user touching the prim
64 string full_URL; // full URL including sim offset and zoom level
65 string sim_name; // simulator name
66 string sim_name_old; // previous simulator name
67 vector map_coord; // global region coordinates returned by dataserver event
68 vector map_coord_old; // previous region coordinates returned by dataserver event
69
70 // Functions
71
72 integer accessGranted(key user, integer access)
73 {
74 integer bitmask = cWorld;
75 if(user == owner) bitmask += cOwner;
76 if(llSameGroup(user)) bitmask += cGroup;
77 return (bitmask & access);
78 }
79
80 string capWords (string name)
81 {
82 integer i;
83 string word;
84 string newname;
85 list lst = llParseString2List(name, [" "], []);
86 integer len = llGetListLength(lst);
87
88 for (i = 0; i < len; i++) {
89 word = llList2String(lst, i);
90 newname += llToUpper(llGetSubString(word, 0, 0)) + llGetSubString(word, 1, -1) + " ";
91 }
92
93 return llStringTrim(newname, STRING_TRIM_TAIL);
94 }
95
96 float setTimer(float sec)
97 {
98 llSetTimerEvent(0.0);
99 llSetTimerEvent(sec);
100 return sec;
101 }
102
103 key getSimulatorData(string name, integer data)
104 {
105 allow_touch = FALSE;
106 float pctlag = 100.0 * (1.0 - llGetRegionTimeDilation()); // try to work around time dilation
107 vRequestTime = cRequestTime + cRequestTime / 100.0 * pctlag; // (more lag = longer timeout)
108 time = setTimer(vRequestTime);
109 return llRequestSimulatorData(name, data);
110 }
111
112 setPrimImage(integer prim, integer face, string image)
113 {
114 llSetLinkMedia(prim, face, [
115 PRIM_MEDIA_CURRENT_URL, image,
116 PRIM_MEDIA_AUTO_PLAY, TRUE,
117 PRIM_MEDIA_AUTO_SCALE, FALSE,
118 PRIM_MEDIA_WIDTH_PIXELS, 256,
119 PRIM_MEDIA_HEIGHT_PIXELS, 256,
120 PRIM_MEDIA_PERMS_CONTROL, PRIM_MEDIA_PERM_NONE,
121 PRIM_MEDIA_PERMS_INTERACT, PRIM_MEDIA_PERM_NONE
122 ]);
123
124 if(zoom_level == 1) llSetLinkPrimitiveParamsFast(prim, [PRIM_DESC, sim_name + " (touch for menu)"]);
125 else llSetLinkPrimitiveParamsFast(prim, [PRIM_DESC, "Touch the map to select a region"]);
126
127 allow_touch = TRUE;
128 }
129
130 setPrimText(integer prim, integer face, string text)
131 {
132 llSetLinkMedia(prim, face, [
133 PRIM_MEDIA_CURRENT_URL, "data:text/html," + text,
134 PRIM_MEDIA_AUTO_PLAY, TRUE,
135 PRIM_MEDIA_AUTO_SCALE, FALSE,
136 PRIM_MEDIA_WIDTH_PIXELS, 256,
137 PRIM_MEDIA_HEIGHT_PIXELS, 256,
138 PRIM_MEDIA_PERMS_CONTROL, PRIM_MEDIA_PERM_NONE,
139 PRIM_MEDIA_PERMS_INTERACT, PRIM_MEDIA_PERM_NONE
140 ]);
141 }
142
143 showMap(integer prim, integer face, vector sim_coord, integer z)
144 {
145 integer x = (integer)(sim_coord.x / 256.0);
146 integer y = (integer)(sim_coord.y / 256.0);
147 x = x - (x % (integer)llPow(2.0, (float)z - 1.0));
148 y = y - (y % (integer)llPow(2.0, (float)z - 1.0));
149 map_coord_old = map_coord; // global
150 zoom_level_old = zoom_level; // global
151 map_coord = <(float)x * 256.0, (float)y * 256.0, 0.0>; // global
152 seqnr++;
153 if(seqnr == DEBUG_CHANNEL) seqnr = 1; // debug channel = max. int.
154 full_URL = cURL + "map-" + (string)z + "-" + (string)x + "-" + (string)y + "-objects.jpg?" + (string)seqnr;
155
156 if(z == 1) {
157 string proxy_URL = cProxy + cSLURL + "&grid_x=" + (string)x + "&grid_y=" + (string)y;
158 if(user) llInstantMessage(user, "Requesting map tile...");
159 if(advanced) request = llHTTPRequest(proxy_URL, [], ""); // will reset timer (dataserver timeout)
160 else query_status = getSimulatorData(sim_name, DATA_SIM_STATUS); // request sim status
161 }
162 else {
163 if(user) llInstantMessage(user, "Loading level " + (string)z + " map tile...");
164 setPrimImage(prim, face, full_URL); // will reset timer (map update)
165 }
166 }
167
168 showZoom(vector sim_coord, integer z, vector touch_pos)
169 {
170 float tiles = llPow(2.0, (float)z - 1.0);
171 integer tile_x = llCeil(tiles * touch_pos.x);
172 integer tile_y = llCeil(tiles * touch_pos.y);
173 integer x = (integer)(sim_coord.x / 256.0);
174 integer y = (integer)(sim_coord.y / 256.0);
175 x = x + tile_x - 1;
176 y = y + tile_y - 1;
177 map_coord_old = map_coord; // global
178 zoom_level_old = zoom_level; // global
179 map_coord = <(float)x * 256.0, (float)y * 256.0, 0.0>; // global
180 zoom_level = 1; // global
181 seqnr++;
182 if(seqnr == DEBUG_CHANNEL) seqnr = 1; // debug channel = max. int.
183 full_URL = cURL + "map-" + (string)z + "-" + (string)x + "-" + (string)y + "-objects.jpg?" + (string)seqnr;
184 full_URL = cURL + "map-1-" + (string)x + "-" + (string)y + "-objects.jpg";
185 string proxy_URL = cProxy + cSLURL + "&grid_x=" + (string)x + "&grid_y=" + (string)y;
186 if(user) llInstantMessage(user, "Requesting map tile...");
187 request = llHTTPRequest(proxy_URL, [], ""); // will reset timer (dataserver timeout)
188 }
189
190 teleport (integer prim, string region, vector pos)
191 {
192 map_teleport = FALSE;
193 vector dest = <pos.x * 255.0, pos.y * 255.0, 0.0>;
194 llSetLinkPrimitiveParamsFast(prim, [PRIM_DESC, region + " (touch for menu)"]);
195 llMapDestination(region, dest, dest);
196 llMapDestination(region, dest, dest);
197 }
198
199 menuDialog(key id)
200 {
201 string message;
202 list menuButtons;
203
204 if(advanced) {
205 message = "\nYou can change the map,\nteleport to the region\nor zoom out to level 2-8";
206 menuButtons = [ "6", "7", "8", "3", "4", "5", "Change map", "Teleport", "2" ];
207 }
208 else {
209 message = "\n";
210 menuButtons = [ "Change map", "Teleport", "Advanced"];
211 }
212
213 if(id == owner) menuButtons = llListInsertList(menuButtons, ["Owner", "Group", "World"], 0);
214
215 dlgChannel = (integer)(llFrand(-1000000000.0) - 1000000000.0);
216 llListenRemove(dlgHandle);
217 dlgHandle = llListen(dlgChannel, "", id, "");
218 time = setTimer(cDialogTime);
219 llDialog(id, message, menuButtons, dlgChannel);
220 }
221
222 default
223 {
225 {
226 if(llGetLinkNumber() == 0) {
227 llOwnerSay("Not a linkset, removing script");
229 return;
230 }
231 if(llGetLinkNumber() == 1) {
232 llOwnerSay("Root prim, removing script");
233 llOwnerSay("Marking map location with media texture");
234 llOwnerSay("1. Use EDIT LINKED to drop the script in any other prim (not the media prim)");
235 llOwnerSay("2. Press Ctrl+Alt+T to see transparent prim");
236 llOwnerSay("3. Rotate the media prim so the map faces the transparent prim");
237 llOwnerSay("4. Make sure the transparent prim has the same rotation");
238 llOwnerSay("5. Shape both prims into flat panels and place the transparent prim in front of the map");
239 llSetColor(<1,1,1>, cPrimFace);
241 llSetTexture(TEXTURE_MEDIA, cPrimFace);
243 return;
244 }
246 llSetLinkColor(cPrim, <1,1,1>, cPrimFace);
247 llSetLinkPrimitiveParamsFast(cPrim, [PRIM_FULLBRIGHT, cPrimFace, TRUE]);
248 llSetLinkPrimitiveParamsFast(cPrim, [PRIM_DESC, "(No Description)"]);
249 string html = "<body bgcolor=lightblue><center><p> </p><h2>Welcome to<br>Map-on-a-Prim!" +
250 "<p>Loading region map...</p></h2></center></body>";
251 setPrimText(cPrim, cPrimFace, html);
252 advanced = TRUE;
253 allow_touch = TRUE;
254 menu_access = cWorld;
255 zoom_level = 1;
256 sim_name = llGetRegionName();
257 owner = llGetOwner();
258 user = owner;
260 if(owner != creator) llSay(0, llBase64ToString("RG93bmxvYWRlZCBmcm9tIHd3dy5mcmVlLWxzbC1zY3JpcHRzLmNvbQ=="));
261 llInstantMessage(user, title + " " + version + " (" + (string)llGetFreeMemory() + " bytes free)");
262 llInstantMessage(user, "Requesting simulator data...");
263 query_pos = getSimulatorData(sim_name, DATA_SIM_POS); // initial map display
264 }
265
266 on_rez(integer start_param)
267 {
269 }
270
271 http_response(key request_id, integer status, list metadata, string body)
272 {
273 integer regIndex = llSubStringIndex(body, "var region = \"");
274 integer errIndex = llSubStringIndex(body, "var region = {error: true};");
275 string region;
276
277 // expected body format: var region = "Simulator Name";
278 // expected error format: var region = {error: true};
279
280 if(regIndex > -1) {
281 region = llGetSubString(body, regIndex + 12, -1);
282 list lst = llParseString2List(region, ["\""], []);
283 sim_name = llList2String(lst, 1);
284 query_status = getSimulatorData(sim_name, DATA_SIM_STATUS); // request sim status
285 }
286 else if(errIndex > -1) {
287 map_coord = map_coord_old;
288 zoom_level = zoom_level_old;
289 if(user) llInstantMessage(user, "There is no region at this location");
290 }
291 else {
292 advanced = FALSE;
293 zoom_level = 1;
294 if(user) llInstantMessage(user, "No response from server. Disabling advanced features...");
295 if(cDebug) llOwnerSay(body);
296 query_pos = getSimulatorData(sim_name, DATA_SIM_POS);
297 }
298 }
299
300 timer()
301 {
302 if(time == cDialogTime) { // dialog timeout
303 if(cDebug) llOwnerSay("Dialog timeout");
304 llListenRemove(dlgHandle);
305 time = setTimer(cUpdateTime);
306 }
307 else if(time == cListenTime) { // listener timeout
308 if(user) llInstantMessage(user, "Timeout: no region name was entered in the last 60 seconds");
309 llListenRemove(lsn);
310 time = setTimer(cUpdateTime);
311 }
312 else if(time == vRequestTime) { // dataserver timeout (variable!)
313 if(user) llInstantMessage(user, "Region does not exist: " + sim_name);
314 sim_name = sim_name_old;
315 allow_touch = TRUE;
316 time = setTimer(cUpdateTime);
317 }
318 else if(time == cUpdateTime) { // map update
319 if(cDebug) llOwnerSay("Map update");
320 user = ""; // unattended
321 showMap(cPrim, cPrimFace, map_coord, zoom_level);
322 }
323 else {
324 if(cDebug) llOwnerSay("Timer out of range: " + (string)time);
325 allow_touch = TRUE;
326 time = setTimer(cUpdateTime);
327 }
328 }
329
330 touch_start(integer total_number)
331 {
332 if(!allow_touch) return;
333
334 user = llDetectedKey(0);
335 vector touchedpos = llDetectedTouchST(0);
336
337 if(zoom_level == 1) {
338 if(map_teleport) {
339 teleport(cPrim, sim_name, touchedpos);
340 }
341 else {
342 if(accessGranted(user, menu_access)) menuDialog(user);
343 else llInstantMessage(user, "Access denied");
344 }
345 }
346 else {
347 if(llDetectedTouchFace(0) == -1) {
348 advanced = FALSE;
349 zoom_level = 1;
350 llInstantMessage(user, "Viewer does not support touched faces. Disabling advanced features...");
351 query_pos = getSimulatorData(sim_name, DATA_SIM_POS);
352 }
353 else if(touchedpos == TOUCH_INVALID_TEXCOORD) {
354 advanced = FALSE;
355 zoom_level = 1;
356 llInstantMessage(user, "Touch position could not be determined. Disabling advanced features...");
357 query_pos = getSimulatorData(sim_name, DATA_SIM_POS);
358 }
359 else if(llDetectedTouchFace(0) != cPrimFace) {
360 llInstantMessage(user, "The map is on the front");
361 }
362 else {
363 showZoom(map_coord, zoom_level, touchedpos);
364 }
365 }
366 }
367
368 listen(integer channel, string name, key id, string msg)
369 {
370 time = setTimer(cUpdateTime);
371
372 if(channel == dlgChannel) {
373 llListenRemove(dlgHandle);
374 if(msg == "Change map") {
375 zoom_level = 1;
376 llListenRemove(lsn);
377 lsn = llListen(0, "", id, "");
378 time = setTimer(cListenTime);
379 llInstantMessage(id, "Type the name of a region within the next minute");
380 }
381 else if(msg == "Teleport") {
382 map_teleport = TRUE;
383 llSetLinkPrimitiveParamsFast(cPrim, [PRIM_DESC, "Touch for map teleport"]);
384 llInstantMessage(id, "Touch the map to display the World Map, then click \"Teleport\"");
385 }
386 else if(msg == "Advanced") {
387 advanced = TRUE;
388 menuDialog(id);
389 }
390 else if(msg == "Owner") {
391 menu_access = cOwner;
392 llInstantMessage(id, "Menu access: Owner");
393 }
394 else if(msg == "Group") {
395 if(cDebug) {
396 menu_access = cGroup;
397 llInstantMessage(id, "Menu access: Group");
398 }
399 else {
400 menu_access = cGroup + cOwner;
401 llInstantMessage(id, "Menu access: Group + Owner");
402 }
403 }
404 else if(msg == "World") {
405 menu_access = cWorld;
406 llInstantMessage(id, "Menu access: World");
407 }
408 else if(llStringLength(msg) == 1) {
409 zoom_level = (integer)msg;
410 showMap(cPrim, cPrimFace, map_coord, zoom_level);
411 }
412 }
413 else if(channel == PUBLIC_CHANNEL) {
414 if(id != user) return; // ignore other avatars
415 llListenRemove(lsn);
416 llInstantMessage(id, "Requesting simulator data...");
417 sim_name = llStringTrim(msg, STRING_TRIM);
418 if(!advanced) sim_name = capWords(sim_name); // capitalize words (e.g. "da boom" = "Da Boom")
419 query_pos = getSimulatorData(sim_name, DATA_SIM_POS);
420 }
421 }
422
423 dataserver(key request, string data)
424 {
425 time = setTimer(cUpdateTime);
426
427 if(request == query_pos) {
428 sim_name_old = sim_name;
429 map_coord = (vector)data;
430 showMap(cPrim, cPrimFace, map_coord, zoom_level);
431 }
432 else if(request == query_status) {
433 if(data == "up") {
434 if(user) llInstantMessage(user, "Loading map of " + sim_name + "...");
435 setPrimImage(cPrim, cPrimFace, full_URL);
436 }
437 else {
438 allow_touch = TRUE;
439 map_coord = map_coord_old;
440 zoom_level = zoom_level_old;
441 if(user) llInstantMessage(user, "Region " + data + ": " + sim_name);
442 }
443 }
444 }
445 }

Back to the Best Free Tools in Second Life and OpenSim.