这是 LSL 原型实现(公共领域,尽管您可能需要对其进行调整以供您使用):
// user configuration parameters
integer CHANNEL = -635;
// ------------------------------------------------
// scripter configuration parameters (in seconds)
float POLL_PERIOD = 60.0; // 1 minute
// minimum length of suspend period
float CORE_SUSPEND_PERIOD = 15.0; // 15 seconds
// maximum length added to core suspend period (minimum is 0)
float MAX_RANDOM_SUSPEND_PERIOD = 30.0; // 30 seconds
float LOCK_PERIOD = 5.0; // 5 seconds
// variables
integer lock = FALSE;
// mock poll method, assumes there are always tasks
integer poll()
{
llSay(0, "Polling for tasks...");
return TRUE;
}
// mock work method
work()
{
llSay(0, "*** Executing task... ***");
}
default
{
state_entry()
{
llSay(0, "Entering default state.");
lock = FALSE;
llListen(CHANNEL, "", NULL_KEY, "");
llSetTimerEvent(POLL_PERIOD);
}
timer()
{
if (lock)
{
// step 4 - do some work
work();
// step 5 - make everybody go into suspend state
// to make sure run times are randomized AND not sooner
// than POLL_PERIOD
llRegionSay(CHANNEL, "suspend");
lock = FALSE;
state suspended;
}
else
{
if (poll())
{
// step 1 - acquire lock
llRegionSay(CHANNEL, "lock");
lock = TRUE;
// step 2 - wait and listen for others
llSetTimerEvent(LOCK_PERIOD);
}
}
}
listen(integer channel, string name, key id, string message)
{
// step 3 - did someone reply?
if (message == "lock" && lock)
{
// other script woke up at the same time - signal
// that you're here and suspend until next round,
// where there will hopefully be a winner
llSay(0, "Collision!");
llRegionSay(CHANNEL, "lock");
state suspended;
}
else if (message == "suspend")
state suspended;
}
}
state suspended
{
state_entry()
{
// this gives random number between 0 and MAX_RANDOM_SUSPEND_PERIOD
float random = llFrand(MAX_RANDOM_SUSPEND_PERIOD);
float total = CORE_SUSPEND_PERIOD + random;
llSetTimerEvent(total);
llSay(0, "Entering suspended state for " + (string) ((integer) total)
+ " seconds.");
}
timer()
{
state default;
}
}