Multi Pose Stand

Written by Kitsune
//Multipose Stand --  Rolig Loon -- 7/09/09 -- Updated March 2011

// Place this script and your animations in a prim pose stand.
// Touch pose stand to select the next animation in inventory or
// type chat commands in channel 47.
// Type /47 HELP to see a list of chat commands.

integer ThisAnim = 0;
integer NumAnims = 0;
string CurrAnim = "turn_180";
string HoldName;
key agent;

default
{
	on_rez(integer start_param)
	{
		llResetScript();
	}

	state_entry()
	{
		HoldName = llGetObjectName();
		llSetAlpha(1.0,ALL_SIDES);
		llSetObjectDesc("Chat commands on channel 47");
		NumAnims = llGetInventoryNumber(INVENTORY_ANIMATION);
		llListen(47,"","","");
		llSetSitText( "Stand" );
		llSitTarget( <0.00, 0.00, 0.80>, ZERO_ROTATION );
	}

	listen(integer channel, string thing, key id, string message)
	{
		if(id == agent)
		{
			if (llToUpper(message) == "ON")  // Make the stand visible
			{
				llSetAlpha(1.0,ALL_SIDES);
			}
			else if (llToUpper(message) == "OFF")  //  Make ths stand invisible
			{
				llSetAlpha(0.0,ALL_SIDES);
			}
			else if (llToUpper(message) == "LIST")  // LIST all available animations by number
			{
				integer j;
				for (j=0;j<= NumAnims-1;j=j+5)
				{
					llInstantMessage(id,"/me " + (string)(j+1) + " = " + llGetInventoryName(INVENTORY_ANIMATION,j) +" \n"
						+  (string)(j+2) + " = " + llGetInventoryName(INVENTORY_ANIMATION,j+1) +" \n"
						+  (string)(j+3) + " = " + llGetInventoryName(INVENTORY_ANIMATION,j+2) +" \n"
						+  (string)(j+4) + " = " + llGetInventoryName(INVENTORY_ANIMATION,j+3) +" \n"
						+  (string)(j+5) + " = " + llGetInventoryName(INVENTORY_ANIMATION,j+4));

				}
			}
			else if (llToUpper(message) == "SHOW") //SHOW the currently active animation in chat
			{
				llInstantMessage(id,"/me Current Animation " + (string)ThisAnim + " = " + CurrAnim);
			}
			else if (llToUpper(message) == "NOTE")
			{
				llGiveInventory(id,"MultiPose Instructions"); // Give instruction notecard
			}
			else if (llToUpper(message) == "HELP")
			{
				llInstantMessage(id,"/me Left click the pose stand to advance poses or use chat commands on channel 47:");
				llInstantMessage(id,"/me     LIST produces a list of available poses with key numbers.\n"
					+ "        Typing a key number advances immediately to that pose in the LIST.\n"
					+ "        SHOW displays the key number and name of the current animation.\n"
					+ "        ON makes the pose stand visible.\n"
					+ "        OFF makes the pose stand invisible.\n"
					+ "        NOTE offers instructions on a notecard. \n"
					+ "        HELP produces this message.");
			}
			else
			{
				ThisAnim = (integer)message -1; //Listen for an optional direct command to apply animation #message
				if ((integer)message > NumAnims | (integer)message <= 0)
				{
					llInstantMessage(id,"/me Please type a number between 1 and " + (string)(NumAnims));
					ThisAnim = 0;
				}
				else
				{
					llStopAnimation(CurrAnim);
					CurrAnim = llGetInventoryName(INVENTORY_ANIMATION, ThisAnim%NumAnims);
					llStartAnimation(CurrAnim);
					llInstantMessage(id,"/me " +(string)(ThisAnim+1) + " = " + CurrAnim);
					++ThisAnim;
					ThisAnim = ThisAnim%NumAnims;
				}
			}
		}
	}

	touch_start(integer num_detected)
	{
		if (llDetectedKey(0) == agent)
		{
			llStopAnimation(CurrAnim);
			if(NumAnims != 0)
			{
				CurrAnim = llGetInventoryName(INVENTORY_ANIMATION, ThisAnim%NumAnims);
				llStartAnimation(CurrAnim);
				llInstantMessage(llDetectedKey(0),"/me " + (string)(ThisAnim+1) + " = " + CurrAnim);
				++ThisAnim;
				ThisAnim = ThisAnim%NumAnims;
			}
			else
			{
				llStartAnimation("turn_180");  // The default if there are no stored animations is the SL Appearance stance
			}
		}
	}

	changed(integer change)
	{
		if (change & CHANGED_LINK)
		{
			agent = llAvatarOnSitTarget();
			if (agent)
			{
				llInstantMessage(agent,"Click the pad to start and advance animations. Type \"/47 help\" for chat commands.");
				llRequestPermissions(agent,PERMISSION_TRIGGER_ANIMATION);
			}
			else 
			{
				if ( llGetPermissions() == PERMISSION_TRIGGER_ANIMATION)
				{
					llStopAnimation(CurrAnim);
					llSetObjectName(HoldName);
				}
				llResetScript();
			}
		}
		else if (change & CHANGED_INVENTORY)
		{
			llResetScript();
		}
	}

	run_time_permissions(integer parm)
	{
		if(parm == PERMISSION_TRIGGER_ANIMATION)
		{
			llStopAnimation("sit");
			llSetAlpha(0.0,ALL_SIDES); //Make the pose stand invisible
			llSetObjectName("");
			llStartAnimation(CurrAnim);
		}
	}
}