Tip Ball

Written by Kitsune
// By Deight Boccara and in the public domain!
// latest version at http://forums.secondlife.com/showthread.php?t=74060
// don't forget to IM Deight any cool improvements!

// these variables are set by the money event so the sensor event can use them
integer m_amount;
key m_giver;

default
{
// when you start this script
state_entry()
{
// make sure you have permissions! please say "yes" to the dialog that pops up!
llRequestPermissions(llGetOwner(),PERMISSION_DEBIT );

// make sure the communist tip ball is communist. feel free to do whatever you want in this bit without fear!
llSetText("Tip Ball!", <1,0,0>, .75); // set the red "Communist Tip Ball" text
llTargetOmega(<0,0,1>,PI/2,1.0); // spin the ball; PI is the speed
llSetPrimitiveParams([PRIM_TEXTURE, 0, "182aed21-dae0-bbf4-d24e-fb342c30adcf", <4,1,0>, <0,0,0>, 3*PI/2]); // Set the texture
}

// when people pay the tip ball...
money(key giver, integer amount)
{
// make sure the sensor event can see these variables, and then...
m_amount = amount;
m_giver = giver;

// start the sensor event. 96 here determines how big--in meters--the influence of communism is
llSensor("", NULL_KEY, AGENT, 96, PI);
}

sensor(integer number)
{
// find out how much everyone gets equally, then count what's left
integer pay = m_amount / number; // (if splitting 52 dollars between 12 people, everyone gets minimum of $4)
integer overflow = m_amount - (pay * number); // (this leaves 4 dollars to give out)

// acknowledge whoever tipped the ball
llShout(0, "Thanks to " + llKey2Name(m_giver) + ", " + (string)(number) +
" people split L$" + (string)(m_amount) + "!");

// if someone paid enough money to split equally among the crowd...
if (pay > 0)
{
// first wave of payment--everyone gets what's determined in "pay" above
integer i;
for (i = 0; i < number; i++)
{
llGiveMoney(llDetectedKey(i), pay);
}

// second wave--randomly choose people to get $1 until we pay the "overflow" amount
for (i = 0; i < overflow; i++)
{
llGiveMoney(llDetectedKey((integer)(llFrand(number ))), 1);
}
}

// if someone didn't pay enough to split equally
else
{
// randomly choose people to get $1 until whatever they paid is given out
integer i;
for (i = 0; i < m_amount; i++)
{
llGiveMoney(llDetectedKey((integer)(llFrand(number ))), 1);
}
}
}

// if no one is around. I really highly doubt this will ever need to be called.
no_sensor()
{
llGiveMoney(m_giver, m_amount);
llDialog(m_giver, "Man, noone is here!", [], 42);
}
}