sloodle_setup_notecard.lslp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle configuration notecard reader
  4. // Reads a configuration notecard and transmits the data via link messages to other scripts
  5. // If the notecard changes, then it automatically resets.
  6. //
  7. // Part of the Sloodle project (www.sloodle.org)
  8. // Copyright (c) 2007-8 Sloodle
  9. // Released under the GNU GPL v3
  10. //
  11. // Contributors:
  12. // Edmund Edgar
  13. // Peter R. Bloomfield
  14. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  15. string SLOODLE_CONFIG_NOTECARD = "sloodle_config";
  16. string SLOODLE_EOF = "sloodleeof";
  17. key sloodle_notecard_key = NULL_KEY;
  18. integer sloodle_notecard_line = 0;
  19. string COMMENT_PREFIX = "//";
  20. key latestnotecard = NULL_KEY; // The most recently read notecard
  21. ///// TRANSLATION /////
  22. // Link message channels
  23. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  24. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  25. // Translation output methods
  26. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  27. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  28. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  29. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  30. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  31. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  32. string SLOODLE_TRANSLATE_DIALOG = "dialog"; // Recipient avatar should be identified in link message keyval. At least 2 output parameters: first the channel number for the dialog, and then 1 to 12 button label strings.
  33. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter, containing the URL.
  34. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  35. // Send a translation request link message
  36. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  37. {
  38. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  39. }
  40. ///// ----------- /////
  41. ///// FUNCTIONS /////
  42. sloodle_tell_other_scripts(string msg)
  43. {
  44. sloodle_debug("notecard sending message to other scripts: "+msg);
  45. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_OBJECT_DIALOG, msg, NULL_KEY);
  46. }
  47. sloodle_debug(string msg)
  48. {
  49. //llWhisper(0,msg);
  50. }
  51. sloodle_handle_command(string str)
  52. {
  53. list bits = llParseString2List(str,["|"],[]);
  54. integer numbits = llGetListLength(bits);
  55. string name = llList2String(bits,0);
  56. string value = "";
  57. if (numbits >= 2) llList2String(bits,1);
  58. // Check the command
  59. if (name == "do:reset") {
  60. // Reset
  61. sloodle_debug("Resetting configuration notecard reader");
  62. llResetScript();
  63. } else if (name == "do:requestconfig") {
  64. // Configuration request
  65. sloodle_start_reading_notecard();
  66. }
  67. }
  68. sloodle_start_reading_notecard()
  69. {
  70. // Do we have a configuratio notecard?
  71. if (llGetInventoryType(SLOODLE_CONFIG_NOTECARD) == INVENTORY_NOTECARD) {
  72. // Start reading it
  73. sloodle_translation_request(SLOODLE_TRANSLATE_HOVER_TEXT, [<0.0,1.0,0.0>, 1.0], "readingconfignotecard", [], NULL_KEY, "");
  74. sloodle_debug("starting reading notecard");
  75. sloodle_notecard_line = 0;
  76. sloodle_notecard_key = llGetNotecardLine("sloodle_config", 0); // read the first line. The dataserver event will get the next one.
  77. latestnotecard = llGetInventoryKey(SLOODLE_CONFIG_NOTECARD);
  78. } else {
  79. sloodle_debug("No notecard called "+SLOODLE_CONFIG_NOTECARD+" found - skipping notecard configuration");
  80. latestnotecard = NULL_KEY;
  81. }
  82. }
  83. default
  84. {
  85. on_rez(integer start_param)
  86. {
  87. llResetScript();
  88. }
  89. state_entry()
  90. {
  91. // Pause for a moment, in case all scripts were reset at the same time
  92. llSleep(0.2);
  93. // Go!
  94. sloodle_start_reading_notecard();
  95. }
  96. dataserver(key requested, string data)
  97. {
  98. if ( requested == sloodle_notecard_key ) // make sure we are getting the data we want
  99. {
  100. sloodle_notecard_key = NULL_KEY;
  101. if ( data != EOF )
  102. {
  103. // If this is a comment line, then do not forward it
  104. string trimmeddata = llStringTrim(data, STRING_TRIM_HEAD);
  105. if (llSubStringIndex(trimmeddata, COMMENT_PREFIX) != 0) sloodle_tell_other_scripts(data);
  106. // Advance to the next line
  107. sloodle_notecard_line++;
  108. sloodle_notecard_key = llGetNotecardLine("sloodle_config",sloodle_notecard_line);
  109. } else {
  110. // This is the end of the configuration data
  111. llSleep(0.2);
  112. sloodle_tell_other_scripts(SLOODLE_EOF);
  113. llSetText("", <0.0,0.0,0.0>, 0.0);
  114. }
  115. }
  116. }
  117. link_message(integer sender_num, integer num, string str, key id) {
  118. // Ignore debug messages
  119. if (num == DEBUG_CHANNEL) return;
  120. // Is this an object dialog message?
  121. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  122. sloodle_handle_command(str);
  123. }
  124. }
  125. changed(integer change) {
  126. // If the inventory is changed, and we have a Sloodle config notecard, then use it to re-initialise
  127. if (change & CHANGED_INVENTORY && llGetInventoryType(SLOODLE_CONFIG_NOTECARD) == INVENTORY_NOTECARD) {
  128. // If the current notecard is not the same as the one we read most recently, then reset
  129. if (llGetInventoryKey(SLOODLE_CONFIG_NOTECARD) != latestnotecard) llResetScript();
  130. }
  131. }
  132. }
  133. // Please leave the following line intact to show where the script lives in Git:
  134. // SLOODLE LSL Script Git Location: assets/sloodle_setup_notecard.lslp