CODE
// Two Truths and a Lie Control Script
// Bob Brightwillow - revision 1 2004-04-07
list POSSIBLERESPONSES = [ "1", "2", "3" ];
string correct;
list responses;
list scores;
integer COMMAND_CHANNEL;
integer inList(string tofind, list selections)
{
integer i;
for (i = 0; i < llGetListLength(selections); i++)
{
if (tofind == llList2String(selections, i))
{
return TRUE;
}
}
return FALSE;
}
list UpdateResponses(list responses, string name, string msg)
{
integer i;
for (i = 0; i < llGetListLength(responses); i += 2)
{
if (llList2String(responses, i) == name)
{
list prevList = [ ];
list nextList = [ ];
if (i != 0)
prevList = llList2List(responses, 0, i - 1);
if (i + 2 < llGetListLength(responses))
nextList = llList2List(responses, i + 2, -1);
return prevList + [ name, msg ] + nextList;
}
}
return responses + [ name, msg ];
}
list AddPoint(list scores, string name)
{
integer i;
for (i = 0; i < llGetListLength(scores); i += 2)
{
if (llList2String(scores, i) == name)
{
list prevList = [ ];
list nextList = [ ];
if (i != 0)
prevList = llList2List(scores, 0, i - 1);
if (i + 2 < llGetListLength(scores))
nextList = llList2List(scores, i + 2, -1);
integer score = llList2Integer(scores, i + 1);
return prevList + [ name, score + 1 ] + nextList;
}
}
return scores + [ name, 1 ];
}
default
{
state_entry()
{
COMMAND_CHANNEL = llFloor(llFrand(2147483645)) + 1;
llInstantMessage(llGetOwner(), "Script has been reset. "
+ "Please watch the dialog boxes for instructions to follow.");
llInstantMessage(llGetOwner(), "Tip: If you lose the dialog box, "
+ "just type '/" + (string)COMMAND_CHANNEL + " selection' where "
+ "selection is the button you wish you had clicked. (To avoid "
+ "embarassment and ruining the event, you'll probably want to "
+ "copy that number onto the clipboard.)");
state waiting;
}
}
// Waiting for Game Host to announce next liar
state waiting
{
state_entry()
{
llDialog(llGetOwner(), "Ready for the next liar. Click 'Start' to "
+ "start recording responses, or 'End game' to stop playing.",
[ "Start", "End game" ], COMMAND_CHANNEL);
llListen(COMMAND_CHANNEL, "", llGetOwner(), "Start");
llListen(COMMAND_CHANNEL, "", llGetOwner(), "End game");
}
listen(integer channel, string name, key id, string msg)
{
if (msg == "End game")
{
state endgame;
}
else
{
state collecting;
}
}
on_rez(integer n)
{
llResetScript();
}
}
// Players are making selections
state collecting
{
state_entry()
{
correct = "";
responses = [ ];
llListen(0, "", "", "");
llListen(COMMAND_CHANNEL, "", llGetOwner(), "");
llDialog(llGetOwner(), "Now collecting responses. Please select the "
+ "correct answer to stop collecting responses.", POSSIBLERESPONSES,
COMMAND_CHANNEL);
}
listen(integer channel, string name, key id, string msg)
{
if (channel == COMMAND_CHANNEL && id == llGetOwner())
{
if (inList(msg, POSSIBLERESPONSES))
{
correct = msg;
state score;
}
return;
}
if (!inList(msg, POSSIBLERESPONSES))
{
return;
}
responses = UpdateResponses(responses, name, msg);
}
on_rez(integer n)
{
llResetScript();
}
}
state score
{
state_entry()
{
integer i;
for (i = 0; i < llGetListLength(responses); i += 2)
{
if (llList2String(responses, i + 1) == correct)
{
scores = AddPoint(scores, llList2String(responses, i));
}
}
state waiting;
}
}
state endgame
{
state_entry()
{
llSay(0, "The game is over! Thank you all for playing!");
llSay(0, "Here are the scores:");
string saytext;
integer i;
integer msgcount = 0;
for (i = 0; i < llGetListLength(scores); i += 2)
{
saytext += "[" + llList2String(scores, i) + ": ";
saytext += (string)llList2Integer(scores, i + 1) + "] ";
if (msgcount++ > 9)
{
msgcount = 0;
llSay(0, saytext);
saytext = "";
}
}
if (msgcount)
{
llSay(0, saytext);
}
llSay(0, "Congratulations to the winner!");
state dead;
}
}
state dead
{
state_entry()
{
}
on_rez(integer n)
{
llResetScript();
}
}