Advertisement
thalesmiletus

Untitled

Nov 15th, 2018
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.34 KB | None | 0 0
  1. // Script: Single Channel Listener with Dialog
  2. // Author: ThalesMiletus
  3. //
  4. // Place script in a prim and rezz. Touch the prim to add your avi to the
  5. // listening list, touch again to remove. Enter the channel you want to listen
  6. // to in the dialog. The script will send whatever messages are broadcast on
  7. // the specified specified channel to all avi's in the list. Changing the
  8. // channel will change it for all avi's in the list.
  9.  
  10. integer dialog_chan = -666;  // The evil channel, avoid at all costs. :)
  11. integer listen_chan;  // listening channel set by user
  12. integer timeout = 30;  // seconds for the input dialog to timeout
  13. integer channel_is_set = FALSE;
  14. string long_name = "Channel Listener";
  15. integer dialog_listener;
  16. integer chan_listener = 0;
  17. vector text_color = <1.0,1.0,1.0>;
  18. list users = [];  // list of avis that are listening
  19.  
  20. SendMessageToAll(integer channel, string message)
  21. {
  22.     integer i=0;
  23.     integer length = llGetListLength(users);
  24.     while (i < length)
  25.     {
  26.         llRegionSayTo(llList2Key(users, i), 0,
  27.                 "(ch "+(string)channel+"): " + message);
  28.         i++;
  29.     }
  30. }
  31.    
  32. default
  33. {
  34.     state_entry()
  35.     {
  36.         llSetText(long_name, text_color, 1);
  37.     }
  38.  
  39.     touch_start(integer num_detected)
  40.     {        
  41.         key id = llDetectedKey(0);
  42.        
  43.         // If avi is already in list, remove, otherwise add.
  44.         integer idx;
  45.         idx = llListFindList(users, [id]);
  46.         if (idx == -1)
  47.         {
  48.             string msg;
  49.             users += id;
  50.             llRegionSayTo(id, 0, "Added you to "+long_name+"!");
  51.             if (channel_is_set)
  52.             {
  53.                 llRegionSayTo(id, 0, "Listening channel already set to: " +
  54.                         (string)listen_chan + ".");
  55.                 msg = "\nNOTE!! Current listening channel set to " + (string)listen_chan
  56.                       + ".\n\nYou can change the channel for everyone or select Ignore.\n"
  57.                       + "You have "+(string)timeout+"s before the dialog times out.";
  58.             }
  59.             else
  60.             {
  61.                 msg = "\nListening channel not set. Set a channel.\n\n"
  62.                       + "You have "+(string)timeout+"s before the dialog times out.";
  63.             }
  64.             llTextBox(id, msg, dialog_chan);  // Ask user what channel to set, if any.
  65.             dialog_listener = llListen(dialog_chan,"",NULL_KEY,"");
  66.             // Set a timer to close the listener in case user gives no input.
  67.             llSetTimerEvent(timeout);
  68.         }
  69.         else
  70.         {
  71.             users = llDeleteSubList(users, idx, idx);
  72.             llRegionSayTo(id, 0, "Removed you from "+long_name+" on channel " +
  73.                     (string)listen_chan + ". Bye!");
  74.         }
  75.     }
  76.    
  77.     listen(integer channel, string name, key id, string message)
  78.     {
  79.         string trim_msg = llStringTrim(message, STRING_TRIM);
  80.         if (channel == dialog_chan)
  81.         {            
  82.             llListenRemove(dialog_listener);  // clean up dialog listener
  83.             llSetTimerEvent(0);
  84.             integer new_listen_chan = (integer)trim_msg;
  85.             // Convert number back to string to confirm that a valid number was
  86.             // found and not just defaulted to 0.
  87.             integer valid_value = ((string)new_listen_chan == trim_msg);
  88.            
  89.             if (valid_value)
  90.             {
  91.                 if (channel_is_set)
  92.                 {
  93.                     llListenRemove(chan_listener);  // clean up previous listener
  94.                 }
  95.                
  96.                 chan_listener = llListen(new_listen_chan,"",NULL_KEY,"");
  97.                
  98.                 SendMessageToAll(new_listen_chan, "!NOTICE! " + llGetUsername(id) +
  99.                         " has set the listening channel to: " + (string)new_listen_chan);
  100.                 llSetText(long_name + " (chan: "+ (string)new_listen_chan +")",
  101.                         text_color, 1);
  102.    
  103.                 listen_chan = new_listen_chan;
  104.                 channel_is_set = TRUE;
  105.             }
  106.         }
  107.         else if (channel == listen_chan)
  108.         {
  109.             SendMessageToAll(channel, message);
  110.         }
  111.     }
  112.    
  113.     //timer event, result of the llSetTimerEvent
  114.     timer()
  115.     {
  116.         llListenRemove(dialog_listener);
  117.         //set timer event to 0
  118.         llSetTimerEvent(0);
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement