corrade-lsl-templates

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 14  →  ?path2? @ 15
/source/eggdrop/joke.lsl
@@ -170,9 +170,20 @@
 
// Notecard reading.
key nQuery = NULL_KEY;
integer nLine = 0;
list nList = [];
string joke = "";
list q = [ ];
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
// Requires: a limit.
// Provides: true random number between [0, max) or (-max, 0].
//////////////////////////////////////////////////////////
integer wasFPSrand(integer max) {
integer r = (integer)(llGetRegionFPS() * 10000000.0) % max;
if(max > 0) return r; else return -r;
}
 
default {
state_entry() {
llOwnerSay("[Joke] Starting module...");
@@ -182,7 +193,7 @@
if(id != "configuration") return;
llOwnerSay("[Joke] Got configuration...");
configuration = message;
state read_jokes;
state count_jokes;
}
timer() {
llOwnerSay("[Joke] Requesting configuration...");
@@ -203,27 +214,33 @@
}
}
 
state read_jokes {
state count_jokes {
state_entry() {
// DEBUG
llOwnerSay("[Joke] Reading jokes...");
if(llGetInventoryType("Joke") != INVENTORY_NOTECARD) {
llOwnerSay("[Joke] Failed to find a notecard named Joke in the primitive's inventory.");
return;
}
nQuery = llGetNotecardLine("Joke", nLine);
// Build a list of dataserver queries and retrieve the number of lines.
joke = (string)llGetInventoryNumber(INVENTORY_NOTECARD);
integer i = (integer)joke - 1;
do {
string card = llGetInventoryName(INVENTORY_NOTECARD, i);
// Filter notecards containing the "jokes" configuration key value.
if(llSubStringIndex(card, wasKeyValueGet("jokes", configuration)) == -1) {
joke = (string)((integer)joke - 1);
jump continue;
}
q = llListInsertList(q, [ llGetNumberOfNotecardLines(card) ], 0);
@continue;
} while(--i >= -1);
q += NULL_KEY;
}
dataserver(key id, string data) {
if(id != nQuery) return;
if(data == EOF) {
// DEBUG
llOwnerSay("[Joke] Read jokes...");
// Find the query in the query list.
integer i = llListFindList(q, [ id ]);
if(i == -1) return;
// Replace the query by the number of notecard lines (lists are covariant).
q = llListReplaceList(q, [ (integer) data ], i, i);
joke = (string)((integer)joke - 1);
if((integer)joke == 0)
state listen_group;
}
if(data == "") jump continue;
nList += data;
@continue;
nQuery = llGetNotecardLine("Joke", ++nLine);
}
on_rez(integer num) {
llResetScript();
@@ -265,23 +282,63 @@
return;
// Check if the command matches the current module.
list command = llParseString2List(data,
[wasKeyValueGet("command", configuration), " "], ["@"]);
if(llList2String(command, 0) != "joke")
list command = llParseString2List(data, [" "], []);
if(llList2String(command, 0) !=
wasKeyValueGet("command", configuration) + "joke")
return;
// Remove command.
command = llDeleteSubList(command, 0, 0);
list range = wasCSVToList(
wasKeyValueGet("roll range", configuration)
);
state read_joke;
}
on_rez(integer num) {
llResetScript();
}
changed(integer change) {
if((change & CHANGED_INVENTORY) ||
(change & CHANGED_REGION_START) ||
(change & CHANGED_OWNER)) {
llResetScript();
}
}
}
 
state read_joke {
state_entry() {
// DEBUG
llOwnerSay("[Joke] Reading a joke...");
data = llList2String(
nList,
integer i = llGetInventoryNumber(INVENTORY_NOTECARD) - 1;
list c = [];
do {
string card = llGetInventoryName(INVENTORY_NOTECARD, i);
if(llSubStringIndex(card, wasKeyValueGet("jokes", configuration)) == -1)
jump continue;
c = llListInsertList(c, [ i ], 0);
@continue;
} while(--i >= -1);
i = (integer)llFrand(llGetListLength(c));
nQuery = llGetNotecardLine(
llGetInventoryName(
INVENTORY_NOTECARD,
llList2Integer(c, i)
),
(integer)
llFrand(
llGetListLength(nList)
(
llFrand(
llList2Integer(
q,
i
)
)
)
);
}
dataserver(key id, string data) {
if(id != nQuery) return;
joke = data;
state tell;
}
on_rez(integer num) {
@@ -321,10 +378,14 @@
)
),
"entity", "group",
"message", wasURLEscape(data)
"message", wasURLEscape(joke)
]
)
);
// GC
joke = "";
state listen_group;
}
}