Script:Wind Powered Random Sounds

From Second Life Wiki
Jump to navigation Jump to search

<lsl> /*

   Sounds are played louder and faster as wind speed increases.
  • /

integer sounds = 0; // # sounds in inventory list soundnames; // names of sounds in inventory

LoadSounds() {

   soundnames = [];
   sounds = llGetInventoryNumber( INVENTORY_SOUND );
   integer n;
   for ( n=0; n < sounds; ++n )
   {
       soundnames += llGetInventoryName( INVENTORY_SOUND, n );
   }

}

float GetWindStrength() {

   return llVecMag(llWind(ZERO_VECTOR));

}

float GetVolume( float str ) {

   return str * 0.1;

}

string GetASound() {

   return llList2String(soundnames,(integer)llFrand( sounds ));

}

integer count = 0;

default {

   state_entry()
   {
       LoadSounds();
       
       if ( sounds > 0 )
           llSetTimerEvent( 0.1 );
       else
           llOwnerSay("No sounds.");
   }
   timer()
   {
       float str = GetWindStrength();
       ++count;
       if ( count > (integer)(10.0 - str) )
       {
           llTriggerSound( GetASound(), GetVolume(str) );
           count = 0;
       }
   }
   
   changed (integer change)
   { 
       if (change & CHANGED_INVENTORY)
       {
           llResetScript();
       }
   }

} </lsl>